Browse files

Add Lua bindings for getcounter and setcounter. (The latter segfaults

for some reason, but seems to at least end up in the right part of
the code.)
  • Loading branch information...
1 parent 44a6916 commit 37966188c8d87ecd9f85a05a5d55d2fadf8b607a nooodl committed Jul 12, 2013
Showing with 57 additions and 11 deletions.
  1. +9 −0 mzx.lua
  2. +4 −2 src/counter.c
  3. +42 −8 src/lua_bindings.c
  4. +2 −1 src/lua_bindings.h
View
9 mzx.lua
@@ -0,0 +1,9 @@
+C = {}
+setmetatable(C, {
+ __index = function (table, key)
+ return mzx.getcounter(key)
+ end,
+ __newindex = function (table, key, value)
+ mzx.setcounter(key, value)
+ end
+})
View
6 src/counter.c
@@ -3062,7 +3062,7 @@ int set_counter_special(struct world *mzx_world, char *char_value,
#ifdef CONFIG_LUA
case FOPEN_LUA_FILE:
{
- FILE *lua_file = fsafeopen(char_value, "r");
+ FILE *lua_file = fsafeopen(char_value, "rb");
if(lua_file)
{
@@ -3075,7 +3075,9 @@ int set_counter_special(struct world *mzx_world, char *char_value,
char *lua_code = malloc(lua_size + 1);
fread(lua_code, lua_size, 1, lua_file);
- run_lua(mzx_world, cur_robot, lua_code);
+ lua_code[lua_size] = '\0';
+
+ run_lua(mzx_world, cur_robot, lua_code, id);
free(lua_code);
}
View
50 src/lua_bindings.c
@@ -18,42 +18,76 @@
// Lua bindings for the MegaZeux world.
-// #include "counter.h"
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
+
+#include "counter.h"
#include "lua_bindings.h"
-#include "robot_struct.h"
#include "world_struct.h"
int l_getcounter(lua_State *L)
{
- // TODO
- lua_pushnumber(L, 42);
+ struct world *mzx_world;
+ int id;
+ const char *name = luaL_checkstring(L, 1);
+
+ lua_getglobal(L, "___MZX_WORLD");
+ mzx_world = lua_touserdata(L, -1);
+ lua_getglobal(L, "___ID");
+ id = (int)lua_tointeger(L, -1);
+
+ lua_pushnumber(L, get_counter(mzx_world, name, id));
return 1;
}
+int l_setcounter(lua_State *L)
+{
+ struct world *mzx_world;
+ int id;
+ const char *name = luaL_checkstring(L, 1);
+ const int value = luaL_checknumber(L, 2);
+
+ lua_getglobal(L, "___MZX_WORLD");
+ mzx_world = lua_touserdata(L, -1);
+ lua_getglobal(L, "___ID");
+ id = (int)lua_tointeger(L, -1);
+
+ set_counter(mzx_world, name, value, id);
+ return 0;
+}
+
static const luaL_Reg _mzx[] = {
{ "getcounter", l_getcounter },
+ { "setcounter", l_setcounter },
{ NULL, NULL }
};
int run_lua(struct world *mzx_world, struct robot *cur_robot,
- const char *lua_code)
+ const char *lua_code, int id)
{
lua_State *L = luaL_newstate();
int lua_error;
+ struct world **mzx_world_p;
luaL_openlibs(L);
+ mzx_world_p = (struct world **)lua_newuserdata(L, sizeof(mzx_world));
+ *mzx_world_p = mzx_world;
+ lua_setglobal(L, "___MZX_WORLD");
+
+ lua_pushinteger(L, id);
+ lua_setglobal(L, "___ID");
+
luaL_newlib(L, _mzx);
lua_setglobal(L, "mzx");
- lua_error = luaL_loadbuffer(L, lua_code, strlen(lua_code),
- cur_robot->robot_name) || lua_pcall(L, 0, 0, 0);
+ fprintf(stderr, "Code: [%s]\n", lua_code);
+ lua_error = luaL_dofile(L, "mzx.lua")
+ || luaL_loadbuffer(L, lua_code, strlen(lua_code), cur_robot->robot_name)
+ || lua_pcall(L, 0, 0, 0);
if (lua_error) {
- fprintf(stderr, "Code: [%s]\n", lua_code);
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
View
3 src/lua_bindings.h
@@ -30,9 +30,10 @@ __M_BEGIN_DECLS
#include <lualib.h>
int l_getcounter(lua_State *L);
+int l_setcounter(lua_State *L);
int luaopen_mzx(lua_State *L);
int run_lua(struct world *mzx_world, struct robot *cur_robot,
- const char *lua_code);
+ const char *lua_code, int id);
#endif

0 comments on commit 3796618

Please sign in to comment.