diff --git a/src/Makefile b/src/Makefile index 3e53d54..d26e205 100644 --- a/src/Makefile +++ b/src/Makefile @@ -20,34 +20,43 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +UNAME := $(shell uname) + CC?=cc -CFLAGS=-std=gnu11 -O3 -Wall -Wextra -Werror -Wno-unused-parameter +CFLAGS=-std=gnu11 -O3 -Wall -Wextra -Werror -Wno-unused-parameter -I/usr/include LDFLAGS=-L/usr/local/lib +LUA_LIBS=-llua +ifeq ($(UNAME), Linux) + LUA_LIBS = -llua5.1 + CFLAGS += -I/usr/include/lua5.1 + LDFLAGS += -L/usr/lib/arm-linux-gnueabihf +endif + EWM_EXECUTABLE=ewm EWM_SOURCES=cpu.c ins.c pia.c mem.c ewm.c fmt.c two.c scr.c dsk.c chr.c alc.c one.c tty.c utl.c boo.c lua.c EWM_OBJECTS=$(EWM_SOURCES:.c=.o) -EWM_LIBS=-lSDL2 -llua +EWM_LIBS=-lSDL2 $(LUA_LIBS) CPU_TEST_EXECUTABLE=cpu_test CPU_TEST_SOURCES=cpu.c ins.c mem.c fmt.c utl.c cpu_test.c lua.c CPU_TEST_OBJECTS=$(CPU_TEST_SOURCES:.c=.o) -CPU_TEST_LIBS=-llua +CPU_TEST_LIBS=$(LUA_LIBS) SCR_TEST_EXECUTABLE=scr_test SCR_TEST_SOURCES=cpu.c ins.c mem.c fmt.c two.c scr.c dsk.c chr.c alc.c utl.c scr_test.c lua.c SCR_TEST_OBJECTS=$(SCR_TEST_SOURCES:.c=.o) -SCR_TEST_LIBS=-lSDL2 -llua +SCR_TEST_LIBS=-lSDL2 $(LUA_LIBS) CPU_BENCH=cpu_bench CPU_BENCH_SOURCES=cpu.c ins.c mem.c fmt.c utl.c cpu_bench.c lua.c CPU_BENCH_OBJECTS=$(CPU_BENCH_SOURCES:.c=.o) -CPU_BENCH_LIBS=-llua +CPU_BENCH_LIBS=$(LUA_LIBS) MEM_BENCH=mem_bench MEM_BENCH_SOURCES=cpu.c ins.c mem.c fmt.c utl.c mem_bench.c lua.c MEM_BENCH_OBJECTS=$(MEM_BENCH_SOURCES:.c=.o) -MEM_BENCH_LIBS=-llua +MEM_BENCH_LIBS=$(LUA_LIBS) all: $(EWM_SOURCES) $(EWM_EXECUTABLE) $(CPU_TEST_SOURCES) $(CPU_TEST_EXECUTABLE) $(SCR_TEST_EXECUTABLE) $(CPU_BENCH) $(MEM_BENCH) diff --git a/src/cpu.c b/src/cpu.c index 2b6e9b1..548bca2 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -148,7 +148,7 @@ static int cpu_execute_instruction(struct cpu_t *cpu) { lua_pushinteger(cpu->lua->state, mem_get_word(cpu, pc+1)); break; } - if (lua_pcall(cpu->lua->state, 3, 0, 0) != LUA_OK) { + if (lua_pcall(cpu->lua->state, 3, 0, 0) != 0) { printf("cpu: script error: %s\n", lua_tostring(cpu->lua->state, -1)); } } @@ -181,7 +181,7 @@ static int cpu_execute_instruction(struct cpu_t *cpu) { lua_pushinteger(cpu->lua->state, mem_get_word(cpu, pc+1)); break; } - if (lua_pcall(cpu->lua->state, 3, 0, 0) != LUA_OK) { + if (lua_pcall(cpu->lua->state, 3, 0, 0) != 0) { printf("cpu: script error: %s\n", lua_tostring(cpu->lua->state, -1)); } } diff --git a/src/dsk.c b/src/dsk.c index 761222e..95c1cf7 100644 --- a/src/dsk.c +++ b/src/dsk.c @@ -557,22 +557,7 @@ int ewm_dsk_set_disk_file(struct ewm_dsk_t *dsk, uint8_t drive, bool readonly, c // Lua Support -static int ewm_dsk_lua_hello(lua_State *lua) { - printf("This is dsk.hello()\n"); - return 0; -} - -int ewm_dsk_luaopen(lua_State *state) { - luaL_Reg functions[] = { - {"hello", ewm_dsk_lua_hello}, - {NULL, NULL} - }; - luaL_newlib(state, functions); - return 1; -} - int ewm_dsk_init_lua(struct ewm_dsk_t *dsk, struct ewm_lua_t *lua) { dsk->lua = lua; - luaL_requiref(dsk->lua->state, "dsk", ewm_dsk_luaopen, 0); return 0; } diff --git a/src/lua.c b/src/lua.c index 45ed841..19daf0c 100644 --- a/src/lua.c +++ b/src/lua.c @@ -49,7 +49,7 @@ struct ewm_lua_t *ewm_lua_create() { } int ewm_lua_load_script(struct ewm_lua_t *lua, char *script_path) { - if (luaL_dofile(lua->state, script_path) != LUA_OK) { + if (luaL_dofile(lua->state, script_path) != 0) { printf("ewm: script error: %s\n", lua_tostring(lua->state, -1)); return -1; } @@ -92,3 +92,16 @@ void ewm_lua_register_component(struct ewm_lua_t *lua, char *name, luaL_Reg *fun luaL_newmetatable(lua->state, table_name); luaL_setfuncs(lua->state, functions, 0); } + +void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { + luaL_checkstack(L, nup+1, "too many upvalues"); + for (; l->name != NULL; l++) { /* fill the table with given functions */ + int i; + lua_pushstring(L, l->name); + for (i = 0; i < nup; i++) /* copy upvalues to the top */ + lua_pushvalue(L, -(nup+1)); + lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ + lua_settable(L, -(nup + 3)); + } + lua_pop(L, nup); /* remove upvalues */ +} diff --git a/src/lua.h b/src/lua.h index 4cb8118..390858b 100644 --- a/src/lua.h +++ b/src/lua.h @@ -44,4 +44,6 @@ void ewm_lua_push_dsk(struct ewm_lua_t *lua, struct ewm_dsk_t *dsk); void ewm_lua_register_component(struct ewm_lua_t *lua, char *name, luaL_Reg *functions); +void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup); + #endif // LUA_H diff --git a/src/two.c b/src/two.c index d7023c0..dcf8655 100644 --- a/src/two.c +++ b/src/two.c @@ -497,7 +497,7 @@ static bool ewm_two_poll_event(struct ewm_two_t *two, SDL_Window *window) { // T ewm_lua_push_two(two->lua, two); lua_pushinteger(two->lua->state, event.key.keysym.mod); lua_pushinteger(two->lua->state, event.key.keysym.sym); - if (lua_pcall(two->lua->state, 3, 1, 0) != LUA_OK) { + if (lua_pcall(two->lua->state, 3, 1, 0) != 0) { printf("two: script error: %s\n", lua_tostring(two->lua->state, -1)); return true; } @@ -571,7 +571,7 @@ static bool ewm_two_poll_event(struct ewm_two_t *two, SDL_Window *window) { // T ewm_lua_push_two(two->lua, two); lua_pushinteger(two->lua->state, event.key.keysym.mod); lua_pushinteger(two->lua->state, event.key.keysym.sym); - if (lua_pcall(two->lua->state, 3, 1, 0) != LUA_OK) { + if (lua_pcall(two->lua->state, 3, 1, 0) != 0) { printf("two: script error: %s\n", lua_tostring(two->lua->state, -1)); return true; }