Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add lua_bindings framework.

  • Loading branch information...
commit 44a6916a7882b0389c1b3bbf4b6d0023f56c55f4 1 parent 9fb8e04
nooodl authored
View
4 src/Makefile.in
@@ -72,6 +72,10 @@ core_cobjs := \
${core_obj}/sprite.o ${core_obj}/util.o ${core_obj}/validation.o \
${core_obj}/window.o ${core_obj}/world.o
+ifeq (${BUILD_LUA},1)
+core_cobjs += ${core_obj}/lua_bindings.o
+endif
+
#
# Lists mandatory C++ language sources (mangled to object names) required
# to build the main binary. Currently there are only optional sources.
View
30 src/counter.c
@@ -44,9 +44,7 @@
#include "util.h"
#ifdef CONFIG_LUA
-#include "lua.h"
-#include "lauxlib.h"
-#include "lualib.h"
+#include "lua_bindings.h"
#endif
#ifdef CONFIG_UTHASH
@@ -4378,29 +4376,3 @@ void free_string_list(struct string **string_list, int num_strings)
free(string_list);
}
-#ifdef CONFIG_LUA
-int run_lua(struct world *mzx_world, struct robot *cur_robot,
- const char *lua_code)
-{
- lua_State *L = luaL_newstate();
- int lua_error;
-
- luaL_openlibs(L);
- lua_newtable(L);
- // TODO
- // luaL_setfuncs(L, mzx_lua_funcs, 0);
- lua_setglobal(L, "mzx");
-
- lua_error = 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);
- }
-
- return lua_error;
-}
-#endif
-
View
2  src/counter.h
@@ -65,8 +65,6 @@ void save_counter(FILE *fp, struct counter *src_counter);
void save_string(FILE *fp, struct string *src_string);
void free_counter_list(struct counter **counter_list, int num_counters);
void free_string_list(struct string **string_list, int num_strings);
-int run_lua(struct world *mzx_world, struct robot *cur_robot,
- const char *lua_code);
// Even old games tended to use at least this many.
#define MIN_COUNTER_ALLOCATE 32
View
63 src/lua_bindings.c
@@ -0,0 +1,63 @@
+/* MegaZeux
+ *
+ * Copyright (C) 2013 Mauris Van Hauwe <0nooodl@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+// Lua bindings for the MegaZeux world.
+
+// #include "counter.h"
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+#include "lua_bindings.h"
+#include "robot_struct.h"
+#include "world_struct.h"
+
+int l_getcounter(lua_State *L)
+{
+ // TODO
+ lua_pushnumber(L, 42);
+ return 1;
+}
+
+static const luaL_Reg _mzx[] = {
+ { "getcounter", l_getcounter },
+ { NULL, NULL }
+};
+
+int run_lua(struct world *mzx_world, struct robot *cur_robot,
+ const char *lua_code)
+{
+ lua_State *L = luaL_newstate();
+ int lua_error;
+
+ luaL_openlibs(L);
+
+ 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);
+
+ if (lua_error) {
+ fprintf(stderr, "Code: [%s]\n", lua_code);
+ fprintf(stderr, "%s\n", lua_tostring(L, -1));
+ lua_pop(L, 1);
+ }
+
+ return lua_error;
+}
+
View
38 src/lua_bindings.h
@@ -0,0 +1,38 @@
+/* MegaZeux
+ *
+ * Copyright (C) 2013 Mauris Van Hauwe <0nooodl@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef __LUA_H
+#define __LUA_H
+
+#include "compat.h"
+
+__M_BEGIN_DECLS
+
+#include "world_struct.h"
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+
+int l_getcounter(lua_State *L);
+int luaopen_mzx(lua_State *L);
+int run_lua(struct world *mzx_world, struct robot *cur_robot,
+ const char *lua_code);
+
+#endif
+
Please sign in to comment.
Something went wrong with that request. Please try again.