Fix print for pass tests.

This commit is contained in:
Enyby
2019-03-09 23:18:47 +02:00
parent ffb686556f
commit 898208365d

View File

@@ -33,21 +33,21 @@ 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;
/** /**
* Subclass of {@link LibFunction} which implements the lua basic library functions. * Subclass of {@link LibFunction} which implements the lua basic library functions.
* <p> * <p>
* This contains all library functions listed as "basic functions" in the lua documentation for JME. * This contains all library functions listed as "basic functions" in the lua documentation for JME.
* The functions dofile and loadfile use the * The functions dofile and loadfile use the
* {@link Globals#finder} instance to find resource files. * {@link Globals#finder} instance to find resource files.
* Since JME has no file system by default, {@link BaseLib} implements * Since JME has no file system by default, {@link BaseLib} implements
* {@link ResourceFinder} using {@link Class#getResource(String)}, * {@link ResourceFinder} using {@link Class#getResource(String)},
* which is the closest equivalent on JME. * which is the closest equivalent on JME.
* The default loader chain in {@link PackageLib} will use these as well. * The default loader chain in {@link PackageLib} will use these as well.
* <p>
* To use basic library functions that include a {@link ResourceFinder} based on
* directory lookup, use {@link org.luaj.vm2.lib.jse.JseBaseLib} instead.
* <p> * <p>
* Typically, this library is included as part of a call to either * To use basic library functions that include a {@link ResourceFinder} based on
* directory lookup, use {@link org.luaj.vm2.lib.jse.JseBaseLib} instead.
* <p>
* Typically, this library is included as part of a call to either
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or * {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or
* {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()} * {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
* <pre> {@code * <pre> {@code
@@ -55,7 +55,7 @@ import org.luaj.vm2.Varargs;
* globals.get("print").call(LuaValue.valueOf("hello, world")); * globals.get("print").call(LuaValue.valueOf("hello, world"));
* } </pre> * } </pre>
* <p> * <p>
* For special cases where the smallest possible footprint is desired, * For special cases where the smallest possible footprint is desired,
* a minimal set of libraries could be loaded * a minimal set of libraries could be loaded
* directly via {@link Globals#load(LuaValue)} using code such as: * directly via {@link Globals#load(LuaValue)} using code such as:
* <pre> {@code * <pre> {@code
@@ -63,8 +63,8 @@ import org.luaj.vm2.Varargs;
* globals.load(new JseBaseLib()); * globals.load(new JseBaseLib());
* globals.get("print").call(LuaValue.valueOf("hello, world")); * globals.get("print").call(LuaValue.valueOf("hello, world"));
* } </pre> * } </pre>
* Doing so will ensure the library is properly initialized * Doing so will ensure the library is properly initialized
* and loaded into the globals table. * and loaded into the globals table.
* <p> * <p>
* This is a direct port of the corresponding library in C. * This is a direct port of the corresponding library in C.
* @see org.luaj.vm2.lib.jse.JseBaseLib * @see org.luaj.vm2.lib.jse.JseBaseLib
@@ -119,9 +119,9 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
return env; return env;
} }
/** ResourceFinder implementation /** ResourceFinder implementation
* *
* Tries to open the file as a resource, which can work for JSE and JME. * Tries to open the file as a resource, which can work for JSE and JME.
*/ */
public InputStream findResource(String filename) { public InputStream findResource(String filename) {
return getClass().getResourceAsStream(filename.startsWith("/")? filename: "/"+filename); return getClass().getResourceAsStream(filename.startsWith("/")? filename: "/"+filename);
@@ -131,7 +131,7 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
// "assert", // ( v [,message] ) -> v, message | ERR // "assert", // ( v [,message] ) -> v, message | ERR
static final class _assert extends VarArgFunction { static final class _assert extends VarArgFunction {
public Varargs invoke(Varargs args) { public Varargs invoke(Varargs args) {
if ( !args.arg1().toboolean() ) if ( !args.arg1().toboolean() )
error( args.narg()>1? args.optjstring(2,"assertion failed!"): "assertion failed!" ); error( args.narg()>1? args.optjstring(2,"assertion failed!"): "assertion failed!" );
return args; return args;
} }
@@ -163,23 +163,23 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
public Varargs invoke(Varargs args) { public Varargs invoke(Varargs args) {
args.argcheck(args.isstring(1) || args.isnil(1), 1, "filename must be string or nil"); args.argcheck(args.isstring(1) || args.isnil(1), 1, "filename must be string or nil");
String filename = args.isstring(1)? args.tojstring(1): null; String filename = args.isstring(1)? args.tojstring(1): null;
Varargs v = filename == null? Varargs v = filename == null?
loadStream( globals.STDIN, "=stdin", "bt", globals ): loadStream( globals.STDIN, "=stdin", "bt", globals ):
loadFile( args.checkjstring(1), "bt", globals ); loadFile( args.checkjstring(1), "bt", globals );
return v.isnil(1)? error(v.tojstring(2)): v.arg1().invoke(); return v.isnil(1)? error(v.tojstring(2)): v.arg1().invoke();
} }
} }
// "error", // ( message [,level] ) -> ERR // "error", // ( message [,level] ) -> ERR
static final class error extends TwoArgFunction { static final class error extends TwoArgFunction {
public LuaValue call(LuaValue arg1, LuaValue arg2) { public LuaValue call(LuaValue arg1, LuaValue arg2) {
throw arg1.isnil()? new LuaError(null, arg2.optint(1)): throw arg1.isnil()? new LuaError(null, arg2.optint(1)):
arg1.isstring()? new LuaError(arg1.tojstring(), arg2.optint(1)): arg1.isstring()? new LuaError(arg1.tojstring(), arg2.optint(1)):
new LuaError(arg1); new LuaError(arg1);
} }
} }
// "getmetatable", // ( object ) -> table // "getmetatable", // ( object ) -> table
static final class getmetatable extends LibFunction { static final class getmetatable extends LibFunction {
public LuaValue call() { public LuaValue call() {
return argerror(1, "value"); return argerror(1, "value");
@@ -197,7 +197,7 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
String source = args.optjstring(2, ld.isstring()? ld.tojstring(): "=(load)"); String source = args.optjstring(2, ld.isstring()? ld.tojstring(): "=(load)");
String mode = args.optjstring(3, "bt"); String mode = args.optjstring(3, "bt");
LuaValue env = args.optvalue(4, globals); LuaValue env = args.optvalue(4, globals);
return loadStream(ld.isstring()? ld.strvalue().toInputStream(): return loadStream(ld.isstring()? ld.strvalue().toInputStream():
new StringInputStream(ld.checkfunction()), source, mode, env); new StringInputStream(ld.checkfunction()), source, mode, env);
} }
} }
@@ -209,7 +209,7 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
String filename = args.isstring(1)? args.tojstring(1): null; String filename = args.isstring(1)? args.tojstring(1): null;
String mode = args.optjstring(2, "bt"); String mode = args.optjstring(2, "bt");
LuaValue env = args.optvalue(3, globals); LuaValue env = args.optvalue(3, globals);
return filename == null? return filename == null?
loadStream( globals.STDIN, "=stdin", mode, env ): loadStream( globals.STDIN, "=stdin", mode, env ):
loadFile( filename, mode, env ); loadFile( filename, mode, env );
} }
@@ -243,13 +243,13 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
this.baselib = baselib; this.baselib = baselib;
} }
public Varargs invoke(Varargs args) { public Varargs invoke(Varargs args) {
LuaValue tostring = globals.get("tostring"); LuaValue tostring = globals.get("tostring");
for ( int i=1, n=args.narg(); i<=n; i++ ) { for ( int i=1, n=args.narg(); i<=n; i++ ) {
if ( i>1 ) globals.STDOUT.print( " \t" ); if ( i>1 ) globals.STDOUT.print( '\t' );
LuaString s = tostring.call( args.arg(i) ).strvalue(); LuaString s = tostring.call( args.arg(i) ).strvalue();
globals.STDOUT.print(s.tojstring()); globals.STDOUT.print(s.tojstring());
} }
globals.STDOUT.println(); globals.STDOUT.print('\n');
return NONE; return NONE;
} }
} }
@@ -308,7 +308,7 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
// "select", // (f, ...) -> value1, ... // "select", // (f, ...) -> value1, ...
static final class select extends VarArgFunction { static final class select extends VarArgFunction {
public Varargs invoke(Varargs args) { public Varargs invoke(Varargs args) {
int n = args.narg()-1; int n = args.narg()-1;
if ( args.arg1().equals(valueOf("#")) ) if ( args.arg1().equals(valueOf("#")) )
return valueOf(n); return valueOf(n);
int i = args.checkint(1); int i = args.checkint(1);
@@ -350,10 +350,10 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
static final class tostring extends LibFunction { static final class tostring extends LibFunction {
public LuaValue call(LuaValue arg) { public LuaValue call(LuaValue arg) {
LuaValue h = arg.metatag(TOSTRING); LuaValue h = arg.metatag(TOSTRING);
if ( ! h.isnil() ) if ( ! h.isnil() )
return h.call(arg); return h.call(arg);
LuaValue v = arg.tostring(); LuaValue v = arg.tostring();
if ( ! v.isnil() ) if ( ! v.isnil() )
return v; return v;
return valueOf(arg.tojstring()); return valueOf(arg.tojstring());
} }
@@ -366,7 +366,7 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
} }
} }
// "xpcall", // (f, err) -> result1, ... // "xpcall", // (f, err) -> result1, ...
final class xpcall extends VarArgFunction { final class xpcall extends VarArgFunction {
public Varargs invoke(Varargs args) { public Varargs invoke(Varargs args) {
final LuaThread t = globals.running; final LuaThread t = globals.running;
@@ -426,10 +426,10 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
} }
} }
/** /**
* Load from a named file, returning the chunk or nil,error of can't load * Load from a named file, returning the chunk or nil,error of can't load
* @param env * @param env
* @param mode * @param mode
* @return Varargs containing chunk, or NIL,error-text on error * @return Varargs containing chunk, or NIL,error-text on error
*/ */
public Varargs loadFile(String filename, String mode, LuaValue env) { public Varargs loadFile(String filename, String mode, LuaValue env) {
@@ -460,7 +460,7 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
private static class StringInputStream extends InputStream { private static class StringInputStream extends InputStream {
final LuaValue func; final LuaValue func;
byte[] bytes; byte[] bytes;
int offset, remaining = 0; int offset, remaining = 0;
StringInputStream(LuaValue func) { StringInputStream(LuaValue func) {
this.func = func; this.func = func;