diff --git a/src/modules/image/wrap_ImageData.lua b/src/modules/image/wrap_ImageData.lua index 31a3a06d6..56c4a01e2 100644 --- a/src/modules/image/wrap_ImageData.lua +++ b/src/modules/image/wrap_ImageData.lua @@ -23,8 +23,9 @@ misrepresented as being the original software. --]] local ImageData_mt, ffifuncspointer = ... +local ImageData = ImageData_mt.__index -local tonumber, assert = tonumber, assert +local tonumber, assert, error = tonumber, assert, error local type, pcall = type, pcall local function inside(x, y, w, h) @@ -33,7 +34,7 @@ end -- Implement thread-safe ImageData:mapPixel regardless of whether the FFI is -- used or not. -function ImageData_mt.__index:mapPixel(func, ix, iy, iw, ih) +function ImageData:mapPixel(func, ix, iy, iw, ih) local idw, idh = self:getDimensions() ix = ix or 0 @@ -41,13 +42,13 @@ function ImageData_mt.__index:mapPixel(func, ix, iy, iw, ih) iw = iw or idw ih = ih or idh - assert(type(ix) == "number", "Invalid argument #2 to ImageData:mapPixel (expected number)") - assert(type(iy) == "number", "Invalid argument #3 to ImageData:mapPixel (expected number)") - assert(type(iw) == "number", "Invalid argument #4 to ImageData:mapPixel (expected number)") - assert(type(ih) == "number", "Invalid argument #5 to ImageData:mapPixel (expected number)") + 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 - assert(type(func) == "function", "Invalid argument #1 to ImageData:mapPixel (expected function)") - assert(inside(ix, iy, idw, idh) and inside(ix+iw-1, iy+ih-1, idw, idh), "Invalid rectangle dimensions") + if type(func) ~= "function" then error("Invalid 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. self:_performAtomic(self._mapPixelUnsafe, self, func, ix, iy, iw, ih) @@ -85,9 +86,9 @@ local ffifuncs = ffi.cast("FFI_ImageData *", ffifuncspointer) local pixelpointer = ffi.typeof("ImageData_Pixel *") -local _getWidth = ImageData_mt.__index.getWidth -local _getHeight = ImageData_mt.__index.getHeight -local _getDimensions = ImageData_mt.__index.getDimensions +local _getWidth = ImageData.getWidth +local _getHeight = ImageData.getHeight +local _getDimensions = ImageData.getDimensions -- Table which holds ImageData objects as keys, and information about the objects -- as values. Uses weak keys so the ImageData objects can still be GC'd properly. @@ -111,7 +112,7 @@ local objectcache = setmetatable({}, { -- Overwrite existing functions with new FFI versions. -function ImageData_mt.__index:_performAtomic(...) +function ImageData:_performAtomic(...) ffifuncs.lockMutex(self) local success, err = pcall(...) ffifuncs.unlockMutex(self) @@ -121,7 +122,7 @@ function ImageData_mt.__index:_performAtomic(...) end end -function ImageData_mt.__index:_mapPixelUnsafe(func, ix, iy, iw, ih) +function ImageData:_mapPixelUnsafe(func, ix, iy, iw, ih) local p = objectcache[self] local idw, idh = p.width, p.height @@ -139,9 +140,9 @@ function ImageData_mt.__index:_mapPixelUnsafe(func, ix, iy, iw, ih) end end -function ImageData_mt.__index:getPixel(x, y) +function ImageData:getPixel(x, y) local p = objectcache[self] - assert(inside(x, y, p.width, p.height), "Attempt to get out-of-range pixel!") + if not inside(x, y, p.width, p.height) then error("Attempt to get out-of-range pixel!", 2) end ffifuncs.lockMutex(self) local pixel = p.pointer[y * p.width + x] @@ -153,9 +154,9 @@ end local temppixel = ffi.new("ImageData_Pixel") -function ImageData_mt.__index:setPixel(x, y, r, g, b, a) +function ImageData:setPixel(x, y, r, g, b, a) local p = objectcache[self] - assert(inside(x, y, p.width, p.height), "Attempt to set out-of-range pixel!") + if not inside(x, y, p.width, p.height) then error("Attempt to set out-of-range pixel!", 2) end if type(r) == "table" then local t = r @@ -172,15 +173,15 @@ function ImageData_mt.__index:setPixel(x, y, r, g, b, a) ffifuncs.unlockMutex(self) end -function ImageData_mt.__index:getWidth() +function ImageData:getWidth() return objectcache[self].width end -function ImageData_mt.__index:getHeight() +function ImageData:getHeight() return objectcache[self].height end -function ImageData_mt.__index:getDimensions() +function ImageData:getDimensions() local p = objectcache[self] return p.width, p.height end diff --git a/src/modules/math/wrap_Math.cpp b/src/modules/math/wrap_Math.cpp index 34381d0fe..ffe1a7590 100644 --- a/src/modules/math/wrap_Math.cpp +++ b/src/modules/math/wrap_Math.cpp @@ -318,33 +318,27 @@ int w_linearToGamma(lua_State *L) int w_noise(lua_State *L) { - float w, x, y, z; - float val; + int nargs = std::min(std::max(lua_gettop(L), 1), 4); + float args[4]; - switch (lua_gettop(L)) + for (int i = 0; i < nargs; i++) + args[i] = (float) luaL_checknumber(L, i + 1); + + float val = 0.0f; + + switch (nargs) { case 1: - x = (float) luaL_checknumber(L, 1); - val = Math::instance.noise(x); + val = Math::instance.noise(args[0]); break; case 2: - x = (float) luaL_checknumber(L, 1); - y = (float) luaL_checknumber(L, 2); - val = Math::instance.noise(x, y); + val = Math::instance.noise(args[0], args[1]); break; case 3: - x = (float) luaL_checknumber(L, 1); - y = (float) luaL_checknumber(L, 2); - z = (float) luaL_checknumber(L, 3); - val = Math::instance.noise(x, y, z); + val = Math::instance.noise(args[0], args[1], args[2]); break; case 4: - default: - x = (float) luaL_checknumber(L, 1); - y = (float) luaL_checknumber(L, 2); - z = (float) luaL_checknumber(L, 3); - w = (float) luaL_checknumber(L, 4); - val = Math::instance.noise(x, y, z, w); + val = Math::instance.noise(args[0], args[1], args[2], args[3]); break; } diff --git a/src/modules/sound/wrap_SoundData.lua b/src/modules/sound/wrap_SoundData.lua index 5f7991504..c71d60e4e 100644 --- a/src/modules/sound/wrap_SoundData.lua +++ b/src/modules/sound/wrap_SoundData.lua @@ -23,6 +23,7 @@ misrepresented as being the original software. --]] local SoundData_mt = ... +local SoundData = SoundData_mt.__index if type(jit) ~= "table" or not jit.status() then -- LuaJIT's FFI is *much* slower than LOVE's regular methods when the JIT @@ -33,18 +34,18 @@ end local status, ffi = pcall(require, "ffi") if not status then return end -local tonumber, assert = tonumber, assert +local tonumber, assert, error = tonumber, assert, error local float = ffi.typeof("float") local datatypes = {ffi.typeof("uint8_t *"), ffi.typeof("int16_t *")} local typemaxvals = {0x7F, 0x7FFF} -local _getBitDepth = SoundData_mt.__index.getBitDepth -local _getSampleCount = SoundData_mt.__index.getSampleCount -local _getSampleRate = SoundData_mt.__index.getSampleRate -local _getChannels = SoundData_mt.__index.getChannels -local _getDuration = SoundData_mt.__index.getDuration +local _getBitDepth = SoundData.getBitDepth +local _getSampleCount = SoundData.getSampleCount +local _getSampleRate = SoundData.getSampleRate +local _getChannels = SoundData.getChannels +local _getDuration = SoundData.getDuration -- Table which holds SoundData objects as keys, and information about the objects -- as values. Uses weak keys so the SoundData objects can still be GC'd properly. @@ -73,9 +74,13 @@ local objectcache = setmetatable({}, { -- Overwrite existing functions with new FFI versions. -function SoundData_mt.__index:getSample(i) +function SoundData:getSample(i) local p = objectcache[self] - assert(i >= 0 and i < p.size/p.bytedepth, "Attempt to get out-of-range sample!") + + if not (i >= 0 and i < p.size/p.bytedepth) then + error("Attempt to get out-of-range sample!", 2) + end + if p.bytedepth == 2 then -- 16-bit data is stored as signed values internally. return tonumber(p.pointer[i]) / p.maxvalue @@ -85,9 +90,13 @@ function SoundData_mt.__index:getSample(i) end end -function SoundData_mt.__index:setSample(i, sample) +function SoundData:setSample(i, sample) local p = objectcache[self] - assert(i >= 0 and i < p.size/p.bytedepth, "Attempt to set out-of-range sample!") + + if not (i >= 0 and i < p.size/p.bytedepth) then + error("Attempt to set out-of-range sample!", 2) + end + if p.bytedepth == 2 then -- 16-bit data is stored as signed values internally. p.pointer[i] = sample * p.maxvalue @@ -99,23 +108,23 @@ function SoundData_mt.__index:setSample(i, sample) end end -function SoundData_mt.__index:getBitDepth() +function SoundData:getBitDepth() return objectcache[self].bytedepth * 8 end -function SoundData_mt.__index:getSampleCount() +function SoundData:getSampleCount() return objectcache[self].samplecount end -function SoundData_mt.__index:getSampleRate() +function SoundData:getSampleRate() return objectcache[self].samplerate end -function SoundData_mt.__index:getChannels() +function SoundData:getChannels() return objectcache[self].channels end -function SoundData_mt.__index:getDuration() +function SoundData:getDuration() return objectcache[self].duration end