/
SandBox_repl.cpp
228 lines (181 loc) · 5.74 KB
/
SandBox_repl.cpp
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
/*
* SandBox_cmd.cpp
*
* Created on: Mar 30, 2014
* Author: lion
*/
#include "object.h"
#include "version.h"
#include "SandBox.h"
#include "ifs/process.h"
#include "ifs/console.h"
#include "ifs/util.h"
#include "BufferedStream.h"
#include "console.h"
#include "parse.h"
namespace fibjs {
void asyncLog(int32_t priority, exlib::string msg);
void output(int32_t priority, exlib::string msg)
{
if (priority == console_base::_ERROR)
msg = logger::error() + msg + COLOR_RESET;
if (priority != console_base::_PRINT)
msg += '\n';
asyncLog(console_base::_PRINT, msg);
}
bool repl_command(exlib::string& line, v8::Local<v8::Array> cmds)
{
_parser p(line);
exlib::string cmd_word;
result_t hr;
int32_t len = cmds->Length();
int32_t i;
p.skipSpace();
p.getWord(cmd_word);
if (cmd_word == ".help") {
exlib::string help_str = ".exit Exit the repl\n"
".help Show repl options\n"
".info Show fibjs build information";
Isolate* isolate = Isolate::current();
for (i = 0; i < len; i++) {
v8::Local<v8::Value> v = cmds->Get(i);
v8::Local<v8::Object> o;
exlib::string cmd;
exlib::string help;
hr = GetArgumentValue(v, o, true);
if (hr >= 0) {
hr = GetConfigValue(isolate->m_isolate, o, "cmd", cmd, true);
if (hr >= 0)
hr = GetConfigValue(isolate->m_isolate, o, "help", help, true);
}
if (hr < 0) {
output(console_base::_ERROR, "Invalid cmds argument.");
return false;
}
cmd.append(10, ' ');
help_str = help_str + "\n" + cmd.substr(0, 10) + help;
}
output(console_base::_INFO, help_str);
return true;
}
if (cmd_word == ".exit")
return false;
if (cmd_word == ".info") {
v8::Local<v8::Object> o;
process_base::get_versions(o);
console_base::dir(o);
return true;
}
Isolate* isolate = Isolate::current();
for (i = 0; i < len; i++) {
v8::Local<v8::Value> v = cmds->Get(i);
v8::Local<v8::Object> o;
exlib::string cmd;
v8::Local<v8::Function> exec;
hr = GetArgumentValue(v, o, true);
if (hr >= 0) {
hr = GetConfigValue(isolate->m_isolate, o, "cmd", cmd, true);
if (hr >= 0)
hr = GetConfigValue(isolate->m_isolate, o, "exec", exec, true);
}
if (hr < 0) {
output(console_base::_ERROR, "Invalid cmds argument.");
return false;
}
if (cmd_word == cmd) {
v8::Local<v8::Array> argv = v8::Array::New(isolate->m_isolate);
int32_t n = 0;
while (!cmd_word.empty()) {
argv->Set(n++, GetReturnValue(isolate->m_isolate, cmd_word));
p.skipSpace();
p.getWord(cmd_word);
}
TryCatch try_catch;
v = argv;
v = exec->Call(o, 1, &v);
if (v.IsEmpty())
ReportException(try_catch, 0);
return true;
}
}
output(console_base::_ERROR, cmd_word + ": command not found.");
return true;
}
result_t SandBox::repl(v8::Local<v8::Array> cmds, Stream_base* out)
{
Context context(this, "repl");
return Context::repl(cmds, out);
}
extern stream_logger* s_stream;
extern std_logger* s_std;
result_t SandBox::Context::repl(v8::Local<v8::Array> cmds, Stream_base* out)
{
result_t hr = 0;
exlib::string buf;
v8::Local<v8::Value> v, v1;
Isolate* isolate = Isolate::current();
v8::Local<v8::String> strFname = isolate->NewString("repl", 4);
obj_ptr<BufferedStream_base> bs;
stream_logger* logger = NULL;
if (out) {
if ((logger = s_stream) != NULL) {
s_stream = NULL;
logger->close();
}
s_stream = logger = new stream_logger(out);
bs = new BufferedStream(out);
}
exlib::string str_ver("Welcome to fibjs ");
str_ver += fibjs_version;
str_ver += '.';
output(console_base::_INFO, str_ver);
output(console_base::_INFO, "Type \".help\" for more information.");
while (true) {
if (!v.IsEmpty() && !v->IsUndefined())
console_base::dir(v);
v = v1;
exlib::string line;
if (out) {
output(console_base::_PRINT, buf.empty() ? "> " : " ... ");
hr = bs->ac_readLine(-1, line);
if (hr >= 0)
s_std->log(console_base::_INFO, line);
} else
hr = console_base::ac_readLine(buf.empty() ? "> " : " ... ", line);
if (hr < 0)
break;
if (line.empty())
continue;
if (line[0] == '.') {
if (!repl_command(line, cmds))
break;
continue;
}
buf += line;
buf.append("\n", 1);
{
v8::Local<v8::Script> script;
TryCatch try_catch;
script = v8::Script::Compile(isolate->NewString(buf), strFname);
if (script.IsEmpty()) {
v8::String::Utf8Value exception(try_catch.Exception());
if (*exception && qstrcmp(*exception, "SyntaxError: Unexpected end of input")) {
buf.clear();
ReportException(try_catch, 0, true);
}
continue;
}
buf.clear();
v = script->Run();
if (v.IsEmpty())
ReportException(try_catch, 0, true);
}
}
if (out) {
if (logger == s_stream)
s_stream = NULL;
delete logger;
}
return hr;
}
}