Fix imported memory fallback and add common stdlib imports to loader

This commit is contained in:
dcodeIO 2018-11-11 10:44:09 +01:00
parent 9ab7384122
commit 6f3209e6c6
4 changed files with 27 additions and 9 deletions

View File

@ -23,17 +23,24 @@ function getStringImpl(U32, U16, ptr) {
function preInstantiate(imports) {
var baseModule = {};
// add the internal abort function that is called when an assertion fails or an error is thrown
if (!imports.env) imports.env = {};
if (!imports.env.abort) imports.env.abort = function abort(mesg, file, line, colm) {
var memory = baseModule.memory || imports.env.memory; // prefer exported, otherwise try imported
function getString(memory, ptr) {
if (!memory) return "<yet unknown>";
var buffer = memory.buffer;
return getStringImpl(new Uint32Array(buffer), new Uint16Array(buffer), ptr);
}
function getString(memory, ptr) {
if (!memory) return "<yet unknown>";
var buffer = memory.buffer;
return getStringImpl(new Uint32Array(buffer), new Uint16Array(buffer), ptr);
}
// add common imports used by stdlib for convenience
var env = (imports.env = imports.env || {});
env.abort = env.abort || function abort(mesg, file, line, colm) {
var memory = baseModule.memory || env.memory; // prefer exported, otherwise try imported
throw Error("abort: " + getString(memory, mesg) + " at " + getString(memory, file) + ":" + line + ":" + colm);
}
env.trace = env.trace || function trace(mesg, n) {
var memory = baseModule.memory || env.memory;
console.log("trace: " + getString(memory, mesg) + (n ? " " : "") + Array.prototype.slice.call(arguments, 2, 2 + n).join(", "));
}
imports.Math = imports.Math || Math;
imports.Date = imports.Date || Date;
return baseModule;
}
@ -176,6 +183,10 @@ function postInstantiate(baseModule, instance) {
baseModule.getFunction = getFunction;
// Pull basic exports to baseModule so code in preInstantiate can use them
baseModule.memory = baseModule.memory || memory;
baseModule.table = baseModule.table || table;
// Demangle exports and provide the usual utility on the prototype
return demangle(rawExports, Object.defineProperties(baseModule, {
I8: { get: function() { checkMem(); return I8; } },

View File

@ -60,3 +60,7 @@ export const varadd_ptr = varadd;
export function calladd(fn: (a: i32, b: i32) => i32, a: i32, b: i32): i32 {
return fn(a, b);
}
export function dotrace(num: f64): void {
trace("The answer is", 1, num);
}

View File

@ -59,3 +59,6 @@ assert.strictEqual(fn(2), 4);
// should be able to create a new function and call it from WASM
ptr = module.newFunction(module.varadd);
assert.strictEqual(module.calladd(ptr, 2, 3), 5);
// should be able to use trace
module.dotrace(42);