org.luaj.vm2.Prototype Java Examples

The following examples show how to use org.luaj.vm2.Prototype. 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: LexState.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
void close_func() {
	FuncState fs = this.fs;
	Prototype f = fs.f;
	fs.ret(0, 0); /* final return */
	fs.leaveblock();
	f.code = realloc(f.code, fs.pc);
	f.lineinfo = realloc(f.lineinfo, fs.pc);
	f.k = realloc(f.k, fs.nk);
	f.p = realloc(f.p, fs.np);
	f.locvars = realloc(f.locvars, fs.nlocvars);
	f.upvalues = realloc(f.upvalues, fs.nups);
	_assert (fs.bl == null);
	this.fs = fs.prev;
	// last token read was anchored in defunct function; must reanchor it
	// ls.anchor_token();
}
 
Example #2
Source File: LuaPrint.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
void buildHeader(Prototype f) {
    String s = String.valueOf(f.source);
    if (s.startsWith("@") || s.startsWith("="))
        s = s.substring(1);
    else if ("\033Lua".equals(s))
        s = "(bstring)";
    else
        s = "(string)";
    String a = (f.linedefined == 0) ? "main" : "function";
    ps.append("\n%" + a + " <" + s + ":" + f.linedefined + ","
            + f.lastlinedefined + "> (" + f.code.length + " instructions, "
            + f.code.length * 4 + " bytes at " + id(f) + ")\n");
    ps.append(f.numparams + " param, " + f.maxstacksize + " slot, "
            + f.upvalues.length + " upvalue, ");
    ps.append(f.locvars.length + " local, " + f.k.length
            + " constant, " + f.p.length + " function\n");
}
 
Example #3
Source File: DumpState.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
void dumpDebug(final Prototype f) throws IOException {
	int i, n;
	if (strip)
		dumpInt(0);
	else
		dumpString(f.source);
	n = strip ? 0 : f.lineinfo.length;
	dumpInt(n);
	for (i = 0; i < n; i++)
		dumpInt(f.lineinfo[i]);
	n = strip ? 0 : f.locvars.length;
	dumpInt(n);
	for (i = 0; i < n; i++) {
		LocVars lvi = f.locvars[i];
		dumpString(lvi.varname);
		dumpInt(lvi.startpc);
		dumpInt(lvi.endpc);
	}
	n = strip ? 0 : f.upvalues.length;
	dumpInt(n);
	for (i = 0; i < n; i++)
		dumpString(f.upvalues[i].name);
}
 
Example #4
Source File: DebugLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
public void funcinfo(LuaFunction f) {
    if (f.isclosure()) {
        Prototype p = f.checkclosure().p;
        this.source = p.source != null ? p.source.tojstring() : "=?";
        this.linedefined = p.linedefined;
        this.lastlinedefined = p.lastlinedefined;
        this.what = (this.linedefined == 0) ? "main" : "Lua";
        this.short_src = p.shortsource();
    } else {
        this.source = "=[Java]";
        this.linedefined = -1;
        this.lastlinedefined = -1;
        this.what = "Java";
        this.short_src = f.name();
    }
}
 
Example #5
Source File: DumpState.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param f the function to dump
 * @param w the output stream to dump to
 * @param stripDebug true to strip debugging info, false otherwise
 * @param numberFormat one of NUMBER_FORMAT_FLOATS_OR_DOUBLES, NUMBER_FORMAT_INTS_ONLY, NUMBER_FORMAT_NUM_PATCH_INT32
 * @param littleendian true to use little endian for numbers, false for big endian
 * @return 0 if dump succeeds
 * @throws IOException
 * @throws IllegalArgumentException if the number format it not supported
 */
public static int dump(Prototype f, OutputStream w, boolean stripDebug, int numberFormat, boolean littleendian) throws IOException {
	switch ( numberFormat ) {
	case NUMBER_FORMAT_FLOATS_OR_DOUBLES:
	case NUMBER_FORMAT_INTS_ONLY:
	case NUMBER_FORMAT_NUM_PATCH_INT32:
		break;
	default:
		throw new IllegalArgumentException("number format not supported: "+numberFormat);
	}
	DumpState D = new DumpState(w,stripDebug);
	D.IS_LITTLE_ENDIAN = littleendian;
	D.NUMBER_FORMAT = numberFormat;
	D.SIZEOF_LUA_NUMBER = (numberFormat==NUMBER_FORMAT_INTS_ONLY? 4: 8);
	D.dumpHeader();
	D.dumpFunction(f);
	return D.status;
}
 
