Skip to content

Commit faf668d

Browse files
authored
Merge pull request #46 from tryandromeda/feat/nova-mid-april
feat: Update with Nova changes (mid April)
2 parents e8fcd97 + 8cce4f1 commit faf668d

File tree

14 files changed

+360
-343
lines changed

14 files changed

+360
-343
lines changed

Cargo.lock

Lines changed: 135 additions & 128 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ anymap = "0.12.1"
1818
clap = { version = "4.5.23", features = ["derive"] }
1919
cliclack = "0.3.5"
2020
console = "0.15.8"
21-
nova_vm = { git = "https://github.com/trynova/nova", branch = "main", features = ["typescript"] }
21+
nova_vm = { git = "https://github.com/trynova/nova", rev = "393be1488c62951b6b9cd5b321d9f36c80f16c70", features = ["typescript"] }
2222
oxc_ast = "0.51.0"
2323
oxc_diagnostics = "0.51.0"
2424
oxc-miette = { version = "1.0.2", features = ["fancy"] }

cli/src/main.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,33 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4949
no_strict,
5050
paths,
5151
} => {
52-
let mut runtime = Runtime::new(RuntimeConfig {
52+
let runtime = Runtime::new(RuntimeConfig {
5353
no_strict,
5454
paths,
5555
verbose,
5656
extensions: recommended_extensions(),
5757
builtins: recommended_builtins(),
5858
eventloop_handler: recommended_eventloop_handler,
5959
});
60-
let runtime_result = runtime.run();
60+
let mut runtime_output = runtime.run();
6161

62-
match runtime_result {
62+
match runtime_output.result {
6363
Ok(result) => {
6464
if verbose {
6565
println!("{:?}", result);
6666
}
6767
}
68-
Err(error) => runtime
69-
.agent
70-
.run_in_realm(&runtime.realm_root, |agent, gc| {
71-
eprintln!(
72-
"Uncaught exception: {}",
73-
error.value().string_repr(agent, gc).as_str(agent)
74-
);
75-
std::process::exit(1);
76-
}),
68+
Err(error) => {
69+
runtime_output
70+
.agent
71+
.run_in_realm(&runtime_output.realm_root, |agent, gc| {
72+
eprintln!(
73+
"Uncaught exception: {}",
74+
error.value().string_repr(agent, gc).as_str(agent)
75+
);
76+
std::process::exit(1);
77+
})
78+
}
7779
}
7880
}
7981
});

core/src/event_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use nova_vm::{ecmascript::types::Value, engine::Global};
44
#[derive(Debug)]
55
pub enum MacroTask<UserMacroTask> {
66
/// Resolve a promise.
7-
ResolvePromise(Global<Value>),
7+
ResolvePromise(Global<Value<'static>>),
88
/// User-defined macro task.
99
User(UserMacroTask),
1010
}

core/src/extension.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use nova_vm::{
55
scripts_and_modules::script::{parse_script, script_evaluation},
66
types::{InternalMethods, IntoValue, Object, PropertyDescriptor, PropertyKey},
77
},
8-
engine::context::GcScope,
8+
engine::context::{Bindable, GcScope},
99
};
1010

1111
use crate::{HostData, OpsStorage, exit_with_parse_errors};
@@ -55,15 +55,15 @@ impl Extension {
5555
let script = match parse_script(
5656
agent,
5757
source_text,
58-
agent.current_realm_id(),
58+
agent.current_realm(gc.nogc()),
5959
true,
6060
None,
6161
gc.nogc(),
6262
) {
6363
Ok(script) => script,
6464
Err(diagnostics) => exit_with_parse_errors(diagnostics, "<runtime>", file),
6565
};
66-
match script_evaluation(agent, script, gc.reborrow()) {
66+
match script_evaluation(agent, script.unbind(), gc.reborrow()) {
6767
Ok(_) => (),
6868
Err(_) => println!("Error in runtime"),
6969
}
@@ -72,7 +72,7 @@ impl Extension {
7272
let function = create_builtin_function(
7373
agent,
7474
Behaviour::Regular(op.function),
75-
BuiltinFunctionArgs::new(op.args, op.name, agent.current_realm_id()),
75+
BuiltinFunctionArgs::new(op.args, op.name),
7676
gc.nogc(),
7777
);
7878
function.unbind();
@@ -82,7 +82,7 @@ impl Extension {
8282
agent,
8383
property_key.unbind(),
8484
PropertyDescriptor {
85-
value: Some(function.into_value()),
85+
value: Some(function.into_value().unbind()),
8686
..Default::default()
8787
},
8888
gc.reborrow(),

core/src/runtime.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use nova_vm::{
1616
scripts_and_modules::script::{parse_script, script_evaluation},
1717
types::{self, Object, Value},
1818
},
19-
engine::context::GcScope,
19+
engine::context::{Bindable, GcScope},
2020
};
2121

2222
use crate::{Extension, HostData, MacroTask, exit_with_parse_errors};
@@ -130,11 +130,11 @@ impl<UserMacroTask> Runtime<UserMacroTask> {
130130
}
131131

132132
/// Run the Runtime with the specified configuration.
133-
pub fn run(&mut self) -> JsResult<Value> {
133+
pub fn run(mut self) -> RuntimeOutput {
134134
// Load the builtins js sources
135135
self.agent.run_in_realm(&self.realm_root, |agent, mut gc| {
136-
let realm = agent.current_realm_id();
137136
for builtin in &self.config.builtins {
137+
let realm = agent.current_realm(gc.nogc());
138138
let source_text = types::String::from_str(agent, builtin, gc.nogc());
139139
let script = match parse_script(
140140
agent,
@@ -147,22 +147,22 @@ impl<UserMacroTask> Runtime<UserMacroTask> {
147147
Ok(script) => script,
148148
Err(diagnostics) => exit_with_parse_errors(diagnostics, "<runtime>", builtin),
149149
};
150-
match script_evaluation(agent, script, gc.reborrow()) {
150+
match script_evaluation(agent, script.unbind(), gc.reborrow()) {
151151
Ok(_) => (),
152152
Err(_) => println!("Error in runtime"),
153153
}
154154
}
155155
});
156156

157-
let mut final_result = Value::Null;
157+
let mut result = JsResult::Ok(Value::Null);
158158

159159
// Fetch the runtime mod.ts file using a macro and add it to the paths
160160
for path in &self.config.paths {
161161
let file = std::fs::read_to_string(path).unwrap();
162162

163-
final_result = self.agent.run_in_realm(&self.realm_root, |agent, gc| {
163+
result = self.agent.run_in_realm(&self.realm_root, |agent, mut gc| {
164164
let source_text = types::String::from_string(agent, file, gc.nogc());
165-
let realm = agent.current_realm_id();
165+
let realm = agent.current_realm(gc.nogc());
166166

167167
let script = match parse_script(
168168
agent,
@@ -176,14 +176,15 @@ impl<UserMacroTask> Runtime<UserMacroTask> {
176176
Err(errors) => exit_with_parse_errors(errors, path, source_text.as_str(agent)),
177177
};
178178

179-
script_evaluation(agent, script, gc)
180-
})?;
179+
script_evaluation(agent, script.unbind(), gc.reborrow()).unbind()
180+
});
181181
}
182182

183183
loop {
184184
while let Some(job) = self.host_hooks.pop_promise_job() {
185-
self.agent
186-
.run_in_realm(&self.realm_root, |agent, gc| job.run(agent, gc))?;
185+
result = self.agent.run_in_realm(&self.realm_root, |agent, mut gc| {
186+
job.run(agent, gc.reborrow()).unbind().map(|_| Value::Null)
187+
});
187188
}
188189

189190
// If both the microtasks and macrotasks queues are empty we can end the event loop
@@ -194,7 +195,11 @@ impl<UserMacroTask> Runtime<UserMacroTask> {
194195
self.handle_macro_task();
195196
}
196197

197-
Ok(final_result)
198+
RuntimeOutput {
199+
agent: self.agent,
200+
realm_root: self.realm_root,
201+
result,
202+
}
198203
}
199204

200205
// Listen for pending macro tasks and resolve one by one
@@ -224,3 +229,9 @@ impl<UserMacroTask> Runtime<UserMacroTask> {
224229
}
225230
}
226231
}
232+
233+
pub struct RuntimeOutput {
234+
pub agent: GcAgent,
235+
pub realm_root: RealmRoot,
236+
pub result: JsResult<'static, Value<'static>>,
237+
}

runtime/src/ext/console/mod.rs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use nova_vm::{
77
execution::{Agent, JsResult},
88
types::Value,
99
},
10-
engine::context::GcScope,
10+
engine::context::{Bindable, GcScope},
1111
};
1212

1313
#[derive(Default)]
@@ -31,16 +31,17 @@ impl ConsoleExt {
3131
}
3232

3333
/// Print function that prints the first argument to the console.
34-
fn internal_print(
34+
fn internal_print<'gc>(
3535
agent: &mut Agent,
3636
_this: Value,
3737
args: ArgumentsList,
38-
mut gc: GcScope<'_, '_>,
39-
) -> JsResult<Value> {
38+
mut gc: GcScope<'gc, '_>,
39+
) -> JsResult<'gc, Value<'gc>> {
4040
stdout()
4141
.write_all(
4242
args[0]
43-
.to_string(agent, gc.reborrow())?
43+
.to_string(agent, gc.reborrow())
44+
.unbind()?
4445
.as_str(agent)
4546
.as_bytes(),
4647
)
@@ -50,69 +51,67 @@ impl ConsoleExt {
5051
}
5152

5253
/// Exit the process with the given exit code.
53-
pub fn internal_exit(
54+
pub fn internal_exit<'gc>(
5455
agent: &mut Agent,
5556
_this: Value,
5657
args: ArgumentsList,
57-
mut gc: GcScope<'_, '_>,
58-
) -> JsResult<Value> {
59-
std::process::exit(args[0].to_int32(agent, gc.reborrow())?);
58+
mut gc: GcScope<'gc, '_>,
59+
) -> JsResult<'gc, Value<'gc>> {
60+
std::process::exit(args[0].to_int32(agent, gc.reborrow()).unbind()?);
6061
}
6162

6263
/// Internal read for reading from the console.
63-
pub fn internal_read(
64+
pub fn internal_read<'gc>(
6465
agent: &mut Agent,
6566
_this: Value,
6667
_args: ArgumentsList,
67-
gc: GcScope<'_, '_>,
68-
) -> JsResult<Value> {
68+
gc: GcScope<'gc, '_>,
69+
) -> JsResult<'gc, Value<'gc>> {
6970
let mut input = String::new();
7071
std::io::stdin().read_line(&mut input).unwrap();
71-
Ok(Value::from_string(
72-
agent,
73-
input.trim_end().to_string(),
74-
gc.nogc(),
75-
))
72+
Ok(Value::from_string(agent, input.trim_end().to_string(), gc.nogc()).unbind())
7673
}
7774

7875
/// Internal read line for reading from the console with a newline.
79-
pub fn internal_read_line(
76+
pub fn internal_read_line<'gc>(
8077
agent: &mut Agent,
8178
_this: Value,
8279
_args: ArgumentsList,
83-
gc: GcScope<'_, '_>,
84-
) -> JsResult<Value> {
80+
gc: GcScope<'gc, '_>,
81+
) -> JsResult<'gc, Value<'gc>> {
8582
let mut input = String::new();
8683
std::io::stdin().read_line(&mut input).unwrap();
87-
Ok(Value::from_string(
88-
agent,
89-
input.trim_end().to_string(),
90-
gc.nogc(),
91-
))
84+
Ok(Value::from_string(agent, input.trim_end().to_string(), gc.nogc()).unbind())
9285
}
9386

9487
/// Internal write for writing to the console.
95-
pub fn internal_write(
88+
pub fn internal_write<'gc>(
9689
agent: &mut Agent,
9790
_this: Value,
9891
args: ArgumentsList,
99-
mut gc: GcScope<'_, '_>,
100-
) -> JsResult<Value> {
92+
mut gc: GcScope<'gc, '_>,
93+
) -> JsResult<'gc, Value<'gc>> {
10194
for arg in args.iter() {
102-
print!("{}", arg.to_string(agent, gc.reborrow())?.as_str(agent));
95+
print!(
96+
"{}",
97+
arg.to_string(agent, gc.reborrow()).unbind()?.as_str(agent)
98+
);
10399
}
104100
Ok(Value::Undefined)
105101
}
106102

107103
/// Internal write line for writing to the console with a newline.
108-
pub fn internal_write_line(
104+
pub fn internal_write_line<'gc>(
109105
agent: &mut Agent,
110106
_this: Value,
111107
args: ArgumentsList,
112-
mut gc: GcScope<'_, '_>,
113-
) -> JsResult<Value> {
108+
mut gc: GcScope<'gc, '_>,
109+
) -> JsResult<'gc, Value<'gc>> {
114110
for arg in args.iter() {
115-
print!("{}", arg.to_string(agent, gc.reborrow())?.as_str(agent));
111+
print!(
112+
"{}",
113+
arg.to_string(agent, gc.reborrow()).unbind()?.as_str(agent)
114+
);
116115
}
117116
println!();
118117
Ok(Value::Undefined)

0 commit comments

Comments
 (0)