int LS_jam_setvar(ls_lua_State *L) { int numParams = ls_lua_gettop(L); if (numParams < 2 || numParams > 3) return 0; if (!ls_lua_isstring(L, 1)) return 0; if (numParams == 2) { var_set(ls_lua_tostring(L, 1), luahelper_addtolist(L, L0, 2), VAR_SET); } else { TARGET *t; if (!ls_lua_isstring(L, 2)) return 0; t = bindtarget(ls_lua_tostring(L, 1)); pushsettings(t->settings); var_set(ls_lua_tostring(L, 2), luahelper_addtolist(L, L0, 3), VAR_SET); popsettings(t->settings); } return 0; }
int LS_jam_evaluaterule(ls_lua_State *L) { LOL lol; int i; LIST *list; int index; int numParams = ls_lua_gettop(L); if (numParams < 1) return 0; if (!ls_lua_isstring(L, 1)) return 0; lol_init(&lol); for (i = 0; i < numParams - 1; ++i) { lol_add(&lol, luahelper_addtolist(L, L0, 2 + i)); } list = evaluate_rule(ls_lua_tostring(L, 1), &lol, L0); lol_free(&lol); ls_lua_newtable(L); index = 1; for (; list; list = list_next(list), ++index) { ls_lua_pushnumber(L, index); ls_lua_pushstring(L, list->string); ls_lua_settable(L, -3); } return 1; }
static LIST *luahelper_addtolist(ls_lua_State *L, LIST *list, int index) { if (ls_lua_isboolean(L, index)) return list_append(list, ls_lua_toboolean(L, index) ? "true" : "false", 0); if (ls_lua_isnumber(L, index)) { LIST* newList; ls_lua_pushvalue(L, index); newList = list_append(list, ls_lua_tostring(L, -1), 0); ls_lua_pop(L, 1); return newList; } if (ls_lua_isstring(L, index)) { const char* value = ls_lua_tostring(L, index); return list_append(list, value, 0); } else if (ls_lua_istable(L, index)) { ls_lua_pushnil(L); while (ls_lua_next(L, index) != 0) { list = luahelper_addtolist(L, list, ls_lua_gettop(L)); ls_lua_pop(L, 1); } } return list; }
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; }
static LIST *luahelper_addtolist(ls_lua_State *L, LIST *list, int index) { if (ls_lua_isboolean(L, index)) return list_new(list, ls_lua_toboolean(L, index) ? "true" : "false", 0); if (ls_lua_isnumber(L, index)) return list_new(list, ls_lua_tostring(L, index), 0); if (ls_lua_isstring(L, index)) return list_new(list, ls_lua_tostring(L, index), 0); else if (ls_lua_istable(L, index)) { ls_lua_pushnil(L); while (ls_lua_next(L, index) != 0) { list = luahelper_addtolist(L, list, index + 2); ls_lua_pop(L, 1); } } return list; }
int LS_jam_evaluaterule(ls_lua_State *L) { LOL lol; int i; LIST *list; LISTITEM* item; int index; const char* rule; int numParams = ls_lua_gettop(L); if (numParams < 1) return 0; if (!ls_lua_isstring(L, 1)) return 0; lol_init(&lol); rule = ls_lua_tostring(L, 1); for (i = 0; i < numParams - 1; ++i) { lol_add(&lol, luahelper_addtolist(L, L0, 2 + i)); } list = evaluate_rule(rule, &lol, L0); lol_free(&lol); ls_lua_newtable(L); index = 1; for (item = list_first(list); item; item = list_next(item), ++index) { ls_lua_pushnumber(L, index); ls_lua_pushstring(L, list_value(item)); ls_lua_settable(L, -3); } return 1; }