|
| 1 | +use std::borrow::Cow; |
| 2 | + |
| 3 | +use hv_lua::{FromLua, ToLua}; |
| 4 | +use tealr::{NamePart, TealType, TypeName}; |
| 5 | + |
| 6 | +///This trait is used to limit what types can be set in the create_type_component_container macro |
| 7 | +///It doesn't really have a use outside of that. |
| 8 | +pub trait Component { |
| 9 | + fn is_component() {} |
| 10 | +} |
| 11 | + |
| 12 | +#[derive(Debug, Clone, Copy)] |
| 13 | +pub struct CopyComponent<T>(T); |
| 14 | +impl<T> Component for CopyComponent<T> {} |
| 15 | +impl<T: Send + Sync + 'static + Clone + Copy> hv_lua::UserData for CopyComponent<T> |
| 16 | +where |
| 17 | + T: for<'a> ToLua<'a> + for<'a> FromLua<'a>, |
| 18 | +{ |
| 19 | + #[allow(clippy::unit_arg)] |
| 20 | + fn add_fields<'lua, F: hv_lua::UserDataFields<'lua, Self>>(fields: &mut F) { |
| 21 | + fields.add_field_method_get("value", |_, this| Ok(this.0)); |
| 22 | + fields.add_field_method_set("value", |_, this, value| Ok(this.0 = value)); |
| 23 | + } |
| 24 | + fn on_metatable_init(t: hv_alchemy::Type<Self>) { |
| 25 | + use hv_lua::hv::LuaUserDataTypeExt; |
| 26 | + t.add_clone().add_copy().mark_component(); |
| 27 | + } |
| 28 | + fn add_type_methods<'lua, M: hv_lua::UserDataMethods<'lua, hv_alchemy::Type<Self>>>( |
| 29 | + methods: &mut M, |
| 30 | + ) { |
| 31 | + methods.add_function("new", |_, i: T| Ok(Self(i))); |
| 32 | + } |
| 33 | + fn on_type_metatable_init(t: hv_alchemy::Type<hv_alchemy::Type<Self>>) { |
| 34 | + use hv_lua::hv::LuaUserDataTypeTypeExt; |
| 35 | + t.mark_component_type(); |
| 36 | + } |
| 37 | +} |
| 38 | +impl<T: tealr::TypeName> tealr::TypeName for CopyComponent<T> { |
| 39 | + fn get_type_parts() -> std::borrow::Cow<'static, [tealr::NamePart]> { |
| 40 | + let name = tealr::NamePart::Type(TealType { |
| 41 | + name: Cow::Borrowed("Component"), |
| 42 | + type_kind: tealr::KindOfType::External, |
| 43 | + generics: None, |
| 44 | + }); |
| 45 | + let mut type_name = vec![name, NamePart::Symbol(Cow::Borrowed("<"))]; |
| 46 | + type_name.append(&mut T::get_type_parts().into_owned()); |
| 47 | + type_name.push(NamePart::Symbol(Cow::Borrowed(">"))); |
| 48 | + Cow::Owned(type_name) |
| 49 | + } |
| 50 | +} |
| 51 | +impl<T: TypeName> tealr::TypeBody for CopyComponent<T> { |
| 52 | + fn get_type_body(gen: &mut tealr::TypeGenerator) { |
| 53 | + get_type_body_component::<T, Self>(gen) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#[derive(Debug, Clone, Copy)] |
| 58 | +pub struct CloneComponent<T>(T); |
| 59 | +impl<T> Component for CloneComponent<T> {} |
| 60 | +impl<T: Send + Sync + 'static + Clone> hv_lua::UserData for CloneComponent<T> |
| 61 | +where |
| 62 | + T: for<'a> ToLua<'a> + for<'a> FromLua<'a>, |
| 63 | +{ |
| 64 | + #[allow(clippy::unit_arg)] |
| 65 | + fn add_fields<'lua, F: hv_lua::UserDataFields<'lua, Self>>(fields: &mut F) { |
| 66 | + fields.add_field_method_get("value", |_, this| Ok(this.0.clone())); |
| 67 | + fields.add_field_method_set("value", |_, this, value| Ok(this.0 = value)); |
| 68 | + } |
| 69 | + fn on_metatable_init(t: hv_alchemy::Type<Self>) { |
| 70 | + use hv_lua::hv::LuaUserDataTypeExt; |
| 71 | + t.add_clone().mark_component(); |
| 72 | + } |
| 73 | + fn add_type_methods<'lua, M: hv_lua::UserDataMethods<'lua, hv_alchemy::Type<Self>>>( |
| 74 | + methods: &mut M, |
| 75 | + ) { |
| 76 | + methods.add_function("new", |_, i: T| Ok(Self(i))); |
| 77 | + } |
| 78 | + fn on_type_metatable_init(t: hv_alchemy::Type<hv_alchemy::Type<Self>>) { |
| 79 | + use hv_lua::hv::LuaUserDataTypeTypeExt; |
| 80 | + t.mark_component_type(); |
| 81 | + } |
| 82 | +} |
| 83 | +impl<T: tealr::TypeName> tealr::TypeName for CloneComponent<T> { |
| 84 | + fn get_type_parts() -> std::borrow::Cow<'static, [tealr::NamePart]> { |
| 85 | + let name = tealr::NamePart::Type(TealType { |
| 86 | + name: Cow::Borrowed("Component"), |
| 87 | + type_kind: tealr::KindOfType::External, |
| 88 | + generics: None, |
| 89 | + }); |
| 90 | + let mut type_name = vec![name, NamePart::Symbol(Cow::Borrowed("<"))]; |
| 91 | + type_name.append(&mut T::get_type_parts().into_owned()); |
| 92 | + type_name.push(NamePart::Symbol(Cow::Borrowed(">"))); |
| 93 | + Cow::Owned(type_name) |
| 94 | + } |
| 95 | +} |
| 96 | +impl<T: TypeName> tealr::TypeBody for CloneComponent<T> { |
| 97 | + fn get_type_body(gen: &mut tealr::TypeGenerator) { |
| 98 | + get_type_body_component::<T, Self>(gen) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +fn get_type_body_component<T: TypeName, SelfType: TypeName>(gen: &mut tealr::TypeGenerator) { |
| 103 | + let mut signature = vec![NamePart::Symbol(Cow::Borrowed("function("))]; |
| 104 | + signature.append(&mut T::get_type_parts().into_owned()); |
| 105 | + signature.push(NamePart::Symbol(Cow::Borrowed("):"))); |
| 106 | + signature.append(&mut SelfType::get_type_parts().into_owned()); |
| 107 | + |
| 108 | + gen.methods.push(tealr::ExportedFunction { |
| 109 | + name: Cow::Borrowed("new").into(), |
| 110 | + signature: Cow::Owned(signature), |
| 111 | + is_meta_method: false, |
| 112 | + }); |
| 113 | + gen.fields.push(( |
| 114 | + Cow::Borrowed("value"), |
| 115 | + tealr::type_parts_to_str(<T as tealr::TypeName>::get_type_parts()), |
| 116 | + )); |
| 117 | +} |
| 118 | + |
| 119 | +#[macro_export] |
| 120 | +macro_rules! create_type_component_container { |
| 121 | + ($name:ident with $($field_name:ident of $type_name:ty,)+) => { |
| 122 | + #[derive(Debug, Clone)] |
| 123 | + pub struct $name<'lua>(hv_lua::Table<'lua>); |
| 124 | + impl<'lua> hv_lua::ToLua<'lua> for $name<'lua> { |
| 125 | + fn to_lua( |
| 126 | + self, |
| 127 | + lua: &'lua hv_lua::Lua, |
| 128 | + ) -> std::result::Result<hv_lua::Value<'lua>, hv_lua::Error> { |
| 129 | + self.0.to_lua(lua) |
| 130 | + } |
| 131 | + } |
| 132 | + impl<'lua> tealr::TypeName for $name<'lua> { |
| 133 | + fn get_type_parts() -> std::borrow::Cow<'static, [tealr::NamePart]> { |
| 134 | + use tealr::new_type; |
| 135 | + new_type!(Components) |
| 136 | + } |
| 137 | + } |
| 138 | + impl<'lua> tealr::TypeBody for $name<'lua> { |
| 139 | + fn get_type_body(gen: &mut tealr::TypeGenerator) { |
| 140 | + $( |
| 141 | + <$type_name as $crate::lua::Component>::is_component(); |
| 142 | + gen.fields.push( |
| 143 | + ( |
| 144 | + std::borrow::Cow::Borrowed( |
| 145 | + stringify!($field_name) |
| 146 | + ), |
| 147 | + tealr::type_parts_to_str( |
| 148 | + <$type_name as tealr::TypeName>::get_type_parts() |
| 149 | + ) |
| 150 | + ) |
| 151 | + ); |
| 152 | + )* |
| 153 | + |
| 154 | + } |
| 155 | + } |
| 156 | + impl<'lua> $name<'lua> { |
| 157 | + pub fn new(lua: &'lua hv_lua::Lua) -> Result<Self, Box<dyn std::error::Error>> { |
| 158 | + let table = lua.create_table()?; |
| 159 | + use $crate::lua::Component; |
| 160 | + $( |
| 161 | + <$type_name as Component>::is_component(); |
| 162 | + table.set(stringify!($field_name),lua.create_userdata_type::<$type_name>()?)?; |
| 163 | + )* |
| 164 | + Ok(Self(table)) |
| 165 | + } |
| 166 | + } |
| 167 | + }; |
| 168 | +} |
0 commit comments