Skip to content

Commit 74610c2

Browse files
committed
Add an explicit retain() function and fix a ref counting bug
1 parent dde8cf2 commit 74610c2

File tree

4 files changed

+9
-0
lines changed

4 files changed

+9
-0
lines changed

Runtime/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ export class SwiftRuntime {
411411
// Call `.slice()` to copy the memory
412412
writeUint32(result_obj, this.heap.retain(array.slice()));
413413
},
414+
swjs_retain: (ref: ref) => {
415+
this.heap.retain(this.heap.referenceHeap(ref))
416+
},
414417
swjs_release: (ref: ref) => {
415418
this.heap.release(ref)
416419
}

Sources/JavaScriptKit/JSTypedArray.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class JSTypedArray<Element>: JSObjectRef, ExpressibleByArrayLiteral where
2121

2222
public init(length: Int) {
2323
super.init(id: Element.typedArrayClass.new(length).id)
24+
_retain(id)
2425
}
2526

2627
required public convenience init(arrayLiteral elements: Element...) {

Sources/JavaScriptKit/XcodeSupport.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import _CJavaScriptKit
7272
_: JavaScriptHostFuncRef,
7373
_: UnsafePointer<JavaScriptObjectRef>!
7474
) { fatalError() }
75+
func _retain(_: JavaScriptObjectRef) { fatalError() }
7576
func _release(_: JavaScriptObjectRef) { fatalError() }
7677
func _create_typed_array<T: TypedArrayElement>(
7778
_: JavaScriptTypedArrayKind,

Sources/_CJavaScriptKit/include/_CJavaScriptKit.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ __attribute__((__import_module__("javascript_kit"),
106106
_create_function(const JavaScriptHostFuncRef host_func_id,
107107
const JavaScriptObjectRef *func_ref_ptr);
108108

109+
__attribute__((__import_module__("javascript_kit"),
110+
__import_name__("swjs_retain"))) extern void
111+
_retain(const JavaScriptObjectRef ref);
112+
109113
__attribute__((__import_module__("javascript_kit"),
110114
__import_name__("swjs_release"))) extern void
111115
_release(const JavaScriptObjectRef ref);

0 commit comments

Comments
 (0)