Пример #1
0
/*
** this function can be called asynchronous (e.g. during a signal)
*/
LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
  if (func == NULL || mask == 0) {  /* turn off hooks? */
    mask = 0;
    func = NULL;
  }
  if (isLua(L->ci))
    L->oldpc = L->ci->u.l.savedpc;
  L->hook = func;
  L->basehookcount = count;
  resethookcount(L);
  L->hookmask = cast_byte(mask);
  return 1;
}
Пример #2
0
/*
** this function can be called asynchronous (e.g. during a signal)
*/
LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count)
{
    if (func == NULL || mask == 0)    /* turn off hooks? */
    {
        mask = 0;
        func = NULL;
    }
    L->hook = func;
    L->basehookcount = count;
    resethookcount(L);
    L->hookmask = cast_byte(mask);
    return 1;
}
Пример #3
0
static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
  Proto *f = fs->f;
  int oldsize = f->sizeupvalues;
  checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues");
  luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues,
                  Upvaldesc, MAXUPVAL, "upvalues");
  while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL;
  f->upvalues[fs->nups].instack = (v->k == VLOCAL);
  f->upvalues[fs->nups].idx = cast_byte(v->u.info);
  f->upvalues[fs->nups].name = name;
  luaC_objbarrier(fs->ls->L, f, name);
  return fs->nups++;
}
Пример #4
0
static int indexupvalue (FuncState *fs, TString *name, expdesc *v) {
  int i;
  Proto *f = fs->f;
  int oldsize = f->sizeupvalues;
  for (i=0; i<f->nups; i++) {
    if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->u.s.info) {
      lua_assert(f->upvalues[i] == name);
      return i;
    }
  }
  /* new one */
  luaY_checklimit(fs, f->nups + 1, LUAI_MAXUPVALUES, "upvalues");
  luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues,
                  TString *, MAX_INT, "");
  while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL;
  f->upvalues[f->nups] = name;
  luaC_objbarrier(fs->L, f, name);
  lua_assert(v->k == VLOCAL || v->k == VUPVAL);
  fs->upvalues[f->nups].k = cast_byte(v->k);
  fs->upvalues[f->nups].info = cast_byte(v->u.s.info);
  return f->nups++;
}
Пример #5
0
/*
* make header for precompiled chunks
* if you change the code below be sure to update LoadHeader and FORMAT above
* and LUAC_HEADERSIZE in lundump.h
*/
void luaU_header (lu_byte* h)
{
 int x=1;
 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char));
 h+=sizeof(LUA_SIGNATURE)-sizeof(char);
 *h++=cast_byte(VERSION);
 *h++=cast_byte(FORMAT);
 *h++=cast_byte(*(char*)&x);			/* endianness */
 *h++=cast_byte(sizeof(int));
 *h++=cast_byte(sizeof(size_t));
 *h++=cast_byte(sizeof(Instruction));
 *h++=cast_byte(sizeof(lua_Number));
 *h++=cast_byte(((lua_Number)0.5)==0);		/* is lua_Number integral? */
 memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char));
}
Пример #6
0
Closure *lua_newLclosure(lua_State *luaState, int numElements, Table *elementTable) {
	Closure *c = (Closure *)lua_malloc(luaState, sizeLclosure(numElements));
	lua_linkObjToGC(luaState, obj2gco(c), LUA_TFUNCTION);

	c->l.isC = 0;
	c->l.env = elementTable;
	c->l.nupvalues = cast_byte(numElements);

	while (numElements--) {
		c->l.upvals[numElements] = NULL;
	}

	return c;
}
Пример #7
0
void luaD_throw (lua_State *L, int errcode) {
  if (L->errorJmp) {
    L->errorJmp->status = errcode;
    LUAI_THROW(L, L->errorJmp);
  }
  else {
    L->status = cast_byte(errcode);
    if (G(L)->panic) {
      resetstack(L, errcode);
      lua_unlock(L);
      G(L)->panic(L);
    }
    exit(EXIT_FAILURE);
  }
}
Пример #8
0
/* Create a new table. Note: the slots are not initialized (yet). */
static GCtab *newtab(lua_State *L, uint32_t asize, uint32_t hbits)
{
  GCtab *t;
  /* First try to colocate the array part. */
  if (LJ_MAX_COLOSIZE && asize > 0 && asize <= LJ_MAX_COLOSIZE) {
    lua_assert((sizeof(GCtab) & 7) == 0);
    t = (GCtab *)lj_mem_newgco(L, sizetabcolo(asize));
    t->gct = ~LJ_TTAB;
    t->nomm = cast_byte(~0);
    t->colo = (int8_t)asize;
    setmref(t->array, (TValue *)((char *)t + sizeof(GCtab)));
    setgcrefnull(t->metatable);
    t->asize = asize;
    t->hmask = 0;
    setmref(t->node, &G(L)->nilnode);
  } else {  /* Otherwise separately allocate the array part. */
    t = lj_mem_newobj(L, GCtab);
    t->gct = ~LJ_TTAB;
    t->nomm = cast_byte(~0);
    t->colo = 0;
    setmref(t->array, NULL);
    setgcrefnull(t->metatable);
    t->asize = 0;  /* In case the array allocation fails. */
    t->hmask = 0;
    setmref(t->node, &G(L)->nilnode);
    if (asize > 0) {
      if (asize > LJ_MAX_ASIZE)
	lj_err_msg(L, LJ_ERR_TABOV);
      setmref(t->array, lj_mem_newvec(L, asize, TValue));
      t->asize = asize;
    }
  }
  if (hbits)
    newhpart(L, t, hbits);
  return t;
}
Пример #9
0
/*
** this function can be called asynchronous (e.g. during a signal)
*/
int lua_sethook (LuaThread *L, LuaHook func, int mask, int count) {
  THREAD_CHECK(L);
  if (func == NULL || mask == 0) {  /* turn off hooks? */
    mask = 0;
    func = NULL;
  }
  if (L->stack_.callinfo_->isLua()) {
    L->oldpc = L->stack_.callinfo_->getCurrentPC();
  }
  L->hook = func;
  L->basehookcount = count;
  L->hookcount = L->basehookcount;
  L->hookmask = cast_byte(mask);
  return 1;
}
Пример #10
0
void luaD_throw (lua_State *L, int errcode) {
  unfixedstack(L); /* make sure the fixedstack & block_gc flags get reset. */
  unset_block_gc(L);
  if (L->errorJmp) {
    L->errorJmp->status = errcode;
    LUAI_THROW(L, L->errorJmp);
  }
  else {
    L->status = cast_byte(errcode);
    if (G(L)->panic) {
      resetstack(L, errcode);
      lua_unlock(L);
      G(L)->panic(L);
    }
    exit(EXIT_FAILURE);
  }
}
Пример #11
0
void luaD_throw (lua_State *L, int errcode) {
  if (L->errorJmp) {
    L->errorJmp->status = errcode;
    // LUAI_THROW is sometimes used to ignore the error and restore LUA state
    LUAI_THROW(L, L->errorJmp);
    error("LUA error occurred, error code is %d (%s)", errcode, luaErrorDescription[errcode]);
  }
  else {
    L->status = cast_byte(errcode);
    if (G(L)->panic) {
      resetstack(L, errcode);
      lua_unlock(L);
      G(L)->panic(L);
    }
    error("luaD_throw failure");
  }
}
Пример #12
0
static l_mem atomic (lua_State *L) {
    global_State *g = G(L);
    l_mem work;
    GCObject *origweak, *origall;
    GCObject *grayagain = g->grayagain;  /* save original list */
    lua_assert(g->ephemeron == NULL && g->weak == NULL);
    lua_assert(!iswhite(g->mainthread));
    g->gcstate = GCSinsideatomic;
    g->GCmemtrav = 0;  /* start counting work */
    markobject(g, L);  /* mark running thread */
    /* registry and global metatables may be changed by API */
    markvalue(g, &g->l_registry);
    markmt(g);  /* mark global metatables */
    /* remark occasional upvalues of (maybe) dead threads */
    remarkupvals(g);
    propagateall(g);  /* propagate changes */
    work = g->GCmemtrav;  /* stop counting (do not recount 'grayagain') */
    g->gray = grayagain;
    propagateall(g);  /* traverse 'grayagain' list */
    g->GCmemtrav = 0;  /* restart counting */
    convergeephemerons(g);
    /* at this point, all strongly accessible objects are marked. */
    /* Clear values from weak tables, before checking finalizers */
    clearvalues(g, g->weak, NULL);
    clearvalues(g, g->allweak, NULL);
    origweak = g->weak;
    origall = g->allweak;
    work += g->GCmemtrav;  /* stop counting (objects being finalized) */
    separatetobefnz(g, 0);  /* separate objects to be finalized */
    g->gcfinnum = 1;  /* there may be objects to be finalized */
    markbeingfnz(g);  /* mark objects that will be finalized */
    propagateall(g);  /* remark, to propagate 'resurrection' */
    g->GCmemtrav = 0;  /* restart counting */
    convergeephemerons(g);
    /* at this point, all resurrected objects are marked. */
    /* remove dead objects from weak tables */
    clearkeys(g, g->ephemeron, NULL);  /* clear keys from all ephemeron tables */
    clearkeys(g, g->allweak, NULL);  /* clear keys from all 'allweak' tables */
    /* clear values from resurrected weak tables */
    clearvalues(g, g->weak, origweak);
    clearvalues(g, g->allweak, origall);
    luaS_clearcache(g);
    g->currentwhite = cast_byte(otherwhite(g));  /* flip current white */
    work += g->GCmemtrav;  /* complete counting */
    return work;  /* estimate of memory marked by 'atomic' */
}
Пример #13
0
void luaD_throw (lua_State *L, int errcode) {
  if (L->errorJmp) {
    L->errorJmp->status = errcode;
    LUAI_THROW(L, L->errorJmp);
  }
  else {
    L->status = cast_byte(errcode);
    if (G(L)->panic) {
      resetstack(L, errcode);
      lua_unlock(L);
      G(L)->panic(L);
    }
    printf("lua throw calling ");
    while(1);
    // todo ezt itt ki kell váltani vmivel
    //exit(EXIT_FAILURE);
  }
}
Пример #14
0
void luaD_throw (lua_State *L, int errcode) {
  if (L->errorJmp) {
    L->errorJmp->status = errcode;
    // LUAI_THROW has been replaced with an error message in ScummVM, together
    // with the LUA error code and description
    //LUAI_THROW(L, L->errorJmp);
    error("LUA error occured, error code is %d (%s)", errcode, luaErrorDescription[errcode]);
  }
  else {
    L->status = cast_byte(errcode);
    if (G(L)->panic) {
      resetstack(L, errcode);
      lua_unlock(L);
      G(L)->panic(L);
    }
    error("luaD_throw failure");
  }
}
Пример #15
0
void luaD_throw (lua_State *L, int errcode) {
  if (L->errorJmp) {
	lprintfln("luaD_throw errcode: %i\n", errcode);
	//maPanic(0, "trhow");
    L->errorJmp->status = errcode;
    LUAI_THROW(L, L->errorJmp);
  }
  else {
	lprintfln("luaD_throw exit errcode: %i\n", errcode);
    L->status = cast_byte(errcode);
    if (G(L)->panic) {
      resetstack(L, errcode);
      lua_unlock(L);
      G(L)->panic(L);
    }
    exit(EXIT_FAILURE);
  }
}
Пример #16
0
void luaX_init (terra_State *L) {
  //initialize the base tstring_table that will hold reserved keywords
  int stk = lua_gettop(L->L);
  lua_newtable(L->L);
  lua_pushlightuserdata(L->L, &L->tstring_table);
  lua_pushvalue(L->L, -2);
  lua_rawset(L->L, LUA_REGISTRYINDEX);
  
  //stack is: <tstring_table>
  
  int i;
  for (i=0; i<NUM_RESERVED; i++) {
    TString *ts = luaS_new(L, luaX_tokens[i]);
    ts->reserved = cast_byte(i+1);  /* reserved word */
  }
  
  lua_pop(L->L,1); //<tstring_table>
  assert(stk == lua_gettop(L->L));
}
Пример #17
0
static int traversetable(global_State *g, Table *h) {
    int i;
    int weakkey = 0;
    int weakvalue = 0;
    const TValue *mode;
    if (h->metatable)
        markobject(g, h->metatable);
    mode = gfasttm(g, h->metatable, TM_MODE);
    if (mode && ttisstring(mode)) { /* is there a weak mode? */
        weakkey = (strchr(svalue(mode), 'k') != NULL);
        weakvalue = (strchr(svalue(mode), 'v') != NULL);
        if (weakkey || weakvalue) { /* is really weak? */
            h->marked &= ~(KEYWEAK | VALUEWEAK); /* clear bits */
            h->marked |= cast_byte((weakkey << KEYWEAKBIT) |
                                   (weakvalue << VALUEWEAKBIT));
            h->gclist = g->weak; /* must be cleared after GC, ... */
            g->weak = obj2gco(h); /* ... so put in the appropriate list */
        }
    }
    if (weakkey && weakvalue)
        return 1;
    if (!weakvalue) {
        i = h->sizearray;
        while (i--)
            markvalue(g, &h->array[i]);
    }
    i = sizenode(h);
    while (i--) {
        Node *n = gnode(h, i);
        lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n)));
        if (ttisnil(gval(n)))
            removeentry(n); /* remove empty entries */
        else {
            lua_assert(!ttisnil(gkey(n)));
            if (!weakkey)
                markvalue(g, gkey(n));
            if (!weakvalue)
                markvalue(g, gval(n));
        }
    }
    return weakkey || weakvalue;
}
Пример #18
0
l_noret luaD_throw (lua_State *L, int errcode) {
  if (L->errorJmp) {  /* thread has an error handler? */
    L->errorJmp->status = errcode;  /* set status */
    LUAI_THROW(L, L->errorJmp);  /* jump to it */
  }
  else {  /* thread has no error handler */
    L->status = cast_byte(errcode);  /* mark it as dead */
    if (G(L)->mainthread->errorJmp) {  /* main thread has a handler? */
      setobjs2s(L, G(L)->mainthread->top++, L->top - 1);  /* copy error obj. */
      luaD_throw(G(L)->mainthread, errcode);  /* re-throw in main thread */
    }
    else {  /* no handler at all; abort */
      if (G(L)->panic) {  /* panic function? */
        lua_unlock(L);
        G(L)->panic(L);  /* call it (last chance to jump out) */
      }
      abort();
    }
  }
}
Пример #19
0
static void parlist (LexState *ls)
{
    /* parlist -> [ param { `,' param } ] */
    FuncState *fs = GetCurrentFuncState( ls );
    Proto *f = fs->f;
    int nparams = 0;

    f->is_vararg = 0;

    if (ls->t.token != ')')
    {  /* is `parlist' not empty? */
        do
        {
            switch (ls->t.token)
            {
            case TK_NAME:
            {  /* param -> NAME */
                new_localvar(ls, str_checkname(ls), nparams++);
                break;
            }
            case TK_DOTS:
            {  /* param -> `...' */
                luaX_next(ls);
                f->is_vararg |= VARARG_ISVARARG;
                break;
            }
            default:
            {
                luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected");
                break;
            }
            }
        }
        while (!f->is_vararg && testnext(ls, ','));
    }
    adjustlocalvars(ls, nparams);

    f->numparams = cast_byte( fs->nactvar - (f->is_vararg & VARARG_HASARG) );

    luaK_reserveregs(fs, fs->nactvar);  /* reserve register for parameters */
}
Пример #20
0
static void parlist(LexState* ls)
{
	/* parlist -> [ param { `,' param } ] */
	FuncState* fs = ls->fs;
	Proto* f = fs->f;
	int nparams = 0;
	f->is_vararg = 0;
	if (ls->t.token != ')')	   /* is `parlist' not empty? */
	{
		do
		{
			switch (ls->t.token)
			{
				case TK_NAME:	 /* param -> NAME */
				{
					new_localvar(ls, str_checkname(ls), nparams++);
					break;
				}
				case TK_DOTS:	 /* param -> `...' */
				{
					luaX_next(ls);
#if defined(LUA_COMPAT_VARARG)
					/* use `arg' as default name */
					new_localvarliteral(ls, "arg", nparams++);
					f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG;
#endif
					f->is_vararg |= VARARG_ISVARARG;
					break;
				}
				default:
					luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected");
			}
		}
		while (!f->is_vararg && testnext(ls, ','));
	}
	adjustlocalvars(ls, nparams);
	f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG));
	luaK_reserveregs(fs, fs->nactvar);  /* reserve register for parameters */
}
Пример #21
0
static l_mem atomic (lua_State *L) {
	global_State *g = G(L);
	l_mem work = -cast(l_mem, g->GCmemtrav);  /* start counting work */
	GCObject *origweak, *origall;
	lua_assert(!iswhite(obj2gco(g->mainthread)));
	markobject(g, L);  /* mark running thread */
	/* registry and global metatables may be changed by API */
	markvalue(g, &g->l_registry);
	markmt(g);  /* mark basic metatables */
	/* remark occasional upvalues of (maybe) dead threads */
	remarkupvals(g);
	propagateall(g);  /* propagate changes */
	work += g->GCmemtrav;  /* stop counting (do not (re)count grays) */
	/* traverse objects caught by write barrier and by 'remarkupvals' */
	retraversegrays(g);
	work -= g->GCmemtrav;  /* restart counting */
	convergeephemerons(g);
	/* at this point, all strongly accessible objects are marked. */
	/* clear values from weak tables, before checking finalizers */
	clearvalues(g, g->weak, NULL);
	clearvalues(g, g->allweak, NULL);
	origweak = g->weak; origall = g->allweak;
	work += g->GCmemtrav;  /* stop counting (objects being finalized) */
	separatetobefnz(L, 0);  /* separate objects to be finalized */
	markbeingfnz(g);  /* mark objects that will be finalized */
	propagateall(g);  /* remark, to propagate `preserveness' */
	work -= g->GCmemtrav;  /* restart counting */
	convergeephemerons(g);
	/* at this point, all resurrected objects are marked. */
	/* remove dead objects from weak tables */
	clearkeys(g, g->ephemeron, NULL);  /* clear keys from all ephemeron tables */
	clearkeys(g, g->allweak, NULL);  /* clear keys from all allweak tables */
	/* clear values from resurrected weak tables */
	clearvalues(g, g->weak, origweak);
	clearvalues(g, g->allweak, origall);
	g->currentwhite = cast_byte(otherwhite(g));  /* flip current white */
	work += g->GCmemtrav;  /* complete counting */
	return work;  /* estimate of memory marked by 'atomic' */
}
Пример #22
0
int TypeCompiler::parList(FunctionLiteral *literal, bool method)
{
    utArray<VariableDeclaration *> *parms = literal->parameters;

    if (!parms && !method)
    {
        return 0;
    }

    FuncState *fs = cs->fs;
    Proto     *f  = fs->f;

    int nparams = 0;
    f->is_vararg = 0;

    if (method)
    {
        BC::newLocalVar(cs, "this", nparams++);
    }

    if (parms)
    {
        for (unsigned int i = 0; i < parms->size(); i++)
        {
            Identifier *ident = parms->at(i)->identifier;
            BC::newLocalVar(cs, ident->string.c_str(), nparams++);
        }
    }

    if (nparams)
    {
        BC::adjustLocalVars(cs, nparams);
        f->numparams = cast_byte(fs->nactvar);
        BC::reserveRegs(fs, fs->nactvar); /* reserve register for parameters */
    }

    return nparams;
}
Пример #23
0
Файл: lgc.c Проект: zapline/zlib
static void atomic (lua_State *L) {
  global_State *g = G(L);
  size_t udsize;  /* total size of userdata to be finalized */
  /* remark occasional upvalues of (maybe) dead threads */
  remarkupvals(g);
  /* traverse objects cautch by write barrier and by 'remarkupvals' */
  propagateall(g);
  /* remark weak tables */
  g->gray = g->weak;
  g->weak = NULL;
  lua_assert(!iswhite(obj2gco(g->mainthread)));
  markobject(g, L);  /* mark running thread */
  markmt(g);  /* mark basic metatables (again) */
#if LUAPLUS_EXTENSIONS
  if (G(L)->userGCFunction)
    G(L)->userGCFunction(L);
#endif /* LUAPLUS_EXTENSIONS */
  propagateall(g);
  /* remark gray again */
  g->gray = g->grayagain;
  g->grayagain = NULL;
  propagateall(g);
  udsize = luaC_separateudata(L, 0);  /* separate userdata to be finalized */
  marktmu(g);  /* mark `preserved' userdata */
  udsize += propagateall(g);  /* remark, to propagate `preserveness' */
#if LUA_REFCOUNT
  cleartable(L, g->weak);  /* remove collected objects from weak tables */
#else
  cleartable(g->weak);  /* remove collected objects from weak tables */
#endif /* LUA_REFCOUNT */
  /* flip current white */
  g->currentwhite = cast_byte(otherwhite(g));
  g->sweepstrgc = 0;
  g->sweepgc = &g->rootgc;
  g->gcstate = GCSsweepstring;
  g->estimate = g->totalbytes - udsize;  /* first estimate */
}
Пример #24
0
Файл: ldo.c Проект: nunb/ravi
l_noret luaD_throw (lua_State *L, int errcode) {
  if (L->errorJmp) {  /* thread has an error handler? */
    L->errorJmp->status = errcode;  /* set status */
    LUAI_THROW(L, L->errorJmp);  /* jump to it */
  }
  else {  /* thread has no error handler */
    global_State *g = G(L);
    L->status = cast_byte(errcode);  /* mark it as dead */
    if (g->mainthread->errorJmp) {  /* main thread has a handler? */
      setobjs2s(L, g->mainthread->top++, L->top - 1);  /* copy error obj. */
      luaD_throw(g->mainthread, errcode);  /* re-throw in main thread */
    }
    else {  /* no handler at all; abort */
      if (g->panic) {  /* panic function? */
        seterrorobj(L, errcode, L->top);  /* assume EXTRA_STACK */
        if (L->ci->top < L->top)
          L->ci->top = L->top;  /* pushing msg. can break this invariant */
        lua_unlock(L);
        g->panic(L);  /* call panic function (last chance to jump out) */
      }
      abort();
    }
  }
}
Пример #25
0
CClosure *luaF_newCclosure (lua_State *L, int n) {
  GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n));
  CClosure *c = gco2ccl(o);
  c->nupvalues = cast_byte(n);
  return c;
}
Пример #26
0
Closure *luaF_newCclosure (lua_State *L, int n) {
  Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n), NULL, 0)->cl;
  c->c.nupvalues = cast_byte(n);
  return c;
}
Пример #27
0
#include "lfunc.h"
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
#include "ldo.h"



Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) {
  Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
  luaC_link(L, obj2gco(c), LUA_TFUNCTION);
  c->c.precall = luaD_precall_c;
  c->c.isC = 1;
  c->c.env = e;
  c->c.nupvalues = cast_byte(nelems);
  return c;
}


Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) {
  Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
  luaC_link(L, obj2gco(c), LUA_TFUNCTION);
  c->l.precall = JIT_PRECALL;
  c->l.isC = 0;
  c->l.env = e;
  c->l.nupvalues = cast_byte(nelems);
  while (nelems--) c->l.upvals[nelems] = NULL;
  return c;
}
Пример #28
0
static void constructor (LexState *ls, expdesc *t) {
  /* constructor -> ?? */
  FuncState *fs = ls->fs;
  int line = ls->linenumber;
  int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
  struct ConsControl cc;
  cc.na = cc.nh = cc.tostore = 0;
  cc.t = t;
  init_exp(t, VRELOCABLE, pc);
  init_exp(&cc.v, VVOID, 0);  /* no value (yet) */
  luaK_exp2nextreg(ls->fs, t);  /* fix it at stack top (for gc) */
  checknext(ls, '{');
#if LUA_OPTIONAL_COMMA
  for (;;) {
#else
  do {
#endif /* LUA_OPTIONAL_COMMA */
    lua_assert(cc.v.k == VVOID || cc.tostore > 0);
    if (ls->t.token == '}') break;
    closelistfield(fs, &cc);
    switch(ls->t.token) {
      case TK_NAME: {  /* may be listfields or recfields */
        luaX_lookahead(ls);
        if (ls->lookahead.token != '=')  /* expression? */
          listfield(ls, &cc);
        else
          recfield(ls, &cc);
        break;
      }
      case '[': {  /* constructor_item -> recfield */
        recfield(ls, &cc);
        break;
      }
      default: {  /* constructor_part -> listfield */
        listfield(ls, &cc);
        break;
      }
    }
#if LUA_OPTIONAL_COMMA
	if (ls->t.token == ',' || ls->t.token == ';')
		next(ls);
	else if (ls->t.token == '}')
		break;
  }
#else
  } while (testnext(ls, ',') || testnext(ls, ';'));
#endif /* LUA_OPTIONAL_COMMA */
  check_match(ls, '}', '{', line);
  lastlistfield(fs, &cc);
  SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
  SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh));  /* set initial table size */
}

/* }====================================================================== */



static void parlist (LexState *ls) {
  /* parlist -> [ param { `,' param } ] */
  FuncState *fs = ls->fs;
  Proto *f = fs->f;
  int nparams = 0;
  f->is_vararg = 0;
  if (ls->t.token != ')') {  /* is `parlist' not empty? */
    do {
      switch (ls->t.token) {
        case TK_NAME: {  /* param -> NAME */
          new_localvar(ls, str_checkname(ls), nparams++);
          break;
        }
        case TK_DOTS: {  /* param -> `...' */
          luaX_next(ls);
#if defined(LUA_COMPAT_VARARG)
          /* use `arg' as default name */
          new_localvarliteral(ls, "arg", nparams++);
          f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG;
#endif
          f->is_vararg |= VARARG_ISVARARG;
          break;
        }
        default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected");
      }
    } while (!f->is_vararg && testnext(ls, ','));
  }
  adjustlocalvars(ls, nparams);
  f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG));
  luaK_reserveregs(fs, fs->nactvar);  /* reserve register for parameters */
}