quickjs-oxide-core 0.4.0

The new quickjs-oxide core: 8-byte values, typed arenas and a mark-sweep heap
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Object headers and the kind-specific payload arenas.

use super::arena::{Arena, Record};
use super::values::Range;
use crate::value::{ShapeId, Value};

/// Sentinel for an absent index in a `u32` payload field.
pub const NONE: u32 = u32::MAX;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum ObjectKind {
    Ordinary,
    Array,
    Function,
    /// A closure environment: `slots` holds the variables, `payload` the
    /// parent scope (or [`NONE`]).
    Scope,
    GlobalObject,
    /// An `arguments` object. Its elements hold the values of unmapped indices;
    /// a mapped one's `payload` is a scope object whose slot 0 is the mapped
    /// count and slots 1.. the parameters' cells, `Value::EMPTY` once an index
    /// is unmapped. An unmapped one's payload is [`NONE`].
    Arguments,
    /// A regular expression: `payload` indexes the runtime's program store;
    /// `lastIndex` is an own data property.
    RegExp,
    /// A `for-in` enumeration, never exposed to JavaScript: the elements are
    /// the key snapshot, slot 0 the object, slot 1 the key count, `payload` the
    /// cursor.
    ForInIterator,
    /// A plain kind no builtin allocates: a host function is a
    /// [`ObjectKind::Function`] with a `native` index.
    Native,
    /// `new String(s)`: internal slot 0 the string. Its indices are exotic;
    /// `length` is an own data property.
    StringWrapper,
    /// `new Number(n)`: internal slot 0 the number.
    NumberWrapper,
    /// `new Boolean(b)`: internal slot 0 the boolean.
    BooleanWrapper,
    /// `Object(symbol)`: internal slot 0 the symbol.
    SymbolWrapper,
    /// `Object(bigint)`: internal slot 0 the BigInt.
    BigIntWrapper,
    /// A `Date`: internal slot 0 the time value.
    Date,
    /// A bound function: internal slots the target, the bound `this`, then
    /// the bound arguments.
    BoundFunction,
    /// A proxy: internal slot 0 the target, slot 1 the handler (`null` once
    /// revoked).
    Proxy,
    /// A typed array: internal slot 0 the buffer, the data its view. Canonical
    /// numeric keys are its elements, never properties.
    TypedArray,
    /// A module namespace: internal slot 0 the module, then per export (the
    /// data's sorted keys) its cell or, for a namespace export, its module.
    ModuleNamespace,
    /// An object whose internal slots hold builtin state the natives read:
    /// iterators, promises, collections and the like. `Internal::tag` says
    /// which.
    Builtin,
    /// Poison written over a freed header.
    Free,
}

impl ObjectKind {
    /// Kinds whose `payload` indexes an [`Internal`] record.
    #[inline]
    pub const fn has_internal(self) -> bool {
        matches!(
            self,
            ObjectKind::StringWrapper
                | ObjectKind::NumberWrapper
                | ObjectKind::BooleanWrapper
                | ObjectKind::SymbolWrapper
                | ObjectKind::BigIntWrapper
                | ObjectKind::Date
                | ObjectKind::BoundFunction
                | ObjectKind::Proxy
                | ObjectKind::TypedArray
                | ObjectKind::ModuleNamespace
                | ObjectKind::Builtin
        )
    }

    /// Kinds whose index properties may live in the element range.
    #[inline]
    pub const fn has_elements(self) -> bool {
        matches!(
            self,
            ObjectKind::Ordinary
                | ObjectKind::Array
                | ObjectKind::Function
                | ObjectKind::Arguments
                | ObjectKind::RegExp
                | ObjectKind::NumberWrapper
                | ObjectKind::BooleanWrapper
                | ObjectKind::SymbolWrapper
                | ObjectKind::BigIntWrapper
                | ObjectKind::Date
                | ObjectKind::BoundFunction
                | ObjectKind::Builtin
                | ObjectKind::GlobalObject
        )
    }

    /// Kinds whose internal methods are not the ordinary ones for every key:
    /// property lookups that meet one leave the heap-only fast paths for
    /// `vm::property`'s dispatch.
    #[inline]
    pub const fn is_exotic(self) -> bool {
        matches!(
            self,
            ObjectKind::StringWrapper
                | ObjectKind::Proxy
                | ObjectKind::TypedArray
                | ObjectKind::ModuleNamespace
        )
    }
}

