Skip to content

Commit 15bafc9

Browse files
committed
Merge pull request #4 from pygy/pygy-patch-tst
Updates to address the remarks in #3.
2 parents 91d8650 + cffb2fc commit 15bafc9

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

require.lua

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,27 @@ local t_concat = table.concat
88

99
--- Helpers
1010

11-
local function _tostring(s) return ""..s end
1211

1312
local function checkstring(s)
14-
local success, res = pcall(_tostring, s)
15-
if success then return res end
16-
error("bad argument #1 to 'require' (string expected, got "..type(s)..")", 3)
13+
local t = type(s)
14+
if t == "string" then
15+
return s
16+
elseif t == "number" then
17+
return tostring(s)
18+
else
19+
error("bad argument #1 to 'require' (string expected, got "..t..")", 3)
20+
end
1721
end
1822

1923
--- for Lua 5.1
2024

2125
local package, p_loaded = package, package.loaded
2226

23-
local sentinel = newproxy and newproxy() or string.char(1,2,3,4,5)
27+
28+
local sentinel do
29+
local function errhandler() error("the require() sentinel can't be indexed or updated", 2) end
30+
sentinel = newproxy and newproxy() or setmetatable({}, {__index = errhandler, __newindex = errhandler, __metatable = false})
31+
end
2432

2533
local function require51 (name)
2634
name = checkstring(name)
@@ -113,4 +121,4 @@ for _, o in ipairs{
113121
end
114122
end
115123

116-
return module
124+
return module

0 commit comments

Comments
 (0)