Skip to content

Commit

Permalink
use the new format for globals
Browse files Browse the repository at this point in the history
improve old format to remove ambiguity
fix tests so they don't break on version number change
  • Loading branch information
lenscas committed Dec 14, 2023
1 parent 5909e43 commit 709d962
Show file tree
Hide file tree
Showing 35 changed files with 327 additions and 90 deletions.
3 changes: 2 additions & 1 deletion src/export_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl InstanceWalker {
instances: Default::default(),
}
}
fn add_instance<T: TypeName>(&mut self, name: Cow<'static, str>) {
fn add_instance<T: ToTypename>(&mut self, name: Cow<'static, str>) {
let teal_type = T::get_type_parts_as_global();
let z = T::get_type_kind();
let is_external = matches!(z, KindOfType::External);
Expand All @@ -60,6 +60,7 @@ impl InstanceWalker {
teal_type,
is_external,
doc,
ty: T::to_typename(),
});
}
fn document_instance(&mut self, doc: &'static str) {
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,13 @@ pub fn new_type_to_old(a: Type, is_callback: bool) -> Cow<'static, [NamePart]> {
}
parts.push(NamePart::symbol(")"));
if !returns.is_empty() {
parts.push(NamePart::symbol(":"));
parts.push(NamePart::symbol(":("));
for ret in returns {
parts.extend(new_type_to_old(ret, true).iter().cloned());
parts.push(NamePart::symbol(" , "))
}
parts.pop();
parts.push(NamePart::symbol(")"));
}

Cow::Owned(parts)
Expand Down
32 changes: 23 additions & 9 deletions src/type_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,31 @@ pub struct Field {
///the name of the field
pub name: NameContainer,

///the type of the field
///the type of the field, according to the old format
#[cfg_attr(
all(any(feature = "rlua", feature = "mlua"), feature = "derive", not(all(feature = "rlua", feature = "mlua"))),
tealr(remote = V)
)]
pub teal_type: Cow<'static, [NamePart]>,
/// the type of the field
pub ty: Type,
}

