org.luaj.vm2.Lua Java Examples

The following examples show how to use org.luaj.vm2.Lua. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProtoInfo.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
private void findUpvalues() {
	int[] code = prototype.code;
	int n = code.length;
	
	// propogate to inner prototypes
	String[] names = findInnerprotoNames();
	for ( int pc=0; pc<n; pc++ ) {
		if ( Lua.GET_OPCODE(code[pc]) == Lua.OP_CLOSURE ) {
			int bx = Lua.GETARG_Bx(code[pc]);
			Prototype newp = prototype.p[bx];
			UpvalInfo[] newu = new UpvalInfo[newp.upvalues.length];
			String newname = name + "$" + names[bx];
			for ( int j=0; j<newp.upvalues.length; ++j ) {
				Upvaldesc u = newp.upvalues[j];
				newu[j] = u.instack? findOpenUp(pc,u.idx) : upvals[u.idx];
			}
			subprotos[bx] = new ProtoInfo(newp, newname, newu);
		}
	}
	
	// mark all upvalues that are written locally as read/write
	for ( int pc=0; pc<n; pc++ ) {
		if ( Lua.GET_OPCODE(code[pc]) == Lua.OP_SETUPVAL )
			upvals[Lua.GETARG_B(code[pc])].rw = true;
	}
}
 
Example #2
Source File: LexState.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
void recfield(ConsControl cc) {
	/* recfield -> (NAME | `['exp1`]') = exp1 */
	FuncState fs = this.fs;
	int reg = this.fs.freereg;
	expdesc key = new expdesc();
	expdesc val = new expdesc();
	int rkkey;
	if (this.t.token == TK_NAME) {
		fs.checklimit(cc.nh, MAX_INT, "items in a constructor");
		this.checkname(key);
	} else
		/* this.t.token == '[' */
		this.yindex(key);
	cc.nh++;
	this.checknext('=');
	rkkey = fs.exp2RK(key);
	this.expr(val);
	fs.codeABC(Lua.OP_SETTABLE, cc.t.u.info, rkkey, fs.exp2RK(val));
	fs.freereg = (short)reg; /* free registers */
}
 
Example #3
Source File: LexState.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
void check_conflict (LHS_assign lh, expdesc v) {
	FuncState fs = this.fs;
	short extra = (short) fs.freereg;  /* eventual position to save local variable */
	boolean conflict = false;
	for (; lh!=null; lh = lh.prev) {
		if (lh.v.k == VINDEXED) {
			/* table is the upvalue/local being assigned now? */
			if (lh.v.u.ind_vt == v.k && lh.v.u.ind_t == v.u.info) {
				conflict = true;
				lh.v.u.ind_vt = VLOCAL;
				lh.v.u.ind_t = extra;  /* previous assignment will use safe copy */
			}
			/* index is the local being assigned? (index cannot be upvalue) */
			if (v.k == VLOCAL && lh.v.u.ind_idx == v.u.info) {
				conflict = true;
				lh.v.u.ind_idx = extra;  /* previous assignment will use safe copy */
			}
		}
	}
	if (conflict) {
	    /* copy upvalue/local value to a temporary (in position 'extra') */
	    int op = (v.k == VLOCAL) ? Lua.OP_MOVE : Lua.OP_GETUPVAL;
	    fs.codeABC(op, extra, v.u.info, 0);
	    fs.reserveregs(1);
	}
}
 
