From 328a39007c6fd51d9e09471df1394ab0c32e547a Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Wed, 28 Apr 2010 14:38:59 +0000 Subject: [PATCH] Improve compatibility with C-based lua. --- src/core/org/luaj/vm2/LuaError.java | 1 + src/core/org/luaj/vm2/lib/PackageLib.java | 21 ++++++++---------- test/lua/oslib.lua | 2 +- test/lua/stringlib.lua | 27 ++++++++++++++++------- test/lua/tailcalls.lua | 18 +++++++++++---- 5 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/core/org/luaj/vm2/LuaError.java b/src/core/org/luaj/vm2/LuaError.java index e57035c6..a3a08f98 100644 --- a/src/core/org/luaj/vm2/LuaError.java +++ b/src/core/org/luaj/vm2/LuaError.java @@ -86,6 +86,7 @@ public class LuaError extends RuntimeException { /** Add file and line info to a message */ private static String addFileLine( String message, int level ) { + if ( message == null ) return message; LuaFunction f = LuaThread.getCallstackFunction(level); return f!=null? f+": "+message: message; } diff --git a/src/core/org/luaj/vm2/lib/PackageLib.java b/src/core/org/luaj/vm2/lib/PackageLib.java index 97a5ed77..af858dda 100644 --- a/src/core/org/luaj/vm2/lib/PackageLib.java +++ b/src/core/org/luaj/vm2/lib/PackageLib.java @@ -37,22 +37,22 @@ public class PackageLib extends OneArgFunction { public static String DEFAULT_LUA_PATH = "?.lua"; - /** Most recent instance of PackageLib */ - public static PackageLib instance; - public InputStream STDIN = null; public PrintStream STDOUT = System.out; public LuaTable LOADED; public LuaTable PACKAGE; + /** Most recent instance of PackageLib */ + public static PackageLib instance; + /** 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 */ - 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 */ - public final LuaValue java_loader; + public LuaValue java_loader; private static final LuaString _M = valueOf("_M"); private static final LuaString _NAME = valueOf("_NAME"); @@ -76,9 +76,6 @@ public class PackageLib extends OneArgFunction { public PackageLib() { 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) { @@ -91,9 +88,9 @@ public class PackageLib extends OneArgFunction { _LOADLIB, new PkgLibV(env,"loadlib",OP_LOADLIB,this), _SEEALL, new PkgLib1(env,"seeall",OP_SEEALL,this), _LOADERS, listOf(new LuaValue[] { - preload_loader, - lua_loader, - java_loader, + 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), }) }) ); return env; } diff --git a/test/lua/oslib.lua b/test/lua/oslib.lua index ce6f6928..a46c4bdb 100644 --- a/test/lua/oslib.lua +++ b/test/lua/oslib.lua @@ -36,5 +36,5 @@ print( 'os.remove(q)', pcall( os.remove, q ) ) -- print( 'os.setlocale("jp")', pcall( os.setlocale, "jp" ) ) -- print( 'os.setlocale("us","monetary")', pcall( os.setlocale, "us", "monetary" ) ) -- 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) ) diff --git a/test/lua/stringlib.lua b/test/lua/stringlib.lua index 4bf95c67..5771f399 100644 --- a/test/lua/stringlib.lua +++ b/test/lua/stringlib.lua @@ -108,15 +108,26 @@ print(string.format("(%08x) (%08d) (%08o)", 255, 255, 255)) 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" -print(string.format("specials (%%s): ----->%s<----", specials) ) -print(string.format("specials (%%q): ----->%q<----", specials) ) -print(string.format("controls (%%q): ----->%q<----", ' \a \b \f \t \v \\ ') ) -print(string.format("extended (%%q): ----->%q<----", ' \222 \223 \224 ') ) -print(string.format("embedded newlines: %s\n%s\n%s", '======>', '<======>', '<=======')) -print(string.format("this is a %s long string", string.rep("really, ", 30))) - +testformat('plain %', "%%") +testformat("specials (%s)", "---%s---", specials) +testformat("specials (%q)", "---%q---", specials) +testformat("controls (%q)", "---%q---", ' \a \b \f \t \v \\ ') +testformat("extended (%q)", "---%q---", ' \222 \223 \224 ') +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 s,e = pcall(...) return s and e or 'false-'..type(e) diff --git a/test/lua/tailcalls.lua b/test/lua/tailcalls.lua index d5185288..017b7dbe 100644 --- a/test/lua/tailcalls.lua +++ b/test/lua/tailcalls.lua @@ -124,10 +124,20 @@ local function fib_good(n) return helper(1, 1, 1) end -print(pcall(fib_bad, 30)) -print(pcall(fib_bad, 25000)) -print(pcall(fib_good, 30)) -print(pcall(fib_good, 25000)) +local aliases = { + ['1.#INF'] = 'inf', + ['-1.#INF'] = '-inf', + ['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) i = i or 1