A wrapper around rlua and/or mlua to help with embedding teal
tealr adds some traits that replace/extend those from rlua and mlua,
allowing it to generate the .d.tl
files needed for teal.
It also contains some macro's to make it easier to load/execute teal scripts.
Both rlua
and mlua
are behind feature flags with the same name.
It also reexports these crates and allows you to set flags through it (the forwarded flags are the same with either the prefix rlua_
or mlua_
. For example if you want to enable mlua/async
then you need to enable tealr/mlua_async
)
Exposing types to teal as userdata is almost the same using tealr as it is using rlua and mlua ```rust ignore use tealr::TypeName;
struct ExampleRlua {}
//now, implement rlu::TealData.
//This tells rlua what methods are available and tealr what the types are
impl tealr::rlu::TealData for ExampleRlua {
//implement your methods/functions
fn addmethods<'lua, T: tealr::rlu::TealDataMethods<'lua, Self>>(methods: &mut T) {
methods.addmethod("examplemethod", |, , x: i8| Ok(x));
methods.addmethodmut("examplemethodmut", |, , x: (i8, String)| Ok(x.1));
methods.addfunction("examplefunction", |, x: Vec
Working with mlua is almost the same
rust ignore
use tealr::TypeName;
struct ExampleMlua {}
impl tealr::mlu::TealData for ExampleMlua {
//implement your methods/functions
fn addmethods<'lua, T: tealr::mlu::TealDataMethods<'lua, Self>>(methods: &mut T) {
methods.addmethod("examplemethod", |, , x: i8| Ok(x));
methods.addmethodmut("examplemethodmut", |, , x: (i8, String)| Ok(x.1));
methods.addfunction("examplefunction", |, x: Vec
Creating of the .d.tl
files works the same for rlua or mlua
```rust
//set your type up with either rlua or mlua
use tealr::{TypeName};
use tealr::{MluaUserData,mlu::TealData};
use tealr::{RluaUserData,rlu::TealData};
struct Example {} impl TealData for Example {};
//time to create the type definitions
let filecontents = tealr::TypeWalker::new()
.processtype::
//write the output to a file println!("{}",file_contents) ```
As you get a string containing the lua code back this feature works the same for both rlua and mlua
rust
use tealr::compile_inline_teal;
let code = compile_inline_teal!("local x : number = 5 return x");
It is possible to embed the teal compiler into your rust application. This makes it possible to easily load it into the lua vm thus allowing it to run teal files as normal lua files.
```rust no_run
{ use tealr::embedcompiler; let compiler = embedcompiler!("v0.13.1"); let res : u8 = rlua::Lua::new().context(|ctx| { let code = compiler("example/basictealfile"); ctx.load(&code).setname("embeddedcompiler")?.eval() })?; };
{
use tealr::embedcompiler;
let compiler = embedcompiler!("v0.13.1");
let lua = mlua::Lua::new();
let code = compiler("example/basictealfile");
let res: u8 = lua.load(&code).setname("embeddedcompiler")?.eval()?;
};
Ok::<(), Box
There are a few sources tealr can use to get the compiler. If no source is specified it defaults to github releases.
Other sources can be specified as follows:
rust ignore
//get the teal compiler using the given path
embedcompiler!(Local(path = "some/path/to/tl.tl"));
//this uses luarocks to try and discover the location of the compiler
embedcompiler!(Local());
//download the compiler at compile time from github (default)
embedcompiler!(GitHub(version = "v0.13.1"));
//download the compiler at compile time from luarocks
embedcompiler!(Luarocks(version = "v0.13.1"));
```
You can find longer ones with comments on what each call does here
Tealr can already help with 2 ways to run teal scripts.
It can compile inline teal code at the same time as your rust code
It can also embed the teal compiler for you, allowing you to execute external teal scripts like normal lua scripts.
There is a third method I want tealr to help with. In this mode, it will compile a teal project, pack it into 1 file and embed it into the project.