static void LoadConstants (LoadState* S, Proto* f)
{
 int i,n;
 n=LoadInt(S);
 f->k=luaM_newvector(S->L,n,TObject);
 f->sizek=n;
 for (i=0; i<n; i++)
 {
  TObject* o=&f->k[i];
  int t=LoadByte(S);
  switch (t)
  {
   case LUA_TNUMBER:
    setnvalue(o,LoadNumber(S));
    break;
   case LUA_TSTRING:
    setsvalue2n(o,LoadString(S));
    break;
   case LUA_TNIL:
       setnilvalue(o);
    break;
   default:
    luaG_runerror(S->L,"bad constant type (%d) in %s",t,S->name);
    break;
  }
 }
 n=LoadInt(S);
 f->p=luaM_newvector(S->L,n,Proto*);
 f->sizep=n;
 for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
}
示例#2
0
static void LoadConstants(LoadState* S, Proto* f)
{
 int i,n;
 n=LoadInt(S);
 f->k=luaM_newvector(S->L,n,TValue);
 f->sizek=n;
 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
 for (i=0; i<n; i++)
 {
  TValue* o=&f->k[i];
  int t=LoadChar(S);
  switch (t)
  {
   case LUA_TNIL:
	setnilvalue(o);
	break;
   case LUA_TBOOLEAN:
	setbvalue(o,LoadChar(S));
	break;
   case LUA_TNUMBER:
	setnvalue(o,LoadNumber(S));
	break;
   case LUA_TSTRING:
	setsvalue2n(S->L,o,LoadString(S));
	break;
  }
 }
 n=LoadInt(S);
 f->p=luaM_newvector(S->L,n,Proto*);
 f->sizep=n;
 for (i=0; i<n; i++) f->p[i]=NULL;
 for (i=0; i<n; i++) f->p[i]=LoadFunction(S);
}
示例#3
0
static void LoadConstants (LoadState *S, Proto *f) {
  int i;
  int n = LoadInt(S);
  f->k = luaM_newvector(S->L, n, TValue);
  f->sizek = n;
  for (i = 0; i < n; i++)
    setnilvalue(&f->k[i]);
  for (i = 0; i < n; i++) {
    TValue *o = &f->k[i];
    int t = LoadByte(S);
    switch (t) {
    case LUA_TNIL:
      setnilvalue(o);
      break;
    case LUA_TBOOLEAN:
      setbvalue(o, LoadByte(S));
      break;
    case LUA_TNUMFLT:
      setfltvalue(o, LoadNumber(S));
      break;
    case LUA_TNUMINT:
      setivalue(o, LoadInteger(S));
      break;
    case LUA_TSHRSTR:
    case LUA_TLNGSTR:
      setsvalue2n(S->L, o, LoadString(S));
      break;
    default:
      lua_assert(0);
    }
  }
}
示例#4
0
int luaK_stringK (FuncState *fs, TString *s) {
  TValue o;
#if LUA_REFCOUNT
  lua_State *L = fs->L;
  int ret;
  setsvalue2n(fs->L, &o, s);
  ret = addk(fs, &o, &o);
  setnilvalue(&o);
  return ret;
#else
  setsvalue(fs->L, &o, s);
  return addk(fs, &o, &o);
#endif /* LUA_REFCOUNT */
}
示例#5
0
static void LoadConstants(LoadState* S, Proto* f)
{
 int i,n;
#if LUA_REFCOUNT    
 lua_State *L = S->L;
#endif /* LUA_REFCOUNT */
 n=LoadInt(S);
 f->k=luaM_newvector(S->L,n,TValue);
 f->sizek=n;
#if LUA_REFCOUNT
 for (i=0; i<n; i++) setnilvalue2n(L, &f->k[i]);
#else
 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
#endif /* LUA_REFCOUNT */
 for (i=0; i<n; i++)
 {
  TValue* o=&f->k[i];
  int t=LoadChar(S);
  switch (t)
  {
   case LUA_TNIL:
   	setnilvalue(o);
	break;
   case LUA_TBOOLEAN:
   	setbvalue(o,LoadChar(S)!=0);
	break;
   case LUA_TNUMBER:
	setnvalue(o,LoadNumber(S));
	break;
   case LUA_TSTRING:
	setsvalue2n(S->L,o,LoadString(S));
	break;
   default:
	error(S,"bad constant");
	break;
  }
 }
 n=LoadInt(S);
 f->p=luaM_newvector(S->L,n,Proto*);
 f->sizep=n;
 for (i=0; i<n; i++) f->p[i]=NULL;
#if LUA_REFCOUNT
 for (i=0; i<n; i++) {
  f->p[i]=LoadFunction(S,f->source);
  luarc_addrefproto(f->p[i]);
 }
#else
 for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
#endif /* LUA_REFCOUNT */
}
示例#6
0
static void LoadConstants(LoadState* S, Proto* f)
{
 int i,n;
 n=LoadInt(S);
 f->k=luaM_newvector(S->L,n,TValue);
 f->sizek=n;
 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
 for (i=0; i<n; i++)
 {
  TValue* o=&f->k[i];
  int t=LoadChar(S);
  switch (t)
  {
   case LUA_TNIL:
   	setnilvalue(o);
	break;
   case LUA_TBOOLEAN:
   	setbvalue(o,LoadChar(S)!=0);
	break;
   case LUA_TNUMBER:
	setnvalue(o,LoadNumber(S));
	break;
#ifdef LUA_TINT
   case LUA_TINT:   /* Integer type saved in bytecode (see lcode.c) */
	setivalue(o,LoadInteger(S));
	break;
#endif
   case LUA_TSTRING:
	setsvalue2n(S->L,o,LoadString(S));
	break;
   default:
	error(S,"bad constant");
	break;
  }
 }
 n=LoadInt(S);
 f->p=luaM_newvector(S->L,n,Proto*);
 f->sizep=n;
 for (i=0; i<n; i++) f->p[i]=NULL;
 for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
}
示例#7
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;
  }
}
示例#8
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;
 }
}
示例#9
0
Proto *load_jit_proto(lua_State *L, jit_proto *p) {
	Proto *f = luaF_newproto(L);
	unsigned int i;

	/* proto source */
	f->source = luaS_new(L, p->name);
	/* jit_func */
	f->jit_func = p->jit_func;
	/* linedefined */
	f->linedefined = p->linedefined;
	/* lastlinedefined */
	f->lastlinedefined = p->lastlinedefined;
	/* nups */
	f->nups = p->nups;
	/* numparams */
	f->numparams = p->numparams;
	/* is_vararg */
	f->is_vararg = p->is_vararg;
	/* maxstacksize */
	f->maxstacksize = p->maxstacksize;
	/* sizek */
	f->sizek = p->sizek;
	/* k */
	f->k=luaM_newvector(L,p->sizek,TValue);
	for(i = 0; i < p->sizek; i++) {
		TValue *o=&f->k[i];
		switch(p->k[i].type) {
			case TYPE_STRING:
				setsvalue2n(L,o, luaS_newlstr(L, p->k[i].val.str, p->k[i].length));
				break;
			case TYPE_BOOLEAN:
				setbvalue(o, p->k[i].val.b != 0);
				break;
			case TYPE_NUMBER:
				setnvalue(o, p->k[i].val.num);
				break;
			case TYPE_NIL:
			default:
				setnilvalue(o);
				break;
		}
	}
	/* sizep */
	f->sizep = p->sizep;
	/* p */
	f->p=luaM_newvector(L,(size_t)p->sizep,Proto*);
	for(i = 0; i < p->sizep; i++) {
		f->p[i] = load_jit_proto(L, &(p->p[i]));
	}
	/* sizecode */
	f->sizecode = p->sizecode;
	/* code */
	f->code=luaM_newvector(L,(size_t)p->sizecode,Instruction);
	for(i = 0; i < p->sizecode; i++) {
		f->code[i] = p->code[i];
	}
	/* sizelineinfo */
	f->sizelineinfo = p->sizelineinfo;
	/* lineinfo */
	f->lineinfo=luaM_newvector(L,(size_t)p->sizelineinfo,int);
	for(i = 0; i < p->sizelineinfo; i++) {
		f->lineinfo[i] = p->lineinfo[i];
	}
	/* sizelocvars */
	f->sizelocvars = p->sizelocvars;
	/* locvars */
	f->locvars=luaM_newvector(L,p->sizelocvars,LocVar);
	for(i = 0; i < p->sizelocvars; i++) {
		jit_LocVar *locvar = &(p->locvars[i]);
		f->locvars[i].varname = luaS_new(L, locvar->varname);
		f->locvars[i].startpc = locvar->startpc;
		f->locvars[i].endpc = locvar->endpc;
	}
	/* sizeupvalues */
	f->sizeupvalues = p->sizeupvalues;
	/* upvalues */
	f->upvalues=luaM_newvector(L,(size_t)p->sizeupvalues,TString*);
	for(i = 0; i < p->sizeupvalues; i++) {
		f->upvalues[i] = luaS_new(L, p->upvalues[i]);
	}
	return f;
}