如何从javascript访问Emscripten类型的数组? [英] How do I access the Emscripten typed array from javascript?
本文介绍了如何从javascript访问Emscripten类型的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经使用Emscripten将C库编译为javascript代码.但是,当我尝试将其与Javascript包装器绑定时遇到问题.
I had compiled a C lib into javascript code by using Emscripten. However I ran into a problem when I try to bind it with my Javascript wrapper.
我编写此代码是为了通过引用传递它,我可以通过编译的lib访问它.
I wrote this to pass it by reference, I am able to access it via the compiled lib just fine.
var str_to_heapu8 = function (str) {
return allocate(intArrayFromString(str), 'i8', ALLOC_NORMAL);
}
但是,我在将其恢复为普通的javascript字符串时遇到了麻烦...返回值是一个空字符串.
However, I have trouble retrieving it back to normal javascript string... the return value is an empty string.
var heapu8_to_str = function (ptr, len){
var array = new Uint8Array(len);
var i = 0;
while( (ptr+i) < len){
array[i] = getValue(ptr+i, 'i8');
i++;
}
return intArrayToString(array);
}
如何将其转换回javascript字符串?e
How can I convert it back to javascript string?e
推荐答案
这对我有用:
var heapu8_to_str = function (ptr, len){
return intArrayToString(HEAPU8.subarray(ptr, ptr+len));
};
这篇关于如何从javascript访问Emscripten类型的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文