Split libraries into pieces.

This commit is contained in:
James Roseborough
2007-11-09 00:15:56 +00:00
parent 4acaa11845
commit 2023ac3464
45 changed files with 3008 additions and 825 deletions

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.compiler;
import java.io.IOException;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.compiler;
import java.io.DataOutputStream;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.compiler;
import java.util.Hashtable;

View File

@@ -1,4 +1,24 @@
/*******************************************************************************
* 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.compiler;
class InstructionPtr {

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.compiler;
public class IntPtr {

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.compiler;
import java.io.IOException;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.compiler;
import org.luaj.vm.LString;

View File

@@ -0,0 +1,445 @@
/**
*
*/
package org.luaj.lib;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import org.luaj.vm.CallInfo;
import org.luaj.vm.LClosure;
import org.luaj.vm.LFunction;
import org.luaj.vm.LInteger;
import org.luaj.vm.LNil;
import org.luaj.vm.LNumber;
import org.luaj.vm.LString;
import org.luaj.vm.LTable;
import org.luaj.vm.LValue;
import org.luaj.vm.Lua;
import org.luaj.vm.LuaState;
import org.luaj.vm.Platform;
public class BaseLib extends LFunction {
public static InputStream STDIN = null;
public static PrintStream STDOUT = System.out;
private static PrintStream stdout = System.out;
private static final String[] NAMES = {
"base",
"print",
"pairs",
"getmetatable",
"setmetatable",
"type",
"pcall",
"ipairs",
"error",
"assert",
"loadfile",
"tonumber",
"rawget",
"rawset",
"setfenv",
"select",
"collectgarbage",
"dofile",
"loadstring",
"load",
"tostring",
"unpack",
"next",
};
private static final int INSTALL = 0;
private static final int PRINT = 1;
private static final int PAIRS = 2;
private static final int GETMETATABLE = 3;
private static final int SETMETATABLE = 4;
private static final int TYPE = 5;
private static final int PCALL = 6;
private static final int IPAIRS = 7;
private static final int ERROR = 8;
private static final int ASSERT = 9;
private static final int LOADFILE = 10;
private static final int TONUMBER = 11;
private static final int RAWGET = 12;
private static final int RAWSET = 13;
private static final int SETFENV = 14;
private static final int SELECT = 15;
private static final int COLLECTGARBAGE = 16;
private static final int DOFILE = 17;
private static final int LOADSTRING = 18;
private static final int LOAD = 19;
private static final int TOSTRING = 20;
private static final int UNPACK = 21;
private static final int NEXT = 22;
public static void install(LTable globals) {
for ( int i=1; i<NAMES.length; i++ )
globals.put( NAMES[i], new BaseLib(i) );
}
private int id;
private BaseLib( int id ) {
this.id = id;
}
public String toString() {
return NAMES[id]+"()";
}
private static void setResult( LuaState vm, LValue value ) {
vm.settop(0);
vm.pushlvalue( value );
}
private static void setErrorResult( LuaState vm, String message ) {
vm.settop(0);
vm.pushnil();
vm.pushstring( message );
}
public boolean luaStackCall(LuaState vm) {
switch ( id ) {
case PRINT: {
int n = vm.gettop();
for ( int i=2; i<=n; i++ ) {
if ( i > 2 )
stdout.print( "\t" );
stdout.print( vm.tostring(i) );
}
stdout.println();
vm.settop(0);
break;
}
case PAIRS:
case IPAIRS: {
LValue v = vm.topointer(2);
LValue r = v.luaPairs(id==PAIRS);
vm.settop(0);
vm.pushlvalue( r );
break;
}
case GETMETATABLE: {
int r = vm.getmetatable(2);
vm.settop(0);
vm.pushinteger(r);
break;
}
case SETMETATABLE: {
vm.setmetatable(2);
vm.remove(1);
break;
}
case TYPE: {
LValue v = vm.topointer(2);
vm.settop(0);
vm.pushlstring( v.luaGetTypeName() );
break;
}
case PCALL: {
int n = vm.gettop();
int s = vm.pcall( n-2, Lua.LUA_MULTRET, 0 );
if ( s == 0 ) { // success, results are on stack above the pcall
vm.remove( 1 );
vm.pushboolean( true );
vm.insert( 1 );
} else { // error, error message is on the stack
vm.pushboolean( false );
vm.insert( 1 );
}
break;
}
case ERROR: {
vm.error(vm.tostring(2), vm.gettop()>2? vm.tointeger(3): 1);
break;
}
case ASSERT: {
if ( ! vm.toboolean(2) )
vm.error( vm.gettop()>2? vm.tostring(3): "assertion failed!", 0 );
vm.remove(1);
break;
}
case LOADFILE:
loadfile(vm, vm.tostring(2));
break;
case TONUMBER: {
LValue input = vm.topointer(2);
vm.settop(0);
if ( input instanceof LNumber ) {
vm.pushlvalue(input);
} else if ( input instanceof LString ) {
int base = 10;
if ( vm.gettop()>=3 ) {
base = vm.tointeger(3);
}
vm.pushlvalue( ( (LString) input ).luaToNumber( base ) );
}
vm.pushnil();
break;
}
case RAWGET: {
LValue t = vm.topointer(2);
LValue k = vm.topointer(3);
vm.settop(0);
if ( t instanceof LTable ) {
vm.pushlvalue(( (LTable) t ).get( k ));
}
} break;
case RAWSET: {
LValue t = vm.topointer(2);
LValue k = vm.topointer(3);
LValue v = vm.topointer(4);
vm.settop(0);
if ( t instanceof LTable ) {
( (LTable) t ).put( k, v );
} else {
vm.error( "expected table" );
}
} break;
case SETFENV:
setfenv( (LuaState) vm );
break;
case SELECT:
select( vm );
break;
case COLLECTGARBAGE:
System.gc();
vm.settop(0);
break;
case DOFILE:
dofile(vm);
break;
case LOADSTRING:
loadstring(vm, vm.topointer(2), vm.tostring(3));
break;
case LOAD:
load(vm, vm.topointer(2), vm.tostring(3));
break;
case TOSTRING: {
LValue v = vm.topointer(2);
vm.settop(0);
vm.pushlvalue( v.luaAsString() );
break;
}
case UNPACK:
unpack(vm);
break;
case NEXT: {
setResult( vm, next(vm, vm.topointer(2), vm.tointeger(3)) );
break;
}
default:
luaUnsupportedOperation();
}
return false;
}
public static void redirectOutput( OutputStream newStdOut ) {
stdout = new PrintStream( newStdOut );
}
public static void restoreStandardOutput() {
stdout = System.out;
}
private void select( LuaState vm ) {
LValue arg = vm.topointer(2);
if ( arg instanceof LNumber ) {
final int start = Math.min(arg.toJavaInt(),vm.gettop());
for ( int i=0; i<=start; i++ )
vm.remove(1);
return;
} else if ( arg.toJavaString().equals( "#" ) ) {
setResult( vm, LInteger.valueOf( vm.gettop() - 2 ) );
}
}
private void setfenv( LuaState state ) {
LValue f = state.topointer(2);
LValue newenv = state.topointer(3);
LClosure c = null;
// Lua reference manual says that first argument, f, can be a "Lua
// function" or an integer. Lots of things extend LFunction, but only
// instances of Closure are "Lua functions".
if ( f instanceof LClosure ) {
c = (LClosure) f;
} else {
int callStackDepth = f.toJavaInt();
if ( callStackDepth > 0 ) {
CallInfo frame = state.getStackFrame( callStackDepth );
if ( frame != null ) {
c = frame.closure;
}
} else {
// This is supposed to set the environment of the current
// "thread". But, we have not implemented coroutines yet.
throw new RuntimeException( "not implemented" );
}
}
if ( c != null ) {
if ( newenv instanceof LTable ) {
c.env = (LTable) newenv;
}
state.settop(0);
state.pushlvalue( c );
return;
}
state.settop(0);
return;
}
// closes the input stream, provided its not null or System.in
private static void closeSafely(InputStream is) {
try {
if ( is != null && is != STDIN )
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// closes the output stream, provided its not null or STDOUT
private static void closeSafely(OutputStream os) {
try {
if ( os != null && os != STDOUT )
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// return true if laoded, false if error put onto the stack
private static boolean loadis(LuaState vm, InputStream is, String chunkname ) {
try {
vm.settop(0);
if ( 0 != vm.load(is, chunkname) ) {
setErrorResult( vm, "cannot load "+chunkname+": "+vm.topointer(-1) );
return false;
} else {
return true;
}
} finally {
closeSafely( is );
}
}
// return true if loaded, false if error put onto stack
public static boolean loadfile( LuaState vm, String fileName ) {
InputStream is;
String script;
if ( ! "".equals(fileName) ) {
script = fileName;
is = Platform.getInstance().openFile(fileName);
if ( is == null ) {
setErrorResult( vm, "cannot open "+fileName+": No such file or directory" );
return false;
}
} else {
is = STDIN;
script = "-";
}
// use vm to load the script
return loadis( vm, is, script );
}
// if load succeeds, return 0 for success, 1 for error (as per lua spec)
private void dofile( LuaState vm ) {
String filename = vm.tostring(2);
if ( loadfile( vm, filename ) ) {
int s = vm.pcall(1, 0, 0);
setResult( vm, LInteger.valueOf( s!=0? 1: 0 ) );
} else {
vm.error("cannot open "+filename);
}
}
// return true if loaded, false if error put onto stack
private boolean loadstring(LuaState vm, LValue string, String chunkname) {
return loadis( vm,
string.luaAsString().toInputStream(),
("".equals(chunkname)? "(string)": chunkname) );
}
// return true if loaded, false if error put onto stack
private boolean load(LuaState vm, LValue chunkPartLoader, String chunkname) {
if ( ! (chunkPartLoader instanceof LClosure) ) {
vm.error("not a closure: "+chunkPartLoader);
}
// load all the parts
LClosure c = (LClosure) chunkPartLoader;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
while ( true ) {
setResult(vm,c);
if ( 0 != vm.pcall(0, 1, 0) ) {
setErrorResult(vm, vm.tostring(2));
return false;
}
LValue v = vm.topointer(2);
if ( v == LNil.NIL )
break;
LString s = v.luaAsString();
s.write(baos, 0, s.length());
}
// load the chunk
return loadis( vm,
new ByteArrayInputStream( baos.toByteArray() ),
("".equals(chunkname)? "=(load)": chunkname) );
} catch (IOException ioe) {
setErrorResult(vm, ioe.getMessage());
return false;
} finally {
closeSafely( baos );
}
}
/** unpack (list [, i [, j]])
*
* Returns the elements from the given table. This function is equivalent to
* return list[i], list[i+1], ···, list[j]
*
* except that the above code can be written only for a fixed number of elements.
* By default, i is 1 and j is the length of the list, as defined by the length operator (see §2.5.5).
*/
private void unpack(LuaState vm) {
LValue v = vm.topointer(2);
int i = vm.tointeger(3);
int j = vm.tointeger(4);
LTable list = (LTable) v;
if ( i == 0 )
i = 1;
if ( j == 0 )
j = list.luaLength();
vm.settop(0);
for ( int k=i; k<=j; k++ )
vm.pushlvalue( list.get(k) );
}
private LValue next(LuaState vm, LValue table, int index) {
throw new java.lang.RuntimeException("next() not supported yet");
}
}

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.lib;
import org.luaj.vm.LClosure;
@@ -9,7 +30,7 @@ import org.luaj.vm.LuaState;
public class CoroutineLib extends LFunction {
private static final String[] NAMES = {
"loadlib",
"install",
"create",
"resume",
"running",
@@ -19,9 +40,18 @@ public class CoroutineLib extends LFunction {
"wrapped"
};
private static final int INSTALL = 0;
private static final int CREATE = 1;
private static final int RESUME = 2;
private static final int RUNNING = 3;
private static final int STATUS = 4;
private static final int WRAP = 5;
private static final int YIELD = 6;
private static final int WRAPPED = 7;
public static void install( LTable globals ) {
LTable lib = new LTable(0,6);
for ( int i=1; i<=6; i++ )
for ( int i=1; i<=YIELD; i++ )
lib.put(NAMES[i], new CoroutineLib(i));
globals.put("coroutine",lib);
}
@@ -39,8 +69,8 @@ public class CoroutineLib extends LFunction {
this.thread = null;
}
public String toJavaString() {
return "coroutine."+NAMES[id];
public String toString() {
return NAMES[id]+"()";
}
private CoroutineLib( int id, LThread thread ) {
@@ -50,22 +80,21 @@ public class CoroutineLib extends LFunction {
public boolean luaStackCall( LuaState vm ) {
switch ( id ) {
case 0: { // load lib
case INSTALL: {
install(vm._G);
vm.pushnil();
break;
}
case 1: { // create
case CREATE: {
LClosure c = (LClosure) vm.topointer(2);
vm.pushlvalue( new LThread(c) );
break;
}
case 2: {// resume
case RESUME: {
LThread t = (LThread) vm.topointer(2);
t.resumeFrom( vm, vm.gettop()-2 );
return false;
}
case 3: { // running
case RUNNING: {
LThread r = LThread.getRunning();
if ( r != null ) {
vm.pushlvalue( r );
@@ -74,16 +103,16 @@ public class CoroutineLib extends LFunction {
}
break;
}
case 4: { // status
case STATUS: {
vm.pushstring( ((LThread) vm.topointer(2)).getStatus() );
break;
}
case 5: { // wrap
case WRAP: {
LClosure c = (LClosure) vm.topointer(2);
vm.pushlvalue( new CoroutineLib(7,new LThread(c)) );
vm.pushlvalue( new CoroutineLib(WRAPPED,new LThread(c)) );
break;
}
case 6: { // yield
case YIELD: {
LThread r = LThread.getRunning();
if ( r == null )
vm.error("main thread can't yield");
@@ -91,7 +120,7 @@ public class CoroutineLib extends LFunction {
return r.yield();
}
}
case 7: { // wrapped resume
case WRAPPED: {
LThread t = this.thread;
t.resumeFrom( vm, vm.gettop()-1 );
if ( vm.toboolean(1) )

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.lib;
import org.luaj.vm.LString;

View File

@@ -1,88 +1,39 @@
/*******************************************************************************
* 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.lib;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import org.luaj.vm.CallInfo;
import org.luaj.vm.LBoolean;
import org.luaj.vm.LClosure;
import org.luaj.vm.LDouble;
import org.luaj.vm.LFunction;
import org.luaj.vm.LInteger;
import org.luaj.vm.LNil;
import org.luaj.vm.LNumber;
import org.luaj.vm.LString;
import org.luaj.vm.LTable;
import org.luaj.vm.LValue;
import org.luaj.vm.Lua;
import org.luaj.vm.LuaState;
import org.luaj.vm.Platform;
public class MathLib extends LFunction {
public static InputStream STDIN = null;
public static PrintStream STDOUT = System.out;
public static LTable LOADED = new LTable();
public static void install( LTable globals ) {
installNames( globals, GLOBAL_NAMES, GLOBALS_BASE );
// math lib
LTable math = new LTable();
installNames( math, MATH_NAMES, MATH_BASE );
math.put( "huge", new LDouble( Double.MAX_VALUE ) );
math.put( "pi", new LDouble( Math.PI ) );
globals.put( "math", math );
// string lib
LTable string = LString.getMetatable();
installNames( string, STRING_NAMES, STRING_BASE );
globals.put( "string", string );
// packages lib
LTable pckg = new LTable();
installNames( pckg, PACKAGE_NAMES, PACKAGES_BASE );
globals.put( "package", pckg );
pckg.put( "loaded", LOADED );
// table lib
LTable table = new LTable();
installNames( table, TABLE_NAMES, TABLES_BASE );
globals.put( "table", table );
// coroutines
CoroutineLib.install(globals);
}
private static void installNames( LTable table, String[] names, int indexBase ) {
for ( int i=0; i<names.length; i++ )
table.put( names[i], new MathLib(indexBase+i) );
}
public static final String[] GLOBAL_NAMES = {
"loadfile",
"tonumber",
"rawget",
"rawset",
"setfenv",
"select",
"collectgarbage",
"dofile",
"loadstring",
"load",
"tostring",
"unpack",
"next",
"module",
"require",
};
public static final String[] MATH_NAMES = {
public static final String[] NAMES = {
"math",
"abs",
"cos",
"max",
@@ -94,91 +45,25 @@ public class MathLib extends LFunction {
"floor",
};
public static final String[] STRING_NAMES = {
"byte",
"char",
"dump",
"find",
"format",
"gmatch",
"gsub",
"len",
"lower",
"match",
"rep",
"reverse",
"sub",
"upper",
};
private static final int INSTALL = 0;
private static final int ABS = 1;
private static final int COS = 2;
private static final int MAX = 3;
private static final int MIN = 4;
private static final int MODF = 5;
private static final int SIN = 6;
private static final int SQRT = 7;
private static final int CEIL = 8;
private static final int FLOOR = 9;
public static final String[] PACKAGE_NAMES = {
"loalib",
"seeall",
};
public static final String[] TABLE_NAMES = {
"concat",
"insert",
"maxn",
"remove",
"sort",
};
private static final int GLOBALS_BASE = 0;
private static final int LOADFILE = GLOBALS_BASE + 0;
private static final int TONUMBER = GLOBALS_BASE + 1;
private static final int RAWGET = GLOBALS_BASE + 2;
private static final int RAWSET = GLOBALS_BASE + 3;
private static final int SETFENV = GLOBALS_BASE + 4;
private static final int SELECT = GLOBALS_BASE + 5;
private static final int COLLECTGARBAGE = GLOBALS_BASE + 6;
private static final int DOFILE = GLOBALS_BASE + 7;
private static final int LOADSTRING = GLOBALS_BASE + 8;
private static final int LOAD = GLOBALS_BASE + 9;
private static final int TOSTRING = GLOBALS_BASE + 10;
private static final int UNPACK = GLOBALS_BASE + 11;
private static final int NEXT = GLOBALS_BASE + 12;
private static final int MODULE = GLOBALS_BASE + 13;
private static final int REQUIRE = GLOBALS_BASE + 14;
private static final int MATH_BASE = 20;
private static final int ABS = MATH_BASE + 0;
private static final int COS = MATH_BASE + 1;
private static final int MAX = MATH_BASE + 2;
private static final int MIN = MATH_BASE + 3;
private static final int MODF = MATH_BASE + 4;
private static final int SIN = MATH_BASE + 5;
private static final int SQRT = MATH_BASE + 6;
private static final int CEIL = MATH_BASE + 7;
private static final int FLOOR = MATH_BASE + 8;
private static final int STRING_BASE = 30;
private static final int BYTE = STRING_BASE + 0;
private static final int CHAR = STRING_BASE + 1;
private static final int DUMP = STRING_BASE + 2;
private static final int FIND = STRING_BASE + 3;
private static final int FORMAT = STRING_BASE + 4;
private static final int GMATCH = STRING_BASE + 5;
private static final int GSUB = STRING_BASE + 6;
private static final int LEN = STRING_BASE + 7;
private static final int LOWER = STRING_BASE + 8;
private static final int MATCH = STRING_BASE + 9;
private static final int REP = STRING_BASE + 10;
private static final int REVERSE = STRING_BASE + 11;
private static final int SUB = STRING_BASE + 12;
private static final int UPPER = STRING_BASE + 13;
private static final int PACKAGES_BASE = 50;
private static final int LOADLIB = PACKAGES_BASE + 0;
private static final int SEEALL = PACKAGES_BASE + 1;
private static final int TABLES_BASE = 60;
private static final int CONCAT = TABLES_BASE + 0;
private static final int INSERT = TABLES_BASE + 1;
private static final int MAXN = TABLES_BASE + 2;
private static final int REMOVE = TABLES_BASE + 3;
private static final int SORT = TABLES_BASE + 4;
public static void install( LTable globals ) {
LTable math = new LTable();
for ( int i=1; i<NAMES.length; i++ )
math.put(NAMES[i], new MathLib(i));
math.put( "huge", new LDouble( Double.MAX_VALUE ) );
math.put( "pi", new LDouble( Math.PI ) );
globals.put( "math", math );
}
private final int id;
@@ -186,80 +71,20 @@ public class MathLib extends LFunction {
this.id = id;
}
public String toString() {
return NAMES[id]+"()";
}
private static void setResult( LuaState vm, LValue value ) {
vm.settop(0);
vm.pushlvalue( value );
}
private static void setErrorResult( LuaState vm, String message ) {
vm.settop(0);
vm.pushnil();
vm.pushstring( message );
}
public boolean luaStackCall( LuaState vm ) {
switch ( id ) {
case LOADFILE:
loadfile(vm, vm.tostring(2));
case INSTALL:
install( vm._G );
break;
case TONUMBER:
setResult( vm, toNumber( vm ) );
break;
case RAWGET: {
LValue t = vm.topointer(2);
LValue k = vm.topointer(3);
vm.settop(0);
if ( t instanceof LTable ) {
vm.pushlvalue(( (LTable) t ).get( k ));
}
} break;
case RAWSET: {
LValue t = vm.topointer(2);
LValue k = vm.topointer(3);
LValue v = vm.topointer(4);
vm.settop(0);
if ( t instanceof LTable ) {
( (LTable) t ).put( k, v );
} else {
vm.error( "expected table" );
}
} break;
case SETFENV:
setfenv( (LuaState) vm );
break;
case SELECT:
select( vm );
break;
case COLLECTGARBAGE:
System.gc();
vm.settop(0);
break;
case DOFILE:
dofile(vm);
break;
case LOADSTRING:
loadstring(vm, vm.topointer(2), vm.tostring(3));
break;
case LOAD:
load(vm, vm.topointer(2), vm.tostring(3));
break;
case TOSTRING:
setResult( vm, tostring(vm, vm.topointer(2)) );
break;
case UNPACK:
unpack(vm);
break;
case NEXT:
setResult( vm, next(vm, vm.topointer(2), vm.tointeger(3)) );
break;
case MODULE:
module(vm);
break;
case REQUIRE:
require(vm);
break;
// Math functions
case ABS:
setResult( vm, abs( vm.topointer( 2 ) ) );
break;
@@ -287,94 +112,11 @@ public class MathLib extends LFunction {
case FLOOR:
setResult( vm, LInteger.valueOf( (int) Math.floor( vm.tonumber(2) ) ) );
break;
// String functions
case BYTE:
StringLib.byte_( vm );
break;
case CHAR:
StringLib.char_( vm );
break;
case DUMP:
StringLib.dump( vm );
break;
case FIND:
StringLib.find( vm );
break;
case FORMAT:
StringLib.format( vm );
break;
case GMATCH:
StringLib.gmatch( vm );
break;
case GSUB:
StringLib.gsub( vm );
break;
case LEN:
StringLib.len( vm );
break;
case LOWER:
StringLib.lower( vm );
break;
case MATCH:
StringLib.match( vm );
break;
case REP:
StringLib.rep( vm );
break;
case REVERSE:
StringLib.reverse( vm );
break;
case SUB:
StringLib.sub( vm );
break;
case UPPER:
StringLib.upper( vm );
break;
// package functions
case LOADLIB:
loadlib(vm);
break;
case SEEALL:
seeall(vm);
break;
// table library
case CONCAT:
concat(vm);
break;
case INSERT:
insert(vm);
break;
case MAXN:
maxn(vm);
break;
case REMOVE:
remove(vm);
break;
case SORT:
sort(vm);
break;
default:
luaUnsupportedOperation();
}
return false;
}
private void select( LuaState vm ) {
LValue arg = vm.topointer(2);
if ( arg instanceof LNumber ) {
final int start = Math.min(arg.toJavaInt(),vm.gettop());
for ( int i=0; i<=start; i++ )
vm.remove(1);
return;
} else if ( arg.toJavaString().equals( "#" ) ) {
setResult( vm, LInteger.valueOf( vm.gettop() - 2 ) );
}
}
private LValue abs( final LValue v ) {
LValue nv = v.luaUnaryMinus();
return max( v, nv );
@@ -398,346 +140,4 @@ public class MathLib extends LFunction {
vm.pushnumber( fracPart );
}
private LValue toNumber( LuaState vm ) {
LValue input = vm.topointer(2);
if ( input instanceof LNumber ) {
return input;
} else if ( input instanceof LString ) {
int base = 10;
if ( vm.gettop()>=3 ) {
base = vm.tointeger(3);
}
return ( (LString) input ).luaToNumber( base );
}
return LNil.NIL;
}
private void setfenv( LuaState state ) {
LValue f = state.topointer(2);
LValue newenv = state.topointer(3);
LClosure c = null;
// Lua reference manual says that first argument, f, can be a "Lua
// function" or an integer. Lots of things extend LFunction, but only
// instances of Closure are "Lua functions".
if ( f instanceof LClosure ) {
c = (LClosure) f;
} else {
int callStackDepth = f.toJavaInt();
if ( callStackDepth > 0 ) {
CallInfo frame = state.getStackFrame( callStackDepth );
if ( frame != null ) {
c = frame.closure;
}
} else {
// This is supposed to set the environment of the current
// "thread". But, we have not implemented coroutines yet.
throw new RuntimeException( "not implemented" );
}
}
if ( c != null ) {
if ( newenv instanceof LTable ) {
c.env = (LTable) newenv;
}
state.settop(0);
state.pushlvalue( c );
return;
}
state.settop(0);
return;
}
// closes the input stream, provided its not null or System.in
private static void closeSafely(InputStream is) {
try {
if ( is != null && is != STDIN )
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// closes the output stream, provided its not null or STDOUT
private static void closeSafely(OutputStream os) {
try {
if ( os != null && os != STDOUT )
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// return true if laoded, false if error put onto the stack
private static boolean loadis(LuaState vm, InputStream is, String chunkname ) {
try {
vm.settop(0);
if ( 0 != vm.load(is, chunkname) ) {
setErrorResult( vm, "cannot load "+chunkname+": "+vm.topointer(-1) );
return false;
} else {
return true;
}
} finally {
closeSafely( is );
}
}
// return true if loaded, false if error put onto stack
public static boolean loadfile( LuaState vm, String fileName ) {
InputStream is;
String script;
if ( ! "".equals(fileName) ) {
script = fileName;
is = Platform.getInstance().openFile(fileName);
if ( is == null ) {
setErrorResult( vm, "cannot open "+fileName+": No such file or directory" );
return false;
}
} else {
is = STDIN;
script = "-";
}
// use vm to load the script
return loadis( vm, is, script );
}
// if load succeeds, return 0 for success, 1 for error (as per lua spec)
private void dofile( LuaState vm ) {
String filename = vm.tostring(2);
if ( loadfile( vm, filename ) ) {
int s = vm.pcall(1, 0, 0);
setResult( vm, LInteger.valueOf( s!=0? 1: 0 ) );
} else {
vm.error("cannot open "+filename);
}
}
// return true if loaded, false if error put onto stack
private boolean loadstring(LuaState vm, LValue string, String chunkname) {
return loadis( vm,
string.luaAsString().toInputStream(),
("".equals(chunkname)? "(string)": chunkname) );
}
// return true if loaded, false if error put onto stack
private boolean load(LuaState vm, LValue chunkPartLoader, String chunkname) {
if ( ! (chunkPartLoader instanceof LClosure) ) {
vm.error("not a closure: "+chunkPartLoader);
}
// load all the parts
LClosure c = (LClosure) chunkPartLoader;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
while ( true ) {
setResult(vm,c);
if ( 0 != vm.pcall(0, 1, 0) ) {
setErrorResult(vm, vm.tostring(2));
return false;
}
LValue v = vm.topointer(2);
if ( v == LNil.NIL )
break;
LString s = v.luaAsString();
s.write(baos, 0, s.length());
}
// load the chunk
return loadis( vm,
new ByteArrayInputStream( baos.toByteArray() ),
("".equals(chunkname)? "=(load)": chunkname) );
} catch (IOException ioe) {
setErrorResult(vm, ioe.getMessage());
return false;
} finally {
closeSafely( baos );
}
}
private LValue tostring(LuaState vm, LValue arg) {
return arg.luaAsString();
}
/** unpack (list [, i [, j]])
*
* Returns the elements from the given table. This function is equivalent to
* return list[i], list[i+1], ···, list[j]
*
* except that the above code can be written only for a fixed number of elements.
* By default, i is 1 and j is the length of the list, as defined by the length operator (see §2.5.5).
*/
private void unpack(LuaState vm) {
LValue v = vm.topointer(2);
int i = vm.tointeger(3);
int j = vm.tointeger(4);
LTable list = (LTable) v;
if ( i == 0 )
i = 1;
if ( j == 0 )
j = list.luaLength();
vm.settop(0);
for ( int k=i; k<=j; k++ )
vm.pushlvalue( list.get(k) );
}
private LValue next(LuaState vm, LValue table, int index) {
throw new java.lang.RuntimeException("next() not supported yet");
}
// ======================== Module, Package loading =============================
public static void module( LuaState vm ) {
vm.error( "module not implemented" );
}
/**
* require (modname)
*
* Loads the given module. The function starts by looking into the package.loaded table to
* determine whether modname is already loaded. If it is, then require returns the value
* stored at package.loaded[modname]. Otherwise, it tries to find a loader for the module.
*
* To find a loader, require is guided by the package.loaders array. By changing this array,
* we can change how require looks for a module. The following explanation is based on the
* default configuration for package.loaders.
*
* First require queries package.preload[modname]. If it has a value, this value
* (which should be a function) is the loader. Otherwise require searches for a Lua loader
* using the path stored in package.path. If that also fails, it searches for a C loader
* using the path stored in package.cpath. If that also fails, it tries an all-in-one loader
* (see package.loaders).
*
* Once a loader is found, require calls the loader with a single argument, modname.
* If the loader returns any value, require assigns the returned value to package.loaded[modname].
* If the loader returns no value and has not assigned any value to package.loaded[modname],
* then require assigns true to this entry. In any case, require returns the final value of
* package.loaded[modname].
*
* If there is any error loading or running the module, or if it cannot find any loader for
* the module, then require signals an error.
*/
public static void require( LuaState vm ) {
LString modname = vm.tolstring(2);
if ( LOADED.containsKey(modname) )
setResult( vm, LOADED.get(modname) );
else {
String s = modname.toJavaString();
if ( ! loadfile(vm, s+".luac") && ! loadfile(vm, s+".lua") )
vm.error( "not found: "+s );
else if ( 0 == vm.pcall(0, 1, 0) ) {
LValue result = vm.topointer( -1 );
if ( result != LNil.NIL )
LOADED.put(modname, result);
else if ( ! LOADED.containsKey(modname) )
LOADED.put(modname, result = LBoolean.TRUE);
setResult( vm, result );
}
}
}
public static void loadlib( LuaState vm ) {
vm.error( "loadlib not implemented" );
}
public static void seeall( LuaState vm ) {
vm.error( "seeall not implemented" );
}
// ============= tables support =============
/** table.concat (table [, sep [, i [, j]]])
*
* Given an array where all elements are strings or numbers, returns table[i]..sep..table[i+1] ··· sep..table[j].
* The default value for sep is the empty string, the default for i is 1, and the default for j is the length of the table.
* If i is greater than j, returns the empty string.
*/
private void concat(LuaState vm) {
int n = vm.gettop();
LTable table = vm.totable(2);
LString sep = (n>=3? vm.tolstring(3): null);
int i = vm.tointeger(4);
int j = vm.tointeger(5);
int len = table.luaLength();
if ( i == 0 )
i = 1;
if ( j == 0 )
j = len;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
for ( int k=i; k<=j; k++ ) {
LValue v = table.get(k);
v.luaAsString().write(baos);
if ( k<j && sep!=null )
sep.write( baos );
}
vm.settop(0);
vm.pushlstring( baos.toByteArray() );
} catch (IOException e) {
vm.error(e.getMessage());
}
}
/** table.insert (table, [pos,] value)
*
* Inserts element value at position pos in table, shifting up other elements to open space, if necessary.
* The default value for pos is n+1, where n is the length of the table (see §2.5.5), so that a call
* table.insert(t,x) inserts x at the end of table t.
*/
private void insert(LuaState vm) {
int n = vm.gettop();
LTable table = vm.totable(2);
int pos = (n>=4? vm.tointeger(3): 0);
LValue value = vm.topointer(-1);
table.luaInsertPos( pos, value );
}
/** table.maxn (table)
*
* Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical
* indices. (To do its job this function does a linear traversal of the whole table.)
*/
private void maxn(LuaState vm) {
LTable table = vm.totable(2);
vm.settop(0);
vm.pushinteger( table.luaMaxN() );
}
/** table.remove (table [, pos])
*
* Removes from table the element at position pos, shifting down other elements to close the space, if necessary.
* Returns the value of the removed element. The default value for pos is n, where n is the length of the table,
* so that a call table.remove(t) removes the last element of table t.
*/
private void remove(LuaState vm) {
int n = vm.gettop();
LTable table = vm.totable(2);
int pos = (n>=3? vm.tointeger(3): 0);
vm.settop(0);
vm.pushlvalue( table.luaRemovePos( pos ) );
}
/** table.sort (table [, comp])
*
* Sorts table elements in a given order, in-place, from table[1] to table[n], where n is the length of the table.
* If comp is given, then it must be a function that receives two table elements, and returns true when the first
* is less than the second (so that not comp(a[i+1],a[i]) will be true after the sort). If comp is not given,
* then the standard Lua operator &lt; is used instead.
*
* The sort algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort.
*/
private void sort(LuaState vm) {
LTable table = vm.totable(2);
LValue compare = vm.topointer(3);
table.luaSort( vm, compare );
}
}

View File

@@ -0,0 +1,161 @@
/*******************************************************************************
* 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.lib;
import java.io.InputStream;
import java.io.PrintStream;
import org.luaj.vm.LBoolean;
import org.luaj.vm.LFunction;
import org.luaj.vm.LNil;
import org.luaj.vm.LString;
import org.luaj.vm.LTable;
import org.luaj.vm.LValue;
import org.luaj.vm.LuaState;
public class PackageLib extends LFunction {
public static InputStream STDIN = null;
public static PrintStream STDOUT = System.out;
public static LTable LOADED = new LTable();
private static final String[] NAMES = {
"package",
"module",
"require",
"loadlib",
"seeall",
};
private static final int INSTALL = 0;
private static final int MODULE = 1;
private static final int REQUIRE = 2;
private static final int LOADLIB = 3;
private static final int SEEALL = 4;
public static void install( LTable globals ) {
for ( int i=1; i<LOADLIB; i++ )
globals.put( NAMES[i], new PackageLib(i) );
LTable pckg = new LTable();
for ( int i=LOADLIB; i<NAMES.length; i++ )
pckg.put( NAMES[i], new PackageLib(i) );
globals.put( "package", pckg );
pckg.put( "loaded", LOADED );
}
private final int id;
private PackageLib( int id ) {
this.id = id;
}
public String toString() {
return NAMES[id]+"()";
}
public boolean luaStackCall( LuaState vm ) {
switch ( id ) {
case INSTALL:
install(vm._G);
break;
case MODULE:
module(vm);
break;
case REQUIRE:
require(vm);
break;
case LOADLIB:
loadlib(vm);
break;
case SEEALL:
seeall(vm);
break;
default:
luaUnsupportedOperation();
}
return false;
}
// ======================== Module, Package loading =============================
public static void module( LuaState vm ) {
vm.error( "module not implemented" );
}
/**
* require (modname)
*
* Loads the given module. The function starts by looking into the package.loaded table to
* determine whether modname is already loaded. If it is, then require returns the value
* stored at package.loaded[modname]. Otherwise, it tries to find a loader for the module.
*
* To find a loader, require is guided by the package.loaders array. By changing this array,
* we can change how require looks for a module. The following explanation is based on the
* default configuration for package.loaders.
*
* First require queries package.preload[modname]. If it has a value, this value
* (which should be a function) is the loader. Otherwise require searches for a Lua loader
* using the path stored in package.path. If that also fails, it searches for a C loader
* using the path stored in package.cpath. If that also fails, it tries an all-in-one loader
* (see package.loaders).
*
* Once a loader is found, require calls the loader with a single argument, modname.
* If the loader returns any value, require assigns the returned value to package.loaded[modname].
* If the loader returns no value and has not assigned any value to package.loaded[modname],
* then require assigns true to this entry. In any case, require returns the final value of
* package.loaded[modname].
*
* If there is any error loading or running the module, or if it cannot find any loader for
* the module, then require signals an error.
*/
public static void require( LuaState vm ) {
LString modname = vm.tolstring(2);
if ( LOADED.containsKey(modname) ) {
vm.settop(0);
vm.pushlvalue( LOADED.get(modname) );
}
else {
String s = modname.toJavaString();
if ( ! BaseLib.loadfile(vm, s+".luac") && ! BaseLib.loadfile(vm, s+".lua") )
vm.error( "not found: "+s );
else if ( 0 == vm.pcall(0, 1, 0) ) {
LValue result = vm.topointer( -1 );
if ( result != LNil.NIL )
LOADED.put(modname, result);
else if ( ! LOADED.containsKey(modname) )
LOADED.put(modname, result = LBoolean.TRUE);
vm.settop(0);
vm.pushlvalue( result );
}
}
}
public static void loadlib( LuaState vm ) {
vm.error( "loadlib not implemented" );
}
public static void seeall( LuaState vm ) {
vm.error( "seeall not implemented" );
}
}

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.lib;
import org.luaj.vm.LFunction;
@@ -8,7 +29,117 @@ import org.luaj.vm.LValue;
import org.luaj.vm.LuaState;
public class StringLib {
public class StringLib extends LFunction {
private static final String[] NAMES = {
"string",
"byte",
"char",
"dump",
"find",
"format",
"gmatch",
"gsub",
"len",
"lower",
"match",
"rep",
"reverse",
"sub",
"upper",
};
private static final int INSTALL = 0;
private static final int BYTE = 1;
private static final int CHAR = 2;
private static final int DUMP = 3;
private static final int FIND = 4;
private static final int FORMAT = 5;
private static final int GMATCH = 6;
private static final int GSUB = 7;
private static final int LEN = 8;
private static final int LOWER = 9;
private static final int MATCH = 10;
private static final int REP = 11;
private static final int REVERSE = 12;
private static final int SUB = 13;
private static final int UPPER = 14;
public static void install( LTable globals ) {
LTable string = LString.getMetatable();
for ( int i=1; i<NAMES.length; i++ )
string.put(NAMES[i], new StringLib(i));
globals.put( "string", string );
}
private final int id;
public StringLib() {
this.id = 0;
}
private StringLib( int id ) {
this.id = id;
}
public String toString() {
return NAMES[id]+"()";
}
public boolean luaStackCall( LuaState vm ) {
switch ( id ) {
case INSTALL:
install( vm._G );
break;
case BYTE:
StringLib.byte_( vm );
break;
case CHAR:
StringLib.char_( vm );
break;
case DUMP:
StringLib.dump( vm );
break;
case FIND:
StringLib.find( vm );
break;
case FORMAT:
StringLib.format( vm );
break;
case GMATCH:
StringLib.gmatch( vm );
break;
case GSUB:
StringLib.gsub( vm );
break;
case LEN:
StringLib.len( vm );
break;
case LOWER:
StringLib.lower( vm );
break;
case MATCH:
StringLib.match( vm );
break;
case REP:
StringLib.rep( vm );
break;
case REVERSE:
StringLib.reverse( vm );
break;
case SUB:
StringLib.sub( vm );
break;
case UPPER:
StringLib.upper( vm );
break;
default:
luaUnsupportedOperation();
}
return false;
}
/**
* string.byte (s [, i [, j]])
*

View File

@@ -0,0 +1,182 @@
/*******************************************************************************
* 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.lib;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.luaj.vm.LFunction;
import org.luaj.vm.LString;
import org.luaj.vm.LTable;
import org.luaj.vm.LValue;
import org.luaj.vm.LuaState;
public class TableLib extends LFunction {
public static final String[] NAMES = {
"table",
"concat",
"insert",
"maxn",
"remove",
"sort",
};
private static final int INSTALL = 0;
private static final int CONCAT = 1;
private static final int INSERT = 2;
private static final int MAXN = 3;
private static final int REMOVE = 4;
private static final int SORT = 5;
public static void install( LTable globals ) {
LTable table = new LTable();
for ( int i=1; i<NAMES.length; i++ )
table.put( NAMES[i], new TableLib(i) );
globals.put( "table", table );
}
private final int id;
/** Default constructor for loading the library dynamically */
private TableLib() {
id = 0;
}
/** Private constructor to construct a table function instance */
private TableLib( int id ) {
this.id = id;
}
public String toString() {
return NAMES[id]+"()";
}
public boolean luaStackCall( LuaState vm ) {
switch ( id ) {
/* Load the table library dynamically
*/
case INSTALL:
install(vm._G);
break;
/* table.concat (table [, sep [, i [, j]]])
*
* Given an array where all elements are strings or numbers, returns table[i]..sep..table[i+1] ··· sep..table[j].
* The default value for sep is the empty string, the default for i is 1, and the default for j is the length of the table.
* If i is greater than j, returns the empty string.
*/
case CONCAT: {
int n = vm.gettop();
LTable table = vm.totable(2);
LString sep = (n>=3? vm.tolstring(3): null);
int i = vm.tointeger(4);
int j = vm.tointeger(5);
int len = table.luaLength();
if ( i == 0 )
i = 1;
if ( j == 0 )
j = len;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
for ( int k=i; k<=j; k++ ) {
LValue v = table.get(k);
v.luaAsString().write(baos);
if ( k<j && sep!=null )
sep.write( baos );
}
vm.settop(0);
vm.pushlstring( baos.toByteArray() );
} catch (IOException e) {
vm.error(e.getMessage());
}
break;
}
/* table.insert (table, [pos,] value)
*
* Inserts element value at position pos in table, shifting up other elements to open space, if necessary.
* The default value for pos is n+1, where n is the length of the table (see §2.5.5), so that a call
* table.insert(t,x) inserts x at the end of table t.
*/
case INSERT: {
int n = vm.gettop();
LTable table = vm.totable(2);
int pos = (n>=4? vm.tointeger(3): 0);
LValue value = vm.topointer(-1);
table.luaInsertPos( pos, value );
break;
}
/* table.maxn (table)
*
* Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical
* indices. (To do its job this function does a linear traversal of the whole table.)
*/
case MAXN: {
LTable table = vm.totable(2);
vm.settop(0);
vm.pushinteger( table.luaMaxN() );
break;
}
/* table.remove (table [, pos])
*
* Removes from table the element at position pos, shifting down other elements to close the space, if necessary.
* Returns the value of the removed element. The default value for pos is n, where n is the length of the table,
* so that a call table.remove(t) removes the last element of table t.
*/
case REMOVE: {
int n = vm.gettop();
LTable table = vm.totable(2);
int pos = (n>=3? vm.tointeger(3): 0);
vm.settop(0);
vm.pushlvalue( table.luaRemovePos( pos ) );
break;
}
/* table.sort (table [, comp])
*
* Sorts table elements in a given order, in-place, from table[1] to table[n], where n is the length of the table.
* If comp is given, then it must be a function that receives two table elements, and returns true when the first
* is less than the second (so that not comp(a[i+1],a[i]) will be true after the sort). If comp is not given,
* then the standard Lua operator &lt; is used instead.
*
* The sort algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort.
*/
case SORT: {
LTable table = vm.totable(2);
LValue compare = vm.topointer(3);
table.luaSort( vm, compare );
vm.settop(0);
break;
}
default:
luaUnsupportedOperation();
}
return false;
}
}

View File

@@ -1,120 +0,0 @@
/**
*
*/
package org.luaj.vm;
import java.io.OutputStream;
import java.io.PrintStream;
public class BaseLib extends LFunction {
static void install(LTable globals) {
for ( int i=0; i<NAMES.length; i++ )
globals.put( NAMES[i], new BaseLib(i) );
}
private static final String[] NAMES = {
"print",
"pairs",
"getmetatable",
"setmetatable",
"type",
"pcall",
"ipairs",
"error",
"assert",
};
private static final int PRINT = 0;
private static final int PAIRS = 1;
private static final int GETMETATABLE = 2;
private static final int SETMETATABLE = 3;
private static final int TYPE = 4;
private static final int PCALL = 5;
private static final int IPAIRS = 6;
private static final int ERROR = 7;
private static final int ASSERT = 8;
private static PrintStream stdout = System.out;
private int id;
private BaseLib( int id ) {
this.id = id;
}
public String toString() {
return "builtin."+NAMES[id];
}
// perform a lua call
/**
* Invoke a builtin
*/
public int invoke(LuaState vm) {
switch ( id ) {
case PRINT: {
int n = vm.gettop();
for ( int i=1; i<=n; i++ ) {
if ( i > 1 )
stdout.print( "\t" );
stdout.print( vm.tostring(i) );
}
stdout.println();
return 0;
}
case PAIRS:
case IPAIRS: {
LValue v = vm.topointer(1);
LValue r = v.luaPairs(id==PAIRS);
vm.pushlvalue( r );
return 1;
}
case GETMETATABLE:
return vm.getmetatable(1);
case SETMETATABLE:
vm.setmetatable(1);
return 1;
case TYPE: {
LValue v = vm.topointer(1);
vm.pushlstring( v.luaGetTypeName() );
return 1;
}
case PCALL: {
int n = vm.gettop();
int s = vm.pcall( n-1, Lua.LUA_MULTRET, 0 );
if ( s == 0 ) { // success
vm.pushboolean( true );
vm.insert( 1 );
return vm.gettop();
} else { // error, error message is on the stack
vm.pushboolean( false );
vm.insert( -2 );
return 2;
}
}
case ERROR: {
vm.error(vm.tostring(1), vm.gettop()>1? vm.tointeger(2): 1);
}
case ASSERT: {
if ( ! vm.toboolean(1) ) {
vm.error( vm.gettop()>1? vm.tostring(2): "assertion failed!", 0 );
} else {
return vm.gettop();
}
}
default:
luaUnsupportedOperation();
return 0;
}
}
public static void redirectOutput( OutputStream newStdOut ) {
stdout = new PrintStream( newStdOut );
}
public static void restoreStandardOutput() {
stdout = System.out;
}
}

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;
import java.io.ByteArrayInputStream;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;
import java.io.DataInputStream;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

File diff suppressed because it is too large Load Diff

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;
import java.io.InputStream;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.vm;

View File

@@ -30,7 +30,11 @@ import org.luaj.debug.DebugLuaState;
import org.luaj.debug.DebugSupport;
import org.luaj.debug.DebugUtils;
import org.luaj.debug.VMException;
import org.luaj.lib.CoroutineLib;
import org.luaj.lib.MathLib;
import org.luaj.lib.PackageLib;
import org.luaj.lib.StringLib;
import org.luaj.lib.TableLib;
import org.luaj.lib.j2se.LuajavaLib;
import org.luaj.vm.LClosure;
import org.luaj.vm.LPrototype;
@@ -160,20 +164,21 @@ public class StandardLuaJVM {
}
}
protected void init(LTable globals) {
protected void init(LuaState state) {
// add standard bindings
state.installStandardLibs();
// add LuaJava bindings
LuajavaLib.install(globals);
LuajavaLib.install(state._G);
// add LuaCompat bindings
MathLib.install(globals);
}
protected void doRun() throws IOException {
// new lua state
state = new LuaState();
init(state._G);
init(state);
// convert args to lua
String[] scriptArgs = getScriptArgs();
@@ -198,7 +203,7 @@ public class StandardLuaJVM {
// new lua debug state
state = new DebugLuaState();
init(state._G);
init(state);
// load the Lua file
DebugUtils.println("loading Lua script '" + getScript() + "'");

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.lib.j2se;
import java.util.HashMap;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.lib.j2se;
import java.util.HashMap;

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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.lib.j2se;

View File

@@ -1,12 +1,38 @@
/*******************************************************************************
* 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.IOException;
import java.io.InputStream;
import org.luaj.lib.CoroutineLib;
import org.luaj.lib.MathLib;
import org.luaj.lib.PackageLib;
import org.luaj.lib.StringLib;
import org.luaj.lib.TableLib;
import org.luaj.lib.j2se.LuajavaLib;
import org.luaj.vm.LClosure;
import org.luaj.vm.LPrototype;
import org.luaj.vm.LTable;
import org.luaj.vm.LValue;
import org.luaj.vm.LoadState;
import org.luaj.vm.LuaState;
@@ -28,9 +54,8 @@ public class LuaRunner {
String script = (args.length>0? args[0]: "/test2.luac");
System.out.println("loading '"+script+"'");
// add LuaCompat bindings
MathLib.install(state._G);
LuajavaLib.install(state._G);
// add standard bindings
state.installStandardLibs();
// load the file
InputStream is = LuaRunner.class.getResourceAsStream( script );

View File

@@ -1,12 +1,38 @@
/*******************************************************************************
* 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.IOException;
import java.io.InputStream;
import org.luaj.lib.CoroutineLib;
import org.luaj.lib.MathLib;
import org.luaj.lib.PackageLib;
import org.luaj.lib.StringLib;
import org.luaj.lib.TableLib;
import org.luaj.lib.j2se.LuajavaLib;
import org.luaj.vm.LClosure;
import org.luaj.vm.LPrototype;
import org.luaj.vm.LTable;
import org.luaj.vm.LValue;
import org.luaj.vm.LoadState;
import org.luaj.vm.LuaState;
@@ -25,8 +51,8 @@ public class LuajavaRunner {
// new lua state
LuaState state = new LuaState();
// add LuaCompat bindings
MathLib.install(state._G);
// add standard bindings
state.installStandardLibs();
// add LuaJava bindings
LuajavaLib.install(state._G);

View File

@@ -1,3 +1,24 @@
/*******************************************************************************
* 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;
public class SampleClass {

View File

@@ -1,18 +1,17 @@
package org.luaj.compiler;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.luaj.compiler.Compiler;
import org.luaj.debug.Print;
import org.luaj.vm.LClosure;
import org.luaj.vm.LValue;
import org.luaj.vm.LPrototype;
import org.luaj.vm.LuaState;
import junit.framework.TestCase;
import org.luaj.debug.Print;
import org.luaj.lib.BaseLib;
import org.luaj.vm.LClosure;
import org.luaj.vm.LPrototype;
import org.luaj.vm.LValue;
import org.luaj.vm.LuaState;
public class SimpleTests extends TestCase {
private void doTest( String script ) {
@@ -24,6 +23,7 @@ public class SimpleTests extends TestCase {
// try running the code!
LuaState state = new LuaState();
BaseLib.install( state._G );
LClosure c = new LClosure( state, p );
state.doCall( c, new LValue[0] );
} catch ( Exception e ) {

View File

@@ -7,7 +7,7 @@ import java.io.OutputStream;
import junit.framework.TestCase;
import org.luaj.debug.DebugLuaState;
import org.luaj.lib.MathLib;
import org.luaj.lib.BaseLib;
import org.luaj.lib.j2se.LuajavaLib;
@@ -114,11 +114,11 @@ public class LuaJTest extends TestCase {
// new lua state
LuaState state = new DebugLuaState();
// add LuaJava bindings
LuajavaLib.install(state._G);
// add standard bindings
state.installStandardLibs();
// add LuaCompat bindings
MathLib.install(state._G);
// add luajava
LuajavaLib.install( state._G );
// load the file
LPrototype p = loadScriptResource( state, testName );

View File

@@ -14,7 +14,7 @@ import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.luaj.debug.DebugLuaState;
import org.luaj.lib.MathLib;
import org.luaj.lib.BaseLib;
public class StandardTest extends TestCase {
@@ -66,7 +66,10 @@ public class StandardTest extends TestCase {
public void runTest() {
LuaState state = new DebugLuaState();
MathLib.install(state._G);
// add standard bindings
state.installStandardLibs();
// hack: it's unpleasant when the test cases fail to terminate;
// unfortunately, there is a test in the standard suite that
// relies on weak tables having their elements removed by