Example #6
Source File: LuaC.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
/** Parse the input */
private Prototype luaY_parser(InputStream z, String name) throws IOException{
	LexState lexstate = new LexState(this, z);
	FuncState funcstate = new FuncState();
	// lexstate.buff = buff;
	lexstate.fs = funcstate;
	lexstate.setinput(this, z.read(), z, (LuaString) LuaValue.valueOf(name) );
	/* main func. is always vararg */
	funcstate.f = new Prototype();
	funcstate.f.source = (LuaString) LuaValue.valueOf(name);
	lexstate.mainfunc(funcstate);
	LuaC._assert (funcstate.prev == null);
	/* all scopes should be correctly finished */
	LuaC._assert (lexstate.dyd == null 
			|| (lexstate.dyd.n_actvar == 0 && lexstate.dyd.n_gt == 0 && lexstate.dyd.n_label == 0));
	return funcstate.f;
}
 
Example #7
Source File: LexState.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
void close_func() {
    FuncState fs = this.fs;
    Prototype f = fs.f;
    fs.ret(0, 0); /*  return */
    fs.leaveblock();
    f.code = LuaC.realloc(f.code, fs.pc);
    f.lineinfo = LuaC.realloc(f.lineinfo, fs.pc);
    f.k = LuaC.realloc(f.k, fs.nk);
    f.p = LuaC.realloc(f.p, fs.np);
    f.locvars = LuaC.realloc(f.locvars, fs.nlocvars);
    f.upvalues = LuaC.realloc(f.upvalues, fs.nups);
    LuaC._assert(fs.bl == null);
    this.fs = fs.prev;
    // last token read was anchored in defunct function; must reanchor it
    // ls.anchor_token();
}
 
Example #8
Source File: DebugLib.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
public void funcinfo(LuaFunction f) {
	if (f.isclosure()) {
		Prototype p = f.checkclosure().p;
		this.source = p.source != null ? p.source.tojstring() : "=?";
		this.linedefined = p.linedefined;
		this.lastlinedefined = p.lastlinedefined;
		this.what = (this.linedefined == 0) ? "main" : "Lua";
		this.short_src = p.shortsource();
	} else {
		this.source = "=[Java]";
		this.linedefined = -1;
		this.lastlinedefined = -1;
		this.what = "Java";
		this.short_src = f.name();
	}
}
 
Example #9
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 #10
Source File: ProtoInfo.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
private ProtoInfo(Prototype p, String name, UpvalInfo[] u) {
	this.name = name;
	this.prototype = p;
	this.upvals = u != null? u: new UpvalInfo[] { new UpvalInfo(this) };
	this.subprotos = p.p!=null&&p.p.length>0? new ProtoInfo[p.p.length]: null;
	
	// find basic blocks
	this.blocks = BasicBlock.findBasicBlocks(p);
	this.blocklist = BasicBlock.findLiveBlocks(blocks);
	
	// params are inputs to first block
	this.params = new VarInfo[p.maxstacksize];
	for ( int slot=0; slot<p.maxstacksize; slot++ ) {
		VarInfo v = VarInfo.PARAM(slot);
		params[slot] = v;
	}
	
	// find variables
	this.vars = findVariables();
	replaceTrivialPhiVariables();

	// find upvalues, create sub-prototypes
	this.openups = new UpvalInfo[p.maxstacksize][];
	findUpvalues();
}
 
