From a86935a39b4586782f9a7be2f79ea618d192cab8 Mon Sep 17 00:00:00 2001 From: mcc Date: Fri, 29 Nov 2019 21:13:30 -0500 Subject: [PATCH] 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. --- modules/bound2.lua | 4 +++- modules/bound3.lua | 4 +++- modules/mat4.lua | 4 +++- modules/quat.lua | 4 +++- modules/vec2.lua | 4 +++- modules/vec3.lua | 4 +++- 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/modules/bound2.lua b/modules/bound2.lua index c3639c8..22da310 100644 --- a/modules/bound2.lua +++ b/modules/bound2.lua @@ -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) diff --git a/modules/bound3.lua b/modules/bound3.lua index 4209f84..8c26236 100644 --- a/modules/bound3.lua +++ b/modules/bound3.lua @@ -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) diff --git a/modules/mat4.lua b/modules/mat4.lua index 67a09ba..3cd06f5 100644 --- a/modules/mat4.lua +++ b/modules/mat4.lua @@ -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) diff --git a/modules/quat.lua b/modules/quat.lua index 1cd1de7..e8ea330 100644 --- a/modules/quat.lua +++ b/modules/quat.lua @@ -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) diff --git a/modules/vec2.lua b/modules/vec2.lua index cfc0653..d7a0786 100644 --- a/modules/vec2.lua +++ b/modules/vec2.lua @@ -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) diff --git a/modules/vec3.lua b/modules/vec3.lua index 97b7f80..95c96eb 100644 --- a/modules/vec3.lua +++ b/modules/vec3.lua @@ -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)