lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Griffin Rock once stated:
> From: lua-l-bounces@lists.lua.org <lua-l-bounces@lists.lua.org> on behalf of Sean Conner <sean@conman.org>
> > Does it ever occur to you to actually *try* to implement your ideas?  
> > This idea, with a separate resource path, could be done in pure Lua.
> > Add a package.rpath (for resource path), then replace require() to look for two
> > parameters and handle it appropriately?
> 
> how about this:
> 
> ---------- resource.lua
> if not package.rpath then
> 	local path = {}
> 	for file in package.path:gmatch "[^;]*?" do
> 		if not path[file] then
> 			path[file] = 1
> 			table.insert(path,file);
> 		end
> 	end
> 	package.rpath = table.concat(path, ";")
> end
> local function resource (fname)
> 	local r = package.searchpath(fname, package.rpath, ":")
> 	if r then
> 		return function()
> 			local f = io.open(r)
> 			local t = f:read "*a"
> 			f:close()
> 			return t
> 		end
> 	end
> end
> table.insert(package.searchers, 2, resource)
> return resource
> ----------
> 
> example of use:
> require "resource"
> icon = require "package:gopher.icon"

  This doesn't work because package.searchpath() isn't exactly what you want
(read it closely).  When I tried this:

lua: t.lua:2: module 'package:gopher.icon' not found:
        no field package.preload['package:gopher.icon']
        no file '/home/spc/.luarocks/share/lua/5.3/package:gopher/icon.lua'
        no file '/home/spc/.luarocks/share/lua/5.3/package:gopher/icon/init.lua'
        no file '/home/spc/.luarocks/lib/lua/5.3/package:gopher/icon.lua'
        no file '/home/spc/.luarocks/lib/lua/5.3/package:gopher/icon/init.lua'
        no file '/usr/local/share/lua/5.3/package:gopher/icon.lua'
        no file '/usr/local/share/lua/5.3/package:gopher/icon/init.lua'
        no file '/usr/local/lib/lua/5.3/package:gopher/icon.lua'
        no file '/usr/local/lib/lua/5.3/package:gopher/icon/init.lua'
        no file './package:gopher/icon.lua'
        no file './package:gopher/icon/init.lua'
        no file '/home/spc/.luarocks/lib/lua/5.3/package:gopher/icon.so'
        no file '/usr/local/lib/lua/5.3/package:gopher/icon.so'
        no file '/usr/local/lib/lua/5.3/loadall.so'
        no file './package:gopher/icon.so'
        no file '/home/spc/.luarocks/lib/lua/5.3/package:gopher.so'
        no file '/usr/local/lib/lua/5.3/package:gopher.so'
        no file '/usr/local/lib/lua/5.3/loadall.so'
        no file './package:gopher.so'
stack traceback:
        [C]: in function 'require'
        t.lua:2: in main chunk
        [C]: in ?

  Notice how it keeps looking for gopher/icon.lua or gopher/icon/init.lua.

  It should look more like:

lua: res.lua:37: resource "gopher.icon" not found:
        no file "/home/spc/.luarocks/share/lua/5.3/org.conman.app.gopherclient/gopher.icon"
        no file "/home/spc/.luarocks/lib/lua/5.3/org.conman.app.gopherclient/gopher.icon"
        no file "/usr/local/share/lua/5.3/org.conman.app.gopherclient/gopher.icon"
        no file "/usr/local/lib/lua/5.3/org.conman.app.gopherclient/gopher.icon"
        no file "./org.conman.app.gopherclient/gopher.icon"
stack traceback:
        [C]: in function 'error'
        res.lua:37: in function 'resource'
        res.lua:40: in main chunk
        [C]: in ?

  -spc