1use crate::{
2 ClassHandle, ClosureHandle, FunctionHandle, HashMapHandle, NativeFunctionError, Value, Vm,
3 backend::chunk::Chunk,
4 memory::{INLINE_UPVALUE_COUNT, OverflowHandle, StringHandle},
5};
6
7pub use crate::memory::UpvalueSlot;
9
10#[derive(Debug, Clone, Default, PartialEq)]
11pub struct Upvalue {
12 pub value: Value,
13 pub is_marked: bool,
14}
15
16#[derive(Debug, Clone, Default, PartialEq)]
17pub struct FunctionObject {
18 pub arity: usize,
19 pub name: StringHandle,
20 pub chunk: Chunk,
21 pub upvalue_count: usize,
22}
23
24impl FunctionObject {
25 pub fn new(name: StringHandle, arity: usize) -> Self {
26 Self {
27 name,
28 arity,
29 chunk: Chunk::new(),
30 upvalue_count: 0,
31 }
32 }
33}
34
35#[derive(Debug, Clone, PartialEq)]
36pub struct ClosureObject {
37 pub function: FunctionHandle,
38 pub upvalue_count: usize,
39 pub inline_upvalues: [UpvalueSlot; INLINE_UPVALUE_COUNT],
40 pub overflow_handle: Option<OverflowHandle>,
41 pub module_context: Option<HashMapHandle>,
42 pub is_marked: bool,
43}
44
45impl ClosureObject {
46 pub fn new(
47 function: FunctionHandle,
48 upvalue_count: usize,
49 module_context: Option<HashMapHandle>,
50 ) -> Self {
51 Self {
52 function,
53 upvalue_count,
54 inline_upvalues: [UpvalueSlot::default(); INLINE_UPVALUE_COUNT],
55 overflow_handle: None,
56 module_context,
57 is_marked: false,
58 }
59 }
60}
61
62#[derive(Debug, Clone, Default, PartialEq)]
63pub struct ClassObject {
64 pub name: StringHandle,
65 pub super_clazz: Option<ClassHandle>,
66 pub method_table: HashMapHandle,
67 pub value_table: HashMapHandle,
68 pub is_marked: bool,
69}
70
71#[derive(Debug, Clone, Default, PartialEq)]
72pub struct InstanceObject {
73 pub clazz: ClassHandle,
74 pub table: HashMapHandle,
75 pub is_marked: bool,
76}
77
78#[derive(Debug, Clone, Default, PartialEq)]
79pub struct BoundMethodObject {
80 pub receiver: Value,
81 pub closure: ClosureHandle,
82 pub is_marked: bool,
83}
84
85impl BoundMethodObject {
86 pub fn new(receiver: Value, closure: ClosureHandle) -> Self {
87 Self {
88 receiver,
89 closure,
90 is_marked: false,
91 }
92 }
93}
94
95pub type NativeFn = fn(arity: usize, vm: &mut Vm) -> Result<Option<Value>, NativeFunctionError>;
96
97#[derive(Debug, Clone)]
98pub struct NativeFunctionObject {
99 pub function: NativeFn,
100 pub arity: usize,
101 pub name_handle: StringHandle,
102}
103
104pub type IntrinsicFn =
105 fn(receiver: Value, arity: usize, vm: &mut Vm) -> Result<Option<Value>, NativeFunctionError>;
106
107#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
108pub enum IntrinsicKind {
109 String(StringHandle),
110 Number(StringHandle),
111 Array(StringHandle),
112 Function(StringHandle),
113}
114
115#[derive(Debug, Clone, Copy)]
116pub enum IntrinsicMethod {
117 Native { function: IntrinsicFn, arity: usize },
118 Call,
119 Apply,
120 NilSafeCall,
121 NilSafeApply,
122 Iter,
123 ToNumber,
124}
125
126#[derive(Debug, Clone)]
127pub struct BoundIntrinsicObject {
128 pub receiver: Value,
129 pub method: IntrinsicMethod,
130 pub name_handle: StringHandle,
131 pub is_marked: bool,
132}
133
134impl BoundIntrinsicObject {
135 pub fn new(receiver: Value, method: IntrinsicMethod, name_handle: StringHandle) -> Self {
136 Self {
137 receiver,
138 method,
139 name_handle,
140 is_marked: false,
141 }
142 }
143}