|
1 | 1 | use clap::{Parser as ClapParser, Subcommand}; |
2 | 2 | use nova_vm::ecmascript::{ |
3 | | - execution::{agent::Options, initialize_host_defined_realm, Agent, DefaultHostHooks, Realm}, |
| 3 | + execution::{ |
| 4 | + agent::{BoxedAgent, Options}, |
| 5 | + initialize_host_defined_realm, Agent, DefaultHostHooks, Realm, |
| 6 | + }, |
4 | 7 | scripts_and_modules::script::{parse_script, script_evaluation}, |
5 | 8 | types::{Object, Value}, |
6 | 9 | }; |
@@ -55,55 +58,56 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { |
55 | 58 | Command::Eval { verbose, paths } => { |
56 | 59 | let allocator = Default::default(); |
57 | 60 |
|
58 | | - let mut agent = Agent::new( |
| 61 | + let mut agent = BoxedAgent::new( |
59 | 62 | Options { |
60 | 63 | disable_gc: false, |
61 | 64 | print_internals: verbose, |
62 | 65 | }, |
63 | 66 | &DefaultHostHooks, |
64 | 67 | ); |
65 | | - { |
| 68 | + assert!(!paths.is_empty()); |
| 69 | + agent.with(|agent, root_realms| { |
66 | 70 | let create_global_object: Option<fn(&mut Realm) -> Object> = None; |
67 | 71 | let create_global_this_value: Option<fn(&mut Realm) -> Object> = None; |
68 | 72 | initialize_host_defined_realm( |
69 | | - &mut agent, |
| 73 | + agent, |
70 | 74 | create_global_object, |
71 | 75 | create_global_this_value, |
72 | 76 | Some(initialize_global_object), |
73 | 77 | ); |
74 | | - } |
75 | | - let realm = agent.current_realm_id(); |
76 | | - |
77 | | - // `final_result` will always be overwritten in the paths loop, but |
78 | | - // we populate it with a dummy value here so rustc won't complain. |
79 | | - let mut final_result = Ok(Value::Undefined); |
80 | | - |
81 | | - assert!(!paths.is_empty()); |
| 78 | + let realm = agent.current_realm_id(); |
| 79 | + root_realms.push(realm); |
| 80 | + }); |
| 81 | + agent.gc(); |
82 | 82 | for path in paths { |
83 | | - let file = std::fs::read_to_string(&path)?; |
84 | | - let script = match parse_script(&allocator, file.into(), realm, None) { |
85 | | - Ok(script) => script, |
86 | | - Err((file, errors)) => exit_with_parse_errors(errors, &path, &file), |
87 | | - }; |
88 | | - final_result = script_evaluation(&mut agent, script); |
89 | | - if final_result.is_err() { |
90 | | - break; |
91 | | - } |
92 | | - } |
93 | | - |
94 | | - match final_result { |
95 | | - Ok(result) => { |
96 | | - if verbose { |
97 | | - println!("{:?}", result); |
98 | | - } |
99 | | - } |
100 | | - Err(error) => { |
101 | | - eprintln!( |
102 | | - "Uncaught exception: {}", |
103 | | - error.value().string_repr(&mut agent).as_str(&agent) |
104 | | - ); |
105 | | - std::process::exit(1); |
106 | | - } |
| 83 | + agent.with( |
| 84 | + |agent, root_realms| -> Result<(), Box<dyn std::error::Error>> { |
| 85 | + let realm = *root_realms.first().unwrap(); |
| 86 | + let file = std::fs::read_to_string(&path)?; |
| 87 | + let script = match parse_script(&allocator, file.into(), realm, None) { |
| 88 | + Ok(script) => script, |
| 89 | + Err((file, errors)) => exit_with_parse_errors(errors, &path, &file), |
| 90 | + }; |
| 91 | + let result = script_evaluation(agent, script); |
| 92 | + |
| 93 | + match result { |
| 94 | + Ok(result) => { |
| 95 | + if verbose { |
| 96 | + println!("{:?}", result); |
| 97 | + } |
| 98 | + } |
| 99 | + Err(error) => { |
| 100 | + eprintln!( |
| 101 | + "Uncaught exception: {}", |
| 102 | + error.value().string_repr(agent).as_str(agent) |
| 103 | + ); |
| 104 | + std::process::exit(1); |
| 105 | + } |
| 106 | + } |
| 107 | + Ok(()) |
| 108 | + }, |
| 109 | + )?; |
| 110 | + agent.gc(); |
107 | 111 | } |
108 | 112 | } |
109 | 113 | } |
|
0 commit comments