Comments (3)
That does look like it'd make lots of bindings much nicer!
from rlua.
For reference, I wrote a macro for registering global constructor functions for my bindings which basically does the alternative proposal:
macro_rules! decl_func_constructor {
($handle: ident: |$ctx: ident| $imp: block) => {
paste::paste! {
fn [<register_ $handle:snake _constructor>]<'lua>(lua: LuaContext<'lua>) -> Result<(), LuaError> {
let globals = lua.globals();
let constructor = lua.create_function(|$ctx: LuaContext, ()| {
$imp
})?;
globals.set(stringify!($handle), constructor)?;
Ok(())
}
}
};
($handle: ident: |$ctx: ident, $($name: ident: $value: ident $( < $($gen: tt),* > )?),*| $imp: block) => {
paste::paste! {
fn [<register_ $handle:snake _constructor>]<'lua>(lua: LuaContext<'lua>) -> Result<(), LuaError> {
let globals = lua.globals();
let constructor = lua.create_function(|$ctx: LuaContext, args: LuaMultiValue| {
let mut args = args.into_iter();
$(
let $name: LuaValue = args.next().ok_or_else(|| LuaError::RuntimeError(
format!("missing '{}' argument in {} constructor; expected a value convertible to {}", stringify!($name), stringify!($handle), stringify!($value))
))?;
let $name: $value$(<$($gen),*>)? = FromLuaMulti::from_lua_multi(LuaMultiValue::from_vec(vec![$name]), $ctx, &mut 0).map_err(|inner| LuaError::CallbackError {
traceback: format!("while converting '{}' argument value", stringify!($name)),
cause: std::sync::Arc::new(inner),
})?;
)*
$imp
})?;
globals.set(stringify!($handle), constructor)?;
Ok(())
}
}
};
($handle: ident: |$ctx: ident, $multi: ident| $imp: block) => {
paste::paste! {
fn [<register_ $handle:snake _constructor>]<'lua>(lua: LuaContext<'lua>) -> Result<(), LuaError> {
let globals = lua.globals();
let constructor = lua.create_function(|$ctx: LuaContext, $multi: LuaMultiValue| {
$imp
})?;
globals.set(stringify!($handle), constructor)?;
Ok(())
}
}
};
}
would require some minor tweaking to wrap add_method(_mut)
but it's a good example.
This would probably have to be tuned to allow FromLuaMulti
as in #287 for the last argument. But I'll try to mix this macro into that PR to provide much better error messages.
The issue with that implementation is that it doesn't handle complex types well (e.g. OneOf<String, LuaTable>
).
from rlua.
FWIW in one of my projects, I use a (declarative) macro which looks like:
wrap_lua!(RServer impl () {
"add_redirect" => server_redirect(mut),
"add_literal" => server_literal(mut),
"add_part" => server_part(mut),
"shutdown" => server_shutdown(mut)
});
where the server_redirect
etc. are the rlua-style functions implementing the method.
I also support traits:
wrap_trait!(MyTrait {
"meth" => mytrait_meth(const),
"othermeth" => mytrait_othermeth(const)
});
wrap_type1(Type1 impl (MyTrait) {
"type1_meth" => type1_meth(const)
});
From Lua a Type1
userdata then has both its own methods and the trait methods.
from rlua.
Related Issues (20)
- Possible bug with __newindex not called on numeric indexing HOT 4
- Question: How to exec multiple things at once? HOT 4
- Segfault on clone of #[repr(C)] userdata subfield HOT 9
- ios build error HOT 7
- examples for error handling HOT 2
- Example for eval return a struct? HOT 1
- Cannot load DLLs in v0.19 HOT 6
- Missing mutable borrows for Table<'lua> HOT 2
- Context.create_sequence_from should support references HOT 1
- Undefined symbol lua_checkstack HOT 8
- Crash on empty source HOT 2
- custom runtime path HOT 3
- creating a `require`-able module HOT 8
- [Request] UserData::from_lua HOT 1
- No documentation on how to use LuaRocks with rlua HOT 3
- Rlua + Luarocks Modules -> Fail to load HOT 3
- Memory leak when using system-luajit HOT 2
- Merge with mlua HOT 32
- [Typo] Example says laoded(instead of loaded) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rlua.