Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript WebAssembly返回字符串使用C_Javascript_C_Webassembly - Fatal编程技术网

Javascript WebAssembly返回字符串使用C

Javascript WebAssembly返回字符串使用C,javascript,c,webassembly,Javascript,C,Webassembly,如何从WebAssembly函数返回JavaScript字符串 https://dev.to/azure/passing-strings-from-c-to-javascript-in-web-assembly-1p01 - not working C #包括 #包括 void jsPrintString(const char*s,uint16_t len); 作废打印(){ const char * STR =“你好,来自C++!” jsPrintString(str,strlen(str)

如何从WebAssembly函数返回JavaScript字符串

https://dev.to/azure/passing-strings-from-c-to-javascript-in-web-assembly-1p01 - not working
C

#包括
#包括
void jsPrintString(const char*s,uint16_t len);
作废打印(){
const char * STR =“你好,来自C++!”
jsPrintString(str,strlen(str));
}
编译器:

Javascript:

constmemory=newwebassembly.memory({initial:1});
函数handlePrintString(偏移量、长度){
console.log(偏移量、长度);
const bytes=新的Uint8Array(memory.buffer、offset、length);
常量字符串=新的文本解码器('utf8')。解码(字节);
console.log(字符串);//空???????????????????????????????????
}
常量importObject={
环境:{
jsPrintString:handlePrintString,
记忆:记忆
},
js:{mem:memory}
};
WebAssembly.InstanceStreaming(fetch('/js/test.wasm'),importObject)
.然后(obj=>{
log(obj.instance.exports);
log(obj.instance.exports.print());
});
内存阵列缓冲区[0,0,0,0,0,0,0,0,0,0,0….]

如何从WebAssembly函数返回JavaScript字符串

https://dev.to/azure/passing-strings-from-c-to-javascript-in-web-assembly-1p01 - not working
你不能。您必须将其复制到与JavaScript共享的内存中

void hello(字符*数组){
const MySype字符串=“来自C++的hello!”
memcpy(数组,my_字符串,strlen(my_字符串));
}
(异步()=>{
const response=wait fetch('/js/test.wasm');
const bytes=wait response.arrayBuffer();
const results=wait WebAssembly.instantiate(字节,{});
const{instance:{exports:{memory,hello}}}=results;
常量数组=新的Uint8Array(memory.buffer,0,15);
hello(array.byteOffset);
控制台。日志(新文本解码器(UTF8))解码(数组);/ /来自C++的问候!
})();

您是否收到任何编译错误/警告?emcc调用中导出的函数中的“\u hello”是什么?它似乎不是源代码中的函数。这看起来像是复制粘贴问题,您修改了一些示例代码,但没有更新命令。