Refactor luaGetTable() to return a value.
This commit is contained in:
@@ -195,8 +195,8 @@ public class PackageLib extends LFunction {
|
|||||||
|
|
||||||
|
|
||||||
/* check whether table already has a _NAME field */
|
/* check whether table already has a _NAME field */
|
||||||
module.luaGetTable(vm, module, _NAME);
|
LValue name = module.luaGetTable(vm, module, _NAME);
|
||||||
if ( vm.isnil(-1) ) {
|
if ( name.isNil() ) {
|
||||||
modinit( vm, module, modname );
|
modinit( vm, module, modname );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -289,9 +289,10 @@ public class PackageLib extends LFunction {
|
|||||||
vm.settop(0);
|
vm.settop(0);
|
||||||
|
|
||||||
/* else must load it; iterate over available loaders */
|
/* else must load it; iterate over available loaders */
|
||||||
pckg.luaGetTable(vm, pckg, _LOADERS);
|
LValue val = pckg.luaGetTable(vm, pckg, _LOADERS);
|
||||||
if ( ! vm.istable(1) )
|
if ( ! val.isTable() )
|
||||||
vm.error( "'package.loaders' must be a table" );
|
vm.error( "'package.loaders' must be a table" );
|
||||||
|
vm.pushlvalue(val);
|
||||||
Vector v = new Vector();
|
Vector v = new Vector();
|
||||||
for ( int i=1; true; i++ ) {
|
for ( int i=1; true; i++ ) {
|
||||||
vm.rawgeti(1, i);
|
vm.rawgeti(1, i);
|
||||||
@@ -330,15 +331,14 @@ public class PackageLib extends LFunction {
|
|||||||
|
|
||||||
private void loader_preload( LuaState vm ) {
|
private void loader_preload( LuaState vm ) {
|
||||||
LString name = vm.tolstring(2);
|
LString name = vm.tolstring(2);
|
||||||
pckg.luaGetTable(vm, pckg, _PRELOAD);
|
LValue preload = pckg.luaGetTable(vm, pckg, _PRELOAD);
|
||||||
if ( ! vm.istable(-1) )
|
if ( ! preload.isTable() )
|
||||||
vm.error("package.preload '"+name+"' must be a table");
|
vm.error("package.preload '"+name+"' must be a table");
|
||||||
LTable preload = vm.totable(-1);
|
LValue val = preload.luaGetTable(vm, preload, name);
|
||||||
preload.luaGetTable(vm, preload, name);
|
if ( val.isNil() )
|
||||||
if ( vm.isnil(-1) )
|
|
||||||
vm.pushstring("\n\tno field package.preload['"+name+"']");
|
vm.pushstring("\n\tno field package.preload['"+name+"']");
|
||||||
vm.insert(1);
|
vm.resettop();
|
||||||
vm.settop(1);
|
vm.pushlvalue(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loader_Lua( LuaState vm ) {
|
private void loader_Lua( LuaState vm ) {
|
||||||
@@ -372,10 +372,10 @@ public class PackageLib extends LFunction {
|
|||||||
|
|
||||||
private InputStream findfile(LuaState vm, String name, LString pname) {
|
private InputStream findfile(LuaState vm, String name, LString pname) {
|
||||||
Platform p = Platform.getInstance();
|
Platform p = Platform.getInstance();
|
||||||
pckg.luaGetTable(vm, pckg, pname);
|
LValue pkg = pckg.luaGetTable(vm, pckg, pname);
|
||||||
if ( ! vm.isstring(-1) )
|
if ( ! pkg.isString() )
|
||||||
vm.error("package."+pname+" must be a string");
|
vm.error("package."+pname+" must be a string");
|
||||||
String path = vm.tostring(-1);
|
String path = pkg.toJavaString();
|
||||||
int e = -1;
|
int e = -1;
|
||||||
int n = path.length();
|
int n = path.length();
|
||||||
StringBuffer sb = null;
|
StringBuffer sb = null;
|
||||||
|
|||||||
@@ -667,7 +667,7 @@ public class StringLib extends LFunction {
|
|||||||
push_onecapture( 0, soffset, end );
|
push_onecapture( 0, soffset, end );
|
||||||
LValue k = vm.topointer( -1 );
|
LValue k = vm.topointer( -1 );
|
||||||
vm.pop( 1 );
|
vm.pop( 1 );
|
||||||
((LTable) repl).luaGetTable( vm, repl, k );
|
vm.pushlvalue( ((LTable) repl).luaGetTable( vm, repl, k ) );
|
||||||
} else {
|
} else {
|
||||||
vm.error( "string/function/table expected" );
|
vm.error( "string/function/table expected" );
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -37,11 +37,12 @@ public class LFunction extends LValue {
|
|||||||
vm.call( 3, 0 );
|
vm.call( 3, 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void luaGetTable(LuaState vm, LValue table, LValue key) {
|
public LValue luaGetTable(LuaState vm, LValue table, LValue key) {
|
||||||
vm.pushlvalue( this );
|
vm.pushlvalue( this );
|
||||||
vm.pushlvalue( table );
|
vm.pushlvalue( table );
|
||||||
vm.pushlvalue( key );
|
vm.pushlvalue( key );
|
||||||
vm.call( 2, 1 );
|
vm.call( 2, 1 );
|
||||||
|
return vm.poplvalue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int luaGetType() {
|
public int luaGetType() {
|
||||||
|
|||||||
@@ -163,7 +163,11 @@ public class LString extends LValue {
|
|||||||
public static LString newStringNoCopy(byte[] buf, int off, int len) {
|
public static LString newStringNoCopy(byte[] buf, int off, int len) {
|
||||||
return new LString( buf, off, len );
|
return new LString( buf, off, len );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isString() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if ( o != null && o instanceof LString ) {
|
if ( o != null && o instanceof LString ) {
|
||||||
LString s = (LString) o;
|
LString s = (LString) o;
|
||||||
|
|||||||
@@ -77,6 +77,10 @@ public class LTable extends LValue {
|
|||||||
hashValues = new Object[nhash];
|
hashValues = new Object[nhash];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isTable() {
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}
|
||||||
/** Get capacity of hash part */
|
/** Get capacity of hash part */
|
||||||
public int getArrayCapacity() {
|
public int getArrayCapacity() {
|
||||||
return array.length;
|
return array.length;
|
||||||
@@ -229,12 +233,12 @@ public class LTable extends LValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void luaGetTable(LuaState vm, LValue table, LValue key) {
|
public LValue luaGetTable(LuaState vm, LValue table, LValue key) {
|
||||||
LValue v = get(key);
|
LValue v = get(key);
|
||||||
if ( v.isNil() && m_metatable != null ) {
|
if ( v.isNil() && m_metatable != null ) {
|
||||||
super.luaGetTable( vm, table, key );
|
return super.luaGetTable( vm, table, key );
|
||||||
} else {
|
} else {
|
||||||
vm.pushlvalue(v);
|
return v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,8 +51,9 @@ public class LValue {
|
|||||||
throw new LuaErrorException( "attempt to compare "+typea+" with "+typeb );
|
throw new LuaErrorException( "attempt to compare "+typea+" with "+typeb );
|
||||||
}
|
}
|
||||||
|
|
||||||
private void indexError(LuaState vm, LValue nontable) {
|
private LValue indexError(LuaState vm, LValue nontable) {
|
||||||
vm.error( "attempt to index ? (a "+nontable.luaGetTypeName()+" value)", 1 );
|
vm.error( "attempt to index ? (a "+nontable.luaGetTypeName()+" value)", 1 );
|
||||||
|
return LNil.NIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String id() {
|
public String id() {
|
||||||
@@ -146,17 +147,17 @@ public class LValue {
|
|||||||
* @param vm the calling vm
|
* @param vm the calling vm
|
||||||
* @param table the table from which to get the value
|
* @param table the table from which to get the value
|
||||||
* @param key the key to look up
|
* @param key the key to look up
|
||||||
|
* @return TODO
|
||||||
*/
|
*/
|
||||||
public void luaGetTable(LuaState vm, LValue table, LValue key) {
|
public LValue luaGetTable(LuaState vm, LValue table, LValue key) {
|
||||||
LTable mt = luaGetMetatable();
|
LTable mt = luaGetMetatable();
|
||||||
if ( mt != null ) {
|
if ( mt != null ) {
|
||||||
LValue event = mt.get( TM_INDEX );
|
LValue event = mt.get( TM_INDEX );
|
||||||
if ( event != null && ! event.isNil() ) {
|
if ( event != null && ! event.isNil() ) {
|
||||||
event.luaGetTable( vm, table, key );
|
return event.luaGetTable( vm, table, key );
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
indexError( vm, table );
|
return indexError( vm, table );
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get the value as a LString
|
/** Get the value as a LString
|
||||||
@@ -329,4 +330,14 @@ public class LValue {
|
|||||||
public void luaConcatTo(ByteArrayOutputStream baos) {
|
public void luaConcatTo(ByteArrayOutputStream baos) {
|
||||||
throw new LuaErrorException( "attempt to concat ? (a "+luaGetTypeName()+" value)");
|
throw new LuaErrorException( "attempt to concat ? (a "+luaGetTypeName()+" value)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Return true if this is a LString */
|
||||||
|
public boolean isString() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Return true if this is a LTable */
|
||||||
|
public boolean isTable() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -558,17 +558,17 @@ public class LuaState extends Lua {
|
|||||||
case LuaState.OP_GETGLOBAL: {
|
case LuaState.OP_GETGLOBAL: {
|
||||||
b = LuaState.GETARG_Bx(i);
|
b = LuaState.GETARG_Bx(i);
|
||||||
key = k[b];
|
key = k[b];
|
||||||
table = cl.env;
|
table = cl.env;
|
||||||
top = base + a;
|
this.top = base + a;
|
||||||
table.luaGetTable(this, table, key);
|
this.stack[base+a] = table.luaGetTable(this, table, key);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case LuaState.OP_GETTABLE: {
|
case LuaState.OP_GETTABLE: {
|
||||||
b = LuaState.GETARG_B(i);
|
b = LuaState.GETARG_B(i);
|
||||||
key = GETARG_RKC(k, i);
|
key = GETARG_RKC(k, i);
|
||||||
table = this.stack[base + b];
|
table = this.stack[base + b];
|
||||||
top = base + a;
|
this.top = base + a;
|
||||||
table.luaGetTable(this, table, key);
|
this.stack[base+a] = table.luaGetTable(this, table, key);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case LuaState.OP_SETGLOBAL: {
|
case LuaState.OP_SETGLOBAL: {
|
||||||
@@ -576,6 +576,7 @@ public class LuaState extends Lua {
|
|||||||
key = k[b];
|
key = k[b];
|
||||||
val = this.stack[base + a];
|
val = this.stack[base + a];
|
||||||
table = cl.env;
|
table = cl.env;
|
||||||
|
this.top = base + a;
|
||||||
table.luaSetTable(this, table, key, val);
|
table.luaSetTable(this, table, key, val);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -588,6 +589,7 @@ public class LuaState extends Lua {
|
|||||||
key = GETARG_RKB(k, i);
|
key = GETARG_RKB(k, i);
|
||||||
val = GETARG_RKC(k, i);
|
val = GETARG_RKC(k, i);
|
||||||
table = this.stack[base + a];
|
table = this.stack[base + a];
|
||||||
|
this.top = base + a;
|
||||||
table.luaSetTable(this, table, key, val);
|
table.luaSetTable(this, table, key, val);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -600,8 +602,8 @@ public class LuaState extends Lua {
|
|||||||
case LuaState.OP_SELF: {
|
case LuaState.OP_SELF: {
|
||||||
rkb = GETARG_RKB(k, i);
|
rkb = GETARG_RKB(k, i);
|
||||||
rkc = GETARG_RKC(k, i);
|
rkc = GETARG_RKC(k, i);
|
||||||
top = base + a;
|
this.top = base + a;
|
||||||
rkb.luaGetTable(this, rkb, rkc);
|
this.stack[base + a] = rkb.luaGetTable(this, rkb, rkc);
|
||||||
this.stack[base + a + 1] = rkb;
|
this.stack[base + a + 1] = rkb;
|
||||||
// StkId rb = RB(i);
|
// StkId rb = RB(i);
|
||||||
// setobjs2s(L, ra+1, rb);
|
// setobjs2s(L, ra+1, rb);
|
||||||
@@ -1327,7 +1329,7 @@ public class LuaState extends Lua {
|
|||||||
*/
|
*/
|
||||||
public void getfield(int index, LString k) {
|
public void getfield(int index, LString k) {
|
||||||
LTable t = totable(index);
|
LTable t = totable(index);
|
||||||
t.luaGetTable(this, t, k);
|
pushlvalue( t.luaGetTable(this, t, k) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1344,7 +1346,7 @@ public class LuaState extends Lua {
|
|||||||
*/
|
*/
|
||||||
public void getglobal(String s) {
|
public void getglobal(String s) {
|
||||||
LTable t = this._G;
|
LTable t = this._G;
|
||||||
t.luaGetTable(this, t, new LString(s));
|
pushlvalue( t.luaGetTable(this, t, new LString(s)) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1385,7 +1387,7 @@ public class LuaState extends Lua {
|
|||||||
LValue k = poplvalue();
|
LValue k = poplvalue();
|
||||||
// todo: what if this triggers metatable ops
|
// todo: what if this triggers metatable ops
|
||||||
// pushlvalue( t.luaGetTable(this, t, k) );
|
// pushlvalue( t.luaGetTable(this, t, k) );
|
||||||
t.luaGetTable(this, t, k);
|
pushlvalue( t.luaGetTable(this, t, k) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1518,7 +1520,7 @@ public class LuaState extends Lua {
|
|||||||
* number (which is always convertible to a string), and 0 otherwise.
|
* number (which is always convertible to a string), and 0 otherwise.
|
||||||
*/
|
*/
|
||||||
public boolean isstring(int index) {
|
public boolean isstring(int index) {
|
||||||
return type(index) == Lua.LUA_TSTRING;
|
return topointer(index).isString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1529,7 +1531,7 @@ public class LuaState extends Lua {
|
|||||||
* 0 otherwise.
|
* 0 otherwise.
|
||||||
*/
|
*/
|
||||||
public boolean istable(int index) {
|
public boolean istable(int index) {
|
||||||
return type(index) == Lua.LUA_TTABLE;
|
return topointer(index).isTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -144,15 +144,14 @@ public final class LuajavaLib extends LFunction {
|
|||||||
super(o);
|
super(o);
|
||||||
this.clazz = clazz;
|
this.clazz = clazz;
|
||||||
}
|
}
|
||||||
public void luaGetTable(LuaState vm, LValue table, LValue key) {
|
public LValue luaGetTable(LuaState vm, LValue table, LValue key) {
|
||||||
final String s = key.toJavaString();
|
final String s = key.toJavaString();
|
||||||
try {
|
try {
|
||||||
Field f = clazz.getField(s);
|
Field f = clazz.getField(s);
|
||||||
Object o = f.get(m_instance);
|
Object o = f.get(m_instance);
|
||||||
LValue v = CoerceJavaToLua.coerce( o );
|
return CoerceJavaToLua.coerce( o );
|
||||||
vm.pushlvalue( v );
|
|
||||||
} catch (NoSuchFieldException nsfe) {
|
} catch (NoSuchFieldException nsfe) {
|
||||||
vm.pushlvalue( new LMethod(m_instance,clazz,s) );
|
return new LMethod(m_instance,clazz,s);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new LuaErrorException(e);
|
throw new LuaErrorException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
89
src/sample/org/luaj/sample/LuaJitRunner.java
Normal file
89
src/sample/org/luaj/sample/LuaJitRunner.java
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2007 LuaJ. All rights reserved.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
******************************************************************************/
|
||||||
|
package org.luaj.sample;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import org.luaj.jit.LuaJit;
|
||||||
|
import org.luaj.platform.J2sePlatform;
|
||||||
|
import org.luaj.vm.LClosure;
|
||||||
|
import org.luaj.vm.LPrototype;
|
||||||
|
import org.luaj.vm.LValue;
|
||||||
|
import org.luaj.vm.LoadState;
|
||||||
|
import org.luaj.vm.LuaErrorException;
|
||||||
|
import org.luaj.vm.LuaState;
|
||||||
|
import org.luaj.vm.Platform;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Program to run a lua chunk
|
||||||
|
*
|
||||||
|
* @author jim_roseborough
|
||||||
|
*/
|
||||||
|
public class LuaJitRunner {
|
||||||
|
|
||||||
|
public static void main( String[] args ) throws IOException {
|
||||||
|
|
||||||
|
// new lua state
|
||||||
|
Platform.setInstance(new J2sePlatform());
|
||||||
|
LuaState state = Platform.newLuaState();
|
||||||
|
LuaJit.install();
|
||||||
|
|
||||||
|
// get script name
|
||||||
|
for ( int i=0; i<args.length; i++ ) {
|
||||||
|
String script = args[i];
|
||||||
|
try {
|
||||||
|
System.out.println("loading '"+script+"'");
|
||||||
|
|
||||||
|
// load the file
|
||||||
|
InputStream is = null;
|
||||||
|
File f = new File(script);
|
||||||
|
if ( f.exists() )
|
||||||
|
is = new FileInputStream( f );
|
||||||
|
else
|
||||||
|
is = LuaJitRunner.class.getResourceAsStream( script );
|
||||||
|
if ( is == null )
|
||||||
|
throw new java.io.FileNotFoundException( "not found: "+script );
|
||||||
|
LPrototype p = LoadState.undump(state, is, script);
|
||||||
|
|
||||||
|
// create closure and execute
|
||||||
|
LClosure c = p.newClosure( state._G );
|
||||||
|
|
||||||
|
// do the call
|
||||||
|
state.pushlvalue(c);
|
||||||
|
state.call(0, 0);
|
||||||
|
|
||||||
|
} catch ( LuaErrorException lee ) {
|
||||||
|
System.err.println(script+" lua error, "+lee.getMessage() );
|
||||||
|
} catch ( Throwable t ) {
|
||||||
|
System.err.println(script+" threw "+t);
|
||||||
|
t.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
System.out.flush();
|
||||||
|
System.err.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -183,11 +183,11 @@ public class LuaScriptEngine extends LFunction implements ScriptEngine {
|
|||||||
b.put(k,v);
|
b.put(k,v);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void luaGetTable(LuaState vm, LValue table, LValue key) {
|
public LValue luaGetTable(LuaState vm, LValue table, LValue key) {
|
||||||
Bindings b = getBindings(ScriptContext.ENGINE_SCOPE);
|
Bindings b = getBindings(ScriptContext.ENGINE_SCOPE);
|
||||||
String k = key.toJavaString();
|
String k = key.toJavaString();
|
||||||
Object v = b.get(k);
|
Object v = b.get(k);
|
||||||
vm.pushlvalue((LValue)v);
|
return (LValue) v;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bindings getBindings(int scope) {
|
public Bindings getBindings(int scope) {
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
version: 0.33
|
version: 0.34
|
||||||
|
|||||||
Reference in New Issue
Block a user