【问题标题】:Using strings in Webassembly module在 Webassembly 模块中使用字符串
【发布时间】:2017-05-31 11:43:34
【问题描述】:

我试图让这个简单的场景工作,但我不明白如何从内存中获取数据:

在 hello.cpp 中:

void EMSCRIPTEN_KEEPALIVE modifyDOM(const char *text, const char *id) {
    printf("modify dom %s with %s\n", id, text);
    EM_ASM_({
        console.log($0);
        console.log($1);
        console.log(arguments);
        const elt = document.getElementById($0);
        elt.innerHTML = $1;
    }, id, text);
}

在 hello.html 中:

document.querySelector('.addDom').addEventListener('click', function(){
    Module.ccall('modifyDOM', null, ['string', 'string'], ['Some fun text', 'textDom']);
});

我得到的是 printf 输出正确的值(即用一些有趣的文本修改 dom textDom),但 console.log 输出数字如 6736 和 6672。

我猜这是因为在 Javascript 世界中,这些值就像指针一样存储在内存中。如果是这种情况,将这些值作为字符串获取的最佳方法是什么?

【问题讨论】:

标签: webassembly


【解决方案1】:

是的,这些是指针。 emscripten 生成的 Javascript 胶水文件为您提供了一个帮助函数,用于将指向以 null 结尾的 ASCII 编码字符串的指针转换为 Javascript 字符串对象:Module.AsciiToString(ptr)

还有UTF8ToStringUTF16ToStringUTF32ToString

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 2021-11-10
    • 1970-01-01
    相关资源
    最近更新 更多