From 7db2997bc3cec68a78c7884c558d14992e05ee16 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Tue, 26 Jan 2016 22:19:35 -0400 Subject: [PATCH] Improved the error messages caused when invalid or missing arguments are given to ImageData and SoundData methods. --- src/modules/image/wrap_ImageData.lua | 27 +++++++++++++++++++-------- src/modules/sound/wrap_SoundData.lua | 5 +++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/modules/image/wrap_ImageData.lua b/src/modules/image/wrap_ImageData.lua index 21ecafb82..22fbc451b 100644 --- a/src/modules/image/wrap_ImageData.lua +++ b/src/modules/image/wrap_ImageData.lua @@ -42,12 +42,12 @@ function ImageData:mapPixel(func, ix, iy, iw, ih) iw = iw or idw ih = ih or idh - if type(ix) ~= "number" then error("Invalid argument #2 to ImageData:mapPixel (expected number)", 2) end - if type(iy) ~= "number" then error("Invalid argument #3 to ImageData:mapPixel (expected number)", 2) end - if type(iw) ~= "number" then error("Invalid argument #4 to ImageData:mapPixel (expected number)", 2) end - if type(ih) ~= "number" then error("Invalid argument #5 to ImageData:mapPixel (expected number)", 2) end + if type(ix) ~= "number" then error("bad argument #2 to ImageData:mapPixel (expected number)", 2) end + if type(iy) ~= "number" then error("bad argument #3 to ImageData:mapPixel (expected number)", 2) end + if type(iw) ~= "number" then error("bad argument #4 to ImageData:mapPixel (expected number)", 2) end + if type(ih) ~= "number" then error("bad argument #5 to ImageData:mapPixel (expected number)", 2) end - if type(func) ~= "function" then error("Invalid argument #1 to ImageData:mapPixel (expected function)", 2) end + if type(func) ~= "function" then error("bad argument #1 to ImageData:mapPixel (expected function)", 2) end if not (inside(ix, iy, idw, idh) and inside(ix+iw-1, iy+ih-1, idw, idh)) then error("Invalid rectangle dimensions", 2) end -- performAtomic and mapPixelUnsafe have Lua-C API and FFI versions. @@ -141,6 +141,9 @@ function ImageData:_mapPixelUnsafe(func, ix, iy, iw, ih) end function ImageData:getPixel(x, y) + if type(x) ~= "number" then error("bad argument #1 to ImageData:getPixel (expected number)", 2) end + if type(y) ~= "number" then error("bad argument #2 to ImageData:getPixel (expected number)", 2) end + local p = objectcache[self] if not inside(x, y, p.width, p.height) then error("Attempt to get out-of-range pixel!", 2) end @@ -154,15 +157,23 @@ end local temppixel = ffi.new("ImageData_Pixel") -function ImageData:setPixel(x, y, r, g, b, a) - local p = objectcache[self] - if not inside(x, y, p.width, p.height) then error("Attempt to set out-of-range pixel!", 2) end +function ImageData:setPixel(x, y, r, g, b, a) + if type(x) ~= "number" then error("bad argument #1 to ImageData:setPixel (expected number)", 2) end + if type(y) ~= "number" then error("bad argument #2 to ImageData:setPixel (expected number)", 2) end if type(r) == "table" then local t = r r, g, b, a = t[1], t[2], t[3], t[4] end + if type(r) ~= "number" then error("bad red color component argument to ImageData:setPixel (expected number)", 2) end + if type(g) ~= "number" then error("bad green color component argument to ImageData:setPixel (expected number)", 2) end + if type(b) ~= "number" then error("bad blue color component argument to ImageData:setPixel (expected number)", 2) end + if a ~= nil and type(a) ~= "number" then error("bad alpha color component argument to ImageData:setPixel (expected number)", 2) end + + local p = objectcache[self] + if not inside(x, y, p.width, p.height) then error("Attempt to set out-of-range pixel!", 2) end + temppixel.r = r temppixel.g = g temppixel.b = b diff --git a/src/modules/sound/wrap_SoundData.lua b/src/modules/sound/wrap_SoundData.lua index 0a9d0500b..803a9a2d1 100644 --- a/src/modules/sound/wrap_SoundData.lua +++ b/src/modules/sound/wrap_SoundData.lua @@ -75,6 +75,8 @@ local objectcache = setmetatable({}, { -- Overwrite existing functions with new FFI versions. function SoundData:getSample(i) + if type(i) ~= "number" then error("bad argument #1 to SoundData:getSample (expected number)", 2) end + local p = objectcache[self] if not (i >= 0 and i < p.size/p.bytedepth) then @@ -91,6 +93,9 @@ function SoundData:getSample(i) end function SoundData:setSample(i, sample) + if type(i) ~= "number" then error("bad argument #1 to SoundData:setSample (expected number)", 2) end + if type(sample) ~= "number" then error("bad argument #2 to SoundData:setSample (expected number)", 2) end + local p = objectcache[self] if not (i >= 0 and i < p.size/p.bytedepth) then