Skip to content

Commit

Permalink
chore: Modernize loader (#1279)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGraey authored Jun 13, 2020
1 parent f67a843 commit 2ec7133
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions lib/loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ const CHUNKSIZE = 1024;
function getStringImpl(buffer, ptr) {
const U32 = new Uint32Array(buffer);
const U16 = new Uint16Array(buffer);
let length = U32[(ptr + SIZE_OFFSET) >>> 2] >>> 1;
let length = U32[ptr + SIZE_OFFSET >>> 2] >>> 1;
let offset = ptr >>> 1;
if (length <= CHUNKSIZE) return String.fromCharCode.apply(String, U16.subarray(offset, offset + length));
const parts = [];
let parts = '';
do {
const last = U16[offset + CHUNKSIZE - 1];
const size = last >= 0xD800 && last < 0xDC00 ? CHUNKSIZE - 1 : CHUNKSIZE;
parts.push(String.fromCharCode.apply(String, U16.subarray(offset, offset += size)));
parts += String.fromCharCode.apply(String, U16.subarray(offset, offset += size));
length -= size;
} while (length > CHUNKSIZE);
return parts.join("") + String.fromCharCode.apply(String, U16.subarray(offset, offset + length));
return parts + String.fromCharCode.apply(String, U16.subarray(offset, offset + length));
}

/** Prepares the base module prior to instantiation. */
Expand All @@ -70,15 +70,13 @@ function preInstantiate(imports) {
const env = (imports.env = imports.env || {});
env.abort = env.abort || function abort(msg, file, line, colm) {
const memory = extendedExports.memory || env.memory; // prefer exported, otherwise try imported
throw Error("abort: " + getString(memory, msg) + " at " + getString(memory, file) + ":" + line + ":" + colm);
throw Error(`abort: ${getString(memory, msg)} at ${getString(memory, file)}:${line}:${colm}`);
};
env.trace = env.trace || function trace(msg, n) {
env.trace = env.trace || function trace(msg, n, ...args) {
const memory = extendedExports.memory || env.memory;
console.log("trace: " + getString(memory, msg) + (n ? " " : "") + Array.prototype.slice.call(arguments, 2, 2 + n).join(", "));
};
env.seed = env.seed || function seed() {
return Date.now();
console.log(`trace: ${getString(memory, msg)}${n ? " " : ""}${args.slice(0, n).join(", ")}`);
};
env.seed = env.seed || Date.now;
imports.Math = imports.Math || Math;
imports.Date = imports.Date || Date;

Expand All @@ -98,15 +96,22 @@ function postInstantiate(extendedExports, instance) {
function getInfo(id) {
const U32 = new Uint32Array(memory.buffer);
const count = U32[rttiBase >>> 2];
if ((id >>>= 0) >= count) throw Error("invalid id: " + id);
if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`);
return U32[(rttiBase + 4 >>> 2) + id * 2];
}

/** Gets and validate runtime type info for the given id for array like objects */
function getArrayInfo(id) {
const info = getInfo(id);
if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error(`not an array: ${id}, flags=${info}`);
return info;
}

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

Expand Down Expand Up @@ -135,7 +140,7 @@ function postInstantiate(extendedExports, instance) {
function __getString(ptr) {
const buffer = memory.buffer;
const id = new Uint32Array(buffer)[ptr + ID_OFFSET >>> 2];
if (id !== STRING_ID) throw Error("not a string: " + ptr);
if (id !== STRING_ID) throw Error(`not a string: ${ptr}`);
return getStringImpl(buffer, ptr);
}

Expand All @@ -157,13 +162,12 @@ function postInstantiate(extendedExports, instance) {
case 3: return new (signed ? BigInt64Array : BigUint64Array)(buffer);
}
}
throw Error("unsupported align: " + alignLog2);
throw Error(`unsupported align: ${alignLog2}`);
}

/** Allocates a new array in the module's memory and returns its retained pointer. */
function __allocArray(id, values) {
const info = getInfo(id);
if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error("not an array: " + id + ", flags= " + info);
const info = getArrayInfo(id);
const align = getValueAlign(info);
const length = values.length;
const buf = alloc(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);
Expand Down Expand Up @@ -194,8 +198,7 @@ function postInstantiate(extendedExports, instance) {
function __getArrayView(arr) {
const U32 = new Uint32Array(memory.buffer);
const id = U32[arr + ID_OFFSET >>> 2];
const info = getInfo(id);
if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error("not an array: " + id + ", flags=" + info);
const info = getArrayInfo(id);
const align = getValueAlign(info);
let buf = info & STATICARRAY
? arr
Expand Down Expand Up @@ -243,8 +246,8 @@ function postInstantiate(extendedExports, instance) {

/** Attach a set of get TypedArray and View functions to the exports. */
function attachTypedArrayFunctions(ctor, name, align) {
extendedExports["__get" + name] = getTypedArray.bind(null, ctor, align);
extendedExports["__get" + name + "View"] = getTypedArrayView.bind(null, ctor, align);
extendedExports[`__get${name}`] = getTypedArray.bind(null, ctor, align);
extendedExports[`__get${name}View`] = getTypedArrayView.bind(null, ctor, align);
}

[
Expand All @@ -270,7 +273,7 @@ function postInstantiate(extendedExports, instance) {
/** Tests whether an object is an instance of the class represented by the specified base id. */
function __instanceof(ptr, baseId) {
const U32 = new Uint32Array(memory.buffer);
let id = U32[(ptr + ID_OFFSET) >>> 2];
let id = U32[ptr + ID_OFFSET >>> 2];
if (id <= U32[rttiBase >>> 2]) {
do {
if (id == baseId) return true;
Expand Down Expand Up @@ -365,9 +368,7 @@ function demangle(exports, extendedExports = {}) {
return ctor.wrap(ctor.prototype.constructor(0, ...args));
};
ctor.prototype = {
valueOf: function valueOf() {
return this[THIS];
}
valueOf() { return this[THIS]; }
};
ctor.wrap = function(thisValue) {
return Object.create(ctor.prototype, { [THIS]: { value: thisValue, writable: false } });
Expand All @@ -384,8 +385,8 @@ function demangle(exports, extendedExports = {}) {
let getter = exports[internalName.replace("set:", "get:")];
let setter = exports[internalName.replace("get:", "set:")];
Object.defineProperty(curr, name, {
get: function() { return getter(this[THIS]); },
set: function(value) { setter(this[THIS], value); },
get() { return getter(this[THIS]); },
set(value) { setter(this[THIS], value); },
enumerable: true
});
}
Expand Down

0 comments on commit 2ec7133

Please sign in to comment.