Make vec2(vec2(1,2)) and vec3(vec3(1,2,3)) consistent between lua and luajit

This commit is contained in:
mcc 2019-11-29 14:44:34 -05:00
parent 6c49915112
commit b80543c242
4 changed files with 23 additions and 2 deletions

View File

@ -54,7 +54,7 @@ function vec2.new(x, y)
return new(x, y)
-- {x, y} or {x=x, y=y}
elseif type(x) == "table" then
elseif type(x) == "table" or type(x) == "cdata" then -- table in vanilla lua, cdata in luajit
local xx, yy = x.x or x[1], x.y or x[2]
assert(type(xx) == "number", "new: Wrong argument type for x (<number> expected)")
assert(type(yy) == "number", "new: Wrong argument type for y (<number> expected)")

View File

@ -55,7 +55,7 @@ function vec3.new(x, y, z)
return new(x, y, z)
-- {x, y, z} or {x=x, y=y, z=z}
elseif type(x) == "table" then
elseif type(x) == "table" or type(x) == "cdata" then -- table in vanilla lua, cdata in luajit
local xx, yy, zz = x.x or x[1], x.y or x[2], x.z or x[3]
assert(type(xx) == "number", "new: Wrong argument type for x (<number> expected)")
assert(type(yy) == "number", "new: Wrong argument type for y (<number> expected)")

View File

@ -41,6 +41,12 @@ describe("vec2:", function()
assert.is.equal(a, b)
end)
it("clones a vector using the constructor", function()
local a = vec2(3, 5)
local b = vec2(a)
assert.is.equal(a, b)
end)
it("adds a vector to another", function()
local a = vec2(3, 5)
local b = vec2(7, 4)
@ -182,4 +188,13 @@ describe("vec2:", function()
local b = a:to_string()
assert.is.equal("(+0.000,+0.000)", b)
end)
-- Do this last, to insulate tests from accidental state contamination
it("converts a vec3 to vec2 using the constructor", function()
local vec3 = require "modules.vec3"
local a = vec2(3, 5)
local b = vec3(3, 5, 7)
local c = vec2(b)
assert.is.equal(a, c)
end)
end)

View File

@ -46,6 +46,12 @@ describe("vec3:", function()
assert.is.equal(a, b)
end)
it("clones a vector using the constructor", function()
local a = vec3(3, 5, 7)
local b = vec3(a)
assert.is.equal(a, b)
end)
it("adds a vector to another", function()
local a = vec3(3, 5, 7)
local b = vec3(7, 4, 1)