Example #4
Source File: LexState.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
void forbody(int base, int line, int nvars, boolean isnum) {
	/* forbody -> DO block */
	BlockCnt bl = new BlockCnt();
	FuncState fs = this.fs;
	int prep, endfor;
	this.adjustlocalvars(3); /* control variables */
	this.checknext(TK_DO);
	prep = isnum ? fs.codeAsBx(Lua.OP_FORPREP, base, NO_JUMP) : fs.jump();
	fs.enterblock(bl, false); /* scope for declared variables */
	this.adjustlocalvars(nvars);
	fs.reserveregs(nvars);
	this.block();
	fs.leaveblock(); /* end of scope for declared variables */
	fs.patchtohere(prep);
	if (isnum)  /* numeric for? */
		endfor = fs.codeAsBx(Lua.OP_FORLOOP, base, NO_JUMP);
	else {  /* generic for */
		fs.codeABC(Lua.OP_TFORCALL, base, 0, nvars);
		fs.fixline(line);
		endfor = fs.codeAsBx(Lua.OP_TFORLOOP, base + 2, NO_JUMP);
	}
	fs.patchlist(endfor, prep + 1);
	fs.fixline(line);
}
 
Example #5
Source File: LexState.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
void fornum(LuaString varname, int line) {
	/* fornum -> NAME = exp1,exp1[,exp1] forbody */
	FuncState fs = this.fs;
	int base = fs.freereg;
	this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_INDEX);
	this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_LIMIT);
	this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_STEP);
	this.new_localvar(varname);
	this.checknext('=');
	this.exp1(); /* initial value */
	this.checknext(',');
	this.exp1(); /* limit */
	if (this.testnext(','))
		this.exp1(); /* optional step */
	else { /* default step = 1 */
		fs.codeABx(Lua.OP_LOADK, fs.freereg, fs.numberK(LuaInteger.valueOf(1)));
		fs.reserveregs(1);
	}
	this.forbody(base, line, 1, true);
}
 
Example #6
Source File: LexState.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
void fornum(LuaString varname, int line) {
	/* fornum -> NAME = exp1,exp1[,exp1] forbody */
	FuncState fs = this.fs;
	int base = fs.freereg;
	this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_INDEX);
	this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_LIMIT);
	this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_STEP);
	this.new_localvar(varname);
	this.checknext('=');
	this.exp1(); /* initial value */
	this.checknext(',');
	this.exp1(); /* limit */
	if (this.testnext(','))
		this.exp1(); /* optional step */
	else { /* default step = 1 */
		fs.codeABx(Lua.OP_LOADK, fs.freereg, fs.numberK(LuaInteger.valueOf(1)));
		fs.reserveregs(1);
	}
	this.forbody(base, line, 1, true);
}
 
Example #7
Source File: ProtoInfo.java    From luaj with MIT License 6 votes vote down vote up
private void findUpvalues() {
	int[] code = prototype.code;
	int n = code.length;
	
	// propogate to inner prototypes
	String[] names = findInnerprotoNames();
	for ( int pc=0; pc<n; pc++ ) {
		if ( Lua.GET_OPCODE(code[pc]) == Lua.OP_CLOSURE ) {
			int bx = Lua.GETARG_Bx(code[pc]);
			Prototype newp = prototype.p[bx];
			UpvalInfo[] newu = new UpvalInfo[newp.upvalues.length];
			String newname = name + "$" + names[bx];
			for ( int j=0; j<newp.upvalues.length; ++j ) {
				Upvaldesc u = newp.upvalues[j];
				newu[j] = u.instack? findOpenUp(pc,u.idx) : upvals[u.idx];
			}
			subprotos[bx] = new ProtoInfo(newp, newname, newu);
		}
	}
	
	// mark all upvalues that are written locally as read/write
	for ( int pc=0; pc<n; pc++ ) {
		if ( Lua.GET_OPCODE(code[pc]) == Lua.OP_SETUPVAL )
			upvals[Lua.GETARG_B(code[pc])].rw = true;
	}
}
 
Example #8
Source File: LexState.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
void fornum(LuaString varname, int line) {
/* fornum -> NAME = exp1,exp1[,exp1] forbody */
      FuncState fs = this.fs;
      int base = fs.freereg;
      this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_INDEX);
      this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_LIMIT);
      this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_STEP);
      this.new_localvar(varname);
      this.checknext('=');
      this.exp1(); /* initial value */
      this.checknext(',');
      this.exp1(); /* limit */
      if (this.testnext(','))
          this.exp1(); /* optional step */
      else { /* default step = 1 */
          fs.codeABx(Lua.OP_LOADK, fs.freereg, fs.numberK(LuaInteger.valueOf(1)));
          fs.reserveregs(1);
      }
      this.forbody(base, line, 1, true);
  }
 
