Allow ffi.metatype to fail so that "busted" unit tests work

On Github we run unit tests inside "busted". At the start of each test "busted" does some sort of trick (clearing package.loaded)? Which causes "require" to run again for all lua files. This breaks ffi.metatype with error "cannot change a protected metatable" if it is called twice with a single type name, since this is true global state. To work around this this patch wraps ffi.metatype calls in a xpcall() so that failure is silently ignored.
This commit is contained in:
mcc 2019-11-29 21:13:30 -05:00
parent d845a479ac
commit a86935a39b
6 changed files with 18 additions and 6 deletions

View File

@ -160,7 +160,9 @@ function bound2_mt.__call(_, a, b)
end
if status then
ffi.metatype(new, bound2_mt)
xpcall(function() -- Allow this to silently fail; assume failure means someone messed with package.loaded
ffi.metatype(new, bound2_mt)
end, function() end)
end
return setmetatable({}, bound2_mt)

View File

@ -160,7 +160,9 @@ function bound3_mt.__call(_, a, b)
end
if status then
ffi.metatype(new, bound3_mt)
xpcall(function() -- Allow this to silently fail; assume failure means someone messed with package.loaded
ffi.metatype(new, bound3_mt)
end, function() end)
end
return setmetatable({}, bound3_mt)

View File

@ -853,7 +853,9 @@ function mat4_mt.__mul(a, b)
end
if status then
ffi.metatype(new, mat4_mt)
xpcall(function() -- Allow this to silently fail; assume failure means someone messed with package.loaded
ffi.metatype(new, mat4_mt)
end, function() end)
end
return setmetatable({}, mat4_mt)

View File

@ -469,7 +469,9 @@ function quat_mt.__pow(a, n)
end
if status then
ffi.metatype(new, quat_mt)
xpcall(function() -- Allow this to silently fail; assume failure means someone messed with package.loaded
ffi.metatype(new, quat_mt)
end, function() end)
end
return setmetatable({}, quat_mt)

View File

@ -381,7 +381,9 @@ function vec2_mt.__div(a, b)
end
if status then
ffi.metatype(new, vec2_mt)
xpcall(function() -- Allow this to silently fail; assume failure means someone messed with package.loaded
ffi.metatype(new, vec2_mt)
end, function() end)
end
return setmetatable({}, vec2_mt)

View File

@ -361,7 +361,9 @@ function vec3_mt.__div(a, b)
end
if status then
ffi.metatype(new, vec3_mt)
xpcall(function() -- Allow this to silently fail; assume failure means someone messed with package.loaded
ffi.metatype(new, vec3_mt)
end, function() end)
end
return setmetatable({}, vec3_mt)