Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Plugins/PackageToJS/Templates/runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
73 changes: 53 additions & 20 deletions Plugins/PackageToJS/Templates/runtime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions Runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
79 changes: 56 additions & 23 deletions Runtime/src/object-heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any, number>;
Expand All @@ -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}`,
);
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Loading
Loading