diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7f4f6c87e..7c7349546 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -65,7 +65,7 @@ jobs: - name: Check if SwiftPM resources are stale run: | make regenerate_swiftpm_resources - git diff --exit-code Sources/JavaScriptKit/Runtime + git diff --exit-code Plugins/PackageToJS/Templates/runtime.mjs Plugins/PackageToJS/Templates/runtime.d.ts - run: swift test --package-path ./Plugins/PackageToJS - run: swift test --package-path ./Plugins/BridgeJS - name: Validate BridgeJS TypeScript declarations diff --git a/Plugins/PackageToJS/Templates/runtime.d.ts b/Plugins/PackageToJS/Templates/runtime.d.ts index e4795bedf..b1d9fcd13 100644 --- a/Plugins/PackageToJS/Templates/runtime.d.ts +++ b/Plugins/PackageToJS/Templates/runtime.d.ts @@ -8,9 +8,12 @@ declare class JSObjectSpace { private _freeSlotStack; constructor(); retain(value: any): number; + retainUnique(value: any): number; + shareUnique(reference: ref): number; retainByRef(reference: ref): number; release(reference: ref): void; getObject(reference: ref): any; + private _allocate; private _getValidatedSlotState; } diff --git a/Plugins/PackageToJS/Templates/runtime.mjs b/Plugins/PackageToJS/Templates/runtime.mjs index 6170e58d0..11422c9bc 100644 --- a/Plugins/PackageToJS/Templates/runtime.mjs +++ b/Plugins/PackageToJS/Templates/runtime.mjs @@ -250,6 +250,9 @@ const globalVariable = globalThis; const SLOT_BITS = 24; const SLOT_MASK = (1 << SLOT_BITS) - 1; const GEN_MASK = (1 << (32 - SLOT_BITS)) - 1; +// Set in the state of a slot the value map does not hold. +const MAP_FREE = 1 << (SLOT_BITS - 1); +const COUNT_MASK = MAP_FREE - 1; class JSObjectSpace { constructor() { this._slotByValue = new Map(); @@ -267,36 +270,42 @@ class JSObjectSpace { if (slot !== undefined) { const state = this._stateBySlot[slot]; const nextState = (state + 1) >>> 0; - if ((nextState & SLOT_MASK) === 0) { + if ((nextState & COUNT_MASK) === 0) { throw new RangeError(`Reference count overflow at slot ${slot}`); } this._stateBySlot[slot] = nextState; return ((nextState & ~SLOT_MASK) | slot) >>> 0; } - let newSlot; - let state; - if (this._freeSlotStack.length > 0) { - newSlot = this._freeSlotStack.pop(); - const gen = this._stateBySlot[newSlot] >>> SLOT_BITS; - state = ((gen << SLOT_BITS) | 1) >>> 0; + const reference = this._allocate(value, 0); + this._slotByValue.set(value, reference & SLOT_MASK); + return reference; + } + // Returns a reference to a new slot the value map does not hold. + retainUnique(value) { + return this._allocate(value, MAP_FREE); + } + // Consumes `reference` and returns the reference `retain` returns for its value. + shareUnique(reference) { + const state = this._getValidatedSlotState(reference); + if ((state & MAP_FREE) === 0) { + return reference; } - else { - newSlot = this._values.length; - if (newSlot > SLOT_MASK) { - throw new RangeError(`Reference slot overflow: ${newSlot} exceeds ${SLOT_MASK}`); - } - state = 1; + const slot = reference & SLOT_MASK; + const value = this._values[slot]; + if (this._slotByValue.has(value)) { + const shared = this.retain(value); + this.release(reference); + return shared; } - this._stateBySlot[newSlot] = state; - this._values[newSlot] = value; - this._slotByValue.set(value, newSlot); - return ((state & ~SLOT_MASK) | newSlot) >>> 0; + this._slotByValue.set(value, slot); + this._stateBySlot[slot] = (state & ~MAP_FREE) >>> 0; + return reference; } retainByRef(reference) { const state = this._getValidatedSlotState(reference); const slot = reference & SLOT_MASK; const nextState = (state + 1) >>> 0; - if ((nextState & SLOT_MASK) === 0) { + if ((nextState & COUNT_MASK) === 0) { throw new RangeError(`Reference count overflow at slot ${slot}`); } this._stateBySlot[slot] = nextState; @@ -305,11 +314,13 @@ class JSObjectSpace { release(reference) { const state = this._getValidatedSlotState(reference); const slot = reference & SLOT_MASK; - if ((state & SLOT_MASK) > 1) { + if ((state & COUNT_MASK) > 1) { this._stateBySlot[slot] = (state - 1) >>> 0; return; } - this._slotByValue.delete(this._values[slot]); + if ((state & MAP_FREE) === 0) { + this._slotByValue.delete(this._values[slot]); + } this._values[slot] = undefined; const nextGen = ((state >>> SLOT_BITS) + 1) & GEN_MASK; this._stateBySlot[slot] = (nextGen << SLOT_BITS) >>> 0; @@ -319,6 +330,25 @@ class JSObjectSpace { this._getValidatedSlotState(reference); return this._values[reference & SLOT_MASK]; } + _allocate(value, flags) { + let newSlot; + let state; + if (this._freeSlotStack.length > 0) { + newSlot = this._freeSlotStack.pop(); + const gen = this._stateBySlot[newSlot] >>> SLOT_BITS; + state = ((gen << SLOT_BITS) | flags | 1) >>> 0; + } + else { + newSlot = this._values.length; + if (newSlot > SLOT_MASK) { + throw new RangeError(`Reference slot overflow: ${newSlot} exceeds ${SLOT_MASK}`); + } + state = flags | 1; + } + this._stateBySlot[newSlot] = state; + this._values[newSlot] = value; + return ((state & ~SLOT_MASK) | newSlot) >>> 0; + } // Returns the packed state for the slot, after validating the reference. _getValidatedSlotState(reference) { const slot = reference & SLOT_MASK; @@ -770,6 +800,9 @@ class SwiftRuntime { swjs_release: (ref) => { this.memory.release(ref); }, + swjs_share_unique: (ref) => { + return this.memory.shareUnique(ref); + }, swjs_release_remote: (tid, ref) => { var _a; if (!this.options.threadChannel) { diff --git a/Runtime/src/index.ts b/Runtime/src/index.ts index e05bf712e..48ff8bd85 100644 --- a/Runtime/src/index.ts +++ b/Runtime/src/index.ts @@ -780,6 +780,10 @@ export class SwiftRuntime { this.memory.release(ref); }, + swjs_share_unique: (ref: ref) => { + return this.memory.shareUnique(ref); + }, + swjs_release_remote: (tid: number, ref: ref) => { if (!this.options.threadChannel) { throw new Error( diff --git a/Runtime/src/object-heap.ts b/Runtime/src/object-heap.ts index 6138953e9..e36bbd29f 100644 --- a/Runtime/src/object-heap.ts +++ b/Runtime/src/object-heap.ts @@ -4,6 +4,9 @@ import { ref } from "./types.js"; const SLOT_BITS = 24; const SLOT_MASK = (1 << SLOT_BITS) - 1; const GEN_MASK = (1 << (32 - SLOT_BITS)) - 1; +// Set in the state of a slot the value map does not hold. +const MAP_FREE = 1 << (SLOT_BITS - 1); +const COUNT_MASK = MAP_FREE - 1; export class JSObjectSpace { private _slotByValue: Map; @@ -29,7 +32,7 @@ export class JSObjectSpace { if (slot !== undefined) { const state = this._stateBySlot[slot]!; const nextState = (state + 1) >>> 0; - if ((nextState & SLOT_MASK) === 0) { + if ((nextState & COUNT_MASK) === 0) { throw new RangeError( `Reference count overflow at slot ${slot}`, ); @@ -38,33 +41,39 @@ export class JSObjectSpace { return ((nextState & ~SLOT_MASK) | slot) >>> 0; } - let newSlot: number; - let state: number; - if (this._freeSlotStack.length > 0) { - newSlot = this._freeSlotStack.pop()!; - const gen = this._stateBySlot[newSlot]! >>> SLOT_BITS; - state = ((gen << SLOT_BITS) | 1) >>> 0; - } else { - newSlot = this._values.length; - if (newSlot > SLOT_MASK) { - throw new RangeError( - `Reference slot overflow: ${newSlot} exceeds ${SLOT_MASK}`, - ); - } - state = 1; - } + const reference = this._allocate(value, 0); + this._slotByValue.set(value, reference & SLOT_MASK); + return reference; + } - this._stateBySlot[newSlot] = state; - this._values[newSlot] = value; - this._slotByValue.set(value, newSlot); - return ((state & ~SLOT_MASK) | newSlot) >>> 0; + // Returns a reference to a new slot the value map does not hold. + retainUnique(value: any) { + return this._allocate(value, MAP_FREE); + } + + // Consumes `reference` and returns the reference `retain` returns for its value. + shareUnique(reference: ref) { + const state = this._getValidatedSlotState(reference); + if ((state & MAP_FREE) === 0) { + return reference; + } + const slot = reference & SLOT_MASK; + const value = this._values[slot]; + if (this._slotByValue.has(value)) { + const shared = this.retain(value); + this.release(reference); + return shared; + } + this._slotByValue.set(value, slot); + this._stateBySlot[slot] = (state & ~MAP_FREE) >>> 0; + return reference; } retainByRef(reference: ref) { const state = this._getValidatedSlotState(reference); const slot = reference & SLOT_MASK; const nextState = (state + 1) >>> 0; - if ((nextState & SLOT_MASK) === 0) { + if ((nextState & COUNT_MASK) === 0) { throw new RangeError(`Reference count overflow at slot ${slot}`); } this._stateBySlot[slot] = nextState; @@ -74,12 +83,14 @@ export class JSObjectSpace { release(reference: ref) { const state = this._getValidatedSlotState(reference); const slot = reference & SLOT_MASK; - if ((state & SLOT_MASK) > 1) { + if ((state & COUNT_MASK) > 1) { this._stateBySlot[slot] = (state - 1) >>> 0; return; } - this._slotByValue.delete(this._values[slot]); + if ((state & MAP_FREE) === 0) { + this._slotByValue.delete(this._values[slot]); + } this._values[slot] = undefined; const nextGen = ((state >>> SLOT_BITS) + 1) & GEN_MASK; this._stateBySlot[slot] = (nextGen << SLOT_BITS) >>> 0; @@ -91,6 +102,28 @@ export class JSObjectSpace { return this._values[reference & SLOT_MASK]; } + private _allocate(value: any, flags: number) { + let newSlot: number; + let state: number; + if (this._freeSlotStack.length > 0) { + newSlot = this._freeSlotStack.pop()!; + const gen = this._stateBySlot[newSlot]! >>> SLOT_BITS; + state = ((gen << SLOT_BITS) | flags | 1) >>> 0; + } else { + newSlot = this._values.length; + if (newSlot > SLOT_MASK) { + throw new RangeError( + `Reference slot overflow: ${newSlot} exceeds ${SLOT_MASK}`, + ); + } + state = flags | 1; + } + + this._stateBySlot[newSlot] = state; + this._values[newSlot] = value; + return ((state & ~SLOT_MASK) | newSlot) >>> 0; + } + // Returns the packed state for the slot, after validating the reference. private _getValidatedSlotState(reference: ref): number { const slot = reference & SLOT_MASK; diff --git a/Runtime/test/object-heap.test.ts b/Runtime/test/object-heap.test.ts new file mode 100644 index 000000000..acf1f6927 --- /dev/null +++ b/Runtime/test/object-heap.test.ts @@ -0,0 +1,199 @@ +import { describe, expect, test } from "vitest"; +import { JSObjectSpace } from "../src/object-heap.js"; + +const SLOT_MASK = (1 << 24) - 1; +const MAX_REFERENCES = (1 << 23) - 1; + +function invalidReference(reference: number) { + return new ReferenceError( + `Attempted to use invalid reference ${reference}`, + ); +} + +function staleReference(reference: number) { + return new ReferenceError(`Attempted to use stale reference ${reference}`); +} + +function countOverflow(reference: number) { + return new RangeError( + `Reference count overflow at slot ${reference & SLOT_MASK}`, + ); +} + +describe("JSObjectSpace", () => { + test("getObject rejects reference 0 and a slot never allocated", () => { + const space = new JSObjectSpace(); + expect(() => space.getObject(0)).toThrow(invalidReference(0)); + expect(() => space.getObject(SLOT_MASK)).toThrow( + invalidReference(SLOT_MASK), + ); + }); + + test("reference 1 is retain's reference to globalThis and outlives a balanced release", () => { + const space = new JSObjectSpace(); + expect(space.retain(globalThis)).toBe(1); + space.release(1); + expect(space.getObject(1)).toBe(globalThis); + }); + + test("retain's reference leaves the value map on its last release", () => { + const space = new JSObjectSpace(); + const value = {}; + const reference = space.retain(value); + space.release(reference); + const other = {}; + const reused = space.retain(other); + expect(reused & SLOT_MASK).toBe(reference & SLOT_MASK); + expect(space.getObject(reused)).toBe(other); + expect(space.getObject(space.retain(value))).toBe(value); + }); + + test("retainUnique returns a reference retain does not", () => { + const space = new JSObjectSpace(); + const value = {}; + const unique = space.retainUnique(value); + const shared = space.retain(value); + expect(shared).not.toBe(unique); + expect(space.getObject(unique)).toBe(value); + expect(space.getObject(shared)).toBe(value); + }); + + test("releasing a unique reference keeps retain's reference", () => { + const space = new JSObjectSpace(); + const value = {}; + const shared = space.retain(value); + space.release(space.retainUnique(value)); + expect(space.retain(value)).toBe(shared); + }); + + test("releasing a unique reference to a reused slot keeps retain's reference", () => { + const space = new JSObjectSpace(); + const value = {}; + const shared = space.retain(value); + const freed = space.retainUnique({}); + space.release(freed); + const unique = space.retainUnique(value); + expect(unique & SLOT_MASK).toBe(freed & SLOT_MASK); + space.release(unique); + expect(space.retain(value)).toBe(shared); + }); + + test("getObject, shareUnique, release and retainByRef reject a stale reference", () => { + const space = new JSObjectSpace(); + const stale = space.retainUnique({}); + space.release(stale); + const unique = space.retainUnique({}); + expect(unique & SLOT_MASK).toBe(stale & SLOT_MASK); + expect(() => space.getObject(stale)).toThrow(staleReference(stale)); + expect(() => space.shareUnique(stale)).toThrow(staleReference(stale)); + expect(() => space.release(stale)).toThrow(staleReference(stale)); + expect(() => space.retainByRef(stale)).toThrow(staleReference(stale)); + }); + + test("shareUnique returns a reference retain returned", () => { + const space = new JSObjectSpace(); + const value = {}; + const shared = space.retain(value); + expect(space.shareUnique(shared)).toBe(shared); + space.release(shared); + expect(() => space.getObject(shared)).toThrow(invalidReference(shared)); + }); + + test("shareUnique returns retain's reference and releases the unique one", () => { + const space = new JSObjectSpace(); + const value = {}; + const shared = space.retain(value); + const unique = space.retainUnique(value); + expect(space.shareUnique(unique)).toBe(shared); + expect(() => space.getObject(unique)).toThrow(invalidReference(unique)); + space.release(shared); + expect(space.getObject(shared)).toBe(value); + }); + + test("shareUnique releases one of two references to a unique slot", () => { + const space = new JSObjectSpace(); + const value = {}; + const shared = space.retain(value); + const unique = space.retainByRef(space.retainUnique(value)); + expect(space.shareUnique(unique)).toBe(shared); + expect(space.getObject(unique)).toBe(value); + space.release(unique); + expect(() => space.getObject(unique)).toThrow(invalidReference(unique)); + }); + + test("shareUnique adds the slot of a value retain has not seen to the value map", () => { + const space = new JSObjectSpace(); + const value = {}; + const unique = space.retainUnique(value); + expect(space.shareUnique(unique)).toBe(unique); + expect(space.retain(value)).toBe(unique); + }); + + test("a slot shareUnique adds leaves the value map on its last release", () => { + const space = new JSObjectSpace(); + const value = {}; + const shared = space.shareUnique(space.retainUnique(value)); + space.release(shared); + const other = {}; + expect(space.retain(other) & SLOT_MASK).toBe(shared & SLOT_MASK); + expect(space.getObject(space.retain(value))).toBe(value); + }); + + test("shareUnique keeps every reference to a unique slot it adds to the value map", () => { + const space = new JSObjectSpace(); + const value = {}; + const unique = space.retainByRef(space.retainUnique(value)); + expect(space.shareUnique(unique)).toBe(unique); + space.release(unique); + expect(space.getObject(unique)).toBe(value); + expect(space.retain(value)).toBe(unique); + space.release(unique); + space.release(unique); + expect(space.retain(value)).not.toBe(unique); + }); + + test("retain and retainUnique throw once every slot is allocated", () => { + const space = new JSObjectSpace(); + (space as any)._values.length = SLOT_MASK; + const value = {}; + const last = space.retainUnique(value); + expect(last).toBe(SLOT_MASK); + expect(space.getObject(last)).toBe(value); + const overflow = new RangeError( + `Reference slot overflow: ${SLOT_MASK + 1} exceeds ${SLOT_MASK}`, + ); + expect(() => space.retain({})).toThrow(overflow); + expect(() => space.retainUnique({})).toThrow(overflow); + }); + + test("a value retain returns holds at most 2^23 - 1 references", () => { + const space = new JSObjectSpace(); + const value = {}; + const reference = space.retain(value); + for (let i = 1; i < MAX_REFERENCES; i++) space.retainByRef(reference); + expect(() => space.retain(value)).toThrow(countOverflow(reference)); + expect(() => space.retainByRef(reference)).toThrow( + countOverflow(reference), + ); + for (let i = 1; i < MAX_REFERENCES; i++) space.release(reference); + expect(space.retain(value)).toBe(reference); + space.release(reference); + space.release(reference); + expect(() => space.getObject(reference)).toThrow( + invalidReference(reference), + ); + }); + + test("a unique slot holds at most 2^23 - 1 references", () => { + const space = new JSObjectSpace(); + const value = {}; + const unique = space.retainUnique(value); + for (let i = 1; i < MAX_REFERENCES; i++) space.retainByRef(unique); + expect(() => space.retainByRef(unique)).toThrow(countOverflow(unique)); + expect(space.retain(value)).not.toBe(unique); + for (let i = 1; i < MAX_REFERENCES; i++) space.release(unique); + expect(space.getObject(unique)).toBe(value); + space.release(unique); + expect(() => space.getObject(unique)).toThrow(invalidReference(unique)); + }); +}); diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift b/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift index 7bc799c64..91b0194ec 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift @@ -45,6 +45,11 @@ public class JSObject: Equatable, ExpressibleByDictionaryLiteral { self.init(id: swjs_create_object()) } + /// Creates an object for the value `ref` references. + public convenience init(_ ref: consuming JSUniqueRef) { + self.init(id: swjs_share_unique(JavaScriptObjectRef(bitPattern: ref.relinquish()))) + } + /// Creates a new object with the key-value pairs in the dictionary literal. /// /// - Parameter elements: A variadic list of key-value pairs where all keys are strings diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSUniqueRef.swift b/Sources/JavaScriptKit/FundamentalObjects/JSUniqueRef.swift new file mode 100644 index 000000000..b8ea536b4 --- /dev/null +++ b/Sources/JavaScriptKit/FundamentalObjects/JSUniqueRef.swift @@ -0,0 +1,53 @@ +import _CJavaScriptKit + +/// Owns one reference to a JavaScript value and releases it when destroyed. +@frozen public struct JSUniqueRef: ~Copyable { + @usableFromInline + internal let _id: Int32 + + #if _runtime(_multithreaded) + @usableFromInline + internal let ownerTid: Int32 + #endif + + @_spi(BridgeJS) + @inlinable + public var id: Int32 { + #if _runtime(_multithreaded) + precondition( + ownerTid == swjs_get_worker_thread_id_cached(), + "JSUniqueRef is being used on a thread other than the one that created it" + ) + #endif + return _id + } + + /// Takes over a reference the caller owns. + @_spi(BridgeJS) public init(adopting id: Int32) { + self._id = id + #if _runtime(_multithreaded) + self.ownerTid = swjs_get_worker_thread_id_cached() + #endif + } + + /// Returns the reference without releasing it. + @_spi(BridgeJS) public consuming func relinquish() -> Int32 { + let id = self.id + discard self + return id + } + + deinit { + #if _runtime(_multithreaded) + if ownerTid != swjs_get_worker_thread_id_cached() { + swjs_release_remote(ownerTid, JavaScriptObjectRef(bitPattern: _id)) + return + } + #endif + swjs_release(JavaScriptObjectRef(bitPattern: _id)) + } +} + +/// The id is valid only on the thread that created it. +@available(*, unavailable) +extension JSUniqueRef: Sendable {} diff --git a/Sources/_CJavaScriptKit/include/_CJavaScriptKit.h b/Sources/_CJavaScriptKit/include/_CJavaScriptKit.h index 3800a6d9e..58deeea8a 100644 --- a/Sources/_CJavaScriptKit/include/_CJavaScriptKit.h +++ b/Sources/_CJavaScriptKit/include/_CJavaScriptKit.h @@ -309,6 +309,13 @@ IMPORT_JS_FUNCTION(swjs_load_typed_array, void, (const JavaScriptObjectRef ref, /// @param ref The target JavaScript object. IMPORT_JS_FUNCTION(swjs_release, void, (const JavaScriptObjectRef ref)) +/// Consumes `ref` and returns the reference that `swjs_get_prop` and the other imports return for the same object, +/// so that `JSObject`s for one object have equal ids. +/// +/// @param ref A reference owned by the caller. +/// @returns A reference owned by the caller. +IMPORT_JS_FUNCTION(swjs_share_unique, JavaScriptObjectRef, (const JavaScriptObjectRef ref)) + /// Decrements reference count of `ref` retained by `SwiftRuntimeHeap` in `object_tid` thread. /// /// @param object_tid The TID of the thread that owns the target object. diff --git a/Tests/JavaScriptKitTests/JSUniqueRefTests.swift b/Tests/JavaScriptKitTests/JSUniqueRefTests.swift new file mode 100644 index 000000000..1e547b61a --- /dev/null +++ b/Tests/JavaScriptKitTests/JSUniqueRefTests.swift @@ -0,0 +1,45 @@ +@_spi(BridgeJS) import JavaScriptKit +import Testing + +@_extern(wasm, module: "JavaScriptKitTests", name: "retainUnique") +@_extern(c) +func retainUnique(_ ref: Int32) -> Int32 + +@_extern(wasm, module: "JavaScriptKitTests", name: "referenceCount") +@_extern(c) +func referenceCount(_ ref: Int32) -> Int32 + +@Suite struct JSUniqueRefTests { + @Test func droppingReleasesTheReference() { + let object = JSObject() + let ref = Self.uniqueRef(to: object) + let id = ref.id + #expect(referenceCount(id) == 1) + _ = consume ref + #expect(referenceCount(id) == 0) + } + + @Test func convertingReturnsTheObjectSwiftHolds() { + let object = JSObject() + let ref = Self.uniqueRef(to: object) + let uniqueID = ref.id + let converted = JSObject(ref) + #expect(converted == object) + #expect(referenceCount(uniqueID) == 0) + #expect(referenceCount(Int32(bitPattern: object.id)) == 2) + } + + @Test func convertingAValueOnlyJavaScriptHoldsKeepsItsReference() { + let holder: JSObject = ["value": JSObject().jsValue] + let ref = Self.uniqueRef(to: holder.value.object!) + let uniqueID = ref.id + let converted = JSObject(ref) + #expect(Int32(bitPattern: converted.id) == uniqueID) + #expect(referenceCount(uniqueID) == 1) + #expect(holder.value.object! == converted) + } + + private static func uniqueRef(to object: JSObject) -> JSUniqueRef { + JSUniqueRef(adopting: retainUnique(Int32(bitPattern: object.id))) + } +} diff --git a/Tests/prelude.mjs b/Tests/prelude.mjs index 6c53eb59f..d9ea5d1f8 100644 --- a/Tests/prelude.mjs +++ b/Tests/prelude.mjs @@ -232,6 +232,13 @@ export async function setupOptions(options, context) { importObject["JavaScriptEventLoopTestSupportTests"] = { "isMainThread": () => context.isMainThread, } + importObject["JavaScriptKitTests"] = { + "retainUnique": (ref) => _swift.memory.retainUnique(_swift.memory.getObject(ref)), + "referenceCount": (ref) => { + const state = _swift.memory._stateBySlot[ref & 0xffffff]; + return state >>> 24 === ref >>> 24 ? state & 0x7fffff : 0; + }, + }; const bridgeJSRuntimeTests = importObject["BridgeJSRuntimeTests"] || {}; bridgeJSRuntimeTests["runJsWorks"] = () => { const exports = getExports();