diff --git a/Plugins/PackageToJS/Templates/runtime.d.ts b/Plugins/PackageToJS/Templates/runtime.d.ts index ed94f7e4..353db389 100644 --- a/Plugins/PackageToJS/Templates/runtime.d.ts +++ b/Plugins/PackageToJS/Templates/runtime.d.ts @@ -1,25 +1,15 @@ type ref = number; type pointer = number; -declare class Memory { - readonly rawMemory: WebAssembly.Memory; - private readonly heap; - constructor(exports: WebAssembly.Exports); - retain: (value: any) => number; - getObject: (ref: number) => any; - release: (ref: number) => void; - retainByRef: (ref: number) => number; - bytes: () => Uint8Array; - dataView: () => DataView; - writeBytes: (ptr: pointer, bytes: Uint8Array) => void; - readUint32: (ptr: pointer) => number; - readUint64: (ptr: pointer) => bigint; - readInt64: (ptr: pointer) => bigint; - readFloat64: (ptr: pointer) => number; - writeUint32: (ptr: pointer, value: number) => void; - writeUint64: (ptr: pointer, value: bigint) => void; - writeInt64: (ptr: pointer, value: bigint) => void; - writeFloat64: (ptr: pointer, value: number) => void; +declare class JSObjectSpace { + private _heapValueById; + private _heapEntryByValue; + private _heapNextKey; + constructor(); + retain(value: any): number; + retainByRef(ref: ref): number; + release(ref: ref): void; + getObject(ref: ref): any; } /** @@ -96,7 +86,7 @@ type SwiftRuntimeThreadChannel = { }; declare class ITCInterface { private memory; - constructor(memory: Memory); + constructor(memory: JSObjectSpace); send(sendingObject: ref, transferringObjects: ref[], sendingContext: pointer): { object: any; sendingContext: pointer; @@ -182,7 +172,7 @@ type SwiftRuntimeOptions = { }; declare class SwiftRuntime { private _instance; - private _memory; + private readonly memory; private _closureDeallocator; private options; private version; @@ -190,6 +180,9 @@ declare class SwiftRuntime { private textEncoder; /** The thread ID of the current thread. */ private tid; + private getDataView; + private getUint8Array; + private wasmMemory; UnsafeEventLoopYield: typeof UnsafeEventLoopYield; constructor(options?: SwiftRuntimeOptions); setInstance(instance: WebAssembly.Instance): void; @@ -202,7 +195,6 @@ declare class SwiftRuntime { startThread(tid: number, startArg: number): void; private get instance(); private get exports(); - private get memory(); private get closureDeallocator(); private callHostFunction; /** @deprecated Use `wasmImports` instead */ diff --git a/Plugins/PackageToJS/Templates/runtime.mjs b/Plugins/PackageToJS/Templates/runtime.mjs index e3673835..fe16a65e 100644 --- a/Plugins/PackageToJS/Templates/runtime.mjs +++ b/Plugins/PackageToJS/Templates/runtime.mjs @@ -21,7 +21,7 @@ function assertNever(x, message) { } const MAIN_THREAD_TID = -1; -const decode = (kind, payload1, payload2, memory) => { +const decode = (kind, payload1, payload2, objectSpace) => { switch (kind) { case 0 /* Kind.Boolean */: switch (payload1) { @@ -37,7 +37,7 @@ const decode = (kind, payload1, payload2, memory) => { case 6 /* Kind.Function */: case 7 /* Kind.Symbol */: case 8 /* Kind.BigInt */: - return memory.getObject(payload1); + return objectSpace.getObject(payload1); case 4 /* Kind.Null */: return null; case 5 /* Kind.Undefined */: @@ -48,21 +48,18 @@ const decode = (kind, payload1, payload2, memory) => { }; // Note: // `decodeValues` assumes that the size of RawJSValue is 16. -const decodeArray = (ptr, length, memory) => { +const decodeArray = (ptr, length, memory, objectSpace) => { // fast path for empty array if (length === 0) { return []; } let result = []; - // It's safe to hold DataView here because WebAssembly.Memory.buffer won't - // change within this function. - const view = memory.dataView(); for (let index = 0; index < length; index++) { const base = ptr + 16 * index; - const kind = view.getUint32(base, true); - const payload1 = view.getUint32(base + 4, true); - const payload2 = view.getFloat64(base + 8, true); - result.push(decode(kind, payload1, payload2, memory)); + const kind = memory.getUint32(base, true); + const payload1 = memory.getUint32(base + 4, true); + const payload2 = memory.getFloat64(base + 8, true); + result.push(decode(kind, payload1, payload2, objectSpace)); } return result; }; @@ -70,27 +67,27 @@ const decodeArray = (ptr, length, memory) => { // Please prefer to use `writeAndReturnKindBits` to avoid unnecessary // memory stores. // This function should be used only when kind flag is stored in memory. -const write = (value, kind_ptr, payload1_ptr, payload2_ptr, is_exception, memory) => { - const kind = writeAndReturnKindBits(value, payload1_ptr, payload2_ptr, is_exception, memory); - memory.writeUint32(kind_ptr, kind); +const write = (value, kind_ptr, payload1_ptr, payload2_ptr, is_exception, memory, objectSpace) => { + const kind = writeAndReturnKindBits(value, payload1_ptr, payload2_ptr, is_exception, memory, objectSpace); + memory.setUint32(kind_ptr, kind, true); }; -const writeAndReturnKindBits = (value, payload1_ptr, payload2_ptr, is_exception, memory) => { +const writeAndReturnKindBits = (value, payload1_ptr, payload2_ptr, is_exception, memory, objectSpace) => { const exceptionBit = (is_exception ? 1 : 0) << 31; if (value === null) { return exceptionBit | 4 /* Kind.Null */; } const writeRef = (kind) => { - memory.writeUint32(payload1_ptr, memory.retain(value)); + memory.setUint32(payload1_ptr, objectSpace.retain(value), true); return exceptionBit | kind; }; const type = typeof value; switch (type) { case "boolean": { - memory.writeUint32(payload1_ptr, value ? 1 : 0); + memory.setUint32(payload1_ptr, value ? 1 : 0, true); return exceptionBit | 0 /* Kind.Boolean */; } case "number": { - memory.writeFloat64(payload2_ptr, value); + memory.setFloat64(payload2_ptr, value, true); return exceptionBit | 2 /* Kind.Number */; } case "string": { @@ -119,88 +116,11 @@ const writeAndReturnKindBits = (value, payload1_ptr, payload2_ptr, is_exception, function decodeObjectRefs(ptr, length, memory) { const result = new Array(length); for (let i = 0; i < length; i++) { - result[i] = memory.readUint32(ptr + 4 * i); + result[i] = memory.getUint32(ptr + 4 * i, true); } return result; } -let globalVariable; -if (typeof globalThis !== "undefined") { - globalVariable = globalThis; -} -else if (typeof window !== "undefined") { - globalVariable = window; -} -else if (typeof global !== "undefined") { - globalVariable = global; -} -else if (typeof self !== "undefined") { - globalVariable = self; -} - -class SwiftRuntimeHeap { - constructor() { - this._heapValueById = new Map(); - this._heapValueById.set(0, globalVariable); - this._heapEntryByValue = new Map(); - this._heapEntryByValue.set(globalVariable, { id: 0, rc: 1 }); - // Note: 0 is preserved for global - this._heapNextKey = 1; - } - retain(value) { - const entry = this._heapEntryByValue.get(value); - if (entry) { - entry.rc++; - return entry.id; - } - const id = this._heapNextKey++; - this._heapValueById.set(id, value); - this._heapEntryByValue.set(value, { id: id, rc: 1 }); - return id; - } - retainByRef(ref) { - return this.retain(this.referenceHeap(ref)); - } - release(ref) { - const value = this._heapValueById.get(ref); - const entry = this._heapEntryByValue.get(value); - entry.rc--; - if (entry.rc != 0) - return; - this._heapEntryByValue.delete(value); - this._heapValueById.delete(ref); - } - referenceHeap(ref) { - const value = this._heapValueById.get(ref); - if (value === undefined) { - throw new ReferenceError("Attempted to read invalid reference " + ref); - } - return value; - } -} - -class Memory { - constructor(exports) { - this.heap = new SwiftRuntimeHeap(); - this.retain = (value) => this.heap.retain(value); - this.getObject = (ref) => this.heap.referenceHeap(ref); - this.release = (ref) => this.heap.release(ref); - this.retainByRef = (ref) => this.heap.retainByRef(ref); - this.bytes = () => new Uint8Array(this.rawMemory.buffer); - this.dataView = () => new DataView(this.rawMemory.buffer); - this.writeBytes = (ptr, bytes) => this.bytes().set(bytes, ptr); - this.readUint32 = (ptr) => this.dataView().getUint32(ptr, true); - this.readUint64 = (ptr) => this.dataView().getBigUint64(ptr, true); - this.readInt64 = (ptr) => this.dataView().getBigInt64(ptr, true); - this.readFloat64 = (ptr) => this.dataView().getFloat64(ptr, true); - this.writeUint32 = (ptr, value) => this.dataView().setUint32(ptr, value, true); - this.writeUint64 = (ptr, value) => this.dataView().setBigUint64(ptr, value, true); - this.writeInt64 = (ptr, value) => this.dataView().setBigInt64(ptr, value, true); - this.writeFloat64 = (ptr, value) => this.dataView().setFloat64(ptr, value, true); - this.rawMemory = exports.memory; - } -} - class ITCInterface { constructor(memory) { this.memory = memory; @@ -305,6 +225,61 @@ function deserializeError(error) { return error.value; } +let globalVariable; +if (typeof globalThis !== "undefined") { + globalVariable = globalThis; +} +else if (typeof window !== "undefined") { + globalVariable = window; +} +else if (typeof global !== "undefined") { + globalVariable = global; +} +else if (typeof self !== "undefined") { + globalVariable = self; +} + +class JSObjectSpace { + constructor() { + this._heapValueById = new Map(); + this._heapValueById.set(0, globalVariable); + this._heapEntryByValue = new Map(); + this._heapEntryByValue.set(globalVariable, { id: 0, rc: 1 }); + // Note: 0 is preserved for global + this._heapNextKey = 1; + } + retain(value) { + const entry = this._heapEntryByValue.get(value); + if (entry) { + entry.rc++; + return entry.id; + } + const id = this._heapNextKey++; + this._heapValueById.set(id, value); + this._heapEntryByValue.set(value, { id: id, rc: 1 }); + return id; + } + retainByRef(ref) { + return this.retain(this.getObject(ref)); + } + release(ref) { + const value = this._heapValueById.get(ref); + const entry = this._heapEntryByValue.get(value); + entry.rc--; + if (entry.rc != 0) + return; + this._heapEntryByValue.delete(value); + this._heapValueById.delete(ref); + } + getObject(ref) { + const value = this._heapValueById.get(ref); + if (value === undefined) { + throw new ReferenceError("Attempted to read invalid reference " + ref); + } + return value; + } +} + class SwiftRuntime { constructor(options) { this.version = 708; @@ -314,13 +289,64 @@ class SwiftRuntime { /** @deprecated Use `wasmImports` instead */ this.importObjects = () => this.wasmImports; this._instance = null; - this._memory = null; + this.memory = new JSObjectSpace(); this._closureDeallocator = null; this.tid = null; this.options = options || {}; + this.getDataView = () => { + throw new Error("Please call setInstance() before using any JavaScriptKit APIs from Swift."); + }; + this.getUint8Array = () => { + throw new Error("Please call setInstance() before using any JavaScriptKit APIs from Swift."); + }; + this.wasmMemory = null; } setInstance(instance) { this._instance = instance; + const wasmMemory = instance.exports.memory; + if (wasmMemory instanceof WebAssembly.Memory) { + // Cache the DataView as it's not a cheap operation + let cachedDataView = new DataView(wasmMemory.buffer); + let cachedUint8Array = new Uint8Array(wasmMemory.buffer); + if (typeof SharedArrayBuffer !== "undefined" && wasmMemory.buffer instanceof SharedArrayBuffer) { + // When the wasm memory is backed by a SharedArrayBuffer, growing the memory + // doesn't invalidate the data view by setting the byte length to 0. Instead, + // the data view points to an old buffer after growing the memory. So we have + // to check the buffer identity to determine if the data view is valid. + this.getDataView = () => { + if (cachedDataView.buffer !== wasmMemory.buffer) { + cachedDataView = new DataView(wasmMemory.buffer); + } + return cachedDataView; + }; + this.getUint8Array = () => { + if (cachedUint8Array.buffer !== wasmMemory.buffer) { + cachedUint8Array = new Uint8Array(wasmMemory.buffer); + } + return cachedUint8Array; + }; + } + else { + this.getDataView = () => { + if (cachedDataView.buffer.byteLength === 0) { + // If the wasm memory is grown, the data view is invalidated, + // so we need to create a new data view. + cachedDataView = new DataView(wasmMemory.buffer); + } + return cachedDataView; + }; + this.getUint8Array = () => { + if (cachedUint8Array.byteLength === 0) { + cachedUint8Array = new Uint8Array(wasmMemory.buffer); + } + return cachedUint8Array; + }; + } + this.wasmMemory = wasmMemory; + } + else { + throw new Error("instance.exports.memory is not a WebAssembly.Memory!?"); + } if (typeof this.exports._start === "function") { throw new Error(`JavaScriptKit supports only WASI reactor ABI. Please make sure you are building with: @@ -385,12 +411,6 @@ class SwiftRuntime { get exports() { return this.instance.exports; } - get memory() { - if (!this._memory) { - this._memory = new Memory(this.instance.exports); - } - return this._memory; - } get closureDeallocator() { if (this._closureDeallocator) return this._closureDeallocator; @@ -405,10 +425,11 @@ class SwiftRuntime { const argc = args.length; const argv = this.exports.swjs_prepare_host_function_call(argc); const memory = this.memory; + const dataView = this.getDataView(); for (let index = 0; index < args.length; index++) { const argument = args[index]; const base = argv + 16 * index; - write(argument, base, base + 4, base + 8, false, memory); + write(argument, base, base + 4, base + 8, false, dataView, memory); } let output; // This ref is released by the swjs_call_host_function implementation @@ -487,7 +508,7 @@ class SwiftRuntime { const obj = memory.getObject(ref); const key = memory.getObject(name); const result = obj[key]; - return writeAndReturnKindBits(result, payload1_ptr, payload2_ptr, false, memory); + return writeAndReturnKindBits(result, payload1_ptr, payload2_ptr, false, this.getDataView(), this.memory); }, swjs_set_subscript: (ref, index, kind, payload1, payload2) => { const memory = this.memory; @@ -498,58 +519,53 @@ class SwiftRuntime { swjs_get_subscript: (ref, index, payload1_ptr, payload2_ptr) => { const obj = this.memory.getObject(ref); const result = obj[index]; - return writeAndReturnKindBits(result, payload1_ptr, payload2_ptr, false, this.memory); + return writeAndReturnKindBits(result, payload1_ptr, payload2_ptr, false, this.getDataView(), this.memory); }, swjs_encode_string: (ref, bytes_ptr_result) => { const memory = this.memory; const bytes = this.textEncoder.encode(memory.getObject(ref)); const bytes_ptr = memory.retain(bytes); - memory.writeUint32(bytes_ptr_result, bytes_ptr); + this.getDataView().setUint32(bytes_ptr_result, bytes_ptr, true); return bytes.length; }, swjs_decode_string: ( // NOTE: TextDecoder can't decode typed arrays backed by SharedArrayBuffer this.options.sharedMemory == true ? ((bytes_ptr, length) => { - const memory = this.memory; - const bytes = memory - .bytes() + const bytes = this.getUint8Array() .slice(bytes_ptr, bytes_ptr + length); const string = this.textDecoder.decode(bytes); - return memory.retain(string); + return this.memory.retain(string); }) : ((bytes_ptr, length) => { - const memory = this.memory; - const bytes = memory - .bytes() + const bytes = this.getUint8Array() .subarray(bytes_ptr, bytes_ptr + length); const string = this.textDecoder.decode(bytes); - return memory.retain(string); + return this.memory.retain(string); })), swjs_load_string: (ref, buffer) => { - const memory = this.memory; - const bytes = memory.getObject(ref); - memory.writeBytes(buffer, bytes); + const bytes = this.memory.getObject(ref); + this.getUint8Array().set(bytes, buffer); }, swjs_call_function: (ref, argv, argc, payload1_ptr, payload2_ptr) => { const memory = this.memory; const func = memory.getObject(ref); let result = undefined; try { - const args = decodeArray(argv, argc, memory); + const args = decodeArray(argv, argc, this.getDataView(), memory); result = func(...args); } catch (error) { - return writeAndReturnKindBits(error, payload1_ptr, payload2_ptr, true, this.memory); + return writeAndReturnKindBits(error, payload1_ptr, payload2_ptr, true, this.getDataView(), this.memory); } - return writeAndReturnKindBits(result, payload1_ptr, payload2_ptr, false, this.memory); + return writeAndReturnKindBits(result, payload1_ptr, payload2_ptr, false, this.getDataView(), this.memory); }, swjs_call_function_no_catch: (ref, argv, argc, payload1_ptr, payload2_ptr) => { const memory = this.memory; const func = memory.getObject(ref); - const args = decodeArray(argv, argc, memory); + const args = decodeArray(argv, argc, this.getDataView(), memory); const result = func(...args); - return writeAndReturnKindBits(result, payload1_ptr, payload2_ptr, false, this.memory); + return writeAndReturnKindBits(result, payload1_ptr, payload2_ptr, false, this.getDataView(), this.memory); }, swjs_call_function_with_this: (obj_ref, func_ref, argv, argc, payload1_ptr, payload2_ptr) => { const memory = this.memory; @@ -557,27 +573,27 @@ class SwiftRuntime { const func = memory.getObject(func_ref); let result; try { - const args = decodeArray(argv, argc, memory); + const args = decodeArray(argv, argc, this.getDataView(), memory); result = func.apply(obj, args); } catch (error) { - return writeAndReturnKindBits(error, payload1_ptr, payload2_ptr, true, this.memory); + return writeAndReturnKindBits(error, payload1_ptr, payload2_ptr, true, this.getDataView(), this.memory); } - return writeAndReturnKindBits(result, payload1_ptr, payload2_ptr, false, this.memory); + return writeAndReturnKindBits(result, payload1_ptr, payload2_ptr, false, this.getDataView(), this.memory); }, swjs_call_function_with_this_no_catch: (obj_ref, func_ref, argv, argc, payload1_ptr, payload2_ptr) => { const memory = this.memory; const obj = memory.getObject(obj_ref); const func = memory.getObject(func_ref); let result = undefined; - const args = decodeArray(argv, argc, memory); + const args = decodeArray(argv, argc, this.getDataView(), memory); result = func.apply(obj, args); - return writeAndReturnKindBits(result, payload1_ptr, payload2_ptr, false, this.memory); + return writeAndReturnKindBits(result, payload1_ptr, payload2_ptr, false, this.getDataView(), this.memory); }, swjs_call_new: (ref, argv, argc) => { const memory = this.memory; const constructor = memory.getObject(ref); - const args = decodeArray(argv, argc, memory); + const args = decodeArray(argv, argc, this.getDataView(), memory); const instance = new constructor(...args); return this.memory.retain(instance); }, @@ -586,15 +602,15 @@ class SwiftRuntime { const constructor = memory.getObject(ref); let result; try { - const args = decodeArray(argv, argc, memory); + const args = decodeArray(argv, argc, this.getDataView(), memory); result = new constructor(...args); } catch (error) { - write(error, exception_kind_ptr, exception_payload1_ptr, exception_payload2_ptr, true, this.memory); + write(error, exception_kind_ptr, exception_payload1_ptr, exception_payload2_ptr, true, this.getDataView(), this.memory); return -1; } memory = this.memory; - write(null, exception_kind_ptr, exception_payload1_ptr, exception_payload2_ptr, false, memory); + write(null, exception_kind_ptr, exception_payload1_ptr, exception_payload2_ptr, false, this.getDataView(), memory); return memory.retain(result); }, swjs_instanceof: (obj_ref, constructor_ref) => { @@ -628,7 +644,7 @@ class SwiftRuntime { // See https://github.com/swiftwasm/swift/issues/5599 return this.memory.retain(new ArrayType()); } - const array = new ArrayType(this.memory.rawMemory.buffer, elementsPtr, length); + const array = new ArrayType(this.wasmMemory.buffer, elementsPtr, length); // Call `.slice()` to copy the memory return this.memory.retain(array.slice()); }, @@ -637,7 +653,7 @@ class SwiftRuntime { const memory = this.memory; const typedArray = memory.getObject(ref); const bytes = new Uint8Array(typedArray.buffer); - memory.writeBytes(buffer, bytes); + this.getUint8Array().set(bytes, buffer); }, swjs_release: (ref) => { this.memory.release(ref); @@ -760,8 +776,7 @@ class SwiftRuntime { throw new Error("threadChannel is not set in options given to SwiftRuntime. Please set it to request transferring objects."); } const broker = getMessageBroker(this.options.threadChannel); - const memory = this.memory; - const transferringObjects = decodeObjectRefs(transferring_objects, transferring_objects_count, memory); + const transferringObjects = decodeObjectRefs(transferring_objects, transferring_objects_count, this.getDataView()); broker.request({ type: "request", data: { @@ -781,9 +796,8 @@ class SwiftRuntime { throw new Error("threadChannel is not set in options given to SwiftRuntime. Please set it to request transferring objects."); } const broker = getMessageBroker(this.options.threadChannel); - const memory = this.memory; - const sendingObjects = decodeObjectRefs(sending_objects, sending_objects_count, memory); - const transferringObjects = decodeObjectRefs(transferring_objects, transferring_objects_count, memory); + const sendingObjects = decodeObjectRefs(sending_objects, sending_objects_count, this.getDataView()); + const transferringObjects = decodeObjectRefs(transferring_objects, transferring_objects_count, this.getDataView()); broker.request({ type: "request", data: { diff --git a/Runtime/src/index.ts b/Runtime/src/index.ts index a747dec1..77cc2451 100644 --- a/Runtime/src/index.ts +++ b/Runtime/src/index.ts @@ -7,9 +7,9 @@ import { MAIN_THREAD_TID, } from "./types.js"; import * as JSValue from "./js-value.js"; -import { Memory } from "./memory.js"; import { deserializeError, MainToWorkerMessage, MessageBroker, ResponseMessage, ITCInterface, serializeError, SwiftRuntimeThreadChannel, WorkerToMainMessage } from "./itc.js"; import { decodeObjectRefs } from "./js-value.js"; +import { JSObjectSpace } from "./object-heap.js"; export { SwiftRuntimeThreadChannel }; export type SwiftRuntimeOptions = { @@ -27,7 +27,7 @@ export type SwiftRuntimeOptions = { export class SwiftRuntime { private _instance: WebAssembly.Instance | null; - private _memory: Memory | null; + private readonly memory: JSObjectSpace; private _closureDeallocator: SwiftClosureDeallocator | null; private options: SwiftRuntimeOptions; private version: number = 708; @@ -36,19 +36,71 @@ export class SwiftRuntime { private textEncoder = new TextEncoder(); // Only support utf-8 /** The thread ID of the current thread. */ private tid: number | null; + private getDataView: (() => DataView); + private getUint8Array: (() => Uint8Array); + private wasmMemory: WebAssembly.Memory | null; UnsafeEventLoopYield = UnsafeEventLoopYield; constructor(options?: SwiftRuntimeOptions) { this._instance = null; - this._memory = null; + this.memory = new JSObjectSpace(); this._closureDeallocator = null; this.tid = null; this.options = options || {}; + this.getDataView = () => { + throw new Error("Please call setInstance() before using any JavaScriptKit APIs from Swift."); + }; + this.getUint8Array = () => { + throw new Error("Please call setInstance() before using any JavaScriptKit APIs from Swift."); + }; + this.wasmMemory = null; } setInstance(instance: WebAssembly.Instance) { this._instance = instance; + const wasmMemory = instance.exports.memory; + if (wasmMemory instanceof WebAssembly.Memory) { + // Cache the DataView as it's not a cheap operation + let cachedDataView = new DataView(wasmMemory.buffer); + let cachedUint8Array = new Uint8Array(wasmMemory.buffer); + if (typeof SharedArrayBuffer !== "undefined" && wasmMemory.buffer instanceof SharedArrayBuffer) { + // When the wasm memory is backed by a SharedArrayBuffer, growing the memory + // doesn't invalidate the data view by setting the byte length to 0. Instead, + // the data view points to an old buffer after growing the memory. So we have + // to check the buffer identity to determine if the data view is valid. + this.getDataView = () => { + if (cachedDataView.buffer !== wasmMemory.buffer) { + cachedDataView = new DataView(wasmMemory.buffer); + } + return cachedDataView; + }; + this.getUint8Array = () => { + if (cachedUint8Array.buffer !== wasmMemory.buffer) { + cachedUint8Array = new Uint8Array(wasmMemory.buffer); + } + return cachedUint8Array; + }; + } else { + this.getDataView = () => { + if (cachedDataView.buffer.byteLength === 0) { + // If the wasm memory is grown, the data view is invalidated, + // so we need to create a new data view. + cachedDataView = new DataView(wasmMemory.buffer); + } + return cachedDataView; + }; + this.getUint8Array = () => { + if (cachedUint8Array.byteLength === 0) { + cachedUint8Array = new Uint8Array(wasmMemory.buffer); + } + return cachedUint8Array; + }; + } + this.wasmMemory = wasmMemory; + } else { + throw new Error("instance.exports.memory is not a WebAssembly.Memory!?"); + } if (typeof (this.exports as any)._start === "function") { throw new Error( `JavaScriptKit supports only WASI reactor ABI. @@ -124,13 +176,6 @@ export class SwiftRuntime { return this.instance.exports as any as ExportedFunctions; } - private get memory() { - if (!this._memory) { - this._memory = new Memory(this.instance.exports); - } - return this._memory; - } - private get closureDeallocator(): SwiftClosureDeallocator | null { if (this._closureDeallocator) return this._closureDeallocator; @@ -154,10 +199,11 @@ export class SwiftRuntime { const argc = args.length; const argv = this.exports.swjs_prepare_host_function_call(argc); const memory = this.memory; + const dataView = this.getDataView(); for (let index = 0; index < args.length; index++) { const argument = args[index]; const base = argv + 16 * index; - JSValue.write(argument, base, base + 4, base + 8, false, memory); + JSValue.write(argument, base, base + 4, base + 8, false, dataView, memory); } let output: any; // This ref is released by the swjs_call_host_function implementation @@ -258,7 +304,8 @@ export class SwiftRuntime { payload1_ptr, payload2_ptr, false, - memory + this.getDataView(), + this.memory ); }, @@ -287,6 +334,7 @@ export class SwiftRuntime { payload1_ptr, payload2_ptr, false, + this.getDataView(), this.memory ); }, @@ -295,33 +343,28 @@ export class SwiftRuntime { const memory = this.memory; const bytes = this.textEncoder.encode(memory.getObject(ref)); const bytes_ptr = memory.retain(bytes); - memory.writeUint32(bytes_ptr_result, bytes_ptr); + this.getDataView().setUint32(bytes_ptr_result, bytes_ptr, true); return bytes.length; }, swjs_decode_string: ( // NOTE: TextDecoder can't decode typed arrays backed by SharedArrayBuffer this.options.sharedMemory == true ? ((bytes_ptr: pointer, length: number) => { - const memory = this.memory; - const bytes = memory - .bytes() + const bytes = this.getUint8Array() .slice(bytes_ptr, bytes_ptr + length); const string = this.textDecoder.decode(bytes); - return memory.retain(string); + return this.memory.retain(string); }) : ((bytes_ptr: pointer, length: number) => { - const memory = this.memory; - const bytes = memory - .bytes() + const bytes = this.getUint8Array() .subarray(bytes_ptr, bytes_ptr + length); const string = this.textDecoder.decode(bytes); - return memory.retain(string); + return this.memory.retain(string); }) ), swjs_load_string: (ref: ref, buffer: pointer) => { - const memory = this.memory; - const bytes = memory.getObject(ref); - memory.writeBytes(buffer, bytes); + const bytes = this.memory.getObject(ref); + this.getUint8Array().set(bytes, buffer); }, swjs_call_function: ( @@ -335,7 +378,7 @@ export class SwiftRuntime { const func = memory.getObject(ref); let result = undefined; try { - const args = JSValue.decodeArray(argv, argc, memory); + const args = JSValue.decodeArray(argv, argc, this.getDataView(), memory); result = func(...args); } catch (error) { return JSValue.writeAndReturnKindBits( @@ -343,6 +386,7 @@ export class SwiftRuntime { payload1_ptr, payload2_ptr, true, + this.getDataView(), this.memory ); } @@ -351,6 +395,7 @@ export class SwiftRuntime { payload1_ptr, payload2_ptr, false, + this.getDataView(), this.memory ); }, @@ -363,13 +408,14 @@ export class SwiftRuntime { ) => { const memory = this.memory; const func = memory.getObject(ref); - const args = JSValue.decodeArray(argv, argc, memory); + const args = JSValue.decodeArray(argv, argc, this.getDataView(), memory); const result = func(...args); return JSValue.writeAndReturnKindBits( result, payload1_ptr, payload2_ptr, false, + this.getDataView(), this.memory ); }, @@ -387,7 +433,7 @@ export class SwiftRuntime { const func = memory.getObject(func_ref); let result: any; try { - const args = JSValue.decodeArray(argv, argc, memory); + const args = JSValue.decodeArray(argv, argc, this.getDataView(), memory); result = func.apply(obj, args); } catch (error) { return JSValue.writeAndReturnKindBits( @@ -395,6 +441,7 @@ export class SwiftRuntime { payload1_ptr, payload2_ptr, true, + this.getDataView(), this.memory ); } @@ -403,6 +450,7 @@ export class SwiftRuntime { payload1_ptr, payload2_ptr, false, + this.getDataView(), this.memory ); }, @@ -418,13 +466,14 @@ export class SwiftRuntime { const obj = memory.getObject(obj_ref); const func = memory.getObject(func_ref); let result = undefined; - const args = JSValue.decodeArray(argv, argc, memory); + const args = JSValue.decodeArray(argv, argc, this.getDataView(), memory); result = func.apply(obj, args); return JSValue.writeAndReturnKindBits( result, payload1_ptr, payload2_ptr, false, + this.getDataView(), this.memory ); }, @@ -432,7 +481,7 @@ export class SwiftRuntime { swjs_call_new: (ref: ref, argv: pointer, argc: number) => { const memory = this.memory; const constructor = memory.getObject(ref); - const args = JSValue.decodeArray(argv, argc, memory); + const args = JSValue.decodeArray(argv, argc, this.getDataView(), memory); const instance = new constructor(...args); return this.memory.retain(instance); }, @@ -448,7 +497,7 @@ export class SwiftRuntime { const constructor = memory.getObject(ref); let result: any; try { - const args = JSValue.decodeArray(argv, argc, memory); + const args = JSValue.decodeArray(argv, argc, this.getDataView(), memory); result = new constructor(...args); } catch (error) { JSValue.write( @@ -457,6 +506,7 @@ export class SwiftRuntime { exception_payload1_ptr, exception_payload2_ptr, true, + this.getDataView(), this.memory ); return -1; @@ -468,6 +518,7 @@ export class SwiftRuntime { exception_payload1_ptr, exception_payload2_ptr, false, + this.getDataView(), memory ); return memory.retain(result); @@ -521,7 +572,7 @@ export class SwiftRuntime { return this.memory.retain(new ArrayType()); } const array = new ArrayType( - this.memory.rawMemory.buffer, + this.wasmMemory!.buffer, elementsPtr, length ); @@ -535,7 +586,7 @@ export class SwiftRuntime { const memory = this.memory; const typedArray = memory.getObject(ref); const bytes = new Uint8Array(typedArray.buffer); - memory.writeBytes(buffer, bytes); + this.getUint8Array().set(bytes, buffer); }, swjs_release: (ref: ref) => { @@ -674,8 +725,7 @@ export class SwiftRuntime { throw new Error("threadChannel is not set in options given to SwiftRuntime. Please set it to request transferring objects."); } const broker = getMessageBroker(this.options.threadChannel); - const memory = this.memory; - const transferringObjects = decodeObjectRefs(transferring_objects, transferring_objects_count, memory); + const transferringObjects = decodeObjectRefs(transferring_objects, transferring_objects_count, this.getDataView()); broker.request({ type: "request", data: { @@ -701,9 +751,9 @@ export class SwiftRuntime { throw new Error("threadChannel is not set in options given to SwiftRuntime. Please set it to request transferring objects."); } const broker = getMessageBroker(this.options.threadChannel); - const memory = this.memory; - const sendingObjects = decodeObjectRefs(sending_objects, sending_objects_count, memory); - const transferringObjects = decodeObjectRefs(transferring_objects, transferring_objects_count, memory); + const dataView = this.getDataView(); + const sendingObjects = decodeObjectRefs(sending_objects, sending_objects_count, dataView); + const transferringObjects = decodeObjectRefs(transferring_objects, transferring_objects_count, dataView); broker.request({ type: "request", data: { diff --git a/Runtime/src/itc.ts b/Runtime/src/itc.ts index e2c93622..08b42064 100644 --- a/Runtime/src/itc.ts +++ b/Runtime/src/itc.ts @@ -1,6 +1,6 @@ // This file defines the interface for the inter-thread communication. import type { ref, pointer } from "./types.js"; -import { Memory } from "./memory.js"; +import { JSObjectSpace as JSObjectSpace } from "./object-heap.js"; /** * A thread channel is a set of functions that are used to communicate between @@ -83,7 +83,7 @@ export type SwiftRuntimeThreadChannel = export class ITCInterface { - constructor(private memory: Memory) {} + constructor(private memory: JSObjectSpace) {} send(sendingObject: ref, transferringObjects: ref[], sendingContext: pointer): { object: any, sendingContext: pointer, transfer: Transferable[] } { const object = this.memory.getObject(sendingObject); diff --git a/Runtime/src/js-value.ts b/Runtime/src/js-value.ts index dcc378f6..b23f39d8 100644 --- a/Runtime/src/js-value.ts +++ b/Runtime/src/js-value.ts @@ -1,4 +1,4 @@ -import { Memory } from "./memory.js"; +import { JSObjectSpace } from "./object-heap.js"; import { assertNever, JavaScriptValueKindAndFlags, pointer, ref } from "./types.js"; export const enum Kind { @@ -17,7 +17,7 @@ export const decode = ( kind: Kind, payload1: number, payload2: number, - memory: Memory + objectSpace: JSObjectSpace ) => { switch (kind) { case Kind.Boolean: @@ -35,7 +35,7 @@ export const decode = ( case Kind.Function: case Kind.Symbol: case Kind.BigInt: - return memory.getObject(payload1); + return objectSpace.getObject(payload1); case Kind.Null: return null; @@ -50,22 +50,19 @@ export const decode = ( // Note: // `decodeValues` assumes that the size of RawJSValue is 16. -export const decodeArray = (ptr: pointer, length: number, memory: Memory) => { +export const decodeArray = (ptr: pointer, length: number, memory: DataView, objectSpace: JSObjectSpace) => { // fast path for empty array if (length === 0) { return []; } let result = []; - // It's safe to hold DataView here because WebAssembly.Memory.buffer won't - // change within this function. - const view = memory.dataView(); for (let index = 0; index < length; index++) { const base = ptr + 16 * index; - const kind = view.getUint32(base, true); - const payload1 = view.getUint32(base + 4, true); - const payload2 = view.getFloat64(base + 8, true); - result.push(decode(kind, payload1, payload2, memory)); + const kind = memory.getUint32(base, true); + const payload1 = memory.getUint32(base + 4, true); + const payload2 = memory.getFloat64(base + 8, true); + result.push(decode(kind, payload1, payload2, objectSpace)); } return result; }; @@ -80,16 +77,18 @@ export const write = ( payload1_ptr: pointer, payload2_ptr: pointer, is_exception: boolean, - memory: Memory + memory: DataView, + objectSpace: JSObjectSpace ) => { const kind = writeAndReturnKindBits( value, payload1_ptr, payload2_ptr, is_exception, - memory + memory, + objectSpace ); - memory.writeUint32(kind_ptr, kind); + memory.setUint32(kind_ptr, kind, true); }; export const writeAndReturnKindBits = ( @@ -97,7 +96,8 @@ export const writeAndReturnKindBits = ( payload1_ptr: pointer, payload2_ptr: pointer, is_exception: boolean, - memory: Memory + memory: DataView, + objectSpace: JSObjectSpace ): JavaScriptValueKindAndFlags => { const exceptionBit = (is_exception ? 1 : 0) << 31; if (value === null) { @@ -105,18 +105,18 @@ export const writeAndReturnKindBits = ( } const writeRef = (kind: Kind) => { - memory.writeUint32(payload1_ptr, memory.retain(value)); + memory.setUint32(payload1_ptr, objectSpace.retain(value), true); return exceptionBit | kind; }; const type = typeof value; switch (type) { case "boolean": { - memory.writeUint32(payload1_ptr, value ? 1 : 0); + memory.setUint32(payload1_ptr, value ? 1 : 0, true); return exceptionBit | Kind.Boolean; } case "number": { - memory.writeFloat64(payload2_ptr, value); + memory.setFloat64(payload2_ptr, value, true); return exceptionBit | Kind.Number; } case "string": { @@ -143,10 +143,10 @@ export const writeAndReturnKindBits = ( throw new Error("Unreachable"); }; -export function decodeObjectRefs(ptr: pointer, length: number, memory: Memory): ref[] { +export function decodeObjectRefs(ptr: pointer, length: number, memory: DataView): ref[] { const result: ref[] = new Array(length); for (let i = 0; i < length; i++) { - result[i] = memory.readUint32(ptr + 4 * i); + result[i] = memory.getUint32(ptr + 4 * i, true); } return result; } diff --git a/Runtime/src/memory.ts b/Runtime/src/memory.ts deleted file mode 100644 index 5ba00c82..00000000 --- a/Runtime/src/memory.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { SwiftRuntimeHeap } from "./object-heap.js"; -import { pointer } from "./types.js"; - -export class Memory { - readonly rawMemory: WebAssembly.Memory; - - private readonly heap = new SwiftRuntimeHeap(); - - constructor(exports: WebAssembly.Exports) { - this.rawMemory = exports.memory as WebAssembly.Memory; - } - - retain = (value: any) => this.heap.retain(value); - getObject = (ref: number) => this.heap.referenceHeap(ref); - release = (ref: number) => this.heap.release(ref); - retainByRef = (ref: number) => this.heap.retainByRef(ref); - - bytes = () => new Uint8Array(this.rawMemory.buffer); - dataView = () => new DataView(this.rawMemory.buffer); - - writeBytes = (ptr: pointer, bytes: Uint8Array) => - this.bytes().set(bytes, ptr); - - readUint32 = (ptr: pointer) => this.dataView().getUint32(ptr, true); - readUint64 = (ptr: pointer) => this.dataView().getBigUint64(ptr, true); - readInt64 = (ptr: pointer) => this.dataView().getBigInt64(ptr, true); - readFloat64 = (ptr: pointer) => this.dataView().getFloat64(ptr, true); - - writeUint32 = (ptr: pointer, value: number) => - this.dataView().setUint32(ptr, value, true); - writeUint64 = (ptr: pointer, value: bigint) => - this.dataView().setBigUint64(ptr, value, true); - writeInt64 = (ptr: pointer, value: bigint) => - this.dataView().setBigInt64(ptr, value, true); - writeFloat64 = (ptr: pointer, value: number) => - this.dataView().setFloat64(ptr, value, true); -} diff --git a/Runtime/src/object-heap.ts b/Runtime/src/object-heap.ts index a239cf2b..ecc2d218 100644 --- a/Runtime/src/object-heap.ts +++ b/Runtime/src/object-heap.ts @@ -5,7 +5,7 @@ type SwiftRuntimeHeapEntry = { id: number; rc: number; }; -export class SwiftRuntimeHeap { +export class JSObjectSpace { private _heapValueById: Map; private _heapEntryByValue: Map; private _heapNextKey: number; @@ -34,7 +34,7 @@ export class SwiftRuntimeHeap { } retainByRef(ref: ref) { - return this.retain(this.referenceHeap(ref)); + return this.retain(this.getObject(ref)); } release(ref: ref) { @@ -47,7 +47,7 @@ export class SwiftRuntimeHeap { this._heapValueById.delete(ref); } - referenceHeap(ref: ref) { + getObject(ref: ref) { const value = this._heapValueById.get(ref); if (value === undefined) { throw new ReferenceError( diff --git a/Tests/prelude.mjs b/Tests/prelude.mjs index 1bc5bdba..b46e3dcc 100644 --- a/Tests/prelude.mjs +++ b/Tests/prelude.mjs @@ -110,7 +110,7 @@ function BridgeJSRuntimeTests_runJsWorks(instance, exports) { exports.throwsSwiftError(); assert.fail("Expected error"); } catch (error) { - assert.equal(error.message, "TestError"); + assert.equal(error.message, "TestError", error); } }