Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove Context from ByteCompiler
Browse files Browse the repository at this point in the history
HalidOdat committed Apr 25, 2024
1 parent b63210a commit a46a04f
Showing 10 changed files with 87 additions and 256 deletions.
42 changes: 26 additions & 16 deletions core/engine/src/builtins/eval/mod.rs
Original file line number Diff line number Diff line change
@@ -9,11 +9,13 @@
//! [spec]: https://tc39.es/ecma262/#sec-eval-x
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
use std::rc::Rc;

use crate::{
builtins::{function::OrdinaryFunction, BuiltInObject},
bytecompiler::{eval_declaration_instantiation_context, ByteCompiler},
context::intrinsics::Intrinsics,
environments::Environment,
environments::{CompileTimeEnvironment, Environment},
error::JsNativeError,
js_string,
object::JsObject,
@@ -229,34 +231,42 @@ impl Eval {
let var_environment = context.vm.environments.outer_function_environment();
let mut var_env = var_environment.compile_env();

let lex_env = context.vm.environments.current_compile_environment();
let lex_env = Rc::new(CompileTimeEnvironment::new(lex_env, strict));

let mut annex_b_function_names = Vec::new();

eval_declaration_instantiation_context(
&mut annex_b_function_names,
&body,
strict,
if strict { &lex_env } else { &var_env },
&lex_env,
context,
)?;

let mut compiler = ByteCompiler::new(
js_string!("<main>"),
body.strict(),
false,
var_env.clone(),
context.vm.environments.current_compile_environment(),
context,
lex_env.clone(),
context.interner_mut(),
);

let env_index = compiler.push_compile_environment(strict);
compiler.current_open_environments_count += 1;

let env_index = compiler.constants.len() as u32;
compiler
.constants
.push(crate::vm::Constant::CompileTimeEnvironment(lex_env.clone()));

compiler.emit_with_varying_operand(Opcode::PushDeclarativeEnvironment, env_index);
let lex_env = compiler.lexical_environment.clone();
if strict {
var_env = lex_env.clone();
compiler.variable_environment = lex_env.clone();
}

let mut annex_b_function_names = Vec::new();

eval_declaration_instantiation_context(
&mut annex_b_function_names,
&body,
strict,
&var_env,
&lex_env,
compiler.context,
)?;

#[cfg(feature = "annex-b")]
{
compiler.annex_b_function_names = annex_b_function_names;
2 changes: 1 addition & 1 deletion core/engine/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
@@ -624,7 +624,7 @@ impl BuiltInFunctionObject {
&body,
context.realm().environment().compile_env(),
context.realm().environment().compile_env(),
context,
context.interner_mut(),
);

let environments = context.vm.environments.pop_to_global();
2 changes: 1 addition & 1 deletion core/engine/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
@@ -118,7 +118,7 @@ impl Json {
true,
context.realm().environment().compile_env(),
context.realm().environment().compile_env(),
context,
context.interner_mut(),
);
compiler.compile_statement_list(script.statements(), true, false);
Gc::new(compiler.finish())
10 changes: 5 additions & 5 deletions core/engine/src/bytecompiler/class.rs
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ impl ByteCompiler<'_> {
self.json_parse,
self.variable_environment.clone(),
self.lexical_environment.clone(),
self.context,
self.interner,
);

compiler.code_block_flags |= CodeBlockFlags::IS_CLASS_CONSTRUCTOR;
@@ -287,7 +287,7 @@ impl ByteCompiler<'_> {
self.json_parse,
self.variable_environment.clone(),
self.lexical_environment.clone(),
self.context,
self.interner,
);

// Function environment
@@ -315,7 +315,7 @@ impl ByteCompiler<'_> {
self.json_parse,
self.variable_environment.clone(),
self.lexical_environment.clone(),
self.context,
self.interner,
);
let _ = field_compiler.push_compile_environment(true);
if let Some(node) = field {
@@ -353,7 +353,7 @@ impl ByteCompiler<'_> {
self.json_parse,
self.variable_environment.clone(),
self.lexical_environment.clone(),
self.context,
self.interner,
);
let _ = field_compiler.push_compile_environment(true);
if let Some(node) = field {
@@ -387,7 +387,7 @@ impl ByteCompiler<'_> {
false,
self.variable_environment.clone(),
self.lexical_environment.clone(),
self.context,
self.interner,
);
let _ = compiler.push_compile_environment(true);

246 changes: 35 additions & 211 deletions core/engine/src/bytecompiler/declarations.rs

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions core/engine/src/bytecompiler/function.rs
Original file line number Diff line number Diff line change
@@ -6,10 +6,11 @@ use crate::{
environments::CompileTimeEnvironment,
js_string,
vm::{CodeBlock, CodeBlockFlags, Opcode},
Context, JsString,
JsString,
};
use boa_ast::function::{FormalParameterList, FunctionBody};
use boa_gc::Gc;
use boa_interner::Interner;

