Backout some FFI code - didn't work.
This commit is contained in:
parent
cb41f1c4a7
commit
d198d7f97f
@ -30,30 +30,12 @@ local sqrt, cos, sin, atan2 = math.sqrt, math.cos, math.sin, math.atan2
|
||||
local vector = {}
|
||||
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
|
||||
local function new(x,y)
|
||||
return setmetatable({x = x or 0, y = y or 0}, vector)
|
||||
end
|
||||
|
||||
if hasffi and jitenabled then
|
||||
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
|
||||
else
|
||||
function new(x,y)
|
||||
return setmetatable({x = x or 0, y = y or 0}, vector)
|
||||
end
|
||||
function isvector(v)
|
||||
return getmetatable(v) == vector
|
||||
end
|
||||
local function isvector(v)
|
||||
return getmetatable(v) == vector
|
||||
end
|
||||
|
||||
local zero = new(0,0)
|
||||
@ -204,10 +186,6 @@ function vector:trim(maxLen)
|
||||
return self:clone():trim_inplace(maxLen)
|
||||
end
|
||||
|
||||
if hasffi and jitenabled and not hot_loaded then
|
||||
ffi.metatype(new, vector)
|
||||
end
|
||||
|
||||
-- the module
|
||||
return setmetatable({new = new, isvector = isvector, zero = zero},
|
||||
{__call = function(_, ...) return new(...) end})
|
||||
|
@ -33,35 +33,16 @@ local sqrt, cos, sin, atan2, acos = math.sqrt, math.cos, math.sin, math.atan2, m
|
||||
local vector = {}
|
||||
vector.__index = vector
|
||||
|
||||
local function new(x,y,z)
|
||||
-- allow construction via vec3(a, b, c), vec3 { a, b, c } or vec3 { x = a, y = b, z = c }
|
||||
if type(x) == "table" then
|
||||
return setmetatable({x=x.x or x[1] or 0, y=x.y or x[2] or 0, z=x.z or x[3] or 0}, vector)
|
||||
end
|
||||
return setmetatable({x = x or 0, y = y or 0, z = z or 0}, vector)
|
||||
end
|
||||
|
||||
-- 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
|
||||
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
|
||||
else
|
||||
function new(x,y,z)
|
||||
-- allow construction via vec3(a, b, c), vec3 { a, b, c } or vec3 { x = a, y = b, z = c }
|
||||
if type(x) == "table" then
|
||||
return setmetatable({x=x.x or x[1] or 0, y=x.y or x[2] or 0, z=x.z or x[3] or 0}, vector)
|
||||
end
|
||||
return setmetatable({x = x or 0, y = y or 0, z = z or 0}, vector)
|
||||
end
|
||||
function isvector(v)
|
||||
return getmetatable(v) == vector or type(v.x and v.y and v.z) == "number"
|
||||
end
|
||||
local function isvector(v)
|
||||
return getmetatable(v) == vector or type(v.x and v.y and v.z) == "number"
|
||||
end
|
||||
|
||||
local zero = new(0,0,0)
|
||||
@ -254,10 +235,6 @@ function vector.lerp(a, b, s)
|
||||
return a + s * (b - a)
|
||||
end
|
||||
|
||||
if hasffi and jitenabled and not hot_loaded then
|
||||
ffi.metatype(new, vector)
|
||||
end
|
||||
|
||||
-- the module
|
||||
return setmetatable(
|
||||
{
|
||||
|
@ -23,6 +23,15 @@ describe("vec3:", function()
|
||||
assert.is.equal(vec3(1, 1, 1) / 2, vec3(0.5, 0.5, 0.5))
|
||||
assert.has.errors(function() return vec3(1, 1, 1) / vec3(2, 2, 2) end)
|
||||
end)
|
||||
|
||||
it("testing value ranges", function()
|
||||
-- This makes sure we are initializing reasonably and that
|
||||
-- we haven't broken everything with some FFI magic.
|
||||
assert.is.equal(vec3(256, 0, 0).x, 256)
|
||||
assert.is.equal(vec3(0, 65537, 0).y, 65537)
|
||||
assert.is.equal(vec3(953, 0, 491.5).z, 491.5)
|
||||
assert.is.equal(vec3(0, 1.2222, 0).y, 1.2222)
|
||||
end)
|
||||
|
||||
it("testing comparison operators", function()
|
||||
-- eq
|
||||
|
Loading…
x
Reference in New Issue
Block a user