use std::marker::PhantomData;
use facet::Facet;
use xur_resolve::NativeTypeId;
use super::hooks::TypeHooks;
use super::into_native::{IntoNativeFn, IntoNativeMethod, IntoNativeMethodMut};
use super::{CallFn, Context, NativeType, Registry, Signature};
use crate::{BuiltinType, RunResult, Value};
pub struct ModuleBuilder<'r> {
registry: &'r Registry,
pub(super) module: usize,
}
impl<'r> ModuleBuilder<'r> {
pub(super) fn new(registry: &'r Registry, module: usize) -> Self {
Self { registry, module }
}
}
impl ModuleBuilder<'_> {
pub fn function<Marker>(&mut self, name: &str, f: impl IntoNativeFn<Marker>) -> &mut Self {
let (signature, call) = f.into_native();
let decl = self.registry.add_fn(name, Some(signature), call);
let module = self.module;
self.registry
.with_inner(|inner| inner.modules[module].functions.push(decl));
self
}
pub fn raw_function(
&mut self,
name: &str,
f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
) -> &mut Self {
let decl = self.registry.add_fn(name, None, Box::new(f));
let module = self.module;
self.registry
.with_inner(|inner| inner.modules[module].functions.push(decl));
self
}
pub fn ty<T: Facet<'static> + Clone + Send + 'static>(
&mut self,
name: &str,
) -> TypeBuilder<'_, T> {
let module = self.module;
let id = self.registry.with_inner(|inner| {
let id = NativeTypeId(inner.types.len() as u32);
inner.types.push(NativeType {
name: name.to_owned(),
shape: T::SHAPE,
functions: Vec::new(),
hooks: TypeHooks::new::<T>(),
});
inner.modules[module].types.push(id);
id
});
TypeBuilder {
registry: self.registry,
ty: id,
_ty: PhantomData,
}
}
}
pub struct TypeBuilder<'r, T> {
registry: &'r Registry,
ty: NativeTypeId,
_ty: PhantomData<T>,
}
impl<T: Facet<'static> + 'static> TypeBuilder<'_, T> {
pub fn function<Marker>(&mut self, name: &str, f: impl IntoNativeFn<Marker>) -> &mut Self {
let (signature, call) = f.into_native();
self.add(name, signature, call)
}
pub fn raw_function(
&mut self,
name: &str,
f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
) -> &mut Self {
let decl = self.registry.add_fn(name, None, Box::new(f));
let ty = self.ty;
self.registry
.with_inner(|inner| inner.types[ty.0 as usize].functions.push(decl));
self
}
pub fn method<Marker>(&mut self, name: &str, f: impl IntoNativeMethod<T, Marker>) -> &mut Self {
let (signature, call) = f.into_native();
self.add(name, signature, call)
}
pub fn method_mut<Marker>(
&mut self,
name: &str,
f: impl IntoNativeMethodMut<T, Marker>,
) -> &mut Self {
let (signature, call) = f.into_native();
self.add(name, signature, call)
}
fn add(&mut self, name: &str, signature: Signature, call: Box<CallFn>) -> &mut Self {
let decl = self.registry.add_fn(name, Some(signature), call);
let ty = self.ty;
self.registry
.with_inner(|inner| inner.types[ty.0 as usize].functions.push(decl));
self
}
}
pub struct BuiltinBuilder<'r> {
registry: &'r Registry,
ty: BuiltinType,
}
impl<'r> BuiltinBuilder<'r> {
pub(super) fn new(registry: &'r Registry, ty: BuiltinType) -> Self {
Self { registry, ty }
}
pub fn function<Marker>(&mut self, name: &str, f: impl IntoNativeFn<Marker>) -> &mut Self {
let (signature, call) = f.into_native();
self.add(name, Some(signature), call)
}
pub fn raw_function(
&mut self,
name: &str,
f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
) -> &mut Self {
self.add(name, None, Box::new(f))
}
fn add(&mut self, name: &str, signature: Option<Signature>, call: Box<CallFn>) -> &mut Self {
let decl = self.registry.add_fn(name, signature, call);
let key = (self.ty, name.to_owned());
self.registry
.with_inner(|inner| inner.builtin_methods.insert(key, decl.id));
self
}
}