├── src ├── CVS │ ├── Entries │ ├── Repository │ ├── Root │ └── Entries.Log ├── jme │ ├── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ └── org │ │ ├── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ │ └── luaj │ │ ├── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ │ └── vm2 │ │ ├── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ │ └── lib │ │ ├── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ │ └── jme │ │ └── CVS │ │ ├── Repository │ │ ├── Root │ │ └── Entries ├── core │ ├── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ └── org │ │ ├── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ │ └── luaj │ │ ├── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ │ └── vm2 │ │ ├── CVS │ │ ├── Repository │ │ ├── Entries.Log │ │ ├── Root │ │ └── Entries │ │ ├── lib │ │ ├── CVS │ │ │ ├── Repository │ │ │ ├── Root │ │ │ └── Entries │ │ ├── ResourceFinder.java │ │ ├── ZeroArgFunction.java │ │ ├── OneArgFunction.java │ │ ├── TwoArgFunction.java │ │ ├── ThreeArgFunction.java │ │ └── VarArgFunction.java │ │ ├── compiler │ │ ├── CVS │ │ │ ├── Repository │ │ │ ├── Root │ │ │ └── Entries │ │ ├── IntPtr.java │ │ └── InstructionPtr.java │ │ ├── LocVars.java │ │ ├── UpValue.java │ │ ├── LuaFunction.java │ │ ├── LuaNumber.java │ │ ├── Prototype.java │ │ ├── TailcallVarargs.java │ │ ├── LuaBoolean.java │ │ ├── LuaNil.java │ │ └── LuaUserdata.java └── jse │ ├── org │ ├── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ └── luaj │ │ ├── vm2 │ │ ├── CVS │ │ │ ├── Entries │ │ │ ├── Repository │ │ │ ├── Root │ │ │ └── Entries.Log │ │ ├── lib │ │ │ ├── CVS │ │ │ │ ├── Entries │ │ │ │ ├── Repository │ │ │ │ └── Root │ │ │ └── jse │ │ │ │ ├── CVS │ │ │ │ ├── Repository │ │ │ │ ├── Root │ │ │ │ └── Entries │ │ │ │ ├── JseBaseLib.java │ │ │ │ └── JseMathLib.java │ │ ├── ast │ │ │ ├── CVS │ │ │ │ ├── Repository │ │ │ │ ├── Root │ │ │ │ └── Entries │ │ │ ├── Name.java │ │ │ ├── Chunk.java │ │ │ ├── TableConstructor.java │ │ │ ├── FuncBody.java │ │ │ ├── Block.java │ │ │ ├── ParList.java │ │ │ ├── FuncName.java │ │ │ ├── TableField.java │ │ │ ├── FuncArgs.java │ │ │ ├── Variable.java │ │ │ ├── NameScope.java │ │ │ ├── Str.java │ │ │ └── NameResolver.java │ │ ├── luajc │ │ │ ├── CVS │ │ │ │ ├── Repository │ │ │ │ ├── Root │ │ │ │ ├── Entries.Log │ │ │ │ └── Entries │ │ │ ├── JavaLoader.java │ │ │ ├── VarInfo.java │ │ │ └── UpvalInfo.java │ │ ├── parser │ │ │ ├── CVS │ │ │ │ ├── Repository │ │ │ │ ├── Root │ │ │ │ └── Entries │ │ │ └── Token.java │ │ ├── script │ │ │ └── CVS │ │ │ │ ├── Repository │ │ │ │ ├── Root │ │ │ │ └── Entries │ │ └── lua2java │ │ │ └── CVS │ │ │ ├── Repository │ │ │ ├── Root │ │ │ └── Entries │ │ └── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ ├── CVS │ ├── Repository │ ├── Root │ └── Entries │ └── META-INF │ ├── CVS │ ├── Entries │ ├── Repository │ └── Root │ └── services │ ├── CVS │ ├── Repository │ ├── Root │ └── Entries │ └── javax.script.ScriptEngineFactory ├── CVS ├── Repository ├── Root ├── Entries.Log └── Entries ├── version.properties ├── test ├── CVS │ ├── Repository │ ├── Entries │ └── Root ├── java │ ├── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ └── org │ │ ├── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ │ └── luaj │ │ ├── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ │ └── luajc │ │ ├── CVS │ │ ├── Repository │ │ ├── Root │ │ └── Entries │ │ ├── TestLuaJ.java │ │ └── TestLuaJC.java ├── junit │ ├── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ └── org │ │ ├── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ │ └── luaj │ │ ├── CVS │ │ ├── Entries │ │ ├── Repository │ │ └── Root │ │ └── vm2 │ │ ├── lib │ │ ├── CVS │ │ │ ├── Entries │ │ │ ├── Repository │ │ │ └── Root │ │ └── jse │ │ │ └── CVS │ │ │ ├── Repository │ │ │ ├── Root │ │ │ └── Entries │ │ ├── CVS │ │ ├── Repository │ │ ├── Root │ │ ├── Entries.Log │ │ └── Entries │ │ ├── vm1 │ │ ├── CVS │ │ │ ├── Repository │ │ │ ├── Root │ │ │ └── Entries │ │ ├── luajvm1-tests.zip │ │ └── sample.lua │ │ ├── compiler │ │ ├── CVS │ │ │ ├── Repository │ │ │ ├── Root │ │ │ └── Entries │ │ ├── lua5.1-tests.zip │ │ ├── regressions.zip │ │ ├── RegressionTests.java │ │ ├── CompilerUnitTests.java │ │ ├── SimpleTests.java │ │ └── AbstractUnitTests.java │ │ ├── require │ │ ├── CVS │ │ │ ├── Repository │ │ │ ├── Root │ │ │ └── Entries │ │ ├── RequireSampleClassCastExcep.java │ │ ├── RequireSampleSuccess.java │ │ ├── RequireSampleLoadRuntimeExcep.java │ │ └── RequireSampleLoadLuaError.java │ │ ├── ErrorsTest.java │ │ ├── RequireClassTest.java │ │ ├── AllTests.java │ │ └── StringTest.java └── lua │ ├── CVS │ ├── Repository │ ├── Entries.Log │ ├── Root │ └── Entries │ ├── errors │ ├── CVS │ │ ├── Repository │ │ ├── Root │ │ └── Entries │ ├── coroutinelibargs.lua │ ├── modulelibargs.lua │ ├── tablelibargs.lua │ ├── iolibargs.lua │ └── mathlibargs.lua │ ├── perf │ ├── CVS │ │ ├── Repository │ │ ├── Root │ │ └── Entries │ ├── nsieve.lua │ ├── binarytrees.lua │ ├── fannkuch.lua │ └── nbody.lua │ ├── manyupvals.lua │ ├── oslib.lua │ ├── upvalues.lua │ ├── functions.lua │ ├── coroutinelib.lua │ └── tailcalls.lua ├── grammar └── CVS │ ├── Repository │ ├── Root │ └── Entries ├── examples ├── CVS │ ├── Repository │ ├── Entries │ └── Root ├── lua │ ├── hello.lua │ ├── CVS │ │ ├── Repository │ │ ├── Root │ │ └── Entries │ └── swingapp.lua ├── jme │ ├── CVS │ │ ├── Repository │ │ ├── Root │ │ └── Entries │ └── SampleMIDlet.java └── jse │ ├── CVS │ ├── Repository │ ├── Root │ └── Entries │ └── SampleJseMain.java └── wtk.xml /src/CVS/Entries: -------------------------------------------------------------------------------- 1 | D 2 | -------------------------------------------------------------------------------- /CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm 2 | -------------------------------------------------------------------------------- /src/jme/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/org//// 2 | -------------------------------------------------------------------------------- /version.properties: -------------------------------------------------------------------------------- 1 | version: 2.0.2 -------------------------------------------------------------------------------- /src/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src 2 | -------------------------------------------------------------------------------- /src/core/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/org//// 2 | -------------------------------------------------------------------------------- /src/jme/org/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/luaj//// 2 | -------------------------------------------------------------------------------- /src/jse/org/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/luaj//// 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/CVS/Entries: -------------------------------------------------------------------------------- 1 | D 2 | -------------------------------------------------------------------------------- /test/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test 2 | -------------------------------------------------------------------------------- /test/java/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/org//// 2 | -------------------------------------------------------------------------------- /test/junit/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/org//// 2 | -------------------------------------------------------------------------------- /grammar/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/grammar 2 | -------------------------------------------------------------------------------- /src/core/org/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/luaj//// 2 | -------------------------------------------------------------------------------- /src/core/org/luaj/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/vm2//// 2 | -------------------------------------------------------------------------------- /src/jme/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jme 2 | -------------------------------------------------------------------------------- /src/jme/org/luaj/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/vm2//// 2 | -------------------------------------------------------------------------------- /src/jse/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jse 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/vm2//// 2 | -------------------------------------------------------------------------------- /test/java/org/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/luaj//// 2 | -------------------------------------------------------------------------------- /test/junit/org/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/luaj//// 2 | -------------------------------------------------------------------------------- /examples/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/examples 2 | -------------------------------------------------------------------------------- /examples/lua/hello.lua: -------------------------------------------------------------------------------- 1 | print( 'hello, world' ) -------------------------------------------------------------------------------- /src/core/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/core 2 | -------------------------------------------------------------------------------- /src/jme/org/luaj/vm2/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/lib//// 2 | -------------------------------------------------------------------------------- /src/jse/META-INF/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/services//// 2 | -------------------------------------------------------------------------------- /test/java/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/java 2 | -------------------------------------------------------------------------------- /test/java/org/luaj/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/luajc//// 2 | -------------------------------------------------------------------------------- /test/junit/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/junit 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/vm2//// 2 | -------------------------------------------------------------------------------- /test/lua/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/lua 2 | -------------------------------------------------------------------------------- /examples/jme/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/examples/jme 2 | -------------------------------------------------------------------------------- /examples/jse/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/examples/jse 2 | -------------------------------------------------------------------------------- /examples/lua/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/examples/lua 2 | -------------------------------------------------------------------------------- /src/core/org/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/core/org 2 | -------------------------------------------------------------------------------- /src/jme/org/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jme/org 2 | -------------------------------------------------------------------------------- /src/jme/org/luaj/vm2/lib/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/jme//// 2 | -------------------------------------------------------------------------------- /src/jse/org/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jse/org 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/lib/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/jse//// 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/lib/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/jse//// 2 | -------------------------------------------------------------------------------- /test/java/org/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/java/org 2 | -------------------------------------------------------------------------------- /test/junit/org/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/junit/org 2 | -------------------------------------------------------------------------------- /test/lua/errors/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/lua/errors 2 | -------------------------------------------------------------------------------- /test/lua/perf/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/lua/perf 2 | -------------------------------------------------------------------------------- /examples/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/jme//// 2 | D/jse//// 3 | D/lua//// 4 | -------------------------------------------------------------------------------- /src/core/org/luaj/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/core/org/luaj 2 | -------------------------------------------------------------------------------- /src/jme/org/luaj/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jme/org/luaj 2 | -------------------------------------------------------------------------------- /src/jse/META-INF/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jse/META-INF 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jse/org/luaj 2 | -------------------------------------------------------------------------------- /test/CVS/Entries: -------------------------------------------------------------------------------- 1 | D/java//// 2 | D/junit//// 3 | D/lua//// 4 | -------------------------------------------------------------------------------- /test/lua/CVS/Entries.Log: -------------------------------------------------------------------------------- 1 | A D/errors//// 2 | A D/perf//// 3 | -------------------------------------------------------------------------------- /src/jme/org/luaj/vm2/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jme/org/luaj/vm2 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jse/org/luaj/vm2 2 | -------------------------------------------------------------------------------- /test/java/org/luaj/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/java/org/luaj 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/junit/org/luaj 2 | -------------------------------------------------------------------------------- /CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/core/org/luaj/vm2 2 | -------------------------------------------------------------------------------- /grammar/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/CVS/Entries.Log: -------------------------------------------------------------------------------- 1 | A D/compiler//// 2 | A D/lib//// 3 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/lib/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/core/org/luaj/vm2/lib 2 | -------------------------------------------------------------------------------- /src/jme/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jme/org/luaj/vm2/lib/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jme/org/luaj/vm2/lib 2 | -------------------------------------------------------------------------------- /src/jse/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jse/META-INF/services/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jse/META-INF/services 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jse/org/luaj/vm2/ast 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/lib/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jse/org/luaj/vm2/lib 2 | -------------------------------------------------------------------------------- /test/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/java/org/luaj/luajc/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/java/org/luaj/luajc 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/junit/org/luaj/vm2 2 | -------------------------------------------------------------------------------- /examples/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /examples/jme/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /examples/jse/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /examples/lua/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/core/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/core/org/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jme/org/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jse/org/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/luajc/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jse/org/luaj/vm2/luajc 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/parser/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jse/org/luaj/vm2/parser 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/script/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jse/org/luaj/vm2/script 2 | -------------------------------------------------------------------------------- /test/java/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/junit/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/lib/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/junit/org/luaj/vm2/lib 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/vm1/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/junit/org/luaj/vm2/vm1 2 | -------------------------------------------------------------------------------- /test/lua/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /examples/jme/CVS/Entries: -------------------------------------------------------------------------------- 1 | /SampleMIDlet.java/1.4/Fri Jul 30 18:09:31 2010// 2 | D 3 | -------------------------------------------------------------------------------- /src/core/org/luaj/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/compiler/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/core/org/luaj/vm2/compiler 2 | -------------------------------------------------------------------------------- /src/jme/org/luaj/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jme/org/luaj/vm2/lib/jme/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jme/org/luaj/vm2/lib/jme 2 | -------------------------------------------------------------------------------- /src/jse/META-INF/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/lib/jse/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jse/org/luaj/vm2/lib/jse 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/lua2java/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/src/jse/org/luaj/vm2/lua2java 2 | -------------------------------------------------------------------------------- /test/java/org/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/junit/org/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/lua/errors/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/lua/perf/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jme/org/luaj/vm2/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/java/org/luaj/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/compiler/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/junit/org/luaj/vm2/compiler 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/lib/jse/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/junit/org/luaj/vm2/lib/jse 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/require/CVS/Repository: -------------------------------------------------------------------------------- 1 | luaj-vm/test/junit/org/luaj/vm2/require 2 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/lib/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jme/org/luaj/vm2/lib/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jse/META-INF/services/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/lib/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/luajc/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/parser/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/script/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/java/org/luaj/luajc/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/lib/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/vm1/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/compiler/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jme/org/luaj/vm2/lib/jme/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jse/META-INF/services/javax.script.ScriptEngineFactory: -------------------------------------------------------------------------------- 1 | org.luaj.vm2.script.LuaScriptEngineFactory -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/lib/jse/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/lua2java/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/compiler/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/lib/jse/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/require/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:anonymous@luaj.cvs.sourceforge.net:/cvsroot/luaj 2 | -------------------------------------------------------------------------------- /src/jse/META-INF/services/CVS/Entries: -------------------------------------------------------------------------------- 1 | /javax.script.ScriptEngineFactory/1.1/Tue Oct 27 06:12:25 2009// 2 | D 3 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/luajc/CVS/Entries.Log: -------------------------------------------------------------------------------- 1 | A D/antlr//// 2 | A D/lst//// 3 | R D/lst//// 4 | R D/antlr//// 5 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/CVS/Entries.Log: -------------------------------------------------------------------------------- 1 | A D/compiler//// 2 | A D/lib//// 3 | A D/require//// 4 | A D/vm1//// 5 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/lib/jse/CVS/Entries: -------------------------------------------------------------------------------- 1 | /LuaJavaCoercionTest.java/1.8/Fri Dec 3 15:39:26 2010// 2 | D 3 | -------------------------------------------------------------------------------- /grammar/CVS/Entries: -------------------------------------------------------------------------------- 1 | /Lua51.jj/1.3/Mon Jun 21 15:44:41 2010// 2 | /LuaParser.jj/1.6/Fri Jul 23 00:39:27 2010// 3 | D 4 | -------------------------------------------------------------------------------- /examples/lua/CVS/Entries: -------------------------------------------------------------------------------- 1 | /hello.lua/1.1/Tue Oct 27 06:12:25 2009// 2 | /swingapp.lua/1.1/Tue Oct 27 06:12:25 2009// 3 | D 4 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/vm1/luajvm1-tests.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/headius/luaj/HEAD/test/junit/org/luaj/vm2/vm1/luajvm1-tests.zip -------------------------------------------------------------------------------- /test/java/org/luaj/luajc/CVS/Entries: -------------------------------------------------------------------------------- 1 | /TestLuaJ.java/1.4/Fri Jul 30 18:09:31 2010// 2 | /TestLuaJC.java/1.8/Thu Aug 5 21:41:19 2010// 3 | D 4 | -------------------------------------------------------------------------------- /examples/jse/CVS/Entries: -------------------------------------------------------------------------------- 1 | /SampleJseMain.java/1.3/Fri Jul 30 18:09:31 2010// 2 | /ScriptEngineSample.java/1.5/Sat Dec 4 00:47:30 2010// 3 | D 4 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/CVS/Entries.Log: -------------------------------------------------------------------------------- 1 | A D/ast//// 2 | A D/lib//// 3 | A D/lua2java//// 4 | A D/luajc//// 5 | A D/parser//// 6 | A D/script//// 7 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/compiler/lua5.1-tests.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/headius/luaj/HEAD/test/junit/org/luaj/vm2/compiler/lua5.1-tests.zip -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/compiler/regressions.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/headius/luaj/HEAD/test/junit/org/luaj/vm2/compiler/regressions.zip -------------------------------------------------------------------------------- /src/jme/org/luaj/vm2/lib/jme/CVS/Entries: -------------------------------------------------------------------------------- 1 | /JmeIoLib.java/1.2/Sun Apr 25 15:50:41 2010// 2 | /JmePlatform.java/1.1/Fri Jul 30 18:09:31 2010// 3 | D 4 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/script/CVS/Entries: -------------------------------------------------------------------------------- 1 | /LuaScriptEngine.java/1.11/Sat Dec 4 00:47:30 2010// 2 | /LuaScriptEngineFactory.java/1.2/Fri May 14 05:13:39 2010// 3 | D 4 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/lua2java/CVS/Entries: -------------------------------------------------------------------------------- 1 | /JavaCodeGen.java/1.22/Mon Aug 23 20:36:41 2010// 2 | /JavaScope.java/1.9/Sat Jul 31 14:43:17 2010// 3 | /Lua2Java.java/1.2/Sat Jul 31 14:43:17 2010// 4 | D 5 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/vm1/sample.lua: -------------------------------------------------------------------------------- 1 | -- helper file for require() tests 2 | module( 'sample', package.seeall ) 3 | function h() 4 | print 'in sample.h' 5 | end 6 | print 'loading sample.lua' 7 | 8 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/vm1/CVS/Entries: -------------------------------------------------------------------------------- 1 | /Luajvm1CompatibilityTest.java/1.11/Fri Jul 30 18:09:31 2010// 2 | /luajvm1-tests.zip/1.6/Mon May 17 00:53:31 2010/-kb/ 3 | /sample.lua/1.1/Mon May 17 00:53:31 2010// 4 | D 5 | -------------------------------------------------------------------------------- /test/lua/perf/CVS/Entries: -------------------------------------------------------------------------------- 1 | /binarytrees.lua/1.1/Sun Aug 15 03:56:03 2010// 2 | /fannkuch.lua/1.1/Sun May 9 18:10:55 2010// 3 | /nbody.lua/1.1/Sun May 9 18:10:55 2010// 4 | /nsieve.lua/1.1/Sun Aug 15 03:56:03 2010// 5 | D 6 | -------------------------------------------------------------------------------- /src/jse/CVS/Entries: -------------------------------------------------------------------------------- 1 | /lua.java/1.14/Sat Sep 4 02:41:09 2010// 2 | /lua2java.java/1.8/Fri Jul 30 23:47:52 2010// 3 | /luac.java/1.4/Fri Jul 30 21:53:15 2010// 4 | /luajc.java/1.14/Fri Aug 13 00:23:53 2010// 5 | D/META-INF//// 6 | D/org//// 7 | -------------------------------------------------------------------------------- /CVS/Entries.Log: -------------------------------------------------------------------------------- 1 | A D/.settings//// 2 | A D/bin//// 3 | A D/doc//// 4 | A D/examples//// 5 | A D/grammar//// 6 | A D/lib//// 7 | A D/main//// 8 | A D/src//// 9 | A D/target//// 10 | A D/test//// 11 | R D/target//// 12 | R D/main//// 13 | R D/lib//// 14 | R D/doc//// 15 | R D/bin//// 16 | R D/.settings//// 17 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/require/CVS/Entries: -------------------------------------------------------------------------------- 1 | /RequireSampleClassCastExcep.java/1.1/Sun May 9 18:10:55 2010// 2 | /RequireSampleLoadLuaError.java/1.1/Sun May 9 18:10:55 2010// 3 | /RequireSampleLoadRuntimeExcep.java/1.1/Sun May 9 18:10:55 2010// 4 | /RequireSampleSuccess.java/1.1/Sun May 9 18:10:55 2010// 5 | D 6 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/compiler/CVS/Entries: -------------------------------------------------------------------------------- 1 | /DumpState.java/1.1/Tue Oct 27 06:12:25 2009// 2 | /FuncState.java/1.4/Fri Nov 19 19:46:34 2010// 3 | /InstructionPtr.java/1.1/Tue Oct 27 06:12:25 2009// 4 | /IntPtr.java/1.1/Tue Oct 27 06:12:25 2009// 5 | /LexState.java/1.2/Sun Apr 25 15:50:41 2010// 6 | /LuaC.java/1.5/Sun Jul 25 22:31:46 2010// 7 | D 8 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/parser/CVS/Entries: -------------------------------------------------------------------------------- 1 | /LuaParser.java/1.3/Thu Jul 22 23:35:26 2010// 2 | /LuaParserConstants.java/1.1/Mon Jul 19 14:56:32 2010// 3 | /LuaParserTokenManager.java/1.2/Fri Jul 23 00:39:28 2010// 4 | /ParseException.java/1.1/Mon Jul 19 14:56:32 2010// 5 | /SimpleCharStream.java/1.1/Mon Jul 19 14:56:32 2010// 6 | /Token.java/1.1/Mon Jul 19 14:56:32 2010// 7 | /TokenMgrError.java/1.1/Mon Jul 19 14:56:32 2010// 8 | D 9 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/compiler/CVS/Entries: -------------------------------------------------------------------------------- 1 | /AbstractUnitTests.java/1.6/Fri Jul 30 18:09:31 2010// 2 | /CompilerUnitTests.java/1.1/Sat Apr 3 15:14:30 2010// 3 | /DumpLoadEndianIntTest.java/1.5/Fri Jul 30 18:09:31 2010// 4 | /RegressionTests.java/1.1/Sat Apr 3 15:14:30 2010// 5 | /SimpleTests.java/1.4/Fri Jul 30 18:09:31 2010// 6 | /lua5.1-tests.zip/1.1/Sat Apr 3 15:14:30 2010/-kb/ 7 | /regressions.zip/1.1/Sat Apr 3 15:14:30 2010/-kb/ 8 | D 9 | -------------------------------------------------------------------------------- /src/CVS/Entries.Log: -------------------------------------------------------------------------------- 1 | A D/addon//// 2 | A D/antlr//// 3 | A D/core//// 4 | A D/debug//// 5 | A D/j2me//// 6 | A D/j2se//// 7 | A D/jme//// 8 | A D/jse//// 9 | A D/main//// 10 | A D/platform//// 11 | A D/sample//// 12 | A D/script//// 13 | A D/test//// 14 | R D/test//// 15 | R D/script//// 16 | R D/sample//// 17 | R D/platform//// 18 | R D/main//// 19 | R D/j2se//// 20 | R D/j2me//// 21 | R D/debug//// 22 | R D/antlr//// 23 | R D/addon//// 24 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/luajc/CVS/Entries: -------------------------------------------------------------------------------- 1 | /BasicBlock.java/1.6/Thu Aug 12 00:49:15 2010// 2 | /JavaBuilder.java/1.18/Mon Aug 23 20:36:41 2010// 3 | /JavaGen.java/1.16/Mon Aug 23 20:36:41 2010// 4 | /JavaLoader.java/1.4/Fri Aug 6 05:35:18 2010// 5 | /LuaJC.java/1.5/Fri Aug 13 00:23:53 2010// 6 | /ProtoInfo.java/1.10/Thu Aug 12 00:49:15 2010// 7 | /UpvalInfo.java/1.10/Thu Aug 12 17:00:47 2010// 8 | /VarInfo.java/1.4/Thu Aug 12 00:49:15 2010// 9 | D 10 | -------------------------------------------------------------------------------- /examples/jse/SampleJseMain.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | import org.luaj.vm2.LuaValue; 4 | import org.luaj.vm2.lib.jse.JsePlatform; 5 | 6 | public class SampleJseMain { 7 | 8 | 9 | public static void main(String[] args) throws Exception { 10 | String script = "examples/lua/hello.lua"; 11 | 12 | // create an environment to run in 13 | LuaValue _G = JsePlatform.standardGlobals(); 14 | _G.get("dofile").call( LuaValue.valueOf(script) ); 15 | } 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/lib/jse/CVS/Entries: -------------------------------------------------------------------------------- 1 | /CoerceJavaToLua.java/1.2/Sat May 22 17:47:32 2010// 2 | /CoerceLuaToJava.java/1.5/Sat May 22 17:47:32 2010// 3 | /JseBaseLib.java/1.2/Fri Apr 2 05:57:54 2010// 4 | /JseIoLib.java/1.4/Mon May 17 00:46:00 2010// 5 | /JseMathLib.java/1.6/Tue Jul 27 22:19:55 2010// 6 | /JseOsLib.java/1.2/Tue Apr 20 14:43:30 2010// 7 | /JsePlatform.java/1.1/Fri Jul 30 18:09:31 2010// 8 | /LuajavaLib.java/1.13/Sat Sep 4 16:26:23 2010// 9 | D 10 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/require/RequireSampleClassCastExcep.java: -------------------------------------------------------------------------------- 1 | package org.luaj.vm2.require; 2 | 3 | import org.luaj.vm2.LuaValue; 4 | 5 | /** 6 | * This should fail while trying to load via "require() because it is not a LibFunction" 7 | * 8 | */ 9 | public class RequireSampleClassCastExcep { 10 | 11 | public RequireSampleClassCastExcep() { 12 | } 13 | 14 | public LuaValue call() { 15 | return LuaValue.valueOf("require-sample-class-cast-excep"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /test/lua/errors/CVS/Entries: -------------------------------------------------------------------------------- 1 | /args.lua/1.5/Sun May 16 17:53:33 2010// 2 | /baselibargs.lua/1.6/Sun May 16 05:09:47 2010// 3 | /coroutinelibargs.lua/1.3/Wed May 12 05:00:09 2010// 4 | /iolibargs.lua/1.4/Mon May 17 00:46:00 2010// 5 | /mathlibargs.lua/1.4/Sun May 16 17:53:33 2010// 6 | /modulelibargs.lua/1.3/Sun May 16 17:53:33 2010// 7 | /operators.lua/1.4/Sat May 15 17:12:50 2010// 8 | /stringlibargs.lua/1.3/Wed May 12 03:55:35 2010// 9 | /tablelibargs.lua/1.3/Wed May 12 03:48:21 2010// 10 | D 11 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/require/RequireSampleSuccess.java: -------------------------------------------------------------------------------- 1 | package org.luaj.vm2.require; 2 | 3 | import org.luaj.vm2.LuaValue; 4 | import org.luaj.vm2.lib.ZeroArgFunction; 5 | 6 | /** 7 | * This should succeed as a library that can be loaded dynamically via "require()" 8 | */ 9 | public class RequireSampleSuccess extends ZeroArgFunction { 10 | 11 | public RequireSampleSuccess() { 12 | } 13 | 14 | public LuaValue call() { 15 | return LuaValue.valueOf("require-sample-success"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CVS/Entries: -------------------------------------------------------------------------------- 1 | /.classpath/1.21/Fri May 14 14:38:02 2010// 2 | /.cvsignore/1.8/Tue Apr 27 14:41:30 2010// 3 | /.project/1.1/Fri Jun 8 05:11:38 2007// 4 | /README.html/1.38/Fri Dec 3 01:37:47 2010// 5 | /build-coverage.xml/1.10/Mon Jul 26 15:25:22 2010// 6 | /build-midlet.xml/1.3/Sun Apr 25 06:29:03 2010// 7 | /build-perf.xml/1.3/Sat Sep 4 02:41:09 2010// 8 | /build.xml/1.52/Wed Dec 1 15:31:17 2010// 9 | /names.csv/1.2/Sun Apr 25 15:50:40 2010// 10 | /version.properties/1.65/Fri Dec 3 01:37:47 2010// 11 | /wtk.xml/1.1/Thu Jul 24 06:35:17 2008// 12 | D 13 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/require/RequireSampleLoadRuntimeExcep.java: -------------------------------------------------------------------------------- 1 | package org.luaj.vm2.require; 2 | 3 | import org.luaj.vm2.LuaValue; 4 | import org.luaj.vm2.lib.ZeroArgFunction; 5 | 6 | /** 7 | * This should fail while trying to load via "require()" because it throws a RuntimeException 8 | * 9 | */ 10 | public class RequireSampleLoadRuntimeExcep extends ZeroArgFunction { 11 | 12 | public RequireSampleLoadRuntimeExcep() { 13 | } 14 | 15 | public LuaValue call() { 16 | throw new RuntimeException("sample-load-runtime-exception"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/require/RequireSampleLoadLuaError.java: -------------------------------------------------------------------------------- 1 | package org.luaj.vm2.require; 2 | 3 | import org.luaj.vm2.LuaValue; 4 | import org.luaj.vm2.lib.ZeroArgFunction; 5 | 6 | /** 7 | * This should fail while trying to load via 8 | * "require()" because it throws a LuaError 9 | * 10 | */ 11 | public class RequireSampleLoadLuaError extends ZeroArgFunction { 12 | 13 | public RequireSampleLoadLuaError() { 14 | } 15 | 16 | public LuaValue call() { 17 | error("sample-load-lua-error"); 18 | return LuaValue.valueOf("require-sample-load-lua-error"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /test/lua/CVS/Entries: -------------------------------------------------------------------------------- 1 | /baselib.lua/1.2/Wed Apr 28 03:30:37 2010// 2 | /coroutinelib.lua/1.1/Tue Oct 27 06:12:25 2009// 3 | /debuglib.lua/1.2/Sun Apr 25 22:40:07 2010// 4 | /errors.lua/1.1/Tue Oct 27 06:12:25 2009// 5 | /functions.lua/1.1/Tue Oct 27 06:12:25 2009// 6 | /iolib.lua/1.2/Fri Apr 2 14:48:02 2010// 7 | /manyupvals.lua/1.1/Sun Jul 25 20:47:54 2010// 8 | /mathlib.lua/1.1/Tue Oct 27 06:12:25 2009// 9 | /metatags.lua/1.9/Wed Aug 25 17:56:54 2010// 10 | /oslib.lua/1.2/Wed Apr 28 14:38:59 2010// 11 | /stringlib.lua/1.3/Sun May 9 03:47:13 2010// 12 | /tablelib.lua/1.1/Tue Oct 27 06:12:25 2009// 13 | /tailcalls.lua/1.4/Mon Jul 26 05:58:34 2010// 14 | /upvalues.lua/1.1/Tue Oct 27 06:12:25 2009// 15 | /vm.lua/1.1/Tue Oct 27 06:12:25 2009// 16 | D 17 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/CVS/Entries: -------------------------------------------------------------------------------- 1 | /Block.java/1.3/Mon Jun 28 05:34:17 2010// 2 | /Chunk.java/1.2/Wed Jun 23 15:00:12 2010// 3 | /Exp.java/1.7/Thu Jul 22 23:35:25 2010// 4 | /FuncArgs.java/1.2/Wed Jun 23 15:00:12 2010// 5 | /FuncBody.java/1.3/Mon Jun 28 05:34:17 2010// 6 | /FuncName.java/1.3/Mon Jun 28 05:34:17 2010// 7 | /Name.java/1.4/Wed Jul 28 18:58:16 2010// 8 | /NameResolver.java/1.12/Sat Jul 31 14:43:17 2010// 9 | /NameScope.java/1.5/Sat Jul 31 14:43:17 2010// 10 | /ParList.java/1.2/Wed Jun 23 15:00:12 2010// 11 | /Stat.java/1.7/Sat Jul 31 14:43:17 2010// 12 | /Str.java/1.5/Fri Jul 23 15:00:35 2010// 13 | /TableConstructor.java/1.4/Thu Jun 24 05:15:41 2010// 14 | /TableField.java/1.1/Thu Jun 24 05:15:41 2010// 15 | /Variable.java/1.1/Wed Jul 28 18:58:16 2010// 16 | /Visitor.java/1.7/Sat Jul 31 14:43:17 2010// 17 | D 18 | -------------------------------------------------------------------------------- /test/lua/manyupvals.lua: -------------------------------------------------------------------------------- 1 | local t = {} 2 | local template = [[ 3 | local f 4 | do 5 | local result 6 | function f() 7 | if not result then 8 | result = f() + f() 9 | end 10 | return result 11 | end 12 | end 13 | ]] 14 | t[1] = [[ 15 | local f1 16 | f1 = function() return 1 end 17 | ]] 18 | t[2] = [[ 19 | local f2 20 | f2 = function() return 1 end 21 | ]] 22 | for i = 3, 199 do 23 | t[i] = template:gsub("<([^>]+)>", function(s) 24 | local c = assert(loadstring('return '..s), 'could not compile: '..s) 25 | setfenv(c, { i = i }) 26 | return c() 27 | end) 28 | end 29 | t[200] = [[ 30 | print("5th fibonacci number is", f5()) 31 | print("10th fibonacci number is", f10()) 32 | print("199th fibonacci number is", f199()) 33 | ]] 34 | 35 | local s = table.concat(t) 36 | print(s) 37 | f = loadstring(s) 38 | f() 39 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/lib/CVS/Entries: -------------------------------------------------------------------------------- 1 | /BaseLib.java/1.24/Wed Aug 25 17:56:54 2010// 2 | /CoroutineLib.java/1.8/Sun May 16 17:53:33 2010// 3 | /DebugLib.java/1.16/Wed Aug 25 23:57:38 2010// 4 | /IoLib.java/1.14/Fri Jul 30 18:09:31 2010// 5 | /LibFunction.java/1.8/Tue Apr 27 05:05:08 2010// 6 | /MathLib.java/1.7/Tue Jul 27 22:19:55 2010// 7 | /OneArgFunction.java/1.2/Fri Apr 2 05:57:54 2010// 8 | /OsLib.java/1.7/Sun May 16 17:53:33 2010// 9 | /PackageLib.java/1.20/Sun May 16 17:53:33 2010// 10 | /ResourceFinder.java/1.1/Tue Oct 27 06:12:25 2009// 11 | /StringLib.java/1.11/Sun May 16 17:53:33 2010// 12 | /TableLib.java/1.8/Sun May 16 17:53:33 2010// 13 | /ThreeArgFunction.java/1.2/Fri Apr 2 05:57:54 2010// 14 | /TwoArgFunction.java/1.2/Fri Apr 2 05:57:54 2010// 15 | /VarArgFunction.java/1.5/Sat Apr 24 23:49:45 2010// 16 | /ZeroArgFunction.java/1.2/Fri Apr 2 05:57:54 2010// 17 | D 18 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/CVS/Entries: -------------------------------------------------------------------------------- 1 | /AllTests.java/1.9/Thu Jul 29 21:28:13 2010// 2 | /CompatibiltyTest.java/1.14/Mon Aug 23 15:26:22 2010// 3 | /ErrorsTest.java/1.5/Sat May 15 21:56:29 2010// 4 | /FragmentsTest.java/1.24/Thu Aug 12 17:00:48 2010// 5 | /LuaOperationsTest.java/1.5/Fri Jul 30 18:09:31 2010// 6 | /MathLibTest.java/1.2/Fri Jul 30 18:09:31 2010// 7 | /MetatableTest.java/1.2/Sun Apr 25 15:50:41 2010// 8 | /RequireClassTest.java/1.2/Fri Jul 30 18:09:31 2010// 9 | /ScriptDrivenTest.java/1.13/Fri Jul 30 18:09:31 2010// 10 | /StringTest.java/1.3/Fri Jul 30 18:09:31 2010// 11 | /TableArrayTest.java/1.3/Mon Jun 21 01:31:40 2010// 12 | /TableHashTest.java/1.3/Mon Jun 21 01:31:40 2010// 13 | /TableTest.java/1.3/Mon Jun 21 01:31:40 2010// 14 | /TypeTest.java/1.5/Sun Aug 22 17:35:09 2010// 15 | /UnaryBinaryOperatorsTest.java/1.8/Wed Aug 25 17:56:54 2010// 16 | /WeakTableTest.java/1.3/Mon Jul 5 18:30:09 2010// 17 | D 18 | -------------------------------------------------------------------------------- /test/lua/perf/nsieve.lua: -------------------------------------------------------------------------------- 1 | -- The Computer Language Shootout 2 | -- http://shootout.alioth.debian.org/ 3 | -- contributed by Isaac Gouy 4 | -- modified by Mike Pall 5 | 6 | 7 | local function nsieve(m,isPrime) 8 | for i=2,m do 9 | isPrime[i] = true 10 | end 11 | local count = 0 12 | 13 | for i=2,m do 14 | if isPrime[i] then 15 | for k=i+i, m, i do 16 | if isPrime[k] then isPrime[k] = false end 17 | end 18 | count = count + 1 19 | end 20 | end 21 | return count 22 | end 23 | 24 | 25 | local n = tonumber(arg and arg[1]) or 1 26 | local flags = {} 27 | 28 | local m = (2^n)*10000 29 | print( string.format("Primes up to %8d %8d", m, nsieve(m,flags))) 30 | 31 | m = (2^(n-1))*10000 32 | print( string.format("Primes up to %8d %8d", m, nsieve(m,flags))) 33 | 34 | m = (2^(n-2))*10000 35 | print( string.format("Primes up to %8d %8d", m, nsieve(m,flags))) 36 | -------------------------------------------------------------------------------- /examples/jme/SampleMIDlet.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | import javax.microedition.midlet.MIDlet; 4 | import javax.microedition.midlet.MIDletStateChangeException; 5 | 6 | import org.luaj.vm2.*; 7 | import org.luaj.vm2.lib.*; 8 | import org.luaj.vm2.lib.jme.JmePlatform; 9 | import org.luaj.vm2.compiler.LuaC; 10 | 11 | 12 | public class SampleMIDlet extends MIDlet { 13 | 14 | // the script will be loaded as a resource 15 | private static final String DEFAULT_SCRIPT = "hello.lua"; 16 | 17 | protected void startApp() throws MIDletStateChangeException { 18 | // get the script as an app property 19 | String script = this.getAppProperty("script"); 20 | if ( script == null ) 21 | script = DEFAULT_SCRIPT; 22 | 23 | // create an environment to run in 24 | LuaValue _G = JmePlatform.standardGlobals(); 25 | _G.get("require").call( LuaValue.valueOf(script) ); 26 | } 27 | 28 | protected void destroyApp(boolean arg0) throws MIDletStateChangeException { 29 | } 30 | 31 | protected void pauseApp() { 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /examples/lua/swingapp.lua: -------------------------------------------------------------------------------- 1 | 2 | frame = luajava.newInstance( "javax.swing.JFrame", "Texts" ); 3 | pane = luajava.newInstance( "javax.swing.JPanel" ); 4 | borderFactory = luajava.bindClass( "javax.swing.BorderFactory" ) 5 | border = borderFactory:createEmptyBorder( 30, 30, 10, 30 ) 6 | pane:setBorder( border ) 7 | label = luajava.newInstance( "javax.swing.JLabel", "This is a Label" ); 8 | 9 | 10 | layout = luajava.newInstance( "java.awt.GridLayout", 2, 2 ); 11 | pane:setLayout( layout ) 12 | pane:add( label ) 13 | pane:setBounds( 20, 30, 10, 30 ) 14 | 15 | borderLayout = luajava.bindClass( "java.awt.BorderLayout" ) 16 | frame:getContentPane():add(pane, borderLayout.CENTER ) 17 | jframe = luajava.bindClass( "javax.swing.JFrame" ) 18 | frame:setDefaultCloseOperation(jframe.EXIT_ON_CLOSE) 19 | frame:pack() 20 | frame:setVisible(true) 21 | 22 | local listener = luajava.createProxy("java.awt.event.MouseListener", 23 | { 24 | mouseClicked = function(me) 25 | print("clicked!", me) 26 | end 27 | }) 28 | 29 | frame:addMouseListener(listener) 30 | -------------------------------------------------------------------------------- /wtk.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | WTK_HOME from env ${env.WTK_HOME} 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | Using WTK found in ${wtk.home} 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/CVS/Entries: -------------------------------------------------------------------------------- 1 | /Buffer.java/1.10/Tue Aug 24 19:05:04 2010// 2 | /LoadState.java/1.8/Sun Jul 25 22:31:45 2010// 3 | /LocVars.java/1.2/Sun Apr 25 15:50:41 2010// 4 | /Lua.java/1.4/Tue Aug 10 17:46:06 2010// 5 | /LuaBoolean.java/1.7/Tue Nov 16 00:07:36 2010// 6 | /LuaClosure.java/1.11/Mon Aug 23 05:17:11 2010// 7 | /LuaDouble.java/1.12/Wed Aug 25 17:56:51 2010// 8 | /LuaError.java/1.8/Tue Aug 10 17:46:06 2010// 9 | /LuaFunction.java/1.4/Wed Aug 25 17:56:51 2010// 10 | /LuaInteger.java/1.9/Wed Aug 25 17:56:51 2010// 11 | /LuaNil.java/1.8/Tue Nov 16 00:07:36 2010// 12 | /LuaNumber.java/1.9/Wed Aug 25 14:55:51 2010// 13 | /LuaString.java/1.19/Wed Aug 25 17:56:51 2010// 14 | /LuaTable.java/1.24/Wed Aug 25 17:56:51 2010// 15 | /LuaThread.java/1.9/Wed Aug 25 17:56:51 2010// 16 | /LuaUserdata.java/1.8/Wed Aug 25 17:56:51 2010// 17 | /LuaValue.java/1.45/Tue Nov 16 00:07:36 2010// 18 | /Print.java/1.7/Wed Aug 25 23:57:41 2010// 19 | /Prototype.java/1.3/Sun May 16 17:53:34 2010// 20 | /TailcallVarargs.java/1.6/Thu Jul 29 15:10:17 2010// 21 | /UpValue.java/1.4/Sun Apr 25 15:50:41 2010// 22 | /Varargs.java/1.10/Sat Aug 21 14:56:48 2010// 23 | /WeakTable.java/1.11/Wed Aug 25 05:05:00 2010// 24 | D 25 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/compiler/RegressionTests.java: -------------------------------------------------------------------------------- 1 | package org.luaj.vm2.compiler; 2 | 3 | /** 4 | * Framework to add regression tests as problem areas are found. 5 | * 6 | * To add a new regression test: 7 | * 1) run "unpack.sh" in the project root 8 | * 2) add a new "lua" file in the "regressions" subdirectory 9 | * 3) run "repack.sh" in the project root 10 | * 4) add a line to the source file naming the new test 11 | * 12 | * After adding a test, check in the zip file 13 | * rather than the individual regression test files. 14 | * 15 | * @author jrosebor 16 | */ 17 | public class RegressionTests extends AbstractUnitTests { 18 | 19 | public RegressionTests() { 20 | super( "regressions.zip", 21 | "regressions" ); 22 | } 23 | 24 | public void testModulo() { doTest("modulo.lua"); } 25 | public void testConstruct() { doTest("construct.lua"); } 26 | public void testBigAttrs() { doTest("bigattr.lua"); } 27 | public void testControlChars() { doTest("controlchars.lua"); } 28 | public void testComparators() { doTest("comparators.lua"); } 29 | public void testMathRandomseed() { doTest("mathrandomseed.lua"); } 30 | public void testVarargs() { doTest("varargs.lua"); } 31 | } 32 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/compiler/IntPtr.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.compiler; 23 | 24 | public class IntPtr { 25 | int i; 26 | IntPtr() { 27 | } 28 | IntPtr(int value) { 29 | this.i = value; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/Name.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2010 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.ast; 23 | 24 | 25 | public class Name { 26 | public final String name; 27 | public Variable variable; 28 | public Name(String name) { 29 | this.name = name; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /test/lua/perf/binarytrees.lua: -------------------------------------------------------------------------------- 1 | -- The Computer Language Benchmarks Game 2 | -- http://shootout.alioth.debian.org/ 3 | -- contributed by Mike Pall 4 | 5 | local function BottomUpTree(item, depth) 6 | if depth > 0 then 7 | local i = item + item 8 | depth = depth - 1 9 | local left, right = BottomUpTree(i-1, depth), BottomUpTree(i, depth) 10 | return { item, left, right } 11 | else 12 | return { item } 13 | end 14 | end 15 | 16 | local function ItemCheck(tree) 17 | if tree[2] then 18 | return tree[1] + ItemCheck(tree[2]) - ItemCheck(tree[3]) 19 | else 20 | return tree[1] 21 | end 22 | end 23 | 24 | local N = tonumber(arg and arg[1]) or 0 25 | local mindepth = 4 26 | local maxdepth = mindepth + 2 27 | if maxdepth < N then maxdepth = N end 28 | 29 | do 30 | local stretchdepth = maxdepth + 1 31 | local stretchtree = BottomUpTree(0, stretchdepth) 32 | io.write(string.format("stretch tree of depth %d\t check: %d\n", 33 | stretchdepth, ItemCheck(stretchtree))) 34 | end 35 | 36 | local longlivedtree = BottomUpTree(0, maxdepth) 37 | 38 | for depth=mindepth,maxdepth,2 do 39 | local iterations = 2 ^ (maxdepth - depth + mindepth) 40 | local check = 0 41 | for i=1,iterations do 42 | check = check + ItemCheck(BottomUpTree(1, depth)) + 43 | ItemCheck(BottomUpTree(-1, depth)) 44 | end 45 | io.write(string.format("%d\t trees of depth %d\t check: %d\n", 46 | iterations*2, depth, check)) 47 | end 48 | 49 | io.write(string.format("long lived tree of depth %d\t check: %d\n", 50 | maxdepth, ItemCheck(longlivedtree))) 51 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/Chunk.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2010 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.ast; 23 | 24 | public class Chunk { 25 | public final Block block; 26 | 27 | public Chunk(Block b) { 28 | this.block = b; 29 | } 30 | 31 | public void accept( Visitor visitor ) { 32 | visitor.visit( this ); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/TableConstructor.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2010 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.ast; 23 | 24 | import java.util.List; 25 | 26 | public class TableConstructor extends Exp { 27 | public List fields; 28 | 29 | public void accept(Visitor visitor) { 30 | visitor.visit(this); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/compiler/CompilerUnitTests.java: -------------------------------------------------------------------------------- 1 | package org.luaj.vm2.compiler; 2 | 3 | 4 | 5 | public class CompilerUnitTests extends AbstractUnitTests { 6 | 7 | public CompilerUnitTests() { 8 | super("lua5.1-tests.zip", "lua5.1-tests"); 9 | } 10 | 11 | public void testAll() { doTest("all.lua"); } 12 | public void testApi() { doTest("api.lua"); } 13 | public void testAttrib() { doTest("attrib.lua"); } 14 | public void testBig() { doTest("big.lua"); } 15 | public void testCalls() { doTest("calls.lua"); } 16 | public void testChecktable() { doTest("checktable.lua"); } 17 | public void testClosure() { doTest("closure.lua"); } 18 | public void testCode() { doTest("code.lua"); } 19 | public void testConstruct() { doTest("constructs.lua"); } 20 | public void testDb() { doTest("db.lua"); } 21 | public void testErrors() { doTest("errors.lua"); } 22 | public void testEvents() { doTest("events.lua"); } 23 | public void testFiles() { doTest("files.lua"); } 24 | public void testGc() { doTest("gc.lua"); } 25 | public void testLiterals() { doTest("literals.lua"); } 26 | public void testLocals() { doTest("locals.lua"); } 27 | public void testMain() { doTest("main.lua"); } 28 | public void testMath() { doTest("math.lua"); } 29 | public void testNextvar() { doTest("nextvar.lua"); } 30 | public void testPm() { doTest("pm.lua"); } 31 | public void testSort() { doTest("sort.lua"); } 32 | public void testStrings() { doTest("strings.lua"); } 33 | public void testVararg() { doTest("vararg.lua"); } 34 | public void testVerybig() { doTest("verybig.lua"); } 35 | } 36 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/compiler/InstructionPtr.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.compiler; 23 | 24 | class InstructionPtr { 25 | final int[] code; 26 | final int idx; 27 | InstructionPtr(int[] code, int idx ) { 28 | this.code = code; 29 | this.idx = idx; 30 | } 31 | int get() { 32 | return code[idx]; 33 | } 34 | void set(int value) { 35 | code[idx] = value; 36 | } 37 | } -------------------------------------------------------------------------------- /test/lua/perf/fannkuch.lua: -------------------------------------------------------------------------------- 1 | -- The Computer Language Benchmarks Game 2 | -- http://shootout.alioth.debian.org/ 3 | -- contributed by Mike Pall 4 | 5 | local function fannkuch(n) 6 | local p, q, s, odd, check, maxflips = {}, {}, {}, true, 0, 0 7 | for i=1,n do p[i] = i; q[i] = i; s[i] = i end 8 | repeat 9 | -- Print max. 30 permutations. 10 | if check < 30 then 11 | if not p[n] then return maxflips end -- Catch n = 0, 1, 2. 12 | io.write(unpack(p)); io.write("\n") 13 | check = check + 1 14 | end 15 | -- Copy and flip. 16 | local q1 = p[1] -- Cache 1st element. 17 | if p[n] ~= n and q1 ~= 1 then -- Avoid useless work. 18 | for i=2,n do q[i] = p[i] end -- Work on a copy. 19 | for flips=1,1000000 do -- Flip ... 20 | local qq = q[q1] 21 | if qq == 1 then -- ... until 1st element is 1. 22 | if flips > maxflips then maxflips = flips end -- New maximum? 23 | break 24 | end 25 | q[q1] = q1 26 | if q1 >= 4 then 27 | local i, j = 2, q1 - 1 28 | repeat q[i], q[j] = q[j], q[i]; i = i + 1; j = j - 1; until i >= j 29 | end 30 | q1 = qq 31 | end 32 | end 33 | -- Permute. 34 | if odd then 35 | p[2], p[1] = p[1], p[2]; odd = false -- Rotate 1<-2. 36 | else 37 | p[2], p[3] = p[3], p[2]; odd = true -- Rotate 1<-2 and 1<-2<-3. 38 | for i=3,n do 39 | local sx = s[i] 40 | if sx ~= 1 then s[i] = sx-1; break end 41 | if i == n then return maxflips end -- Out of permutations. 42 | s[i] = i 43 | -- Rotate 1<-...<-i+1. 44 | local t = p[1]; for j=1,i do p[j] = p[j+1] end; p[i+1] = t 45 | end 46 | end 47 | until false 48 | end 49 | 50 | local n = tonumber(arg and arg[1]) or 1 51 | io.write("Pfannkuchen(", n, ") = ", fannkuch(n), "\n") 52 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/FuncBody.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2010 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.ast; 23 | 24 | public class FuncBody { 25 | public ParList parlist; 26 | public Block block; 27 | public NameScope scope; 28 | 29 | public FuncBody(ParList parlist, Block block) { 30 | this.parlist = parlist!=null? parlist: ParList.EMPTY_PARLIST; 31 | this.block = block; 32 | } 33 | public void accept(Visitor visitor) { 34 | visitor.visit(this); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /test/lua/errors/coroutinelibargs.lua: -------------------------------------------------------------------------------- 1 | package.path = "?.lua;test/lua/errors/?.lua" 2 | require 'args' 3 | 4 | -- arg type tests for coroutine library functions 5 | 6 | -- coroutine.create 7 | banner('coroutine.create') 8 | checkallpass('coroutine.create',{somefunction}) 9 | checkallerrors('coroutine.create',{notafunction},'bad argument') 10 | 11 | -- coroutine.resume 12 | banner('coroutine.resume') 13 | local co = coroutine.create(function() while true do coroutine.yield() end end) 14 | checkallpass('coroutine.resume',{{co},anylua}) 15 | checkallerrors('coroutine.resume',{notathread},'bad argument') 16 | 17 | -- coroutine.running 18 | banner('coroutine.running') 19 | checkallpass('coroutine.running',{anylua}) 20 | 21 | -- coroutine.status 22 | banner('coroutine.status') 23 | checkallpass('coroutine.status',{{co}}) 24 | checkallerrors('coroutine.status',{notathread},'bad argument') 25 | 26 | -- coroutine.wrap 27 | banner('coroutine.wrap') 28 | checkallpass('coroutine.wrap',{somefunction}) 29 | checkallerrors('coroutine.wrap',{notafunction},'bad argument') 30 | 31 | -- coroutine.yield 32 | banner('coroutine.yield') 33 | local function f() 34 | print( 'yield', coroutine.yield() ) 35 | print( 'yield', coroutine.yield(astring,anumber,aboolean) ) 36 | error('error within coroutine thread') 37 | end 38 | local co = coroutine.create( f ) 39 | print( 'status', coroutine.status(co) ) 40 | print( coroutine.resume(co,astring,anumber) ) 41 | print( 'status', coroutine.status(co) ) 42 | print( coroutine.resume(co,astring,anumber) ) 43 | print( 'status', coroutine.status(co) ) 44 | local s,e = coroutine.resume(co,astring,anumber) 45 | print( s, string.match(e,'error within coroutine thread') or 'bad message: '..tostring(e) ) 46 | print( 'status', coroutine.status(co) ) 47 | 48 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/Block.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2010 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.ast; 23 | 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | public class Block extends Stat { 28 | 29 | public List stats = new ArrayList(); 30 | public NameScope scope; 31 | 32 | public void add(Stat s) { 33 | if ( s == null ) 34 | return; 35 | stats.add(s); 36 | } 37 | 38 | public void accept(Visitor visitor) { 39 | visitor.visit(this); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /test/lua/oslib.lua: -------------------------------------------------------------------------------- 1 | -- simple os-library tests 2 | -- 3 | -- because the nature of the "os" library is to provide os-specific behavior, 4 | -- the compatibility tests must be extremely loose, and can really only 5 | -- compare things like return value type to be meaningful. 6 | -- 7 | -- actual os behavior needs to go in an oslib function test 8 | -- 9 | local pcall = function(...) 10 | local s,e,f = pcall(...) 11 | return s,type(e) 12 | end 13 | print( 'os', type(os) ) 14 | print( 'os.clock()', pcall( os.clock ) ) 15 | print( 'os.date()', pcall( os.date ) ) 16 | print( 'os.difftime(123000, 21500)', pcall( os.difftime, 123000, 21250 ) ) 17 | print( 'os.execute("bogus")', pcall( os.execute, '' ) ) 18 | print( 'os.getenv()', pcall( os.getenv ) ) 19 | print( 'os.getenv("bogus.key")', pcall( os.getenv, 'bogus.key' ) ) 20 | local s,p = pcall( os.tmpname ) 21 | local s,q = pcall( os.tmpname ) 22 | print( 'os.tmpname()', s, p ) 23 | print( 'os.tmpname()', s, q ) 24 | -- permission denied on windows 25 | --print( 'os.remove(p)', pcall( os.remove, p ) ) 26 | --print( 'os.rename(p,q)', pcall( os.rename, p, q ) ) 27 | local s,f = pcall( io.open, p,"w" ) 28 | print( 'io.open', s, f ) 29 | print( 'write', pcall( f.write, f, "abcdef 12345" ) ) 30 | print( 'close', pcall( f.close, f ) ) 31 | print( 'os.rename(p,q)', pcall( os.rename, p, q ) ) 32 | print( 'os.remove(q)', pcall( os.remove, q ) ) 33 | print( 'os.remove(q)', pcall( os.remove, q ) ) 34 | -- setlocale not supported on jse yet 35 | -- print( 'os.setlocale()', pcall( os.setlocale ) ) 36 | -- print( 'os.setlocale("jp")', pcall( os.setlocale, "jp" ) ) 37 | -- print( 'os.setlocale("us","monetary")', pcall( os.setlocale, "us", "monetary" ) ) 38 | -- print( 'os.setlocale(nil,"all")', pcall( os.setlocale, nil, "all" ) ) 39 | print( 'os.setlocale("C")', pcall( os.setlocale, "C" ) ) 40 | print( 'os.exit', type(os.exit) ) 41 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/ParList.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2010 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.ast; 23 | 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | public class ParList { 28 | public static final List EMPTY_NAMELIST = new ArrayList(); 29 | public static final ParList EMPTY_PARLIST = new ParList(EMPTY_NAMELIST,false); 30 | 31 | public final List names; 32 | public final boolean isvararg; 33 | 34 | public ParList(List names, boolean isvararg) { 35 | this.names = names; 36 | this.isvararg = isvararg; 37 | } 38 | 39 | public void accept(Visitor visitor) { 40 | visitor.visit(this); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/FuncName.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2010 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.ast; 23 | 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | public class FuncName { 28 | // example: a.b.c.d:e 29 | 30 | // initial base name: "a" 31 | public final Name name; 32 | 33 | // intermediate field accesses: "b", "c", "d" 34 | public List dots; 35 | 36 | // optional final method name: "e" 37 | public String method; 38 | 39 | public FuncName( String name ) { 40 | this.name = new Name(name); 41 | } 42 | 43 | public void adddot(String dot) { 44 | if ( dots == null ) 45 | dots = new ArrayList(); 46 | dots.add(dot); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /test/lua/upvalues.lua: -------------------------------------------------------------------------------- 1 | 2 | print( '-------- simple upvalues tests --------' ) 3 | local function simpleupvalues() 4 | function test() 5 | local x = 5 6 | function f() 7 | x = x + 1 8 | return x 9 | end 10 | function g() 11 | x = x - 1 12 | return x 13 | end 14 | print(f()) 15 | print(g()) 16 | return f, g 17 | end 18 | 19 | f1, g1 = test() 20 | print("f1()=", f1()) 21 | print("g1()=", g1()) 22 | 23 | f2, g2 = test() 24 | print("f2()=", f2()) 25 | print("g2()=", g2()) 26 | 27 | print("g1()=", g1()) 28 | print("f1()=", f1()) 29 | end 30 | print( 'simplevalues result:', pcall( simpleupvalues ) ) 31 | 32 | 33 | -- The point of this test is that when an upvalue is created, it may 34 | -- need to be inserted in the middle of the list, rather than always 35 | -- appended at the end. Otherwise, it may not be found when it is 36 | -- needed by another closure. 37 | print( '----------- upvalued in middle ------------' ) 38 | local function middleupvaluestest() 39 | local function test() 40 | local x = 3 41 | local y = 5 42 | local z = 7 43 | 44 | local function f() 45 | print("y=", y) 46 | end 47 | 48 | local function g() 49 | print("z=", z) 50 | end 51 | 52 | local function h() 53 | print("x=", x) 54 | end 55 | 56 | local function setter(x1, y1, z1) 57 | x = x1 58 | y = y1 59 | z = z1 60 | end 61 | 62 | return f, g, h, setter 63 | end 64 | 65 | local f, g, h, setter = test() 66 | 67 | h() 68 | f() 69 | g() 70 | 71 | setter("x", "y", "z") 72 | 73 | h() 74 | f() 75 | g() 76 | end 77 | print( pcall( middleupvaluestest ) ) 78 | 79 | 80 | print( '--------- nested upvalues ----------' ) 81 | local function nestedupvaluestest() 82 | local f 83 | do 84 | local x = 10 85 | function g() 86 | print(x, f()) 87 | end 88 | end 89 | 90 | function f() 91 | return 20 92 | end 93 | 94 | g() 95 | end 96 | print( 'nestedupvaluestest result:', pcall( nestedupvaluestest ) ) 97 | 98 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/TableField.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2010 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.ast; 23 | 24 | public class TableField { 25 | 26 | public final Exp index; 27 | public final String name; 28 | public final Exp rhs; 29 | 30 | public TableField(Exp index, String name, Exp rhs) { 31 | this.index = index; 32 | this.name = name; 33 | this.rhs = rhs; 34 | } 35 | 36 | public static TableField keyedField(Exp index, Exp rhs) { 37 | return new TableField(index, null, rhs); 38 | } 39 | 40 | public static TableField namedField(String name, Exp rhs) { 41 | return new TableField(null, name, rhs); 42 | } 43 | 44 | public static TableField listField(Exp rhs) { 45 | return new TableField(null, null, rhs); 46 | } 47 | 48 | public void accept(Visitor visitor) { 49 | visitor.visit(this); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /test/lua/errors/modulelibargs.lua: -------------------------------------------------------------------------------- 1 | package.path = "?.lua;test/lua/errors/?.lua" 2 | require 'args' 3 | 4 | -- arg type tests for module library functions 5 | 6 | -- require 7 | banner('require') 8 | checkallpass('require',{{'math','coroutine','package','string','table'}},true) 9 | checkallerrors('require',{{anumber}},'not found') 10 | checkallerrors('require',{{anil,aboolean,afunction,atable}},'bad argument') 11 | 12 | -- package.loadlib 13 | banner('package.loadlib') 14 | checkallpass('package.loadlib',{{'foo'},{'bar'}},true) 15 | checkallerrors('package.loadlib',{notastring},'bad argument') 16 | 17 | -- package.seeall 18 | banner('package.seeall') 19 | checkallpass('package.seeall',{sometable}) 20 | checkallerrors('package.seeall',{notatable},'bad argument') 21 | 22 | 23 | -- module tests - require special rigging 24 | banner('module') 25 | checkallerrors('module',{{20001},{nil,package.seeall,n=2},{nil,function()end,n=2}},"'module' not called from a Lua function") 26 | checkallerrors('module',{{'testmodule1'},{nil,'pqrs',aboolean,athread,atable}},"'module' not called from a Lua function") 27 | checkallerrors('module',{{aboolean,atable,function() end}},'bad argument') 28 | checkallerrors('module',{{aboolean,atable,function() end},{package.seeall}},'bad argument') 29 | 30 | -- enclose each invokation in its own function 31 | function invoke( name, arglist ) 32 | assert( name=='module', 'module rig used for '..name ) 33 | local func = function() 34 | module( unpack(arglist,1,arglist.n or #arglist) ) 35 | end 36 | return pcall( func ) 37 | end 38 | checkallpass('module',{{'foo1',20001}}) 39 | checkallpass('module',{{'foo2',20002},{package.seeall}}) 40 | checkallpass('module',{{'foo3',20003},{package.seeall},{function() end}}) 41 | checkallerrors('module',{{aboolean,atable,function() end}},'bad argument') 42 | checkallerrors('module',{{aboolean,atable,function() end},{package.seeall}},'bad argument') 43 | checkallerrors('module',{{'testmodule2'},{'pqrs'}},'attempt to call') 44 | checkallerrors('module',{{'testmodule3'},{aboolean}},'attempt to call') 45 | checkallerrors('module',{{'testmodule4'},{athread}},'attempt to call') 46 | checkallerrors('module',{{'testmodule5'},{atable}},'attempt to call') 47 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/LocVars.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009-2011 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2; 23 | 24 | /** 25 | * Data class to hold debug information relatign to local variables for a {@link Prototype} 26 | */ 27 | public class LocVars { 28 | /** The local variable name */ 29 | public LuaString varname; 30 | 31 | /** The instruction offset when the variable comes into scope */ 32 | public int startpc; 33 | 34 | /** The instruction offset when the variable goes out of scope */ 35 | public int endpc; 36 | 37 | /** 38 | * Construct a LocVars instance. 39 | * @param varname The local variable name 40 | * @param startpc The instruction offset when the variable comes into scope 41 | * @param endpc The instruction offset when the variable goes out of scope 42 | */ 43 | public LocVars(LuaString varname, int startpc, int endpc) { 44 | this.varname = varname; 45 | this.startpc = startpc; 46 | this.endpc = endpc; 47 | } 48 | 49 | public String tojstring() { 50 | return varname+" "+startpc+"-"+endpc; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/FuncArgs.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2010 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.ast; 23 | 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | import org.luaj.vm2.LuaString; 28 | 29 | public class FuncArgs { 30 | 31 | public final List exps; 32 | 33 | /** exp1,exp2... */ 34 | public static FuncArgs explist(List explist) { 35 | return new FuncArgs(explist); 36 | } 37 | 38 | /** {...} */ 39 | public static FuncArgs tableconstructor(TableConstructor table) { 40 | return new FuncArgs(table); 41 | } 42 | 43 | /** "mylib" */ 44 | public static FuncArgs string(LuaString string) { 45 | return new FuncArgs(string); 46 | } 47 | 48 | public FuncArgs(List exps) { 49 | this.exps = exps; 50 | } 51 | 52 | public FuncArgs(LuaString string) { 53 | this.exps = new ArrayList(); 54 | this.exps.add( Exp.constant(string) ); 55 | } 56 | 57 | public FuncArgs(TableConstructor table) { 58 | this.exps = new ArrayList(); 59 | this.exps.add( table ); 60 | } 61 | 62 | public void accept(Visitor visitor) { 63 | visitor.visit(this); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /test/lua/functions.lua: -------------------------------------------------------------------------------- 1 | 2 | function f0() print( "f0:" ) end 3 | function f1(a) print( "f1:", a ) end 4 | function f2(a,b) print( "f2:", a, b ) end 5 | function f3(a,b,c) print( "f3:", a, b, c ) end 6 | function f4(a,b,c,d) print( "f4:", a, b, c, d ) end 7 | 8 | f0() f0( "a1/1" ) f0( "a1/2", "a2/2" ) f0( "a1/3", "a2/3", "a3/3" ) f0( "a1/4", "a2/4", "a3/4", "a4/4" ) 9 | f1() f1( "a1/1" ) f1( "a1/2", "a2/2" ) f1( "a1/3", "a2/3", "a3/3" ) f1( "a1/4", "a2/4", "a3/4", "a4/4" ) 10 | f2() f2( "a1/1" ) f2( "a1/2", "a2/2" ) f2( "a1/3", "a2/3", "a3/3" ) f2( "a1/4", "a2/4", "a3/4", "a4/4" ) 11 | f3() f3( "a1/1" ) f3( "a1/2", "a2/2" ) f3( "a1/3", "a2/3", "a3/3" ) f3( "a1/4", "a2/4", "a3/4", "a4/4" ) 12 | f4() f4( "a1/1" ) f4( "a1/2", "a2/2" ) f4( "a1/3", "a2/3", "a3/3" ) f4( "a1/4", "a2/4", "a3/4", "a4/4" ) 13 | 14 | function g0(a,b,c,d) return end 15 | function g1(a,b,c,d) return d end 16 | function g2(a,b,c,d) return c, d end 17 | function g3(a,b,c,d) return b, c, d end 18 | function g4(a,b,c,d) return a, b, c, d end 19 | 20 | z = g0("c0.1/4", "c0.2/4", "c0.3/4", "c0.4/4") 21 | print( "z0:", z ) 22 | z = g2("c2.1/4", "c2.2/4", "c2.3/4", "c2.4/4") 23 | print( "z2:", z ) 24 | z = g4("c4.1/4", "c4.2/4", "c4.3/4", "c4.4/4") 25 | print( "z4:", z ) 26 | 27 | a,b,c,d = g0( "c0.1/4", "c0.2/4", "c0.3/4", "c0.4/4" ) 28 | print( "g0:", a, b, c, d, "(eol)" ) 29 | a,b,c,d = g2( "b2.1/4", "b2.2/4", "b2.3/4", "b2.4/4" ) 30 | print( "g2:", a, b, c, d, "(eol)" ) 31 | a,b,c,d = g4( "b4.1/4", "b4.2/4", "b4.3/4", "b4.4/4" ) 32 | print( "g4:", a, b, c, d, "(eol)" ) 33 | 34 | function func(a,b,c) 35 | return a, b, c 36 | end 37 | 38 | print( func(11, 12, 13) ) 39 | print( func(23, 22, 21) ) 40 | print( func(func(32,33,34), func(45,46,47), func(58,59,50)) ) 41 | 42 | function p(a,...) 43 | print("a",a) 44 | print("...",...) 45 | print("...,a",...,a) 46 | print("a,...",a,...) 47 | end 48 | p() 49 | p("q") 50 | p("q","r") 51 | p("q","r","s") 52 | 53 | -- tail call tests 54 | function first(...) 55 | return 'abc', ..., '|', ... 56 | end 57 | 58 | function second(a,...) 59 | return 'def', ..., '|', a, ... 60 | end 61 | 62 | function third( a, b, c ) 63 | print( 'third', first( a, b, c ) ) 64 | print( 'third', second( a, b, c ) ) 65 | return second( a, b, c ) 66 | end 67 | 68 | print( 'third', third() ) 69 | print( 'third', third('p') ) 70 | print( 'third', third('p','q') ) 71 | print( 'third', third('p','q','r') ) 72 | print( 'third', third('p','q','r','s') ) 73 | print( 'third', third() ) 74 | 75 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/lib/ResourceFinder.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009-2011 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.lib; 23 | 24 | import java.io.InputStream; 25 | 26 | /** 27 | * Interface for opening application resource files such as scripts sources. 28 | *

