From cb41f1c4a747f0dfcde69796812f798f9c868578 Mon Sep 17 00:00:00 2001 From: Colby Klein Date: Wed, 19 Aug 2015 14:30:48 -0700 Subject: [PATCH] Don't explode if the module has been hot reloaded. Argh, struct redefinition errors. I'm glad that you can pcall this at least. --- modules/vec2.lua | 12 +++++++++--- modules/vec3.lua | 12 +++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/modules/vec2.lua b/modules/vec2.lua index a954829..7df0a99 100644 --- a/modules/vec2.lua +++ b/modules/vec2.lua @@ -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 diff --git a/modules/vec3.lua b/modules/vec3.lua index 6127371..3b04efa 100644 --- a/modules/vec3.lua +++ b/modules/vec3.lua @@ -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