Skip to content

Commit 6f3209e

Browse files
committed
Fix imported memory fallback and add common stdlib imports to loader
1 parent 9ab7384 commit 6f3209e

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

lib/loader/index.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,24 @@ function getStringImpl(U32, U16, ptr) {
2323
function preInstantiate(imports) {
2424
var baseModule = {};
2525

26-
// add the internal abort function that is called when an assertion fails or an error is thrown
27-
if (!imports.env) imports.env = {};
28-
if (!imports.env.abort) imports.env.abort = function abort(mesg, file, line, colm) {
29-
var memory = baseModule.memory || imports.env.memory; // prefer exported, otherwise try imported
30-
function getString(memory, ptr) {
31-
if (!memory) return "<yet unknown>";
32-
var buffer = memory.buffer;
33-
return getStringImpl(new Uint32Array(buffer), new Uint16Array(buffer), ptr);
34-
}
26+
function getString(memory, ptr) {
27+
if (!memory) return "<yet unknown>";
28+
var buffer = memory.buffer;
29+
return getStringImpl(new Uint32Array(buffer), new Uint16Array(buffer), ptr);
30+
}
31+
32+
// add common imports used by stdlib for convenience
33+
var env = (imports.env = imports.env || {});
34+
env.abort = env.abort || function abort(mesg, file, line, colm) {
35+
var memory = baseModule.memory || env.memory; // prefer exported, otherwise try imported
3536
throw Error("abort: " + getString(memory, mesg) + " at " + getString(memory, file) + ":" + line + ":" + colm);
3637
}
38+
env.trace = env.trace || function trace(mesg, n) {
39+
var memory = baseModule.memory || env.memory;
40+
console.log("trace: " + getString(memory, mesg) + (n ? " " : "") + Array.prototype.slice.call(arguments, 2, 2 + n).join(", "));
41+
}
42+
imports.Math = imports.Math || Math;
43+
imports.Date = imports.Date || Date;
3744

3845
return baseModule;
3946
}
@@ -176,6 +183,10 @@ function postInstantiate(baseModule, instance) {
176183

177184
baseModule.getFunction = getFunction;
178185

186+
// Pull basic exports to baseModule so code in preInstantiate can use them
187+
baseModule.memory = baseModule.memory || memory;
188+
baseModule.table = baseModule.table || table;
189+
179190
// Demangle exports and provide the usual utility on the prototype
180191
return demangle(rawExports, Object.defineProperties(baseModule, {
181192
I8: { get: function() { checkMem(); return I8; } },

lib/loader/tests/assembly/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ export const varadd_ptr = varadd;
6060
export function calladd(fn: (a: i32, b: i32) => i32, a: i32, b: i32): i32 {
6161
return fn(a, b);
6262
}
63+
64+
export function dotrace(num: f64): void {
65+
trace("The answer is", 1, num);
66+
}

lib/loader/tests/build/untouched.wasm

-1.54 KB
Binary file not shown.

lib/loader/tests/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,6 @@ assert.strictEqual(fn(2), 4);
5959
// should be able to create a new function and call it from WASM
6060
ptr = module.newFunction(module.varadd);
6161
assert.strictEqual(module.calladd(ptr, 2, 3), 5);
62+
63+
// should be able to use trace
64+
module.dotrace(42);

0 commit comments

Comments
 (0)