Skip to content

Commit e03b71d

Browse files
committed
Add a fallback for argumentsLength without mutable-globals
1 parent 60c0d83 commit e03b71d

File tree

9 files changed

+232
-207
lines changed

9 files changed

+232
-207
lines changed

cli/asc.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,16 @@
210210
" exception-handling Exception handling.",
211211
" tail-calls Tail call operations."
212212
],
213-
"type": "s"
213+
"type": "S"
214214
},
215215
"disable": {
216216
"description": [
217217
"Disables WebAssembly features that are enabled by default.",
218218
"",
219219
" mutable-globals Mutable global imports and exports.",
220220
""
221-
]
221+
],
222+
"type": "S"
222223
},
223224
"transform": {
224225
"description": "Specifies the path to a custom transform to 'require'.",

lib/loader/index.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,9 @@ function postInstantiate(baseModule, instance) {
277277
}
278278

279279
/** Wraps a WebAssembly function while also taking care of variable arguments. */
280-
function wrapFunction(fn, argumentsLength) {
280+
function wrapFunction(fn, setArgumentsLength) {
281281
var wrap = (...args) => {
282-
if (argumentsLength) argumentsLength.value = args.length;
282+
setArgumentsLength(args.length);
283283
return fn(...args);
284284
}
285285
wrap.original = fn;
@@ -342,7 +342,10 @@ exports.instantiateStreaming = instantiateStreaming;
342342
/** Demangles an AssemblyScript module's exports to a friendly object structure. */
343343
function demangle(exports, baseModule) {
344344
var module = baseModule ? Object.create(baseModule) : {};
345-
var argumentsLength = exports["__argumentsLength"];
345+
function setArgumentsLength(length) {
346+
if (exports["__argumentsLength"]) exports["__argumentsLength"].value = length;
347+
else if (exports["set:__argumentsLength"]) exports["set:__argumentsLength"](length);
348+
}
346349
function hasOwnProperty(elem, prop) {
347350
return Object.prototype.hasOwnProperty.call(elem, prop);
348351
}
@@ -392,11 +395,11 @@ function demangle(exports, baseModule) {
392395
}
393396
} else {
394397
if (name === 'constructor') {
395-
curr[name] = wrapFunction(elem, argumentsLength);
398+
curr[name] = wrapFunction(elem, setArgumentsLength);
396399
} else { // for methods
397400
Object.defineProperty(curr, name, {
398401
value: function (...args) {
399-
if (argumentsLength) argumentsLength.value = args.length;
402+
setArgumentsLength(args.length);
400403
return elem(this[THIS], ...args);
401404
}
402405
});
@@ -412,7 +415,7 @@ function demangle(exports, baseModule) {
412415
});
413416
}
414417
} else if (typeof elem === "function") {
415-
curr[name] = wrapFunction(elem, argumentsLength);
418+
curr[name] = wrapFunction(elem, setArgumentsLength);
416419
} else {
417420
curr[name] = elem;
418421
}

lib/loader/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
"main": "index.js",
2525
"types": "index.d.ts",
2626
"scripts": {
27-
"test:build": "asc tests/assembly/index.ts -b tests/build/untouched.wasm",
27+
"asbuild": "npm run asbuild:default && npm run asbuild:legacy",
28+
"asbuild:default": "asc tests/assembly/index.ts -b tests/build/default.wasm",
29+
"asbuild:legacy": "asc tests/assembly/index.ts --disable mutable-globals -b tests/build/legacy.wasm",
2830
"test": "node tests"
2931
},
3032
"files": [

lib/loader/tests/build/default.wasm

8.99 KB
Binary file not shown.

lib/loader/tests/build/legacy.wasm

9 KB
Binary file not shown.

lib/loader/tests/build/untouched.wasm

-10.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)