CHDK_DE Vorschauversion  Trunk Rev. 5163
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
lvm.c-Dateireferenz
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#include "lvm.h"
+ Include-Abhängigkeitsdiagramm für lvm.c:

gehe zum Quellcode dieser Datei

Makrodefinitionen

#define lvm_c
 
#define LUA_CORE
 
#define MAXTAGLOOP   100
 
#define runtime_check(L, c)   { if (!(c)) break; }
 
#define RA(i)   (base+GETARG_A(i))
 
#define RB(i)   check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
 
#define RC(i)   check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
 
#define RKB(i)
 
#define RKC(i)
 
#define KBx(i)   check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
 
#define dojump(L, pc, i)   {(pc) += (i); luai_threadyield(L);}
 
#define Protect(x)   { L->savedpc = pc; {x;}; base = L->base; }
 
#define arith_op(op, tm)
 

Funktionen

LUA_NUMBER luai_ipow (LUA_NUMBER a, LUA_NUMBER b)
 
LUAI_FUNC const TValueluaV_tonumber (const TValue *obj, TValue *n)
 
LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj)
 
static void traceexec (lua_State *L, const Instruction *pc)
 
static void callTMres (lua_State *L, StkId res, const TValue *f, const TValue *p1, const TValue *p2)
 
static void callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, const TValue *p3)
 
LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val)
 
LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val)
 
static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event)
 
static const TValueget_compTM (lua_State *L, Table *mt1, Table *mt2, TMS event)
 
static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event)
 
static int l_strcmp (const TString *ls, const TString *rs)
 
LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r)
 
static int lessequal (lua_State *L, const TValue *l, const TValue *r)
 
LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2)
 
LUAI_FUNC void luaV_concat (lua_State *L, int total, int last)
 
static void Arith (lua_State *L, StkId ra, const TValue *rb, const TValue *rc, TMS op)
 
LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls)
 

Makro-Dokumentation

#define arith_op (   op,
  tm 
)
Wert:
{ \
TValue *rb = RKB(i); \
TValue *rc = RKC(i); \
if (ttisnumber(rb) && ttisnumber(rc)) { \
lua_Number nb = nvalue(rb), nc = nvalue(rc); \
setnvalue(ra, op(nb, nc)); \
} \
Protect(Arith(L, ra, rb, rc, tm)); \
}

Definiert in Zeile 380 der Datei lvm.c.

#define dojump (   L,
  pc,
 
)    {(pc) += (i); luai_threadyield(L);}

Definiert in Zeile 374 der Datei lvm.c.

#define KBx (   i)    check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))

Definiert in Zeile 371 der Datei lvm.c.

#define LUA_CORE

Definiert in Zeile 13 der Datei lvm.c.

#define lvm_c

Definiert in Zeile 12 der Datei lvm.c.

#define MAXTAGLOOP   100

Definiert in Zeile 48 der Datei lvm.c.

#define Protect (   x)    { L->savedpc = pc; {x;}; base = L->base; }

Definiert in Zeile 377 der Datei lvm.c.

#define RA (   i)    (base+GETARG_A(i))

Definiert in Zeile 363 der Datei lvm.c.

#define RB (   i)    check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))

Definiert in Zeile 365 der Datei lvm.c.

#define RC (   i)    check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))

Definiert in Zeile 366 der Datei lvm.c.

#define RKB (   i)
Wert:

Definiert in Zeile 367 der Datei lvm.c.

#define RKC (   i)
Wert:

Definiert in Zeile 369 der Datei lvm.c.

#define runtime_check (   L,
 
)    { if (!(c)) break; }

Definiert in Zeile 361 der Datei lvm.c.

Dokumentation der Funktionen

static void Arith ( lua_State L,
StkId  ra,
const TValue rb,
const TValue rc,
TMS  op 
)
static

Definiert in Zeile 333 der Datei lvm.c.

