Skip to content

Commit 6694c37

Browse files
author
Enyby
committed
Fix build errors for j2me. luaj#32
1 parent e70eb5e commit 6694c37

File tree

4 files changed

+53
-42
lines changed

4 files changed

+53
-42
lines changed

examples/jse/SampleApplet.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
import org.luaj.vm2.lib.CoroutineLib;
1212
import org.luaj.vm2.lib.PackageLib;
1313
import org.luaj.vm2.lib.ResourceFinder;
14-
import org.luaj.vm2.lib.StringLib;
1514
import org.luaj.vm2.lib.TableLib;
1615
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
1716
import org.luaj.vm2.lib.jse.JseBaseLib;
1817
import org.luaj.vm2.lib.jse.JseIoLib;
1918
import org.luaj.vm2.lib.jse.JseMathLib;
2019
import org.luaj.vm2.lib.jse.JseOsLib;
20+
import org.luaj.vm2.lib.jse.JseStringLib;
2121
import org.luaj.vm2.lib.jse.LuajavaLib;
2222

2323
/**
@@ -76,7 +76,7 @@ public void init() {
7676
globals.load(new PackageLib());
7777
globals.load(new Bit32Lib());
7878
globals.load(new TableLib());
79-
globals.load(new StringLib());
79+
globals.load(new JseStringLib());
8080
globals.load(new CoroutineLib());
8181
globals.load(new JseMathLib());
8282
globals.load(new JseIoLib());

examples/jse/SampleSandboxed.java

+32-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
import org.luaj.vm2.*;
1+
import org.luaj.vm2.Globals;
2+
import org.luaj.vm2.LoadState;
3+
import org.luaj.vm2.LuaBoolean;
4+
import org.luaj.vm2.LuaString;
5+
import org.luaj.vm2.LuaTable;
6+
import org.luaj.vm2.LuaThread;
7+
import org.luaj.vm2.LuaValue;
8+
import org.luaj.vm2.Varargs;
29
import org.luaj.vm2.compiler.LuaC;
3-
import org.luaj.vm2.lib.*;
4-
import org.luaj.vm2.lib.jse.*;
10+
import org.luaj.vm2.lib.Bit32Lib;
11+
import org.luaj.vm2.lib.DebugLib;
12+
import org.luaj.vm2.lib.PackageLib;
13+
import org.luaj.vm2.lib.TableLib;
14+
import org.luaj.vm2.lib.TwoArgFunction;
15+
import org.luaj.vm2.lib.ZeroArgFunction;
16+
import org.luaj.vm2.lib.jse.JseBaseLib;
17+
import org.luaj.vm2.lib.jse.JseMathLib;
18+
import org.luaj.vm2.lib.jse.JseStringLib;
519

620
/** Simple program that illustrates basic sand-boxing of client scripts
721
* in a server environment.
822
*
9-
* <p>Although this sandboxing is done primarily in Java here, these
23+
* <p>Although this sandboxing is done primarily in Java here, these
1024
* same techniques should all be possible directly from lua using metatables,
1125
* and examples are shown in examples/lua/samplesandboxed.lua.
1226
*
@@ -29,7 +43,7 @@ public static void main(String[] args) {
2943
server_globals = new Globals();
3044
server_globals.load(new JseBaseLib());
3145
server_globals.load(new PackageLib());
32-
server_globals.load(new StringLib());
46+
server_globals.load(new JseStringLib());
3347

3448
// To load scripts, we occasionally need a math library in addition to compiler support.
3549
// To limit scripts using the debug library, they must be closures, so we only install LuaC.
@@ -47,7 +61,7 @@ public static void main(String[] args) {
4761
runScriptInSandbox( "return getmetatable('abc').len" );
4862
runScriptInSandbox( "return getmetatable('abc').__index" );
4963

50-
// Example user scripts that attempt rogue operations, and will fail.
64+
// Example user scripts that attempt rogue operations, and will fail.
5165
runScriptInSandbox( "return setmetatable('abc', {})" );
5266
runScriptInSandbox( "getmetatable('abc').len = function() end" );
5367
runScriptInSandbox( "getmetatable('abc').__index = {}" );
@@ -61,9 +75,9 @@ public static void main(String[] args) {
6175
LuaValue.ADD, new TwoArgFunction() {
6276
public LuaValue call(LuaValue x, LuaValue y) {
6377
return LuaValue.valueOf(
64-
(x == TRUE ? 1.0 : x.todouble()) +
78+
(x == TRUE ? 1.0 : x.todouble()) +
6579
(y == TRUE ? 1.0 : y.todouble()) );
66-
}
80+
}
6781
},
6882
}));
6983
runScriptInSandbox( "return 5 + 6, 5 + true, false + 6" );
@@ -75,21 +89,21 @@ public LuaValue call(LuaValue x, LuaValue y) {
7589
// that contain functions that can be abused.
7690
static void runScriptInSandbox(String script) {
7791

78-
// Each script will have it's own set of globals, which should
92+
// Each script will have it's own set of globals, which should
7993
// prevent leakage between scripts running on the same server.
8094
Globals user_globals = new Globals();
8195
user_globals.load(new JseBaseLib());
8296
user_globals.load(new PackageLib());
8397
user_globals.load(new Bit32Lib());
8498
user_globals.load(new TableLib());
85-
user_globals.load(new StringLib());
99+
user_globals.load(new JseStringLib());
86100
user_globals.load(new JseMathLib());
87101

88102
// This library is dangerous as it gives unfettered access to the
89-
// entire Java VM, so it's not suitable within this lightweight sandbox.
103+
// entire Java VM, so it's not suitable within this lightweight sandbox.
90104
// user_globals.load(new LuajavaLib());
91105

92-
// Starting coroutines in scripts will result in threads that are
106+
// Starting coroutines in scripts will result in threads that are
93107
// not under the server control, so this libary should probably remain out.
94108
// user_globals.load(new CoroutineLib());
95109

@@ -98,31 +112,31 @@ static void runScriptInSandbox(String script) {
98112
// user_globals.load(new JseIoLib());
99113
// user_globals.load(new JseOsLib());
100114

101-
// Loading and compiling scripts from within scripts may also be
115+
// Loading and compiling scripts from within scripts may also be
102116
// prohibited, though in theory it should be fairly safe.
103117
// LoadState.install(user_globals);
104118
// LuaC.install(user_globals);
105119

106-
// The debug library must be loaded for hook functions to work, which
120+
// The debug library must be loaded for hook functions to work, which
107121
// allow us to limit scripts to run a certain number of instructions at a time.
108122
// However we don't wish to expose the library in the user globals,
109123
// so it is immediately removed from the user globals once created.
110124
user_globals.load(new DebugLib());
111125
LuaValue sethook = user_globals.get("debug").get("sethook");
112126
user_globals.set("debug", LuaValue.NIL);
113127

114-
// Set up the script to run in its own lua thread, which allows us
128+
// Set up the script to run in its own lua thread, which allows us
115129
// to set a hook function that limits the script to a specific number of cycles.
116-
// Note that the environment is set to the user globals, even though the
130+
// Note that the environment is set to the user globals, even though the
117131
// compiling is done with the server globals.
118132
LuaValue chunk = server_globals.load(script, "main", user_globals);
119133
LuaThread thread = new LuaThread(user_globals, chunk);
120134

121-
// Set the hook function to immediately throw an Error, which will not be
135+
// Set the hook function to immediately throw an Error, which will not be
122136
// handled by any Lua code other than the coroutine.
123137
LuaValue hookfunc = new ZeroArgFunction() {
124138
public LuaValue call() {
125-
// A simple lua error may be caught by the script, but a
139+
// A simple lua error may be caught by the script, but a
126140
// Java Error will pass through to top and stop the script.
127141
throw new Error("Script overran resource limits.");
128142
}

src/core/org/luaj/vm2/lib/StringLib.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
* Globals globals = new Globals();
5050
* globals.load(new JseBaseLib());
5151
* globals.load(new PackageLib());
52-
* globals.load(new StringLib());
52+
* globals.load(new JseStringLib());
5353
* System.out.println( globals.get("string").get("upper").call( LuaValue.valueOf("abcde") ) );
5454
* } </pre>
5555
* <p>
@@ -230,7 +230,7 @@ public Varargs invoke(Varargs args) {
230230
* This function does not accept string values containing embedded zeros,
231231
* except as arguments to the q option.
232232
*/
233-
static final class format extends VarArgFunction {
233+
final class format extends VarArgFunction {
234234
public Varargs invoke(Varargs args) {
235235
LuaString fmt = args.checkstring( 1 );
236236
final int n = fmt.length();
@@ -330,7 +330,7 @@ static void addquoted(Buffer buf, LuaString s) {
330330

331331
private static final String FLAGS = "-+ #0";
332332

333-
static class FormatDesc {
333+
class FormatDesc {
334334

335335
private boolean leftAdjust;
336336
private boolean zeroPad;
@@ -470,13 +470,7 @@ else if ( precision == -1 && zeroPad && width > minwidth )
470470
}
471471

472472
public void format(Buffer buf, double x) {
473-
String out;
474-
try {
475-
out = String.format(src, x);
476-
} catch (Throwable e) {
477-
out = String.valueOf( x );
478-
}
479-
buf.append( out );
473+
buf.append( StringLib.this.format(src, x) );
480474
}
481475

482476
public void format(Buffer buf, LuaString s) {
@@ -486,13 +480,17 @@ public void format(Buffer buf, LuaString s) {
486480
buf.append(s);
487481
}
488482

489-
public static final void pad(Buffer buf, char c, int n) {
483+
public final void pad(Buffer buf, char c, int n) {
490484
byte b = (byte)c;
491485
while ( n-- > 0 )
492486
buf.append(b);
493487
}
494488
}
495489

490+
protected String format(String src, double x) {
491+
return String.valueOf(x);
492+
}
493+
496494
/**
497495
* string.gmatch (s, pattern)
498496
*

src/jse/org/luaj/vm2/lib/jse/JsePlatform.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import org.luaj.vm2.Globals;
2525
import org.luaj.vm2.LoadState;
26-
import org.luaj.vm2.LuaThread;
2726
import org.luaj.vm2.LuaValue;
2827
import org.luaj.vm2.Varargs;
2928
import org.luaj.vm2.compiler.LuaC;
@@ -35,10 +34,10 @@
3534
import org.luaj.vm2.lib.StringLib;
3635
import org.luaj.vm2.lib.TableLib;
3736

38-
/** The {@link org.luaj.vm2.lib.jse.JsePlatform} class is a convenience class to standardize
39-
* how globals tables are initialized for the JSE platform.
37+
/** The {@link org.luaj.vm2.lib.jse.JsePlatform} class is a convenience class to standardize
38+
* how globals tables are initialized for the JSE platform.
4039
* <p>
41-
* It is used to allocate either a set of standard globals using
40+
* It is used to allocate either a set of standard globals using
4241
* {@link #standardGlobals()} or debug globals using {@link #debugGlobals()}
4342
* <p>
4443
* A simple example of initializing globals and using them from Java is:
@@ -52,7 +51,7 @@
5251
* globals.load( new FileInputStream("main.lua"), "main.lua" ).call();
5352
* } </pre>
5453
* <p>
55-
* although {@code require} could also be used:
54+
* although {@code require} could also be used:
5655
* <pre> {@code
5756
* globals.get("require").call(LuaValue.valueOf("main"));
5857
* } </pre>
@@ -73,8 +72,8 @@
7372
* <li>{@link org.luaj.vm2.lib.jse.JseOsLib}</li>
7473
* <li>{@link org.luaj.vm2.lib.jse.LuajavaLib}</li>
7574
* </ul>
76-
* In addition, the {@link LuaC} compiler is installed so lua files may be loaded in their source form.
77-
* <p>
75+
* In addition, the {@link LuaC} compiler is installed so lua files may be loaded in their source form.
76+
* <p>
7877
* The debug globals are simply the standard globals plus the {@code debug} library {@link DebugLib}.
7978
* <p>
8079
* The class ensures that initialization is done in the correct order.
@@ -98,15 +97,15 @@ public static Globals standardGlobals() {
9897
globals.load(new PackageLib());
9998
globals.load(new Bit32Lib());
10099
globals.load(new TableLib());
101-
globals.load(new StringLib());
100+
globals.load(new JseStringLib());
102101
globals.load(new CoroutineLib());
103102
globals.load(new JseMathLib());
104103
globals.load(new JseIoLib());
105104
globals.load(new JseOsLib());
106105
globals.load(new LuajavaLib());
107106
LoadState.install(globals);
108107
LuaC.install(globals);
109-
return globals;
108+
return globals;
110109
}
111110

112111
/** Create standard globals including the {@link DebugLib} library.
@@ -124,9 +123,9 @@ public static Globals debugGlobals() {
124123
}
125124

126125

127-
/** Simple wrapper for invoking a lua function with command line arguments.
126+
/** Simple wrapper for invoking a lua function with command line arguments.
128127
* The supplied function is first given a new Globals object as its environment
129-
* then the program is run with arguments.
128+
* then the program is run with arguments.
130129
* @return {@link Varargs} containing any values returned by mainChunk.
131130
*/
132131
public static Varargs luaMain(LuaValue mainChunk, String[] args) {

0 commit comments

Comments
 (0)