diff --git a/src/core/org/luaj/compiler/Compiler.java b/src/core/org/luaj/compiler/Compiler.java index 1bd21705..fc1f25b1 100644 --- a/src/core/org/luaj/compiler/Compiler.java +++ b/src/core/org/luaj/compiler/Compiler.java @@ -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; diff --git a/src/core/org/luaj/compiler/DumpState.java b/src/core/org/luaj/compiler/DumpState.java index 3c7cc75d..7acad0c0 100644 --- a/src/core/org/luaj/compiler/DumpState.java +++ b/src/core/org/luaj/compiler/DumpState.java @@ -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; diff --git a/src/core/org/luaj/compiler/FuncState.java b/src/core/org/luaj/compiler/FuncState.java index 290bed52..350f8805 100644 --- a/src/core/org/luaj/compiler/FuncState.java +++ b/src/core/org/luaj/compiler/FuncState.java @@ -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; diff --git a/src/core/org/luaj/compiler/InstructionPtr.java b/src/core/org/luaj/compiler/InstructionPtr.java index cb2b9900..4393eba8 100644 --- a/src/core/org/luaj/compiler/InstructionPtr.java +++ b/src/core/org/luaj/compiler/InstructionPtr.java @@ -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 { diff --git a/src/core/org/luaj/compiler/IntPtr.java b/src/core/org/luaj/compiler/IntPtr.java index 4150cfc2..f196e1ff 100644 --- a/src/core/org/luaj/compiler/IntPtr.java +++ b/src/core/org/luaj/compiler/IntPtr.java @@ -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 { diff --git a/src/core/org/luaj/compiler/LexState.java b/src/core/org/luaj/compiler/LexState.java index 18d267af..94af8d00 100644 --- a/src/core/org/luaj/compiler/LexState.java +++ b/src/core/org/luaj/compiler/LexState.java @@ -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; diff --git a/src/core/org/luaj/compiler/LuaC.java b/src/core/org/luaj/compiler/LuaC.java index 60d4fef9..e902660c 100644 --- a/src/core/org/luaj/compiler/LuaC.java +++ b/src/core/org/luaj/compiler/LuaC.java @@ -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; diff --git a/src/core/org/luaj/lib/BaseLib.java b/src/core/org/luaj/lib/BaseLib.java new file mode 100644 index 00000000..f00d69bf --- /dev/null +++ b/src/core/org/luaj/lib/BaseLib.java @@ -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 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"); + } + + + +} diff --git a/src/core/org/luaj/lib/CoroutineLib.java b/src/core/org/luaj/lib/CoroutineLib.java index 3695623f..9edd4876 100644 --- a/src/core/org/luaj/lib/CoroutineLib.java +++ b/src/core/org/luaj/lib/CoroutineLib.java @@ -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) ) diff --git a/src/core/org/luaj/lib/LBuffer.java b/src/core/org/luaj/lib/LBuffer.java index 17e8ee85..05195bbb 100644 --- a/src/core/org/luaj/lib/LBuffer.java +++ b/src/core/org/luaj/lib/LBuffer.java @@ -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; diff --git a/src/core/org/luaj/lib/MathLib.java b/src/core/org/luaj/lib/MathLib.java index f35463d1..21064e40 100644 --- a/src/core/org/luaj/lib/MathLib.java +++ b/src/core/org/luaj/lib/MathLib.java @@ -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=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=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 < 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 ); - } } diff --git a/src/core/org/luaj/lib/PackageLib.java b/src/core/org/luaj/lib/PackageLib.java new file mode 100644 index 00000000..345f6721 --- /dev/null +++ b/src/core/org/luaj/lib/PackageLib.java @@ -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=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=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 < 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; + } +} diff --git a/src/core/org/luaj/vm/BaseLib.java b/src/core/org/luaj/vm/BaseLib.java deleted file mode 100644 index 6e0fc526..00000000 --- a/src/core/org/luaj/vm/BaseLib.java +++ /dev/null @@ -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 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; - } - -} diff --git a/src/core/org/luaj/vm/CallInfo.java b/src/core/org/luaj/vm/CallInfo.java index c832c68a..1f15aa42 100644 --- a/src/core/org/luaj/vm/CallInfo.java +++ b/src/core/org/luaj/vm/CallInfo.java @@ -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; diff --git a/src/core/org/luaj/vm/LBoolean.java b/src/core/org/luaj/vm/LBoolean.java index 07c09065..d04d4691 100644 --- a/src/core/org/luaj/vm/LBoolean.java +++ b/src/core/org/luaj/vm/LBoolean.java @@ -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; diff --git a/src/core/org/luaj/vm/LClosure.java b/src/core/org/luaj/vm/LClosure.java index 1de1269b..3bd7baa5 100644 --- a/src/core/org/luaj/vm/LClosure.java +++ b/src/core/org/luaj/vm/LClosure.java @@ -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; diff --git a/src/core/org/luaj/vm/LDouble.java b/src/core/org/luaj/vm/LDouble.java index 7a514223..df28b63f 100644 --- a/src/core/org/luaj/vm/LDouble.java +++ b/src/core/org/luaj/vm/LDouble.java @@ -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; diff --git a/src/core/org/luaj/vm/LFunction.java b/src/core/org/luaj/vm/LFunction.java index 27670078..c008f8b9 100644 --- a/src/core/org/luaj/vm/LFunction.java +++ b/src/core/org/luaj/vm/LFunction.java @@ -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; diff --git a/src/core/org/luaj/vm/LInteger.java b/src/core/org/luaj/vm/LInteger.java index 30c7d32d..23f6e470 100644 --- a/src/core/org/luaj/vm/LInteger.java +++ b/src/core/org/luaj/vm/LInteger.java @@ -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; diff --git a/src/core/org/luaj/vm/LNil.java b/src/core/org/luaj/vm/LNil.java index 400c9081..c8f607f0 100644 --- a/src/core/org/luaj/vm/LNil.java +++ b/src/core/org/luaj/vm/LNil.java @@ -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; diff --git a/src/core/org/luaj/vm/LNumber.java b/src/core/org/luaj/vm/LNumber.java index ae29037c..01633cd1 100644 --- a/src/core/org/luaj/vm/LNumber.java +++ b/src/core/org/luaj/vm/LNumber.java @@ -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; diff --git a/src/core/org/luaj/vm/LPrototype.java b/src/core/org/luaj/vm/LPrototype.java index 93847a7d..ec25b5db 100644 --- a/src/core/org/luaj/vm/LPrototype.java +++ b/src/core/org/luaj/vm/LPrototype.java @@ -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; diff --git a/src/core/org/luaj/vm/LString.java b/src/core/org/luaj/vm/LString.java index 817f363d..a2622f90 100644 --- a/src/core/org/luaj/vm/LString.java +++ b/src/core/org/luaj/vm/LString.java @@ -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; diff --git a/src/core/org/luaj/vm/LTable.java b/src/core/org/luaj/vm/LTable.java index 4cbc8f70..1a10f205 100644 --- a/src/core/org/luaj/vm/LTable.java +++ b/src/core/org/luaj/vm/LTable.java @@ -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; diff --git a/src/core/org/luaj/vm/LThread.java b/src/core/org/luaj/vm/LThread.java index 864dfb8a..350dc26f 100644 --- a/src/core/org/luaj/vm/LThread.java +++ b/src/core/org/luaj/vm/LThread.java @@ -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; diff --git a/src/core/org/luaj/vm/LUserData.java b/src/core/org/luaj/vm/LUserData.java index ef437b12..b2814876 100644 --- a/src/core/org/luaj/vm/LUserData.java +++ b/src/core/org/luaj/vm/LUserData.java @@ -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; diff --git a/src/core/org/luaj/vm/LValue.java b/src/core/org/luaj/vm/LValue.java index 1b266199..5c2ebc64 100644 --- a/src/core/org/luaj/vm/LValue.java +++ b/src/core/org/luaj/vm/LValue.java @@ -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; diff --git a/src/core/org/luaj/vm/LoadState.java b/src/core/org/luaj/vm/LoadState.java index ff535042..07cd72e1 100644 --- a/src/core/org/luaj/vm/LoadState.java +++ b/src/core/org/luaj/vm/LoadState.java @@ -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; diff --git a/src/core/org/luaj/vm/LocVars.java b/src/core/org/luaj/vm/LocVars.java index ea3e2e77..15cc0690 100644 --- a/src/core/org/luaj/vm/LocVars.java +++ b/src/core/org/luaj/vm/LocVars.java @@ -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; diff --git a/src/core/org/luaj/vm/Lua.java b/src/core/org/luaj/vm/Lua.java index d82b9432..7bb373f4 100644 --- a/src/core/org/luaj/vm/Lua.java +++ b/src/core/org/luaj/vm/Lua.java @@ -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; diff --git a/src/core/org/luaj/vm/LuaState.java b/src/core/org/luaj/vm/LuaState.java index 243a8aa2..5ae783f9 100644 --- a/src/core/org/luaj/vm/LuaState.java +++ b/src/core/org/luaj/vm/LuaState.java @@ -1,13 +1,73 @@ +/******************************************************************************* +* 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.ByteArrayOutputStream; import java.io.InputStream; import java.util.Stack; +import org.luaj.lib.BaseLib; +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; - - - +/** + *
+ *

LuaState

+ * + *
+ * typedef struct LuaState;
+ * 
+ * + *

+ * Opaque structure that keeps the whole state of a Lua interpreter. The Lua + * library is fully reentrant: it has no global variables. All information about + * a state is kept in this structure. + * + * + *

+ * Here we list all functions and types from the C API in alphabetical + * order. Each function has an indicator like this: [-o, +p, + * x] + * + *

+ * The first field, o, is how many elements the function pops + * from the stack. The second field, p, is how many elements the + * function pushes onto the stack. (Any function always pushes its results after + * popping its arguments.) A field in the form x|y means the + * function may push (or pop) x or y elements, + * depending on the situation; an interrogation mark '?' means + * that we cannot know how many elements the function pops/pushes by looking + * only at its arguments (e.g., they may depend on what is on the stack). The + * third field, x, tells whether the function may throw errors: '-' + * means the function never throws any error; 'm' means the + * function may throw an error only due to not enough memory; 'e' + * means the function may throw other kinds of errors; 'v' + * means the function may throw an error on purpose. + * + * + */ public class LuaState extends Lua { /* thread status; 0 is OK */ @@ -55,15 +115,32 @@ public class LuaState extends Lua { public LuaState() { _G = new LTable(); _G.put("_G", _G); - BaseLib.install( _G ); } public LuaState(LTable globals) { _G = globals; } + + /** + * Install the standard set of libraries used by most implementations: + * BaseLib, CoroutineLib, MathLib, PackageLib, TableLib, StringLib + */ + public void installStandardLibs() { + BaseLib.install(_G); + CoroutineLib.install(_G); + MathLib.install(_G); + PackageLib.install(_G); + TableLib.install(_G); + StringLib.install(_G); + } // ================ interfaces for performing calls + /** + * Create a call frame for a call that has been set up on + * the stack. The first value on the stack must be a Closure, + * and subsequent values are arguments to the closure. + */ public void prepStackCall() { LClosure c = (LClosure) stack[base]; int resultbase = base; @@ -96,11 +173,23 @@ public class LuaState extends Lua { cc = newcc; } + /** + * Execute bytecodes until the current call completes + * or the vm yields. + */ public void execute() { for ( int cb=cc; cc>=cb; ) exec(); } + /** + * Put the closure on the stack with arguments, + * then perform the call. Leave return values + * on the stack for later querying. + * + * @param c + * @param values + */ public void doCall( LClosure c, LValue[] args ) { settop(0); pushlvalue( c ); @@ -111,6 +200,10 @@ public class LuaState extends Lua { base = (cc>=0? calls[cc].base: 0); } + /** + * Invoke a LFunction being called via prepStackCall() + * @param javaFunction + */ public void invokeJavaFunction(LFunction javaFunction) { int resultbase = base; int resultsneeded = nresults; @@ -126,7 +219,58 @@ public class LuaState extends Lua { // ================== error processing ================= - // return 0 for success, non-zero for error condition + /** + * Calls a function. [-(nargs + 1), +nresults, e] + * + * + *

+ * To call a function you must use the following protocol: first, the + * function to be called is pushed onto the stack; then, the arguments to + * the function are pushed in direct order; that is, the first argument is + * pushed first. Finally you call lua_call; + * nargs is the number of arguments that you pushed onto the + * stack. All arguments and the function value are popped from the stack + * when the function is called. The function results are pushed onto the + * stack when the function returns. The number of results is adjusted to + * nresults, unless nresults is LUA_MULTRET. In this case, + * all results from the function are pushed. Lua takes care that + * the returned values fit into the stack space. The function results are + * pushed onto the stack in direct order (the first result is pushed first), + * so that after the call the last result is on the top of the stack. + * + * + *

+ * Any error inside the called function is propagated upwards (with a + * longjmp). + * + * + *

+ * The following example shows how the host program may do the equivalent to + * this Lua code: + * + *

+	 * a = f("how", t.x, 14)
+	 * 
+ * + *

+ * Here it is in C: + * + *

+	 * lua_getfield(L, LUA_GLOBALSINDEX, "f"); // function to be called 
+	 * lua_pushstring(L, "how"); // 1st argument 
+	 * lua_getfield(L, LUA_GLOBALSINDEX, "t"); // table to be indexed 
+	 * lua_getfield(L, -1, "x"); // push result of t.x (2nd arg) 
+	 * lua_remove(L, -2); // remove 't' from the stack 
+	 * lua_pushinteger(L, 14); // 3rd argument 
+	 * lua_call(L, 3, 1); // call 'f' with 3 arguments and 1 result 
+	 * lua_setfield(L, LUA_GLOBALSINDEX, "a"); // set global 'a' 
+	 * 
+ * + *

+ * Note that the code above is "balanced": at its end, the stack is back to + * its original configuration. This is considered good programming practice. + */ public void call( int nargs, int nreturns ) { // save stack state int oldbase = base; @@ -152,7 +296,59 @@ public class LuaState extends Lua { this.base = oldbase; } - // return 0 for success, non-zero for error condition + /** + * Calls a function in protected mode. [-(nargs + 1), + * +(nresults|1), -] + * + * + *

+ * Both nargs and nresults have the same + * meaning as in lua_call. If there + * are no errors during the call, lua_pcall + * behaves exactly like lua_call. + * However, if there is any error, lua_pcall + * catches it, pushes a single value on the stack (the error message), and + * returns an error code. Like lua_call, + * lua_pcall always removes the + * function and its arguments from the stack. + * + * + *

+ * If errfunc is 0, then the error message returned on the + * stack is exactly the original error message. Otherwise, + * errfunc is the stack index of an + * error handler function. (In the current implementation, this + * index cannot be a pseudo-index.) In case of runtime errors, this function + * will be called with the error message and its return value will be the + * message returned on the stack by lua_pcall. + * + * + *

+ * Typically, the error handler function is used to add more debug + * information to the error message, such as a stack traceback. Such + * information cannot be gathered after the return of lua_pcall, + * since by then the stack has unwound. + * + * + *

+ * The lua_pcall function returns + * 0 in case of success or one of the following error codes (defined in + * lua.h): + * + *

    + * + *
  • LUA_ERRRUN: a + * runtime error.
  • + * + *
  • LUA_ERRMEM: + * memory allocation error. For such errors, Lua does not call the error + * handler function.
  • + * + *
  • LUA_ERRERR: + * error while running the error handler function.
  • + * + *
+ */ public int pcall( int nargs, int nreturns, int errfunc ) { // save stack state int oldtop = top; @@ -184,7 +380,7 @@ public class LuaState extends Lua { debugPcallError( t ); this.base = oldbase; this.cc = oldcc; - close(oldtop); /* close eventual pending closures */ + closeUpVals(oldtop); /* close eventual pending closures */ String s = t.getMessage(); settop(0); pushstring( s!=null? s: t.toString() ); @@ -192,7 +388,50 @@ public class LuaState extends Lua { } } - // loader + /** + * Loads a Lua chunk. [-0, +1, -] + * + *

+ * If there are no errors, lua_load + * pushes the compiled chunk as a Lua function on top of the stack. + * Otherwise, it pushes an error message. The return values of lua_load are: + * + *

    + * + *
  • 0: no errors;
  • + * + *
  • LUA_ERRSYNTAX: + * syntax error during pre-compilation;
  • + * + *
  • LUA_ERRMEM: + * memory allocation error.
  • + * + *
+ * + *

+ * This function only loads a chunk; it does not run it. + * + * + *

+ * lua_load automatically detects + * whether the chunk is text or binary, and loads it accordingly (see + * program luac). + * + * + *

+ * The lua_load function uses a + * user-supplied reader function to read the chunk (see lua_Reader). The + * data argument is an opaque value passed to the reader + * function. + * + * + *

+ * The chunkname argument gives a name to the chunk, which is + * used for error messages and in debug information (see §3.8). + */ public int load( InputStream is, String chunkname ) { try { LPrototype p = LoadState.undump(this, is, chunkname ); @@ -449,7 +688,7 @@ public class LuaState extends Lua { } case LuaState.OP_TAILCALL: { - close(base); + closeUpVals(base); // copy down the frame before calling! @@ -495,7 +734,7 @@ public class LuaState extends Lua { b = top - (base + a); // close open upvals - close( base ); + closeUpVals( base ); // number to copy down System.arraycopy(stack, base + a, stack, ci.resultbase, b); @@ -581,7 +820,7 @@ public class LuaState extends Lua { continue; } case LuaState.OP_CLOSE: { - close( a ); // close upvals higher in the stack than position a + closeUpVals( a ); // close upvals higher in the stack than position a continue; } case LuaState.OP_CLOSURE: { @@ -643,7 +882,7 @@ public class LuaState extends Lua { return up; } - private void close( int limit ) { + private void closeUpVals( int limit ) { while ( !upvals.empty() && ( (UpVal) this.upvals.lastElement() ).close( limit ) ) { this.upvals.pop(); } @@ -662,62 +901,223 @@ public class LuaState extends Lua { throw new java.lang.RuntimeException("AbstractStack: not yet implemented"); } + /** + * Sets a new panic function and returns the old one. [-0, +0, -] + * + * + *

+ * If an error happens outside any protected environment, Lua calls a + * panic function and then calls exit(EXIT_FAILURE), + * thus exiting the host application. Your panic function may avoid this + * exit by never returning (e.g., doing a long jump). + * + * + *

+ * The panic function can access the error message at the top of the stack. + */ public LFunction atpanic(LFunction panicf) { LFunction f = panic; panic = panicf; return f; } + /** + * Raises an error. The message is pushed onto the stack and used as the error message. + * It also adds at the beginning of the message the file name and the line number where + * the error occurred, if this information is available. + * + * In the java implementation this throws a RuntimeException, possibly filling + * line number information first. + */ public void error(String message, int level) { error( message ); } + /** + * Raises an error with the default level. + * + * In the java implementation this throws a RuntimeException, possibly filling + * line number information first. + */ public void error(String message) { throw new RuntimeException( message ); } + /** + * Generates a Lua error. [-1, +0, v] + * + *

+ * The error message (which can actually be a Lua value of any type) must be + * on the stack top. This function does a long jump, and therefore never + * returns. (see luaL_error). + * + */ public void error() { String message = tostring(-1); pop(1); error( message ); } + /** + * Dumps a function as a binary chunk. [-0, +0, + * m] + * + *

+ * Receives a Lua function on the top of the stack and produces a binary + * chunk that, if loaded again, results in a function equivalent to the one + * dumped. As it produces parts of the chunk, lua_dump + * calls function writer (see lua_Writer) + * with the given data to write them. + *

+ * The value returned is the error code returned by the last call to the + * writer; 0 means no errors. + * + * + *

+ * This function does not pop the Lua function from the stack. + * + */ public void dump() { notImplemented(); } + + /** + * Pushes a new C closure onto the stack. [-n, +1, + * m] + * + * + *

+ * When a Java function is created, it is possible to associate some + * values with it, thus creating a C closure (see §3.4); these values are then accessible to the + * function whenever it is called. To associate values with a + * C function, first these values should be pushed onto the stack (when + * there are multiple values, the first value is pushed first). Then lua_pushcclosure is called + * to create and push the C function onto the stack, with the argument + * n telling how many values should be associated with the + * function. lua_pushcclosure + * also pops these values from the stack. + */ public void pushclosure(LFunction fn, int n) { notImplemented(); } + /** + * Calls the C function func in protected mode. [-0, +(0|1), -] + * + *

+ * func starts with only one element in its stack, a light + * userdata containing ud. In case of errors, lua_cpcall returns the same error + * codes as lua_pcall, plus the + * error object on the top of the stack; otherwise, it returns zero, and + * does not change the stack. All values returned by func are + * discarded. + */ public int javapcall(LFunction func, Object ud) { this.pushjavafunction(func); this.pushlightuserdata(ud); return this.pcall(1, 0, 0); } + /** + * Format and push a string. [-0, +1, m] + * + *

+ * Pushes onto the stack a formatted string and returns a pointer to this + * string. It is similar to the C function sprintf, but + * has some important differences: + * + *

    + * + *
  • You do not have to allocate space for the result: the result is a + * Lua string and Lua takes care of memory allocation (and deallocation, + * through garbage collection).
  • + * + *
  • The conversion specifiers are quite restricted. There are no flags, + * widths, or precisions. The conversion specifiers can only be '%%' + * (inserts a '%' in the string), '%s' + * (inserts a zero-terminated string, with no size restrictions), '%f' + * (inserts a lua_Number), '%p' + * (inserts a pointer as a hexadecimal numeral), '%d' + * (inserts an int), and '%c' (inserts an + * int as a character).
  • + * + *
+ */ public String pushfstring(String fmt, Object[] args) { notImplemented(); return null; } + + /** + * Format and push a string. [-0, +1, m] + * + *

+ * Equivalent to lua_pushfstring, + * except that it receives a va_list instead of a variable + * number of arguments. + */ public void pushvfstring(String format, Object[] args) { notImplemented(); } + /** + * Test if two objects are the same object. [-0, +0, + * -] + * + *

+ * Returns 1 if the two values in acceptable indices index1 + * and index2 are primitively equal (that is, without calling + * metamethods). Otherwise returns 0. Also returns 0 if any of the + * indices are non valid. + */ public void rawequal(int index1, int index2) { notImplemented(); } + /** + * Pushes a value's environment table. [-0, +1, + * -] + * + *

+ * Pushes onto the stack the environment table of the value at the given + * index. + */ public void getfenv(int index) { notImplemented(); } + /** + * Set the environment for a value. [-1, +0, -] + * + *

+ * Pops a table from the stack and sets it as the new environment for the + * value at the given index. If the value at the given index is neither a + * function nor a thread nor a userdata, lua_setfenv + * returns 0. Otherwise it returns 1. + */ public int setfenv(int index) { notImplemented(); return 0; } + /** + * + * Ensures that there are at least extra free stack slots in + * the stack. [-0, +0, -] + * + *

+ * It returns false if it cannot grow the stack to that size. This function + * never shrinks the stack; if the stack is already larger than the new + * size, it is left unchanged. + * + */ public void checkstack(int extra) { if ( top + extra > stack.length ) { int n = Math.max( top + extra + LUA_MINSTACK, stack.length * 2 ); @@ -727,11 +1127,36 @@ public class LuaState extends Lua { } } + + /** + * Closes the given Lua state. [-0, +0, -] + * + *

+ * Destroys all objects in the given Lua state (calling the corresponding + * garbage-collection metamethods, if any) and frees all dynamic memory used + * by this state. On several platforms, you may not need to call this + * function, because all resources are naturally released when the host + * program ends. On the other hand, long-running programs, such as a daemon + * or a web server, might need to release states as soon as they are not + * needed, to avoid growing too large. + */ public void close() { stack = new LValue[20]; base = top = 0; } + /** + * Concatenates the n values at the top of the stack. [-n, +1, e] + * + *

+ * Concatenates the n values at the top of the stack, pops + * them, and leaves the result at the top. If n is 1, + * the result is the single value on the stack (that is, the function does + * nothing); if n is 0, the result is the empty string. + * Concatenation is performed following the usual semantics of Lua (see §2.5.4). + */ public void concat(int n) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); for ( int i=-n; i<0; i++ ) { @@ -742,19 +1167,95 @@ public class LuaState extends Lua { pushlvalue( new LString(baos.toByteArray())); } + /** + * Creates a new empty table and pushes it onto the stack. [-0, +1, m] + * + *

+ * The new table has space pre-allocated for narr array + * elements and nrec non-array elements. This pre-allocation + * is useful when you know exactly how many elements the table will have. + * Otherwise you can use the function lua_newtable. + */ public void createtable(int narr, int nrec) { stack[top++] = new LTable(narr, nrec); } + /** + * Tests if two items on the stack are equal. [-0, +0, + * e] + * + *

+ * Returns 1 if the two values in acceptable indices index1 + * and index2 are equal, following the semantics of the Lua + * == operator (that is, may call metamethods). Otherwise + * returns 0. Also returns 0 if any of the indices is non valid. + * + * + */ public boolean equal(int index1, int index2) { return topointer(index2).luaBinOpUnknown(Lua.OP_EQ, topointer(index1)).toJavaBoolean(); } + /** + * Controls the garbage collector. [-0, +0, e] + * + *

+ * This function performs several tasks, according to the value of the + * parameter what: + * + *

    + * + *
  • LUA_GCSTOP: stops the garbage collector. + *
  • + * + *
  • LUA_GCRESTART: restarts the garbage + * collector.
  • + * + *
  • LUA_GCCOLLECT: performs a full + * garbage-collection cycle.
  • + * + *
  • LUA_GCCOUNT: returns the current amount of + * memory (in Kbytes) in use by Lua.
  • + * + *
  • LUA_GCCOUNTB: returns the remainder of + * dividing the current amount of bytes of memory in use by Lua by 1024. + *
  • + * + *
  • LUA_GCSTEP: performs an incremental step of + * garbage collection. The step "size" is controlled by data + * (larger values mean more steps) in a non-specified way. If you want to + * control the step size you must experimentally tune the value of + * data. The function returns 1 if the step finished a + * garbage-collection cycle.
  • + * + *
  • LUA_GCSETPAUSE: sets data/100 + * as the new value for the pause of the collector (see §2.10). The function returns the previous value of + * the pause.
  • + * + *
  • LUA_GCSETSTEPMUL: sets data/100 + * as the new value for the step multiplier of the collector (see + * §2.10). The function returns the previous value + * of the step multiplier.
  • + * + *
+ */ public void gc(int what, int data) { notImplemented(); } + /** + * Dereference a tables field. [-0, +1, e] + * + *

+ * Pushes onto the stack the value t[k], where + * t is the value at the given valid index. As in Lua, this + * function may trigger a metamethod for the "index" event (see §2.8). + * + */ public void getfield(int index, String k) { LTable t = totable(index); // TODO: what if this triggers metatable ops @@ -762,6 +1263,18 @@ public class LuaState extends Lua { t.luaGetTable(this, t, new LString(k)); } + /** + * Look up a global value. [-0, +1, e] + * + *

+ * Pushes onto the stack the value of the global name. It is + * defined as a macro: + * + *

+	 * 	 #define lua_getglobal(L,s)  lua_getfield(L, LUA_GLOBALSINDEX, s)
+	 * 	
+	 * 
+ */ public void getglobal(String s) { LTable t = this._G; // TODO: what if this triggers metatable ops @@ -769,6 +1282,14 @@ public class LuaState extends Lua { t.luaGetTable(this, t, new LString(s)); } + /** + * Get a value's metatable. [-0, +(0|1), -] + * + *

+ * Pushes onto the stack the metatable of the value at the given acceptable + * index. If the index is not valid, or if the value does not have a + * metatable, the function returns 0 and pushes nothing on the stack. + */ public int getmetatable(int index) { LTable mt = topointer(index).luaGetMetatable(); if ( mt != null ) { @@ -778,6 +1299,20 @@ public class LuaState extends Lua { return 0; } + /** + * Dereference a table's list element. [-1, +1, + * e] + * + *

+ * Pushes onto the stack the value t[k], where + * t is the value at the given valid index and k + * is the value at the top of the stack. + * + *

+ * This function pops the key from the stack (putting the resulting value in + * its place). As in Lua, this function may trigger a metamethod for the + * "index" event (see §2.8). + */ public void gettable(int index) { LValue t = totable(index); LValue k = poplvalue(); @@ -786,6 +1321,16 @@ public class LuaState extends Lua { t.luaGetTable(this, t, k); } + /** + * Insert the top item somewhere in the stack. [-1, +1, + * -] + * + *

+ * Moves the top element into the given valid index, shifting up the + * elements above this index to open space. Cannot be called with a + * pseudo-index, because a pseudo-index is not an actual stack position. + * + */ public void insert(int index) { int ai = index2adr(index); LValue v = stack[top-1]; @@ -793,81 +1338,272 @@ public class LuaState extends Lua { stack[ai] = v; } + /** + * Test if a value is boolean. [-0, +0, -] + * + *

+ * Returns 1 if the value at the given acceptable index has type boolean, + * and 0 otherwise. + * + */ public boolean isboolean(int index) { return type(index) == Lua.LUA_TBOOLEAN; } + /** + * Test if a value is a function. [-0, +0, -] + * + *

+ * Returns true if the value at the given acceptable index is a function + * (either C or Lua), and false otherwise. + * + */ public boolean isfunction(int index) { return type(index) == Lua.LUA_TFUNCTION; } + /** + * Test if a value is a JavaFunction. [-0, +0, -] + * + *

+ * Returns 1 if the value at the given acceptable index is a + * C function, and 0 otherwise. + * + */ public boolean isjavafunction(int index) { return topointer(index) instanceof LFunction; } + /** + * Test if a value is light user data [-0, +0, -] + * + *

+ * Returns 1 if the value at the given acceptable index is a light userdata, + * and 0 otherwise. + */ public boolean islightuserdata(int index) { return false; } + /** + * Test if a value is nil [-0, +0, -] + * + *

+ * Returns 1 if the value at the given acceptable index is nil, and + * 0 otherwise. + */ public boolean isnil(int index) { return topointer(index) == LNil.NIL; } + /** + * Test if a value is not valid [-0, +0, -] + * + *

+ * Returns 1 if the the given acceptable index is not valid (that is, it + * refers to an element outside the current stack), and 0 otherwise. + */ public boolean isnone(int index) { return topointer(index) == null; } + /** + * Test if a value is nil or not valid [-0, +0, + * -] + * + *

+ * Returns 1 if the the given acceptable index is not valid (that is, it + * refers to an element outside the current stack) or if the value at this + * index is nil, and 0 otherwise. + */ public boolean isnoneornil(int index) { Object v = topointer(index); return v == null || v == LNil.NIL; } + /** + * Test if a value is a number [-0, +0, -] + * + *

+ * Returns 1 if the value at the given acceptable index is a number or a + * string convertible to a number, and 0 otherwise. + */ public boolean isnumber(int index) { return type(index) == Lua.LUA_TNUMBER; } + /** + * Test if a value is a string [-0, +0, m] + * + *

+ * Returns 1 if the value at the given acceptable index is a string or a + * number (which is always convertible to a string), and 0 otherwise. + */ public boolean isstring(int index) { return type(index) == Lua.LUA_TSTRING; } + /** + * Test if a value is a table [-0, +0, -] + * + *

+ * Returns 1 if the value at the given acceptable index is a table, and + * 0 otherwise. + */ public boolean istable(int index) { return type(index) == Lua.LUA_TTABLE; } + /** + * Test if a value is a thread [-0, +0, -] + * + *

+ * Returns 1 if the value at the given acceptable index is a thread, and + * 0 otherwise. + */ public boolean isthread(int index) { return type(index) == Lua.LUA_TTHREAD; } + /** + * Test if a value is a userdata [-0, +0, -] + * + *

+ * Returns 1 if the value at the given acceptable index is a userdata + * (either full or light), and 0 otherwise. + */ public boolean isuserdata(int index) { return type(index) == Lua.LUA_TUSERDATA; } + /** + * Compare two values [-0, +0, e] + * + *

+ * Returns 1 if the value at acceptable index index1 is + * smaller than the value at acceptable index index2, + * following the semantics of the Lua < operator (that is, + * may call metamethods). Otherwise returns 0. Also returns 0 if + * any of the indices is non valid. + */ public boolean lessthan(int index1, int index2) { return topointer(index2).luaBinOpUnknown(Lua.OP_LT, topointer(index1)).toJavaBoolean(); } + /** + * Create a table [-0, +1, m] + * + *

+ * Creates a new empty table and pushes it onto the stack. It is equivalent + * to lua_createtable(L, 0, 0). + */ public void newtable() { stack[top++] = new LTable(); } + /** + * Create a thread [-0, +1, m] + * + *

+ * Creates a new thread, pushes it on the stack, and returns a pointer to a + * lua_State that represents this + * new thread. The new state returned by this function shares with the + * original state all global objects (such as tables), but has an + * independent execution stack. + * + * + *

+ * There is no explicit function to close or to destroy a thread. Threads + * are subject to garbage collection, like any Lua object. + */ public void newthread() { notImplemented(); } + /** + * Create a userdata [-0, +1, m] + * + *

+ * This function allocates a new block of memory with the given size, pushes + * onto the stack a new full userdata with the block address, and returns + * this address. + * + * + *

+ * Userdata represent C values in Lua. A full userdata + * represents a block of memory. It is an object (like a table): you must + * create it, it can have its own metatable, and you can detect when it is + * being collected. A full userdata is only equal to itself (under raw + * equality). + * + * + *

+ * When Lua collects a full userdata with a gc metamethod, + * Lua calls the metamethod and marks the userdata as finalized. When this + * userdata is collected again then Lua frees its corresponding memory. + */ public void newuserdata(Object o) { stack[top++] = new LUserData(o); } + /** + * Traverse to the next table item. [-1, +(2|0), + * e] + * + *

+ * Pops a key from the stack, and pushes a key-value pair from the table at + * the given index (the "next" pair after the given key). If there are no + * more elements in the table, then lua_next + * returns 0 (and pushes nothing). + * + * + *

+ * A typical traversal looks like this: + * + *

+	 * // table is in the stack at index 't' 
+	 * lua_pushnil(L); // first key 
+	 * while (lua_next(L, t) != 0) {
+	 * 	// uses 'key' (at index -2) and 'value' (at index -1) 
+	 * 	printf("%s - %s\n", lua_typename(L, lua_type(L, -2)), lua_typename(L,
+	 * 			lua_type(L, -1)));
+	 * 	// removes 'value'; keeps 'key' for next iteration 
+	 * 	lua_pop(L, 1);
+	 * }
+	 * 
+ * + *

+ * While traversing a table, do not call lua_tolstring + * directly on a key, unless you know that the key is actually a string. + * Recall that lua_tolstring + * changes the value at the given index; this confuses the next + * call to lua_next. + */ public int next(int index) { notImplemented(); return 0; } + /** + * Get the length of an object [-0, +0, -] + * + *

+ * Returns the "length" of the value at the given acceptable index: for + * strings, this is the string length; for tables, this is the result of the + * length operator ('#'); for userdata, this is the size of + * the block of memory allocated for the userdata; for other values, it + * is 0. + */ public int objlen(int index) { return tostring(index).length(); } + + /** + * Pops n elements from the stack. [-n, + * +0, -] + */ public void pop(int n) { for ( int i=0; i[-0, +1, + * m] + */ public void pushlvalue(LValue value) { if ( value == null ) throw new java.lang.IllegalArgumentException("stack values cannot be null"); stack[top++] = value; } + /** + * Pushes a boolean value with value b onto the stack. [-0, +1, -] + * + */ public void pushboolean(boolean b) { pushlvalue( LBoolean.valueOf(b) ); } - + /** + * Pushes a number with value n onto the stack. [-0, +1, -] + */ public void pushinteger(int n) { pushlvalue( LInteger.valueOf(n) ); } + /** + * Pushes a Java function onto the stack. [-0, +1, + * m] + * + *

+ * This function receives a pointer to a C function and pushes onto the + * stack a Lua value of type function that, when called, + * invokes the corresponding C function. + * + * + *

+ * Any function to be registered in Lua must follow the correct protocol to + * receive its parameters and return its results (see lua_CFunction). + * + */ public void pushjavafunction(LFunction f) { pushlvalue( f ); } + /** + * Pushes a light userdata onto the stack. [-0, +1, + * -] + * + * + *

+ * Userdata represent C values in Lua. A light userdata + * represents a pointer. It is a value (like a number): you do not create + * it, it has no individual metatable, and it is not collected (as it was + * never created). A light userdata is equal to "any" light userdata with + * the same C address. + */ public void pushlightuserdata(Object p) { notImplemented(); } + /** + * Push an LString onto the stack. [-0, +1, + * m] + */ public void pushlstring(LString s) { pushlvalue(s); } + /** + * Push string bytes onto the stack as a string. [-0, +1, + * m] + * + * Pushes the string pointed to by s with size + * len onto the stack. Lua makes (or reuses) an internal copy + * of the given string, so the memory at s can be freed or + * reused immediately after the function returns. The string can contain + * embedded zeros. + */ public void pushlstring(byte[] bytes, int offset, int length) { pushlvalue(new LString(bytes, offset, length)); } + /** + * Push string bytes onto the stack as a string. [-0, +1, + * m] + * + * Pushes the bytes in byteArray onto the stack as a lua string. + */ public void pushlstring(byte[] byteArray) { pushlstring(byteArray, 0, byteArray.length); } + /** + * Pushes a nil value onto the stack. [-0, +1, -] + * + */ public void pushnil() { pushlvalue(LNil.NIL); } + /** + * Pushes a number with value d onto the stack. [-0, +1, -] + * + */ public void pushnumber(double d) { pushlvalue(new LDouble(d)); } + /** + * Push a String onto the stack. [-0, +1, m] + * + *

+ * Pushes the String s onto the stack. Lua makes (or reuses) + * an internal copy of the given string, so the memory at s + * can be freed or reused immediately after the function returns. The string + * cannot contain embedded zeros; it is assumed to end at the first zero. + */ public void pushstring(String s) { if ( s == null ) pushnil(); @@ -930,22 +1744,60 @@ public class LuaState extends Lua { pushlstring( LString.valueOf(s) ); } + /** + * Push a thread onto the stack. [-0, +1, -] + * + * Pushes the thread represented by L onto the stack. Returns + * 1 if this thread is the main thread of its state. + */ public void pushthread() { notImplemented(); } + /** + * Push a value from the stack onto the stack. [-0, +1, + * -] + * + *

+ * Pushes a copy of the element at the given valid index onto the stack. + */ public void pushvalue(int index) { pushlvalue(topointer(index)); } + /** + * Do a table get without metadata calls. [-1, +1, + * -] + * + *

+ * Similar to lua_gettable, but + * does a raw access (i.e., without metamethods). + */ public void rawget(int index) { pushlvalue( totable(index).get(poplvalue()) ); } + /** + * Do a integer-key table get without metadata calls. [-0, +1, -] + * + *

+ * Pushes onto the stack the value t[n], where + * t is the value at the given valid index. The access is + * raw; that is, it does not invoke metamethods. + */ public void rawgeti(int index, int n) { pushlvalue( totable(index).get(n) ); } + /** + * Do a table set without metadata calls. [-2, +0, + * m] + * + *

+ * Similar to lua_settable, but + * does a raw assignment (i.e., without metamethods). + */ public void rawset(int index) { LTable t = totable(index); LValue v = poplvalue(); @@ -953,50 +1805,166 @@ public class LuaState extends Lua { t.put(k,v); } + /** + * Do a integer-key table set without metadata calls. [-1, +0, m] + * + *

+ * Does the equivalent of t[n] = v, where t + * is the value at the given valid index and v is the value + * at the top of the stack. + * + * + *

+ * This function pops the value from the stack. The assignment is raw; that + * is, it does not invoke metamethods. + */ public void rawseti(int index, int n) { LTable t = totable(index); LValue v = poplvalue(); t.put(n,v); } + /** + * Register a LFunction with a specific name. [-0, +0, + * m] + * + *

+ * Sets the function f as the new value of global + * name. It is defined as a macro: + * + *

+	 * 	 #define lua_register(L,n,f) \
+	 * 	 (lua_pushcfunction(L, f), lua_setglobal(L, n))
+	 * 	
+	 * 
+ */ public void register(String name, LFunction f) { pushjavafunction(f); setglobal(name); } + /** + * Remove an element from the stack. [-1, +0, -] + * + *

+ * Removes the element at the given valid index, shifting down the elements + * above this index to fill the gap. Cannot be called with a pseudo-index, + * because a pseudo-index is not an actual stack position. + */ public void remove(int index) { int ai = index2adr(index); System.arraycopy(stack, ai+1, stack, ai, top-ai-1); --top; } + /** + * Replace an element on the stack. [-1, +0, -] + * + *

+ * Moves the top element into the given position (and pops it), without + * shifting any element (therefore replacing the value at the given + * position). + */ public void replace(int index) { int ai = index2adr(index); stack[ai] = poplvalue(); } + /** + * Starts and resumes a coroutine in a given thread. [-?, + * +?, -] + * + * + *

+ * To start a coroutine, you first create a new thread (see lua_newthread); then you push + * onto its stack the main function plus any arguments; then you call lua_resume, with + * narg being the number of arguments. This call returns when + * the coroutine suspends or finishes its execution. When it returns, the + * stack contains all values passed to lua_yield, + * or all values returned by the body function. lua_resume + * returns LUA_YIELD if the + * coroutine yields, 0 if the coroutine finishes its execution without + * errors, or an error code in case of errors (see lua_pcall). + * In case of errors, the stack is not unwound, so you can use the debug API + * over it. The error message is on the top of the stack. To restart a + * coroutine, you put on its stack only the values to be passed as results + * from yield, and then call lua_resume. + */ public void resume(int narg) { notImplemented(); } + /** + * Set the value of a table field. [-1, +0, e] + * + *

+ * Does the equivalent to t[k] = v, where t + * is the value at the given valid index and v is the value + * at the top of the stack. + * + * + *

+ * This function pops the value from the stack. As in Lua, this function may + * trigger a metamethod for the "newindex" event (see §2.8). + */ public void setfield(int index, String k) { LTable t = totable(index); LValue v = poplvalue(); t.luaSetTable(this, t, new LString(k), v); } + /** + * Set the value of a global variable. [-1, +0, + * e] + * + *

+ * Pops a value from the stack and sets it as the new value of global + * name. It is defined as a macro: + * + *

+	 * 	 #define lua_setglobal(L,s)   lua_setfield(L, LUA_GLOBALSINDEX, s)
+	 * 	
+	 * 
+ */ public void setglobal(String name) { LTable g = this._G; LValue v = poplvalue(); g.luaSetTable(this, g, new LString(name), v); } + /** + * Set the metatable of a value. [-1, +0, -] + * + *

+ * Pops a table from the stack and sets it as the new metatable for the + * value at the given acceptable index. + */ public void setmetatable(int index) { LTable t = totable(index); LValue v = poplvalue(); t.luaSetMetatable(v); } + /** + * Set the value of a table for a key. [-2, +0, + * e] + * + *

+ * Does the equivalent to t[k] = v, where t + * is the value at the given valid index, v is the value at + * the top of the stack, and k is the value just below the + * top. + * + * + *

+ * This function pops both the key and the value from the stack. As in Lua, + * this function may trigger a metamethod for the "newindex" event (see §2.8). + */ public void settable(int index) { LTable t = totable(index); LValue v = poplvalue(); @@ -1004,39 +1972,156 @@ public class LuaState extends Lua { t.luaSetTable(this, t, k, v); } + /** + * Returns the status of the thread L. [-0, +0, -] + * + * + * + *

+ * The status can be 0 for a normal thread, an error code if the thread + * finished its execution with an error, or LUA_YIELD + * if the thread is suspended. + * + */ public void status() { notImplemented(); } + /** + * Get a thread value from the stack. [-0, +0, -] + * + *

+ * Converts the value at the given acceptable index to a Lua thread + * (represented as lua_State*). This value must be a thread; + * otherwise, the function returns NULL. + */ public LuaState tothread(int index) { notImplemented(); return null; } + /** + * Get a value as a boolean. [-0, +0, -] + * + *

+ * Converts the Lua value at the given acceptable index to a C boolean + * value (0 or 1). Like all tests in Lua, lua_toboolean returns 1 for + * any Lua value different from false and nil; otherwise it + * returns 0. It also returns 0 when called with a non-valid index. (If you + * want to accept only actual boolean values, use lua_isboolean + * to test the value's type.) + * + */ public boolean toboolean(int index) { return topointer(index).toJavaBoolean(); } + /** + * Get a value as an int. [-0, +0, -] + * + *

+ * Converts the Lua value at the given acceptable index to the signed + * integral type lua_Integer. + * The Lua value must be a number or a string convertible to a number (see + * §2.2.1); otherwise, lua_tointeger + * returns 0. + * + * + *

+ * If the number is not an integer, it is truncated in some non-specified + * way. + */ public int tointeger(int index) { return topointer(index).toJavaInt(); } + /** + * Get a value as a JavaFunction. + *


+ *

lua_tocfunction

+ *

+ * [-0, +0, -] + * + *

+	 * lua_CFunction lua_tocfunction (lua_State *L, int index);
+	 * 
+ * + *

+ * Converts a value at the given acceptable index to a C function. That + * value must be a C function; otherwise, returns NULL. + */ public LFunction tojavafunction(int index) { return (LFunction) topointer(index); } + /** + * Gets the value of a string as byte array. [-0, +0, + * m] + * + *

+ * Converts the Lua value at the given acceptable index to a C string. + * If len is not NULL, it also sets + * *len with the string length. The Lua value must be a + * string or a number; otherwise, the function returns NULL. + * If the value is a number, then lua_tolstring + * also changes the actual value in the stack to a string. (This + * change confuses lua_next when lua_tolstring is applied to + * keys during a table traversal.) + * + * + *

+ * lua_tolstring returns a + * fully aligned pointer to a string inside the Lua state. This string + * always has a zero ('\0') after its last character (as + * in C), but may contain other zeros in its body. Because Lua has + * garbage collection, there is no guarantee that the pointer returned by lua_tolstring will be valid + * after the corresponding value is removed from the stack. + */ public LString tolstring(int index) { return topointer(index).luaAsString(); } + /** + * Convert a value to a double. [-0, +0, -] + * + *

+ * Converts the Lua value at the given acceptable index to the C type + * lua_Number (see lua_Number). The Lua value must + * be a number or a string convertible to a number (see §2.2.1); otherwise, lua_tonumber + * returns 0. + * + */ public double tonumber(int index) { return topointer(index).toJavaDouble(); } + /** + * Returns the index of the top element in the stack. [-0, +0, -] + * + *

+ * Because indices start at 1, this result is equal to the number of + * elements in the stack (and so 0 means an empty stack). + */ public int gettop() { return top - base; } + /** + * Set the top of the stack. [-?, +?, -] + * + *

+ * Accepts any acceptable index, or 0, and sets the stack top to this + * index. If the new top is larger than the old one, then the new elements + * are filled with nil. If index is 0, then all + * stack elements are removed. + */ public void settop(int nt) { int ant = nt>=0? base+nt: top+nt; if ( ant < base ) @@ -1055,6 +2140,22 @@ public class LuaState extends Lua { return ai; } + /** + * Get the raw Object at a stack location. [-0, +0, + * -] + * + *

+ * Converts the value at the given acceptable index to a generic + * C pointer (void*). The value may be a userdata, a + * table, a thread, or a function; otherwise, lua_topointer + * returns NULL. Different objects will give different + * pointers. There is no way to convert the pointer back to its original + * value. + * + * + *

+ * Typically this function is used only for debug information. + */ public LValue topointer(int index) { int ai = index2adr(index); if ( ai >= top ) @@ -1062,14 +2163,38 @@ public class LuaState extends Lua { return stack[ai]; } + /** + * Get a stack value as a String. [-0, +0, m] + * + *

+ * Equivalent to lua_tolstring + * with len equal to NULL. + */ public String tostring(int index) { return topointer(index).toJavaString(); } + /** + * Get a value from the stack as a lua table. [-0, +0, -] + * + *

+ * Converts the value at the given acceptable index to a Lua table + * This value must be a table otherwise, the function returns NIL. + */ public LTable totable(int index) { return (LTable) topointer(index); } + /** + * Get the Object from a userdata value. [-0, +0, + * -] + * + *

+ * If the value at the given acceptable index is a full userdata, returns + * its block address. If the value is a light userdata, returns its pointer. + * Otherwise, returns NULL. + * + */ public Object touserdata(int index) { LValue v = topointer(index); if ( v.luaGetType() != Lua.LUA_TUSERDATA ) @@ -1077,14 +2202,47 @@ public class LuaState extends Lua { return ((LUserData)v).m_instance; } + /** + * Get the type of a value. [-0, +0, -] + * + *

+ * Returns the type of the value in the given acceptable index, or + * LUA_TNONE for a non-valid index (that is, an index to an + * "empty" stack position). The types returned by lua_type + * are coded by the following constants defined in lua.h: + * LUA_TNIL, LUA_TNUMBER, + * LUA_TBOOLEAN, LUA_TSTRING, + * LUA_TTABLE, LUA_TFUNCTION, + * LUA_TUSERDATA, LUA_TTHREAD, and + * LUA_TLIGHTUSERDATA. + */ public int type(int index) { return topointer(index).luaGetType(); } + /** + * Get the type name for a value. [-0, +0, -] + * + *

+ * Returns the name of the type encoded by the value tp, + * which must be one the values returned by lua_type. + */ public String typename(int index) { return topointer(index).luaGetTypeName().toJavaString(); } + /** + * Exchange values between threads. [-?, +?, -] + * + *

+ * Exchange values between different threads of the same global + * state. + * + * + *

+ * This function pops n values from the stack + * from, and pushes them onto the stack to. + */ public void xmove(LuaState to, int n) { if ( n > 0 ) { to.checkstack(n); @@ -1094,12 +2252,37 @@ public class LuaState extends Lua { } } + /** + * Yields a coroutine. [-?, +?, -] + * + * + *

+ * This function should only be called as the return expression of a + * C function, as follows: + * + *

+	 * return lua_yield(L, nresults);
+	 * 
+ * + *

+ * When a C function calls lua_yield + * in that way, the running coroutine suspends its execution, and the call + * to lua_resume that started + * this coroutine returns. The parameter nresults is the + * number of values from the stack that are passed as results to lua_resume. + * + */ public void yield(int nresults) { notImplemented(); } // ============================= conversion to and from Java boxed types ==================== + /** + * Push a Java Boolean value, or nil if the value is null. + * @param b Boolean value to convert, or null to to nil. + */ public void pushboolean(Boolean b) { if ( b == null ) pushnil(); @@ -1107,6 +2290,10 @@ public class LuaState extends Lua { pushboolean( b.booleanValue() ); } + /** + * Push a Java Byte value, or nil if the value is null. + * @param b Byte value to convert, or null to to nil. + */ public void pushinteger(Byte b) { if ( b == null ) pushnil(); @@ -1114,6 +2301,10 @@ public class LuaState extends Lua { pushinteger( b.byteValue() ); } + /** + * Push a Java Character value, or nil if the value is null. + * @param c Character value to convert, or null to to nil. + */ public void pushinteger(Character c) { if ( c == null ) pushnil(); @@ -1121,6 +2312,10 @@ public class LuaState extends Lua { pushinteger( c.charValue() ); } + /** + * Push a Java Double as a double, or nil if the value is null. + * @param d Double value to convert, or null to to nil. + */ public void pushnumber(Double d) { if ( d == null ) pushnil(); @@ -1128,6 +2323,10 @@ public class LuaState extends Lua { pushnumber( d.doubleValue() ); } + /** + * Push a Java Float value, or nil if the value is null. + * @param f Float value to convert, or null to to nil. + */ public void pushnumber(Float f) { if ( f == null ) pushnil(); @@ -1135,6 +2334,10 @@ public class LuaState extends Lua { pushnumber( f.doubleValue() ); } + /** + * Push a Java Integer value, or nil if the value is null. + * @param i Integer value to convert, or null to to nil. + */ public void pushinteger(Integer i) { if ( i == null ) pushnil(); @@ -1142,6 +2345,10 @@ public class LuaState extends Lua { pushinteger( i.intValue() ); } + /** + * Push a Java Short value, or nil if the value is null. + * @param s Short value to convert, or null to to nil. + */ public void pushinteger(Short s) { if ( s == null ) pushnil(); @@ -1149,6 +2356,10 @@ public class LuaState extends Lua { pushinteger( s.shortValue() ); } + /** + * Push a Java Long value, or nil if the value is null. + * @param l Long value to convert, or null to to nil. + */ public void pushnumber(Long l) { if ( l == null ) pushnil(); @@ -1156,6 +2367,10 @@ public class LuaState extends Lua { pushnumber( l.doubleValue() ); } + /** + * Push a Java Object as userdata, or nil if the value is null. + * @param o Object value to push, or null to to nil. + */ public void pushuserdata( Object o ) { if ( o == null ) pushnil(); @@ -1163,26 +2378,57 @@ public class LuaState extends Lua { newuserdata( o ); } + + /** + * Convert a value to a Java Boolean value, or null if the value is nil. + * @param index index of the parameter to convert. + * @return Boolean value at the index, or null if the value was not a boolean. + */ public Boolean toboxedboolean(int index) { return topointer(index).toJavaBoxedBoolean(); } + /** + * Convert a value to a Java Byte value, or null if the value is not a number. + * @param index index of the parameter to convert. + * @return Byte value at the index, or null if the value was not a number. + */ public Byte toboxedbyte(int index) { return topointer(index).toJavaBoxedByte(); } + /** + * Convert a value to a Java Double value, or null if the value is not a number. + * @param index index of the parameter to convert. + * @return Double value at the index, or null if the value was not a number. + */ public Double toboxeddouble(int index) { return topointer(index).toJavaBoxedDouble(); } + /** + * Convert a value to a Java Float value, or null if the value is not a number. + * @param index index of the parameter to convert. + * @return Float value at the index, or null if the value was not a boolean. + */ public Float toboxedfloat(int index) { return topointer(index).toJavaBoxedFloat(); } + /** + * Convert a value to a Java Integer value, or null if the value is not a number. + * @param index index of the parameter to convert. + * @return Integer value at the index, or null if the value was not a number. + */ public Integer toboxedinteger(int index) { return topointer(index).toJavaBoxedInteger(); } + /** + * Convert a value to a Java Long value, or null if the value is nil. + * @param index index of the parameter to convert. + * @return Long value at the index, or null if the value was not a number. + */ public Long toboxedlong(int index) { return topointer(index).toJavaBoxedLong(); } diff --git a/src/core/org/luaj/vm/Platform.java b/src/core/org/luaj/vm/Platform.java index bc99945d..426886b8 100644 --- a/src/core/org/luaj/vm/Platform.java +++ b/src/core/org/luaj/vm/Platform.java @@ -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; diff --git a/src/core/org/luaj/vm/UpVal.java b/src/core/org/luaj/vm/UpVal.java index 56b4ddbc..42b6cc27 100644 --- a/src/core/org/luaj/vm/UpVal.java +++ b/src/core/org/luaj/vm/UpVal.java @@ -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; diff --git a/src/debug/org/luaj/debug/j2se/StandardLuaJVM.java b/src/debug/org/luaj/debug/j2se/StandardLuaJVM.java index f1e650fc..cc03ab9b 100644 --- a/src/debug/org/luaj/debug/j2se/StandardLuaJVM.java +++ b/src/debug/org/luaj/debug/j2se/StandardLuaJVM.java @@ -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() + "'"); diff --git a/src/j2se/org/luaj/lib/j2se/CoerceJavaToLua.java b/src/j2se/org/luaj/lib/j2se/CoerceJavaToLua.java index c54dd12c..29117dc0 100644 --- a/src/j2se/org/luaj/lib/j2se/CoerceJavaToLua.java +++ b/src/j2se/org/luaj/lib/j2se/CoerceJavaToLua.java @@ -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; diff --git a/src/j2se/org/luaj/lib/j2se/CoerceLuaToJava.java b/src/j2se/org/luaj/lib/j2se/CoerceLuaToJava.java index 56aa6f3d..048e817f 100644 --- a/src/j2se/org/luaj/lib/j2se/CoerceLuaToJava.java +++ b/src/j2se/org/luaj/lib/j2se/CoerceLuaToJava.java @@ -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; diff --git a/src/j2se/org/luaj/lib/j2se/LuajavaLib.java b/src/j2se/org/luaj/lib/j2se/LuajavaLib.java index 86d905a3..15ad3b51 100644 --- a/src/j2se/org/luaj/lib/j2se/LuajavaLib.java +++ b/src/j2se/org/luaj/lib/j2se/LuajavaLib.java @@ -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; diff --git a/src/sample/org/luaj/sample/LuaRunner.java b/src/sample/org/luaj/sample/LuaRunner.java index 00941d0c..ceaefc92 100644 --- a/src/sample/org/luaj/sample/LuaRunner.java +++ b/src/sample/org/luaj/sample/LuaRunner.java @@ -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,10 +54,9 @@ 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 ); LPrototype p = LoadState.undump(state, is, script); diff --git a/src/sample/org/luaj/sample/LuajavaRunner.java b/src/sample/org/luaj/sample/LuajavaRunner.java index 1fcd82a7..2689f7a8 100644 --- a/src/sample/org/luaj/sample/LuajavaRunner.java +++ b/src/sample/org/luaj/sample/LuajavaRunner.java @@ -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,9 +51,9 @@ 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); diff --git a/src/sample/org/luaj/sample/SampleClass.java b/src/sample/org/luaj/sample/SampleClass.java index d38f480a..bdb21d6c 100644 --- a/src/sample/org/luaj/sample/SampleClass.java +++ b/src/sample/org/luaj/sample/SampleClass.java @@ -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 { diff --git a/src/test/java/org/luaj/compiler/SimpleTests.java b/src/test/java/org/luaj/compiler/SimpleTests.java index e6c6f35a..1db2c739 100644 --- a/src/test/java/org/luaj/compiler/SimpleTests.java +++ b/src/test/java/org/luaj/compiler/SimpleTests.java @@ -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 ) { diff --git a/src/test/java/org/luaj/vm/LuaJTest.java b/src/test/java/org/luaj/vm/LuaJTest.java index d6a8bab6..8fbe079e 100644 --- a/src/test/java/org/luaj/vm/LuaJTest.java +++ b/src/test/java/org/luaj/vm/LuaJTest.java @@ -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 LuaCompat bindings - MathLib.install(state._G); + // add standard bindings + state.installStandardLibs(); + + // add luajava + LuajavaLib.install( state._G ); // load the file LPrototype p = loadScriptResource( state, testName ); diff --git a/src/test/java/org/luaj/vm/StandardTest.java b/src/test/java/org/luaj/vm/StandardTest.java index bcfe20b7..1f5d88a2 100644 --- a/src/test/java/org/luaj/vm/StandardTest.java +++ b/src/test/java/org/luaj/vm/StandardTest.java @@ -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