334  {
335  TValue tempb, tempc;
336  const TValue *b, *c;
337  if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
338  (c = luaV_tonumber(rc, &tempc)) != NULL) {
339  lua_Number nb = nvalue(b), nc = nvalue(c);
340  switch (op) {
341  case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
342  case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
343  case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
344  case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
345  case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
346  case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
347  case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
348  default: lua_assert(0); break;
349  }
350  }
351  else if (!call_binTM(L, rb, rc, ra, op))
352  luaG_aritherror(L, rb, rc);
353 }
static int call_binTM ( lua_State L,
const TValue p1,
const TValue p2,
StkId  res,
TMS  event 
)
static

Definiert in Zeile 181 der Datei lvm.c.

182  {
183  const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
184  if (ttisnil(tm))
185  tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
186  if (ttisnil(tm)) return 0;
187  callTMres(L, res, tm, p1, p2);
188  return 1;
189 }
static int call_orderTM ( lua_State L,
const TValue p1,
const TValue p2,
TMS  event 
)
static

Definiert in Zeile 206 der Datei lvm.c.

207  {
208  const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
209  const TValue *tm2;
210  if (ttisnil(tm1)) return -1; /* no metamethod? */
211  tm2 = luaT_gettmbyobj(L, p2, event);
212  if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */
213  return -1;
214  callTMres(L, L->top, tm1, p1, p2);
215  return !l_isfalse(L->top);
216 }
static void callTM ( lua_State L,
const TValue f,
const TValue p1,
const TValue p2,
const TValue p3 
)
static

Definiert in Zeile 112 der Datei lvm.c.

113  {
114  setobj2s(L, L->top, f); /* push function */
115  setobj2s(L, L->top+1, p1); /* 1st argument */
116  setobj2s(L, L->top+2, p2); /* 2nd argument */
117  setobj2s(L, L->top+3, p3); /* 3th argument */
118  luaD_checkstack(L, 4);
119  L->top += 4;
120  luaD_call(L, L->top - 4, 0);
121 }
static void callTMres ( lua_State L,
StkId  res,
const TValue f,
const TValue p1,
const TValue p2 
)
static

Definiert in Zeile 96 der Datei lvm.c.

97  {
98  ptrdiff_t result = savestack(L, res);
99  setobj2s(L, L->top, f); /* push function */
100  setobj2s(L, L->top+1, p1); /* 1st argument */
101  setobj2s(L, L->top+2, p2); /* 2nd argument */
102  luaD_checkstack(L, 3);
103  L->top += 3;
104  luaD_call(L, L->top - 3, 1);
105  res = restorestack(L, result);
106  L->top--;
107  setobjs2s(L, res, L->top);
108 }
static const TValue* get_compTM ( lua_State L,
Table mt1,
Table mt2,
TMS  event 
)
static

Definiert in Zeile 192 der Datei lvm.c.

193  {
194  const TValue *tm1 = fasttm(L, mt1, event);
195  const TValue *tm2;
196  if (tm1 == NULL) return NULL; /* no metamethod */
197  if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
198  tm2 = fasttm(L, mt2, event);
199  if (tm2 == NULL) return NULL; /* no metamethod */
200  if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
201  return tm1;
202  return NULL;
203 }
static int l_strcmp ( const TString ls,
const TString rs 
)
static

Definiert in Zeile 219 der Datei lvm.c.

219  {
220  const char *l = getstr(ls);
221  size_t ll = ls->tsv.len;
222  const char *r = getstr(rs);
223  size_t lr = rs->tsv.len;
224  for (;;) {
225  int temp = strcoll(l, r);
226  if (temp != 0) return temp;
227  else { /* strings are equal up to a `\0' */
228  size_t len = strlen(l); /* index of first `\0' in both strings */
229  if (len == lr) /* r is finished? */
230  return (len == ll) ? 0 : 1;
231  else if (len == ll) /* l is finished? */
232  return -1; /* l is smaller than r (because r is not finished) */
233  /* both strings longer than `len'; go on comparing (after the `\0') */
234  len++;
235  l += len; ll -= len; r += len; lr -= len;
236  }
237  }
238 }
static int lessequal ( lua_State L,
const TValue l,
const TValue r 
)
static

