summaryrefslogtreecommitdiffstats
path: root/third_party/rust/jsparagus-stencil/src/gcthings.rs
blob: bbe18fd20f8a68cad9c74a0f19abacace96c9b77 (plain)
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
use crate::regexp::RegExpIndex;
use crate::scope::ScopeIndex;
use crate::script::ScriptStencilIndex;
use ast::source_atom_set::SourceAtomSetIndex;

/// Corresponds to js::frontend::GCThingList::ListType
/// in m-c/js/src/frontend/BytecodeSection.h.
#[derive(Debug)]
pub enum GCThing {
    Null,
    Atom(SourceAtomSetIndex),
    Function(ScriptStencilIndex),
    RegExp(RegExpIndex),
    Scope(ScopeIndex),
}

/// Index into GCThingList.things.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GCThingIndex {
    index: usize,
}

impl GCThingIndex {
    fn new(index: usize) -> Self {
        Self { index }
    }
}

impl From<GCThingIndex> for usize {
    fn from(index: GCThingIndex) -> usize {
        index.index
    }
}

/// List of GC things.
///
/// Maps to JSScript::gcthings() in js/src/vm/JSScript.h.
#[derive(Debug)]
pub struct GCThingList {
    things: Vec<GCThing>,
}

impl GCThingList {
    pub fn new() -> Self {
        Self { things: Vec::new() }
    }

    pub fn push_atom(&mut self, atom_index: SourceAtomSetIndex) -> GCThingIndex {
        let index = self.things.len();
        self.things.push(GCThing::Atom(atom_index));
        GCThingIndex::new(index)
    }

    pub fn push_function(&mut self, fun_index: ScriptStencilIndex) -> GCThingIndex {
        let index = self.things.len();
        self.things.push(GCThing::Function(fun_index));
        GCThingIndex::new(index)
    }

    pub fn push_regexp(&mut self, regexp_index: RegExpIndex) -> GCThingIndex {
        let index = self.things.len();
        self.things.push(GCThing::RegExp(regexp_index));
        GCThingIndex::new(index)
    }

    pub fn push_scope(&mut self, scope_index: ScopeIndex) -> GCThingIndex {
        let index = self.things.len();
        self.things.push(GCThing::Scope(scope_index));
        GCThingIndex::new(index)
    }
}

impl From<GCThingList> for Vec<GCThing> {
    fn from(list: GCThingList) -> Vec<GCThing> {
        list.things
    }
}