Skip to content

Backport minor changes made in #1559 #1591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cli/asc.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ const fs = require("fs");
const path = require("path");
const process = require("process"); // ensure shim

process.exit = ((exit) => function(code) {
if (code) console.log(new Error("exit " + code.toString()).stack);
exit(code);
})(process.exit);

const utf8 = require("./util/utf8");
const colorsUtil = require("./util/colors");
const optionsUtil = require("./util/options");
Expand Down
29 changes: 15 additions & 14 deletions lib/loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ function postInstantiate(extendedExports, instance) {
const exports = instance.exports;
const memory = exports.memory;
const table = exports.table;
const new_ = exports["__new"];
const retain = exports["__retain"];
const rttiBase = exports["__rtti_base"] || ~0; // oob if not present
const __new = exports["__new"];
const __retain = exports["__retain"];
const __rtti_base = exports["__rtti_base"] || ~0; // oob if not present

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

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

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

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

/** Reads a string from the module's memory by its pointer. */
function __getString(ptr) {
if (!ptr) return null;
const buffer = memory.buffer;
const id = new Uint32Array(buffer)[ptr + ID_OFFSET >>> 2];
if (id !== STRING_ID) throw Error(`not a string: ${ptr}`);
Expand Down Expand Up @@ -163,22 +165,22 @@ function postInstantiate(extendedExports, instance) {
const info = getArrayInfo(id);
const align = getValueAlign(info);
const length = values.length;
const buf = new_(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);
const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);
let result;
if (info & STATICARRAY) {
result = buf;
} else {
const arr = new_(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id);
const arr = __new(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id);
const U32 = new Uint32Array(memory.buffer);
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = retain(buf);
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = __retain(buf);
U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2] = buf;
U32[arr + ARRAYBUFFERVIEW_DATALENGTH_OFFSET >>> 2] = length << align;
if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length;
result = arr;
}
const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT);
if (info & VAL_MANAGED) {
for (let i = 0; i < length; ++i) view[(buf >>> align) + i] = retain(values[i]);
for (let i = 0; i < length; ++i) view[(buf >>> align) + i] = __retain(values[i]);
} else {
view.set(values, buf >>> align);
}
Expand Down Expand Up @@ -267,7 +269,7 @@ function postInstantiate(extendedExports, instance) {
function __instanceof(ptr, baseId) {
const U32 = new Uint32Array(memory.buffer);
let id = U32[ptr + ID_OFFSET >>> 2];
if (id <= U32[rttiBase >>> 2]) {
if (id <= U32[__rtti_base >>> 2]) {
do {
if (id == baseId) return true;
id = getBase(id);
Expand Down Expand Up @@ -331,7 +333,6 @@ export async function instantiateStreaming(source, imports = {}) {

/** Demangles an AssemblyScript module's exports to a friendly object structure. */
export function demangle(exports, extendedExports = {}) {
extendedExports = Object.create(extendedExports);
const setArgumentsLength = exports["__argumentsLength"]
? length => { exports["__argumentsLength"].value = length; }
: exports["__setArgumentsLength"] || exports["__setargc"] || (() => { /* nop */ });
Expand Down
2 changes: 1 addition & 1 deletion lib/loader/umd/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "..";
export * from "../index";
36 changes: 22 additions & 14 deletions lib/loader/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,19 @@ var loader = (function(exports) {
const exports = instance.exports;
const memory = exports.memory;
const table = exports.table;
const new_ = exports["__new"];
const retain = exports["__retain"];
const rttiBase = exports["__rtti_base"] || ~0; // oob if not present
const __new = exports["__new"];
const __retain = exports["__retain"];

const __rtti_base = exports["__rtti_base"] || ~0; // oob if not present

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


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

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

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

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


function __newString(str) {
if (str == null) return 0;
const length = str.length;
const ptr = new_(length << 1, STRING_ID);

const ptr = __new(length << 1, STRING_ID);

const U16 = new Uint16Array(memory.buffer);

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

function __getString(ptr) {
if (!ptr) return null;
const buffer = memory.buffer;
const id = new Uint32Array(buffer)[ptr + ID_OFFSET >>> 2];
if (id !== STRING_ID) throw Error(`not a string: ${ptr}`);
Expand Down Expand Up @@ -197,15 +203,18 @@ var loader = (function(exports) {
const info = getArrayInfo(id);
const align = getValueAlign(info);
const length = values.length;
const buf = new_(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);

const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);

let result;

if (info & STATICARRAY) {
result = buf;
} else {
const arr = new_(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id);
const arr = __new(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id);

const U32 = new Uint32Array(memory.buffer);
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = retain(buf);
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = __retain(buf);
U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2] = buf;
U32[arr + ARRAYBUFFERVIEW_DATALENGTH_OFFSET >>> 2] = length << align;
if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length;
Expand All @@ -215,7 +224,7 @@ var loader = (function(exports) {
const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT);

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

if (id <= U32[rttiBase >>> 2]) {
if (id <= U32[__rtti_base >>> 2]) {
do {
if (id == baseId) return true;
id = getBase(id);
Expand Down Expand Up @@ -371,7 +380,6 @@ var loader = (function(exports) {


function demangle(exports, extendedExports = {}) {
extendedExports = Object.create(extendedExports);
const setArgumentsLength = exports["__argumentsLength"] ? length => {
exports["__argumentsLength"].value = length;
} : exports["__setArgumentsLength"] || exports["__setargc"] || (() => {
Expand Down
2 changes: 1 addition & 1 deletion lib/rtrace/umd/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "..";
export * from "../index";
20 changes: 11 additions & 9 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,13 @@ export class Compiler extends DiagnosticEmitter {
let signature = startFunctionInstance.signature;
if (!startIsEmpty && explicitStart) {
module.addGlobal(BuiltinNames.started, NativeType.I32, true, module.i32(0));
startFunctionBody.unshift(
module.global_set(BuiltinNames.started, module.i32(1))
);
startFunctionBody.unshift(
module.if(
module.global_get(BuiltinNames.started, NativeType.I32),
module.return(),
module.global_set(BuiltinNames.started, module.i32(1))
module.return()
)
);
}
Expand Down Expand Up @@ -5354,7 +5356,7 @@ export class Compiler extends DiagnosticEmitter {
case TypeKind.U32: {
let instance = this.i32PowInstance;
if (!instance) {
let prototype = this.program.lookupGlobal(CommonNames.ipow32);
let prototype = this.program.lookup(CommonNames.ipow32);
if (!prototype) {
this.error(
DiagnosticCode.Cannot_find_name_0,
Expand All @@ -5380,7 +5382,7 @@ export class Compiler extends DiagnosticEmitter {
case TypeKind.U64: {
let instance = this.i64PowInstance;
if (!instance) {
let prototype = this.program.lookupGlobal(CommonNames.ipow64);
let prototype = this.program.lookup(CommonNames.ipow64);
if (!prototype) {
this.error(
DiagnosticCode.Cannot_find_name_0,
Expand All @@ -5401,7 +5403,7 @@ export class Compiler extends DiagnosticEmitter {
let isWasm64 = this.options.isWasm64;
let instance = isWasm64 ? this.i64PowInstance : this.i32PowInstance;
if (!instance) {
let prototype = this.program.lookupGlobal(isWasm64 ? CommonNames.ipow64 : CommonNames.ipow32);
let prototype = this.program.lookup(isWasm64 ? CommonNames.ipow64 : CommonNames.ipow32);
if (!prototype) {
this.error(
DiagnosticCode.Cannot_find_name_0,
Expand All @@ -5425,7 +5427,7 @@ export class Compiler extends DiagnosticEmitter {
case TypeKind.F32: {
let instance = this.f32PowInstance;
if (!instance) {
let namespace = this.program.lookupGlobal(CommonNames.Mathf);
let namespace = this.program.lookup(CommonNames.Mathf);
if (!namespace) {
this.error(
DiagnosticCode.Cannot_find_name_0,
Expand Down Expand Up @@ -5454,7 +5456,7 @@ export class Compiler extends DiagnosticEmitter {
case TypeKind.F64: {
let instance = this.f64PowInstance;
if (!instance) {
let namespace = this.program.lookupGlobal(CommonNames.Math);
let namespace = this.program.lookup(CommonNames.Math);
if (!namespace) {
this.error(
DiagnosticCode.Cannot_find_name_0,
Expand Down Expand Up @@ -5592,7 +5594,7 @@ export class Compiler extends DiagnosticEmitter {
case TypeKind.F32: {
let instance = this.f32ModInstance;
if (!instance) {
let namespace = this.program.lookupGlobal(CommonNames.Mathf);
let namespace = this.program.lookup(CommonNames.Mathf);
if (!namespace) {
this.error(
DiagnosticCode.Cannot_find_name_0,
Expand Down Expand Up @@ -5620,7 +5622,7 @@ export class Compiler extends DiagnosticEmitter {
case TypeKind.F64: {
let instance = this.f64ModInstance;
if (!instance) {
let namespace = this.program.lookupGlobal(CommonNames.Math);
let namespace = this.program.lookup(CommonNames.Math);
if (!namespace) {
this.error(
DiagnosticCode.Cannot_find_name_0,
Expand Down
4 changes: 4 additions & 0 deletions src/glue/binaryen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,10 @@ export declare function _BinaryenFunctionGetParams(func: BinaryenFunctionRef): B
export declare function _BinaryenFunctionGetResults(func: BinaryenFunctionRef): BinaryenType;
export declare function _BinaryenFunctionGetNumVars(func: BinaryenFunctionRef): BinaryenIndex;
export declare function _BinaryenFunctionGetVar(func: BinaryenFunctionRef, index: BinaryenIndex): BinaryenType;
export declare function _BinaryenFunctionGetNumLocals(func: BinaryenFunctionRef): BinaryenIndex;
export declare function _BinaryenFunctionHasLocalName(func: BinaryenFunctionRef, index: BinaryenIndex): bool;
export declare function _BinaryenFunctionGetLocalName(func: BinaryenFunctionRef, index: BinaryenIndex): BinaryenString;
export declare function _BinaryenFunctionSetLocalName(func: BinaryenFunctionRef, index: BinaryenIndex, name: BinaryenString): void;
export declare function _BinaryenFunctionGetBody(func: BinaryenFunctionRef): BinaryenExpressionRef;
export declare function _BinaryenFunctionSetBody(func: BinaryenFunctionRef, bodyExpr: BinaryenExpressionRef): void;
export declare function _BinaryenFunctionOptimize(func: BinaryenFunctionRef, module: BinaryenModuleRef): void;
Expand Down
Loading