1use crate::runtime::context::JSContext;
2use crate::value::JSValue;
3
4pub type HostFunc = fn(&mut JSContext, &[JSValue]) -> JSValue;
5
6#[derive(Clone, Copy)]
7pub struct HostFunction {
8 pub func: HostFunc,
9 pub name: &'static str,
10 pub arity: u32,
11 pub needs_this: bool,
12 pub is_constructor: bool,
13}
14
15impl HostFunction {
16 pub fn new(name: &'static str, arity: u32, func: HostFunc) -> Self {
17 HostFunction {
18 name,
19 arity,
20 func,
21 needs_this: false,
22 is_constructor: false,
23 }
24 }
25
26 pub fn method(name: &'static str, arity: u32, func: HostFunc) -> Self {
27 HostFunction {
28 name,
29 arity,
30 func,
31 needs_this: true,
32 is_constructor: false,
33 }
34 }
35
36 pub fn ctor(name: &'static str, arity: u32, func: HostFunc) -> Self {
37 HostFunction {
38 name,
39 arity,
40 func,
41 needs_this: false,
42 is_constructor: true,
43 }
44 }
45
46 pub fn call(&self, ctx: &mut JSContext, args: &[JSValue]) -> JSValue {
47 (self.func)(ctx, args)
48 }
49}