Improve compatibility with C-based lua.

This commit is contained in:
James Roseborough
2010-04-28 14:38:59 +00:00
parent 27bf60b903
commit 328a39007c
5 changed files with 44 additions and 25 deletions

View File

@@ -86,6 +86,7 @@ public class LuaError extends RuntimeException {
/** Add file and line info to a message */ /** Add file and line info to a message */
private static String addFileLine( String message, int level ) { private static String addFileLine( String message, int level ) {
if ( message == null ) return message;
LuaFunction f = LuaThread.getCallstackFunction(level); LuaFunction f = LuaThread.getCallstackFunction(level);
return f!=null? f+": "+message: message; return f!=null? f+": "+message: message;
} }

View File

@@ -37,22 +37,22 @@ public class PackageLib extends OneArgFunction {
public static String DEFAULT_LUA_PATH = "?.lua"; public static String DEFAULT_LUA_PATH = "?.lua";
/** Most recent instance of PackageLib */
public static PackageLib instance;
public InputStream STDIN = null; public InputStream STDIN = null;
public PrintStream STDOUT = System.out; public PrintStream STDOUT = System.out;
public LuaTable LOADED; public LuaTable LOADED;
public LuaTable PACKAGE; public LuaTable PACKAGE;
/** Most recent instance of PackageLib */
public static PackageLib instance;
/** Loader that loads from preload table if found there */ /** Loader that loads from preload table if found there */
public final LuaValue preload_loader; public LuaValue preload_loader;
/** Loader that loads as a lua script using the LUA_PATH */ /** Loader that loads as a lua script using the LUA_PATH */
public final LuaValue lua_loader; public LuaValue lua_loader;
/** Loader that loads as a Java class. Class must have public constructor and be a LuaValue */ /** Loader that loads as a Java class. Class must have public constructor and be a LuaValue */
public final LuaValue java_loader; public LuaValue java_loader;
private static final LuaString _M = valueOf("_M"); private static final LuaString _M = valueOf("_M");
private static final LuaString _NAME = valueOf("_NAME"); private static final LuaString _NAME = valueOf("_NAME");
@@ -76,9 +76,6 @@ public class PackageLib extends OneArgFunction {
public PackageLib() { public PackageLib() {
instance = this; instance = this;
preload_loader = new PkgLibV(env,"preload_loader", OP_PRELOAD_LOADER,this);
lua_loader = new PkgLibV(env,"lua_loader", OP_LUA_LOADER,this);
java_loader = new PkgLibV(env,"java_loader", OP_JAVA_LOADER,this);
} }
public LuaValue call(LuaValue arg) { public LuaValue call(LuaValue arg) {
@@ -91,9 +88,9 @@ public class PackageLib extends OneArgFunction {
_LOADLIB, new PkgLibV(env,"loadlib",OP_LOADLIB,this), _LOADLIB, new PkgLibV(env,"loadlib",OP_LOADLIB,this),
_SEEALL, new PkgLib1(env,"seeall",OP_SEEALL,this), _SEEALL, new PkgLib1(env,"seeall",OP_SEEALL,this),
_LOADERS, listOf(new LuaValue[] { _LOADERS, listOf(new LuaValue[] {
preload_loader, preload_loader = new PkgLibV(env,"preload_loader", OP_PRELOAD_LOADER,this),
lua_loader, lua_loader = new PkgLibV(env,"lua_loader", OP_LUA_LOADER,this),
java_loader, java_loader = new PkgLibV(env,"java_loader", OP_JAVA_LOADER,this),
}) }) ); }) }) );
return env; return env;
} }

View File

@@ -36,5 +36,5 @@ print( 'os.remove(q)', pcall( os.remove, q ) )
-- print( 'os.setlocale("jp")', pcall( os.setlocale, "jp" ) ) -- print( 'os.setlocale("jp")', pcall( os.setlocale, "jp" ) )
-- print( 'os.setlocale("us","monetary")', pcall( os.setlocale, "us", "monetary" ) ) -- print( 'os.setlocale("us","monetary")', pcall( os.setlocale, "us", "monetary" ) )
-- print( 'os.setlocale(nil,"all")', pcall( os.setlocale, nil, "all" ) ) -- print( 'os.setlocale(nil,"all")', pcall( os.setlocale, nil, "all" ) )
print( 'os.setlocale("c")', pcall( os.setlocale, "c" ) ) print( 'os.setlocale("C")', pcall( os.setlocale, "C" ) )
print( 'os.exit', type(os.exit) ) print( 'os.exit', type(os.exit) )

View File

@@ -108,15 +108,26 @@ print(string.format("(%08x) (%08d) (%08o)", 255, 255, 255))
print(string.format("simple%ssimple", " simple ")) print(string.format("simple%ssimple", " simple "))
print(string.format("%%")) local testformat = function(message,fmt,...)
local s,e = pcall( string.format, fmt, ... )
if s then
print( message, string.byte(e,1,#e) )
else
print( message, 'error', e )
end
end
specials = "\"specials\": %% \000 \r \n" specials = "\"specials\": %% \000 \r \n"
print(string.format("specials (%%s): ----->%s<----", specials) ) testformat('plain %', "%%")
print(string.format("specials (%%q): ----->%q<----", specials) ) testformat("specials (%s)", "---%s---", specials)
print(string.format("controls (%%q): ----->%q<----", ' \a \b \f \t \v \\ ') ) testformat("specials (%q)", "---%q---", specials)
print(string.format("extended (%%q): ----->%q<----", ' \222 \223 \224 ') ) testformat("controls (%q)", "---%q---", ' \a \b \f \t \v \\ ')
print(string.format("embedded newlines: %s\n%s\n%s", '======>', '<======>', '<=======')) testformat("extended (%q)", "---%q---", ' \222 \223 \224 ')
print(string.format("this is a %s long string", string.rep("really, ", 30))) testformat("embedded newlines", "%s\r%s\n%s", '===', '===', '===')
-- format long string
print("this is a %s long string", string.rep("really, ", 30))
local function pc(...) local function pc(...)
local s,e = pcall(...) local s,e = pcall(...)
return s and e or 'false-'..type(e) return s and e or 'false-'..type(e)

View File

@@ -124,10 +124,20 @@ local function fib_good(n)
return helper(1, 1, 1) return helper(1, 1, 1)
end end
print(pcall(fib_bad, 30)) local aliases = {
print(pcall(fib_bad, 25000)) ['1.#INF'] = 'inf',
print(pcall(fib_good, 30)) ['-1.#INF'] = '-inf',
print(pcall(fib_good, 25000)) ['1.#IND'] = 'nan',
['-1.#IND'] = 'nan',
}
local p = function( s,e )
print( s, e and aliases[tostring(e)] or e )
end
p(pcall(fib_bad, 30))
p((pcall(fib_bad, 25000)))
p(pcall(fib_good, 30))
p(pcall(fib_good, 25000))
local function fib_all(n, i, a, b) local function fib_all(n, i, a, b)
i = i or 1 i = i or 1