mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-25 15:12:12 +00:00
Fix imported memory fallback and add common stdlib imports to loader
This commit is contained in:
parent
9ab7384122
commit
6f3209e6c6
@ -23,17 +23,24 @@ function getStringImpl(U32, U16, ptr) {
|
|||||||
function preInstantiate(imports) {
|
function preInstantiate(imports) {
|
||||||
var baseModule = {};
|
var baseModule = {};
|
||||||
|
|
||||||
// add the internal abort function that is called when an assertion fails or an error is thrown
|
function getString(memory, ptr) {
|
||||||
if (!imports.env) imports.env = {};
|
if (!memory) return "<yet unknown>";
|
||||||
if (!imports.env.abort) imports.env.abort = function abort(mesg, file, line, colm) {
|
var buffer = memory.buffer;
|
||||||
var memory = baseModule.memory || imports.env.memory; // prefer exported, otherwise try imported
|
return getStringImpl(new Uint32Array(buffer), new Uint16Array(buffer), ptr);
|
||||||
function getString(memory, ptr) {
|
}
|
||||||
if (!memory) return "<yet unknown>";
|
|
||||||
var buffer = memory.buffer;
|
// add common imports used by stdlib for convenience
|
||||||
return getStringImpl(new Uint32Array(buffer), new Uint16Array(buffer), ptr);
|
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);
|
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;
|
return baseModule;
|
||||||
}
|
}
|
||||||
@ -176,6 +183,10 @@ function postInstantiate(baseModule, instance) {
|
|||||||
|
|
||||||
baseModule.getFunction = getFunction;
|
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
|
// Demangle exports and provide the usual utility on the prototype
|
||||||
return demangle(rawExports, Object.defineProperties(baseModule, {
|
return demangle(rawExports, Object.defineProperties(baseModule, {
|
||||||
I8: { get: function() { checkMem(); return I8; } },
|
I8: { get: function() { checkMem(); return I8; } },
|
||||||
|
@ -60,3 +60,7 @@ export const varadd_ptr = varadd;
|
|||||||
export function calladd(fn: (a: i32, b: i32) => i32, a: i32, b: i32): i32 {
|
export function calladd(fn: (a: i32, b: i32) => i32, a: i32, b: i32): i32 {
|
||||||
return fn(a, b);
|
return fn(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function dotrace(num: f64): void {
|
||||||
|
trace("The answer is", 1, num);
|
||||||
|
}
|
||||||
|
Binary file not shown.
@ -59,3 +59,6 @@ assert.strictEqual(fn(2), 4);
|
|||||||
// should be able to create a new function and call it from WASM
|
// should be able to create a new function and call it from WASM
|
||||||
ptr = module.newFunction(module.varadd);
|
ptr = module.newFunction(module.varadd);
|
||||||
assert.strictEqual(module.calladd(ptr, 2, 3), 5);
|
assert.strictEqual(module.calladd(ptr, 2, 3), 5);
|
||||||
|
|
||||||
|
// should be able to use trace
|
||||||
|
module.dotrace(42);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user