-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathJs.mm
56 lines (41 loc) · 1.55 KB
/
Js.mm
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
#import "Js.h"
#include <functional>
NSString * const Yes = @"yes";
NSString * const No = @"no";
namespace Js{
void call(NSString* cb, NSArray* args) {
if (cb && [cb isEqualToString:@""]) {
return;
}
NSMutableArray* ma = [[NSMutableArray alloc]init];
for(int i=0; i<args.count; i++){
NSObject* obj = [args objectAtIndex:i];
if (obj == Yes) {
[ma addObject:@"true"];
} else if (obj == No) {
[ma addObject:@"false"];
} else if ([obj isKindOfClass:[NSNumber class]]) {
[ma addObject:[NSString stringWithFormat:@"%@", obj]];
} else if ([obj isKindOfClass:[NSString class]]){
[ma addObject:[NSString stringWithFormat:@"'%@'", obj]];
} else if ([obj isKindOfClass:[NSError class]]) {
[ma addObject:[NSString stringWithFormat:@"'%@'", [(NSError*)obj localizedDescription]]];
} else {
[ma addObject:@"null"];
}
}
NSString* s = [NSString stringWithFormat:@"native.callback(%@, %@)", cb, [ma componentsJoinedByString:@", "]];
eval(s);
}
void eval(NSString* content) {
eval(std::string([content UTF8String]));
}
static EvalFunc efunc;
void setEvalFunc(const EvalFunc& func) {
efunc = func;
}
void eval(std::string content) {
NSLog(@"evalString: %s", content.c_str());
efunc(content);
}
}