Add Platform manager
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
58
src/addon/java/lua/addon/luacompat/Platform.java
Normal file
58
src/addon/java/lua/addon/luacompat/Platform.java
Normal file
@@ -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 );
|
||||
}
|
||||
Reference in New Issue
Block a user