Skip to main content

gc/
header.rs

1/// GC object type tags, adapted from QuickJS `JSGCObjectTypeEnum`.
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum GcObjectType {
4    MonkeyObject,
5    FunctionBytecode,
6    Shape,
7    VarRef,
8    AsyncFunction,
9    MonkeyContext,
10}
11
12/// Reentrancy guard during cascade free and cycle removal.
13/// Matches QuickJS `JSGCPhaseEnum`.
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub enum GcPhase {
16    None,
17    Decref,
18    RemoveCycles,
19}
20
21/// Which intrusive GC list currently owns an object. Each object is on at most one list.
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum GcListKind {
24    GcObj,
25    Tmp,
26    ZeroRef,
27}
28
29/// Header shared by all cycle-GC'd objects.
30/// Matches QuickJS `JSGCObjectHeader`.
31#[derive(Debug, Clone)]
32pub struct GcObjectHeader {
33    pub ref_count: i32,
34    pub gc_obj_type: GcObjectType,
35    /// GC-phase flag (not a permanent mark bit). Set to 1 after `gc_decref` processes the object.
36    pub mark: u8,
37    /// Zombie detection during cycle free, inspired by QuickJS `free_mark`.
38    pub free_mark: bool,
39    pub list_kind: Option<GcListKind>,
40    pub list_prev: Option<GcId>,
41    pub list_next: Option<GcId>,
42}
43
44/// Header for simple refcounted values (strings, bigints, etc.) that are not cycle-collected.
45/// Matches QuickJS `JSRefCountHeader`.
46#[derive(Debug, Clone)]
47pub struct RefCountHeader {
48    pub ref_count: i32,
49}
50
51pub type GcId = usize;
52pub type RefCountId = usize;
53
54impl GcObjectHeader {
55    pub fn new(gc_obj_type: GcObjectType, ref_count: i32) -> Self {
56        GcObjectHeader {
57            ref_count,
58            gc_obj_type,
59            mark: 0,
60            free_mark: false,
61            list_kind: None,
62            list_prev: None,
63            list_next: None,
64        }
65    }
66}
67
68impl RefCountHeader {
69    pub fn new(ref_count: i32) -> Self {
70        RefCountHeader {
71            ref_count,
72        }
73    }
74}