forked from parmanoir/jscocoa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9 auto setter.js
69 lines (56 loc) · 1.89 KB
/
9 auto setter.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
/*
auto setter
object.setValue(v)
object.value = v
*/
// JSCocoaController.sharedController.evalJSFile(NSBundle.mainBundle.bundlePath + '/Contents/Resources/class.js')
// Define a new class
var newClass = JSCocoaController.createClass_parentClass("AutoSetterTester", "NSObject")
var globalValue = null
function getterCall()
{
// JSCocoaController.log('Calling getter')
return globalValue
}
function setterCall(newValue)
{
// JSCocoaController.log('Calling setter with newValue=' + newValue)
globalValue = newValue
}
//
// Test bool
//
var encoding = '*'
var encodingName = reverseEncodings[encoding]
// Add getter
var fn = getterCall
var fnName = 'myValue'
var fnEncoding = objc_encoding('charpointer')
JSCocoaController.addInstanceMethod_class_jsFunction_encoding(fnName, AutoSetterTester, fn, fnEncoding)
// Add setter
var fn = setterCall
var fnName = 'setMyValue:'
var fnEncoding = objc_encoding('void', 'charpointer')
// JSCocoaController.log('e=' + fnEncoding);
JSCocoaController.addInstanceMethod_class_jsFunction_encoding(fnName, AutoSetterTester, fn, fnEncoding)
var o = AutoSetterTester.alloc.init
o.release
// Try standard setter as test
var someNewValue = 'hello world !'
o.setMyValue(someNewValue)
var r1 = o.myValue
var r2 = o.myValue
// JSCocoaController.log('r1=' + r1 + '*')
// JSCocoaController.log('r2=' + r2 + '*')
if (r1 != someNewValue) throw 'setter 1 failed - got ' + r1 + ', expecting ' + someNewValue + '.'
if (r2 != someNewValue) throw 'setter 2 failed - got ' + r2 + ', expecting ' + someNewValue + '.'
// Try auto setter
var someNewValue = 'bonjour monde !'
o.myValue = someNewValue
var r1 = o.myValue
var r2 = o.myValue
// JSCocoaController.log('auto r1=' + r1)
// JSCocoaController.log('auto r2=' + r2)
if ((r1) != someNewValue) throw 'auto setter 1 failed'
if ((r2) != someNewValue) throw 'auto setter 2 failed'
o = null