Example #9
Source File: LexState.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
void forbody(int base, int line, int nvars, boolean isnum) {
/* forbody -> DO block */
      BlockCnt bl = new BlockCnt();
      FuncState fs = this.fs;
      int prep, endfor;
      this.adjustlocalvars(3); /* control variables */
      this.checknext(TK_DO);
      prep = isnum ? fs.codeAsBx(Lua.OP_FORPREP, base, NO_JUMP) : fs.jump();
      fs.enterblock(bl, false); /* scope for declared variables */
      this.adjustlocalvars(nvars);
      fs.reserveregs(nvars);
      this.block();
      fs.leaveblock(); /* end of scope for declared variables */
      fs.patchtohere(prep);
      if (isnum)  /* numeric for? */
          endfor = fs.codeAsBx(Lua.OP_FORLOOP, base, NO_JUMP);
      else {  /* generic for */
          fs.codeABC(Lua.OP_TFORCALL, base, 0, nvars);
          fs.fixline(line);
          endfor = fs.codeAsBx(Lua.OP_TFORLOOP, base + 2, NO_JUMP);
      }
      fs.patchlist(endfor, prep + 1);
      fs.fixline(line);
  }
 
Example #10
Source File: LexState.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
void check_conflict(LHS_assign lh, expdesc v) {
    FuncState fs = this.fs;
    short extra = (short) fs.freereg;  /* eventual position to save local variable */
    boolean conflict = false;
    for (; lh != null; lh = lh.prev) {
        if (lh.v.k == VINDEXED) {
/* table is the upvalue/local being assigned now? */
            if (lh.v.u.ind_vt == v.k && lh.v.u.ind_t == v.u.info) {
                conflict = true;
                lh.v.u.ind_vt = VLOCAL;
                lh.v.u.ind_t = extra;  /* previous assignment will use safe copy */
            }
/* index is the local being assigned? (index cannot be upvalue) */
            if (v.k == VLOCAL && lh.v.u.ind_idx == v.u.info) {
                conflict = true;
                lh.v.u.ind_idx = extra;  /* previous assignment will use safe copy */
            }
        }
    }
    if (conflict) {
  /* copy upvalue/local value to a temporary (in position 'extra') */
        int op = (v.k == VLOCAL) ? Lua.OP_MOVE : Lua.OP_GETUPVAL;
        fs.codeABC(op, extra, v.u.info, 0);
        fs.reserveregs(1);
    }
}
 
Example #11
Source File: LexState.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
void forbody(int base, int line, int nvars, boolean isnum) {
	/* forbody -> DO block */
	BlockCnt bl = new BlockCnt();
	FuncState fs = this.fs;
	int prep, endfor;
	this.adjustlocalvars(3); /* control variables */
	this.checknext(TK_DO);
	prep = isnum ? fs.codeAsBx(Lua.OP_FORPREP, base, NO_JUMP) : fs.jump();
	fs.enterblock(bl, false); /* scope for declared variables */
	this.adjustlocalvars(nvars);
	fs.reserveregs(nvars);
	this.block();
	fs.leaveblock(); /* end of scope for declared variables */
	fs.patchtohere(prep);
	if (isnum)  /* numeric for? */
		endfor = fs.codeAsBx(Lua.OP_FORLOOP, base, NO_JUMP);
	else {  /* generic for */
		fs.codeABC(Lua.OP_TFORCALL, base, 0, nvars);
		fs.fixline(line);
		endfor = fs.codeAsBx(Lua.OP_TFORLOOP, base + 2, NO_JUMP);
	}
	fs.patchlist(endfor, prep + 1);
	fs.fixline(line);
}
 
