diff options
| author | ruki <[email protected]> | 2022-03-02 23:18:40 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-03-02 23:18:40 +0800 |
| commit | 6026401e3bc55e0b5974c0aa18aefe4d7135fe89 (patch) | |
| tree | 435735b6009ece189bda720db95ceda0f69cb928 | |
| parent | 0654d3ea2b7a9063adadd6b3a2c5fe2b0ff7883f (diff) | |
improve table.wrap
| -rw-r--r-- | tests/modules/table/test.lua | 16 | ||||
| -rw-r--r-- | xmake/core/base/scopeinfo.lua | 10 | ||||
| -rw-r--r-- | xmake/core/base/table.lua | 10 |
3 files changed, 27 insertions, 9 deletions
diff --git a/tests/modules/table/test.lua b/tests/modules/table/test.lua index 5ed17620c..426dfe516 100644 --- a/tests/modules/table/test.lua +++ b/tests/modules/table/test.lua @@ -9,3 +9,19 @@ function test_find_if(t) t:are_equal(table.find({1, 2, 4, 4, 5, 6}, 4), {3, 4}) t:are_equal(table.find_first({1, 2, 3, 4, 5, 6}, 4), 4) end + +function test_wrap() + t:are_equal(talbe.wrap(1), {1}) + t:are_equal(talbe.wrap(nil), {}) + t:are_equal(talbe.wrap({}), {}) + t:are_equal(talbe.wrap({1}), {1}) + t:are_equal(talbe.wrap({{}}), {{}}) +end + +function test_unwrap() + t:are_equal(talbe.unwrap(1), 1) + t:are_equal(talbe.unwrap(nil), nil) + t:are_equal(talbe.unwrap({}), nil) + t:are_equal(talbe.unwrap({1}), 1) + t:are_equal(talbe.unwrap({{}}), {}) +end diff --git a/xmake/core/base/scopeinfo.lua b/xmake/core/base/scopeinfo.lua index 8a69a9299..e3d9a8500 100644 --- a/xmake/core/base/scopeinfo.lua +++ b/xmake/core/base/scopeinfo.lua @@ -154,8 +154,14 @@ function _instance:_api_add_values(name, ...) extra_config = nil end - -- expand values - if not extra_config or extra_config.expand ~= false then + if extra_config and extra_config.expand == false then + for _, value in ipairs(values) do + if type(value) == "table" then + setmetatable(value, {}) + end + end + else + -- expand values values = table.join(table.unpack(values)) end diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua index 2cf617f3b..1c19f588d 100644 --- a/xmake/core/base/table.lua +++ b/xmake/core/base/table.lua @@ -165,13 +165,9 @@ end -- copy the table to self function table.copy2(self, copied) - - -- clear self first + assert(type(copied) == "table") table.clear(self) - - -- copy it - copied = copied or {} - for k, v in pairs(table.wrap(copied)) do + for k, v in pairs(copied) do self[k] = v end end @@ -327,7 +323,7 @@ function table.wrap(object) if nil == object then return {} end - if type(object) ~= "table" then + if type(object) ~= "table" or getmetatable(object) then -- maybe object, we need wrap it too return {object} end return object |
