Skip to content

Commit

Permalink
Allow optimizing with destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Sep 27, 2023
1 parent 055ce0c commit 61d45f8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
33 changes: 21 additions & 12 deletions boa_engine/src/bytecompiler/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
create_function_object_fast, create_generator_function_object, BindingOpcode,
CodeBlockFlags, Opcode,
},
JsNativeError, JsResult,
JsNativeError, JsResult, JsString,
};
use boa_ast::{
declaration::{Binding, LexicalDeclaration, VariableList},
Expand Down Expand Up @@ -848,6 +848,16 @@ impl ByteCompiler<'_, '_> {
strict: bool,
generator: bool,
) -> (Option<Label>, bool) {
println!(
"Name: {}",
std::convert::Into::<JsString>::into(
self.context
.interner()
.resolve_expect(self.function_name)
.utf16()
)
.to_std_string_escaped()
);
let mut env_label = None;
let mut additional_env = false;

Expand Down Expand Up @@ -957,7 +967,13 @@ impl ByteCompiler<'_, '_> {
// NOTE(HalidOdat): Has been moved up, so "arguments" gets registed as
// the first binding in the environment with index 0.
if arguments_object_needed {
let function_environment_index = self.function_environment_index.take();
if !strict {
self.can_optimize_local_variables = false;
}

let can_optimize_local_variables = self.can_optimize_local_variables;
self.can_optimize_local_variables = false;

// Note: This happens at runtime.
// a. If strict is true or simpleParameterList is false, then
// i. Let ao be CreateUnmappedArgumentsObject(argumentsList).
Expand All @@ -980,16 +996,9 @@ impl ByteCompiler<'_, '_> {
self.create_mutable_binding(arguments, false);
}

if self.can_optimize_local_variables {
let binding = self.get_binding_value(arguments);
self.get_or_insert_binding(binding);
}

if !strict {
self.can_optimize_local_variables = false;
}

self.function_environment_index = function_environment_index;
let binding = self.get_binding_value(arguments);
self.get_or_insert_binding(binding);
self.can_optimize_local_variables = can_optimize_local_variables;

self.code_block_flags |= CodeBlockFlags::NEEDS_ARGUMENTS_OBJECT;
}
Expand Down
9 changes: 8 additions & 1 deletion boa_engine/src/bytecompiler/statement/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl ByteCompiler<'_, '_> {
) {
let mut let_binding_indices = None;
let mut env_labels = None;
let mut env_labels_optimized_out = false;
let mut iteration_env_labels = None;

if let Some(init) = for_loop.init() {
Expand All @@ -33,7 +34,9 @@ impl ByteCompiler<'_, '_> {
}
ForLoopInitializer::Lexical(decl) => {
self.push_compile_environment(false);
if !self.can_optimize_local_variables {
if self.can_optimize_local_variables {
env_labels_optimized_out = true;
} else {
env_labels =
Some(self.emit_opcode_with_operand(Opcode::PushDeclarativeEnvironment));
}
Expand Down Expand Up @@ -128,6 +131,10 @@ impl ByteCompiler<'_, '_> {
self.emit_opcode(Opcode::PopEnvironment);
}

if env_labels_optimized_out {
self.pop_compile_environment();
}

if let Some(env_labels) = env_labels {
let env_index = self.pop_compile_environment();
self.patch_jump_with_target(env_labels, env_index);
Expand Down

0 comments on commit 61d45f8

Please sign in to comment.