1 use crate::regexp::RegExpIndex; 2 use crate::scope::ScopeIndex; 3 use crate::script::ScriptStencilIndex; 4 use ast::source_atom_set::SourceAtomSetIndex; 5 6 /// Corresponds to js::frontend::GCThingList::ListType 7 /// in m-c/js/src/frontend/BytecodeSection.h. 8 #[derive(Debug)] 9 pub enum GCThing { 10 Null, 11 Atom(SourceAtomSetIndex), 12 Function(ScriptStencilIndex), 13 RegExp(RegExpIndex), 14 Scope(ScopeIndex), 15 } 16 17 /// Index into GCThingList.things. 18 #[derive(Debug, Clone, Copy, PartialEq)] 19 pub struct GCThingIndex { 20 index: usize, 21 } 22 23 impl GCThingIndex { new(index: usize) -> Self24 fn new(index: usize) -> Self { 25 Self { index } 26 } 27 } 28 29 impl From<GCThingIndex> for usize { from(index: GCThingIndex) -> usize30 fn from(index: GCThingIndex) -> usize { 31 index.index 32 } 33 } 34 35 /// List of GC things. 36 /// 37 /// Maps to JSScript::gcthings() in js/src/vm/JSScript.h. 38 #[derive(Debug)] 39 pub struct GCThingList { 40 things: Vec<GCThing>, 41 } 42 43 impl GCThingList { new() -> Self44 pub fn new() -> Self { 45 Self { things: Vec::new() } 46 } 47 push_atom(&mut self, atom_index: SourceAtomSetIndex) -> GCThingIndex48 pub fn push_atom(&mut self, atom_index: SourceAtomSetIndex) -> GCThingIndex { 49 let index = self.things.len(); 50 self.things.push(GCThing::Atom(atom_index)); 51 GCThingIndex::new(index) 52 } 53 push_function(&mut self, fun_index: ScriptStencilIndex) -> GCThingIndex54 pub fn push_function(&mut self, fun_index: ScriptStencilIndex) -> GCThingIndex { 55 let index = self.things.len(); 56 self.things.push(GCThing::Function(fun_index)); 57 GCThingIndex::new(index) 58 } 59 push_regexp(&mut self, regexp_index: RegExpIndex) -> GCThingIndex60 pub fn push_regexp(&mut self, regexp_index: RegExpIndex) -> GCThingIndex { 61 let index = self.things.len(); 62 self.things.push(GCThing::RegExp(regexp_index)); 63 GCThingIndex::new(index) 64 } 65 push_scope(&mut self, scope_index: ScopeIndex) -> GCThingIndex66 pub fn push_scope(&mut self, scope_index: ScopeIndex) -> GCThingIndex { 67 let index = self.things.len(); 68 self.things.push(GCThing::Scope(scope_index)); 69 GCThingIndex::new(index) 70 } 71 } 72 73 impl From<GCThingList> for Vec<GCThing> { from(list: GCThingList) -> Vec<GCThing>74 fn from(list: GCThingList) -> Vec<GCThing> { 75 list.things 76 } 77 } 78