From edbe168e4223631b144d5a503ad1db0b1d918aee Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Tue, 25 Sep 2007 00:32:25 +0000 Subject: [PATCH] Add Platform manager --- .../java/lua/addon/compile/Compiler.java | 7 +-- .../java/lua/addon/compile/FuncState.java | 2 +- .../java/lua/addon/luacompat/LuaCompat.java | 2 +- .../java/lua/addon/luacompat/Platform.java | 58 +++++++++++++++++++ 4 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 src/addon/java/lua/addon/luacompat/Platform.java diff --git a/src/addon/java/lua/addon/compile/Compiler.java b/src/addon/java/lua/addon/compile/Compiler.java index 6287f46e..920c44e5 100644 --- a/src/addon/java/lua/addon/compile/Compiler.java +++ b/src/addon/java/lua/addon/compile/Compiler.java @@ -2,10 +2,10 @@ package lua.addon.compile; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.Reader; import java.util.Hashtable; +import lua.addon.luacompat.Platform; import lua.io.Proto; import lua.value.LString; @@ -33,10 +33,9 @@ public class Compiler { int c = stream.read(); if ( c == LUAC_BINARY_SIG ) return null; - // TODO: handle UTF-8 here! - InputStreamReader isr = new InputStreamReader(stream); + Reader r = Platform.getInstance().createReader( stream ); Compiler compiler = new Compiler(); - return compiler.luaY_parser(c, isr, name); + return compiler.luaY_parser(c, r, name); } public int nCcalls; diff --git a/src/addon/java/lua/addon/compile/FuncState.java b/src/addon/java/lua/addon/compile/FuncState.java index 44fb85bb..7c7efe2f 100644 --- a/src/addon/java/lua/addon/compile/FuncState.java +++ b/src/addon/java/lua/addon/compile/FuncState.java @@ -818,7 +818,7 @@ public class FuncState extends LuaC { case OP_MOD: r = (LNumber) v2.luaBinOpUnknown(op, v1); break; - //* TODO: replace this with something reasonable. + /* TODO: replace this with something reasonable. case OP_POW: r = new LDouble( Math.pow(v1.luaAsDouble(), v2.luaAsDouble() ) ); break; diff --git a/src/addon/java/lua/addon/luacompat/LuaCompat.java b/src/addon/java/lua/addon/luacompat/LuaCompat.java index 966e4588..ec036a4c 100644 --- a/src/addon/java/lua/addon/luacompat/LuaCompat.java +++ b/src/addon/java/lua/addon/luacompat/LuaCompat.java @@ -497,7 +497,7 @@ public class LuaCompat extends LFunction { String script; if ( ! "".equals(fileName) ) { script = fileName; - is = vm.getClass().getResourceAsStream( "/"+script ); + is = Platform.getInstance().openFile(fileName); if ( is == null ) { vm.setErrorResult( LNil.NIL, "cannot open "+fileName+": No such file or directory" ); return false; diff --git a/src/addon/java/lua/addon/luacompat/Platform.java b/src/addon/java/lua/addon/luacompat/Platform.java new file mode 100644 index 00000000..4aa89ceb --- /dev/null +++ b/src/addon/java/lua/addon/luacompat/Platform.java @@ -0,0 +1,58 @@ +package lua.addon.luacompat; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; + +/** + * Singleton to manage platform-specific behaviors. + * + */ +abstract public class Platform { + private static Platform instance; + + /** + * Singleton to be used for platform operations. + * + * The default Platform gets files as resources, + * and converts them to characters using the default + * InputStreamReader class. + */ + public static Platform getInstance() { + if ( instance == null ) { + instance = new Platform() { + public Reader createReader(InputStream inputStream) { + return new InputStreamReader(inputStream); + } + public InputStream openFile(String fileName) { + return getClass().getResourceAsStream("/"+fileName); + } + }; + } + return instance; + } + + /** + * Set the Platform instance. + * + * This may be useful to define a file search path, + * or custom character encoding conversion properties. + */ + public static void setInstance( Platform platform ) { + instance = platform; + } + + /** + * Return an InputStream or null if not found for a particular file name. + * @param fileName Name of the file to open + * @return InputStream or null if not found. + */ + abstract public InputStream openFile( String fileName ); + + /** + * Create Reader from an InputStream + * @param inputStream InputStream to read from + * @return Reader instance to use for character input + */ + abstract public Reader createReader( InputStream inputStream ); +}