Lua binding's style and compatibility

- Now it support both lua 5.1 and 5.3(5.2 for sure)
- Constant is provided in another way
This commit is contained in:
avaicode 2016-10-26 21:17:12 +08:00
parent a6143a9ad2
commit 52535d7de8
2 changed files with 42 additions and 43 deletions

View File

@ -1,8 +1,10 @@
# Makefile for Judger's lua binding # Makefile for Judger's lua binding
CC = gcc CC = gcc
CFLAGS = -Wall -Werror -O3 -std=c99 -fPIC LDFLAGS ?= -pthread -lseccomp -fPIC -shared
LDFLAGS ?= -pthread -lseccomp -fPIC -shared LUAVER ?= 5.1
LUAVERPURE ?= $(subst .,,${LUAVER})
CFLAGS ?= -Wall -Werror -O3 -std=c99 -fPIC -I/usr/include/lua${LUAVER} -DLUA${LUAVERPURE}
JUDGER_OBJECTS = child.o killer.o logger.o runner.o rules/c_cpp.o rules/general.o JUDGER_OBJECTS = child.o killer.o logger.o runner.o rules/c_cpp.o rules/general.o
OBJECTS = $(addprefix ../../src/,${JUDGER_OBJECTS}) judger.o OBJECTS = $(addprefix ../../src/,${JUDGER_OBJECTS}) judger.o
@ -15,8 +17,8 @@ judger.so: ${OBJECTS}
${CC} ${OBJECTS} ${LDFLAGS} -o judger.so ${CC} ${OBJECTS} ${LDFLAGS} -o judger.so
install: all install: all
@mkdir -p /usr/local/lib/lua/5.3/ @mkdir -p /usr/local/lib/lua/${LUAVER}/
@cp -f judger.so /usr/local/lib/lua/5.3/ @cp -f judger.so /usr/local/lib/lua/${LUAVER}/
clean: clean:
@rm -f ${OBJECTS} judger.so @rm -f ${OBJECTS} judger.so

View File

@ -18,8 +18,8 @@ Usage:
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <lua5.3/lua.h> #include <lua.h>
#include <lua5.3/lauxlib.h> #include <lauxlib.h>
#include "../../src/runner.h" #include "../../src/runner.h"
typedef struct ConfItem { typedef struct ConfItem {
@ -27,27 +27,27 @@ typedef struct ConfItem {
int value; int value;
} ConfItem; } ConfItem;
static ConfItem conf_item[] = { static ConfItem conf_item[] = {
{"VERSION", VERSION}, {"_VERSION", VERSION},
{"UNLIMITED", UNLIMITED}, {"_UNLIMITED", UNLIMITED},
{"RESULT_WRONG_ANSWER", WRONG_ANSWER}, {"_RESULT_WRONG_ANSWER", WRONG_ANSWER},
{"RESULT_SUCCESS", SUCCESS}, {"_RESULT_SUCCESS", SUCCESS},
{"RESULT_CPU_TIME_LIMIT_EXCEEDED", CPU_TIME_LIMIT_EXCEEDED}, {"_RESULT_CPU_TIME_LIMIT_EXCEEDED", CPU_TIME_LIMIT_EXCEEDED},
{"RESULT_REAL_TIME_LIMIT_EXCEEDED", REAL_TIME_LIMIT_EXCEEDED}, {"_RESULT_REAL_TIME_LIMIT_EXCEEDED", REAL_TIME_LIMIT_EXCEEDED},
{"RESULT_MEMORY_LIMIT_EXCEEDED", MEMORY_LIMIT_EXCEEDED}, {"_RESULT_MEMORY_LIMIT_EXCEEDED", MEMORY_LIMIT_EXCEEDED},
{"RESULT_RUNTIME_ERROR", RUNTIME_ERROR}, {"_RESULT_RUNTIME_ERROR", RUNTIME_ERROR},
{"RESULT_SYSTEM_ERROR", SYSTEM_ERROR}, {"_RESULT_SYSTEM_ERROR", SYSTEM_ERROR},
{"ERROR_INVALID_CONFIG", INVALID_CONFIG}, {"_ERROR_INVALID_CONFIG", INVALID_CONFIG},
{"ERROR_FORK_FAILED", FORK_FAILED}, {"_ERROR_FORK_FAILED", FORK_FAILED},
{"ERROR_PTHREAD_FAILED", PTHREAD_FAILED}, {"_ERROR_PTHREAD_FAILED", PTHREAD_FAILED},
{"ERROR_WAIT_FAILED", WAIT_FAILED}, {"_ERROR_WAIT_FAILED", WAIT_FAILED},
{"ERROR_ROOT_REQUIRED", ROOT_REQUIRED}, {"_ERROR_ROOT_REQUIRED", ROOT_REQUIRED},
{"ERROR_LOAD_SECCOMP_FAILED", LOAD_SECCOMP_FAILED}, {"_ERROR_LOAD_SECCOMP_FAILED", LOAD_SECCOMP_FAILED},
{"ERROR_SETRLIMIT_FAILED", SETRLIMIT_FAILED}, {"_ERROR_SETRLIMIT_FAILED", SETRLIMIT_FAILED},
{"ERROR_DUP2_FAILED", DUP2_FAILED}, {"_ERROR_DUP2_FAILED", DUP2_FAILED},
{"ERROR_SETUID_FAILED", SETUID_FAILED}, {"_ERROR_SETUID_FAILED", SETUID_FAILED},
{"ERROR_EXECVE_FAILED", EXECVE_FAILED}, {"_ERROR_EXECVE_FAILED", EXECVE_FAILED},
{"ERROR_SPJ_ERROR", SPJ_ERROR} {"_ERROR_SPJ_ERROR", SPJ_ERROR}
}; };
static const int conf_item_size = sizeof(conf_item) / sizeof(ConfItem); static const int conf_item_size = sizeof(conf_item) / sizeof(ConfItem);
@ -75,11 +75,9 @@ static ArgMask arg_mask[] = {
}; };
static const int arg_mask_size = sizeof(arg_mask) / sizeof(ArgMask); static const int arg_mask_size = sizeof(arg_mask) / sizeof(ArgMask);
static int global_getconfig(lua_State* L);
static int global_judge(lua_State* L); static int global_judge(lua_State* L);
static const struct luaL_Reg judger[] = { static const struct luaL_Reg judger[] = {
{"getconfig", global_getconfig},
{"run", global_judge}, {"run", global_judge},
{NULL, NULL} {NULL, NULL}
}; };
@ -91,20 +89,6 @@ static char* _strdup(const char* s) {
return t; return t;
} }
static int global_getconfig(lua_State* L) {
int i = 0;
lua_createtable(L, 0, conf_item_size);
for (; i < conf_item_size; ++i) {
lua_pushstring(L, conf_item[i].name);
lua_pushnumber(L, conf_item[i].value);
lua_rawset(L, -3);
}
assert(lua_gettop(L) == 1 && lua_type(L, -1) == LUA_TTABLE); //One table left
return 1;
}
static void destory_config(struct config* config) { static void destory_config(struct config* config) {
int i = 0; int i = 0;
@ -218,6 +202,19 @@ static int global_judge(lua_State* L) {
int luaopen_judger(lua_State* L) { int luaopen_judger(lua_State* L) {
int i = 0;
#ifdef LUA51
luaL_register(L, "judger", judger);
#else
luaL_newlib(L, judger); luaL_newlib(L, judger);
#endif
for (; i < conf_item_size; ++i) {
lua_pushstring(L, conf_item[i].name);
lua_pushnumber(L, conf_item[i].value);
lua_rawset(L, -3);
}
return 1; return 1;
} }