Browse Source

Functions that take a boolean argument now properly type-check for it.

--HG--
branch : minor
Alex Szpakowski 8 years ago
parent
commit
fee157b6b9

+ 6 - 0
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);

+ 6 - 0
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.

+ 1 - 1
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;
 }
 

+ 2 - 2
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;
 }
 

+ 1 - 1
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;
 }
 

+ 8 - 12
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
 			{

+ 1 - 1
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;
 }

+ 2 - 2
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;
 }
 

+ 1 - 1
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++;
 	}
 

+ 2 - 2
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);

+ 1 - 1
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++;
 	}
 

+ 3 - 3
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;
 }

+ 1 - 1
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)

+ 5 - 5
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;
 }

+ 1 - 1
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;
 }

+ 1 - 1
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;
 }

+ 2 - 2
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;
 }

+ 2 - 2
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;
 }

+ 1 - 1
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;
 }

+ 1 - 1
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;
 }

+ 2 - 2
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;
 }