This code works in Firefox nicely - though for some reason, changing the Uint8Array into Uint32array, breaks. Do I need to coerce numbers differently in that case?
function Module(stdlib, foreign, heap) {
"use asm";
// Variable Declarations
var els = new stdlib.Uint8Array(heap);
// Function Declarations
function firstn(x) {
x = x|0 //32-bit (int)
var i=0;
for (; (i|0) < (x|0); i = (i+1)|0) {
els[i] = i;
}
}
return { firstn: firstn };
}
buf = ArrayBuffer(1024*8)
f = Module(window,{},buf).firstn;
f(5)
console.log(new Uint8Array(buf));
And one more thing - is it possible to send an arraybuffer, and have a reference to the final array it produces, with C++ emscriptem-compiled asm.js too?
It looks like asm.js only allows indexing into an Int32Array or Uint32Array using an expression of the form (foo >> 2), though I can't find any reference to this in the spec. That is, it assumes that what you have is an address and are trying to look up the integer at that address. I guess that makes sense, kinda since C code like this:
int32_t arr[5];
arr[i];
would get compiled down to machine code that does the equivalent of *((char*)arr + 4*i)
... Anyway, replacing els[i] = i
in your code with:
els[(i<<2)>>2] = i;
seems to make things work with a Uint32Array.