Example #12
Source File: LexState.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
void recfield(ConsControl cc) {
	/* recfield -> (NAME | `['exp1`]') = exp1 */
	FuncState fs = this.fs;
	int reg = this.fs.freereg;
	expdesc key = new expdesc();
	expdesc val = new expdesc();
	int rkkey;
	if (this.t.token == TK_NAME) {
		fs.checklimit(cc.nh, MAX_INT, "items in a constructor");
		this.checkname(key);
	} else
		/* this.t.token == '[' */
		this.yindex(key);
	cc.nh++;
	this.checknext('=');
	rkkey = fs.exp2RK(key);
	this.expr(val);
	fs.codeABC(Lua.OP_SETTABLE, cc.t.u.info, rkkey, fs.exp2RK(val));
	fs.freereg = (short)reg; /* free registers */
}
 
Example #13
Source File: LexState.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
void recfield(ConsControl cc) {
/* recfield -> (NAME | `['exp1`]') = exp1 */
      FuncState fs = this.fs;
      int reg = this.fs.freereg;
      expdesc key = new expdesc();
      expdesc val = new expdesc();
      int rkkey;
      if (this.t.token == TK_NAME) {
          fs.checklimit(cc.nh, MAX_INT, "items in a constructor");
          this.checkname(key);
      } else
	/* this.t.token == '[' */
          this.yindex(key);
      cc.nh++;
      this.checknext('=');
      rkkey = fs.exp2RK(key);
      this.expr(val);
      fs.codeABC(Lua.OP_SETTABLE, cc.t.u.info, rkkey, fs.exp2RK(val));
      fs.freereg = (short) reg; /* free registers */
  }
 
Example #14
Source File: LexState.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
void check_conflict (LHS_assign lh, expdesc v) {
	FuncState fs = this.fs;
	short extra = (short) fs.freereg;  /* eventual position to save local variable */
	boolean conflict = false;
	for (; lh!=null; lh = lh.prev) {
		if (lh.v.k == VINDEXED) {
			/* table is the upvalue/local being assigned now? */
			if (lh.v.u.ind_vt == v.k && lh.v.u.ind_t == v.u.info) {
				conflict = true;
				lh.v.u.ind_vt = VLOCAL;
				lh.v.u.ind_t = extra;  /* previous assignment will use safe copy */
			}
			/* index is the local being assigned? (index cannot be upvalue) */
			if (v.k == VLOCAL && lh.v.u.ind_idx == v.u.info) {
				conflict = true;
				lh.v.u.ind_idx = extra;  /* previous assignment will use safe copy */
			}
		}
	}
	if (conflict) {
	    /* copy upvalue/local value to a temporary (in position 'extra') */
	    int op = (v.k == VLOCAL) ? Lua.OP_MOVE : Lua.OP_GETUPVAL;
	    fs.codeABC(op, extra, v.u.info, 0);
	    fs.reserveregs(1);
	}
}
 
Example #15
Source File: LexState.java    From luaj with MIT License 6 votes vote down vote up
void recfield(ConsControl cc) {
	/* recfield -> (NAME | `['exp1`]') = exp1 */
	FuncState fs = this.fs;
	int reg = this.fs.freereg;
	expdesc key = new expdesc();
	expdesc val = new expdesc();
	int rkkey;
	if (this.t.token == TK_NAME) {
		fs.checklimit(cc.nh, MAX_INT, "items in a constructor");
		this.checkname(key);
	} else
		/* this.t.token == '[' */
		this.yindex(key);
	cc.nh++;
	this.checknext('=');
	rkkey = fs.exp2RK(key);
	this.expr(val);
	fs.codeABC(Lua.OP_SETTABLE, cc.t.u.info, rkkey, fs.exp2RK(val));
	fs.freereg = (short)reg; /* free registers */
}
 
