Skip to content

Commit

Permalink
Fix getmetatable calls
Browse files Browse the repository at this point in the history
Use `debug.getmetatable` instead of `getmetatable` in case there is a
`__metatable` field present in the object's metatable.
  • Loading branch information
o-lim committed May 8, 2015
1 parent 366b39c commit d8218f0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion busted/context.lua
Expand Up @@ -6,7 +6,7 @@ local function save()
g[k] = rawget(_G, k)
end
return {
gmt = getmetatable(_G),
gmt = debug.getmetatable(_G),
g = g,
loaded = tablex.copy(package.loaded)
}
Expand Down
10 changes: 5 additions & 5 deletions busted/core.lua
Expand Up @@ -24,15 +24,15 @@ local pendingMt = {

local function metatype(obj)
local otype = type(obj)
return otype == 'table' and (getmetatable(obj) or {}).__type or otype
return otype == 'table' and (debug.getmetatable(obj) or {}).__type or otype
end

local function hasToString(obj)
return type(obj) == 'string' or (getmetatable(obj) or {}).__tostring
return type(obj) == 'string' or (debug.getmetatable(obj) or {}).__tostring
end

local function isCallable(obj)
return (type(obj) == 'function' or (getmetatable(obj) or {}).__call)
return type(obj) == 'function' or (debug.getmetatable(obj) or {}).__call
end

return function()
Expand Down Expand Up @@ -143,7 +143,7 @@ return function()

function busted.bindfenv(callable, var, value)
local env = {}
local f = (getmetatable(callable) or {}).__call or callable
local f = (debug.getmetatable(callable) or {}).__call or callable
setmetatable(env, { __index = getfenv(f) })
env[var] = value
setfenv(f, env)
Expand All @@ -152,7 +152,7 @@ return function()
function busted.wrap(callable)
if isCallable(callable) then
-- prioritize __call if it exists, like in files
environment.wrap((getmetatable(callable) or {}).__call or callable)
environment.wrap((debug.getmetatable(callable) or {}).__call or callable)
end
end

Expand Down
2 changes: 1 addition & 1 deletion busted/runner.lua
Expand Up @@ -171,7 +171,7 @@ return function(options)
local ctx = busted.context.get()
local children = busted.context.children(ctx)
local file = children[#children]
getmetatable(file.run).__call = info.func
debug.getmetatable(file.run).__call = info.func
end

local runs = cliArgs['repeat']
Expand Down

0 comments on commit d8218f0

Please sign in to comment.