Skip to content

Truly independent web browser

LibJS: Shrink DeclarativeEnvironment from 40 to 32 bytes

Andreas Klinge5a51ef6355c
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
index 542ed11e0d2..e9d0bc85b7b 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
@@ -3665,7 +3665,10 @@ asm_handler_SetLexicalBinding:
cmp w2, #0
csel x2, x9, xzr, ne
cbnz x2, .Lasm_SetLexicalBinding.ssa_block_9
- ldr x1, [x1, #48]
+ ldr w1, [x1, #48]
+ add x9, x24, w1, uxtw #3
+ cmp w1, #0
+ csel x1, x9, xzr, ne
ldr x1, [x1]
cbz x1, .Lasm_SetLexicalBinding.assert_failure_1
ldrb w1, [x1, x0]
@@ -3674,7 +3677,10 @@ asm_handler_SetLexicalBinding:
ldr w4, [x2, #24]
cmp x4, x0
b.hi .Lasm_SetLexicalBinding.ssa_block_12
- ldr x1, [x1, #48]
+ ldr w1, [x1, #48]
+ add x9, x24, w1, uxtw #3
+ cmp w1, #0
+ csel x1, x9, xzr, ne
ldr x1, [x1]
cbz x1, .Lasm_SetLexicalBinding.assert_failure_2
mov x2, x0
@@ -3726,7 +3732,10 @@ asm_handler_SetVariableBinding:
cmp w2, #0
csel x2, x9, xzr, ne
cbnz x2, .Lasm_SetVariableBinding.ssa_block_9
- ldr x1, [x1, #48]
+ ldr w1, [x1, #48]
+ add x9, x24, w1, uxtw #3
+ cmp w1, #0
+ csel x1, x9, xzr, ne
ldr x1, [x1]
cbz x1, .Lasm_SetVariableBinding.assert_failure_1
ldrb w1, [x1, x0]
@@ -3735,7 +3744,10 @@ asm_handler_SetVariableBinding:
ldr w4, [x2, #24]
cmp x4, x0
b.hi .Lasm_SetVariableBinding.ssa_block_12
- ldr x1, [x1, #48]
+ ldr w1, [x1, #48]
+ add x9, x24, w1, uxtw #3
+ cmp w1, #0
+ csel x1, x9, xzr, ne
ldr x1, [x1]
cbz x1, .Lasm_SetVariableBinding.assert_failure_2
mov x2, x0
@@ -3801,7 +3813,10 @@ asm_handler_DynamicSetLexicalBinding:
cmp w2, #0
csel x2, x9, xzr, ne
cbnz x2, .Lasm_DynamicSetLexicalBinding.ssa_block_13
- ldr x0, [x0, #48]
+ ldr w0, [x0, #48]
+ add x9, x24, w0, uxtw #3
+ cmp w0, #0
+ csel x0, x9, xzr, ne
ldr x0, [x0]
cbz x0, .Lasm_DynamicSetLexicalBinding.assert_failure_1
ldrb w0, [x0, x1]
@@ -3810,7 +3825,10 @@ asm_handler_DynamicSetLexicalBinding:
ldr w4, [x2, #24]
cmp x4, x1
b.hi .Lasm_DynamicSetLexicalBinding.ssa_block_16
- ldr x0, [x0, #48]
+ ldr w0, [x0, #48]
+ add x9, x24, w0, uxtw #3
+ cmp w0, #0
+ csel x0, x9, xzr, ne
ldr x0, [x0]
cbz x0, .Lasm_DynamicSetLexicalBinding.assert_failure_2
mov x2, x1
@@ -3876,7 +3894,10 @@ asm_handler_DynamicSetVariableBinding:
cmp w2, #0
csel x2, x9, xzr, ne
cbnz x2, .Lasm_DynamicSetVariableBinding.ssa_block_13
- ldr x0, [x0, #48]
+ ldr w0, [x0, #48]
+ add x9, x24, w0, uxtw #3
+ cmp w0, #0
+ csel x0, x9, xzr, ne
ldr x0, [x0]
cbz x0, .Lasm_DynamicSetVariableBinding.assert_failure_1
ldrb w0, [x0, x1]
@@ -3885,7 +3906,10 @@ asm_handler_DynamicSetVariableBinding:
ldr w4, [x2, #24]
cmp x4, x1
b.hi .Lasm_DynamicSetVariableBinding.ssa_block_16
- ldr x0, [x0, #48]
+ ldr w0, [x0, #48]
+ add x9, x24, w0, uxtw #3
+ cmp w0, #0
+ csel x0, x9, xzr, ne
ldr x0, [x0]
cbz x0, .Lasm_DynamicSetVariableBinding.assert_failure_2
mov x2, x1
@@ -5920,7 +5944,10 @@ asm_handler_SetGlobal:
cmp w2, #0
csel x2, x9, xzr, ne
cbnz x2, .Lasm_SetGlobal.ssa_block_6
- ldr x1, [x1, #48]
+ ldr w1, [x1, #48]
+ add x9, x24, w1, uxtw #3
+ cmp w1, #0
+ csel x1, x9, xzr, ne
ldr x1, [x1]
cbz x1, .Lasm_SetGlobal.assert_failure_4
ldrb w1, [x1, x0]
@@ -5929,7 +5956,10 @@ asm_handler_SetGlobal:
ldr w4, [x2, #24]
cmp x4, x0
b.hi .Lasm_SetGlobal.ssa_block_9
- ldr x1, [x1, #48]
+ ldr w1, [x1, #48]
+ add x9, x24, w1, uxtw #3
+ cmp w1, #0
+ csel x1, x9, xzr, ne
ldr x1, [x1]
cbz x1, .Lasm_SetGlobal.assert_failure_5
mov x2, x0
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
index a0ac18558b5..971d6059011 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
@@ -3641,7 +3641,10 @@ asm_handler_SetLexicalBinding:
cmovnz rdx, r11
test rdx, rdx
jnz .Lasm_SetLexicalBinding.ssa_block_9
- mov rcx, QWORD PTR [rcx + 48]
+ mov ecx, DWORD PTR [rcx + 48]
+ lea r11, [r15 + rcx * 8]
+ test ecx, ecx
+ cmovnz rcx, r11
mov rcx, QWORD PTR [rcx]
test rcx, rcx
jz .Lasm_SetLexicalBinding.assert_failure_1
@@ -3651,7 +3654,10 @@ asm_handler_SetLexicalBinding:
mov edi, DWORD PTR [rdx + 24]
cmp rdi, rax
ja .Lasm_SetLexicalBinding.ssa_block_12
- mov rcx, QWORD PTR [rcx + 48]
+ mov ecx, DWORD PTR [rcx + 48]
+ lea r11, [r15 + rcx * 8]
+ test ecx, ecx
+ cmovnz rcx, r11
mov rcx, QWORD PTR [rcx]
test rcx, rcx
jz .Lasm_SetLexicalBinding.assert_failure_2
@@ -3712,7 +3718,10 @@ asm_handler_SetVariableBinding:
cmovnz rdx, r11
test rdx, rdx
jnz .Lasm_SetVariableBinding.ssa_block_9
- mov rcx, QWORD PTR [rcx + 48]
+ mov ecx, DWORD PTR [rcx + 48]
+ lea r11, [r15 + rcx * 8]
+ test ecx, ecx
+ cmovnz rcx, r11
mov rcx, QWORD PTR [rcx]
test rcx, rcx
jz .Lasm_SetVariableBinding.assert_failure_1
@@ -3722,7 +3731,10 @@ asm_handler_SetVariableBinding:
mov edi, DWORD PTR [rdx + 24]
cmp rdi, rax
ja .Lasm_SetVariableBinding.ssa_block_12
- mov rcx, QWORD PTR [rcx + 48]
+ mov ecx, DWORD PTR [rcx + 48]
+ lea r11, [r15 + rcx * 8]
+ test ecx, ecx
+ cmovnz rcx, r11
mov rcx, QWORD PTR [rcx]
test rcx, rcx
jz .Lasm_SetVariableBinding.assert_failure_2
@@ -3796,7 +3808,10 @@ asm_handler_DynamicSetLexicalBinding:
cmovnz rdx, r11
test rdx, rdx
jnz .Lasm_DynamicSetLexicalBinding.ssa_block_13
- mov rax, QWORD PTR [rax + 48]
+ mov eax, DWORD PTR [rax + 48]
+ lea r11, [r15 + rax * 8]
+ test eax, eax
+ cmovnz rax, r11
mov rax, QWORD PTR [rax]
test rax, rax
jz .Lasm_DynamicSetLexicalBinding.assert_failure_1
@@ -3806,7 +3821,10 @@ asm_handler_DynamicSetLexicalBinding:
mov edi, DWORD PTR [rdx + 24]
cmp rdi, rcx
ja .Lasm_DynamicSetLexicalBinding.ssa_block_16
- mov rax, QWORD PTR [rax + 48]
+ mov eax, DWORD PTR [rax + 48]
+ lea r11, [r15 + rax * 8]
+ test eax, eax
+ cmovnz rax, r11
mov rax, QWORD PTR [rax]
test rax, rax
jz .Lasm_DynamicSetLexicalBinding.assert_failure_2
@@ -3880,7 +3898,10 @@ asm_handler_DynamicSetVariableBinding:
cmovnz rdx, r11
test rdx, rdx
jnz .Lasm_DynamicSetVariableBinding.ssa_block_13
- mov rax, QWORD PTR [rax + 48]
+ mov eax, DWORD PTR [rax + 48]
+ lea r11, [r15 + rax * 8]
+ test eax, eax
+ cmovnz rax, r11
mov rax, QWORD PTR [rax]
test rax, rax
jz .Lasm_DynamicSetVariableBinding.assert_failure_1
@@ -3890,7 +3911,10 @@ asm_handler_DynamicSetVariableBinding:
mov edi, DWORD PTR [rdx + 24]
cmp rdi, rcx
ja .Lasm_DynamicSetVariableBinding.ssa_block_16
- mov rax, QWORD PTR [rax + 48]
+ mov eax, DWORD PTR [rax + 48]
+ lea r11, [r15 + rax * 8]
+ test eax, eax
+ cmovnz rax, r11
mov rax, QWORD PTR [rax]
test rax, rax
jz .Lasm_DynamicSetVariableBinding.assert_failure_2
@@ -6002,7 +6026,10 @@ asm_handler_SetGlobal:
cmovnz rdx, r11
test rdx, rdx
jnz .Lasm_SetGlobal.ssa_block_6
- mov rcx, QWORD PTR [rcx + 48]
+ mov ecx, DWORD PTR [rcx + 48]
+ lea r11, [r15 + rcx * 8]
+ test ecx, ecx
+ cmovnz rcx, r11
mov rcx, QWORD PTR [rcx]
test rcx, rcx
jz .Lasm_SetGlobal.assert_failure_4
@@ -6012,7 +6039,10 @@ asm_handler_SetGlobal:
mov edi, DWORD PTR [rdx + 24]
cmp rdi, rax
ja .Lasm_SetGlobal.ssa_block_9
- mov rcx, QWORD PTR [rcx + 48]
+ mov ecx, DWORD PTR [rcx + 48]
+ lea r11, [r15 + rcx * 8]
+ test ecx, ecx
+ cmovnz rcx, r11
mov rcx, QWORD PTR [rcx]
test rcx, rcx
jz .Lasm_SetGlobal.assert_failure_5
diff --git a/Libraries/LibJS/Flap/tests/interpreter-layout.conf b/Libraries/LibJS/Flap/tests/interpreter-layout.conf
index 525cbf757b1..130e5486e1a 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-layout.conf
+++ b/Libraries/LibJS/Flap/tests/interpreter-layout.conf
@@ -323,7 +323,7 @@ field WatchpointSet.valid bool WATCHPOINT_SET_VALID nonnull scalar
# DeclarativeEnvironment layout
const DECLARATIVE_ENVIRONMENT_RARE_DATA = 48
-field DeclarativeEnvironment.rare_data DeclarativeEnvironmentRareData DECLARATIVE_ENVIRONMENT_RARE_DATA nullable scalar
+field DeclarativeEnvironment.rare_data DeclarativeEnvironmentRareData DECLARATIVE_ENVIRONMENT_RARE_DATA nullable compressed
const DECLARATIVE_ENVIRONMENT_SERIAL = 56
field DeclarativeEnvironment.serial_number u64 DECLARATIVE_ENVIRONMENT_SERIAL nullable scalar
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/environments.rs b/Libraries/LibJS/JIT/Rust/src/codegen/environments.rs
index dce0a2cf646..1fd36b218df 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/environments.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/environments.rs
@@ -39,7 +39,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
let (rare_at_index, rare_at_offset, check) =
(self.masm.new_label(), self.masm.new_label(), self.masm.new_label());
let rare_flags = |codegen: &mut Self| {
- codegen.masm.load64(
+ codegen.emit_load_compressed_cell(
scratch,
&Address::new(environment, layout.declarative_environment_rare_data as i32),
);
@@ -148,14 +148,25 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
self.masm.move_imm64(shape, name);
self.masm.branch64(Condition::NotEqual, scratch, shape, fail);
- // Values that do not fit in their storage move first.
- self.masm.load32(
+ // Values that do not fit in their storage move first. Outside of the cell, they have their capacity in front of
+ // the first value, if there is any storage.
+ let have_capacity = self.masm.new_label();
+ self.masm.load8(
scratch,
&Address::new(
environment,
- layout.declarative_environment_binding_values_capacity as i32,
+ layout.declarative_environment_inline_binding_capacity as i32,
),
);
+ self.masm
+ .branch_test32(Condition::NonZero, scratch, 0xFF, have_capacity);
+ self.masm.load64(
+ scratch,
+ &Address::new(environment, layout.declarative_environment_binding_values as i32),
+ );
+ self.masm.branch64_imm(Condition::Equal, scratch, 0, fail);
+ self.masm.load64(scratch, &Address::new(scratch, -8));
+ self.masm.bind(have_capacity);
self.masm.branch64(Condition::BelowOrEqual, scratch, index, fail);
}
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
index cc4a185a9e8..614d29cb3ac 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
@@ -1268,7 +1268,7 @@ fn fake_object_layout() -> RuntimeLayout {
generator_object_pending_completion_value: 0,
generator_object_pending_completion_type: 0,
declarative_environment_binding_values_size: 32,
- declarative_environment_binding_values_capacity: 40,
+ declarative_environment_inline_binding_capacity: 40,
environment_shape_binding_names: 48,
environment_shape_has_unique_binding_names: 32,
declarative_environment_shape: 24,
@@ -1877,7 +1877,7 @@ const BINDING_MUTABLE: u8 = 2;
struct FakeEnvironment {
words: &'static mut [u64],
values: Vec<u64>,
- rare_data: Vec<u64>,
+ rare_data: &'static mut [u64],
rare_flags: Vec<u8>,
shape: &'static mut [u64],
shape_flags: Vec<u8>,
@@ -1888,10 +1888,10 @@ impl FakeEnvironment {
/// a shape (if any) and whose other flags are in its rare data.
fn new(values: &[u64], flags: &[u8], shape_flags: Option<usize>, outer: Option<&FakeEnvironment>) -> Box<Self> {
let in_shape = shape_flags.unwrap_or(0);
- let mut environment = Box::new(Self {
+ let environment = Box::new(Self {
words: super::fake_heap::cell(&[0; 10]),
values: values.to_vec(),
- rare_data: vec![0; 8],
+ rare_data: super::fake_heap::cell(&[0; 8]),
rare_flags: flags[in_shape..].to_vec(),
shape: super::fake_heap::cell(&[0; 8]),
shape_flags: flags[..in_shape].to_vec(),
@@ -1904,7 +1904,7 @@ impl FakeEnvironment {
environment.words[3] = super::fake_heap::compressed(environment.shape.as_ptr() as u64);
}
environment.words[6] = environment.values.as_ptr() as u64;
- environment.words[7] = environment.rare_data.as_ptr() as u64;
+ environment.words[7] = super::fake_heap::compressed(environment.rare_data.as_ptr() as u64);
environment
}
@@ -2203,6 +2203,10 @@ impl FakeGlobals {
globals.object[7] = globals.named_properties.as_ptr() as u64;
globals.shape[10] = 7 << 32;
globals.environment.words[8] = 99;
+ // NB: The code of these tests has a heap region base of 0, so compressed pointers name low fake cells.
+ let rare_data = low_cell(&[0; 8]);
+ rare_data[5] = globals.environment.rare_flags.as_ptr() as u64;
+ globals.environment.words[7] = compressed(rare_data);
globals.realm[3] = globals.object.as_ptr() as u64;
globals.realm[4] = globals.environment.address();
globals.executable[1] = globals.caches.as_ptr() as u64;
diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
index 294eb8a2416..dbb1ede5713 100644
--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs
+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
@@ -1822,20 +1822,20 @@ pub struct RuntimeLayout {
/// `GeneratorObject::pending_completion_type` (u32): the type of that
/// completion, as `GetCompletionFields` writes it.
pub generator_object_pending_completion_type: u32,
- /// `DeclarativeEnvironment::binding_values.size` (u32).
+ /// `DeclarativeEnvironment::binding_count` (u32).
pub declarative_environment_binding_values_size: u32,
- /// The capacity (u32) of `DeclarativeEnvironment::binding_values`.
- pub declarative_environment_binding_values_capacity: u32,
+ /// `Environment::inline_binding_capacity` (u8), zero if the binding values are not in the cell.
+ pub declarative_environment_inline_binding_capacity: u32,
/// The pointer to the first binding name (a `Utf16FlyString`, one
/// word) of an `EnvironmentShape`.
pub environment_shape_binding_names: u32,
/// `EnvironmentShape::has_unique_binding_names` (bool).
pub environment_shape_has_unique_binding_names: u32,
- /// `DeclarativeEnvironment::binding_values.data`, the pointer to the first `Value`.
+ /// `DeclarativeEnvironment::binding_values`, the pointer to the first `Value`.
pub declarative_environment_binding_values: u32,
/// `DeclarativeEnvironment::shape` (cell pointer, may be null).
pub declarative_environment_shape: u32,
- /// `DeclarativeEnvironment::rare_data` (pointer).
+ /// `DeclarativeEnvironment::rare_data` (compressed pointer).
pub declarative_environment_rare_data: u32,
/// `DeclarativeEnvironmentRareData::binding_flags.data`, the pointer to the first flag byte.
pub rare_data_binding_flags: u32,
diff --git a/Libraries/LibJS/Rust/generate/layout_table.rs b/Libraries/LibJS/Rust/generate/layout_table.rs
index ec8682883a4..7d6dee682b5 100644
--- a/Libraries/LibJS/Rust/generate/layout_table.rs
+++ b/Libraries/LibJS/Rust/generate/layout_table.rs
@@ -447,7 +447,7 @@ pub fn generate(configuration: &LayoutConfiguration) -> LayoutWriter {
field!(w, "WATCHPOINT_SET_VALID", "WatchpointSet.valid", "bool", WatchpointSet, valid, 1, "nonnull", "scalar");
w.section("DeclarativeEnvironment layout");
- field!(w, "DECLARATIVE_ENVIRONMENT_RARE_DATA", "DeclarativeEnvironment.rare_data", "DeclarativeEnvironmentRareData", DeclarativeEnvironment, rare_data, 8, "nullable", "scalar");
+ field!(w, "DECLARATIVE_ENVIRONMENT_RARE_DATA", "DeclarativeEnvironment.rare_data", "DeclarativeEnvironmentRareData", DeclarativeEnvironment, rare_data, 4, "nullable", "compressed");
field!(w, "DECLARATIVE_ENVIRONMENT_SERIAL", "DeclarativeEnvironment.serial_number", "u64", GlobalDeclarativeEnvironment, serial_number, 8, "nullable", "scalar");
w.section("GlobalVariableCache layout");
@@ -557,7 +557,7 @@ pub fn generate(configuration: &LayoutConfiguration) -> LayoutWriter {
offset!(w, "DECLARATIVE_ENVIRONMENT_RARE_DATA_BINDING_FLAGS", DeclarativeEnvironmentRareData, binding_flags);
offset!(w, "ENVIRONMENT_SHAPE_BINDING_FLAGS", EnvironmentShape, binding_flags);
w.constant("BINDING_FLAG_MUTABLE", BINDING_FLAG_MUTABLE);
- offset!(w, "BINDING_VALUES_DATA_PTR", DeclarativeEnvironment, binding_values.data);
+ offset!(w, "BINDING_VALUES_DATA_PTR", DeclarativeEnvironment, binding_values);
w.line("field DeclarativeEnvironment.binding_values BindingValues BINDING_VALUES_DATA_PTR nonnull scalar");
offset!(w, "BINDING_FLAGS_DATA_PTR", DeclarativeEnvironmentRareData, binding_flags.data);
w.line("field DeclarativeEnvironmentRareData.binding_flags BindingFlags BINDING_FLAGS_DATA_PTR nonnull scalar");
diff --git a/Libraries/LibJS/Rust/src/gc/class_id.rs b/Libraries/LibJS/Rust/src/gc/class_id.rs
index a65134fa3e6..aead02ba35d 100644
--- a/Libraries/LibJS/Rust/src/gc/class_id.rs
+++ b/Libraries/LibJS/Rust/src/gc/class_id.rs
@@ -33,6 +33,7 @@ define_class_ids! {
Shape,
WatchpointSet,
SpecialPropertyCache,
+ DeclarativeEnvironmentRareData,
DescriptorArray,
DictionaryPropertyTable,
ValueStorage,
diff --git a/Libraries/LibJS/Rust/src/gc/interpreter_buffer.rs b/Libraries/LibJS/Rust/src/gc/interpreter_buffer.rs
index b39dc0471d9..c26646d52d5 100644
--- a/Libraries/LibJS/Rust/src/gc/interpreter_buffer.rs
+++ b/Libraries/LibJS/Rust/src/gc/interpreter_buffer.rs
@@ -16,13 +16,6 @@ use std::alloc::{Layout, alloc, dealloc, handle_alloc_error};
use super::visitor::{Trace, Visitor};
use crate::layout::buffer::{InterpreterBuffer, ThinSlice};
-/// Whether a buffer owns its storage, which it then allocated itself.
-#[derive(PartialEq, Eq)]
-enum Ownership {
- Owned,
- Unowned,
-}
-
/// Mirrors AK::Vector's growth policy.
fn padded_capacity(capacity: usize) -> usize {
4 + capacity + capacity / 4
@@ -108,36 +101,7 @@ impl<T: Copy> InterpreterBuffer<T> {
self.reallocate(0);
}
- /// Makes room for `capacity` elements at `data`, which the buffer does not own, the storage of this buffer.
- ///
- /// # Safety
- ///
- /// The buffer must have no storage, and `data` must have room for `capacity` elements for as long as the buffer
- /// uses it.
- pub unsafe fn use_unowned_storage(&self, data: *mut T, capacity: usize) {
- assert!(self.data.get().is_null());
- self.data.set(data);
- self.set_capacity(capacity);
- }
-
- /// Moves the elements from storage the buffer does not own to storage of its own, with room for at least
- /// `needed_capacity` elements and as much more as append() would make.
- pub fn move_to_owned_storage(&self, needed_capacity: usize) {
- self.reallocate_from(needed_capacity.max(padded_capacity(self.size())), Ownership::Unowned);
- }
-
- /// Removes every element and forgets storage the buffer does not own.
- pub fn forget_unowned_storage(&self) {
- self.data.set(core::ptr::null_mut());
- self.size.set(0);
- self.capacity.set(0);
- }
-
fn reallocate(&self, new_capacity: usize) {
- self.reallocate_from(new_capacity, Ownership::Owned);
- }
-
- fn reallocate_from(&self, new_capacity: usize, ownership: Ownership) {
const { assert!(size_of::<T>() != 0) };
let size = self.size();
assert!(new_capacity >= size);
@@ -157,7 +121,7 @@ impl<T: Copy> InterpreterBuffer<T> {
}
new_data
};
- if !old_data.is_null() && ownership == Ownership::Owned {
+ if !old_data.is_null() {
let old_layout = Layout::array::<T>(self.capacity()).expect("the buffer was allocated with this layout");
// SAFETY: The old storage was allocated by this method with this layout.
unsafe { dealloc(old_data.cast(), old_layout) };
diff --git a/Libraries/LibJS/Rust/src/jit/allocation.rs b/Libraries/LibJS/Rust/src/jit/allocation.rs
index a97b0e0836a..a989ea820c4 100644
--- a/Libraries/LibJS/Rust/src/jit/allocation.rs
+++ b/Libraries/LibJS/Rust/src/jit/allocation.rs
@@ -285,9 +285,9 @@ pub fn make_call_environment_template(vm: &Vm, function: Gc<EcmascriptFunctionOb
return;
};
let has_inline_binding_values = binding_count != 0;
- if !environment.rare_data.get().is_null()
- || environment.binding_values_are_inline.get() != has_inline_binding_values
- || environment.binding_values.size() != 0
+ if !environment.rare_data.is_null()
+ || environment.binding_values_are_inline() != has_inline_binding_values
+ || environment.binding_count.get() != 0
|| (has_inline_binding_values && environment.shape.get().is_none())
{
return;
@@ -307,7 +307,7 @@ pub fn make_call_environment_template(vm: &Vm, function: Gc<EcmascriptFunctionOb
};
template.words.copy_from_slice(&words);
for (offset, size) in [
- (core::mem::offset_of!(DeclarativeEnvironment, binding_values.data), 8),
+ (core::mem::offset_of!(DeclarativeEnvironment, binding_values), 8),
(
core::mem::offset_of!(Environment, outer),
size_of::<NullableMember<Environment>>(),
@@ -346,7 +346,7 @@ pub fn function_environment_template(
if binds_this {
sample.bind_this_value(vm, Value::UNDEFINED).must();
}
- if !sample.rare_data.get().is_null() || sample.binding_values_are_inline.get() != (binding_count != 0) {
+ if !sample.rare_data.is_null() || sample.binding_values_are_inline() != (binding_count != 0) {
return None;
}
let mut cells = Vec::new();
@@ -364,7 +364,7 @@ pub fn function_environment_template(
cell_size,
words: cell_words(sample, size_of::<FunctionEnvironment>()),
inline_binding_values: binding_count != 0,
- binding_values_offset: offset(core::mem::offset_of!(DeclarativeEnvironment, binding_values.data)),
+ binding_values_offset: offset(core::mem::offset_of!(DeclarativeEnvironment, binding_values)),
binds_this,
this_value_offset: offset(FUNCTION_ENVIRONMENT_THIS_VALUE_OFFSET),
cells,
@@ -405,7 +405,7 @@ pub fn lexical_environment_templates(
u32::try_from(binding_count).ok()?,
false,
);
- if !sample.rare_data.get().is_null() || sample.binding_values_are_inline.get() != (binding_count != 0) {
+ if !sample.rare_data.is_null() || sample.binding_values_are_inline() != (binding_count != 0) {
return None;
}
Some(LexicalEnvironmentTemplateSnapshot {
@@ -414,7 +414,7 @@ pub fn lexical_environment_templates(
words: cell_words(sample, size_of::<DeclarativeEnvironment>()),
binding_count: u32::try_from(binding_count).ok()?,
inline_binding_values: binding_count != 0,
- binding_values_offset: offset(core::mem::offset_of!(DeclarativeEnvironment, binding_values.data)),
+ binding_values_offset: offset(core::mem::offset_of!(DeclarativeEnvironment, binding_values)),
outer_offset: offset(core::mem::offset_of!(Environment, outer)),
shape: shape.map(|shape| cell(header_of(shape))),
})
diff --git a/Libraries/LibJS/Rust/src/jit/runtime_info.rs b/Libraries/LibJS/Rust/src/jit/runtime_info.rs
index a8eb762f5d2..93fd24d3046 100644
--- a/Libraries/LibJS/Rust/src/jit/runtime_info.rs
+++ b/Libraries/LibJS/Rust/src/jit/runtime_info.rs
@@ -268,14 +268,11 @@ fn runtime_layout() -> RuntimeLayout {
generator_object_class: core::ptr::from_ref(GeneratorObject::CLASS) as u64,
generator_object_pending_completion_value: offset(offset_of!(GeneratorObject, pending_completion_value)),
generator_object_pending_completion_type: offset(offset_of!(GeneratorObject, pending_completion_type)),
- declarative_environment_binding_values_size: offset(offset_of!(DeclarativeEnvironment, binding_values.size)),
- declarative_environment_binding_values_capacity: offset(offset_of!(
- DeclarativeEnvironment,
- binding_values.capacity
- )),
+ declarative_environment_binding_values_size: offset(offset_of!(DeclarativeEnvironment, binding_count)),
+ declarative_environment_inline_binding_capacity: offset(offset_of!(Environment, inline_binding_capacity)),
environment_shape_binding_names: offset(offset_of!(EnvironmentShape, binding_names)),
environment_shape_has_unique_binding_names: offset(offset_of!(EnvironmentShape, has_unique_binding_names)),
- declarative_environment_binding_values: offset(offset_of!(DeclarativeEnvironment, binding_values.data)),
+ declarative_environment_binding_values: offset(offset_of!(DeclarativeEnvironment, binding_values)),
declarative_environment_shape: offset(offset_of!(Environment, shape)),
declarative_environment_rare_data: offset(offset_of!(DeclarativeEnvironment, rare_data)),
rare_data_binding_flags: offset(offset_of!(DeclarativeEnvironmentRareData, binding_flags.data)),
@@ -615,7 +612,7 @@ fn dynamic_call_layout(vm: &Vm) -> DynamicCallLayout {
call_environment_template_binds_this: offset(offset_of!(CallEnvironmentTemplate, binds_this)),
call_environment_template_words: offset(offset_of!(CallEnvironmentTemplate, words)),
function_environment_words: offset(FUNCTION_ENVIRONMENT_WORDS),
- function_environment_binding_values: offset(offset_of!(DeclarativeEnvironment, binding_values.data)),
+ function_environment_binding_values: offset(offset_of!(DeclarativeEnvironment, binding_values)),
function_environment_outer: offset(offset_of!(Environment, outer)),
function_environment_function_object: offset(FUNCTION_ENVIRONMENT_FUNCTION_OBJECT_OFFSET),
function_environment_this_value: offset(FUNCTION_ENVIRONMENT_THIS_VALUE_OFFSET),
diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rs
index 0166a37cf68..cae25cb8848 100644
--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs
+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs
@@ -1152,7 +1152,7 @@ impl SnapshotBuilder<'_> {
let Some(environment) = environment.as_declarative_environment() else {
continue;
};
- if coordinate.index as usize >= environment.binding_values.size() {
+ if coordinate.index as usize >= environment.binding_count.get() as usize {
continue;
}
let value = environment.get_initialized_binding_value_direct(coordinate.index as usize);
diff --git a/Libraries/LibJS/Rust/src/layout/environment.rs b/Libraries/LibJS/Rust/src/layout/environment.rs
index 68b41519009..80eac8efdfc 100644
--- a/Libraries/LibJS/Rust/src/layout/environment.rs
+++ b/Libraries/LibJS/Rust/src/layout/environment.rs
@@ -27,8 +27,9 @@ pub struct Environment {
pub this_binding_status: Cell<u8>,
pub permanently_screwed_by_eval: Cell<bool>,
pub declarative: Cell<bool>,
- /// Whether the binding values of a declarative environment are stored in its cell, after the fields of its class.
- pub binding_values_are_inline: Cell<bool>,
+ /// How many binding values a declarative environment has room for in its cell, after the fields of its class. Zero
+ /// if it keeps them elsewhere.
+ pub inline_binding_capacity: Cell<u8>,
}
const _: () = assert!(size_of::<Environment>() == 16);
@@ -36,11 +37,14 @@ const _: () = assert!(size_of::<Environment>() == 16);
#[repr(C)]
pub struct DeclarativeEnvironment {
pub base: Environment,
- pub binding_values: InterpreterBuffer<Value>,
- pub rare_data: Cell<*mut DeclarativeEnvironmentRareData>,
+ /// The first binding value, in the cell or in a malloc allocation that has its capacity in front of the first
+ /// value.
+ pub binding_values: Cell<*mut Value>,
+ pub binding_count: Cell<u32>,
+ pub rare_data: NullableMember<DeclarativeEnvironmentRareData>,
}
-const _: () = assert!(size_of::<DeclarativeEnvironment>() == 40);
+const _: () = assert!(size_of::<DeclarativeEnvironment>() == 32);
/// The declarative environment of a global environment. Caches of global variables hold on to its serial number, which
/// changes when the bindings do.
@@ -52,6 +56,7 @@ pub struct GlobalDeclarativeEnvironment {
#[repr(C)]
pub struct DeclarativeEnvironmentRareData {
+ pub header: CellHeader,
pub binding_flags: InterpreterBuffer<u8>,
pub storage: DeclarativeEnvironmentRareDataStorage,
}
diff --git a/Libraries/LibJS/Rust/src/runtime/declarative_environment.rs b/Libraries/LibJS/Rust/src/runtime/declarative_environment.rs
index 876eb440ea2..a5262933642 100644
--- a/Libraries/LibJS/Rust/src/runtime/declarative_environment.rs
+++ b/Libraries/LibJS/Rust/src/runtime/declarative_environment.rs
@@ -11,6 +11,7 @@
use core::cell::{Cell, RefCell};
use core::ops::Deref;
+use std::alloc::{Layout, alloc, dealloc, handle_alloc_error};
use std::collections::HashMap;
use ak::Utf16FlyString;
@@ -23,10 +24,11 @@ use crate::gc::visitor::{Trace, Visitor};
use crate::interpreter::vm::Vm;
use crate::jit::dependencies::{CellId, Dependency};
use crate::layout::buffer::InterpreterBuffer;
-use crate::layout::cell::Gc;
+use crate::layout::cell::{CellHeader, Gc};
pub use crate::layout::environment::{
DeclarativeEnvironment, DeclarativeEnvironmentRareData, GlobalDeclarativeEnvironment,
};
+use crate::layout::member::NullableMember;
use crate::layout::value::Value;
use crate::runtime::abstract_operations::{DisposeCapability, add_disposable_resource};
use crate::runtime::completion::ThrowCompletionOr;
@@ -171,6 +173,138 @@ unsafe impl Trace for DeclarativeEnvironmentRareData {
}
}
+define_cell!(DeclarativeEnvironmentRareData, Other);
+
+impl Drop for DeclarativeEnvironmentRareData {
+ fn drop(&mut self) {
+ self.binding_flags.clear();
+ }
+}
+
+/// Mirrors AK::Vector's growth policy.
+fn padded_binding_capacity(capacity: usize) -> usize {
+ 4 + capacity + capacity / 4
+}
+
+fn owned_binding_values_layout(capacity: usize) -> Layout {
+ Layout::array::<u64>(capacity + 1).expect("the binding values fit in memory")
+}
+
+/// The binding values of an environment, which it keeps in its cell or in a malloc allocation.
+struct BindingValues<'a> {
+ environment: &'a DeclarativeEnvironment,
+}
+
+impl BindingValues<'_> {
+ fn size(&self) -> usize {
+ self.environment.binding_count.get() as usize
+ }
+
+ fn capacity(&self) -> usize {
+ if self.environment.binding_values_are_inline() {
+ return usize::from(self.environment.inline_binding_capacity.get());
+ }
+ let data = self.environment.binding_values.get();
+ if data.is_null() {
+ return 0;
+ }
+ // SAFETY: A malloc allocation has its capacity in front of the first value.
+ unsafe { data.cast::<u64>().sub(1).read() as usize }
+ }
+
+ fn is_empty(&self) -> bool {
+ self.size() == 0
+ }
+
+ fn get(&self, index: usize) -> Value {
+ assert!(index < self.size(), "the binding exists");
+ // SAFETY: The first `size` values are initialized.
+ unsafe { self.environment.binding_values.get().add(index).read() }
+ }
+
+ fn set(&self, index: usize, value: Value) {
+ assert!(index < self.size(), "the binding exists");
+ // SAFETY: The value is in bounds, and nothing borrows the values.
+ unsafe { self.environment.binding_values.get().add(index).write(value) };
+ }
+
+ fn append(&self, value: Value) {
+ let size = self.size();
+ if size == self.capacity() {
+ self.reallocate(padded_binding_capacity(size + 1));
+ }
+ // SAFETY: There is room for one more value.
+ unsafe { self.environment.binding_values.get().add(size).write(value) };
+ self.environment
+ .binding_count
+ .set(u32::try_from(size + 1).expect("the binding count fits in u32"));
+ }
+
+ fn ensure_capacity(&self, needed_capacity: usize) {
+ if needed_capacity > self.capacity() {
+ self.reallocate(needed_capacity);
+ }
+ }
+
+ fn shrink_to_fit(&self) {
+ if self.size() != self.capacity() {
+ self.reallocate(self.size());
+ }
+ }
+
+ /// Removes every value and frees the malloc allocation, if there is one.
+ fn clear(&self) {
+ self.environment.binding_count.set(0);
+ self.reallocate(0);
+ }
+
+ /// Moves the values from the cell to a malloc allocation, with room for at least `needed_capacity` values and as
+ /// much more as append() would make.
+ fn move_to_owned_storage(&self, needed_capacity: usize) {
+ self.reallocate(needed_capacity.max(padded_binding_capacity(self.size())));
+ }
+
+ fn reallocate(&self, new_capacity: usize) {
+ let size = self.size();
+ assert!(new_capacity >= size);
+ let old_data = self.environment.binding_values.get();
+ let old_capacity = self.capacity();
+ let was_inline = self.environment.binding_values_are_inline();
+ let new_data = if new_capacity == 0 {
+ core::ptr::null_mut()
+ } else {
+ let layout = owned_binding_values_layout(new_capacity);
+ // SAFETY: The layout has a non-zero size.
+ let allocation = unsafe { alloc(layout) }.cast::<u64>();
+ if allocation.is_null() {
+ handle_alloc_error(layout);
+ }
+ // SAFETY: The allocation has room for the capacity and `new_capacity` values, of which `size` come from the
+ // old storage, which is another one.
+ unsafe {
+ allocation.write(new_capacity as u64);
+ let new_data = allocation.add(1).cast::<Value>();
+ if size != 0 {
+ core::ptr::copy_nonoverlapping(old_data, new_data, size);
+ }
+ new_data
+ }
+ };
+ if !old_data.is_null() && !was_inline {
+ // SAFETY: The old storage is a malloc allocation that this method made with this layout, with the capacity
+ // in front of the first value.
+ unsafe {
+ dealloc(
+ old_data.cast::<u64>().sub(1).cast(),
+ owned_binding_values_layout(old_capacity),
+ );
+ }
+ }
+ self.environment.binding_values.set(new_data);
+ self.environment.inline_binding_capacity.set(0);
+ }
+}
+
define_cell!(DeclarativeEnvironment, Other, extends: [Environment], finalize: finalize);
// SAFETY: Visits the outer environment and the shape (in the Environment), the rare data's dispose capability and shape
@@ -178,21 +312,19 @@ define_cell!(DeclarativeEnvironment, Other, extends: [Environment], finalize: fi
unsafe impl Trace for DeclarativeEnvironment {
fn trace(&self, visitor: &mut Visitor) {
self.base.trace(visitor);
- if let Some(rare_data) = self.rare_data() {
- rare_data.trace(visitor);
- }
- self.binding_values.trace(visitor);
+ self.rare_data.trace(visitor);
+ // SAFETY: The first `binding_count` values are initialized, and nothing writes them while they are visited.
+ visitor.visit_values(unsafe {
+ ak::slice_from_raw_parts(self.binding_values.get(), self.binding_count.get() as usize)
+ });
}
}
impl Finalize for DeclarativeEnvironment {
fn finalize(&self) {
- if self.binding_values_are_inline.get() {
- self.binding_values.forget_unowned_storage();
- } else {
- self.binding_values.clear();
+ if !self.binding_values_are_inline() {
+ self.binding_values().clear();
}
- self.free_rare_data();
}
}
@@ -243,13 +375,11 @@ pub fn allocate_environment<T: GcCell>(
);
let declarative_environment = declarative(&environment);
// SAFETY: The cell has room for the size class's binding values after the fields of T, as long as it lives.
- unsafe {
- let inline_values = environment.as_ptr().cast::<u8>().add(size_of::<T>()).cast::<Value>();
- declarative_environment
- .binding_values
- .use_unowned_storage(inline_values, usize::from(INLINE_BINDING_CAPACITIES[size_class]));
- }
- declarative_environment.binding_values_are_inline.set(true);
+ let inline_values = unsafe { environment.as_ptr().cast::<u8>().add(size_of::<T>()) }.cast::<Value>();
+ declarative_environment.binding_values.set(inline_values);
+ declarative_environment
+ .inline_binding_capacity
+ .set(INLINE_BINDING_CAPACITIES[size_class]);
environment
}
@@ -335,8 +465,9 @@ impl DeclarativeEnvironment {
pub fn new(class: &'static Class, outer_environment: Option<Gc<Environment>>) -> Self {
Self {
base: Environment::new(class, outer_environment, true),
- binding_values: InterpreterBuffer::new(),
- rare_data: Cell::new(core::ptr::null_mut()),
+ binding_values: Cell::new(core::ptr::null_mut()),
+ binding_count: Cell::new(0),
+ rare_data: NullableMember::null(),
}
}
@@ -359,36 +490,39 @@ impl DeclarativeEnvironment {
)
}
- fn rare_data(&self) -> Option<&DeclarativeEnvironmentRareData> {
- // SAFETY: The rare data belongs to this environment. Only drop_rare_data_if_empty and finalize free it, and
- // nothing keeps a reference to it across either.
- unsafe { self.rare_data.get().as_ref() }
+ fn rare_data(&self) -> Option<Gc<DeclarativeEnvironmentRareData>> {
+ self.rare_data.get()
}
- fn ensure_rare_data(&self) -> &DeclarativeEnvironmentRareData {
- if self.rare_data.get().is_null() {
- self.rare_data
- .set(Box::into_raw(Box::new(DeclarativeEnvironmentRareData {
- binding_flags: InterpreterBuffer::new(),
- storage: DeclarativeEnvironmentRareDataStorage::default(),
- })));
+ fn ensure_rare_data(&self) -> Gc<DeclarativeEnvironmentRareData> {
+ if let Some(rare_data) = self.rare_data() {
+ return rare_data;
}
- self.rare_data().expect("the rare data was just allocated")
+ let rare_data = Vm::of_cell(self).heap().allocate(|_| DeclarativeEnvironmentRareData {
+ header: CellHeader::for_class(DeclarativeEnvironmentRareData::CLASS),
+ binding_flags: InterpreterBuffer::new(),
+ storage: DeclarativeEnvironmentRareDataStorage::default(),
+ });
+ self.rare_data.set(Some(rare_data));
+ rare_data
}
fn drop_rare_data_if_empty(&self) {
- if self.rare_data().is_some_and(DeclarativeEnvironmentRareData::is_empty) {
+ if self.rare_data().is_some_and(|rare_data| rare_data.is_empty()) {
self.free_rare_data();
}
}
fn free_rare_data(&self) {
- let rare_data = self.rare_data.replace(core::ptr::null_mut());
- if rare_data.is_null() {
- return;
- }
- // SAFETY: ensure_rare_data allocated the rare data as a Box, and nothing refers to it anymore.
- unsafe { Box::from_raw(rare_data) }.binding_flags.clear();
+ self.rare_data.set(None);
+ }
+
+ pub fn binding_values_are_inline(&self) -> bool {
+ self.inline_binding_capacity.get() != 0
+ }
+
+ fn binding_values(&self) -> BindingValues<'_> {
+ BindingValues { environment: self }
}
fn increment_environment_serial_number(&self, vm: &Vm) {
@@ -499,11 +633,16 @@ impl DeclarativeEnvironment {
}
pub fn dispose_capability(&self) -> &GcRefCell<DisposeCapability> {
- &self.ensure_rare_data().storage.dispose_capability
+ let rare_data = self.ensure_rare_data();
+ // SAFETY: The environment keeps its rare data alive, and only lets it go when it is empty, which having a
+ // disposable resource stack keeps it from being.
+ &unsafe { &*rare_data.as_ptr() }.storage.dispose_capability
}
pub fn dispose_capability_if_exists(&self) -> Option<&GcRefCell<DisposeCapability>> {
- let dispose_capability = &self.rare_data()?.storage.dispose_capability;
+ let rare_data = self.rare_data()?;
+ // SAFETY: As for dispose_capability().
+ let dispose_capability = &unsafe { &*rare_data.as_ptr() }.storage.dispose_capability;
let exists = dispose_capability.borrow().disposable_resource_stack.is_some();
exists.then_some(dispose_capability)
}
@@ -513,7 +652,7 @@ impl DeclarativeEnvironment {
}
fn binding_count(&self) -> usize {
- self.binding_values.size()
+ self.binding_values().size()
}
fn local_binding_index(&self, index: usize) -> usize {
@@ -521,7 +660,7 @@ impl DeclarativeEnvironment {
}
fn append_binding(&self, binding: Binding) {
- let index = self.binding_values.size();
+ let index = self.binding_values().size();
let mut flags = 0;
if binding.strict {
@@ -550,10 +689,10 @@ impl DeclarativeEnvironment {
rare_data.binding_flags.append(flags);
}
- if self.binding_values_are_inline.get() && index == self.binding_values.capacity() {
+ if self.binding_values_are_inline() && index == self.binding_values().capacity() {
self.move_binding_values_out_of_cell(index + 1);
}
- self.binding_values.append(if binding.initialized {
+ self.binding_values().append(if binding.initialized {
binding.value
} else {
Value::EMPTY
@@ -567,7 +706,7 @@ impl DeclarativeEnvironment {
.rare_data()
.expect("ensure_deleted_bindings_capacity allocates the rare data");
rare_data.storage.deleted_bindings.borrow_mut().set(index, true);
- self.binding_values.set(index, Value::EMPTY);
+ self.binding_values().set(index, Value::EMPTY);
return;
}
@@ -577,7 +716,7 @@ impl DeclarativeEnvironment {
rare_data.storage.bindings_assoc.borrow_mut().remove(name);
let local_index = self.local_binding_index(index);
rare_data.storage.binding_names.borrow_mut()[local_index] = Utf16FlyString::default();
- self.binding_values.set(index, Value::EMPTY);
+ self.binding_values().set(index, Value::EMPTY);
rare_data.binding_flags.set(local_index, 0);
}
@@ -593,7 +732,7 @@ impl DeclarativeEnvironment {
fn binding_at(&self, index: usize) -> Binding {
Binding {
name: self.binding_name(index),
- value: self.binding_values.get(index),
+ value: self.binding_values().get(index),
strict: self.binding_is_strict(index),
mutable: self.binding_is_mutable(index),
can_be_deleted: self.binding_can_be_deleted(index),
@@ -652,7 +791,7 @@ impl DeclarativeEnvironment {
}
fn binding_is_initialized(&self, index: usize) -> bool {
- !self.binding_values.get(index).is_empty()
+ !self.binding_values().get(index).is_empty()
}
fn binding_is_deleted(&self, index: usize) -> bool {
@@ -680,7 +819,7 @@ impl DeclarativeEnvironment {
fn set_environment_shape(&self, shape: Gc<EnvironmentShape>) {
assert!(self.shape.get().is_none());
- assert!(self.binding_values.size() <= shape.size());
+ assert!(self.binding_values().size() <= shape.size());
self.shape.set(Some(shape));
if let Some(rare_data) = self.rare_data() {
@@ -852,7 +991,7 @@ impl DeclarativeEnvironment {
}
// 3. Set the bound value for N in envRec to V.
- self.binding_values.set(index, value);
+ self.binding_values().set(index, value);
// 5. Return unused.
Ok(())
@@ -916,7 +1055,7 @@ impl DeclarativeEnvironment {
}
if self.binding_is_mutable(index) {
- self.binding_values.set(index, value);
+ self.binding_values().set(index, value);
} else if strict {
return vm.throw_completion(ErrorKind::TypeError, ErrorType::InvalidAssignToConst, &[]);
}
@@ -976,11 +1115,11 @@ impl DeclarativeEnvironment {
);
}
- Ok(self.binding_values.get(index))
+ Ok(self.binding_values().get(index))
}
pub fn get_initialized_binding_value_direct(&self, index: usize) -> Value {
- self.binding_values.get(index)
+ self.binding_values().get(index)
}
/// get_binding_value_direct for a binding found outside the environment's own storage.
@@ -1071,7 +1210,7 @@ impl DeclarativeEnvironment {
let Some(shape) = self.shape.get() else {
return false;
};
- let index = self.binding_values.size();
+ let index = self.binding_values().size();
if !shape.has_unique_binding_names
|| index >= shape.size()
|| shape.binding_name(index) != name
@@ -1080,23 +1219,22 @@ impl DeclarativeEnvironment {
{
return false;
}
- if self.binding_values_are_inline.get() && index == self.binding_values.capacity() {
+ if self.binding_values_are_inline() && index == self.binding_values().capacity() {
self.move_binding_values_out_of_cell(index + 1);
}
- self.binding_values.append(Value::EMPTY);
+ self.binding_values().append(Value::EMPTY);
true
}
fn move_binding_values_out_of_cell(&self, needed_capacity: usize) {
- self.binding_values.move_to_owned_storage(needed_capacity);
- self.binding_values_are_inline.set(false);
+ self.binding_values().move_to_owned_storage(needed_capacity);
}
pub fn ensure_capacity(&self, needed_capacity: usize) {
- if needed_capacity > self.binding_values.capacity() && self.binding_values_are_inline.get() {
+ if needed_capacity > self.binding_values().capacity() && self.binding_values_are_inline() {
self.move_binding_values_out_of_cell(needed_capacity);
}
- self.binding_values.ensure_capacity(needed_capacity);
+ self.binding_values().ensure_capacity(needed_capacity);
if self.shape.get().is_some() || needed_capacity == 0 {
return;
}
@@ -1111,8 +1249,8 @@ impl DeclarativeEnvironment {
}
pub fn shrink_to_fit(&self) {
- if !self.binding_values_are_inline.get() {
- self.binding_values.shrink_to_fit();
+ if !self.binding_values_are_inline() {
+ self.binding_values().shrink_to_fit();
}
let Some(rare_data) = self.rare_data() else {
@@ -1122,7 +1260,7 @@ impl DeclarativeEnvironment {
rare_data.storage.binding_names.borrow_mut().shrink_to_fit();
rare_data.binding_flags.shrink_to_fit();
- if self.binding_values.is_empty() {
+ if self.binding_values().is_empty() {
rare_data.storage.deleted_bindings.replace(Bitmap::default());
self.drop_rare_data_if_empty();
return;
@@ -1133,8 +1271,8 @@ impl DeclarativeEnvironment {
return;
}
- let mut deleted_bindings = Bitmap::create(self.binding_values.size(), false);
- for index in 0..self.binding_values.size() {
+ let mut deleted_bindings = Bitmap::create(self.binding_values().size(), false);
+ for index in 0..self.binding_values().size() {
deleted_bindings.set(index, self.binding_is_deleted(index));
}
rare_data.storage.deleted_bindings.replace(deleted_bindings);
diff --git a/Libraries/LibJS/Rust/src/runtime/environment.rs b/Libraries/LibJS/Rust/src/runtime/environment.rs
index 0af98220c7c..6b126078781 100644
--- a/Libraries/LibJS/Rust/src/runtime/environment.rs
+++ b/Libraries/LibJS/Rust/src/runtime/environment.rs
@@ -130,7 +130,7 @@ impl Environment {
this_binding_status: (ThisBindingStatus::Uninitialized as u8).into(),
permanently_screwed_by_eval: false.into(),
declarative: is_declarative.into(),
- binding_values_are_inline: false.into(),
+ inline_binding_capacity: 0.into(),
}
}
diff --git a/Tests/LibJS/Runtime/functions/function-environment-many-bindings.js b/Tests/LibJS/Runtime/functions/function-environment-many-bindings.js
new file mode 100644
index 00000000000..316c64077ba
--- /dev/null
+++ b/Tests/LibJS/Runtime/functions/function-environment-many-bindings.js
@@ -0,0 +1,54 @@
+test("a hot function keeps more bindings than fit in its environment's cell", () => {
+ function many(value) {
+ var a0 = value + 0,
+ a1 = value + 1,
+ a2 = value + 2,
+ a3 = value + 3,
+ a4 = value + 4,
+ a5 = value + 5;
+ var a6 = value + 6,
+ a7 = value + 7,
+ a8 = value + 8,
+ a9 = value + 9,
+ a10 = value + 10,
+ a11 = value + 11;
+ var a12 = value + 12,
+ a13 = value + 13,
+ a14 = value + 14,
+ a15 = value + 15,
+ a16 = value + 16;
+ var a17 = value + 17,
+ a18 = value + 18,
+ a19 = value + 19;
+ return eval("a0 + a10 + a19");
+ }
+
+ for (let i = 0; i < 5000; ++i) expect(many(i)).toBe(3 * i + 29);
+});
+
+test("a binding created by a direct eval joins the many others", () => {
+ function many() {
+ var a0 = 0,
+ a1 = 1,
+ a2 = 2,
+ a3 = 3,
+ a4 = 4,
+ a5 = 5,
+ a6 = 6,
+ a7 = 7,
+ a8 = 8,
+ a9 = 9;
+ var a10 = 10,
+ a11 = 11,
+ a12 = 12,
+ a13 = 13,
+ a14 = 14,
+ a15 = 15,
+ a16 = 16,
+ a17 = 17;
+ eval("var added = a17 + 1;");
+ return added;
+ }
+
+ for (let i = 0; i < 5000; ++i) expect(many()).toBe(18);
+});