Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mlua types for Lua 5.1 #237

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
:range(), Send
Kampfkarren committed Sep 15, 2022
commit c79d1231e25a7c1c4fcbf89ed6813b65f5e6b761
31 changes: 29 additions & 2 deletions full-moon-lua-types-derive/src/lua_user_data.rs
Original file line number Diff line number Diff line change
@@ -175,13 +175,39 @@ pub fn derive(input: TokenStream) -> TokenStream {
let fields = field
.named
.iter()
.map(|field| &field.ident)
.filter_map(|field| field.ident.as_ref())
.map(quote::ToTokens::to_token_stream)
.collect::<Vec<_>>();

let mut added_fields: Vec<proc_macro2::TokenStream> = Vec::new();

for attr in &variant.attrs {
if !attr.path.is_ident("lua") {
continue;
}

let name_value = attr
.parse_args::<syn::MetaNameValue>()
.expect("expected name value for lua attribute");

if !name_value.path.is_ident("add_field") {
continue;
}

added_fields.push(
syn::parse_str(&match name_value.lit {
syn::Lit::Str(lit_str) => lit_str.value(),
_ => panic!("expected string for add_field"),
})
.unwrap(),
);
}

cases.push(quote! {
#input_ident::#variant_ident { #(#fields),* } => {
Some(full_moon::ast::#input_ident::#variant_ident {
#(#fields: #fields.create_ast_node()?),*
#(#fields: #fields.create_ast_node()?,)*
#(#added_fields,)*
})
}
});
@@ -343,6 +369,7 @@ pub fn derive(input: TokenStream) -> TokenStream {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
crate::mlua_util::add_core_metamethods_no_tostring(stringify!(#input_ident), methods);
crate::mlua_util::add_create_ast_node_methods(methods);
crate::mlua_util::add_print(methods);
crate::mlua_util::add_visit(methods);

2 changes: 1 addition & 1 deletion full-moon-lua-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ edition = "2021"
[dependencies]
full_moon = { version = "0.15.1", path = "../full-moon" }
full_moon_lua_types_derive = { path = "../full-moon-lua-types-derive" }
mlua = { version = "0.8.3", features = ["luau"] }
mlua = { version = "0.8.3", features = ["luau", "send"] }
paste = "1.0.9"

[features]
35 changes: 28 additions & 7 deletions full-moon-lua-types/src/core.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,10 @@ use mlua::{Table, ToLua, UserData};

use crate::{
create_ast_node::CreateAstNode,
mlua_util::{add_core_meta_methods, add_newindex_block, add_print, add_visit, ArcLocked},
mlua_util::{
add_core_meta_methods, add_create_ast_node_methods, add_newindex_block, add_print,
add_visit, ArcLocked,
},
prepare_for_lua::PrepareForLua,
shared::*,
};
@@ -20,8 +23,8 @@ pub struct Ast {
eof: ArcLocked<TokenReference>,
}

impl From<ast::Ast> for Ast {
fn from(ast: ast::Ast) -> Self {
impl From<&ast::Ast> for Ast {
fn from(ast: &ast::Ast) -> Self {
Ast {
nodes: l(Block::new(ast.nodes())),
eof: l(TokenReference::new(ast.eof())),
@@ -97,6 +100,7 @@ impl UserData for Assignment {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("Assignment", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -209,6 +213,7 @@ impl UserData for Block {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("Block", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -280,6 +285,7 @@ impl UserData for Do {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("Do", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -335,6 +341,7 @@ impl UserData for ElseIf {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("ElseIf", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -370,9 +377,8 @@ pub enum Expression {
expression: Box<ArcLocked<Expression>>,
},

Value {
value: Box<ArcLocked<Value>>,
},
#[cfg_attr(feature = "luau", lua(add_field = "type_assertion: None"))]
Value { value: Box<ArcLocked<Value>> },
}

impl Expression {
@@ -397,7 +403,7 @@ impl Expression {
expression: Box::new(l(Expression::new(expression))),
},

ast::Expression::Value { value } => Expression::Value {
ast::Expression::Value { value, .. } => Expression::Value {
value: Box::new(l(Value::new(value))),
},

@@ -488,6 +494,7 @@ impl UserData for FunctionBody {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("FunctionBody", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -608,6 +615,7 @@ impl UserData for FunctionCall {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("FunctionCall", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -662,6 +670,7 @@ impl UserData for FunctionDeclaration {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("FunctionDeclaration", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -722,6 +731,7 @@ impl UserData for FunctionName {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("FunctionName", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -802,6 +812,7 @@ impl UserData for GenericFor {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("GenericFor", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -884,6 +895,7 @@ impl UserData for If {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("If", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -993,6 +1005,7 @@ impl UserData for LocalAssignment {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("LocalAssignment", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -1047,6 +1060,7 @@ impl UserData for LocalFunction {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("LocalFunction", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -1094,6 +1108,7 @@ impl UserData for MethodCall {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("MethodCall", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -1187,6 +1202,7 @@ impl UserData for NumericFor {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("NumericFor", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -1276,6 +1292,7 @@ impl UserData for Return {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("Return", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -1328,6 +1345,7 @@ impl UserData for Repeat {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("Repeat", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -1438,6 +1456,7 @@ impl UserData for TableConstructor {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("TableConstructor", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -1563,6 +1582,7 @@ impl UserData for VarExpression {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("VarExpression", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
@@ -1624,6 +1644,7 @@ impl UserData for While {

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
add_core_meta_methods("While", methods);
add_create_ast_node_methods(methods);
add_print(methods);
}
}
5 changes: 4 additions & 1 deletion full-moon-lua-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -9,4 +9,7 @@ mod prepare_for_lua;
mod shared;
mod visitor;

pub use lua::create_lua;
pub use create_ast_node::CreateAstNode;
pub use lua::*;

pub use crate::core::Ast;
2 changes: 1 addition & 1 deletion full-moon-lua-types/src/lua.rs
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ pub fn full_moon_table(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
lua.create_function(|_, code: String| {
let ast = full_moon::parse(&code).expect("NYI: Error on failure");

Ok(core::Ast::from(ast))
Ok(core::Ast::from(&ast))
})?,
)?;

26 changes: 25 additions & 1 deletion full-moon-lua-types/src/mlua_util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::{Arc, RwLock};

use mlua::{ToLua, UserData};
use full_moon::node::Node;
use mlua::{ToLua, ToLuaMulti, UserData};

use crate::create_ast_node::CreateAstNode;

@@ -23,6 +24,29 @@ pub fn add_core_metamethods_no_tostring<'lua, T: UserData>(
add_newindex_block(name, methods);
}

pub fn add_create_ast_node_methods<'lua, T, N>(methods: &mut impl mlua::UserDataMethods<'lua, T>)
where
T: UserData + CreateAstNode<Node = N>,
N: Node,
{
add_range(methods);
}

pub fn add_range<'lua, T, N>(methods: &mut impl mlua::UserDataMethods<'lua, T>)
where
T: UserData + CreateAstNode<Node = N>,
N: Node,
{
methods.add_method("range", |lua, this, ()| {
let node = this.create_ast_node().unwrap();

match node.range() {
Some((start, end)) => (start.bytes(), end.bytes()).to_lua_multi(lua),
None => mlua::Value::Nil.to_lua_multi(lua),
}
});
}

pub fn add_print<'lua, T, N>(methods: &mut impl mlua::UserDataMethods<'lua, T>)
where
T: UserData + CreateAstNode<Node = N>,
6 changes: 3 additions & 3 deletions full-moon-lua-types/src/prepare_for_lua.rs
Original file line number Diff line number Diff line change
@@ -8,13 +8,13 @@ pub trait PrepareForLua {
fn prepare_for_lua<'lua>(&self, lua: &'lua mlua::Lua) -> mlua::Result<mlua::Value<'lua>>;
}

impl<T: 'static + UserData> PrepareForLua for ArcLocked<T> {
impl<T: 'static + UserData + Send + Sync> PrepareForLua for ArcLocked<T> {
fn prepare_for_lua<'lua>(&self, lua: &'lua mlua::Lua) -> mlua::Result<mlua::Value<'lua>> {
Arc::clone(self).to_lua(lua)
}
}

impl<T: 'static + UserData> PrepareForLua for Vec<ArcLocked<T>> {
impl<T: 'static + UserData + Send + Sync> PrepareForLua for Vec<ArcLocked<T>> {
fn prepare_for_lua<'lua>(&self, lua: &'lua mlua::Lua) -> mlua::Result<mlua::Value<'lua>> {
self.iter()
.map(Arc::clone)
@@ -24,7 +24,7 @@ impl<T: 'static + UserData> PrepareForLua for Vec<ArcLocked<T>> {
}
}

impl<T: 'static + Clone + UserData> PrepareForLua for Box<T> {
impl<T: 'static + Clone + UserData + Send + Sync> PrepareForLua for Box<T> {
fn prepare_for_lua<'lua>(&self, lua: &'lua mlua::Lua) -> mlua::Result<mlua::Value<'lua>> {
(**self).clone().to_lua(lua)
}
Loading