Output the type that was incorrect to make it more obvious what the user
is doing wrong without them adding logging.
To avoid string building during lots of vector math, introduced a
private precond module that only builds the strings when an error
occurs. It uses error() to put the resulting error message at the caller
to cpml.
Before:
lua: ~/cpml/modules/vec2.lua:52: new: Wrong argument type for x (<number> expected)
lua: ~/cpml/modules/vec2.lua:424: __add: Wrong argument type for right hand operand. (<cpml.vec2> expected)
example stack traceback:
[C]: in function 'assert'
~/cpml/modules/vec2.lua:424: in metamethod '__add'
test_cpml.lua:32: in main chunk
[C]: in ?
After:
lua: test_cpml.lua:31: new: Wrong argument type for x: string (<number> expected)
lua: test_cpml.lua:32: __add: Wrong argument type 'string' for right hand operand. (<cpml.vec2> expected)
example stack traceback:
[C]: in function 'error'
~/cpml/modules/_private_precond.lua:13: in function 'modules._private_precond.assert'
~/cpml/modules/vec2.lua:425: in metamethod '__add'
test_cpml.lua:32: in main chunk
[C]: in ?
The tracebacks are longer, but the initial error is at the location of
the mistake and the output includes the input type.
forward, side, new_up are unused but are shadowed in many functions.
Doesn't seem like they're intended to be used.
No reason for variables in CreateNDCScaleAndOffsetFromFov to be global.
Don't try running this change, but I'm just making them local.
On Github we run unit tests inside "busted". At the start of each test "busted" does some sort of trick (clearing package.loaded)? Which causes "require" to run again for all lua files. This breaks ffi.metatype with error "cannot change a protected metatable" if it is called twice with a single type name, since this is true global state. To work around this this patch wraps ffi.metatype calls in a xpcall() so that failure is silently ignored.
- vec2 to_polar/from_cartesian tests were testing for equality rather than using an epsilon.
- bound2.contains had two tests that were plain wrong.
- While I'm fixing the bounds test, bound2.contains and bound3.contains probably ought to test their own min and max values for inclusion.
- The implementation of mat4.look_at appears to be wrong. The final column was being set to 0,0,0,1 which comparing against other implementations does not seem to be correct. My replacement code is modeled on the method used in mat4x4_look_at() in linmath.h in GLFW, which says it's a reimplementation on the method from gluLookAt(). With this change the test passes as originally written.