Definiert in Zeile 255 der Datei lvm.c.

255  {
256  int res;
257  if (ttype(l) != ttype(r))
258  return luaG_ordererror(L, l, r);
259  else if (ttisnumber(l))
260  return luai_numle(nvalue(l), nvalue(r));
261  else if (ttisstring(l))
262  return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
263  else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
264  return res;
265  else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
266  return !res;
267  return luaG_ordererror(L, l, r);
268 }
LUA_NUMBER luai_ipow ( LUA_NUMBER  a,
LUA_NUMBER  b 
)

Definiert in Zeile 29 der Datei lvm.c.

29  {
30  if (b < 0)
31  return 0;
32  else if (b == 0)
33  return 1;
34  else {
35  LUA_NUMBER c = 1;
36  for (;;) {
37  if (b & 1)
38  c *= a;
39  b = b >> 1;
40  if (b == 0)
41  return c;
42  a *= a;
43  }
44  }
45 }
LUAI_FUNC void luaV_concat ( lua_State L,
int  total,
int  last 
)

Definiert in Zeile 298 der Datei lvm.c.

298  {
299  do {
300  StkId top = L->base + last + 1;
301  int n = 2; /* number of elements handled in this pass (at least 2) */
302  if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
303  if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
304  luaG_concaterror(L, top-2, top-1);
305  } else if (tsvalue(top-1)->len == 0) /* second op is empty? */
306  (void)tostring(L, top - 2); /* result is first op (as string) */
307  else {
308  /* at least two string values; get as many as possible */
309  size_t tl = tsvalue(top-1)->len;
310  char *buffer;
311  int i;
312  /* collect total length */
313  for (n = 1; n < total && tostring(L, top-n-1); n++) {
314  size_t l = tsvalue(top-n-1)->len;
315  if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
316  tl += l;
317  }
318  buffer = luaZ_openspace(L, &G(L)->buff, tl);
319  tl = 0;
320  for (i=n; i>0; i--) { /* concat all strings */
321  size_t l = tsvalue(top-i)->len;
322  memcpy(buffer+tl, svalue(top-i), l);
323  tl += l;
324  }
325  setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
326  }
327  total -= n-1; /* got `n' strings to create 1 new */
328  last -= n-1;
329  } while (total > 1); /* repeat until only 1 result left */
330 }
LUAI_FUNC int luaV_equalval ( lua_State L,
const TValue t1,
const TValue t2 
)

Definiert in Zeile 271 der Datei lvm.c.

271  {
272  const TValue *tm;
273  lua_assert(ttype(t1) == ttype(t2));
274  switch (ttype(t1)) {
275  case LUA_TNIL: return 1;
276  case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
277  case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
278  case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
279  case LUA_TUSERDATA: {
280  if (uvalue(t1) == uvalue(t2)) return 1;
281  tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
282  TM_EQ);
283  break; /* will try TM */
284  }
285  case LUA_TTABLE: {
286  if (hvalue(t1) == hvalue(t2)) return 1;
287  tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
288  break; /* will try TM */
289  }
290  default: return gcvalue(t1) == gcvalue(t2);
291  }
292  if (tm == NULL) return 0; /* no TM? */
293  callTMres(L, L->top, tm, t1, t2); /* call TM */
294  return !l_isfalse(L->top);
295 }
LUAI_FUNC void luaV_execute ( lua_State L,
int  nexeccalls 
)

Definiert in Zeile 393 der Datei lvm.c.

