Skip to content
Merged
Changes from 2 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
30 changes: 18 additions & 12 deletions lib/loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,29 @@ const ARRAY_SIZE = 16;

const BIGINT = typeof BigUint64Array !== "undefined";
const THIS = Symbol();
const CHUNKSIZE = 1024;

const STRING_DECODE_THRESHOLD = 32;

let decoder;
if (typeof process !== 'undefined') {
// node.js
decoder = new (require('util').TextDecoder)('utf-16le');
} else {
// browser
decoder = new TextDecoder('utf-16le');
}

/** Gets a string from an U32 and an U16 view on a memory. */
function getStringImpl(buffer, ptr) {
const U32 = new Uint32Array(buffer);
const U16 = new Uint16Array(buffer);
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));
let parts = '';
do {
const last = U16[offset + CHUNKSIZE - 1];
const size = last >= 0xD800 && last < 0xDC00 ? CHUNKSIZE - 1 : CHUNKSIZE;
parts += String.fromCharCode.apply(String, U16.subarray(offset, offset += size));
length -= size;
} while (length > CHUNKSIZE);
return parts + String.fromCharCode.apply(String, U16.subarray(offset, offset + length));
const length = U32[ptr + SIZE_OFFSET >>> 2] >>> 1;
const offset = ptr >>> 1;
const data = U16.subarray(offset, offset + length);
if (length <= STRING_DECODE_THRESHOLD) {
return String.fromCharCode.apply(String, data);
}
return decoder.decode(data);
}

/** Prepares the base module prior to instantiation. */
Expand Down