@assemblyscript/loader
Advanced tools
Comparing version 0.9.4-nightly.20200314 to 0.9.4-nightly.20200315
/// <reference lib="esnext.bigint" /> | ||
interface ResultObject { | ||
module: WebAssembly.Module, | ||
instance: WebAssembly.Instance | ||
}; | ||
/** WebAssembly imports with two levels of nesting. */ | ||
@@ -95,11 +100,20 @@ interface ImportsObject extends Record<string, any> { | ||
/** Asynchronously instantiates an AssemblyScript module from anything that can be instantiated. */ | ||
export declare function instantiate<T extends {}>(source: WebAssembly.Module | BufferSource | Response | PromiseLike<WebAssembly.Module | BufferSource | Response>, imports?: ImportsObject): Promise<ASUtil & T>; | ||
export declare function instantiate<T extends {}>( | ||
source: WebAssembly.Module | BufferSource | Response | PromiseLike<WebAssembly.Module | BufferSource | Response>, | ||
imports?: ImportsObject | ||
): Promise<ResultObject & { exports: ASUtil & T }>; | ||
/** Synchronously instantiates an AssemblyScript module from a WebAssembly.Module or binary buffer. */ | ||
export declare function instantiateSync<T extends {}>(source: WebAssembly.Module | BufferSource, imports?: ImportsObject): ASUtil & T; | ||
export declare function instantiateSync<T extends {}>( | ||
source: WebAssembly.Module | BufferSource, | ||
imports?: ImportsObject | ||
): ResultObject & { exports: ASUtil & T }; | ||
/** Asynchronously instantiates an AssemblyScript module from a response, i.e. as obtained by `fetch`. */ | ||
export declare function instantiateStreaming<T extends {}>(source: Response | PromiseLike<Response>, imports?: ImportsObject): Promise<ASUtil & T>; | ||
export declare function instantiateStreaming<T extends {}>( | ||
source: Response | PromiseLike<Response>, | ||
imports?: ImportsObject | ||
): Promise<ResultObject & { exports: ASUtil & T }>; | ||
/** Demangles an AssemblyScript module's exports to a friendly object structure. */ | ||
export declare function demangle<T extends {}>(exports: {}, baseModule?: {}): T; | ||
export declare function demangle<T extends {}>(exports: {}, extendedExports?: {}): T; |
178
index.js
@@ -46,4 +46,4 @@ "use strict"; | ||
const U16 = new Uint16Array(buffer); | ||
var length = U32[(ptr + SIZE_OFFSET) >>> 2] >>> 1; | ||
var offset = ptr >>> 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)); | ||
@@ -62,3 +62,3 @@ const parts = []; | ||
function preInstantiate(imports) { | ||
const baseModule = {}; | ||
const extendedExports = {}; | ||
@@ -72,9 +72,9 @@ function getString(memory, ptr) { | ||
const env = (imports.env = imports.env || {}); | ||
env.abort = env.abort || function abort(mesg, file, line, colm) { | ||
const memory = baseModule.memory || env.memory; // prefer exported, otherwise try imported | ||
throw Error("abort: " + getString(memory, mesg) + " in " + getString(memory, file) + "(" + line + ":" + colm + ")"); | ||
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); | ||
}; | ||
env.trace = env.trace || function trace(mesg, n) { | ||
const memory = baseModule.memory || env.memory; | ||
console.log("trace: " + getString(memory, mesg) + (n ? " " : "") + Array.prototype.slice.call(arguments, 2, 2 + n).join(", ")); | ||
env.trace = env.trace || function trace(msg, n) { | ||
const memory = extendedExports.memory || env.memory; | ||
console.log("trace: " + getString(memory, msg) + (n ? " " : "") + Array.prototype.slice.call(arguments, 2, 2 + n).join(", ")); | ||
}; | ||
@@ -87,13 +87,13 @@ env.seed = env.seed || function seed() { | ||
return baseModule; | ||
return extendedExports; | ||
} | ||
/** Prepares the final module once instantiation is complete. */ | ||
function postInstantiate(baseModule, instance) { | ||
const rawExports = instance.exports; | ||
const memory = rawExports.memory; | ||
const table = rawExports.table; | ||
const alloc = rawExports["__alloc"]; | ||
const retain = rawExports["__retain"]; | ||
const rttiBase = rawExports["__rtti_base"] || ~0; // oob if not present | ||
function postInstantiate(extendedExports, instance) { | ||
const exports = instance.exports; | ||
const memory = exports.memory; | ||
const table = exports.table; | ||
const alloc = exports["__alloc"]; | ||
const retain = exports["__retain"]; | ||
const rttiBase = exports["__rtti_base"] || ~0; // oob if not present | ||
@@ -135,3 +135,3 @@ /** Gets the runtime type info for the given id. */ | ||
baseModule.__allocString = __allocString; | ||
extendedExports.__allocString = __allocString; | ||
@@ -146,3 +146,3 @@ /** Reads a string from the module's memory by its pointer. */ | ||
baseModule.__getString = __getString; | ||
extendedExports.__getString = __getString; | ||
@@ -171,3 +171,3 @@ /** Gets the view matching the specified alignment, signedness and floatness. */ | ||
const info = getInfo(id); | ||
if (!(info & (ARRAYBUFFERVIEW | ARRAY))) throw Error("not an array: " + id + " @ " + info); | ||
if (!(info & (ARRAYBUFFERVIEW | ARRAY))) throw Error("not an array: " + id + ", flags= " + info); | ||
const align = getValueAlign(info); | ||
@@ -191,3 +191,3 @@ const length = values.length; | ||
baseModule.__allocArray = __allocArray; | ||
extendedExports.__allocArray = __allocArray; | ||
@@ -199,5 +199,5 @@ /** Gets a live view on an array's values in the module's memory. Infers the array type from RTTI. */ | ||
const info = getInfo(id); | ||
if (!(info & ARRAYBUFFERVIEW)) throw Error("not an array: " + id); | ||
if (!(info & (ARRAYBUFFERVIEW | ARRAY))) throw Error("not an array: " + id + ", flags=" + info); | ||
const align = getValueAlign(info); | ||
var buf = U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2]; | ||
let buf = U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2]; | ||
const length = info & ARRAY | ||
@@ -210,3 +210,3 @@ ? U32[arr + ARRAY_LENGTH_OFFSET >>> 2] | ||
baseModule.__getArrayView = __getArrayView; | ||
extendedExports.__getArrayView = __getArrayView; | ||
@@ -222,3 +222,3 @@ /** Copies an array's values from the module's memory. Infers the array type from RTTI. */ | ||
baseModule.__getArray = __getArray; | ||
extendedExports.__getArray = __getArray; | ||
@@ -232,3 +232,3 @@ /** Copies an ArrayBuffer's value from the module's memory. */ | ||
baseModule.__getArrayBuffer = __getArrayBuffer; | ||
extendedExports.__getArrayBuffer = __getArrayBuffer; | ||
@@ -248,26 +248,26 @@ /** Copies a typed array's values from the module's memory. */ | ||
baseModule.__getInt8Array = getTypedArray.bind(null, Int8Array, 0); | ||
baseModule.__getInt8ArrayView = getTypedArrayView.bind(null, Int8Array, 0); | ||
baseModule.__getUint8Array = getTypedArray.bind(null, Uint8Array, 0); | ||
baseModule.__getUint8ArrayView = getTypedArrayView.bind(null, Uint8Array, 0); | ||
baseModule.__getUint8ClampedArray = getTypedArray.bind(null, Uint8ClampedArray, 0); | ||
baseModule.__getUint8ClampedArrayView = getTypedArrayView.bind(null, Uint8ClampedArray, 0); | ||
baseModule.__getInt16Array = getTypedArray.bind(null, Int16Array, 1); | ||
baseModule.__getInt16ArrayView = getTypedArrayView.bind(null, Int16Array, 1); | ||
baseModule.__getUint16Array = getTypedArray.bind(null, Uint16Array, 1); | ||
baseModule.__getUint16ArrayView = getTypedArrayView.bind(null, Uint16Array, 1); | ||
baseModule.__getInt32Array = getTypedArray.bind(null, Int32Array, 2); | ||
baseModule.__getInt32ArrayView = getTypedArrayView.bind(null, Int32Array, 2); | ||
baseModule.__getUint32Array = getTypedArray.bind(null, Uint32Array, 2); | ||
baseModule.__getUint32ArrayView = getTypedArrayView.bind(null, Uint32Array, 2); | ||
extendedExports.__getInt8Array = getTypedArray.bind(null, Int8Array, 0); | ||
extendedExports.__getInt8ArrayView = getTypedArrayView.bind(null, Int8Array, 0); | ||
extendedExports.__getUint8Array = getTypedArray.bind(null, Uint8Array, 0); | ||
extendedExports.__getUint8ArrayView = getTypedArrayView.bind(null, Uint8Array, 0); | ||
extendedExports.__getUint8ClampedArray = getTypedArray.bind(null, Uint8ClampedArray, 0); | ||
extendedExports.__getUint8ClampedArrayView = getTypedArrayView.bind(null, Uint8ClampedArray, 0); | ||
extendedExports.__getInt16Array = getTypedArray.bind(null, Int16Array, 1); | ||
extendedExports.__getInt16ArrayView = getTypedArrayView.bind(null, Int16Array, 1); | ||
extendedExports.__getUint16Array = getTypedArray.bind(null, Uint16Array, 1); | ||
extendedExports.__getUint16ArrayView = getTypedArrayView.bind(null, Uint16Array, 1); | ||
extendedExports.__getInt32Array = getTypedArray.bind(null, Int32Array, 2); | ||
extendedExports.__getInt32ArrayView = getTypedArrayView.bind(null, Int32Array, 2); | ||
extendedExports.__getUint32Array = getTypedArray.bind(null, Uint32Array, 2); | ||
extendedExports.__getUint32ArrayView = getTypedArrayView.bind(null, Uint32Array, 2); | ||
if (BIGINT) { | ||
baseModule.__getInt64Array = getTypedArray.bind(null, BigInt64Array, 3); | ||
baseModule.__getInt64ArrayView = getTypedArrayView.bind(null, BigInt64Array, 3); | ||
baseModule.__getUint64Array = getTypedArray.bind(null, BigUint64Array, 3); | ||
baseModule.__getUint64ArrayView = getTypedArrayView.bind(null, BigUint64Array, 3); | ||
extendedExports.__getInt64Array = getTypedArray.bind(null, BigInt64Array, 3); | ||
extendedExports.__getInt64ArrayView = getTypedArrayView.bind(null, BigInt64Array, 3); | ||
extendedExports.__getUint64Array = getTypedArray.bind(null, BigUint64Array, 3); | ||
extendedExports.__getUint64ArrayView = getTypedArrayView.bind(null, BigUint64Array, 3); | ||
} | ||
baseModule.__getFloat32Array = getTypedArray.bind(null, Float32Array, 2); | ||
baseModule.__getFloat32ArrayView = getTypedArrayView.bind(null, Float32Array, 2); | ||
baseModule.__getFloat64Array = getTypedArray.bind(null, Float64Array, 3); | ||
baseModule.__getFloat64ArrayView = getTypedArrayView.bind(null, Float64Array, 3); | ||
extendedExports.__getFloat32Array = getTypedArray.bind(null, Float32Array, 2); | ||
extendedExports.__getFloat32ArrayView = getTypedArrayView.bind(null, Float32Array, 2); | ||
extendedExports.__getFloat64Array = getTypedArray.bind(null, Float64Array, 3); | ||
extendedExports.__getFloat64ArrayView = getTypedArrayView.bind(null, Float64Array, 3); | ||
@@ -277,3 +277,3 @@ /** Tests whether an object is an instance of the class represented by the specified base id. */ | ||
const U32 = new Uint32Array(memory.buffer); | ||
var id = U32[(ptr + ID_OFFSET) >>> 2]; | ||
let id = U32[(ptr + ID_OFFSET) >>> 2]; | ||
if (id <= U32[rttiBase >>> 2]) { | ||
@@ -286,28 +286,28 @@ do if (id == baseId) return true; | ||
baseModule.__instanceof = __instanceof; | ||
extendedExports.__instanceof = __instanceof; | ||
// Pull basic exports to baseModule so code in preInstantiate can use them | ||
baseModule.memory = baseModule.memory || memory; | ||
baseModule.table = baseModule.table || table; | ||
// Pull basic exports to extendedExports so code in preInstantiate can use them | ||
extendedExports.memory = extendedExports.memory || memory; | ||
extendedExports.table = extendedExports.table || table; | ||
// Demangle exports and provide the usual utility on the prototype | ||
return demangle(rawExports, baseModule); | ||
return demangle(exports, extendedExports); | ||
} | ||
function isResponse(o) { | ||
return typeof Response !== "undefined" && o instanceof Response; | ||
function isResponse(src) { | ||
return typeof Response !== "undefined" && src instanceof Response; | ||
} | ||
function isModule(src) { | ||
return src instanceof WebAssembly.Module; | ||
} | ||
/** Asynchronously instantiates an AssemblyScript module from anything that can be instantiated. */ | ||
async function instantiate(source, imports) { | ||
async function instantiate(source, imports = {}) { | ||
if (isResponse(source = await source)) return instantiateStreaming(source, imports); | ||
return postInstantiate( | ||
preInstantiate(imports || (imports = {})), | ||
await WebAssembly.instantiate( | ||
source instanceof WebAssembly.Module | ||
? source | ||
: await WebAssembly.compile(source), | ||
imports | ||
) | ||
); | ||
const module = isModule(source) ? source : await WebAssembly.compile(source); | ||
const extended = preInstantiate(imports); | ||
const instance = await WebAssembly.instantiate(module, imports); | ||
const exports = postInstantiate(extended, instance); | ||
return { module, instance, exports }; | ||
} | ||
@@ -318,12 +318,8 @@ | ||
/** Synchronously instantiates an AssemblyScript module from a WebAssembly.Module or binary buffer. */ | ||
function instantiateSync(source, imports) { | ||
return postInstantiate( | ||
preInstantiate(imports || (imports = {})), | ||
new WebAssembly.Instance( | ||
source instanceof WebAssembly.Module | ||
? source | ||
: new WebAssembly.Module(source), | ||
imports | ||
) | ||
) | ||
function instantiateSync(source, imports = {}) { | ||
const module = isModule(source) ? source : new WebAssembly.Module(source); | ||
const extended = preInstantiate(imports); | ||
const instance = new WebAssembly.Instance(module, imports); | ||
const exports = postInstantiate(extended, instance); | ||
return { module, instance, exports }; | ||
} | ||
@@ -334,3 +330,3 @@ | ||
/** Asynchronously instantiates an AssemblyScript module from a response, i.e. as obtained by `fetch`. */ | ||
async function instantiateStreaming(source, imports) { | ||
async function instantiateStreaming(source, imports = {}) { | ||
if (!WebAssembly.instantiateStreaming) { | ||
@@ -344,6 +340,6 @@ return instantiate( | ||
} | ||
return postInstantiate( | ||
preInstantiate(imports || (imports = {})), | ||
(await WebAssembly.instantiateStreaming(source, imports)).instance | ||
); | ||
const extended = preInstantiate(imports); | ||
const result = await WebAssembly.instantiateStreaming(source, imports); | ||
const exports = postInstantiate(extended, result.instance); | ||
return { ...result, exports }; | ||
} | ||
@@ -354,7 +350,7 @@ | ||
/** Demangles an AssemblyScript module's exports to a friendly object structure. */ | ||
function demangle(exports, baseModule) { | ||
var module = baseModule ? Object.create(baseModule) : {}; | ||
var setArgumentsLength = exports["__argumentsLength"] | ||
? function(length) { exports["__argumentsLength"].value = length; } | ||
: exports["__setArgumentsLength"] || exports["__setargc"] || function() {}; | ||
function demangle(exports, extendedExports = {}) { | ||
extendedExports = Object.create(extendedExports); | ||
const setArgumentsLength = exports["__argumentsLength"] | ||
? length => { exports["__argumentsLength"].value = length; } | ||
: exports["__setArgumentsLength"] || exports["__setargc"] || (() => {}); | ||
for (let internalName in exports) { | ||
@@ -364,3 +360,3 @@ if (!Object.prototype.hasOwnProperty.call(exports, internalName)) continue; | ||
let parts = internalName.split("."); | ||
let curr = module; | ||
let curr = extendedExports; | ||
while (parts.length > 1) { | ||
@@ -374,6 +370,6 @@ let part = parts.shift(); | ||
if (hash >= 0) { | ||
let className = name.substring(0, hash); | ||
let classElem = curr[className]; | ||
const className = name.substring(0, hash); | ||
const classElem = curr[className]; | ||
if (typeof classElem === "undefined" || !classElem.prototype) { | ||
let ctor = function(...args) { | ||
const ctor = function(...args) { | ||
return ctor.wrap(ctor.prototype.constructor(0, ...args)); | ||
@@ -438,5 +434,5 @@ }; | ||
} | ||
return module; | ||
return extendedExports; | ||
} | ||
exports.demangle = demangle; |
@@ -12,3 +12,3 @@ { | ||
], | ||
"version": "0.9.4-nightly.20200314", | ||
"version": "0.9.4-nightly.20200315", | ||
"author": "Daniel Wirtz <dcode+assemblyscript@dcode.io>", | ||
@@ -15,0 +15,0 @@ "license": "Apache-2.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33859
477