Don't explode if the module has been hot reloaded.
Argh, struct redefinition errors. I'm glad that you can pcall this at least.
This commit is contained in:
parent
e6b9c84a6c
commit
cb41f1c4a7
@ -33,11 +33,17 @@ vector.__index = vector
|
||||
-- Courtesy of slime
|
||||
local hasffi, ffi = pcall(require, "ffi")
|
||||
local jitenabled = jit and jit.status() or false
|
||||
local hot_loaded = false
|
||||
local new, isvector
|
||||
|
||||
if hasffi and jitenabled then
|
||||
ffi.cdef "struct cpml_vec2 { double x, y; };"
|
||||
new = ffi.typeof("struct cpml_vec2")
|
||||
xpcall(function()
|
||||
hot_loaded = true
|
||||
new = ffi.typeof("struct cpml_vec2")
|
||||
end, function()
|
||||
ffi.cdef "struct cpml_vec2 { double x, y; };"
|
||||
new = ffi.typeof("struct cpml_vec2")
|
||||
end)
|
||||
function isvector(v)
|
||||
return ffi.istype(new, v)
|
||||
end
|
||||
@ -198,7 +204,7 @@ function vector:trim(maxLen)
|
||||
return self:clone():trim_inplace(maxLen)
|
||||
end
|
||||
|
||||
if hasffi and jitenabled then
|
||||
if hasffi and jitenabled and not hot_loaded then
|
||||
ffi.metatype(new, vector)
|
||||
end
|
||||
|
||||
|
@ -37,11 +37,17 @@ vector.__index = vector
|
||||
-- Courtesy of slime
|
||||
local hasffi, ffi = pcall(require, "ffi")
|
||||
local jitenabled = jit and jit.status() or false
|
||||
local hot_loaded = false
|
||||
local new, isvector
|
||||
|
||||
if hasffi and jitenabled then
|
||||
ffi.cdef "struct cpml_vec3 { double x, y, z; };"
|
||||
new = ffi.typeof("struct cpml_vec3")
|
||||
xpcall(function()
|
||||
hot_loaded = true
|
||||
new = ffi.typeof("struct cpml_vec3")
|
||||
end, function()
|
||||
ffi.cdef "struct cpml_vec3 { double x, y, z; };"
|
||||
new = ffi.typeof("struct cpml_vec3")
|
||||
end)
|
||||
function isvector(v)
|
||||
return ffi.istype(new, v) or type(v.x and v.y and v.z) == "number"
|
||||
end
|
||||
@ -248,7 +254,7 @@ function vector.lerp(a, b, s)
|
||||
return a + s * (b - a)
|
||||
end
|
||||
|
||||
if hasffi and jitenabled then
|
||||
if hasffi and jitenabled and not hot_loaded then
|
||||
ffi.metatype(new, vector)
|
||||
end
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user