@@ -32,7 +32,9 @@ enum JavaScriptValueKind {
32
32
}
33
33
34
34
class SwiftRuntimeHeap {
35
- private _heapValues : Map < number , any > ;
35
+ private _heapValueById : Map < number , any > ;
36
+ private _heapIdByValue : Map < any , number > ;
37
+ private _heapRcByValue : Map < any , number > ;
36
38
private _heapNextKey : number ;
37
39
38
40
constructor ( ) {
@@ -42,43 +44,54 @@ class SwiftRuntimeHeap {
42
44
} else if ( typeof global !== "undefined" ) {
43
45
_global = global
44
46
}
45
- this . _heapValues = new Map ( ) ;
46
- this . _heapValues . set ( 0 , _global ) ;
47
+ this . _heapValueById = new Map ( ) ;
48
+ this . _heapValueById . set ( 0 , _global ) ;
49
+
50
+ this . _heapIdByValue = new Map ( ) ;
51
+ this . _heapValueById . set ( _global , 0 ) ;
52
+
53
+ this . _heapRcByValue = new Map ( ) ;
54
+
47
55
// Note: 0 is preserved for global
48
56
this . _heapNextKey = 1 ;
49
57
}
50
58
51
59
allocHeap ( value : any ) {
52
- const isObject = typeof value == "object"
53
- if ( isObject && value . swjs_heap_id ) {
54
- value . swjs_heap_rc ++ ;
55
- return value . swjs_heap_id
60
+ const isObject = typeof value == "object" ;
61
+ const heapId = this . _heapIdByValue . get ( value ) ;
62
+ if ( isObject && heapId ) {
63
+ const rc = this . _heapRcByValue . get ( value ) ! ;
64
+ this . _heapRcByValue . set ( value , rc + 1 ) ;
65
+ return heapId
56
66
}
57
67
const id = this . _heapNextKey ++ ;
58
- this . _heapValues . set ( id , value )
68
+ this . _heapValueById . set ( id , value )
59
69
if ( isObject ) {
60
- Reflect . set ( value , "swjs_heap_id" , id ) ;
61
- Reflect . set ( value , "swjs_heap_rc" , 1 ) ;
70
+ this . _heapIdByValue . set ( value , id ) ;
71
+ this . _heapRcByValue . set ( value , 1 ) ;
62
72
}
63
73
return id
64
74
}
65
75
66
76
freeHeap ( ref : ref ) {
67
- const value = this . _heapValues . get ( ref ) ;
77
+ const value = this . _heapValueById . get ( ref ) ;
68
78
const isObject = typeof value == "object"
69
79
if ( isObject ) {
70
- const rc = -- value . swjs_heap_rc ;
71
- if ( rc != 0 ) return ;
72
- delete value . swjs_heap_id ;
73
- delete value . swjs_heap_rc ;
74
- this . _heapValues . delete ( ref )
80
+ const rc = this . _heapRcByValue . get ( value ) ! ;
81
+ const newRc = rc - 1
82
+ this . _heapRcByValue . set ( value , newRc ) ;
83
+ if ( newRc != 0 ) return ;
84
+
85
+ this . _heapIdByValue . delete ( value ) ;
86
+ this . _heapRcByValue . delete ( value ) ;
87
+ this . _heapValueById . delete ( ref )
75
88
} else {
76
- this . _heapValues . delete ( ref )
89
+ this . _heapValueById . delete ( ref )
77
90
}
78
91
}
79
92
80
93
referenceHeap ( ref : ref ) {
81
- return this . _heapValues . get ( ref )
94
+ return this . _heapValueById . get ( ref )
82
95
}
83
96
}
84
97
0 commit comments