Example #16
Source File: LexState.java    From luaj with MIT License 6 votes vote down vote up
void check_conflict (LHS_assign lh, expdesc v) {
	FuncState fs = this.fs;
	short extra = (short) fs.freereg;  /* eventual position to save local variable */
	boolean conflict = false;
	for (; lh!=null; lh = lh.prev) {
		if (lh.v.k == VINDEXED) {
			/* table is the upvalue/local being assigned now? */
			if (lh.v.u.ind_vt == v.k && lh.v.u.ind_t == v.u.info) {
				conflict = true;
				lh.v.u.ind_vt = VLOCAL;
				lh.v.u.ind_t = extra;  /* previous assignment will use safe copy */
			}
			/* index is the local being assigned? (index cannot be upvalue) */
			if (v.k == VLOCAL && lh.v.u.ind_idx == v.u.info) {
				conflict = true;
				lh.v.u.ind_idx = extra;  /* previous assignment will use safe copy */
			}
		}
	}
	if (conflict) {
	    /* copy upvalue/local value to a temporary (in position 'extra') */
	    int op = (v.k == VLOCAL) ? Lua.OP_MOVE : Lua.OP_GETUPVAL;
	    fs.codeABC(op, extra, v.u.info, 0);
	    fs.reserveregs(1);
	}
}
 
Example #17
Source File: LexState.java    From luaj with MIT License 6 votes vote down vote up
void forbody(int base, int line, int nvars, boolean isnum) {
	/* forbody -> DO block */
	BlockCnt bl = new BlockCnt();
	FuncState fs = this.fs;
	int prep, endfor;
	this.adjustlocalvars(3); /* control variables */
	this.checknext(TK_DO);
	prep = isnum ? fs.codeAsBx(Lua.OP_FORPREP, base, NO_JUMP) : fs.jump();
	fs.enterblock(bl, false); /* scope for declared variables */
	this.adjustlocalvars(nvars);
	fs.reserveregs(nvars);
	this.block();
	fs.leaveblock(); /* end of scope for declared variables */
	fs.patchtohere(prep);
	if (isnum)  /* numeric for? */
		endfor = fs.codeAsBx(Lua.OP_FORLOOP, base, NO_JUMP);
	else {  /* generic for */
		fs.codeABC(Lua.OP_TFORCALL, base, 0, nvars);
		fs.fixline(line);
		endfor = fs.codeAsBx(Lua.OP_TFORLOOP, base + 2, NO_JUMP);
	}
	fs.patchlist(endfor, prep + 1);
	fs.fixline(line);
}
 
Example #18
Source File: FuncState.java    From luaj with MIT License 5 votes vote down vote up
boolean patchtestreg(int node, int reg) {
	InstructionPtr i = this.getjumpcontrol(node);
	if (GET_OPCODE(i.get()) != OP_TESTSET)
		/* cannot patch other instructions */
		return false;
	if (reg != NO_REG && reg != GETARG_B(i.get()))
		SETARG_A(i, reg);
	else
		/* no register to put value or register already has the value */
		i.set(CREATE_ABC(OP_TEST, GETARG_B(i.get()), 0, Lua.GETARG_C(i.get())));

	return true;
}
 
