Skip to content

Commit e383e8c

Browse files
committedJul 23, 2014
Fix runtime error checking with lua* commands
1 parent 82ef580 commit e383e8c

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed
 

Diff for: ‎worldedit/code.lua

+26-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
worldedit = worldedit or {}
2-
local minetest = minetest --local copy of global
2+
local minetest = minetest -- local copy of global
33

4-
--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
4+
-- Copies and modifies positions `pos1` and `pos2` so that each component of
5+
-- `pos1` is less than or equal to the corresponding component of `pos2`.
6+
-- Returns the new positions.
57
worldedit.sort_pos = function(pos1, pos2)
68
pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}
79
pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}
@@ -17,30 +19,33 @@ worldedit.sort_pos = function(pos1, pos2)
1719
return pos1, pos2
1820
end
1921

20-
--executes `code` as a Lua chunk in the global namespace, returning an error if the code fails or nil otherwise
22+
-- Executes `code` as a Lua chunk in the global namespace,
23+
-- returning an error if the code fails, or nil otherwise.
2124
worldedit.lua = function(code)
22-
local operation, message = loadstring(code)
23-
if operation == nil then --code parsing failed
24-
return message
25+
local func, err = loadstring(code)
26+
if not func then -- Syntax error
27+
return err
2528
end
26-
local status, message = pcall(operation)
27-
if status == nil then --operation failed
28-
return message
29+
local good, err = pcall(operation)
30+
if not good then -- Runtime error
31+
return err
2932
end
3033
return nil
3134
end
3235

33-
--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
36+
-- Executes `code` as a Lua chunk in the global namespace with the variable
37+
-- pos available, for each node in a region defined by positions `pos1` and
38+
-- `pos2`, returning an error if the code fails, or nil otherwise
3439
worldedit.luatransform = function(pos1, pos2, code)
35-
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
40+
pos1, pos2 = worldedit.sort_pos(pos1, pos2)
3641

37-
local factory, message = loadstring("return function(pos) " .. code .. " end")
38-
if factory == nil then --code parsing failed
39-
return message
42+
local factory, err = loadstring("return function(pos) " .. code .. " end")
43+
if not factory then -- Syntax error
44+
return err
4045
end
41-
local operation = factory()
46+
local func = factory()
4247

43-
--make area stay loaded
48+
-- Keep area loaded
4449
local manip = minetest.get_voxel_manip()
4550
manip:read_from_map(pos1, pos2)
4651

@@ -50,9 +55,9 @@ worldedit.luatransform = function(pos1, pos2, code)
5055
while pos.y <= pos2.y do
5156
pos.z = pos1.z
5257
while pos.z <= pos2.z do
53-
local status, message = pcall(operation, pos)
54-
if status == nil then --operation failed
55-
return message
58+
local good, err = pcall(func, pos)
59+
if not good then -- Runtime error
60+
return err
5661
end
5762
pos.z = pos.z + 1
5863
end
@@ -61,4 +66,5 @@ worldedit.luatransform = function(pos1, pos2, code)
6166
pos.x = pos.x + 1
6267
end
6368
return nil
64-
end
69+
end
70+

Diff for: ‎worldedit/manipulations.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
worldedit = worldedit or {}
22
local minetest = minetest --local copy of global
33

4-
--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
4+
-- Copies and modifies positions `pos1` and `pos2` so that each component of
5+
-- `pos1` is less than or equal to the corresponding component of `pos2`.
6+
-- Returns the new positions.
57
worldedit.sort_pos = function(pos1, pos2)
68
pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}
79
pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}

0 commit comments

Comments
 (0)
Please sign in to comment.