difftreelog
refactor fancier builtin param type
in: master
5 files changed
bindings/jsonnet/src/native.rsdiffbeforeafterboth1use std::{2 borrow::Cow,3 ffi::{c_void, CStr},4 os::raw::{c_char, c_int},5};67use jrsonnet_evaluator::{8 error::{Error, ErrorKind},9 function::builtin::{NativeCallback, NativeCallbackHandler},10 typed::Typed,11 IStr, Val,12};1314use crate::VM;1516/// The returned `JsonnetJsonValue*` should be allocated with `jsonnet_realloc`. It will be cleaned up17/// along with the objects rooted at `argv` by `libjsonnet` when no-longer needed. Return a string upon18/// failure, which will appear in Jsonnet as an error. The `argv` pointer is an array whose size19/// matches the array of parameters supplied when the native callback was originally registered.20///21/// - `ctx` User pointer, given in jsonnet_native_callback.22/// - `argv` Array of arguments from Jsonnet code.23/// - `param` success Set this byref param to 1 to indicate success and 0 for failure.24/// Returns the content of the imported file, or an error message.25type JsonnetNativeCallback = unsafe extern "C" fn(26 ctx: *const c_void,27 argv: *const *const Val,28 success: *mut c_int,29) -> *mut Val;3031#[derive(jrsonnet_gcmodule::Trace)]32struct JsonnetNativeCallbackHandler {33 #[trace(skip)]34 ctx: *const c_void,35 #[trace(skip)]36 cb: JsonnetNativeCallback,37}38impl NativeCallbackHandler for JsonnetNativeCallbackHandler {39 fn call(&self, args: &[Val]) -> Result<Val, Error> {40 let mut n_args = Vec::new();41 for a in args {42 n_args.push(Some(Box::new(a.clone())));43 }44 n_args.push(None);45 let mut success = 1;46 let v = unsafe { (self.cb)(self.ctx, n_args.as_ptr().cast(), &mut success) };47 let v = unsafe { *Box::from_raw(v) };48 if success == 1 {49 Ok(v)50 } else {51 let e = IStr::from_untyped(v).expect("error msg should be a string");52 Err(ErrorKind::RuntimeError(e).into())53 }54 }55}5657/// Callback to provide native extensions to Jsonnet.58///59/// # Safety60///61/// `vm` should be a vm allocated by `jsonnet_make`62/// `name` should be a NUL-terminated string63/// `cb` should be a function pointer64/// `raw_params` should point to a NULL-terminated array of NUL-terminated strings65#[no_mangle]66pub unsafe extern "C" fn jsonnet_native_callback(67 vm: &VM,68 name: *const c_char,69 cb: JsonnetNativeCallback,70 ctx: *const c_void,71 mut raw_params: *const *const c_char,72) {73 let name = CStr::from_ptr(name)74 .to_str()75 .expect("name is not utf-8")76 .into();77 let mut params = Vec::new();78 loop {79 if (*raw_params).is_null() {80 break;81 }82 let param = CStr::from_ptr(*raw_params)83 .to_str()84 .expect("param name is not utf-8");85 params.push(Cow::Owned(param.into()));86 raw_params = raw_params.offset(1);87 }8889 let any_resolver = vm.state.context_initializer();90 any_resolver91 .as_any()92 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()93 .expect("only stdlib context initializer supported")94 .add_native(95 name,96 #[allow(deprecated)]97 NativeCallback::new(params, JsonnetNativeCallbackHandler { ctx, cb }),98 )99}1use std::{2 ffi::{c_void, CStr},3 os::raw::{c_char, c_int},4};56use jrsonnet_evaluator::{7 error::{Error, ErrorKind},8 function::builtin::{NativeCallback, NativeCallbackHandler},9 typed::Typed,10 IStr, Val,11};1213use crate::VM;1415/// The returned `JsonnetJsonValue*` should be allocated with `jsonnet_realloc`. It will be cleaned up16/// along with the objects rooted at `argv` by `libjsonnet` when no-longer needed. Return a string upon17/// failure, which will appear in Jsonnet as an error. The `argv` pointer is an array whose size18/// matches the array of parameters supplied when the native callback was originally registered.19///20/// - `ctx` User pointer, given in jsonnet_native_callback.21/// - `argv` Array of arguments from Jsonnet code.22/// - `param` success Set this byref param to 1 to indicate success and 0 for failure.23/// Returns the content of the imported file, or an error message.24type JsonnetNativeCallback = unsafe extern "C" fn(25 ctx: *const c_void,26 argv: *const *const Val,27 success: *mut c_int,28) -> *mut Val;2930#[derive(jrsonnet_gcmodule::Trace)]31struct JsonnetNativeCallbackHandler {32 #[trace(skip)]33 ctx: *const c_void,34 #[trace(skip)]35 cb: JsonnetNativeCallback,36}37impl NativeCallbackHandler for JsonnetNativeCallbackHandler {38 fn call(&self, args: &[Val]) -> Result<Val, Error> {39 let mut n_args = Vec::new();40 for a in args {41 n_args.push(Some(Box::new(a.clone())));42 }43 n_args.push(None);44 let mut success = 1;45 let v = unsafe { (self.cb)(self.ctx, n_args.as_ptr().cast(), &mut success) };46 let v = unsafe { *Box::from_raw(v) };47 if success == 1 {48 Ok(v)49 } else {50 let e = IStr::from_untyped(v).expect("error msg should be a string");51 Err(ErrorKind::RuntimeError(e).into())52 }53 }54}5556/// Callback to provide native extensions to Jsonnet.57///58/// # Safety59///60/// `vm` should be a vm allocated by `jsonnet_make`61/// `name` should be a NUL-terminated string62/// `cb` should be a function pointer63/// `raw_params` should point to a NULL-terminated array of NUL-terminated strings64#[no_mangle]65pub unsafe extern "C" fn jsonnet_native_callback(66 vm: &VM,67 name: *const c_char,68 cb: JsonnetNativeCallback,69 ctx: *const c_void,70 mut raw_params: *const *const c_char,71) {72 let name = CStr::from_ptr(name)73 .to_str()74 .expect("name is not utf-8")75 .into();76 let mut params = Vec::new();77 loop {78 if (*raw_params).is_null() {79 break;80 }81 let param = CStr::from_ptr(*raw_params)82 .to_str()83 .expect("param name is not utf-8");84 params.push(param.into());85 raw_params = raw_params.offset(1);86 }8788 let any_resolver = vm.state.context_initializer();89 any_resolver90 .as_any()91 .downcast_ref::<jrsonnet_stdlib::ContextInitializer>()92 .expect("only stdlib context initializer supported")93 .add_native(94 name,95 #[allow(deprecated)]96 NativeCallback::new(params, JsonnetNativeCallbackHandler { ctx, cb }),97 )98}crates/jrsonnet-evaluator/src/function/builtin.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/builtin.rs
+++ b/crates/jrsonnet-evaluator/src/function/builtin.rs
@@ -1,18 +1,56 @@
use std::{any::Any, borrow::Cow};
use jrsonnet_gcmodule::Trace;
+use jrsonnet_interner::IStr;
use super::{arglike::ArgsLike, parse::parse_builtin_call, CallLocation};
use crate::{error::Result, gc::TraceBox, tb, Context, Val};
-pub type BuiltinParamName = Cow<'static, str>;
+/// Can't have str | IStr, because constant BuiltinParam causes
+/// E0492: constant functions cannot refer to interior mutable data
+#[derive(Clone, Trace)]
+pub struct ParamName(Option<Cow<'static, str>>);
+impl ParamName {
+ pub const ANONYMOUS: Self = Self(None);
+ pub const fn new_static(name: &'static str) -> Self {
+ Self(Some(Cow::Borrowed(name)))
+ }
+ pub fn new_dynamic(name: String) -> Self {
+ Self(Some(Cow::Owned(name)))
+ }
+ pub fn as_str(&self) -> Option<&str> {
+ self.0.as_deref()
+ }
+ pub fn is_anonymous(&self) -> bool {
+ self.0.is_none()
+ }
+}
+impl PartialEq<IStr> for ParamName {
+ fn eq(&self, other: &IStr) -> bool {
+ match &self.0 {
+ Some(s) => s.as_bytes() == other.as_bytes(),
+ None => false,
+ }
+ }
+}
#[derive(Clone, Trace)]
pub struct BuiltinParam {
+ name: ParamName,
+ has_default: bool,
+}
+impl BuiltinParam {
+ pub const fn new(name: ParamName, has_default: bool) -> Self {
+ Self { name, has_default }
+ }
/// Parameter name for named call parsing
- pub name: Option<BuiltinParamName>,
+ pub fn name(&self) -> &ParamName {
+ &self.name
+ }
/// Is implementation allowed to return empty value
- pub has_default: bool,
+ pub fn has_default(&self) -> bool {
+ self.has_default
+ }
}
/// Description of function defined by native code
@@ -44,12 +82,12 @@
}
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 {
+ pub fn new(params: Vec<String>, handler: impl NativeCallbackHandler) -> Self {
Self {
params: params
.into_iter()
.map(|n| BuiltinParam {
- name: Some(n),
+ name: ParamName::new_dynamic(n.to_string()),
has_default: false,
})
.collect(),
crates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -8,7 +8,7 @@
use self::{
arglike::OptionalContext,
- builtin::{Builtin, StaticBuiltin},
+ builtin::{Builtin, BuiltinParam, ParamName, StaticBuiltin},
native::NativeDesc,
parse::{parse_default_function_call, parse_function_call},
};
@@ -113,17 +113,45 @@
}
}
+#[allow(clippy::unnecessary_wraps)]
+#[builtin]
+const fn builtin_id(x: Val) -> Val {
+ x
+}
+static ID: &builtin_id = &builtin_id {};
+
impl FuncVal {
pub fn builtin(builtin: impl Builtin) -> Self {
Self::Builtin(Cc::new(tb!(builtin)))
}
+
+ pub fn params(&self) -> Vec<BuiltinParam> {
+ match self {
+ Self::Id => ID.params().to_vec(),
+ Self::StaticBuiltin(i) => i.params().to_vec(),
+ Self::Builtin(i) => i.params().to_vec(),
+ Self::Normal(p) => p
+ .params
+ .iter()
+ .map(|p| {
+ BuiltinParam::new(
+ p.0.name()
+ .as_ref()
+ .map(IStr::to_string)
+ .map_or(ParamName::ANONYMOUS, ParamName::new_dynamic),
+ p.1.is_some(),
+ )
+ })
+ .collect(),
+ }
+ }
/// Amount of non-default required arguments
pub fn params_len(&self) -> usize {
match self {
Self::Id => 1,
Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),
- Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default).count(),
- Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default).count(),
+ Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default()).count(),
+ Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default()).count(),
}
}
/// Function name, as defined in code.
@@ -146,16 +174,7 @@
tailstrict: bool,
) -> Result<Val> {
match self {
- Self::Id => {
- #[allow(clippy::unnecessary_wraps)]
- #[builtin]
- const fn builtin_id(x: Val) -> Val {
- x
- }
- static ID: &builtin_id = &builtin_id {};
-
- ID.call(call_ctx, loc, args)
- }
+ Self::Id => ID.call(call_ctx, loc, args),
Self::Normal(func) => {
let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;
evaluate(body_ctx, &func.body)
crates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -163,7 +163,7 @@
params.len(),
params
.iter()
- .map(|p| (p.name.as_ref().map(|v| v.as_ref().into()), p.has_default))
+ .map(|p| (p.name().as_str().map(IStr::from), p.has_default()))
.collect()
))
}
@@ -180,7 +180,7 @@
// FIXME: O(n) for arg existence check
let id = params
.iter()
- .position(|p| p.name.as_ref().map_or(false, |v| v as &str == name as &str))
+ .position(|p| p.name() == name)
.ok_or_else(|| UnknownFunctionParameter((name as &str).to_owned()))?;
if replace(&mut passed_args[id], Some(arg)).is_some() {
throw!(BindingParameterASecondTime(name.clone()));
@@ -190,7 +190,7 @@
})?;
if filled_args < params.len() {
- for (id, _) in params.iter().enumerate().filter(|(_, p)| p.has_default) {
+ for (id, _) in params.iter().enumerate().filter(|(_, p)| p.has_default()) {
if passed_args[id].is_some() {
continue;
}
@@ -202,20 +202,16 @@
for param in params.iter().skip(args.unnamed_len()) {
let mut found = false;
args.named_names(&mut |name| {
- if param
- .name
- .as_ref()
- .map_or(false, |v| v as &str == name as &str)
- {
+ if param.name() == name {
found = true;
}
});
if !found {
throw!(FunctionParameterNotBoundInCall(
- param.name.as_ref().map(|v| v.as_ref().into()),
+ param.name().as_str().map(IStr::from),
params
.iter()
- .map(|p| (p.name.as_ref().map(|p| p.as_ref().into()), p.has_default))
+ .map(|p| (p.name().as_str().map(IStr::from), p.has_default()))
.collect()
));
}
tests/tests/common.rsdiffbeforeafterboth--- a/tests/tests/common.rs
+++ b/tests/tests/common.rs
@@ -1,5 +1,3 @@
-use std::borrow::Cow;
-
use jrsonnet_evaluator::{
error::Result,
function::{builtin, FuncVal},
@@ -66,22 +64,12 @@
FuncVal::StaticBuiltin(b) => b
.params()
.iter()
- .map(|p| {
- p.name
- .as_ref()
- .unwrap_or(&Cow::Borrowed("<unnamed>"))
- .to_string()
- })
+ .map(|p| p.name().as_str().unwrap_or(&"<unnamed>").to_string())
.collect(),
FuncVal::Builtin(b) => b
.params()
.iter()
- .map(|p| {
- p.name
- .as_ref()
- .unwrap_or(&Cow::Borrowed("<unnamed>"))
- .to_string()
- })
+ .map(|p| p.name().as_str().unwrap_or(&"<unnamed>").to_string())
.collect(),
}
}