Fix variable arguments handling with setargc in loader

This commit is contained in:
dcodeIO 2018-10-11 17:55:11 +02:00
parent 87ec6c59ce
commit 086d96b299
2 changed files with 11 additions and 10 deletions

View File

@ -40,10 +40,13 @@ function preInstantiate(imports) {
/** Prepares the final module once instantiation is complete. */ /** Prepares the final module once instantiation is complete. */
function postInstantiate(baseModule, instance) { function postInstantiate(baseModule, instance) {
var memory = instance.exports.memory; var rawExports = instance.exports;
var memory_allocate = instance.exports["memory.allocate"]; var memory = rawExports.memory;
var memory_fill = instance.exports["memory.fill"]; var memory_allocate = rawExports["memory.allocate"];
var memory_free = instance.exports["memory.free"]; var memory_fill = rawExports["memory.fill"];
var memory_free = rawExports["memory.free"];
var table = rawExports.table;
var setargc = rawExports._setargc || function() {};
// Provide views for all sorts of basic values // Provide views for all sorts of basic values
var buffer, I8, U8, I16, U16, I32, U32, F32, F64, I64, U64; var buffer, I8, U8, I16, U16, I32, U32, F32, F64, I64, U64;
@ -152,9 +155,6 @@ function postInstantiate(baseModule, instance) {
baseModule.freeArray = freeArray; 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. */ /** Creates a new function in the module's table and returns its pointer. */
function newFunction(fn) { function newFunction(fn) {
var index = table.length; var index = table.length;
@ -169,7 +169,7 @@ function postInstantiate(baseModule, instance) {
function getFunction(ptr) { function getFunction(ptr) {
var fn = table.get(ptr); var fn = table.get(ptr);
return (...args) => { return (...args) => {
exports._setargc(args.length); setargc(args.length);
return fn(...args); return fn(...args);
}; };
} }
@ -177,7 +177,7 @@ function postInstantiate(baseModule, instance) {
baseModule.getFunction = getFunction; baseModule.getFunction = getFunction;
// Demangle exports and provide the usual utility on the prototype // Demangle exports and provide the usual utility on the prototype
return demangle(instance.exports, Object.defineProperties(baseModule, { return demangle(rawExports, Object.defineProperties(baseModule, {
I8: { get: function() { checkMem(); return I8; } }, I8: { get: function() { checkMem(); return I8; } },
U8: { get: function() { checkMem(); return U8; } }, U8: { get: function() { checkMem(); return U8; } },
I16: { get: function() { checkMem(); return I16; } }, I16: { get: function() { checkMem(); return I16; } },
@ -221,6 +221,7 @@ exports.instantiateStreaming = instantiateStreaming;
/** Demangles an AssemblyScript module's exports to a friendly object structure. */ /** Demangles an AssemblyScript module's exports to a friendly object structure. */
function demangle(exports, baseModule) { function demangle(exports, baseModule) {
var module = baseModule ? Object.create(baseModule) : {}; var module = baseModule ? Object.create(baseModule) : {};
var setargc = exports._setargc || function() {};
function hasOwnProperty(elem, prop) { function hasOwnProperty(elem, prop) {
return Object.prototype.hasOwnProperty.call(elem, prop); return Object.prototype.hasOwnProperty.call(elem, prop);
} }
@ -286,7 +287,7 @@ function demangle(exports, baseModule) {
function wrapFunction(fn) { function wrapFunction(fn) {
var ret = function(...args) { var ret = function(...args) {
exports._setargc(args.length); setargc(args.length);
return fn(...args); return fn(...args);
}; };
// adding a function to the table with `newFunction` is limited to actual exported WebAssembly // adding a function to the table with `newFunction` is limited to actual exported WebAssembly