int32 luaO_equalObj(TObject *t1, TObject *t2) { if (ttype(t1) != ttype(t2)) return 0; switch (ttype(t1)) { case LUA_T_NIL: return 1; case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2); case LUA_T_STRING: case LUA_T_USERDATA: return svalue(t1) == svalue(t2); case LUA_T_ARRAY: return avalue(t1) == avalue(t2); case LUA_T_PROTO: return tfvalue(t1) == tfvalue(t2); case LUA_T_CPROTO: return fvalue(t1) == fvalue(t2); case LUA_T_CLOSURE: return t1->value.cl == t2->value.cl; case LUA_T_TASK: return nvalue(t1) == nvalue(t2); default: #ifdef LUA_DEBUG LUA_INTERNALERROR("internal error in `lua_equalObj'"); #endif return 0; // UNREACHABLE } }
static void adjust_varargs (StkId first_extra_arg) { Object arg; Object *firstelem = stack+first_extra_arg; int nvararg = top-firstelem; int i; if (nvararg < 0) nvararg = 0; avalue(&arg) = lua_createarray(nvararg+1); /* +1 for field 'n' */ tag(&arg) = LUA_T_ARRAY; for (i=0; i<nvararg; i++) { Object index; tag(&index) = LUA_T_NUMBER; nvalue(&index) = i+1; *(lua_hashdefine(avalue(&arg), &index)) = *(firstelem+i); } /* store counter in field "n" */ { Object index, extra; tag(&index) = LUA_T_STRING; tsvalue(&index) = lua_createstring("n"); tag(&extra) = LUA_T_NUMBER; nvalue(&extra) = nvararg; *(lua_hashdefine(avalue(&arg), &index)) = extra; } adjust_top(first_extra_arg); *top = arg; incr_top; }
int luaO_equalval (TObject *t1, TObject *t2) { switch (ttype(t1)) { case LUA_T_NIL: return 1; case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2); case LUA_T_STRING: case LUA_T_USERDATA: return svalue(t1) == svalue(t2); case LUA_T_ARRAY: return avalue(t1) == avalue(t2); case LUA_T_PROTO: return tfvalue(t1) == tfvalue(t2); case LUA_T_CPROTO: return fvalue(t1) == fvalue(t2); case LUA_T_CLOSURE: return t1->value.cl == t2->value.cl; default: LUA_INTERNALERROR("invalid type"); return 0; /* UNREACHABLE */ } }
int lua_equalObj (Object *t1, Object *t2) { if (tag(t1) != tag(t2)) return 0; switch (tag(t1)) { case LUA_T_NIL: return 1; case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2); case LUA_T_STRING: return svalue(t1) == svalue(t2); case LUA_T_ARRAY: return avalue(t1) == avalue(t2); case LUA_T_FUNCTION: return t1->value.tf == t2->value.tf; case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2); default: return uvalue(t1) == uvalue(t2); } }
int luaO_equalval (const TObject *t1, const TObject *t2) { switch (ttype(t1)) { case TAG_NUMBER: return nvalue(t1) == nvalue(t2); case TAG_STRING: case TAG_USERDATA: return svalue(t1) == svalue(t2); case TAG_TABLE: return avalue(t1) == avalue(t2); case TAG_CCLOSURE: case TAG_LCLOSURE: return clvalue(t1) == clvalue(t2); default: LUA_ASSERT(L, ttype(t1) == TAG_NIL, "invalid type"); return 1; /* TAG_NIL */ } }
/* ** Function to index a table. Receives the table at top-2 and the index ** at top-1. */ static void pushsubscript (void) { TObject *im; if (ttype(top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */ im = luaI_getimbyObj(top-2, IM_GETTABLE); else { /* object is a table... */ int tg = (top-2)->value.a->htag; im = luaI_getim(tg, IM_GETTABLE); if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */ TObject *h = lua_hashget(avalue(top-2), top-1); if (h != NULL && ttype(h) != LUA_T_NIL) { --top; *(top-1) = *h; } else if (ttype(im=luaI_getim(tg, IM_INDEX)) != LUA_T_NIL) callIM(im, 2, 1); else { --top; ttype(top-1) = LUA_T_NIL; } return; } /* else it has a "gettable" method, go through to next command */ } /* object is not a table, or it has a "gettable" method */ if (ttype(im) != LUA_T_NIL) callIM(im, 2, 1); else lua_error("indexed expression not a table"); }
int lua_equalObj (TObject *t1, TObject *t2) { if (ttype(t1) != ttype(t2)) return 0; switch (ttype(t1)) { case LUA_T_NIL: return 1; case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2); case LUA_T_STRING: case LUA_T_USERDATA: return svalue(t1) == svalue(t2); case LUA_T_ARRAY: return avalue(t1) == avalue(t2); case LUA_T_FUNCTION: return t1->value.tf == t2->value.tf; case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2); default: lua_error("internal error in `lua_equalObj'"); return 0; /* UNREACHEABLE */ } }
/* ** API: creates a new table */ lua_Object lua_createtable (void) { TObject o; avalue(&o) = lua_createarray(0); ttype(&o) = LUA_T_ARRAY; return put_luaObject(&o); }
lua_Object lua_createtable() { TObject o; luaC_checkGC(); avalue(&o) = luaH_new(0); ttype(&o) = LUA_T_ARRAY; return put_luaObject(&o); }
static int32 hashindex(TObject *r) { int32 h; switch (ttype(r)) { case LUA_T_NUMBER: h = (int32)nvalue(r); break; case LUA_T_USERDATA: h = (int32)r->value.ud.id; case LUA_T_STRING: h = (int32)tsvalue(r); break; case LUA_T_ARRAY: h = (int32)avalue(r); break; case LUA_T_PROTO: h = (int32)tfvalue(r); break; case LUA_T_CPROTO: h = (int32)fvalue(r); break; case LUA_T_CLOSURE: h = (int32)clvalue(r); break; case LUA_T_TASK: h = (int32)nvalue(r); break; default: lua_error("unexpected type to index table"); h = 0; // to avoid warnings } return (h >= 0 ? h : -(h + 1)); }
/* ** Function to index a table. ** Receives the table at top-2 and the index at top-1. */ void luaV_gettable (void) { struct Stack *S = &L->stack; TObject *im; if (ttype(S->top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */ im = luaT_getimbyObj(S->top-2, IM_GETTABLE); else { /* object is a table... */ int32 tg = (S->top-2)->value.a->htag; im = luaT_getim(tg, IM_GETTABLE); if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */ TObject *h = luaH_get(avalue(S->top-2), S->top-1); if (h != NULL && ttype(h) != LUA_T_NIL) { --S->top; *(S->top-1) = *h; } else if (ttype(im=luaT_getim(tg, IM_INDEX)) != LUA_T_NIL) luaD_callTM(im, 2, 1); else { --S->top; ttype(S->top-1) = LUA_T_NIL; } return; } /* else it has a "gettable" method, go through to next command */ } /* object is not a table, or it has a "gettable" method */ if (ttype(im) != LUA_T_NIL) luaD_callTM(im, 2, 1); else lua_error("indexed expression not a table"); }
static long int hashindex (TObject *ref) { long int h; switch (ttype(ref)) { case LUA_T_NUMBER: h = (long int)nvalue(ref); break; case LUA_T_STRING: case LUA_T_USERDATA: h = (IntPoint)tsvalue(ref); break; case LUA_T_ARRAY: h = (IntPoint)avalue(ref); break; case LUA_T_PROTO: h = (IntPoint)tfvalue(ref); break; case LUA_T_CPROTO: h = (IntPoint)fvalue(ref); break; case LUA_T_CLOSURE: h = (IntPoint)clvalue(ref); break; default: lua_error("unexpected type to index table"); h = 0; /* to avoid warnings */ } return (h >= 0 ? h : -(h+1)); }
lua_Object lua_rawgettable (void) { checkCparams(2); if (ttype(L->stack.top-2) != LUA_T_ARRAY) lua_error("indexed expression not a table in rawgettable"); *(L->stack.top-2) = *luaH_get(avalue(L->stack.top-2), L->stack.top-1); --L->stack.top; return put_luaObjectonTop(); }
void luaC_hashcallIM(Hash *l) { TObject t; ttype(&t) = LUA_T_ARRAY; for (; l; l = (Hash *)l->head.next) { avalue(&t) = l; luaD_gcIM(&t); } }
int lua_next (lua_Object o, int i) { TObject *t = Address(o); if (ttype(t) != LUA_T_ARRAY) lua_error("API error - object is not a table in `lua_next'"); i = luaA_next(avalue(t), i); top2LC((i==0) ? 0 : 2); return i; }
void luaI_hashcallIM (Hash *l) { TObject t; ttype(&t) = LUA_T_ARRAY; for (; l; l=l->next) { avalue(&t) = l; luaI_gcIM(&t); } }
static void foreach (void) { TObject t = *luaA_Address(luaL_tablearg(1)); TObject f = *luaA_Address(luaL_functionarg(2)); int32 i; for (i=0; i<avalue(&t)->nhash; i++) { Node *nd = &(avalue(&t)->node[i]); if (ttype(ref(nd)) != LUA_T_NIL && ttype(val(nd)) != LUA_T_NIL) { luaA_pushobject(&f); luaA_pushobject(ref(nd)); luaA_pushobject(val(nd)); luaD_call((L->stack.top-L->stack.stack)-2, 1); if (ttype(L->stack.top-1) != LUA_T_NIL) return; L->stack.top--; } } }
/* ** API: creates a new table */ lua_Object lua_createtable (void) { adjustC(0); avalue(top) = lua_createarray(0); tag(top) = LUA_T_ARRAY; incr_top; CLS_current.base++; /* incorporate object in the stack */ return Ref(top-1); }
/* ** Mark an object if it is a string or a unmarked array. */ int lua_markobject (Object *o) {/* if already marked, does not change mark value */ if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked) tsvalue(o)->marked = 1; else if (tag(o) == LUA_T_ARRAY) lua_hashmark (avalue(o)); else if ((o->tag == LUA_T_FUNCTION || o->tag == LUA_T_MARK) && !o->value.tf->marked) o->value.tf->marked = 1; return 0; }
/* ** Function to store indexed based on values at the top */ static void storesubscript (void) { if (tag(top-3) != LUA_T_ARRAY) callFB(FB_SETTABLE); else { Object *h = lua_hashdefine (avalue(top-3), top-2); *h = *(top-1); top -= 3; } }
Node *luaH_next(TObject *o, TObject *r) { Hash *t = avalue(o); if (ttype(r) == LUA_T_NIL) return hashnext(t, 0); else { int32 i = present(t, r); Node *n = node(t, i); luaL_arg_check(ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL, 2, "key not found"); return hashnext(t, i + 1); } }
void luaV_pack (StkId firstel, int32 nvararg, TObject *tab) { TObject *firstelem = L->stack.stack+firstel; int32 i; if (nvararg < 0) nvararg = 0; avalue(tab) = luaH_new(nvararg+1); /* +1 for field 'n' */ ttype(tab) = LUA_T_ARRAY; for (i=0; i<nvararg; i++) { TObject index; ttype(&index) = LUA_T_NUMBER; nvalue(&index) = (real)i+1; *(luaH_set(avalue(tab), &index)) = *(firstelem+i); } /* store counter in field "n" */ { TObject index, extra; ttype(&index) = LUA_T_STRING; tsvalue(&index) = luaS_new("n"); ttype(&extra) = LUA_T_NUMBER; nvalue(&extra) = (real)nvararg; *(luaH_set(avalue(tab), &index)) = extra; } }
void lua_next (void) { Hash *t; lua_Object o = lua_getparam(1); lua_Object r = lua_getparam(2); luaL_arg_check(lua_istable(o), 1, "table expected"); luaL_arg_check(r != LUA_NOOBJECT, 2, "value expected"); t = avalue(luaI_Address(o)); if (lua_isnil(r)) hashnext(t, 0); else hashnext(t, present(t, luaI_Address(r))+1); }
/* lua next optimized */ int lraw_next(lua_State *L, lua_Object lobj, int index, Node **n) { TObject *obj = lapi_address(L, lobj); Hash *hash = avalue(obj); int tsize = nhash(L, hash); while (index < tsize) { *n = node(L, hash, index); if (ttype(val(L, *n)) != LUA_T_NIL) { return index + 1; /* index to be used next time */ } index++; } return 0; /* no more elements */ }
static void call_fallbacks (void) { Hash *curr_array; Object t; tag(&t) = LUA_T_ARRAY; for (curr_array = listhead; curr_array; curr_array = curr_array->next) if (markarray(curr_array) != 1) { avalue(&t) = curr_array; luaI_gcFB(&t); } tag(&t) = LUA_T_NIL; luaI_gcFB(&t); /* end of list */ }
lua_Object lua_rawgettable (void) { checkCparams(2); if (ttype(top-2) != LUA_T_ARRAY) lua_error("indexed expression not a table in raw gettable"); else { TObject *h = lua_hashget(avalue(top-2), top-1); --top; if (h != NULL) *(top-1) = *h; else ttype(top-1) = LUA_T_NIL; } return put_luaObjectonTop(); }
lua_Object lua_rawgettable() { checkCparams(2); if (ttype(lua_state->stack.top-2) != LUA_T_ARRAY) { lua_error("indexed expression not a table in rawgettable"); } else { TObject *h = luaH_get(avalue(lua_state->stack.top - 2), lua_state->stack.top - 1); --lua_state->stack.top; if (h) { *(lua_state->stack.top-1) = *h; } else { ttype(lua_state->stack.top-1) = LUA_T_NIL; } } return put_luaObjectonTop(); }
/* ** Function to index a table. Receives the table at top-2 and the index ** at top-1. */ static void pushsubscript (void) { if (tag(top-2) != LUA_T_ARRAY) callFB(FB_GETTABLE); else { Object *h = lua_hashget(avalue(top-2), top-1); if (h == NULL || tag(h) == LUA_T_NIL) callFB(FB_INDEX); else { --top; *(top-1) = *h; } } }
/* ** Internal function: print object values */ void lua_print (void) { int i=1; lua_Object obj; while ((obj=lua_getparam (i++)) != LUA_NOOBJECT) { if (lua_isnumber(obj)) printf("%g\n",lua_getnumber(obj)); else if (lua_isstring(obj)) printf("%s\n",lua_getstring(obj)); else if (lua_isfunction(obj)) printf("function: %p\n",bvalue(luaI_Address(obj))); else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction(obj) ); else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata(obj)); else if (lua_istable(obj)) printf("table: %p\n",avalue(luaI_Address(obj))); else if (lua_isnil(obj)) printf("nil\n"); else printf("invalid value to print\n"); } }
static int32 markobject (TObject *o) { switch (ttype(o)) { case LUA_T_USERDATA: case LUA_T_STRING: strmark(tsvalue(o)); break; case LUA_T_ARRAY: hashmark(avalue(o)); break; case LUA_T_CLOSURE: case LUA_T_CLMARK: closuremark(o->value.cl); break; case LUA_T_PROTO: case LUA_T_PMARK: protomark(o->value.tf); break; default: break; /* numbers, cprotos, etc */ } return 0; }