Update Binaryen to latest; Various fixes

This commit is contained in:
Daniel Wirtz
2018-10-11 08:49:08 +02:00
committed by GitHub
parent b54a97c0fe
commit f7c734789e
228 changed files with 12372 additions and 14996 deletions

View File

@ -61,9 +61,11 @@ interface ASUtil {
/** Creates a typed array in the module's memory and returns its pointer. */
newArray(ctor: TypedArrayConstructor, length: number, unsafe?: boolean): number;
/** Gets a view on a typed array in the module's memory by its pointer. */
getArray(ctor: TypedArrayConstructor, ptr: number): TypedArray;
getArray<T extends TypedArray = TypedArray>(ctor: TypedArrayConstructor, ptr: number): T;
/** Frees a typed array in the module's memory. Must not be accessed anymore afterwards. */
freeArray(ptr: number): void;
/** Gets a function by its pointer. */
getFunction<R = any>(ptr: number): (...args: any[]) => R;
}
/** Instantiates an AssemblyScript module using the specified imports. */

View File

@ -152,6 +152,30 @@ function postInstantiate(baseModule, instance) {
baseModule.freeArray = freeArray;
// Reference the table and remember where to insert the next function
var table = exports.table;
/** Creates a new function in the module's table and returns its pointer. */
function newFunction(fn) {
var index = table.length;
table.grow(1);
table.set(index, fn);
return index;
}
baseModule.newFunction = newFunction;
/** Gets a function by its pointer. */
function getFunction(ptr) {
var fn = table.get(ptr);
return (...args) => {
exports._setargc(args.length);
return fn(...args);
};
}
baseModule.getFunction = getFunction;
// Demangle exports and provide the usual utility on the prototype
return demangle(instance.exports, Object.defineProperties(baseModule, {
I8: { get: function() { checkMem(); return I8; } },
@ -240,7 +264,9 @@ function demangle(exports, baseModule) {
enumerable: true
});
}
} else curr[name] = function(...args) { return elem(this.this, ...args); };
} else {
curr[name] = wrapFunction(elem);
}
} else {
if (/^(get|set):/.test(name)) {
if (!hasOwnProperty(curr, name = name.substring(4))) {
@ -250,9 +276,25 @@ function demangle(exports, baseModule) {
enumerable: true
});
}
} else curr[name] = elem;
} else if (typeof elem === "function") {
curr[name] = wrapFunction(elem);
} else {
curr[name] = elem;
}
}
}
function wrapFunction(fn) {
var ret = function(...args) {
exports._setargc(args.length);
return fn(...args);
};
// adding a function to the table with `newFunction` is limited to actual exported WebAssembly
// functions, hence we can't use the wrapper for that and instead need to pass a workaround:
ret.constructor = fn;
return ret;
}
return module;
}

View File

@ -50,3 +50,13 @@ export function sum(arr: Int32Array): i32 {
for (let i = 0, k = arr.length; i < k; ++i) v += arr[i];
return v;
}
export function varadd(a: i32 = 1, b: i32 = 2): i32 {
return a + b;
}
export const varadd_ptr = varadd;
export function calladd(fn: (a: i32, b: i32) => i32, a: i32, b: i32): i32 {
return fn(a, b);
}

View File

@ -18,8 +18,6 @@ assert(proto.I32 instanceof Int32Array);
assert(proto.U32 instanceof Uint32Array);
assert(proto.F32 instanceof Float32Array);
assert(proto.F64 instanceof Float64Array);
assert(typeof proto.newString === "function");
assert(typeof proto.getString === "function");
// should export memory
assert(module.memory instanceof WebAssembly.Memory);
@ -46,3 +44,18 @@ assert.deepEqual(module.getArray(Int32Array, ptr), arr);
module.freeArray(ptr);
var ptr2 = module.newArray(new Int32Array(arr));
assert.strictEqual(ptr, ptr2);
// should be able to just call a function with variable arguments
assert.strictEqual(module.varadd(), 3);
assert.strictEqual(module.varadd(2, 3), 5);
assert.strictEqual(module.varadd(2), 4);
// should be able to get a function from the table and just call it with variable arguments
var fn = module.getFunction(module.varadd_ptr);
assert.strictEqual(fn(), 3);
assert.strictEqual(fn(2, 3), 5);
assert.strictEqual(fn(2), 4);
// should be able to create a new function and call it from WASM
ptr = module.newFunction(module.varadd.constructor); // must be an actual exported wasm function
assert.strictEqual(module.calladd(ptr, 2, 3), 5);