Skip to content
Navigation Menu
{{ message }}
forked from sketch-hq/CocoaScript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCOScript.m
More file actions
874 lines (663 loc) · 29 KB
/
Copy pathCOScript.m
File metadata and controls
874 lines (663 loc) · 29 KB
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
//
// JSTalk.m
// jstalk
//
// Created by August Mueller on 1/15/09.
// Copyright 2009 Flying Meat Inc. All rights reserved.
//
#import "COScript.h"
#import "COSListener.h"
#import "COSPreprocessor.h"
#import "COScript+Fiber.h"
#import "COScript+Interval.h"
#import "COCacheBox.h"
#import <ScriptingBridge/ScriptingBridge.h>
#import "MochaRuntime.h"
#import "MOMethod.h"
#import "MOUndefined.h"
#import "MOBridgeSupportController.h"
#import <stdarg.h>
extern int *_NSGetArgc(void);
extern char ***_NSGetArgv(void);
static BOOL JSTalkShouldLoadJSTPlugins = YES;
static NSMutableArray *JSTalkPluginList;
static NSMutableDictionary<NSString*, NSString*>* coreModuleScriptCache; // we are keeping the core modules' script in memory as they are required very often
static id<COFlowDelegate> COFlowDelegate = nil;
@interface Mocha (Private)
- (JSValueRef)setObject:(id)object withName:(NSString *)name;
- (JSValueRef)setJSValue:(JSValueRef)jsValue withName:(NSString *)name;
- (BOOL)removeObjectWithName:(NSString *)name;
- (JSValueRef)callJSFunction:(JSObjectRef)jsFunction withArgumentsInArray:(NSArray *)arguments;
- (id)objectForJSValue:(JSValueRef)value;
@end
@interface COScript (Private)
@end
@implementation COScript {
NSMutableDictionary<NSString*, COCacheBox*>* moduleCache;
}
+ (id)setFlowDelegate:(id<COFlowDelegate>)flowDelegate {
id oldDelegate = COFlowDelegate;
COFlowDelegate = flowDelegate;
return oldDelegate;
}
+ (void)listen {
[COSListener listen];
}
+ (void)setShouldLoadExtras:(BOOL)b {
JSTalkShouldLoadJSTPlugins = b;
}
+ (void)setShouldLoadJSTPlugins:(BOOL)b {
JSTalkShouldLoadJSTPlugins = b;
}
- (id)init {
return [self initWithCoreModules:@{} andName:nil];
}
- (instancetype)initWithCoreModules:(NSDictionary*)coreModules andName:(NSString*)name {
self = [super init];
if ((self != nil)) {
_mochaRuntime = [[Mocha alloc] initWithName:name ? name : @"Untitled"];
self.coreModuleMap = coreModules;
if (!coreModuleScriptCache) {
coreModuleScriptCache = [NSMutableDictionary dictionary];
}
moduleCache = [NSMutableDictionary dictionary];
[self setEnv:[NSMutableDictionary dictionary]];
[self setShouldPreprocess:YES];
[self addExtrasToRuntime];
}
return self;
}
+ (void)resetCache {
coreModuleScriptCache = [NSMutableDictionary dictionary];
[[MOBridgeSupportController sharedController] reset];
}
- (void)dealloc {
// debug(@"%s:%d", __FUNCTION__, __LINE__);
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)cleanup {
// clean up fibers to shut everything down nicely
[self cleanupFibers];
// clean up the global object that we injected in the runtime
[self deleteObjectWithName:@"jstalk"];
[self deleteObjectWithName:@"coscript"];
[self deleteObjectWithName:@"print"];
[self deleteObjectWithName:@"log"];
[self deleteObjectWithName:@"require"];
if ([self.coreModuleMap objectForKey:@"console"]) {
[self deleteObjectWithName:@"console"];
}
if ([self.coreModuleMap objectForKey:@"buffer"]) {
[self deleteObjectWithName:@"Buffer"];
}
if ([self.coreModuleMap objectForKey:@"timers"]) {
[self deleteObjectWithName:@"setTimeout"];
[self deleteObjectWithName:@"clearTimeout"];
[self deleteObjectWithName:@"setInterval"];
[self deleteObjectWithName:@"clearInterval"];
[self deleteObjectWithName:@"setImmediate"];
[self deleteObjectWithName:@"clearImmediate"];
}
[moduleCache enumerateKeysAndObjectsUsingBlock:^(NSString *key, COCacheBox *cacheBox, BOOL *stop) {
[cacheBox cleanup];
}];
// clean up mocha
[_mochaRuntime shutdown];
_mochaRuntime = nil;
}
- (void)fiberWasCleared {
if (COFlowDelegate != nil) {
if (![self shouldKeepRunning]) {
[COFlowDelegate didClearEventStack:self];
}
}
}
- (void)garbageCollect {
// NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
[_mochaRuntime garbageCollect];
// debug(@"gc took %f seconds", [NSDate timeIntervalSinceReferenceDate] - start); (void)start;
}
- (BOOL)shouldKeepRunning {
if (_shouldKeepAround) {
return YES;
}
if (_activeFibers != nil) {
return [_activeFibers count] > 0;
}
return NO;
}
- (JSGlobalContextRef)context {
return [_mochaRuntime context];
}
- (void)addExtrasToRuntime {
[self pushObject:self withName:@"jstalk"];
[self pushObject:self withName:@"coscript"];
[_mochaRuntime evalString:@"var nil=null;\n"];
[_mochaRuntime setValue:[MOMethod methodWithTarget:self selector:@selector(print:)] forKey:@"print"];
[_mochaRuntime setValue:[MOMethod methodWithTarget:self selector:@selector(print:)] forKey:@"log"];
[_mochaRuntime setValue:[MOMethod methodWithTarget:self selector:@selector(require:)] forKey:@"require"];
[_mochaRuntime loadFrameworkWithName:@"AppKit"];
[_mochaRuntime loadFrameworkWithName:@"Foundation"];
BOOL previousShouldPreprocess = self.shouldPreprocess;
self.shouldPreprocess = NO;
// if there is a console module, use it to polyfill the console global
if ([self.coreModuleMap objectForKey:@"console"]) {
[self pushJSValue:[self executeStringAndReturnJSValue:@"(function() { return require('console')(); })()"] withName:@"console"];
}
// if there is a buffer module, use it to polyfill the Buffer global
if ([self.coreModuleMap objectForKey:@"buffer"]) {
[self pushJSValue:[self executeStringAndReturnJSValue:@"(function() { return require('buffer').Buffer; })()"] withName:@"Buffer"];
}
// if there is a timers module, use it to polyfill the setTimeout globals
if ([self.coreModuleMap objectForKey:@"timers"]) {
[self pushJSValue:[self executeStringAndReturnJSValue:@"(function() { return require('timers').setTimeout; })()"] withName:@"setTimeout"];
[self pushJSValue:[self executeStringAndReturnJSValue:@"(function() { return require('timers').clearTimeout; })()"] withName:@"clearTimeout"];
[self pushJSValue:[self executeStringAndReturnJSValue:@"(function() { return require('timers').setInterval; })()"] withName:@"setInterval"];
[self pushJSValue:[self executeStringAndReturnJSValue:@"(function() { return require('timers').clearInterval; })()"] withName:@"clearInterval"];
[self pushJSValue:[self executeStringAndReturnJSValue:@"(function() { return require('timers').setImmediate; })()"] withName:@"setImmediate"];
[self pushJSValue:[self executeStringAndReturnJSValue:@"(function() { return require('timers').clearImmediate; })()"] withName:@"clearImmediate"];
}
// if there is a process module, use it to polyfill the process global
if ([self.coreModuleMap objectForKey:@"process"]) {
[self pushJSValue:[self executeStringAndReturnJSValue:@"(function() { return require('process'); })()"] withName:@"process"];
}
self.shouldPreprocess = previousShouldPreprocess;
}
+ (void)loadExtraAtPath:(NSString*)fullPath {
Class pluginClass;
@try {
NSBundle *pluginBundle = [NSBundle bundleWithPath:fullPath];
if (!pluginBundle) {
return;
}
NSString *principalClassName = [[pluginBundle infoDictionary] objectForKey:@"NSPrincipalClass"];
if (principalClassName && NSClassFromString(principalClassName)) {
NSLog(@"The class %@ is already loaded, skipping the load of %@", principalClassName, fullPath);
return;
}
[principalClassName class]; // force loading of it.
NSError *err = nil;
[pluginBundle loadAndReturnError:&err];
#ifndef __clang_analyzer__
if (err) {
NSLog(@"Error loading plugin at %@", fullPath);
NSLog(@"%@", err);
}
else if ((pluginClass = [pluginBundle principalClass])) {
// do we want to actually do anything with em' at this point?
NSString *bridgeSupportName = [[pluginBundle infoDictionary] objectForKey:@"BridgeSuportFileName"];
if (bridgeSupportName) {
NSString *bridgeSupportPath = [pluginBundle pathForResource:bridgeSupportName ofType:nil];
NSError *outErr = nil;
if (![[MOBridgeSupportController sharedController] loadBridgeSupportAtURL:[NSURL fileURLWithPath:bridgeSupportPath] error:&outErr]) {
NSLog(@"Could not load bridge support file at %@", bridgeSupportPath);
}
}
}
else {
//debug(@"Could not load the principal class of %@", fullPath);
//debug(@"infoDictionary: %@", [pluginBundle infoDictionary]);
}
#endif
}
@catch (NSException * e) {
NSLog(@"EXCEPTION: %@: %@", [e name], e);
}
}
+ (void)resetPlugins {
JSTalkPluginList = nil;
}
+ (void)loadPlugins {
// install plugins that were passed via the command line
int i = 0;
char **argv = *_NSGetArgv();
for (i = 0; argv[i] != NULL; ++i) {
NSString *a = [NSString stringWithUTF8String:argv[i]];
if ([@"-jstplugin" isEqualToString:a] || [@"-cosplugin" isEqualToString:a]) {
i++;
NSLog(@"Loading plugin at: [%@]", [NSString stringWithUTF8String:argv[i]]);
[self loadExtraAtPath:[NSString stringWithUTF8String:argv[i]]];
}
}
JSTalkPluginList = [NSMutableArray array];
NSString *appSupport = @"Library/Application Support/JSTalk/Plug-ins";
NSString *appPath = [[NSBundle mainBundle] builtInPlugInsPath];
NSString *sysPath = [@"/" stringByAppendingPathComponent:appSupport];
NSString *userPath = [NSHomeDirectory() stringByAppendingPathComponent:appSupport];
// only make the JSTalk dir if we're JSTalkEditor.
// or don't ever make it, since you'll get rejected from the App Store. *sigh*
/*
if (![[NSFileManager defaultManager] fileExistsAtPath:userPath]) {
NSString *mainBundleId = [[NSBundle mainBundle] bundleIdentifier];
if ([@"org.jstalk.JSTalkEditor" isEqualToString:mainBundleId]) {
[[NSFileManager defaultManager] createDirectoryAtPath:userPath withIntermediateDirectories:YES attributes:nil error:nil];
}
}
*/
for (NSString *folder in [NSArray arrayWithObjects:appPath, sysPath, userPath, nil]) {
for (NSString *bundle in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folder error:nil]) {
if (!([bundle hasSuffix:@".jstplugin"] || [bundle hasSuffix:@".cosplugin"])) {
continue;
}
[self loadExtraAtPath:[folder stringByAppendingPathComponent:bundle]];
}
}
if (![[[NSBundle mainBundle] bundleIdentifier] isEqualToString:@"org.jstalk.JSTalkEditor"]) {
NSURL *jst = [[NSWorkspace sharedWorkspace] URLForApplicationWithBundleIdentifier:@"org.jstalk.JSTalkEditor"];
if (jst) {
NSURL *folder = [[jst URLByAppendingPathComponent:@"Contents"] URLByAppendingPathComponent:@"PlugIns"];
for (NSString *bundle in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[folder path] error:nil]) {
if (!([bundle hasSuffix:@".jstplugin"])) {
continue;
}
[self loadExtraAtPath:[[folder path] stringByAppendingPathComponent:bundle]];
}
}
}
}
+ (void)loadBridgeSupportFileAtURL:(NSURL*)url {
NSError *outErr = nil;
if (![[MOBridgeSupportController sharedController] loadBridgeSupportAtURL:url error:&outErr]) {
NSLog(@"Could not load bridge support file at %@", url);
}
}
NSString *currentCOScriptThreadIdentifier = @"org.jstalk.currentCOScriptHack";
// FIXME: Change currentCOScript and friends to use a stack in the thread dictionary, instead of just overwriting what might already be there."
+ (NSMutableArray*)currentCOSThreadStack {
NSMutableArray *ar = [[[NSThread currentThread] threadDictionary] objectForKey:currentCOScriptThreadIdentifier];
if (!ar) {
ar = [NSMutableArray array];
[[[NSThread currentThread] threadDictionary] setObject:ar forKey:currentCOScriptThreadIdentifier];
}
return ar;
}
+ (COScript*)currentCOScript {
return [[self currentCOSThreadStack] lastObject];
}
- (void)pushAsCurrentCOScript {
[[[self class] currentCOSThreadStack] addObject:self];
}
- (void)popAsCurrentCOScript {
if ([[[self class] currentCOSThreadStack] count]) {
[[[self class] currentCOSThreadStack] removeLastObject];
}
else {
NSLog(@"popAsCurrentCOScript - currentCOSThreadStack is empty");
}
}
- (void)pushObject:(id)obj withName:(NSString*)name {
[_mochaRuntime setObject:obj withName:name];
}
- (void)pushJSValue:(JSValueRef)obj withName:(NSString*)name {
[_mochaRuntime setJSValue:obj withName:name];
}
- (void)deleteObjectWithName:(NSString*)name {
[_mochaRuntime removeObjectWithName:name];
}
# pragma mark - require
/// This aims to implement the require resolution algorithm from NodeJS.
/// Given a `module` string required by a given script at the `currentURL`
/// using `require('./path/to/module')`, this method returns a URL
/// corresponding to the `module`.
/// `module` could also be the name of a core module that we shipped with the app
/// (for example `util`), in which case, it will return the URL to that one
/// and set `isRequiringCore` to YES.
- (NSURL*)resolveModule:(NSString*)module currentURL:(NSURL*)currentURL isRequiringCoreModule: (BOOL*)isRequiringCore {
if (![module hasPrefix: @"."] && ![module hasPrefix: @"/"] && ![module hasPrefix: @"~"]) {
*isRequiringCore = YES;
return self.coreModuleMap[module];
}
*isRequiringCore = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isRelative = [module hasPrefix: @"."];
NSString *modulePath = [module stringByStandardizingPath];
NSURL *moduleURL = isRelative ? [NSURL URLWithString:modulePath relativeToURL:currentURL] : [NSURL fileURLWithPath:modulePath];
if (moduleURL == nil) {
return nil;
}
BOOL isDir;
if ([fileManager fileExistsAtPath:moduleURL.path isDirectory:&isDir]) {
if (!isDir) {
// if the module is a proper path to a file, just use it
return moduleURL;
}
// if it's a path to a directory, let's try to find a package.json
NSURL *packageJSONURL = [moduleURL URLByAppendingPathComponent:@"package.json"];
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:packageJSONURL.path];
if (jsonData != nil) {
id packageJSON = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
if (packageJSON != nil) {
// we have a package.json, so let's find the `main` key
NSString *main = [packageJSON objectForKey:@"main"];
if (main) {
// main is always a relative path, so let's transform it to one
if ([module hasPrefix: @"/"]) {
main = [@"." stringByAppendingString:main];
} else if (![module hasPrefix: @"."]) {
main = [@"./" stringByAppendingString:main];
}
return [self resolveModule:[moduleURL URLByAppendingPathComponent:main].path currentURL:currentURL isRequiringCoreModule:isRequiringCore];
}
}
}
// default to index.js otherwise
NSURL *indexURL = [moduleURL URLByAppendingPathComponent:@"index.js"];
if ([fileManager fileExistsAtPath:indexURL.path isDirectory:&isDir] && !isDir) {
return indexURL;
}
// couldn't find anything :(
return nil;
}
// try by adding the js extension which can be ommited
NSURL *jsURL = [moduleURL URLByAppendingPathExtension:@"js"];
if ([fileManager fileExistsAtPath:jsURL.path isDirectory:&isDir] && !isDir) {
return jsURL;
}
// unlucky :(
return nil;
}
- (JSValueRef)require:(NSString *)module {
// store the current script URL so that we can put it back after requiring the module
NSURL *currentURL = [_env objectForKey:@"scriptURL"];
// we never want to preprocess the modules - it shouldn't use Cocoascript syntax.
BOOL savedPreprocess = self.shouldPreprocess;
self.shouldPreprocess = NO;
JSValueRef result = NULL;
BOOL isRequiringCore;
NSURL *moduleURL = [self resolveModule:module currentURL:currentURL isRequiringCoreModule:&isRequiringCore];
if (moduleURL == nil) {
@throw [NSException
exceptionWithName:NSInvalidArgumentException
reason:isRequiringCore
? [NSString stringWithFormat:@"%@ is not a core package", module]
: [NSString stringWithFormat:@"Cannot find module %@ from package %@", module, currentURL.path]
userInfo:nil];
}
if (moduleCache[moduleURL.path]) {
return moduleCache[moduleURL.path].jsValueRef;
}
if (isRequiringCore) {
// we set `isRequiringCore` in the environment so that if a core module is requiring other files,
// we know that it's still a core module and should be cached as such
[_env setObject:@"true" forKey:@"isRequiringCore"];
}
result = [self executeModuleAtURL:moduleURL];
if (isRequiringCore) {
[_env setObject:@"false" forKey:@"isRequiringCore"];
}
// go back to previous settings
self.shouldPreprocess = savedPreprocess;
if (currentURL) {
[_env setObject:currentURL forKey:@"scriptURL"];
}
// cache the module so it keeps it state if required again
COCacheBox *cacheBox = [[COCacheBox alloc] initWithJSValueRef:result inContext:_mochaRuntime.context];
[moduleCache setObject:cacheBox forKey:moduleURL.path];
return result;
}
- (JSValueRef)executeModuleAtURL:(NSURL*)scriptURL {
JSValueRef result = NULL;
if (scriptURL) {
NSError *error;
NSString *script;
if (coreModuleScriptCache[scriptURL.path]) {
script = coreModuleScriptCache[scriptURL.path];
} else {
script = [NSString stringWithContentsOfURL:scriptURL encoding:NSUTF8StringEncoding error:&error];
if (script && [[_env objectForKey:@"isRequiringCore"] isEqualToString:@"true"]) {
// cache the core module's so that we don't need to read it from disk again
[coreModuleScriptCache setObject:script forKey:scriptURL.path];
}
}
if (script) {
NSString *module = [scriptURL.pathExtension isEqual:@"json"]
? [NSString stringWithFormat:@"(function() { return %@ })()", script]
: [NSString stringWithFormat:@"(function() { var module = { exports : {} }; var exports = module.exports; %@ ; return module.exports; })()", script];
result = [self executeStringAndReturnJSValue:module baseURL:scriptURL];
} else if (error) {
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"Cannot find module %@", scriptURL.path] userInfo:nil];
}
}
return result;
}
# pragma mark - execute string
- (id)executeString:(NSString*)str {
return [self executeString:str baseURL:nil];
}
- (JSValueRef)executeStringAndReturnJSValue:(NSString*)str {
return [self executeStringAndReturnJSValue:str baseURL:nil];
}
- (JSValueRef)executeStringAndReturnJSValue:(NSString*)str baseURL:(NSURL*)base {
if (!JSTalkPluginList && JSTalkShouldLoadJSTPlugins) {
[COScript loadPlugins];
}
if (base) {
[_env setObject:base forKey:@"scriptURL"];
}
if (!base && [[_env objectForKey:@"scriptURL"] isKindOfClass:[NSURL class]]) {
base = [_env objectForKey:@"scriptURL"];
}
if ([self shouldPreprocess]) {
str = [COSPreprocessor preprocessCode:str withBaseURL:base];
}
self.processedSource = str;
[self pushAsCurrentCOScript];
JSValueRef resultObj = NULL;
@try {
resultObj = [_mochaRuntime evalJSString:str scriptPath:[base path]];
}
@catch (NSException *e) {
[self printException:e];
}
[self popAsCurrentCOScript];
return resultObj;
}
- (id)executeString:(NSString*)str baseURL:(NSURL*)base {
id resultObj = [_mochaRuntime objectForJSValue:[self executeStringAndReturnJSValue:str baseURL:base]];
if (resultObj == [MOUndefined undefined]) {
resultObj = nil;
}
return resultObj;
}
- (BOOL)hasFunctionNamed:(NSString*)name {
JSValueRef exception = nil;
JSStringRef jsFunctionName = JSStringCreateWithUTF8CString([name UTF8String]);
JSValueRef jsFunctionValue = JSObjectGetProperty([_mochaRuntime context], JSContextGetGlobalObject([_mochaRuntime context]), jsFunctionName, &exception);
JSStringRelease(jsFunctionName);
return jsFunctionValue && (JSValueGetType([_mochaRuntime context], jsFunctionValue) == kJSTypeObject);
}
- (id)callFunctionNamed:(NSString*)name withArguments:(NSArray*)args {
id returnValue = nil;
@try {
[self pushAsCurrentCOScript];
returnValue = [_mochaRuntime callFunctionWithName:name withArgumentsInArray:args];
if (returnValue == [MOUndefined undefined]) {
returnValue = nil;
}
}
@catch (NSException * e) {
[self printException:e];
}
[self popAsCurrentCOScript];
return returnValue;
}
- (id)callJSFunction:(JSObjectRef)jsFunction withArgumentsInArray:(NSArray *)arguments {
[self pushAsCurrentCOScript];
JSValueRef r = nil;
@try {
r = [_mochaRuntime callJSFunction:jsFunction withArgumentsInArray:arguments];
}
@catch (NSException * e) {
[self printException:e];
}
[self popAsCurrentCOScript];
if (r) {
return [_mochaRuntime objectForJSValue:r];
}
return nil;
}
- (void)unprotect:(id)o {
JSValueRef value = [_mochaRuntime JSValueForObject:o];
assert(value);
if (value) {
JSObjectRef jsObject = JSValueToObject([_mochaRuntime context], value, NULL);
id private = (__bridge id)JSObjectGetPrivate(jsObject);
assert([private representedObject] == o);
// debug(@"COS unprotecting %@", o);
JSValueUnprotect([_mochaRuntime context], value);
}
}
- (void)protect:(id)o {
JSValueRef value = [_mochaRuntime JSValueForObject:o];
assert(value);
if (value) {
JSObjectRef jsObject = JSValueToObject([_mochaRuntime context], value, NULL);
// debug(@"COS protecting %@ / v: %p o: %p", o, value, jsObject);
id private = (__bridge id)JSObjectGetPrivate(jsObject);
assert([private representedObject] == o);
JSValueProtect([_mochaRuntime context], value);
}
}
// JavaScriptCore isn't safe for recursion. So calling this function from
// within a script is a really bad idea. Of couse, that's what it was written
// for, so it really needs to be taken out.
- (void)include:(NSString*)fileName {
if (![fileName hasPrefix:@"/"] && [_env objectForKey:@"scriptURL"]) {
NSString *parentDir = [[[_env objectForKey:@"scriptURL"] path] stringByDeletingLastPathComponent];
fileName = [parentDir stringByAppendingPathComponent:fileName];
}
NSURL *scriptURL = [NSURL fileURLWithPath:fileName];
NSError *err = nil;
NSString *str = [NSString stringWithContentsOfURL:scriptURL encoding:NSUTF8StringEncoding error:&err];
if (!str) {
NSLog(@"Could not open file '%@'", scriptURL);
NSLog(@"Error: %@", err);
return;
}
if (_shouldPreprocess) {
str = [COSPreprocessor preprocessCode:str];
}
self.processedSource = str;
[_mochaRuntime evalString:str];
}
# pragma mark - print
- (void)printException:(NSException*)e {
NSMutableDictionary* errorToPrint = [@{
@"payload": @[e],
@"level": @"error"
} mutableCopy];
if (_printController) {
[_printController scriptEncounteredException:e];
errorToPrint[@"command"] = _printController;
}
NSMutableString *s = [NSMutableString string];
[s appendFormat:@"%@", e];
NSDictionary *d = [e userInfo];
if ([d objectForKey:@"stack"] != nil && ![[d objectForKey:@"stack"] isEqualToString:@"undefined"]) {
// this is the same algo as in skpm/util
// https://github.com/skpm/util/blob/5edb84f6d7983320ab064b7f2cf575fbedbf183b/index.js#L679-L695
// so that it's consistent when logging an error
NSArray* stacks = [[d objectForKey:@"stack"] componentsSeparatedByString:@"\n"];
for (NSString* stack in stacks) {
NSMutableArray* parts = [[stack componentsSeparatedByString:@"/"] mutableCopy];
if ([parts count] > 0) {
NSString* fn = [parts[0] stringByReplacingOccurrencesOfString:@"@" withString:@""];
[parts removeObjectAtIndex:0];
NSString* callsite = [NSString stringWithFormat:@"/%@", [parts componentsJoinedByString:@"/"]];
[s appendFormat:@"\n at "];
if (fn != nil && ![fn isEqualToString:@""]) {
[s appendFormat:@"%@ (", fn];
}
[s appendFormat:@"%@", callsite];
if (fn != nil && ![fn isEqualToString:@""]) {
[s appendFormat:@")"];
}
}
}
} else if ([d objectForKey:@"sourceURL"] != nil) {
[s appendFormat:@"\n at %@", [d objectForKey:@"sourceURL"]];
if ([d objectForKey:@"line"] != nil) {
[s appendFormat:@":%@", [d objectForKey:@"line"]];
}
if ([d objectForKey:@"column"] != nil) {
[s appendFormat:@":%@", [d objectForKey:@"column"]];
}
}
for (id o in [d allKeys]) {
if (![o isEqualToString:@"stack"] && ![o isEqualToString:@"line"] && ![o isEqualToString:@"column"] && ![o isEqualToString:@"sourceURL"]) {
[s appendFormat:@"\n %@: %@", o, [d objectForKey:o]];
}
}
errorToPrint[@"stringValue"] = s;
[self print:errorToPrint];
}
- (void)print:(id)s {
if (_printController) {
[_printController print:s];
} else {
if (![s isKindOfClass:[NSString class]]) {
s = [s description];
}
printf("%s\n", [s UTF8String]);
}
}
# pragma mark - proxy
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+ (id)applicationOnPort:(NSString*)port {
NSConnection *conn = nil;
NSUInteger tries = 0;
while (!conn && tries < 10) {
conn = [NSConnection connectionWithRegisteredName:port host:nil];
tries++;
if (!conn) {
// debug(@"Sleeping, waiting for %@ to open", port);
sleep(1);
}
}
if (!conn) {
NSBeep();
NSLog(@"Could not find a JSTalk connection to %@", port);
}
return [conn rootProxy];
}
+ (id)application:(NSString*)app {
NSString *appPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:app];
if (!appPath) {
NSLog(@"Could not find application '%@'", app);
// fixme: why are we returning a bool?
return [NSNumber numberWithBool:NO];
}
NSBundle *appBundle = [NSBundle bundleWithPath:appPath];
NSString *bundleId = [appBundle bundleIdentifier];
// make sure it's running
NSArray *runningApps = [[NSWorkspace sharedWorkspace] runningApplications];
BOOL found = NO;
for (NSRunningApplication *rapp in runningApps) {
if ([[rapp bundleIdentifier] isEqualToString:bundleId]) {
found = YES;
break;
}
}
if (!found) {
BOOL launched = [[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier:bundleId
options:NSWorkspaceLaunchWithoutActivation | NSWorkspaceLaunchAsync
additionalEventParamDescriptor:nil
launchIdentifier:nil];
if (!launched) {
NSLog(@"Could not open up %@", appPath);
return nil;
}
}
return [self applicationOnPort:[NSString stringWithFormat:@"%@.JSTalk", bundleId]];
}
+ (id)app:(NSString*)app {
return [self application:app];
}
+ (id)proxyForApp:(NSString*)app {
return [self application:app];
}
#pragma clang diagnostic pop
@end
@implementation JSTalk
@end
You can’t perform that action at this time.
