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
use crate::{
exec::Interpreter,
js::{
object::{ObjectData, Property},
value::{to_value, ResultValue, Value, ValueData},
},
syntax::ast::expr::Expr,
};
use gc::{custom_trace, Gc};
use gc_derive::{Finalize, Trace};
use std::fmt::{self, Debug};
pub type NativeFunctionData = fn(&Value, &[Value], &Interpreter) -> ResultValue;
#[derive(Trace, Finalize, Debug, Clone)]
pub enum Function {
NativeFunc(NativeFunction),
RegularFunc(RegularFunction),
}
#[derive(Trace, Finalize, Debug, Clone)]
pub struct RegularFunction {
pub object: ObjectData,
pub expr: Expr,
pub args: Vec<String>,
}
impl RegularFunction {
pub fn new(expr: Expr, args: Vec<String>) -> Self {
let mut object = ObjectData::default();
object.properties.insert(
"arguments".to_string(),
Property::new(Gc::new(ValueData::Integer(args.len() as i32))),
);
Self { object, expr, args }
}
}
#[derive(Finalize, Clone)]
pub struct NativeFunction {
pub object: ObjectData,
pub data: NativeFunctionData,
}
impl NativeFunction {
pub fn new(data: NativeFunctionData) -> Self {
let object = ObjectData::default();
Self { object, data }
}
}
impl Debug for NativeFunction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({:?})", self.object)
}
}
unsafe impl gc::Trace for NativeFunction {
custom_trace!(this, mark(&this.object));
}
pub fn _create() -> Value {
let function: ObjectData = ObjectData::default();
to_value(function)
}
pub fn init(global: &Value) {
let global_ptr = global;
global_ptr.set_field_slice("Function", _create());
}