Open TingPing opened 7 years ago
Two comments on your code (I know you write "basic implementation", sorry): It only works in Lua 5.1 (package.loaders
was replaced by package.searchers
, but I think the semantics are still the same; loadstring
was replaced by load
) and as far as I can tell, data:get_data()
cannot fail (g_bytes_get_data()
only returns NULL
when size=0
), so I'd replace the second part of the code with loadstring(data:get_data() or "")
. Oh and perhaps the second argument to loadstring
/ load
should be str
so that Lua can use the URI to the 'file' in error messages.
For the idea: I like it. A lot. However, I do not see how to add it nicely to LGI, too.
Thanks for the comments. Honestly it could be as simple as lgi.require_resource('/foo/bar/window.lua')
Here is an updated version with better error printing:
function (str)
if str:sub(1, 12) == 'resources://' then
local data = Gio.resources_lookup_data(str:sub(13), Gio.ResourceLookupFlags.NONE)
if not data then
return ' - Failed to find data for that path!'
end
local module, err = loadstring(data:get_data() or '', str)
if not module then
return ' - ' .. err
end
return module
end
end
One of the nice features of
gjs
is that you can import modules from within a GResource. This leads to the common usage of applications shipping a single resource bundle or single executable and avoids doing extra blocking disk io.Here is a basic implementation of this:
Now I am not overly familiar with the codebase or even Lua so I am unsure where this best belongs but any feedback on the idea would be appreciated.