pub mod flags {
    pub const EXTENSIBLE: u8 = 1 << 0;
    pub const USED_AS_PROTOTYPE: u8 = 1 << 1;
    pub const CALLABLE: u8 = 1 << 2;
    pub const CONSTRUCTOR: u8 = 1 << 3;
    pub const DICTIONARY: u8 = 1 << 4;
    /// An error object (`new_error` sets it), so `Object.prototype.toString`
    /// and the error checks need no prototype walk.
    pub const ERROR: u8 = 1 << 5;
    /// Index properties live as named properties only: the element range is
    /// empty and stays so. Set when an element gets attributes other than
    /// writable, enumerable, configurable, or the object stops being
    /// extensible.
    pub const SPARSE: u8 = 1 << 6;
    /// An array whose `length` is not writable.
    pub const LENGTH_READONLY: u8 = 1 << 7;
}

/// One fixed-size record per object (20 bytes).
#[derive(Clone, Copy, Debug)]
pub struct ObjectHeader {
    pub shape: ShapeId,
    pub slots_start: u32,
    pub elements_start: u32,
    /// Index into the kind's side arena, or an inline payload: the array
    /// length, the parent scope.
    pub payload: u32,
    pub slots_log: u8,
    pub elements_log: u8,
    pub kind: ObjectKind,
    pub flags: u8,
}

const _: () = assert!(size_of::<ObjectHeader>() <= 24);

impl ObjectHeader {
    pub fn new(
        kind: ObjectKind,
        shape: ShapeId,
        slots: Range,
        elements: Range,
        payload: u32,
        flags: u8,
    ) -> Self {
        ObjectHeader {
            shape,
            slots_start: slots.start,
            elements_start: elements.start,
            payload,
            slots_log: slots.exponent(),
            elements_log: elements.log(),
            kind,
            flags,
        }
    }

    /// The named property slots: always a power of two
    /// ([`ValueHeap::alloc_slots`](super::values::ValueHeap::alloc_slots)).
    #[inline]
    pub fn slots(&self) -> Range {
        Range::from_exponent(self.slots_start, self.slots_log)
    }

    #[inline]
    pub fn set_slots(&mut self, range: Range) {
        debug_assert!(range.len == 0 || range.len.is_power_of_two());
        self.slots_start = range.start;
        self.slots_log = range.exponent();
    }

    #[inline]
    pub fn elements(&self) -> Range {
        Range::from_log(self.elements_start, self.elements_log)
    }

    #[inline]
    pub fn set_elements(&mut self, range: Range) {
        self.elements_start = range.start;
        self.elements_log = range.log();
    }

    #[inline]
    pub fn has(&self, flag: u8) -> bool {
        self.flags & flag != 0
    }
}

impl Record for ObjectHeader {
    fn vacant() -> Self {
        ObjectHeader::new(
            ObjectKind::Free,
            ShapeId(NONE),
            Range::EMPTY,
            Range::EMPTY,
            NONE,
            0,
        )
    }
}

/// The payload of a `Function` object.
#[derive(Clone, Copy, Debug)]
pub struct FunctionRecord {
    /// The captured scope object, or [`NONE`].
    pub scope: u32,
    /// A [`FunctionBytecode`] index, or [`NONE`].
    pub bytecode: u32,
    /// A [`Native`](crate::vm::native::Native) index for a host function, or
    /// [`NONE`].
    pub native: u32,
    /// The home object of a method that uses `super`, or [`NONE`].
    pub home_object: u32,
    /// A class constructor's instance-field initializer function, or
    /// [`NONE`].
    pub fields: u32,
}

impl Record for FunctionRecord {
    fn vacant() -> Self {
        FunctionRecord {
            scope: NONE,
            bytecode: NONE,
            native: NONE,
            home_object: NONE,
            fields: NONE,
        }
    }
}

/// The heap side of one published code unit, shared by every closure over
/// it: the constant table, and the records of the unit's children (as
/// `Int32` record indices) so that a live function keeps every function it
/// can create alive. Both are value-heap ranges, traced like everything else.
#[derive(Clone, Copy, Debug)]
pub struct FunctionBytecode {
    /// The [`CodeUnitId`](crate::bytecode::CodeUnitId) index.
    pub unit: u32,
    pub constants: Range,
    pub constant_count: u32,
    pub children: Range,
    pub child_count: u32,
}

impl Record for FunctionBytecode {
    fn vacant() -> Self {
        FunctionBytecode {
            unit: NONE,
            constants: Range::EMPTY,
            constant_count: 0,
            children: Range::EMPTY,
            child_count: 0,
        }
    }
}

/// The internal state of an object of a kind with [`ObjectKind::has_internal`]:
/// internal slots, traced like properties, and data that holds no values.
#[derive(Debug)]
pub struct Internal {
    /// What the slots mean, for [`ObjectKind::Builtin`] objects.
    pub tag: InternalTag,
    pub slots: Vec<Value>,
    pub data: InternalData,
}

