forked from denoland/rusty_v8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.rs
235 lines (202 loc) · 5.57 KB
/
shell.rs
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
fn main() {
// Initialize V8.
let platform = v8::new_default_platform(0, false).make_shared();
v8::V8::initialize_platform(platform);
v8::V8::initialize();
// Pass command line arguments to V8.
let args: Vec<String> = std::env::args().collect();
let args = v8::V8::set_flags_from_command_line(args);
let mut run_shell_flag = args.len() == 1;
let isolate = &mut v8::Isolate::new(v8::CreateParams::default());
let handle_scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(handle_scope);
let context_scope = &mut v8::ContextScope::new(handle_scope, context);
let scope = &mut v8::HandleScope::new(context_scope);
run_main(scope, &args, &mut run_shell_flag);
if run_shell_flag {
run_shell(scope);
}
}
/// Process remaining command line arguments and execute files
fn run_shell(scope: &mut v8::HandleScope) {
use std::io::{self, Write};
println!("V8 version {} [sample shell]", v8::V8::get_version());
loop {
print!("> ");
io::stdout().flush().unwrap();
let mut buf = String::new();
match io::stdin().read_line(&mut buf) {
Ok(n) => {
if n == 0 {
println!();
return;
}
execute_string(scope, &buf, "(shell)", true, true);
}
Err(error) => println!("error: {}", error),
}
}
}
/// Process remaining command line arguments and execute files
fn run_main(
scope: &mut v8::HandleScope,
args: &[String],
run_shell: &mut bool,
) {
let mut skip_next = false;
// Parse command-line arguments.
for (i, arg) in args.iter().enumerate().skip(1) {
if skip_next {
continue;
}
match &**arg {
"--shell" => {
// Enables the shell.
*run_shell = true;
}
"-f" => {
// Ignore any -f flags for compatibility with the other stand-
// alone JavaScript engines.
}
"-e" => {
// Execute script.
let script: &str = &args[i + 1];
skip_next = true;
execute_string(scope, script, "unnamed", false, true);
while v8::Platform::pump_message_loop(
&v8::V8::get_current_platform(),
scope,
false,
) {
// do nothing
}
}
arg => {
if arg.starts_with("--") {
eprintln!("Warning: unknown flag {}.\nTry --help for options", arg);
continue;
}
// Use all other arguments as names of files to load and run.
let script = std::fs::read_to_string(arg).expect("failed to read file");
execute_string(scope, &script, arg, false, true);
while v8::Platform::pump_message_loop(
&v8::V8::get_current_platform(),
scope,
false,
) {
// do nothing
}
}
}
}
}
fn execute_string(
scope: &mut v8::HandleScope,
script: &str,
filename: &str,
print_result: bool,
report_exceptions_flag: bool,
) {
let mut scope = v8::TryCatch::new(scope);
let filename = v8::String::new(&mut scope, filename).unwrap();
let undefined = v8::undefined(&mut scope);
let script = v8::String::new(&mut scope, script).unwrap();
let origin = v8::ScriptOrigin::new(
&mut scope,
filename.into(),
0,
0,
false,
0,
undefined.into(),
false,
false,
false,
);
let script = if let Some(script) =
v8::Script::compile(&mut scope, script, Some(&origin))
{
script
} else {
assert!(scope.has_caught());
if report_exceptions_flag {
report_exceptions(scope);
}
return;
};
if let Some(result) = script.run(&mut scope) {
if print_result {
println!(
"{}",
result
.to_string(&mut scope)
.unwrap()
.to_rust_string_lossy(&mut scope)
);
}
} else {
assert!(scope.has_caught());
if report_exceptions_flag {
report_exceptions(scope);
}
}
}
fn report_exceptions(mut try_catch: v8::TryCatch<v8::HandleScope>) {
let exception = try_catch.exception().unwrap();
let exception_string = exception
.to_string(&mut try_catch)
.unwrap()
.to_rust_string_lossy(&mut try_catch);
let message = if let Some(message) = try_catch.message() {
message
} else {
eprintln!("{}", exception_string);
return;
};
// Print (filename):(line number): (message).
let filename = message
.get_script_resource_name(&mut try_catch)
.map_or_else(
|| "(unknown)".into(),
|s| {
s.to_string(&mut try_catch)
.unwrap()
.to_rust_string_lossy(&mut try_catch)
},
);
let line_number = message.get_line_number(&mut try_catch).unwrap_or_default();
eprintln!("{}:{}: {}", filename, line_number, exception_string);
// Print line of source code.
let source_line = message
.get_source_line(&mut try_catch)
.map(|s| {
s.to_string(&mut try_catch)
.unwrap()
.to_rust_string_lossy(&mut try_catch)
})
.unwrap();
eprintln!("{}", source_line);
// Print wavy underline (GetUnderline is deprecated).
let start_column = message.get_start_column();
let end_column = message.get_end_column();
for _ in 0..start_column {
eprint!(" ");
}
for _ in start_column..end_column {
eprint!("^");
}
eprintln!();
// Print stack trace
let stack_trace = if let Some(stack_trace) = try_catch.stack_trace() {
stack_trace
} else {
return;
};
let stack_trace = unsafe { v8::Local::<v8::String>::cast(stack_trace) };
let stack_trace = stack_trace
.to_string(&mut try_catch)
.map(|s| s.to_rust_string_lossy(&mut try_catch));
if let Some(stack_trace) = stack_trace {
eprintln!("{}", stack_trace);
}
}