Improve package lib behavior and error reporting.
This commit is contained in:
@@ -361,7 +361,7 @@ public class Print extends Lua {
|
||||
break;
|
||||
case LuaValue.TFUNCTION:
|
||||
ps.print( ( v instanceof LuaClosure )?
|
||||
((LuaClosure)v).p.tojstring(): v.tojstring() );
|
||||
((LuaClosure)v).p.toString(): v.tojstring() );
|
||||
break;
|
||||
case LuaValue.TUSERDATA:
|
||||
Object o = v.touserdata();
|
||||
|
||||
@@ -44,8 +44,8 @@ public class Prototype {
|
||||
public int maxstacksize;
|
||||
|
||||
|
||||
public String tojstring() {
|
||||
return source + ":" + linedefined;
|
||||
public String toString() {
|
||||
return source + ":" + linedefined+"-"+lastlinedefined;
|
||||
}
|
||||
|
||||
/** Get the name of a local variable.
|
||||
|
||||
@@ -46,6 +46,7 @@ public class CoroutineLib extends VarArgFunction {
|
||||
"create", "resume", "running", "status", "yield", "wrap" },
|
||||
CREATE);
|
||||
env.set("coroutine", t);
|
||||
PackageLib.instance.LOADED.set("coroutine", t);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
@@ -114,6 +114,7 @@ public class DebugLib extends VarArgFunction {
|
||||
LuaTable t = new LuaTable();
|
||||
bind(t, DebugLib.class, NAMES, DEBUG);
|
||||
env.set("debug", t);
|
||||
PackageLib.instance.LOADED.set("debug", t);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
@@ -197,6 +197,7 @@ public class IoLib extends OneArgFunction {
|
||||
|
||||
// return the table
|
||||
env.set("io", t);
|
||||
PackageLib.instance.LOADED.set("io", t);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +61,7 @@ public class MathLib extends OneArgFunction {
|
||||
((MathLibV) t.get("randomseed")).mathlib = this;
|
||||
((MathLibV) t.get("random" )).mathlib = this;
|
||||
env.set("math", t);
|
||||
PackageLib.instance.LOADED.set("math", t);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
@@ -90,6 +90,7 @@ public class OsLib extends VarArgFunction {
|
||||
LuaTable t = new LuaTable();
|
||||
bind(t, this.getClass(), NAMES, CLOCK);
|
||||
env.set("os", t);
|
||||
PackageLib.instance.LOADED.set("os", t);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
******************************************************************************/
|
||||
package org.luaj.vm2.lib;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
@@ -92,6 +91,7 @@ public class PackageLib extends OneArgFunction {
|
||||
lua_loader = new PkgLibV(env,"lua_loader", OP_LUA_LOADER,this),
|
||||
java_loader = new PkgLibV(env,"java_loader", OP_JAVA_LOADER,this),
|
||||
}) }) );
|
||||
LOADED.set("package", PACKAGE);
|
||||
return env;
|
||||
}
|
||||
|
||||
@@ -212,6 +212,8 @@ public class PackageLib extends OneArgFunction {
|
||||
LuaFunction f = LuaThread.getCallstackFunction(1);
|
||||
if ( f == null )
|
||||
error("no calling function");
|
||||
if ( ! f.isclosure() )
|
||||
error("'module' not called from a Lua function");
|
||||
f.setfenv(module);
|
||||
|
||||
// apply the functions
|
||||
@@ -321,6 +323,7 @@ public class PackageLib extends OneArgFunction {
|
||||
}
|
||||
|
||||
public static Varargs loadlib( Varargs args ) {
|
||||
args.checkstring(1);
|
||||
return varargsOf(NIL, valueOf("dynamic libraries not enabled"), valueOf("absent"));
|
||||
}
|
||||
|
||||
|
||||
@@ -51,6 +51,7 @@ public class StringLib extends OneArgFunction {
|
||||
instance = t;
|
||||
if ( LuaString.s_metatable == null )
|
||||
LuaString.s_metatable = tableOf( new LuaValue[] { INDEX, t } );
|
||||
PackageLib.instance.LOADED.set("string", t);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ public class TableLib extends OneArgFunction {
|
||||
bind(t, TableLibV.class, new String[] {
|
||||
"remove", "concat", "insert", "sort", "foreach", "foreachi", } );
|
||||
env.set("table", t);
|
||||
PackageLib.instance.LOADED.set("table", t);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.Varargs;
|
||||
import org.luaj.vm2.lib.BaseLib;
|
||||
import org.luaj.vm2.lib.OneArgFunction;
|
||||
import org.luaj.vm2.lib.PackageLib;
|
||||
import org.luaj.vm2.lib.ThreeArgFunction;
|
||||
import org.luaj.vm2.lib.TwoArgFunction;
|
||||
import org.luaj.vm2.lib.VarArgFunction;
|
||||
@@ -72,10 +73,6 @@ public class LuajavaLib extends VarArgFunction {
|
||||
|
||||
private static LuaValue LENGTH = valueOf("length");
|
||||
|
||||
public static void install(LuaValue globals) {
|
||||
globals.set("luajava", new LuajavaLib());
|
||||
}
|
||||
|
||||
public LuajavaLib() {
|
||||
}
|
||||
|
||||
@@ -86,6 +83,7 @@ public class LuajavaLib extends VarArgFunction {
|
||||
LuaTable t = new LuaTable();
|
||||
bind( t, LuajavaLib.class, NAMES, BINDCLASS );
|
||||
env.set("luajava", t);
|
||||
PackageLib.instance.LOADED.set("luajava", t);
|
||||
return t;
|
||||
}
|
||||
case BINDCLASS: {
|
||||
|
||||
@@ -132,12 +132,11 @@ local function arglists(typesets)
|
||||
return ipairs(argsets)
|
||||
end
|
||||
|
||||
|
||||
local function lookup( name )
|
||||
function lookup( name )
|
||||
return loadstring('return '..name)()
|
||||
end
|
||||
|
||||
local function invoke( name, arglist )
|
||||
function invoke( name, arglist )
|
||||
local s,c = pcall(lookup, name)
|
||||
if not s then return s,c end
|
||||
return pcall(c, unpack(arglist,1,arglist.n or #arglist))
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
package.path = "?.lua;test/lua/errors/?.lua"
|
||||
require 'args'
|
||||
|
||||
local tostring = tostring
|
||||
_G.tostring = function(x)
|
||||
local s = tostring(x)
|
||||
return type(x)=='number' and #s>4 and (s:sub(1,5)..'...') or s
|
||||
end
|
||||
|
||||
-- arg type tests for math library functions
|
||||
local somenumber = {1,0.75,'-1','-0.25'}
|
||||
local somepositive = {1,0.75,'2', '2.5'}
|
||||
|
||||
@@ -22,28 +22,25 @@ checkallerrors('package.seeall',{notatable},'bad argument')
|
||||
|
||||
-- module tests - require special rigging
|
||||
banner('module')
|
||||
print( pcall( function()
|
||||
checkallpass('module',{{20001}})
|
||||
end ) )
|
||||
print( pcall( function()
|
||||
checkallpass('module',{{20002},{package.seeall}})
|
||||
end ) )
|
||||
print( pcall( function()
|
||||
checkallpass('module',{{20003},{package.seeall},{function() end}})
|
||||
end ) )
|
||||
print( pcall( function()
|
||||
checkallerrors('module',{{aboolean,atable,function() end}},'bad argument')
|
||||
checkallerrors('module',{{aboolean,atable,function() end},{package.seeall}},'bad argument')
|
||||
end ) )
|
||||
print( pcall( function()
|
||||
checkallerrors('module',{{'testmodule1'},{'pqrs'}},'attempt to call')
|
||||
end ) )
|
||||
print( pcall( function()
|
||||
checkallerrors('module',{{'testmodule2'},{aboolean}},'attempt to call')
|
||||
end ) )
|
||||
print( pcall( function()
|
||||
checkallerrors('module',{{'testmodule3'},{athread}},'attempt to call')
|
||||
end ) )
|
||||
print( pcall( function()
|
||||
checkallerrors('module',{{'testmodule4'},{atable}},'attempt to call')
|
||||
end ) )
|
||||
checkallerrors('module',{{20001},{nil,package.seeall,n=2},{nil,function()end,n=2}},"'module' not called from a Lua function")
|
||||
checkallerrors('module',{{'testmodule1'},{nil,'pqrs',aboolean,athread,atable}},"'module' not called from a Lua function")
|
||||
checkallerrors('module',{{aboolean,atable,function() end}},'bad argument')
|
||||
checkallerrors('module',{{aboolean,atable,function() end},{package.seeall}},'bad argument')
|
||||
|
||||
-- enclose each invokation in its own function
|
||||
function invoke( name, arglist )
|
||||
assert( name=='module', 'module rig used for '..name )
|
||||
local func = function()
|
||||
module( unpack(arglist,1,arglist.n or #arglist) )
|
||||
end
|
||||
return pcall( func )
|
||||
end
|
||||
checkallpass('module',{{'foo1',20001}})
|
||||
checkallpass('module',{{'foo2',20002},{package.seeall}})
|
||||
checkallpass('module',{{'foo3',20003},{package.seeall},{function() end}})
|
||||
checkallerrors('module',{{aboolean,atable,function() end}},'bad argument')
|
||||
checkallerrors('module',{{aboolean,atable,function() end},{package.seeall}},'bad argument')
|
||||
checkallerrors('module',{{'testmodule2'},{'pqrs'}},'attempt to call')
|
||||
checkallerrors('module',{{'testmodule3'},{aboolean}},'attempt to call')
|
||||
checkallerrors('module',{{'testmodule4'},{athread}},'attempt to call')
|
||||
checkallerrors('module',{{'testmodule5'},{atable}},'attempt to call')
|
||||
|
||||
Reference in New Issue
Block a user