Updated to Lua 5.4
This commit is contained in:
7
jme/src/main/java/javax/microedition/io/Connection.java
Normal file
7
jme/src/main/java/javax/microedition/io/Connection.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package javax.microedition.io;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface Connection {
|
||||
void close() throws IOException;
|
||||
}
|
||||
125
jme/src/main/java/javax/microedition/io/Connector.java
Normal file
125
jme/src/main/java/javax/microedition/io/Connector.java
Normal file
@@ -0,0 +1,125 @@
|
||||
package javax.microedition.io;
|
||||
|
||||
import java.io.FilterInputStream;
|
||||
import java.io.FilterOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
|
||||
public final class Connector {
|
||||
public static final int READ = 1;
|
||||
public static final int WRITE = 2;
|
||||
public static final int READ_WRITE = READ | WRITE;
|
||||
|
||||
private Connector() {
|
||||
}
|
||||
|
||||
public static Connection open(String name, int mode) throws IOException {
|
||||
if (name == null || !name.startsWith("file:///")) {
|
||||
throw new IOException("unsupported connection: " + name);
|
||||
}
|
||||
if (mode != READ && mode != WRITE && mode != READ_WRITE) {
|
||||
throw new IOException("unsupported connector mode: " + mode);
|
||||
}
|
||||
return new FileStreamConnection(toPath(name), mode);
|
||||
}
|
||||
|
||||
private static Path toPath(String name) {
|
||||
String raw = name.substring("file:///".length());
|
||||
if (raw.length() > 2 && raw.charAt(1) == ':' && raw.charAt(2) == '/') {
|
||||
return Paths.get(raw);
|
||||
}
|
||||
return Paths.get(raw.startsWith("/") ? raw.substring(1) : raw);
|
||||
}
|
||||
|
||||
private static final class FileStreamConnection implements StreamConnection {
|
||||
private final Path path;
|
||||
private final int mode;
|
||||
private InputStream input;
|
||||
private OutputStream output;
|
||||
private boolean closed;
|
||||
|
||||
private FileStreamConnection(Path path, int mode) {
|
||||
this.path = path;
|
||||
this.mode = mode;
|
||||
}
|
||||
|
||||
public InputStream openInputStream() throws IOException {
|
||||
ensureOpen();
|
||||
if ((mode & READ) == 0) {
|
||||
throw new IOException("connection not opened for reading");
|
||||
}
|
||||
if (input != null) {
|
||||
throw new IOException("input stream already open");
|
||||
}
|
||||
input = new FilterInputStream(Files.newInputStream(path, StandardOpenOption.READ)) {
|
||||
public void close() throws IOException {
|
||||
super.close();
|
||||
input = null;
|
||||
}
|
||||
};
|
||||
return input;
|
||||
}
|
||||
|
||||
public OutputStream openOutputStream() throws IOException {
|
||||
ensureOpen();
|
||||
if ((mode & WRITE) == 0) {
|
||||
throw new IOException("connection not opened for writing");
|
||||
}
|
||||
if (output != null) {
|
||||
throw new IOException("output stream already open");
|
||||
}
|
||||
Path parent = path.getParent();
|
||||
if (parent != null) {
|
||||
Files.createDirectories(parent);
|
||||
}
|
||||
output = new FilterOutputStream(Files.newOutputStream(path,
|
||||
StandardOpenOption.CREATE,
|
||||
StandardOpenOption.TRUNCATE_EXISTING,
|
||||
StandardOpenOption.WRITE)) {
|
||||
public void close() throws IOException {
|
||||
super.close();
|
||||
output = null;
|
||||
}
|
||||
};
|
||||
return output;
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
if (closed) {
|
||||
return;
|
||||
}
|
||||
closed = true;
|
||||
IOException failure = null;
|
||||
if (input != null) {
|
||||
try {
|
||||
input.close();
|
||||
} catch (IOException e) {
|
||||
failure = e;
|
||||
}
|
||||
}
|
||||
if (output != null) {
|
||||
try {
|
||||
output.close();
|
||||
} catch (IOException e) {
|
||||
if (failure == null) {
|
||||
failure = e;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (failure != null) {
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureOpen() throws IOException {
|
||||
if (closed) {
|
||||
throw new IOException("connection closed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package javax.microedition.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public interface InputConnection extends Connection {
|
||||
InputStream openInputStream() throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package javax.microedition.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public interface OutputConnection extends Connection {
|
||||
OutputStream openOutputStream() throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package javax.microedition.io;
|
||||
|
||||
public interface StreamConnection extends InputConnection, OutputConnection {
|
||||
}
|
||||
@@ -96,7 +96,6 @@ public class JmePlatform {
|
||||
globals.load(new BaseLib());
|
||||
globals.load(new PackageLib());
|
||||
globals.load(new CjsonLib());
|
||||
globals.load(new Bit32Lib());
|
||||
globals.load(new OsLib());
|
||||
globals.load(new MathLib());
|
||||
globals.load(new TableLib());
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package org.luaj.vm2;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import org.luaj.vm2.compiler.LuaC;
|
||||
import org.luaj.vm2.libs.BaseLib;
|
||||
import org.luaj.vm2.libs.CjsonLib;
|
||||
import org.luaj.vm2.libs.CoroutineLib;
|
||||
import org.luaj.vm2.libs.MathLib;
|
||||
import org.luaj.vm2.libs.OsLib;
|
||||
import org.luaj.vm2.libs.PackageLib;
|
||||
import org.luaj.vm2.libs.StringLib;
|
||||
import org.luaj.vm2.libs.TableLib;
|
||||
import org.luaj.vm2.libs.Utf8Lib;
|
||||
|
||||
public final class Lua54JmeCoreSmokeTestMain {
|
||||
private static Globals newGlobals() {
|
||||
Globals globals = new Globals();
|
||||
globals.load(new BaseLib());
|
||||
globals.load(new PackageLib());
|
||||
globals.load(new CjsonLib());
|
||||
globals.load(new OsLib());
|
||||
globals.load(new MathLib());
|
||||
globals.load(new TableLib());
|
||||
globals.load(new StringLib());
|
||||
globals.load(new Utf8Lib());
|
||||
globals.load(new CoroutineLib());
|
||||
LoadState.install(globals);
|
||||
LuaC.install(globals);
|
||||
return globals;
|
||||
}
|
||||
|
||||
private static void check(String name, Varargs actual, LuaValue... expected) {
|
||||
if (actual.narg() != expected.length) {
|
||||
throw new IllegalStateException(name + " expected " + expected.length + " values but got " + actual.narg() + ": " + actual);
|
||||
}
|
||||
for (int i = 0; i < expected.length; i++) {
|
||||
LuaValue value = actual.arg(i + 1);
|
||||
if (!value.eq_b(expected[i])) {
|
||||
throw new IllegalStateException(name + " mismatch at #" + (i + 1) + ": expected " + expected[i] + " but got " + value);
|
||||
}
|
||||
}
|
||||
System.out.println("ok " + name + " -> " + actual);
|
||||
}
|
||||
|
||||
private static Varargs run(String name, String script) throws Exception {
|
||||
Globals globals = newGlobals();
|
||||
return globals.load(new StringReader(script), name).invoke();
|
||||
}
|
||||
|
||||
private static void checkCompileError(String name, String script, String expectedMessagePart) throws Exception {
|
||||
Globals globals = newGlobals();
|
||||
try {
|
||||
globals.load(new StringReader(script), name);
|
||||
throw new IllegalStateException(name + " expected compile error containing: " + expectedMessagePart);
|
||||
} catch (LuaError e) {
|
||||
if (e.getMessage() == null || e.getMessage().indexOf(expectedMessagePart) < 0) {
|
||||
throw new IllegalStateException(name + " unexpected compile error: " + e.getMessage(), e);
|
||||
}
|
||||
System.out.println("ok " + name + " -> " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
check(
|
||||
"bit32_absent_jme_core",
|
||||
run("bit32_absent_jme_core", "return bit32 == nil\n"),
|
||||
LuaValue.TRUE);
|
||||
|
||||
check(
|
||||
"randomseed_54_jme_core",
|
||||
run("randomseed_54_jme_core",
|
||||
"local a, b = math.randomseed(7, 9)\n" +
|
||||
"local x1, x2 = math.random(), math.random(1, 1000000)\n" +
|
||||
"math.randomseed(7, 9)\n" +
|
||||
"local y1, y2 = math.random(), math.random(1, 1000000)\n" +
|
||||
"return a, b, x1 == y1 and x2 == y2\n"),
|
||||
LuaValue.valueOf(7),
|
||||
LuaValue.valueOf(9),
|
||||
LuaValue.TRUE);
|
||||
|
||||
checkCompileError(
|
||||
"const_local_jme_core",
|
||||
"local x <const> = 1\nx = 2\n",
|
||||
"const variable");
|
||||
|
||||
check(
|
||||
"close_scope_jme_core",
|
||||
run("close_scope_jme_core",
|
||||
"local mt = { __close = function(self, err) _G.marker = err or 'closed' end }\n" +
|
||||
"do local h <close> = setmetatable({}, mt) end\n" +
|
||||
"return marker\n"),
|
||||
LuaValue.valueOf("closed"));
|
||||
}
|
||||
}
|
||||
66
jme/src/test/java/org/luaj/vm2/Lua54JmeSmokeTestMain.java
Normal file
66
jme/src/test/java/org/luaj/vm2/Lua54JmeSmokeTestMain.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package org.luaj.vm2;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import org.luaj.vm2.libs.jme.JmePlatform;
|
||||
|
||||
public final class Lua54JmeSmokeTestMain {
|
||||
private static void check(String name, Varargs actual, LuaValue... expected) {
|
||||
if (actual.narg() != expected.length) {
|
||||
throw new IllegalStateException(name + " expected " + expected.length + " values but got " + actual.narg() + ": " + actual);
|
||||
}
|
||||
for (int i = 0; i < expected.length; i++) {
|
||||
LuaValue value = actual.arg(i + 1);
|
||||
if (!value.eq_b(expected[i])) {
|
||||
throw new IllegalStateException(name + " mismatch at #" + (i + 1) + ": expected " + expected[i] + " but got " + value);
|
||||
}
|
||||
}
|
||||
System.out.println("ok " + name + " -> " + actual);
|
||||
}
|
||||
|
||||
private static Varargs run(String name, String script) throws Exception {
|
||||
Globals globals = JmePlatform.debugGlobals();
|
||||
return globals.load(new StringReader(script), name).invoke();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
check(
|
||||
"bit32_absent_jme",
|
||||
run("bit32_absent_jme", "return bit32 == nil\n"),
|
||||
LuaValue.TRUE);
|
||||
|
||||
check(
|
||||
"randomseed_54_jme",
|
||||
run("randomseed_54_jme",
|
||||
"local a, b = math.randomseed(13, 17)\n" +
|
||||
"local x1, x2 = math.random(), math.random(1, 1000000)\n" +
|
||||
"math.randomseed(13, 17)\n" +
|
||||
"local y1, y2 = math.random(), math.random(1, 1000000)\n" +
|
||||
"return a, b, x1 == y1 and x2 == y2\n"),
|
||||
LuaValue.valueOf(13),
|
||||
LuaValue.valueOf(17),
|
||||
LuaValue.TRUE);
|
||||
|
||||
check(
|
||||
"close_scope_jme",
|
||||
run("close_scope_jme",
|
||||
"local mt = { __close = function(self, err) _G.marker = err or 'closed' end }\n" +
|
||||
"do local h <close> = setmetatable({}, mt) end\n" +
|
||||
"return marker\n"),
|
||||
LuaValue.valueOf("closed"));
|
||||
|
||||
check(
|
||||
"io_roundtrip_jme",
|
||||
run("io_roundtrip_jme",
|
||||
"local path = 'build/lua54-jme-io-smoke.txt'\n" +
|
||||
"local out = assert(io.open(path, 'w'))\n" +
|
||||
"out:write('smoke')\n" +
|
||||
"out:close()\n" +
|
||||
"local input = assert(io.open(path, 'r'))\n" +
|
||||
"local text = input:read('*a')\n" +
|
||||
"input:close()\n" +
|
||||
"return text, type(debug)\n"),
|
||||
LuaValue.valueOf("smoke"),
|
||||
LuaValue.valueOf("table"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user