diff --git a/src/glue/binaryen.js b/src/glue/binaryen.js index f173b76539..fc106ff8c8 100644 --- a/src/glue/binaryen.js +++ b/src/glue/binaryen.js @@ -9,7 +9,7 @@ module.exports = binaryen; const { Module } = require("../module"); -Module.prototype.toText = function() { +Module.prototype.toText = function toText() { // NOTE: Conversion to StackIR can yield conversion artifacts like sequences // of unreachable statements not actually emitted by the compiler. Optimizing // StackIR removes these again, but may also suppress useless code emitted by @@ -17,6 +17,6 @@ Module.prototype.toText = function() { return binaryen.wrapModule(this.ref).emitStackIR(/* optimize-stack-ir */ true); }; -Module.prototype.toAsmjs = function() { +Module.prototype.toAsmjs = function toAsmjs() { return binaryen.wrapModule(this.ref).emitAsmjs(); }; diff --git a/src/glue/js/collections.js b/src/glue/js/collections.js index a94224f0a3..63a3b9499d 100644 --- a/src/glue/js/collections.js +++ b/src/glue/js/collections.js @@ -3,14 +3,14 @@ * @license Apache-2.0 */ -global.Map_keys = function(map) { +global.Map_keys = function Map_keys(map) { return Array.from(map.keys()); }; -global.Map_values = function(map) { +global.Map_values = function Map_values(map) { return Array.from(map.values()); }; -global.Set_values = function(set) { +global.Set_values = function Set_values(set) { return Array.from(set.values()); }; diff --git a/src/glue/js/float.js b/src/glue/js/float.js index ee3992b149..0c32ed5a0e 100644 --- a/src/glue/js/float.js +++ b/src/glue/js/float.js @@ -9,22 +9,22 @@ const F64 = new Float64Array(1); const F32 = new Float32Array(F64.buffer); const I32 = new Int32Array(F64.buffer); -global.f32_as_i32 = function(value) { +global.f32_as_i32 = function f32_as_i32(value) { F32[0] = value; return I32[0]; }; -global.i32_as_f32 = function(value) { +global.i32_as_f32 = function i32_as_f32(value) { I32[0] = value; return F32[0]; }; -global.f64_as_i64 = function(value) { +global.f64_as_i64 = function f64_as_i64(value) { F64[0] = value; return i64_new(I32[0], I32[1]); }; -global.i64_as_f64 = function(value) { +global.i64_as_f64 = function i64_as_f64(value) { I32[0] = i64_low(value); I32[1] = i64_high(value); return F64[0]; diff --git a/src/glue/js/i64.js b/src/glue/js/i64.js index 47b634f59d..9e05e7a22b 100644 --- a/src/glue/js/i64.js +++ b/src/glue/js/i64.js @@ -11,31 +11,31 @@ global.i64_zero = Long.ZERO; global.i64_one = Long.ONE; global.i64_neg_one = Long.fromInt(-1); -global.i64_new = function(lo, hi) { +global.i64_new = function i64_new(lo, hi) { return Long.fromBits(lo, hi); }; -global.i64_low = function(value) { +global.i64_low = function i64_low(value) { return value.low; }; -global.i64_high = function(value) { +global.i64_high = function i64_high(value) { return value.high; }; -global.i64_add = function(left, right) { +global.i64_add = function i64_add(left, right) { return left.add(right); }; -global.i64_sub = function(left, right) { +global.i64_sub = function i64_sub(left, right) { return left.sub(right); }; -global.i64_mul = function(left, right) { +global.i64_mul = function i64_mul(left, right) { return left.mul(right); }; -global.i64_pow = function(left, right) { +global.i64_pow = function i64_pow(left, right) { var rightLo = right.low; var rightHi = right.high; if (rightHi <= 0) { @@ -60,121 +60,121 @@ global.i64_pow = function(left, right) { return result; }; -global.i64_div = function(left, right) { +global.i64_div = function i64_div(left, right) { return left.div(right); }; -global.i64_div_u = function(left, right) { +global.i64_div_u = function i64_div_u(left, right) { return left.toUnsigned().div(right.toUnsigned()).toSigned(); }; -global.i64_rem = function(left, right) { +global.i64_rem = function i64_rem(left, right) { return left.mod(right); }; -global.i64_rem_u = function(left, right) { +global.i64_rem_u = function i64_rem_u(left, right) { return left.toUnsigned().mod(right.toUnsigned()).toSigned(); }; -global.i64_and = function(left, right) { +global.i64_and = function i64_and(left, right) { return left.and(right); }; -global.i64_or = function(left, right) { +global.i64_or = function i64_or(left, right) { return left.or(right); }; -global.i64_xor = function(left, right) { +global.i64_xor = function i64_xor(left, right) { return left.xor(right); }; -global.i64_shl = function(left, right) { +global.i64_shl = function i64_shl(left, right) { return left.shl(right); }; -global.i64_shr = function(left, right) { +global.i64_shr = function i64_shr(left, right) { return left.shr(right); }; -global.i64_shr_u = function(left, right) { +global.i64_shr_u = function i64_shr_u(left, right) { return left.shru(right); }; -global.i64_not = function(value) { +global.i64_not = function i64_not(value) { return value.not(); }; -global.i64_eq = function(left, right) { +global.i64_eq = function i64_eq(left, right) { return left.eq(right); }; -global.i64_ne = function(left, right) { +global.i64_ne = function i64_ne(left, right) { return left.ne(right); }; -global.i64_gt = function(left, right) { +global.i64_gt = function i64_gt(left, right) { return left.gt(right); }; -global.i64_align = function(value, alignment) { +global.i64_align = function i64_align(value, alignment) { assert(alignment && (alignment & (alignment - 1)) == 0); var mask = Long.fromInt(alignment - 1); return value.add(mask).and(mask.not()); }; -global.i64_is_i8 = function(value) { +global.i64_is_i8 = function i64_is_i8(value) { return value.high === 0 && (value.low >= 0 && value.low <= i8.MAX_VALUE) || value.high === -1 && (value.low >= i8.MIN_VALUE && value.low < 0); }; -global.i64_is_i16 = function(value) { +global.i64_is_i16 = function i64_is_i16(value) { return value.high === 0 && (value.low >= 0 && value.low <= i16.MAX_VALUE) || value.high === -1 && (value.low >= i16.MIN_VALUE && value.low < 0); }; -global.i64_is_i32 = function(value) { +global.i64_is_i32 = function i64_is_i32(value) { return (value.high === 0 && value.low >= 0) || (value.high === -1 && value.low < 0); }; -global.i64_is_u8 = function(value) { - return value.high === 0 && value.low >= 0 && value.low <= u8.MAX_VALUE; +global.i64_is_u8 = function i64_is_u8(value) { + return value.high === 0 && (value.low >>> 0) <= u8.MAX_VALUE; }; -global.i64_is_u16 = function(value) { - return value.high === 0 && value.low >= 0 && value.low <= u16.MAX_VALUE; +global.i64_is_u16 = function i64_is_u16(value) { + return value.high === 0 && (value.low >>> 0) <= u16.MAX_VALUE; }; -global.i64_is_u32 = function(value) { +global.i64_is_u32 = function i64_is_u32(value) { return value.high === 0; }; -global.i64_is_bool = function(value) { - return value.high === 0 && (value.low === 0 || value.low === 1); +global.i64_is_bool = function i64_is_bool(value) { + return (value.high | (value.low & ~1)) === 0; }; const minSafeF32 = Long.fromNumber(f32.MIN_SAFE_INTEGER); const maxSafeF32 = Long.fromNumber(f32.MAX_SAFE_INTEGER); -global.i64_is_f32 = function(value) { +global.i64_is_f32 = function i64_is_f32(value) { return value.gte(minSafeF32) && value.lte(maxSafeF32); }; const minSafeF64 = Long.fromNumber(f64.MIN_SAFE_INTEGER); const maxSafeF64 = Long.fromNumber(f64.MAX_SAFE_INTEGER); -global.i64_is_f64 = function(value) { +global.i64_is_f64 = function i64_is_f64(value) { return value.gte(minSafeF64) && value.lte(maxSafeF64); }; -global.i64_to_f32 = function(value) { +global.i64_to_f32 = function i64_to_f32(value) { return global.Math.fround(value.toNumber()); }; -global.i64_to_f64 = function(value) { +global.i64_to_f64 = function i64_to_f64(value) { return value.toNumber(); }; -global.i64_to_string = function(value, unsigned) { - return (unsigned ? value.toUnsigned() : value).toString(); +global.i64_to_string = function i64_to_string(value, unsigned) { + return unsigned ? value.toUnsigned().toString() : value.toString(); }; diff --git a/src/glue/wasm/collections.ts b/src/glue/wasm/collections.ts index 11fe30208e..9862fc13fc 100644 --- a/src/glue/wasm/collections.ts +++ b/src/glue/wasm/collections.ts @@ -6,19 +6,19 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ // @ts-ignore: decorator -@global +@global @inline function Map_keys(map: Map): K[] { return map.keys(); // preliminary } // @ts-ignore: decorator -@global +@global @inline function Map_values(map: Map): V[] { return map.values(); // preliminary } // @ts-ignore: decorator -@global +@global @inline function Set_values(set: Set): V[] { return set.values(); // preliminary } diff --git a/src/glue/wasm/float.ts b/src/glue/wasm/float.ts index ad852e04fe..8df450b509 100644 --- a/src/glue/wasm/float.ts +++ b/src/glue/wasm/float.ts @@ -6,25 +6,25 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ // @ts-ignore: decorator -@global +@global @inline function f32_as_i32(value: f32): i32 { return reinterpret(value); } // @ts-ignore: decorator -@global +@global @inline function i32_as_f32(value: i32): f32 { return reinterpret(value); } // @ts-ignore: decorator -@global +@global @inline function f64_as_i64(value: f64): i64 { return reinterpret(value); } // @ts-ignore: decorator -@global +@global @inline function i64_as_f64(value: i64): f64 { return reinterpret(value); } diff --git a/src/glue/wasm/i64.ts b/src/glue/wasm/i64.ts index ff69d03c18..852060ddf5 100644 --- a/src/glue/wasm/i64.ts +++ b/src/glue/wasm/i64.ts @@ -14,37 +14,37 @@ @global const i64_neg_one: i64 = -1; // @ts-ignore: decorator -@global +@global @inline function i64_new(lo: i32, hi: i32 = 0): i64 { return lo | (hi << 32); } // @ts-ignore: decorator -@global +@global @inline function i64_low(value: i64): i32 { return value; } // @ts-ignore: decorator -@global +@global @inline function i64_high(value: i64): i32 { return (value >>> 32); } // @ts-ignore: decorator -@global +@global @inline function i64_add(left: i64, right: i64): i64 { return left + right; } // @ts-ignore: decorator -@global +@global @inline function i64_sub(left: i64, right: i64): i64 { return left - right; } // @ts-ignore: decorator -@global +@global @inline function i64_mul(left: i64, right: i64): i64 { return left * right; } @@ -68,91 +68,91 @@ function i64_pow(left: i64, right: i64): i64 { } // @ts-ignore: decorator -@global +@global @inline function i64_div(left: i64, right: i64): i64 { return left / right; } // @ts-ignore: decorator -@global +@global @inline function i64_div_u(left: i64, right: i64): i64 { return left / right; } // @ts-ignore: decorator -@global +@global @inline function i64_rem(left: i64, right: i64): i64 { return left % right; } // @ts-ignore: decorator -@global +@global @inline function i64_rem_u(left: i64, right: i64): i64 { return left % right; } // @ts-ignore: decorator -@global +@global @inline function i64_and(left: i64, right: i64): i64 { return left & right; } // @ts-ignore: decorator -@global +@global @inline function i64_or(left: i64, right: i64): i64 { return left | right; } // @ts-ignore: decorator -@global +@global @inline function i64_xor(left: i64, right: i64): i64 { return left ^ right; } // @ts-ignore: decorator -@global +@global @inline function i64_shl(left: i64, right: i64): i64 { return left << right; } // @ts-ignore: decorator -@global +@global @inline function i64_shr(left: i64, right: i64): i64 { return left >> right; } // @ts-ignore: decorator -@global +@global @inline function i64_shr_u(left: i64, right: i64): i64 { return left >>> right; } // @ts-ignore: decorator -@global +@global @inline function i64_not(value: i64): i64 { return ~value; } // @ts-ignore: decorator -@global +@global @inline function i64_eq(left: i64, right: i64): bool { return left == right; } // @ts-ignore: decorator -@global +@global @inline function i64_ne(left: i64, right: i64): bool { return left != right; } // @ts-ignore: decorator -@global +@global @inline function i64_gt(left: i64, right: i64): bool { return left > right; } // @ts-ignore: decorator -@global +@global @inline function i64_align(value: i64, alignment: i64): i64 { var mask: i64 = alignment - 1; assert(alignment && (alignment & mask) == 0); @@ -160,84 +160,73 @@ function i64_align(value: i64, alignment: i64): i64 { } // @ts-ignore: decorator -@global +@global @inline function i64_is_i8(value: i64): bool { return value >= i8.MIN_VALUE && value <= i8.MAX_VALUE; } // @ts-ignore: decorator -@global +@global @inline function i64_is_i16(value: i64): bool { return value >= i16.MIN_VALUE && value <= i16.MAX_VALUE; } // @ts-ignore: decorator -@global +@global @inline function i64_is_i32(value: i64): bool { return value >= i32.MIN_VALUE && value <= i32.MAX_VALUE; } // @ts-ignore: decorator -@global +@global @inline function i64_is_u8(value: i64): bool { - return value >= 0 && value <= u8.MAX_VALUE; + return value <= u8.MAX_VALUE; } // @ts-ignore: decorator -@global +@global @inline function i64_is_u16(value: i64): bool { - return value >= 0 && value <= u16.MAX_VALUE; + return value <= u16.MAX_VALUE; } // @ts-ignore: decorator -@global +@global @inline function i64_is_u32(value: i64): bool { - return value >= 0 && value <= u32.MAX_VALUE; + return value <= u32.MAX_VALUE; } // @ts-ignore: decorator -@global +@global @inline function i64_is_bool(value: i64): bool { - return value === 0 || value === 1; + return (value & ~1) == 0; } // @ts-ignore: decorator -@global +@global @inline function i64_is_f32(value: i64): bool { return value >= f32.MIN_SAFE_INTEGER && value <= f32.MAX_SAFE_INTEGER; } // @ts-ignore: decorator -@global +@global @inline function i64_is_f64(value: i64): bool { return value >= f64.MIN_SAFE_INTEGER && value <= f64.MAX_SAFE_INTEGER; } // @ts-ignore: decorator -@global +@global @inline function i64_to_f32(value: i64): f32 { return value; } // @ts-ignore: decorator -@global +@global @inline function i64_to_f64(value: i64): f64 { return value; } -import { CharCode } from "../../util"; - // @ts-ignore: decorator -@global +@global @inline function i64_to_string(value: i64, unsigned: bool = false): string { - var chars = new Array(); - if (!unsigned && value < 0) { - chars.push(CharCode.MINUS); - value = -value; - } - do { - chars.push(CharCode._0 + (value % 10)); - value = value / 10; - } while (value); - return String.fromCharCodes(chars); + return unsigned ? u64(value).toString() : value.toString(); } diff --git a/src/module.ts b/src/module.ts index 99c1f504ca..873516ad92 100644 --- a/src/module.ts +++ b/src/module.ts @@ -552,7 +552,9 @@ export class Module { v128(bytes: Uint8Array): ExpressionRef { assert(bytes.length == 16); var out = this.lit; - for (let i = 0; i < 16; ++i) binaryen.__i32_store8(out + i, bytes[i]); + for (let i = 0; i < 16; ++i) { + binaryen.__i32_store8(out + i, bytes[i]); + } binaryen._BinaryenLiteralVec128(out, out); return binaryen._BinaryenConst(this.ref, out); } @@ -585,7 +587,9 @@ export class Module { ): ExpressionRef { var cStr = this.allocStringCached(name); var cArr = allocPtrArray(operands); - var ret = binaryen._BinaryenHost(this.ref, op, cStr, cArr, operands ? (operands).length : 0); + var ret = binaryen._BinaryenHost( + this.ref, op, cStr, cArr, operands ? (operands).length : 0 + ); binaryen._free(cArr); return ret; } @@ -845,8 +849,12 @@ export class Module { var cStr = this.allocStringCached(target); var cArr = allocPtrArray(operands); var ret = isReturn - ? binaryen._BinaryenReturnCall(this.ref, cStr, cArr, operands ? operands.length : 0, returnType) - : binaryen._BinaryenCall(this.ref, cStr, cArr, operands ? operands.length : 0, returnType); + ? binaryen._BinaryenReturnCall( + this.ref, cStr, cArr, operands ? operands.length : 0, returnType + ) + : binaryen._BinaryenCall( + this.ref, cStr, cArr, operands ? operands.length : 0, returnType + ); binaryen._free(cArr); return ret; } @@ -868,8 +876,12 @@ export class Module { ): ExpressionRef { var cArr = allocPtrArray(operands); var ret = isReturn - ? binaryen._BinaryenReturnCallIndirect(this.ref, index, cArr, operands ? operands.length : 0, params, results) - : binaryen._BinaryenCallIndirect(this.ref, index, cArr, operands ? operands.length : 0, params, results); + ? binaryen._BinaryenReturnCallIndirect( + this.ref, index, cArr, operands ? operands.length : 0, params, results + ) + : binaryen._BinaryenCallIndirect( + this.ref, index, cArr, operands ? operands.length : 0, params, results + ); binaryen._free(cArr); return ret; } @@ -1095,7 +1107,15 @@ export class Module { ): FunctionRef { var cStr = this.allocStringCached(name); var cArr = allocPtrArray(varTypes); - var ret = binaryen._BinaryenAddFunction(this.ref, cStr, params, results, cArr, varTypes ? varTypes.length : 0, body); + var ret = binaryen._BinaryenAddFunction( + this.ref, + cStr, + params, + results, + cArr, + varTypes ? varTypes.length : 0, + body + ); binaryen._free(cArr); return ret; } @@ -1114,7 +1134,11 @@ export class Module { private hasTemporaryFunction: bool = false; - addTemporaryFunction(result: NativeType, paramTypes: NativeType[] | null, body: ExpressionRef): FunctionRef { + addTemporaryFunction( + result: NativeType, + paramTypes: NativeType[] | null, + body: ExpressionRef + ): FunctionRef { this.hasTemporaryFunction = assert(!this.hasTemporaryFunction); var tempName = this.allocStringCached(""); var cArr = allocPtrArray(paramTypes); @@ -1253,7 +1277,9 @@ export class Module { var cStr1 = this.allocStringCached(internalName); var cStr2 = this.allocStringCached(externalModuleName); var cStr3 = this.allocStringCached(externalBaseName); - binaryen._BinaryenAddEventImport(this.ref, cStr1, cStr2, cStr3, attribute, params, results); + binaryen._BinaryenAddEventImport( + this.ref, cStr1, cStr2, cStr3, attribute, params, results + ); } // memory @@ -1276,8 +1302,9 @@ export class Module { var offs = new Array(k); var sizs = new Array(k); for (let i = 0; i < k; ++i) { - let buffer = segments[i].buffer; - let offset = segments[i].offset; + let segment = segments[i]; + let buffer = segment.buffer; + let offset = segment.offset; segs[i] = allocU8Array(buffer); psvs[i] = 0; // no passive segments currently offs[i] = target == Target.WASM64 @@ -1289,12 +1316,16 @@ export class Module { var cArr2 = allocU8Array(psvs); var cArr3 = allocPtrArray(offs); var cArr4 = allocU32Array(sizs); - binaryen._BinaryenSetMemory(this.ref, initial, maximum, cStr, cArr1, cArr2, cArr3, cArr4, k, shared); + binaryen._BinaryenSetMemory( + this.ref, initial, maximum, cStr, cArr1, cArr2, cArr3, cArr4, k, shared + ); binaryen._free(cArr4); binaryen._free(cArr3); binaryen._free(cArr2); binaryen._free(cArr1); - for (let i = k - 1; i >= 0; --i) binaryen._free(segs[i]); + for (let i = k - 1; i >= 0; --i) { + binaryen._free(segs[i]); + } } // table @@ -1314,7 +1345,9 @@ export class Module { names[i] = this.allocStringCached(funcs[i]); } var cArr = allocPtrArray(names); - binaryen._BinaryenSetFunctionTable(this.ref, initial, maximum, cArr, numNames, offset); + binaryen._BinaryenSetFunctionTable( + this.ref, initial, maximum, cArr, numNames, offset + ); binaryen._free(cArr); } @@ -1731,10 +1764,14 @@ export class Module { case ExpressionId.GlobalGet: { let globalName = binaryen._BinaryenGlobalGetGetName(expr); if (!globalName) break; - return binaryen._BinaryenGlobalGet(this.ref, globalName, binaryen._BinaryenExpressionGetType(expr)); + return binaryen._BinaryenGlobalGet( + this.ref, globalName, binaryen._BinaryenExpressionGetType(expr) + ); } case ExpressionId.Load: { - if (!(nested1 = this.cloneExpression(binaryen._BinaryenLoadGetPtr(expr), noSideEffects, maxDepth))) { + if (!(nested1 = this.cloneExpression( + binaryen._BinaryenLoadGetPtr(expr), noSideEffects, maxDepth) + )) { break; } return ( @@ -1756,19 +1793,29 @@ export class Module { ); } case ExpressionId.Unary: { - if (!(nested1 = this.cloneExpression(binaryen._BinaryenUnaryGetValue(expr), noSideEffects, maxDepth))) { + if (!(nested1 = this.cloneExpression( + binaryen._BinaryenUnaryGetValue(expr), noSideEffects, maxDepth) + )) { break; } - return binaryen._BinaryenUnary(this.ref, binaryen._BinaryenUnaryGetOp(expr), nested1); + return binaryen._BinaryenUnary( + this.ref, binaryen._BinaryenUnaryGetOp(expr), nested1 + ); } case ExpressionId.Binary: { - if (!(nested1 = this.cloneExpression(binaryen._BinaryenBinaryGetLeft(expr), noSideEffects, maxDepth))) { + if (!(nested1 = this.cloneExpression( + binaryen._BinaryenBinaryGetLeft(expr), noSideEffects, maxDepth) + )) { break; } - if (!(nested2 = this.cloneExpression(binaryen._BinaryenBinaryGetRight(expr), noSideEffects, maxDepth))) { + if (!(nested2 = this.cloneExpression( + binaryen._BinaryenBinaryGetRight(expr), noSideEffects, maxDepth) + )) { break; } - return binaryen._BinaryenBinary(this.ref, binaryen._BinaryenBinaryGetOp(expr), nested1, nested2); + return binaryen._BinaryenBinary( + this.ref, binaryen._BinaryenBinaryGetOp(expr), nested1, nested2 + ); } } return 0; @@ -1778,7 +1825,12 @@ export class Module { return binaryen._BinaryenExpressionCopy(expr, this.ref); } - runExpression(expr: ExpressionRef, flags: ExpressionRunnerFlags, maxDepth: i32 = 50, maxLoopIterations: i32 = 1): ExpressionRef { + runExpression( + expr: ExpressionRef, + flags: ExpressionRunnerFlags, + maxDepth: i32 = 50, + maxLoopIterations: i32 = 1 + ): ExpressionRef { var runner = binaryen._ExpressionRunnerCreate(this.ref, flags, maxDepth, maxLoopIterations); var precomp = binaryen._ExpressionRunnerRunAndDispose(runner, expr); if (precomp) { @@ -2256,20 +2308,20 @@ export function hasSideEffects(expr: ExpressionRef, features: FeatureFlags = Fea function allocU8Array(u8s: Uint8Array | null): usize { if (!u8s) return 0; - var numValues = u8s.length; - var ptr = binaryen._malloc(numValues); - var idx = ptr; - for (let i = 0; i < numValues; ++i) { - binaryen.__i32_store8(idx++, u8s[i]); + var len = u8s.length; + var ptr = binaryen._malloc(len); + for (let i = 0; i < len; ++i) { + binaryen.__i32_store8(ptr + i, u8s[i]); } return ptr; } function allocI32Array(i32s: i32[] | null): usize { if (!i32s) return 0; - var ptr = binaryen._malloc(i32s.length << 2); + var len = i32s.length; + var ptr = binaryen._malloc(len << 2); var idx = ptr; - for (let i = 0, k = i32s.length; i < k; ++i) { + for (let i = 0; i < len; ++i) { let val = i32s[i]; binaryen.__i32_store(idx, val); idx += 4; @@ -2279,9 +2331,10 @@ function allocI32Array(i32s: i32[] | null): usize { function allocU32Array(u32s: u32[] | null): usize { if (!u32s) return 0; - var ptr = binaryen._malloc(u32s.length << 2); + var len = u32s.length; + var ptr = binaryen._malloc(len << 2); var idx = ptr; - for (let i = 0, k = u32s.length; i < k; ++i) { + for (let i = 0; i < len; ++i) { let val = u32s[i]; binaryen.__i32_store(idx, val); idx += 4; @@ -2293,9 +2346,10 @@ function allocPtrArray(ptrs: usize[] | null): usize { if (!ptrs) return 0; // TODO: WASM64 assert(ASC_TARGET != Target.WASM64); - var ptr = binaryen._malloc(ptrs.length << 2); + var len = ptrs.length; + var ptr = binaryen._malloc(len << 2); var idx = ptr; - for (let i = 0, k = ptrs.length; i < k; ++i) { + for (let i = 0, k = len; i < k; ++i) { let val = ptrs[i]; binaryen.__i32_store(idx, val); idx += 4; @@ -2316,7 +2370,7 @@ function stringLengthUTF8(str: string): usize { len += 2; } else if (u <= 0xFFFF) { len += 3; - } else if (u <= 0x1FFFFF) { + } else { len += 4; } } @@ -2413,16 +2467,23 @@ export class BinaryModule { /** Tests if an expression needs an explicit 'unreachable' when it is the terminating statement. */ export function needsExplicitUnreachable(expr: ExpressionRef): bool { // not applicable if pushing a value to the stack - if (binaryen._BinaryenExpressionGetType(expr) != NativeType.Unreachable) return false; + if (binaryen._BinaryenExpressionGetType(expr) != NativeType.Unreachable) { + return false; + } switch (binaryen._BinaryenExpressionGetId(expr)) { case ExpressionId.Unreachable: case ExpressionId.Return: return false; - case ExpressionId.Break: return binaryen._BinaryenBreakGetCondition(expr) != 0; + case ExpressionId.Break: { + return binaryen._BinaryenBreakGetCondition(expr) != 0; + } case ExpressionId.Block: { if (!binaryen._BinaryenBlockGetName(expr)) { // can't break out of it let numChildren = binaryen._BinaryenBlockGetNumChildren(expr); // last child needs unreachable - return numChildren > 0 && needsExplicitUnreachable(binaryen._BinaryenBlockGetChild(expr, numChildren - 1)); + return ( + numChildren > 0 && + needsExplicitUnreachable(binaryen._BinaryenBlockGetChild(expr, numChildren - 1)) + ); } } } @@ -2430,7 +2491,11 @@ export function needsExplicitUnreachable(expr: ExpressionRef): bool { } /** Traverses all expression members of an expression, calling the given visitor. */ -export function traverse(expr: ExpressionRef, data: T, visit: (expr: ExpressionRef, data: T) => void): bool { +export function traverse( + expr: ExpressionRef, + data: T, + visit: (expr: ExpressionRef, data: T) => void +): bool { switch (getExpressionId(expr)) { case ExpressionId.Block: { for (let i: Index = 0, n = binaryen._BinaryenBlockGetNumChildren(expr); i < n; ++i) {