Fix loadlib.
This commit is contained in:
@@ -30,9 +30,9 @@ import org.luaj.vm2.LuaTable;
|
|||||||
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 standard package and module
|
* Subclass of {@link LibFunction} which implements the lua standard package and module
|
||||||
* library functions.
|
* library functions.
|
||||||
*
|
*
|
||||||
* <h3>Lua Environment Variables</h3>
|
* <h3>Lua Environment Variables</h3>
|
||||||
* The following variables are available to lua scrips when this library has been loaded:
|
* The following variables are available to lua scrips when this library has been loaded:
|
||||||
@@ -50,14 +50,14 @@ import org.luaj.vm2.Varargs;
|
|||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* <h3>Loading</h3>
|
* <h3>Loading</h3>
|
||||||
* Typically, this library is included as part of a call to either
|
* 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.jme.JmePlatform#standardGlobals()}
|
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} or {@link org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()}
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = JsePlatform.standardGlobals();
|
* Globals globals = JsePlatform.standardGlobals();
|
||||||
* System.out.println( globals.get("require").call"foo") );
|
* System.out.println( globals.get("require").call"foo") );
|
||||||
* } </pre>
|
* } </pre>
|
||||||
* <p>
|
* <p>
|
||||||
* To instantiate and use it directly,
|
* To instantiate and use it directly,
|
||||||
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
* Globals globals = new Globals();
|
* Globals globals = new Globals();
|
||||||
@@ -67,8 +67,8 @@ import org.luaj.vm2.Varargs;
|
|||||||
* } </pre>
|
* } </pre>
|
||||||
* <h3>Limitations</h3>
|
* <h3>Limitations</h3>
|
||||||
* This library has been implemented to match as closely as possible the behavior in the corresponding library in C.
|
* This library has been implemented to match as closely as possible the behavior in the corresponding library in C.
|
||||||
* However, the default filesystem search semantics are different and delegated to the bas library
|
* However, the default filesystem search semantics are different and delegated to the bas library
|
||||||
* as outlined in the {@link BaseLib} and {@link org.luaj.vm2.lib.jse.JseBaseLib} documentation.
|
* as outlined in the {@link BaseLib} and {@link org.luaj.vm2.lib.jse.JseBaseLib} documentation.
|
||||||
* <p>
|
* <p>
|
||||||
* @see LibFunction
|
* @see LibFunction
|
||||||
* @see BaseLib
|
* @see BaseLib
|
||||||
@@ -156,7 +156,7 @@ public class PackageLib extends TwoArgFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Set the lua path used by this library instance to a new value.
|
/** Set the lua path used by this library instance to a new value.
|
||||||
* Merely sets the value of {@link path} to be used in subsequent searches. */
|
* Merely sets the value of {@link path} to be used in subsequent searches. */
|
||||||
public void setLuaPath( String newLuaPath ) {
|
public void setLuaPath( String newLuaPath ) {
|
||||||
package_.set(_PATH, LuaValue.valueOf(newLuaPath));
|
package_.set(_PATH, LuaValue.valueOf(newLuaPath));
|
||||||
@@ -168,33 +168,33 @@ public class PackageLib extends TwoArgFunction {
|
|||||||
|
|
||||||
// ======================== Package loading =============================
|
// ======================== Package loading =============================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* require (modname)
|
* require (modname)
|
||||||
*
|
*
|
||||||
* Loads the given module. The function starts by looking into the package.loaded table
|
* Loads the given module. The function starts by looking into the package.loaded table
|
||||||
* to determine whether modname is already loaded. If it is, then require returns the value
|
* to determine whether modname is already loaded. If it is, then require returns the value
|
||||||
* stored at package.loaded[modname]. Otherwise, it tries to find a loader for the module.
|
* stored at package.loaded[modname]. Otherwise, it tries to find a loader for the module.
|
||||||
*
|
*
|
||||||
* To find a loader, require is guided by the package.searchers sequence.
|
* To find a loader, require is guided by the package.searchers sequence.
|
||||||
* By changing this sequence, we can change how require looks for a module.
|
* By changing this sequence, we can change how require looks for a module.
|
||||||
* The following explanation is based on the default configuration for package.searchers.
|
* The following explanation is based on the default configuration for package.searchers.
|
||||||
*
|
*
|
||||||
* First require queries package.preload[modname]. If it has a value, this value
|
* First require queries package.preload[modname]. If it has a value, this value
|
||||||
* (which should be a function) is the loader. Otherwise require searches for a Lua loader using
|
* (which should be a function) is the loader. Otherwise require searches for a Lua loader using
|
||||||
* the path stored in package.path. If that also fails, it searches for a Java loader using
|
* the path stored in package.path. If that also fails, it searches for a Java loader using
|
||||||
* the classpath, using the public default constructor, and casting the instance to LuaFunction.
|
* the classpath, using the public default constructor, and casting the instance to LuaFunction.
|
||||||
*
|
*
|
||||||
* Once a loader is found, require calls the loader with two arguments: modname and an extra value
|
* Once a loader is found, require calls the loader with two arguments: modname and an extra value
|
||||||
* dependent on how it got the loader. If the loader came from a file, this extra value is the file name.
|
* dependent on how it got the loader. If the loader came from a file, this extra value is the file name.
|
||||||
* If the loader is a Java instance of LuaFunction, this extra value is the environment.
|
* If the loader is a Java instance of LuaFunction, this extra value is the environment.
|
||||||
* If the loader returns any non-nil value, require assigns the returned value to package.loaded[modname].
|
* If the loader returns any non-nil value, require assigns the returned value to package.loaded[modname].
|
||||||
* If the loader does not return a non-nil value and has not assigned any value to package.loaded[modname],
|
* If the loader does not return a non-nil value and has not assigned any value to package.loaded[modname],
|
||||||
* then require assigns true to this entry.
|
* then require assigns true to this entry.
|
||||||
* In any case, require returns the final value of package.loaded[modname].
|
* In any case, require returns the final value of package.loaded[modname].
|
||||||
*
|
*
|
||||||
* If there is any error loading or running the module, or if it cannot find any loader for the module,
|
* If there is any error loading or running the module, or if it cannot find any loader for the module,
|
||||||
* then require raises an error.
|
* then require raises an error.
|
||||||
*/
|
*/
|
||||||
public class require extends OneArgFunction {
|
public class require extends OneArgFunction {
|
||||||
public LuaValue call( LuaValue arg ) {
|
public LuaValue call( LuaValue arg ) {
|
||||||
LuaString name = arg.checkstring();
|
LuaString name = arg.checkstring();
|
||||||
@@ -213,7 +213,7 @@ public class PackageLib extends TwoArgFunction {
|
|||||||
for ( int i=1; true; i++ ) {
|
for ( int i=1; true; i++ ) {
|
||||||
LuaValue searcher = tbl.get(i);
|
LuaValue searcher = tbl.get(i);
|
||||||
if ( searcher.isnil() ) {
|
if ( searcher.isnil() ) {
|
||||||
error( "module '"+name+"' not found: "+name+sb );
|
error( "module '"+name+"' not found: "+name+sb );
|
||||||
}
|
}
|
||||||
|
|
||||||
/* call loader with module name as argument */
|
/* call loader with module name as argument */
|
||||||
@@ -229,14 +229,14 @@ public class PackageLib extends TwoArgFunction {
|
|||||||
result = loader.arg1().call(name, loader.arg(2));
|
result = loader.arg1().call(name, loader.arg(2));
|
||||||
if ( ! result.isnil() )
|
if ( ! result.isnil() )
|
||||||
loaded.set( name, result );
|
loaded.set( name, result );
|
||||||
else if ( (result = loaded.get(name)) == _SENTINEL )
|
else if ( (result = loaded.get(name)) == _SENTINEL )
|
||||||
loaded.set( name, result = LuaValue.TRUE );
|
loaded.set( name, result = LuaValue.TRUE );
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class loadlib extends VarArgFunction {
|
public static class loadlib extends VarArgFunction {
|
||||||
public Varargs loadlib( Varargs args ) {
|
public Varargs invoke( Varargs args ) {
|
||||||
args.checkstring(1);
|
args.checkstring(1);
|
||||||
return varargsOf(NIL, valueOf("dynamic libraries not enabled"), valueOf("absent"));
|
return varargsOf(NIL, valueOf("dynamic libraries not enabled"), valueOf("absent"));
|
||||||
}
|
}
|
||||||
@@ -246,7 +246,7 @@ public class PackageLib extends TwoArgFunction {
|
|||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
LuaString name = args.checkstring(1);
|
LuaString name = args.checkstring(1);
|
||||||
LuaValue val = package_.get(_PRELOAD).get(name);
|
LuaValue val = package_.get(_PRELOAD).get(name);
|
||||||
return val.isnil()?
|
return val.isnil()?
|
||||||
valueOf("\n\tno field package.preload['"+name+"']"):
|
valueOf("\n\tno field package.preload['"+name+"']"):
|
||||||
val;
|
val;
|
||||||
}
|
}
|
||||||
@@ -258,7 +258,7 @@ public class PackageLib extends TwoArgFunction {
|
|||||||
|
|
||||||
// get package path
|
// get package path
|
||||||
LuaValue path = package_.get(_PATH);
|
LuaValue path = package_.get(_PATH);
|
||||||
if ( ! path.isstring() )
|
if ( ! path.isstring() )
|
||||||
return valueOf("package.path is not a string");
|
return valueOf("package.path is not a string");
|
||||||
|
|
||||||
// get the searchpath function.
|
// get the searchpath function.
|
||||||
@@ -270,7 +270,7 @@ public class PackageLib extends TwoArgFunction {
|
|||||||
LuaString filename = v.arg1().strvalue();
|
LuaString filename = v.arg1().strvalue();
|
||||||
|
|
||||||
// Try to load the file.
|
// Try to load the file.
|
||||||
v = globals.loadfile(filename.tojstring());
|
v = globals.loadfile(filename.tojstring());
|
||||||
if ( v.arg1().isfunction() )
|
if ( v.arg1().isfunction() )
|
||||||
return LuaValue.varargsOf(v.arg1(), filename);
|
return LuaValue.varargsOf(v.arg1(), filename);
|
||||||
|
|
||||||
@@ -355,9 +355,9 @@ public class PackageLib extends TwoArgFunction {
|
|||||||
StringBuffer sb = new StringBuffer(j);
|
StringBuffer sb = new StringBuffer(j);
|
||||||
for ( int i=0; i<j; i++ ) {
|
for ( int i=0; i<j; i++ ) {
|
||||||
c = filename.charAt(i);
|
c = filename.charAt(i);
|
||||||
sb.append(
|
sb.append(
|
||||||
(isClassnamePart(c))? c:
|
(isClassnamePart(c))? c:
|
||||||
((c=='/') || (c=='\\'))? '.': '_' );
|
((c=='/') || (c=='\\'))? '.': '_' );
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
@@ -376,5 +376,5 @@ public class PackageLib extends TwoArgFunction {
|
|||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user