Example #19
Source File: DebugLib.java    From luaj with MIT License 5 votes vote down vote up
static NameWhat getfuncname(DebugLib.CallFrame frame) {
	if (!frame.f.isclosure())
		return new NameWhat(frame.f.classnamestub(), "Java");
	Prototype p = frame.f.checkclosure().p;
	int pc = frame.pc;
	int i = p.code[pc]; /* calling instruction */
	LuaString tm;
	switch (Lua.GET_OPCODE(i)) {
		case Lua.OP_CALL:
		case Lua.OP_TAILCALL: /* get function name */
			return getobjname(p, pc, Lua.GETARG_A(i));
		case Lua.OP_TFORCALL: /* for iterator */
	    	return new NameWhat("(for iterator)", "(for iterator");
	    /* all other instructions can call only through metamethods */
	    case Lua.OP_SELF:
	    case Lua.OP_GETTABUP:
	    case Lua.OP_GETTABLE: tm = LuaValue.INDEX; break;
	    case Lua.OP_SETTABUP:
	    case Lua.OP_SETTABLE: tm = LuaValue.NEWINDEX; break;
	    case Lua.OP_EQ: tm = LuaValue.EQ; break;
	    case Lua.OP_ADD: tm = LuaValue.ADD; break;
	    case Lua.OP_SUB: tm = LuaValue.SUB; break;
	    case Lua.OP_MUL: tm = LuaValue.MUL; break;
	    case Lua.OP_DIV: tm = LuaValue.DIV; break;
	    case Lua.OP_MOD: tm = LuaValue.MOD; break;
	    case Lua.OP_POW: tm = LuaValue.POW; break;
	    case Lua.OP_UNM: tm = LuaValue.UNM; break;
	    case Lua.OP_LEN: tm = LuaValue.LEN; break;
	    case Lua.OP_LT: tm = LuaValue.LT; break;
	    case Lua.OP_LE: tm = LuaValue.LE; break;
	    case Lua.OP_CONCAT: tm = LuaValue.CONCAT; break;
	    default:
	      return null;  /* else no useful name can be found */
	}
	return new NameWhat( tm.tojstring(), "metamethod" );
}
 
Example #20
Source File: LexState.java    From luaj with MIT License 5 votes vote down vote up
void lexerror( String msg, int token ) {
	String cid = Lua.chunkid( source.tojstring() );
	L.pushfstring( cid+":"+linenumber+": "+msg );
	if ( token != 0 )
		L.pushfstring( "syntax error: "+msg+" near "+txtToken(token) );
	throw new LuaError(cid+":"+linenumber+": "+msg);
}
 
Example #21
Source File: BaseLib.java    From luaj with MIT License 5 votes vote down vote up
/** Perform one-time initialization on the library by adding base functions
 * to the supplied environment, and returning it as the return value.
 * @param modname the module name supplied if this is loaded via 'require'.
 * @param env the environment to load into, which must be a Globals instance.
 */
public LuaValue call(LuaValue modname, LuaValue env) {
	globals = env.checkglobals();
	globals.finder = this;
	globals.baselib = this;
	env.set( "_G", env );
	env.set( "_VERSION", Lua._VERSION );
	env.set("assert", new _assert());
	env.set("collectgarbage", new collectgarbage());
	env.set("dofile", new dofile());
	env.set("error", new error());
	env.set("getmetatable", new getmetatable());
	env.set("load", new load());
	env.set("loadfile", new loadfile());
	env.set("pcall", new pcall());
	env.set("print", new print(this));
	env.set("rawequal", new rawequal());
	env.set("rawget", new rawget());
	env.set("rawlen", new rawlen());
	env.set("rawset", new rawset());
	env.set("select", new select());
	env.set("setmetatable", new setmetatable());
	env.set("tonumber", new tonumber());
	env.set("tostring", new tostring());
	env.set("type", new type());
	env.set("xpcall", new xpcall());

	next next;
	env.set("next", next = new next());
	env.set("pairs", new pairs(next));
	env.set("ipairs", new ipairs());
	
	return env;
}
 
Example #22
Source File: DebugLib.java    From luaj with MIT License 5 votes vote down vote up
static String kname(Prototype p, int pc, int c) {
	if (Lua.ISK(c)) {  /* is 'c' a constant? */
		LuaValue k = p.k[Lua.INDEXK(c)];
		if (k.isstring()) {  /* literal constant? */
			return k.tojstring();  /* it is its own name */
		} /* else no reasonable name found */
	} else {  /* 'c' is a register */
		NameWhat what = getobjname(p, pc, c); /* search for 'c' */
	    if (what != null && "constant".equals(what.namewhat)) {  /* found a constant name? */
	      return what.name;  /* 'name' already filled */
	    }
	    /* else no reasonable name found */
	}
	return "?";  /* no reasonable name found */
}
 