/// `FunctionCompiler` is used to compile AST functions to bytecode.
#[derive(Debug, Clone)]
@@ -91,7 +92,7 @@ impl FunctionCompiler {
body: &FunctionBody,
variable_environment: Rc<CompileTimeEnvironment>,
lexical_environment: Rc<CompileTimeEnvironment>,
context: &mut Context,
interner: &mut Interner,
) -> Gc<CodeBlock> {
self.strict = self.strict || body.strict();

@@ -103,7 +104,7 @@ impl FunctionCompiler {
false,
variable_environment,
lexical_environment,
context,
interner,
);
compiler.length = length;
compiler
22 changes: 10 additions & 12 deletions core/engine/src/bytecompiler/mod.rs
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ use crate::{
BindingOpcode, CodeBlock, CodeBlockFlags, Constant, GeneratorResumeKind, Handler,
InlineCache, Opcode, VaryingOperandKind,
},
Context, JsBigInt, JsString,
JsBigInt, JsString,
};
use boa_ast::{
declaration::{Binding, LexicalDeclaration, VarDeclaration},
@@ -44,7 +44,7 @@ use rustc_hash::FxHashMap;
use thin_vec::ThinVec;

pub(crate) use declarations::{
eval_declaration_instantiation_context, global_declaration_instantiation_annex_b,
eval_declaration_instantiation_context, global_declaration_instantiation_context,
};
pub(crate) use function::FunctionCompiler;
pub(crate) use jump_control::JumpControlInfo;
@@ -280,7 +280,7 @@ pub struct ByteCompiler<'ctx> {
/// The current lexical environment.
pub(crate) lexical_environment: Rc<CompileTimeEnvironment>,

current_open_environments_count: u32,
pub(crate) current_open_environments_count: u32,
current_stack_value_count: u32,
pub(crate) code_block_flags: CodeBlockFlags,
handlers: ThinVec<Handler>,
@@ -296,8 +296,7 @@ pub struct ByteCompiler<'ctx> {
pub(crate) async_handler: Option<u32>,
json_parse: bool,

// TODO: remove when we separate scripts from the context
pub(crate) context: &'ctx mut Context,
pub(crate) interner: &'ctx mut Interner,

#[cfg(feature = "annex-b")]
pub(crate) annex_b_function_names: Vec<Identifier>,
@@ -316,8 +315,7 @@ impl<'ctx> ByteCompiler<'ctx> {
json_parse: bool,
variable_environment: Rc<CompileTimeEnvironment>,
lexical_environment: Rc<CompileTimeEnvironment>,
// TODO: remove when we separate scripts from the context
context: &'ctx mut Context,
interner: &'ctx mut Interner,
) -> ByteCompiler<'ctx> {
let mut code_block_flags = CodeBlockFlags::empty();
code_block_flags.set(CodeBlockFlags::STRICT, strict);
@@ -346,7 +344,7 @@ impl<'ctx> ByteCompiler<'ctx> {
json_parse,
variable_environment,
lexical_environment,
context,
interner,

#[cfg(feature = "annex-b")]
annex_b_function_names: Vec::new(),
@@ -370,7 +368,7 @@ impl<'ctx> ByteCompiler<'ctx> {
}

pub(crate) fn interner(&self) -> &Interner {
self.context.interner()
self.interner
}

fn get_or_insert_literal(&mut self, literal: Literal) -> u32 {
@@ -1320,7 +1318,7 @@ impl<'ctx> ByteCompiler<'ctx> {
body,
self.variable_environment.clone(),
self.lexical_environment.clone(),
self.context,
self.interner,
);

self.push_function_to_constants(code)
@@ -1395,7 +1393,7 @@ impl<'ctx> ByteCompiler<'ctx> {
body,
self.variable_environment.clone(),
self.lexical_environment.clone(),
self.context,
self.interner,
);

let index = self.push_function_to_constants(code);
@@ -1442,7 +1440,7 @@ impl<'ctx> ByteCompiler<'ctx> {
body,
self.variable_environment.clone(),
self.lexical_environment.clone(),
self.context,
self.interner,
);

let index = self.push_function_to_constants(code);
2 changes: 1 addition & 1 deletion core/engine/src/module/source.rs
Original file line number Diff line number Diff line change
@@ -1432,7 +1432,7 @@ impl SourceTextModule {
false,
env.clone(),
env.clone(),
context,
context.interner_mut(),
);

compiler.code_block_flags |= CodeBlockFlags::IS_ASYNC;
2 changes: 1 addition & 1 deletion core/engine/src/module/synthetic.rs
Original file line number Diff line number Diff line change
@@ -286,7 +286,7 @@ impl SyntheticModule {
false,
module_compile_env.clone(),
module_compile_env.clone(),
context,
context.interner_mut(),
);

// 4. For each String exportName in module.[[ExportNames]], do
8 changes: 3 additions & 5 deletions core/engine/src/script.rs
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ use boa_parser::{source::ReadChar, Parser, Source};
use boa_profiler::Profiler;

use crate::{
bytecompiler::{global_declaration_instantiation_annex_b, ByteCompiler},
bytecompiler::{global_declaration_instantiation_context, ByteCompiler},
js_string,
realm::Realm,
vm::{ActiveRunnable, CallFrame, CallFrameFlags, CodeBlock},
@@ -119,11 +119,9 @@ impl Script {

let _timer = Profiler::global().start_event("Script compilation", "Main");

#[cfg(feature = "annex-b")]
let mut annex_b_function_names = Vec::new();

#[cfg(feature = "annex-b")]
global_declaration_instantiation_annex_b(
global_declaration_instantiation_context(
&mut annex_b_function_names,
&self.inner.source,
&self.inner.realm.environment().compile_env(),
@@ -136,7 +134,7 @@ impl Script {
false,
self.inner.realm.environment().compile_env(),
self.inner.realm.environment().compile_env(),
context,
context.interner_mut(),
);

#[cfg(feature = "annex-b")]

0 comments on commit a46a04f

Please sign in to comment.