Example #11
Source File: LuaC.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
/** Parse the input */
private Prototype luaY_parser(InputStream z, String name) throws IOException{
	LexState lexstate = new LexState(this, z);
	FuncState funcstate = new FuncState();
	// lexstate.buff = buff;
	lexstate.fs = funcstate;
	lexstate.setinput(this, z.read(), z, (LuaString) LuaValue.valueOf(name) );
	/* main func. is always vararg */
	funcstate.f = new Prototype();
	funcstate.f.source = (LuaString) LuaValue.valueOf(name);
	lexstate.mainfunc(funcstate);
	LuaC._assert (funcstate.prev == null);
	/* all scopes should be correctly finished */
	LuaC._assert (lexstate.dyd == null 
			|| (lexstate.dyd.n_actvar == 0 && lexstate.dyd.n_gt == 0 && lexstate.dyd.n_label == 0));
	return funcstate.f;
}
 
Example #12
Source File: DumpState.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
void dumpDebug( Prototype f) throws IOException {
	int i, n;
	if (strip)
		dumpInt(0);
	else
		dumpString(f.source);
	n = strip ? 0 : f.lineinfo.length;
	dumpInt(n);
	for (i = 0; i < n; i++)
		dumpInt(f.lineinfo[i]);
	n = strip ? 0 : f.locvars.length;
	dumpInt(n);
	for (i = 0; i < n; i++) {
		LocVars lvi = f.locvars[i];
		dumpString(lvi.varname);
		dumpInt(lvi.startpc);
		dumpInt(lvi.endpc);
	}
	n = strip ? 0 : f.upvalues.length;
	dumpInt(n);
	for (i = 0; i < n; i++)
		dumpString(f.upvalues[i].name);
}
 
Example #13
Source File: DumpState.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 
 * @param f the function to dump
 * @param w the output stream to dump to
 * @param stripDebug true to strip debugging info, false otherwise
 * @param numberFormat one of NUMBER_FORMAT_FLOATS_OR_DOUBLES, NUMBER_FORMAT_INTS_ONLY, NUMBER_FORMAT_NUM_PATCH_INT32
 * @param littleendian true to use little endian for numbers, false for big endian
 * @return 0 if dump succeeds
 * @throws IOException
 * @throws IllegalArgumentException if the number format it not supported
 */
public static int dump(Prototype f, OutputStream w, boolean stripDebug, int numberFormat, boolean littleendian) throws IOException {
	switch ( numberFormat ) {
	case NUMBER_FORMAT_FLOATS_OR_DOUBLES:
	case NUMBER_FORMAT_INTS_ONLY:
	case NUMBER_FORMAT_NUM_PATCH_INT32:
		break;
	default:
		throw new IllegalArgumentException("number format not supported: "+numberFormat);
	}
	DumpState D = new DumpState(w,stripDebug);
	D.IS_LITTLE_ENDIAN = littleendian;
	D.NUMBER_FORMAT = numberFormat;
	D.SIZEOF_LUA_NUMBER = (numberFormat==NUMBER_FORMAT_INTS_ONLY? 4: 8);
	D.dumpHeader();
	D.dumpFunction(f);
	return D.status;
}
 
Example #14
Source File: DumpState.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
void dumpDebug(final Prototype f) throws IOException {
	int i, n;
	if (strip)
		dumpInt(0);
	else
		dumpString(f.source);
	n = strip ? 0 : f.lineinfo.length;
	dumpInt(n);
	for (i = 0; i < n; i++)
		dumpInt(f.lineinfo[i]);
	n = strip ? 0 : f.locvars.length;
	dumpInt(n);
	for (i = 0; i < n; i++) {
		LocVars lvi = f.locvars[i];
		dumpString(lvi.varname);
		dumpInt(lvi.startpc);
		dumpInt(lvi.endpc);
	}
	n = strip ? 0 : f.upvalues.length;
	dumpInt(n);
	for (i = 0; i < n; i++)
		dumpString(f.upvalues[i].name);
}
 
Example #15
Source File: LuaC.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Parse the input
   */
  private Prototype luaY_parser(InputStream z, String name, boolean standardSyntax) throws IOException {
      LexState lexstate = new LexState(this, z, standardSyntax);
      FuncState funcstate = new FuncState();
      // lexstate.buff = buff;
      lexstate.fs = funcstate;
      lexstate.setinput(this, z.read(), z, (LuaString) LuaValue.valueOf(name));
      /* main func. is always vararg */
      funcstate.f = new Prototype();
      funcstate.f.source = (LuaString) LuaValue.valueOf(name);
      lexstate.mainfunc(funcstate);
      LuaC._assert(funcstate.prev == null);
/* all scopes should be correctly finished */
      LuaC._assert(lexstate.dyd == null
              || (lexstate.dyd.n_actvar == 0 && lexstate.dyd.n_gt == 0 && lexstate.dyd.n_label == 0));
      return funcstate.f;
  }
 
