From a51581bf947c5841324c6ad1568d5d000cf23069 Mon Sep 17 00:00:00 2001 From: Haled Odat <8566042+HalidOdat@users.noreply.github.com> Date: Wed, 4 Oct 2023 01:17:35 +0200 Subject: [PATCH] Make environments opcodes use varying operands (#3340) --- boa_engine/src/builtins/eval/mod.rs | 9 +-- boa_engine/src/bytecompiler/class.rs | 37 +++++----- boa_engine/src/bytecompiler/declarations.rs | 13 ++-- boa_engine/src/bytecompiler/env.rs | 17 +++-- boa_engine/src/bytecompiler/function.rs | 9 +-- .../src/bytecompiler/statement/block.rs | 8 +- boa_engine/src/bytecompiler/statement/loop.rs | 74 ++++++++----------- .../src/bytecompiler/statement/switch.rs | 7 +- boa_engine/src/bytecompiler/statement/try.rs | 7 +- boa_engine/src/bytecompiler/statement/with.rs | 3 +- boa_engine/src/vm/code_block.rs | 14 ++-- boa_engine/src/vm/opcode/mod.rs | 4 +- boa_engine/src/vm/opcode/push/environment.rs | 32 ++++++-- 13 files changed, 117 insertions(+), 117 deletions(-) diff --git a/boa_engine/src/builtins/eval/mod.rs b/boa_engine/src/builtins/eval/mod.rs index 1eca3e654a0..6023687351e 100644 --- a/boa_engine/src/builtins/eval/mod.rs +++ b/boa_engine/src/builtins/eval/mod.rs @@ -232,16 +232,13 @@ impl Eval { context, ); - compiler.push_compile_environment(strict); - - let push_env = compiler.emit_opcode_with_operand(Opcode::PushDeclarativeEnvironment); + let env_index = compiler.push_compile_environment(strict); + compiler.emit_with_varying_operand(Opcode::PushDeclarativeEnvironment, env_index); compiler.eval_declaration_instantiation(&body, strict)?; compiler.compile_statement_list(body.statements(), true, false); - let env_index = compiler.pop_compile_environment(); - compiler.patch_jump_with_target(push_env, env_index); - + compiler.pop_compile_environment(); compiler.emit_opcode(Opcode::PopEnvironment); let code_block = Gc::new(compiler.finish()); diff --git a/boa_engine/src/bytecompiler/class.rs b/boa_engine/src/bytecompiler/class.rs index 88f8834b090..1bc57de76ca 100644 --- a/boa_engine/src/bytecompiler/class.rs +++ b/boa_engine/src/bytecompiler/class.rs @@ -26,13 +26,14 @@ impl ByteCompiler<'_, '_> { pub(crate) fn compile_class(&mut self, class: &Class, expression: bool) { let class_name = class.name().map_or(Sym::EMPTY_STRING, Identifier::sym); - let class_env: Option = match class.name() { + let class_env = match class.name() { Some(name) if class.has_binding_identifier() => { - self.push_compile_environment(false); + let env_index = self.push_compile_environment(false); self.create_immutable_binding(name, true); - Some(self.emit_opcode_with_operand(Opcode::PushDeclarativeEnvironment)) + self.emit_with_varying_operand(Opcode::PushDeclarativeEnvironment, env_index); + true } - _ => None, + _ => false, }; let mut compiler = ByteCompiler::new( @@ -43,7 +44,8 @@ impl ByteCompiler<'_, '_> { self.context, ); - compiler.push_compile_environment(true); + // Function environment + let _ = compiler.push_compile_environment(true); if let Some(expr) = class.constructor() { compiler.length = expr.parameters().length(); @@ -59,15 +61,11 @@ impl ByteCompiler<'_, '_> { compiler.compile_statement_list(expr.body().statements(), false, false); - let env_index = compiler.pop_compile_environment(); - - if let Some(env_label) = env_label { - compiler.patch_jump_with_target(env_label, env_index); + if env_label { compiler.pop_compile_environment(); } else { compiler.code_block_flags |= CodeBlockFlags::IS_CLASS_CONSTRUCTOR; } - compiler.emit_opcode(Opcode::PushUndefined); } else { if class.super_ref().is_some() { @@ -76,10 +74,10 @@ impl ByteCompiler<'_, '_> { compiler.emit_opcode(Opcode::RestParameterPop); compiler.emit_opcode(Opcode::PushUndefined); } - compiler.pop_compile_environment(); compiler.code_block_flags |= CodeBlockFlags::IS_CLASS_CONSTRUCTOR; } compiler.emit_opcode(Opcode::SetReturnValue); + compiler.pop_compile_environment(); let code = Gc::new(compiler.finish()); let index = self.functions.len() as u32; @@ -119,7 +117,7 @@ impl ByteCompiler<'_, '_> { let mut static_elements = Vec::new(); let mut static_field_name_count = 0; - if class_env.is_some() { + if class_env { self.emit_opcode(Opcode::Dup); self.emit_binding(BindingOpcode::InitConst, class_name.into()); } @@ -282,7 +280,9 @@ impl ByteCompiler<'_, '_> { self.current_environment.clone(), self.context, ); - field_compiler.push_compile_environment(true); + + // Function environment + let _ = field_compiler.push_compile_environment(true); if let Some(node) = field { field_compiler.compile_expr(node, true); } else { @@ -314,7 +314,7 @@ impl ByteCompiler<'_, '_> { self.current_environment.clone(), self.context, ); - field_compiler.push_compile_environment(true); + let _ = field_compiler.push_compile_environment(true); if let Some(node) = field { field_compiler.compile_expr(node, true); } else { @@ -358,7 +358,7 @@ impl ByteCompiler<'_, '_> { self.current_environment.clone(), self.context, ); - field_compiler.push_compile_environment(true); + let _ = field_compiler.push_compile_environment(true); if let Some(node) = field { field_compiler.compile_expr(node, true); } else { @@ -393,7 +393,7 @@ impl ByteCompiler<'_, '_> { self.current_environment.clone(), self.context, ); - compiler.push_compile_environment(true); + let _ = compiler.push_compile_environment(true); compiler.function_declaration_instantiation( body, @@ -589,9 +589,8 @@ impl ByteCompiler<'_, '_> { self.emit_opcode(Opcode::Swap); self.emit_opcode(Opcode::Pop); - if let Some(class_env) = class_env { - let env_index = self.pop_compile_environment(); - self.patch_jump_with_target(class_env, env_index); + if class_env { + self.pop_compile_environment(); self.emit_opcode(Opcode::PopEnvironment); } diff --git a/boa_engine/src/bytecompiler/declarations.rs b/boa_engine/src/bytecompiler/declarations.rs index 5ea9269d1b6..2ebfdd7a405 100644 --- a/boa_engine/src/bytecompiler/declarations.rs +++ b/boa_engine/src/bytecompiler/declarations.rs @@ -1,5 +1,5 @@ use crate::{ - bytecompiler::{ByteCompiler, FunctionCompiler, FunctionSpec, Label, NodeKind}, + bytecompiler::{ByteCompiler, FunctionCompiler, FunctionSpec, NodeKind}, environments::BindingLocatorError, vm::{ create_function_object_fast, create_generator_function_object, BindingOpcode, @@ -809,8 +809,8 @@ impl ByteCompiler<'_, '_> { arrow: bool, strict: bool, generator: bool, - ) -> (Option