Beispiel #1
0
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
                    int firstchar) {
  ls->t.token = 0;
  ls->L = L;
  ls->current = firstchar;
  ls->lookahead.token = TK_EOS;  /* no look-ahead token */
  ls->z = z;
  ls->fs = NULL;
  ls->linenumber = 1;
  ls->lastline = 1;
  ls->source = source;
  ls->envn = luaS_newliteral(L, LUA_ENV);  /* get env name */
  luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER);  /* initialize buffer */
}
Beispiel #2
0
/*
 * open parts that may cause memory-allocation errors
 */
static void f_luaopen(lua_State * L, void * ud)
{
	global_State *g = G(L);

	UNUSED(ud);
	stack_init(L, L); 								/* init stack */
	sethvalue(L, gt(L), luaH_new(L, 0, 2)); 		/* table of globals */
	sethvalue(L, registry(L), luaH_new(L, 0, 2)); 	/* registry */
	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->totalbytes;
}
Beispiel #3
0
/*
** load precompiled chunk
*/
Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
{
 LoadState S;
 if (*name=='@' || *name=='=')
  S.name=name+1;
 else if (*name==LUA_SIGNATURE[0])
  S.name="binary string";
 else
  S.name=name;
 S.L=L;
 S.Z=Z;
 S.b=buff;
 LoadHeader(&S);
#if LUA_REFCOUNT
 {
  Proto *proto = LoadFunction(&S,luaS_newliteral(L,"=?"));
  luarc_addrefproto(proto);
  return proto;
 }
#else
 return LoadFunction(&S,luaS_newliteral(L,"=?"));
#endif /* LUA_REFCOUNT */
}
Beispiel #4
0
/*
** load precompiled chunk
*/
Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
{
 LoadState S;
 if (*name=='@' || *name=='=')
  S.name=name+1;
 else if (*name==LUA_SIGNATURE[0])
  S.name="binary string";
 else
  S.name=name;
 S.L=L;
 S.Z=Z;
 S.b=buff;
 LoadHeader(&S);
 return LoadFunction(&S,luaS_newliteral(L,"=?"));
}
Beispiel #5
0
/*
** open parts of the state that may cause memory-allocation errors
*/
static void f_luaopen (lua_State *L, void *ud) {
  global_State *g = G(L);
  UNUSED(ud);
  stack_init(L, L);  /* init stack */
  init_registry(L, g);
  luaS_resize(L, MINSTRTABSIZE);  /* initial size of string table */
  luaT_init(L);
  luaX_init(L);
  /* pre-create memory-error message */
  g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
  luaS_fix(g->memerrmsg);  /* it should never be collected */
  g->gcrunning = 1;  /* allow gc */
  g->version = lua_version(NULL);
  luai_userstateopen(L);
}
Beispiel #6
0
static void seterrorobj (lua_State *L, int errcode, StkId oldtop) {
  switch (errcode) {
    case LUA_ERRMEM: {  /* memory error? */
      setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
      break;
    }
    case LUA_ERRERR: {
      setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
      break;
    }
    default: {
      setobjs2s(L, oldtop, L->top - 1);  /* error message on current top */
      break;
    }
  }
  L->top = oldtop + 1;
}
Beispiel #7
0
/*
** load precompiled chunk
*/
Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name,
                    size_t nameLength)
{
    LoadState S;
    if (*name=='@' || *name=='=') {
        S.name=name+1;
        S.nameLength = nameLength -1;
    } else if (*name==LUA_SIGNATURE[0]) {
        S.name="binary string";
        S.nameLength = strlen(S.name);
    } else {
        S.name=name;
        S.nameLength = nameLength;
    }
    S.L=L;
    S.Z=Z;
    S.b=buff;
    LoadHeader(&S);
    return LoadFunction(&S,luaS_newliteral(L,"=?"));
}
Beispiel #8
0
static Proto* combine(lua_State* L, int n)
{
	if (n==1) {
		int i;
		Proto* f = toproto(L,-1); 
		if (LDS2) {
		  Inject(f,0);
		  for (i=0; i<f->sizep; i++) {
		  	Inject(f->p[i],i+1);
		  }
  	}
		return f;
	}
 else
 {
  int i,pc=0;
  Proto* f=luaF_newproto(L);
  f->source=luaS_newliteral(L,"=(" PROGNAME ")");
  f->maxstacksize=1;
  f->p=luaM_newvector(L,n,Proto*);
  f->sizep=n;
  f->sizecode=2*n+1;
  f->code=luaM_newvector(L,f->sizecode,Instruction);
  for (i=0; i<n; i++)
  {
   f->p[i]=toproto(L,i-n);
   f->code[pc++]=CREATE_ABx(HKS_OPCODE_CLOSURE,0,i);
   f->code[pc++]=CREATE_ABC(HKS_OPCODE_CALL,0,1,1);
  }
  f->code[pc++]=CREATE_ABC(HKS_OPCODE_RETURN,0,1,0);
	if (LDS2) {
		Inject(f,0);
		for (i=0; i<n; i++) {
			Inject(f->p[i],i+1);
		}
	}
  return f;
 }
}
Beispiel #9
0
/*
** load precompiled chunk
*/
Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
{
 LoadState S;

    // MTA Specific
    int bUnexpected = pUndumpHook && !pUndumpHook( Z->p, Z->n );

 if (*name=='@' || *name=='=')
  S.name=name+1;
 else if (*name==LUA_SIGNATURE[0])
  S.name="binary string";
 else
  S.name=name;
 S.L=L;
 S.Z=Z;
 S.b=buff;
 LoadHeader(&S);

    // MTA Specific
    if ( bUnexpected )
        error(&S,"unexpected");

 return LoadFunction(&S,luaS_newliteral(L,"=?"));
}
Beispiel #10
0
static const Proto* combine(lua_State* L, int n) {
	if (n == 1)
		return toproto(L,-1);
	else {
		int i, pc;
		Proto* f = luaF_newproto(L);
		setptvalue2s(L,L->top,f);incr_top(L);
		f->source = luaS_newliteral(L,"=(" PROGNAME ")");
		f->maxstacksize = 1;
		pc = 2 * n + 1;
		f->code = luaM_newvector(L,pc,Instruction);
		f->sizecode = pc;
		f->p = luaM_newvector(L,n,Proto*);
		f->sizep = n;
		pc = 0;
		for (i = 0; i < n; i++) {
			f->p[i] = toproto(L,i-n-1);
			f->code[pc++] = CREATE_ABx(OP_CLOSURE,0,i);
			f->code[pc++] = CREATE_ABC(OP_CALL,0,1,1);
		}
		f->code[pc++] = CREATE_ABC(OP_RETURN,0,1,0);
		return f;
	}
}
Beispiel #11
0
static Proto* combine(lua_State* L, int scripts) {
  if (scripts==1 && preloads==0)
    return toproto(L,-1);
  else {
    TString *s;
    TValue *k;
    int i,pc,n;
    Proto* f=luaF_newproto(L);
    setptvalue2s(L,L->top,f); incr_top(L);
    f->source=luaS_newliteral(L,"=(" PROGNAME ")");
    f->maxstacksize=1;
    pc=(2*scripts) + 1;
    if(preloads > 0) {
      pc+=(2*preloads) + 2;
    }
    f->code=luaM_newvector(L,pc,Instruction);
    f->sizecode=pc;
    n=(scripts + preloads);
    f->p=luaM_newvector(L,n,Proto*);
    f->sizep=n;
    pc=0;
    n=0;
    /* preload libraries. */
    if (preloads > 0) {
      /* create constants array. */
      f->k=luaM_newvector(L, (preloads + 2),TValue);
      f->sizek=(preloads + 2);
      /* make room for "local t" variable. */
      f->maxstacksize=2;
      /* add "package" & "preload" constants. */
      k=&(f->k[0]);
      s=luaS_newliteral(L, "package");
      setsvalue2n(L,k,s);
      k=&(f->k[1]);
      s=luaS_newliteral(L, "preload");
      setsvalue2n(L,k,s);
      /* code: local t = package.preload */
      f->code[pc++]=CREATE_ABx(OP_GETGLOBAL,0,0);
      f->code[pc++]=CREATE_ABC(OP_GETTABLE,0,0,RKASK(1));
    }
    /* add preload libraries to "package.preload" */
    for (i=0; i < preloads; i++) {
      /* create constant for library name. */
      k=&(f->k[i+2]);
      s=luaS_new(L, preload_libs[i]);
      setsvalue2n(L,k,s);
      /* code: t['name'] = function() --[[ lib code ]] end */
      f->code[pc++]=CREATE_ABx(OP_CLOSURE,1,n);
      f->code[pc++]=CREATE_ABC(OP_SETTABLE,0,RKASK(i+2),1);
      f->p[n++]=toproto(L,i-preloads-1);
    }
    /* call scripts. */
    for (i=0; i < scripts; i++) {
      /* code: (function() --[[ script code ]] end)() */
      f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,n);
      f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1);
      f->p[n++]=toproto(L,i-scripts-1-preloads);
    }
    f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0);
    return f;
  }
}
Beispiel #12
0
/*
 * If the luac command line includes multiple files or has the -f option 
 * then luac generates a main function to reference all sub-main prototypes.
 * This is one of two types:
 *   Type 0   The standard luac combination main
 *   Type 1   A lookup wrapper that facilitates indexing into the generated protos 
 */
