Skip to content

Commit d64def7

Browse files
Remove manual reference counting
1 parent e0ef55f commit d64def7

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

Sources/JavaScriptKit/JSTypedArray.swift renamed to Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,40 @@ public protocol TypedArrayElement: JSValueConvertible, JSValueConstructible {
99
static var typedArrayClass: JSFunctionRef { get }
1010
}
1111

12-
public class JSTypedArray<Element>: JSObjectRef, ExpressibleByArrayLiteral where Element: TypedArrayElement {
12+
public class JSTypedArray<Element>: JSValueConvertible, ExpressibleByArrayLiteral where Element: TypedArrayElement {
13+
let ref: JSObject
14+
public func jsValue() -> JSValue {
15+
.object(ref)
16+
}
17+
1318
public subscript(_ index: Int) -> Element {
1419
get {
15-
return Element.construct(from: getJSValue(this: self, index: Int32(index)))!
20+
return Element.construct(from: getJSValue(this: ref, index: Int32(index)))!
1621
}
1722
set {
18-
setJSValue(this: self, index: Int32(index), value: newValue.jsValue())
23+
setJSValue(this: ref, index: Int32(index), value: newValue.jsValue())
1924
}
2025
}
26+
27+
public init(_ object: JSObject) {
28+
self.ref = object
29+
}
2130

22-
public init(length: Int) {
31+
public convenience init(length: Int) {
2332
let jsObject = Element.typedArrayClass.new(length)
24-
// _retain is necessary here because the JSObjectRef we used to create the array
25-
// goes out of scope and is deinitialized when this init() returns, causing
26-
// the JS side to decrement the object's reference count. JSTypedArray will also
27-
// call _release() when deinitialized because it inherits from JSObjectRef, so this
28-
// will not leak memory.
29-
_retain(jsObject.id)
30-
super.init(id: jsObject.id)
33+
self.init(jsObject)
3134
}
3235

3336
required public convenience init(arrayLiteral elements: Element...) {
3437
self.init(elements)
3538
}
3639

37-
public init(_ array: [Element]) {
40+
public convenience init(_ array: [Element]) {
3841
var resultObj = JavaScriptObjectRef()
3942
array.withUnsafeBufferPointer { ptr in
4043
_create_typed_array(Element.typedArrayKind, ptr.baseAddress!, Int32(array.count), &resultObj)
4144
}
42-
super.init(id: resultObj)
45+
self.init(JSObject(id: resultObj))
4346
}
4447

4548
public convenience init(_ stride: StrideTo<Element>) where Element: Strideable {

Sources/JavaScriptKit/XcodeSupport.swift

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

Sources/_CJavaScriptKit/include/_CJavaScriptKit.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ __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-
113109
__attribute__((__import_module__("javascript_kit"),
114110
__import_name__("swjs_release"))) extern void
115111
_release(const JavaScriptObjectRef ref);

0 commit comments

Comments
 (0)