summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-05-03 15:58:45 +0200
committerBenoit Giannangeli <giann008@gmail.com>2017-05-03 15:58:45 +0200
commit6258925c955a31ac0c520ffabdbaf138910e48c1 (patch)
tree3f046469a15fced89a887ee94a3183b9bba0c474
parentee05d5644a8ae872c9b8c8b437651b3f34d88591 (diff)
downloadfengari-6258925c955a31ac0c520ffabdbaf138910e48c1.tar.gz
fengari-6258925c955a31ac0c520ffabdbaf138910e48c1.tar.bz2
fengari-6258925c955a31ac0c520ffabdbaf138910e48c1.zip
package.searchpath
-rw-r--r--README.md11
-rw-r--r--src/loadlib.js19
-rw-r--r--tests/loadlib.js32
3 files changed, 51 insertions, 11 deletions
diff --git a/README.md b/README.md
index 16701df..ebfb588 100644
--- a/README.md
+++ b/README.md
@@ -23,16 +23,7 @@
- [x] Table
- [x] utf8
- [x] os (~~`os.setlocale()`~~)
- - [ ] Package
- - [x] `package.config`
- - [x] `package.cpath`
- - [x] `package.loaded`
- - [x] `package.loadlib`
- - [x] `package.path`
- - [x] `package.preload`
- - [x] `package.searchers`
- - [x] `require`
- - [ ] `package.searchpath`
+ - [x] Package
- [ ] io
- [x] `file:__tostring()`
- [x] `file:write()`
diff --git a/src/loadlib.js b/src/loadlib.js
index f25e5dc..a161d49 100644
--- a/src/loadlib.js
+++ b/src/loadlib.js
@@ -217,6 +217,22 @@ const searchpath = function(L, name, path, sep, dirsep) {
return null; /* not found */
};
+const ll_searchpath = function(L) {
+ let f = searchpath(
+ L,
+ lauxlib.luaL_checkstring(L, 1),
+ lauxlib.luaL_checkstring(L, 2),
+ lauxlib.luaL_optstring(L, 3, [".".charCodeAt(0)]),
+ lauxlib.luaL_optstring(L, 4, [lua.LUA_DIRSEP.charCodeAt(0)])
+ );
+ if (f !== null) return 1;
+ else { /* error message is on top of the stack */
+ lua.lua_pushnil(L);
+ lua.lua_insert(L, -2);
+ return 2; /* return nil + error message */
+ }
+};
+
const findfile = function(L, name, pname, dirsep) {
lua.lua_getfield(L, lua.lua_upvalueindex(1), pname);
let path = lua.lua_tostring(L, -1);
@@ -348,7 +364,8 @@ const ll_require = function(L) {
};
const pk_funcs = {
- "loadlib": ll_loadlib
+ "loadlib": ll_loadlib,
+ "searchpath": ll_searchpath
};
const ll_funcs = {
diff --git a/tests/loadlib.js b/tests/loadlib.js
index b840bf5..ee87b4a 100644
--- a/tests/loadlib.js
+++ b/tests/loadlib.js
@@ -98,3 +98,35 @@ test('package.loadlib', function (t) {
);
});
+
+
+test('package.searchpath', function (t) {
+ let luaCode = `
+ return package.searchpath('module-hello', './?.lua;./tests/?.lua')
+ `, L;
+
+ t.plan(3);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lauxlib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+ t.strictEqual(
+ lua.lua_tojsstring(L, -1),
+ "./tests/module-hello.lua",
+ "Correct element(s) on the stack"
+ );
+
+});