1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
(function() {
var wasmJS = WasmJS({}); // do not use the normal Module in the current scope
// XXX don't be confused. Module here is in the outside program. wasmJS is the inner wasm-js.cpp.
// Generate a module instance of the asm.js converted into wasm.
var code;
if (typeof read === 'function') {
// spidermonkey or v8 shells
code = read(Module['asmjsCodeFile']);
} else if (typeof process === 'object' && typeof require === 'function') {
// node.js
code = require('fs')['readFileSync'](Module['asmjsCodeFile']).toString();
} else {
throw 'TODO: loading in other platforms';
}
var temp = wasmJS._malloc(code.length + 1);
wasmJS.writeAsciiToMemory(code, temp);
wasmJS._load_asm(temp);
wasmJS._free(temp);
// Generate memory XXX TODO get the right size
var theBuffer = Module['buffer'] = new ArrayBuffer(Module['providedTotalMemory'] || 64*1024*1024);
// Information for the instance of the module.
var info = wasmJS['info'] = {
global: null,
env: null,
asm2wasm: { // special asm2wasm imports
"f64-rem": function(x, y) {
return x % y;
},
},
parent: Module // Module inside wasm-js.cpp refers to wasm-js.cpp; this allows access to the outside program.
};
wasmJS['lookupImport'] = function(mod, base) {
var lookup = info;
if (mod.indexOf('.') < 0) {
lookup = (lookup || {})[mod];
} else {
var parts = mod.split('.');
lookup = (lookup || {})[parts[0]];
lookup = (lookup || {})[parts[1]];
}
lookup = (lookup || {})[base];
if (lookup === undefined) {
abort('bad lookupImport to (' + mod + ').' + base);
}
return lookup;
}
// The asm.js function, called to "link" the asm.js module.
Module['asm'] = function(global, env, buffer) {
assert(buffer === theBuffer); // we should not even need to pass it as a 3rd arg for wasm, but that's the asm.js way.
// write the provided data to a location the wasm instance can get at it.
info.global = global;
info.env = env;
wasmJS['_load_mapped_globals'](); // now that we have global and env, we can ready the provided imported globals, copying them to their mapped locations.
return wasmJS['asmExports'];
};
})();
|