393  {
394  LClosure *cl;
395  StkId base;
396  TValue *k;
397  const Instruction *pc;
398  reentry: /* entry point */
399  lua_assert(isLua(L->ci));
400  pc = L->savedpc;
401  cl = &clvalue(L->ci->func)->l;
402  base = L->base;
403  k = cl->p->k;
404  /* main loop of interpreter */
405  for (;;) {
406  const Instruction i = *pc++;
407  StkId ra;
408  if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
409  (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
410  traceexec(L, pc);
411  if (L->status == LUA_YIELD) { /* did hook yield? */
412  L->savedpc = pc - 1;
413  return;
414  }
415  base = L->base;
416  }
417  /* warning!! several calls may realloc the stack and invalidate `ra' */
418  ra = RA(i);
419  lua_assert(base == L->base && L->base == L->ci->base);
420  lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
421  lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
422  switch (GET_OPCODE(i)) {
423  case OP_MOVE: {
424  setobjs2s(L, ra, RB(i));
425  continue;
426  }
427  case OP_LOADK: {
428  setobj2s(L, ra, KBx(i));
429  continue;
430  }
431  case OP_LOADBOOL: {
432  setbvalue(ra, GETARG_B(i));
433  if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
434  continue;
435  }
436  case OP_LOADNIL: {
437  TValue *rb = RB(i);
438  do {
439  setnilvalue(rb--);
440  } while (rb >= ra);
441  continue;
442  }
443  case OP_GETUPVAL: {
444  int b = GETARG_B(i);
445  setobj2s(L, ra, cl->upvals[b]->v);
446  continue;
447  }
448  case OP_GETGLOBAL: {
449  TValue g;
450  TValue *rb = KBx(i);
451  sethvalue(L, &g, cl->env);
452  lua_assert(ttisstring(rb));
453  Protect(luaV_gettable(L, &g, rb, ra));
454  continue;
455  }
456  case OP_GETTABLE: {
457  Protect(luaV_gettable(L, RB(i), RKC(i), ra));
458  continue;
459  }
460  case OP_SETGLOBAL: {
461  TValue g;
462  sethvalue(L, &g, cl->env);
464  Protect(luaV_settable(L, &g, KBx(i), ra));
465  continue;
466  }
467  case OP_SETUPVAL: {
468  UpVal *uv = cl->upvals[GETARG_B(i)];
469  setobj(L, uv->v, ra);
470  luaC_barrier(L, uv, ra);
471  continue;
472  }
473  case OP_SETTABLE: {
474  Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
475  continue;
476  }
477  case OP_NEWTABLE: {
478  int b = GETARG_B(i);
479  int c = GETARG_C(i);
480  sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
481  Protect(luaC_checkGC(L));
482  continue;
483  }
484  case OP_SELF: {
485  StkId rb = RB(i);
486  setobjs2s(L, ra+1, rb);
487  Protect(luaV_gettable(L, rb, RKC(i), ra));
488  continue;
489  }
490  case OP_ADD: {
491  arith_op(luai_numadd, TM_ADD);
492  continue;
493  }
494  case OP_SUB: {
495  arith_op(luai_numsub, TM_SUB);
496  continue;
497  }
498  case OP_MUL: {
499  arith_op(luai_nummul, TM_MUL);
500  continue;
501  }
502  case OP_DIV: {
503  arith_op(luai_numdiv, TM_DIV);
504  continue;
505  }
506  case OP_MOD: {
507  arith_op(luai_nummod, TM_MOD);
508  continue;
509  }
510  case OP_POW: {
511  arith_op(luai_numpow, TM_POW);
512  continue;
513  }
514  case OP_UNM: {
515  TValue *rb = RB(i);
516  if (ttisnumber(rb)) {
517  lua_Number nb = nvalue(rb);
518  setnvalue(ra, luai_numunm(nb));
519  }
520  else {
521  Protect(Arith(L, ra, rb, rb, TM_UNM));
522  }
523  continue;
524  }
525  case OP_NOT: {
526  int res = l_isfalse(RB(i)); /* next assignment may change this value */
527  setbvalue(ra, res);
528  continue;
529  }
530  case OP_LEN: {
531  const TValue *rb = RB(i);
532  switch (ttype(rb)) {
533  case LUA_TTABLE: {
534  setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
535  break;
536  }
537  case LUA_TSTRING: {
538  setnvalue(ra, cast_num(tsvalue(rb)->len));
539  break;
540  }
541  default: { /* try metamethod */
542  Protect(
543  if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN))
544  luaG_typeerror(L, rb, "get length of");
545  )
546  }
547  }
548  continue;
549  }
550  case OP_CONCAT: {
551  int b = GETARG_B(i);
552  int c = GETARG_C(i);
553  Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
554  setobjs2s(L, RA(i), base+b);
555  continue;
556  }
557  case OP_JMP: {
558  dojump(L, pc, GETARG_sBx(i));
559  continue;
560  }
561  case OP_EQ: {
562  TValue *rb = RKB(i);
563  TValue *rc = RKC(i);
564  Protect(
565  if (equalobj(L, rb, rc) == GETARG_A(i))
566  dojump(L, pc, GETARG_sBx(*pc));
567  )
568  pc++;
569  continue;
570  }
571  case OP_LT: {
572  Protect(
573  if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
574  dojump(L, pc, GETARG_sBx(*pc));
575  )
576  pc++;
577  continue;
578  }
579  case OP_LE: {
580  Protect(
581  if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
582  dojump(L, pc, GETARG_sBx(*pc));
583  )
584  pc++;
585  continue;
586  }
587  case OP_TEST: {
588  if (l_isfalse(ra) != GETARG_C(i))
589  dojump(L, pc, GETARG_sBx(*pc));
590  pc++;
591  continue;
592  }
593  case OP_TESTSET: {
594  TValue *rb = RB(i);
595  if (l_isfalse(rb) != GETARG_C(i)) {
596  setobjs2s(L, ra, rb);
597  dojump(L, pc, GETARG_sBx(*pc));
598  }
599  pc++;
600  continue;
601  }
602  case OP_CALL: {
603  int b = GETARG_B(i);
604  int nresults = GETARG_C(i) - 1;
605  if (b != 0) L->top = ra+b; /* else previous instruction set top */
606  L->savedpc = pc;
607  switch (luaD_precall(L, ra, nresults)) {
608  case PCRLUA: {
609  nexeccalls++;
610  goto reentry; /* restart luaV_execute over new Lua function */
611  }
612  case PCRC: {
613  /* it was a C function (`precall' called it); adjust results */
614  if (nresults >= 0) L->top = L->ci->top;
615  base = L->base;
616  continue;
617  }
618  default: {
619  return; /* yield */
620  }
621  }
622  }
623  case OP_TAILCALL: {
624  int b = GETARG_B(i);
625  if (b != 0) L->top = ra+b; /* else previous instruction set top */
626  L->savedpc = pc;
627  lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
628  switch (luaD_precall(L, ra, LUA_MULTRET)) {
629  case PCRLUA: {
630  /* tail call: put new frame in place of previous one */
631  CallInfo *ci = L->ci - 1; /* previous frame */
632  int aux;
633  StkId func = ci->func;
634  StkId pfunc = (ci+1)->func; /* previous function index */
635  if (L->openupval) luaF_close(L, ci->base);
636  L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
637  for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
638  setobjs2s(L, func+aux, pfunc+aux);
639  ci->top = L->top = func+aux; /* correct top */
640  lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
641  ci->savedpc = L->savedpc;
642  ci->tailcalls++; /* one more call lost */
643  L->ci--; /* remove new frame */
644  goto reentry;
645  }
646  case PCRC: { /* it was a C function (`precall' called it) */
647  base = L->base;
648  continue;
649  }
650  default: {
651  return; /* yield */
652  }
653  }
654  }
655  case OP_RETURN: {
656  int b = GETARG_B(i);
657  if (b != 0) L->top = ra+b-1;
658  if (L->openupval) luaF_close(L, base);
659  L->savedpc = pc;
660  b = luaD_poscall(L, ra);
661  if (--nexeccalls == 0) /* was previous function running `here'? */
662  return; /* no: return */
663  else { /* yes: continue its execution */
664  if (b) L->top = L->ci->top;
665  lua_assert(isLua(L->ci));
666  lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
667  goto reentry;
668  }
669  }
670  case OP_FORLOOP: {
671  lua_Number step = nvalue(ra+2);
672  lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
673  lua_Number limit = nvalue(ra+1);
674  if (luai_numlt(0, step) ? luai_numle(idx, limit)
675  : luai_numle(limit, idx)) {
676  dojump(L, pc, GETARG_sBx(i)); /* jump back */
677  setnvalue(ra, idx); /* update internal index... */
678  setnvalue(ra+3, idx); /* ...and external index */
679  }
680  continue;
681  }
682  case OP_FORPREP: {
683  const TValue *init = ra;
684  const TValue *plimit = ra+1;
685  const TValue *pstep = ra+2;
686  L->savedpc = pc; /* next steps may throw errors */
687  if (!tonumber(init, ra))
688  luaG_runerror(L, LUA_QL("for") " initial value must be a number");
689  else if (!tonumber(plimit, ra+1))
690  luaG_runerror(L, LUA_QL("for") " limit must be a number");
691  else if (!tonumber(pstep, ra+2))
692  luaG_runerror(L, LUA_QL("for") " step must be a number");
693  setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
694  dojump(L, pc, GETARG_sBx(i));
695  continue;
696  }
697  case OP_TFORLOOP: {
698  StkId cb = ra + 3; /* call base */
699  setobjs2s(L, cb+2, ra+2);
700  setobjs2s(L, cb+1, ra+1);
701  setobjs2s(L, cb, ra);
702  L->top = cb+3; /* func. + 2 args (state and index) */
703  Protect(luaD_call(L, cb, GETARG_C(i)));
704  L->top = L->ci->top;
705  cb = RA(i) + 3; /* previous call may change the stack */
706  if (!ttisnil(cb)) { /* continue loop? */
707  setobjs2s(L, cb-1, cb); /* save control variable */
708  dojump(L, pc, GETARG_sBx(*pc)); /* jump back */
709  }
710  pc++;
711  continue;
712  }
713  case OP_SETLIST: {
714  int n = GETARG_B(i);
715  int c = GETARG_C(i);
716  int last;
717  Table *h;
718  if (n == 0) {
719  n = cast_int(L->top - ra) - 1;
720  L->top = L->ci->top;
721  }
722  if (c == 0) c = cast_int(*pc++);
723  runtime_check(L, ttistable(ra));
724  h = hvalue(ra);
725  last = ((c-1)*LFIELDS_PER_FLUSH) + n;
726  if (last > h->sizearray) /* needs more space? */
727  luaH_resizearray(L, h, last); /* pre-alloc it at once */
728  for (; n > 0; n--) {
729  TValue *val = ra+n;
730  setobj2t(L, luaH_setnum(L, h, last--), val);
731  luaC_barriert(L, h, val);
732  }
733  continue;
734  }
735  case OP_CLOSE: {
736  luaF_close(L, ra);
737  continue;
738  }
739  case OP_CLOSURE: {
740  Proto *p;
741  Closure *ncl;
742  int nup, j;
743  p = cl->p->p[GETARG_Bx(i)];
744  nup = p->nups;
745  ncl = luaF_newLclosure(L, nup, cl->env);
746  ncl->l.p = p;
747  for (j=0; j<nup; j++, pc++) {
748  if (GET_OPCODE(*pc) == OP_GETUPVAL)
749  ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
750  else {
751  lua_assert(GET_OPCODE(*pc) == OP_MOVE);
752  ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
753  }
754  }
755  setclvalue(L, ra, ncl);
756  Protect(luaC_checkGC(L));
757  continue;
758  }
759  case OP_VARARG: {
760  int b = GETARG_B(i) - 1;
761  int j;
762  CallInfo *ci = L->ci;
763  int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;
764  if (b == LUA_MULTRET) {
765  Protect(luaD_checkstack(L, n));
766  ra = RA(i); /* previous call may change the stack */
767  b = n;
768  L->top = ra + n;
769  }
770  for (j = 0; j < b; j++) {
771  if (j < n) {
772  setobjs2s(L, ra + j, ci->base - n + j);
773  }
774  else {
775  setnilvalue(ra + j);
776  }
777  }
778  continue;
779  }
780  }
781  }
782 }
LUAI_FUNC void luaV_gettable ( lua_State L,
const TValue t,
TValue key,
StkId  val 
)

