Esempio n. 1
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();
    }
  }
}
Esempio n. 2
0
/*
** generic allocation routine.
*/
void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
  void *newblock;
  global_State *g = G(L);
  size_t realosize = (block) ? osize : 0;
  lua_assert((realosize == 0) == (block == NULL));
#if defined(HARDMEMTESTS)
  if (nsize > realosize && g->gcrunning)
    luaC_fullgc(L, 1);  /* force a GC whenever possible */
#endif
  newblock = (*g->frealloc)(g->ud, block, osize, nsize);
  if (newblock == NULL && nsize > 0) {
    api_check(L, nsize > realosize,
                 "realloc cannot fail when shrinking a block");
    if (g->gcrunning) {
      luaC_fullgc(L, 1);  /* try to free some memory... */
      newblock = (*g->frealloc)(g->ud, block, osize, nsize);  /* try again */
    }
    if (newblock == NULL)
      luaD_throw(L, LUA_ERRMEM);
  }
  lua_assert((nsize == 0) == (newblock == NULL));
  g->GCdebt = (g->GCdebt + nsize) - realosize;
#if defined(TRACEMEM)
  { /* auxiliary patch to monitor garbage collection.
    ** To plot, gnuplot with following command:
    ** plot TRACEMEM using 1:2 with lines, TRACEMEM using 1:3 with lines
    */
    static unsigned long total = 0;  /* our "time" */
    static FILE *f = NULL;  /* output file */
    total++;  /* "time" always grows */
    if ((total % 200) == 0) {
      if (f == NULL) f = fopen(TRACEMEM, "w");
      fprintf(f, "%lu %u %d %d\n", total,
              gettotalbytes(g), g->GCdebt, g->gcstate * 10000);
    }
  }
#endif

  return newblock;
}
Esempio n. 3
0
/**
 * 调用此函数前,参数都经过检查, 保证了分配的内存块不会过大;
 * 
 * 创建对象的函数 luaM_newobject 也调用此函数(lmem.h):
 *  #define luaM_newobject(L,tag,s)	luaM_realloc_(L, NULL, tag, (s))
 *  
 *  nsize 为 0 表示释放内存
 *  #define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0)
 *  
 *  内部调用 g->frealloc 来重新分配内存
 */
void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
  void *newblock;
  global_State *g = G(L);
  size_t realosize = (block) ? osize : 0;
  lua_assert((realosize == 0) == (block == NULL));
#if defined(HARDMEMTESTS)
  if (nsize > realosize && g->gcrunning)
    luaC_fullgc(L, 1);  /* force a GC whenever possible */
