Skip to content

Commit

Permalink
added lua shell and reworked lua binding
Browse files Browse the repository at this point in the history
  • Loading branch information
cnvogelg committed Apr 8, 2015
1 parent 5e7cc11 commit 201a758
Show file tree
Hide file tree
Showing 31 changed files with 1,863 additions and 333 deletions.
10 changes: 8 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ libuae_a_SOURCES = \
src/include/isofs_api.h \
src/include/keyboard.h \
src/include/keybuf.h \
src/include/lualibuae.h \
src/include/luascript.h \
src/include/mackbd.h \
src/include/mmu_common.h \
Expand Down Expand Up @@ -577,6 +578,7 @@ libfsemu_a_SOURCES = \
libfsemu/include/fs/ml.h \
libfsemu/include/fs/ml/opengl.h \
libfsemu/include/fs/ml/options.h \
libfsemu/include/fs/net.h \
libfsemu/include/fs/ref.h \
libfsemu/include/fs/thread.h \
libfsemu/include/fs/time.h \
Expand Down Expand Up @@ -610,6 +612,8 @@ libfsemu_a_SOURCES = \
libfsemu/src/emu/input.h \
libfsemu/src/emu/keynames.c \
libfsemu/src/emu/libfsemu.h \
libfsemu/src/emu/lua_shell.c \
libfsemu/src/emu/lua_shell.h \
libfsemu/src/emu/menu.c \
libfsemu/src/emu/menu.h \
libfsemu/src/emu/netplay.c \
Expand Down Expand Up @@ -828,6 +832,7 @@ if WITH_LUA
noinst_LIBRARIES += liblua.a
AM_CPPFLAGS += -I$(s)/lua/src
fs_uae_LDADD += liblua.a
fs_uae_device_helper_LDADD += liblua.a
liblua_a_SOURCES = \
lua/src/lapi.c \
lua/src/lapi.h \
Expand Down Expand Up @@ -886,8 +891,9 @@ liblua_a_SOURCES = \
lua/src/lzio.c \
lua/src/lzio.h
fs_uae_SOURCES += \
src/fs-uae/luascript.c \
src/luascript.cpp
src/fs-uae/lualibfsuae.c \
src/luascript.cpp \
src/lualibuae.cpp
libfsemu_a_SOURCES += \
libfsemu/src/emu/emu_lua.c
endif
Expand Down
10 changes: 1 addition & 9 deletions libfsemu/include/fs/emu.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <fs/log.h>
#include <fs/conf.h>
#include <fs/ml.h>
#include <fs/emu_lua.h>

#if 0

