Add #[lua_module_init]

Closes #4
master
Tomaka17 2014-07-13 13:04:12 +02:00
parent 5ab3916bf6
commit 66b73c0ac8
3 changed files with 20 additions and 0 deletions

View File

@ -200,6 +200,11 @@ pub mod mylib { // <-- must be the name of the Lua module
fn function2(a: int) -> int {
a + 5
}
#[lua_module_init]
fn init() {
println!("module initialized!")
}
}
```

View File

@ -11,4 +11,9 @@ pub mod mylib {
fn function1(a: int, b: int) -> int { a + b }
fn function2(a: int) -> int { a + 5 }
#[lua_module_init]
fn init() {
println!("mylib is now loaded!")
}
}

View File

@ -7,6 +7,7 @@ extern crate syntax;
use std::gc::{GC, Gc};
use syntax::parse::token;
use syntax::ast;
use syntax::attr::AttrMetaMethods;
use syntax::ext::build::AstBuilder;
use syntax::ext::base;
use syntax::ext::quote::rt::ToSource;
@ -49,6 +50,7 @@ pub fn expand_lua_module(ecx: &mut base::ExtCtxt, span: codemap::Span,
let module_handler_body: Vec<Gc<ast::Stmt>> = {
let mut module_handler_body = Vec::new();
// iterating over elements inside the module
for moditem in module.items.iter() {
let moditem_name = moditem.ident.to_source();
@ -71,6 +73,14 @@ pub fn expand_lua_module(ecx: &mut base::ExtCtxt, span: codemap::Span,
continue
}
};
// handling lua_module_init
if moditem.attrs.iter().find(|at| at.check_name("lua_module_init")).is_some() {
let moditem_name_item = ecx.ident_of(moditem.ident.to_source().as_slice());
module_handler_body.push(
quote_stmt!(ecx, $moditem_name_item())
);
}
}
module_handler_body