Skip to content

Commit 2ec7133

Browse files
authored
chore: Modernize loader (#1279)
1 parent f67a843 commit 2ec7133

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

Diff for: lib/loader/index.js

+27-26
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ const CHUNKSIZE = 1024;
4444
function getStringImpl(buffer, ptr) {
4545
const U32 = new Uint32Array(buffer);
4646
const U16 = new Uint16Array(buffer);
47-
let length = U32[(ptr + SIZE_OFFSET) >>> 2] >>> 1;
47+
let length = U32[ptr + SIZE_OFFSET >>> 2] >>> 1;
4848
let offset = ptr >>> 1;
4949
if (length <= CHUNKSIZE) return String.fromCharCode.apply(String, U16.subarray(offset, offset + length));
50-
const parts = [];
50+
let parts = '';
5151
do {
5252
const last = U16[offset + CHUNKSIZE - 1];
5353
const size = last >= 0xD800 && last < 0xDC00 ? CHUNKSIZE - 1 : CHUNKSIZE;
54-
parts.push(String.fromCharCode.apply(String, U16.subarray(offset, offset += size)));
54+
parts += String.fromCharCode.apply(String, U16.subarray(offset, offset += size));
5555
length -= size;
5656
} while (length > CHUNKSIZE);
57-
return parts.join("") + String.fromCharCode.apply(String, U16.subarray(offset, offset + length));
57+
return parts + String.fromCharCode.apply(String, U16.subarray(offset, offset + length));
5858
}
5959

6060
/** Prepares the base module prior to instantiation. */
@@ -70,15 +70,13 @@ function preInstantiate(imports) {
7070
const env = (imports.env = imports.env || {});
7171
env.abort = env.abort || function abort(msg, file, line, colm) {
7272
const memory = extendedExports.memory || env.memory; // prefer exported, otherwise try imported
73-
throw Error("abort: " + getString(memory, msg) + " at " + getString(memory, file) + ":" + line + ":" + colm);
73+
throw Error(`abort: ${getString(memory, msg)} at ${getString(memory, file)}:${line}:${colm}`);
7474
};
75-
env.trace = env.trace || function trace(msg, n) {
75+
env.trace = env.trace || function trace(msg, n, ...args) {
7676
const memory = extendedExports.memory || env.memory;
77-
console.log("trace: " + getString(memory, msg) + (n ? " " : "") + Array.prototype.slice.call(arguments, 2, 2 + n).join(", "));
78-
};
79-
env.seed = env.seed || function seed() {
80-
return Date.now();
77+
console.log(`trace: ${getString(memory, msg)}${n ? " " : ""}${args.slice(0, n).join(", ")}`);
8178
};
79+
env.seed = env.seed || Date.now;
8280
imports.Math = imports.Math || Math;
8381
imports.Date = imports.Date || Date;
8482

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

103+
/** Gets and validate runtime type info for the given id for array like objects */
104+
function getArrayInfo(id) {
105+
const info = getInfo(id);
106+
if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error(`not an array: ${id}, flags=${info}`);
107+
return info;
108+
}
109+
105110
/** Gets the runtime base id for the given id. */
106111
function getBase(id) {
107112
const U32 = new Uint32Array(memory.buffer);
108113
const count = U32[rttiBase >>> 2];
109-
if ((id >>>= 0) >= count) throw Error("invalid id: " + id);
114+
if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`);
110115
return U32[(rttiBase + 4 >>> 2) + id * 2 + 1];
111116
}
112117

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

@@ -157,13 +162,12 @@ function postInstantiate(extendedExports, instance) {
157162
case 3: return new (signed ? BigInt64Array : BigUint64Array)(buffer);
158163
}
159164
}
160-
throw Error("unsupported align: " + alignLog2);
165+
throw Error(`unsupported align: ${alignLog2}`);
161166
}
162167

163168
/** Allocates a new array in the module's memory and returns its retained pointer. */
164169
function __allocArray(id, values) {
165-
const info = getInfo(id);
166-
if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error("not an array: " + id + ", flags= " + info);
170+
const info = getArrayInfo(id);
167171
const align = getValueAlign(info);
168172
const length = values.length;
169173
const buf = alloc(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);
@@ -194,8 +198,7 @@ function postInstantiate(extendedExports, instance) {
194198
function __getArrayView(arr) {
195199
const U32 = new Uint32Array(memory.buffer);
196200
const id = U32[arr + ID_OFFSET >>> 2];
197-
const info = getInfo(id);
198-
if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error("not an array: " + id + ", flags=" + info);
201+
const info = getArrayInfo(id);
199202
const align = getValueAlign(info);
200203
let buf = info & STATICARRAY
201204
? arr
@@ -243,8 +246,8 @@ function postInstantiate(extendedExports, instance) {
243246

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

250253
[
@@ -270,7 +273,7 @@ function postInstantiate(extendedExports, instance) {
270273
/** Tests whether an object is an instance of the class represented by the specified base id. */
271274
function __instanceof(ptr, baseId) {
272275
const U32 = new Uint32Array(memory.buffer);
273-
let id = U32[(ptr + ID_OFFSET) >>> 2];
276+
let id = U32[ptr + ID_OFFSET >>> 2];
274277
if (id <= U32[rttiBase >>> 2]) {
275278
do {
276279
if (id == baseId) return true;
@@ -365,9 +368,7 @@ function demangle(exports, extendedExports = {}) {
365368
return ctor.wrap(ctor.prototype.constructor(0, ...args));
366369
};
367370
ctor.prototype = {
368-
valueOf: function valueOf() {
369-
return this[THIS];
370-
}
371+
valueOf() { return this[THIS]; }
371372
};
372373
ctor.wrap = function(thisValue) {
373374
return Object.create(ctor.prototype, { [THIS]: { value: thisValue, writable: false } });
@@ -384,8 +385,8 @@ function demangle(exports, extendedExports = {}) {
384385
let getter = exports[internalName.replace("set:", "get:")];
385386
let setter = exports[internalName.replace("get:", "set:")];
386387
Object.defineProperty(curr, name, {
387-
get: function() { return getter(this[THIS]); },
388-
set: function(value) { setter(this[THIS], value); },
388+
get() { return getter(this[THIS]); },
389+
set(value) { setter(this[THIS], value); },
389390
enumerable: true
390391
});
391392
}

0 commit comments

Comments
 (0)