Browse Source

Shrink environment binding locators (#2950)

pull/2953/head
Haled Odat 1 year ago committed by GitHub
parent
commit
3f5bad7888
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      boa_engine/src/builtins/eval/mod.rs
  2. 20
      boa_engine/src/builtins/function/arguments.rs
  3. 8
      boa_engine/src/bytecompiler/class.rs
  4. 6
      boa_engine/src/bytecompiler/env.rs
  5. 4
      boa_engine/src/bytecompiler/function.rs
  6. 4
      boa_engine/src/bytecompiler/mod.rs
  7. 4
      boa_engine/src/bytecompiler/statement/block.rs
  8. 24
      boa_engine/src/bytecompiler/statement/loop.rs
  9. 4
      boa_engine/src/bytecompiler/statement/switch.rs
  10. 4
      boa_engine/src/bytecompiler/statement/try.rs
  11. 16
      boa_engine/src/environments/compile.rs
  12. 6
      boa_engine/src/environments/runtime/declarative/function.rs
  13. 4
      boa_engine/src/environments/runtime/declarative/global.rs
  14. 6
      boa_engine/src/environments/runtime/declarative/lexical.rs
  15. 20
      boa_engine/src/environments/runtime/declarative/mod.rs
  16. 18
      boa_engine/src/environments/runtime/declarative/module.rs
  17. 52
      boa_engine/src/environments/runtime/mod.rs
  18. 12
      boa_engine/src/object/internal_methods/arguments.rs
  19. 4
      boa_engine/src/realm.rs
  20. 4
      boa_engine/src/vm/code_block.rs
  21. 4
      boa_engine/src/vm/opcode/push/environment.rs

4
boa_engine/src/builtins/eval/mod.rs

@ -232,8 +232,8 @@ impl Eval {
compiler.compile_statement_list(&body, true, false); compiler.compile_statement_list(&body, true, false);
let env_info = compiler.pop_compile_environment(); let env_info = compiler.pop_compile_environment();
compiler.patch_jump_with_target(push_env.0, env_info.num_bindings as u32); compiler.patch_jump_with_target(push_env.0, env_info.num_bindings);
compiler.patch_jump_with_target(push_env.1, env_info.index as u32); compiler.patch_jump_with_target(push_env.1, env_info.index);
compiler.emit_opcode(Opcode::PopEnvironment); compiler.emit_opcode(Opcode::PopEnvironment);
let code_block = Gc::new(compiler.finish()); let code_block = Gc::new(compiler.finish());

20
boa_engine/src/builtins/function/arguments.rs

@ -12,14 +12,14 @@ use rustc_hash::FxHashMap;
/// This struct stores all the data to access mapped function parameters in their environment. /// This struct stores all the data to access mapped function parameters in their environment.
#[derive(Debug, Clone, Trace, Finalize)] #[derive(Debug, Clone, Trace, Finalize)]
pub struct ParameterMap { pub struct ParameterMap {
binding_indices: Vec<Option<usize>>, binding_indices: Vec<Option<u32>>,
environment: Gc<DeclarativeEnvironment>, environment: Gc<DeclarativeEnvironment>,
} }
impl ParameterMap { impl ParameterMap {
/// Deletes the binding with the given index from the parameter map. /// Deletes the binding with the given index from the parameter map.
pub(crate) fn delete(&mut self, index: usize) { pub(crate) fn delete(&mut self, index: u32) {
if let Some(binding) = self.binding_indices.get_mut(index) { if let Some(binding) = self.binding_indices.get_mut(index as usize) {
*binding = None; *binding = None;
} }
} }
@ -32,8 +32,12 @@ impl ParameterMap {
/// - [ECMAScript reference][spec] /// - [ECMAScript reference][spec]
/// ///
/// [spec]: https://tc39.es/ecma262/#sec-makearggetter /// [spec]: https://tc39.es/ecma262/#sec-makearggetter
pub(crate) fn get(&self, index: usize) -> Option<JsValue> { pub(crate) fn get(&self, index: u32) -> Option<JsValue> {
let binding_index = self.binding_indices.get(index).copied().flatten()?; let binding_index = self
.binding_indices
.get(index as usize)
.copied()
.flatten()?;
self.environment.get(binding_index) self.environment.get(binding_index)
} }
@ -45,8 +49,8 @@ impl ParameterMap {
/// - [ECMAScript reference][spec] /// - [ECMAScript reference][spec]
/// ///
/// [spec]: https://tc39.es/ecma262/#sec-makeargsetter /// [spec]: https://tc39.es/ecma262/#sec-makeargsetter
pub(crate) fn set(&self, index: usize, value: &JsValue) { pub(crate) fn set(&self, index: u32, value: &JsValue) {
if let Some(binding_index) = self.binding_indices.get(index).copied().flatten() { if let Some(binding_index) = self.binding_indices.get(index as usize).copied().flatten() {
self.environment.set(binding_index, value.clone()); self.environment.set(binding_index, value.clone());
} }
} }
@ -174,7 +178,7 @@ impl Arguments {
if property_index >= len { if property_index >= len {
break; break;
} }
let binding_index = bindings.len(); let binding_index = bindings.len() as u32;
let entry = bindings let entry = bindings
.entry(name) .entry(name)
.or_insert((binding_index, property_index)); .or_insert((binding_index, property_index));

8
boa_engine/src/bytecompiler/class.rs

@ -52,8 +52,8 @@ impl ByteCompiler<'_, '_> {
let env_info = compiler.pop_compile_environment(); let env_info = compiler.pop_compile_environment();
if let Some(env_labels) = env_labels { if let Some(env_labels) = env_labels {
compiler.patch_jump_with_target(env_labels.0, env_info.num_bindings as u32); compiler.patch_jump_with_target(env_labels.0, env_info.num_bindings);
compiler.patch_jump_with_target(env_labels.1, env_info.index as u32); compiler.patch_jump_with_target(env_labels.1, env_info.index);
compiler.pop_compile_environment(); compiler.pop_compile_environment();
} else { } else {
compiler.num_bindings = env_info.num_bindings; compiler.num_bindings = env_info.num_bindings;
@ -548,8 +548,8 @@ impl ByteCompiler<'_, '_> {
if let Some(class_env) = class_env { if let Some(class_env) = class_env {
let env_info = self.pop_compile_environment(); let env_info = self.pop_compile_environment();
self.patch_jump_with_target(class_env.0, env_info.num_bindings as u32); self.patch_jump_with_target(class_env.0, env_info.num_bindings);
self.patch_jump_with_target(class_env.1, env_info.index as u32); self.patch_jump_with_target(class_env.1, env_info.index);
self.emit_opcode(Opcode::PopEnvironment); self.emit_opcode(Opcode::PopEnvironment);
} }

6
boa_engine/src/bytecompiler/env.rs

@ -7,9 +7,9 @@ use boa_gc::{Gc, GcRefCell};
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub(crate) struct PopEnvironmentInfo { pub(crate) struct PopEnvironmentInfo {
/// Number of bindings declared. /// Number of bindings declared.
pub(crate) num_bindings: usize, pub(crate) num_bindings: u32,
/// Index in the compile time envs array. /// Index in the compile time envs array.
pub(crate) index: usize, pub(crate) index: u32,
} }
impl ByteCompiler<'_, '_> { impl ByteCompiler<'_, '_> {
@ -24,7 +24,7 @@ impl ByteCompiler<'_, '_> {
/// Pops the top compile time environment and returns its index and number of bindings. /// Pops the top compile time environment and returns its index and number of bindings.
#[track_caller] #[track_caller]
pub(crate) fn pop_compile_environment(&mut self) -> PopEnvironmentInfo { pub(crate) fn pop_compile_environment(&mut self) -> PopEnvironmentInfo {
let index = self.compile_environments.len(); let index = self.compile_environments.len() as u32;
self.compile_environments self.compile_environments
.push(self.current_environment.clone()); .push(self.current_environment.clone());

4
boa_engine/src/bytecompiler/function.rs

@ -129,8 +129,8 @@ impl FunctionCompiler {
if let Some(env_labels) = env_labels { if let Some(env_labels) = env_labels {
let env_info = compiler.pop_compile_environment(); let env_info = compiler.pop_compile_environment();
compiler.patch_jump_with_target(env_labels.0, env_info.num_bindings as u32); compiler.patch_jump_with_target(env_labels.0, env_info.num_bindings);
compiler.patch_jump_with_target(env_labels.1, env_info.index as u32); compiler.patch_jump_with_target(env_labels.1, env_info.index);
} }
if additional_env { if additional_env {

4
boa_engine/src/bytecompiler/mod.rs

@ -251,7 +251,7 @@ pub struct ByteCompiler<'ctx, 'host> {
pub(crate) bindings: Vec<BindingLocator>, pub(crate) bindings: Vec<BindingLocator>,
/// Number of binding for the function environment. /// Number of binding for the function environment.
pub(crate) num_bindings: usize, pub(crate) num_bindings: u32,
/// Functions inside this function /// Functions inside this function
pub(crate) functions: Vec<Gc<CodeBlock>>, pub(crate) functions: Vec<Gc<CodeBlock>>,
@ -278,7 +278,7 @@ pub struct ByteCompiler<'ctx, 'host> {
pub(crate) current_environment: Gc<GcRefCell<CompileTimeEnvironment>>, pub(crate) current_environment: Gc<GcRefCell<CompileTimeEnvironment>>,
/// The number of bindings in the parameters environment. /// The number of bindings in the parameters environment.
pub(crate) parameters_env_bindings: Option<usize>, pub(crate) parameters_env_bindings: Option<u32>,
literals_map: FxHashMap<Literal, u32>, literals_map: FxHashMap<Literal, u32>,
names_map: FxHashMap<Identifier, u32>, names_map: FxHashMap<Identifier, u32>,

4
boa_engine/src/bytecompiler/statement/block.rs

@ -11,8 +11,8 @@ impl ByteCompiler<'_, '_> {
self.compile_statement_list(block.statement_list(), use_expr, true); self.compile_statement_list(block.statement_list(), use_expr, true);
let env_info = self.pop_compile_environment(); let env_info = self.pop_compile_environment();
self.patch_jump_with_target(push_env.0, env_info.num_bindings as u32); self.patch_jump_with_target(push_env.0, env_info.num_bindings);
self.patch_jump_with_target(push_env.1, env_info.index as u32); self.patch_jump_with_target(push_env.1, env_info.index);
self.emit_opcode(Opcode::PopEnvironment); self.emit_opcode(Opcode::PopEnvironment);
} }

24
boa_engine/src/bytecompiler/statement/loop.rs

@ -115,11 +115,11 @@ impl ByteCompiler<'_, '_> {
if let Some(env_labels) = env_labels { if let Some(env_labels) = env_labels {
let env_info = self.pop_compile_environment(); let env_info = self.pop_compile_environment();
self.patch_jump_with_target(env_labels.0, env_info.num_bindings as u32); self.patch_jump_with_target(env_labels.0, env_info.num_bindings);
self.patch_jump_with_target(env_labels.1, env_info.index as u32); self.patch_jump_with_target(env_labels.1, env_info.index);
if let Some(iteration_env_labels) = iteration_env_labels { if let Some(iteration_env_labels) = iteration_env_labels {
self.patch_jump_with_target(iteration_env_labels.0, env_info.num_bindings as u32); self.patch_jump_with_target(iteration_env_labels.0, env_info.num_bindings);
self.patch_jump_with_target(iteration_env_labels.1, env_info.index as u32); self.patch_jump_with_target(iteration_env_labels.1, env_info.index);
} }
self.emit_opcode(Opcode::PopEnvironment); self.emit_opcode(Opcode::PopEnvironment);
} }
@ -157,8 +157,8 @@ impl ByteCompiler<'_, '_> {
self.compile_expr(for_in_loop.target(), true); self.compile_expr(for_in_loop.target(), true);
let env_info = self.pop_compile_environment(); let env_info = self.pop_compile_environment();
self.patch_jump_with_target(push_env.0, env_info.num_bindings as u32); self.patch_jump_with_target(push_env.0, env_info.num_bindings);
self.patch_jump_with_target(push_env.1, env_info.index as u32); self.patch_jump_with_target(push_env.1, env_info.index);
self.emit_opcode(Opcode::PopEnvironment); self.emit_opcode(Opcode::PopEnvironment);
} }
@ -239,8 +239,8 @@ impl ByteCompiler<'_, '_> {
if let Some(iteration_environment) = iteration_environment { if let Some(iteration_environment) = iteration_environment {
let env_info = self.pop_compile_environment(); let env_info = self.pop_compile_environment();
self.patch_jump_with_target(iteration_environment.0, env_info.num_bindings as u32); self.patch_jump_with_target(iteration_environment.0, env_info.num_bindings);
self.patch_jump_with_target(iteration_environment.1, env_info.index as u32); self.patch_jump_with_target(iteration_environment.1, env_info.index);
self.emit_opcode(Opcode::PopEnvironment); self.emit_opcode(Opcode::PopEnvironment);
} }
@ -289,8 +289,8 @@ impl ByteCompiler<'_, '_> {
self.compile_expr(for_of_loop.iterable(), true); self.compile_expr(for_of_loop.iterable(), true);
let env_info = self.pop_compile_environment(); let env_info = self.pop_compile_environment();
self.patch_jump_with_target(push_env.0, env_info.num_bindings as u32); self.patch_jump_with_target(push_env.0, env_info.num_bindings);
self.patch_jump_with_target(push_env.1, env_info.index as u32); self.patch_jump_with_target(push_env.1, env_info.index);
self.emit_opcode(Opcode::PopEnvironment); self.emit_opcode(Opcode::PopEnvironment);
} }
@ -384,8 +384,8 @@ impl ByteCompiler<'_, '_> {
if let Some(iteration_environment) = iteration_environment { if let Some(iteration_environment) = iteration_environment {
let env_info = self.pop_compile_environment(); let env_info = self.pop_compile_environment();
self.patch_jump_with_target(iteration_environment.0, env_info.num_bindings as u32); self.patch_jump_with_target(iteration_environment.0, env_info.num_bindings);
self.patch_jump_with_target(iteration_environment.1, env_info.index as u32); self.patch_jump_with_target(iteration_environment.1, env_info.index);
self.emit_opcode(Opcode::PopEnvironment); self.emit_opcode(Opcode::PopEnvironment);
} }

4
boa_engine/src/bytecompiler/statement/switch.rs

@ -61,8 +61,8 @@ impl ByteCompiler<'_, '_> {
} }
let env_info = self.pop_compile_environment(); let env_info = self.pop_compile_environment();
self.patch_jump_with_target(push_env.0, env_info.num_bindings as u32); self.patch_jump_with_target(push_env.0, env_info.num_bindings);
self.patch_jump_with_target(push_env.1, env_info.index as u32); self.patch_jump_with_target(push_env.1, env_info.index);
self.emit_opcode(Opcode::PopEnvironment); self.emit_opcode(Opcode::PopEnvironment);
} }
} }

4
boa_engine/src/bytecompiler/statement/try.rs

@ -88,8 +88,8 @@ impl ByteCompiler<'_, '_> {
} }
let env_info = self.pop_compile_environment(); let env_info = self.pop_compile_environment();
self.patch_jump_with_target(push_env.0, env_info.num_bindings as u32); self.patch_jump_with_target(push_env.0, env_info.num_bindings);
self.patch_jump_with_target(push_env.1, env_info.index as u32); self.patch_jump_with_target(push_env.1, env_info.index);
self.emit_opcode(Opcode::PopEnvironment); self.emit_opcode(Opcode::PopEnvironment);
if finally { if finally {

16
boa_engine/src/environments/compile.rs

@ -9,7 +9,7 @@ use rustc_hash::FxHashMap;
/// It contains the binding index and a flag to indicate if this is a mutable binding or not. /// It contains the binding index and a flag to indicate if this is a mutable binding or not.
#[derive(Debug)] #[derive(Debug)]
struct CompileTimeBinding { struct CompileTimeBinding {
index: usize, index: u32,
mutable: bool, mutable: bool,
lex: bool, lex: bool,
strict: bool, strict: bool,
@ -21,7 +21,7 @@ struct CompileTimeBinding {
#[derive(Debug, Finalize, Trace)] #[derive(Debug, Finalize, Trace)]
pub(crate) struct CompileTimeEnvironment { pub(crate) struct CompileTimeEnvironment {
outer: Option<Gc<GcRefCell<Self>>>, outer: Option<Gc<GcRefCell<Self>>>,
environment_index: usize, environment_index: u32,
#[unsafe_ignore_trace] #[unsafe_ignore_trace]
bindings: FxHashMap<Identifier, CompileTimeBinding>, bindings: FxHashMap<Identifier, CompileTimeBinding>,
function_scope: bool, function_scope: bool,
@ -74,8 +74,8 @@ impl CompileTimeEnvironment {
} }
/// Returns the number of bindings in this environment. /// Returns the number of bindings in this environment.
pub(crate) fn num_bindings(&self) -> usize { pub(crate) fn num_bindings(&self) -> u32 {
self.bindings.len() self.bindings.len() as u32
} }
/// Check if the environment is a function environment. /// Check if the environment is a function environment.
@ -157,7 +157,7 @@ impl CompileTimeEnvironment {
if let Some(outer) = &self.outer { if let Some(outer) = &self.outer {
if !function_scope || self.function_scope { if !function_scope || self.function_scope {
if !self.bindings.contains_key(&name) { if !self.bindings.contains_key(&name) {
let binding_index = self.bindings.len(); let binding_index = self.bindings.len() as u32;
self.bindings.insert( self.bindings.insert(
name, name,
CompileTimeBinding { CompileTimeBinding {
@ -178,7 +178,7 @@ impl CompileTimeEnvironment {
false false
} else { } else {
if !self.bindings.contains_key(&name) { if !self.bindings.contains_key(&name) {
let binding_index = self.bindings.len(); let binding_index = self.bindings.len() as u32;
self.bindings.insert( self.bindings.insert(
name, name,
CompileTimeBinding { CompileTimeBinding {
@ -195,7 +195,7 @@ impl CompileTimeEnvironment {
/// Crate an immutable binding. /// Crate an immutable binding.
pub(crate) fn create_immutable_binding(&mut self, name: Identifier, strict: bool) { pub(crate) fn create_immutable_binding(&mut self, name: Identifier, strict: bool) {
let binding_index = self.bindings.len(); let binding_index = self.bindings.len() as u32;
self.bindings.insert( self.bindings.insert(
name, name,
CompileTimeBinding { CompileTimeBinding {
@ -288,7 +288,7 @@ impl CompileTimeEnvironment {
} }
/// Gets the environment index of this environment. /// Gets the environment index of this environment.
pub(crate) const fn environment_index(&self) -> usize { pub(crate) const fn environment_index(&self) -> u32 {
self.environment_index self.environment_index
} }
} }

6
boa_engine/src/environments/runtime/declarative/function.rs

@ -12,7 +12,7 @@ pub(crate) struct FunctionEnvironment {
impl FunctionEnvironment { impl FunctionEnvironment {
/// Creates a new `FunctionEnvironment`. /// Creates a new `FunctionEnvironment`.
pub(crate) fn new(bindings: usize, poisoned: bool, with: bool, slots: FunctionSlots) -> Self { pub(crate) fn new(bindings: u32, poisoned: bool, with: bool, slots: FunctionSlots) -> Self {
Self { Self {
inner: PoisonableEnvironment::new(bindings, poisoned, with), inner: PoisonableEnvironment::new(bindings, poisoned, with),
slots, slots,
@ -35,7 +35,7 @@ impl FunctionEnvironment {
/// ///
/// Panics if the binding value is out of range or not initialized. /// Panics if the binding value is out of range or not initialized.
#[track_caller] #[track_caller]
pub(crate) fn get(&self, index: usize) -> Option<JsValue> { pub(crate) fn get(&self, index: u32) -> Option<JsValue> {
self.inner.get(index) self.inner.get(index)
} }
@ -45,7 +45,7 @@ impl FunctionEnvironment {
/// ///
/// Panics if the binding value is out of range. /// Panics if the binding value is out of range.
#[track_caller] #[track_caller]
pub(crate) fn set(&self, index: usize, value: JsValue) { pub(crate) fn set(&self, index: u32, value: JsValue) {
self.inner.set(index, value); self.inner.set(index, value);
} }

4
boa_engine/src/environments/runtime/declarative/global.rs

@ -30,7 +30,7 @@ impl GlobalEnvironment {
/// ///
/// Panics if the binding value is out of range or not initialized. /// Panics if the binding value is out of range or not initialized.
#[track_caller] #[track_caller]
pub(crate) fn get(&self, index: usize) -> Option<JsValue> { pub(crate) fn get(&self, index: u32) -> Option<JsValue> {
self.inner.get(index) self.inner.get(index)
} }
@ -40,7 +40,7 @@ impl GlobalEnvironment {
/// ///
/// Panics if the binding value is out of range. /// Panics if the binding value is out of range.
#[track_caller] #[track_caller]
pub(crate) fn set(&self, index: usize, value: JsValue) { pub(crate) fn set(&self, index: u32, value: JsValue) {
self.inner.set(index, value); self.inner.set(index, value);
} }

6
boa_engine/src/environments/runtime/declarative/lexical.rs

@ -11,7 +11,7 @@ pub(crate) struct LexicalEnvironment {
impl LexicalEnvironment { impl LexicalEnvironment {
/// Creates a new `LexicalEnvironment`. /// Creates a new `LexicalEnvironment`.
pub(crate) fn new(bindings: usize, poisoned: bool, with: bool) -> Self { pub(crate) fn new(bindings: u32, poisoned: bool, with: bool) -> Self {
Self { Self {
inner: PoisonableEnvironment::new(bindings, poisoned, with), inner: PoisonableEnvironment::new(bindings, poisoned, with),
} }
@ -28,7 +28,7 @@ impl LexicalEnvironment {
/// ///
/// Panics if the binding value is out of range or not initialized. /// Panics if the binding value is out of range or not initialized.
#[track_caller] #[track_caller]
pub(crate) fn get(&self, index: usize) -> Option<JsValue> { pub(crate) fn get(&self, index: u32) -> Option<JsValue> {
self.inner.get(index) self.inner.get(index)
} }
@ -38,7 +38,7 @@ impl LexicalEnvironment {
/// ///
/// Panics if the binding value is out of range. /// Panics if the binding value is out of range.
#[track_caller] #[track_caller]
pub(crate) fn set(&self, index: usize, value: JsValue) { pub(crate) fn set(&self, index: u32, value: JsValue) {
self.inner.set(index, value); self.inner.set(index, value);
} }
} }

20
boa_engine/src/environments/runtime/declarative/mod.rs

@ -72,7 +72,7 @@ impl DeclarativeEnvironment {
/// ///
/// Panics if the binding value is out of range or not initialized. /// Panics if the binding value is out of range or not initialized.
#[track_caller] #[track_caller]
pub(crate) fn get(&self, index: usize) -> Option<JsValue> { pub(crate) fn get(&self, index: u32) -> Option<JsValue> {
self.kind.get(index) self.kind.get(index)
} }
@ -82,7 +82,7 @@ impl DeclarativeEnvironment {
/// ///
/// Panics if the binding value is out of range. /// Panics if the binding value is out of range.
#[track_caller] #[track_caller]
pub(crate) fn set(&self, index: usize, value: JsValue) { pub(crate) fn set(&self, index: u32, value: JsValue) {
self.kind.set(index, value); self.kind.set(index, value);
} }
@ -173,7 +173,7 @@ impl DeclarativeEnvironmentKind {
/// ///
/// Panics if the binding value is out of range or not initialized. /// Panics if the binding value is out of range or not initialized.
#[track_caller] #[track_caller]
pub(crate) fn get(&self, index: usize) -> Option<JsValue> { pub(crate) fn get(&self, index: u32) -> Option<JsValue> {
match self { match self {
Self::Lexical(inner) => inner.get(index), Self::Lexical(inner) => inner.get(index),
Self::Global(inner) => inner.get(index), Self::Global(inner) => inner.get(index),
@ -188,7 +188,7 @@ impl DeclarativeEnvironmentKind {
/// ///
/// Panics if the binding value is out of range. /// Panics if the binding value is out of range.
#[track_caller] #[track_caller]
pub(crate) fn set(&self, index: usize, value: JsValue) { pub(crate) fn set(&self, index: u32, value: JsValue) {
match self { match self {
Self::Lexical(inner) => inner.set(index, value), Self::Lexical(inner) => inner.set(index, value),
Self::Global(inner) => inner.set(index, value), Self::Global(inner) => inner.set(index, value),
@ -274,9 +274,9 @@ pub(crate) struct PoisonableEnvironment {
impl PoisonableEnvironment { impl PoisonableEnvironment {
/// Creates a new `PoisonableEnvironment`. /// Creates a new `PoisonableEnvironment`.
pub(crate) fn new(bindings_count: usize, poisoned: bool, with: bool) -> Self { pub(crate) fn new(bindings_count: u32, poisoned: bool, with: bool) -> Self {
Self { Self {
bindings: GcRefCell::new(vec![None; bindings_count]), bindings: GcRefCell::new(vec![None; bindings_count as usize]),
poisoned: Cell::new(poisoned), poisoned: Cell::new(poisoned),
with: Cell::new(with), with: Cell::new(with),
} }
@ -293,8 +293,8 @@ impl PoisonableEnvironment {
/// ///
/// Panics if the binding value is out of range. /// Panics if the binding value is out of range.
#[track_caller] #[track_caller]
fn get(&self, index: usize) -> Option<JsValue> { fn get(&self, index: u32) -> Option<JsValue> {
self.bindings.borrow()[index].clone() self.bindings.borrow()[index as usize].clone()
} }
/// Sets the binding value from the environment by index. /// Sets the binding value from the environment by index.
@ -303,8 +303,8 @@ impl PoisonableEnvironment {
/// ///
/// Panics if the binding value is out of range. /// Panics if the binding value is out of range.
#[track_caller] #[track_caller]
pub(crate) fn set(&self, index: usize, value: JsValue) { pub(crate) fn set(&self, index: u32, value: JsValue) {
self.bindings.borrow_mut()[index] = Some(value); self.bindings.borrow_mut()[index as usize] = Some(value);
} }
/// Returns `true` if this environment is poisoned. /// Returns `true` if this environment is poisoned.

18
boa_engine/src/environments/runtime/declarative/module.rs

@ -9,7 +9,7 @@ use crate::{module::Module, JsValue};
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum BindingAccessor { enum BindingAccessor {
Identifier(Identifier), Identifier(Identifier),
Index(usize), Index(u32),
} }
/// An indirect reference to a binding inside an environment. /// An indirect reference to a binding inside an environment.
@ -41,9 +41,9 @@ pub(crate) struct ModuleEnvironment {
impl ModuleEnvironment { impl ModuleEnvironment {
/// Creates a new `LexicalEnvironment`. /// Creates a new `LexicalEnvironment`.
pub(crate) fn new(bindings: usize) -> Self { pub(crate) fn new(bindings: u32) -> Self {
Self { Self {
bindings: GcRefCell::new(vec![BindingType::Direct(None); bindings]), bindings: GcRefCell::new(vec![BindingType::Direct(None); bindings as usize]),
} }
} }
@ -53,10 +53,10 @@ impl ModuleEnvironment {
/// ///
/// Panics if the binding value is out of range or not initialized. /// Panics if the binding value is out of range or not initialized.
#[track_caller] #[track_caller]
pub(crate) fn get(&self, index: usize) -> Option<JsValue> { pub(crate) fn get(&self, index: u32) -> Option<JsValue> {
let bindings = self.bindings.borrow(); let bindings = self.bindings.borrow();
match &bindings[index] { match &bindings[index as usize] {
BindingType::Direct(v) => v.clone(), BindingType::Direct(v) => v.clone(),
BindingType::Indirect(IndirectBinding { module, accessor }) => { BindingType::Indirect(IndirectBinding { module, accessor }) => {
let env = module.environment()?; let env = module.environment()?;
@ -87,10 +87,10 @@ impl ModuleEnvironment {
/// ///
/// Panics if the binding value is out of range. /// Panics if the binding value is out of range.
#[track_caller] #[track_caller]
pub(crate) fn set(&self, index: usize, value: JsValue) { pub(crate) fn set(&self, index: u32, value: JsValue) {
let mut bindings = self.bindings.borrow_mut(); let mut bindings = self.bindings.borrow_mut();
match &mut bindings[index] { match &mut bindings[index as usize] {
BindingType::Direct(v) => *v = Some(value), BindingType::Direct(v) => *v = Some(value),
BindingType::Indirect(_) => { BindingType::Indirect(_) => {
panic!("cannot modify indirect references to other environments") panic!("cannot modify indirect references to other environments")
@ -106,13 +106,13 @@ impl ModuleEnvironment {
#[track_caller] #[track_caller]
pub(crate) fn set_indirect( pub(crate) fn set_indirect(
&self, &self,
index: usize, index: u32,
target_module: Module, target_module: Module,
target_binding: Identifier, target_binding: Identifier,
) { ) {
let mut bindings = self.bindings.borrow_mut(); let mut bindings = self.bindings.borrow_mut();
bindings[index] = BindingType::Indirect(IndirectBinding { bindings[index as usize] = BindingType::Indirect(IndirectBinding {
module: target_module, module: target_module,
accessor: Cell::new(BindingAccessor::Identifier(target_binding)), accessor: Cell::new(BindingAccessor::Identifier(target_binding)),
}); });

52
boa_engine/src/environments/runtime/mod.rs

@ -89,12 +89,11 @@ impl EnvironmentStack {
.rev() .rev()
{ {
if let DeclarativeEnvironmentKind::Function(fun) = &env.kind() { if let DeclarativeEnvironmentKind::Function(fun) = &env.kind() {
let compile_bindings_number = env.compile_env().borrow().num_bindings(); let compile_bindings_number = env.compile_env().borrow().num_bindings() as usize;
let mut bindings_mut = fun.poisonable_environment().bindings().borrow_mut(); let mut bindings = fun.poisonable_environment().bindings().borrow_mut();
if compile_bindings_number > bindings_mut.len() { if compile_bindings_number > bindings.len() {
let diff = compile_bindings_number - bindings_mut.len(); bindings.resize(compile_bindings_number, None);
bindings_mut.extend(vec![None; diff]);
} }
break; break;
} }
@ -223,9 +222,9 @@ impl EnvironmentStack {
#[track_caller] #[track_caller]
pub(crate) fn push_lexical( pub(crate) fn push_lexical(
&mut self, &mut self,
num_bindings: usize, num_bindings: u32,
compile_environment: Gc<GcRefCell<CompileTimeEnvironment>>, compile_environment: Gc<GcRefCell<CompileTimeEnvironment>>,
) -> usize { ) -> u32 {
let (poisoned, with) = { let (poisoned, with) = {
let with = self let with = self
.stack .stack
@ -243,7 +242,7 @@ impl EnvironmentStack {
(environment.poisoned(), with || environment.with()) (environment.poisoned(), with || environment.with())
}; };
let index = self.stack.len(); let index = self.stack.len() as u32;
self.stack.push(Environment::Declarative(Gc::new( self.stack.push(Environment::Declarative(Gc::new(
DeclarativeEnvironment::new( DeclarativeEnvironment::new(
@ -267,7 +266,7 @@ impl EnvironmentStack {
#[track_caller] #[track_caller]
pub(crate) fn push_function( pub(crate) fn push_function(
&mut self, &mut self,
num_bindings: usize, num_bindings: u32,
compile_environment: Gc<GcRefCell<CompileTimeEnvironment>>, compile_environment: Gc<GcRefCell<CompileTimeEnvironment>>,
function_slots: FunctionSlots, function_slots: FunctionSlots,
) { ) {
@ -310,11 +309,11 @@ impl EnvironmentStack {
#[track_caller] #[track_caller]
pub(crate) fn push_function_inherit( pub(crate) fn push_function_inherit(
&mut self, &mut self,
num_bindings: usize, num_bindings: u32,
compile_environment: Gc<GcRefCell<CompileTimeEnvironment>>, compile_environment: Gc<GcRefCell<CompileTimeEnvironment>>,
) { ) {
debug_assert!( debug_assert!(
self.stack.len() == compile_environment.borrow().environment_index(), self.stack.len() as u32 == compile_environment.borrow().environment_index(),
"tried to push an invalid compile environment" "tried to push an invalid compile environment"
); );
@ -434,13 +433,13 @@ impl EnvironmentStack {
#[track_caller] #[track_caller]
pub(crate) fn put_lexical_value( pub(crate) fn put_lexical_value(
&mut self, &mut self,
environment_index: usize, environment_index: u32,
binding_index: usize, binding_index: u32,
value: JsValue, value: JsValue,
) { ) {
let env = self let env = self
.stack .stack
.get(environment_index) .get(environment_index as usize)
.expect("environment index must be in range") .expect("environment index must be in range")
.declarative_expect(); .declarative_expect();
env.set(binding_index, value); env.set(binding_index, value);
@ -454,13 +453,13 @@ impl EnvironmentStack {
#[track_caller] #[track_caller]
pub(crate) fn put_value_if_uninitialized( pub(crate) fn put_value_if_uninitialized(
&mut self, &mut self,
environment_index: usize, environment_index: u32,
binding_index: usize, binding_index: u32,
value: JsValue, value: JsValue,
) { ) {
let env = self let env = self
.stack .stack
.get(environment_index) .get(environment_index as usize)
.expect("environment index must be in range") .expect("environment index must be in range")
.declarative_expect(); .declarative_expect();
if env.get(binding_index).is_none() { if env.get(binding_index).is_none() {
@ -520,8 +519,8 @@ impl EnvironmentStack {
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Finalize)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Finalize)]
pub(crate) struct BindingLocator { pub(crate) struct BindingLocator {
name: Identifier, name: Identifier,
environment_index: usize, environment_index: u32,
binding_index: usize, binding_index: u32,
global: bool, global: bool,
mutate_immutable: bool, mutate_immutable: bool,
silent: bool, silent: bool,
@ -535,8 +534,8 @@ impl BindingLocator {
/// Creates a new declarative binding locator that has knows indices. /// Creates a new declarative binding locator that has knows indices.
pub(crate) const fn declarative( pub(crate) const fn declarative(
name: Identifier, name: Identifier,
environment_index: usize, environment_index: u32,
binding_index: usize, binding_index: u32,
) -> Self { ) -> Self {
Self { Self {
name, name,
@ -596,12 +595,12 @@ impl BindingLocator {
} }
/// Returns the environment index of the binding. /// Returns the environment index of the binding.
pub(crate) const fn environment_index(&self) -> usize { pub(crate) const fn environment_index(&self) -> u32 {
self.environment_index self.environment_index
} }
/// Returns the binding index of the binding. /// Returns the binding index of the binding.
pub(crate) const fn binding_index(&self) -> usize { pub(crate) const fn binding_index(&self) -> u32 {
self.binding_index self.binding_index
} }
@ -644,7 +643,8 @@ impl Context<'_> {
} }
} }
for env_index in (locator.environment_index..self.vm.environments.stack.len()).rev() { for env_index in (locator.environment_index..self.vm.environments.stack.len() as u32).rev()
{
match self.environment_expect(env_index) { match self.environment_expect(env_index) {
Environment::Declarative(env) => { Environment::Declarative(env) => {
if env.poisoned() { if env.poisoned() {
@ -814,11 +814,11 @@ impl Context<'_> {
} }
/// Return the environment at the given index. Panics if the index is out of range. /// Return the environment at the given index. Panics if the index is out of range.
pub(crate) fn environment_expect(&self, index: usize) -> &Environment { pub(crate) fn environment_expect(&self, index: u32) -> &Environment {
self.vm self.vm
.environments .environments
.stack .stack
.get(index) .get(index as usize)
.expect("environment index must be in range") .expect("environment index must be in range")
} }
} }

12
boa_engine/src/object/internal_methods/arguments.rs

@ -41,7 +41,7 @@ pub(crate) fn arguments_exotic_get_own_property(
.borrow() .borrow()
.as_mapped_arguments() .as_mapped_arguments()
.expect("arguments exotic method must only be callable from arguments objects") .expect("arguments exotic method must only be callable from arguments objects")
.get(*index as usize) .get(*index)
{ {
// a. Set desc.[[Value]] to Get(map, P). // a. Set desc.[[Value]] to Get(map, P).
return Ok(Some( return Ok(Some(
@ -78,8 +78,8 @@ pub(crate) fn arguments_exotic_define_own_property(
obj.borrow() obj.borrow()
.as_mapped_arguments() .as_mapped_arguments()
.expect("arguments exotic method must only be callable from arguments objects") .expect("arguments exotic method must only be callable from arguments objects")
.get(index as usize) .get(index)
.map(|value| (index as usize, value)) .map(|value| (index, value))
} else { } else {
None None
}; };
@ -170,7 +170,7 @@ pub(crate) fn arguments_exotic_get(
.borrow() .borrow()
.as_mapped_arguments() .as_mapped_arguments()
.expect("arguments exotic method must only be callable from arguments objects") .expect("arguments exotic method must only be callable from arguments objects")
.get(*index as usize) .get(*index)
{ {
// a. Assert: map contains a formal parameter mapping for P. // a. Assert: map contains a formal parameter mapping for P.
// b. Return Get(map, P). // b. Return Get(map, P).
@ -209,7 +209,7 @@ pub(crate) fn arguments_exotic_set(
obj.borrow_mut() obj.borrow_mut()
.as_mapped_arguments_mut() .as_mapped_arguments_mut()
.expect("arguments exotic method must only be callable from arguments objects") .expect("arguments exotic method must only be callable from arguments objects")
.set(index as usize, &value); .set(index, &value);
} }
} }
@ -240,7 +240,7 @@ pub(crate) fn arguments_exotic_delete(
obj.borrow_mut() obj.borrow_mut()
.as_mapped_arguments_mut() .as_mapped_arguments_mut()
.expect("arguments exotic method must only be callable from arguments objects") .expect("arguments exotic method must only be callable from arguments objects")
.delete(*index as usize); .delete(*index);
} }
} }

4
boa_engine/src/realm.rs

@ -116,8 +116,8 @@ impl Realm {
.poisonable_environment(); .poisonable_environment();
let mut bindings = env.bindings().borrow_mut(); let mut bindings = env.bindings().borrow_mut();
if bindings.len() < binding_number { if bindings.len() < binding_number as usize {
bindings.resize(binding_number, None); bindings.resize(binding_number as usize, None);
} }
} }

4
boa_engine/src/vm/code_block.rs

@ -97,7 +97,7 @@ pub struct CodeBlock {
pub(crate) bindings: Box<[BindingLocator]>, pub(crate) bindings: Box<[BindingLocator]>,
/// Number of binding for the function environment. /// Number of binding for the function environment.
pub(crate) num_bindings: usize, pub(crate) num_bindings: u32,
/// Functions inside this function /// Functions inside this function
pub(crate) functions: Box<[Gc<Self>]>, pub(crate) functions: Box<[Gc<Self>]>,
@ -123,7 +123,7 @@ pub struct CodeBlock {
pub(crate) function_environment_push_location: u32, pub(crate) function_environment_push_location: u32,
/// The number of bindings in the parameters environment. /// The number of bindings in the parameters environment.
pub(crate) parameters_env_bindings: Option<usize>, pub(crate) parameters_env_bindings: Option<u32>,
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
/// Trace instruction execution to `stdout`. /// Trace instruction execution to `stdout`.

4
boa_engine/src/vm/opcode/push/environment.rs

@ -25,7 +25,7 @@ impl Operation for PushDeclarativeEnvironment {
context context
.vm .vm
.environments .environments
.push_lexical(num_bindings as usize, compile_environment); .push_lexical(num_bindings, compile_environment);
context.vm.frame_mut().inc_frame_env_stack(); context.vm.frame_mut().inc_frame_env_stack();
Ok(CompletionType::Normal) Ok(CompletionType::Normal)
} }
@ -51,7 +51,7 @@ impl Operation for PushFunctionEnvironment {
context context
.vm .vm
.environments .environments
.push_function_inherit(num_bindings as usize, compile_environment); .push_function_inherit(num_bindings, compile_environment);
Ok(CompletionType::Normal) Ok(CompletionType::Normal)
} }
} }

Loading…
Cancel
Save