Example #16
Source File: DebugLib.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
public void funcinfo(LuaFunction f) {
	if (f.isclosure()) {
		Prototype p = f.checkclosure().p;
		this.source = p.source != null ? p.source.tojstring() : "=?";
		this.linedefined = p.linedefined;
		this.lastlinedefined = p.lastlinedefined;
		this.what = (this.linedefined == 0) ? "main" : "Lua";
		this.short_src = p.shortsource();
	} else {
		this.source = "=[Java]";
		this.linedefined = -1;
		this.lastlinedefined = -1;
		this.what = "Java";
		this.short_src = f.name();
	}
}
 
Example #17
Source File: FuncState.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
int code(int instruction, int line) {
	Prototype f = this.f;
	this.dischargejpc(); /* `pc' will change */
	/* put new instruction in code array */
	if (f.code == null || this.pc + 1 > f.code.length)
		f.code = LuaC.realloc(f.code, this.pc * 2 + 1);
	f.code[this.pc] = instruction;
	/* save corresponding line information */
	if (f.lineinfo == null || this.pc + 1 > f.lineinfo.length)
		f.lineinfo = LuaC.realloc(f.lineinfo,
				this.pc * 2 + 1);
	f.lineinfo[this.pc] = line;
	return this.pc++;
}
 
Example #18
Source File: LexState.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
void parlist () {
  /* parlist -> [ param { `,' param } ] */
  FuncState fs = this.fs;
  Prototype f = fs.f;
  int nparams = 0;
  f.is_vararg = 0;
  if (this.t.token != ')') {  /* is `parlist' not empty? */
    do {
      switch (this.t.token) {
        case TK_NAME: {  /* param . NAME */
          this.new_localvar(this.str_checkname());
          ++nparams;
          break;
        }
        case TK_DOTS: {  /* param . `...' */
          this.next();
          f.is_vararg = 1;
          break;
        }
        default: this.syntaxerror("<name> or " + LUA_QL("...") + " expected");
      }
    } while ((f.is_vararg==0) && this.testnext(','));
  }
  this.adjustlocalvars(nparams);
  f.numparams = fs.nactvar;
  fs.reserveregs(fs.nactvar);  /* reserve register for parameters */
}
 
Example #19
Source File: FuncState.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
int addk(LuaValue v) {
	if (this.h == null) {
		this.h = new Hashtable();
	} else if (this.h.containsKey(v)) {
		return ((Integer) h.get(v)).intValue();
	} 
	final int idx = this.nk;
	this.h.put(v, new Integer(idx));
	final Prototype f = this.f;
	if (f.k == null || nk + 1 >= f.k.length)
		f.k = realloc( f.k, nk*2 + 1 );
	f.k[this.nk++] = v;
	return idx;
}
 
Example #20
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 #21
Source File: LexState.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
int registerlocalvar(LuaString varname) {
	FuncState fs = this.fs;
	Prototype f = fs.f;
	if (f.locvars == null || fs.nlocvars + 1 > f.locvars.length)
		f.locvars = realloc( f.locvars, fs.nlocvars*2+1 );
	f.locvars[fs.nlocvars] = new LocVars(varname,0,0);
	return fs.nlocvars++;
}
 
Example #22
Source File: DumpState.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
void dumpUpvalues(final Prototype f) throws IOException {
	int n = f.upvalues.length;
	dumpInt(n);
	for (int i = 0; i < n; i++) {
		writer.writeByte(f.upvalues[i].instack ? 1 : 0);
		writer.writeByte(f.upvalues[i].idx);
	}
}
 
