source: trunk/package/lua/patches/600-refcounting.patch @ 20470

Last change on this file since 20470 was 20470, checked in by juhosg, 6 years ago

package/lua: refresh patches

File size: 32.7 KB
  • src/lapi.c

    a b  
    2727#include "ltable.h" 
    2828#include "ltm.h" 
    2929#include "lundump.h" 
    30 #include "lvm.h" 
    3130#include "lnum.h" 
     31#include "lvm.h" 
    3232 
    3333 
    3434const char lua_ident[] = 
    LUA_API void lua_xmove (lua_State *from, 
    117117  from->top -= n; 
    118118  for (i = 0; i < n; i++) { 
    119119    setobj2s(to, to->top++, from->top + i); 
     120    setnilvalue(from, from->top + i); 
    120121  } 
    121122  lua_unlock(to); 
    122123} 
    LUA_API void lua_settop (lua_State *L, i 
    166167  if (idx >= 0) { 
    167168    api_check(L, idx <= L->stack_last - L->base); 
    168169    while (L->top < L->base + idx) 
    169       setnilvalue(L->top++); 
     170      setnilvalue(L, L->top++); 
    170171    L->top = L->base + idx; 
     172    setnilvalue(L, L->top); 
    171173  } 
    172174  else { 
     175    int i; 
    173176    api_check(L, -(idx+1) <= (L->top - L->base)); 
    174     L->top += idx+1; /* `subtract' index (index is negative) */ 
     177        setlvmtop(L, L->top + idx + 1); /* `subtract' index (index is negative) */ 
    175178  } 
    176179  lua_unlock(L); 
    177180} 
    LUA_API void lua_remove (lua_State *L, i 
    183186  p = index2adr(L, idx); 
    184187  api_checkvalidindex(L, p); 
    185188  while (++p < L->top) setobjs2s(L, p-1, p); 
    186   L->top--; 
     189  setlvmtop(L, L->top - 1); 
    187190  lua_unlock(L); 
    188191} 
    189192 
    LUA_API void lua_insert (lua_State *L, i 
    196199  api_checkvalidindex(L, p); 
    197200  for (q = L->top; q>p; q--) setobjs2s(L, q, q-1); 
    198201  setobjs2s(L, p, L->top); 
     202  setnilvalue(L, L->top); 
    199203  lua_unlock(L); 
    200204} 
    201205 
    LUA_API void lua_replace (lua_State *L,  
    220224    if (idx < LUA_GLOBALSINDEX)  /* function upvalue? */ 
    221225      luaC_barrier(L, curr_func(L), L->top - 1); 
    222226  } 
    223   L->top--; 
     227  setlvmtop(L, L->top - 1); 
    224228  lua_unlock(L); 
    225229} 
    226230 
    LUA_API int lua_iscfunction (lua_State * 
    259263 
    260264 
    261265LUA_API int lua_isnumber (lua_State *L, int idx) { 
    262   TValue n; 
     266  TValue n = tvinit(); 
    263267  const TValue *o = index2adr(L, idx); 
    264268  return tonumber(o, &n); 
    265269} 
    266270 
    267271 
    268272LUA_API int lua_isinteger (lua_State *L, int idx) { 
    269   TValue tmp; 
     273  TValue tmp = tvinit(); 
    270274  lua_Integer dum; 
    271275  const TValue *o = index2adr(L, idx); 
    272276  return tonumber(o,&tmp) && (ttisint(o) || tt_integer_valued(o,&dum)); 
    LUA_API int lua_lessthan (lua_State *L,  
    319323 
    320324 
    321325LUA_API lua_Number lua_tonumber (lua_State *L, int idx) { 
    322   TValue n; 
     326  TValue n = tvinit(); 
    323327  const TValue *o = index2adr(L, idx); 
    324328  if (tonumber(o, &n)) { 
    325329#ifdef LNUM_COMPLEX 
    LUA_API lua_Number lua_tonumber (lua_Sta 
    333337 
    334338 
    335339LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) { 
    336   TValue n; 
     340  TValue n = tvinit(); 
    337341    /* Lua 5.1 documented behaviour is to return nonzero for non-integer: 
    338342     * "If the number is not an integer, it is truncated in some non-specified way."  
    339343     * I would suggest to change this, to return 0 for anything that would 
    LUA_API lua_Integer lua_tointeger (lua_S 
    369373 
    370374#ifdef LNUM_COMPLEX 
    371375LUA_API lua_Complex lua_tocomplex (lua_State *L, int idx) { 
    372   TValue tmp; 
     376  TValue tmp = tvinit(); 
    373377  const TValue *o = index2adr(L, idx); 
    374378  if (tonumber(o, &tmp)) 
    375379    return nvalue_complex(o); 
    LUA_API const void *lua_topointer (lua_S 
    465469 
    466470LUA_API void lua_pushnil (lua_State *L) { 
    467471  lua_lock(L); 
    468   setnilvalue(L->top); 
     472  setnilvalue(L, L->top); 
    469473  api_incr_top(L); 
    470474  lua_unlock(L); 
    471475} 
    LUA_API void lua_pushcclosure (lua_State 
    548552  cl = luaF_newCclosure(L, n, getcurrenv(L)); 
    549553  cl->c.f = fn; 
    550554  L->top -= n; 
    551   while (n--) 
     555  while (n--) { 
    552556    setobj2n(L, &cl->c.upvalue[n], L->top+n); 
     557    setnilvalue(L, L->top + n); 
     558  } 
    553559  setclvalue(L, L->top, cl); 
    554560  lua_assert(iswhite(obj2gco(cl))); 
    555561  api_incr_top(L); 
    LUA_API void lua_gettable (lua_State *L, 
    600606 
    601607LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { 
    602608  StkId t; 
    603   TValue key; 
     609  TValue key = tvinit(); 
    604610  lua_lock(L); 
    605611  t = index2adr(L, idx); 
    606612  api_checkvalidindex(L, t); 
    LUA_API void lua_getfenv (lua_State *L,  
    689695      setobj2s(L, L->top,  gt(thvalue(o))); 
    690696      break; 
    691697    default: 
    692       setnilvalue(L->top); 
     698      setnilvalue(L, L->top); 
    693699      break; 
    694700  } 
    695701  api_incr_top(L); 
    LUA_API void lua_settable (lua_State *L, 
    709715  t = index2adr(L, idx); 
    710716  api_checkvalidindex(L, t); 
    711717  luaV_settable(L, t, L->top - 2, L->top - 1); 
    712   L->top -= 2;  /* pop index and value */ 
     718  setlvmtop(L, L->top - 2);  /* pop index and value */ 
    713719  lua_unlock(L); 
    714720} 
    715721 
    716722 
    717723LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { 
    718724  StkId t; 
    719   TValue key; 
     725  TValue key = tvinit(); 
    720726  lua_lock(L); 
    721727  api_checknelems(L, 1); 
    722728  t = index2adr(L, idx); 
    723729  api_checkvalidindex(L, t); 
    724730  setsvalue(L, &key, luaS_new(L, k)); 
    725731  luaV_settable(L, t, &key, L->top - 1); 
    726   L->top--;  /* pop value */ 
     732  setlvmtop(L, L->top - 1);  /* pop value */ 
    727733  lua_unlock(L); 
    728734} 
    729735 
    LUA_API void lua_rawset (lua_State *L, i 
    736742  api_check(L, ttistable(t)); 
    737743  setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1); 
    738744  luaC_barriert(L, hvalue(t), L->top-1); 
    739   L->top -= 2; 
     745  setlvmtop(L, L->top - 2); 
    740746  lua_unlock(L); 
    741747} 
    742748 
    LUA_API void lua_rawseti (lua_State *L,  
    749755  api_check(L, ttistable(o)); 
    750756  setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1); 
    751757  luaC_barriert(L, hvalue(o), L->top-1); 
    752   L->top--; 
     758  setlvmtop(L, L->top - 1); 
    753759  lua_unlock(L); 
    754760} 
    755761 
    LUA_API int lua_setmetatable (lua_State  
    785791      break; 
    786792    } 
    787793  } 
    788   L->top--; 
     794  setlvmtop(L, L->top - 1); 
    789795  lua_unlock(L); 
    790796  return 1; 
    791797} 
    LUA_API int lua_setfenv (lua_State *L, i 
    814820      break; 
    815821  } 
    816822  if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); 
    817   L->top--; 
     823  setlvmtop(L, L->top - 1); 
    818824  lua_unlock(L); 
    819825  return res; 
    820826} 
    LUA_API int lua_next (lua_State *L, int  
    10401046  if (more) { 
    10411047    api_incr_top(L); 
    10421048  } 
    1043   else  /* no more elements */ 
    1044     L->top -= 1;  /* remove key */ 
     1049  else {  /* no more elements */ 
     1050    setlvmtop(L, L->top - 1);  /* remove key */ 
     1051  } 
    10451052  lua_unlock(L); 
    10461053  return more; 
    10471054} 
    LUA_API void lua_concat (lua_State *L, i 
    10531060  if (n >= 2) { 
    10541061    luaC_checkGC(L); 
    10551062    luaV_concat(L, n, cast_int(L->top - L->base) - 1); 
    1056     L->top -= (n-1); 
     1063    setlvmtop(L, L->top - (n-1)); 
    10571064  } 
    10581065  else if (n == 0) {  /* push empty string */ 
    10591066    setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); 
    LUA_API const char *lua_setupvalue (lua_ 
    11391146  if (name) { 
    11401147    L->top--; 
    11411148    setobj(L, val, L->top); 
     1149    setnilvalue(L, L->top); 
    11421150    luaC_barrier(L, clvalue(fi), L->top); 
    11431151  } 
    11441152  lua_unlock(L); 
    LUA_API const char *lua_setupvalue (lua_ 
    11601168int lua_pushvalue_as_number (lua_State *L, int idx) 
    11611169{ 
    11621170  const TValue *o = index2adr(L, idx); 
    1163   TValue tmp; 
     1171  TValue tmp = tvinit(); 
    11641172  lua_Integer i; 
    11651173  if (ttisnumber(o)) { 
    11661174    if ( (!ttisint(o)) && tt_integer_valued(o,&i)) { 
  • src/lcode.c

    a b  
    2323#include "lparser.h" 
    2424#include "ltable.h" 
    2525#include "lnum.h" 
     26#include "lvm.h" 
    2627 
    2728 
    2829#define hasjumps(e)     ((e)->t != (e)->f) 
    static int addk (FuncState *fs, TValue * 
    248249    setivalue(idx, fs->nk); 
    249250    luaM_growvector(L, f->k, fs->nk, f->sizek, TValue, 
    250251                    MAXARG_Bx, "constant table overflow"); 
    251     while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); 
     252    while (oldsize < f->sizek) setnilvalue(L, &f->k[oldsize++]); 
    252253    setobj(L, &f->k[fs->nk], v); 
    253254    luaC_barrier(L, f, v); 
    254255    return fs->nk++; 
    static int addk (FuncState *fs, TValue * 
    257258 
    258259 
    259260int luaK_stringK (FuncState *fs, TString *s) { 
    260   TValue o; 
     261  TValue o = tvinit(); 
    261262  setsvalue(fs->L, &o, s); 
     263  luaV_unref(fs->L, &o); 
    262264  return addk(fs, &o, &o); 
    263265} 
    264266 
    265267 
    266268int luaK_numberK (FuncState *fs, lua_Number r) { 
    267   TValue o; 
     269  lua_State *L = fs->L; 
     270  TValue o = tvinit(); 
    268271  setnvalue(&o, r); 
    269272  return addk(fs, &o, &o); 
    270273} 
    271274 
    272275 
    273276int luaK_integerK (FuncState *fs, lua_Integer r) { 
    274   TValue o; 
     277  lua_State *L = fs->L; 
     278  TValue o = tvinit(); 
    275279  setivalue(&o, r); 
    276280  return addk(fs, &o, &o); 
    277281} 
    int luaK_integerK (FuncState *fs, lua_In 
    279283 
    280284#ifdef LNUM_COMPLEX 
    281285static int luaK_imagK (FuncState *fs, lua_Number r) { 
    282   TValue o; 
     286  lua_State *L = fs->L; 
     287  TValue o = tvinit(); 
    283288  setnvalue_complex(&o, r*I); 
    284289  return addk(fs, &o, &o); 
    285290} 
    286291#endif 
    287292 
    288293static int boolK (FuncState *fs, int b) { 
    289   TValue o; 
     294  lua_State *L = fs->L; 
     295  TValue o = tvinit(); 
    290296  setbvalue(&o, b); 
    291297  return addk(fs, &o, &o); 
    292298} 
    293299 
    294300 
    295301static int nilK (FuncState *fs) { 
    296   TValue k, v; 
    297   setnilvalue(&v); 
     302  TValue k = tvinit(), v = tvinit(); 
     303  setnilvalue(fs->L, &v); 
    298304  /* cannot use nil as key; instead use table itself to represent nil */ 
    299305  sethvalue(fs->L, &k, fs->h); 
    300306  return addk(fs, &k, &v); 
  • src/ldebug.c

    a b LUA_API const char *lua_setlocal (lua_St 
    142142  if (name) 
    143143      setobjs2s(L, ci->base + (n - 1), L->top - 1); 
    144144  L->top--;  /* pop value */ 
     145  setnilvalue(L, L->top); 
    145146  lua_unlock(L); 
    146147  return name; 
    147148} 
    static void info_tailcall (lua_Debug *ar 
    176177 
    177178static void collectvalidlines (lua_State *L, Closure *f) { 
    178179  if (f == NULL || f->c.isC) { 
    179     setnilvalue(L->top); 
     180    setnilvalue(L, L->top); 
    180181  } 
    181182  else { 
    182183    Table *t = luaH_new(L, 0, 0); 
    LUA_API int lua_getinfo (lua_State *L, c 
    248249  } 
    249250  status = auxgetinfo(L, what, ar, f, ci); 
    250251  if (strchr(what, 'f')) { 
    251     if (f == NULL) setnilvalue(L->top); 
     252    if (f == NULL) setnilvalue(L, L->top); 
    252253    else setclvalue(L, L->top, f); 
    253254    incr_top(L); 
    254255  } 
    void luaG_concaterror (lua_State *L, Stk 
    586587 
    587588 
    588589void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { 
    589   TValue temp; 
     590  TValue temp = tvinit(); 
    590591  if (luaV_tonumber(p1, &temp) == NULL) 
    591592    p2 = p1;  /* first operand is wrong */ 
    592593  luaG_typeerror(L, p2, "perform arithmetic on"); 
  • src/ldo.c

    a b static StkId adjust_varargs (lua_State * 
    211211  Table *htab = NULL; 
    212212  StkId base, fixed; 
    213213  for (; actual < nfixargs; ++actual) 
    214     setnilvalue(L->top++); 
     214    setnilvalue(L, L->top++); 
    215215#if defined(LUA_COMPAT_VARARG) 
    216216  if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */ 
    217217    int nvar = actual - nfixargs;  /* number of extra arguments */ 
    static StkId adjust_varargs (lua_State * 
    229229  base = L->top;  /* final position of first argument */ 
    230230  for (i=0; i<nfixargs; i++) { 
    231231    setobjs2s(L, L->top++, fixed+i); 
    232     setnilvalue(fixed+i); 
     232    setnilvalue(L, fixed+i); 
    233233  } 
    234234  /* add `arg' parameter */ 
    235235  if (htab) { 
    int luaD_precall (lua_State *L, StkId fu 
    294294    ci->tailcalls = 0; 
    295295    ci->nresults = nresults; 
    296296    for (st = L->top; st < ci->top; st++) 
    297       setnilvalue(st); 
     297      setnilvalue(L, st); 
    298298    L->top = ci->top; 
    299299    if (L->hookmask & LUA_MASKCALL) { 
    300300      L->savedpc++;  /* hooks assume 'pc' is already incremented */ 
    int luaD_poscall (lua_State *L, StkId fi 
    354354  for (i = wanted; i != 0 && firstResult < L->top; i--) 
    355355    setobjs2s(L, res++, firstResult++); 
    356356  while (i-- > 0) 
    357     setnilvalue(res++); 
    358   L->top = res; 
     357    setnilvalue(L, res++); 
     358  setlvmtop(L, res); 
    359359  return (wanted - LUA_MULTRET);  /* 0 iff wanted == LUA_MULTRET */ 
    360360} 
    361361 
    int luaD_pcall (lua_State *L, Pfunc func 
    463463  status = luaD_rawrunprotected(L, func, u); 
    464464  if (status != 0) {  /* an error occurred? */ 
    465465    StkId oldtop = restorestack(L, old_top); 
     466        StkId curtop = L->top; 
     467    int i; 
    466468    luaF_close(L, oldtop);  /* close eventual pending closures */ 
    467469    luaD_seterrorobj(L, status, oldtop); 
     470    for (i = (curtop - L->top); i-- > 0;) 
     471      setnilvalue(L, L->top + i); 
    468472    L->nCcalls = oldnCcalls; 
    469473    L->ci = restoreci(L, old_ci); 
    470474    L->base = L->ci->base; 
  • src/lfunc.c

    a b  
    1717#include "lmem.h" 
    1818#include "lobject.h" 
    1919#include "lstate.h" 
    20  
     20#include "lvm.h" 
    2121 
    2222 
    2323Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) { 
    UpVal *luaF_newupval (lua_State *L) { 
    4545  UpVal *uv = luaM_new(L, UpVal); 
    4646  luaC_link(L, obj2gco(uv), LUA_TUPVAL); 
    4747  uv->v = &uv->u.value; 
    48   setnilvalue(uv->v); 
     48  setnilvalue(L, uv->v); 
    4949  return uv; 
    5050} 
    5151 
    UpVal *luaF_findupval (lua_State *L, Stk 
    6767  uv = luaM_new(L, UpVal);  /* not found: create a new one */ 
    6868  uv->tt = LUA_TUPVAL; 
    6969  uv->marked = luaC_white(g); 
    70   uv->v = level;  /* current value lives in the stack */ 
     70  uv->v = luaV_ref(level);  /* current value lives in the stack */ 
    7171  uv->next = *pp;  /* chain it in the proper position */ 
     72  if (uv->next) { 
     73        uv->prev = uv->next->gch.prev; 
     74    uv->next->gch.prev = (GCObject *)uv; 
     75  } else { 
     76    uv->prev = NULL; 
     77  } 
    7278  *pp = obj2gco(uv); 
    7379  uv->u.l.prev = &g->uvhead;  /* double link it in `uvhead' list */ 
    7480  uv->u.l.next = g->uvhead.u.l.next; 
  • src/lgc.c

    a b  
    2121#include "lstring.h" 
    2222#include "ltable.h" 
    2323#include "ltm.h" 
     24#include "lvm.h" 
    2425 
    2526 
    2627#define GCSTEPSIZE      1024u 
    static void traversestack (global_State  
    265266  for (o = l->stack; o < l->top; o++) 
    266267    markvalue(g, o); 
    267268  for (; o <= lim; o++) 
    268     setnilvalue(o); 
     269    setnilvalue(l, o); 
    269270  checkstacksizes(l, lim); 
    270271} 
    271272 
    static int iscleared (const TValue *o, i 
    348349/* 
    349350** clear collected entries from weaktables 
    350351*/ 
    351 static void cleartable (GCObject *l) { 
     352static void cleartable (lua_State *L, GCObject *l) { 
    352353  while (l) { 
    353354    Table *h = gco2h(l); 
    354355    int i = h->sizearray; 
    static void cleartable (GCObject *l) { 
    358359      while (i--) { 
    359360        TValue *o = &h->array[i]; 
    360361        if (iscleared(o, 0))  /* value was collected? */ 
    361           setnilvalue(o);  /* remove value */ 
     362          setnilvalue(L, o);  /* remove value */ 
    362363      } 
    363364    } 
    364365    i = sizenode(h); 
    static void cleartable (GCObject *l) { 
    366367      Node *n = gnode(h, i); 
    367368      if (!ttisnil(gval(n)) &&  /* non-empty entry? */ 
    368369          (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) { 
    369         setnilvalue(gval(n));  /* remove value ... */ 
     370        setnilvalue(L, gval(n));  /* remove value ... */ 
    370371        removeentry(n);  /* remove entry from table */ 
    371372      } 
    372373    } 
    static void cleartable (GCObject *l) { 
    375376} 
    376377 
    377378 
    378 static void freeobj (lua_State *L, GCObject *o) { 
     379void luaC_freeobj (lua_State *L, GCObject *o) { 
    379380  switch (o->gch.tt) { 
    380381    case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; 
    381382    case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break; 
    static GCObject **sweeplist (lua_State * 
    418419    } 
    419420    else {  /* must erase `curr' */ 
    420421      lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT)); 
     422      if (curr->gch.next) 
     423        curr->gch.next->gch.prev = curr->gch.prev; 
    421424      *p = curr->gch.next; 
    422425      if (curr == g->rootgc)  /* is the first element of the list? */ 
    423426        g->rootgc = curr->gch.next;  /* adjust first */ 
    424       freeobj(L, curr); 
     427      luaC_freeobj(L, curr); 
    425428    } 
    426429  } 
    427430  return p; 
    static void atomic (lua_State *L) { 
    543546  udsize = luaC_separateudata(L, 0);  /* separate userdata to be finalized */ 
    544547  marktmu(g);  /* mark `preserved' userdata */ 
    545548  udsize += propagateall(g);  /* remark, to propagate `preserveness' */ 
    546   cleartable(g->weak);  /* remove collected objects from weak tables */ 
     549  cleartable(L, g->weak);  /* remove collected objects from weak tables */ 
    547550  /* flip current white */ 
    548551  g->currentwhite = cast_byte(otherwhite(g)); 
    549552  g->sweepstrgc = 0; 
    void luaC_barrierback (lua_State *L, Tab 
    685688 
    686689void luaC_link (lua_State *L, GCObject *o, lu_byte tt) { 
    687690  global_State *g = G(L); 
     691  o->gch.prev = (GCObject*)&g->rootgc; 
    688692  o->gch.next = g->rootgc; 
    689693  g->rootgc = o; 
     694  if (o->gch.next) 
     695    o->gch.next->gch.prev = o; 
    690696  o->gch.marked = luaC_white(g); 
    691697  o->gch.tt = tt; 
    692698} 
  • src/lgc.h

    a b LUAI_FUNC void luaC_link (lua_State *L,  
    105105LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv); 
    106106LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); 
    107107LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t); 
    108  
     108LUAI_FUNC void luaC_freeobj (lua_State *L, GCObject *o); 
    109109 
    110110#endif 
  • src/lmem.c

    a b  
    66 
    77 
    88#include <stddef.h> 
     9#include <string.h> 
    910 
    1011#define lmem_c 
    1112#define LUA_CORE 
    void *luaM_realloc_ (lua_State *L, void  
    8081  if (block == NULL && nsize > 0) 
    8182    luaD_throw(L, LUA_ERRMEM); 
    8283  lua_assert((nsize == 0) == (block == NULL)); 
     84  if (nsize > osize) 
     85    memset((char *)block + osize, 0, nsize - osize); 
    8386  g->totalbytes = (g->totalbytes - osize) + nsize; 
    8487  return block; 
    8588} 
  • src/lobject.h

    a b typedef union GCObject GCObject; 
    4444** Common Header for all collectable objects (in macro form, to be 
    4545** included in other objects) 
    4646*/ 
    47 #define CommonHeader    GCObject *next; lu_byte tt; lu_byte marked 
     47#define CommonHeader    GCObject *next; GCObject *prev; lu_byte tt; lu_byte marked 
    4848 
    4949 
    5050/* 
    typedef struct lua_TValue { 
    8383  TValuefields; 
    8484} TValue; 
    8585 
     86#define tvinit() { .value.b = 0, .tt = 0 } 
    8687 
    8788/* Macros to test type */ 
    8889#define ttisnil(o)      (ttype(o) == LUA_TNIL) 
    typedef struct lua_TValue { 
    145146 
    146147 
    147148/* Macros to set values */ 
    148 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL) 
     149#define setnilvalue(L, obj) (luaV_unref(L, (obj))->tt=LUA_TNIL) 
    149150 
    150151/* Must not have side effects, 'x' may be expression. 
    151152*/ 
    152153#define setivalue(obj,x) \ 
    153     { TValue *i_o=(obj); i_o->value.i=(x); i_o->tt=LUA_TINT; } 
     154    { TValue *i_o=luaV_unref(L, (obj)); i_o->value.i=(x); i_o->tt=LUA_TINT; } 
    154155 
    155156# define setnvalue(obj,x) \ 
    156     { TValue *i_o=(obj); i_o->value.n= (x); i_o->tt=LUA_TNUMBER; } 
     157    { TValue *i_o=luaV_unref(L, (obj)); i_o->value.n= (x); i_o->tt=LUA_TNUMBER; } 
    157158 
    158159/* Note: Complex always has "inline", both are C99. 
    159160*/ 
    typedef struct lua_TValue { 
    170171 
    171172 
    172173#define setpvalue(obj,x) \ 
    173   { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; } 
     174  { TValue *i_o=luaV_unref(L, (obj)); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; } 
    174175 
    175176#define setbvalue(obj,x) \ 
    176   { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; } 
     177  { TValue *i_o=luaV_unref(L, (obj)); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; } 
    177178 
    178179#define setsvalue(L,obj,x) \ 
    179   { TValue *i_o=(obj); \ 
    180     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \ 
     180  { TValue *i_o=(obj); TString *val=(x); luaS_ref(val); luaV_unref(L, obj); \ 
     181    i_o->value.gc=cast(GCObject *, (val)); i_o->tt=LUA_TSTRING; \ 
    181182    checkliveness(G(L),i_o); } 
    182183 
    183184#define setuvalue(L,obj,x) \ 
    184   { TValue *i_o=(obj); \ 
     185  { TValue *i_o=luaV_unref(L, (obj)); \ 
    185186    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \ 
    186187    checkliveness(G(L),i_o); } 
    187188 
    188189#define setthvalue(L,obj,x) \ 
    189   { TValue *i_o=(obj); \ 
     190  { TValue *i_o=luaV_unref(L, (obj)); \ 
    190191    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \ 
    191192    checkliveness(G(L),i_o); } 
    192193 
    193194#define setclvalue(L,obj,x) \ 
    194   { TValue *i_o=(obj); \ 
     195  { TValue *i_o=luaV_unref(L, (obj)); \ 
    195196    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \ 
    196197    checkliveness(G(L),i_o); } 
    197198 
    198199#define sethvalue(L,obj,x) \ 
    199   { TValue *i_o=(obj); \ 
     200  { TValue *i_o=luaV_unref(L, (obj)); \ 
    200201    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \ 
    201202    checkliveness(G(L),i_o); } 
    202203 
    203204#define setptvalue(L,obj,x) \ 
    204   { TValue *i_o=(obj); \ 
     205  { TValue *i_o=luaV_unref(L, (obj)); \ 
    205206    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \ 
    206207    checkliveness(G(L),i_o); } 
    207208 
    208209#define setobj(L,obj1,obj2) \ 
    209   { const TValue *o2=(obj2); TValue *o1=(obj1); \ 
     210  do { const TValue *o2=luaV_ref((TValue *)(obj2)); TValue *o1=luaV_unref(L, (obj1)); \ 
    210211    o1->value = o2->value; o1->tt=o2->tt; \ 
    211     checkliveness(G(L),o1); } 
     212    checkliveness(G(L),o1); } while(0) 
    212213 
    213214 
    214215/* 
    typedef union TString { 
    253254    lu_byte reserved; 
    254255    unsigned int hash; 
    255256    size_t len; 
     257    int refcount; 
    256258  } tsv; 
    257259} TString; 
    258260 
    typedef struct Table { 
    409411#define twoto(x)        (1<<(x)) 
    410412#define sizenode(t)     (twoto((t)->lsizenode)) 
    411413 
     414#include "lstring.h" 
    412415 
    413416#define luaO_nilobject          (&luaO_nilobject_) 
    414417 
  • src/lparser.c

    a b  
    2424#include "lstate.h" 
    2525#include "lstring.h" 
    2626#include "ltable.h" 
     27#include "lvm.h" 
    2728 
    2829 
    2930 
    static int registerlocalvar (LexState *l 
    146147  luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, 
    147148                  LocVar, SHRT_MAX, "too many local variables"); 
    148149  while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; 
    149   f->locvars[fs->nlocvars].varname = varname; 
     150  f->locvars[fs->nlocvars].varname = luaS_ref(varname); 
    150151  luaC_objbarrier(ls->L, f, varname); 
    151152  return fs->nlocvars++; 
    152153} 
    static int indexupvalue (FuncState *fs,  
    194195  luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues, 
    195196                  TString *, MAX_INT, ""); 
    196197  while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL; 
    197   f->upvalues[f->nups] = name; 
     198  f->upvalues[f->nups] = luaS_ref(name); 
    198199  luaC_objbarrier(fs->L, f, name); 
    199200  lua_assert(v->k == VLOCAL || v->k == VUPVAL); 
    200201  fs->upvalues[f->nups].k = cast_byte(v->k); 
    static void open_func (LexState *ls, Fun 
    341342  fs->nlocvars = 0; 
    342343  fs->nactvar = 0; 
    343344  fs->bl = NULL; 
    344   f->source = ls->source; 
     345  f->source = luaS_ref(ls->source); 
    345346  f->maxstacksize = 2;  /* registers 0/1 are always valid */ 
    346347  fs->h = luaH_new(L, 0, 0); 
    347348  /* anchor table of constants and prototype (to avoid being collected) */ 
  • src/lstate.c

    a b  
    2222#include "lstring.h" 
    2323#include "ltable.h" 
    2424#include "ltm.h" 
     25#include "lvm.h" 
    2526 
    2627 
    2728#define state_size(x)   (sizeof(x) + LUAI_EXTRASPACE) 
    static void stack_init (lua_State *L1, l 
    5253  L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1; 
    5354  /* initialize first ci */ 
    5455  L1->ci->func = L1->top; 
    55   setnilvalue(L1->top++);  /* `function' entry for this `ci' */ 
     56  setnilvalue(L1, L1->top++);  /* `function' entry for this `ci' */ 
    5657  L1->base = L1->ci->base = L1->top; 
    5758  L1->ci->top = L1->top + LUA_MINSTACK; 
    5859} 
    static void preinit_state (lua_State *L, 
    9899  L->base_ci = L->ci = NULL; 
    99100  L->savedpc = NULL; 
    100101  L->errfunc = 0; 
    101   setnilvalue(gt(L)); 
     102  setnilvalue(L, gt(L)); 
    102103} 
    103104 
    104105 
    LUA_API lua_State *lua_newstate (lua_All 
    163164  g->strt.size = 0; 
    164165  g->strt.nuse = 0; 
    165166  g->strt.hash = NULL; 
    166   setnilvalue(registry(L)); 
     167  setnilvalue(L, registry(L)); 
    167168  luaZ_initbuffer(L, &g->buff); 
    168169  g->panic = NULL; 
    169170  g->gcstate = GCSpause; 
  • src/lstring.c

    a b void luaS_resize (lua_State *L, int news 
    3737      int h1 = lmod(h, newsize);  /* new position */ 
    3838      lua_assert(cast_int(h%newsize) == lmod(h, newsize)); 
    3939      p->gch.next = newhash[h1];  /* chain it */ 
     40      if (p->gch.next) 
     41        p->gch.next->gch.prev = p; 
     42      p->gch.prev = NULL; 
    4043      newhash[h1] = p; 
    4144      p = next; 
    4245    } 
    static TString *newlstr (lua_State *L, c 
    5962  ts->tsv.marked = luaC_white(G(L)); 
    6063  ts->tsv.tt = LUA_TSTRING; 
    6164  ts->tsv.reserved = 0; 
     65  ts->tsv.refcount = 0; 
    6266  memcpy(ts+1, str, l*sizeof(char)); 
    6367  ((char *)(ts+1))[l] = '\0';  /* ending 0 */ 
    6468  tb = &G(L)->strt; 
    6569  h = lmod(h, tb->size); 
    6670  ts->tsv.next = tb->hash[h];  /* chain new entry */ 
     71  if (ts->tsv.next) 
     72    ts->tsv.next->gch.prev = (GCObject *)ts; 
     73  ts->tsv.prev = NULL; 
    6774  tb->hash[h] = obj2gco(ts); 
    6875  tb->nuse++; 
    6976  if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) 
    Udata *luaS_newudata (lua_State *L, size 
    109116  return u; 
    110117} 
    111118 
     119void luaS_unref(lua_State *L, TString *ts) { 
     120  if (!L || !ts) 
     121    return; 
     122  if (testbit(ts->tsv.marked, FIXEDBIT)) 
     123    return; 
     124  ts->tsv.refcount--; 
     125  if (ts->tsv.refcount < 0) { 
     126    fprintf(stderr, "REFCOUNT BUG, COUNT=%d, str=%s, len=%d\n", ts->tsv.refcount, (char *) (ts + 1), (int) ts->tsv.len); 
     127  } else if (ts->tsv.refcount) 
     128    return; 
     129 
     130  if (ts->tsv.prev) { 
     131    ts->tsv.prev->gch.next = ts->tsv.next; 
     132  } else { 
     133    unsigned int idx = lmod(ts->tsv.hash, G(L)->strt.size); 
     134    lua_assert(G(L)->strt.hash[index] == (GCObject*)ts); 
     135    G(L)->strt.hash[idx] = ts->tsv.next; 
     136  } 
     137 
     138  if (ts->tsv.next) 
     139    ts->tsv.next->gch.prev = ts->tsv.prev; 
     140 
     141  luaC_freeobj(L, (GCObject *) ts); 
     142} 
     143 
     144 
  • src/lstring.h

    a b  
    77#ifndef lstring_h 
    88#define lstring_h 
    99 
    10  
     10#include <stdio.h> 
    1111#include "lgc.h" 
    1212#include "lobject.h" 
    1313#include "lstate.h" 
     
    2323 
    2424#define luaS_fix(s)     l_setbit((s)->tsv.marked, FIXEDBIT) 
    2525 
     26static inline TString *luaS_ref(TString *ts) { 
     27  ts->tsv.refcount++; 
     28  return ts; 
     29} 
     30 
     31LUA_API void luaS_unref(lua_State *L, TString *ts); 
    2632LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 
    2733LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 
    2834LUA_API TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 
  • src/ltable.c

    a b  
    3434#include "lstate.h" 
    3535#include "ltable.h" 
    3636#include "lnum.h" 
     37#include "lvm.h" 
    3738 
    3839 
    3940/* 
    static void setarrayvector (lua_State *L 
    278279  int i; 
    279280  luaM_reallocvector(L, t->array, t->sizearray, size, TValue); 
    280281  for (i=t->sizearray; i<size; i++) 
    281      setnilvalue(&t->array[i]); 
     282     setnilvalue(L, &t->array[i]); 
    282283  t->sizearray = size; 
    283284} 
    284285 
    static void setnodevector (lua_State *L, 
    299300    for (i=0; i<size; i++) { 
    300301      Node *n = gnode(t, i); 
    301302      gnext(n) = NULL; 
    302       setnilvalue(gkey(n)); 
    303       setnilvalue(gval(n)); 
     303      setnilvalue(L, gkey(n)); 
     304      setnilvalue(L, gval(n)); 
    304305    } 
    305306  } 
    306307  t->lsizenode = cast_byte(lsize); 
    static TValue *newkey (lua_State *L, Tab 
    427428        othern = gnext(othern);  /* find previous */ 
    428429      } 
    429430      gnext(othern) = n;  /* redo the chain with `n' in place of `mp' */ 
     431      luaV_ref((TValue *) gkey(mp)); 
     432      luaV_ref(gval(mp)); 
    430433      *n = *mp;  /* copy colliding node into free pos. (mp->next also goes) */ 
    431434      gnext(mp) = NULL;  /* now `mp' is free */ 
    432       setnilvalue(gval(mp)); 
     435      setnilvalue(L, gval(mp)); 
    433436    } 
    434437    else {  /* colliding node is in its own main position */ 
    435438      /* new node will go into free position */ 
    static TValue *newkey (lua_State *L, Tab 
    438441      mp = n; 
    439442    } 
    440443  } 
     444  luaV_ref((TValue *) key); 
    441445  gkey(mp)->value = key->value; gkey(mp)->tt = key->tt; 
    442446  luaC_barriert(L, t, key); 
    443447  lua_assert(ttisnil(gval(mp))); 
    TValue *luaH_setint (lua_State *L, Table 
    530534  if (p != luaO_nilobject) 
    531535    return cast(TValue *, p); 
    532536  else { 
    533     TValue k; 
     537    TValue k = tvinit(); 
    534538    setivalue(&k, key); 
    535539    return newkey(L, t, &k); 
    536540  } 
    TValue *luaH_setstr (lua_State *L, Table 
    542546  if (p != luaO_nilobject) 
    543547    return cast(TValue *, p); 
    544548  else { 
    545     TValue k; 
     549    TValue k = tvinit(); 
    546550    setsvalue(L, &k, key); 
    547551    return newkey(L, t, &k); 
    548552  } 
  • src/luac.c

    a b  
    2020#include "lmem.h" 
    2121#include "lobject.h" 
    2222#include "lopcodes.h" 
    23 #include "lstring.h" 
    2423#include "lundump.h" 
     24#include "lvm.h" 
     25#include "lstring.h" 
    2526 
    2627#define PROGNAME        "luac"          /* default program name */ 
    2728#define OUTPUT          PROGNAME ".out" /* default output file */ 
  • src/lundump.c

    a b  
    1919#include "lstring.h" 
    2020#include "lundump.h" 
    2121#include "lzio.h" 
     22#include "lvm.h" 
    2223 
    2324typedef struct { 
    2425 lua_State* L; 
    static TString* LoadString(LoadState* S) 
    133134 { 
    134135  char* s=luaZ_openspace(S->L,S->b,size); 
    135136  LoadBlock(S,s,size); 
    136   return luaS_newlstr(S->L,s,size-1);           /* remove trailing '\0' */ 
     137  return luaS_ref(luaS_newlstr(S->L,s,size-1));         /* remove trailing '\0' */ 
    137138 } 
    138139} 
    139140 
    static Proto* LoadFunction(LoadState* S, 
    149150 
    150151static void LoadConstants(LoadState* S, Proto* f) 
    151152{ 
     153 lua_State *L = S->L; 
    152154 int i,n; 
    153155 n=LoadInt(S); 
    154156 f->k=luaM_newvector(S->L,n,TValue); 
    155157 f->sizek=n; 
    156  for (i=0; i<n; i++) setnilvalue(&f->k[i]); 
     158 for (i=0; i<n; i++) setnilvalue(L, &f->k[i]); 
    157159 for (i=0; i<n; i++) 
    158160 { 
    159161  TValue* o=&f->k[i]; 
    static void LoadConstants(LoadState* S,  
    161163  switch (t) 
    162164  { 
    163165   case LUA_TNIL: 
    164         setnilvalue(o); 
     166        setnilvalue(L, o); 
    165167        break; 
    166168   case LUA_TBOOLEAN: 
    167169        setbvalue(o,LoadChar(S)!=0); 
    static Proto* LoadFunction(LoadState* S, 
    229231 LoadDebug(S,f); 
    230232 IF (!luaG_checkcode(f), "bad code"); 
    231233 S->L->top--; 
     234 setnilvalue(S->L, S->L->top); 
    232235 S->L->nCcalls--; 
    233236 return f; 
    234237} 
  • src/lvm.c

    a b  
    3939 * If 'obj' is a string, it is tried to be interpreted as a number. 
    4040 */ 
    4141const TValue *luaV_tonumber ( const TValue *obj, TValue *n) { 
     42  lua_State *L = NULL; /* FIXME */ 
    4243  lua_Number d; 
    4344  lua_Integer i; 
    4445   
    void luaV_concat (lua_State *L, int tota 
    384385        size_t l = tsvalue(top-i)->len; 
    385386        memcpy(buffer+tl, svalue(top-i), l); 
    386387        tl += l; 
     388                setnilvalue(L, top - i); 
    387389      } 
    388390      setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); 
    389391    } 
    void luaV_concat (lua_State *L, int tota 
    420422 */ 
    421423static void Arith (lua_State *L, StkId ra, const TValue *rb, 
    422424                   const TValue *rc, TMS op) { 
    423   TValue tempb, tempc; 
     425  TValue tempb = tvinit(), tempc = tvinit(); 
    424426  const TValue *b, *c; 
    425427  lua_Number nb,nc; 
    426428 
    void luaV_execute (lua_State *L, int nex 
    663665      OPCODE_TARGET(LOADNIL) { 
    664666        TValue *rb = RB(i); 
    665667        do { 
    666           setnilvalue(rb--); 
     668          setnilvalue(L, rb--); 
    667669        } while (rb >= ra); 
    668670        continue; 
    669671      } 
    void luaV_execute (lua_State *L, int nex 
    673675        continue; 
    674676      } 
    675677      OPCODE_TARGET(GETGLOBAL) { 
    676         TValue g; 
     678        TValue g = tvinit(); 
    677679        TValue *rb = KBx(i); 
    678680        sethvalue(L, &g, cl->env); 
    679681        lua_assert(ttisstring(rb)); 
    void luaV_execute (lua_State *L, int nex 
    685687        continue; 
    686688      } 
    687689      OPCODE_TARGET(SETGLOBAL) { 
    688         TValue g; 
     690        TValue g = tvinit(); 
    689691        sethvalue(L, &g, cl->env); 
    690692        lua_assert(ttisstring(KBx(i))); 
    691693        Protect(luaV_settable(L, &g, KBx(i), ra)); 
    void luaV_execute (lua_State *L, int nex 
    895897        if (--nexeccalls == 0)  /* was previous function running `here'? */ 
    896898          return;  /* no: return */ 
    897899        else {  /* yes: continue its execution */ 
    898           if (b) L->top = L->ci->top; 
     900          if (b) setlvmtop(L, L->ci->top); 
    899901          lua_assert(isLua(L->ci)); 
    900902          lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL); 
    901903          goto reentry; 
    void luaV_execute (lua_State *L, int nex 
    986988        for (; n > 0; n--) { 
    987989          TValue *val = ra+n; 
    988990          setobj2t(L, luaH_setint(L, h, last--), val); 
     991          setnilvalue(L, val); 
    989992          luaC_barriert(L, h, val); 
    990993        } 
    991994        continue; 
    void luaV_execute (lua_State *L, int nex 
    10301033            setobjs2s(L, ra + j, ci->base - n + j); 
    10311034          } 
    10321035          else { 
    1033             setnilvalue(ra + j); 
     1036            setnilvalue(L, ra + j); 
    10341037          } 
    10351038        } 
    10361039        continue; 
  • src/lvm.h

    a b  
    1111#include "ldo.h" 
    1212#include "lobject.h" 
    1313#include "ltm.h" 
     14#include "lstring.h" 
    1415 
    1516 
    1617#define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o))) 
     
    1920 
    2021#define equalobj(L,o1,o2) (ttype_ext_same(o1,o2) && luaV_equalval(L, o1, o2)) 
    2122 
     23static inline TValue *luaV_ref(TValue *tv) 
     24{ 
     25  if (ttisstring(tv)) 
     26    luaS_ref(rawtsvalue(tv)); 
     27  return tv; 
     28} 
     29 
     30static inline TValue *luaV_unref(lua_State *L, TValue *tv) 
     31{ 
     32  if (ttisstring(tv)) 
     33    luaS_unref(L, rawtsvalue(tv)); 
     34  return tv; 
     35} 
    2236 
    2337LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 
    2438LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2); 
  • src/llex.c

    a b  
    2323#include "ltable.h" 
    2424#include "lzio.h" 
    2525#include "lnum.h" 
     26#include "lvm.h" 
    2627 
    2728 
    2829 
    static void save (LexState *ls, int c) { 
    6970void luaX_init (lua_State *L) { 
    7071  int i; 
    7172  for (i=0; i<NUM_RESERVED; i++) { 
    72     TString *ts = luaS_new(L, luaX_tokens[i]); 
     73    TString *ts = luaS_ref(luaS_new(L, luaX_tokens[i])); 
    7374    luaS_fix(ts);  /* reserved words are never collected */ 
    7475    lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN); 
    7576    ts->tsv.reserved = cast_byte(i+1);  /* reserved word */ 
    void luaX_syntaxerror (LexState *ls, con 
    125126 
    126127TString *luaX_newstring (LexState *ls, const char *str, size_t l) { 
    127128  lua_State *L = ls->L; 
    128   TString *ts = luaS_newlstr(L, str, l); 
     129  TString *ts = luaS_ref(luaS_newlstr(L, str, l)); 
    129130  TValue *o = luaH_setstr(L, ls->fs->h, ts);  /* entry for `str' */ 
    130131  if (ttisnil(o)) 
    131132    setbvalue(o, 1);  /* make sure `str' will not be collected */ 
    void luaX_setinput (lua_State *L, LexSta 
    152153  ls->fs = NULL; 
    153154  ls->linenumber = 1; 
    154155  ls->lastline = 1; 
    155   ls->source = source; 
     156  ls->source = luaS_ref(source); 
    156157  luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER);  /* initialize buffer */ 
    157158  next(ls);  /* read first char */ 
    158159} 
  • src/lstate.h

    a b union GCObject { 
    144144  struct lua_State th;  /* thread */ 
    145145}; 
    146146 
     147#define setlvmtop(L, val) do { \ 
     148        int __i; \ 
     149        for (__i = L->top - val; __i-- > 0;) \ 
     150                setnilvalue(L, val + __i); \ 
     151        L->top = val; \ 
     152} while (0) 
     153 
    147154 
    148155/* macros to convert a GCObject into a specific value */ 
    149156#define rawgco2ts(o)    check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) 
Note: See TracBrowser for help on using the repository browser.