Skip to content

Commit

Permalink
Merge branch 'master' into github-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaka committed Apr 16, 2024
2 parents be52212 + 94f4a80 commit b3d46d1
Show file tree
Hide file tree
Showing 21 changed files with 855 additions and 835 deletions.
57 changes: 0 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,63 +234,6 @@ fn main() {
}
```

### Creating a Lua module

**Note: OBSOLETE ; this is still some pre-Rust-1.0 stuff**

This library also includes a second library named `rust-hl-lua-module` which allows you to create Lua modules in Rust.

To use it, add this to `Cargo.toml`:

```toml
[dependencies.rust-hl-lua-modules]
git = "https://github.com/tomaka/hlua"
```

Then you can use it like this:

```rust
#![feature(phase)]
#[!plugin(rust-hl-lua-modules)]

#[export_lua_module]
pub mod mylib { // <-- must be the name of the Lua module
static PI: f32 = 3.141592;

fn function1(a: int, b: int) -> int {
a + b
}

fn function2(a: int) -> int {
a + 5
}

#[lua_module_init]
fn init() {
println!("module initialized!")
}
}
```

This module will then be usable by Lua:

```lua
> mylib = require("mylib")
module initialized!
> return mylib.function1(2, 4)
6
> return mylib.PI
3.141592
```

Two syntax extensions are defined:
- `#[export_lua_module]`: Must be put in front of a module. The name of the module must be the same as the name of your Lua module.
- `#[lua_module_init]`: Can be put in front of a function inside the module. This function will be executed when the module is loaded.

**Restrictions**:
- `fail!()` will crash the program.
- If you spawn tasks, they will have to end before the hand is given back to lua.

### Contributing

Contributions are welcome!
2 changes: 1 addition & 1 deletion hlua/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hlua"
version = "0.4.1"
version = "0.4.2"
authors = [ "pierre.krieger1708@gmail.com" ]
description = "Zero-cost high-level wrapper for Lua"
keywords = ["lua"]
Expand Down
14 changes: 9 additions & 5 deletions hlua/examples/sound-api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ fn main() {
sound_namespace.set("new", hlua::function0(|| Sound::new()));
}

lua.execute::<()>(r#"
lua.execute::<()>(
r#"
s = Sound.new();
s:play();
Expand All @@ -23,8 +24,9 @@ fn main() {
s:stop();
print("is the sound playing:", s:is_playing());
"#)
.unwrap();
"#,
)
.unwrap();
}

// this `Sound` struct is the object that we will use to demonstrate hlua
Expand All @@ -43,8 +45,10 @@ implement_lua_push!(Sound, |mut metatable| {

index.set("stop", hlua::function1(|snd: &mut Sound| snd.stop()));

index.set("is_playing",
hlua::function1(|snd: &Sound| snd.is_playing()));
index.set(
"is_playing",
hlua::function1(|snd: &Sound| snd.is_playing()),
);
});

// this macro implements the require traits so that we can *read* the object back
Expand Down
Loading

0 comments on commit b3d46d1

Please sign in to comment.