-
-
Save bstrie/5296606 to your computer and use it in GitHub Desktop.
Nontrivial Lua binding from Rust, with C equivalent.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <lua.h> | |
#include <lauxlib.h> | |
#include <lualib.h> | |
int main() | |
{ | |
lua_State *L = luaL_newstate(); | |
if (luaL_dostring(L, "function foo (x,y) return x+y end")) exit(1); | |
lua_getglobal(L, "foo"); | |
lua_pushinteger(L, 5); | |
lua_pushinteger(L, 3); | |
lua_call(L, 2, 1); | |
printf("Result: %d\n", lua_tointeger(L, -1)); | |
lua_close(L); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mod lua52; | |
fn main() { | |
let lua = lua52::LuaState::new(); | |
if lua.L_dostring("function foo (x,y) return x+y end") { | |
fail!(); | |
} | |
lua.getglobal("foo"); | |
lua.push(5); | |
lua.push(3); | |
lua.call(2, 1); | |
println(fmt!("Result: %?", lua.tointeger(-1))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment