Fix bug that didn't read package.path from environment.

This commit is contained in:
James Roseborough
2013-06-05 23:50:41 +00:00
parent 424e80900a
commit 56fe850437
5 changed files with 34 additions and 36 deletions

View File

@@ -844,6 +844,9 @@ Files are no longer hosted at LuaForge.
<li>Add artifacts to Maven central repository.</li> <li>Add artifacts to Maven central repository.</li>
<li>Limit pluggable scripting to use compatible bindings and contexts, implement redirection.</li> <li>Limit pluggable scripting to use compatible bindings and contexts, implement redirection.</li>
<tr valign="top"><td>&nbsp;&nbsp;<b>3.0-beta1</b></td><td><ul>
<li>Fix bug that didn't read package.path from environment.</li>
</ul></td></tr> </ul></td></tr>
</table></td></tr></table> </table></td></tr></table>

View File

@@ -120,7 +120,7 @@ public class OsLib extends OneArgFunction {
for (int i = 0; i < NAMES.length; ++i) for (int i = 0; i < NAMES.length; ++i)
os.set(NAMES[i], new OsLibFunc(i, NAMES[i])); os.set(NAMES[i], new OsLibFunc(i, NAMES[i]));
env.set("os", os); env.set("os", os);
globals.package_.loaded.set("os", os); env.get("package").get("loaded").set("os", os);
return os; return os;
} }

View File

@@ -71,26 +71,18 @@ public class PackageLib extends OneArgFunction {
DEFAULT_LUA_PATH = "?.lua"; DEFAULT_LUA_PATH = "?.lua";
} }
private static final LuaString _LOADED = valueOf("loaded");
private static final LuaString _LOADLIB = valueOf("loadlib");
private static final LuaString _PRELOAD = valueOf("preload");
private static final LuaString _PATH = valueOf("path");
private static final LuaString _SEARCHPATH = valueOf("searchpath");
private static final LuaString _SEARCHERS = valueOf("searchers");
/** The globals that were used to load this library. */ /** The globals that were used to load this library. */
Globals globals; Globals globals;
/** The table used by require to check for loaded modules, and exposed initially as package.loaded. */ /** The table for this package. */
public LuaTable loaded; LuaTable package_;
/** The table used by the {@link preload_loader}, and exposed initially as package.preload. */
public LuaTable preload;
/** The value in use as the package path, and set as the initial value of package.path. */
public LuaString path;
/** The loadlib function used by the package library. */
public loadlib loadlib;
/** The searchpath function used by the package library. */
public searchpath searchpath;
/** The initial searchers list, and the list exposed initially as package.searchers */
public LuaTable searchers;
/** Loader that loads from {@link preload} table if found there */ /** Loader that loads from {@link preload} table if found there */
public preload_searcher preload_searcher; public preload_searcher preload_searcher;
@@ -109,19 +101,19 @@ public class PackageLib extends OneArgFunction {
public LuaValue call(LuaValue env) { public LuaValue call(LuaValue env) {
globals = env.checkglobals(); globals = env.checkglobals();
env.set("require", new require()); globals.set("require", new require());
LuaTable package_ = new LuaTable(); package_ = new LuaTable();
package_.set("loaded", loaded = new LuaTable()); package_.set(_LOADED, new LuaTable());
package_.set("preload", preload = new LuaTable()); package_.set(_PRELOAD, new LuaTable());
package_.set("path", path = LuaValue.valueOf(DEFAULT_LUA_PATH)); package_.set(_PATH, LuaValue.valueOf(DEFAULT_LUA_PATH));
package_.set("loadlib", loadlib = new loadlib()); package_.set(_LOADLIB, new loadlib());
package_.set("searchpath", searchpath = new searchpath()); package_.set(_SEARCHPATH, new searchpath());
searchers = new LuaTable(); LuaTable searchers = new LuaTable();
searchers.set(1, preload_searcher = new preload_searcher()); searchers.set(1, preload_searcher = new preload_searcher());
searchers.set(2, lua_searcher = new lua_searcher()); searchers.set(2, lua_searcher = new lua_searcher());
searchers.set(3, java_searcher = new java_searcher()); searchers.set(3, java_searcher = new java_searcher());
package_.set("searchers", searchers); package_.set(_SEARCHERS, searchers);
loaded.set("package", package_); package_.get(_LOADED).set("package", package_);
env.set("package", package_); env.set("package", package_);
globals.package_ = this; globals.package_ = this;
return env; return env;
@@ -129,14 +121,14 @@ public class PackageLib extends OneArgFunction {
/** Allow packages to mark themselves as loaded */ /** Allow packages to mark themselves as loaded */
public void setIsLoaded(String name, LuaTable value) { public void setIsLoaded(String name, LuaTable value) {
loaded.set(name, value); package_.get(_LOADED).set(name, value);
} }
/** 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 ) {
path = LuaValue.valueOf(newLuaPath); package_.set(_PATH, LuaValue.valueOf(newLuaPath));
} }
public String tojstring() { public String tojstring() {
@@ -175,6 +167,7 @@ public class PackageLib extends OneArgFunction {
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();
LuaValue loaded = package_.get(_LOADED);
LuaValue result = loaded.get(name); LuaValue result = loaded.get(name);
if ( result.toboolean() ) { if ( result.toboolean() ) {
if ( result == _SENTINEL ) if ( result == _SENTINEL )
@@ -183,7 +176,7 @@ public class PackageLib extends OneArgFunction {
} }
/* else must load it; iterate over available loaders */ /* else must load it; iterate over available loaders */
LuaTable tbl = PackageLib.this.searchers.checktable(); LuaTable tbl = package_.get(_SEARCHERS).checktable();
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
Varargs loader = null; Varargs loader = null;
for ( int i=1; true; i++ ) { for ( int i=1; true; i++ ) {
@@ -205,7 +198,7 @@ public class PackageLib extends OneArgFunction {
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 = PackageLib.this.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;
} }
@@ -221,7 +214,7 @@ public class PackageLib extends OneArgFunction {
public class preload_searcher extends VarArgFunction { public class preload_searcher extends VarArgFunction {
public Varargs invoke(Varargs args) { public Varargs invoke(Varargs args) {
LuaString name = args.checkstring(1); LuaString name = args.checkstring(1);
LuaValue val = 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;
@@ -234,11 +227,12 @@ public class PackageLib extends OneArgFunction {
InputStream is = null; InputStream is = null;
// get package path // get package 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.
Varargs v = searchpath.invoke(varargsOf(name, path)); Varargs v = package_.get(_SEARCHPATH).invoke(varargsOf(name, path));
// Did we get a result? // Did we get a result?
if (!v.isstring(1)) if (!v.isstring(1))

View File

@@ -67,6 +67,7 @@ public class TableLib extends OneArgFunction {
table.set("sort", new sort()); table.set("sort", new sort());
table.set("unpack", new unpack()); table.set("unpack", new unpack());
env.set("table", table); env.set("table", table);
env.get("package").get("loaded").set("table", table);
return NIL; return NIL;
} }

View File

@@ -1 +1 @@
version: 3.0-alpha3 version: 3.0-beta1