Skip to content

Commit dc85424

Browse files
authored
Add a fallback for argumentsLength without mutable-globals (AssemblyScript#1084)
1 parent 60c0d83 commit dc85424

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2351
-2152
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,21 @@ jobs:
123123
npm run build
124124
cd ..
125125
npm test rt-stub
126+
test-loader:
127+
name: "Test loader on node: node"
128+
runs-on: ubuntu-latest
129+
needs: check
130+
steps:
131+
- uses: actions/checkout@v1.0.0
132+
- uses: dcodeIO/setup-node-nvm@v1.0.0
133+
with:
134+
node-version: node
135+
- name: Install dependencies
136+
run: npm ci --no-audit
137+
- name: Clean distribution files
138+
run: npm run clean
139+
- name: Test the loader
140+
run: |
141+
cd lib/loader
142+
npm run asbuild
143+
npm run test

cli/asc.json

Lines changed: 3 additions & 2 deletions
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

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -276,16 +276,6 @@ function postInstantiate(baseModule, instance) {
276276
return demangle(rawExports, baseModule);
277277
}
278278

279-
/** Wraps a WebAssembly function while also taking care of variable arguments. */
280-
function wrapFunction(fn, argumentsLength) {
281-
var wrap = (...args) => {
282-
if (argumentsLength) argumentsLength.value = args.length;
283-
return fn(...args);
284-
}
285-
wrap.original = fn;
286-
return wrap;
287-
}
288-
289279
function isResponse(o) {
290280
return typeof Response !== "undefined" && o instanceof Response;
291281
}
@@ -342,18 +332,17 @@ exports.instantiateStreaming = instantiateStreaming;
342332
/** Demangles an AssemblyScript module's exports to a friendly object structure. */
343333
function demangle(exports, baseModule) {
344334
var module = baseModule ? Object.create(baseModule) : {};
345-
var argumentsLength = exports["__argumentsLength"];
346-
function hasOwnProperty(elem, prop) {
347-
return Object.prototype.hasOwnProperty.call(elem, prop);
348-
}
335+
var setArgumentsLength = exports["__argumentsLength"]
336+
? function(length) { exports["__argumentsLength"].value = length; }
337+
: exports["__setArgumentsLength"] || exports["__setargc"] || function() {};
349338
for (let internalName in exports) {
350-
if (!hasOwnProperty(exports, internalName)) continue;
351-
let elem = exports[internalName];
339+
if (!Object.prototype.hasOwnProperty.call(exports, internalName)) continue;
340+
const elem = exports[internalName];
352341
let parts = internalName.split(".");
353342
let curr = module;
354343
while (parts.length > 1) {
355344
let part = parts.shift();
356-
if (!hasOwnProperty(curr, part)) curr[part] = {};
345+
if (!Object.prototype.hasOwnProperty.call(curr, part)) curr[part] = {};
357346
curr = curr[part];
358347
}
359348
let name = parts[0];
@@ -381,7 +370,7 @@ function demangle(exports, baseModule) {
381370
name = name.substring(hash + 1);
382371
curr = curr[className].prototype;
383372
if (/^(get|set):/.test(name)) {
384-
if (!hasOwnProperty(curr, name = name.substring(4))) {
373+
if (!Object.prototype.hasOwnProperty.call(curr, name = name.substring(4))) {
385374
let getter = exports[internalName.replace("set:", "get:")];
386375
let setter = exports[internalName.replace("get:", "set:")];
387376
Object.defineProperty(curr, name, {
@@ -392,33 +381,36 @@ function demangle(exports, baseModule) {
392381
}
393382
} else {
394383
if (name === 'constructor') {
395-
curr[name] = wrapFunction(elem, argumentsLength);
396-
} else { // for methods
397-
Object.defineProperty(curr, name, {
398-
value: function (...args) {
399-
if (argumentsLength) argumentsLength.value = args.length;
400-
return elem(this[THIS], ...args);
401-
}
402-
});
384+
(curr[name] = (...args) => {
385+
setArgumentsLength(args.length);
386+
return elem(...args);
387+
}).original = elem;
388+
} else { // instance method
389+
(curr[name] = function(...args) { // !
390+
setArgumentsLength(args.length);
391+
return elem(this[THIS], ...args);
392+
}).original = elem;
403393
}
404394
}
405395
} else {
406396
if (/^(get|set):/.test(name)) {
407-
if (!hasOwnProperty(curr, name = name.substring(4))) {
397+
if (!Object.prototype.hasOwnProperty.call(curr, name = name.substring(4))) {
408398
Object.defineProperty(curr, name, {
409399
get: exports[internalName.replace("set:", "get:")],
410400
set: exports[internalName.replace("get:", "set:")],
411401
enumerable: true
412402
});
413403
}
414-
} else if (typeof elem === "function") {
415-
curr[name] = wrapFunction(elem, argumentsLength);
404+
} else if (typeof elem === "function" && elem !== setArgumentsLength) {
405+
(curr[name] = (...args) => {
406+
setArgumentsLength(args.length);
407+
return elem(...args);
408+
}).original = elem;
416409
} else {
417410
curr[name] = elem;
418411
}
419412
}
420413
}
421-
422414
return module;
423415
}
424416

lib/loader/package.json

Lines changed: 3 additions & 1 deletion
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": "node ../../bin/asc tests/assembly/index.ts -b tests/build/default.wasm",
29+
"asbuild:legacy": "node ../../bin/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

9 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)