Domanda

I am using emscripten to generate some Javascript, and I was trying to figure out how to get function that returned a typed array. The array I am attempting to return is of static length, but I am a bit confused how a pointer based return type will be treated in JS land.

Is this sort of wrapper the way to do things: How do I access the Emscripten typed array from javascript?

Much thanks.

È stato utile?

Soluzione

emscripten works by having one big ArrayBuffer for the heap (assuming typed arrays are enabled, which they are by default).

The module emscripten compiles contains properties for both this single ArrayBuffer (Module.HEAP) and many typed ArrayBufferViews of the heap (Module.HEAPU8, Module.HEAPU32, etc.).

If you have received a pointer to the start of a contiguous array, you can grab a new view of the data it represents by doing (note, I'm using HEAP8 assuming this is perhaps a character array):

 var array = Module.HEAP8.subarray(ptr, ptr+number_of_elements);

Also worth noting, each typed view takes in arguments based on elements, and the pointers emscripten returns will represent a byte offset into the heap. So the same example for say, an array of 13 integers may be:

 var array = Module.HEAP32.subarray((ptr>>2), (ptr>>2)+13);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top