Skip to content

Commit bef5bea

Browse files
authored
Backport minor changes made in #1559 (#1591)
1 parent f314512 commit bef5bea

33 files changed

+130
-142
lines changed

cli/asc.js

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ const fs = require("fs");
3434
const path = require("path");
3535
const process = require("process"); // ensure shim
3636

37+
process.exit = ((exit) => function(code) {
38+
if (code) console.log(new Error("exit " + code.toString()).stack);
39+
exit(code);
40+
})(process.exit);
41+
3742
const utf8 = require("./util/utf8");
3843
const colorsUtil = require("./util/colors");
3944
const optionsUtil = require("./util/options");

lib/loader/index.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,16 @@ function postInstantiate(extendedExports, instance) {
8181
const exports = instance.exports;
8282
const memory = exports.memory;
8383
const table = exports.table;
84-
const new_ = exports["__new"];
85-
const retain = exports["__retain"];
86-
const rttiBase = exports["__rtti_base"] || ~0; // oob if not present
84+
const __new = exports["__new"];
85+
const __retain = exports["__retain"];
86+
const __rtti_base = exports["__rtti_base"] || ~0; // oob if not present
8787

8888
/** Gets the runtime type info for the given id. */
8989
function getInfo(id) {
9090
const U32 = new Uint32Array(memory.buffer);
91-
const count = U32[rttiBase >>> 2];
91+
const count = U32[__rtti_base >>> 2];
9292
if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`);
93-
return U32[(rttiBase + 4 >>> 2) + id * 2];
93+
return U32[(__rtti_base + 4 >>> 2) + id * 2];
9494
}
9595

9696
/** Gets and validate runtime type info for the given id for array like objects */
@@ -103,9 +103,9 @@ function postInstantiate(extendedExports, instance) {
103103
/** Gets the runtime base id for the given id. */
104104
function getBase(id) {
105105
const U32 = new Uint32Array(memory.buffer);
106-
const count = U32[rttiBase >>> 2];
106+
const count = U32[__rtti_base >>> 2];
107107
if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`);
108-
return U32[(rttiBase + 4 >>> 2) + id * 2 + 1];
108+
return U32[(__rtti_base + 4 >>> 2) + id * 2 + 1];
109109
}
110110

