13
13
14
14
NSMutableDictionary * arrayPrototype;
15
15
16
+ NSInteger compare (JawaObjectRef* o1, JawaObjectRef* o2, void * context) {
17
+ NSArray * ctxt = (__bridge NSArray *)context;
18
+ JawaExecutor* ex = [ctxt objectAtIndex: 0 ];
19
+ JawaObjectRef* comparator = [ctxt objectAtIndex: 1 ];
20
+ return [ex compare: o1 and: o2 with: comparator];
21
+ }
22
+
16
23
@implementation JawaArray
17
24
18
25
-(id )initIn : (JawaExecutor *)ex {
@@ -25,6 +32,7 @@ -(id)initIn:(JawaExecutor *)ex {
25
32
return self;
26
33
}
27
34
35
+
28
36
-(NSString *)description {
29
37
BOOL first = true ;
30
38
NSMutableString * ret = [NSMutableString stringWithString: @" [" ];
@@ -47,7 +55,7 @@ -(NSMutableString*) toJSON:(NSMutableString*)ret {
47
55
if (!first)
48
56
[ret appendString: @" ," ];
49
57
first = false ;
50
- if ([obj.object isMemberOfClass: [ NSMutableString class ]]) {
58
+ if ([obj.object isKindOfClass: [ NSString class ]]) {
51
59
[ret appendString: @" \" " ];
52
60
NSString * r = [[obj description ] stringByReplacingOccurrencesOfString: @" \" " withString: @" \\\" " ];
53
61
[ret appendString: r];
@@ -85,15 +93,15 @@ -(JawaObjectRef*)invokeBuiltin:(NSString*)funcName {
85
93
JawaObjectRef* start = [[self .executor.currentActivation lastObject ]objectForKey:@" start" ];
86
94
if (![start.object isMemberOfClass: [JawaNumber class ]])
87
95
[NSException raise: @" JawaScript Runtime Exception" format: @" start of slice() must be a number" ];
88
- NSUInteger startInt = ((NSNumber *)start.object ).unsignedIntValue ;
96
+ NSUInteger startInt = ((JawaNumber *)start.object ).unsignedIntValue ;
89
97
if (startInt >= self.elements .count )
90
98
[NSException raise: @" JawaScript Runtime Exception" format: @" start of slice() out of bound" ];
91
99
NSUInteger endInt = self.elements .count ;
92
100
JawaObjectRef* end = [[self .executor.currentActivation lastObject ]objectForKey:@" end" ];
93
101
if (end != nil ) {
94
- if (![end.object isKindOfClass: [ NSNumber class ]])
102
+ if (![end.object isMemberOfClass: [JawaNumber class ]])
95
103
[NSException raise: @" JawaScript Runtime Exception" format: @" end of slice() must be a number" ];
96
- endInt = ((NSNumber *)end.object ).unsignedIntValue ;
104
+ endInt = ((JawaNumber *)end.object ).unsignedIntValue ;
97
105
if (endInt > self.elements .count )
98
106
[NSException raise: @" JawaScript Runtime Exception" format: @" end of slice() out of bound" ];
99
107
}
@@ -119,6 +127,56 @@ -(JawaObjectRef*)invokeBuiltin:(NSString*)funcName {
119
127
}
120
128
return [JawaObjectRef RefWithString: ret in: self .executor];
121
129
}
130
+ // Array.pop()
131
+ case 3 : {
132
+ if (self.elements .count <= 0 )
133
+ return nil ;
134
+ JawaObjectRef* ret = [self .elements pointerAtIndex: self .elements.count - 1 ];
135
+ [self .elements removePointerAtIndex: self .elements.count - 1 ];
136
+ return ret;
137
+ }
138
+ // Array.push(item)
139
+ case 4 : {
140
+ JawaObjectRef* item = [[self .executor.currentActivation lastObject ]objectForKey:@" item" ];
141
+ [self .elements addPointer: (__bridge void *)item];
142
+ return [JawaObjectRef RefWithNumber: self .elements.count in: self .executor];
143
+ }
144
+ // Array.reverse()
145
+ case 5 : {
146
+ NSUInteger n = self.elements .count ;
147
+ for (NSUInteger i = 0 ; i < n / 2 ; i++) {
148
+ void *t = [self .elements pointerAtIndex: i];
149
+ [self .elements replacePointerAtIndex: i withPointer: [self .elements pointerAtIndex: n - i - 1 ]];
150
+ [self .elements replacePointerAtIndex: n - i - 1 withPointer: t];
151
+ }
152
+ return [JawaObjectRef RefWithJawaArray: self ];
153
+ }
154
+ // Array.shift()
155
+ case 6 : {
156
+ if (self.elements .count <= 0 )
157
+ return nil ;
158
+ JawaObjectRef* item = [self .elements pointerAtIndex: 0 ];
159
+ [self .elements removePointerAtIndex: 0 ];
160
+ return item;
161
+ }
162
+ // Array.sort(compareFunction)
163
+ case 7 : {
164
+ JawaObjectRef* compareFunction = [[self .executor.currentActivation lastObject ]objectForKey:@" compareFunction" ];
165
+ if (compareFunction == nil )
166
+ compareFunction = [JawaObjectRef RefIn: self .executor];
167
+ NSArray * tempArray = self.elements .allObjects ;
168
+ NSArray * sorted = [tempArray sortedArrayUsingFunction: compare context: (__bridge void * _Nullable)(@[self .executor, compareFunction])];
169
+ for (NSUInteger i = 0 ; i < sorted.count ; i++) {
170
+ [self .elements replacePointerAtIndex: i withPointer: (__bridge void * _Nullable)([sorted objectAtIndex: i])];
171
+ }
172
+ return [JawaObjectRef RefWithJawaArray: self ];
173
+ }
174
+ // Array.unshift()
175
+ case 8 : {
176
+ JawaObjectRef* item = [[self .executor.currentActivation lastObject ]objectForKey:@" item" ];
177
+ [self .elements insertPointer: (__bridge void *)item atIndex: 0 ];
178
+ return [JawaObjectRef RefWithNumber: self .elements.count in: self .executor];
179
+ }
122
180
default :
123
181
[NSException raise: @" JavaScript Runtime Exception" format: @" %@ not implemented yet" , funcName];
124
182
}
0 commit comments