Skip to content

Commit 086d96b

Browse files
committed
Fix variable arguments handling with setargc in loader
1 parent 87ec6c5 commit 086d96b

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

lib/loader/index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ function preInstantiate(imports) {
4040

4141
/** Prepares the final module once instantiation is complete. */
4242
function postInstantiate(baseModule, instance) {
43-
var memory = instance.exports.memory;
44-
var memory_allocate = instance.exports["memory.allocate"];
45-
var memory_fill = instance.exports["memory.fill"];
46-
var memory_free = instance.exports["memory.free"];
43+
var rawExports = instance.exports;
44+
var memory = rawExports.memory;
45+
var memory_allocate = rawExports["memory.allocate"];
46+
var memory_fill = rawExports["memory.fill"];
47+
var memory_free = rawExports["memory.free"];
48+
var table = rawExports.table;
49+
var setargc = rawExports._setargc || function() {};
4750

4851
// Provide views for all sorts of basic values
4952
var buffer, I8, U8, I16, U16, I32, U32, F32, F64, I64, U64;
@@ -152,9 +155,6 @@ function postInstantiate(baseModule, instance) {
152155

153156
baseModule.freeArray = freeArray;
154157

155-
// Reference the table and remember where to insert the next function
156-
var table = exports.table;
157-
158158
/** Creates a new function in the module's table and returns its pointer. */
159159
function newFunction(fn) {
160160
var index = table.length;
@@ -169,15 +169,15 @@ function postInstantiate(baseModule, instance) {
169169
function getFunction(ptr) {
170170
var fn = table.get(ptr);
171171
return (...args) => {
172-
exports._setargc(args.length);
172+
setargc(args.length);
173173
return fn(...args);
174174
};
175175
}
176176

177177
baseModule.getFunction = getFunction;
178178

179179
// Demangle exports and provide the usual utility on the prototype
180-
return demangle(instance.exports, Object.defineProperties(baseModule, {
180+
return demangle(rawExports, Object.defineProperties(baseModule, {
181181
I8: { get: function() { checkMem(); return I8; } },
182182
U8: { get: function() { checkMem(); return U8; } },
183183
I16: { get: function() { checkMem(); return I16; } },
@@ -221,6 +221,7 @@ exports.instantiateStreaming = instantiateStreaming;
221221
/** Demangles an AssemblyScript module's exports to a friendly object structure. */
222222
function demangle(exports, baseModule) {
223223
var module = baseModule ? Object.create(baseModule) : {};
224+
var setargc = exports._setargc || function() {};
224225
function hasOwnProperty(elem, prop) {
225226
return Object.prototype.hasOwnProperty.call(elem, prop);
226227
}
@@ -286,7 +287,7 @@ function demangle(exports, baseModule) {
286287

287288
function wrapFunction(fn) {
288289
var ret = function(...args) {
289-
exports._setargc(args.length);
290+
setargc(args.length);
290291
return fn(...args);
291292
};
292293
// adding a function to the table with `newFunction` is limited to actual exported WebAssembly
-72 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)