Example #23
Source File: LexState.java    From luaj with MIT License 5 votes vote down vote up
void retstat() {
	/* stat -> RETURN explist */
	FuncState fs = this.fs;
	expdesc e = new expdesc();
	int first, nret; /* registers with returned values */
	if (block_follow(true) || this.t.token == ';')
		first = nret = 0; /* return no values */
	else {
		nret = this.explist(e); /* optional return values */
		if (hasmultret(e.k)) {
			fs.setmultret(e);
			if (e.k == VCALL && nret == 1) { /* tail call? */
				SET_OPCODE(fs.getcodePtr(e), Lua.OP_TAILCALL);
				_assert (Lua.GETARG_A(fs.getcode(e)) == fs.nactvar);
			}
			first = fs.nactvar;
			nret = Lua.LUA_MULTRET; /* return all values */
		} else {
			if (nret == 1) /* only one single value? */
				first = fs.exp2anyreg(e);
			else {
				fs.exp2nextreg(e); /* values must go to the `stack' */
				first = fs.nactvar; /* return all `active' values */
				_assert (nret == fs.freereg - first);
			}
		}
	}
	fs.ret(first, nret);
	testnext(';');  /* skip optional semicolon */
}
 
Example #24
Source File: JavaBuilder.java    From luaj with MIT License 5 votes vote down vote up
public void binaryop(int o) {
	String op;
	switch (o) {
		default: 
		case Lua.OP_ADD: op = "add"; break;
		case Lua.OP_SUB: op = "sub"; break;
		case Lua.OP_MUL: op = "mul"; break;
		case Lua.OP_DIV: op = "div"; break;
		case Lua.OP_MOD: op = "mod"; break;
		case Lua.OP_POW: op = "pow"; break;
	}
       append(factory.createInvoke(STR_LUAVALUE, op, TYPE_LUAVALUE, ARG_TYPES_LUAVALUE, Constants.INVOKEVIRTUAL));
}
 
Example #25
Source File: FuncState.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
void invertjump(expdesc e) {
	InstructionPtr pc = this.getjumpcontrol(e.u.info);
	_assert (testTMode(GET_OPCODE(pc.get()))
			&& GET_OPCODE(pc.get()) != OP_TESTSET && Lua
			.GET_OPCODE(pc.get()) != OP_TEST);
	// SETARG_A(pc, !(GETARG_A(pc.get())));
	int a = GETARG_A(pc.get());
	int nota = (a!=0? 0: 1);
	SETARG_A(pc, nota);
}
 
Example #26
Source File: FuncState.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
boolean patchtestreg(int node, int reg) {
	InstructionPtr i = this.getjumpcontrol(node);
	if (GET_OPCODE(i.get()) != OP_TESTSET)
		/* cannot patch other instructions */
		return false;
	if (reg != NO_REG && reg != GETARG_B(i.get()))
		SETARG_A(i, reg);
	else
		/* no register to put value or register already has the value */
		i.set(CREATE_ABC(OP_TEST, GETARG_B(i.get()), 0, Lua.GETARG_C(i.get())));

	return true;
}
 
Example #27
Source File: LexState.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
void retstat() {
	/* stat -> RETURN explist */
	FuncState fs = this.fs;
	expdesc e = new expdesc();
	int first, nret; /* registers with returned values */
	if (block_follow(true) || this.t.token == ';')
		first = nret = 0; /* return no values */
	else {
		nret = this.explist(e); /* optional return values */
		if (hasmultret(e.k)) {
			fs.setmultret(e);
			if (e.k == VCALL && nret == 1) { /* tail call? */
				SET_OPCODE(fs.getcodePtr(e), Lua.OP_TAILCALL);
				_assert (Lua.GETARG_A(fs.getcode(e)) == fs.nactvar);
			}
			first = fs.nactvar;
			nret = Lua.LUA_MULTRET; /* return all values */
		} else {
			if (nret == 1) /* only one single value? */
				first = fs.exp2anyreg(e);
			else {
				fs.exp2nextreg(e); /* values must go to the `stack' */
				first = fs.nactvar; /* return all `active' values */
				_assert (nret == fs.freereg - first);
			}
		}
	}
	fs.ret(first, nret);
	testnext(';');  /* skip optional semicolon */
}
 