/// Which builtin an [`ObjectKind::Builtin`] object belongs to.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum InternalTag {
    None,
    ArrayIterator,
    StringIterator,
    MapIterator,
    SetIterator,
    RegExpStringIterator,
    IteratorHelper,
    WrapForValidIterator,
    AsyncFromSyncIterator,
    Promise,
    /// A `then` record: the capability's promise, resolve and reject, the two
    /// handlers, and the async frame an `await` resumes.
    PromiseReaction,
    Map,
    Set,
    WeakMap,
    WeakSet,
    WeakRef,
    FinalizationRegistry,
    Generator,
    AsyncGenerator,
    AsyncFunction,
    ArrayBuffer,
    SharedArrayBuffer,
    TypedArray,
    DataView,
    ModuleNamespace,
    /// A closure environment for a native function (`FunctionRecord::scope`
    /// of a native): its slots are the native's captured values.
    NativeData,
    /// Test262's `$262.IsHTMLDDA`: falsy, `typeof` "undefined", loosely equal
    /// to `null` and `undefined`, and callable (returning `null`).
    HtmlDda,
    /// A `JSON.rawJSON` object (its brand).
    RawJson,
    /// A module record.
    Module,
    /// An object the `Iterator` constructor made for a subclass: QuickJS's
    /// `JS_CLASS_ITERATOR`, which its printer names.
    Iterator,
    /// An embedder's host object: its datum is `InternalData::HostData`.
    HostData,
}

/// Non-value state of an internal record.
#[derive(Debug, Default)]
pub enum InternalData {
    #[default]
    None,
    Bytes(Vec<u8>),
    /// A suspended generator or async frame; its registers are the record's
    /// slots from [`FRAME_SLOTS`].
    Frame(SavedFrame),
    /// A collection's index; its entries are the record's slots.
    Table(Box<super::table::Table>),
    /// A collection iterator's position, moved by compactions.
    Cursor(std::rc::Rc<std::cell::Cell<u32>>),
    /// An `ArrayBuffer` or `SharedArrayBuffer`.
    Buffer(Box<super::buffer::Buffer>),
    /// A typed array's or `DataView`'s window; slot 0 is the buffer.
    View(super::buffer::View),
    /// A module record's state.
    Module(Box<crate::vm::module::ModuleData>),
    /// A module namespace's export names, sorted.
    Exports(Box<[super::atom::PropertyKey]>),
    /// A host function's body (`InternalTag::NativeData` of a host
    /// closure); the record's slots are the values it captured.
    HostFn(crate::vm::native::HostBody),
    /// An embedder's value attached to a host object
    /// (`InternalTag::HostData`).
    HostData(std::rc::Rc<dyn std::any::Any>),
}

/// The internal slots of a suspended activation before its registers: the
/// function, then the kind's own (an async function's promise, an async
/// generator's request queue).
pub const FRAME_SLOTS: usize = 2;

/// The header of a suspended frame: everything `Frame` holds that is not a
/// heap reference (the function is internal slot 0) or a position in the
/// register file.
#[derive(Clone, Copy, Debug)]
pub struct SavedFrame {
    pub unit: u32,
    pub bytecode: u32,
    /// Where it resumes: the instruction after its suspension point.
    pub pc: u32,
    pub argc: u32,
    /// The activation's state (`vm::suspend`'s constants).
    pub state: u8,
}

impl Record for Internal {
    fn vacant() -> Self {
        Internal {
            tag: InternalTag::None,
            slots: Vec::new(),
            data: InternalData::None,
        }
    }
}

impl Internal {
    /// Bytes owned outside the arena: charged to the trigger by the
    /// allocating calls that grow them, not tracked by the arena (the slots
    /// change size after allocation).
    pub fn owned_bytes(&self) -> usize {
        self.slots.capacity() * size_of::<Value>()
            + match &self.data {
                InternalData::None
                | InternalData::Frame(_)
                | InternalData::Cursor(_)
                | InternalData::View(_)
                | InternalData::Module(_)
                | InternalData::HostFn(_)
                | InternalData::HostData(_) => 0,
                InternalData::Exports(keys) => keys.len() * 8,
                InternalData::Buffer(buffer) => buffer.owned_bytes(),
                InternalData::Bytes(bytes) => bytes.capacity(),
                InternalData::Table(table) => table.owned_bytes(),
            }
    }
}

#[derive(Default)]
pub struct PayloadArenas {
    pub functions: Arena<FunctionRecord>,
    pub bytecode: Arena<FunctionBytecode>,
    pub internals: Arena<Internal>,
}