Skip to content

Commit

Permalink
Use to-be-closed variables to scope WML variables in tag definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Feb 13, 2021
1 parent 4a43d00 commit 3c44e28
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 17 deletions.
11 changes: 3 additions & 8 deletions data/lua/wml-flow.lua
Expand Up @@ -111,7 +111,7 @@ wesnoth.wml_actions["for"] = function(cfg)
return
end
local i_var = cfg.variable or "i"
local save_i = utils.start_var_scope(i_var)
local save_i <close> = utils.scoped_var(i_var)
wml.variables[i_var] = first
local function loop_condition()
local sentinel = loop_lim.last
Expand Down Expand Up @@ -146,7 +146,6 @@ wesnoth.wml_actions["for"] = function(cfg)
wml.variables[i_var] = wml.variables[i_var] + loop_lim.step
end
::exit::
utils.end_var_scope(i_var, save_i)
end

wml_actions["repeat"] = function(cfg)
Expand Down Expand Up @@ -180,9 +179,9 @@ function wml_actions.foreach(cfg)
local array = wml.array_variables[array_name]
if #array == 0 then return end -- empty and scalars unwanted
local item_name = cfg.variable or "this_item"
local this_item = utils.start_var_scope(item_name) -- if this_item is already set
local this_item <close> = utils.scoped_var(item_name) -- if this_item is already set
local i_name = cfg.index_var or "i"
local i = utils.start_var_scope(i_name) -- if i is already set
local i <close> = utils.scoped_var(i_name) -- if i is already set
local array_length = wml.variables[array_name .. ".length"]

for index, value in ipairs(array) do
Expand Down Expand Up @@ -214,10 +213,6 @@ function wml_actions.foreach(cfg)
end
::exit::

-- house cleaning
utils.end_var_scope(item_name, this_item)
utils.end_var_scope(i_name, i)

--[[
This forces the readonly key to be taken literally.
Expand Down
7 changes: 7 additions & 0 deletions data/lua/wml-utils.lua
Expand Up @@ -182,8 +182,15 @@ function utils.end_var_scope(name, var)
end
end

function utils.scoped_var(name)
utils.start_var_scope(name)
return setmetatable({}, {__close = function() utils.end_var_scope(name) end})
end

utils.trim = wesnoth.deprecate_api('wml_utils.trim', 'stringx.trim', 1, nil, stringx.trim)
utils.parenthetical_split = wesnoth.deprecate_api('wml_utils.parenthetical_split', 'stringx.quoted_split or stringx.split', 1, nil, utils.parenthetical_split)
utils.split = wesnoth.deprecate_api('wml_utils.split', 'stringx.split', 1, nil, utils.split)
utils.start_var_scope = wesnoth.deprecate_api('wml_utils.start_var_scope', 'wml_utils.scoped_var', 1, nil, utils.start_var_scope, 'Assign the scoped_var to a to-be-closed local variable.')
utils.end_var_scope = wesnoth.deprecate_api('wml_utils.end_var_scope', 'wml_utils.scoped_var', 1, nil, utils.end_var_scope, 'Assign the scoped_var to a to-be-closed local variable.')

return utils
3 changes: 1 addition & 2 deletions data/lua/wml/find_path.lua
Expand Up @@ -6,7 +6,7 @@ function wesnoth.wml_actions.find_path(cfg)
local unit = wesnoth.units.find_on_map(filter_unit)[1] or wml.error("[find_path]'s filter didn't match any unit")
local filter_location = wml.get_child(cfg, "destination") or wml.error( "[find_path] missing required [destination] tag" )
-- support for $this_unit
local this_unit = utils.start_var_scope("this_unit")
local this_unit <close> = utils.scoped_var("this_unit")

wml.variables["this_unit"] = nil -- clearing this_unit
wml.variables["this_unit"] = unit.__cfg -- cfg field needed
Expand Down Expand Up @@ -168,5 +168,4 @@ function wesnoth.wml_actions.find_path(cfg)

-- support for $this_unit
wml.variables["this_unit"] = nil -- clearing this_unit
utils.end_var_scope("this_unit", this_unit)
end
3 changes: 1 addition & 2 deletions data/lua/wml/harm_unit.lua
Expand Up @@ -19,7 +19,7 @@ function wml_actions.harm_unit(cfg)
else return false end
end

local this_unit = utils.start_var_scope("this_unit")
local this_unit <close> = utils.scoepd_var("this_unit")

for index, unit_to_harm in ipairs(wesnoth.units.find_on_map(filter)) do
if unit_to_harm.valid then
Expand Down Expand Up @@ -204,5 +204,4 @@ function wml_actions.harm_unit(cfg)
end

wml.variables["this_unit"] = nil -- clearing this_unit
utils.end_var_scope("this_unit", this_unit)
end
6 changes: 2 additions & 4 deletions data/lua/wml/modify_unit.lua
Expand Up @@ -184,12 +184,11 @@ local function simple_modify_unit(cfg)
end
end

local this_unit = utils.start_var_scope("this_unit")
local this_unit <close> = utils.scoped_var("this_unit")
for i, u in ipairs(wesnoth.units.find(filter)) do
wml.variables["this_unit"] = u.__cfg
handle_unit(u)
end
utils.end_var_scope("this_unit", this_unit)
end

function wml_actions.modify_unit(cfg)
Expand Down Expand Up @@ -289,11 +288,10 @@ function wml_actions.modify_unit(cfg)
wml_actions.store_unit { {"filter", filter}, variable = unit_variable }
local max_index = wml.variables[unit_variable .. ".length"] - 1

local this_unit = utils.start_var_scope("this_unit")
local this_unit <close> = utils.scoped_var("this_unit")
for current_unit = 0, max_index do
handle_unit(current_unit)
end
utils.end_var_scope("this_unit", this_unit)

wml.variables[unit_variable] = nil
end
2 changes: 1 addition & 1 deletion data/lua/wml/random_placement.lua
Expand Up @@ -9,7 +9,7 @@ wesnoth.wml_actions.random_placement = function(cfg)
local num_items = cfg.num_items or wml.error("[random_placement] missing required 'num_items' attribute")
local variable = cfg.variable or wml.error("[random_placement] missing required 'variable' attribute")
local allow_less = cfg.allow_less == true
local variable_previous = utils.start_var_scope(variable)
local variable_previous <close> = utils.scoped_var(variable)
local math_abs = math.abs
local locs = wesnoth.get_locations(filter)
if type(num_items) == "string" then
Expand Down

0 comments on commit 3c44e28

Please sign in to comment.