-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy path55 javascript method override.js
86 lines (54 loc) · 2.07 KB
/
55 javascript method override.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
/*
Use Javascript functions to override behaviour of existing ObjC objects
(This override exists only on the JSCocoa side and lets the ObjC methods intact)
*/
var canSet = __jsc__.canSetOnBoxedObjects
__jsc__.canSetOnBoxedObjects = true
var wentThrough1, wentThrough2, wentThrough3
@implementation JavascriptMethodOverride : NSObject
- (int)add:(int)a
{
wentThrough1 = true
return a + 10
}
- (int)add:(int)a and:(int)b
{
wentThrough2 = true
return a + b + 20
}
@end
var o = JavascriptMethodOverride.instance
o.add = function ()
{
wentThrough3 = true
if (arguments.length == 1) return this.add_(arguments[0])
if (arguments.length == 2) return this.add_and_(arguments[0], arguments[1])
return null
}
wentThrough1 = wentThrough2 = wentThrough3 = false
if (o.add_(15) != 25) throw 'Javascript method override failed (1)'
if (!(wentThrough1 && !wentThrough2 && !wentThrough3)) throw 'Javascript method override failed (2)'
wentThrough1 = wentThrough2 = wentThrough3 = false
if (o.add_and_(5, 7) != 32) throw 'Javascript method override failed (3)'
if (!(!wentThrough1 && wentThrough2 && !wentThrough3)) throw 'Javascript method override failed (4)'
wentThrough1 = wentThrough2 = wentThrough3 = false
if (o.add(15) != 25) throw 'Javascript method override failed (5)'
if (!(wentThrough1 && !wentThrough2 && wentThrough3)) throw 'Javascript method override failed (6)'
wentThrough1 = wentThrough2 = wentThrough3 = false
if (o.add(5, 7) != 32) throw 'Javascript method override failed (7)'
if (!(!wentThrough1 && wentThrough2 && wentThrough3)) throw 'Javascript method override failed (8)'
o = null
//
// Test on a raw ObjC object, not a JSCocoa derived one
//
var wentThrough4 = false
o = NSObject.instance
o.respondsToSelector = function (sel)
{
wentThrough4 = true
return this.respondsToSelector_(sel)
}
o.respondsToSelector('hello')
if (!wentThrough4) throw 'Javascript method override failed (9)'
o = null
__jsc__.canSetOnBoxedObjects = canSet