]> git.lizzy.rs Git - worldedit.git/commitdiff
Fix runtime error checking with lua* commands
authorShadowNinja <shadowninja@minetest.net>
Sun, 20 Jul 2014 17:42:57 +0000 (13:42 -0400)
committerShadowNinja <shadowninja@minetest.net>
Wed, 23 Jul 2014 22:21:21 +0000 (18:21 -0400)
worldedit/code.lua
worldedit/manipulations.lua

index fb04ce9bb9c6e615f4207ef642443bd9ef5ea3a7..dc582995ae06587fc7161d9466f79439a18decb2 100644 (file)
@@ -1,7 +1,9 @@
 worldedit = worldedit or {}\r
-local minetest = minetest --local copy of global\r
+local minetest = minetest -- local copy of global\r
 \r
---modifies positions `pos1` and `pos2` so that each component of `pos1` is less than or equal to its corresponding conent of `pos2`, returning two new positions\r
+-- Copies and modifies positions `pos1` and `pos2` so that each component of\r
+-- `pos1` is less than or equal to the corresponding component of `pos2`.\r
+-- Returns the new positions.\r
 worldedit.sort_pos = function(pos1, pos2)\r
        pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}\r
        pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}\r
@@ -17,30 +19,33 @@ worldedit.sort_pos = function(pos1, pos2)
        return pos1, pos2\r
 end\r
 \r
---executes `code` as a Lua chunk in the global namespace, returning an error if the code fails or nil otherwise\r
+-- Executes `code` as a Lua chunk in the global namespace,\r
+-- returning an error if the code fails, or nil otherwise.\r
 worldedit.lua = function(code)\r
-       local operation, message = loadstring(code)\r
-       if operation == nil then --code parsing failed\r
-               return message\r
+       local func, err = loadstring(code)\r
+       if not func then  -- Syntax error\r
+               return err\r
        end\r
-       local status, message = pcall(operation)\r
-       if status == nil then --operation failed\r
-               return message\r
+       local good, err = pcall(operation)\r
+       if not good then  -- Runtime error\r
+               return err\r
        end\r
        return nil\r
 end\r
 \r
---executes `code` as a Lua chunk in the global namespace with the variable pos available, for each node in a region defined by positions `pos1` and `pos2`, returning an error if the code fails or nil otherwise\r
+-- Executes `code` as a Lua chunk in the global namespace with the variable\r
+-- pos available, for each node in a region defined by positions `pos1` and\r
+-- `pos2`, returning an error if the code fails, or nil otherwise\r
 worldedit.luatransform = function(pos1, pos2, code)\r
-       local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
+       pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
 \r
-       local factory, message = loadstring("return function(pos) " .. code .. " end")\r
-       if factory == nil then --code parsing failed\r
-               return message\r
+       local factory, err = loadstring("return function(pos) " .. code .. " end")\r
+       if not factory then  -- Syntax error\r
+               return err\r
        end\r
-       local operation = factory()\r
+       local func = factory()\r
 \r
-       --make area stay loaded\r
+       -- Keep area loaded\r
        local manip = minetest.get_voxel_manip()\r
        manip:read_from_map(pos1, pos2)\r
 \r
@@ -50,9 +55,9 @@ worldedit.luatransform = function(pos1, pos2, code)
                while pos.y <= pos2.y do\r
                        pos.z = pos1.z\r
                        while pos.z <= pos2.z do\r
-                               local status, message = pcall(operation, pos)\r
-                               if status == nil then --operation failed\r
-                                       return message\r
+                               local good, err = pcall(func, pos)\r
+                               if not good then -- Runtime error\r
+                                       return err\r
                                end\r
                                pos.z = pos.z + 1\r
                        end\r
@@ -61,4 +66,5 @@ worldedit.luatransform = function(pos1, pos2, code)
                pos.x = pos.x + 1\r
        end\r
        return nil\r
-end
\ No newline at end of file
+end\r
+\r
index 2e3e3696a3fbf5bbbc6e9486df2a7e16bd354f1e..71eef5deb8cd3d0a1df11c8698b356fe1e618d86 100644 (file)
@@ -1,7 +1,9 @@
 worldedit = worldedit or {}\r
 local minetest = minetest --local copy of global\r
 \r
---modifies positions `pos1` and `pos2` so that each component of `pos1` is less than or equal to its corresponding conent of `pos2`, returning two new positions\r
+-- Copies and modifies positions `pos1` and `pos2` so that each component of\r
+-- `pos1` is less than or equal to the corresponding component of `pos2`.\r
+-- Returns the new positions.\r
 worldedit.sort_pos = function(pos1, pos2)\r
        pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}\r
        pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}\r