#endif
  newblock = (*g->frealloc)(g->ud, block, osize, nsize);
  if (newblock == NULL && nsize > 0) {
    api_check( nsize > realosize,
                 "realloc cannot fail when shrinking a block");
    luaC_fullgc(L, 1);  /* try to free some memory... */
    newblock = (*g->frealloc)(g->ud, block, osize, nsize);  /* try again */
    if (newblock == NULL)
      luaD_throw(L, LUA_ERRMEM);
  }
  lua_assert((nsize == 0) == (newblock == NULL));
  g->GCdebt = (g->GCdebt + nsize) - realosize;
  return newblock;
}
Esempio n. 4
0
static void traceexec (lua_State *L) {
  CallInfo *ci = L->ci;
  lu_byte mask = L->hookmask;
  if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
    resethookcount(L);
    luaD_hook(L, LUA_HOOKCOUNT, -1);
  }
  if (mask & LUA_MASKLINE) {
    Proto *p = ci_func(ci)->p;
    int npc = pcRel(ci->u.l.savedpc, p);
    int newline = getfuncline(p, npc);
    if (npc == 0 ||  /* call linehook when enter a new function, */
        ci->u.l.savedpc <= L->oldpc ||  /* when jump back (loop), or when */
        newline != getfuncline(p, pcRel(L->oldpc, p)))  /* enter a new line */
      luaD_hook(L, LUA_HOOKLINE, newline);
  }
  L->oldpc = ci->u.l.savedpc;
  if (L->status == LUA_YIELD) {  /* did hook yield? */
    ci->u.l.savedpc--;  /* undo increment (resume will increment it again) */
    luaD_throw(L, LUA_YIELD);
  }
}
Esempio n. 5
0
File: ldo.c Progetto: 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();
    }
  }
}
Esempio n. 6
0
static l_noret lexerror (LexState *ls, const char *msg, int token) {
  msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber);
  if (token)
    luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token));
  luaD_throw(ls->L, LUA_ERRSYNTAX);
}
Esempio n. 7
0
static void error(LoadState* S, const char* why)
{
 luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
 luaD_throw(S->L,LUA_ERRSYNTAX);
}
Esempio n. 8
0
/*
** open parts that may cause memory-allocation errors
*/
static void f_luaopen (lua_State *L, void *ud) {
  int i;
  global_State globalState;
  lua_State luaState;
  global_State *g;
#ifdef _DEBUG
  luaState.allocName = "Lua_global_State";
#endif _DEBUG
  luaState.l_G = &globalState;
  globalState.reallocFunc = luaHelper_Realloc;
  globalState.freeFunc = luaHelper_Free;
  globalState.memData = luaHelper_memData;
  globalState.nblocks = sizeof(lua_State) + sizeof(global_State);	// Bogus.
  /* create a new global state */
  g = luaM_new(&luaState, global_State);
  UNUSED(ud);
  if (g == NULL) luaD_throw(L, LUA_ERRMEM);
  L->l_G = g;
  g->mainthread = L;
  g->GCthreshold = 0;  /* mark it as unfinished state */
  g->strt.size = 0;
  g->strt.nuse = 0;
  g->strt.hash = NULL;
  setnilvalue2n(defaultmeta(L));
  setnilvalue2n(registry(L));
  luaZ_initbuffer(L, &g->buff);
  g->panic = default_panic;
#if !LUA_REFCOUNT
  g->rootgc = NULL;
  g->rootudata = NULL;
  g->tmudata = NULL;
#else LUA_REFCOUNT
  g->rootgc_head.next = (GCObject*)&g->rootgc_tail;
  g->rootgc_head.prev = NULL;
  g->rootgc_tail.next = NULL;
  g->rootgc_tail.prev = (GCObject*)&g->rootgc_head;
  g->rootgc_head.tt = LUA_TNIL;
  g->rootgc_head.marked = 0;
  g->rootgc_head.ref = 0;
  g->rootgc_tail.tt = LUA_TNIL;
  g->rootgc_tail.marked = 0;
  g->rootgc_tail.ref = 0;

  g->rootudata_head.next = (GCObject*)&g->rootudata_tail;
  g->rootudata_head.prev = NULL;
  g->rootudata_tail.next = NULL;
  g->rootudata_tail.prev = (GCObject*)&g->rootudata_head;
  g->rootudata_head.tt = LUA_TNIL;
  g->rootudata_head.marked = 0;
  g->rootudata_head.ref = 0;
  g->rootudata_tail.tt = LUA_TNIL;
  g->rootudata_tail.marked = 0;
  g->rootudata_tail.ref = 0;

  g->tmudata_head.next = (GCObject*)&g->tmudata_tail;
  g->tmudata_head.prev = NULL;
  g->tmudata_tail.next = NULL;
  g->tmudata_tail.prev = (GCObject*)&g->tmudata_head;
  g->tmudata_head.tt = LUA_TNIL;
  g->tmudata_head.marked = 0;
  g->tmudata_head.ref = 0;
  g->tmudata_tail.tt = LUA_TNIL;
  g->tmudata_tail.marked = 0;
  g->tmudata_tail.ref = 0;
#endif LUA_REFCOUNT
  setnilvalue2n(gkey(g->dummynode));
  setnilvalue2n(gval(g->dummynode));
  g->dummynode->next = NULL;
  g->nblocks = sizeof(lua_State) + sizeof(global_State);

  g->reallocFunc = luaHelper_Realloc;
  g->freeFunc = luaHelper_Free;
  g->memData = luaHelper_memData;
  g->fatalErrorFunc = defaultFatalErrorFunc;
#ifdef LUA_MTSUPPORT
  g->lockData = NULL;
  g->lockFunc = NULL;
  g->unlockFunc = NULL;
#endif LUA_MTSUPPORT
  g->userGCFunction = NULL;
  g->globalUserData = NULL;

  stack_init(L, L);  /* init stack */

  for (i = 0; i < LUA_NTYPES; i++)
  {
    defaultmetatypes(L, i)->value.gc = NULL;
  }

  /* create default meta table with a dummy table, and then close the loop */
  defaultmeta(L)->tt = LUA_TNUMBER;
  defaultmeta(L)->value.gc = NULL;
  sethvalue2n(defaultmeta(L), luaH_new(L, 0, 0));
  __AddRefDirect(hvalue(defaultmeta(L)));
  hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L));
  __AddRefDirect(hvalue(defaultmeta(L))->metatable);

  /* build meta tables */
  for (i = 0; i < LUA_NTYPES; i++)
  {
    luaM_setname(L, "Lua_defaultMetaTypes");
    sethvalue2n(defaultmetatypes(L, i), luaH_new(L, 0, 0));
    hvalue(defaultmetatypes(L, i))->metatable = hvalue(defaultmeta(L));
  }

  luaM_setname(L, "Lua_Globals");
  sethvalue(gt(L), luaH_new(L, 0, 4));  /* table of globals */
  __AddRefDirect(hvalue(gt(L)));
  luaM_setname(L, "Lua_Registry");
  sethvalue(registry(L), luaH_new(L, 4, 4));  /* registry */
  __AddRef(registry(L));
  g->minimumstrings = lua_minimumnumstrings;
  luaS_resize(L, MINSTRTABSIZE);  /* initial size of string table */
  luaT_init(L);
  luaX_init(L);
  luaS_fix(luaS_newliteral(L, MEMERRMSG));
  g->GCthreshold = 4*G(L)->nblocks;

  luaZ_openspace(L, &g->buff, lua_minimumauxspace);
}
Esempio n. 9
0
static l_noret error(DumpState* D, const char* why)
{
 luaO_pushfstring(D->L,"unable to write precompiled chunk: %s",why);
 luaD_throw(D->L,LUA_ERRSYNTAX);
}