Fix build errors for j2me. #32

This commit is contained in:
Enyby
2019-03-09 17:38:32 +02:00
parent e70eb5edc2
commit 6694c375c9
4 changed files with 53 additions and 42 deletions

View File

@@ -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 StringLib()); globals.load(new JseStringLib());
globals.load(new CoroutineLib()); globals.load(new CoroutineLib());
globals.load(new JseMathLib()); globals.load(new JseMathLib());
globals.load(new JseIoLib()); globals.load(new JseIoLib());

View File

@@ -1,12 +1,26 @@
import org.luaj.vm2.*; import org.luaj.vm2.Globals;
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.*; import org.luaj.vm2.lib.Bit32Lib;
import org.luaj.vm2.lib.jse.*; import org.luaj.vm2.lib.DebugLib;
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.
* *
@@ -29,7 +43,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 StringLib()); server_globals.load(new JseStringLib());
// 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.
@@ -47,7 +61,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 = {}" );
@@ -61,9 +75,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" );
@@ -75,21 +89,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 StringLib()); user_globals.load(new JseStringLib());
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());
@@ -98,12 +112,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.
@@ -111,18 +125,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.");
} }

View File

@@ -49,7 +49,7 @@ import org.luaj.vm2.compiler.DumpState;
* 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 StringLib()); * globals.load(new JseStringLib());
* 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>
@@ -230,7 +230,7 @@ public class StringLib extends TwoArgFunction {
* This function does not accept string values containing embedded zeros, * This function does not accept string values containing embedded zeros,
* except as arguments to the q option. * except as arguments to the q option.
*/ */
static final class format extends VarArgFunction { 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();
@@ -330,7 +330,7 @@ public class StringLib extends TwoArgFunction {
private static final String FLAGS = "-+ #0"; private static final String FLAGS = "-+ #0";
static class FormatDesc { class FormatDesc {
private boolean leftAdjust; private boolean leftAdjust;
private boolean zeroPad; private boolean zeroPad;
@@ -470,13 +470,7 @@ public class StringLib extends TwoArgFunction {
} }
public void format(Buffer buf, double x) { public void format(Buffer buf, double x) {
String out; buf.append( StringLib.this.format(src, x) );
try {
out = String.format(src, x);
} catch (Throwable e) {
out = String.valueOf( x );
}
buf.append( out );
} }
public void format(Buffer buf, LuaString s) { public void format(Buffer buf, LuaString s) {
@@ -486,13 +480,17 @@ public class StringLib extends TwoArgFunction {
buf.append(s); buf.append(s);
} }
public static final void pad(Buffer buf, char c, int n) { public 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)
* *

View File

@@ -23,7 +23,6 @@ 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.Varargs;
import org.luaj.vm2.compiler.LuaC; import org.luaj.vm2.compiler.LuaC;
@@ -35,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:
@@ -52,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>
@@ -73,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.
@@ -98,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 StringLib()); globals.load(new JseStringLib());
globals.load(new CoroutineLib()); globals.load(new CoroutineLib());
globals.load(new JseMathLib()); globals.load(new JseMathLib());
globals.load(new JseIoLib()); globals.load(new JseIoLib());
@@ -106,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.
@@ -124,9 +123,9 @@ 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 as its environment
* then the program is run with arguments. * then the program is run with arguments.
* @return {@link Varargs} containing any values returned by mainChunk. * @return {@link Varargs} containing any values returned by mainChunk.
*/ */
public static Varargs luaMain(LuaValue mainChunk, String[] args) { public static Varargs luaMain(LuaValue mainChunk, String[] args) {