forked from koreader/koreader-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffi_wrapper.lua
38 lines (35 loc) · 1.1 KB
/
ffi_wrapper.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
-- Check if we're running a busted version recent enough that we don't need to deal with the LuaJIT hacks...
-- That currently means > 2.0.0 (i.e., scm-2, which isn't on LuaRocks...).
local busted_ok = false
for name, _ in pairs(package.loaded) do
if name == "busted.luajit" then
busted_ok = true
break
end
end
-- Whee!
if busted_ok then
return
end
-- Don't try to overwrite metatables so we can use --auto-insulate-tests
-- Shamelessly copied from https://github.com/Olivine-Labs/busted/commit/2dfff99bda01fd3da56fd23415aba5a2a4cc0ffd
local ffi = require "ffi"
local original_metatype = ffi.metatype
local original_store = {}
ffi.metatype = function (primary, ...)
if original_store[primary] then
return original_store[primary]
end
local success, result, err = pcall(original_metatype, primary, ...)
if not success then
-- hard error was thrown
error(result, 2)
end
if not result then
-- soft error was returned
return result, err
end
-- it worked, store and return
original_store[primary] = result
return result
end