forked from parmanoir/jscocoa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path36 safe dealloc.js
142 lines (100 loc) · 3.47 KB
/
36 safe dealloc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// Safe dealloc
// Allow -(void)dealloc to be overloaded. As dealloc is called during JS GC, no js code can be called then.
// Overloaded dealloc will be called in the next run loop cycle.
//
// Split call disabled by default since ObjJ syntax
var useSplitCall = __jsc__.useSplitCall
__jsc__.useSplitCall = true
var safe1Called = 0
var safe2Called = 0
var bindingCleaned
//
// Safe dealloc test completion
// AT THIS POINT OBJECTS WILL STILL BE ALIVE, as JavscriptCore is still retaining the JSCocoaPrivateObjects holding them.
// They will be deallocated on next GC cycle.
//
function checkEndTest36()
{
if (safe1Called == 2 && safe2Called == 1 && bindingCleaned)
{
completeDelayedTest('36 safe dealloc', true)
// JSCocoa.logInstanceStats
// __jsc__.garbageCollect
// JSCocoa.logInstanceStats
}
}
// This js dealloc method will be called in the next run loop cycle, then the ObjC release method, deallocating the object
class SafeDeallocTest < NSObject
{
- (void)dealloc
{
// log('safe dealloc called')
this.Super(arguments)
safe1Called++
checkEndTest36()
}
}
// js deallocs are standard methods and can be derived.
class SafeDeallocTest2 < SafeDeallocTest
{
- (void)dealloc
{
// log('SECOND safe dealloc called')
this.Super(arguments)
safe2Called++
}
}
// Test safe dealloc
var o1 = SafeDeallocTest.instance
var o2 = SafeDeallocTest2.instance
o1.safeDealloc = o2.safeDealloc = true
// Need to remove this : needed to put __jsc__ ivar in instances
// o1.blaaaaaaaaaaah = 'hello'
// o2.blaaaaaaaaaaah = 'hello'
o1 = null
o2 = null
// Test binding dealloc
// Bindinds safe dealloc test
class BindingsSafeDeallocSource < NSObject
{
Key sourceValue
}
class BindingsSafeDeallocTarget < NSObject
{
- (void)dealloc
{
// log('exposedBindings=' + this.exposedBindings)
// log('dealloc! ' + this)
this.unbind('targetValue')
bindingCleaned = true
checkEndTest36()
}
Key targetValue
}
if (!('bindingsAlreadyTested2' in this) && !NSApplication.sharedApplication.delegate.bindingsAlreadyTested2)
{
var oSource = BindingsSafeDeallocSource.instance
var oTarget = BindingsSafeDeallocTarget.instance
// log('oSource=' + oSource + ' rc=' + oSource.retainCount)
// log('oTarget=' + oTarget + ' rc=' + oTarget.retainCount)
// - (void)bind:(NSString *)binding toObject:(id)observable withKeyPath:(NSString *)keyPath options:(NSDictionary *)options; // placeholders and value transformers are specified in options dictionary
oTarget.bind_toObject_withKeyPath_options('targetValue', oSource, 'sourceValue', null)
oSource.set({ value : 'hello', forKey : 'sourceValue' })
// log('t=' + oTarget.targetValue)
if (oTarget.targetValue != 'hello') throw '(safe dealloc) invalid binding (1)'
oSource.set({ value : 'world', forKey : 'sourceValue' })
// log('t=' + oTarget.targetValue)
if (oTarget.targetValue != 'world') throw '(safe dealloc) invalid binding (2)'
oSource = null
oTarget = null
}
bindingsAlreadyTested2 = true
NSApplication.sharedApplication.delegate.bindingsAlreadyTested2 = true
// Force GC to trigger safe dealloc
__jsc__.garbageCollect
registerDelayedTest('36 safe dealloc')
// careful of context being deallocated : context need to be deallocated AFTER safe dealloc is called
// ^disabled for now. Any safe dealloc will have to be called before JSCocoaController is deallocated
// throw ('bindings auto remove')
__jsc__.useSplitCall = useSplitCall