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
use std::borrow::Cow;
use jrsonnet_gcmodule::Trace;
use super::{arglike::ArgsLike, parse::parse_builtin_call, CallLocation};
use crate::{error::Result, gc::TraceBox, tb, Context, Val};
pub type BuiltinParamName = Cow<'static, str>;
#[derive(Clone, Trace)]
pub struct BuiltinParam {
pub name: Option<BuiltinParamName>,
pub has_default: bool,
}
pub trait Builtin: Trace {
fn name(&self) -> &str;
fn params(&self) -> &[BuiltinParam];
fn call(&self, ctx: Context, loc: CallLocation<'_>, args: &dyn ArgsLike) -> Result<Val>;
}
pub trait StaticBuiltin: Builtin + Send + Sync
where
Self: 'static,
{
}
#[derive(Trace)]
pub struct NativeCallback {
pub(crate) params: Vec<BuiltinParam>,
handler: TraceBox<dyn NativeCallbackHandler>,
}
impl NativeCallback {
#[deprecated = "prefer using builtins directly, use this interface only for bindings"]
pub fn new(params: Vec<Cow<'static, str>>, handler: impl NativeCallbackHandler) -> Self {
Self {
params: params
.into_iter()
.map(|n| BuiltinParam {
name: Some(n),
has_default: false,
})
.collect(),
handler: tb!(handler),
}
}
}
impl Builtin for NativeCallback {
fn name(&self) -> &str {
"<native>"
}
fn params(&self) -> &[BuiltinParam] {
&self.params
}
fn call(&self, ctx: Context, _loc: CallLocation<'_>, args: &dyn ArgsLike) -> Result<Val> {
let args = parse_builtin_call(ctx, &self.params, args, true)?;
let args = args
.into_iter()
.map(|a| a.expect("legacy natives have no default params"))
.map(|a| a.evaluate())
.collect::<Result<Vec<Val>>>()?;
self.handler.call(&args)
}
}
pub trait NativeCallbackHandler: Trace {
fn call(&self, args: &[Val]) -> Result<Val>;
}