Example #28
Source File: DebugLib.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
static NameWhat getfuncname(DebugLib.CallFrame frame) {
	if (!frame.f.isclosure())
		return new NameWhat(frame.f.classnamestub(), "Java");
	Prototype p = frame.f.checkclosure().p;
	int pc = frame.pc;
	int i = p.code[pc]; /* calling instruction */
	LuaString tm;
	switch (Lua.GET_OPCODE(i)) {
		case Lua.OP_CALL:
		case Lua.OP_TAILCALL: /* get function name */
			return getobjname(p, pc, Lua.GETARG_A(i));
		case Lua.OP_TFORCALL: /* for iterator */
	    	return new NameWhat("(for iterator)", "(for iterator");
	    /* all other instructions can call only through metamethods */
	    case Lua.OP_SELF:
	    case Lua.OP_GETTABUP:
	    case Lua.OP_GETTABLE: tm = LuaValue.INDEX; break;
	    case Lua.OP_SETTABUP:
	    case Lua.OP_SETTABLE: tm = LuaValue.NEWINDEX; break;
	    case Lua.OP_EQ: tm = LuaValue.EQ; break;
	    case Lua.OP_ADD: tm = LuaValue.ADD; break;
	    case Lua.OP_SUB: tm = LuaValue.SUB; break;
	    case Lua.OP_MUL: tm = LuaValue.MUL; break;
	    case Lua.OP_DIV: tm = LuaValue.DIV; break;
	    case Lua.OP_MOD: tm = LuaValue.MOD; break;
	    case Lua.OP_POW: tm = LuaValue.POW; break;
	    case Lua.OP_UNM: tm = LuaValue.UNM; break;
	    case Lua.OP_LEN: tm = LuaValue.LEN; break;
	    case Lua.OP_LT: tm = LuaValue.LT; break;
	    case Lua.OP_LE: tm = LuaValue.LE; break;
	    case Lua.OP_CONCAT: tm = LuaValue.CONCAT; break;
	    default:
	      return null;  /* else no useful name can be found */
	}
	return new NameWhat( tm.tojstring(), "metamethod" );
}
 
Example #29
Source File: BaseLib.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
/**
 * Perform one-time initialization on the library by adding base functions
 * to the supplied environment, and returning it as the return value.
 *
 * @param modname the module name supplied if this is loaded via 'require'.
 * @param env     the environment to load into, which must be a Globals instance.
 */
public LuaValue call(LuaValue modname, LuaValue env) {
    globals = env.checkglobals();
    globals.finder = this;
    globals.baselib = this;
    env.set("_G", env);
    env.set("_VERSION", Lua._VERSION);
    env.set("assert", new _assert());
    env.set("collectgarbage", new collectgarbage());
    env.set("dofile", new dofile());
    env.set("error", new error());
    env.set("getmetatable", new getmetatable());
    env.set("load", new load());
    env.set("loadfile", new loadfile());
    env.set("pcall", new pcall());
    env.set("print", new print(this));
    env.set("rawequal", new rawequal());
    env.set("rawget", new rawget());
    env.set("rawlen", new rawlen());
    env.set("rawset", new rawset());
    env.set("select", new select());
    env.set("setmetatable", new setmetatable());
    env.set("tonumber", new tonumber());
    env.set("tostring", new tostring());
    env.set("type", new type());
    env.set("xpcall", new xpcall());

    next next;
    env.set("next", next = new next());
    env.set("pairs", new pairs(next));
    env.set("ipairs", new ipairs());

    return env;
}
 
Example #30
Source File: LexState.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
void lexerror( String msg, int token ) {
	String cid = Lua.chunkid( source.tojstring() );
	L.pushfstring( cid+":"+linenumber+": "+msg );
	if ( token != 0 )
		L.pushfstring( "syntax error: "+msg+" near "+txtToken(token) );
	throw new LuaError(cid+":"+linenumber+": "+msg);
}