/
47 ObjJ syntax super.js
111 lines (89 loc) · 2.05 KB
/
47 ObjJ syntax super.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
//
// Objective-J, JSTalk - like syntax
// super addon
//
/*
- (void)myMethod:(id)arg
{
// Standard argument, gotten trough arguments.callee
this.Super(arguments)
// ObjC like call
[super myMethod:arg]
// Translates to
this.Super(arguments, 'myMethod')
// Same for swizzling
[original myMethod:arg]
// ->
this.Original(arguments, 'myMethod')
}
*/
var wentThrough1 = false
var wentThrough2 = false
var wentThrough3 = false
var wentThrough4 = false
//
// Super test
//
class ObjJSuperTest1 < NSObject
{
- (int)paramLessMethod
{
return 3
}
- (int)method1:(int)a and2:(int)b
{
wentThrough1 = true
return a+b+1
}
}
class ObjJSuperTest2 < ObjJSuperTest1
{
- (int)paramLessMethod
{
return 4 + [super paramLessMethod]
}
- (int)superTestWith:(int)a and:(int)b
{
return [super method1:a and2:b]
}
- (int)method1:(int)a and2:(int)b
{
wentThrough2 = true
return a + b + [super method1:a and2:b] + 10
}
}
var o = [ObjJSuperTest2 instance]
wentThrough1 = wentThrough2 = false
var r = [o superTestWith:4 and:3]
if (r != (4+3+1)) throw 'ObjJ super syntax failed (1)'
if (!wentThrough1 || wentThrough2) throw 'ObjJ super syntax failed (2)'
wentThrough1 = wentThrough2 = false
var r = [o method1:10 and2:5]
if (r != (10+5+1+10+5+10)) throw 'ObjJ super syntax failed (3)'
if (!wentThrough1 || !wentThrough2) throw 'ObjJ super syntax failed (4)'
var r = [o paramLessMethod]
if (r != 7) throw 'ObjJ super syntax failed (5)'
//
// Swizzle test
//
class ObjJSuperTest3 < NSObject
{
- (int)addThis:(int)a andThat:(int)b
{
wentThrough3 = true
return a + b + 100
}
}
class ObjJSuperTest3
{
swizzle - (int)addThis:(int)a andThat:(int)b
{
wentThrough4 = true
return [original addThis:a andThat:b] + 1000
}
}
var o = [ObjJSuperTest3 instance]
var r = [o addThis:11 andThat:2]
if (r != (11+2+100+1000)) throw 'ObjJ super syntax failed (5)'
if (!wentThrough3 || !wentThrough4) throw 'ObjJ super syntax failed (6)'
o = null