From fee157b6b9326589bbb44367f84226d6394f47eb Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Mon, 14 Aug 2017 22:56:20 -0300 Subject: [PATCH] Functions that take a boolean argument now properly type-check for it. --HG-- branch : minor --- src/common/runtime.cpp | 6 ++++++ src/common/runtime.h | 6 ++++++ src/modules/audio/wrap_Audio.cpp | 2 +- src/modules/audio/wrap_Source.cpp | 4 ++-- src/modules/filesystem/wrap_Filesystem.cpp | 2 +- src/modules/graphics/wrap_Graphics.cpp | 20 ++++++++----------- src/modules/graphics/wrap_Mesh.cpp | 2 +- src/modules/graphics/wrap_ParticleSystem.cpp | 4 ++-- src/modules/graphics/wrap_Shader.cpp | 2 +- src/modules/keyboard/wrap_Keyboard.cpp | 4 ++-- src/modules/math/wrap_Transform.cpp | 2 +- src/modules/mouse/wrap_Mouse.cpp | 6 +++--- src/modules/physics/box2d/Physics.cpp | 2 +- src/modules/physics/box2d/wrap_Body.cpp | 10 +++++----- src/modules/physics/box2d/wrap_Contact.cpp | 2 +- src/modules/physics/box2d/wrap_Fixture.cpp | 2 +- .../physics/box2d/wrap_PrismaticJoint.cpp | 4 ++-- .../physics/box2d/wrap_RevoluteJoint.cpp | 4 ++-- src/modules/physics/box2d/wrap_WheelJoint.cpp | 2 +- src/modules/physics/box2d/wrap_World.cpp | 2 +- src/modules/window/wrap_Window.cpp | 4 ++-- 21 files changed, 50 insertions(+), 42 deletions(-) diff --git a/src/common/runtime.cpp b/src/common/runtime.cpp index a56519aca..cccbdc735 100644 --- a/src/common/runtime.cpp +++ b/src/common/runtime.cpp @@ -171,6 +171,12 @@ bool luax_toboolean(lua_State *L, int idx) return (lua_toboolean(L, idx) != 0); } +bool luax_checkboolean(lua_State *L, int idx) +{ + luaL_checktype(L, idx, LUA_TBOOLEAN); + return luax_toboolean(L, idx); +} + void luax_pushboolean(lua_State *L, bool b) { lua_pushboolean(L, b ? 1 : 0); diff --git a/src/common/runtime.h b/src/common/runtime.h index e728ea0ae..569dc5274 100644 --- a/src/common/runtime.h +++ b/src/common/runtime.h @@ -125,6 +125,12 @@ bool luax_isarrayoftables(lua_State *L, int idx); **/ bool luax_toboolean(lua_State *L, int idx); +/** + * Returns the boolean value at idx. Causes a Lua error if the value is not + * a boolean. + **/ +bool luax_checkboolean(lua_State *L, int idx); + /** * Pushes a bool onto the stack. It's the same as lua_pushboolean, * but with bool instead of int. diff --git a/src/modules/audio/wrap_Audio.cpp b/src/modules/audio/wrap_Audio.cpp index 395b179b1..4b19cb0e9 100644 --- a/src/modules/audio/wrap_Audio.cpp +++ b/src/modules/audio/wrap_Audio.cpp @@ -520,7 +520,7 @@ int w_isEffectsSupported(lua_State *L) int w_setMixWithSystem(lua_State *L) { - luax_pushboolean(L, instance()->setMixWithSystem(luax_toboolean(L, 1))); + luax_pushboolean(L, instance()->setMixWithSystem(luax_checkboolean(L, 1))); return 1; } diff --git a/src/modules/audio/wrap_Source.cpp b/src/modules/audio/wrap_Source.cpp index 6ce02a32d..5d2891adf 100644 --- a/src/modules/audio/wrap_Source.cpp +++ b/src/modules/audio/wrap_Source.cpp @@ -235,7 +235,7 @@ int w_Source_getCone(lua_State *L) int w_Source_setRelative(lua_State *L) { Source *t = luax_checksource(L, 1); - luax_catchexcept(L, [&](){ t->setRelative(luax_toboolean(L, 2)); }); + luax_catchexcept(L, [&](){ t->setRelative(luax_checkboolean(L, 2)); }); return 0; } @@ -249,7 +249,7 @@ int w_Source_isRelative(lua_State *L) int w_Source_setLooping(lua_State *L) { Source *t = luax_checksource(L, 1); - luax_catchexcept(L, [&](){ t->setLooping(luax_toboolean(L, 2)); }); + luax_catchexcept(L, [&](){ t->setLooping(luax_checkboolean(L, 2)); }); return 0; } diff --git a/src/modules/filesystem/wrap_Filesystem.cpp b/src/modules/filesystem/wrap_Filesystem.cpp index fa746850e..42e191bc1 100644 --- a/src/modules/filesystem/wrap_Filesystem.cpp +++ b/src/modules/filesystem/wrap_Filesystem.cpp @@ -595,7 +595,7 @@ int w_getSize(lua_State *L) int w_setSymlinksEnabled(lua_State *L) { - instance()->setSymlinksEnabled(luax_toboolean(L, 1)); + instance()->setSymlinksEnabled(luax_checkboolean(L, 1)); return 0; } diff --git a/src/modules/graphics/wrap_Graphics.cpp b/src/modules/graphics/wrap_Graphics.cpp index 4ec960959..8d27574c9 100644 --- a/src/modules/graphics/wrap_Graphics.cpp +++ b/src/modules/graphics/wrap_Graphics.cpp @@ -1168,9 +1168,8 @@ int w_newCanvas(lua_State *L) lua_getfield(L, startidx, "readable"); if (!lua_isnoneornil(L, -1)) { - luaL_checktype(L, -1, LUA_TBOOLEAN); settings.readable.hasValue = true; - settings.readable.value = luax_toboolean(L, -1); + settings.readable.value = luax_checkboolean(L, -1); } lua_pop(L, 1); @@ -1327,8 +1326,7 @@ int w_newShader(lua_State *L) int w_validateShader(lua_State *L) { - luaL_checktype(L, 1, LUA_TBOOLEAN); - bool gles = luax_toboolean(L, 1); + bool gles = luax_checkboolean(L, 1); Shader::ShaderSource source; w_getShaderSource(L, 2, gles, source); @@ -1712,10 +1710,10 @@ int w_setColorMask(lua_State *L) } else { - mask.r = luax_toboolean(L, 1); - mask.g = luax_toboolean(L, 2); - mask.b = luax_toboolean(L, 3); - mask.a = luax_toboolean(L, 4); + mask.r = luax_checkboolean(L, 1); + mask.g = luax_checkboolean(L, 2); + mask.b = luax_checkboolean(L, 3); + mask.a = luax_checkboolean(L, 4); } instance()->setColorMask(mask); @@ -1912,7 +1910,7 @@ int w_getPointSize(lua_State *L) int w_setWireframe(lua_State *L) { - instance()->setWireframe(luax_toboolean(L, 1)); + instance()->setWireframe(luax_checkboolean(L, 1)); return 0; } @@ -2033,9 +2031,7 @@ int w_getCanvasFormats(lua_State *L) if (!lua_isnoneornil(L, 1)) { - luaL_checktype(L, 1, LUA_TBOOLEAN); - - if (luax_toboolean(L, 1)) + if (luax_checkboolean(L, 1)) { supported = [](PixelFormat format) -> bool { diff --git a/src/modules/graphics/wrap_Mesh.cpp b/src/modules/graphics/wrap_Mesh.cpp index 6f6b37f14..4a0c3b352 100644 --- a/src/modules/graphics/wrap_Mesh.cpp +++ b/src/modules/graphics/wrap_Mesh.cpp @@ -315,7 +315,7 @@ int w_Mesh_setAttributeEnabled(lua_State *L) { Mesh *t = luax_checkmesh(L, 1); const char *name = luaL_checkstring(L, 2); - bool enable = luax_toboolean(L, 3); + bool enable = luax_checkboolean(L, 3); luax_catchexcept(L, [&](){ t->setAttributeEnabled(name, enable); }); return 0; } diff --git a/src/modules/graphics/wrap_ParticleSystem.cpp b/src/modules/graphics/wrap_ParticleSystem.cpp index 0f0e7642e..2be508c1b 100644 --- a/src/modules/graphics/wrap_ParticleSystem.cpp +++ b/src/modules/graphics/wrap_ParticleSystem.cpp @@ -251,7 +251,7 @@ int w_ParticleSystem_getAreaSpreadAngle(lua_State *L) int w_ParticleSystem_setAreaSpreadIsRelativeDirection(lua_State *L) { ParticleSystem *t = luax_checkparticlesystem(L, 1); - bool arg1 = luax_toboolean(L, 2); + bool arg1 = luax_checkboolean(L, 2); t->setAreaSpreadIsRelativeDirection(arg1); return 0; } @@ -650,7 +650,7 @@ int w_ParticleSystem_getQuads(lua_State *L) int w_ParticleSystem_setRelativeRotation(lua_State *L) { ParticleSystem *t = luax_checkparticlesystem(L, 1); - t->setRelativeRotation(luax_toboolean(L, 2)); + t->setRelativeRotation(luax_checkboolean(L, 2)); return 0; } diff --git a/src/modules/graphics/wrap_Shader.cpp b/src/modules/graphics/wrap_Shader.cpp index fb48b4e02..26ca2f535 100644 --- a/src/modules/graphics/wrap_Shader.cpp +++ b/src/modules/graphics/wrap_Shader.cpp @@ -159,7 +159,7 @@ int w_Shader_sendMatrices(lua_State *L, int startidx, Shader *shader, const Shad if (lua_isboolean(L, startidx)) { - columnmajor = lua_toboolean(L, startidx); + columnmajor = luax_toboolean(L, startidx); startidx++; } diff --git a/src/modules/keyboard/wrap_Keyboard.cpp b/src/modules/keyboard/wrap_Keyboard.cpp index f8633353b..818dd394c 100644 --- a/src/modules/keyboard/wrap_Keyboard.cpp +++ b/src/modules/keyboard/wrap_Keyboard.cpp @@ -33,7 +33,7 @@ namespace keyboard int w_setKeyRepeat(lua_State *L) { - instance()->setKeyRepeat(luax_toboolean(L, 1)); + instance()->setKeyRepeat(luax_checkboolean(L, 1)); return 0; } @@ -159,7 +159,7 @@ int w_getKeyFromScancode(lua_State *L) int w_setTextInput(lua_State *L) { - bool enable = luax_toboolean(L, 1); + bool enable = luax_checkboolean(L, 1); if (lua_gettop(L) <= 1) instance()->setTextInput(enable); diff --git a/src/modules/math/wrap_Transform.cpp b/src/modules/math/wrap_Transform.cpp index 0bd834a54..45622ca89 100644 --- a/src/modules/math/wrap_Transform.cpp +++ b/src/modules/math/wrap_Transform.cpp @@ -137,7 +137,7 @@ int w_Transform_setMatrix(lua_State *L) int idx = 2; if (lua_isboolean(L, idx)) { - columnmajor = lua_toboolean(L, idx); + columnmajor = luax_toboolean(L, idx); idx++; } diff --git a/src/modules/mouse/wrap_Mouse.cpp b/src/modules/mouse/wrap_Mouse.cpp index 05baaa5a0..3295c1f91 100644 --- a/src/modules/mouse/wrap_Mouse.cpp +++ b/src/modules/mouse/wrap_Mouse.cpp @@ -170,7 +170,7 @@ int w_isDown(lua_State *L) int w_setVisible(lua_State *L) { - bool b = luax_toboolean(L, 1); + bool b = luax_checkboolean(L, 1); instance()->setVisible(b); return 0; } @@ -183,7 +183,7 @@ int w_isVisible(lua_State *L) int w_setGrabbed(lua_State *L) { - bool b = luax_toboolean(L, 1); + bool b = luax_checkboolean(L, 1); instance()->setGrabbed(b); return 0; } @@ -196,7 +196,7 @@ int w_isGrabbed(lua_State *L) int w_setRelativeMode(lua_State *L) { - bool relative = luax_toboolean(L, 1); + bool relative = luax_checkboolean(L, 1); luax_pushboolean(L, instance()->setRelativeMode(relative)); return 1; } diff --git a/src/modules/physics/box2d/Physics.cpp b/src/modules/physics/box2d/Physics.cpp index cddfe2eb9..f5ffa40a6 100644 --- a/src/modules/physics/box2d/Physics.cpp +++ b/src/modules/physics/box2d/Physics.cpp @@ -164,7 +164,7 @@ int Physics::newChainShape(lua_State *L) return luaL_error(L, "Number of vertex components must be a multiple of two."); int vcount = argc/2; - bool loop = luax_toboolean(L, 1); + bool loop = luax_checkboolean(L, 1); b2Vec2 *vecs = new b2Vec2[vcount]; if (istable) diff --git a/src/modules/physics/box2d/wrap_Body.cpp b/src/modules/physics/box2d/wrap_Body.cpp index b6e342f2e..b68c34085 100644 --- a/src/modules/physics/box2d/wrap_Body.cpp +++ b/src/modules/physics/box2d/wrap_Body.cpp @@ -456,7 +456,7 @@ int w_Body_isBullet(lua_State *L) int w_Body_setBullet(lua_State *L) { Body *t = luax_checkbody(L, 1); - bool b = luax_toboolean(L, 2); + bool b = luax_checkboolean(L, 2); t->setBullet(b); return 0; } @@ -478,7 +478,7 @@ int w_Body_isAwake(lua_State *L) int w_Body_setSleepingAllowed(lua_State *L) { Body *t = luax_checkbody(L, 1); - bool b = luax_toboolean(L, 2); + bool b = luax_checkboolean(L, 2); t->setSleepingAllowed(b); return 0; } @@ -493,7 +493,7 @@ int w_Body_isSleepingAllowed(lua_State *L) int w_Body_setActive(lua_State *L) { Body *t = luax_checkbody(L, 1); - bool b = luax_toboolean(L, 2); + bool b = luax_checkboolean(L, 2); luax_catchexcept(L, [&](){ t->setActive(b); }); return 0; } @@ -501,7 +501,7 @@ int w_Body_setActive(lua_State *L) int w_Body_setAwake(lua_State *L) { Body *t = luax_checkbody(L, 1); - bool b = luax_toboolean(L, 2); + bool b = luax_checkboolean(L, 2); t->setAwake(b); return 0; } @@ -509,7 +509,7 @@ int w_Body_setAwake(lua_State *L) int w_Body_setFixedRotation(lua_State *L) { Body *t = luax_checkbody(L, 1); - bool b = luax_toboolean(L, 2); + bool b = luax_checkboolean(L, 2); luax_catchexcept(L, [&](){ t->setFixedRotation(b); }); return 0; } diff --git a/src/modules/physics/box2d/wrap_Contact.cpp b/src/modules/physics/box2d/wrap_Contact.cpp index 66c006740..7e60bbe4e 100644 --- a/src/modules/physics/box2d/wrap_Contact.cpp +++ b/src/modules/physics/box2d/wrap_Contact.cpp @@ -95,7 +95,7 @@ int w_Contact_setRestitution(lua_State *L) int w_Contact_setEnabled(lua_State *L) { Contact *t = luax_checkcontact(L, 1); - bool e = luax_toboolean(L, 2); + bool e = luax_checkboolean(L, 2); t->setEnabled(e); return 0; } diff --git a/src/modules/physics/box2d/wrap_Fixture.cpp b/src/modules/physics/box2d/wrap_Fixture.cpp index bfa6efbae..da6e5a178 100644 --- a/src/modules/physics/box2d/wrap_Fixture.cpp +++ b/src/modules/physics/box2d/wrap_Fixture.cpp @@ -72,7 +72,7 @@ int w_Fixture_setDensity(lua_State *L) int w_Fixture_setSensor(lua_State *L) { Fixture *t = luax_checkfixture(L, 1); - bool arg1 = luax_toboolean(L, 2); + bool arg1 = luax_checkboolean(L, 2); t->setSensor(arg1); return 0; } diff --git a/src/modules/physics/box2d/wrap_PrismaticJoint.cpp b/src/modules/physics/box2d/wrap_PrismaticJoint.cpp index 2cedebe34..9ea26d352 100644 --- a/src/modules/physics/box2d/wrap_PrismaticJoint.cpp +++ b/src/modules/physics/box2d/wrap_PrismaticJoint.cpp @@ -53,7 +53,7 @@ int w_PrismaticJoint_getJointSpeed(lua_State *L) int w_PrismaticJoint_setMotorEnabled(lua_State *L) { PrismaticJoint *t = luax_checkprismaticjoint(L, 1); - bool arg1 = luax_toboolean(L, 2); + bool arg1 = luax_checkboolean(L, 2); t->setMotorEnabled(arg1); return 0; } @@ -106,7 +106,7 @@ int w_PrismaticJoint_getMaxMotorForce(lua_State *L) int w_PrismaticJoint_setLimitsEnabled(lua_State *L) { PrismaticJoint *t = luax_checkprismaticjoint(L, 1); - bool arg1 = luax_toboolean(L, 2); + bool arg1 = luax_checkboolean(L, 2); t->setLimitsEnabled(arg1); return 0; } diff --git a/src/modules/physics/box2d/wrap_RevoluteJoint.cpp b/src/modules/physics/box2d/wrap_RevoluteJoint.cpp index b59821d2f..62124f8b8 100644 --- a/src/modules/physics/box2d/wrap_RevoluteJoint.cpp +++ b/src/modules/physics/box2d/wrap_RevoluteJoint.cpp @@ -53,7 +53,7 @@ int w_RevoluteJoint_getJointSpeed(lua_State *L) int w_RevoluteJoint_setMotorEnabled(lua_State *L) { RevoluteJoint *t = luax_checkrevolutejoint(L, 1); - bool arg1 = luax_toboolean(L, 2); + bool arg1 = luax_checkboolean(L, 2); t->setMotorEnabled(arg1); return 0; } @@ -106,7 +106,7 @@ int w_RevoluteJoint_getMaxMotorTorque(lua_State *L) int w_RevoluteJoint_setLimitsEnabled(lua_State *L) { RevoluteJoint *t = luax_checkrevolutejoint(L, 1); - bool arg1 = luax_toboolean(L, 2); + bool arg1 = luax_checkboolean(L, 2); t->setLimitsEnabled(arg1); return 0; } diff --git a/src/modules/physics/box2d/wrap_WheelJoint.cpp b/src/modules/physics/box2d/wrap_WheelJoint.cpp index ace9806b7..8d3d921e2 100644 --- a/src/modules/physics/box2d/wrap_WheelJoint.cpp +++ b/src/modules/physics/box2d/wrap_WheelJoint.cpp @@ -52,7 +52,7 @@ int w_WheelJoint_getJointSpeed(lua_State *L) int w_WheelJoint_setMotorEnabled(lua_State *L) { WheelJoint *t = luax_checkwheeljoint(L, 1); - bool arg1 = luax_toboolean(L, 2); + bool arg1 = luax_checkboolean(L, 2); t->setMotorEnabled(arg1); return 0; } diff --git a/src/modules/physics/box2d/wrap_World.cpp b/src/modules/physics/box2d/wrap_World.cpp index ef29143a8..6230ad17e 100644 --- a/src/modules/physics/box2d/wrap_World.cpp +++ b/src/modules/physics/box2d/wrap_World.cpp @@ -111,7 +111,7 @@ int w_World_translateOrigin(lua_State *L) int w_World_setSleepingAllowed(lua_State *L) { World *t = luax_checkworld(L, 1); - bool b = luax_toboolean(L, 2); + bool b = luax_checkboolean(L, 2); t->setSleepingAllowed(b); return 0; } diff --git a/src/modules/window/wrap_Window.cpp b/src/modules/window/wrap_Window.cpp index 843f512d5..8b8692029 100644 --- a/src/modules/window/wrap_Window.cpp +++ b/src/modules/window/wrap_Window.cpp @@ -266,7 +266,7 @@ int w_getFullscreenModes(lua_State *L) int w_setFullscreen(lua_State *L) { - bool fullscreen = luax_toboolean(L, 1); + bool fullscreen = luax_checkboolean(L, 1); Window::FullscreenType fstype = Window::FULLSCREEN_MAX_ENUM; const char *typestr = lua_isnoneornil(L, 2) ? 0 : luaL_checkstring(L, 2); @@ -375,7 +375,7 @@ int w_getIcon(lua_State *L) int w_setDisplaySleepEnabled(lua_State *L) { - instance()->setDisplaySleepEnabled(luax_toboolean(L, 1)); + instance()->setDisplaySleepEnabled(luax_checkboolean(L, 1)); return 0; }