Skip to content

Truly independent web browser

6.1 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

use core::cell::Cell;

use super::buffer::{InterpreterBuffer, ThinSlice};
use super::cell::{CellHeader, Gc};
use super::function_object::FunctionObject;
use super::member::NullableMember;
use super::object::Object;
use super::shape::{Shape, WatchpointSet};
use super::value::Value;
use crate::layout_forward::ObjectPropertyIteratorCacheDataStorage;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u32)]
pub enum PropertyLookupCacheEntryType {
    Empty,
    AddOwnProperty,
    ChangeOwnProperty,
    GetOwnProperty,
    ChangePropertyInPrototypeChain,
    GetPropertyInPrototypeChain,
    GetMissingProperty,
    /// Only in the VM's keyed property lookup cache: objects of the shape whose own string-keyed properties are those
    /// of the shape have no own property of the name, whatever their prototypes have. Own property checks
    /// (`hasOwnProperty`, `in`) look for these; property gets ignore them.
    MissingOwnProperty,
}

/// One cached property access. The shapes and objects are not traced; the VM's sweep callback clears entries whose
/// cells did not survive a collection.
#[repr(C)]
pub struct PropertyLookupCacheEntry {
    pub entry_type: Cell<PropertyLookupCacheEntryType>,
    pub property_offset: Cell<u32>,
    pub shape_dictionary_generation: Cell<u32>,
    pub writes_data_property: Cell<bool>,
    pub new_shape: Cell<Option<Gc<Shape>>>,
    pub shape: Cell<Option<Gc<Shape>>>,
    pub prototype: Cell<Option<Gc<Object>>>,
    pub prototype_chain_validity: Cell<Option<Gc<WatchpointSet>>>,
    /// The encoded string or symbol Value whose property this entry describes, for the caches of keyed accesses like
    /// GetByValue, or 0 for the caches of named accesses like GetById, whose instruction determines the property. Like
    /// the cells above, the key is not kept alive by the cache.
    pub key: Cell<u64>,
    /// For entries of named accesses that get an accessor property, a getter the interpreter called through the entry.
    /// For an own property, it is the last one, for JIT code to speculate on. A DirectGetterFunction is only recorded
    /// once it returned for an object of the entry's shape. Not kept alive either, and forgotten when it dies. It also
    /// makes entries 64 bytes, so that JIT code finds an entry in the megamorphic tables by masking bits of its hash
    /// (see megamorphic_hash() of the bytecode executable).
    pub getter: Cell<Option<Gc<FunctionObject>>>,
}

const _: () = assert!(size_of::<PropertyLookupCacheEntry>() == 64);

/// The layout of an entry of the VM's keyed property lookup cache (KeyedPropertyLookupCacheEntry of the bytecode
/// executable, which asserts that it matches), for the interpreter, which looks up own data properties in it.
#[repr(C, align(64))]
pub struct KeyedPropertyLookupCacheEntryLayout {
    pub entry_type: PropertyLookupCacheEntryType,
    pub property_offset: u32,
    pub shape_dictionary_generation: u32,
    pub shape: Option<Gc<Shape>>,
    pub prototype: Option<Gc<Object>>,
    pub prototype_chain_validity: Option<Gc<WatchpointSet>>,
    pub new_shape: Option<Gc<Shape>>,
    /// The identity of the name of the property, the word of a fly string, or 0.
    pub property_name: u64,
}

const _: () = assert!(size_of::<KeyedPropertyLookupCacheEntryLayout>() == 64);

/// How the interpreter finds the entry of the keyed property lookup cache that may hold a lookup: the top this many bits
/// of the 32-bit product of this multiplier and the low 32 bits of the shape's address XORed with the name's identity
/// (see KeyedPropertyLookupCache::entry_index_for() of the bytecode executable, which asserts that these match).
pub const KEYED_PROPERTY_LOOKUP_CACHE_INTERPRETER_INDEX_BITS: u32 = 11;
pub const KEYED_PROPERTY_LOOKUP_CACHE_INTERPRETER_HASH_MULTIPLIER: u32 = 0x9e37_79b9;

/// A tagged pointer to the entries of a property lookup cache; the interpreter only ever consults the first entry.
#[repr(C)]
pub struct PropertyLookupCache {
    pub data: Cell<usize>,
}

pub const PROPERTY_LOOKUP_CACHE_DATA_TAG_MASK: usize = 3;

/// The tag of a polymorphic cache's data, whose entries are an array the interpreter probes.
pub const PROPERTY_LOOKUP_CACHE_POLYMORPHIC_DATA_TAG: usize = 1;

/// The data of a keyed generic cache: the tag without data (see PropertyLookupCache::is_keyed_generic()).
pub const PROPERTY_LOOKUP_CACHE_KEYED_GENERIC_DATA: usize = 3;

#[repr(C)]
pub struct GlobalVariableCache {
    pub entry: PropertyLookupCacheEntry,
    pub environment_serial_number: Cell<u64>,
    pub environment_binding_index: Cell<u32>,
    pub has_environment_binding_index: Cell<bool>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum ObjectPropertyIteratorFastPath {
    None,
    PlainNamed,
    PackedIndexed,
}

#[repr(C)]
pub struct ObjectPropertyIteratorCacheData {
    pub header: CellHeader,
    pub shape_is_dictionary: Cell<bool>,
    pub fast_path: Cell<ObjectPropertyIteratorFastPath>,
    pub receiver_has_magical_length: Cell<bool>,
    pub indexed_property_count: Cell<u32>,
    pub shape_dictionary_generation: Cell<u32>,
    pub shape: NullableMember<Shape>,
    pub prototype_chain_validity: NullableMember<WatchpointSet>,
    pub property_values: InterpreterBuffer<Value>,
    /// For each key, the offset of the receiver's own property it names in the named property storage of objects of
    /// the shape, or `u32::MAX` where it names none (an index, or a key of the prototype chain). Empty where the shape
    /// is a dictionary, which can change in place. JIT code reads `object[key]` and `object.hasOwnProperty(key)` for
    /// the key at an index of the loop through it, for objects of the shape.
    pub property_offsets: ThinSlice<u32>,
    pub storage: ObjectPropertyIteratorCacheDataStorage,
}

#[repr(C)]
pub struct ObjectPropertyIteratorCache {
    pub data: Cell<Option<Gc<ObjectPropertyIteratorCacheData>>>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct EnvironmentCoordinate {
    pub hops: u32,
    pub index: u32,
}

impl EnvironmentCoordinate {
    pub const INVALID_MARKER: u32 = 0xffff_fffe;
}