impl From<(NameContainer, Cow<'static, [NamePart]>)> for Field {
fn from((name, teal_type): (NameContainer, Cow<'static, [NamePart]>)) -> Self {
Self { name, teal_type }
impl From<(NameContainer, Type)> for Field {
fn from((name, ty): (NameContainer, Type)) -> Self {
#[allow(deprecated)]
let teal_type = crate::new_type_to_old(ty.clone(), false);
Self {
name,
teal_type,
ty,
}
}
}
impl Field {
///creates a new field
pub fn new<A: ToTypename>(name: impl Into<Cow<'static, str>>) -> Self {
(NameContainer::from(name.into()), A::to_typename()).into()
}
}

Expand Down Expand Up @@ -842,7 +856,7 @@ where
{
self.copy_docs(name.as_ref());
self.fields
.push((name.as_ref().to_vec().into(), R::get_type_parts()).into());
.push((name.as_ref().to_vec().into(), R::to_typename()).into());
}

fn add_field_method_set<S, A, M>(&mut self, name: &S, _: M)
Expand All @@ -853,7 +867,7 @@ where
{
self.copy_docs(name.as_ref());
self.fields
.push((name.as_ref().to_vec().into(), A::get_type_parts()).into());
.push((name.as_ref().to_vec().into(), A::to_typename()).into());
}

fn add_field_function_get<S, R, F>(&mut self, name: &S, _: F)
Expand All @@ -864,7 +878,7 @@ where
{
self.copy_docs(name.as_ref());
self.static_fields
.push((name.as_ref().to_vec().into(), R::get_type_parts()).into());
.push((name.as_ref().to_vec().into(), R::to_typename()).into());
}

fn add_field_function_set<S, A, F>(&mut self, name: &S, _: F)
Expand All @@ -875,7 +889,7 @@ where
{
self.copy_docs(name.as_ref());
self.static_fields
.push((name.as_ref().to_vec().into(), A::get_type_parts()).into());
.push((name.as_ref().to_vec().into(), A::to_typename()).into());
}

fn add_meta_field_with<R, F>(&mut self, meta: MetaMethodM, _: F)
Expand All @@ -887,7 +901,7 @@ where
let name: Cow<'_, str> = Cow::Owned(x.name().to_string());
self.copy_docs(name.as_bytes());
self.static_fields
.push((NameContainer::from(name), R::get_type_parts()).into());
.push((NameContainer::from(name), R::to_typename()).into());
}

fn document(&mut self, documentation: &str) {
Expand Down
7 changes: 5 additions & 2 deletions src/type_walker.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{borrow::Cow, string::FromUtf8Error};

use crate::{type_parts_to_str, NamePart, ToTypename, TypeBody, TypeGenerator};
use crate::{type_parts_to_str, NamePart, ToTypename, Type, TypeBody, TypeGenerator};

type V = Vec<NamePart>;

Expand All @@ -24,11 +24,13 @@ pub struct GlobalInstance {
all(any(feature = "rlua", feature = "mlua"), feature = "derive", not(all(feature = "rlua", feature = "mlua"))),
tealr(remote = String))]
pub name: Cow<'static, str>,
///the type
///the type according to the old format
#[cfg_attr(
all(any(feature = "rlua", feature = "mlua"), feature = "derive", not(all(feature = "rlua", feature = "mlua"))),
tealr(remote = V))]
pub teal_type: Cow<'static, [NamePart]>,
///the type
pub ty: Type,
///if the type is external
pub is_external: bool,
///documentation for this global
Expand Down Expand Up @@ -180,6 +182,7 @@ impl TypeWalker {
teal_type,
is_external,
doc,
ty: _,
} = global;
let doc = doc
.lines()
Expand Down
7 changes: 4 additions & 3 deletions tealr_derive/src/from_to_lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ fn implement_for_struct(structure: Struct, config: BasicConfig) -> TokenStream {
.enumerate()
.map(|(key, x)| {
let ty = &x.0.ty;
let name = format!("param{key}");
let key_as_str = Literal::usize_unsuffixed(key);
let (set_value, get_value, type_name) =
find_tag_with_value("remote", &x.0.attributes)
Expand All @@ -154,8 +155,8 @@ fn implement_for_struct(structure: Struct, config: BasicConfig) -> TokenStream {
gen
.fields
.push(
::std::convert::From::from((::std::borrow::Cow::Borrowed(stringify!(#key_as_str)).into(),
<(#type_name) as #type_name_path>::to_old_type_parts()))
::std::convert::From::from((::std::borrow::Cow::Borrowed(#name).into(),
<(#type_name) as #type_name_path>::to_typename()))
);
},
),
Expand Down Expand Up @@ -197,7 +198,7 @@ fn implement_for_struct(structure: Struct, config: BasicConfig) -> TokenStream {
.fields
.push(
::std::convert::From::from((::std::borrow::Cow::Borrowed(stringify!(#name)).into(),
<(#type_name) as #type_name_path>::to_old_type_parts()))
<(#type_name) as #type_name_path>::to_typename()))
);
gen.copy_docs(stringify!(#name).as_bytes());
},
Expand Down
1 change: 1 addition & 0 deletions tests/mlua/async.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"tealr_version_used":"0.9.0-alpha4","given_types":[{"Record":{"should_be_inlined":false,"is_user_data":true,"type_name":[{"Type":{"name":"Example","type_kind":"External","generics":null}}],"fields":[],"static_fields":[],"methods":[{"name":"example_method","signature":[{"Symbol":"function"},{"Symbol":"("},{"Symbol":"self"},{"Symbol":":"},{"Type":{"name":"Example","type_kind":"External","generics":null}},{"Symbol":" , "},{"Type":{"name":"integer","type_kind":"Builtin","generics":null}},{"Symbol":")"},{"Symbol":":("},{"Type":{"name":"integer","type_kind":"Builtin","generics":null}},{"Symbol":")"}],"params":[{"param_name":"self","ty":{"Single":{"name":"Example","kind":"External"}}},{"param_name":null,"ty":{"Single":{"name":"integer","kind":"Builtin"}}}],"returns":[{"Single":{"name":"integer","kind":"Builtin"}}],"is_meta_method":false}],"mut_methods":[],"functions":[{"name":"example_method_mut","signature":[{"Symbol":"function"},{"Symbol":"("},{"Type":{"name":"integer","type_kind":"Builtin","generics":null}},{"Symbol":" , "},{"Type":{"name":"string","type_kind":"Builtin","generics":null}},{"Symbol":")"},{"Symbol":":("},{"Type":{"name":"string","type_kind":"Builtin","generics":null}},{"Symbol":")"}],"params":[{"param_name":null,"ty":{"Single":{"name":"integer","kind":"Builtin"}}},{"param_name":null,"ty":{"Single":{"name":"string","kind":"Builtin"}}}],"returns":[{"Single":{"name":"string","kind":"Builtin"}}],"is_meta_method":false},{"name":"example_function","signature":[{"Symbol":"function"},{"Symbol":"("},{"Symbol":"{"},{"Type":{"name":"string","type_kind":"Builtin","generics":null}},{"Symbol":"}"},{"Symbol":")"},{"Symbol":":("},{"Symbol":"{"},{"Type":{"name":"string","type_kind":"Builtin","generics":null}},{"Symbol":"}"},{"Symbol":" , "},{"Type":{"name":"integer","type_kind":"Builtin","generics":null}},{"Symbol":")"}],"params":[{"param_name":null,"ty":{"Array":{"Single":{"name":"string","kind":"Builtin"}}}}],"returns":[{"Array":{"Single":{"name":"string","kind":"Builtin"}}},{"Single":{"name":"integer","kind":"Builtin"}}],"is_meta_method":false}],"mut_functions":[{"name":"example_function_mut","signature":[{"Symbol":"function"},{"Symbol":"("},{"Type":{"name":"boolean","type_kind":"Builtin","generics":null}},{"Symbol":" , "},{"Type":{"name":"Example","type_kind":"External","generics":null}},{"Symbol":")"},{"Symbol":":("},{"Type":{"name":"boolean","type_kind":"Builtin","generics":null}},{"Symbol":" , "},{"Type":{"name":"Example","type_kind":"External","generics":null}},{"Symbol":")"}],"params":[{"param_name":null,"ty":{"Single":{"name":"boolean","kind":"Builtin"}}},{"param_name":null,"ty":{"Single":{"name":"Example","kind":"External"}}}],"returns":[{"Single":{"name":"boolean","kind":"Builtin"}},{"Single":{"name":"Example","kind":"External"}}],"is_meta_method":false}],"meta_method":[],"meta_method_mut":[],"meta_function":[],"meta_function_mut":[],"documentation":{},"type_doc":"","next_docs":null,"should_generate_help_method":true}}],"global_instances_off":[],"extra_page":[]}
23 changes: 14 additions & 9 deletions tests/mlua/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,21 @@ fn async_fn() -> Result<()> {
//add more calls to process_type to generate more types in the same file
.process_type::<Example>()
//generate the file
.generate_global("test")
//the name parameter for TealDataMethods::{add_method,add_method_mut,add_function,add_function_mut}
//takes anything that can be used as a &[u8]
//this is to match the types from UserDataMethods
//however, as we turn it back into a string it is technically possible to get an error
//in this case, as &str's where used it can't happen though, so the .expect is fine
.to_json()
.expect("oh no :(");
assert_eq!(file_contents, "global record test\n\trecord Example\n\t\tuserdata\n\n\t\t-- Pure methods\n\t\texample_method: function(self:Example , integer):integer\n\n\t\t-- Pure functions\n\t\texample_method_mut: function(integer , string):string\n\n\t\texample_function: function({string}):{string} , integer\n\n\t\t-- Mutating functions\n\t\texample_function_mut: function(boolean , Example):boolean , Example\n\n\n\tend\nend\nreturn test");
//normally you would now save the file somewhere.
println!("{}\n ", file_contents);

let generated: serde_json::Value = serde_json::from_str(&file_contents).unwrap();

let mut original: serde_json::Value =
serde_json::from_str(include_str!("./async.json")).unwrap();
let x = original
.get_mut("tealr_version_used")
.expect("missing tealr_version_used in original");
if let serde_json::Value::String(x) = x {
*x = tealr::get_tealr_version().to_string();
}

assert_eq!(generated, original);

//how you pass this type to lua hasn't changed:
let lua = Lua::new();
Expand Down
Loading

0 comments on commit 709d962

Please sign in to comment.