-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
KrollContext.m
980 lines (855 loc) Β· 31.9 KB
/
KrollContext.m
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2018 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#import "KrollContext.h"
#import "KrollCallback.h"
#import "KrollObject.h"
#import "KrollTimerManager.h"
#import "TiApp.h"
#import "TiLocale.h"
#import "TiUtils.h"
#import "TiExceptionHandler.h"
#include <pthread.h>
static unsigned short KrollContextCount = 0;
static pthread_mutex_t KrollEntryLock;
@implementation KrollUnprotectOperation
- (id)initWithContext:(JSContextRef)newContext withJsobject:(JSObjectRef)newFirst
{
return [self initWithContext:newContext withJsobject:newFirst andJsobject:NULL];
}
- (id)initWithContext:(JSContextRef)newContext withJsobject:(JSObjectRef)newFirst andJsobject:(JSObjectRef)newSecond
{
self = [super init];
if (self != nil) {
jsContext = newContext;
firstObject = newFirst;
secondObject = newSecond;
}
return self;
}
- (void)main
{
JSValueUnprotect(jsContext, firstObject);
if (secondObject != NULL) {
JSValueUnprotect(jsContext, secondObject);
}
}
@end
@implementation KrollInvocation
- (id)initWithTarget:(id)target_ method:(SEL)method_ withObject:(id)obj_ condition:(NSCondition *)condition_
{
if (self = [super init]) {
target = [target_ retain];
method = method_;
obj = [obj_ retain];
condition = [condition_ retain];
}
return self;
}
- (id)initWithTarget:(id)target_ method:(SEL)method_ withObject:(id)obj_ callback:(id)callback_ selector:(SEL)selector_
{
if (self = [super init]) {
target = [target_ retain];
method = method_;
obj = [obj_ retain];
notify = [callback_ retain];
notifySelector = selector_;
}
return self;
}
- (void)dealloc
{
[target release];
[obj release];
[condition release];
[notify release];
[super dealloc];
}
- (void)invoke:(KrollContext *)context
{
pthread_mutex_lock(&KrollEntryLock);
@try {
if (target != nil) {
[target performSelector:method withObject:obj withObject:context];
}
if (condition != nil) {
[condition lock];
[condition signal];
[condition unlock];
}
if (notify != nil) {
[notify performSelector:notifySelector];
}
}
@catch (NSException *e) {
@throw e;
}
@finally {
pthread_mutex_unlock(&KrollEntryLock);
}
}
@end
JSContextRef appJsContextRef = NULL;
KrollContext *appJsKrollContext = nil;
KrollContext *GetKrollContext(JSContextRef context)
{
if (context == appJsContextRef) {
return appJsKrollContext;
}
static const char *krollNS = "Kroll";
JSGlobalContextRef globalContext = JSContextGetGlobalContext(context);
JSObjectRef global = JSContextGetGlobalObject(globalContext);
JSStringRef string = JSStringCreateWithUTF8CString(krollNS);
JSValueRef value = JSObjectGetProperty(globalContext, global, string, NULL);
KrollContext *ctx = (KrollContext *)JSObjectGetPrivate(JSValueToObject(globalContext, value, NULL));
JSStringRelease(string);
return ctx;
}
JSValueRef ThrowException(JSContextRef ctx, NSString *message, JSValueRef *exception)
{
JSGlobalContextRef globalContextRef = JSContextGetGlobalContext(ctx);
JSContext *context = [JSContext contextWithJSGlobalContextRef:globalContextRef];
JSValue *value = [JSValue valueWithNewErrorFromMessage:message inContext:context];
*exception = value.JSValueRef;
return JSValueMakeUndefined(ctx);
}
static JSValueRef CommonJSRequireCallback(JSContextRef jsContext, JSObjectRef jsFunction, JSObjectRef jsThis, size_t argCount,
const JSValueRef args[], JSValueRef *exception)
{
if (argCount != 1) {
return ThrowException(jsContext, @"invalid number of arguments", exception);
}
KrollContext *ctx = GetKrollContext(jsContext);
id path = [KrollObject toID:ctx value:args[0]];
@try {
id result = [ctx.delegate require:ctx path:path];
return [KrollObject toValue:ctx value:result];
}
@catch (NSException *e) {
return ThrowException(jsContext, [e reason], exception);
}
}
static JSValueRef LCallback(JSContextRef jsContext, JSObjectRef jsFunction, JSObjectRef jsThis, size_t argCount,
const JSValueRef args[], JSValueRef *exception)
{
if (argCount < 1) {
return ThrowException(jsContext, @"invalid number of arguments", exception);
}
KrollContext *ctx = GetKrollContext(jsContext);
NSString *key = [KrollObject toID:ctx value:args[0]];
NSString *comment = nil;
// ignore non-String default values
if (argCount > 1) {
id defaultValue = [KrollObject toID:ctx value:args[1]];
if ([defaultValue isKindOfClass:[NSString class]]) {
comment = (NSString *)defaultValue;
}
}
@try {
id result = [TiLocale getString:key comment:comment];
return [KrollObject toValue:ctx value:result];
}
@catch (NSException *e) {
return ThrowException(jsContext, [e reason], exception);
}
}
static JSValueRef AlertCallback(JSContextRef jsContext, JSObjectRef jsFunction, JSObjectRef jsThis, size_t argCount,
const JSValueRef args[], JSValueRef *exception)
{
if (argCount < 1) {
return ThrowException(jsContext, @"invalid number of arguments", exception);
}
KrollContext *ctx = GetKrollContext(jsContext);
NSString *message = [TiUtils stringValue:[KrollObject toID:ctx value:args[0]]];
[[[TiApp app] controller] incrementActiveAlertControllerCount];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
[[[TiApp app] controller] decrementActiveAlertControllerCount];
}]];
[[TiApp app] showModalController:alert animated:YES];
return JSValueMakeUndefined(jsContext);
}
static JSValueRef StringFormatCallback(JSContextRef jsContext, JSObjectRef jsFunction, JSObjectRef jsThis, size_t argCount,
const JSValueRef args[], JSValueRef *exception)
{
if (argCount < 2) {
return ThrowException(jsContext, @"invalid number of arguments", exception);
}
KrollContext *ctx = GetKrollContext(jsContext);
NSString *format = [KrollObject toID:ctx value:args[0]];
#if TARGET_IPHONE_SIMULATOR
// convert string references to objects
format = [format stringByReplacingOccurrencesOfString:@"%@" withString:@"%@_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%s" withString:@"%@_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%1$s" withString:@"%1$@_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%2$s" withString:@"%2$@_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%3$s" withString:@"%3$@_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%4$s" withString:@"%4$@_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%5$s" withString:@"%5$@_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%6$s" withString:@"%6$@_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%7$s" withString:@"%7$@_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%8$s" withString:@"%8$@_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%9$s" withString:@"%9$@_TIDELIMITER_"];
// we're dealing with double, so convert so that it formats right
format = [format stringByReplacingOccurrencesOfString:@"%d" withString:@"%1.0f_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%1$d" withString:@"%1$1.0f_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%2$d" withString:@"%2$1.0f_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%3$d" withString:@"%3$1.0f_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%4$d" withString:@"%4$1.0f_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%5$d" withString:@"%5$1.0f_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%6$d" withString:@"%6$1.0f_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%7$d" withString:@"%7$1.0f_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%8$d" withString:@"%8$1.0f_TIDELIMITER_"];
format = [format stringByReplacingOccurrencesOfString:@"%9$d" withString:@"%9$1.0f_TIDELIMITER_"];
NSArray *formatArray = [format componentsSeparatedByString:@"_TIDELIMITER_"];
NSUInteger formatCount = [formatArray count];
NSMutableString *result = [[NSMutableString alloc] init];
size_t lastArgIndex = 0;
@try {
for (size_t x = 1; (x < argCount) && (x <= formatCount); x++) {
NSString *theFormat = [formatArray objectAtIndex:(x - 1)];
JSValueRef valueRef = args[x];
if (JSValueIsString(jsContext, valueRef) || JSValueIsObject(jsContext, valueRef)) {
id theResult = [KrollObject toID:ctx value:valueRef];
[result appendString:[NSString stringWithFormat:theFormat, theResult]];
} else if (JSValueIsNumber(jsContext, valueRef)) {
double theResult = JSValueToNumber(jsContext, valueRef, NULL);
[result appendString:[NSString stringWithFormat:theFormat, theResult]];
} else if (JSValueIsBoolean(jsContext, valueRef)) {
bool theResult = JSValueToBoolean(jsContext, valueRef);
[result appendString:[NSString stringWithFormat:theFormat, theResult]];
}
lastArgIndex = x;
}
if (lastArgIndex < formatCount) {
// Append any remaining format components
[result appendString:[[formatArray subarrayWithRange:NSMakeRange(lastArgIndex, formatCount - lastArgIndex)] componentsJoinedByString:@""]];
}
JSValueRef value = [KrollObject toValue:ctx value:result];
[result release];
return value;
}
@catch (NSException *e) {
return ThrowException(jsContext, [e reason], exception);
}
#else
// convert string references to objects
format = [format stringByReplacingOccurrencesOfString:@"%s" withString:@"%@"];
format = [format stringByReplacingOccurrencesOfString:@"%1$s" withString:@"%1$@"];
format = [format stringByReplacingOccurrencesOfString:@"%2$s" withString:@"%2$@"];
format = [format stringByReplacingOccurrencesOfString:@"%3$s" withString:@"%3$@"];
format = [format stringByReplacingOccurrencesOfString:@"%4$s" withString:@"%4$@"];
format = [format stringByReplacingOccurrencesOfString:@"%5$s" withString:@"%5$@"];
format = [format stringByReplacingOccurrencesOfString:@"%6$s" withString:@"%6$@"];
format = [format stringByReplacingOccurrencesOfString:@"%7$s" withString:@"%7$@"];
format = [format stringByReplacingOccurrencesOfString:@"%8$s" withString:@"%8$@"];
format = [format stringByReplacingOccurrencesOfString:@"%9$s" withString:@"%9$@"];
// we're dealing with double, so convert so that it formats right
format = [format stringByReplacingOccurrencesOfString:@"%d" withString:@"%1.0f"];
format = [format stringByReplacingOccurrencesOfString:@"%1$d" withString:@"%1$1.0f"];
format = [format stringByReplacingOccurrencesOfString:@"%2$d" withString:@"%2$1.0f"];
format = [format stringByReplacingOccurrencesOfString:@"%3$d" withString:@"%3$1.0f"];
format = [format stringByReplacingOccurrencesOfString:@"%4$d" withString:@"%4$1.0f"];
format = [format stringByReplacingOccurrencesOfString:@"%5$d" withString:@"%5$1.0f"];
format = [format stringByReplacingOccurrencesOfString:@"%6$d" withString:@"%6$1.0f"];
format = [format stringByReplacingOccurrencesOfString:@"%7$d" withString:@"%7$1.0f"];
format = [format stringByReplacingOccurrencesOfString:@"%8$d" withString:@"%8$1.0f"];
format = [format stringByReplacingOccurrencesOfString:@"%9$d" withString:@"%9$1.0f"];
@try {
int size = 0;
// we have to walk each type to detect the right size and alignment
for (size_t x = 1; x < argCount; x++) {
JSValueRef valueRef = args[x];
if (JSValueIsString(jsContext, valueRef) || JSValueIsObject(jsContext, valueRef)) {
size += sizeof(id);
} else if (JSValueIsNumber(jsContext, valueRef)) {
size += sizeof(double);
} else if (JSValueIsBoolean(jsContext, valueRef)) {
size += sizeof(bool);
}
}
void *argList = malloc(size);
void *bm = argList; // copy pointer since we move the other forward
for (size_t x = 1; x < argCount; x++) {
JSValueRef valueRef = args[x];
if (JSValueIsString(jsContext, valueRef) || JSValueIsObject(jsContext, valueRef)) {
(*(id *)argList) = [KrollObject toID:ctx value:valueRef];
argList += sizeof(id);
} else if (JSValueIsNumber(jsContext, valueRef)) {
(*(double *)argList) = JSValueToNumber(jsContext, valueRef, NULL);
argList += sizeof(double);
} else if (JSValueIsBoolean(jsContext, valueRef)) {
(*(bool *)argList) = JSValueToBoolean(jsContext, valueRef);
argList += sizeof(bool);
}
}
NSString *result = [[NSString alloc] initWithFormat:format arguments:bm];
JSValueRef value = [KrollObject toValue:ctx value:result];
free(bm);
[result release];
return value;
}
@catch (NSException *e) {
return ThrowException(jsContext, [e reason], exception);
}
#endif
}
static JSValueRef StringFormatDateCallback(JSContextRef jsContext, JSObjectRef jsFunction, JSObjectRef jsThis, size_t argCount,
const JSValueRef args[], JSValueRef *exception)
{
if (argCount < 1) {
return ThrowException(jsContext, @"invalid number of arguments", exception);
}
KrollContext *ctx = GetKrollContext(jsContext);
NSDate *date = [KrollObject toID:ctx value:args[0]];
NSDateFormatterStyle style = NSDateFormatterShortStyle;
if (argCount > 1) {
NSString *s = [KrollObject toID:ctx value:args[1]];
if ([s isEqualToString:@"short"]) {
// default
} else if ([s isEqualToString:@"medium"]) {
style = NSDateFormatterMediumStyle;
} else if ([s isEqualToString:@"long"]) {
style = NSDateFormatterLongStyle;
} else if ([s isEqualToString:@"full"]) {
style = NSDateFormatterFullStyle;
}
}
@try {
NSString *result = [NSDateFormatter localizedStringFromDate:date dateStyle:style timeStyle:NSDateFormatterNoStyle];
JSValueRef value = [KrollObject toValue:ctx value:result];
return value;
}
@catch (NSException *e) {
return ThrowException(jsContext, [e reason], exception);
}
}
static JSValueRef StringFormatTimeCallback(JSContextRef jsContext, JSObjectRef jsFunction, JSObjectRef jsThis, size_t argCount,
const JSValueRef args[], JSValueRef *exception)
{
if (argCount < 1) {
return ThrowException(jsContext, @"invalid number of arguments", exception);
}
KrollContext *ctx = GetKrollContext(jsContext);
NSDate *date = [KrollObject toID:ctx value:args[0]];
NSDateFormatterStyle style = NSDateFormatterShortStyle;
if (argCount > 1) {
NSString *s = [KrollObject toID:ctx value:args[1]];
if ([s isEqualToString:@"short"]) {
// default
} else if ([s isEqualToString:@"medium"]) {
style = NSDateFormatterMediumStyle;
} else if ([s isEqualToString:@"long"]) {
style = NSDateFormatterLongStyle;
}
}
@try {
NSString *result = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterNoStyle timeStyle:style];
JSValueRef value = [KrollObject toValue:ctx value:result];
return value;
}
@catch (NSException *e) {
return ThrowException(jsContext, [e reason], exception);
}
}
static JSValueRef StringFormatCurrencyCallback(JSContextRef jsContext, JSObjectRef jsFunction, JSObjectRef jsThis, size_t argCount,
const JSValueRef args[], JSValueRef *exception)
{
if (argCount < 1) {
return ThrowException(jsContext, @"invalid number of arguments", exception);
}
KrollContext *ctx = GetKrollContext(jsContext);
NSNumber *number = [KrollObject toID:ctx value:args[0]];
@try {
NSString *result = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterCurrencyStyle];
JSValueRef value = [KrollObject toValue:ctx value:result];
return value;
}
@catch (NSException *e) {
return ThrowException(jsContext, [e reason], exception);
}
}
static JSValueRef StringFormatDecimalCallback(JSContextRef jsContext, JSObjectRef jsFunction, JSObjectRef jsThis, size_t argCount,
const JSValueRef args[], JSValueRef *exception)
{
if (argCount < 1) {
return ThrowException(jsContext, @"invalid number of arguments", exception);
}
KrollContext *ctx = GetKrollContext(jsContext);
NSNumber *number = [KrollObject toID:ctx value:args[0]];
@try {
NSString *result;
NSLocale *locale = nil;
NSString *formatString = nil;
if (argCount > 1) {
NSString *arg = [KrollObject toID:ctx value:args[1]];
if ([arg rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"0#.,"]].location != NSNotFound) {
formatString = arg;
} else {
locale = [[[NSLocale alloc] initWithLocaleIdentifier:arg] autorelease];
}
}
// If locale is nil, either: Single argument, or the second arg is format string and not locale ID.
if (locale == nil) {
locale = [NSLocale currentLocale];
}
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setLocale:locale];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
// Format handling to match the extremely vague android specs
if (argCount == 3) {
formatString = [KrollObject toID:ctx value:args[2]];
}
if (formatString != nil) {
NSArray *formats = [formatString componentsSeparatedByString:@";"];
[formatter setPositiveFormat:[formats objectAtIndex:0]];
if ([formats count] > 1) {
[formatter setNegativeFormat:[formats objectAtIndex:1]];
} else {
[formatter setNegativeFormat:[formats objectAtIndex:0]];
}
}
result = [formatter stringFromNumber:number];
JSValueRef value = [KrollObject toValue:ctx value:result];
return value;
}
@catch (NSException *e) {
return ThrowException(jsContext, [e reason], exception);
}
}
@implementation KrollEval
- (id)initWithCode:(NSString *)code_
{
return [self initWithCode:code_ sourceURL:nil];
}
- (id)initWithCode:(NSString *)code_ sourceURL:(NSURL *)sourceURL_
{
return [self initWithCode:code_ sourceURL:sourceURL_ startingLineNo:1];
}
- (id)initWithCode:(NSString *)code_ sourceURL:(NSURL *)sourceURL_ startingLineNo:(NSInteger)startingLineNo_
{
self = [super init];
if (self) {
code = [code_ copy];
sourceURL = [sourceURL_ copy];
startingLineNo = startingLineNo_;
}
return self;
}
- (void)dealloc
{
[code release];
[sourceURL release];
[super dealloc];
}
- (JSValueRef)jsInvokeInContext:(KrollContext *)context exception:(JSValueRef *)exceptionPointer
{
pthread_mutex_lock(&KrollEntryLock);
JSStringRef jsCode = JSStringCreateWithCFString((CFStringRef)code);
JSStringRef jsURL = NULL;
if (sourceURL != nil) {
jsURL = JSStringCreateWithUTF8CString([[sourceURL absoluteString] UTF8String]);
}
JSObjectRef global = JSContextGetGlobalObject([context context]);
JSValueRef result = JSEvaluateScript([context context], jsCode, global, jsURL, (int)startingLineNo, exceptionPointer);
JSStringRelease(jsCode);
if (jsURL != NULL) {
JSStringRelease(jsURL);
}
pthread_mutex_unlock(&KrollEntryLock);
return result;
}
- (void)invoke:(KrollContext *)context
{
pthread_mutex_lock(&KrollEntryLock);
JSValueRef exception = NULL;
[self jsInvokeInContext:context exception:&exception];
if (exception != NULL) {
[TiExceptionHandler.defaultExceptionHandler reportScriptError:exception inKrollContext:context];
pthread_mutex_unlock(&KrollEntryLock);
}
pthread_mutex_unlock(&KrollEntryLock);
}
- (id)invokeWithResult:(KrollContext *)context
{
pthread_mutex_lock(&KrollEntryLock);
JSValueRef exception = NULL;
JSValueRef result = [self jsInvokeInContext:context exception:&exception];
if (exception != NULL) {
[TiExceptionHandler.defaultExceptionHandler reportScriptError:exception inKrollContext:context];
pthread_mutex_unlock(&KrollEntryLock);
}
pthread_mutex_unlock(&KrollEntryLock);
return [KrollObject toID:context value:result];
}
@end
@implementation KrollEvent
- (id)initWithType:(NSString *)newType ForKrollObject:(KrollObject *)newCallbackObject eventObject:(NSDictionary *)newEventObject thisObject:(id)newThisObject;
{
if (self = [super init]) {
type = [newType copy];
callbackObject = [newCallbackObject retain];
eventObject = [newEventObject retain];
thisObject = [newThisObject retain];
}
return self;
}
- (id)initWithCallback:(KrollCallback *)newCallback eventObject:(NSDictionary *)newEventObject thisObject:(id)newThisObject
{
if (self = [super init]) {
callback = [newCallback retain];
eventObject = [newEventObject retain];
thisObject = [newThisObject retain];
}
return self;
}
- (void)dealloc
{
[callback release];
[type release];
[thisObject release];
[callbackObject release];
[eventObject release];
[super dealloc];
}
- (void)invoke:(KrollContext *)context
{
pthread_mutex_lock(&KrollEntryLock);
if (callbackObject != nil) {
[callbackObject triggerEvent:type withObject:eventObject thisObject:thisObject];
}
if (callback != nil) {
[callback call:[NSArray arrayWithObject:eventObject] thisObject:thisObject];
}
pthread_mutex_unlock(&KrollEntryLock);
}
@end
@implementation KrollContext
@synthesize delegate;
+ (void)initialize
{
if (self == [KrollContext class]) {
pthread_mutexattr_t entryLockAttrs;
pthread_mutexattr_init(&entryLockAttrs);
pthread_mutexattr_settype(&entryLockAttrs, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&KrollEntryLock, &entryLockAttrs);
}
}
- (NSString *)threadName
{
#ifdef TI_USE_KROLL_THREAD
return [NSString stringWithFormat:@"KrollContext<%@>", krollContextId];
#else
return @"KrollContext<MainThread>";
#endif
}
- (id)init
{
if (self = [super init]) {
#if CONTEXT_MEMORY_DEBUG == 1
NSLog(@"[DEBUG] INIT: %@", self);
#endif
stopped = YES;
KrollContextCount++;
WARN_IF_BACKGROUND_THREAD_OBJ; //NSNotificationCenter is not threadsafe!
}
return self;
}
- (void)destroy
{
#if CONTEXT_MEMORY_DEBUG == 1
NSLog(@"[DEBUG] DESTROY: %@", self);
#endif
[self stop];
RELEASE_TO_NIL(timerManager);
}
#if CONTEXT_MEMORY_DEBUG == 1
- (id)retain
{
NSLog(@"[DEBUG] RETAIN: %@ (%d)", self, [self retainCount] + 1);
return [super retain];
}
- (oneway void)release
{
NSLog(@"[DEBUG] RELEASE: %@ (%d)", self, [self retainCount] - 1);
[super release];
}
#endif
- (void)unregisterForNotifications
{
WARN_IF_BACKGROUND_THREAD_OBJ; //NSNotificationCenter is not threadsafe!
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)dealloc
{
#if CONTEXT_MEMORY_DEBUG == 1
NSLog(@"[DEBUG] DEALLOC: %@", self);
#endif
assert(!destroyed);
destroyed = YES;
[self destroy];
KrollContextCount--;
[super dealloc];
}
- (void)start
{
if (stopped != YES) {
@throw [NSException exceptionWithName:@"org.appcelerator.kroll"
reason:@"already started"
userInfo:nil];
}
stopped = NO;
TiThreadPerformOnMainThread(
^{
[self main];
},
NO);
}
- (void)stop
{
if (!stopped) {
stopped = YES;
}
}
- (BOOL)running
{
return !stopped;
}
- (JSGlobalContextRef)context
{
return context;
}
- (BOOL)isKJSThread
{
return [NSThread isMainThread];
}
- (void)invoke:(id)object
{
pthread_mutex_lock(&KrollEntryLock);
if ([object isKindOfClass:[NSOperation class]]) {
[(NSOperation *)object start];
pthread_mutex_unlock(&KrollEntryLock);
return;
}
[object invoke:self];
pthread_mutex_unlock(&KrollEntryLock);
}
- (void)enqueue:(id)obj
{
dispatch_block_t block = ^{
[self invoke:obj];
};
TiThreadPerformOnMainThread(block, [NSThread isMainThread]);
}
- (void)evalJS:(NSString *)code
{
KrollEval *eval = [[[KrollEval alloc] initWithCode:code] autorelease];
[self enqueue:eval];
}
- (id)evalJSAndWait:(NSString *)code
{
KrollEval *eval = [[[KrollEval alloc] initWithCode:code] autorelease];
return [eval invokeWithResult:self];
}
- (void)invokeOnThread:(id)callback_ method:(SEL)method_ withObject:(id)obj condition:(NSCondition *)condition_
{
KrollInvocation *invocation = [[[KrollInvocation alloc] initWithTarget:callback_ method:method_ withObject:obj condition:condition_] autorelease];
[self invoke:invocation];
}
- (void)invokeOnThread:(id)callback_ method:(SEL)method_ withObject:(id)obj callback:(id)callback selector:(SEL)selector_
{
KrollInvocation *invocation = [[[KrollInvocation alloc] initWithTarget:callback_ method:method_ withObject:obj callback:callback selector:selector_] autorelease];
[self invoke:invocation];
}
- (void)invokeBlockOnThread:(void (^)(void))block
{
pthread_mutex_lock(&KrollEntryLock);
block();
pthread_mutex_unlock(&KrollEntryLock);
}
+ (void)invokeBlock:(void (^)(void))block
{
pthread_mutex_lock(&KrollEntryLock);
block();
pthread_mutex_unlock(&KrollEntryLock);
}
- (void)bindCallback:(NSString *)name callback:(JSObjectCallAsFunctionCallback)fn
{
// create the invoker bridge
JSStringRef invokerFnName = JSStringCreateWithCFString((CFStringRef)name);
JSValueRef invoker = JSObjectMakeFunctionWithCallback(context, invokerFnName, fn);
if (invoker) {
JSObjectRef global = JSContextGetGlobalObject(context);
JSObjectSetProperty(context, global,
invokerFnName, invoker,
kJSPropertyAttributeNone,
NULL);
}
JSStringRelease(invokerFnName);
}
- (void)gc
{
// don't worry about locking, not that important
gcrequest = YES;
}
- (int)forceGarbageCollectNow
{
JSGarbageCollect(context);
gcrequest = NO;
loopCount = 0;
return 0;
}
- (void)main
{
pthread_mutex_lock(&KrollEntryLock);
context = JSGlobalContextCreate(NULL);
JSObjectRef globalRef = JSContextGetGlobalObject(context);
if (appJsKrollContext == nil) {
appJsKrollContext = self;
appJsContextRef = context;
}
// we register an empty kroll string that allows us to pluck out this instance
KrollObject *kroll = [[KrollObject alloc] initWithTarget:nil context:self];
JSValueRef krollRef = [KrollObject toValue:self value:kroll];
JSStringRef prop = JSStringCreateWithUTF8CString("Kroll");
JSObjectSetProperty(context, globalRef, prop, krollRef,
kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum | kJSPropertyAttributeReadOnly,
NULL);
JSObjectRef krollObj = JSValueToObject(context, krollRef, NULL);
bool set = JSObjectSetPrivate(krollObj, self);
assert(set);
[kroll release];
JSStringRelease(prop);
JSContext *jsContext = [JSContext contextWithJSGlobalContextRef:context];
timerManager = [[KrollTimerManager alloc] initInContext:jsContext];
[self bindCallback:@"require"
callback:&CommonJSRequireCallback];
[self bindCallback:@"L" callback:&LCallback];
[self bindCallback:@"alert" callback:&AlertCallback];
prop = JSStringCreateWithUTF8CString("String");
// create a special method -- String.format -- that will act as a string formatter
JSStringRef formatName = JSStringCreateWithUTF8CString("format");
JSValueRef invoker = JSObjectMakeFunctionWithCallback(context, formatName, &StringFormatCallback);
JSValueRef stringValueRef = JSObjectGetProperty(context, globalRef, prop, NULL);
JSObjectRef stringRef = JSValueToObject(context, stringValueRef, NULL);
JSObjectSetProperty(context, stringRef,
formatName, invoker,
kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete,
NULL);
JSStringRelease(formatName);
// create a special method -- String.formatDate -- that will act as a date formatter
formatName = JSStringCreateWithUTF8CString("formatDate");
invoker = JSObjectMakeFunctionWithCallback(context, formatName, &StringFormatDateCallback);
stringValueRef = JSObjectGetProperty(context, globalRef, prop, NULL);
stringRef = JSValueToObject(context, stringValueRef, NULL);
JSObjectSetProperty(context, stringRef,
formatName, invoker,
kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete,
NULL);
JSStringRelease(formatName);
// create a special method -- String.formatTime -- that will act as a time formatter
formatName = JSStringCreateWithUTF8CString("formatTime");
invoker = JSObjectMakeFunctionWithCallback(context, formatName, &StringFormatTimeCallback);
stringValueRef = JSObjectGetProperty(context, globalRef, prop, NULL);
stringRef = JSValueToObject(context, stringValueRef, NULL);
JSObjectSetProperty(context, stringRef,
formatName, invoker,
kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete,
NULL);
JSStringRelease(formatName);
// create a special method -- String.formatDecimal -- that will act as a decimal formatter
formatName = JSStringCreateWithUTF8CString("formatDecimal");
invoker = JSObjectMakeFunctionWithCallback(context, formatName, &StringFormatDecimalCallback);
stringValueRef = JSObjectGetProperty(context, globalRef, prop, NULL);
stringRef = JSValueToObject(context, stringValueRef, NULL);
JSObjectSetProperty(context, stringRef,
formatName, invoker,
kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete,
NULL);
JSStringRelease(formatName);
// create a special method -- String.formatCurrency -- that will act as a currency formatter
formatName = JSStringCreateWithUTF8CString("formatCurrency");
invoker = JSObjectMakeFunctionWithCallback(context, formatName, &StringFormatCurrencyCallback);
stringValueRef = JSObjectGetProperty(context, globalRef, prop, NULL);
stringRef = JSValueToObject(context, stringValueRef, NULL);
JSObjectSetProperty(context, stringRef,
formatName, invoker,
kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete,
NULL);
JSStringRelease(formatName);
JSStringRelease(prop);
if (delegate != nil && [delegate respondsToSelector:@selector(willStartNewContext:)]) {
[delegate performSelector:@selector(willStartNewContext:) withObject:self];
}
loopCount = 0;
if (delegate != nil && [delegate respondsToSelector:@selector(didStartNewContext:)]) {
[delegate performSelector:@selector(didStartNewContext:) withObject:self];
}
pthread_mutex_unlock(&KrollEntryLock);
}
@end
@implementation ExpandedInvocationOperation
@synthesize invocationTarget, invocationSelector;
@synthesize invocationArg1, invocationArg2, invocationArg3, invocationArg4;
- (id)initWithTarget:(id)target selector:(SEL)sel object:(id)arg1 object:(id)arg2;
{
self = [super init];
if (self != nil) {
[self setInvocationTarget:target];
[self setInvocationSelector:sel];
[self setInvocationArg1:arg1];
[self setInvocationArg2:arg2];
}
return self;
}
- (id)initWithTarget:(id)target selector:(SEL)sel object:(id)arg1 object:(id)arg2 object:(id)arg3;
{
self = [self initWithTarget:target selector:sel object:arg1 object:arg2];
if (self != nil) {
[self setInvocationArg3:arg3];
}
return self;
}
- (id)initWithTarget:(id)target selector:(SEL)sel object:(id)arg1 object:(id)arg2 object:(id)arg3 object:(id)arg4;
{
self = [self initWithTarget:target selector:sel object:arg1 object:arg2 object:arg3];
if (self != nil) {
[self setInvocationArg4:arg4];
}
return self;
}
- (void)main
{
NSMethodSignature *msignature = [invocationTarget methodSignatureForSelector:invocationSelector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:msignature];
NSUInteger argCount = [msignature numberOfArguments];
if (argCount >= 3) {
[invocation setArgument:&invocationArg1 atIndex:2];
}
if (argCount >= 4) {
[invocation setArgument:&invocationArg2 atIndex:3];
}
if (argCount >= 5) {
[invocation setArgument:&invocationArg3 atIndex:4];
}
if (argCount >= 6) {
[invocation setArgument:&invocationArg4 atIndex:5];
}
[invocation setSelector:invocationSelector];
[invocation invokeWithTarget:invocationTarget];
}
- (void)dealloc
{
[invocationTarget release];
[invocationArg1 release];
[invocationArg2 release];
[invocationArg3 release];
[invocationArg4 release];
[super dealloc];
}
@end