Commit | Line | Data |
---|---|---|
62ca2b82 JF |
1 | #include <iostream> |
2 | #include <string> | |
3 | ||
4 | #include <CoreFoundation/CoreFoundation.h> | |
5 | #include <CoreFoundation/CFLogUtilities.h> | |
6 | ||
7 | #include <Foundation/Foundation.h> | |
8 | ||
9 | #include <JavaScriptCore/JSBase.h> | |
10 | #include <JavaScriptCore/JSValueRef.h> | |
11 | #include <JavaScriptCore/JSObjectRef.h> | |
12 | #include <JavaScriptCore/JSContextRef.h> | |
13 | #include <JavaScriptCore/JSStringRef.h> | |
14 | #include <JavaScriptCore/JSStringRefCF.h> | |
15 | ||
16 | #define _trace() do { \ | |
17 | CFLog(kCFLogLevelNotice, CFSTR("_trace(%u)"), __LINE__); \ | |
18 | } while (false) | |
19 | ||
20 | JSContextRef JSGetContext(void); | |
0c862573 JF |
21 | void CYThrow(JSContextRef context, id error, JSValueRef *exception); |
22 | CFStringRef JSValueToJSONCopy(JSContextRef context, JSValueRef value); | |
62ca2b82 JF |
23 | |
24 | int main() { | |
25 | for (;;) { | |
26 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
27 | ||
28 | std::cout << ">>> " << std::flush; | |
29 | ||
30 | std::string line; | |
31 | if (!std::getline(std::cin, line)) | |
32 | break; | |
33 | ||
34 | JSStringRef script(JSStringCreateWithUTF8CString(line.c_str())); | |
35 | ||
0c862573 JF |
36 | JSContextRef context(JSGetContext()); |
37 | ||
38 | JSValueRef exception(NULL); | |
39 | JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception)); | |
62ca2b82 JF |
40 | JSStringRelease(script); |
41 | ||
0c862573 JF |
42 | if (exception != NULL) |
43 | result = exception; | |
44 | ||
45 | if (!JSValueIsUndefined(context, result)) { | |
46 | CFStringRef json; | |
47 | ||
48 | @try { json: | |
49 | json = JSValueToJSONCopy(context, result); | |
50 | } @catch (id error) { | |
51 | CYThrow(context, error, &result); | |
52 | goto json; | |
53 | } | |
54 | ||
7ba62cfd JF |
55 | std::cout << [reinterpret_cast<const NSString *>(json) UTF8String] << std::endl; |
56 | CFRelease(json); | |
57 | } | |
62ca2b82 JF |
58 | |
59 | [pool release]; | |
60 | } | |
61 | ||
62 | return 0; | |
63 | } |