Example #23
Source File: DumpState.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
void dumpFunction(final Prototype f) throws IOException {
	dumpInt(f.linedefined);
	dumpInt(f.lastlinedefined);
	dumpChar(f.numparams);
	dumpChar(f.is_vararg);
	dumpChar(f.maxstacksize);
	dumpCode(f);
	dumpConstants(f);
	dumpUpvalues(f);
	dumpDebug(f);
}
 
Example #24
Source File: DumpState.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
void dumpCode( final Prototype f ) throws IOException {
	final int[] code = f.code;
	int n = code.length;
	dumpInt( n );
	for ( int i=0; i<n; i++ )
		dumpInt( code[i] );
}
 
Example #25
Source File: LuaTest.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Test
public void testCallStringResult() throws IOException {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final Prototype thePrototype = theGlobals.compilePrototype(new StringReader("function add(a,b) return 'hello' end"), "script");
    new LuaClosure(thePrototype, theGlobals).call();
    final Varargs theArguments = LuaValue.varargsOf(new LuaValue[] {
            LuaInteger.valueOf(100),
            LuaInteger.valueOf(200)
    });
    final LuaValue theFunction = theGlobals.get("add");
    final LuaValue theValue = (LuaValue) theFunction.invoke(theArguments);
    Assert.assertTrue(theValue.isstring());
    Assert.assertEquals("hello", theValue.tojstring());
}
 
Example #26
Source File: FuncState.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
int addk(LuaValue v) {
	if (this.h == null) {
		this.h = new Hashtable();
	} else if (this.h.containsKey(v)) {
		return ((Integer) h.get(v)).intValue();
	} 
	final int idx = this.nk;
	this.h.put(v, new Integer(idx));
	final Prototype f = this.f;
	if (f.k == null || nk + 1 >= f.k.length)
		f.k = realloc( f.k, nk*2 + 1 );
	f.k[this.nk++] = v;
	return idx;
}
 
Example #27
Source File: LexState.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
void parlist () {
  /* parlist -> [ param { `,' param } ] */
  FuncState fs = this.fs;
  Prototype f = fs.f;
  int nparams = 0;
  f.is_vararg = 0;
  if (this.t.token != ')') {  /* is `parlist' not empty? */
    do {
      switch (this.t.token) {
        case TK_NAME: {  /* param . NAME */
          this.new_localvar(this.str_checkname());
          ++nparams;
          break;
        }
        case TK_DOTS: {  /* param . `...' */
          this.next();
          f.is_vararg = 1;
          break;
        }
        default: this.syntaxerror("<name> or " + LUA_QL("...") + " expected");
      }
    } while ((f.is_vararg==0) && this.testnext(','));
  }
  this.adjustlocalvars(nparams);
  f.numparams = fs.nactvar;
  fs.reserveregs(fs.nactvar);  /* reserve register for parameters */
}
 
Example #28
Source File: LuaPrint.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Print the code in a prototype
 *
 * @param f the {@link Prototype}
 */
void buildCode(Prototype f) {
    int[] code = f.code;
    int pc, n = code.length;
    for (pc = 0; pc < n; pc++) {
        buildOpCode(f, pc);
        ps.append("\n");
    }
}
 
Example #29
Source File: LuaTest.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Test
public void testCall() throws IOException {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final Prototype thePrototype = theGlobals.compilePrototype(new StringReader("function add(a,b) return a + b end"), "script");
    new LuaClosure(thePrototype, theGlobals).call();
    final LuaValue theFunction = theGlobals.get("add");
    Assert.assertFalse(theFunction.isnil());
    final Varargs theArguments = LuaValue.varargsOf(new LuaValue[] {
            LuaInteger.valueOf(100),
            LuaInteger.valueOf(200)
    });
    final LuaInteger theResult = (LuaInteger) theFunction.invoke(theArguments);
    Assert.assertEquals("300", theResult.tojstring());
}
 
Example #30
Source File: LexState.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
Prototype addprototype () {
  Prototype clp;
  Prototype f = fs.f;  /* prototype of current function */
  if (f.p == null || fs.np >= f.p.length) {
    f.p = realloc(f.p, Math.max(1, fs.np * 2));
  }
  f.p[fs.np++] = clp = new Prototype();
  return clp;
}