Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,27 @@ Object_Conversion: do {
} catch {
print(error)
}

ObjectRef_Lifetime: do {
// ```js
// global.globalObject1 = {
// "prop_1": {
// "nested_prop": 1,
// },
// "prop_2": 2,
// "prop_3": true,
// "prop_4": [
// 3, 4, "str_elm_1", 5,
// ],
// ...
// }
// ```

let identity = JSClosure { $0[0] }
let ref1 = getJSValue(this: .global, name: "globalObject1").object!
let ref2 = identity(ref1).object!
try expectEqual(ref1.prop_2, .number(2))
try expectEqual(ref2.prop_2, .number(2))
} catch {
print(error)
}
13 changes: 10 additions & 3 deletions Runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,29 @@ class SwiftRuntimeHeap {
allocHeap(value: any) {
const isObject = typeof value == "object"
if (isObject && value.swjs_heap_id) {
value.swjs_heap_rc++;
return value.swjs_heap_id
}
const id = this._heapNextKey++;
this._heapValues.set(id, value)
if (isObject)
if (isObject) {
Reflect.set(value, "swjs_heap_id", id);
Reflect.set(value, "swjs_heap_rc", 1);
}
return id
}

freeHeap(ref: ref) {
const value = this._heapValues.get(ref);
const isObject = typeof value == "object"
if (isObject && value.swjs_heap_id) {
if (isObject) {
const rc = --value.swjs_heap_rc;
if (rc != 0) return;
delete value.swjs_heap_id;
this._heapValues.delete(ref)
} else {
this._heapValues.delete(ref)
}
this._heapValues.delete(ref)
}

referenceHeap(ref: ref) {
Expand Down