int luahelper_taskadd(const char* taskscript) { int ret; int ref; size_t taskscriptlen = strlen(taskscript); char* newTaskScript; ls_lua_init(); ls_lua_getfield(L, LUA_GLOBALSINDEX, "lanes"); /* lanes */ ls_lua_getfield(L, -1, "gen"); /* lanes gen */ ls_lua_pushstring(L, "*"); /* lanes gen * */ newTaskScript = malloc( taskscriptlen + 1 ); strncpy(newTaskScript, taskscript, taskscriptlen); newTaskScript[taskscriptlen] = 0; ret = ls_luaL_loadstring(L, newTaskScript); /* lanes gen * script */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error compiling Lua lane\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); printf("%s\n", newTaskScript); free(newTaskScript); return -1; } free(newTaskScript); ret = ls_lua_pcall(L, 2, 1, 0); /* lanes lane_h */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error creating Lua lane\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); return -1; } if (!ls_lua_isfunction(L, -1)) /* lanes lane_h */ { ls_lua_pop(L, 2); return -1; } ret = ls_lua_pcall(L, 0, 1, 0); /* lanes ret */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error calling Lua lane\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); return -1; } ref = ls_luaL_ref(L, LUA_REGISTRYINDEX); ls_lua_pop(L, 1); return ref; }
static LIST *ls_lua_callhelper(int top, int ret) { LIST *retList; int numParams; int i; if (ret != 0) { if (ls_lua_isstring(L, -1)) { printf("jam: Error compiling Lua code\n%s\n", ls_lua_tostring(L, -1)); exit(EXITBAD); } ls_lua_pop(L, 1); return L0; } ret = ls_lua_pcall(L, 0, -1, 0); if (ret != 0) { if (ls_lua_isstring(L, -1)) { int ret; printf("jam: Error running Lua code\n%s\n", ls_lua_tostring(L, -1)); ls_lua_getglobal(L, "debug"); ls_lua_getfield(L, -1, "traceback"); ret = ls_lua_pcall(L, 0, 1, 0); if (ret == 0) { if (ls_lua_isstring(L, -1)) { puts(ls_lua_tostring(L, -1)); } } exit(EXITBAD); } ls_lua_pop(L, 1); return L0; } retList = L0; numParams = ls_lua_gettop(L) - top; for (i = 1; i <= numParams; ++i) { retList = luahelper_addtolist(L, retList, top + i); } return retList; }
const char* luahelper_linefilter(const char* line, size_t lineSize) { int ret; int top; char* out; if (linefilter_stack_position == -1) { fprintf(stderr, "jam: Line filter access not enabled.\n"); exit(1); } ls_lua_init(); top = ls_lua_gettop(L); ls_lua_pushvalue(L, linefilter_stack_position); ls_lua_pushlstring(L, line, lineSize); /* function line */ ret = ls_lua_pcall(L, 1, 1, 0); if (ret != 0) { if (ls_lua_isstring(L, -1)) fprintf(stderr, "jam: Error running line filter.\n%s\n", ls_lua_tostring(L, -1)); ls_lua_settop(L, top); return NULL; } out = malloc(ls_lua_rawlen(L, -1) + 1); strcpy(out, ls_lua_tostring(L, -1)); ls_lua_settop(L, top); return out; }
int luahelper_md5callback(const char *filename, MD5SUM sum, const char* callback) { int ret; ls_lua_init(); ls_lua_getfield(L, LUA_GLOBALSINDEX, callback); if (!ls_lua_isfunction(L, -1)) { ls_lua_pop(L, 1); printf("jam: Error calling Lua md5 callback '%s'.\n", callback); memset(sum, 0, sizeof(MD5SUM)); return 0; } ls_lua_pushstring(L, filename); ret = ls_lua_pcall(L, 1, 1, 0); if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error running Lua md5 callback\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 1); memset(sum, 0, sizeof(MD5SUM)); return 0; } if (ls_lua_isnil(L, -1)) { memset(sum, 0, sizeof(MD5SUM)); ls_lua_pop(L, 1); return 0; } if (!ls_lua_isstring(L, -1) || ls_lua_objlen(L, -1) != sizeof(MD5SUM)) { printf("jam: Error running Lua md5 callback '%s'.\n", callback); memset(sum, 0, sizeof(MD5SUM)); ls_lua_pop(L, 1); return 0; } memcpy(sum, ls_lua_tostring(L, -1), sizeof(MD5SUM)); ls_lua_pop(L, 1); return 1; }
static int pmain (ls_lua_State *L) { int top; int ret; ls_luaL_openlibs(L); ls_lua_pushcclosure(L, LS_jam_getvar, 0); ls_lua_setglobal(L, "jam_getvar"); ls_lua_pushcclosure(L, LS_jam_setvar, 0); ls_lua_setglobal(L, "jam_setvar"); ls_lua_pushcclosure(L, LS_jam_action, 0); ls_lua_setglobal(L, "jam_action"); ls_lua_pushcclosure(L, LS_jam_evaluaterule, 0); ls_lua_setglobal(L, "jam_evaluaterule"); ls_lua_pushcclosure(L, LS_jam_expand, 0); ls_lua_setglobal(L, "jam_expand"); ls_lua_pushcclosure(L, LS_jam_parse, 0); ls_lua_setglobal(L, "jam_parse"); ls_lua_pushcclosure(L, LS_jam_print, 0); ls_lua_setglobal(L, "jam_print"); top = ls_lua_gettop(L); ret = ls_luaL_loadstring(L, "lanes = require 'lanes'"); ls_lua_callhelper(top, ret); ls_lua_getglobal(L, "lanes"); /* lanes */ ls_lua_getfield(L, -1, "configure"); /* lanes configure */ ls_lua_newtable(L); /* lanes configure table */ ls_lua_pushcclosure(L, lanes_on_state_create, 0); /* lanes configure table lanes_on_state_create */ ls_lua_setfield(L, -2, "on_state_create"); /* lanes configure table */ ret = ls_lua_pcall(L, 1, 0, 0); /* lanes */ if (ret != 0) { const char* err = ls_lua_tostring(L, -1); (void)err; } ls_lua_pop(L, 2); ls_lua_newtable(L); ls_lua_setglobal(L, "LineFilters"); return 0; }
void luahelper_taskcancel(int taskid) { int ret; ls_lua_init(); ls_lua_rawgeti(L, LUA_REGISTRYINDEX, taskid); ls_lua_pushvalue(L, -1); ls_lua_getfield(L, -1, "cancel"); ls_lua_pushvalue(L, -2); ls_lua_pushnumber(L, 0); ls_lua_pushboolean(L, 1); ret = ls_lua_pcall(L, 3, -1, 0); if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error running Lua task.cancel\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); return; } ls_lua_pop(L, 1); }
int luahelper_taskisrunning(int taskid, int* returnValue) { const char* status; ls_lua_init(); ls_lua_rawgeti(L, LUA_REGISTRYINDEX, taskid); /* lane_h */ if (!ls_lua_istable(L, -1)) { *returnValue = 1; ls_lua_pop(L, 1); return 0; } ls_lua_getfield(L, -1, "status"); /* lane_h status */ status = ls_lua_tostring(L, -1); if (strcmp(status, "done") == 0) { int ret; ls_lua_pop(L, 1); /* lane_h */ ls_lua_getfield(L, -1, "join"); /* lane_h join(function) */ ls_lua_pushvalue(L, -2); /* lane_h join(function) lane_h */ ret = ls_lua_pcall(L, 1, 1, 0); /* lane_h ret */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error in Lua lane\n%s\n", ls_lua_tostring(L, -1)); *returnValue = 1; } if (ls_lua_isnumber(L, -1)) *returnValue = (int)ls_lua_tonumber(L, -1); ls_lua_pop(L, 2); ls_luaL_unref(L, LUA_REGISTRYINDEX, taskid); return 0; } else if (strcmp(status, "error") == 0 || strcmp(status, "cancelled") == 0) { int ret; *returnValue = 1; ls_lua_pop(L, 1); /* lane_h */ ls_lua_getfield(L, -1, "join"); /* lane_h join(function) */ ls_lua_pushvalue(L, -2); /* lane_h join(function) lane_h */ ret = ls_lua_pcall(L, 1, 3, 0); /* lane_h nil err stack_tbl */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error in Lua lane\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); return 0; } if (ls_lua_isstring(L, -2)) { printf("jam: Error in Lua lane\n%s\n", ls_lua_tostring(L, -2)); } ls_lua_pop(L, 4); /* */ ls_luaL_unref(L, LUA_REGISTRYINDEX, taskid); return 0; } ls_lua_pop(L, 2); return 1; }
void ls_lua_init() { char fileName[4096]; LIST *luaSharedLibrary; #ifdef OS_NT HINSTANCE handle = NULL; #else void* handle = NULL; #endif if (L) return; #ifdef _DEBUG luaSharedLibrary = var_get("LUA_SHARED_LIBRARY.DEBUG"); #else luaSharedLibrary = var_get("LUA_SHARED_LIBRARY.RELEASE"); #endif if (list_first(luaSharedLibrary)) { strcpy(fileName, list_value(list_first(luaSharedLibrary))); handle = ls_lua_loadlibrary(fileName); } if (!handle) { getprocesspath(fileName, 4096); #ifdef OS_NT #ifdef _DEBUG strcat(fileName, "/lua/lua53_debug.dll"); #else strcat(fileName, "/lua/lua53.dll"); #endif #else #ifdef _DEBUG strcat(fileName, "/lua/liblua53_debug.so"); #else strcat(fileName, "/lua/liblua53.so"); #endif #endif handle = ls_lua_loadlibrary(fileName); if (!handle) { printf("jam: Unable to find the LuaPlus shared library.\n"); exit(EXITBAD); } } ls_lua_close = (void (*)(ls_lua_State *))ls_lua_loadsymbol(handle, "lua_close"); ls_lua_gettop = (int (*)(ls_lua_State *))ls_lua_loadsymbol(handle, "lua_gettop"); ls_lua_settop = (void (*)(ls_lua_State *, int))ls_lua_loadsymbol(handle, "lua_settop"); ls_lua_pushvalue = (void (*)(ls_lua_State *, int))ls_lua_loadsymbol(handle, "lua_pushvalue"); ls_lua_rotate = (void (*)(ls_lua_State *, int, int))ls_lua_loadsymbol(handle, "lua_rotate"); ls_lua_isnumber = (int (*)(ls_lua_State *, int))ls_lua_loadsymbol(handle, "lua_isnumber"); ls_lua_isstring = (int (*)(ls_lua_State *, int))ls_lua_loadsymbol(handle, "lua_isstring"); ls_lua_isuserdata = (int (*)(ls_lua_State *, int))ls_lua_loadsymbol(handle, "lua_isuserdata"); ls_lua_type = (int (*)(ls_lua_State *, int))ls_lua_loadsymbol(handle, "lua_type"); ls_lua_tonumberx = (ls_lua_Number (*)(ls_lua_State *, int, int *))ls_lua_loadsymbol(handle, "lua_tonumberx"); ls_lua_toboolean = (int (*)(ls_lua_State *, int))ls_lua_loadsymbol(handle, "lua_toboolean"); ls_lua_tolstring = (const char *(*)(ls_lua_State *, int, size_t *))ls_lua_loadsymbol(handle, "lua_tolstring"); ls_lua_rawlen = (size_t (*)(ls_lua_State *, int))ls_lua_loadsymbol(handle, "lua_rawlen"); ls_lua_pushnil = (void (*) (ls_lua_State *))ls_lua_loadsymbol(handle, "lua_pushnil"); ls_lua_pushnumber = (void (*) (ls_lua_State *, ls_lua_Number))ls_lua_loadsymbol(handle, "lua_pushnumber"); ls_lua_pushinteger = (void (*) (ls_lua_State *, ls_lua_Integer))ls_lua_loadsymbol(handle, "lua_pushinteger"); ls_lua_pushstring = (const char *(*) (ls_lua_State *, const char *))ls_lua_loadsymbol(handle, "lua_pushstring"); ls_lua_pushlstring = (const char *(*) (ls_lua_State *, const char *, size_t))ls_lua_loadsymbol(handle, "lua_pushlstring"); ls_lua_pushcclosure = (void (*) (ls_lua_State *, ls_lua_CFunction, int))ls_lua_loadsymbol(handle, "lua_pushcclosure"); ls_lua_pushboolean = (void (*)(ls_lua_State *, int))ls_lua_loadsymbol(handle, "lua_pushboolean"); ls_lua_getglobal = (void (*) (ls_lua_State *, const char *))ls_lua_loadsymbol(handle, "lua_getglobal"); ls_lua_gettable = (void (*) (ls_lua_State *, int id))ls_lua_loadsymbol(handle, "lua_gettable"); ls_lua_getfield = (void (*)(ls_lua_State *, int, const char *))ls_lua_loadsymbol(handle, "lua_getfield"); ls_lua_rawgeti = (void (*) (ls_lua_State *, int, ls_lua_Integer))ls_lua_loadsymbol(handle, "lua_rawgeti"); ls_lua_createtable = (void (*)(ls_lua_State *, int, int))ls_lua_loadsymbol(handle, "lua_createtable"); ls_lua_setglobal = (void (*)(ls_lua_State *, const char *))ls_lua_loadsymbol(handle, "lua_setglobal"); ls_lua_settable = (void (*)(ls_lua_State *, int))ls_lua_loadsymbol(handle, "lua_settable"); ls_lua_setfield = (void (*)(ls_lua_State *, int, const char *))ls_lua_loadsymbol(handle, "lua_setfield"); ls_lua_rawseti = (void (*)(ls_lua_State *, int, ls_lua_Integer))ls_lua_loadsymbol(handle, "lua_rawseti"); ls_lua_pcallk = (int (*)(ls_lua_State *, int, int, int, ls_lua_KContext, ls_lua_CFunction))ls_lua_loadsymbol(handle, "lua_pcallk"); ls_lua_next = (int (*)(ls_lua_State *, int))ls_lua_loadsymbol(handle, "lua_next"); ls_lua_getstack = (int(*)(ls_lua_State *, int, ls_lua_Debug *))ls_lua_loadsymbol(handle, "lua_getstack"); ls_lua_getinfo = (int(*)(ls_lua_State *, const char *, ls_lua_Debug *))ls_lua_loadsymbol(handle, "lua_getinfo"); ls_luaL_openlibs = (void (*)(ls_lua_State *))ls_lua_loadsymbol(handle, "luaL_openlibs"); ls_luaL_loadstring = (int (*)(ls_lua_State *, const char *))ls_lua_loadsymbol(handle, "luaL_loadstring"); ls_luaL_loadfilex = (int (*)(ls_lua_State *, const char *, const char *))ls_lua_loadsymbol(handle, "luaL_loadfilex"); ls_luaL_newstate = (ls_lua_State *(*)(void))ls_lua_loadsymbol(handle, "luaL_newstate"); ls_luaL_ref = (int (*)(ls_lua_State *, int))ls_lua_loadsymbol(handle, "luaL_ref"); ls_luaL_unref = (void (*)(ls_lua_State *, int, int))ls_lua_loadsymbol(handle, "luaL_unref"); L = ls_luaL_newstate(); ls_lua_pushcclosure(L, &pmain, 0); ls_lua_pcall(L, 0, 0, 0); if (globs.lua_debugger) { char* slashPtr; char* backslashPtr; slashPtr = strrchr(fileName, '/'); backslashPtr = strrchr(fileName, '\\'); slashPtr = slashPtr > backslashPtr ? slashPtr : backslashPtr; if (slashPtr) { ++slashPtr; } else { slashPtr = fileName; } #ifdef OS_NT #ifdef _DEBUG strcpy(slashPtr, "lua-tilde.debug.dll"); #else strcpy(slashPtr, "lua-tilde.dll"); #endif #else #ifdef _DEBUG strcpy(slashPtr, "lua-tilde.debug.so"); #else strcpy(slashPtr, "lua-tilde.so"); #endif #endif luaTildeModule = ls_lua_loadlibrary(fileName); if (luaTildeModule) { LuaTildeHost* host; LuaTilde_Command = (LuaTildeHost* (*)(LuaTildeHost*, const char*, void*, void*))ls_lua_loadsymbol(luaTildeModule, "LuaTilde_Command"); host = LuaTilde_Command(NULL, "create", (void*)10000, NULL); LuaTilde_Command(host, "registerstate", "State", L); LuaTilde_Command(host, "waitfordebuggerconnection", NULL, NULL); } } }
int luahelper_taskadd(const char* taskscript, LOL* args) { int ret; int ref; size_t taskscriptlen = strlen(taskscript); char* newTaskScript; int i; ls_lua_init(); ls_lua_getglobal(L, "lanes"); /* lanes */ ls_lua_getfield(L, -1, "gen"); /* lanes gen */ ls_lua_pushstring(L, "*"); /* lanes gen * */ newTaskScript = malloc( taskscriptlen + 1 ); strncpy(newTaskScript, taskscript, taskscriptlen); newTaskScript[taskscriptlen] = 0; ret = ls_luaL_loadstring(L, newTaskScript); /* lanes gen * script */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error compiling Lua lane\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); printf("%s\n", newTaskScript); free(newTaskScript); return -1; } free(newTaskScript); ret = ls_lua_pcall(L, 2, 1, 0); /* lanes lane_h */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error creating Lua lane\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); return -1; } if (!ls_lua_isfunction(L, -1)) /* lanes lane_h */ { ls_lua_pop(L, 2); return -1; } for (i = 0; i < args->count; ++i) { LIST* list = lol_get(args, i); LISTITEM *l2; int index = 0; ls_lua_newtable(L); for (l2 = list_first(list); l2; l2 = list_next(l2)) { ls_lua_pushstring(L, list_value(l2)); ls_lua_rawseti(L, -2, ++index); } } ret = ls_lua_pcall(L, args->count, 1, 0); /* lanes ret */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error calling Lua lane\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); return -1; } ref = ls_luaL_ref(L, LUA_REGISTRYINDEX); ls_lua_pop(L, 1); return ref; }