Skip to content

Commit

Permalink
Fix require to use proper file paths for the require caches
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Dec 8, 2011
1 parent 19f5384 commit b4e3324
Showing 1 changed file with 39 additions and 26 deletions.
65 changes: 39 additions & 26 deletions lib/luvit.lua
Expand Up @@ -161,9 +161,27 @@ error_meta = {__tostring=function(table) return table.message end}

local global_meta = {__index=_G}

local function partial_realpath(path)
-- Do some minimal realpathing
local link = FS.lstat_sync(path).is_symbolic_link and FS.readlink_sync(path)
while link do
path = Path.join(Path.dirname(path), link)

This comment has been minimized.

Copy link
@dvv

dvv Dec 12, 2011

Member

Path.resolve here, or nasty bug occurs -- you can't run a luvit script if it's a symlink.
Fix is at dvv@e00ddd7#diff-0

link = FS.lstat_sync(path).is_symbolic_link and FS.readlink_sync(path)
end
return path
end

local function myloadfile(path)
if not FS.exists_sync(path) then return end

path = partial_realpath(path)

if package.loaded[path] then
return function ()
return package.loaded[path]
end
end

local code = FS.read_file_sync(path)

local fn = assert(loadstring(code, '@' .. path))
Expand All @@ -176,19 +194,32 @@ local function myloadfile(path)
return real_require(path, dirname)
end,
}, global_meta))
return fn
local module = fn()
package.loaded[path] = module
return function() return module end
end

local function myloadlib(path, name)
-- TODO: realpath path first
if not FS.exists_sync(path) then return end

path = partial_realpath(path)

if package.loaded[path] then
return function ()
return package.loaded[path]
end
end

local name = Path.basename(path)
if name == "init.luvit" then
name = Path.basename(Path.dirname(path))
end
local fn, error_message = package.loadlib(path, "luaopen_" .. name:sub(1, #name - 6))
if fn then return fn end
if fn then
local module = fn()
package.loaded[path] = module
return function() return module end
end
error(error_message)
end

Expand All @@ -212,20 +243,9 @@ local function load_module(path, verbose)
end
end

-- Then try with lua appended
fn = myloadfile(path .. ".lua")
if fn then return fn end

-- Then try C addon with luvit appended
fn = myloadlib(path .. ".luvit")
if fn then return fn end

-- Then Try a folder with init.lua in it
fn = myloadfile(path .. "/init.lua")
if fn then return fn end

-- Finally try the same for a C addon
fn = myloadlib(path .. "/init.luvit")
-- Try to load as either lua script or binary extension
local fn = myloadfile(path .. ".lua") or myloadfile(path .. "/init.lua")
or myloadlib(path .. ".luvit") or myloadlib(path .. "/init.luvit")
if fn then return fn end

return "\n\tCannot find module " .. path
Expand Down Expand Up @@ -253,13 +273,9 @@ function require(path, dirname)
absolute_path = Path.join(dirname, path)
end
if absolute_path then
module = package.loaded[absolute_path]
if module then return module end
local loader = load_module(absolute_path)
if type(loader) == "function" then
module = loader()
package.loaded[absolute_path] = module
return module
return loader()
else
error("Failed to find module '" .. path .."'")
end
Expand All @@ -286,12 +302,9 @@ function require(path, dirname)
repeat
dir = dir:sub(1, dir:find("/[^/]*$") - 1)
local full_path = dir .. "/modules/" .. path
if package.loaded[full_path] then return package.loaded[full_path] end
local loader = load_module(dir .. "/modules/" .. path)
if type(loader) == "function" then
local module = loader()
package.loaded[full_path] = module
return module
return loader()
else
errors[#errors + 1] = loader
end
Expand Down

0 comments on commit b4e3324

Please sign in to comment.