Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
70cb74b4d6 |
@@ -1,22 +1,22 @@
|
|||||||
# This is a fork!
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
<div style="border: 1px dotted red; margin: 1.em 0.5em; font-weight: bold; color: red;">
|
<html>
|
||||||
This repository has been forked from the original CVS sources of Luaj.
|
|
||||||
The commit history has been converted to make sure that the original work of
|
|
||||||
James Roseborough and Ian Farmer is not lost.
|
|
||||||
Unfortunately, I was not able to contact either James or Ian to hand over
|
|
||||||
ownership of the Github organization/repo as I have originally intended to.
|
|
||||||
The community however seems interested enough to continue work on the original
|
|
||||||
sources and therefore I have decided to make sure that any useful pull requests
|
|
||||||
that may add some value to the original code base shall be merged in from now
|
|
||||||
on.<br>
|
|
||||||
-- Benjamin P. Jung, Jan. 26th 2018
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1>Getting Started with LuaJ</h1>
|
<head>
|
||||||
|
<title>Getting Started with LuaJ</title>
|
||||||
|
<link rel="stylesheet" type="text/css" href="http://www.lua.org/lua.css">
|
||||||
|
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1">
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
|
||||||
<h1>Getting Started with LuaJ</h1>
|
<hr>
|
||||||
James Roseborough, Ian Farmer, Version 3.0.2
|
<h1>
|
||||||
|
<a href="README.html"><img src="http://sourceforge.net/dbimage.php?id=196139" alt="" border="0"></a>
|
||||||
|
|
||||||
|
Getting Started with LuaJ
|
||||||
|
|
||||||
|
</h1>
|
||||||
|
James Roseborough, Ian Farmer, Version 3.0.1
|
||||||
<p>
|
<p>
|
||||||
<small>
|
<small>
|
||||||
Copyright © 2009-2014 Luaj.org.
|
Copyright © 2009-2014 Luaj.org.
|
||||||
@@ -120,7 +120,7 @@ in comparison with the standard C distribution.
|
|||||||
<td>16.794</td>
|
<td>16.794</td>
|
||||||
<td>11.274</td>
|
<td>11.274</td>
|
||||||
<td>Java</td>
|
<td>Java</td>
|
||||||
<td>java -cp luaj-jse-3.0.2.jar;bcel-5.2.jar lua <b>-b</b> fannkuch.lua 10</td></tr>
|
<td>java -cp luaj-jse-3.0.1.jar;bcel-5.2.jar lua <b>-b</b> fannkuch.lua 10</td></tr>
|
||||||
<tr valign="top">
|
<tr valign="top">
|
||||||
<td></td>
|
<td></td>
|
||||||
<td></td>
|
<td></td>
|
||||||
@@ -130,7 +130,7 @@ in comparison with the standard C distribution.
|
|||||||
<td>36.894</td>
|
<td>36.894</td>
|
||||||
<td>15.163</td>
|
<td>15.163</td>
|
||||||
<td></td>
|
<td></td>
|
||||||
<td>java -cp luaj-jse-3.0.2.jar lua -n fannkuch.lua 10</td></tr>
|
<td>java -cp luaj-jse-3.0.1.jar lua -n fannkuch.lua 10</td></tr>
|
||||||
<tr valign="top">
|
<tr valign="top">
|
||||||
<td>lua</td>
|
<td>lua</td>
|
||||||
<td>5.1.4</td>
|
<td>5.1.4</td>
|
||||||
@@ -186,7 +186,7 @@ It is also faster than Java-lua implementations Jill, Kahlua, and Mochalua for a
|
|||||||
From the main distribution directory line type:
|
From the main distribution directory line type:
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
java -cp lib/luaj-jse-3.0.2.jar lua examples/lua/hello.lua
|
java -cp lib/luaj-jse-3.0.1.jar lua examples/lua/hello.lua
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@@ -198,7 +198,7 @@ You should see the following output:
|
|||||||
To see how luaj can be used to acccess most Java API's including swing, try:
|
To see how luaj can be used to acccess most Java API's including swing, try:
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
java -cp lib/luaj-jse-3.0.2.jar lua examples/lua/swingapp.lua
|
java -cp lib/luaj-jse-3.0.1.jar lua examples/lua/swingapp.lua
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@@ -213,8 +213,8 @@ Links to sources:<pre>
|
|||||||
From the main distribution directory line type:
|
From the main distribution directory line type:
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
java -cp lib/luaj-jse-3.0.2.jar luac examples/lua/hello.lua
|
java -cp lib/luaj-jse-3.0.1.jar luac examples/lua/hello.lua
|
||||||
java -cp lib/luaj-jse-3.0.2.jar lua luac.out
|
java -cp lib/luaj-jse-3.0.1.jar lua luac.out
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@@ -228,8 +228,8 @@ Luaj can compile lua sources or binaries directly to java bytecode if the bcel l
|
|||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
ant bcel-lib
|
ant bcel-lib
|
||||||
java -cp "lib/luaj-jse-3.0.2.jar;lib/bcel-5.2.jar" luajc -s examples/lua -d . hello.lua
|
java -cp "lib/luaj-jse-3.0.1.jar;lib/bcel-5.2.jar" luajc -s examples/lua -d . hello.lua
|
||||||
java -cp "lib/luaj-jse-3.0.2.jar;." lua -l hello
|
java -cp "lib/luaj-jse-3.0.1.jar;." lua -l hello
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@@ -240,7 +240,7 @@ but the compiled classes must be in the class path at runtime, unless runtime ji
|
|||||||
<p>
|
<p>
|
||||||
Lua scripts can also be run directly in this mode without precompiling using the <em>lua</em> command with the <b><em>-b</em></b> option and providing the <em>bcel</em> library in the class path:
|
Lua scripts can also be run directly in this mode without precompiling using the <em>lua</em> command with the <b><em>-b</em></b> option and providing the <em>bcel</em> library in the class path:
|
||||||
<pre>
|
<pre>
|
||||||
java -cp "lib/luaj-jse-3.0.2.jar;lib/bcel-5.2.jar" lua -b examples/lua/hello.lua
|
java -cp "lib/luaj-jse-3.0.1.jar;lib/bcel-5.2.jar" lua -b examples/lua/hello.lua
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
@@ -284,7 +284,7 @@ A simple example may be found in
|
|||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
You must include the library <b>lib/luaj-jse-3.0.2.jar</b> in your class path.
|
You must include the library <b>lib/luaj-jse-3.0.1.jar</b> in your class path.
|
||||||
|
|
||||||
<h2>Run a script in a MIDlet</h2>
|
<h2>Run a script in a MIDlet</h2>
|
||||||
|
|
||||||
@@ -311,7 +311,7 @@ A simple example may be found in
|
|||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
You must include the library <b>lib/luaj-jme-3.0.2.jar</b> in your midlet jar.
|
You must include the library <b>lib/luaj-jme-3.0.1.jar</b> in your midlet jar.
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
An ant script to build and run the midlet is in
|
An ant script to build and run the midlet is in
|
||||||
@@ -341,7 +341,7 @@ You can also look up the engine by language "lua" or mimetypes "text/lua" or "ap
|
|||||||
All standard aspects of script engines including compiled statements are supported.
|
All standard aspects of script engines including compiled statements are supported.
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
You must include the library <b>lib/luaj-jse-3.0.2.jar</b> in your class path.
|
You must include the library <b>lib/luaj-jse-3.0.1.jar</b> in your class path.
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
A working example may be found in
|
A working example may be found in
|
||||||
@@ -352,8 +352,8 @@ A working example may be found in
|
|||||||
To compile and run it using Java 1.6 or higher:
|
To compile and run it using Java 1.6 or higher:
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
javac -cp lib/luaj-jse-3.0.2.jar examples/jse/ScriptEngineSample.java
|
javac -cp lib/luaj-jse-3.0.1.jar examples/jse/ScriptEngineSample.java
|
||||||
java -cp "lib/luaj-jse-3.0.2.jar;examples/jse" ScriptEngineSample
|
java -cp "lib/luaj-jse-3.0.1.jar;examples/jse" ScriptEngineSample
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<h2>Excluding the lua bytecode compiler</h2>
|
<h2>Excluding the lua bytecode compiler</h2>
|
||||||
@@ -593,7 +593,7 @@ The following lua script will open a swing frame on Java SE:
|
|||||||
See a longer sample in <em>examples/lua/swingapp.lua</em> for details, including a simple animation loop, rendering graphics, mouse and key handling, and image loading.
|
See a longer sample in <em>examples/lua/swingapp.lua</em> for details, including a simple animation loop, rendering graphics, mouse and key handling, and image loading.
|
||||||
Or try running it using:
|
Or try running it using:
|
||||||
<pre>
|
<pre>
|
||||||
java -cp lib/luaj-jse-3.0.2.jar lua examples/lua/swingapp.lua
|
java -cp lib/luaj-jse-3.0.1.jar lua examples/lua/swingapp.lua
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@@ -842,7 +842,7 @@ For JSE projects, add this dependency for the luaj-jse jar:
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.luaj</groupId>
|
<groupId>org.luaj</groupId>
|
||||||
<artifactId>luaj-jse</artifactId>
|
<artifactId>luaj-jse</artifactId>
|
||||||
<version>3.0.2</version>
|
<version>3.0.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</pre>
|
</pre>
|
||||||
while for JME projects, use the luaj-jme jar:
|
while for JME projects, use the luaj-jme jar:
|
||||||
@@ -850,7 +850,7 @@ while for JME projects, use the luaj-jme jar:
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.luaj</groupId>
|
<groupId>org.luaj</groupId>
|
||||||
<artifactId>luaj-jme</artifactId>
|
<artifactId>luaj-jme</artifactId>
|
||||||
<version>3.0.2</version>
|
<version>3.0.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@@ -880,7 +880,7 @@ Unit test scripts can be found in these locations
|
|||||||
test/lua/*.lua
|
test/lua/*.lua
|
||||||
test/lua/errors/*.lua
|
test/lua/errors/*.lua
|
||||||
test/lua/perf/*.lua
|
test/lua/perf/*.lua
|
||||||
test/lua/luaj3.0.2-tests.zip
|
test/lua/luaj3.0.1-tests.zip
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<h2>Code coverage</h2>
|
<h2>Code coverage</h2>
|
||||||
@@ -1017,11 +1017,6 @@ and at <a href="http://luaj.sourceforge.net/api/2.0/index.html">http://luaj.sour
|
|||||||
<li>Move online docs to <a href="http://luaj.org/luaj/3.0/api/index.html">http://luaj.org/luaj/3.0/api/</a></li>
|
<li>Move online docs to <a href="http://luaj.org/luaj/3.0/api/index.html">http://luaj.org/luaj/3.0/api/</a></li>
|
||||||
<li>Fix os.time() conversions for pm times.</li>
|
<li>Fix os.time() conversions for pm times.</li>
|
||||||
|
|
||||||
<tr valign="top"><td> <b>3.0.2</b></td><td><ul>
|
|
||||||
<li>Fix JsePlatform.luaMain() to provide an "arg" table in the chunk's environment.</li>
|
|
||||||
<li>Let JsePlatform.luaMain() return values returned by the main chunk.</li>
|
|
||||||
<li>Add synchronization to CoerceJavaToLua.COERCIONS map.</li>
|
|
||||||
|
|
||||||
</ul></td></tr>
|
</ul></td></tr>
|
||||||
</table></td></tr></table>
|
</table></td></tr></table>
|
||||||
|
|
||||||
@@ -11,13 +11,13 @@ import org.luaj.vm2.lib.Bit32Lib;
|
|||||||
import org.luaj.vm2.lib.CoroutineLib;
|
import org.luaj.vm2.lib.CoroutineLib;
|
||||||
import org.luaj.vm2.lib.PackageLib;
|
import org.luaj.vm2.lib.PackageLib;
|
||||||
import org.luaj.vm2.lib.ResourceFinder;
|
import org.luaj.vm2.lib.ResourceFinder;
|
||||||
|
import org.luaj.vm2.lib.StringLib;
|
||||||
import org.luaj.vm2.lib.TableLib;
|
import org.luaj.vm2.lib.TableLib;
|
||||||
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
|
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
|
||||||
import org.luaj.vm2.lib.jse.JseBaseLib;
|
import org.luaj.vm2.lib.jse.JseBaseLib;
|
||||||
import org.luaj.vm2.lib.jse.JseIoLib;
|
import org.luaj.vm2.lib.jse.JseIoLib;
|
||||||
import org.luaj.vm2.lib.jse.JseMathLib;
|
import org.luaj.vm2.lib.jse.JseMathLib;
|
||||||
import org.luaj.vm2.lib.jse.JseOsLib;
|
import org.luaj.vm2.lib.jse.JseOsLib;
|
||||||
import org.luaj.vm2.lib.jse.JseStringLib;
|
|
||||||
import org.luaj.vm2.lib.jse.LuajavaLib;
|
import org.luaj.vm2.lib.jse.LuajavaLib;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,7 +76,7 @@ public class SampleApplet extends Applet implements ResourceFinder {
|
|||||||
globals.load(new PackageLib());
|
globals.load(new PackageLib());
|
||||||
globals.load(new Bit32Lib());
|
globals.load(new Bit32Lib());
|
||||||
globals.load(new TableLib());
|
globals.load(new TableLib());
|
||||||
globals.load(new JseStringLib());
|
globals.load(new StringLib());
|
||||||
globals.load(new CoroutineLib());
|
globals.load(new CoroutineLib());
|
||||||
globals.load(new JseMathLib());
|
globals.load(new JseMathLib());
|
||||||
globals.load(new JseIoLib());
|
globals.load(new JseIoLib());
|
||||||
|
|||||||
@@ -1,26 +1,12 @@
|
|||||||
import org.luaj.vm2.Globals;
|
import org.luaj.vm2.*;
|
||||||
import org.luaj.vm2.LoadState;
|
|
||||||
import org.luaj.vm2.LuaBoolean;
|
|
||||||
import org.luaj.vm2.LuaString;
|
|
||||||
import org.luaj.vm2.LuaTable;
|
|
||||||
import org.luaj.vm2.LuaThread;
|
|
||||||
import org.luaj.vm2.LuaValue;
|
|
||||||
import org.luaj.vm2.Varargs;
|
|
||||||
import org.luaj.vm2.compiler.LuaC;
|
import org.luaj.vm2.compiler.LuaC;
|
||||||
import org.luaj.vm2.lib.Bit32Lib;
|
import org.luaj.vm2.lib.*;
|
||||||
import org.luaj.vm2.lib.DebugLib;
|
import org.luaj.vm2.lib.jse.*;
|
||||||
import org.luaj.vm2.lib.PackageLib;
|
|
||||||
import org.luaj.vm2.lib.TableLib;
|
|
||||||
import org.luaj.vm2.lib.TwoArgFunction;
|
|
||||||
import org.luaj.vm2.lib.ZeroArgFunction;
|
|
||||||
import org.luaj.vm2.lib.jse.JseBaseLib;
|
|
||||||
import org.luaj.vm2.lib.jse.JseMathLib;
|
|
||||||
import org.luaj.vm2.lib.jse.JseStringLib;
|
|
||||||
|
|
||||||
/** Simple program that illustrates basic sand-boxing of client scripts
|
/** Simple program that illustrates basic sand-boxing of client scripts
|
||||||
* in a server environment.
|
* in a server environment.
|
||||||
*
|
*
|
||||||
* <p>Although this sandboxing is done primarily in Java here, these
|
* <p>Although this sandboxing is done primarily in Java here, these
|
||||||
* same techniques should all be possible directly from lua using metatables,
|
* same techniques should all be possible directly from lua using metatables,
|
||||||
* and examples are shown in examples/lua/samplesandboxed.lua.
|
* and examples are shown in examples/lua/samplesandboxed.lua.
|
||||||
*
|
*
|
||||||
@@ -43,7 +29,7 @@ public class SampleSandboxed {
|
|||||||
server_globals = new Globals();
|
server_globals = new Globals();
|
||||||
server_globals.load(new JseBaseLib());
|
server_globals.load(new JseBaseLib());
|
||||||
server_globals.load(new PackageLib());
|
server_globals.load(new PackageLib());
|
||||||
server_globals.load(new JseStringLib());
|
server_globals.load(new StringLib());
|
||||||
|
|
||||||
// To load scripts, we occasionally need a math library in addition to compiler support.
|
// To load scripts, we occasionally need a math library in addition to compiler support.
|
||||||
// To limit scripts using the debug library, they must be closures, so we only install LuaC.
|
// To limit scripts using the debug library, they must be closures, so we only install LuaC.
|
||||||
@@ -61,7 +47,7 @@ public class SampleSandboxed {
|
|||||||
runScriptInSandbox( "return getmetatable('abc').len" );
|
runScriptInSandbox( "return getmetatable('abc').len" );
|
||||||
runScriptInSandbox( "return getmetatable('abc').__index" );
|
runScriptInSandbox( "return getmetatable('abc').__index" );
|
||||||
|
|
||||||
// Example user scripts that attempt rogue operations, and will fail.
|
// Example user scripts that attempt rogue operations, and will fail.
|
||||||
runScriptInSandbox( "return setmetatable('abc', {})" );
|
runScriptInSandbox( "return setmetatable('abc', {})" );
|
||||||
runScriptInSandbox( "getmetatable('abc').len = function() end" );
|
runScriptInSandbox( "getmetatable('abc').len = function() end" );
|
||||||
runScriptInSandbox( "getmetatable('abc').__index = {}" );
|
runScriptInSandbox( "getmetatable('abc').__index = {}" );
|
||||||
@@ -75,9 +61,9 @@ public class SampleSandboxed {
|
|||||||
LuaValue.ADD, new TwoArgFunction() {
|
LuaValue.ADD, new TwoArgFunction() {
|
||||||
public LuaValue call(LuaValue x, LuaValue y) {
|
public LuaValue call(LuaValue x, LuaValue y) {
|
||||||
return LuaValue.valueOf(
|
return LuaValue.valueOf(
|
||||||
(x == TRUE ? 1.0 : x.todouble()) +
|
(x == TRUE ? 1.0 : x.todouble()) +
|
||||||
(y == TRUE ? 1.0 : y.todouble()) );
|
(y == TRUE ? 1.0 : y.todouble()) );
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
runScriptInSandbox( "return 5 + 6, 5 + true, false + 6" );
|
runScriptInSandbox( "return 5 + 6, 5 + true, false + 6" );
|
||||||
@@ -89,21 +75,21 @@ public class SampleSandboxed {
|
|||||||
// that contain functions that can be abused.
|
// that contain functions that can be abused.
|
||||||
static void runScriptInSandbox(String script) {
|
static void runScriptInSandbox(String script) {
|
||||||
|
|
||||||
// Each script will have it's own set of globals, which should
|
// Each script will have it's own set of globals, which should
|
||||||
// prevent leakage between scripts running on the same server.
|
// prevent leakage between scripts running on the same server.
|
||||||
Globals user_globals = new Globals();
|
Globals user_globals = new Globals();
|
||||||
user_globals.load(new JseBaseLib());
|
user_globals.load(new JseBaseLib());
|
||||||
user_globals.load(new PackageLib());
|
user_globals.load(new PackageLib());
|
||||||
user_globals.load(new Bit32Lib());
|
user_globals.load(new Bit32Lib());
|
||||||
user_globals.load(new TableLib());
|
user_globals.load(new TableLib());
|
||||||
user_globals.load(new JseStringLib());
|
user_globals.load(new StringLib());
|
||||||
user_globals.load(new JseMathLib());
|
user_globals.load(new JseMathLib());
|
||||||
|
|
||||||
// This library is dangerous as it gives unfettered access to the
|
// This library is dangerous as it gives unfettered access to the
|
||||||
// entire Java VM, so it's not suitable within this lightweight sandbox.
|
// entire Java VM, so it's not suitable within this lightweight sandbox.
|
||||||
// user_globals.load(new LuajavaLib());
|
// user_globals.load(new LuajavaLib());
|
||||||
|
|
||||||
// Starting coroutines in scripts will result in threads that are
|
// Starting coroutines in scripts will result in threads that are
|
||||||
// not under the server control, so this libary should probably remain out.
|
// not under the server control, so this libary should probably remain out.
|
||||||
// user_globals.load(new CoroutineLib());
|
// user_globals.load(new CoroutineLib());
|
||||||
|
|
||||||
@@ -112,12 +98,12 @@ public class SampleSandboxed {
|
|||||||
// user_globals.load(new JseIoLib());
|
// user_globals.load(new JseIoLib());
|
||||||
// user_globals.load(new JseOsLib());
|
// user_globals.load(new JseOsLib());
|
||||||
|
|
||||||
// Loading and compiling scripts from within scripts may also be
|
// Loading and compiling scripts from within scripts may also be
|
||||||
// prohibited, though in theory it should be fairly safe.
|
// prohibited, though in theory it should be fairly safe.
|
||||||
// LoadState.install(user_globals);
|
// LoadState.install(user_globals);
|
||||||
// LuaC.install(user_globals);
|
// LuaC.install(user_globals);
|
||||||
|
|
||||||
// The debug library must be loaded for hook functions to work, which
|
// The debug library must be loaded for hook functions to work, which
|
||||||
// allow us to limit scripts to run a certain number of instructions at a time.
|
// allow us to limit scripts to run a certain number of instructions at a time.
|
||||||
// However we don't wish to expose the library in the user globals,
|
// However we don't wish to expose the library in the user globals,
|
||||||
// so it is immediately removed from the user globals once created.
|
// so it is immediately removed from the user globals once created.
|
||||||
@@ -125,18 +111,18 @@ public class SampleSandboxed {
|
|||||||
LuaValue sethook = user_globals.get("debug").get("sethook");
|
LuaValue sethook = user_globals.get("debug").get("sethook");
|
||||||
user_globals.set("debug", LuaValue.NIL);
|
user_globals.set("debug", LuaValue.NIL);
|
||||||
|
|
||||||
// Set up the script to run in its own lua thread, which allows us
|
// Set up the script to run in its own lua thread, which allows us
|
||||||
// to set a hook function that limits the script to a specific number of cycles.
|
// to set a hook function that limits the script to a specific number of cycles.
|
||||||
// Note that the environment is set to the user globals, even though the
|
// Note that the environment is set to the user globals, even though the
|
||||||
// compiling is done with the server globals.
|
// compiling is done with the server globals.
|
||||||
LuaValue chunk = server_globals.load(script, "main", user_globals);
|
LuaValue chunk = server_globals.load(script, "main", user_globals);
|
||||||
LuaThread thread = new LuaThread(user_globals, chunk);
|
LuaThread thread = new LuaThread(user_globals, chunk);
|
||||||
|
|
||||||
// Set the hook function to immediately throw an Error, which will not be
|
// Set the hook function to immediately throw an Error, which will not be
|
||||||
// handled by any Lua code other than the coroutine.
|
// handled by any Lua code other than the coroutine.
|
||||||
LuaValue hookfunc = new ZeroArgFunction() {
|
LuaValue hookfunc = new ZeroArgFunction() {
|
||||||
public LuaValue call() {
|
public LuaValue call() {
|
||||||
// A simple lua error may be caught by the script, but a
|
// A simple lua error may be caught by the script, but a
|
||||||
// Java Error will pass through to top and stop the script.
|
// Java Error will pass through to top and stop the script.
|
||||||
throw new Error("Script overran resource limits.");
|
throw new Error("Script overran resource limits.");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,19 +97,14 @@ public class LuaClosure extends LuaFunction {
|
|||||||
*/
|
*/
|
||||||
public LuaClosure(Prototype p, LuaValue env) {
|
public LuaClosure(Prototype p, LuaValue env) {
|
||||||
this.p = p;
|
this.p = p;
|
||||||
this.initupvalue1(env);
|
|
||||||
globals = env instanceof Globals? (Globals) env: null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initupvalue1(LuaValue env) {
|
|
||||||
if (p.upvalues == null || p.upvalues.length == 0)
|
if (p.upvalues == null || p.upvalues.length == 0)
|
||||||
this.upValues = NOUPVALUES;
|
this.upValues = NOUPVALUES;
|
||||||
else {
|
else {
|
||||||
this.upValues = new UpValue[p.upvalues.length];
|
this.upValues = new UpValue[p.upvalues.length];
|
||||||
this.upValues[0] = new UpValue(new LuaValue[] {env}, 0);
|
this.upValues[0] = new UpValue(new LuaValue[] {env}, 0);
|
||||||
}
|
}
|
||||||
|
globals = env instanceof Globals? (Globals) env: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean isclosure() {
|
public boolean isclosure() {
|
||||||
return true;
|
return true;
|
||||||
@@ -123,24 +118,26 @@ public class LuaClosure extends LuaFunction {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LuaValue getmetatable() {
|
||||||
|
return s_metatable;
|
||||||
|
}
|
||||||
|
|
||||||
public String tojstring() {
|
public String tojstring() {
|
||||||
return "function: " + p.toString();
|
return "function: " + p.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private LuaValue[] getNewStack() {
|
|
||||||
int max = p.maxstacksize;
|
|
||||||
LuaValue[] stack = new LuaValue[max];
|
|
||||||
System.arraycopy(NILS, 0, stack, 0, max);
|
|
||||||
return stack;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final LuaValue call() {
|
public final LuaValue call() {
|
||||||
LuaValue[] stack = getNewStack();
|
LuaValue[] stack = new LuaValue[p.maxstacksize];
|
||||||
|
for (int i = 0; i < p.numparams; ++i )
|
||||||
|
stack[i] = NIL;
|
||||||
return execute(stack,NONE).arg1();
|
return execute(stack,NONE).arg1();
|
||||||
}
|
}
|
||||||
|
|
||||||
public final LuaValue call(LuaValue arg) {
|
public final LuaValue call(LuaValue arg) {
|
||||||
LuaValue[] stack = getNewStack();
|
LuaValue[] stack = new LuaValue[p.maxstacksize];
|
||||||
|
System.arraycopy(NILS, 0, stack, 0, p.maxstacksize);
|
||||||
|
for (int i = 1; i < p.numparams; ++i )
|
||||||
|
stack[i] = NIL;
|
||||||
switch ( p.numparams ) {
|
switch ( p.numparams ) {
|
||||||
default: stack[0]=arg; return execute(stack,NONE).arg1();
|
default: stack[0]=arg; return execute(stack,NONE).arg1();
|
||||||
case 0: return execute(stack,arg).arg1();
|
case 0: return execute(stack,arg).arg1();
|
||||||
@@ -148,7 +145,9 @@ public class LuaClosure extends LuaFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public final LuaValue call(LuaValue arg1, LuaValue arg2) {
|
public final LuaValue call(LuaValue arg1, LuaValue arg2) {
|
||||||
LuaValue[] stack = getNewStack();
|
LuaValue[] stack = new LuaValue[p.maxstacksize];
|
||||||
|
for (int i = 2; i < p.numparams; ++i )
|
||||||
|
stack[i] = NIL;
|
||||||
switch ( p.numparams ) {
|
switch ( p.numparams ) {
|
||||||
default: stack[0]=arg1; stack[1]=arg2; return execute(stack,NONE).arg1();
|
default: stack[0]=arg1; stack[1]=arg2; return execute(stack,NONE).arg1();
|
||||||
case 1: stack[0]=arg1; return execute(stack,arg2).arg1();
|
case 1: stack[0]=arg1; return execute(stack,arg2).arg1();
|
||||||
@@ -157,7 +156,9 @@ public class LuaClosure extends LuaFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public final LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
|
public final LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
|
||||||
LuaValue[] stack = getNewStack();
|
LuaValue[] stack = new LuaValue[p.maxstacksize];
|
||||||
|
for (int i = 3; i < p.numparams; ++i )
|
||||||
|
stack[i] = NIL;
|
||||||
switch ( p.numparams ) {
|
switch ( p.numparams ) {
|
||||||
default: stack[0]=arg1; stack[1]=arg2; stack[2]=arg3; return execute(stack,NONE).arg1();
|
default: stack[0]=arg1; stack[1]=arg2; stack[2]=arg3; return execute(stack,NONE).arg1();
|
||||||
case 2: stack[0]=arg1; stack[1]=arg2; return execute(stack,arg3).arg1();
|
case 2: stack[0]=arg1; stack[1]=arg2; return execute(stack,arg3).arg1();
|
||||||
@@ -171,9 +172,9 @@ public class LuaClosure extends LuaFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public final Varargs onInvoke(Varargs varargs) {
|
public final Varargs onInvoke(Varargs varargs) {
|
||||||
LuaValue[] stack = getNewStack();
|
LuaValue[] stack = new LuaValue[p.maxstacksize];
|
||||||
for ( int i=0; i<p.numparams; i++ )
|
for ( int i=0; i<p.numparams; i++ )
|
||||||
stack[i] = varargs.arg(i+1);
|
stack[i] = varargs.arg(i+1);
|
||||||
return execute(stack,p.is_vararg!=0? varargs.subargs(p.numparams+1): NONE);
|
return execute(stack,p.is_vararg!=0? varargs.subargs(p.numparams+1): NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -301,7 +302,7 @@ public class LuaClosure extends LuaFunction {
|
|||||||
if ( c > b+1 ) {
|
if ( c > b+1 ) {
|
||||||
Buffer sb = stack[c].buffer();
|
Buffer sb = stack[c].buffer();
|
||||||
while ( --c>=b )
|
while ( --c>=b )
|
||||||
sb.concatTo(stack[c]);
|
sb = stack[c].concat(sb);
|
||||||
stack[a] = sb.value();
|
stack[a] = sb.value();
|
||||||
} else {
|
} else {
|
||||||
stack[a] = stack[c-1].concat(stack[c]);
|
stack[a] = stack[c-1].concat(stack[c]);
|
||||||
|
|||||||
@@ -177,14 +177,7 @@ public class LuaDouble extends LuaNumber {
|
|||||||
* @see #dmod_d(double, double)
|
* @see #dmod_d(double, double)
|
||||||
*/
|
*/
|
||||||
public static LuaValue dmod(double lhs, double rhs) {
|
public static LuaValue dmod(double lhs, double rhs) {
|
||||||
if (rhs == 0 || lhs == Double.POSITIVE_INFINITY || lhs == Double.NEGATIVE_INFINITY) return NAN;
|
return rhs!=0? valueOf( lhs-rhs*Math.floor(lhs/rhs) ): NAN;
|
||||||
if (rhs == Double.POSITIVE_INFINITY) {
|
|
||||||
return lhs < 0 ? POSINF : valueOf(lhs);
|
|
||||||
}
|
|
||||||
if (rhs == Double.NEGATIVE_INFINITY) {
|
|
||||||
return lhs > 0 ? NEGINF : valueOf(lhs);
|
|
||||||
}
|
|
||||||
return valueOf( lhs-rhs*Math.floor(lhs/rhs) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Take modulo for double numbers according to lua math, and return a double result.
|
/** Take modulo for double numbers according to lua math, and return a double result.
|
||||||
@@ -195,14 +188,7 @@ public class LuaDouble extends LuaNumber {
|
|||||||
* @see #dmod(double, double)
|
* @see #dmod(double, double)
|
||||||
*/
|
*/
|
||||||
public static double dmod_d(double lhs, double rhs) {
|
public static double dmod_d(double lhs, double rhs) {
|
||||||
if (rhs == 0 || lhs == Double.POSITIVE_INFINITY || lhs == Double.NEGATIVE_INFINITY) return Double.NaN;
|
return rhs!=0? lhs-rhs*Math.floor(lhs/rhs): Double.NaN;
|
||||||
if (rhs == Double.POSITIVE_INFINITY) {
|
|
||||||
return lhs < 0 ? Double.POSITIVE_INFINITY : lhs;
|
|
||||||
}
|
|
||||||
if (rhs == Double.NEGATIVE_INFINITY) {
|
|
||||||
return lhs > 0 ? Double.NEGATIVE_INFINITY : lhs;
|
|
||||||
}
|
|
||||||
return lhs-rhs*Math.floor(lhs/rhs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// relational operators
|
// relational operators
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ public class LuaString extends LuaValue {
|
|||||||
/** Construct a new LuaString using a copy of the bytes array supplied */
|
/** Construct a new LuaString using a copy of the bytes array supplied */
|
||||||
private static LuaString valueFromCopy(byte[] bytes, int off, int len) {
|
private static LuaString valueFromCopy(byte[] bytes, int off, int len) {
|
||||||
final byte[] copy = new byte[len];
|
final byte[] copy = new byte[len];
|
||||||
System.arraycopy(bytes, off, copy, 0, len);
|
for (int i=0; i<len; ++i) copy[i] = bytes[off+i];
|
||||||
return new LuaString(copy, 0, len);
|
return new LuaString(copy, 0, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -391,23 +391,23 @@ public class LuaString extends LuaValue {
|
|||||||
public short toshort() { return (short) toint(); }
|
public short toshort() { return (short) toint(); }
|
||||||
|
|
||||||
public double optdouble(double defval) {
|
public double optdouble(double defval) {
|
||||||
return checkdouble();
|
return checknumber().checkdouble();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int optint(int defval) {
|
public int optint(int defval) {
|
||||||
return checkint();
|
return checknumber().checkint();
|
||||||
}
|
}
|
||||||
|
|
||||||
public LuaInteger optinteger(LuaInteger defval) {
|
public LuaInteger optinteger(LuaInteger defval) {
|
||||||
return checkinteger();
|
return checknumber().checkinteger();
|
||||||
}
|
}
|
||||||
|
|
||||||
public long optlong(long defval) {
|
public long optlong(long defval) {
|
||||||
return checklong();
|
return checknumber().checklong();
|
||||||
}
|
}
|
||||||
|
|
||||||
public LuaNumber optnumber(LuaNumber defval) {
|
public LuaNumber optnumber(LuaNumber defval) {
|
||||||
return checknumber();
|
return checknumber().checknumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
public LuaString optstring(LuaString defval) {
|
public LuaString optstring(LuaString defval) {
|
||||||
|
|||||||
@@ -266,8 +266,8 @@ public class LuaTable extends LuaValue implements Metatable {
|
|||||||
|
|
||||||
/** caller must ensure key is not nil */
|
/** caller must ensure key is not nil */
|
||||||
public void set( LuaValue key, LuaValue value ) {
|
public void set( LuaValue key, LuaValue value ) {
|
||||||
if (key == null || !key.isvalidkey() && !metatag(NEWINDEX).isfunction())
|
if (!key.isvalidkey() && !metatag(NEWINDEX).isfunction())
|
||||||
throw new LuaError("value ('" + key + "') can not be used as a table index");
|
typerror("table index");
|
||||||
if ( m_metatable==null || ! rawget(key).isnil() || ! settable(this,key,value) )
|
if ( m_metatable==null || ! rawget(key).isnil() || ! settable(this,key,value) )
|
||||||
rawset(key, value);
|
rawset(key, value);
|
||||||
}
|
}
|
||||||
@@ -347,12 +347,7 @@ public class LuaTable extends LuaValue implements Metatable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int length() {
|
public int length() {
|
||||||
if (m_metatable != null) {
|
return m_metatable != null? len().toint(): rawlen();
|
||||||
LuaValue len = len();
|
|
||||||
if (!len.isint()) throw new LuaError("table length is not an integer: " + len);
|
|
||||||
return len.toint();
|
|
||||||
}
|
|
||||||
return rawlen();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public LuaValue len() {
|
public LuaValue len() {
|
||||||
@@ -395,7 +390,7 @@ public class LuaTable extends LuaValue implements Metatable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( hash.length == 0 )
|
if ( hash.length == 0 )
|
||||||
error( "invalid key to 'next' 1: " + key );
|
error( "invalid key to 'next'" );
|
||||||
i = hashSlot( key );
|
i = hashSlot( key );
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
for ( Slot slot = hash[i]; slot != null; slot = slot.rest() ) {
|
for ( Slot slot = hash[i]; slot != null; slot = slot.rest() ) {
|
||||||
@@ -409,7 +404,7 @@ public class LuaTable extends LuaValue implements Metatable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( !found ) {
|
if ( !found ) {
|
||||||
error( "invalid key to 'next' 2: " + key );
|
error( "invalid key to 'next'" );
|
||||||
}
|
}
|
||||||
i += 1+array.length;
|
i += 1+array.length;
|
||||||
}
|
}
|
||||||
@@ -785,7 +780,6 @@ public class LuaTable extends LuaValue implements Metatable {
|
|||||||
* @param comparator {@link LuaValue} to be called to compare elements.
|
* @param comparator {@link LuaValue} to be called to compare elements.
|
||||||
*/
|
*/
|
||||||
public void sort(LuaValue comparator) {
|
public void sort(LuaValue comparator) {
|
||||||
if (len().tolong() >= (long)Integer.MAX_VALUE) throw new LuaError("array too big: " + len().tolong());
|
|
||||||
if (m_metatable != null && m_metatable.useWeakValues()) {
|
if (m_metatable != null && m_metatable.useWeakValues()) {
|
||||||
dropWeakArrayValues();
|
dropWeakArrayValues();
|
||||||
}
|
}
|
||||||
@@ -898,11 +892,6 @@ public class LuaTable extends LuaValue implements Metatable {
|
|||||||
|
|
||||||
/** Unpack the elements from i to j inclusive */
|
/** Unpack the elements from i to j inclusive */
|
||||||
public Varargs unpack(int i, int j) {
|
public Varargs unpack(int i, int j) {
|
||||||
if (j < i) return NONE;
|
|
||||||
int count = j - i;
|
|
||||||
if (count < 0) throw new LuaError("too many results to unpack: greater " + Integer.MAX_VALUE); // integer overflow
|
|
||||||
int max = 0x00ffffff;
|
|
||||||
if (count >= max) throw new LuaError("too many results to unpack: " + count + " (max is " + max + ')');
|
|
||||||
int n = j + 1 - i;
|
int n = j + 1 - i;
|
||||||
switch (n) {
|
switch (n) {
|
||||||
case 0: return NONE;
|
case 0: return NONE;
|
||||||
@@ -911,14 +900,10 @@ public class LuaTable extends LuaValue implements Metatable {
|
|||||||
default:
|
default:
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
return NONE;
|
return NONE;
|
||||||
try {
|
LuaValue[] v = new LuaValue[n];
|
||||||
LuaValue[] v = new LuaValue[n];
|
while (--n >= 0)
|
||||||
while (--n >= 0)
|
v[n] = get(i+n);
|
||||||
v[n] = get(i+n);
|
return varargsOf(v);
|
||||||
return varargsOf(v);
|
|
||||||
} catch (OutOfMemoryError e) {
|
|
||||||
throw new LuaError("too many results to unpack [out of memory]: " + n);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1246,14 +1231,13 @@ public class LuaTable extends LuaValue implements Metatable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Entry set(LuaValue value) {
|
public Entry set(LuaValue value) {
|
||||||
if (value.type() == TNUMBER) {
|
LuaValue n = value.tonumber();
|
||||||
LuaValue n = value.tonumber();
|
if ( !n.isnil() ) {
|
||||||
if (!n.isnil()) {
|
this.value = n.todouble();
|
||||||
this.value = n.todouble();
|
return this;
|
||||||
return this;
|
} else {
|
||||||
}
|
return new NormalEntry( this.key, value );
|
||||||
}
|
}
|
||||||
return new NormalEntry( this.key, value );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int keyindex( int mask ) {
|
public int keyindex( int mask ) {
|
||||||
|
|||||||
@@ -643,7 +643,7 @@ public class LuaValue extends Varargs {
|
|||||||
* @see #isnumber()
|
* @see #isnumber()
|
||||||
* @see #TNUMBER
|
* @see #TNUMBER
|
||||||
*/
|
*/
|
||||||
public double optdouble(double defval) { argerror("number"); return 0; }
|
public double optdouble(double defval) { argerror("double"); return 0; }
|
||||||
|
|
||||||
/** Check that optional argument is a function and return as {@link LuaFunction}
|
/** Check that optional argument is a function and return as {@link LuaFunction}
|
||||||
* <p>
|
* <p>
|
||||||
@@ -855,7 +855,7 @@ public class LuaValue extends Varargs {
|
|||||||
* @see #optdouble(double)
|
* @see #optdouble(double)
|
||||||
* @see #TNUMBER
|
* @see #TNUMBER
|
||||||
*/
|
*/
|
||||||
public double checkdouble() { argerror("number"); return 0; }
|
public double checkdouble() { argerror("double"); return 0; }
|
||||||
|
|
||||||
/** Check that the value is a function , or throw {@link LuaError} if not
|
/** Check that the value is a function , or throw {@link LuaError} if not
|
||||||
* <p>
|
* <p>
|
||||||
@@ -2045,7 +2045,7 @@ public class LuaValue extends Varargs {
|
|||||||
* @see #eqmtcall(LuaValue, LuaValue, LuaValue, LuaValue)
|
* @see #eqmtcall(LuaValue, LuaValue, LuaValue, LuaValue)
|
||||||
* @see #EQ
|
* @see #EQ
|
||||||
*/
|
*/
|
||||||
public LuaValue eq( LuaValue val ) { return eq_b(val)? TRUE: FALSE; }
|
public LuaValue eq( LuaValue val ) { return this == val? TRUE: FALSE; }
|
||||||
|
|
||||||
/** Equals: Perform equality comparison with another value
|
/** Equals: Perform equality comparison with another value
|
||||||
* including metatag processing using {@link #EQ},
|
* including metatag processing using {@link #EQ},
|
||||||
@@ -3143,7 +3143,7 @@ public class LuaValue extends Varargs {
|
|||||||
* @return {@link LuaString} corresponding to the value if a string or number
|
* @return {@link LuaString} corresponding to the value if a string or number
|
||||||
* @throws LuaError if not a string or number
|
* @throws LuaError if not a string or number
|
||||||
*/
|
*/
|
||||||
public LuaString strvalue() { typerror("string or number"); return null; }
|
public LuaString strvalue() { typerror("strValue"); return null; }
|
||||||
|
|
||||||
/** Return this value as a strong reference, or null if it was weak and is no longer referenced.
|
/** Return this value as a strong reference, or null if it was weak and is no longer referenced.
|
||||||
* @return {@link LuaValue} referred to, or null if it was weak and is no longer referenced.
|
* @return {@link LuaValue} referred to, or null if it was weak and is no longer referenced.
|
||||||
@@ -3298,7 +3298,7 @@ public class LuaValue extends Varargs {
|
|||||||
if ((!res.isnil()) || (tm = t.metatag(INDEX)).isnil())
|
if ((!res.isnil()) || (tm = t.metatag(INDEX)).isnil())
|
||||||
return res;
|
return res;
|
||||||
} else if ((tm = t.metatag(INDEX)).isnil())
|
} else if ((tm = t.metatag(INDEX)).isnil())
|
||||||
t.indexerror(key.tojstring());
|
t.indexerror();
|
||||||
if (tm.isfunction())
|
if (tm.isfunction())
|
||||||
return tm.call(t, key);
|
return tm.call(t, key);
|
||||||
t = tm;
|
t = tm;
|
||||||
@@ -3326,7 +3326,7 @@ public class LuaValue extends Varargs {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else if ((tm = t.metatag(NEWINDEX)).isnil())
|
} else if ((tm = t.metatag(NEWINDEX)).isnil())
|
||||||
throw new LuaError("table expected for set index ('" + key + "') value, got " + t.typename());
|
t.typerror("index");
|
||||||
if (tm.isfunction()) {
|
if (tm.isfunction()) {
|
||||||
tm.call(t, key, value);
|
tm.call(t, key, value);
|
||||||
return true;
|
return true;
|
||||||
@@ -3389,8 +3389,8 @@ public class LuaValue extends Varargs {
|
|||||||
/** Throw {@link LuaError} indicating index was attempted on illegal type
|
/** Throw {@link LuaError} indicating index was attempted on illegal type
|
||||||
* @throws LuaError when called.
|
* @throws LuaError when called.
|
||||||
*/
|
*/
|
||||||
private void indexerror(String key) {
|
private void indexerror() {
|
||||||
error( "attempt to index ? (a "+typename()+" value) with key '" + key + "'" );
|
error( "attempt to index ? (a "+typename()+" value)" );
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Construct a {@link Varargs} around an array of {@link LuaValue}s.
|
/** Construct a {@link Varargs} around an array of {@link LuaValue}s.
|
||||||
|
|||||||
@@ -128,10 +128,6 @@ public class Print extends Lua {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void printValue( PrintStream ps, LuaValue v ) {
|
static void printValue( PrintStream ps, LuaValue v ) {
|
||||||
if (v == null) {
|
|
||||||
ps.print("null");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
switch ( v.type() ) {
|
switch ( v.type() ) {
|
||||||
case LuaValue.TSTRING: printString( ps, (LuaString) v ); break;
|
case LuaValue.TSTRING: printString( ps, (LuaString) v ); break;
|
||||||
default: ps.print( v.tojstring() );
|
default: ps.print( v.tojstring() );
|
||||||
@@ -140,7 +136,7 @@ public class Print extends Lua {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void printConstant(PrintStream ps, Prototype f, int i) {
|
static void printConstant(PrintStream ps, Prototype f, int i) {
|
||||||
printValue( ps, i < f.k.length ? f.k[i] : LuaValue.valueOf("UNKNOWN_CONST_" + i) );
|
printValue( ps, f.k[i] );
|
||||||
}
|
}
|
||||||
|
|
||||||
static void printUpvalue(PrintStream ps, Upvaldesc u) {
|
static void printUpvalue(PrintStream ps, Upvaldesc u) {
|
||||||
@@ -156,7 +152,7 @@ public class Print extends Lua {
|
|||||||
int[] code = f.code;
|
int[] code = f.code;
|
||||||
int pc, n = code.length;
|
int pc, n = code.length;
|
||||||
for (pc = 0; pc < n; pc++) {
|
for (pc = 0; pc < n; pc++) {
|
||||||
pc = printOpCode(f, pc);
|
printOpCode(f, pc);
|
||||||
ps.println();
|
ps.println();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -165,10 +161,9 @@ public class Print extends Lua {
|
|||||||
* Print an opcode in a prototype
|
* Print an opcode in a prototype
|
||||||
* @param f the {@link Prototype}
|
* @param f the {@link Prototype}
|
||||||
* @param pc the program counter to look up and print
|
* @param pc the program counter to look up and print
|
||||||
* @return pc same as above or changed
|
|
||||||
*/
|
*/
|
||||||
public static int printOpCode(Prototype f, int pc) {
|
public static void printOpCode(Prototype f, int pc) {
|
||||||
return printOpCode(ps,f,pc);
|
printOpCode(ps,f,pc);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -176,9 +171,8 @@ public class Print extends Lua {
|
|||||||
* @param ps the {@link PrintStream} to print to
|
* @param ps the {@link PrintStream} to print to
|
||||||
* @param f the {@link Prototype}
|
* @param f the {@link Prototype}
|
||||||
* @param pc the program counter to look up and print
|
* @param pc the program counter to look up and print
|
||||||
* @return pc same as above or changed
|
|
||||||
*/
|
*/
|
||||||
public static int printOpCode(PrintStream ps, Prototype f, int pc) {
|
public static void printOpCode(PrintStream ps, Prototype f, int pc) {
|
||||||
int[] code = f.code;
|
int[] code = f.code;
|
||||||
int i = code[pc];
|
int i = code[pc];
|
||||||
int o = GET_OPCODE(i);
|
int o = GET_OPCODE(i);
|
||||||
@@ -193,67 +187,80 @@ public class Print extends Lua {
|
|||||||
ps.print("[" + line + "] ");
|
ps.print("[" + line + "] ");
|
||||||
else
|
else
|
||||||
ps.print("[-] ");
|
ps.print("[-] ");
|
||||||
if (o >= OPNAMES.length - 1) {
|
ps.print(OPNAMES[o] + " ");
|
||||||
ps.print("UNKNOWN_OP_" + o + " ");
|
switch (getOpMode(o)) {
|
||||||
} else {
|
case iABC:
|
||||||
ps.print(OPNAMES[o] + " ");
|
ps.print( a );
|
||||||
switch (getOpMode(o)) {
|
if (getBMode(o) != OpArgN)
|
||||||
case iABC:
|
ps.print(" "+(ISK(b) ? (-1 - INDEXK(b)) : b));
|
||||||
ps.print( a );
|
if (getCMode(o) != OpArgN)
|
||||||
if (getBMode(o) != OpArgN)
|
ps.print(" "+(ISK(c) ? (-1 - INDEXK(c)) : c));
|
||||||
ps.print(" "+(ISK(b) ? (-1 - INDEXK(b)) : b));
|
break;
|
||||||
if (getCMode(o) != OpArgN)
|
case iABx:
|
||||||
ps.print(" "+(ISK(c) ? (-1 - INDEXK(c)) : c));
|
if (getBMode(o) == OpArgK) {
|
||||||
break;
|
ps.print(a + " " + (-1 - bx));
|
||||||
case iABx:
|
} else {
|
||||||
if (getBMode(o) == OpArgK) {
|
ps.print(a + " " + (bx));
|
||||||
ps.print(a + " " + (-1 - bx));
|
|
||||||
} else {
|
|
||||||
ps.print(a + " " + (bx));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case iAsBx:
|
|
||||||
if (o == OP_JMP)
|
|
||||||
ps.print( sbx );
|
|
||||||
else
|
|
||||||
ps.print(a + " " + sbx);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
switch (o) {
|
break;
|
||||||
case OP_LOADK:
|
case iAsBx:
|
||||||
|
if (o == OP_JMP)
|
||||||
|
ps.print( sbx );
|
||||||
|
else
|
||||||
|
ps.print(a + " " + sbx);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
switch (o) {
|
||||||
|
case OP_LOADK:
|
||||||
|
ps.print(" ; ");
|
||||||
|
printConstant(ps, f, bx);
|
||||||
|
break;
|
||||||
|
case OP_GETUPVAL:
|
||||||
|
case OP_SETUPVAL:
|
||||||
|
ps.print(" ; ");
|
||||||
|
printUpvalue(ps, f.upvalues[b]);
|
||||||
|
break;
|
||||||
|
case OP_GETTABUP:
|
||||||
|
ps.print(" ; ");
|
||||||
|
printUpvalue(ps, f.upvalues[b]);
|
||||||
|
ps.print(" ");
|
||||||
|
if (ISK(c))
|
||||||
|
printConstant(ps, f, INDEXK(c));
|
||||||
|
else
|
||||||
|
ps.print("-");
|
||||||
|
break;
|
||||||
|
case OP_SETTABUP:
|
||||||
|
ps.print(" ; ");
|
||||||
|
printUpvalue(ps, f.upvalues[a]);
|
||||||
|
ps.print(" ");
|
||||||
|
if (ISK(b))
|
||||||
|
printConstant(ps, f, INDEXK(b));
|
||||||
|
else
|
||||||
|
ps.print("-");
|
||||||
|
ps.print(" ");
|
||||||
|
if (ISK(c))
|
||||||
|
printConstant(ps, f, INDEXK(c));
|
||||||
|
else
|
||||||
|
ps.print("-");
|
||||||
|
break;
|
||||||
|
case OP_GETTABLE:
|
||||||
|
case OP_SELF:
|
||||||
|
if (ISK(c)) {
|
||||||
ps.print(" ; ");
|
ps.print(" ; ");
|
||||||
printConstant(ps, f, bx);
|
printConstant(ps, f, INDEXK(c));
|
||||||
break;
|
}
|
||||||
case OP_GETUPVAL:
|
break;
|
||||||
case OP_SETUPVAL:
|
case OP_SETTABLE:
|
||||||
|
case OP_ADD:
|
||||||
|
case OP_SUB:
|
||||||
|
case OP_MUL:
|
||||||
|
case OP_DIV:
|
||||||
|
case OP_POW:
|
||||||
|
case OP_EQ:
|
||||||
|
case OP_LT:
|
||||||
|
case OP_LE:
|
||||||
|
if (ISK(b) || ISK(c)) {
|
||||||
ps.print(" ; ");
|
ps.print(" ; ");
|
||||||
if (b < f.upvalues.length) {
|
|
||||||
printUpvalue(ps, f.upvalues[b]);
|
|
||||||
} else {
|
|
||||||
ps.print("UNKNOWN_UPVALUE_" + b);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case OP_GETTABUP:
|
|
||||||
ps.print(" ; ");
|
|
||||||
if (b < f.upvalues.length) {
|
|
||||||
printUpvalue(ps, f.upvalues[b]);
|
|
||||||
} else {
|
|
||||||
ps.print("UNKNOWN_UPVALUE_" + b);
|
|
||||||
}
|
|
||||||
ps.print(" ");
|
|
||||||
if (ISK(c))
|
|
||||||
printConstant(ps, f, INDEXK(c));
|
|
||||||
else
|
|
||||||
ps.print("-");
|
|
||||||
break;
|
|
||||||
case OP_SETTABUP:
|
|
||||||
ps.print(" ; ");
|
|
||||||
if (a < f.upvalues.length) {
|
|
||||||
printUpvalue(ps, f.upvalues[a]);
|
|
||||||
} else {
|
|
||||||
ps.print("UNKNOWN_UPVALUE_" + a);
|
|
||||||
}
|
|
||||||
ps.print(" ");
|
|
||||||
if (ISK(b))
|
if (ISK(b))
|
||||||
printConstant(ps, f, INDEXK(b));
|
printConstant(ps, f, INDEXK(b));
|
||||||
else
|
else
|
||||||
@@ -263,62 +270,28 @@ public class Print extends Lua {
|
|||||||
printConstant(ps, f, INDEXK(c));
|
printConstant(ps, f, INDEXK(c));
|
||||||
else
|
else
|
||||||
ps.print("-");
|
ps.print("-");
|
||||||
break;
|
|
||||||
case OP_GETTABLE:
|
|
||||||
case OP_SELF:
|
|
||||||
if (ISK(c)) {
|
|
||||||
ps.print(" ; ");
|
|
||||||
printConstant(ps, f, INDEXK(c));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case OP_SETTABLE:
|
|
||||||
case OP_ADD:
|
|
||||||
case OP_SUB:
|
|
||||||
case OP_MUL:
|
|
||||||
case OP_DIV:
|
|
||||||
case OP_POW:
|
|
||||||
case OP_EQ:
|
|
||||||
case OP_LT:
|
|
||||||
case OP_LE:
|
|
||||||
if (ISK(b) || ISK(c)) {
|
|
||||||
ps.print(" ; ");
|
|
||||||
if (ISK(b))
|
|
||||||
printConstant(ps, f, INDEXK(b));
|
|
||||||
else
|
|
||||||
ps.print("-");
|
|
||||||
ps.print(" ");
|
|
||||||
if (ISK(c))
|
|
||||||
printConstant(ps, f, INDEXK(c));
|
|
||||||
else
|
|
||||||
ps.print("-");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case OP_JMP:
|
|
||||||
case OP_FORLOOP:
|
|
||||||
case OP_FORPREP:
|
|
||||||
ps.print(" ; to " + (sbx + pc + 2));
|
|
||||||
break;
|
|
||||||
case OP_CLOSURE:
|
|
||||||
if (bx < f.p.length) {
|
|
||||||
ps.print(" ; " + f.p[bx].getClass().getName());
|
|
||||||
} else {
|
|
||||||
ps.print(" ; UNKNOWN_PROTYPE_" + bx);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case OP_SETLIST:
|
|
||||||
if (c == 0)
|
|
||||||
ps.print(" ; " + ((int) code[++pc]) + " (stored in the next OP)");
|
|
||||||
else
|
|
||||||
ps.print(" ; " + ((int) c));
|
|
||||||
break;
|
|
||||||
case OP_VARARG:
|
|
||||||
ps.print( " ; is_vararg="+ f.is_vararg );
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case OP_JMP:
|
||||||
|
case OP_FORLOOP:
|
||||||
|
case OP_FORPREP:
|
||||||
|
ps.print(" ; to " + (sbx + pc + 2));
|
||||||
|
break;
|
||||||
|
case OP_CLOSURE:
|
||||||
|
ps.print(" ; " + f.p[bx].getClass().getName());
|
||||||
|
break;
|
||||||
|
case OP_SETLIST:
|
||||||
|
if (c == 0)
|
||||||
|
ps.print(" ; " + ((int) code[++pc]));
|
||||||
|
else
|
||||||
|
ps.print(" ; " + ((int) c));
|
||||||
|
break;
|
||||||
|
case OP_VARARG:
|
||||||
|
ps.print( " ; is_vararg="+ f.is_vararg );
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return pc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int getline(Prototype f, int pc) {
|
private static int getline(Prototype f, int pc) {
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ public class LuaC extends Constants implements Globals.Compiler, Globals.Loader
|
|||||||
protected CompileState() {}
|
protected CompileState() {}
|
||||||
|
|
||||||
/** Parse the input */
|
/** Parse the input */
|
||||||
Prototype luaY_parser(InputStream z, String name) throws IOException{
|
private Prototype luaY_parser(InputStream z, String name) throws IOException{
|
||||||
LexState lexstate = new LexState(this, z);
|
LexState lexstate = new LexState(this, z);
|
||||||
FuncState funcstate = new FuncState();
|
FuncState funcstate = new FuncState();
|
||||||
// lexstate.buff = buff;
|
// lexstate.buff = buff;
|
||||||
|
|||||||
@@ -245,7 +245,7 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
|
|||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaValue tostring = globals.get("tostring");
|
LuaValue tostring = globals.get("tostring");
|
||||||
for ( int i=1, n=args.narg(); i<=n; i++ ) {
|
for ( int i=1, n=args.narg(); i<=n; i++ ) {
|
||||||
if ( i>1 ) globals.STDOUT.print( " \t" );
|
if ( i>1 ) globals.STDOUT.print( '\t' );
|
||||||
LuaString s = tostring.call( args.arg(i) ).strvalue();
|
LuaString s = tostring.call( args.arg(i) ).strvalue();
|
||||||
globals.STDOUT.print(s.tojstring());
|
globals.STDOUT.print(s.tojstring());
|
||||||
}
|
}
|
||||||
@@ -299,8 +299,7 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
|
|||||||
}
|
}
|
||||||
public LuaValue call(LuaValue table, LuaValue index, LuaValue value) {
|
public LuaValue call(LuaValue table, LuaValue index, LuaValue value) {
|
||||||
LuaTable t = table.checktable();
|
LuaTable t = table.checktable();
|
||||||
if (!index.isvalidkey()) argerror(2, "value");
|
t.rawset(index.checknotnil(), value);
|
||||||
t.rawset(index, value);
|
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,15 +27,15 @@ import org.luaj.vm2.Varargs;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Subclass of LibFunction that implements the Lua standard {@code bit32} library.
|
* Subclass of LibFunction that implements the Lua standard {@code bit32} library.
|
||||||
* <p>
|
* <p>
|
||||||
* Typically, this library is included as part of a call to either
|
* Typically, this library is included as part of a call to either
|
||||||
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = JsePlatform.standardGlobals();
|
* Globals globals = JsePlatform.standardGlobals();
|
||||||
* System.out.println( globals.get("bit32").get("bnot").call( LuaValue.valueOf(2) ) );
|
* System.out.println( globals.get("bit32").get("bnot").call( LuaValue.valueOf(2) ) );
|
||||||
* } </pre>
|
* } </pre>
|
||||||
* <p>
|
* <p>
|
||||||
* To instantiate and use it directly,
|
* To instantiate and use it directly,
|
||||||
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = new Globals();
|
* Globals globals = new Globals();
|
||||||
@@ -71,7 +71,7 @@ public class Bit32Lib extends TwoArgFunction {
|
|||||||
"arshift", "lrotate", "lshift", "rrotate", "rshift"
|
"arshift", "lrotate", "lshift", "rrotate", "rshift"
|
||||||
});
|
});
|
||||||
env.set("bit32", t);
|
env.set("bit32", t);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("bit32", t);
|
env.get("package").get("loaded").set("bit32", t);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,6 +219,6 @@ public class Bit32Lib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static LuaValue bitsToValue( int x ) {
|
private static LuaValue bitsToValue( int x ) {
|
||||||
return ( x < 0 ) ? valueOf((double) ((long) x & 0xFFFFFFFFL)) : valueOf(x);
|
return ( x < 0 ) ? valueOf((double) ((long) x & 0xFFFFFFFFL)) : valueOf(x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,25 +27,25 @@ import org.luaj.vm2.LuaThread;
|
|||||||
import org.luaj.vm2.LuaValue;
|
import org.luaj.vm2.LuaValue;
|
||||||
import org.luaj.vm2.Varargs;
|
import org.luaj.vm2.Varargs;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subclass of {@link LibFunction} which implements the lua standard {@code coroutine}
|
* Subclass of {@link LibFunction} which implements the lua standard {@code coroutine}
|
||||||
* library.
|
* library.
|
||||||
* <p>
|
* <p>
|
||||||
* The coroutine library in luaj has the same behavior as the
|
* The coroutine library in luaj has the same behavior as the
|
||||||
* coroutine library in C, but is implemented using Java Threads to maintain
|
* coroutine library in C, but is implemented using Java Threads to maintain
|
||||||
* the call state between invocations. Therefore it can be yielded from anywhere,
|
* the call state between invocations. Therefore it can be yielded from anywhere,
|
||||||
* similar to the "Coco" yield-from-anywhere patch available for C-based lua.
|
* similar to the "Coco" yield-from-anywhere patch available for C-based lua.
|
||||||
* However, coroutines that are yielded but never resumed to complete their execution
|
* However, coroutines that are yielded but never resumed to complete their execution
|
||||||
* may not be collected by the garbage collector.
|
* may not be collected by the garbage collector.
|
||||||
* <p>
|
* <p>
|
||||||
* Typically, this library is included as part of a call to either
|
* Typically, this library is included as part of a call to either
|
||||||
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = JsePlatform.standardGlobals();
|
* Globals globals = JsePlatform.standardGlobals();
|
||||||
* System.out.println( globals.get("coroutine").get("running").call() );
|
* System.out.println( globals.get("coroutine").get("running").call() );
|
||||||
* } </pre>
|
* } </pre>
|
||||||
* <p>
|
* <p>
|
||||||
* To instantiate and use it directly,
|
* To instantiate and use it directly,
|
||||||
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = new Globals();
|
* Globals globals = new Globals();
|
||||||
@@ -82,7 +82,7 @@ public class CoroutineLib extends TwoArgFunction {
|
|||||||
coroutine.set("yield", new yield());
|
coroutine.set("yield", new yield());
|
||||||
coroutine.set("wrap", new wrap());
|
coroutine.set("wrap", new wrap());
|
||||||
env.set("coroutine", coroutine);
|
env.set("coroutine", coroutine);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("coroutine", coroutine);
|
env.get("package").get("loaded").set("coroutine", coroutine);
|
||||||
return coroutine;
|
return coroutine;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,7 +92,7 @@ public class CoroutineLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static final class resume extends VarArgFunction {
|
final class resume extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
final LuaThread t = args.checkthread(1);
|
final LuaThread t = args.checkthread(1);
|
||||||
return t.resume( args.subargs(2) );
|
return t.resume( args.subargs(2) );
|
||||||
@@ -127,7 +127,7 @@ public class CoroutineLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static final class wrapper extends VarArgFunction {
|
final class wrapper extends VarArgFunction {
|
||||||
final LuaThread luathread;
|
final LuaThread luathread;
|
||||||
wrapper(LuaThread luathread) {
|
wrapper(LuaThread luathread) {
|
||||||
this.luathread = luathread;
|
this.luathread = luathread;
|
||||||
|
|||||||
@@ -38,26 +38,26 @@ import org.luaj.vm2.Print;
|
|||||||
import org.luaj.vm2.Prototype;
|
import org.luaj.vm2.Prototype;
|
||||||
import org.luaj.vm2.Varargs;
|
import org.luaj.vm2.Varargs;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subclass of {@link LibFunction} which implements the lua standard {@code debug}
|
* Subclass of {@link LibFunction} which implements the lua standard {@code debug}
|
||||||
* library.
|
* library.
|
||||||
* <p>
|
* <p>
|
||||||
* The debug library in luaj tries to emulate the behavior of the corresponding C-based lua library.
|
* The debug library in luaj tries to emulate the behavior of the corresponding C-based lua library.
|
||||||
* To do this, it must maintain a separate stack of calls to {@link LuaClosure} and {@link LibFunction}
|
* To do this, it must maintain a separate stack of calls to {@link LuaClosure} and {@link LibFunction}
|
||||||
* instances.
|
* instances.
|
||||||
* Especially when lua-to-java bytecode compiling is being used
|
* Especially when lua-to-java bytecode compiling is being used
|
||||||
* via a {@link org.luaj.vm2.Globals.Compiler} such as {@link org.luaj.vm2.luajc.LuaJC},
|
* via a {@link org.luaj.vm2.Globals.Compiler} such as {@link org.luaj.vm2.luajc.LuaJC},
|
||||||
* this cannot be done in all cases.
|
* this cannot be done in all cases.
|
||||||
* <p>
|
* <p>
|
||||||
* Typically, this library is included as part of a call to either
|
* Typically, this library is included as part of a call to either
|
||||||
* {@link org.luaj.vm2.lib.jse.JsePlatform#debugGlobals()} or
|
* {@link org.luaj.vm2.lib.jse.JsePlatform#debugGlobals()} or
|
||||||
* {@link org.luaj.vm2.lib.jme.JmePlatform#debugGlobals()}
|
* {@link org.luaj.vm2.lib.jme.JmePlatform#debugGlobals()}
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = JsePlatform.debugGlobals();
|
* Globals globals = JsePlatform.debugGlobals();
|
||||||
* System.out.println( globals.get("debug").get("traceback").call() );
|
* System.out.println( globals.get("debug").get("traceback").call() );
|
||||||
* } </pre>
|
* } </pre>
|
||||||
* <p>
|
* <p>
|
||||||
* To instantiate and use it directly,
|
* To instantiate and use it directly,
|
||||||
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = new Globals();
|
* Globals globals = new Globals();
|
||||||
@@ -84,27 +84,27 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
try { TRACE = (null != System.getProperty("TRACE")); } catch (Exception e) {}
|
try { TRACE = (null != System.getProperty("TRACE")); } catch (Exception e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
static final LuaString LUA = valueOf("Lua");
|
private static final LuaString LUA = valueOf("Lua");
|
||||||
private static final LuaString QMARK = valueOf("?");
|
private static final LuaString QMARK = valueOf("?");
|
||||||
private static final LuaString CALL = valueOf("call");
|
private static final LuaString CALL = valueOf("call");
|
||||||
private static final LuaString LINE = valueOf("line");
|
private static final LuaString LINE = valueOf("line");
|
||||||
private static final LuaString COUNT = valueOf("count");
|
private static final LuaString COUNT = valueOf("count");
|
||||||
private static final LuaString RETURN = valueOf("return");
|
private static final LuaString RETURN = valueOf("return");
|
||||||
|
|
||||||
static final LuaString FUNC = valueOf("func");
|
private static final LuaString FUNC = valueOf("func");
|
||||||
static final LuaString ISTAILCALL = valueOf("istailcall");
|
private static final LuaString ISTAILCALL = valueOf("istailcall");
|
||||||
static final LuaString ISVARARG = valueOf("isvararg");
|
private static final LuaString ISVARARG = valueOf("isvararg");
|
||||||
static final LuaString NUPS = valueOf("nups");
|
private static final LuaString NUPS = valueOf("nups");
|
||||||
static final LuaString NPARAMS = valueOf("nparams");
|
private static final LuaString NPARAMS = valueOf("nparams");
|
||||||
static final LuaString NAME = valueOf("name");
|
private static final LuaString NAME = valueOf("name");
|
||||||
static final LuaString NAMEWHAT = valueOf("namewhat");
|
private static final LuaString NAMEWHAT = valueOf("namewhat");
|
||||||
static final LuaString WHAT = valueOf("what");
|
private static final LuaString WHAT = valueOf("what");
|
||||||
static final LuaString SOURCE = valueOf("source");
|
private static final LuaString SOURCE = valueOf("source");
|
||||||
static final LuaString SHORT_SRC = valueOf("short_src");
|
private static final LuaString SHORT_SRC = valueOf("short_src");
|
||||||
static final LuaString LINEDEFINED = valueOf("linedefined");
|
private static final LuaString LINEDEFINED = valueOf("linedefined");
|
||||||
static final LuaString LASTLINEDEFINED = valueOf("lastlinedefined");
|
private static final LuaString LASTLINEDEFINED = valueOf("lastlinedefined");
|
||||||
static final LuaString CURRENTLINE = valueOf("currentline");
|
private static final LuaString CURRENTLINE = valueOf("currentline");
|
||||||
static final LuaString ACTIVELINES = valueOf("activelines");
|
private static final LuaString ACTIVELINES = valueOf("activelines");
|
||||||
|
|
||||||
Globals globals;
|
Globals globals;
|
||||||
|
|
||||||
@@ -135,19 +135,19 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
debug.set("upvalueid", new upvalueid());
|
debug.set("upvalueid", new upvalueid());
|
||||||
debug.set("upvaluejoin", new upvaluejoin());
|
debug.set("upvaluejoin", new upvaluejoin());
|
||||||
env.set("debug", debug);
|
env.set("debug", debug);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("debug", debug);
|
env.get("package").get("loaded").set("debug", debug);
|
||||||
return debug;
|
return debug;
|
||||||
}
|
}
|
||||||
|
|
||||||
// debug.debug()
|
// debug.debug()
|
||||||
static final class debug extends ZeroArgFunction {
|
static final class debug extends ZeroArgFunction {
|
||||||
public LuaValue call() {
|
public LuaValue call() {
|
||||||
return NONE;
|
return NONE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// debug.gethook ([thread])
|
// debug.gethook ([thread])
|
||||||
final class gethook extends VarArgFunction {
|
final class gethook extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaThread t = args.narg() > 0 ? args.checkthread(1): globals.running;
|
LuaThread t = args.narg() > 0 ? args.checkthread(1): globals.running;
|
||||||
LuaThread.State s = t.state;
|
LuaThread.State s = t.state;
|
||||||
@@ -159,10 +159,10 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// debug.getinfo ([thread,] f [, what])
|
// debug.getinfo ([thread,] f [, what])
|
||||||
final class getinfo extends VarArgFunction {
|
final class getinfo extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
int a=1;
|
int a=1;
|
||||||
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running;
|
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running;
|
||||||
LuaValue func = args.arg(a++);
|
LuaValue func = args.arg(a++);
|
||||||
String what = args.optjstring(a++, "flnStu");
|
String what = args.optjstring(a++, "flnStu");
|
||||||
DebugLib.CallStack callstack = callstack(thread);
|
DebugLib.CallStack callstack = callstack(thread);
|
||||||
@@ -222,10 +222,10 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// debug.getlocal ([thread,] f, local)
|
// debug.getlocal ([thread,] f, local)
|
||||||
final class getlocal extends VarArgFunction {
|
final class getlocal extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
int a=1;
|
int a=1;
|
||||||
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running;
|
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running;
|
||||||
int level = args.checkint(a++);
|
int level = args.checkint(a++);
|
||||||
int local = args.checkint(a++);
|
int local = args.checkint(a++);
|
||||||
CallFrame f = callstack(thread).getCallFrame(level);
|
CallFrame f = callstack(thread).getCallFrame(level);
|
||||||
@@ -234,7 +234,7 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// debug.getmetatable (value)
|
// debug.getmetatable (value)
|
||||||
static final class getmetatable extends LibFunction {
|
final class getmetatable extends LibFunction {
|
||||||
public LuaValue call(LuaValue v) {
|
public LuaValue call(LuaValue v) {
|
||||||
LuaValue mt = v.getmetatable();
|
LuaValue mt = v.getmetatable();
|
||||||
return mt != null? mt: NIL;
|
return mt != null? mt: NIL;
|
||||||
@@ -249,7 +249,7 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// debug.getupvalue (f, up)
|
// debug.getupvalue (f, up)
|
||||||
static final class getupvalue extends VarArgFunction {
|
static final class getupvalue extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaValue func = args.checkfunction(1);
|
LuaValue func = args.checkfunction(1);
|
||||||
int up = args.checkint(2);
|
int up = args.checkint(2);
|
||||||
@@ -265,7 +265,7 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// debug.getuservalue (u)
|
// debug.getuservalue (u)
|
||||||
static final class getuservalue extends LibFunction {
|
static final class getuservalue extends LibFunction {
|
||||||
public LuaValue call(LuaValue u) {
|
public LuaValue call(LuaValue u) {
|
||||||
return u.isuserdata()? u: NIL;
|
return u.isuserdata()? u: NIL;
|
||||||
}
|
}
|
||||||
@@ -273,10 +273,10 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
|
|
||||||
|
|
||||||
// debug.sethook ([thread,] hook, mask [, count])
|
// debug.sethook ([thread,] hook, mask [, count])
|
||||||
final class sethook extends VarArgFunction {
|
final class sethook extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
int a=1;
|
int a=1;
|
||||||
LuaThread t = args.isthread(a)? args.checkthread(a++): globals.running;
|
LuaThread t = args.isthread(a)? args.checkthread(a++): globals.running;
|
||||||
LuaValue func = args.optfunction(a++, null);
|
LuaValue func = args.optfunction(a++, null);
|
||||||
String str = args.optjstring(a++,"");
|
String str = args.optjstring(a++,"");
|
||||||
int count = args.optint(a++,0);
|
int count = args.optint(a++,0);
|
||||||
@@ -298,20 +298,20 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// debug.setlocal ([thread,] level, local, value)
|
// debug.setlocal ([thread,] level, local, value)
|
||||||
final class setlocal extends VarArgFunction {
|
final class setlocal extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
int a=1;
|
int a=1;
|
||||||
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running;
|
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running;
|
||||||
int level = args.checkint(a++);
|
int level = args.checkint(a++);
|
||||||
int local = args.checkint(a++);
|
int local = args.checkint(a++);
|
||||||
LuaValue value = args.arg(a++);
|
LuaValue value = args.arg(a++);
|
||||||
CallFrame f = callstack(thread).getCallFrame(level);
|
CallFrame f = callstack(thread).getCallFrame(level);
|
||||||
return f != null? f.setLocal(local, value): NONE;
|
return f != null? f.setLocal(local, value): NONE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// debug.setmetatable (value, table)
|
// debug.setmetatable (value, table)
|
||||||
static final class setmetatable extends TwoArgFunction {
|
final class setmetatable extends TwoArgFunction {
|
||||||
public LuaValue call(LuaValue value, LuaValue table) {
|
public LuaValue call(LuaValue value, LuaValue table) {
|
||||||
LuaValue mt = table.opttable(null);
|
LuaValue mt = table.opttable(null);
|
||||||
switch ( value.type() ) {
|
switch ( value.type() ) {
|
||||||
@@ -328,7 +328,7 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// debug.setupvalue (f, up, value)
|
// debug.setupvalue (f, up, value)
|
||||||
static final class setupvalue extends VarArgFunction {
|
final class setupvalue extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaValue func = args.checkfunction(1);
|
LuaValue func = args.checkfunction(1);
|
||||||
int up = args.checkint(2);
|
int up = args.checkint(2);
|
||||||
@@ -346,7 +346,7 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// debug.setuservalue (udata, value)
|
// debug.setuservalue (udata, value)
|
||||||
static final class setuservalue extends VarArgFunction {
|
final class setuservalue extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
Object o = args.checkuserdata(1);
|
Object o = args.checkuserdata(1);
|
||||||
LuaValue v = args.checkvalue(2);
|
LuaValue v = args.checkvalue(2);
|
||||||
@@ -358,10 +358,10 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// debug.traceback ([thread,] [message [, level]])
|
// debug.traceback ([thread,] [message [, level]])
|
||||||
final class traceback extends VarArgFunction {
|
final class traceback extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
int a=1;
|
int a=1;
|
||||||
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running;
|
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running;
|
||||||
String message = args.optjstring(a++, null);
|
String message = args.optjstring(a++, null);
|
||||||
int level = args.optint(a++,1);
|
int level = args.optint(a++,1);
|
||||||
String tb = callstack(thread).traceback(level);
|
String tb = callstack(thread).traceback(level);
|
||||||
@@ -370,7 +370,7 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// debug.upvalueid (f, n)
|
// debug.upvalueid (f, n)
|
||||||
static final class upvalueid extends VarArgFunction {
|
final class upvalueid extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaValue func = args.checkfunction(1);
|
LuaValue func = args.checkfunction(1);
|
||||||
int up = args.checkint(2);
|
int up = args.checkint(2);
|
||||||
@@ -385,7 +385,7 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// debug.upvaluejoin (f1, n1, f2, n2)
|
// debug.upvaluejoin (f1, n1, f2, n2)
|
||||||
static final class upvaluejoin extends VarArgFunction {
|
final class upvaluejoin extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaClosure f1 = args.checkclosure(1);
|
LuaClosure f1 = args.checkclosure(1);
|
||||||
int n1 = args.checkint(2);
|
int n1 = args.checkint(2);
|
||||||
@@ -565,11 +565,7 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
sb.append( ar.name );
|
sb.append( ar.name );
|
||||||
sb.append( '\'' );
|
sb.append( '\'' );
|
||||||
} else {
|
} else {
|
||||||
sb.append( "function <" );
|
sb.append( "function <"+c.shortsource()+":"+c.linedefined()+">" );
|
||||||
sb.append( c.shortsource() );
|
|
||||||
sb.append( ':' );
|
|
||||||
sb.append( c.linedefined() );
|
|
||||||
sb.append( '>' );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sb.append("\n\t[Java]: in ?");
|
sb.append("\n\t[Java]: in ?");
|
||||||
@@ -587,7 +583,7 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
if (frame[calls-i].f == func)
|
if (frame[calls-i].f == func)
|
||||||
return frame[i];
|
return frame[i];
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
synchronized DebugInfo auxgetinfo(String what, LuaFunction f, CallFrame ci) {
|
synchronized DebugInfo auxgetinfo(String what, LuaFunction f, CallFrame ci) {
|
||||||
@@ -641,7 +637,7 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ar;
|
return ar;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -677,16 +673,16 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
Varargs getLocal(int i) {
|
Varargs getLocal(int i) {
|
||||||
LuaString name = getlocalname(i);
|
LuaString name = getlocalname(i);
|
||||||
if ( i >= 1 && i <= stack.length && stack[i-1] != null )
|
if ( name != null )
|
||||||
return varargsOf( name == null ? NIL : name, stack[i-1] );
|
return varargsOf( name, stack[i-1] );
|
||||||
else
|
else
|
||||||
return NIL;
|
return NIL;
|
||||||
}
|
}
|
||||||
Varargs setLocal(int i, LuaValue value) {
|
Varargs setLocal(int i, LuaValue value) {
|
||||||
LuaString name = getlocalname(i);
|
LuaString name = getlocalname(i);
|
||||||
if ( i >= 1 && i <= stack.length && stack[i-1] != null ) {
|
if ( name != null ) {
|
||||||
stack[i-1] = value;
|
stack[i-1] = value;
|
||||||
return name == null ? NIL : name;
|
return name;
|
||||||
} else {
|
} else {
|
||||||
return NIL;
|
return NIL;
|
||||||
}
|
}
|
||||||
@@ -694,13 +690,13 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
int currentline() {
|
int currentline() {
|
||||||
if ( !f.isclosure() ) return -1;
|
if ( !f.isclosure() ) return -1;
|
||||||
int[] li = f.checkclosure().p.lineinfo;
|
int[] li = f.checkclosure().p.lineinfo;
|
||||||
return li==null || pc<0 || pc>=li.length? -1: li[pc];
|
return li==null || pc<0 || pc>=li.length? -1: li[pc];
|
||||||
}
|
}
|
||||||
String sourceline() {
|
String sourceline() {
|
||||||
if ( !f.isclosure() ) return f.tojstring();
|
if ( !f.isclosure() ) return f.tojstring();
|
||||||
return f.checkclosure().p.shortsource() + ":" + currentline();
|
return f.checkclosure().p.shortsource() + ":" + currentline();
|
||||||
}
|
}
|
||||||
int linedefined() {
|
private int linedefined() {
|
||||||
return f.isclosure()? f.checkclosure().p.linedefined: -1;
|
return f.isclosure()? f.checkclosure().p.linedefined: -1;
|
||||||
}
|
}
|
||||||
LuaString getlocalname(int index) {
|
LuaString getlocalname(int index) {
|
||||||
@@ -721,7 +717,7 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
|
|
||||||
static void lua_assert(boolean x) {
|
static void lua_assert(boolean x) {
|
||||||
if (!x) throw new RuntimeException("lua_assert failed");
|
if (!x) throw new RuntimeException("lua_assert failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
static class NameWhat {
|
static class NameWhat {
|
||||||
final String name;
|
final String name;
|
||||||
@@ -871,10 +867,6 @@ public class DebugLib extends TwoArgFunction {
|
|||||||
if (reg == a) setreg = pc; /* jumped code can change 'a' */
|
if (reg == a) setreg = pc; /* jumped code can change 'a' */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Lua.OP_SETLIST: { // Lua.testAMode(Lua.OP_SETLIST) == false
|
|
||||||
if ( ((i>>14)&0x1ff) == 0 ) pc++; // if c == 0 then c stored in next op -> skip
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
if (Lua.testAMode(op) && reg == a) /* any instruction that set A */
|
if (Lua.testAMode(op) && reg == a) /* any instruction that set A */
|
||||||
setreg = pc;
|
setreg = pc;
|
||||||
|
|||||||
@@ -31,31 +31,31 @@ import org.luaj.vm2.LuaTable;
|
|||||||
import org.luaj.vm2.LuaValue;
|
import org.luaj.vm2.LuaValue;
|
||||||
import org.luaj.vm2.Varargs;
|
import org.luaj.vm2.Varargs;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract base class extending {@link LibFunction} which implements the
|
* Abstract base class extending {@link LibFunction} which implements the
|
||||||
* core of the lua standard {@code io} library.
|
* core of the lua standard {@code io} library.
|
||||||
* <p>
|
* <p>
|
||||||
* It contains the implementation of the io library support that is common to
|
* It contains the implementation of the io library support that is common to
|
||||||
* the JSE and JME platforms.
|
* the JSE and JME platforms.
|
||||||
* In practice on of the concrete IOLib subclasses is chosen:
|
* In practice on of the concrete IOLib subclasses is chosen:
|
||||||
* {@link org.luaj.vm2.lib.jse.JseIoLib} for the JSE platform, and
|
* {@link org.luaj.vm2.lib.jse.JseIoLib} for the JSE platform, and
|
||||||
* {@link org.luaj.vm2.lib.jme.JmeIoLib} for the JME platform.
|
* {@link org.luaj.vm2.lib.jme.JmeIoLib} for the JME platform.
|
||||||
* <p>
|
* <p>
|
||||||
* The JSE implementation conforms almost completely to the C-based lua library,
|
* The JSE implementation conforms almost completely to the C-based lua library,
|
||||||
* while the JME implementation follows closely except in the area of random-access files,
|
* while the JME implementation follows closely except in the area of random-access files,
|
||||||
* which are difficult to support properly on JME.
|
* which are difficult to support properly on JME.
|
||||||
* <p>
|
* <p>
|
||||||
* Typically, this library is included as part of a call to either
|
* Typically, this library is included as part of a call to either
|
||||||
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = JsePlatform.standardGlobals();
|
* Globals globals = JsePlatform.standardGlobals();
|
||||||
* globals.get("io").get("write").call(LuaValue.valueOf("hello, world\n"));
|
* globals.get("io").get("write").call(LuaValue.valueOf("hello, world\n"));
|
||||||
* } </pre>
|
* } </pre>
|
||||||
* In this example the platform-specific {@link org.luaj.vm2.lib.jse.JseIoLib} library will be loaded, which will include
|
* In this example the platform-specific {@link org.luaj.vm2.lib.jse.JseIoLib} library will be loaded, which will include
|
||||||
* the base functionality provided by this class, whereas the {@link org.luaj.vm2.lib.jse.JsePlatform} would load the
|
* the base functionality provided by this class, whereas the {@link org.luaj.vm2.lib.jse.JsePlatform} would load the
|
||||||
* {@link org.luaj.vm2.lib.jse.JseIoLib}.
|
* {@link org.luaj.vm2.lib.jse.JseIoLib}.
|
||||||
* <p>
|
* <p>
|
||||||
* To instantiate and use it directly,
|
* To instantiate and use it directly,
|
||||||
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = new Globals();
|
* Globals globals = new Globals();
|
||||||
@@ -73,10 +73,10 @@ import org.luaj.vm2.Varargs;
|
|||||||
* @see org.luaj.vm2.lib.jme.JmeIoLib
|
* @see org.luaj.vm2.lib.jme.JmeIoLib
|
||||||
* @see <a href="http://www.lua.org/manual/5.1/manual.html#5.7">http://www.lua.org/manual/5.1/manual.html#5.7</a>
|
* @see <a href="http://www.lua.org/manual/5.1/manual.html#5.7">http://www.lua.org/manual/5.1/manual.html#5.7</a>
|
||||||
*/
|
*/
|
||||||
abstract
|
abstract
|
||||||
public class IoLib extends TwoArgFunction {
|
public class IoLib extends TwoArgFunction {
|
||||||
|
|
||||||
abstract
|
abstract
|
||||||
protected class File extends LuaValue{
|
protected class File extends LuaValue{
|
||||||
abstract public void write( LuaString string ) throws IOException;
|
abstract public void write( LuaString string ) throws IOException;
|
||||||
abstract public void flush() throws IOException;
|
abstract public void flush() throws IOException;
|
||||||
@@ -85,12 +85,12 @@ public class IoLib extends TwoArgFunction {
|
|||||||
abstract public boolean isclosed();
|
abstract public boolean isclosed();
|
||||||
// returns new position
|
// returns new position
|
||||||
abstract public int seek(String option, int bytecount) throws IOException;
|
abstract public int seek(String option, int bytecount) throws IOException;
|
||||||
abstract public void setvbuf(String mode, int size);
|
abstract public void setvbuf(String mode, int size);
|
||||||
// get length remaining to read
|
// get length remaining to read
|
||||||
abstract public int remaining() throws IOException;
|
abstract public int remaining() throws IOException;
|
||||||
// peek ahead one character
|
// peek ahead one character
|
||||||
abstract public int peek() throws IOException, EOFException;
|
abstract public int peek() throws IOException, EOFException;
|
||||||
// return char if read, -1 if eof, throw IOException on other exception
|
// return char if read, -1 if eof, throw IOException on other exception
|
||||||
abstract public int read() throws IOException, EOFException;
|
abstract public int read() throws IOException, EOFException;
|
||||||
// return number of bytes read if positive, false if eof, throw IOException on other exception
|
// return number of bytes read if positive, false if eof, throw IOException on other exception
|
||||||
abstract public int read(byte[] bytes, int offset, int length) throws IOException;
|
abstract public int read(byte[] bytes, int offset, int length) throws IOException;
|
||||||
@@ -123,29 +123,29 @@ public class IoLib extends TwoArgFunction {
|
|||||||
/** Enumerated value representing a file type for a named file */
|
/** Enumerated value representing a file type for a named file */
|
||||||
protected static final int FTYPE_NAMED = 3;
|
protected static final int FTYPE_NAMED = 3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrap the standard input.
|
* Wrap the standard input.
|
||||||
* @return File
|
* @return File
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
abstract protected File wrapStdin() throws IOException;
|
abstract protected File wrapStdin() throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrap the standard output.
|
* Wrap the standard output.
|
||||||
* @return File
|
* @return File
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
abstract protected File wrapStdout() throws IOException;
|
abstract protected File wrapStdout() throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrap the standard error output.
|
* Wrap the standard error output.
|
||||||
* @return File
|
* @return File
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
abstract protected File wrapStderr() throws IOException;
|
abstract protected File wrapStderr() throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open a file in a particular mode.
|
* Open a file in a particular mode.
|
||||||
* @param filename
|
* @param filename
|
||||||
* @param readMode true if opening in read mode
|
* @param readMode true if opening in read mode
|
||||||
* @param appendMode true if opening in append mode
|
* @param appendMode true if opening in append mode
|
||||||
@@ -157,7 +157,7 @@ public class IoLib extends TwoArgFunction {
|
|||||||
abstract protected File openFile( String filename, boolean readMode, boolean appendMode, boolean updateMode, boolean binaryMode ) throws IOException;
|
abstract protected File openFile( String filename, boolean readMode, boolean appendMode, boolean updateMode, boolean binaryMode ) throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open a temporary file.
|
* Open a temporary file.
|
||||||
* @return File object if successful
|
* @return File object if successful
|
||||||
* @throws IOException if could not be opened
|
* @throws IOException if could not be opened
|
||||||
*/
|
*/
|
||||||
@@ -167,7 +167,7 @@ public class IoLib extends TwoArgFunction {
|
|||||||
* Start a new process and return a file for input or output
|
* Start a new process and return a file for input or output
|
||||||
* @param prog the program to execute
|
* @param prog the program to execute
|
||||||
* @param mode "r" to read, "w" to write
|
* @param mode "r" to read, "w" to write
|
||||||
* @return File to read to or write from
|
* @return File to read to or write from
|
||||||
* @throws IOException if an i/o exception occurs
|
* @throws IOException if an i/o exception occurs
|
||||||
*/
|
*/
|
||||||
abstract protected File openProgram(String prog, String mode) throws IOException;
|
abstract protected File openProgram(String prog, String mode) throws IOException;
|
||||||
@@ -178,7 +178,7 @@ public class IoLib extends TwoArgFunction {
|
|||||||
|
|
||||||
private static final LuaValue STDIN = valueOf("stdin");
|
private static final LuaValue STDIN = valueOf("stdin");
|
||||||
private static final LuaValue STDOUT = valueOf("stdout");
|
private static final LuaValue STDOUT = valueOf("stdout");
|
||||||
private static final LuaValue STDERR = valueOf("stderr");
|
private static final LuaValue STDERR = valueOf("stderr");
|
||||||
private static final LuaValue FILE = valueOf("file");
|
private static final LuaValue FILE = valueOf("file");
|
||||||
private static final LuaValue CLOSED_FILE = valueOf("closed file");
|
private static final LuaValue CLOSED_FILE = valueOf("closed file");
|
||||||
|
|
||||||
@@ -256,7 +256,7 @@ public class IoLib extends TwoArgFunction {
|
|||||||
|
|
||||||
// return the table
|
// return the table
|
||||||
env.set("io", t);
|
env.set("io", t);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("io", t);
|
env.get("package").get("loaded").set("io", t);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,7 +316,7 @@ public class IoLib extends TwoArgFunction {
|
|||||||
return infile!=null? infile: (infile=ioopenfile(FTYPE_STDIN, "-","r"));
|
return infile!=null? infile: (infile=ioopenfile(FTYPE_STDIN, "-","r"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// io.flush() -> bool
|
// io.flush() -> bool
|
||||||
public Varargs _io_flush() throws IOException {
|
public Varargs _io_flush() throws IOException {
|
||||||
checkopen(output());
|
checkopen(output());
|
||||||
outfile.flush();
|
outfile.flush();
|
||||||
@@ -337,7 +337,7 @@ public class IoLib extends TwoArgFunction {
|
|||||||
|
|
||||||
// io.input([file]) -> file
|
// io.input([file]) -> file
|
||||||
public Varargs _io_input(LuaValue file) {
|
public Varargs _io_input(LuaValue file) {
|
||||||
infile = file.isnil()? input():
|
infile = file.isnil()? input():
|
||||||
file.isstring()? ioopenfile(FTYPE_NAMED, file.checkjstring(),"r"):
|
file.isstring()? ioopenfile(FTYPE_NAMED, file.checkjstring(),"r"):
|
||||||
checkfile(file);
|
checkfile(file);
|
||||||
return infile;
|
return infile;
|
||||||
@@ -345,7 +345,7 @@ public class IoLib extends TwoArgFunction {
|
|||||||
|
|
||||||
// io.output(filename) -> file
|
// io.output(filename) -> file
|
||||||
public Varargs _io_output(LuaValue filename) {
|
public Varargs _io_output(LuaValue filename) {
|
||||||
outfile = filename.isnil()? output():
|
outfile = filename.isnil()? output():
|
||||||
filename.isstring()? ioopenfile(FTYPE_NAMED, filename.checkjstring(),"w"):
|
filename.isstring()? ioopenfile(FTYPE_NAMED, filename.checkjstring(),"w"):
|
||||||
checkfile(filename);
|
checkfile(filename);
|
||||||
return outfile;
|
return outfile;
|
||||||
@@ -420,7 +420,7 @@ public class IoLib extends TwoArgFunction {
|
|||||||
return valueOf( checkfile(file).seek(whence,offset) );
|
return valueOf( checkfile(file).seek(whence,offset) );
|
||||||
}
|
}
|
||||||
|
|
||||||
// file:write(...) -> void
|
// file:write(...) -> void
|
||||||
public Varargs _file_write(LuaValue file, Varargs subargs) throws IOException {
|
public Varargs _file_write(LuaValue file, Varargs subargs) throws IOException {
|
||||||
return iowrite(checkfile(file),subargs);
|
return iowrite(checkfile(file),subargs);
|
||||||
}
|
}
|
||||||
@@ -467,8 +467,8 @@ public class IoLib extends TwoArgFunction {
|
|||||||
return LuaValue.TRUE;
|
return LuaValue.TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Varargs errorresult(Exception ioe) {
|
private static Varargs errorresult(Exception ioe) {
|
||||||
String s = ioe.getMessage();
|
String s = ioe.getMessage();
|
||||||
return errorresult("io error: "+(s!=null? s: ioe.toString()));
|
return errorresult("io error: "+(s!=null? s: ioe.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -509,8 +509,8 @@ public class IoLib extends TwoArgFunction {
|
|||||||
case 'a': vi = freadall(f); break item;
|
case 'a': vi = freadall(f); break item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return argerror( i+1, "(invalid format)" );
|
return argerror( i+1, "(invalid format)" );
|
||||||
}
|
}
|
||||||
if ( (v[i++] = vi).isnil() )
|
if ( (v[i++] = vi).isnil() )
|
||||||
break;
|
break;
|
||||||
@@ -544,7 +544,7 @@ public class IoLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
boolean isreadmode = mode.startsWith("r");
|
boolean isreadmode = mode.startsWith("r");
|
||||||
boolean isappend = mode.startsWith("a");
|
boolean isappend = mode.startsWith("a");
|
||||||
boolean isupdate = mode.indexOf('+') > 0;
|
boolean isupdate = mode.indexOf("+") > 0;
|
||||||
boolean isbinary = mode.endsWith("b");
|
boolean isbinary = mode.endsWith("b");
|
||||||
return openFile( filename, isreadmode, isappend, isupdate, isbinary );
|
return openFile( filename, isreadmode, isappend, isupdate, isbinary );
|
||||||
}
|
}
|
||||||
@@ -564,7 +564,7 @@ public class IoLib extends TwoArgFunction {
|
|||||||
int c;
|
int c;
|
||||||
try {
|
try {
|
||||||
if ( lineonly ) {
|
if ( lineonly ) {
|
||||||
loop: while ( (c = f.read()) > 0 ) {
|
loop: while ( (c = f.read()) > 0 ) {
|
||||||
switch ( c ) {
|
switch ( c ) {
|
||||||
case '\r': break;
|
case '\r': break;
|
||||||
case '\n': break loop;
|
case '\n': break loop;
|
||||||
@@ -572,13 +572,13 @@ public class IoLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
while ( (c = f.read()) > 0 )
|
while ( (c = f.read()) > 0 )
|
||||||
baos.write(c);
|
baos.write(c);
|
||||||
}
|
}
|
||||||
} catch ( EOFException e ) {
|
} catch ( EOFException e ) {
|
||||||
c = -1;
|
c = -1;
|
||||||
}
|
}
|
||||||
return ( c < 0 && baos.size() == 0 )?
|
return ( c < 0 && baos.size() == 0 )?
|
||||||
(LuaValue) NIL:
|
(LuaValue) NIL:
|
||||||
(LuaValue) LuaString.valueUsing(baos.toByteArray());
|
(LuaValue) LuaString.valueUsing(baos.toByteArray());
|
||||||
}
|
}
|
||||||
@@ -619,7 +619,7 @@ public class IoLib extends TwoArgFunction {
|
|||||||
if ( baos != null )
|
if ( baos != null )
|
||||||
baos.write( c );
|
baos.write( c );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ abstract public class LibFunction extends LuaFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String tojstring() {
|
public String tojstring() {
|
||||||
return name != null ? "function: " + name : super.tojstring();
|
return name != null? name: super.tojstring();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -28,13 +28,13 @@ import org.luaj.vm2.LuaTable;
|
|||||||
import org.luaj.vm2.LuaValue;
|
import org.luaj.vm2.LuaValue;
|
||||||
import org.luaj.vm2.Varargs;
|
import org.luaj.vm2.Varargs;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subclass of {@link LibFunction} which implements the lua standard {@code math}
|
* Subclass of {@link LibFunction} which implements the lua standard {@code math}
|
||||||
* library.
|
* library.
|
||||||
* <p>
|
* <p>
|
||||||
* It contains only the math library support that is possible on JME.
|
* It contains only the math library support that is possible on JME.
|
||||||
* For a more complete implementation based on math functions specific to JSE
|
* For a more complete implementation based on math functions specific to JSE
|
||||||
* use {@link org.luaj.vm2.lib.jse.JseMathLib}.
|
* use {@link org.luaj.vm2.lib.jse.JseMathLib}.
|
||||||
* In Particular the following math functions are <b>not</b> implemented by this library:
|
* In Particular the following math functions are <b>not</b> implemented by this library:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>acos</li>
|
* <li>acos</li>
|
||||||
@@ -47,21 +47,21 @@ import org.luaj.vm2.Varargs;
|
|||||||
* <li>atan2</li>
|
* <li>atan2</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
* <p>
|
* <p>
|
||||||
* The implementations of {@code exp()} and {@code pow()} are constructed by
|
* The implementations of {@code exp()} and {@code pow()} are constructed by
|
||||||
* hand for JME, so will be slower and less accurate than when executed on the JSE platform.
|
* hand for JME, so will be slower and less accurate than when executed on the JSE platform.
|
||||||
* <p>
|
* <p>
|
||||||
* Typically, this library is included as part of a call to either
|
* Typically, this library is included as part of a call to either
|
||||||
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or
|
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or
|
||||||
* {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
* {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = JsePlatform.standardGlobals();
|
* Globals globals = JsePlatform.standardGlobals();
|
||||||
* System.out.println( globals.get("math").get("sqrt").call( LuaValue.valueOf(2) ) );
|
* System.out.println( globals.get("math").get("sqrt").call( LuaValue.valueOf(2) ) );
|
||||||
* } </pre>
|
* } </pre>
|
||||||
* When using {@link org.luaj.vm2.lib.jse.JsePlatform} as in this example,
|
* When using {@link org.luaj.vm2.lib.jse.JsePlatform} as in this example,
|
||||||
* the subclass {@link org.luaj.vm2.lib.jse.JseMathLib} will
|
* the subclass {@link org.luaj.vm2.lib.jse.JseMathLib} will
|
||||||
* be included, which also includes this base functionality.
|
* be included, which also includes this base functionality.
|
||||||
* <p>
|
* <p>
|
||||||
* To instantiate and use it directly,
|
* To instantiate and use it directly,
|
||||||
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = new Globals();
|
* Globals globals = new Globals();
|
||||||
@@ -70,8 +70,8 @@ import org.luaj.vm2.Varargs;
|
|||||||
* globals.load(new MathLib());
|
* globals.load(new MathLib());
|
||||||
* System.out.println( globals.get("math").get("sqrt").call( LuaValue.valueOf(2) ) );
|
* System.out.println( globals.get("math").get("sqrt").call( LuaValue.valueOf(2) ) );
|
||||||
* } </pre>
|
* } </pre>
|
||||||
* Doing so will ensure the library is properly initialized
|
* Doing so will ensure the library is properly initialized
|
||||||
* and loaded into the globals table.
|
* and loaded into the globals table.
|
||||||
* <p>
|
* <p>
|
||||||
* This has been implemented to match as closely as possible the behavior in the corresponding library in C.
|
* This has been implemented to match as closely as possible the behavior in the corresponding library in C.
|
||||||
* @see LibFunction
|
* @see LibFunction
|
||||||
@@ -87,8 +87,8 @@ public class MathLib extends TwoArgFunction {
|
|||||||
*/
|
*/
|
||||||
public static MathLib MATHLIB = null;
|
public static MathLib MATHLIB = null;
|
||||||
|
|
||||||
/** Construct a MathLib, which can be initialized by calling it with a
|
/** Construct a MathLib, which can be initialized by calling it with a
|
||||||
* modname string, and a global environment table as arguments using
|
* modname string, and a global environment table as arguments using
|
||||||
* {@link #call(LuaValue, LuaValue)}. */
|
* {@link #call(LuaValue, LuaValue)}. */
|
||||||
public MathLib() {
|
public MathLib() {
|
||||||
MATHLIB = this;
|
MATHLIB = this;
|
||||||
@@ -125,7 +125,7 @@ public class MathLib extends TwoArgFunction {
|
|||||||
math.set("sqrt", new sqrt());
|
math.set("sqrt", new sqrt());
|
||||||
math.set("tan", new tan());
|
math.set("tan", new tan());
|
||||||
env.set("math", math);
|
env.set("math", math);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("math", math);
|
env.get("package").get("loaded").set("math", math);
|
||||||
return math;
|
return math;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,17 +158,15 @@ public class MathLib extends TwoArgFunction {
|
|||||||
exp(MathLib mathlib) {
|
exp(MathLib mathlib) {
|
||||||
this.mathlib = mathlib;
|
this.mathlib = mathlib;
|
||||||
}
|
}
|
||||||
protected double call(double d) {
|
protected double call(double d) {
|
||||||
return mathlib.dpow_lib(Math.E,d);
|
return mathlib.dpow_lib(Math.E,d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static final class fmod extends TwoArgFunction {
|
static final class fmod extends BinaryOp {
|
||||||
public LuaValue call(LuaValue xv, LuaValue yv) {
|
protected double call(double x, double y) {
|
||||||
if (xv.islong() && yv.islong()) {
|
double q = x/y;
|
||||||
return valueOf(xv.tolong() % yv.tolong());
|
return x - y * (q>=0? Math.floor(q): Math.ceil(q));
|
||||||
}
|
|
||||||
return valueOf(xv.checkdouble() % yv.checkdouble());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static final class ldexp extends BinaryOp {
|
static final class ldexp extends BinaryOp {
|
||||||
@@ -196,36 +194,27 @@ public class MathLib extends TwoArgFunction {
|
|||||||
|
|
||||||
static class max extends VarArgFunction {
|
static class max extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaValue m = args.checkvalue(1);
|
double m = args.checkdouble(1);
|
||||||
for ( int i=2,n=args.narg(); i<=n; ++i ) {
|
for ( int i=2,n=args.narg(); i<=n; ++i )
|
||||||
LuaValue v = args.checkvalue(i);
|
m = Math.max(m,args.checkdouble(i));
|
||||||
if (m.lt_b(v)) m = v;
|
return valueOf(m);
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class min extends VarArgFunction {
|
static class min extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaValue m = args.checkvalue(1);
|
double m = args.checkdouble(1);
|
||||||
for ( int i=2,n=args.narg(); i<=n; ++i ) {
|
for ( int i=2,n=args.narg(); i<=n; ++i )
|
||||||
LuaValue v = args.checkvalue(i);
|
m = Math.min(m,args.checkdouble(i));
|
||||||
if (v.lt_b(m)) m = v;
|
return valueOf(m);
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class modf extends VarArgFunction {
|
static class modf extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaValue n = args.arg1();
|
double x = args.checkdouble(1);
|
||||||
/* number is its own integer part, no fractional part */
|
|
||||||
if (n.islong()) return varargsOf(n, valueOf(0.0));
|
|
||||||
double x = n.checkdouble();
|
|
||||||
/* integer part (rounds toward zero) */
|
|
||||||
double intPart = ( x > 0 ) ? Math.floor( x ) : Math.ceil( x );
|
double intPart = ( x > 0 ) ? Math.floor( x ) : Math.ceil( x );
|
||||||
/* fractional part (test needed for inf/-inf) */
|
double fracPart = x - intPart;
|
||||||
double fracPart = x == intPart ? 0.0 : x - intPart;
|
|
||||||
return varargsOf( valueOf(intPart), valueOf(fracPart) );
|
return varargsOf( valueOf(intPart), valueOf(fracPart) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -263,26 +252,26 @@ public class MathLib extends TwoArgFunction {
|
|||||||
|
|
||||||
/** compute power using installed math library, or default if there is no math library installed */
|
/** compute power using installed math library, or default if there is no math library installed */
|
||||||
public static LuaValue dpow(double a, double b) {
|
public static LuaValue dpow(double a, double b) {
|
||||||
return LuaDouble.valueOf(
|
return LuaDouble.valueOf(
|
||||||
MATHLIB!=null?
|
MATHLIB!=null?
|
||||||
MATHLIB.dpow_lib(a,b):
|
MATHLIB.dpow_lib(a,b):
|
||||||
dpow_default(a,b) );
|
dpow_default(a,b) );
|
||||||
}
|
}
|
||||||
public static double dpow_d(double a, double b) {
|
public static double dpow_d(double a, double b) {
|
||||||
return MATHLIB!=null?
|
return MATHLIB!=null?
|
||||||
MATHLIB.dpow_lib(a,b):
|
MATHLIB.dpow_lib(a,b):
|
||||||
dpow_default(a,b);
|
dpow_default(a,b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook to override default dpow behavior with faster implementation.
|
* Hook to override default dpow behavior with faster implementation.
|
||||||
*/
|
*/
|
||||||
public double dpow_lib(double a, double b) {
|
public double dpow_lib(double a, double b) {
|
||||||
return dpow_default(a,b);
|
return dpow_default(a,b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default JME version computes using longhand heuristics.
|
* Default JME version computes using longhand heuristics.
|
||||||
*/
|
*/
|
||||||
protected static double dpow_default(double a, double b) {
|
protected static double dpow_default(double a, double b) {
|
||||||
if ( b < 0 )
|
if ( b < 0 )
|
||||||
|
|||||||
@@ -35,17 +35,17 @@ import org.luaj.vm2.Varargs;
|
|||||||
* Subclass of {@link LibFunction} which implements the standard lua {@code os} library.
|
* Subclass of {@link LibFunction} which implements the standard lua {@code os} library.
|
||||||
* <p>
|
* <p>
|
||||||
* It is a usable base with simplified stub functions
|
* It is a usable base with simplified stub functions
|
||||||
* for library functions that cannot be implemented uniformly
|
* for library functions that cannot be implemented uniformly
|
||||||
* on Jse and Jme.
|
* on Jse and Jme.
|
||||||
* <p>
|
* <p>
|
||||||
* This can be installed as-is on either platform, or extended
|
* This can be installed as-is on either platform, or extended
|
||||||
* and refined to be used in a complete Jse implementation.
|
* and refined to be used in a complete Jse implementation.
|
||||||
* <p>
|
* <p>
|
||||||
* Because the nature of the {@code os} library is to encapsulate
|
* Because the nature of the {@code os} library is to encapsulate
|
||||||
* os-specific features, the behavior of these functions varies considerably
|
* os-specific features, the behavior of these functions varies considerably
|
||||||
* from their counterparts in the C platform.
|
* from their counterparts in the C platform.
|
||||||
* <p>
|
* <p>
|
||||||
* The following functions have limited implementations of features
|
* The following functions have limited implementations of features
|
||||||
* that are not supported well on Jme:
|
* that are not supported well on Jme:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>{@code execute()}</li>
|
* <li>{@code execute()}</li>
|
||||||
@@ -54,7 +54,7 @@ import org.luaj.vm2.Varargs;
|
|||||||
* <li>{@code tmpname()}</li>
|
* <li>{@code tmpname()}</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
* <p>
|
* <p>
|
||||||
* Typically, this library is included as part of a call to either
|
* Typically, this library is included as part of a call to either
|
||||||
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = JsePlatform.standardGlobals();
|
* Globals globals = JsePlatform.standardGlobals();
|
||||||
@@ -63,7 +63,7 @@ import org.luaj.vm2.Varargs;
|
|||||||
* In this example the platform-specific {@link org.luaj.vm2.lib.jse.JseOsLib} library will be loaded, which will include
|
* In this example the platform-specific {@link org.luaj.vm2.lib.jse.JseOsLib} library will be loaded, which will include
|
||||||
* the base functionality provided by this class.
|
* the base functionality provided by this class.
|
||||||
* <p>
|
* <p>
|
||||||
* To instantiate and use it directly,
|
* To instantiate and use it directly,
|
||||||
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = new Globals();
|
* Globals globals = new Globals();
|
||||||
@@ -80,8 +80,8 @@ import org.luaj.vm2.Varargs;
|
|||||||
* @see <a href="http://www.lua.org/manual/5.1/manual.html#5.8">http://www.lua.org/manual/5.1/manual.html#5.8</a>
|
* @see <a href="http://www.lua.org/manual/5.1/manual.html#5.8">http://www.lua.org/manual/5.1/manual.html#5.8</a>
|
||||||
*/
|
*/
|
||||||
public class OsLib extends TwoArgFunction {
|
public class OsLib extends TwoArgFunction {
|
||||||
public static final String TMP_PREFIX = ".luaj";
|
public static String TMP_PREFIX = ".luaj";
|
||||||
public static final String TMP_SUFFIX = "tmp";
|
public static String TMP_SUFFIX = "tmp";
|
||||||
|
|
||||||
private static final int CLOCK = 0;
|
private static final int CLOCK = 0;
|
||||||
private static final int DATE = 1;
|
private static final int DATE = 1;
|
||||||
@@ -114,8 +114,8 @@ public class OsLib extends TwoArgFunction {
|
|||||||
|
|
||||||
protected Globals globals;
|
protected Globals globals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create and OsLib instance.
|
* Create and OsLib instance.
|
||||||
*/
|
*/
|
||||||
public OsLib() {
|
public OsLib() {
|
||||||
}
|
}
|
||||||
@@ -132,7 +132,7 @@ public class OsLib extends TwoArgFunction {
|
|||||||
for (int i = 0; i < NAMES.length; ++i)
|
for (int i = 0; i < NAMES.length; ++i)
|
||||||
os.set(NAMES[i], new OsLibFunc(i, NAMES[i]));
|
os.set(NAMES[i], new OsLibFunc(i, NAMES[i]));
|
||||||
env.set("os", os);
|
env.set("os", os);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("os", os);
|
env.get("package").get("loaded").set("os", os);
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,8 +200,8 @@ public class OsLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return an approximation of the amount in seconds of CPU time used by
|
* @return an approximation of the amount in seconds of CPU time used by
|
||||||
* the program. For luaj this simple returns the elapsed time since the
|
* the program. For luaj this simple returns the elapsed time since the
|
||||||
* OsLib class was loaded.
|
* OsLib class was loaded.
|
||||||
*/
|
*/
|
||||||
protected double clock() {
|
protected double clock() {
|
||||||
@@ -209,7 +209,7 @@ public class OsLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the number of seconds from time t1 to time t2.
|
* Returns the number of seconds from time t1 to time t2.
|
||||||
* In POSIX, Windows, and some other systems, this value is exactly t2-t1.
|
* In POSIX, Windows, and some other systems, this value is exactly t2-t1.
|
||||||
* @param t2
|
* @param t2
|
||||||
* @param t1
|
* @param t1
|
||||||
@@ -220,21 +220,21 @@ public class OsLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the time argument is present, this is the time to be formatted
|
* If the time argument is present, this is the time to be formatted
|
||||||
* (see the os.time function for a description of this value).
|
* (see the os.time function for a description of this value).
|
||||||
* Otherwise, date formats the current time.
|
* Otherwise, date formats the current time.
|
||||||
*
|
*
|
||||||
* Date returns the date as a string,
|
* Date returns the date as a string,
|
||||||
* formatted according to the same rules as ANSII strftime, but without
|
* formatted according to the same rules as ANSII strftime, but without
|
||||||
* support for %g, %G, or %V.
|
* support for %g, %G, or %V.
|
||||||
*
|
*
|
||||||
* When called without arguments, date returns a reasonable date and
|
* When called without arguments, date returns a reasonable date and
|
||||||
* time representation that depends on the host system and on the
|
* time representation that depends on the host system and on the
|
||||||
* current locale (that is, os.date() is equivalent to os.date("%c")).
|
* current locale (that is, os.date() is equivalent to os.date("%c")).
|
||||||
*
|
*
|
||||||
* @param format
|
* @param format
|
||||||
* @param time time since epoch, or -1 if not supplied
|
* @param time time since epoch, or -1 if not supplied
|
||||||
* @return a LString or a LTable containing date and time,
|
* @return a LString or a LTable containing date and time,
|
||||||
* formatted according to the given string format.
|
* formatted according to the given string format.
|
||||||
*/
|
*/
|
||||||
public String date(String format, double time) {
|
public String date(String format, double time) {
|
||||||
@@ -277,8 +277,8 @@ public class OsLib extends TwoArgFunction {
|
|||||||
break;
|
break;
|
||||||
case 'B':
|
case 'B':
|
||||||
result.append(MonthName[d.get(Calendar.MONTH)]);
|
result.append(MonthName[d.get(Calendar.MONTH)]);
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
result.append(date("%a %b %d %H:%M:%S %Y", time));
|
result.append(date("%a %b %d %H:%M:%S %Y", time));
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
@@ -314,7 +314,7 @@ public class OsLib extends TwoArgFunction {
|
|||||||
case 'w':
|
case 'w':
|
||||||
result.append(String.valueOf((d.get(Calendar.DAY_OF_WEEK)+6)%7));
|
result.append(String.valueOf((d.get(Calendar.DAY_OF_WEEK)+6)%7));
|
||||||
break;
|
break;
|
||||||
case 'W':
|
case 'W':
|
||||||
result.append(String.valueOf(weekNumber(d, 1)));
|
result.append(String.valueOf(weekNumber(d, 1)));
|
||||||
break;
|
break;
|
||||||
case 'x':
|
case 'x':
|
||||||
@@ -372,7 +372,7 @@ public class OsLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int timeZoneOffset(Calendar d) {
|
private int timeZoneOffset(Calendar d) {
|
||||||
int localStandarTimeMillis = (
|
int localStandarTimeMillis = (
|
||||||
d.get(Calendar.HOUR_OF_DAY) * 3600 +
|
d.get(Calendar.HOUR_OF_DAY) * 3600 +
|
||||||
d.get(Calendar.MINUTE) * 60 +
|
d.get(Calendar.MINUTE) * 60 +
|
||||||
d.get(Calendar.SECOND)) * 1000;
|
d.get(Calendar.SECOND)) * 1000;
|
||||||
@@ -381,28 +381,28 @@ public class OsLib extends TwoArgFunction {
|
|||||||
d.get(Calendar.YEAR),
|
d.get(Calendar.YEAR),
|
||||||
d.get(Calendar.MONTH),
|
d.get(Calendar.MONTH),
|
||||||
d.get(Calendar.DAY_OF_MONTH),
|
d.get(Calendar.DAY_OF_MONTH),
|
||||||
d.get(Calendar.DAY_OF_WEEK),
|
d.get(Calendar.DAY_OF_WEEK),
|
||||||
localStandarTimeMillis) / 1000;
|
localStandarTimeMillis) / 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isDaylightSavingsTime(Calendar d) {
|
private boolean isDaylightSavingsTime(Calendar d) {
|
||||||
return timeZoneOffset(d) != d.getTimeZone().getRawOffset() / 1000;
|
return timeZoneOffset(d) != d.getTimeZone().getRawOffset() / 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function is equivalent to the C function system.
|
* This function is equivalent to the C function system.
|
||||||
* It passes command to be executed by an operating system shell.
|
* It passes command to be executed by an operating system shell.
|
||||||
* It returns a status code, which is system-dependent.
|
* It returns a status code, which is system-dependent.
|
||||||
* If command is absent, then it returns nonzero if a shell
|
* If command is absent, then it returns nonzero if a shell
|
||||||
* is available and zero otherwise.
|
* is available and zero otherwise.
|
||||||
* @param command command to pass to the system
|
* @param command command to pass to the system
|
||||||
*/
|
*/
|
||||||
protected Varargs execute(String command) {
|
protected Varargs execute(String command) {
|
||||||
return varargsOf(NIL, valueOf("exit"), ONE);
|
return varargsOf(NIL, valueOf("exit"), ONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calls the C function exit, with an optional code, to terminate the host program.
|
* Calls the C function exit, with an optional code, to terminate the host program.
|
||||||
* @param code
|
* @param code
|
||||||
*/
|
*/
|
||||||
protected void exit(int code) {
|
protected void exit(int code) {
|
||||||
@@ -415,13 +415,13 @@ public class OsLib extends TwoArgFunction {
|
|||||||
* or null if the variable is not defined in either environment.
|
* or null if the variable is not defined in either environment.
|
||||||
*
|
*
|
||||||
* The default implementation, which is used by the JmePlatform,
|
* The default implementation, which is used by the JmePlatform,
|
||||||
* only queryies System.getProperty().
|
* only queryies System.getProperty().
|
||||||
*
|
*
|
||||||
* The JsePlatform overrides this behavior and returns the
|
* The JsePlatform overrides this behavior and returns the
|
||||||
* environment variable value using System.getenv() if it exists,
|
* environment variable value using System.getenv() if it exists,
|
||||||
* or the System property value if it does not.
|
* or the System property value if it does not.
|
||||||
*
|
*
|
||||||
* A SecurityException may be thrown if access is not allowed
|
* A SecurityException may be thrown if access is not allowed
|
||||||
* for 'varname'.
|
* for 'varname'.
|
||||||
* @param varname
|
* @param varname
|
||||||
* @return String value, or null if not defined
|
* @return String value, or null if not defined
|
||||||
@@ -431,10 +431,10 @@ public class OsLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes the file or directory with the given name.
|
* Deletes the file or directory with the given name.
|
||||||
* Directories must be empty to be removed.
|
* Directories must be empty to be removed.
|
||||||
* If this function fails, it throws and IOException
|
* If this function fails, it throws and IOException
|
||||||
*
|
*
|
||||||
* @param filename
|
* @param filename
|
||||||
* @throws IOException if it fails
|
* @throws IOException if it fails
|
||||||
*/
|
*/
|
||||||
@@ -443,9 +443,9 @@ public class OsLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renames file or directory named oldname to newname.
|
* Renames file or directory named oldname to newname.
|
||||||
* If this function fails,it throws and IOException
|
* If this function fails,it throws and IOException
|
||||||
*
|
*
|
||||||
* @param oldname old file name
|
* @param oldname old file name
|
||||||
* @param newname new file name
|
* @param newname new file name
|
||||||
* @throws IOException if it fails
|
* @throws IOException if it fails
|
||||||
@@ -455,21 +455,21 @@ public class OsLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the current locale of the program. locale is a string specifying
|
* Sets the current locale of the program. locale is a string specifying
|
||||||
* a locale; category is an optional string describing which category to change:
|
* a locale; category is an optional string describing which category to change:
|
||||||
* "all", "collate", "ctype", "monetary", "numeric", or "time"; the default category
|
* "all", "collate", "ctype", "monetary", "numeric", or "time"; the default category
|
||||||
* is "all".
|
* is "all".
|
||||||
*
|
*
|
||||||
* If locale is the empty string, the current locale is set to an implementation-
|
* If locale is the empty string, the current locale is set to an implementation-
|
||||||
* defined native locale. If locale is the string "C", the current locale is set
|
* defined native locale. If locale is the string "C", the current locale is set
|
||||||
* to the standard C locale.
|
* to the standard C locale.
|
||||||
*
|
*
|
||||||
* When called with null as the first argument, this function only returns the
|
* When called with null as the first argument, this function only returns the
|
||||||
* name of the current locale for the given category.
|
* name of the current locale for the given category.
|
||||||
*
|
*
|
||||||
* @param locale
|
* @param locale
|
||||||
* @param category
|
* @param category
|
||||||
* @return the name of the new locale, or null if the request
|
* @return the name of the new locale, or null if the request
|
||||||
* cannot be honored.
|
* cannot be honored.
|
||||||
*/
|
*/
|
||||||
protected String setlocale(String locale, String category) {
|
protected String setlocale(String locale, String category) {
|
||||||
@@ -477,10 +477,10 @@ public class OsLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current time when called without arguments,
|
* Returns the current time when called without arguments,
|
||||||
* or a time representing the date and time specified by the given table.
|
* or a time representing the date and time specified by the given table.
|
||||||
* This table must have fields year, month, and day,
|
* This table must have fields year, month, and day,
|
||||||
* and may have fields hour, min, sec, and isdst
|
* and may have fields hour, min, sec, and isdst
|
||||||
* (for a description of these fields, see the os.date function).
|
* (for a description of these fields, see the os.date function).
|
||||||
* @param table
|
* @param table
|
||||||
* @return long value for the time
|
* @return long value for the time
|
||||||
@@ -504,15 +504,15 @@ public class OsLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a string with a file name that can be used for a temporary file.
|
* Returns a string with a file name that can be used for a temporary file.
|
||||||
* The file must be explicitly opened before its use and explicitly removed
|
* The file must be explicitly opened before its use and explicitly removed
|
||||||
* when no longer needed.
|
* when no longer needed.
|
||||||
*
|
*
|
||||||
* On some systems (POSIX), this function also creates a file with that name,
|
* On some systems (POSIX), this function also creates a file with that name,
|
||||||
* to avoid security risks. (Someone else might create the file with wrong
|
* to avoid security risks. (Someone else might create the file with wrong
|
||||||
* permissions in the time between getting the name and creating the file.)
|
* permissions in the time between getting the name and creating the file.)
|
||||||
* You still have to open the file to use it and to remove it (even if you
|
* You still have to open the file to use it and to remove it (even if you
|
||||||
* do not use it).
|
* do not use it).
|
||||||
*
|
*
|
||||||
* @return String filename to use
|
* @return String filename to use
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -81,26 +81,23 @@ public class PackageLib extends TwoArgFunction {
|
|||||||
|
|
||||||
/** The default value to use for package.path. This can be set with the system property
|
/** The default value to use for package.path. This can be set with the system property
|
||||||
* <code>"luaj.package.path"</code>, and is <code>"?.lua"</code> by default. */
|
* <code>"luaj.package.path"</code>, and is <code>"?.lua"</code> by default. */
|
||||||
public static final String DEFAULT_LUA_PATH;
|
public static String DEFAULT_LUA_PATH;
|
||||||
static {
|
static {
|
||||||
String path = null;
|
|
||||||
try {
|
try {
|
||||||
path = System.getProperty("luaj.package.path");
|
DEFAULT_LUA_PATH = System.getProperty("luaj.package.path");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println(e.toString());
|
System.out.println(e.toString());
|
||||||
}
|
}
|
||||||
if (path == null) {
|
if (DEFAULT_LUA_PATH == null)
|
||||||
path = "?.lua";
|
DEFAULT_LUA_PATH = "?.lua";
|
||||||
}
|
|
||||||
DEFAULT_LUA_PATH = path;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static final LuaString _LOADED = valueOf("loaded");
|
private static final LuaString _LOADED = valueOf("loaded");
|
||||||
private static final LuaString _LOADLIB = valueOf("loadlib");
|
private static final LuaString _LOADLIB = valueOf("loadlib");
|
||||||
static final LuaString _PRELOAD = valueOf("preload");
|
private static final LuaString _PRELOAD = valueOf("preload");
|
||||||
static final LuaString _PATH = valueOf("path");
|
private static final LuaString _PATH = valueOf("path");
|
||||||
static final LuaString _SEARCHPATH = valueOf("searchpath");
|
private static final LuaString _SEARCHPATH = valueOf("searchpath");
|
||||||
static final LuaString _SEARCHERS = valueOf("searchers");
|
private static final LuaString _SEARCHERS = valueOf("searchers");
|
||||||
|
|
||||||
/** The globals that were used to load this library. */
|
/** The globals that were used to load this library. */
|
||||||
Globals globals;
|
Globals globals;
|
||||||
@@ -255,6 +252,7 @@ public class PackageLib extends TwoArgFunction {
|
|||||||
public class lua_searcher extends VarArgFunction {
|
public class lua_searcher extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaString name = args.checkstring(1);
|
LuaString name = args.checkstring(1);
|
||||||
|
InputStream is = null;
|
||||||
|
|
||||||
// get package path
|
// get package path
|
||||||
LuaValue path = package_.get(_PATH);
|
LuaValue path = package_.get(_PATH);
|
||||||
|
|||||||
@@ -24,32 +24,32 @@ package org.luaj.vm2.lib;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.luaj.vm2.Buffer;
|
|
||||||
import org.luaj.vm2.LuaClosure;
|
import org.luaj.vm2.LuaClosure;
|
||||||
|
import org.luaj.vm2.Buffer;
|
||||||
import org.luaj.vm2.LuaString;
|
import org.luaj.vm2.LuaString;
|
||||||
import org.luaj.vm2.LuaTable;
|
import org.luaj.vm2.LuaTable;
|
||||||
import org.luaj.vm2.LuaValue;
|
import org.luaj.vm2.LuaValue;
|
||||||
import org.luaj.vm2.Varargs;
|
import org.luaj.vm2.Varargs;
|
||||||
import org.luaj.vm2.compiler.DumpState;
|
import org.luaj.vm2.compiler.DumpState;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subclass of {@link LibFunction} which implements the lua standard {@code string}
|
* Subclass of {@link LibFunction} which implements the lua standard {@code string}
|
||||||
* library.
|
* library.
|
||||||
* <p>
|
* <p>
|
||||||
* Typically, this library is included as part of a call to either
|
* Typically, this library is included as part of a call to either
|
||||||
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = JsePlatform.standardGlobals();
|
* Globals globals = JsePlatform.standardGlobals();
|
||||||
* System.out.println( globals.get("string").get("upper").call( LuaValue.valueOf("abcde") ) );
|
* System.out.println( globals.get("string").get("upper").call( LuaValue.valueOf("abcde") ) );
|
||||||
* } </pre>
|
* } </pre>
|
||||||
* <p>
|
* <p>
|
||||||
* To instantiate and use it directly,
|
* To instantiate and use it directly,
|
||||||
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = new Globals();
|
* Globals globals = new Globals();
|
||||||
* globals.load(new JseBaseLib());
|
* globals.load(new JseBaseLib());
|
||||||
* globals.load(new PackageLib());
|
* globals.load(new PackageLib());
|
||||||
* globals.load(new JseStringLib());
|
* globals.load(new StringLib());
|
||||||
* System.out.println( globals.get("string").get("upper").call( LuaValue.valueOf("abcde") ) );
|
* System.out.println( globals.get("string").get("upper").call( LuaValue.valueOf("abcde") ) );
|
||||||
* } </pre>
|
* } </pre>
|
||||||
* <p>
|
* <p>
|
||||||
@@ -61,8 +61,8 @@ import org.luaj.vm2.compiler.DumpState;
|
|||||||
*/
|
*/
|
||||||
public class StringLib extends TwoArgFunction {
|
public class StringLib extends TwoArgFunction {
|
||||||
|
|
||||||
/** Construct a StringLib, which can be initialized by calling it with a
|
/** Construct a StringLib, which can be initialized by calling it with a
|
||||||
* modname string, and a global environment table as arguments using
|
* modname string, and a global environment table as arguments using
|
||||||
* {@link #call(LuaValue, LuaValue)}. */
|
* {@link #call(LuaValue, LuaValue)}. */
|
||||||
public StringLib() {
|
public StringLib() {
|
||||||
}
|
}
|
||||||
@@ -75,8 +75,8 @@ public class StringLib extends TwoArgFunction {
|
|||||||
* If the shared strings metatable instance is null, will set the metatable as
|
* If the shared strings metatable instance is null, will set the metatable as
|
||||||
* the global shared metatable for strings.
|
* the global shared metatable for strings.
|
||||||
* <P>
|
* <P>
|
||||||
* All tables and metatables are read-write by default so if this will be used in
|
* All tables and metatables are read-write by default so if this will be used in
|
||||||
* a server environment, sandboxing should be used. In particular, the
|
* a server environment, sandboxing should be used. In particular, the
|
||||||
* {@link LuaString#s_metatable} table should probably be made read-only.
|
* {@link LuaString#s_metatable} table should probably be made read-only.
|
||||||
* @param modname the module name supplied if this is loaded via 'require'.
|
* @param modname the module name supplied if this is loaded via 'require'.
|
||||||
* @param env the environment to load into, typically a Globals instance.
|
* @param env the environment to load into, typically a Globals instance.
|
||||||
@@ -97,17 +97,17 @@ public class StringLib extends TwoArgFunction {
|
|||||||
string.set("reverse", new reverse());
|
string.set("reverse", new reverse());
|
||||||
string.set("sub", new sub());
|
string.set("sub", new sub());
|
||||||
string.set("upper", new upper());
|
string.set("upper", new upper());
|
||||||
|
LuaTable mt = LuaValue.tableOf(
|
||||||
|
new LuaValue[] { INDEX, string });
|
||||||
env.set("string", string);
|
env.set("string", string);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("string", string);
|
env.get("package").get("loaded").set("string", string);
|
||||||
if (LuaString.s_metatable == null) {
|
if (LuaString.s_metatable == null)
|
||||||
LuaString.s_metatable = LuaValue.tableOf(new LuaValue[] { INDEX, string });
|
LuaString.s_metatable = mt;
|
||||||
}
|
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string.byte (s [, i [, j]])
|
* string.byte (s [, i [, j]])
|
||||||
*
|
*
|
||||||
* Returns the internal numerical codes of the
|
* Returns the internal numerical codes of the
|
||||||
* characters s[i], s[i+1], ..., s[j]. The default value for i is 1; the
|
* characters s[i], s[i+1], ..., s[j]. The default value for i is 1; the
|
||||||
@@ -137,12 +137,12 @@ public class StringLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string.char (...)
|
* string.char (...)
|
||||||
*
|
*
|
||||||
* Receives zero or more integers. Returns a string with length equal
|
* Receives zero or more integers. Returns a string with length equal
|
||||||
* to the number of arguments, in which each character has the internal
|
* to the number of arguments, in which each character has the internal
|
||||||
* numerical code equal to its corresponding argument.
|
* numerical code equal to its corresponding argument.
|
||||||
*
|
*
|
||||||
* Note that numerical codes are not necessarily portable across platforms.
|
* Note that numerical codes are not necessarily portable across platforms.
|
||||||
*
|
*
|
||||||
@@ -154,30 +154,28 @@ public class StringLib extends TwoArgFunction {
|
|||||||
byte[] bytes = new byte[n];
|
byte[] bytes = new byte[n];
|
||||||
for ( int i=0, a=1; i<n; i++, a++ ) {
|
for ( int i=0, a=1; i<n; i++, a++ ) {
|
||||||
int c = args.checkint(a);
|
int c = args.checkint(a);
|
||||||
if (c<0 || c>=256) argerror(a, "invalid value for string.char [0; 255]: " + c);
|
if (c<0 || c>=256) argerror(a, "invalid value");
|
||||||
bytes[i] = (byte) c;
|
bytes[i] = (byte) c;
|
||||||
}
|
}
|
||||||
return LuaString.valueUsing( bytes );
|
return LuaString.valueUsing( bytes );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string.dump (function[, stripDebug])
|
* string.dump (function)
|
||||||
*
|
*
|
||||||
* Returns a string containing a binary representation of the given function,
|
* Returns a string containing a binary representation of the given function,
|
||||||
* so that a later loadstring on this string returns a copy of the function.
|
* so that a later loadstring on this string returns a copy of the function.
|
||||||
* function must be a Lua function without upvalues.
|
* function must be a Lua function without upvalues.
|
||||||
* Boolean param stripDebug - true to strip debugging info, false otherwise.
|
*
|
||||||
* The default value for stripDebug is true.
|
|
||||||
*
|
|
||||||
* TODO: port dumping code as optional add-on
|
* TODO: port dumping code as optional add-on
|
||||||
*/
|
*/
|
||||||
static final class dump extends VarArgFunction {
|
static final class dump extends OneArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public LuaValue call(LuaValue arg) {
|
||||||
LuaValue f = args.checkfunction(1);
|
LuaValue f = arg.checkfunction();
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
try {
|
try {
|
||||||
DumpState.dump( ((LuaClosure)f).p, baos, args.optboolean(2, true) );
|
DumpState.dump( ((LuaClosure)f).p, baos, true );
|
||||||
return LuaString.valueUsing(baos.toByteArray());
|
return LuaString.valueUsing(baos.toByteArray());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return error( e.getMessage() );
|
return error( e.getMessage() );
|
||||||
@@ -185,20 +183,20 @@ public class StringLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string.find (s, pattern [, init [, plain]])
|
* string.find (s, pattern [, init [, plain]])
|
||||||
*
|
*
|
||||||
* Looks for the first match of pattern in the string s.
|
* Looks for the first match of pattern in the string s.
|
||||||
* If it finds a match, then find returns the indices of s
|
* If it finds a match, then find returns the indices of s
|
||||||
* where this occurrence starts and ends; otherwise, it returns nil.
|
* where this occurrence starts and ends; otherwise, it returns nil.
|
||||||
* A third, optional numerical argument init specifies where to start the search;
|
* A third, optional numerical argument init specifies where to start the search;
|
||||||
* its default value is 1 and may be negative. A value of true as a fourth,
|
* its default value is 1 and may be negative. A value of true as a fourth,
|
||||||
* optional argument plain turns off the pattern matching facilities,
|
* optional argument plain turns off the pattern matching facilities,
|
||||||
* so the function does a plain "find substring" operation,
|
* so the function does a plain "find substring" operation,
|
||||||
* with no characters in pattern being considered "magic".
|
* with no characters in pattern being considered "magic".
|
||||||
* Note that if plain is given, then init must be given as well.
|
* Note that if plain is given, then init must be given as well.
|
||||||
*
|
*
|
||||||
* If the pattern has captures, then in a successful match the captured values
|
* If the pattern has captures, then in a successful match the captured values
|
||||||
* are also returned, after the two indices.
|
* are also returned, after the two indices.
|
||||||
*/
|
*/
|
||||||
static final class find extends VarArgFunction {
|
static final class find extends VarArgFunction {
|
||||||
@@ -207,30 +205,30 @@ public class StringLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string.format (formatstring, ...)
|
* string.format (formatstring, ...)
|
||||||
*
|
*
|
||||||
* Returns a formatted version of its variable number of arguments following
|
* Returns a formatted version of its variable number of arguments following
|
||||||
* the description given in its first argument (which must be a string).
|
* the description given in its first argument (which must be a string).
|
||||||
* The format string follows the same rules as the printf family of standard C functions.
|
* The format string follows the same rules as the printf family of standard C functions.
|
||||||
* The only differences are that the options/modifiers *, l, L, n, p, and h are not supported
|
* The only differences are that the options/modifiers *, l, L, n, p, and h are not supported
|
||||||
* and that there is an extra option, q. The q option formats a string in a form suitable
|
* and that there is an extra option, q. The q option formats a string in a form suitable
|
||||||
* to be safely read back by the Lua interpreter: the string is written between double quotes,
|
* to be safely read back by the Lua interpreter: the string is written between double quotes,
|
||||||
* and all double quotes, newlines, embedded zeros, and backslashes in the string are correctly
|
* and all double quotes, newlines, embedded zeros, and backslashes in the string are correctly
|
||||||
* escaped when written. For instance, the call
|
* escaped when written. For instance, the call
|
||||||
* string.format('%q', 'a string with "quotes" and \n new line')
|
* string.format('%q', 'a string with "quotes" and \n new line')
|
||||||
*
|
*
|
||||||
* will produce the string:
|
* will produce the string:
|
||||||
* "a string with \"quotes\" and \
|
* "a string with \"quotes\" and \
|
||||||
* new line"
|
* new line"
|
||||||
|
*
|
||||||
|
* The options c, d, E, e, f, g, G, i, o, u, X, and x all expect a number as argument,
|
||||||
|
* whereas q and s expect a string.
|
||||||
*
|
*
|
||||||
* The options c, d, E, e, f, g, G, i, o, u, X, and x all expect a number as argument,
|
* This function does not accept string values containing embedded zeros,
|
||||||
* whereas q and s expect a string.
|
* except as arguments to the q option.
|
||||||
*
|
|
||||||
* This function does not accept string values containing embedded zeros,
|
|
||||||
* except as arguments to the q option.
|
|
||||||
*/
|
*/
|
||||||
final class format extends VarArgFunction {
|
static final class format extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaString fmt = args.checkstring( 1 );
|
LuaString fmt = args.checkstring( 1 );
|
||||||
final int n = fmt.length();
|
final int n = fmt.length();
|
||||||
@@ -261,7 +259,7 @@ public class StringLib extends TwoArgFunction {
|
|||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
case 'd':
|
case 'd':
|
||||||
fdsc.format( result, args.checklong( arg ) );
|
fdsc.format( result, args.checkint( arg ) );
|
||||||
break;
|
break;
|
||||||
case 'o':
|
case 'o':
|
||||||
case 'u':
|
case 'u':
|
||||||
@@ -300,7 +298,7 @@ public class StringLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void addquoted(Buffer buf, LuaString s) {
|
private static void addquoted(Buffer buf, LuaString s) {
|
||||||
int c;
|
int c;
|
||||||
buf.append( (byte) '"' );
|
buf.append( (byte) '"' );
|
||||||
for ( int i = 0, n = s.length(); i < n; i++ ) {
|
for ( int i = 0, n = s.length(); i < n; i++ ) {
|
||||||
@@ -330,7 +328,7 @@ public class StringLib extends TwoArgFunction {
|
|||||||
|
|
||||||
private static final String FLAGS = "-+ #0";
|
private static final String FLAGS = "-+ #0";
|
||||||
|
|
||||||
class FormatDesc {
|
static class FormatDesc {
|
||||||
|
|
||||||
private boolean leftAdjust;
|
private boolean leftAdjust;
|
||||||
private boolean zeroPad;
|
private boolean zeroPad;
|
||||||
@@ -340,13 +338,11 @@ public class StringLib extends TwoArgFunction {
|
|||||||
private static final int MAX_FLAGS = 5;
|
private static final int MAX_FLAGS = 5;
|
||||||
|
|
||||||
private int width;
|
private int width;
|
||||||
int precision;
|
private int precision;
|
||||||
|
|
||||||
public final int conversion;
|
public final int conversion;
|
||||||
public final int length;
|
public final int length;
|
||||||
|
|
||||||
public final String src;
|
|
||||||
|
|
||||||
public FormatDesc(Varargs args, LuaString strfrmt, final int start) {
|
public FormatDesc(Varargs args, LuaString strfrmt, final int start) {
|
||||||
int p = start, n = strfrmt.length();
|
int p = start, n = strfrmt.length();
|
||||||
int c = 0;
|
int c = 0;
|
||||||
@@ -394,7 +390,6 @@ public class StringLib extends TwoArgFunction {
|
|||||||
zeroPad &= !leftAdjust; // '-' overrides '0'
|
zeroPad &= !leftAdjust; // '-' overrides '0'
|
||||||
conversion = c;
|
conversion = c;
|
||||||
length = p - start;
|
length = p - start;
|
||||||
src = strfrmt.substring(start - 1, p).tojstring();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void format(Buffer buf, byte c) {
|
public void format(Buffer buf, byte c) {
|
||||||
@@ -470,7 +465,8 @@ public class StringLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void format(Buffer buf, double x) {
|
public void format(Buffer buf, double x) {
|
||||||
buf.append( StringLib.this.format(src, x) );
|
// TODO
|
||||||
|
buf.append( String.valueOf( x ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void format(Buffer buf, LuaString s) {
|
public void format(Buffer buf, LuaString s) {
|
||||||
@@ -480,31 +476,27 @@ public class StringLib extends TwoArgFunction {
|
|||||||
buf.append(s);
|
buf.append(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void pad(Buffer buf, char c, int n) {
|
public static final void pad(Buffer buf, char c, int n) {
|
||||||
byte b = (byte)c;
|
byte b = (byte)c;
|
||||||
while ( n-- > 0 )
|
while ( n-- > 0 )
|
||||||
buf.append(b);
|
buf.append(b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String format(String src, double x) {
|
/**
|
||||||
return String.valueOf(x);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* string.gmatch (s, pattern)
|
* string.gmatch (s, pattern)
|
||||||
*
|
*
|
||||||
* Returns an iterator function that, each time it is called, returns the next captures
|
* Returns an iterator function that, each time it is called, returns the next captures
|
||||||
* from pattern over string s. If pattern specifies no captures, then the
|
* from pattern over string s. If pattern specifies no captures, then the
|
||||||
* whole match is produced in each call.
|
* whole match is produced in each call.
|
||||||
*
|
*
|
||||||
* As an example, the following loop
|
* As an example, the following loop
|
||||||
* s = "hello world from Lua"
|
* s = "hello world from Lua"
|
||||||
* for w in string.gmatch(s, "%a+") do
|
* for w in string.gmatch(s, "%a+") do
|
||||||
* print(w)
|
* print(w)
|
||||||
* end
|
* end
|
||||||
*
|
*
|
||||||
* will iterate over all the words from string s, printing one per line.
|
* will iterate over all the words from string s, printing one per line.
|
||||||
* The next example collects all pairs key=value from the given string into a table:
|
* The next example collects all pairs key=value from the given string into a table:
|
||||||
* t = {}
|
* t = {}
|
||||||
* s = "from=world, to=Lua"
|
* s = "from=world, to=Lua"
|
||||||
@@ -512,7 +504,7 @@ public class StringLib extends TwoArgFunction {
|
|||||||
* t[k] = v
|
* t[k] = v
|
||||||
* end
|
* end
|
||||||
*
|
*
|
||||||
* For this function, a '^' at the start of a pattern does not work as an anchor,
|
* For this function, a '^' at the start of a pattern does not work as an anchor,
|
||||||
* as this would prevent the iteration.
|
* as this would prevent the iteration.
|
||||||
*/
|
*/
|
||||||
static final class gmatch extends VarArgFunction {
|
static final class gmatch extends VarArgFunction {
|
||||||
@@ -533,13 +525,12 @@ public class StringLib extends TwoArgFunction {
|
|||||||
this.soffset = 0;
|
this.soffset = 0;
|
||||||
}
|
}
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
for ( ; soffset<=srclen; soffset++ ) {
|
for ( ; soffset<srclen; soffset++ ) {
|
||||||
ms.reset();
|
ms.reset();
|
||||||
int res = ms.match(soffset, 0);
|
int res = ms.match(soffset, 0);
|
||||||
if ( res >=0 ) {
|
if ( res >=0 ) {
|
||||||
int soff = soffset;
|
int soff = soffset;
|
||||||
soffset = res;
|
soffset = res;
|
||||||
if (soff == res) soffset++; /* empty match? go at least one position */
|
|
||||||
return ms.push_captures( true, soff, res );
|
return ms.push_captures( true, soff, res );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -548,28 +539,28 @@ public class StringLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string.gsub (s, pattern, repl [, n])
|
* string.gsub (s, pattern, repl [, n])
|
||||||
* Returns a copy of s in which all (or the first n, if given) occurrences of the
|
* Returns a copy of s in which all (or the first n, if given) occurrences of the
|
||||||
* pattern have been replaced by a replacement string specified by repl, which
|
* pattern have been replaced by a replacement string specified by repl, which
|
||||||
* may be a string, a table, or a function. gsub also returns, as its second value,
|
* may be a string, a table, or a function. gsub also returns, as its second value,
|
||||||
* the total number of matches that occurred.
|
* the total number of matches that occurred.
|
||||||
*
|
*
|
||||||
* If repl is a string, then its value is used for replacement.
|
* If repl is a string, then its value is used for replacement.
|
||||||
* The character % works as an escape character: any sequence in repl of the form %n,
|
* The character % works as an escape character: any sequence in repl of the form %n,
|
||||||
* with n between 1 and 9, stands for the value of the n-th captured substring (see below).
|
* with n between 1 and 9, stands for the value of the n-th captured substring (see below).
|
||||||
* The sequence %0 stands for the whole match. The sequence %% stands for a single %.
|
* The sequence %0 stands for the whole match. The sequence %% stands for a single %.
|
||||||
|
*
|
||||||
|
* If repl is a table, then the table is queried for every match, using the first capture
|
||||||
|
* as the key; if the pattern specifies no captures, then the whole match is used as the key.
|
||||||
*
|
*
|
||||||
* If repl is a table, then the table is queried for every match, using the first capture
|
* If repl is a function, then this function is called every time a match occurs,
|
||||||
* as the key; if the pattern specifies no captures, then the whole match is used as the key.
|
* with all captured substrings passed as arguments, in order; if the pattern specifies
|
||||||
|
* no captures, then the whole match is passed as a sole argument.
|
||||||
*
|
*
|
||||||
* If repl is a function, then this function is called every time a match occurs,
|
* If the value returned by the table query or by the function call is a string or a number,
|
||||||
* with all captured substrings passed as arguments, in order; if the pattern specifies
|
* then it is used as the replacement string; otherwise, if it is false or nil,
|
||||||
* no captures, then the whole match is passed as a sole argument.
|
* then there is no replacement (that is, the original match is kept in the string).
|
||||||
*
|
|
||||||
* If the value returned by the table query or by the function call is a string or a number,
|
|
||||||
* then it is used as the replacement string; otherwise, if it is false or nil,
|
|
||||||
* then there is no replacement (that is, the original match is kept in the string).
|
|
||||||
*
|
*
|
||||||
* Here are some examples:
|
* Here are some examples:
|
||||||
* x = string.gsub("hello world", "(%w+)", "%1 %1")
|
* x = string.gsub("hello world", "(%w+)", "%1 %1")
|
||||||
@@ -628,11 +619,11 @@ public class StringLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string.len (s)
|
* string.len (s)
|
||||||
*
|
*
|
||||||
* Receives a string and returns its length. The empty string "" has length 0.
|
* Receives a string and returns its length. The empty string "" has length 0.
|
||||||
* Embedded zeros are counted, so "a\000bc\000" has length 5.
|
* Embedded zeros are counted, so "a\000bc\000" has length 5.
|
||||||
*/
|
*/
|
||||||
static final class len extends OneArgFunction {
|
static final class len extends OneArgFunction {
|
||||||
public LuaValue call(LuaValue arg) {
|
public LuaValue call(LuaValue arg) {
|
||||||
@@ -640,11 +631,11 @@ public class StringLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string.lower (s)
|
* string.lower (s)
|
||||||
*
|
*
|
||||||
* Receives a string and returns a copy of this string with all uppercase letters
|
* Receives a string and returns a copy of this string with all uppercase letters
|
||||||
* changed to lowercase. All other characters are left unchanged.
|
* changed to lowercase. All other characters are left unchanged.
|
||||||
* The definition of what an uppercase letter is depends on the current locale.
|
* The definition of what an uppercase letter is depends on the current locale.
|
||||||
*/
|
*/
|
||||||
static final class lower extends OneArgFunction {
|
static final class lower extends OneArgFunction {
|
||||||
@@ -671,7 +662,7 @@ public class StringLib extends TwoArgFunction {
|
|||||||
/**
|
/**
|
||||||
* string.rep (s, n)
|
* string.rep (s, n)
|
||||||
*
|
*
|
||||||
* Returns a string that is the concatenation of n copies of the string s.
|
* Returns a string that is the concatenation of n copies of the string s.
|
||||||
*/
|
*/
|
||||||
static final class rep extends VarArgFunction {
|
static final class rep extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
@@ -686,10 +677,10 @@ public class StringLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string.reverse (s)
|
* string.reverse (s)
|
||||||
*
|
*
|
||||||
* Returns a string that is the string s reversed.
|
* Returns a string that is the string s reversed.
|
||||||
*/
|
*/
|
||||||
static final class reverse extends OneArgFunction {
|
static final class reverse extends OneArgFunction {
|
||||||
public LuaValue call(LuaValue arg) {
|
public LuaValue call(LuaValue arg) {
|
||||||
@@ -702,15 +693,15 @@ public class StringLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string.sub (s, i [, j])
|
* string.sub (s, i [, j])
|
||||||
*
|
*
|
||||||
* Returns the substring of s that starts at i and continues until j;
|
* Returns the substring of s that starts at i and continues until j;
|
||||||
* i and j may be negative. If j is absent, then it is assumed to be equal to -1
|
* i and j may be negative. If j is absent, then it is assumed to be equal to -1
|
||||||
* (which is the same as the string length). In particular, the call
|
* (which is the same as the string length). In particular, the call
|
||||||
* string.sub(s,1,j)
|
* string.sub(s,1,j)
|
||||||
* returns a prefix of s with length j, and
|
* returns a prefix of s with length j, and
|
||||||
* string.sub(s, -i)
|
* string.sub(s, -i)
|
||||||
* returns a suffix of s with length i.
|
* returns a suffix of s with length i.
|
||||||
*/
|
*/
|
||||||
static final class sub extends VarArgFunction {
|
static final class sub extends VarArgFunction {
|
||||||
@@ -734,12 +725,12 @@ public class StringLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string.upper (s)
|
* string.upper (s)
|
||||||
*
|
*
|
||||||
* Receives a string and returns a copy of this string with all lowercase letters
|
* Receives a string and returns a copy of this string with all lowercase letters
|
||||||
* changed to uppercase. All other characters are left unchanged.
|
* changed to uppercase. All other characters are left unchanged.
|
||||||
* The definition of what a lowercase letter is depends on the current locale.
|
* The definition of what a lowercase letter is depends on the current locale.
|
||||||
*/
|
*/
|
||||||
static final class upper extends OneArgFunction {
|
static final class upper extends OneArgFunction {
|
||||||
public LuaValue call(LuaValue arg) {
|
public LuaValue call(LuaValue arg) {
|
||||||
@@ -773,7 +764,7 @@ public class StringLib extends TwoArgFunction {
|
|||||||
|
|
||||||
boolean anchor = false;
|
boolean anchor = false;
|
||||||
int poff = 0;
|
int poff = 0;
|
||||||
if ( pat.length() > 0 && pat.luaByte( 0 ) == '^' ) {
|
if ( pat.luaByte( 0 ) == '^' ) {
|
||||||
anchor = true;
|
anchor = true;
|
||||||
poff = 1;
|
poff = 1;
|
||||||
}
|
}
|
||||||
@@ -794,7 +785,7 @@ public class StringLib extends TwoArgFunction {
|
|||||||
return NIL;
|
return NIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int posrelat( int pos, int len ) {
|
private static int posrelat( int pos, int len ) {
|
||||||
return ( pos >= 0 ) ? pos : len + pos + 1;
|
return ( pos >= 0 ) ? pos : len + pos + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -816,7 +807,7 @@ public class StringLib extends TwoArgFunction {
|
|||||||
private static final byte MASK_CONTROL = 0x40;
|
private static final byte MASK_CONTROL = 0x40;
|
||||||
private static final byte MASK_HEXDIGIT = (byte)0x80;
|
private static final byte MASK_HEXDIGIT = (byte)0x80;
|
||||||
|
|
||||||
static final byte[] CHAR_TABLE;
|
private static final byte[] CHAR_TABLE;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
CHAR_TABLE = new byte[256];
|
CHAR_TABLE = new byte[256];
|
||||||
@@ -875,14 +866,8 @@ public class StringLib extends TwoArgFunction {
|
|||||||
lbuf.append( (byte) b );
|
lbuf.append( (byte) b );
|
||||||
} else {
|
} else {
|
||||||
++i; // skip ESC
|
++i; // skip ESC
|
||||||
b = (byte)(i < l ? news.luaByte( i ) : 0);
|
b = (byte) news.luaByte( i );
|
||||||
if ( !Character.isDigit( (char) b ) ) {
|
if ( !Character.isDigit( (char) b ) ) {
|
||||||
if (b != L_ESC) error( "invalid use of '" + (char)L_ESC +
|
|
||||||
"' in replacement string: after '" + (char)L_ESC +
|
|
||||||
"' must be '0'-'9' or '" + (char)L_ESC +
|
|
||||||
"', but found " + (i < l ? "symbol '" + (char)b + "' with code " + b +
|
|
||||||
" at pos " + (i + 1) :
|
|
||||||
"end of string"));
|
|
||||||
lbuf.append( b );
|
lbuf.append( b );
|
||||||
} else if ( b == '0' ) {
|
} else if ( b == '0' ) {
|
||||||
lbuf.append( s.substring( soff, e ) );
|
lbuf.append( s.substring( soff, e ) );
|
||||||
@@ -976,19 +961,19 @@ public class StringLib extends TwoArgFunction {
|
|||||||
switch ( p.luaByte( poffset++ ) ) {
|
switch ( p.luaByte( poffset++ ) ) {
|
||||||
case L_ESC:
|
case L_ESC:
|
||||||
if ( poffset == p.length() ) {
|
if ( poffset == p.length() ) {
|
||||||
error( "malformed pattern (ends with '%')" );
|
error( "malformed pattern (ends with %)" );
|
||||||
}
|
}
|
||||||
return poffset + 1;
|
return poffset + 1;
|
||||||
|
|
||||||
case '[':
|
case '[':
|
||||||
if ( poffset != p.length() && p.luaByte( poffset ) == '^' ) poffset++;
|
if ( p.luaByte( poffset ) == '^' ) poffset++;
|
||||||
do {
|
do {
|
||||||
if ( poffset == p.length() ) {
|
if ( poffset == p.length() ) {
|
||||||
error( "malformed pattern (missing ']')" );
|
error( "malformed pattern (missing ])" );
|
||||||
}
|
}
|
||||||
if ( p.luaByte( poffset++ ) == L_ESC && poffset < p.length() )
|
if ( p.luaByte( poffset++ ) == L_ESC && poffset != p.length() )
|
||||||
poffset++; /* skip escapes (e.g. '%]') */
|
poffset++;
|
||||||
} while ( poffset == p.length() || p.luaByte( poffset ) != ']' );
|
} while ( p.luaByte( poffset ) != ']' );
|
||||||
return poffset + 1;
|
return poffset + 1;
|
||||||
default:
|
default:
|
||||||
return poffset;
|
return poffset;
|
||||||
@@ -1053,7 +1038,7 @@ public class StringLib extends TwoArgFunction {
|
|||||||
*/
|
*/
|
||||||
int match( int soffset, int poffset ) {
|
int match( int soffset, int poffset ) {
|
||||||
while ( true ) {
|
while ( true ) {
|
||||||
// Check if we are at the end of the pattern -
|
// Check if we are at the end of the pattern -
|
||||||
// equivalent to the '\0' case in the C version, but our pattern
|
// equivalent to the '\0' case in the C version, but our pattern
|
||||||
// string is not NUL-terminated.
|
// string is not NUL-terminated.
|
||||||
if ( poffset == p.length() )
|
if ( poffset == p.length() )
|
||||||
@@ -1077,14 +1062,13 @@ public class StringLib extends TwoArgFunction {
|
|||||||
continue;
|
continue;
|
||||||
case 'f': {
|
case 'f': {
|
||||||
poffset += 2;
|
poffset += 2;
|
||||||
if ( poffset == p.length() || p.luaByte( poffset ) != '[' ) {
|
if ( p.luaByte( poffset ) != '[' ) {
|
||||||
error("Missing '[' after '%f' in pattern");
|
error("Missing [ after %f in pattern");
|
||||||
}
|
}
|
||||||
int ep = classend( poffset );
|
int ep = classend( poffset );
|
||||||
int previous = ( soffset == 0 ) ? '\0' : s.luaByte( soffset - 1 );
|
int previous = ( soffset == 0 ) ? -1 : s.luaByte( soffset - 1 );
|
||||||
int next = ( soffset == s.length() ) ? '\0' : s.luaByte( soffset );
|
|
||||||
if ( matchbracketclass( previous, poffset, ep - 1 ) ||
|
if ( matchbracketclass( previous, poffset, ep - 1 ) ||
|
||||||
!matchbracketclass( next, poffset, ep - 1 ) )
|
matchbracketclass( s.luaByte( soffset ), poffset, ep - 1 ) )
|
||||||
return -1;
|
return -1;
|
||||||
poffset = ep;
|
poffset = ep;
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -25,19 +25,19 @@ import org.luaj.vm2.LuaTable;
|
|||||||
import org.luaj.vm2.LuaValue;
|
import org.luaj.vm2.LuaValue;
|
||||||
import org.luaj.vm2.Varargs;
|
import org.luaj.vm2.Varargs;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subclass of {@link LibFunction} which implements the lua standard {@code table}
|
* Subclass of {@link LibFunction} which implements the lua standard {@code table}
|
||||||
* library.
|
* library.
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* Typically, this library is included as part of a call to either
|
* Typically, this library is included as part of a call to either
|
||||||
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = JsePlatform.standardGlobals();
|
* Globals globals = JsePlatform.standardGlobals();
|
||||||
* System.out.println( globals.get("table").get("length").call( LuaValue.tableOf() ) );
|
* System.out.println( globals.get("table").get("length").call( LuaValue.tableOf() ) );
|
||||||
* } </pre>
|
* } </pre>
|
||||||
* <p>
|
* <p>
|
||||||
* To instantiate and use it directly,
|
* To instantiate and use it directly,
|
||||||
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = new Globals();
|
* Globals globals = new Globals();
|
||||||
@@ -70,7 +70,7 @@ public class TableLib extends TwoArgFunction {
|
|||||||
table.set("sort", new sort());
|
table.set("sort", new sort());
|
||||||
table.set("unpack", new unpack());
|
table.set("unpack", new unpack());
|
||||||
env.set("table", table);
|
env.set("table", table);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("table", table);
|
env.get("package").get("loaded").set("table", table);
|
||||||
return NIL;
|
return NIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ abstract public class Visitor {
|
|||||||
visitExps(args.exps);
|
visitExps(args.exps);
|
||||||
}
|
}
|
||||||
public void visit(TableField field) {
|
public void visit(TableField field) {
|
||||||
if ( field.name != null )
|
if ( field.name != null );
|
||||||
visit( field.name );
|
visit( field.name );
|
||||||
if ( field.index != null )
|
if ( field.index != null )
|
||||||
field.index.accept(this);
|
field.index.accept(this);
|
||||||
|
|||||||
@@ -21,7 +21,6 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package org.luaj.vm2.lib.jse;
|
package org.luaj.vm2.lib.jse;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -134,7 +133,7 @@ public class CoerceJavaToLua {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static final Map COERCIONS = Collections.synchronizedMap(new HashMap());
|
static final Map COERCIONS = new HashMap();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Coercion boolCoercion = new BoolCoercion() ;
|
Coercion boolCoercion = new BoolCoercion() ;
|
||||||
|
|||||||
@@ -21,7 +21,6 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package org.luaj.vm2.lib.jse;
|
package org.luaj.vm2.lib.jse;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -107,10 +106,9 @@ public class JseBaseLib extends org.luaj.vm2.lib.BaseLib {
|
|||||||
if ( ! f.exists() )
|
if ( ! f.exists() )
|
||||||
return super.findResource(filename);
|
return super.findResource(filename);
|
||||||
try {
|
try {
|
||||||
return new BufferedInputStream(new FileInputStream(f));
|
return new FileInputStream(f);
|
||||||
} catch ( IOException ioe ) {
|
} catch ( IOException ioe ) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ package org.luaj.vm2.lib.jse;
|
|||||||
import org.luaj.vm2.Globals;
|
import org.luaj.vm2.Globals;
|
||||||
import org.luaj.vm2.LuaValue;
|
import org.luaj.vm2.LuaValue;
|
||||||
import org.luaj.vm2.lib.LibFunction;
|
import org.luaj.vm2.lib.LibFunction;
|
||||||
import org.luaj.vm2.lib.TwoArgFunction;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subclass of {@link LibFunction} which implements the lua standard {@code math}
|
* Subclass of {@link LibFunction} which implements the lua standard {@code math}
|
||||||
@@ -77,9 +76,8 @@ public class JseMathLib extends org.luaj.vm2.lib.MathLib {
|
|||||||
LuaValue math = env.get("math");
|
LuaValue math = env.get("math");
|
||||||
math.set("acos", new acos());
|
math.set("acos", new acos());
|
||||||
math.set("asin", new asin());
|
math.set("asin", new asin());
|
||||||
LuaValue atan = new atan2();
|
math.set("atan", new atan());
|
||||||
math.set("atan", atan);
|
math.set("atan2", new atan2());
|
||||||
math.set("atan2", atan);
|
|
||||||
math.set("cosh", new cosh());
|
math.set("cosh", new cosh());
|
||||||
math.set("exp", new exp());
|
math.set("exp", new exp());
|
||||||
math.set("log", new log());
|
math.set("log", new log());
|
||||||
@@ -91,21 +89,11 @@ public class JseMathLib extends org.luaj.vm2.lib.MathLib {
|
|||||||
|
|
||||||
static final class acos extends UnaryOp { protected double call(double d) { return Math.acos(d); } }
|
static final class acos extends UnaryOp { protected double call(double d) { return Math.acos(d); } }
|
||||||
static final class asin extends UnaryOp { protected double call(double d) { return Math.asin(d); } }
|
static final class asin extends UnaryOp { protected double call(double d) { return Math.asin(d); } }
|
||||||
static final class atan2 extends TwoArgFunction {
|
static final class atan extends UnaryOp { protected double call(double d) { return Math.atan(d); } }
|
||||||
public LuaValue call(LuaValue x, LuaValue y) {
|
static final class atan2 extends BinaryOp { protected double call(double y, double x) { return Math.atan2(y, x); } }
|
||||||
return valueOf(Math.atan2(x.checkdouble(), y.optdouble(1)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
static final class cosh extends UnaryOp { protected double call(double d) { return Math.cosh(d); } }
|
static final class cosh extends UnaryOp { protected double call(double d) { return Math.cosh(d); } }
|
||||||
static final class exp extends UnaryOp { protected double call(double d) { return Math.exp(d); } }
|
static final class exp extends UnaryOp { protected double call(double d) { return Math.exp(d); } }
|
||||||
static final class log extends TwoArgFunction {
|
static final class log extends UnaryOp { protected double call(double d) { return Math.log(d); } }
|
||||||
public LuaValue call(LuaValue x, LuaValue base) {
|
|
||||||
double nat = Math.log(x.checkdouble());
|
|
||||||
double b = base.optdouble(Math.E);
|
|
||||||
if (b != Math.E) nat /= Math.log(b);
|
|
||||||
return valueOf(nat);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
static final class pow extends BinaryOp { protected double call(double x, double y) { return Math.pow(x, y); } }
|
static final class pow extends BinaryOp { protected double call(double x, double y) { return Math.pow(x, y); } }
|
||||||
static final class sinh extends UnaryOp { protected double call(double d) { return Math.sinh(d); } }
|
static final class sinh extends UnaryOp { protected double call(double d) { return Math.sinh(d); } }
|
||||||
static final class tanh extends UnaryOp { protected double call(double d) { return Math.tanh(d); } }
|
static final class tanh extends UnaryOp { protected double call(double d) { return Math.tanh(d); } }
|
||||||
@@ -117,4 +105,3 @@ public class JseMathLib extends org.luaj.vm2.lib.MathLib {
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -74,13 +74,13 @@ import org.luaj.vm2.lib.OsLib;
|
|||||||
public class JseOsLib extends org.luaj.vm2.lib.OsLib {
|
public class JseOsLib extends org.luaj.vm2.lib.OsLib {
|
||||||
|
|
||||||
/** return code indicating the execute() threw an I/O exception */
|
/** return code indicating the execute() threw an I/O exception */
|
||||||
public static final int EXEC_IOEXCEPTION = 1;
|
public static int EXEC_IOEXCEPTION = 1;
|
||||||
|
|
||||||
/** return code indicating the execute() was interrupted */
|
/** return code indicating the execute() was interrupted */
|
||||||
public static final int EXEC_INTERRUPTED = -2;
|
public static int EXEC_INTERRUPTED = -2;
|
||||||
|
|
||||||
/** return code indicating the execute() threw an unknown exception */
|
/** return code indicating the execute() threw an unknown exception */
|
||||||
public static final int EXEC_ERROR = -3;
|
public static int EXEC_ERROR = -3;
|
||||||
|
|
||||||
/** public constructor */
|
/** public constructor */
|
||||||
public JseOsLib() {
|
public JseOsLib() {
|
||||||
@@ -120,7 +120,7 @@ public class JseOsLib extends org.luaj.vm2.lib.OsLib {
|
|||||||
if ( ! f.exists() )
|
if ( ! f.exists() )
|
||||||
throw new IOException("No such file or directory");
|
throw new IOException("No such file or directory");
|
||||||
if ( ! f.renameTo(new File(newname)) )
|
if ( ! f.renameTo(new File(newname)) )
|
||||||
throw new IOException("Failed to rename");
|
throw new IOException("Failed to delete");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String tmpname() {
|
protected String tmpname() {
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ package org.luaj.vm2.lib.jse;
|
|||||||
|
|
||||||
import org.luaj.vm2.Globals;
|
import org.luaj.vm2.Globals;
|
||||||
import org.luaj.vm2.LoadState;
|
import org.luaj.vm2.LoadState;
|
||||||
|
import org.luaj.vm2.LuaThread;
|
||||||
import org.luaj.vm2.LuaValue;
|
import org.luaj.vm2.LuaValue;
|
||||||
import org.luaj.vm2.Varargs;
|
|
||||||
import org.luaj.vm2.compiler.LuaC;
|
import org.luaj.vm2.compiler.LuaC;
|
||||||
import org.luaj.vm2.lib.Bit32Lib;
|
import org.luaj.vm2.lib.Bit32Lib;
|
||||||
import org.luaj.vm2.lib.CoroutineLib;
|
import org.luaj.vm2.lib.CoroutineLib;
|
||||||
@@ -34,10 +34,10 @@ import org.luaj.vm2.lib.ResourceFinder;
|
|||||||
import org.luaj.vm2.lib.StringLib;
|
import org.luaj.vm2.lib.StringLib;
|
||||||
import org.luaj.vm2.lib.TableLib;
|
import org.luaj.vm2.lib.TableLib;
|
||||||
|
|
||||||
/** The {@link org.luaj.vm2.lib.jse.JsePlatform} class is a convenience class to standardize
|
/** The {@link org.luaj.vm2.lib.jse.JsePlatform} class is a convenience class to standardize
|
||||||
* how globals tables are initialized for the JSE platform.
|
* how globals tables are initialized for the JSE platform.
|
||||||
* <p>
|
* <p>
|
||||||
* It is used to allocate either a set of standard globals using
|
* It is used to allocate either a set of standard globals using
|
||||||
* {@link #standardGlobals()} or debug globals using {@link #debugGlobals()}
|
* {@link #standardGlobals()} or debug globals using {@link #debugGlobals()}
|
||||||
* <p>
|
* <p>
|
||||||
* A simple example of initializing globals and using them from Java is:
|
* A simple example of initializing globals and using them from Java is:
|
||||||
@@ -51,7 +51,7 @@ import org.luaj.vm2.lib.TableLib;
|
|||||||
* globals.load( new FileInputStream("main.lua"), "main.lua" ).call();
|
* globals.load( new FileInputStream("main.lua"), "main.lua" ).call();
|
||||||
* } </pre>
|
* } </pre>
|
||||||
* <p>
|
* <p>
|
||||||
* although {@code require} could also be used:
|
* although {@code require} could also be used:
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* globals.get("require").call(LuaValue.valueOf("main"));
|
* globals.get("require").call(LuaValue.valueOf("main"));
|
||||||
* } </pre>
|
* } </pre>
|
||||||
@@ -72,8 +72,8 @@ import org.luaj.vm2.lib.TableLib;
|
|||||||
* <li>{@link org.luaj.vm2.lib.jse.JseOsLib}</li>
|
* <li>{@link org.luaj.vm2.lib.jse.JseOsLib}</li>
|
||||||
* <li>{@link org.luaj.vm2.lib.jse.LuajavaLib}</li>
|
* <li>{@link org.luaj.vm2.lib.jse.LuajavaLib}</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
* In addition, the {@link LuaC} compiler is installed so lua files may be loaded in their source form.
|
* In addition, the {@link LuaC} compiler is installed so lua files may be loaded in their source form.
|
||||||
* <p>
|
* <p>
|
||||||
* The debug globals are simply the standard globals plus the {@code debug} library {@link DebugLib}.
|
* The debug globals are simply the standard globals plus the {@code debug} library {@link DebugLib}.
|
||||||
* <p>
|
* <p>
|
||||||
* The class ensures that initialization is done in the correct order.
|
* The class ensures that initialization is done in the correct order.
|
||||||
@@ -97,7 +97,7 @@ public class JsePlatform {
|
|||||||
globals.load(new PackageLib());
|
globals.load(new PackageLib());
|
||||||
globals.load(new Bit32Lib());
|
globals.load(new Bit32Lib());
|
||||||
globals.load(new TableLib());
|
globals.load(new TableLib());
|
||||||
globals.load(new JseStringLib());
|
globals.load(new StringLib());
|
||||||
globals.load(new CoroutineLib());
|
globals.load(new CoroutineLib());
|
||||||
globals.load(new JseMathLib());
|
globals.load(new JseMathLib());
|
||||||
globals.load(new JseIoLib());
|
globals.load(new JseIoLib());
|
||||||
@@ -105,7 +105,7 @@ public class JsePlatform {
|
|||||||
globals.load(new LuajavaLib());
|
globals.load(new LuajavaLib());
|
||||||
LoadState.install(globals);
|
LoadState.install(globals);
|
||||||
LuaC.install(globals);
|
LuaC.install(globals);
|
||||||
return globals;
|
return globals;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Create standard globals including the {@link DebugLib} library.
|
/** Create standard globals including the {@link DebugLib} library.
|
||||||
@@ -123,12 +123,11 @@ public class JsePlatform {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Simple wrapper for invoking a lua function with command line arguments.
|
/** Simple wrapper for invoking a lua function with command line arguments.
|
||||||
* The supplied function is first given a new Globals object as its environment
|
* The supplied function is first given a new Globals object,
|
||||||
* then the program is run with arguments.
|
* then the program is run with arguments.
|
||||||
* @return {@link Varargs} containing any values returned by mainChunk.
|
|
||||||
*/
|
*/
|
||||||
public static Varargs luaMain(LuaValue mainChunk, String[] args) {
|
public static void luaMain(LuaValue mainChunk, String[] args) {
|
||||||
Globals g = standardGlobals();
|
Globals g = standardGlobals();
|
||||||
int n = args.length;
|
int n = args.length;
|
||||||
LuaValue[] vargs = new LuaValue[args.length];
|
LuaValue[] vargs = new LuaValue[args.length];
|
||||||
@@ -138,6 +137,6 @@ public class JsePlatform {
|
|||||||
arg.set("n", n);
|
arg.set("n", n);
|
||||||
g.set("arg", arg);
|
g.set("arg", arg);
|
||||||
mainChunk.initupvalue1(g);
|
mainChunk.initupvalue1(g);
|
||||||
return mainChunk.invoke(LuaValue.varargsOf(vargs));
|
mainChunk.invoke(LuaValue.varargsOf(vargs));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,38 +37,38 @@ import org.luaj.vm2.compiler.LuaC;
|
|||||||
import org.luaj.vm2.lib.LibFunction;
|
import org.luaj.vm2.lib.LibFunction;
|
||||||
import org.luaj.vm2.lib.VarArgFunction;
|
import org.luaj.vm2.lib.VarArgFunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subclass of {@link LibFunction} which implements the features of the luajava package.
|
* Subclass of {@link LibFunction} which implements the features of the luajava package.
|
||||||
* <p>
|
* <p>
|
||||||
* Luajava is an approach to mixing lua and java using simple functions that bind
|
* Luajava is an approach to mixing lua and java using simple functions that bind
|
||||||
* java classes and methods to lua dynamically. The API is documented on the
|
* java classes and methods to lua dynamically. The API is documented on the
|
||||||
* <a href="http://www.keplerproject.org/luajava/">luajava</a> documentation pages.
|
* <a href="http://www.keplerproject.org/luajava/">luajava</a> documentation pages.
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* Typically, this library is included as part of a call to
|
* Typically, this library is included as part of a call to
|
||||||
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()}
|
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()}
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = JsePlatform.standardGlobals();
|
* Globals globals = JsePlatform.standardGlobals();
|
||||||
* System.out.println( globals.get("luajava").get("bindClass").call( LuaValue.valueOf("java.lang.System") ).invokeMethod("currentTimeMillis") );
|
* System.out.println( globals.get("luajava").get("bindClass").call( LuaValue.valueOf("java.lang.System") ).invokeMethod("currentTimeMillis") );
|
||||||
* } </pre>
|
* } </pre>
|
||||||
* <p>
|
* <p>
|
||||||
* To instantiate and use it directly,
|
* To instantiate and use it directly,
|
||||||
* link it into your globals table via {@link Globals#load} using code such as:
|
* link it into your globals table via {@link Globals#load} using code such as:
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = new Globals();
|
* Globals globals = new Globals();
|
||||||
* globals.load(new JseBaseLib());
|
* globals.load(new JseBaseLib());
|
||||||
* globals.load(new PackageLib());
|
* globals.load(new PackageLib());
|
||||||
* globals.load(new LuajavaLib());
|
* globals.load(new LuajavaLib());
|
||||||
* globals.load(
|
* globals.load(
|
||||||
* "sys = luajava.bindClass('java.lang.System')\n"+
|
* "sys = luajava.bindClass('java.lang.System')\n"+
|
||||||
* "print ( sys:currentTimeMillis() )\n", "main.lua" ).call();
|
* "print ( sys:currentTimeMillis() )\n", "main.lua" ).call();
|
||||||
* } </pre>
|
* } </pre>
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
* The {@code luajava} library is available
|
* The {@code luajava} library is available
|
||||||
* on all JSE platforms via the call to {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()}
|
* on all JSE platforms via the call to {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()}
|
||||||
* and the luajava api's are simply invoked from lua.
|
* and the luajava api's are simply invoked from lua.
|
||||||
* Because it makes extensive use of Java's reflection API, it is not available
|
* Because it makes extensive use of Java's reflection API, it is not available
|
||||||
* on JME, but can be used in Android applications.
|
* on JME, but can be used in Android applications.
|
||||||
* <p>
|
* <p>
|
||||||
* This has been implemented to match as closely as possible the behavior in the corresponding library in C.
|
* This has been implemented to match as closely as possible the behavior in the corresponding library in C.
|
||||||
@@ -91,10 +91,10 @@ public class LuajavaLib extends VarArgFunction {
|
|||||||
static final int LOADLIB = 5;
|
static final int LOADLIB = 5;
|
||||||
|
|
||||||
static final String[] NAMES = {
|
static final String[] NAMES = {
|
||||||
"bindClass",
|
"bindClass",
|
||||||
"newInstance",
|
"newInstance",
|
||||||
"new",
|
"new",
|
||||||
"createProxy",
|
"createProxy",
|
||||||
"loadLib",
|
"loadLib",
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -112,7 +112,7 @@ public class LuajavaLib extends VarArgFunction {
|
|||||||
LuaTable t = new LuaTable();
|
LuaTable t = new LuaTable();
|
||||||
bind( t, this.getClass(), NAMES, BINDCLASS );
|
bind( t, this.getClass(), NAMES, BINDCLASS );
|
||||||
env.set("luajava", t);
|
env.set("luajava", t);
|
||||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("luajava", t);
|
env.get("package").get("loaded").set("luajava", t);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
case BINDCLASS: {
|
case BINDCLASS: {
|
||||||
@@ -122,13 +122,13 @@ public class LuajavaLib extends VarArgFunction {
|
|||||||
case NEWINSTANCE:
|
case NEWINSTANCE:
|
||||||
case NEW: {
|
case NEW: {
|
||||||
// get constructor
|
// get constructor
|
||||||
final LuaValue c = args.checkvalue(1);
|
final LuaValue c = args.checkvalue(1);
|
||||||
final Class clazz = (opcode==NEWINSTANCE? classForName(c.tojstring()): (Class) c.checkuserdata(Class.class));
|
final Class clazz = (opcode==NEWINSTANCE? classForName(c.tojstring()): (Class) c.checkuserdata(Class.class));
|
||||||
final Varargs consargs = args.subargs(2);
|
final Varargs consargs = args.subargs(2);
|
||||||
return JavaClass.forClass(clazz).getConstructor().invoke(consargs);
|
return JavaClass.forClass(clazz).getConstructor().invoke(consargs);
|
||||||
}
|
}
|
||||||
|
|
||||||
case CREATEPROXY: {
|
case CREATEPROXY: {
|
||||||
final int niface = args.narg()-1;
|
final int niface = args.narg()-1;
|
||||||
if ( niface <= 0 )
|
if ( niface <= 0 )
|
||||||
throw new LuaError("no interfaces");
|
throw new LuaError("no interfaces");
|
||||||
@@ -136,7 +136,7 @@ public class LuajavaLib extends VarArgFunction {
|
|||||||
|
|
||||||
// get the interfaces
|
// get the interfaces
|
||||||
final Class[] ifaces = new Class[niface];
|
final Class[] ifaces = new Class[niface];
|
||||||
for ( int i=0; i<niface; i++ )
|
for ( int i=0; i<niface; i++ )
|
||||||
ifaces[i] = classForName(args.checkjstring(i+1));
|
ifaces[i] = classForName(args.checkjstring(i+1));
|
||||||
|
|
||||||
// create the invocation handler
|
// create the invocation handler
|
||||||
@@ -191,16 +191,16 @@ public class LuajavaLib extends VarArgFunction {
|
|||||||
if ( func.isnil() )
|
if ( func.isnil() )
|
||||||
return null;
|
return null;
|
||||||
boolean isvarargs = ((method.getModifiers() & METHOD_MODIFIERS_VARARGS) != 0);
|
boolean isvarargs = ((method.getModifiers() & METHOD_MODIFIERS_VARARGS) != 0);
|
||||||
int n = args!=null? args.length: 0;
|
int n = args!=null? args.length: 0;
|
||||||
LuaValue[] v;
|
LuaValue[] v;
|
||||||
if ( isvarargs ) {
|
if ( isvarargs ) {
|
||||||
Object o = args[--n];
|
Object o = args[--n];
|
||||||
int m = Array.getLength( o );
|
int m = Array.getLength( o );
|
||||||
v = new LuaValue[n+m];
|
v = new LuaValue[n+m];
|
||||||
for ( int i=0; i<n; i++ )
|
for ( int i=0; i<n; i++ )
|
||||||
v[i] = CoerceJavaToLua.coerce(args[i]);
|
v[i] = CoerceJavaToLua.coerce(args[i]);
|
||||||
for ( int i=0; i<m; i++ )
|
for ( int i=0; i<m; i++ )
|
||||||
v[i+n] = CoerceJavaToLua.coerce(Array.get(o,i));
|
v[i+n] = CoerceJavaToLua.coerce(Array.get(o,i));
|
||||||
} else {
|
} else {
|
||||||
v = new LuaValue[n];
|
v = new LuaValue[n];
|
||||||
for ( int i=0; i<n; i++ )
|
for ( int i=0; i<n; i++ )
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ import org.luaj.vm2.compiler.DumpLoadEndianIntTest;
|
|||||||
import org.luaj.vm2.compiler.LuaParserTests;
|
import org.luaj.vm2.compiler.LuaParserTests;
|
||||||
import org.luaj.vm2.compiler.RegressionTests;
|
import org.luaj.vm2.compiler.RegressionTests;
|
||||||
import org.luaj.vm2.compiler.SimpleTests;
|
import org.luaj.vm2.compiler.SimpleTests;
|
||||||
import org.luaj.vm2.lib.jse.JsePlatformTest;
|
|
||||||
import org.luaj.vm2.lib.jse.LuaJavaCoercionTest;
|
import org.luaj.vm2.lib.jse.LuaJavaCoercionTest;
|
||||||
import org.luaj.vm2.lib.jse.LuajavaAccessibleMembersTest;
|
import org.luaj.vm2.lib.jse.LuajavaAccessibleMembersTest;
|
||||||
import org.luaj.vm2.lib.jse.LuajavaClassMembersTest;
|
import org.luaj.vm2.lib.jse.LuajavaClassMembersTest;
|
||||||
@@ -86,7 +85,6 @@ public class AllTests {
|
|||||||
|
|
||||||
// library tests
|
// library tests
|
||||||
TestSuite lib = new TestSuite("Library Tests");
|
TestSuite lib = new TestSuite("Library Tests");
|
||||||
lib.addTestSuite(JsePlatformTest.class);
|
|
||||||
lib.addTestSuite(LuajavaAccessibleMembersTest.class);
|
lib.addTestSuite(LuajavaAccessibleMembersTest.class);
|
||||||
lib.addTestSuite(LuajavaClassMembersTest.class);
|
lib.addTestSuite(LuajavaClassMembersTest.class);
|
||||||
lib.addTestSuite(LuaJavaCoercionTest.class);
|
lib.addTestSuite(LuaJavaCoercionTest.class);
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
package org.luaj.vm2.lib.jse;
|
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
|
|
||||||
import org.luaj.vm2.Globals;
|
|
||||||
import org.luaj.vm2.LuaValue;
|
|
||||||
import org.luaj.vm2.Varargs;
|
|
||||||
|
|
||||||
|
|
||||||
public class JsePlatformTest extends TestCase {
|
|
||||||
public void testLuaMainPassesArguments() {
|
|
||||||
Globals globals = JsePlatform.standardGlobals();
|
|
||||||
LuaValue chunk = globals.load("return #arg, arg.n, arg[2], arg[1]");
|
|
||||||
Varargs results = JsePlatform.luaMain(chunk, new String[] { "aaa", "bbb" });
|
|
||||||
assertEquals(results.narg(), 4);
|
|
||||||
assertEquals(results.arg(1), LuaValue.valueOf(2));
|
|
||||||
assertEquals(results.arg(2), LuaValue.valueOf(2));
|
|
||||||
assertEquals(results.arg(3), LuaValue.valueOf("bbb"));
|
|
||||||
assertEquals(results.arg(4), LuaValue.valueOf("aaa"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +1 @@
|
|||||||
version: 3.0.2
|
version: 3.0.1
|
||||||
Reference in New Issue
Block a user