]> git.saurik.com Git - cycript.git/blame - Application.mm
Fixed argument serialization comma bug.
[cycript.git] / Application.mm
CommitLineData
057f943f
JF
1#define _GNU_SOURCE
2
693d501b 3#include <substrate.h>
30ddc20c 4#include "cycript.hpp"
057f943f
JF
5
6#include <cstdio>
7#include <sstream>
8
9#include <setjmp.h>
10
11#include <readline/readline.h>
12#include <readline/history.h>
13
14#include "Cycript.tab.hh"
15
16static jmp_buf ctrlc_;
17
18void sigint(int) {
19 longjmp(ctrlc_, 1);
20}
21
22int main(int argc, const char *argv[]) {
23 FILE *fout(stdout);
24
25 rl_bind_key('\t', rl_insert);
26
27 struct sigaction action;
28 sigemptyset(&action.sa_mask);
29 action.sa_handler = &sigint;
30 action.sa_flags = 0;
31 sigaction(SIGINT, &action, NULL);
32
33 restart: for (;;) {
34 std::string command;
35 std::vector<std::string> lines;
36
931b816a
JF
37 bool extra(false);
38 const char *prompt("cy# ");
39
057f943f
JF
40 if (setjmp(ctrlc_) != 0) {
41 fputs("\n", fout);
42 fflush(fout);
43 goto restart;
44 }
45
057f943f
JF
46 read:
47 char *line(readline(prompt));
48 if (line == NULL)
49 break;
931b816a
JF
50
51 if (!extra) {
52 extra = true;
53 if (line[0] == '\\') {
54 goto restart;
55 }
56 }
57
057f943f
JF
58 lines.push_back(line);
59 command += line;
60 free(line);
61
62 CYDriver driver("");
63 cy::parser parser(driver);
64
65 driver.data_ = command.c_str();
66 driver.size_ = command.size();
67
94d55b5c 68 if (parser.parse() != 0 || !driver.errors_.empty()) {
057f943f
JF
69 for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) {
70 cy::position begin(i->location_.begin);
71 if (begin.line != lines.size() || begin.column - 1 != lines.back().size()) {
72 std::cerr << i->message_ << std::endl;
73 goto restart;
74 }
75 }
76
77 driver.errors_.clear();
78
79 command += '\n';
80 prompt = "cy> ";
81 goto read;
82 }
83
84 if (driver.source_ == NULL)
85 goto restart;
86
87 add_history(command.c_str());
88
89 std::ostringstream str;
90 driver.source_->Show(str);
91
92 std::string code(str.str());
93 std::cout << code << std::endl;
94
95 _pooled
96
97 JSStringRef script(JSStringCreateWithUTF8CString(code.c_str()));
98
99 JSContextRef context(CYGetJSContext());
100
101 JSValueRef exception(NULL);
102 JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception));
103 JSStringRelease(script);
104
105 if (exception != NULL)
106 result = exception;
107
108 if (JSValueIsUndefined(context, result))
109 goto restart;
110
111 CFStringRef json;
112
113 @try { json:
114 json = CYCopyJSONString(context, result);
115 } @catch (id error) {
116 CYThrow(context, error, &result);
117 goto json;
118 }
119
120 fputs([reinterpret_cast<const NSString *>(json) UTF8String], fout);
121 CFRelease(json);
62ca2b82 122
057f943f
JF
123 fputs("\n", fout);
124 fflush(fout);
125 }
62ca2b82 126
057f943f
JF
127 fputs("\n", fout);
128 fflush(fout);
e7ed5354 129
057f943f 130 return 0;
62ca2b82 131}