use std::marker::PhantomData;
use facet::{Facet, Shape};
use xur_resolve::NativeTypeId;
use super::hooks::TypeHooks;
use super::into_native::{
IntoBuiltinMethod, IntoBuiltinMethodMut, IntoNativeFn, IntoNativeMethod, IntoNativeMethodMut,
};
use super::{CallFn, Context, NativeType, Registry, Signature};
use crate::{BuiltinType, RunResult, Value};
use xur_resolve::{NativeVariantDecl, NativeVariantKind};
pub trait Returns {
const RET: &'static Shape;
}
impl<T: Facet<'static>> Returns for T {
const RET: &'static Shape = T::SHAPE;
}
#[derive(Clone, Copy)]
pub enum ElemHint {
Shape(&'static Shape),
Name(&'static str),
Param(u8),
}
#[derive(Clone, Copy)]
pub enum XurType {
Ptr(Option<&'static Shape>),
List(Option<ElemHint>),
Map(Option<&'static Shape>, Option<&'static Shape>),
Named(&'static str, &'static [ElemHint]),
Record,
Module,
Unit,
}
impl XurType {
pub const PTR: Self = Self::Ptr(None);
pub const LIST: Self = Self::List(None);
pub const MAP: Self = Self::Map(None, None);
pub const UNIT: Self = Self::Unit;
pub fn ptr<T: Facet<'static>>() -> Self {
Self::Ptr(Some(T::SHAPE))
}
pub fn list<T: Facet<'static>>() -> Self {
Self::List(Some(ElemHint::Shape(T::SHAPE)))
}
pub fn list_named(name: &'static str) -> Self {
Self::List(Some(ElemHint::Name(name)))
}
pub fn map<K: Facet<'static>, V: Facet<'static>>() -> Self {
Self::Map(Some(K::SHAPE), Some(V::SHAPE))
}
pub fn map_keys<K: Facet<'static>>() -> Self {
Self::Map(Some(K::SHAPE), None)
}
pub fn map_values<V: Facet<'static>>() -> Self {
Self::Map(None, Some(V::SHAPE))
}
}
#[derive(Clone, Copy)]
pub enum ReturnHint {
Shape(&'static Shape),
Xur(XurType),
}
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 register_macro(
&mut self,
name: &str,
expander: impl xur_resolve::macros::MacroExpander + 'static,
) -> &mut Self {
self.registry.with_inner(|inner| {
inner.modules[self.module]
.macros
.insert(name.to_owned(), std::sync::Arc::new(expander));
});
self
}
pub fn function<Marker>(&mut self, name: &str, f: impl IntoNativeFn<Marker>) -> &mut Self {
let (signature, call) = f.into_native();
self.add_module_fn(name, Some(signature), Vec::new(), call, None)
}
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();
self.add_module_fn(name, Some(signature), names, call, None)
}
pub fn raw_function(
&mut self,
name: &str,
f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
) -> &mut Self {
self.add_module_fn(name, None, Vec::new(), Box::new(f), None)
}
pub fn raw_function_returns<R: Returns>(
&mut self,
name: &str,
f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
) -> &mut Self {
self.add_module_fn(
name,
None,
Vec::new(),
Box::new(f),
Some(ReturnHint::Shape(R::RET)),
)
}
pub fn raw_function_returns_xur(
&mut self,
name: &str,
ret: XurType,
f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
) -> &mut Self {
self.add_module_fn(
name,
None,
Vec::new(),
Box::new(f),
Some(ReturnHint::Xur(ret)),
)
}
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();
self.add_module_fn(name, None, names, Box::new(f), None)
}
pub fn named_raw_function_returns<R: Returns>(
&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();
self.add_module_fn(
name,
None,
names,
Box::new(f),
Some(ReturnHint::Shape(R::RET)),
)
}
pub fn named_raw_function_returns_xur(
&mut self,
name: &str,
params: &[&str],
ret: XurType,
f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
) -> &mut Self {
let names = params.iter().map(|s| s.to_string()).collect();
self.add_module_fn(name, None, names, Box::new(f), Some(ReturnHint::Xur(ret)))
}
fn add_module_fn(
&mut self,
name: &str,
signature: Option<Signature>,
names: Vec<String>,
call: Box<CallFn>,
return_hint: Option<ReturnHint>,
) -> &mut Self {
let decl = self.registry.add_fn(name, signature, names, call);
if let Some(hint) = return_hint {
self.registry.set_return_hint(decl.id, hint);
}
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_existing<T: Facet<'static> + Clone + Send + 'static>(
&mut self,
) -> TypeBuilder<'_, T> {
let id = self
.registry
.type_of_shape(T::SHAPE)
.expect("ty_existing: type not registered");
TypeBuilder {
registry: self.registry,
ty: id,
_ty: PhantomData,
}
}
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, Some(signature), call, None)
}
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), None)
}
pub fn raw_function_returns<R: Returns>(
&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), Some(ReturnHint::Shape(R::RET)))
}
pub fn raw_function_returns_xur(
&mut self,
name: &str,
ret: XurType,
f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
) -> &mut Self {
self.add(name, None, Box::new(f), Some(ReturnHint::Xur(ret)))
}
pub fn method<Marker>(&mut self, name: &str, f: impl IntoNativeMethod<T, Marker>) -> &mut Self {
let (signature, call) = f.into_native();
self.add(name, Some(signature), call, None)
}
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, Some(signature), call, None)
}
fn add(
&mut self,
name: &str,
signature: Option<Signature>,
call: Box<CallFn>,
return_hint: Option<ReturnHint>,
) -> &mut Self {
let decl = self.registry.add_fn(name, signature, Vec::new(), call);
if let Some(hint) = return_hint {
self.registry.set_return_hint(decl.id, hint);
}
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, None)
}
pub fn method<Recv: ?Sized + 'static, Marker>(
&mut self,
name: &str,
f: impl IntoBuiltinMethod<Recv, Marker>,
) -> &mut Self {
let (signature, call) = f.into_native();
self.add(name, Some(signature), call, None)
}
pub fn method_mut<Recv: 'static, Marker>(
&mut self,
name: &str,
f: impl IntoBuiltinMethodMut<Recv, Marker>,
) -> &mut Self {
let (signature, call) = f.into_native();
self.add(name, Some(signature), call, None)
}
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), None)
}
pub fn raw_function_returns<R: Returns>(
&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), Some(ReturnHint::Shape(R::RET)))
}
pub fn raw_function_returns_xur(
&mut self,
name: &str,
ret: XurType,
f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
) -> &mut Self {
self.add(name, None, Box::new(f), Some(ReturnHint::Xur(ret)))
}
fn add(
&mut self,
name: &str,
signature: Option<Signature>,
call: Box<CallFn>,
return_hint: Option<ReturnHint>,
) -> &mut Self {
let decl = self.registry.add_fn(name, signature, Vec::new(), call);
if let Some(hint) = return_hint {
self.registry.set_return_hint(decl.id, hint);
}
let key = (self.ty, name.to_owned());
self.registry
.with_inner(|inner| inner.builtin_methods.insert(key, decl.id));
self
}
}