Skip to content

Instantly share code, notes, and snippets.

@bstrie
Last active December 15, 2015 17:29
Show Gist options
  • Save bstrie/5296606 to your computer and use it in GitHub Desktop.
Save bstrie/5296606 to your computer and use it in GitHub Desktop.
Nontrivial Lua binding from Rust, with C equivalent.
#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;
}
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