111111
/** Gets the runtime alignment of a collection's values. */
@@ -120,8 +120,9 @@ function postInstantiate(extendedExports, instance) {
120120

121121
/** Allocates a new string in the module's memory and returns its retained pointer. */
122122
function __newString(str) {
123+
if (str == null) return 0;
123124
const length = str.length;
124-
const ptr = new_(length << 1, STRING_ID);
125+
const ptr = __new(length << 1, STRING_ID);
125126
const U16 = new Uint16Array(memory.buffer);
126127
for (var i = 0, p = ptr >>> 1; i < length; ++i) U16[p + i] = str.charCodeAt(i);
127128
return ptr;
@@ -131,6 +132,7 @@ function postInstantiate(extendedExports, instance) {
131132

132133
/** Reads a string from the module's memory by its pointer. */
133134
function __getString(ptr) {
135+
if (!ptr) return null;
134136
const buffer = memory.buffer;
135137
const id = new Uint32Array(buffer)[ptr + ID_OFFSET >>> 2];
136138
if (id !== STRING_ID) throw Error(`not a string: ${ptr}`);
@@ -163,22 +165,22 @@ function postInstantiate(extendedExports, instance) {
163165
const info = getArrayInfo(id);
164166
const align = getValueAlign(info);
165167
const length = values.length;
166-
const buf = new_(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);
168+
const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);
167169
let result;
168170
if (info & STATICARRAY) {
169171
result = buf;
170172
} else {
171-
const arr = new_(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id);
173+
const arr = __new(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id);
172174
const U32 = new Uint32Array(memory.buffer);
173-
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = retain(buf);
175+
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = __retain(buf);
174176
U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2] = buf;
175177
U32[arr + ARRAYBUFFERVIEW_DATALENGTH_OFFSET >>> 2] = length << align;
176178
if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length;
177179
result = arr;
178180
}
179181
const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT);
180182
if (info & VAL_MANAGED) {
181-
for (let i = 0; i < length; ++i) view[(buf >>> align) + i] = retain(values[i]);
183+
for (let i = 0; i < length; ++i) view[(buf >>> align) + i] = __retain(values[i]);
182184
} else {
183185
view.set(values, buf >>> align);
184186
}
@@ -267,7 +269,7 @@ function postInstantiate(extendedExports, instance) {
267269
function __instanceof(ptr, baseId) {
268270
const U32 = new Uint32Array(memory.buffer);
269271
let id = U32[ptr + ID_OFFSET >>> 2];
270-
if (id <= U32[rttiBase >>> 2]) {
272+
if (id <= U32[__rtti_base >>> 2]) {
271273
do {
272274
if (id == baseId) return true;
273275
id = getBase(id);
@@ -331,7 +333,6 @@ export async function instantiateStreaming(source, imports = {}) {
331333

332334
/** Demangles an AssemblyScript module's exports to a friendly object structure. */
333335
export function demangle(exports, extendedExports = {}) {
334-
extendedExports = Object.create(extendedExports);
335336
const setArgumentsLength = exports["__argumentsLength"]
336337
? length => { exports["__argumentsLength"].value = length; }
337338
: exports["__setArgumentsLength"] || exports["__setargc"] || (() => { /* nop */ });

lib/loader/umd/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "..";
1+
export * from "../index";

lib/loader/umd/index.js

+22-14
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,19 @@ var loader = (function(exports) {
9595
const exports = instance.exports;
9696
const memory = exports.memory;
9797
const table = exports.table;
98-
const new_ = exports["__new"];
99-
const retain = exports["__retain"];
100-
const rttiBase = exports["__rtti_base"] || ~0; // oob if not present
98+
const __new = exports["__new"];
99+
const __retain = exports["__retain"];
100+
101+
const __rtti_base = exports["__rtti_base"] || ~0; // oob if not present
101102

102103
/** Gets the runtime type info for the given id. */
103104

105+
104106
function getInfo(id) {
105107
const U32 = new Uint32Array(memory.buffer);
106-
const count = U32[rttiBase >>> 2];
108+
const count = U32[__rtti_base >>> 2];
107109
if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`);
108-
return U32[(rttiBase + 4 >>> 2) + id * 2];
110+
return U32[(__rtti_base + 4 >>> 2) + id * 2];
109111
}
110112
/** Gets and validate runtime type info for the given id for array like objects */
111113

@@ -120,9 +122,9 @@ var loader = (function(exports) {
120122

121123
function getBase(id) {
122124
const U32 = new Uint32Array(memory.buffer);
123-
const count = U32[rttiBase >>> 2];
125+
const count = U32[__rtti_base >>> 2];
124126
if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`);
125-
return U32[(rttiBase + 4 >>> 2) + id * 2 + 1];
127+
return U32[(__rtti_base + 4 >>> 2) + id * 2 + 1];
126128
}
127129
/** Gets the runtime alignment of a collection's values. */
128130

@@ -139,8 +141,11 @@ var loader = (function(exports) {
139141

140142

141143
function __newString(str) {
144+
if (str == null) return 0;
142145
const length = str.length;
143-
const ptr = new_(length << 1, STRING_ID);
146+
147+
const ptr = __new(length << 1, STRING_ID);
148+
144149
const U16 = new Uint16Array(memory.buffer);
145150

146151
for (var i = 0, p = ptr >>> 1; i < length; ++i) U16[p + i] = str.charCodeAt(i);
@@ -152,6 +157,7 @@ var loader = (function(exports) {
152157
/** Reads a string from the module's memory by its pointer. */
153158

154159
function __getString(ptr) {
160+
if (!ptr) return null;
155161
const buffer = memory.buffer;
156162
const id = new Uint32Array(buffer)[ptr + ID_OFFSET >>> 2];
157163
if (id !== STRING_ID) throw Error(`not a string: ${ptr}`);
@@ -197,15 +203,18 @@ var loader = (function(exports) {
197203
const info = getArrayInfo(id);
198204
const align = getValueAlign(info);
199205
const length = values.length;
200-
const buf = new_(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);
206+
207+
const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);
208+
201209
let result;
202210

203211
if (info & STATICARRAY) {
204212
result = buf;
205213
} else {
206-
const arr = new_(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id);
214+
const arr = __new(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id);
215+
207216
const U32 = new Uint32Array(memory.buffer);
208-
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = retain(buf);
217+
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = __retain(buf);
209218
U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2] = buf;
210219
U32[arr + ARRAYBUFFERVIEW_DATALENGTH_OFFSET >>> 2] = length << align;
211220
if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length;
@@ -215,7 +224,7 @@ var loader = (function(exports) {
215224
const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT);
216225

217226
if (info & VAL_MANAGED) {
218-
for (let i = 0; i < length; ++i) view[(buf >>> align) + i] = retain(values[i]);
227+
for (let i = 0; i < length; ++i) view[(buf >>> align) + i] = __retain(values[i]);
219228
} else {
220229
view.set(values, buf >>> align);
221230
}
@@ -298,7 +307,7 @@ var loader = (function(exports) {
298307
const U32 = new Uint32Array(memory.buffer);
299308
let id = U32[ptr + ID_OFFSET >>> 2];
300309

301-
if (id <= U32[rttiBase >>> 2]) {
310+
if (id <= U32[__rtti_base >>> 2]) {
302311
do {
303312
if (id == baseId) return true;
304313
id = getBase(id);
@@ -371,7 +380,6 @@ var loader = (function(exports) {
371380

372381

373382
function demangle(exports, extendedExports = {}) {
374-
extendedExports = Object.create(extendedExports);
375383
const setArgumentsLength = exports["__argumentsLength"] ? length => {
376384
exports["__argumentsLength"].value = length;
377385
} : exports["__setArgumentsLength"] || exports["__setargc"] || (() => {

lib/rtrace/umd/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "..";
1+
export * from "../index";

src/compiler.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,13 @@ export class Compiler extends DiagnosticEmitter {
459459
let signature = startFunctionInstance.signature;
460460
if (!startIsEmpty && explicitStart) {
461461
module.addGlobal(BuiltinNames.started, NativeType.I32, true, module.i32(0));
462+
startFunctionBody.unshift(
463+
module.global_set(BuiltinNames.started, module.i32(1))
464+
);
462465
startFunctionBody.unshift(
463466
module.if(
464467
module.global_get(BuiltinNames.started, NativeType.I32),
465-
module.return(),
466-
module.global_set(BuiltinNames.started, module.i32(1))
468+
module.return()
467469
)
468470
);
469471
}
@@ -5354,7 +5356,7 @@ export class Compiler extends DiagnosticEmitter {
53545356
case TypeKind.U32: {
53555357
let instance = this.i32PowInstance;
53565358
if (!instance) {
5357-
let prototype = this.program.lookupGlobal(CommonNames.ipow32);
5359+
let prototype = this.program.lookup(CommonNames.ipow32);
53585360
if (!prototype) {
53595361
this.error(
53605362
DiagnosticCode.Cannot_find_name_0,
@@ -5380,7 +5382,7 @@ export class Compiler extends DiagnosticEmitter {
53805382
case TypeKind.U64: {
53815383
let instance = this.i64PowInstance;
53825384
if (!instance) {
5383-
let prototype = this.program.lookupGlobal(CommonNames.ipow64);
5385+
let prototype = this.program.lookup(CommonNames.ipow64);
53845386
if (!prototype) {
53855387
this.error(
53865388
DiagnosticCode.Cannot_find_name_0,
@@ -5401,7 +5403,7 @@ export class Compiler extends DiagnosticEmitter {
54015403
let isWasm64 = this.options.isWasm64;
54025404
let instance = isWasm64 ? this.i64PowInstance : this.i32PowInstance;
54035405
if (!instance) {
5404-
let prototype = this.program.lookupGlobal(isWasm64 ? CommonNames.ipow64 : CommonNames.ipow32);
5406+
let prototype = this.program.lookup(isWasm64 ? CommonNames.ipow64 : CommonNames.ipow32);
54055407
if (!prototype) {
54065408
this.error(
54075409
DiagnosticCode.Cannot_find_name_0,
@@ -5425,7 +5427,7 @@ export class Compiler extends DiagnosticEmitter {
54255427
case TypeKind.F32: {
54265428
let instance = this.f32PowInstance;
54275429
if (!instance) {
5428-
let namespace = this.program.lookupGlobal(CommonNames.Mathf);
5430+
let namespace = this.program.lookup(CommonNames.Mathf);
54295431
if (!namespace) {
54305432
this.error(
54315433
DiagnosticCode.Cannot_find_name_0,
@@ -5454,7 +5456,7 @@ export class Compiler extends DiagnosticEmitter {
54545456
case TypeKind.F64: {
54555457
let instance = this.f64PowInstance;
54565458
if (!instance) {
5457-
let namespace = this.program.lookupGlobal(CommonNames.Math);
5459+
let namespace = this.program.lookup(CommonNames.Math);
54585460
if (!namespace) {
54595461
this.error(
54605462
DiagnosticCode.Cannot_find_name_0,
@@ -5592,7 +5594,7 @@ export class Compiler extends DiagnosticEmitter {
55925594
case TypeKind.F32: {
55935595
let instance = this.f32ModInstance;
55945596
if (!instance) {
5595-
let namespace = this.program.lookupGlobal(CommonNames.Mathf);
5597+
let namespace = this.program.lookup(CommonNames.Mathf);
55965598
if (!namespace) {
55975599
this.error(
55985600
DiagnosticCode.Cannot_find_name_0,
@@ -5620,7 +5622,7 @@ export class Compiler extends DiagnosticEmitter {
56205622
case TypeKind.F64: {
56215623
let instance = this.f64ModInstance;
56225624
if (!instance) {
5623-
let namespace = this.program.lookupGlobal(CommonNames.Math);
5625+
let namespace = this.program.lookup(CommonNames.Math);
56245626
if (!namespace) {
56255627
this.error(
56265628
DiagnosticCode.Cannot_find_name_0,

src/glue/binaryen.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,10 @@ export declare function _BinaryenFunctionGetParams(func: BinaryenFunctionRef): B
851851
export declare function _BinaryenFunctionGetResults(func: BinaryenFunctionRef): BinaryenType;
852852
export declare function _BinaryenFunctionGetNumVars(func: BinaryenFunctionRef): BinaryenIndex;
853853
export declare function _BinaryenFunctionGetVar(func: BinaryenFunctionRef, index: BinaryenIndex): BinaryenType;
854+
export declare function _BinaryenFunctionGetNumLocals(func: BinaryenFunctionRef): BinaryenIndex;
855+
export declare function _BinaryenFunctionHasLocalName(func: BinaryenFunctionRef, index: BinaryenIndex): bool;
856+
export declare function _BinaryenFunctionGetLocalName(func: BinaryenFunctionRef, index: BinaryenIndex): BinaryenString;
857+
export declare function _BinaryenFunctionSetLocalName(func: BinaryenFunctionRef, index: BinaryenIndex, name: BinaryenString): void;
854858
export declare function _BinaryenFunctionGetBody(func: BinaryenFunctionRef): BinaryenExpressionRef;
855859
export declare function _BinaryenFunctionSetBody(func: BinaryenFunctionRef, bodyExpr: BinaryenExpressionRef): void;
856860
export declare function _BinaryenFunctionOptimize(func: BinaryenFunctionRef, module: BinaryenModuleRef): void;

0 commit comments

Comments
 (0)