Skip to content

Commit

Permalink
Migrate to register vm
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed Dec 10, 2024
1 parent d91a10c commit 2bab5ac
Show file tree
Hide file tree
Showing 101 changed files with 8,127 additions and 5,348 deletions.
2 changes: 1 addition & 1 deletion core/engine/src/builtins/async_generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub(crate) struct AsyncGeneratorRequest {
}

/// The internal representation of an `AsyncGenerator` object.
#[derive(Debug, Clone, Finalize, Trace, JsData)]
#[derive(Debug, Finalize, Trace, JsData)]
pub struct AsyncGenerator {
/// The `[[AsyncGeneratorState]]` internal slot.
#[unsafe_ignore_trace]
Expand Down
5 changes: 3 additions & 2 deletions core/engine/src/builtins/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
object::JsObject,
realm::Realm,
string::StaticJsStrings,
vm::{CallFrame, CallFrameFlags, Constant, Opcode},
vm::{CallFrame, CallFrameFlags, Constant, Opcode, Registers},
Context, JsArgs, JsResult, JsString, JsValue,
};
use boa_ast::{
Expand Down Expand Up @@ -328,7 +328,8 @@ impl Eval {

context.realm().resize_global_env();

let record = context.run();
let register_count = context.vm.frame().code_block().register_count;
let record = context.run(&mut Registers::new(register_count as usize));
context.vm.pop_frame();

record.consume()
Expand Down
6 changes: 4 additions & 2 deletions core/engine/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,7 @@ pub(crate) fn function_call(
let code = function.code.clone();
let environments = function.environments.clone();
let script_or_module = function.script_or_module.clone();
let register_count = code.register_count as usize;

drop(function);

Expand Down Expand Up @@ -1036,7 +1037,7 @@ pub(crate) fn function_call(
);
}

Ok(CallValue::Ready)
Ok(CallValue::Ready { register_count })
}

/// Construct an instance of this object with the specified arguments.
Expand Down Expand Up @@ -1065,6 +1066,7 @@ fn function_construct(
let code = function.code.clone();
let environments = function.environments.clone();
let script_or_module = function.script_or_module.clone();
let register_count = code.register_count as usize;
drop(function);

let env_fp = environments.len() as u32;
Expand Down Expand Up @@ -1143,5 +1145,5 @@ fn function_construct(
this.map(JsValue::new).unwrap_or_default(),
);

Ok(CallValue::Ready)
Ok(CallValue::Ready { register_count })
}
29 changes: 21 additions & 8 deletions core/engine/src/builtins/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
string::StaticJsStrings,
symbol::JsSymbol,
value::JsValue,
vm::{CallFrame, CallFrameFlags, CompletionRecord, GeneratorResumeKind},
vm::{CallFrame, CallFrameFlags, CompletionRecord, GeneratorResumeKind, Registers},
Context, JsArgs, JsData, JsError, JsResult, JsString,
};
use boa_gc::{custom_trace, Finalize, Trace};
Expand All @@ -29,7 +29,7 @@ use boa_profiler::Profiler;
use super::{BuiltInBuilder, IntrinsicObject};

/// Indicates the state of a generator.
#[derive(Debug, Clone, Finalize)]
#[derive(Debug, Finalize)]
pub(crate) enum GeneratorState {
SuspendedStart {
/// The `[[GeneratorContext]]` internal slot.
Expand Down Expand Up @@ -57,15 +57,20 @@ unsafe impl Trace for GeneratorState {
///
/// All of the fields must be changed with those that are currently present in the
/// context/vm before the generator execution starts/resumes and after it has ended/yielded.
#[derive(Debug, Clone, Trace, Finalize)]
#[derive(Debug, Trace, Finalize)]
pub(crate) struct GeneratorContext {
pub(crate) stack: Vec<JsValue>,
pub(crate) call_frame: Option<CallFrame>,
stack: Vec<JsValue>,
call_frame: Option<CallFrame>,
registers: Registers,
}

impl GeneratorContext {
/// Creates a new `GeneratorContext` from the current `Context` state.
pub(crate) fn from_current(context: &mut Context) -> Self {
pub(crate) fn from_current(
context: &mut Context,
mut registers: Registers,
async_generator: Option<JsObject>,
) -> Self {
let mut frame = context.vm.frame().clone();
frame.environments = context.vm.environments.clone();
frame.realm = context.realm().clone();
Expand All @@ -78,9 +83,17 @@ impl GeneratorContext {
// So we don't need to push the registers in subsequent calls.
frame.flags |= CallFrameFlags::REGISTERS_ALREADY_PUSHED;

if let Some(async_generator) = async_generator {
registers.set(
CallFrame::ASYNC_GENERATOR_OBJECT_REGISTER_INDEX,
async_generator.into(),
);
}

Self {
call_frame: Some(frame),
stack,
registers,
}
}

Expand All @@ -105,7 +118,7 @@ impl GeneratorContext {
}
context.vm.push(resume_kind);

let result = context.run();
let result = context.run(&mut self.registers);

std::mem::swap(&mut context.vm.stack, &mut self.stack);
self.call_frame = context.vm.pop_frame();
Expand All @@ -117,7 +130,7 @@ impl GeneratorContext {
pub(crate) fn async_generator_object(&self) -> Option<JsObject> {
self.call_frame
.as_ref()
.and_then(|frame| frame.async_generator_object(&self.stack))
.and_then(|frame| frame.async_generator_object(&self.registers))
}
}

Expand Down
5 changes: 3 additions & 2 deletions core/engine/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
string::{CodePoint, StaticJsStrings},
symbol::JsSymbol,
value::IntegerOrInfinity,
vm::{CallFrame, CallFrameFlags},
vm::{CallFrame, CallFrameFlags, Registers},
Context, JsArgs, JsBigInt, JsResult, JsString, JsValue,
};
use boa_gc::Gc;
Expand Down Expand Up @@ -142,7 +142,8 @@ impl Json {
);

context.realm().resize_global_env();
let record = context.run();
let register_count = context.vm.frame().code_block().register_count;
let record = context.run(&mut Registers::new(register_count as usize));
context.vm.pop_frame();

let unfiltered = record.consume()?;
Expand Down
Loading

0 comments on commit 2bab5ac

Please sign in to comment.