static void message (char *s) { TObject *em = &(luaS_new("_ERRORMESSAGE")->u.s.globalval); if (ttype(em) == LUA_T_PROTO || ttype(em) == LUA_T_CPROTO || ttype(em) == LUA_T_CLOSURE) { *L->stack.top = *em; incr_top; lua_pushstring(s); luaD_calln(1, 0); } }
static int sort_comp (lua_Object f, TObject *a, TObject *b) { /* notice: the caller (auxsort) must check stack space */ if (f != LUA_NOOBJECT) { *(L->stack.top) = *luaA_Address(f); *(L->stack.top+1) = *a; *(L->stack.top+2) = *b; L->stack.top += 3; luaD_calln(2, 1); } else { /* a < b? */ *(L->stack.top) = *a; *(L->stack.top+1) = *b; L->stack.top += 2; luaV_comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT); } return ttype(--(L->stack.top)) != LUA_T_NIL; }
static void luaB_foreach (void) { Hash *a = gethash(1); int i; TObject f; /* see comment in 'foreachi' */ f = *luaA_Address(luaL_functionarg(2)); luaD_checkstack(3); /* for f, ref, and val */ for (i=0; i<a->nhash; i++) { Node *nd = &(a->node[i]); if (ttype(val(nd)) != LUA_T_NIL) { *(L->stack.top++) = f; *(L->stack.top++) = *ref(nd); *(L->stack.top++) = *val(nd); luaD_calln(2, 1); if (ttype(L->stack.top-1) != LUA_T_NIL) return; L->stack.top--; /* remove result */ } } }
static void luaB_foreachi (void) { Hash *t = gethash(1); int i; int n = (int)getnarg(t); TObject f; /* 'f' cannot be a pointer to TObject, because it is on the stack, and the stack may be reallocated by the call. Moreover, some C compilers do not initialize structs, so we must do the assignment after the declaration */ f = *luaA_Address(luaL_functionarg(2)); luaD_checkstack(3); /* for f, ref, and val */ for (i=1; i<=n; i++) { *(L->stack.top++) = f; ttype(L->stack.top) = LUA_T_NUMBER; nvalue(L->stack.top++) = i; *(L->stack.top++) = *luaH_getint(t, i); luaD_calln(2, 1); if (ttype(L->stack.top-1) != LUA_T_NIL) return; L->stack.top--; } }
/* ** Execute a protected call. Assumes that function is at L->Cstack.base and ** parameters are on top of it. Leave nResults on the stack. */ int luaD_protectedrun (void) { volatile struct C_Lua_Stack oldCLS = L->Cstack; struct lua_longjmp myErrorJmp; volatile int status; struct lua_longjmp *volatile oldErr = L->errorJmp; L->errorJmp = &myErrorJmp; if (setjmp(myErrorJmp.b) == 0) { StkId base = L->Cstack.base; luaD_calln((L->stack.top-L->stack.stack)-base-1, MULT_RET); L->Cstack.lua2C = base; /* position of the new results */ L->Cstack.num = (L->stack.top-L->stack.stack) - base; L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */ status = 0; } else { /* an error occurred: restore L->Cstack and L->stack.top */ L->Cstack = oldCLS; L->stack.top = L->stack.stack+L->Cstack.base; status = 1; } L->errorJmp = oldErr; return status; }
static void luaB_foreachvar (void) { GCnode *g; TObject f; /* see comment in 'foreachi' */ f = *luaA_Address(luaL_functionarg(1)); luaD_checkstack(4); /* for extra var name, f, var name, and globalval */ for (g = L->rootglobal.next; g; g = g->next) { TaggedString *s = (TaggedString *)g; if (s->u.s.globalval.ttype != LUA_T_NIL) { pushtagstring(s); /* keep (extra) s on stack to avoid GC */ *(L->stack.top++) = f; pushtagstring(s); *(L->stack.top++) = s->u.s.globalval; luaD_calln(2, 1); if (ttype(L->stack.top-1) != LUA_T_NIL) { L->stack.top--; *(L->stack.top-1) = *L->stack.top; /* remove extra s */ return; } L->stack.top-=2; /* remove result and extra s */ } } }
void luaD_callTM (TObject *f, int nParams, int nResults) { luaD_openstack(nParams); *(L->stack.top-nParams-1) = *f; luaD_calln(nParams, nResults); }