Skip to content

Commit b6df887

Browse files
committed
replace default lua searcher by soluna.file
1 parent ce12d18 commit b6df887

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/embedlua.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "text.lua.h"
2121
#include "util.lua.h"
2222
#include "coroutine.lua.h"
23+
#include "packageloader.lua.h"
2324

2425
#include "lua.h"
2526
#include "lauxlib.h"
@@ -60,6 +61,7 @@ luaopen_embedsource(lua_State *L) {
6061
REG_SOURCE(main)
6162
REG_SOURCE(print_r)
6263
REG_SOURCE(fontmgr)
64+
REG_SOURCE(packageloader)
6365
lua_setfield(L, -2, "runtime");
6466

6567
lua_newtable(L); // runtime

src/lualib/main.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ local init_func_temp = [=[
99
package.path = [[${lua_path}]]
1010
package.cpath = [[${lua_cpath}]]
1111
_G.print_r = load(embedsource.runtime.print_r(), "@src/lualib/print_r.lua")()
12+
local packageloader = load(embedsource.runtime.packageloader(), "@src/lualib/packageloader.lua")
13+
packageloader()
1214
local function embedloader(name)
1315
local ename
1416
if name == "soluna" then
@@ -20,8 +22,9 @@ local init_func_temp = [=[
2022
local code = embedsource.lib[ename]
2123
if code then
2224
return function()
23-
local f = load(code(), "@src/lualib/"..ename..".lua")
24-
return f()
25+
local srcname = "src/lualib/"..ename..".lua"
26+
local f = load(code(), "@" .. srcname)
27+
return f(ename, srcname)
2528
end
2629
end
2730
return "no embed soluna." .. ename
@@ -178,6 +181,8 @@ end
178181
function api.init(desc)
179182
-- todo : settings
180183
local embedsource = require "soluna.embedsource"
184+
local packageloader = load(embedsource.runtime.packageloader(), "@src/lualib/packageloader.lua")
185+
packageloader()
181186
local initsetting = load(embedsource.lib.initsetting, "@3rd/ltask/lualib/initsetting.lua")()
182187
local settings = initsetting.init(args)
183188
local soluna_app = require "soluna.app"

src/lualib/packageloader.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local file = require "soluna.file"
2+
3+
local package = package
4+
local string = string
5+
local io = io
6+
7+
global load, print
8+
9+
local dir_sep, temp_sep, temp_marker = package.config:match "(.)\n(.)\n(.)"
10+
local temp_pat = "[^"..temp_sep.."]+"
11+
12+
local function fileload(name, fullname)
13+
local s, err = file.load(fullname)
14+
local f = load(s, "@"..fullname)
15+
return f(name, fullname)
16+
end
17+
18+
local function search_file(name)
19+
local cname = name:gsub("%.", "/")
20+
for temp in package.path:gmatch(temp_pat) do
21+
local fullname = temp:gsub(temp_marker, cname)
22+
if dir_sep ~= '/' then
23+
fullname = fullname:gsub(dir_sep, "/")
24+
end
25+
if file.exist(fullname) then
26+
return fileload, fullname
27+
end
28+
end
29+
return "No package : " .. name
30+
end
31+
32+
package.searchers[2] = search_file

0 commit comments

Comments
 (0)