4
4
mod helper;
5
5
mod theme;
6
6
7
- use std:: { cell:: RefCell , collections:: VecDeque , fmt:: Debug } ;
8
-
9
7
use clap:: { Parser as ClapParser , Subcommand } ;
10
8
use cliclack:: { input, intro, set_theme} ;
11
- use helper:: { exit_with_parse_errors, initialize_global_object} ;
12
- use nova_vm:: ecmascript:: {
13
- execution:: {
14
- agent:: { HostHooks , Job , Options } ,
15
- initialize_host_defined_realm, Agent , Realm ,
16
- } ,
17
- scripts_and_modules:: script:: { parse_script, script_evaluation} ,
18
- types:: { Object , Value } ,
19
- } ;
9
+ use helper:: { exit_with_parse_errors, CliRunner } ;
10
+ use nova_vm:: ecmascript:: types:: Value ;
20
11
use oxc_parser:: Parser ;
21
12
use oxc_semantic:: { SemanticBuilder , SemanticBuilderReturn } ;
22
13
use oxc_span:: SourceType ;
@@ -56,32 +47,6 @@ enum Command {
56
47
Repl { } ,
57
48
}
58
49
59
- #[ derive( Default ) ]
60
- struct CliHostHooks {
61
- promise_job_queue : RefCell < VecDeque < Job > > ,
62
- }
63
-
64
- // RefCell doesn't implement Debug
65
- impl Debug for CliHostHooks {
66
- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
67
- f. debug_struct ( "CliHostHooks" )
68
- //.field("promise_job_queue", &*self.promise_job_queue.borrow())
69
- . finish ( )
70
- }
71
- }
72
-
73
- impl CliHostHooks {
74
- fn pop_promise_job ( & self ) -> Option < Job > {
75
- self . promise_job_queue . borrow_mut ( ) . pop_front ( )
76
- }
77
- }
78
-
79
- impl HostHooks for CliHostHooks {
80
- fn enqueue_promise_job ( & self , job : Job ) {
81
- self . promise_job_queue . borrow_mut ( ) . push_back ( job) ;
82
- }
83
- }
84
-
85
50
fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
86
51
let args = Cli :: parse ( ) ;
87
52
@@ -112,27 +77,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
112
77
no_strict,
113
78
paths,
114
79
} => {
115
- let allocator = Default :: default ( ) ;
116
-
117
- let host_hooks: & CliHostHooks = & * Box :: leak ( Box :: default ( ) ) ;
118
- let mut agent = Agent :: new (
119
- Options {
120
- disable_gc : false ,
121
- print_internals : verbose,
122
- } ,
123
- host_hooks,
124
- ) ;
125
- {
126
- let create_global_object: Option < fn ( & mut Realm ) -> Object > = None ;
127
- let create_global_this_value: Option < fn ( & mut Realm ) -> Object > = None ;
128
- initialize_host_defined_realm (
129
- & mut agent,
130
- create_global_object,
131
- create_global_this_value,
132
- Some ( initialize_global_object) ,
133
- ) ;
134
- }
135
- let realm = agent. current_realm_id ( ) ;
80
+ let mut cli_runner = CliRunner :: new ( verbose) ;
136
81
137
82
// `final_result` will always be overwritten in the paths loop, but
138
83
// we populate it with a dummy value here so rustc won't complain.
@@ -141,25 +86,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
141
86
assert ! ( !paths. is_empty( ) ) ;
142
87
for path in paths {
143
88
let file = std:: fs:: read_to_string ( & path) ?;
144
- let script = match parse_script ( & allocator, file. into ( ) , realm, !no_strict, None ) {
145
- Ok ( script) => script,
146
- Err ( ( file, errors) ) => exit_with_parse_errors ( errors, & path, & file) ,
147
- } ;
148
- final_result = script_evaluation ( & mut agent, script) ;
89
+ final_result = cli_runner. run_script_and_microtasks ( file. into ( ) , & path, no_strict) ;
149
90
if final_result. is_err ( ) {
150
91
break ;
151
92
}
152
93
}
153
94
154
- if final_result. is_ok ( ) {
155
- while let Some ( job) = host_hooks. pop_promise_job ( ) {
156
- if let Err ( err) = job. run ( & mut agent) {
157
- final_result = Err ( err) ;
158
- break ;
159
- }
160
- }
161
- }
162
-
163
95
match final_result {
164
96
Ok ( result) => {
165
97
if verbose {
@@ -169,33 +101,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
169
101
Err ( error) => {
170
102
eprintln ! (
171
103
"Uncaught exception: {}" ,
172
- error. value( ) . string_repr( & mut agent) . as_str( & agent)
104
+ error
105
+ . value( )
106
+ . string_repr( cli_runner. agent( ) )
107
+ . as_str( cli_runner. agent( ) )
173
108
) ;
174
109
std:: process:: exit ( 1 ) ;
175
110
}
176
111
}
112
+ std:: process:: exit ( 0 ) ;
177
113
}
178
114
Command :: Repl { } => {
179
- let allocator = Default :: default ( ) ;
180
- let host_hooks: & CliHostHooks = & * Box :: leak ( Box :: default ( ) ) ;
181
- let mut agent = Agent :: new (
182
- Options {
183
- disable_gc : false ,
184
- print_internals : true ,
185
- } ,
186
- host_hooks,
187
- ) ;
188
- {
189
- let create_global_object: Option < fn ( & mut Realm ) -> Object > = None ;
190
- let create_global_this_value: Option < fn ( & mut Realm ) -> Object > = None ;
191
- initialize_host_defined_realm (
192
- & mut agent,
193
- create_global_object,
194
- create_global_this_value,
195
- Some ( initialize_global_object) ,
196
- ) ;
197
- }
198
- let realm = agent. current_realm_id ( ) ;
115
+ let mut cli_runner = CliRunner :: new ( false ) ;
199
116
200
117
set_theme ( DefaultTheme ) ;
201
118
println ! ( "\n \n " ) ;
@@ -209,21 +126,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
209
126
std:: process:: exit ( 0 ) ;
210
127
}
211
128
placeholder = input. to_string ( ) ;
212
- let script = match parse_script ( & allocator, input. into ( ) , realm, true , None ) {
213
- Ok ( script) => script,
214
- Err ( ( file, errors) ) => {
215
- exit_with_parse_errors ( errors, "<stdin>" , & file) ;
216
- }
217
- } ;
218
- let result = script_evaluation ( & mut agent, script) ;
219
- match result {
129
+
130
+ match cli_runner. run_script_and_microtasks ( input. into ( ) , "<stdin>" , false ) {
220
131
Ok ( result) => {
221
132
println ! ( "{:?}\n " , result) ;
222
133
}
223
134
Err ( error) => {
224
135
eprintln ! (
225
136
"Uncaught exception: {}" ,
226
- error. value( ) . string_repr( & mut agent) . as_str( & agent)
137
+ error
138
+ . value( )
139
+ . string_repr( cli_runner. agent( ) )
140
+ . as_str( cli_runner. agent( ) )
227
141
) ;
228
142
}
229
143
}
0 commit comments