Definiert in Zeile 124 der Datei lvm.c.

124  {
125  int loop;
126  for (loop = 0; loop < MAXTAGLOOP; loop++) {
127  const TValue *tm;
128  if (ttistable(t)) { /* `t' is a table? */
129  Table *h = hvalue(t);
130  const TValue *res = luaH_get(h, key); /* do a primitive get */
131  if (!ttisnil(res) || /* result is no nil? */
132  (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
133  setobj2s(L, val, res);
134  return;
135  }
136  /* else will try the tag method */
137  }
138  else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
139  luaG_typeerror(L, t, "index");
140  if (ttisfunction(tm)) {
141  callTMres(L, val, tm, t, key);
142  return;
143  }
144  t = tm; /* else repeat with `tm' */
145  }
146  luaG_runerror(L, "loop in gettable");
147 }
LUAI_FUNC int luaV_lessthan ( lua_State L,
const TValue l,
const TValue r 
)

Definiert in Zeile 241 der Datei lvm.c.

241  {
242  int res;
243  if (ttype(l) != ttype(r))
244  return luaG_ordererror(L, l, r);
245  else if (ttisnumber(l))
246  return luai_numlt(nvalue(l), nvalue(r));
247  else if (ttisstring(l))
248  return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
249  else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
250  return res;
251  return luaG_ordererror(L, l, r);
252 }
LUAI_FUNC void luaV_settable ( lua_State L,
const TValue t,
TValue key,
StkId  val 
)

Definiert in Zeile 150 der Datei lvm.c.

150  {
151  int loop;
152  TValue temp;
153  for (loop = 0; loop < MAXTAGLOOP; loop++) {
154  const TValue *tm;
155  if (ttistable(t)) { /* `t' is a table? */
156  Table *h = hvalue(t);
157  TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
158  if (!ttisnil(oldval) || /* result is no nil? */
159  (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
160  setobj2t(L, oldval, val);
161  h->flags = 0;
162  luaC_barriert(L, h, val);
163  return;
164  }
165  /* else will try the tag method */
166  }
167  else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
168  luaG_typeerror(L, t, "index");
169  if (ttisfunction(tm)) {
170  callTM(L, tm, t, key, val);
171  return;
172  }
173  /* else repeat with `tm' */
174  setobj(L, &temp, tm); /* avoid pointing inside table (may rehash) */
175  t = &temp;
176  }
177  luaG_runerror(L, "loop in settable");
178 }
LUAI_FUNC const TValue* luaV_tonumber ( const TValue obj,
TValue n 
)

Definiert in Zeile 51 der Datei lvm.c.

51  {
53  if (ttisnumber(obj)) return obj;
54  if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
55  setnvalue(n, num);
56  return n;
57  }
58  else
59  return NULL;
60 }
LUAI_FUNC int luaV_tostring ( lua_State L,
StkId  obj 
)

Definiert in Zeile 63 der Datei lvm.c.

63  {
64  if (!ttisnumber(obj))
65  return 0;
66  else {
67  char s[LUAI_MAXNUMBER2STR];
68  lua_Number n = nvalue(obj);
69  lua_number2str(s, n);
70  setsvalue2s(L, obj, luaS_new(L, s));
71  return 1;
72  }
73 }
static void traceexec ( lua_State L,
const Instruction pc 
)
static

Definiert in Zeile 76 der Datei lvm.c.

76  {
77  lu_byte mask = L->hookmask;
78  const Instruction *oldpc = L->savedpc;
79  L->savedpc = pc;
80  if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
81  resethookcount(L);
83  }
84  if (mask & LUA_MASKLINE) {
85  Proto *p = ci_func(L->ci)->l.p;
86  int npc = pcRel(pc, p);
87  int newline = getline(p, npc);
88  /* call linehook when enter a new function, when jump back (loop),
89  or when enter a new line */
90  if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p)))
91  luaD_callhook(L, LUA_HOOKLINE, newline);
92  }
93 }