29 | * This is used by required to load files that are part of 30 | * the application, and implemented by BaseLib 31 | * for both the Jme and Jse platforms. 32 | *

33 | * The Jme version of base lib {@link BaseLib} 34 | * implements {@link BaseLib#FINDER} via {@link Class#getResourceAsStream(String)}, 35 | * while the Jse version {@link JseBaseLib} implements it using {@link java.io.File#File(String)}. 36 | *

37 | * The io library does not use this API for file manipulation. 38 | *

39 | * @see BaseLib 40 | * @see BaseLib#FINDER 41 | * @see JseBaseLib 42 | * @see JmePlatform 43 | * @see JsePlatform 44 | */ 45 | public interface ResourceFinder { 46 | 47 | /** 48 | * Try to open a file, or return null if not found. 49 | * 50 | * @see org.luaj.vm2.lib.BaseLib 51 | * @see org.luaj.vm2.lib.jse.JseBaseLib 52 | * 53 | * @param filename 54 | * @return InputStream, or null if not found. 55 | */ 56 | public InputStream findResource( String filename ); 57 | } -------------------------------------------------------------------------------- /test/lua/coroutinelib.lua: -------------------------------------------------------------------------------- 1 | function printrunning() 2 | if coroutine.running() == nil then 3 | print("running is nil"); 4 | else 5 | print("running is not nil") 6 | end 7 | end 8 | 9 | function foo (a) 10 | print("foo", a) 11 | return coroutine.yield(2*a) 12 | end 13 | 14 | co = coroutine.create(function (a,b) 15 | print("co-body", a, b) 16 | local r = foo(a+1) 17 | print("co-body", r) 18 | local r, s = coroutine.yield(a+b, a-b) 19 | print("co-body", r, s) 20 | 21 | printrunning() 22 | print("co.status.inside",coroutine.status(co)); 23 | local co2 = coroutine.create(function() 24 | print("co.status.inside2",coroutine.status(co)); 25 | end) 26 | print("co.status.inside",coroutine.status(co)); 27 | coroutine.resume(co2); 28 | 29 | return b, "end" 30 | end) 31 | 32 | function exercise() 33 | printrunning() 34 | print("co.status",coroutine.status(co)); 35 | print("main", coroutine.resume(co, 1, 10)) 36 | print("co.status",coroutine.status(co)); 37 | print("main", coroutine.resume(co, "r")) 38 | print("co.status",coroutine.status(co)); 39 | print("main", coroutine.resume(co, "x", "y")) 40 | print("co.status",coroutine.status(co)); 41 | print("main", coroutine.resume(co, "x", "y")) 42 | print("co.status",coroutine.status(co)); 43 | end 44 | 45 | exercise(); 46 | 47 | co = coroutine.create(function (a,b) 48 | print("co-body", a, b) 49 | -- TODO: make java and C behave the same for yielding in pcalls 50 | -- local status,r = pcall( foo, a+1 ) 51 | foo(a+1) 52 | print("co-body", status,r) 53 | local r, s = coroutine.yield(a+b, a-b) 54 | print("co-body", r, s) 55 | return b, "end" 56 | end) 57 | 58 | exercise(); 59 | 60 | 61 | -- wrap test 62 | local g = coroutine.wrap(function (a,b) 63 | print("co-body", a, b) 64 | local r = foo(a+1) 65 | print("co-body", r) 66 | local r, s = coroutine.yield(a+b, a-b) 67 | print("co-body", r, s) 68 | return b, "end" 69 | end ) 70 | 71 | print("g", g(1, 10)) 72 | print("g", g("r")) 73 | print("g", g("x", "y")) 74 | local s,e = pcall( g, "x", "y" ) 75 | print("g", string.match(e,'cannot resume dead coroutine') or 'badmessage: '..tostring(e)) 76 | 77 | -- varargs passing 78 | local echo = function(msg,...) 79 | print( msg, ...) 80 | return ... 81 | end 82 | local echocr = function(...) 83 | echo('(echocr) first args', unpack(arg,1,arg.n)) 84 | local a = arg 85 | while true do 86 | a = { echo( '(echoch) yield returns', coroutine.yield( unpack(a) ) ) } 87 | end 88 | end 89 | local c = coroutine.create( echocr ) 90 | local step = function(...) 91 | echo( '(main) resume returns', 92 | coroutine.resume(c, echo('(main) sending args', ...)) ) 93 | end 94 | step(111,222,333) 95 | step() 96 | step(111) 97 | step(111,222,333) 98 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/Variable.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2010 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.ast; 23 | 24 | import org.luaj.vm2.LuaValue; 25 | 26 | /** Variable is created lua name scopes, and is a named, lua variable that 27 | * either refers to a lua local, global, or upvalue storage location. 28 | */ 29 | public class Variable { 30 | 31 | /** The name as it appears in lua source code */ 32 | public final String name; 33 | 34 | /** The lua scope in which this variable is defined. */ 35 | public final NameScope definingScope; 36 | 37 | /** true if this variable is an upvalue */ 38 | public boolean isupvalue; 39 | 40 | /** true if there are assignments made to this variable */ 41 | public boolean hasassignments; 42 | 43 | /** When hasassignments == false, and the initial value is a constant, this is the initial value */ 44 | public LuaValue initialValue; 45 | 46 | /** Global is named variable not associated with a defining scope */ 47 | public Variable(String name) { 48 | this.name = name; 49 | this.definingScope = null; 50 | } 51 | public Variable(String name, NameScope definingScope) { 52 | /** Local variable is defined in a particular scope. */ 53 | this.name = name; 54 | this.definingScope = definingScope; 55 | } 56 | public boolean isLocal() { 57 | return this.definingScope != null; 58 | } 59 | public boolean isConstant() { 60 | return ! hasassignments && initialValue != null; 61 | } 62 | } -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/UpValue.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009-2011 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2; 23 | 24 | 25 | /** Upvalue used with Closure formulation 26 | *

27 | * @see LuaClosure 28 | * @see Prototype 29 | */ 30 | public final class UpValue { 31 | 32 | LuaValue[] array; // initially the stack, becomes a holder 33 | int index; 34 | 35 | /** 36 | * Create an upvalue relative to a stack 37 | * @param stack the stack 38 | * @param index the index on the stack for the upvalue 39 | */ 40 | public UpValue( LuaValue[] stack, int index) { 41 | this.array = stack; 42 | this.index = index; 43 | } 44 | 45 | /** 46 | * Convert this upvalue to a Java String 47 | * @return the Java String for this upvalue. 48 | * @see LuaValue#tojstring() 49 | */ 50 | public String tojstring() { 51 | return array[index].tojstring(); 52 | } 53 | 54 | /** 55 | * Get the value of the upvalue 56 | * @return the {@link LuaValue} for this upvalue 57 | */ 58 | public final LuaValue getValue() { 59 | return array[index]; 60 | } 61 | 62 | /** 63 | * Set the value of the upvalue 64 | * @param the {@link LuaValue} to set it to 65 | */ 66 | public final void setValue( LuaValue value ) { 67 | array[index] = value; 68 | } 69 | 70 | /** 71 | * Close this upvalue so it is no longer on the stack 72 | */ 73 | public final void close() { 74 | array = new LuaValue[] { array[index] }; 75 | index = 0; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/ErrorsTest.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2; 23 | 24 | import java.io.IOException; 25 | import java.io.InputStream; 26 | 27 | import org.luaj.vm2.compiler.LuaC; 28 | import org.luaj.vm2.lib.BaseLib; 29 | 30 | 31 | /** 32 | * Test argument type check errors 33 | * 34 | * Results are compared for exact match with 35 | * the installed C-based lua environment. 36 | */ 37 | public class ErrorsTest extends ScriptDrivenTest { 38 | 39 | private static final String dir = "test/lua/errors"; 40 | 41 | public ErrorsTest() { 42 | super(ScriptDrivenTest.PlatformType.JSE, dir); 43 | } 44 | 45 | protected void setUp() throws Exception { 46 | super.setUp(); 47 | } 48 | 49 | public void testBaseLibArgs() { 50 | BaseLib.instance.STDIN = new InputStream() { 51 | public int read() throws IOException { 52 | return -1; 53 | } 54 | }; 55 | runTest("baselibargs"); 56 | } 57 | public void testCoroutineLibArgs() { runTest("coroutinelibargs"); } 58 | public void testIoLibArgs() { runTest("iolibargs"); } 59 | public void testMathLibArgs() { runTest("mathlibargs"); } 60 | public void testModuleLibArgs() { runTest("modulelibargs"); } 61 | public void testOperators() { runTest("operators"); } 62 | public void testStringLibArgs() { runTest("stringlibargs"); } 63 | public void testTableLibArgs() { runTest("tablelibargs"); } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /test/lua/errors/tablelibargs.lua: -------------------------------------------------------------------------------- 1 | package.path = "?.lua;test/lua/errors/?.lua" 2 | require 'args' 3 | 4 | -- arg type tests for table library functions 5 | 6 | -- table.concat 7 | local somestringstable = {{8,7,6,5,4,3,2,1,}} 8 | local somenonstringtable = {{true,true,true,true,true,true,true,true,}} 9 | local somesep = {',',1.23} 10 | local notasep = {aboolean,atable,afunction} 11 | local somei = {2,'2','2.2'} 12 | local somej = {4,'4','4.4'} 13 | local notij = {astring,aboolean,atable,afunction} 14 | banner('table.concat') 15 | checkallpass('table.concat',{somestringstable}) 16 | checkallpass('table.concat',{somestringstable,somesep}) 17 | checkallpass('table.concat',{somestringstable,{'-'},somei}) 18 | checkallpass('table.concat',{somestringstable,{'-'},{2},somej}) 19 | checkallerrors('table.concat',{notatable},'bad argument') 20 | checkallerrors('table.concat',{somenonstringtable},'boolean') 21 | checkallerrors('table.concat',{somestringstable,notasep},'bad argument') 22 | checkallerrors('table.concat',{somestringstable,{'-'},notij},'bad argument') 23 | checkallerrors('table.concat',{somestringstable,{'-'},{2},notij},'bad argument') 24 | 25 | -- table.insert 26 | banner('table.insert') 27 | checkallpass('table.insert',{sometable,notanil}) 28 | checkallpass('table.insert',{sometable,somei,notanil}) 29 | checkallerrors('table.insert',{notatable,somestring},'bad argument') 30 | checkallerrors('table.insert',{sometable,notij,notanil},'bad argument') 31 | 32 | -- table.maxn 33 | banner('table.maxn') 34 | checkallpass('table.maxn',{sometable}) 35 | checkallerrors('table.maxn',{notatable},'bad argument') 36 | 37 | -- table.remove 38 | banner('table.remove') 39 | checkallpass('table.remove',{sometable}) 40 | checkallpass('table.remove',{sometable,somei}) 41 | checkallerrors('table.remove',{notatable},'bad argument') 42 | checkallerrors('table.remove',{notatable,somei},'bad argument') 43 | checkallerrors('table.remove',{sometable,notij},'bad argument') 44 | 45 | -- table.sort 46 | local somecomp = {nil,afunction,n=2} 47 | local notacomp = {astring,anumber,aboolean,atable} 48 | banner('table.sort') 49 | checkallpass('table.sort',{somestringstable,somecomp}) 50 | checkallerrors('table.sort',{sometable},'attempt to') 51 | checkallerrors('table.sort',{notatable,somecomp},'bad argument') 52 | checkallerrors('table.sort',{sometable,notacomp},'bad argument') 53 | 54 | -- table get 55 | banner('table_get - tbl[key]') 56 | function table_get(tbl,key) return tbl[key] end 57 | checkallpass('table_get',{sometable,anylua}) 58 | 59 | -- table set 60 | banner('table_set - tbl[key]=val') 61 | function table_set(tbl,key,val) tbl[key]=val end 62 | function table_set_nil_key(tbl,val) tbl[nil]=val end 63 | checkallpass('table_set',{sometable,notanil,anylua}) 64 | checkallerrors('table_set_nil_key',{sometable,anylua},'table index') 65 | 66 | 67 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/LuaFunction.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009-2011 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2; 23 | 24 | /** 25 | * Base class for functions implemented in Java. 26 | *

27 | * Direct subclass include {@link LibFunction} which is the base class for 28 | * all built-in library functions coded in Java, 29 | * and {@link LuaClosure}, which represents a lua closure 30 | * whose bytecode is interpreted when the function is invoked. 31 | * @see LuaValue 32 | * @see LibFunction 33 | * @see LuaClosure 34 | */ 35 | abstract 36 | public class LuaFunction extends LuaValue { 37 | 38 | /** Shared static metatable for all functions and closures. */ 39 | public static LuaValue s_metatable; 40 | 41 | protected LuaValue env; 42 | 43 | public LuaFunction() { 44 | this.env = NIL; 45 | } 46 | 47 | public LuaFunction(LuaValue env) { 48 | this.env = env; 49 | } 50 | 51 | public int type() { 52 | return TFUNCTION; 53 | } 54 | 55 | public String typename() { 56 | return "function"; 57 | } 58 | 59 | public boolean isfunction() { 60 | return true; 61 | } 62 | 63 | public LuaValue checkfunction() { 64 | return this; 65 | } 66 | 67 | public LuaFunction optfunction(LuaFunction defval) { 68 | return this; 69 | } 70 | 71 | public LuaValue getmetatable() { 72 | return s_metatable; 73 | } 74 | 75 | public LuaValue getfenv() { 76 | return env; 77 | } 78 | 79 | public void setfenv(LuaValue env) { 80 | this.env = env!=null? env: NIL; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /test/java/org/luaj/luajc/TestLuaJ.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.luajc; 23 | 24 | import java.io.ByteArrayInputStream; 25 | import java.io.InputStream; 26 | 27 | import org.luaj.vm2.LuaClosure; 28 | import org.luaj.vm2.LuaTable; 29 | import org.luaj.vm2.LuaValue; 30 | import org.luaj.vm2.Print; 31 | import org.luaj.vm2.Prototype; 32 | import org.luaj.vm2.compiler.LuaC; 33 | import org.luaj.vm2.lib.jse.JsePlatform; 34 | 35 | /** Test the plain old bytecode interpreter */ 36 | public class TestLuaJ { 37 | // create the script 38 | public static String name = "script"; 39 | public static String script = 40 | "function r(q,...)\n"+ 41 | " local a=arg\n"+ 42 | " return a and a[2]\n"+ 43 | "end\n" + 44 | "function s(q,...)\n"+ 45 | " local a=arg\n"+ 46 | " local b=...\n"+ 47 | " return a and a[2],b\n"+ 48 | "end\n" + 49 | "print( r(111,222,333),s(111,222,333) )"; 50 | 51 | public static void main(String[] args) throws Exception { 52 | System.out.println(script); 53 | 54 | // create an environment to run in 55 | LuaTable _G = JsePlatform.standardGlobals(); 56 | 57 | // compile into a chunk, or load as a class 58 | InputStream is = new ByteArrayInputStream( script.getBytes() ); 59 | LuaValue chunk = LuaC.instance.load(is, "script",_G); 60 | chunk.call(); 61 | } 62 | 63 | private static void print(Prototype p) { 64 | System.out.println("--- "+p.is_vararg); 65 | Print.printCode(p); 66 | if (p.p!=null) 67 | for ( int i=0,n=p.p.length; i10 then\n"+ 57 | " break\n"+ 58 | " end\n"+ 59 | " a=a+1\n"+ 60 | " print( a )\n"+ 61 | "end\n"; 62 | doTest( s ); 63 | } 64 | 65 | public void testShebang() { 66 | String s = "#!../lua\n"+ 67 | "print( 2 )\n"; 68 | doTest( s ); 69 | } 70 | 71 | public void testInlineTable() { 72 | String s = "A = {g=10}\n"+ 73 | "print( A )\n"; 74 | doTest( s ); 75 | } 76 | 77 | public void testEqualsAnd() { 78 | String s = "print( 1 == b and b )\n"; 79 | doTest( s ); 80 | } 81 | 82 | private static final int [] samehash = { 0, 1, -1, 2, -2, 4, 8, 16, 32, Integer.MAX_VALUE, Integer.MIN_VALUE }; 83 | private static final double [] diffhash = { .5, 1, 1.5, 1, .5, 1.5, 1.25, 2.5 }; 84 | 85 | public void testDoubleHashCode() { 86 | for ( int i=0; i 27 | * The main subclasses are {@link LuaInteger} which holds values that fit in a java int, 28 | * and {@link LuaDouble} which holds all other number values. 29 | * @see LuaInteger 30 | * @see LuaDouble 31 | * @see LuaValue 32 | * 33 | */ 34 | abstract 35 | public class LuaNumber extends LuaValue { 36 | 37 | /** Shared static metatable for all number values represented in lua. */ 38 | public static LuaValue s_metatable; 39 | 40 | public int type() { 41 | return TNUMBER; 42 | } 43 | 44 | public String typename() { 45 | return "number"; 46 | } 47 | 48 | public LuaNumber checknumber() { 49 | return this; 50 | } 51 | 52 | public LuaNumber checknumber(String errmsg) { 53 | return this; 54 | } 55 | 56 | public LuaNumber optnumber(LuaNumber defval) { 57 | return this; 58 | } 59 | 60 | public LuaValue tonumber() { 61 | return this; 62 | } 63 | 64 | public boolean isnumber() { 65 | return true; 66 | } 67 | 68 | public boolean isstring() { 69 | return true; 70 | } 71 | 72 | public LuaValue getmetatable() { 73 | return s_metatable; 74 | } 75 | 76 | public LuaValue concat(LuaValue rhs) { return rhs.concatTo(this); } 77 | public Buffer concat(Buffer rhs) { return rhs.concatTo(this); } 78 | public LuaValue concatTo(LuaNumber lhs) { return strvalue().concatTo(lhs.strvalue()); } 79 | public LuaValue concatTo(LuaString lhs) { return strvalue().concatTo(lhs); } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/luajc/JavaLoader.java: -------------------------------------------------------------------------------- 1 | package org.luaj.vm2.luajc; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.luaj.vm2.LuaFunction; 7 | import org.luaj.vm2.LuaValue; 8 | import org.luaj.vm2.Prototype; 9 | 10 | /******************************************************************************* 11 | * Copyright (c) 2010 Luaj.org. All rights reserved. 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy 14 | * of this software and associated documentation files (the "Software"), to deal 15 | * in the Software without restriction, including without limitation the rights 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | * copies of the Software, and to permit persons to whom the Software is 18 | * furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included in 21 | * all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | * THE SOFTWARE. 30 | ******************************************************************************/ 31 | public class JavaLoader extends ClassLoader { 32 | 33 | private final LuaValue env; 34 | 35 | private Map unloaded = new HashMap(); 36 | 37 | public JavaLoader( LuaValue env ) { 38 | this.env = env; 39 | } 40 | 41 | public LuaFunction load( Prototype p, String classname, String filename ) { 42 | JavaGen jg = new JavaGen( p, classname, filename ); 43 | return load( jg ); 44 | } 45 | 46 | public LuaFunction load( JavaGen jg ) { 47 | include( jg ); 48 | return load( jg.classname ); 49 | } 50 | 51 | public LuaFunction load(String classname) { 52 | try { 53 | Class c = loadClass( classname ); 54 | LuaFunction v = (LuaFunction) c.newInstance(); 55 | v.setfenv(env); 56 | return v; 57 | } catch ( Exception e ) { 58 | e.printStackTrace(); 59 | throw new IllegalStateException("bad class gen: "+e); 60 | } 61 | } 62 | 63 | public void include( JavaGen jg ) { 64 | unloaded.put( jg.classname, jg.bytecode ); 65 | for ( int i=0, n=jg.inners!=null? jg.inners.length: 0; i 27 | * This is both a straight translation of the corresponding C type, 28 | * and the main data structure for execution of compiled lua bytecode. 29 | *

30 | * See documentatation on {@link LuaClosure} for information on how to load 31 | * and execute a {@link Prototype}. 32 | * @see LuaClosure 33 | */ 34 | 35 | public class Prototype { 36 | /* constants used by the function */ 37 | public LuaValue[] k; 38 | public int[] code; 39 | /* functions defined inside the function */ 40 | public Prototype[] p; 41 | /* map from opcodes to source lines */ 42 | public int[] lineinfo; 43 | /* information about local variables */ 44 | public LocVars[] locvars; 45 | /* upvalue names */ 46 | public LuaString[] upvalues; 47 | public LuaString source; 48 | public int nups; 49 | public int linedefined; 50 | public int lastlinedefined; 51 | public int numparams; 52 | public int is_vararg; 53 | public int maxstacksize; 54 | 55 | 56 | public String toString() { 57 | return source + ":" + linedefined+"-"+lastlinedefined; 58 | } 59 | 60 | /** Get the name of a local variable. 61 | * 62 | * @param number the local variable number to look up 63 | * @param pc the program counter 64 | * @return the name, or null if not found 65 | */ 66 | public LuaString getlocalname(int number, int pc) { 67 | int i; 68 | for (i = 0; i 30 | * Subclasses need only implement {@link LuaValue#call()} to complete this class, 31 | * simplifying development. 32 | * All other uses of {@link #call(LuaValue)}, {@link #invoke(Varargs)},etc, 33 | * are routed through this method by this class. 34 | *

35 | * If one or more arguments are required, or variable argument or variable return values, 36 | * then use one of the related function 37 | * {@link OneArgFunction}, {@link TwoArgFunction}, {@link ThreeArgFunction}, or {@link VarArgFunction}. 38 | *

39 | * See {@link LibFunction} for more information on implementation libraries and library functions. 40 | * @see #call() 41 | * @see LibFunction 42 | * @see OneArgFunction 43 | * @see TwoArgFunction 44 | * @see ThreeArgFunction 45 | * @see VarArgFunction 46 | */ 47 | abstract public class ZeroArgFunction extends LibFunction { 48 | 49 | abstract public LuaValue call(); 50 | 51 | /** Default constructor */ 52 | public ZeroArgFunction() { 53 | } 54 | 55 | /** Constructor with specific environment 56 | * @param env The environment to apply during constructon. 57 | */ 58 | public ZeroArgFunction( LuaValue env ) { 59 | this.env = env; 60 | } 61 | 62 | public LuaValue call(LuaValue arg) { 63 | return call(); 64 | } 65 | 66 | public LuaValue call(LuaValue arg1, LuaValue arg2) { 67 | return call(); 68 | } 69 | 70 | public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) { 71 | return call(); 72 | } 73 | 74 | public Varargs invoke(Varargs varargs) { 75 | return call(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/lib/OneArgFunction.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009-2011 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.lib; 23 | 24 | import org.luaj.vm2.LuaValue; 25 | import org.luaj.vm2.Varargs; 26 | 27 | /** Abstract base class for Java function implementations that take one argument and 28 | * return one value. 29 | *

30 | * Subclasses need only implement {@link LuaValue#call(LuaValue)} to complete this class, 31 | * simplifying development. 32 | * All other uses of {@link #call()}, {@link #invoke(Varargs)},etc, 33 | * are routed through this method by this class, 34 | * dropping or extending arguments with {@code nil} values as required. 35 | *

36 | * If more than one argument are required, or no arguments are required, 37 | * or variable argument or variable return values, 38 | * then use one of the related function 39 | * {@link ZeroArgFunction}, {@link TwoArgFunction}, {@link ThreeArgFunction}, or {@link VarArgFunction}. 40 | *

41 | * See {@link LibFunction} for more information on implementation libraries and library functions. 42 | * @see #call(LuaValue) 43 | * @see LibFunction 44 | * @see ZeroArgFunction 45 | * @see TwoArgFunction 46 | * @see ThreeArgFunction 47 | * @see VarArgFunction 48 | */ 49 | abstract public class OneArgFunction extends LibFunction { 50 | 51 | abstract public LuaValue call(LuaValue arg); 52 | 53 | /** Default constructor */ 54 | public OneArgFunction() { 55 | } 56 | 57 | /** Constructor with specific environment 58 | * @param env The environment to apply during constructon. 59 | */ 60 | public OneArgFunction( LuaValue env ) { 61 | this.env = env; 62 | } 63 | 64 | public final LuaValue call() { 65 | return call(NIL); 66 | } 67 | 68 | public final LuaValue call(LuaValue arg1, LuaValue arg2) { 69 | return call(arg1); 70 | } 71 | 72 | public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) { 73 | return call(arg1); 74 | } 75 | 76 | public Varargs invoke(Varargs varargs) { 77 | return call(varargs.arg1()); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/RequireClassTest.java: -------------------------------------------------------------------------------- 1 | package org.luaj.vm2; 2 | 3 | import junit.framework.TestCase; 4 | 5 | import org.luaj.vm2.lib.jse.JsePlatform; 6 | import org.luaj.vm2.require.RequireSampleClassCastExcep; 7 | import org.luaj.vm2.require.RequireSampleLoadLuaError; 8 | import org.luaj.vm2.require.RequireSampleLoadRuntimeExcep; 9 | 10 | public class RequireClassTest extends TestCase { 11 | 12 | private LuaTable globals; 13 | private LuaValue require; 14 | 15 | public void setUp() { 16 | globals = JsePlatform.standardGlobals(); 17 | require = globals.get("require"); 18 | } 19 | 20 | public void testRequireClassSuccess() { 21 | LuaValue result = require.call( LuaValue.valueOf("org.luaj.vm2.require.RequireSampleSuccess") ); 22 | assertEquals( "require-sample-success", result.tojstring() ); 23 | result = require.call( LuaValue.valueOf("org.luaj.vm2.require.RequireSampleSuccess") ); 24 | assertEquals( "require-sample-success", result.tojstring() ); 25 | } 26 | 27 | public void testRequireClassLoadLuaError() { 28 | try { 29 | LuaValue result = require.call( LuaValue.valueOf(RequireSampleLoadLuaError.class.getName()) ); 30 | fail( "incorrectly loaded class that threw lua error"); 31 | } catch ( LuaError le ) { 32 | assertEquals( 33 | "sample-load-lua-error", 34 | le.getMessage() ); 35 | } 36 | try { 37 | LuaValue result = require.call( LuaValue.valueOf(RequireSampleLoadLuaError.class.getName()) ); 38 | fail( "incorrectly loaded class that threw lua error"); 39 | } catch ( LuaError le ) { 40 | assertEquals( 41 | "loop or previous error loading module '"+RequireSampleLoadLuaError.class.getName()+"'", 42 | le.getMessage() ); 43 | } 44 | } 45 | 46 | public void testRequireClassLoadRuntimeException() { 47 | try { 48 | LuaValue result = require.call( LuaValue.valueOf(RequireSampleLoadRuntimeExcep.class.getName()) ); 49 | fail( "incorrectly loaded class that threw runtime exception"); 50 | } catch ( RuntimeException le ) { 51 | assertEquals( 52 | "sample-load-runtime-exception", 53 | le.getMessage() ); 54 | } 55 | try { 56 | LuaValue result = require.call( LuaValue.valueOf(RequireSampleLoadRuntimeExcep.class.getName()) ); 57 | fail( "incorrectly loaded class that threw runtime exception"); 58 | } catch ( LuaError le ) { 59 | assertEquals( 60 | "loop or previous error loading module '"+RequireSampleLoadRuntimeExcep.class.getName()+"'", 61 | le.getMessage() ); 62 | } 63 | } 64 | 65 | 66 | public void testRequireClassClassCastException() { 67 | try { 68 | LuaValue result = require.call( LuaValue.valueOf(RequireSampleClassCastExcep.class.getName()) ); 69 | fail( "incorrectly loaded class that threw class cast exception"); 70 | } catch ( LuaError le ) { 71 | String msg = le.getMessage(); 72 | if ( msg.indexOf("not found") < 0 ) 73 | fail( "expected 'not found' message but got "+msg ); 74 | } 75 | try { 76 | LuaValue result = require.call( LuaValue.valueOf(RequireSampleClassCastExcep.class.getName()) ); 77 | fail( "incorrectly loaded class that threw class cast exception"); 78 | } catch ( LuaError le ) { 79 | String msg = le.getMessage(); 80 | if ( msg.indexOf("not found") < 0 ) 81 | fail( "expected 'not found' message but got "+msg ); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /test/lua/errors/iolibargs.lua: -------------------------------------------------------------------------------- 1 | package.path = "?.lua;test/lua/errors/?.lua" 2 | require 'args' 3 | 4 | -- arg type tests for io library functions 5 | local f 6 | 7 | -- io.close ([file]) 8 | banner('io.close') 9 | f = io.open("abc.txt","w") 10 | checkallpass('io.close',{{f}}) 11 | checkallerrors('io.close',{notanil},'bad argument') 12 | 13 | -- io.input ([file]) 14 | banner('io.input') 15 | f = io.open("abc.txt","r") 16 | checkallpass('io.input',{{nil,f,"abc.txt",n=3}}) 17 | checkallerrors('io.input',{nonstring},'bad argument') 18 | 19 | -- io.lines ([filename]) 20 | banner('io.lines') 21 | io.input("abc.txt") 22 | checkallpass('io.lines',{{"abc.txt"}}) 23 | checkallerrors('io.lines',{{f}},'bad argument') 24 | checkallerrors('io.lines',{notastring},'bad argument') 25 | 26 | -- io.open (filename [, mode]) 27 | banner('io.open') 28 | checkallpass('io.open',{{"abc.txt"},{nil,"r","w","a","r+","w+","a+"}}) 29 | checkallerrors('io.open',{notastring},'bad argument') 30 | checkallerrors('io.open',{{"abc.txt"},{nonstring}},'bad argument') 31 | 32 | -- io.output ([file]) 33 | banner('io.output') 34 | checkallpass('io.output',{{nil,f,"abc.txt",n=3}}) 35 | checkallerrors('io.output',{nonstring},'bad argument') 36 | 37 | -- io.popen (prog [, mode]) 38 | banner('io.popen') 39 | --checkallpass('io.popen',{{"hostname"},{nil,"w",n=2}}) 40 | checkallerrors('io.popen',{notastring},'bad argument') 41 | checkallerrors('io.popen',{{"hostname"},{nonstring}},'bad argument') 42 | 43 | -- io.read (���) 44 | banner('io.read') 45 | checkallpass('io.read',{}) 46 | checkallpass('io.read',{{2,"*n","*a","*l"}}) 47 | checkallpass('io.read',{{2,"*n","*a","*l"},{2,"*a","*l"}}) 48 | checkallerrors('io.read',{{aboolean,afunction,atable,"3"}},'bad argument') 49 | 50 | -- io.write (���) 51 | banner('io.write') 52 | checkallpass('io.write',{}) 53 | checkallpass('io.write',{somestring}) 54 | checkallpass('io.write',{somestring,somestring}) 55 | checkallerrors('io.write',{nonstring},'bad argument') 56 | checkallerrors('io.write',{somestring,nonstring},'bad argument') 57 | 58 | -- file:write () 59 | banner('file:write') 60 | file = io.open("seektest.txt","w") 61 | checkallpass('file.write',{{file},somestring}) 62 | checkallpass('file.write',{{file},somestring,somestring}) 63 | checkallerrors('file.write',{},'bad argument') 64 | checkallerrors('file.write',{{file},nonstring},'bad argument') 65 | checkallerrors('file.write',{{file},somestring,nonstring},'bad argument') 66 | pcall( file.close, file ) 67 | 68 | -- file:seek ([whence] [, offset]) 69 | banner('file:seek') 70 | file = io.open("seektest.txt","r") 71 | checkallpass('file.seek',{{file}}) 72 | checkallpass('file.seek',{{file},{"set","cur","end"}}) 73 | checkallpass('file.seek',{{file},{"set","cur","end"},{2,"3"}}) 74 | checkallerrors('file.seek',{},'bad argument') 75 | checkallerrors('file.seek',{{file},nonstring},'bad argument') 76 | checkallerrors('file.seek',{{file},{"set","cur","end"},nonnumber},'bad argument') 77 | 78 | -- file:setvbuf (mode [, size]) 79 | banner('file:setvbuf') 80 | checkallpass('file.setvbuf',{{file},{"no","full","line"}}) 81 | checkallpass('file.setvbuf',{{file},{"full"},{1024,"512"}}) 82 | checkallerrors('file.setvbuf',{},'bad argument') 83 | checkallerrors('file.setvbuf',{{file},notastring},'bad argument') 84 | checkallerrors('file.setvbuf',{{file},{"full"},nonnumber},'bad argument') 85 | 86 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/lib/TwoArgFunction.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.lib; 23 | 24 | import org.luaj.vm2.LuaValue; 25 | import org.luaj.vm2.Varargs; 26 | 27 | /** Abstract base class for Java function implementations that take two arguments and 28 | * return one value. 29 | *

30 | * Subclasses need only implement {@link LuaValue#call(LuaValue,LuaValue)} to complete this class, 31 | * simplifying development. 32 | * All other uses of {@link #call()}, {@link #invoke(Varargs)},etc, 33 | * are routed through this method by this class, 34 | * dropping or extending arguments with {@code nil} values as required. 35 | *

36 | * If more or less than two arguments are required, 37 | * or variable argument or variable return values, 38 | * then use one of the related function 39 | * {@link ZeroArgFunction}, {@link OneArgFunction}, {@link ThreeArgFunction}, or {@link VarArgFunction}. 40 | *

41 | * See {@link LibFunction} for more information on implementation libraries and library functions. 42 | * @see #call(LuaValue,LuaValue) 43 | * @see LibFunction 44 | * @see ZeroArgFunction 45 | * @see OneArgFunction 46 | * @see ThreeArgFunction 47 | * @see VarArgFunction 48 | */ 49 | abstract public class TwoArgFunction extends LibFunction { 50 | 51 | abstract public LuaValue call(LuaValue arg1, LuaValue arg2); 52 | 53 | /** Default constructor */ 54 | public TwoArgFunction() { 55 | } 56 | 57 | /** Constructor with specific environment 58 | * @param env The environment to apply during constructon. 59 | */ 60 | public TwoArgFunction( LuaValue env ) { 61 | this.env = env; 62 | } 63 | 64 | public final LuaValue call() { 65 | return call(NIL, NIL); 66 | } 67 | 68 | public final LuaValue call(LuaValue arg) { 69 | return call(arg, NIL); 70 | } 71 | 72 | public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) { 73 | return call(arg1, arg2); 74 | } 75 | 76 | public Varargs invoke(Varargs varargs) { 77 | return call(varargs.arg1(),varargs.arg(2)); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/lib/ThreeArgFunction.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.lib; 23 | 24 | import org.luaj.vm2.LuaValue; 25 | import org.luaj.vm2.Varargs; 26 | 27 | /** Abstract base class for Java function implementations that take two arguments and 28 | * return one value. 29 | *

30 | * Subclasses need only implement {@link LuaValue#call(LuaValue,LuaValue,LuaValue)} to complete this class, 31 | * simplifying development. 32 | * All other uses of {@link #call()}, {@link #invoke(Varargs)},etc, 33 | * are routed through this method by this class, 34 | * dropping or extending arguments with {@code nil} values as required. 35 | *

36 | * If more or less than three arguments are required, 37 | * or variable argument or variable return values, 38 | * then use one of the related function 39 | * {@link ZeroArgFunction}, {@link OneArgFunction}, {@link TwoArgFunction}, or {@link VarArgFunction}. 40 | *

41 | * See {@link LibFunction} for more information on implementation libraries and library functions. 42 | * @see #call(LuaValue,LuaValue,LuaValue) 43 | * @see LibFunction 44 | * @see ZeroArgFunction 45 | * @see OneArgFunction 46 | * @see TwoArgFunction 47 | * @see VarArgFunction 48 | */ 49 | abstract public class ThreeArgFunction extends LibFunction { 50 | 51 | abstract public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3); 52 | 53 | /** Default constructor */ 54 | public ThreeArgFunction() { 55 | } 56 | 57 | /** Constructor with specific environment 58 | * @param env The environment to apply during constructon. 59 | */ 60 | public ThreeArgFunction( LuaValue env ) { 61 | this.env = env; 62 | } 63 | 64 | public final LuaValue call() { 65 | return call(NIL, NIL, NIL); 66 | } 67 | 68 | public final LuaValue call(LuaValue arg) { 69 | return call(arg, NIL, NIL); 70 | } 71 | 72 | public LuaValue call(LuaValue arg1, LuaValue arg2) { 73 | return call(arg1, arg2, NIL); 74 | } 75 | 76 | public Varargs invoke(Varargs varargs) { 77 | return call(varargs.arg1(),varargs.arg(2),varargs.arg(3)); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/TailcallVarargs.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2010-2011 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2; 23 | 24 | /** 25 | * Subclass of {@link Varargs} that represents a lua tail call 26 | * in a Java library function execution environment. 27 | *

28 | * Since Java doesn't have direct support for tail calls, 29 | * any lua function whose {@link Prototype} contains the 30 | * {@link Lua#OP_TAILCALL} bytecode needs a mechanism 31 | * for tail calls when converting lua-bytecode to java-bytecode. 32 | *

33 | * The tail call holds the next function and arguments, 34 | * and the client a call to {@link #eval()} executes the function 35 | * repeatedly until the tail calls are completed. 36 | *

37 | * Normally, users of luaj need not concern themselves with the 38 | * details of this mechanism, as it is built into the core 39 | * execution framework. 40 | * @see Prototype 41 | * @see LuaJC 42 | */ 43 | public class TailcallVarargs extends Varargs { 44 | 45 | private LuaValue func; 46 | private Varargs args; 47 | private Varargs result; 48 | 49 | public TailcallVarargs(LuaValue f, Varargs args) { 50 | this.func = f; 51 | this.args = args; 52 | } 53 | 54 | public TailcallVarargs(LuaValue object, LuaValue methodname, Varargs args) { 55 | this.func = object.get(methodname); 56 | this.args = LuaValue.varargsOf(object, args); 57 | } 58 | 59 | public boolean isTailcall() { 60 | return true; 61 | } 62 | 63 | public Varargs eval() { 64 | while ( result == null ) { 65 | Varargs r = func.onInvoke(args); 66 | if (r.isTailcall()) { 67 | TailcallVarargs t = (TailcallVarargs) r; 68 | func = t.func; 69 | args = t.args; 70 | } 71 | else { 72 | result = r; 73 | func = null; 74 | args = null; 75 | } 76 | } 77 | return result; 78 | } 79 | 80 | public LuaValue arg( int i ) { 81 | if ( result == null ) 82 | eval(); 83 | return result.arg(i); 84 | } 85 | 86 | public LuaValue arg1() { 87 | if (result == null) 88 | eval(); 89 | return result.arg1(); 90 | } 91 | 92 | public int narg() { 93 | if (result == null) 94 | eval(); 95 | return result.narg(); 96 | } 97 | } -------------------------------------------------------------------------------- /test/lua/tailcalls.lua: -------------------------------------------------------------------------------- 1 | 2 | -- tostring replacement that assigns ids 3 | local ts,id,nid,types = tostring,{},0,{table='tbl',thread='thr',userdata='uda',['function']='func'} 4 | tostring = function(x) 5 | if not x or not types[type(x)] then return ts(x) end 6 | if not id[x] then nid=nid+1; id[x]=types[type(x)]..'.'..nid end 7 | return id[x] 8 | end 9 | 10 | 11 | function a() 12 | return pcall( function() end ) 13 | end 14 | 15 | function b() 16 | return pcall( function() print 'b' end ) 17 | end 18 | 19 | function c() 20 | return pcall( function() return 'c' end ) 21 | end 22 | 23 | print( pcall( a ) ) 24 | print( pcall( b ) ) 25 | print( pcall( c ) ) 26 | 27 | local function sum(...) 28 | local s = 0 29 | for i,v in ipairs({...}) do 30 | s = s + v 31 | end 32 | return s 33 | end 34 | 35 | local function f1(n,a,b,c) 36 | local a = a or 0 37 | local b = b or 0 38 | local c = c or 0 39 | if n <= 0 then 40 | return a,a+b,a+b+c 41 | end 42 | return f1(n-1,a,a+b,a+b+c) 43 | end 44 | 45 | local function f2(n,...) 46 | if n <= 0 then 47 | print( " --f2, n<=0, returning sum(...)", ... ) 48 | return sum(...) 49 | end 50 | print( " --f2, n>0, returning f2(n-1,n,...)", n-1,n,... ) 51 | return f2(n-1,n,...) 52 | end 53 | 54 | local function f3(n,...) 55 | if n <= 0 then 56 | return sum(...) 57 | end 58 | print( " f3,n-1,n,...", f3,n-1,n,... ) 59 | return pcall(f3,n-1,n,...) 60 | end 61 | 62 | local function all(f) 63 | for n=0,3 do 64 | t = {} 65 | for m=1,5 do 66 | print( "--f, n, unpack(t)", f, n, unpack(t) ) 67 | print( pcall( f, n, unpack(t)) ) 68 | t[m] = m 69 | end 70 | end 71 | end 72 | 73 | all(f1) 74 | all(f2) 75 | all(f3) 76 | 77 | 78 | local function f(x) 79 | -- tailcall to a builtin 80 | return math.abs(x) 81 | end 82 | 83 | local function factorial(i) 84 | local function helper(product, n) 85 | if n <= 0 then 86 | return product 87 | else 88 | -- tail call to a nested Lua function 89 | return helper(n * product, n - 1) 90 | end 91 | end 92 | return helper(1, i) 93 | end 94 | 95 | local result1 = factorial(5) 96 | print(result1) 97 | print(factorial(5)) 98 | 99 | local result2 = f(-1234) 100 | print( result2 ) 101 | 102 | local function fib_bad(n) 103 | local function helper(i, a, b) 104 | if i >= n then 105 | return a 106 | else 107 | -- not recognized by luac as a tailcall! 108 | local result = helper(i + 1, b, a + b) 109 | return result 110 | end 111 | end 112 | return helper(1, 1, 1) 113 | end 114 | 115 | local function fib_good(n) 116 | local function helper(i, a, b) 117 | if i >= n then 118 | return a 119 | else 120 | -- must be a tail call! 121 | return helper(i + 1, b, a + b) 122 | end 123 | end 124 | return helper(1, 1, 1) 125 | end 126 | 127 | local aliases = { 128 | ['1.#INF'] = 'inf', 129 | ['-1.#INF'] = '-inf', 130 | ['1.#IND'] = 'nan', 131 | ['-1.#IND'] = 'nan', 132 | } 133 | 134 | local p = function( s,e ) 135 | print( s, e and aliases[tostring(e)] or e ) 136 | end 137 | p(pcall(fib_bad, 30)) 138 | --p((pcall(fib_bad, 25000))) 139 | p(pcall(fib_good, 30)) 140 | p(pcall(fib_good, 25000)) 141 | 142 | local function fib_all(n, i, a, b) 143 | i = i or 1 144 | a = a or 1 145 | b = b or 1 146 | if i >= n then 147 | return 148 | else 149 | return a, fib_all(n, i+1, b, a+b) 150 | end 151 | end 152 | 153 | print(fib_all(10)) 154 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/NameScope.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2010 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.ast; 23 | 24 | import java.util.HashMap; 25 | import java.util.HashSet; 26 | import java.util.Map; 27 | import java.util.Set; 28 | 29 | 30 | public class NameScope { 31 | 32 | private static final Set LUA_KEYWORDS = new HashSet(); 33 | 34 | static { 35 | String[] k = new String[] { 36 | "and", "break", "do", "else", "elseif", "end", 37 | "false", "for", "function", "if", "in", "local", 38 | "nil", "not", "or", "repeat", "return", 39 | "then", "true", "until", "while" }; 40 | for ( int i=0; i namedVariables = new HashMap(); 45 | 46 | public final NameScope outerScope; 47 | 48 | public int functionNestingCount; 49 | 50 | /** Construct default names scope */ 51 | public NameScope() { 52 | this.outerScope = null; 53 | this.functionNestingCount = 0; 54 | } 55 | 56 | /** Construct name scope within another scope*/ 57 | public NameScope(NameScope outerScope) { 58 | this.outerScope = outerScope; 59 | this.functionNestingCount = outerScope!=null? outerScope.functionNestingCount: 0; 60 | } 61 | 62 | /** Look up a name. If it is a global name, then throw IllegalArgumentException. */ 63 | public Variable find( String name ) throws IllegalArgumentException { 64 | validateIsNotKeyword(name); 65 | for ( NameScope n = this; n!=null; n=n.outerScope ) 66 | if ( n.namedVariables.containsKey(name) ) 67 | return (Variable)n.namedVariables.get(name); 68 | Variable value = new Variable(name); 69 | this.namedVariables.put(name, value); 70 | return value; 71 | } 72 | 73 | /** Define a name in this scope. If it is a global name, then throw IllegalArgumentException. */ 74 | public Variable define( String name ) throws IllegalStateException, IllegalArgumentException { 75 | validateIsNotKeyword(name); 76 | Variable value = new Variable(name, this); 77 | this.namedVariables.put(name, value); 78 | return value; 79 | } 80 | 81 | private void validateIsNotKeyword(String name) { 82 | if ( LUA_KEYWORDS.contains(name) ) 83 | throw new IllegalArgumentException("name is a keyword: '"+name+"'"); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/LuaBoolean.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009-2011 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2; 23 | 24 | /** 25 | * Extension of {@link LuaValue} which can hold a Java boolean as its value. 26 | *

27 | * These instance are not instantiated directly by clients. 28 | * Instead, there are exactly twon instances of this class, 29 | * {@link LuaValue#TRUE} and {@link LuaValue#FALSE} 30 | * representing the lua values {@code true} and {@link false}. 31 | * The function {@link LuaValue#valueOf(boolean)} will always 32 | * return one of these two values. 33 | *

34 | * Any {@link LuaValue} can be converted to its equivalent 35 | * boolean representation using {@link LuaValue#toboolean()} 36 | *

37 | * @see LuaValue 38 | * @see LuaValue#valueOf(boolean) 39 | * @see LuaValue#TRUE 40 | * @see LuaValue#FALSE 41 | */ 42 | public final class LuaBoolean extends LuaValue { 43 | 44 | /** The singleton instance representing lua {@code true} */ 45 | static final LuaBoolean _TRUE = new LuaBoolean(true); 46 | 47 | /** The singleton instance representing lua {@code false} */ 48 | static final LuaBoolean _FALSE = new LuaBoolean(false); 49 | 50 | /** Shared static metatable for boolean values represented in lua. */ 51 | public static LuaValue s_metatable; 52 | 53 | /** The value of the boolean */ 54 | public final boolean v; 55 | 56 | LuaBoolean(boolean b) { 57 | this.v = b; 58 | } 59 | 60 | public int type() { 61 | return LuaValue.TBOOLEAN; 62 | } 63 | 64 | public String typename() { 65 | return "boolean"; 66 | } 67 | 68 | public boolean isboolean() { 69 | return true; 70 | } 71 | 72 | public LuaValue not() { 73 | return v ? FALSE : LuaValue.TRUE; 74 | } 75 | 76 | /** 77 | * Return the boolean value for this boolean 78 | * @return value as a Java boolean 79 | */ 80 | public boolean booleanValue() { 81 | return v; 82 | } 83 | 84 | public boolean toboolean() { 85 | return v; 86 | } 87 | 88 | public String tojstring() { 89 | return v ? "true" : "false"; 90 | } 91 | 92 | public boolean optboolean(boolean defval) { 93 | return this.v; 94 | } 95 | 96 | public boolean checkboolean() { 97 | return v; 98 | } 99 | 100 | public LuaValue getmetatable() { 101 | return s_metatable; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/AllTests.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2; 23 | 24 | import junit.framework.Test; 25 | import junit.framework.TestSuite; 26 | 27 | import org.luaj.vm2.WeakTableTest.WeakKeyTableTest; 28 | import org.luaj.vm2.WeakTableTest.WeakKeyValueTableTest; 29 | import org.luaj.vm2.WeakTableTest.WeakValueTableTest; 30 | import org.luaj.vm2.compiler.CompilerUnitTests; 31 | import org.luaj.vm2.compiler.DumpLoadEndianIntTest; 32 | import org.luaj.vm2.compiler.RegressionTests; 33 | import org.luaj.vm2.compiler.SimpleTests; 34 | import org.luaj.vm2.lib.jse.LuaJavaCoercionTest; 35 | import org.luaj.vm2.vm1.Luajvm1CompatibilityTest; 36 | 37 | public class AllTests { 38 | 39 | public static Test suite() { 40 | TestSuite suite = new TestSuite("All Tests for Luaj-vm2"); 41 | 42 | // vm tests 43 | TestSuite vm = new TestSuite("VM Tests"); 44 | vm.addTestSuite(TypeTest.class); 45 | vm.addTestSuite(UnaryBinaryOperatorsTest.class); 46 | vm.addTestSuite(MetatableTest.class); 47 | vm.addTestSuite(LuaOperationsTest.class); 48 | vm.addTestSuite(StringTest.class); 49 | suite.addTest(vm); 50 | 51 | // table tests 52 | TestSuite table = new TestSuite("Table Tests"); 53 | table.addTestSuite(TableTest.class); 54 | table.addTestSuite(TableArrayTest.class); 55 | table.addTestSuite(TableHashTest.class); 56 | table.addTestSuite(WeakValueTableTest.class); 57 | table.addTestSuite(WeakKeyTableTest.class); 58 | table.addTestSuite(WeakKeyValueTableTest.class); 59 | suite.addTest(table); 60 | 61 | // bytecode compilers regression tests 62 | TestSuite bytecodetests = FragmentsTest.suite(); 63 | suite.addTest(bytecodetests); 64 | 65 | // prototype compiler 66 | TestSuite compiler = new TestSuite("Lua Compiler Tests"); 67 | compiler.addTestSuite(CompilerUnitTests.class); 68 | compiler.addTestSuite(DumpLoadEndianIntTest.class); 69 | compiler.addTestSuite(RegressionTests.class); 70 | compiler.addTestSuite(SimpleTests.class); 71 | suite.addTest(compiler); 72 | 73 | // library tests 74 | TestSuite lib = new TestSuite("Library Tests"); 75 | lib.addTestSuite(LuaJavaCoercionTest.class); 76 | lib.addTestSuite(RequireClassTest.class); 77 | suite.addTest(lib); 78 | 79 | // compatiblity tests 80 | TestSuite compat = CompatibiltyTest.suite(); 81 | suite.addTest( compat ); 82 | compat.addTestSuite(ErrorsTest.class); 83 | compat.addTestSuite(Luajvm1CompatibilityTest.class); 84 | 85 | return suite; 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/Str.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2010 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.ast; 23 | 24 | import java.io.ByteArrayOutputStream; 25 | import java.io.UnsupportedEncodingException; 26 | 27 | import org.luaj.vm2.LuaString; 28 | 29 | public class Str { 30 | 31 | private Str() {} 32 | 33 | public static LuaString quoteString(String image) { 34 | String s = image.substring(1, image.length()-1); 35 | byte[] bytes = unquote(s); 36 | return LuaString.valueOf(bytes); 37 | } 38 | 39 | public static LuaString charString(String image) { 40 | String s = image.substring(1, image.length()-1); 41 | byte[] bytes = unquote(s); 42 | return LuaString.valueOf(bytes); 43 | } 44 | 45 | public static LuaString longString(String image) { 46 | int i = image.indexOf('[', image.indexOf('[')+1)+1; 47 | String s = image.substring(i,image.length()-i); 48 | byte[] b = iso88591bytes(s); 49 | return LuaString.valueOf(b); 50 | } 51 | 52 | public static byte[] iso88591bytes( String s ) { 53 | try { 54 | return s.getBytes("ISO8859-1"); 55 | } catch (UnsupportedEncodingException e) { 56 | throw new IllegalStateException("ISO8859-1 not supported"); 57 | } 58 | } 59 | 60 | public static byte[] unquote(String s) { 61 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 62 | char[] c = s.toCharArray(); 63 | int n = c.length; 64 | for ( int i=0; i='0' && c[i]<='9'; i++, j++ ) 71 | d = d * 10 + (int) (c[i]-'0'); 72 | baos.write( (byte) d ); 73 | --i; 74 | continue; 75 | case 'a': baos.write( (byte) 7 ); continue; 76 | case 'b': baos.write( (byte) '\b' ); continue; 77 | case 'f': baos.write( (byte) '\f' ); continue; 78 | case 'n': baos.write( (byte) '\n' ); continue; 79 | case 'r': baos.write( (byte) '\r' ); continue; 80 | case 't': baos.write( (byte) '\t' ); continue; 81 | case 'v': baos.write( (byte) 11 ); continue; 82 | case '"': baos.write( (byte) '"' ); continue; 83 | case '\'': baos.write( (byte) '\'' ); continue; 84 | case '\\': baos.write( (byte) '\\' ); continue; 85 | default: baos.write( (byte) c[i] ); break; 86 | } 87 | } else { 88 | baos.write( (byte) c[i] ); 89 | } 90 | } 91 | return baos.toByteArray(); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /test/junit/org/luaj/vm2/StringTest.java: -------------------------------------------------------------------------------- 1 | package org.luaj.vm2; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.UnsupportedEncodingException; 6 | 7 | import junit.framework.TestCase; 8 | 9 | import org.luaj.vm2.lib.jse.JsePlatform; 10 | 11 | public class StringTest extends TestCase { 12 | 13 | protected void setUp() throws Exception { 14 | JsePlatform.standardGlobals(); 15 | } 16 | 17 | public void testToInputStream() throws IOException { 18 | LuaString str = LuaString.valueOf("Hello"); 19 | 20 | InputStream is = str.toInputStream(); 21 | 22 | assertEquals( 'H', is.read() ); 23 | assertEquals( 'e', is.read() ); 24 | assertEquals( 2, is.skip( 2 ) ); 25 | assertEquals( 'o', is.read() ); 26 | assertEquals( -1, is.read() ); 27 | 28 | assertTrue( is.markSupported() ); 29 | 30 | is.reset(); 31 | 32 | assertEquals( 'H', is.read() ); 33 | is.mark( 4 ); 34 | 35 | assertEquals( 'e', is.read() ); 36 | is.reset(); 37 | assertEquals( 'e', is.read() ); 38 | 39 | LuaString substr = str.substring( 1, 4 ); 40 | assertEquals( 3, substr.length() ); 41 | 42 | is.close(); 43 | is = substr.toInputStream(); 44 | 45 | assertEquals( 'e', is.read() ); 46 | assertEquals( 'l', is.read() ); 47 | assertEquals( 'l', is.read() ); 48 | assertEquals( -1, is.read() ); 49 | 50 | is = substr.toInputStream(); 51 | is.reset(); 52 | 53 | assertEquals( 'e', is.read() ); 54 | } 55 | 56 | 57 | private static final String userFriendly( String s ) { 58 | StringBuffer sb = new StringBuffer(); 59 | for ( int i=0, n=s.length(); i= 0x80 ) { 62 | sb.append( "\\u"+Integer.toHexString(0x10000+c).substring(1) ); 63 | } else { 64 | sb.append( (char) c ); 65 | } 66 | } 67 | return sb.toString(); 68 | } 69 | 70 | public void testUtf820482051() throws UnsupportedEncodingException { 71 | int i = 2048; 72 | char[] c = { (char) (i+0), (char) (i+1), (char) (i+2), (char) (i+3) }; 73 | String before = new String(c)+" "+i+"-"+(i+4); 74 | LuaString ls = LuaString.valueOf(before); 75 | String after = ls.tojstring(); 76 | assertEquals( userFriendly( before ), userFriendly( after ) ); 77 | 78 | } 79 | 80 | public void testUtf8() { 81 | for ( int i=4; i<0xffff; i+=4 ) { 82 | char[] c = { (char) (i+0), (char) (i+1), (char) (i+2), (char) (i+3) }; 83 | String before = new String(c)+" "+i+"-"+(i+4); 84 | LuaString ls = LuaString.valueOf(before); 85 | String after = ls.tojstring(); 86 | assertEquals( userFriendly( before ), userFriendly( after ) ); 87 | } 88 | char[] c = { (char) (1), (char) (2), (char) (3) }; 89 | String before = new String(c)+" 1-3"; 90 | LuaString ls = LuaString.valueOf(before); 91 | String after = ls.tojstring(); 92 | assertEquals( userFriendly( before ), userFriendly( after ) ); 93 | 94 | } 95 | 96 | public void testSpotCheckUtf8() throws UnsupportedEncodingException { 97 | byte[] bytes = {(byte)194,(byte)160,(byte)194,(byte)161,(byte)194,(byte)162,(byte)194,(byte)163,(byte)194,(byte)164}; 98 | String expected = new String(bytes, "UTF8"); 99 | String actual = LuaString.valueOf(bytes).tojstring(); 100 | char[] d = actual.toCharArray(); 101 | assertEquals(160, d[0]); 102 | assertEquals(161, d[1]); 103 | assertEquals(162, d[2]); 104 | assertEquals(163, d[3]); 105 | assertEquals(164, d[4]); 106 | 107 | } 108 | 109 | public void testNullTerminated() { 110 | char[] c = { 'a', 'b', 'c', '\0', 'd', 'e', 'f' }; 111 | String before = new String(c); 112 | LuaString ls = LuaString.valueOf(before); 113 | String after = ls.tojstring(); 114 | assertEquals( userFriendly( "abc\0def" ), userFriendly( after ) ); 115 | 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/ast/NameResolver.java: -------------------------------------------------------------------------------- 1 | package org.luaj.vm2.ast; 2 | 3 | import java.util.List; 4 | 5 | import org.luaj.vm2.LuaValue; 6 | import org.luaj.vm2.ast.Exp.Constant; 7 | import org.luaj.vm2.ast.Exp.NameExp; 8 | import org.luaj.vm2.ast.Exp.VarExp; 9 | import org.luaj.vm2.ast.Stat.Assign; 10 | import org.luaj.vm2.ast.Stat.FuncDef; 11 | import org.luaj.vm2.ast.Stat.GenericFor; 12 | import org.luaj.vm2.ast.Stat.LocalAssign; 13 | import org.luaj.vm2.ast.Stat.LocalFuncDef; 14 | import org.luaj.vm2.ast.Stat.NumericFor; 15 | 16 | /** 17 | * Visitor that resolves names to scopes. 18 | * Each Name is resolved to a NamedVarible, possibly in a NameScope 19 | * if it is a local, or in no named scope if it is a global. 20 | */ 21 | public class NameResolver extends Visitor { 22 | 23 | private NameScope scope = null; 24 | 25 | private void pushScope() { 26 | scope = new NameScope(scope); 27 | } 28 | private void popScope() { 29 | scope = scope.outerScope; 30 | } 31 | 32 | public void visit(NameScope scope) { 33 | } 34 | 35 | public void visit(Block block) { 36 | pushScope(); 37 | block.scope = scope; 38 | super.visit(block); 39 | popScope(); 40 | } 41 | 42 | public void visit(FuncBody body) { 43 | pushScope(); 44 | scope.functionNestingCount++; 45 | body.scope = scope; 46 | super.visit(body); 47 | popScope(); 48 | } 49 | 50 | public void visit(LocalFuncDef stat) { 51 | defineLocalVar(stat.name); 52 | super.visit(stat); 53 | } 54 | 55 | public void visit(NumericFor stat) { 56 | pushScope(); 57 | stat.scope = scope; 58 | defineLocalVar(stat.name); 59 | super.visit(stat); 60 | popScope(); 61 | } 62 | 63 | public void visit(GenericFor stat) { 64 | pushScope(); 65 | stat.scope = scope; 66 | defineLocalVars( stat.names ); 67 | super.visit(stat); 68 | popScope(); 69 | } 70 | 71 | public void visit(NameExp exp) { 72 | exp.name.variable = resolveNameReference(exp.name); 73 | super.visit(exp); 74 | } 75 | 76 | public void visit(FuncDef stat) { 77 | stat.name.name.variable = resolveNameReference(stat.name.name); 78 | stat.name.name.variable.hasassignments = true; 79 | super.visit(stat); 80 | } 81 | 82 | public void visit(Assign stat) { 83 | super.visit(stat); 84 | for ( int i=0, n=stat.vars.size(); i0 && m names) { 113 | for ( int i=0, n=names.size(); i= 0) 92 | baos.write(buffer, 0, n); 93 | is.close(); 94 | return baos.toByteArray(); 95 | } 96 | 97 | protected Prototype loadFromBytes(byte[] bytes, String script) 98 | throws IOException { 99 | InputStream is = new ByteArrayInputStream(bytes); 100 | return LoadState.loadBinaryChunk(is.read(), is, script); 101 | } 102 | 103 | protected String protoToString(Prototype p) { 104 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 105 | PrintStream ps = new PrintStream(baos); 106 | Print.ps = ps; 107 | new Print().printFunction(p, true); 108 | return baos.toString(); 109 | } 110 | 111 | } 112 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/luajc/VarInfo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package org.luaj.vm2.luajc; 5 | 6 | import java.util.HashSet; 7 | import java.util.Iterator; 8 | import java.util.Set; 9 | 10 | public class VarInfo { 11 | 12 | public static VarInfo INVALID = new VarInfo(-1, -1); 13 | 14 | public static VarInfo PARAM(int slot) { 15 | return new VarInfo(slot, -1) { 16 | public String toString() { 17 | return slot + ".p"; 18 | } 19 | }; 20 | } 21 | 22 | public static VarInfo NIL(final int slot) { 23 | return new VarInfo(slot, -1) { 24 | public String toString() { 25 | return "nil"; 26 | } 27 | }; 28 | } 29 | 30 | public static VarInfo PHI(final ProtoInfo pi, final int slot, final int pc) { 31 | return new PhiVarInfo(pi, slot, pc); 32 | } 33 | 34 | public final int slot; // where assigned 35 | public final int pc; // where assigned, or -1 if for block inputs 36 | 37 | public UpvalInfo upvalue; // not null if this var is an upvalue 38 | public boolean allocupvalue; // true if this variable allocates r/w upvalue 39 | // storage 40 | public boolean isreferenced; // true if this variable is refenced by some 41 | // opcode 42 | 43 | public VarInfo(int slot, int pc) { 44 | this.slot = slot; 45 | this.pc = pc; 46 | } 47 | 48 | public String toString() { 49 | return slot < 0 ? "x.x" : (slot + "." + pc); 50 | } 51 | 52 | /** Return replacement variable if there is exactly one value possible, 53 | * otherwise compute entire collection of variables and return null. 54 | * Computes the list of aall variable values, and saves it for the future. 55 | * 56 | * @return new Variable to replace with if there is only one value, or null to leave alone. 57 | */ 58 | public VarInfo resolvePhiVariableValues() { 59 | return null; 60 | } 61 | 62 | protected void collectUniqueValues(Set visitedBlocks, Set vars) { 63 | vars.add(this); 64 | } 65 | 66 | public boolean isPhiVar() { 67 | return false; 68 | } 69 | 70 | private static final class PhiVarInfo extends VarInfo { 71 | private final ProtoInfo pi; 72 | VarInfo[] values; 73 | 74 | private PhiVarInfo(ProtoInfo pi, int slot, int pc) { 75 | super(slot, pc); 76 | this.pi = pi; 77 | } 78 | 79 | public boolean isPhiVar() { 80 | return true; 81 | } 82 | 83 | public String toString() { 84 | StringBuffer sb = new StringBuffer(); 85 | sb.append( super.toString() ); 86 | sb.append("={"); 87 | for (int i=0, n=(values!=null? values.length : 0); i0 ) 89 | sb.append( "," ); 90 | sb.append(String.valueOf(values[i])); 91 | } 92 | sb.append("}"); 93 | return sb.toString(); 94 | } 95 | 96 | public VarInfo resolvePhiVariableValues() { 97 | Set visitedBlocks = new HashSet(); 98 | Set vars = new HashSet(); 99 | this.collectUniqueValues(visitedBlocks, vars); 100 | if (vars.contains(INVALID)) 101 | return INVALID; 102 | int n = vars.size(); 103 | Iterator it = vars.iterator(); 104 | if (n == 1) { 105 | VarInfo v = (VarInfo) it.next(); 106 | v.isreferenced |= this.isreferenced; 107 | return v; 108 | } 109 | this.values = new VarInfo[n]; 110 | for ( int i=0; i 31 | * Subclasses need only implement {@link LuaValue#invoke(Varargs)} to complete this class, 32 | * simplifying development. 33 | * All other uses of {@link #call(LuaValue)}, {@link #invoke()},etc, 34 | * are routed through this method by this class, 35 | * converting arguments to {@linnk Varargs} and 36 | * dropping or extending return values with {@code nil} values as required. 37 | *

38 | * If between one and three arguments are required, and only one return value is returned, 39 | * {@link ZeroArgFunction}, {@link OneArgFunction}, {@link TwoArgFunction}, or {@link ThreeArgFunction}. 40 | *

41 | * See {@link LibFunction} for more information on implementation libraries and library functions. 42 | * @see #invoke(Varargs) 43 | * @see LibFunction 44 | * @see ZeroArgFunction 45 | * @see OneArgFunction 46 | * @see TwoArgFunction 47 | * @see ThreeArgFunction 48 | */ 49 | abstract public class VarArgFunction extends LibFunction { 50 | public VarArgFunction() { 51 | } 52 | 53 | public VarArgFunction( LuaValue env ) { 54 | this.env = env; 55 | } 56 | 57 | public LuaValue call() { 58 | return invoke(NONE).arg1(); 59 | } 60 | 61 | public LuaValue call(LuaValue arg) { 62 | return invoke(arg).arg1(); 63 | } 64 | 65 | public LuaValue call(LuaValue arg1, LuaValue arg2) { 66 | return invoke(varargsOf(arg1,arg2)).arg1(); 67 | } 68 | 69 | public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) { 70 | return invoke(varargsOf(arg1,arg2,arg3)).arg1(); 71 | } 72 | 73 | /** 74 | * Override and implement for the best performance. 75 | * May not have expected behavior for tail calls. 76 | * Should not be used if either: 77 | * - function needs to be used as a module 78 | * - function has a possibility of returning a TailcallVarargs 79 | * @param args the arguments to the function call. 80 | */ 81 | public Varargs invoke(Varargs args) { 82 | LuaThread.onCall(this); 83 | try { 84 | return this.onInvoke(args).eval(); 85 | } finally { 86 | LuaThread.onReturn(); 87 | } 88 | } 89 | 90 | /** 91 | * Override to provide a call implementation that runs in an environment 92 | * that can participate in setfenv, and behaves as expected 93 | * when returning TailcallVarargs. 94 | * @param args the arguments to the function call. 95 | */ 96 | public Varargs onInvoke(Varargs args) { 97 | return invoke(args); 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /test/java/org/luaj/luajc/TestLuaJC.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.luajc; 23 | 24 | import java.io.ByteArrayInputStream; 25 | import java.io.FileOutputStream; 26 | import java.io.InputStream; 27 | import java.util.Enumeration; 28 | import java.util.Hashtable; 29 | 30 | import org.luaj.vm2.LuaTable; 31 | import org.luaj.vm2.LuaValue; 32 | import org.luaj.vm2.Varargs; 33 | import org.luaj.vm2.lib.jse.JsePlatform; 34 | import org.luaj.vm2.luajc.LuaJC; 35 | 36 | public class TestLuaJC { 37 | // create the script 38 | public static String name = "script"; 39 | public static String script = 40 | "local t = a or nil\n"+ 41 | "local t\n" + 42 | "b = function()\n"+ 43 | " return t\n"+ 44 | "end\n"+ 45 | "return t\n"+ 46 | ""; 47 | 48 | public static void main(String[] args) throws Exception { 49 | System.out.println(script); 50 | try { 51 | 52 | // create an environment to run in 53 | LuaTable _G = JsePlatform.standardGlobals(); 54 | 55 | // compile into a chunk, or load as a class 56 | LuaValue chunk; 57 | if ( ! (args.length>0 && args[0].equals("nocompile")) ) { 58 | InputStream is = new ByteArrayInputStream( script.getBytes() ); 59 | chunk = LuaJC.getInstance().load(is, "script", _G); 60 | } else { 61 | chunk = (LuaValue) Class.forName("script").newInstance(); 62 | } 63 | chunk.setfenv(_G); 64 | 65 | // call with arguments 66 | LuaValue[] vargs = new LuaValue[args.length]; 67 | for ( int i=0; i4 and (s:sub(1,5)..'...') or s 8 | end 9 | 10 | -- arg type tests for math library functions 11 | local somenumber = {1,0.75,'-1','-0.25'} 12 | local somepositive = {1,0.75,'2', '2.5'} 13 | local notanumber = {nil,astring,aboolean,afunction,atable,athread} 14 | local nonnumber = {astring,aboolean,afunction,atable} 15 | 16 | local singleargfunctions = { 17 | 'abs', 'acos', 'asin', 'atan', 'cos', 'cosh', 'deg', 'exp', 'floor', 18 | 'rad', 'randomseed', 'sin', 'sinh', 'tan', 'tanh', 'frexp', 19 | } 20 | 21 | local singleargposdomain = { 22 | 'log', 'log10', 'sqrt', 'ceil', 23 | } 24 | 25 | local twoargfunctions = { 26 | 'atan2', 27 | } 28 | 29 | local twoargsposdomain = { 30 | 'pow', 'fmod', 31 | } 32 | 33 | -- single argument tests 34 | for i,v in ipairs(singleargfunctions) do 35 | local funcname = 'math.'..v 36 | banner(funcname) 37 | checkallpass(funcname,{somenumber}) 38 | checkallerrors(funcname,{notanumber},'bad argument') 39 | end 40 | 41 | -- single argument, positive domain tests 42 | for i,v in ipairs(singleargposdomain) do 43 | local funcname = 'math.'..v 44 | banner(funcname) 45 | checkallpass(funcname,{somepositive}) 46 | checkallerrors(funcname,{notanumber},'bad argument') 47 | end 48 | 49 | -- two-argument tests 50 | for i,v in ipairs(twoargfunctions) do 51 | local funcname = 'math.'..v 52 | banner(funcname) 53 | checkallpass(funcname,{somenumber,somenumber}) 54 | checkallerrors(funcname,{},'bad argument') 55 | checkallerrors(funcname,{notanumber},'bad argument') 56 | checkallerrors(funcname,{notanumber,somenumber},'bad argument') 57 | checkallerrors(funcname,{somenumber},'bad argument') 58 | checkallerrors(funcname,{somenumber,notanumber},'bad argument') 59 | end 60 | 61 | -- two-argument, positive domain tests 62 | for i,v in ipairs(twoargsposdomain) do 63 | local funcname = 'math.'..v 64 | banner(funcname) 65 | checkallpass(funcname,{somepositive,somenumber}) 66 | checkallerrors(funcname,{},'bad argument') 67 | checkallerrors(funcname,{notanumber},'bad argument') 68 | checkallerrors(funcname,{notanumber,somenumber},'bad argument') 69 | checkallerrors(funcname,{somenumber},'bad argument') 70 | checkallerrors(funcname,{somenumber,notanumber},'bad argument') 71 | end 72 | 73 | -- math.max 74 | banner('math.max') 75 | checkallpass('math.max',{somenumber}) 76 | checkallpass('math.max',{somenumber,somenumber}) 77 | checkallerrors('math.max',{},'bad argument') 78 | checkallerrors('math.max',{nonnumber},'bad argument') 79 | checkallerrors('math.max',{somenumber,nonnumber},'bad argument') 80 | 81 | -- math.min 82 | banner('math.min') 83 | checkallpass('math.min',{somenumber}) 84 | checkallpass('math.min',{somenumber,somenumber}) 85 | checkallerrors('math.min',{},'bad argument') 86 | checkallerrors('math.min',{nonnumber},'bad argument') 87 | checkallerrors('math.min',{somenumber,nonnumber},'bad argument') 88 | 89 | -- math.random 90 | local somem = {3,4.5,'6.7'} 91 | local somen = {8,9.10,'12.34'} 92 | local notamn = {astring,aboolean,atable,afunction} 93 | banner('math.random') 94 | checkallpass('math.random',{},true) 95 | checkallpass('math.random',{somem},true) 96 | checkallpass('math.random',{somem,somen},true) 97 | checkallpass('math.random',{{-4,-5.6,'-7','-8.9'},{-1,100,23.45,'-1.23'}},true) 98 | checkallerrors('math.random',{{-4,-5.6,'-7','-8.9'}},'interval is empty') 99 | checkallerrors('math.random',{somen,somem},'interval is empty') 100 | checkallerrors('math.random',{notamn,somen},'bad argument') 101 | checkallerrors('math.random',{somem,notamn},'bad argument') 102 | 103 | -- math.ldexp 104 | local somee = {-3,0,3,9.10,'12.34'} 105 | local notae = {nil,astring,aboolean,atable,afunction} 106 | banner('math.ldexp') 107 | checkallpass('math.ldexp',{somenumber,somee}) 108 | checkallerrors('math.ldexp',{},'bad argument') 109 | checkallerrors('math.ldexp',{notanumber},'bad argument') 110 | checkallerrors('math.ldexp',{notanumber,somee},'bad argument') 111 | checkallerrors('math.ldexp',{somenumber},'bad argument') 112 | checkallerrors('math.ldexp',{somenumber,notae},'bad argument') -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/lib/jse/JseBaseLib.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.lib.jse; 23 | 24 | import java.io.File; 25 | import java.io.FileInputStream; 26 | import java.io.IOException; 27 | import java.io.InputStream; 28 | 29 | import org.luaj.vm2.LuaValue; 30 | import org.luaj.vm2.lib.BaseLib; 31 | import org.luaj.vm2.lib.LibFunction; 32 | import org.luaj.vm2.lib.ResourceFinder; 33 | 34 | /** 35 | * Subclass of {@link BaseLib} and {@link LibFunction} which implements the lua basic library functions 36 | * and provides a directory based {@link ResourceFinder} as the {@link #FINDER}. 37 | *

38 | * Since JME has no file system by default, {@link BaseLib} implements 39 | * {@link ResourceFinder} using {@link Class#getResource(String)}. 40 | * The {@link JseBaseLib} implements {@link FINDER} by scanning the current directory 41 | * first, then falling back to {@link Class#getResource(String)} if that fails. 42 | * Otherwise, the behavior is the same as that of {@link BaseLib}. 43 | *

44 | * Typically, this library is included as part of a call to 45 | * {@link JsePlatform#standardGlobals()} 46 | *

47 | * To instantiate and use it directly, 48 | * link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as: 49 | *

 {@code
 50 |  * LuaTable _G = new LuaTable();
 51 |  * LuaThread.setGlobals(_G);
 52 |  * _G.load(new JseBaseLib());
 53 |  * _G.get("print").call(LuaValue.valueOf("hello, world"));
 54 |  * } 
55 | * Doing so will ensure the library is properly initialized 56 | * and loaded into the globals table. 57 | *

58 | * This is a direct port of the corresponding library in C. 59 | * @see BaseLib 60 | * @see ResourceFinder 61 | * @see #FINDER 62 | * @see LibFunction 63 | * @see JsePlatform 64 | * @see JmePlatform 65 | * @see http://www.lua.org/manual/5.1/manual.html#5.1 66 | */ 67 | 68 | public class JseBaseLib extends org.luaj.vm2.lib.BaseLib { 69 | 70 | /** Construct a JSE base library instance */ 71 | public JseBaseLib() { 72 | STDIN = System.in; 73 | } 74 | 75 | /** 76 | * Try to open a file in the current working directory, 77 | * or fall back to base opener if not found. 78 | * 79 | * This implementation attempts to open the file using new File(filename). 80 | * It falls back to the base implementation that looks it up as a resource 81 | * in the class path if not found as a plain file. 82 | * 83 | * @see org.luaj.vm2.lib.BaseLib 84 | * @see org.luaj.vm2.lib.ResourceFinder 85 | * 86 | * @param filename 87 | * @return InputStream, or null if not found. 88 | */ 89 | public InputStream findResource(String filename) { 90 | File f = new File(filename); 91 | if ( ! f.exists() ) 92 | return super.findResource(filename); 93 | try { 94 | return new FileInputStream(f); 95 | } catch ( IOException ioe ) { 96 | return null; 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/luajc/UpvalInfo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package org.luaj.vm2.luajc; 5 | 6 | import org.luaj.vm2.Lua; 7 | 8 | public class UpvalInfo { 9 | ProtoInfo pi; // where defined 10 | int slot; // where defined 11 | int nvars; // number of vars involved 12 | VarInfo var[]; // list of vars 13 | boolean rw; // read-write 14 | 15 | public UpvalInfo(ProtoInfo pi, int pc, int slot) { 16 | this.pi = pi; 17 | this.slot = slot; 18 | this.nvars = 0; 19 | this.var = null; 20 | includeVarAndPosteriorVars( pi.vars[slot][pc] ); 21 | for ( int i=0; i 1; 24 | } 25 | 26 | private boolean includeVarAndPosteriorVars( VarInfo var ) { 27 | if ( var == null || var == VarInfo.INVALID ) 28 | return false; 29 | if ( var.upvalue == this ) 30 | return true; 31 | var.upvalue = this; 32 | appendVar( var ); 33 | if ( isLoopVariable( var ) ) 34 | return false; 35 | boolean loopDetected = includePosteriorVarsCheckLoops( var ); 36 | if ( loopDetected ) 37 | includePriorVarsIgnoreLoops( var ); 38 | return loopDetected; 39 | } 40 | 41 | private boolean isLoopVariable(VarInfo var) { 42 | if ( var.pc >= 0 ) { 43 | switch ( Lua.GET_OPCODE(pi.prototype.code[var.pc]) ) { 44 | case Lua.OP_TFORLOOP: 45 | case Lua.OP_FORLOOP: 46 | return true; 47 | } 48 | } 49 | return false; 50 | } 51 | 52 | private boolean includePosteriorVarsCheckLoops( VarInfo prior ) { 53 | boolean loopDetected = false; 54 | for ( int i=0, n=pi.blocklist.length; i=b.pc0; pc-- ) { 69 | if ( pi.vars[slot][pc] == prior ) { 70 | loopDetected |= includeVarAndPosteriorVars( pi.vars[slot][pc+1] ); 71 | break; 72 | } 73 | } 74 | } 75 | } 76 | return loopDetected; 77 | } 78 | 79 | private void includePriorVarsIgnoreLoops(VarInfo poster) { 80 | for ( int i=0, n=pi.blocklist.length; i= var.length ) { 105 | VarInfo[] s = var; 106 | var = new VarInfo[nvars*2+1]; 107 | System.arraycopy(s, 0, var, 0, nvars); 108 | } 109 | var[nvars++] = v; 110 | } 111 | 112 | public String toString() { 113 | StringBuffer sb = new StringBuffer(); 114 | sb.append( pi.name ); 115 | for ( int i=0; i0? ",": " " ); 117 | sb.append( String.valueOf(var[i])); 118 | } 119 | if ( rw ) 120 | sb.append( "(rw)" ); 121 | return sb.toString(); 122 | } 123 | 124 | private boolean testIsAllocUpvalue(VarInfo v) { 125 | if ( v.pc < 0 ) 126 | return true; 127 | BasicBlock b = pi.blocks[v.pc]; 128 | if ( v.pc > b.pc0 ) 129 | return pi.vars[slot][v.pc-1].upvalue != this; 130 | if ( b.prev == null ) { 131 | v = pi.params[slot]; 132 | if ( v != null && v.upvalue != this ) 133 | return true; 134 | } else { 135 | for ( int i=0, n=b.prev.length; i 27 | * There will be one instance of this class, {@link LuaValue#NIL}, 28 | * per Java virtual machine. 29 | * However, the {@link Varargs} instance {@link LuaValue#NONE} 30 | * which is the empty list, 31 | * is also considered treated as a nil value by default. 32 | *

33 | * Although it is possible to test for nil using Java == operator, 34 | * the recommended approach is to use the method {@link LuaValue#isnil()} 35 | * instead. By using that any ambiguities between 36 | * {@link LuaValue#NIL} and {@link LuaValue#NONE} are avoided. 37 | * @see LuaValue 38 | * @see LuaValue#NIL 39 | */ 40 | public class LuaNil extends LuaValue { 41 | 42 | static final LuaNil _NIL = new LuaNil(); 43 | 44 | public static LuaValue s_metatable; 45 | 46 | LuaNil() {} 47 | 48 | public int type() { 49 | return LuaValue.TNIL; 50 | } 51 | 52 | public String typename() { 53 | return "nil"; 54 | } 55 | 56 | public String tojstring() { 57 | return "nil"; 58 | } 59 | 60 | public LuaValue not() { 61 | return LuaValue.TRUE; 62 | } 63 | 64 | public boolean toboolean() { 65 | return false; 66 | } 67 | 68 | public boolean isnil() { 69 | return true; 70 | } 71 | 72 | public LuaValue getmetatable() { 73 | return s_metatable; 74 | } 75 | 76 | public boolean equals(Object o) { 77 | return o instanceof LuaNil; 78 | } 79 | 80 | public LuaValue checknotnil() { 81 | return argerror("value"); 82 | } 83 | 84 | public LuaValue checkvalidkey() { 85 | return typerror("table index"); 86 | } 87 | 88 | // optional argument conversions - nil alwas falls badk to default value 89 | public boolean optboolean(boolean defval) { return defval; } 90 | public LuaClosure optclosure(LuaClosure defval) { return defval; } 91 | public double optdouble(double defval) { return defval; } 92 | public LuaFunction optfunction(LuaFunction defval) { return defval; } 93 | public int optint(int defval) { return defval; } 94 | public LuaInteger optinteger(LuaInteger defval) { return defval; } 95 | public long optlong(long defval) { return defval; } 96 | public LuaNumber optnumber(LuaNumber defval) { return defval; } 97 | public LuaTable opttable(LuaTable defval) { return defval; } 98 | public LuaThread optthread(LuaThread defval) { return defval; } 99 | public String optjstring(String defval) { return defval; } 100 | public LuaString optstring(LuaString defval) { return defval; } 101 | public Object optuserdata(Object defval) { return defval; } 102 | public Object optuserdata(Class c, Object defval) { return defval; } 103 | public LuaValue optvalue(LuaValue defval) { return defval; } 104 | } 105 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/lib/jse/JseMathLib.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009-2011 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2.lib.jse; 23 | 24 | import org.luaj.vm2.LuaValue; 25 | import org.luaj.vm2.lib.LibFunction; 26 | import org.luaj.vm2.lib.OneArgFunction; 27 | import org.luaj.vm2.lib.TwoArgFunction; 28 | 29 | /** 30 | * Subclass of {@link LibFunction} which implements the lua standard {@code math} 31 | * library. 32 | *

33 | * It contains all lua math functions, including those not available on the JME platform. 34 | * See {@link org.luaj.lib.MathLib} for the exception list. 35 | *

36 | * Typically, this library is included as part of a call to 37 | * {@link JsePlatform#standardGlobals()} 38 | *

39 | * To instantiate and use it directly, 40 | * link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as: 41 | *

 {@code
 42 |  * LuaTable _G = new LuaTable();
 43 |  * LuaThread.setGlobals(_G);
 44 |  * _G.load(new JseBaseLib());
 45 |  * _G.load(new PackageLib());
 46 |  * _G.load(new JseMathLib());
 47 |  * System.out.println( _G.get("math").get("sqrt").call( LuaValue.valueOf(2) ) );
 48 |  * } 
49 | * Doing so will ensure the library is properly initialized 50 | * and loaded into the globals table. 51 | *

52 | * This has been implemented to match as closely as possible the behavior in the corresponding library in C. 53 | * @see LibFunction 54 | * @see JsePlatform 55 | * @see JmePlatform 56 | * @see JseMathLib 57 | * @see http://www.lua.org/manual/5.1/manual.html#5.6 58 | */ 59 | public class JseMathLib extends org.luaj.vm2.lib.MathLib { 60 | 61 | public JseMathLib() {} 62 | 63 | public LuaValue call(LuaValue arg) { 64 | LuaValue t = super.call(arg); 65 | bind( t, JseMathLib1.class, new String[] { 66 | "acos", "asin", "atan", "cosh", 67 | "exp", "log", "log10", "sinh", 68 | "tanh" } ); 69 | bind( t, JseMathLib2.class, new String[] { 70 | "atan2", "pow", } ); 71 | return t; 72 | } 73 | 74 | public static final class JseMathLib1 extends OneArgFunction { 75 | public LuaValue call(LuaValue arg) { 76 | switch ( opcode ) { 77 | case 0: return valueOf(Math.acos(arg.checkdouble())); 78 | case 1: return valueOf(Math.asin(arg.checkdouble())); 79 | case 2: return valueOf(Math.atan(arg.checkdouble())); 80 | case 3: return valueOf(Math.cosh(arg.checkdouble())); 81 | case 4: return valueOf(Math.exp(arg.checkdouble())); 82 | case 5: return valueOf(Math.log(arg.checkdouble())); 83 | case 6: return valueOf(Math.log10(arg.checkdouble())); 84 | case 7: return valueOf(Math.sinh(arg.checkdouble())); 85 | case 8: return valueOf(Math.tanh(arg.checkdouble())); 86 | } 87 | return NIL; 88 | } 89 | } 90 | 91 | public static final class JseMathLib2 extends TwoArgFunction { 92 | public LuaValue call(LuaValue arg1, LuaValue arg2) { 93 | switch ( opcode ) { 94 | case 0: return valueOf(Math.atan2(arg1.checkdouble(), arg2.checkdouble())); 95 | case 1: return valueOf(Math.pow(arg1.checkdouble(), arg2.checkdouble())); 96 | } 97 | return NIL; 98 | } 99 | } 100 | 101 | /** Faster, better version of pow() used by arithmetic operator ^ */ 102 | public double dpow_lib(double a, double b) { 103 | return Math.pow(a, b); 104 | } 105 | 106 | 107 | } 108 | -------------------------------------------------------------------------------- /src/jse/org/luaj/vm2/parser/Token.java: -------------------------------------------------------------------------------- 1 | /* Generated By:JavaCC: Do not edit this line. Token.java Version 5.0 */ 2 | /* JavaCCOptions:TOKEN_EXTENDS=,KEEP_LINE_COL=null,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */ 3 | package org.luaj.vm2.parser; 4 | 5 | /** 6 | * Describes the input token stream. 7 | */ 8 | 9 | public class Token implements java.io.Serializable { 10 | 11 | /** 12 | * The version identifier for this Serializable class. 13 | * Increment only if the serialized form of the 14 | * class changes. 15 | */ 16 | private static final long serialVersionUID = 1L; 17 | 18 | /** 19 | * An integer that describes the kind of this token. This numbering 20 | * system is determined by JavaCCParser, and a table of these numbers is 21 | * stored in the file ...Constants.java. 22 | */ 23 | public int kind; 24 | 25 | /** The line number of the first character of this Token. */ 26 | public int beginLine; 27 | /** The column number of the first character of this Token. */ 28 | public int beginColumn; 29 | /** The line number of the last character of this Token. */ 30 | public int endLine; 31 | /** The column number of the last character of this Token. */ 32 | public int endColumn; 33 | 34 | /** 35 | * The string image of the token. 36 | */ 37 | public String image; 38 | 39 | /** 40 | * A reference to the next regular (non-special) token from the input 41 | * stream. If this is the last token from the input stream, or if the 42 | * token manager has not read tokens beyond this one, this field is 43 | * set to null. This is true only if this token is also a regular 44 | * token. Otherwise, see below for a description of the contents of 45 | * this field. 46 | */ 47 | public Token next; 48 | 49 | /** 50 | * This field is used to access special tokens that occur prior to this 51 | * token, but after the immediately preceding regular (non-special) token. 52 | * If there are no such special tokens, this field is set to null. 53 | * When there are more than one such special token, this field refers 54 | * to the last of these special tokens, which in turn refers to the next 55 | * previous special token through its specialToken field, and so on 56 | * until the first special token (whose specialToken field is null). 57 | * The next fields of special tokens refer to other special tokens that 58 | * immediately follow it (without an intervening regular token). If there 59 | * is no such token, this field is null. 60 | */ 61 | public Token specialToken; 62 | 63 | /** 64 | * An optional attribute value of the Token. 65 | * Tokens which are not used as syntactic sugar will often contain 66 | * meaningful values that will be used later on by the compiler or 67 | * interpreter. This attribute value is often different from the image. 68 | * Any subclass of Token that actually wants to return a non-null value can 69 | * override this method as appropriate. 70 | */ 71 | public Object getValue() { 72 | return null; 73 | } 74 | 75 | /** 76 | * No-argument constructor 77 | */ 78 | public Token() {} 79 | 80 | /** 81 | * Constructs a new token for the specified Image. 82 | */ 83 | public Token(int kind) 84 | { 85 | this(kind, null); 86 | } 87 | 88 | /** 89 | * Constructs a new token for the specified Image and Kind. 90 | */ 91 | public Token(int kind, String image) 92 | { 93 | this.kind = kind; 94 | this.image = image; 95 | } 96 | 97 | /** 98 | * Returns the image. 99 | */ 100 | public String toString() 101 | { 102 | return image; 103 | } 104 | 105 | /** 106 | * Returns a new Token object, by default. However, if you want, you 107 | * can create and return subclass objects based on the value of ofKind. 108 | * Simply add the cases to the switch for all those special cases. 109 | * For example, if you have a subclass of Token called IDToken that 110 | * you want to create if ofKind is ID, simply add something like : 111 | * 112 | * case MyParserConstants.ID : return new IDToken(ofKind, image); 113 | * 114 | * to the following switch statement. Then you can cast matchedToken 115 | * variable to the appropriate type and use sit in your lexical actions. 116 | */ 117 | public static Token newToken(int ofKind, String image) 118 | { 119 | switch(ofKind) 120 | { 121 | default : return new Token(ofKind, image); 122 | } 123 | } 124 | 125 | public static Token newToken(int ofKind) 126 | { 127 | return newToken(ofKind, null); 128 | } 129 | 130 | } 131 | /* JavaCC - OriginalChecksum=70d73add5771158f10d1ae81755e7cfc (do not edit this line) */ 132 | -------------------------------------------------------------------------------- /src/core/org/luaj/vm2/LuaUserdata.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2009 Luaj.org. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | ******************************************************************************/ 22 | package org.luaj.vm2; 23 | 24 | 25 | public class LuaUserdata extends LuaValue { 26 | 27 | public final Object m_instance; 28 | public LuaValue m_metatable; 29 | 30 | public LuaUserdata(Object obj) { 31 | m_instance = obj; 32 | } 33 | 34 | public LuaUserdata(Object obj, LuaValue metatable) { 35 | m_instance = obj; 36 | m_metatable = metatable; 37 | } 38 | 39 | public String tojstring() { 40 | return String.valueOf(m_instance); 41 | } 42 | 43 | public int type() { 44 | return LuaValue.TUSERDATA; 45 | } 46 | 47 | public String typename() { 48 | return "userdata"; 49 | } 50 | 51 | public int hashCode() { 52 | return m_instance.hashCode(); 53 | } 54 | 55 | public Object userdata() { 56 | return m_instance; 57 | } 58 | 59 | public boolean isuserdata() { return true; } 60 | public boolean isuserdata(Class c) { return c.isAssignableFrom(m_instance.getClass()); } 61 | public Object touserdata() { return m_instance; } 62 | public Object touserdata(Class c) { return c.isAssignableFrom(m_instance.getClass())? m_instance: null; } 63 | public Object optuserdata(Object defval) { return m_instance; } 64 | public Object optuserdata(Class c, Object defval) { 65 | if (!c.isAssignableFrom(m_instance.getClass())) 66 | typerror(c.getName()); 67 | return m_instance; 68 | } 69 | 70 | public LuaValue getmetatable() { 71 | return m_metatable; 72 | } 73 | 74 | public LuaValue setmetatable(LuaValue metatable) { 75 | this.m_metatable = metatable; 76 | return this; 77 | } 78 | 79 | public Object checkuserdata() { 80 | return m_instance; 81 | } 82 | 83 | public Object checkuserdata(Class c) { 84 | if ( c.isAssignableFrom(m_instance.getClass()) ) 85 | return m_instance; 86 | return typerror(c.getName()); 87 | } 88 | 89 | public LuaValue get( LuaValue key ) { 90 | return m_metatable!=null? gettable(this,key): NIL; 91 | } 92 | 93 | public void set( LuaValue key, LuaValue value ) { 94 | if ( m_metatable!=null && ! settable(this,key,value) ) 95 | error( "cannot set "+key+" for userdata" ); 96 | } 97 | 98 | public boolean equals( Object val ) { 99 | if ( this == val ) 100 | return true; 101 | if ( ! (val instanceof LuaUserdata) ) 102 | return false; 103 | LuaUserdata u = (LuaUserdata) val; 104 | return m_instance.equals(u.m_instance); 105 | } 106 | 107 | // equality w/ metatable processing 108 | public LuaValue eq( LuaValue val ) { return eq_b(val)? TRUE: FALSE; } 109 | public boolean eq_b( LuaValue val ) { 110 | if ( val.raweq(this) ) return true; 111 | if ( m_metatable == null || !val.isuserdata() ) return false; 112 | LuaValue valmt = val.getmetatable(); 113 | return valmt!=null && LuaValue.eqmtcall(this, m_metatable, val, valmt); 114 | } 115 | 116 | // equality w/o metatable processing 117 | public boolean raweq( LuaValue val ) { return val.raweq(this); } 118 | public boolean raweq( LuaUserdata val ) { 119 | return this == val || (m_metatable == val.m_metatable && m_instance.equals(val.m_instance)); 120 | } 121 | 122 | // __eq metatag processing 123 | public boolean eqmt( LuaValue val ) { 124 | return m_metatable!=null && val.isuserdata()? LuaValue.eqmtcall(this, m_metatable, val, val.getmetatable()): false; 125 | } 126 | } 127 | --------------------------------------------------------------------------------