mirror of
https://github.com/nmap/nmap.git
synced 2026-08-04 14:49:29 +00:00
Modify stdnse.output_table to handle empty values better
Two changes here, both minor. First, explicitly assigning a new key to nil does not add the key to the ordered set of keys. This better emulates the behavior of regular tables. > o = stdnse.output_table() > o["test"] = nil This previously resulted in output like this: |_ test: nil Now it simply omits the "test:" key. Second, I needed a way to tell whether an output table was empty or not. Since Lua's next() function doesn't call the __pairs metamethod, it was always returning nil. Instead, I used the __call metamethod, since it had the least preexisting semantic meaning: > o = stdnse.output_table() > =o() false > o["test"] = 1 > =o() true
This commit is contained in:
parent
cb32101ae5
commit
930bc91359
1 changed files with 5 additions and 1 deletions
|
|
@ -18,6 +18,7 @@ local error = error;
|
|||
local getmetatable = getmetatable;
|
||||
local ipairs = ipairs
|
||||
local pairs = pairs
|
||||
local next = next
|
||||
local rawset = rawset
|
||||
local require = require;
|
||||
local select = select
|
||||
|
|
@ -1131,7 +1132,7 @@ function output_table ()
|
|||
end
|
||||
local mt = {
|
||||
__newindex = function (_, k, v)
|
||||
if t[k] == nil then
|
||||
if t[k] == nil and v ~= nil then
|
||||
-- New key?
|
||||
table.insert(order, k)
|
||||
elseif v == nil then
|
||||
|
|
@ -1151,6 +1152,9 @@ function output_table ()
|
|||
__pairs = function (_)
|
||||
return coroutine.wrap(iterator)
|
||||
end,
|
||||
__call = function (_) -- hack to mean "not_empty?"
|
||||
return not not next(order)
|
||||
end
|
||||
}
|
||||
return setmetatable({}, mt)
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue