Skip to content

Commit 1763891

Browse files
committed
Use gc
1 parent dda355a commit 1763891

38 files changed

+438
-256
lines changed

nova_cli/src/main.rs

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use clap::{Parser as ClapParser, Subcommand};
22
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+
},
47
scripts_and_modules::script::{parse_script, script_evaluation},
58
types::{Object, Value},
69
};
@@ -55,55 +58,56 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5558
Command::Eval { verbose, paths } => {
5659
let allocator = Default::default();
5760

58-
let mut agent = Agent::new(
61+
let mut agent = BoxedAgent::new(
5962
Options {
6063
disable_gc: false,
6164
print_internals: verbose,
6265
},
6366
&DefaultHostHooks,
6467
);
65-
{
68+
assert!(!paths.is_empty());
69+
agent.with(|agent, root_realms| {
6670
let create_global_object: Option<fn(&mut Realm) -> Object> = None;
6771
let create_global_this_value: Option<fn(&mut Realm) -> Object> = None;
6872
initialize_host_defined_realm(
69-
&mut agent,
73+
agent,
7074
create_global_object,
7175
create_global_this_value,
7276
Some(initialize_global_object),
7377
);
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();
8282
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();
107111
}
108112
}
109113
}

nova_vm/src/ecmascript/builtins/array.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ impl HeapMarkAndSweep for Array {
477477
}
478478

479479
fn sweep_values(&mut self, compactions: &crate::heap::CompactionLists) {
480-
let idx = self.0.into_u32_index();
481-
self.0 = ArrayIndex::from_u32_index(idx - compactions.arrays.get_shift_for_index(idx));
480+
compactions.arrays.shift_index(&mut self.0);
482481
}
483482
}

nova_vm/src/ecmascript/builtins/array_buffer.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,7 @@ impl HeapMarkAndSweep for ArrayBuffer {
129129
}
130130

131131
fn sweep_values(&mut self, compactions: &CompactionLists) {
132-
let self_index = self.0.into_u32();
133-
self.0 = ArrayBufferIndex::from_u32(
134-
self_index - compactions.array_buffers.get_shift_for_index(self_index),
135-
);
132+
compactions.array_buffers.shift_index(&mut self.0);
136133
}
137134
}
138135

nova_vm/src/ecmascript/builtins/bound_function.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,7 @@ impl HeapMarkAndSweep for BoundFunction {
348348
}
349349

350350
fn sweep_values(&mut self, compactions: &CompactionLists) {
351-
let self_index = self.0.into_u32();
352-
self.0 = BoundFunctionIndex::from_u32(
353-
self_index - compactions.bound_functions.get_shift_for_index(self_index),
354-
);
351+
compactions.bound_functions.shift_index(&mut self.0);
355352
}
356353
}
357354

@@ -360,13 +357,15 @@ impl HeapMarkAndSweep for BoundFunctionHeapData {
360357
self.name.mark_values(queues);
361358
self.bound_target_function.mark_values(queues);
362359
self.object_index.mark_values(queues);
360+
self.bound_this.mark_values(queues);
363361
self.bound_arguments.mark_values(queues);
364362
}
365363

366364
fn sweep_values(&mut self, compactions: &CompactionLists) {
367365
self.name.sweep_values(compactions);
368366
self.bound_target_function.sweep_values(compactions);
369367
self.object_index.sweep_values(compactions);
368+
self.bound_this.sweep_values(compactions);
370369
self.bound_arguments.sweep_values(compactions);
371370
}
372371
}

nova_vm/src/ecmascript/builtins/builtin_function.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -716,23 +716,20 @@ impl HeapMarkAndSweep for BuiltinFunction {
716716
}
717717

718718
fn sweep_values(&mut self, compactions: &CompactionLists) {
719-
let self_index = self.0.into_u32();
720-
self.0 = BuiltinFunctionIndex::from_u32(
721-
self_index
722-
- compactions
723-
.builtin_functions
724-
.get_shift_for_index(self_index),
725-
);
719+
compactions.builtin_functions.shift_index(&mut self.0);
726720
}
727721
}
728722

729723
impl HeapMarkAndSweep for BuiltinFunctionHeapData {
730724
fn mark_values(&self, queues: &mut WorkQueues) {
725+
// Note: Builtin functions cannot keep their realm alive.
726+
// self.realm.mark_values(queues);
731727
self.initial_name.mark_values(queues);
732728
self.object_index.mark_values(queues);
733729
}
734730

735731
fn sweep_values(&mut self, compactions: &CompactionLists) {
732+
self.realm.sweep_values(compactions);
736733
self.initial_name.sweep_values(compactions);
737734
self.object_index.sweep_values(compactions);
738735
}

nova_vm/src/ecmascript/builtins/date.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,7 @@ impl HeapMarkAndSweep for Date {
142142
}
143143

144144
fn sweep_values(&mut self, compactions: &CompactionLists) {
145-
let self_index = self.0.into_u32();
146-
self.0 =
147-
DateIndex::from_u32(self_index - compactions.dates.get_shift_for_index(self_index));
145+
compactions.dates.shift_index(&mut self.0);
148146
}
149147
}
150148

nova_vm/src/ecmascript/builtins/ecmascript_function.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,13 +1398,7 @@ impl HeapMarkAndSweep for ECMAScriptFunction {
13981398
}
13991399

14001400
fn sweep_values(&mut self, compactions: &CompactionLists) {
1401-
let self_index = self.0.into_u32();
1402-
self.0 = ECMAScriptFunctionIndex::from_u32(
1403-
self_index
1404-
- compactions
1405-
.ecmascript_functions
1406-
.get_shift_for_index(self_index),
1407-
);
1401+
compactions.ecmascript_functions.shift_index(&mut self.0);
14081402
}
14091403
}
14101404

nova_vm/src/ecmascript/builtins/error.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,7 @@ impl HeapMarkAndSweep for Error {
249249
}
250250

251251
fn sweep_values(&mut self, compactions: &CompactionLists) {
252-
let self_index = self.0.into_u32();
253-
self.0 =
254-
ErrorIndex::from_u32(self_index - compactions.errors.get_shift_for_index(self_index));
252+
compactions.errors.shift_index(&mut self.0);
255253
}
256254
}
257255

nova_vm/src/ecmascript/builtins/map.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ impl HeapMarkAndSweep for Map {
125125
}
126126

127127
fn sweep_values(&mut self, compactions: &CompactionLists) {
128-
let self_index = self.0.into_u32();
129-
self.0 = MapIndex::from_u32(self_index - compactions.maps.get_shift_for_index(self_index));
128+
compactions.maps.shift_index(&mut self.0);
130129
}
131130
}
132131

nova_vm/src/ecmascript/builtins/module/data.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,19 @@ impl ModuleRecord {
8181
}
8282

8383
impl HeapMarkAndSweep for ModuleHeapData {
84-
fn mark_values(&self, _queues: &mut WorkQueues) {}
84+
fn mark_values(&self, queues: &mut WorkQueues) {
85+
for ele in self.exports.iter() {
86+
ele.mark_values(queues);
87+
}
88+
self.module.namespace.mark_values(queues);
89+
self.object_index.mark_values(queues);
90+
}
8591

86-
fn sweep_values(&mut self, _compactions: &CompactionLists) {}
92+
fn sweep_values(&mut self, compactions: &CompactionLists) {
93+
for ele in self.exports.iter_mut() {
94+
ele.sweep_values(compactions);
95+
}
96+
self.module.namespace.sweep_values(compactions);
97+
self.object_index.sweep_values(compactions);
98+
}
8799
}

0 commit comments

Comments
 (0)