|
@@ -19,10 +19,6 @@
|
|
#define _CRT_SEQURE_NO_WARNINGS
|
|
#define _CRT_SEQURE_NO_WARNINGS
|
|
#include "la_util.h"
|
|
#include "la_util.h"
|
|
#include "la_interface.h"
|
|
#include "la_interface.h"
|
|
-#include "lua.h"
|
|
|
|
-#include "lauxlib.h"
|
|
|
|
-#include "lualib.h"
|
|
|
|
-#include "luajit.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdlib.h>
|
|
|
|
|
|
@@ -2236,126 +2232,52 @@ void laSpinUnlock(SYSLOCK* lock) {
|
|
//======================================= lua utils
|
|
//======================================= lua utils
|
|
|
|
|
|
static const char *progname = LUA_PROGNAME;
|
|
static const char *progname = LUA_PROGNAME;
|
|
-static int traceback(lua_State *L){
|
|
|
|
|
|
+
|
|
|
|
+static int la_luaTraceback(lua_State *L){
|
|
if (!lua_isstring(L, 1)) { /* Non-string error object? Try metamethod. */
|
|
if (!lua_isstring(L, 1)) { /* Non-string error object? Try metamethod. */
|
|
- if (lua_isnoneornil(L, 1) ||
|
|
|
|
- !luaL_callmeta(L, 1, "__tostring") ||
|
|
|
|
- !lua_isstring(L, -1))
|
|
|
|
- return 1; /* Return non-string error object. */
|
|
|
|
- lua_remove(L, 1); /* Replace object by result of __tostring metamethod. */
|
|
|
|
|
|
+ if (lua_isnoneornil(L, 1) ||
|
|
|
|
+ !luaL_callmeta(L, 1, "__tostring") ||
|
|
|
|
+ !lua_isstring(L, -1))
|
|
|
|
+ return 1; /* Return non-string error object. */
|
|
|
|
+ lua_remove(L, 1); /* Replace object by result of __tostring metamethod. */
|
|
}
|
|
}
|
|
luaL_traceback(L, L, lua_tostring(L, 1), 1);
|
|
luaL_traceback(L, L, lua_tostring(L, 1), 1);
|
|
return 1;
|
|
return 1;
|
|
}
|
|
}
|
|
-
|
|
|
|
-static void l_message(const char *msg){
|
|
|
|
- if (progname) { fputs(progname, stderr); fputc(':', stderr); fputc(' ', stderr); }
|
|
|
|
- fputs(msg, stderr); fputc('\n', stderr);
|
|
|
|
- fflush(stderr);
|
|
|
|
|
|
+static void la_luaMessage(const char *msg){
|
|
|
|
+ if (progname) { logPrint("%s: ",progname); } logPrint("%s\n",msg);
|
|
}
|
|
}
|
|
-static int report(lua_State *L, int status){
|
|
|
|
|
|
+static int la_luaReport(lua_State *L, int status){
|
|
if (status && !lua_isnil(L, -1)) {
|
|
if (status && !lua_isnil(L, -1)) {
|
|
const char *msg = lua_tostring(L, -1);
|
|
const char *msg = lua_tostring(L, -1);
|
|
if (msg == NULL) msg = "(error object is not a string)";
|
|
if (msg == NULL) msg = "(error object is not a string)";
|
|
- l_message(msg);
|
|
|
|
|
|
+ la_luaMessage(msg);
|
|
lua_pop(L, 1);
|
|
lua_pop(L, 1);
|
|
}
|
|
}
|
|
return status;
|
|
return status;
|
|
}
|
|
}
|
|
-static int docall(lua_State *L, int narg, int clear){
|
|
|
|
|
|
+static int la_luaDoCall(lua_State *L, int narg, int clear){
|
|
int status;
|
|
int status;
|
|
int base = lua_gettop(L) - narg; /* function index */
|
|
int base = lua_gettop(L) - narg; /* function index */
|
|
- lua_pushcfunction(L, traceback); /* push traceback function */
|
|
|
|
|
|
+ lua_pushcfunction(L, la_luaTraceback); /* push la_luaTraceback function */
|
|
lua_insert(L, base); /* put it under chunk and args */
|
|
lua_insert(L, base); /* put it under chunk and args */
|
|
|
|
|
|
status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
|
|
status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
|
|
|
|
|
|
- lua_remove(L, base); /* remove traceback function */
|
|
|
|
|
|
+ lua_remove(L, base); /* remove la_luaTraceback function */
|
|
/* force a complete garbage collection in case of errors */
|
|
/* force a complete garbage collection in case of errors */
|
|
if (status != LUA_OK) lua_gc(L, LUA_GCCOLLECT, 0);
|
|
if (status != LUA_OK) lua_gc(L, LUA_GCCOLLECT, 0);
|
|
return status;
|
|
return status;
|
|
}
|
|
}
|
|
-static void write_prompt(lua_State *L, int firstline){
|
|
|
|
- const char *p;
|
|
|
|
- lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
|
|
|
|
- p = lua_tostring(L, -1);
|
|
|
|
- if (p == NULL) p = firstline ? LUA_PROMPT : LUA_PROMPT2;
|
|
|
|
- fputs(p, stdout);
|
|
|
|
- fflush(stdout);
|
|
|
|
- lua_pop(L, 1); /* remove global */
|
|
|
|
-}
|
|
|
|
-static int incomplete(lua_State *L, int status){
|
|
|
|
|
|
+static int la_luaIncomplete(lua_State *L, int status){
|
|
if (status == LUA_ERRSYNTAX) {
|
|
if (status == LUA_ERRSYNTAX) {
|
|
size_t lmsg;
|
|
size_t lmsg;
|
|
const char *msg = lua_tolstring(L, -1, &lmsg);
|
|
const char *msg = lua_tolstring(L, -1, &lmsg);
|
|
const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
|
|
const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
|
|
- if (strstr(msg, LUA_QL("<eof>")) == tp) {
|
|
|
|
- lua_pop(L, 1);
|
|
|
|
- return 1;
|
|
|
|
- }
|
|
|
|
|
|
+ if (strstr(msg, LUA_QL("<eof>")) == tp) { lua_pop(L, 1); return 1; }
|
|
}
|
|
}
|
|
return 0; /* else... */
|
|
return 0; /* else... */
|
|
}
|
|
}
|
|
-static int pushline(lua_State *L, int firstline){
|
|
|
|
- char buf[LUA_MAXINPUT];
|
|
|
|
- write_prompt(L, firstline);
|
|
|
|
- if (fgets(buf, LUA_MAXINPUT, stdin)) {
|
|
|
|
- size_t len = strlen(buf);
|
|
|
|
- if (len > 0 && buf[len-1] == '\n')
|
|
|
|
- buf[len-1] = '\0';
|
|
|
|
- if (firstline && buf[0] == '=')
|
|
|
|
- lua_pushfstring(L, "return %s", buf+1);
|
|
|
|
- else
|
|
|
|
- lua_pushstring(L, buf);
|
|
|
|
- return 1;
|
|
|
|
- }
|
|
|
|
- return 0;
|
|
|
|
-}
|
|
|
|
-static int loadline(lua_State *L){
|
|
|
|
- int status;
|
|
|
|
- lua_settop(L, 0);
|
|
|
|
- if (!pushline(L, 1))
|
|
|
|
- return -1; /* no input */
|
|
|
|
- for (;;) { /* repeat until gets a complete line */
|
|
|
|
- status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
|
|
|
|
- if (!incomplete(L, status)) break; /* cannot try to add lines? */
|
|
|
|
- if (!pushline(L, 0)) /* no more input? */
|
|
|
|
- return -1;
|
|
|
|
- lua_pushliteral(L, "\n"); /* add a new line... */
|
|
|
|
- lua_insert(L, -2); /* ...between the two lines */
|
|
|
|
- lua_concat(L, 3); /* join them */
|
|
|
|
- }
|
|
|
|
- lua_remove(L, 1); /* remove line */
|
|
|
|
- return status;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-static int lalua_Log(lua_State *L) {
|
|
|
|
- int n = lua_gettop(L); int i;
|
|
|
|
- lua_getglobal(L, "tostring");
|
|
|
|
- for (i=1; i<=n; i++) {
|
|
|
|
- const char *s;
|
|
|
|
- lua_pushvalue(L, -1); /* tostring function */
|
|
|
|
- lua_pushvalue(L, i); /* value to print */
|
|
|
|
- lua_call(L, 1, 1);
|
|
|
|
- s = lua_tostring(L, -1); /* get result */
|
|
|
|
- if (s == NULL) return luaL_error(L, LUA_QL("tostring") " must return a string to "LUA_QL("use `log`"));
|
|
|
|
- if (i>1) logPrint(" ");
|
|
|
|
- logPrint(s); lua_pop(L, 1); /* pop result */
|
|
|
|
- }
|
|
|
|
- logPrint("\n");
|
|
|
|
- return 0;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-void la_LuaJITLoadLibs(){
|
|
|
|
- lua_State *L=MAIN.L;
|
|
|
|
- lua_gc(MAIN.L, LUA_GCSTOP, 0);
|
|
|
|
-
|
|
|
|
- luaL_openlibs(MAIN.L);
|
|
|
|
- lua_register(L,"log",lalua_Log);
|
|
|
|
- if(luaL_loadstring(L, LA_LUA_LIB_COMMON) || lua_pcall(L, 0, 0, 0)){ logPrint(" Error loading lagui lua libs"); };
|
|
|
|
-
|
|
|
|
- lua_gc(MAIN.L, LUA_GCRESTART, -1);
|
|
|
|
-}
|
|
|
|
|
|
|
|
int terLoadLine(char* buf, int firstline){
|
|
int terLoadLine(char* buf, int firstline){
|
|
lua_State *L=MAIN.L;
|
|
lua_State *L=MAIN.L;
|
|
@@ -2372,7 +2294,7 @@ int terLoadLine(char* buf, int firstline){
|
|
}
|
|
}
|
|
|
|
|
|
int status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "terLoadLine");
|
|
int status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "terLoadLine");
|
|
- if(incomplete(L,status)){
|
|
|
|
|
|
+ if(la_luaIncomplete(L,status)){
|
|
MAIN.TerminalIncomplete=1;
|
|
MAIN.TerminalIncomplete=1;
|
|
}else{
|
|
}else{
|
|
MAIN.TerminalIncomplete=0;
|
|
MAIN.TerminalIncomplete=0;
|
|
@@ -2380,13 +2302,13 @@ int terLoadLine(char* buf, int firstline){
|
|
}
|
|
}
|
|
|
|
|
|
if(status==LUA_OK && (!MAIN.TerminalIncomplete)){
|
|
if(status==LUA_OK && (!MAIN.TerminalIncomplete)){
|
|
- status = docall(L, 0, 0);
|
|
|
|
- report(L, status);
|
|
|
|
|
|
+ status = la_luaDoCall(L, 0, 0);
|
|
|
|
+ la_luaReport(L, status);
|
|
if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */
|
|
if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */
|
|
lua_getglobal(L, "log");
|
|
lua_getglobal(L, "log");
|
|
lua_insert(L, 1);
|
|
lua_insert(L, 1);
|
|
if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
|
|
if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
|
|
- l_message(lua_pushfstring(L, "error calling " LUA_QL("log") " (%s)",
|
|
|
|
|
|
+ la_luaMessage(lua_pushfstring(L, "error calling " LUA_QL("log") " (%s)",
|
|
lua_tostring(L, -1)));
|
|
lua_tostring(L, -1)));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -2394,8 +2316,34 @@ int terLoadLine(char* buf, int firstline){
|
|
return status;
|
|
return status;
|
|
}
|
|
}
|
|
|
|
|
|
-void la_PrintLuaJITStatus(){
|
|
|
|
- lua_State *L=MAIN.L;
|
|
|
|
|
|
+static int lalua_Log(lua_State *L) {
|
|
|
|
+ int n = lua_gettop(L); int i;
|
|
|
|
+ lua_getglobal(L, "tostring");
|
|
|
|
+ for (i=1; i<=n; i++) {
|
|
|
|
+ const char *s;
|
|
|
|
+ lua_pushvalue(L, -1); /* tostring function */
|
|
|
|
+ lua_pushvalue(L, i); /* value to print */
|
|
|
|
+ lua_call(L, 1, 1);
|
|
|
|
+ s = lua_tostring(L, -1); /* get result */
|
|
|
|
+ if (s == NULL) return luaL_error(L, LUA_QL("tostring") " must return a string to "LUA_QL("use `log`"));
|
|
|
|
+ if (i>1) logPrint(" ");
|
|
|
|
+ logPrint(s); lua_pop(L, 1); /* pop result */
|
|
|
|
+ }
|
|
|
|
+ logPrint("\n");
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void la_luaLoadLibs(lua_State *L){
|
|
|
|
+ lua_gc(L, LUA_GCSTOP, 0);
|
|
|
|
+
|
|
|
|
+ luaL_openlibs(L);
|
|
|
|
+ lua_register(L,"log",lalua_Log);
|
|
|
|
+ if(luaL_loadstring(L, LA_LUA_LIB_COMMON) || lua_pcall(L, 0, 0, 0)){ logPrint(" Error loading lagui lua libs"); };
|
|
|
|
+
|
|
|
|
+ lua_gc(L, LUA_GCRESTART, -1);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void la_luaPrintStatus(lua_State *L){
|
|
logPrint(LUAJIT_VERSION " -- " LUAJIT_COPYRIGHT ". " LUAJIT_URL "\n");
|
|
logPrint(LUAJIT_VERSION " -- " LUAJIT_COPYRIGHT ". " LUAJIT_URL "\n");
|
|
int n; const char *s;
|
|
int n; const char *s;
|
|
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
|
|
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
|
|
@@ -2404,9 +2352,20 @@ void la_PrintLuaJITStatus(){
|
|
n = lua_gettop(L);
|
|
n = lua_gettop(L);
|
|
lua_call(L, 0, LUA_MULTRET);
|
|
lua_call(L, 0, LUA_MULTRET);
|
|
logPrint(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF");
|
|
logPrint(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF");
|
|
- for (n++; (s = lua_tostring(L, n)); n++) {
|
|
|
|
- logPrint("%s ",s);
|
|
|
|
- }
|
|
|
|
- putc('\n', stdout);
|
|
|
|
|
|
+ for (n++; (s = lua_tostring(L, n)); n++) { logPrint("%s ",s); }
|
|
|
|
+ logPrint("\n");
|
|
lua_settop(L, 0); /* clear stack */
|
|
lua_settop(L, 0); /* clear stack */
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+void la_luaDumpStack(lua_State *L){
|
|
|
|
+ int top=lua_gettop(L); for (int i=1; i <= top; i++) {
|
|
|
|
+ printf("%d\t%s\t", i, luaL_typename(L,i));
|
|
|
|
+ switch (lua_type(L, i)) {
|
|
|
|
+ case LUA_TNUMBER: printf("%g\n",lua_tonumber(L,i)); break;
|
|
|
|
+ case LUA_TSTRING: printf("%s\n",lua_tostring(L,i)); break;
|
|
|
|
+ case LUA_TBOOLEAN: printf("%s\n", (lua_toboolean(L, i) ? "true" : "false")); break;
|
|
|
|
+ case LUA_TNIL: printf("%s\n", "nil"); break;
|
|
|
|
+ default: printf("%p\n",lua_topointer(L,i)); break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|