ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

lbuiltin.cpp

Go to the documentation of this file.
00001 /*
00002 ** Built-in functions
00003 ** See Copyright Notice in lua.h
00004 */
00005 
00006 #define FORBIDDEN_SYMBOL_EXCEPTION_setjmp
00007 #define FORBIDDEN_SYMBOL_EXCEPTION_longjmp
00008 #define FORBIDDEN_SYMBOL_EXCEPTION_printf
00009 
00010 #include "common/util.h"
00011 
00012 #include "engines/grim/lua/lapi.h"
00013 #include "engines/grim/lua/lauxlib.h"
00014 #include "engines/grim/lua/lbuiltin.h"
00015 #include "engines/grim/lua/ldo.h"
00016 #include "engines/grim/lua/lfunc.h"
00017 #include "engines/grim/lua/lmem.h"
00018 #include "engines/grim/lua/lobject.h"
00019 #include "engines/grim/lua/lstate.h"
00020 #include "engines/grim/lua/lstring.h"
00021 #include "engines/grim/lua/ltable.h"
00022 #include "engines/grim/lua/ltask.h"
00023 #include "engines/grim/lua/ltm.h"
00024 #include "engines/grim/lua/lua.h"
00025 
00026 namespace Grim {
00027 
00028 static void pushstring(TaggedString *s) {
00029     TObject o;
00030     o.ttype = LUA_T_STRING;
00031     o.value.ts = s;
00032     luaA_pushobject(&o);
00033 }
00034 
00035 static void nextvar() {
00036     TObject *o = luaA_Address(luaL_nonnullarg(1));
00037     TaggedString *g;
00038     if (ttype(o) == LUA_T_NIL)
00039         g = (TaggedString *)rootglobal.next;
00040     else {
00041         luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
00042         g = tsvalue(o);
00043         // check whether name is in global var list
00044         luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected");
00045         g = (TaggedString *)g->head.next;
00046     }
00047     while (g && g->globalval.ttype == LUA_T_NIL) {
00048         // skip globals with nil
00049         g = (TaggedString *)g->head.next;
00050     }
00051     if (g) {
00052         pushstring(g);
00053         luaA_pushobject(&g->globalval);
00054     }
00055 }
00056 
00057 static void foreachvar() {
00058     TObject f = *luaA_Address(luaL_functionarg(1));
00059     GCnode *g;
00060     StkId name = lua_state->Cstack.base++;  // place to keep var name (to avoid GC)
00061     ttype(lua_state->stack.stack + name) = LUA_T_NIL;
00062     lua_state->stack.top++;
00063     for (g = rootglobal.next; g; g = g->next) {
00064         TaggedString *s = (TaggedString *)g;
00065         if (s->globalval.ttype != LUA_T_NIL) {
00066             ttype(lua_state->stack.stack + name) = LUA_T_STRING;
00067             tsvalue(lua_state->stack.stack + name) = s;  // keep s on stack to avoid GC
00068             luaA_pushobject(&f);
00069             pushstring(s);
00070             luaA_pushobject(&s->globalval);
00071             lua_state->state_counter1++;
00072             luaD_call((lua_state->stack.top - lua_state->stack.stack) - 2, 1);
00073             lua_state->state_counter1--;
00074             if (ttype(lua_state->stack.top - 1) != LUA_T_NIL)
00075                 return;
00076             lua_state->stack.top--;
00077         }
00078     }
00079 }
00080 
00081 static void next() {
00082     lua_Object o = luaL_tablearg(1);
00083     lua_Object r = luaL_nonnullarg(2);
00084     Node *n = luaH_next(luaA_Address(o), luaA_Address(r));
00085     if (n) {
00086         luaA_pushobject(&n->ref);
00087         luaA_pushobject(&n->val);
00088     }
00089 }
00090 
00091 static void foreach() {
00092     TObject t = *luaA_Address(luaL_tablearg(1));
00093     TObject f = *luaA_Address(luaL_functionarg(2));
00094     int32 i;
00095     for (i = 0; i < avalue(&t)->nhash; i++) {
00096         Node *nd = &(avalue(&t)->node[i]);
00097         if (ttype(ref(nd)) != LUA_T_NIL && ttype(val(nd)) != LUA_T_NIL) {
00098             luaA_pushobject(&f);
00099             luaA_pushobject(ref(nd));
00100             luaA_pushobject(val(nd));
00101             lua_state->state_counter1++;
00102             luaD_call((lua_state->stack.top - lua_state->stack.stack) - 2, 1);
00103             lua_state->state_counter1--;
00104             if (ttype(lua_state->stack.top - 1) != LUA_T_NIL)
00105                 return;
00106             lua_state->stack.top--;
00107         }
00108     }
00109 }
00110 
00111 static void internaldostring() {
00112     if (lua_getparam(2) != LUA_NOOBJECT)
00113         lua_error("invalid 2nd argument (probably obsolete code)");
00114     if (lua_dostring(luaL_check_string(1)) == 0)
00115         if (luaA_passresults() == 0)
00116             lua_pushuserdata(0);  // at least one result to signal no errors
00117 }
00118 
00119 static const char *to_string(lua_Object obj) {
00120     char *buff = luaL_openspace(30);
00121     TObject *o = luaA_Address(obj);
00122     switch (ttype(o)) {
00123     case LUA_T_NUMBER:
00124     case LUA_T_STRING:
00125         return lua_getstring(obj);
00126     case LUA_T_ARRAY:
00127         {
00128             sprintf(buff, "table: %p", (void *)o->value.a);
00129             return buff;
00130         }
00131     case LUA_T_CLOSURE:
00132         {
00133             sprintf(buff, "function: %p", (void *)o->value.cl);
00134             return buff;
00135         }
00136     case LUA_T_PROTO:
00137         {
00138             sprintf(buff, "function: %p", (void *)o->value.tf);
00139             return buff;
00140         }
00141     case LUA_T_CPROTO:
00142         {
00143             // WORKAROUND: C++ forbids casting from a pointer-to-function to a
00144             // pointer-to-object. We use a union to work around that.
00145             union {
00146                 void *objPtr;
00147                 lua_CFunction funcPtr;
00148             } ptrUnion;
00149 
00150             ptrUnion.funcPtr = o->value.f;
00151 
00152             sprintf(buff, "function: %p", ptrUnion.objPtr);
00153             return buff;
00154         }
00155     case LUA_T_USERDATA:
00156         {
00157             sprintf(buff, "userdata: %08X", o->value.ud.id);
00158             return buff;
00159         }
00160     case LUA_T_TASK:
00161         {
00162             sprintf(buff, "task: %d", (int)o->value.n);
00163             return buff;
00164         }
00165     case LUA_T_NIL:
00166         return "nil";
00167     default:
00168 #ifdef LUA_DEBUG
00169         LUA_INTERNALERROR("internal error");
00170 #endif
00171         return nullptr;
00172     }
00173 }
00174 
00175 static void bi_tostring() {
00176     lua_pushstring(to_string(lua_getparam(1)));
00177 }
00178 
00179 static void luaI_print() {
00180     int32 i = 1;
00181     lua_Object obj;
00182     while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
00183         printf("%s\t", to_string(obj));
00184     printf("\n");
00185 }
00186 
00187 static void luaI_type() {
00188     lua_Object o = luaL_nonnullarg(1);
00189     lua_pushstring(luaO_typenames[-ttype(luaA_Address(o))]);
00190     lua_pushnumber(lua_tag(o));
00191 }
00192 
00193 static void tonumber() {
00194     int32 base = (int32)luaL_opt_number(2, 10);
00195     if (base == 10) {  // standard conversion
00196         lua_Object o = lua_getparam(1);
00197         if (lua_isnumber(o))
00198             lua_pushnumber(lua_getnumber(o));
00199     } else {
00200         const char *s = luaL_check_string(1);
00201         char *e;
00202         int32 n;
00203         luaL_arg_check(0 <= base && base <= 36, 2, "base out of range");
00204         n = (int32)strtol(s, &e, base);
00205         while (Common::isSpace(*e))
00206             e++; // skip trailing spaces
00207         if (*e)
00208             return; // invalid format: return nil
00209         lua_pushnumber(n);
00210     }
00211 }
00212 
00213 static void luaI_error() {
00214     lua_error(lua_getstring(lua_getparam(1)));
00215 }
00216 
00217 static void luaI_assert() {
00218     lua_Object p = lua_getparam(1);
00219     if (p == LUA_NOOBJECT || lua_isnil(p))
00220         luaL_verror("assertion failed!  %.100s", luaL_opt_string(2, ""));
00221 }
00222 
00223 static void setglobal() {
00224     const char *n = luaL_check_string(1);
00225     lua_Object value = luaL_nonnullarg(2);
00226     lua_pushobject(value);
00227     lua_setglobal(n);
00228     lua_pushobject(value);  // return given value
00229 }
00230 
00231 static void rawsetglobal() {
00232     const char *n = luaL_check_string(1);
00233     lua_Object value = luaL_nonnullarg(2);
00234     lua_pushobject(value);
00235     lua_rawsetglobal(n);
00236     lua_pushobject(value);  // return given value
00237 }
00238 
00239 static void getglobal() {
00240     lua_pushobject(lua_getglobal(luaL_check_string(1)));
00241 }
00242 
00243 static void rawgetglobal() {
00244     lua_pushobject(lua_rawgetglobal(luaL_check_string(1)));
00245 }
00246 
00247 static void luatag() {
00248     lua_pushnumber(lua_tag(lua_getparam(1)));
00249 }
00250 
00251 static int32 getnarg(lua_Object table) {
00252     lua_Object temp;
00253     // temp = table.n
00254     lua_pushobject(table);
00255     lua_pushstring("n");
00256     temp = lua_rawgettable();
00257     return (lua_isnumber(temp) ? (int32)lua_getnumber(temp) : MAX_WORD);
00258 }
00259 
00260 static void luaI_call() {
00261     lua_Object f = luaL_nonnullarg(1);
00262     lua_Object arg = luaL_tablearg(2);
00263     const char *options = luaL_opt_string(3, "");
00264     lua_Object err = lua_getparam(4);
00265     int32 narg = getnarg(arg);
00266     int32 i, status;
00267     if (err != LUA_NOOBJECT) {  // set new error method
00268         lua_pushobject(err);
00269         err = lua_seterrormethod();
00270     }
00271     // push arg[1...n]
00272     for (i = 0; i < narg; i++) {
00273         lua_Object temp;
00274         // temp = arg[i + 1]
00275         lua_pushobject(arg);
00276         lua_pushnumber(i + 1);
00277         temp = lua_rawgettable();
00278         if (narg == MAX_WORD && lua_isnil(temp))
00279             break;
00280         lua_pushobject(temp);
00281     }
00282     status = lua_callfunction(f);
00283     if (err != LUA_NOOBJECT) {  // restore old error method
00284         lua_pushobject(err);
00285         lua_seterrormethod();
00286     }
00287     if (status) {  // error in call?
00288         if (strchr(options, 'x'))
00289             return;  // return nil to signal the error
00290         else
00291             lua_error(nullptr);
00292     } else { // no errors
00293         if (strchr(options, 'p'))
00294             luaA_packresults();
00295         else
00296             luaA_passresults();
00297     }
00298 }
00299 
00300 static void settag() {
00301     lua_Object o = luaL_tablearg(1);
00302     lua_pushobject(o);
00303     lua_settag((int32)luaL_check_number(2));
00304 }
00305 
00306 static void newtag() {
00307     lua_pushnumber(lua_newtag());
00308 }
00309 
00310 static void copytagmethods() {
00311     lua_pushnumber(lua_copytagmethods((int32)luaL_check_number(1), (int32)luaL_check_number(2)));
00312 }
00313 
00314 static void rawgettable() {
00315     lua_Object t = luaL_nonnullarg(1);
00316     lua_Object i = luaL_nonnullarg(2);
00317     lua_pushobject(t);
00318     lua_pushobject(i);
00319     lua_pushobject(lua_rawgettable());
00320 }
00321 
00322 static void rawsettable() {
00323     lua_Object t = luaL_nonnullarg(1);
00324     lua_Object i = luaL_nonnullarg(2);
00325     lua_Object v = luaL_nonnullarg(3);
00326     lua_pushobject(t);
00327     lua_pushobject(i);
00328     lua_pushobject(v);
00329     lua_rawsettable();
00330 }
00331 
00332 static void settagmethod() {
00333     lua_Object nf = luaL_nonnullarg(3);
00334     lua_pushobject(nf);
00335     lua_pushobject(lua_settagmethod((int32)luaL_check_number(1), luaL_check_string(2)));
00336 }
00337 
00338 static void gettagmethod() {
00339     lua_pushobject(lua_gettagmethod((int32)luaL_check_number(1), luaL_check_string(2)));
00340 }
00341 
00342 static void seterrormethod() {
00343     lua_Object nf = luaL_functionarg(1);
00344     lua_pushobject(nf);
00345     lua_pushobject(lua_seterrormethod());
00346 }
00347 
00348 static void luaI_collectgarbage() {
00349     lua_pushnumber(lua_collectgarbage((int32)luaL_opt_number(1, 0)));
00350 }
00351 
00352 /*
00353 ** =======================================================
00354 ** some DEBUG functions
00355 ** =======================================================
00356 */
00357 #ifdef LUA_DEBUG
00358 
00359 static void mem_query() {
00360     lua_pushnumber(totalmem);
00361     lua_pushnumber(numblocks);
00362 }
00363 
00364 static void countlist() {
00365     char *s = luaL_check_string(1);
00366     GCnode *l = (s[0] == 't') ? lua_state->roottable.next : (s[0] == 'c') ? lua_state->rootcl.next :
00367             (s[0] == 'p') ? lua_state->rootproto.next : lua_state->rootglobal.next;
00368     int32 i = 0;
00369     while (l) {
00370         i++;
00371         l = l->next;
00372     }
00373     lua_pushnumber(i);
00374 }
00375 
00376 static void testC() {
00377 #define getnum(s)   ((*s++) - '0')
00378 #define getname(s)  (nome[0] = *s++, nome)
00379 
00380     static int32 locks[10];
00381     lua_Object reg[10];
00382     char nome[2];
00383     char *s = luaL_check_string(1);
00384     nome[1] = 0;
00385     while (1) {
00386         switch (*s++) {
00387             case '0': case '1': case '2': case '3': case '4':
00388             case '5': case '6': case '7': case '8': case '9':
00389                 lua_pushnumber(*(s - 1) - '0');
00390                 break;
00391             case 'c':
00392                 reg[getnum(s)] = lua_createtable();
00393                 break;
00394             case 'C':
00395                 {
00396                     lua_CFunction f = lua_getcfunction(lua_getglobal(getname(s)));
00397                     lua_pushCclosure(f, getnum(s));
00398                     break;
00399                 }
00400             case 'P':
00401                 reg[getnum(s)] = lua_pop();
00402                 break;
00403             case 'g':
00404                 {
00405                     int32 n=getnum(s);
00406                     reg[n] = lua_getglobal(getname(s));
00407                     break;
00408                 }
00409             case 'G':
00410                 {
00411                     int32 n = getnum(s);
00412                     reg[n] = lua_rawgetglobal(getname(s));
00413                     break;
00414                 }
00415             case 'l':
00416                 locks[getnum(s)] = lua_ref(1);
00417                 break;
00418             case 'L':
00419                 locks[getnum(s)] = lua_ref(0);
00420                 break;
00421             case 'r':
00422                 {
00423                     int32 n = getnum(s);
00424                     reg[n] = lua_getref(locks[getnum(s)]);
00425                     break;
00426                 }
00427             case 'u':
00428                 lua_unref(locks[getnum(s)]);
00429                 break;
00430             case 'p':
00431                 {
00432                     int32 n = getnum(s);
00433                     reg[n] = lua_getparam(getnum(s));
00434                     break;
00435                 }
00436             case '=':
00437                 lua_setglobal(getname(s));
00438                 break;
00439             case 's':
00440                 lua_pushstring(getname(s));
00441                 break;
00442             case 'o':
00443                 lua_pushobject(reg[getnum(s)]);
00444                 break;
00445             case 'f':
00446                 lua_call(getname(s));
00447                 break;
00448             case 'i':
00449                 reg[getnum(s)] = lua_gettable();
00450                 break;
00451             case 'I':
00452                 reg[getnum(s)] = lua_rawgettable();
00453                 break;
00454             case 't':
00455                 lua_settable();
00456                 break;
00457             case 'T':
00458                 lua_rawsettable();
00459                 break;
00460             default:
00461                 luaL_verror("unknown command in `testC': %c", *(s - 1));
00462         }
00463         if (*s == 0)
00464             return;
00465         if (*s++ != ' ')
00466             lua_error("missing ` ' between commands in `testC'");
00467     }
00468 }
00469 
00470 #endif
00471 
00472 /*
00473 ** Internal functions
00474 */
00475 static struct luaL_reg int_funcs[] = {
00476 #ifdef LUA_COMPAT2_5
00477     { "setfallback", luaT_setfallback },
00478 #endif
00479 #ifdef LUA_DEBUG
00480     { "testC", testC },
00481     { "totalmem", mem_query },
00482     { "count", countlist },
00483 #endif
00484     { "assert", luaI_assert },
00485     { "call", luaI_call },
00486     { "collectgarbage", luaI_collectgarbage },
00487     { "copytagmethods", copytagmethods },
00488     { "dostring", internaldostring },
00489     { "error", luaI_error },
00490     { "foreach", foreach },
00491     { "foreachvar", foreachvar },
00492     { "getglobal", getglobal },
00493     { "newtag", newtag },
00494     { "next", next },
00495     { "nextvar", nextvar },
00496     { "print", luaI_print },
00497     { "rawgetglobal", rawgetglobal },
00498     { "rawgettable", rawgettable },
00499     { "rawsetglobal", rawsetglobal },
00500     { "rawsettable", rawsettable },
00501     { "seterrormethod", seterrormethod },
00502     { "setglobal", setglobal },
00503     { "settagmethod", settagmethod },
00504     { "gettagmethod", gettagmethod },
00505     { "settag", settag },
00506     { "tonumber", tonumber },
00507     { "tostring", bi_tostring },
00508     { "tag", luatag },
00509     { "type", luaI_type },
00510     { "start_script", start_script },
00511     { "stop_script", stop_script },
00512     { "next_script", next_script },
00513     { "identify_script", identify_script },
00514     { "pause_scripts", pause_scripts },
00515     { "unpause_scripts", unpause_scripts },
00516     { "find_script", find_script },
00517     { "sleep_for", sleep_for },
00518     { "break_here", break_here },
00519     { "pause_script", pause_script },
00520     { "unpause_script", unpause_script }
00521 };
00522 
00523 void luaB_predefine() {
00524     // pre-register mem error messages, to avoid loop when error arises
00525     luaS_newfixedstring(tableEM);
00526     luaS_newfixedstring(memEM);
00527     luaL_openlib(int_funcs, sizeof(int_funcs) / sizeof(int_funcs[0]));
00528     lua_pushstring(LUA_VERSION);
00529     lua_setglobal("_VERSION");
00530 }
00531 
00532 } // end of namespace Grim


Generated on Sat Oct 12 2019 05:00:50 for ResidualVM by doxygen 1.7.1
curved edge   curved edge