//c++ code:
EMSCRIPTEN_KEEPALIVE
int EncryptionAES(const char * strSrc, const char * gkey, const char* giv)
{
... ...
EM_ASM_({
var jsStr = Pointer_stringify($0);
jstr=jsStr;
}, *str);
return 1;
}
//js:
var jstr = [];
Module['printString'] = function(str) {
console.log(str);
};
// Module.ccall('EncryptionAES', 'number', ['string', 'string', 'string'], [str, gkey, giv]);
console.log('EncryptionAES:', Module._EncryptionAES("str", "aaab", "kkrr"));
console.log(jstr[0]);
//chrome run error message:
webassemencdec.js:884 Uncaught (in promise) ReferenceError: Pointer_stringify is not defined
at 92708 (webassemencdec.js:884:32)
at runEmAsmFunction (webassemencdec.js:1084:30)
at _emscripten_asm_const_int (webassemencdec.js:1087:14)
at webassemencdec.wasm:0x1385
at Object._EncryptionAES (webassemencdec.js:611:12)
at Module.onRuntimeInitialized (demo.html:33:40)
at doRun (webassemencdec.js:4732:71)
at run (webassemencdec.js:4749:5)
at runCaller (webassemencdec.js:4693:19)
at removeRunDependency (webassemencdec.js:547:7)
webassembly运行出错的问题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
5条回答 默认 最新
关注 引自免费微信小程序:皆我百晓生
看起来你在尝试将C++中的字符串传递到JavaScript中,但是遇到了
Pointer_stringify
未定义的错误。这是因为在Emscripten编译后的代码中,你需要使用特定的API来在C++和JavaScript之间进行数据交换。首先,确保你的Emscripten配置正确并且启用了
--bind
选项,这会提供一些有用的绑定函数,包括Pointer_stringify
。如果你是通过命令行构建的,确保添加了以下标志:emcc -s WASM=1 -semprebind -O3 your_source.cpp -o output.js
接下来,你需要修改C++代码来正确地处理字符串传递。这里是一个修正过的版本:
#include <emscripten/bind.h> using namespace emscripten; EMSCRIPTEN_BINDINGS(encrypt_module) { function("EncryptionAES", &EncryptionAES); } int EncryptionAES(const std::string& strSrc, const std::string& gkey, const std::string& giv) { // Your encryption logic here... return 1; }
然后,在JavaScript中调用这个函数:
Module['onRuntimeInitialized'] = function() { Module._EncryptionAES = Module.cwrap('EncryptionAES', 'number', ['string', 'string', 'string']); var str = "str"; var gkey = "aaab"; var giv = "kkrr"; Module._EncryptionAES(str, gkey, giv); // No need to print the result as a string since it's an int // If you still want to access the encrypted string in JavaScript, consider returning it from C++ and parsing it here. }; console.log('EncryptionAES:', Module._EncryptionAES(str, gkey, giv));
这样应该可以解决
Pointer_stringify
未定义的问题,并允许你在JavaScript中调用加密函数。注意,上述代码中,我们不再需要显式地调用Pointer_stringify
,因为emscripten-bindings
库已经为我们处理了类型转换。如果你确实需要将加密结果作为字符串返回给JavaScript,那么你应该在C++中返回一个字符串,并在JavaScript中解析它。但根据你的例子,似乎你只需要返回一个整数结果,所以无需额外操作。
解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
- ¥15 C#不用正则表达式如何全字匹配
- ¥15 怎么生成确定数目的泊松点过程
- ¥15 layui数据表格多次重载的数据覆盖问题
- ¥15 python点云生成mesh精度不够怎么办
- ¥15 QT C++ 鼠标键盘通信
- ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
- ¥50 高维数据处理方法求指导
- ¥100 数字取证课程 关于FAT文件系统的操作
- ¥15 如何使用js实现打印时每页设置统一的标题