The problem is with the following lines:
F2 = (sqrt(3.0)-1.0)*0.5; F3 = 1.0/3.0;
G2 = (3.0-sqrt(3.0))/6.0; G3 = 1.0/6.0;
Only declarations are allowed in the top level scope of an asm.js module. You're not allowed to define a variable in the top level scope.
According to the spec you may only assign literal values to global variables (amongst other import statements). Hence you can't do the following either because F2
, F3
, G2
and G3
are not being assigned literal values:
var floor = stdlib.Math.floor,
sqrt = stdlib.Math.sqrt,
random = new stdlib.Float32Array(heap),
buf = new stdlib.Int32Array(heap),
F2 = (sqrt(3.0)-1.0)*0.5, F3 = 1.0/3.0,
G2 = (3.0-sqrt(3.0))/6.0, G3 = 1.0/6.0;
Hence you will need to move the definition of F2
, F3
, G2
and G3
into a function (perhaps the init
function). Thus your code should look like:
function SimplexNoise(stdlib,foreign,heap) {
"use asm";
var floor = stdlib.Math.floor,
sqrt = stdlib.Math.sqrt,
random = new stdlib.Float32Array(heap),
buf = new stdlib.Int32Array(heap),
F2 = 0.0, F3 = 0.0,
G2 = 0.0, G3 = 0.0;
function init() {
F2 = (sqrt(3.0)-1.0)*0.5; F3 = 1.0/3.0;
G2 = (3.0-sqrt(3.0))/6.0; G3 = 1.0/6.0;
// rest of init
}
function noise2D(xin,yin) {
// function body
}
return {
init: init,
noise2D: noise2D
};
};
Human asm.js does tell you about this problem, but they give you the example of an array instead of a simple variable:
Note that these typed arrays cannot be modified outside of a function:
function MyModule(stdlib, foreign, heap) {
var arr = new stdlib.Int8Array(heap);
arr[0] = 1; // "asm.js must end with a return export statement"
// ...
}
Instead do something like this:
function MyModule(stdlib, foreign, heap) {
var arr = new stdlib.Int8Array(heap);
function init() {
arr[0] = 1;
}
return {
init: init
};
}
There you go. Isn't asm.js a big pain?