Skip to content

Commit

Permalink
Decouple Context from ByteCompiler (#3829)
Browse files Browse the repository at this point in the history
* Decouple GlobalDeclarationInstantiation from Context

* Decouple EvalDeclarationInstantiation from Context

* Remove `Context` from `ByteCompiler`

* Fix typo

* Apply review
  • Loading branch information
HalidOdat committed Apr 28, 2024
1 parent 6156768 commit 6ee208f
Show file tree
Hide file tree
Showing 16 changed files with 864 additions and 375 deletions.
40 changes: 33 additions & 7 deletions core/engine/src/builtins/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::ByteCompiler,
bytecompiler::{eval_declaration_instantiation_context, ByteCompiler},
context::intrinsics::Intrinsics,
environments::Environment,
environments::{CompileTimeEnvironment, Environment},
error::JsNativeError,
js_string,
object::JsObject,
Expand Down Expand Up @@ -229,24 +231,48 @@ 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();
}

compiler.eval_declaration_instantiation(&body, strict, &var_env, &lex_env)?;
#[cfg(feature = "annex-b")]
{
compiler.annex_b_function_names = annex_b_function_names;
}

compiler.eval_declaration_instantiation(&body, strict, &var_env, &lex_env);
compiler.compile_statement_list(body.statements(), true, false);

let code_block = Gc::new(compiler.finish());
Expand Down
2 changes: 1 addition & 1 deletion core/engine/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion core/engine/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
10 changes: 5 additions & 5 deletions core/engine/src/bytecompiler/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -287,7 +287,7 @@ impl ByteCompiler<'_> {
self.json_parse,
self.variable_environment.clone(),
self.lexical_environment.clone(),
self.context,
self.interner,
);

// Function environment
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down
Loading

0 comments on commit 6ee208f

Please sign in to comment.