File tree 3 files changed +12
-24
lines changed
3 files changed +12
-24
lines changed Original file line number Diff line number Diff line change @@ -50,22 +50,29 @@ class SwiftRuntimeHeap {
50
50
allocHeap ( value : any ) {
51
51
const isObject = typeof value == "object"
52
52
if ( isObject && value . swjs_heap_id ) {
53
+ value . swjs_heap_rc ++ ;
53
54
return value . swjs_heap_id
54
55
}
55
56
const id = this . _heapNextKey ++ ;
56
57
this . _heapValues . set ( id , value )
57
- if ( isObject )
58
+ if ( isObject ) {
58
59
Reflect . set ( value , "swjs_heap_id" , id ) ;
60
+ Reflect . set ( value , "swjs_heap_rc" , 1 ) ;
61
+ }
59
62
return id
60
63
}
61
64
62
65
freeHeap ( ref : ref ) {
63
66
const value = this . _heapValues . get ( ref ) ;
64
67
const isObject = typeof value == "object"
65
- if ( isObject && value . swjs_heap_id ) {
68
+ if ( isObject ) {
69
+ const rc = -- value . swjs_heap_rc ;
70
+ if ( rc != 0 ) return ;
66
71
delete value . swjs_heap_id ;
72
+ this . _heapValues . delete ( ref )
73
+ } else {
74
+ this . _heapValues . delete ( ref )
67
75
}
68
- this . _heapValues . delete ( ref )
69
76
}
70
77
71
78
referenceHeap ( ref : ref ) {
Original file line number Diff line number Diff line change 1
1
import _CJavaScriptKit
2
2
3
- private struct Weak < T: AnyObject > {
4
- weak var ref : T ?
5
- }
6
-
7
- private var cache = [ UInt32 : Weak < JSObjectRef > ] ( )
8
-
9
3
@dynamicMemberLookup
10
4
public class JSObjectRef : Equatable {
11
5
internal var id : UInt32
12
6
init ( id: UInt32 ) {
13
7
self . id = id
14
8
}
15
9
16
- static func retrieve( id: UInt32 ) -> JSObjectRef {
17
- if id != 0 , let ref = cache [ id] ? . ref {
18
- return ref
19
- } else {
20
- let ref = JSObjectRef ( id: id)
21
- cache [ id] = Weak ( ref: ref)
22
- return ref
23
- }
24
- }
25
-
26
10
@_disfavoredOverload
27
11
public subscript( dynamicMember name: String ) -> ( ( JSValueConvertible . . . ) -> JSValue ) ? {
28
12
get {
@@ -62,10 +46,7 @@ public class JSObjectRef: Equatable {
62
46
static let _JS_Predef_Value_Global : UInt32 = 0
63
47
public static let global = JSObjectRef ( id: _JS_Predef_Value_Global)
64
48
65
- deinit {
66
- cache [ id] = nil
67
- _destroy_ref ( id)
68
- }
49
+ deinit { _destroy_ref ( id) }
69
50
70
51
public static func == ( lhs: JSObjectRef , rhs: JSObjectRef ) -> Bool {
71
52
return lhs. id == rhs. id
Original file line number Diff line number Diff line change @@ -111,7 +111,7 @@ extension RawJSValue: JSValueConvertible {
111
111
let string = String ( decodingCString: UnsafePointer ( buffer) , as: UTF8 . self)
112
112
return . string( string)
113
113
case JavaScriptValueKind_Object:
114
- return . object( JSObjectRef . retrieve ( id: UInt32 ( payload1) ) )
114
+ return . object( JSObjectRef ( id: UInt32 ( payload1) ) )
115
115
case JavaScriptValueKind_Null:
116
116
return . null
117
117
case JavaScriptValueKind_Undefined:
You can’t perform that action at this time.
0 commit comments