use std::marker::PhantomData;
use facet::{Facet, Shape};
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};
use xur_resolve::{NativeVariantDecl, NativeVariantKind};
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 }
}
pub fn registry(&self) -> &'r Registry {
self.registry
}
pub fn submodule(&self, name: &str) -> ModuleBuilder<'r> {
let parent = self.module;
let module = self
.registry
.with_inner(|inner| inner.child(Some(parent), name));
ModuleBuilder::new(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), Vec::new(), call);
let module = self.module;
self.registry
.with_inner(|inner| inner.modules[module].functions.push(decl));
self
}
pub fn named_function<Marker>(
&mut self,
name: &str,
params: &[&str],
f: impl IntoNativeFn<Marker>,
) -> &mut Self {
let (signature, call) = f.into_native();
let names = params.iter().map(|s| s.to_string()).collect();
let decl = self.registry.add_fn(name, Some(signature), names, 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, Vec::new(), Box::new(f));
let module = self.module;
self.registry
.with_inner(|inner| inner.modules[module].functions.push(decl));
self
}
pub fn named_raw_function(
&mut self,
name: &str,
params: &[&str],
f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
) -> &mut Self {
let names = params.iter().map(|s| s.to_string()).collect();
let decl = self.registry.add_fn(name, None, names, Box::new(f));
let module = self.module;
self.registry
.with_inner(|inner| inner.modules[module].functions.push(decl));
self
}
pub fn shape_type(&mut self, name: &str, shape: &'static Shape) -> NativeTypeId {
let module = self.module;
self.registry.with_inner(|inner| {
let id = NativeTypeId(inner.types.len() as u32);
inner.types.push(NativeType {
name: name.to_owned(),
shape,
functions: Vec::new(),
hooks: None,
explicit_variants: None,
});
inner.modules[module].types.push(id);
id
})
}
pub fn repr_enum(&mut self, name: &str, variants: &[(&str, i64)]) -> &mut Self {
let module = self.module;
let variant_decls: Vec<NativeVariantDecl> = variants
.iter()
.map(|(vname, value)| NativeVariantDecl {
name: vname.to_string(),
kind: NativeVariantKind::Unit,
repr_value: Some(*value),
})
.collect();
self.registry.with_inner(|inner| {
let id = NativeTypeId(inner.types.len() as u32);
inner.types.push(NativeType {
name: name.to_owned(),
shape: <()>::SHAPE,
functions: Vec::new(),
hooks: None,
explicit_variants: Some(variant_decls),
});
inner.modules[module].types.push(id);
});
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: Some(TypeHooks::new::<T>()),
explicit_variants: None,
});
inner.modules[module].types.push(id);
id
});
TypeBuilder {
registry: self.registry,
ty: id,
_ty: PhantomData,
}
}
pub fn ty_borrowing<T: Facet<'static> + 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: Some(TypeHooks::borrowing::<T>()),
explicit_variants: None,
});
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, Vec::new(), 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), Vec::new(), 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, Vec::new(), call);
let key = (self.ty, name.to_owned());
self.registry
.with_inner(|inner| inner.builtin_methods.insert(key, decl.id));
self
}
}