Expand Down Expand Up @@ -35,15 +36,6 @@
extern "C" {
#endif

#ifdef WITH_LUA
#include <lauxlib.h>
lua_State *fs_emu_get_lua_state(void);
void fs_emu_acquire_lua(void);
void fs_emu_release_lua(void);
#endif

void fs_emu_lua_run_handler(const char *name);

// Can (or should) be called before fs_emu_init

#define fs_emu_log fs_log
Expand Down
39 changes: 39 additions & 0 deletions libfsemu/include/fs/emu_lua.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef LIBFSEMU_EMU_LUA_H_
#define LIBFSEMU_EMU_LUA_H_

#ifdef WITH_LUA

#include <lauxlib.h>

typedef struct fs_emu_lua_binding {
int (*run_handler)(const char *name);
int (*run_script)(const char *path);
lua_State *(*create_state)(void);
void (*destroy_state)(lua_State *);
void (*lock_state)(lua_State *);
void (*unlock_state)(lua_State *);
} fs_emu_lua_binding;

typedef int (*fs_emu_lua_func)(lua_State *);

// bind lua interpreter
extern void fs_emu_lua_set_binding(fs_emu_lua_binding *);
extern void fs_emu_lua_bind(void);
extern void fs_emu_lua_unbind(void);

// register functions
extern void fs_emu_lua_init(void);
extern void fs_emu_lua_register_func(const char *name, fs_emu_lua_func f);
extern int luaopen_fsemulib(lua_State *state);

extern lua_State *fs_emu_lua_create_state(void);
extern void fs_emu_lua_destroy_state(lua_State *);
extern void fs_emu_lua_lock_state(lua_State *);
extern void fs_emu_lua_unlock_state(lua_State *);

#endif

extern int fs_emu_lua_run_handler(const char *name);
extern int fs_emu_lua_run_script(const char *path);

#endif // LIBFSEMU_EMU_LUA_H_
11 changes: 11 additions & 0 deletions libfsemu/include/fs/net.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifdef WINDOWS
#include <Winsock2.h>
#include <Ws2tcpip.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
#endif
2 changes: 1 addition & 1 deletion libfsemu/src/emu/emu.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
#include <fs/ml.h>
#include <fs/thread.h>
#include <fs/time.h>
#include <fs/emu_lua.h>

#ifdef USE_GLIB
#include <glib.h>
#endif

#include "audio.h"
#include "dialog.h"
#include "emu_lua.h"
#include "hud.h"
#include "input.h"
#include "libfsemu.h"
Expand Down
203 changes: 163 additions & 40 deletions libfsemu/src/emu/emu_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,36 @@

#ifdef WITH_LUA

#include <string.h>

#include <fs/emu.h>
#include "emu_lua.h"
#include <fs/emu_lua.h>
#include <fs/log.h>
#include <fs/thread.h>
#include "lua_shell.h"

#ifdef __cplusplus
extern "C" {
#endif
#include <lualib.h>
#ifdef __cplusplus
}
#endif

static fs_mutex *g_mutex;
lua_State *fs_emu_lua_state = NULL;
#define MAX_LUA_FUNCS 32
typedef struct lua_func {
const char *name;
fs_emu_lua_func func;
} lua_func;

void fs_emu_lua_run_handler(const char *name) {
lua_State **L = &fs_emu_lua_state;
while(*L) {
fs_emu_acquire_lua();
lua_getglobal(*L, name);
if (lua_isnil(*L, -1)) {
//lua_pop(*L, 1);
}
else if (lua_pcall(*L, 0, 0, 0) != 0) {
fs_emu_lua_log_error(name);
//lua_pop(*L, 1);
}
lua_settop(*L, 0);
fs_emu_release_lua();
#define MAX_POSTPONED_HANDLERS 16
static const char *g_postponed_handlers[MAX_POSTPONED_HANDLERS];
static int g_num_postponed_handlers;

//L++;
break;
}
}

lua_State *fs_emu_get_lua_state(void) {
return fs_emu_lua_state;
}
static int g_num_func = 0;
static lua_func g_func_table[MAX_LUA_FUNCS];
static fs_emu_lua_binding *g_binding = NULL;
static int g_is_bound = 0;

static int l_fs_emu_log(lua_State *L) {
int n = lua_gettop(L);
Expand All @@ -49,32 +47,157 @@ static int l_fs_emu_log(lua_State *L) {
return 0;
}

void fs_emu_lua_log_error(const char *msg) {
fs_log("%s: %s\n", msg, lua_tostring(fs_emu_lua_state, -1));
printf("%s: %s\n", msg, lua_tostring(fs_emu_lua_state, -1));
void fs_emu_lua_init(void) {
fs_log("lua-fs: init\n");
fs_emu_lua_register_func("log", l_fs_emu_log);
}

void fs_emu_lua_set_binding(fs_emu_lua_binding *b)
{
g_binding = b;
}

void fs_emu_acquire_lua(void) {
fs_mutex_lock(g_mutex);
void fs_emu_lua_bind(void)
{
if((g_binding != NULL) && !g_is_bound) {
g_is_bound = 1;
fs_log("lua-fs: bound to emu\n");

// recall postponed handlers?
for(int i=0;i<g_num_postponed_handlers;i++) {
const char *name = g_postponed_handlers[i];
fs_log("lua-fs: calling postponed handler '%s'\n", name);
g_binding->run_handler(name);
}
g_num_postponed_handlers = 0;

// setup lua shell if configured
fs_emu_lua_shell_init();
} else {
fs_log("lua-fs: ERROR binding to emu!\n");
}
}

void fs_emu_release_lua(void) {
fs_mutex_unlock(g_mutex);
void fs_emu_lua_unbind(void)
{
if(g_is_bound) {
// shutdown lua shell if configured
fs_emu_lua_shell_free();

g_is_bound = 0;
fs_log("lua-fs: unbound from emu\n");
} else {
fs_log("lua-fs: ERROR unbinding from emu!\n");
}
}

void fs_emu_lua_init(void) {
fs_log("fs_emu_lua_init\n");
fs_emu_lua_state = luaL_newstate();
fs_log("new lua_State at %p\n", fs_emu_lua_state);
luaL_openlibs(fs_emu_lua_state);
lua_register(fs_emu_lua_state, "fs_emu_log", l_fs_emu_log);
g_mutex = fs_mutex_create();
void fs_emu_lua_register_func(const char *name, fs_emu_lua_func func)
{
if(g_num_func < MAX_LUA_FUNCS) {
lua_func *f = &g_func_table[g_num_func++];
f->name = name;
f->func = func;
fs_log("lua-fs: registered function '%s'\n", name);
} else {
fs_log("lua-fs: ERROR registering function '%s'\n", name);
}
}

int luaopen_fsemulib(lua_State *L)
{
fs_log("lua-fs: setup state %p with %d 'fsemu' functions\n", L, g_num_func);

// create "fsemu" table
lua_newtable(L);
for(int i=0;i<g_num_func;i++) {
lua_func *f = &g_func_table[i];
lua_pushstring(L, f->name);
lua_pushcfunction(L, f->func);
lua_settable(L, -3);
}
return 1;
}

lua_State *fs_emu_lua_create_state(void)
{
if(g_is_bound) {
return g_binding->create_state();
} else {
fs_log("lua-fs: not bound: can't create state\n");
return NULL;
}
}

void fs_emu_lua_destroy_state(lua_State *state)
{
if(g_is_bound) {
g_binding->destroy_state(state);
} else {
fs_log("lua-fs: not bound: can't destroy state\n");
}
}

void fs_emu_lua_lock_state(lua_State *state)
{
if(g_is_bound) {
g_binding->lock_state(state);
} else {
fs_log("lua-fs: not bound: can't lock state\n");
}
}

void fs_emu_lua_unlock_state(lua_State *state)
{
if(g_is_bound) {
g_binding->unlock_state(state);
} else {
fs_log("lua-fs: not bound: can't unlock state\n");
}
}

int fs_emu_lua_run_handler(const char *name) {
if(g_is_bound) {
return g_binding->run_handler(name);
} else {
// try to postpone handler
for(int i=0;i<g_num_postponed_handlers;i++) {
// already postponed?
if(strcmp(g_postponed_handlers[i], name)==0) {
return 0;
}
}
// no more slots free?
if(g_num_postponed_handlers == MAX_POSTPONED_HANDLERS) {
fs_log("lua-fs: ignoring handler '%s'. no slots free!\n", name);
return -1;
}
// store in slot
g_postponed_handlers[g_num_postponed_handlers++] = name;
fs_log("lua-fs: not bound: postpone handler '%s'\n", name);
return 0;
}
}

int fs_emu_lua_run_script(const char *path)
{
if(g_is_bound) {
return g_binding->run_script(path);
} else {
fs_log("lua-fs: not bound: ignoring script '%s'\n", path);
return 0;
}
}

#else

void fs_emu_lua_run_handler(const char *name) {
int fs_emu_lua_run_handler(const char *name) {
// do nothing
return 0;
}

int fs_emu_lua_run_script(const char *path) {
// do nothing
return 0;
}

#endif
15 changes: 0 additions & 15 deletions libfsemu/src/emu/emu_lua.h

This file was deleted.

Loading

0 comments on commit 201a758

Please sign in to comment.