static const Proto* combine(lua_State* L, int n, int type)
{
 if (n==1 && type == 0)
  return toproto(L,-1);
 else
 {
  int i;
  Instruction *pc;
  Proto* f=luaF_newproto(L);
  setptvalue2s(L,L->top,f); incr_top(L);
  f->source=luaS_newliteral(L,"=(" PROGNAME ")");
  f->p=luaM_newvector(L,n,Proto*);
  f->sizep=n;
  for (i=0; i<n; i++) 
    f->p[i]=toproto(L,i-n-1);
  pc=0;

  if (type == 0) {
  /*
   * Type 0 is as per the standard luac, which is just a main routine which 
   * invokes all of the compiled functions sequentially.  This is fine if 
   * they are self registering modules, but useless otherwise.
   */
   f->numparams    = 0;
   f->maxstacksize = 1;
   f->sizecode     = 2*n + 1 ;
   f->sizek        = 0;
   f->code         = luaM_newvector(L, f->sizecode , Instruction);
   f->k            = luaM_newvector(L,f->sizek,TValue);

   for (i=0, pc = f->code; i<n; i++) {
    *pc++ = CREATE_ABx(OP_CLOSURE,0,i);
    *pc++ = CREATE_ABC(OP_CALL,0,1,1);
   }
   *pc++ = CREATE_ABC(OP_RETURN,0,1,0);
  } else {
  /*
   * The Type 1 main() is a lookup which takes a single argument, the name to  
   * be resolved. If this matches root name of one of the compiled files then
   * a closure to this file main is returned.  Otherwise the Unixtime of the
   * compile and the list of root names is returned.
   */
   if (n > LFIELDS_PER_FLUSH) {
#define NO_MOD_ERR_(n) ": Number of modules > " #n
#define NO_MOD_ERR(n) NO_MOD_ERR_(n)
    usage(LUA_QL("-f")  NO_MOD_ERR(LFIELDS_PER_FLUSH));
   }
   f->numparams    = 1;
   f->maxstacksize = n + 3;
   f->sizecode     = 5*n + 5 ;
   f->sizek        = n + 1;
   f->sizelocvars  = 0;
   f->code         = luaM_newvector(L, f->sizecode , Instruction);
   f->k            = luaM_newvector(L,f->sizek,TValue);
   for (i=0, pc = f->code; i<n; i++)  
   {
    /* if arg1 == FnameA then return function (...) -- funcA -- end end */
    setsvalue2n(L,f->k+i,corename(L, f->p[i]->source));
    *pc++ = CREATE_ABC(OP_EQ,0,0,RKASK(i)); 
    *pc++ = CREATE_ABx(OP_JMP,0,MAXARG_sBx+2);
    *pc++ = CREATE_ABx(OP_CLOSURE,1,i);
    *pc++ = CREATE_ABC(OP_RETURN,1,2,0);
   }

   setnvalue(f->k+n, (lua_Number) time(NULL));

   *pc++ = CREATE_ABx(OP_LOADK,1,n);
   *pc++ = CREATE_ABC(OP_NEWTABLE,2,luaO_int2fb(i),0);   
   for (i=0; i<n; i++) 
     *pc++ = CREATE_ABx(OP_LOADK,i+3,i);
   *pc++ = CREATE_ABC(OP_SETLIST,2,i,1);   
   *pc++ = CREATE_ABC(OP_RETURN,1,3,0);
   *pc++ = CREATE_ABC(OP_RETURN,0,1,0);
  }
  lua_assert((pc-f->code) == f->sizecode);

  return f;
 }
}
Beispiel #13
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);
}