diff options
| author | ruki <[email protected]> | 2022-03-02 23:20:48 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-03-02 23:20:48 +0800 |
| commit | cfdffbc896a2999c594caccfdaa323df16e3950b (patch) | |
| tree | 233d14b1ac1e8c918b0d1e23aa76f87b06e4e69e | |
| parent | 6026401e3bc55e0b5974c0aa18aefe4d7135fe89 (diff) | |
improve table.wrap and unwrap
| -rw-r--r-- | tests/modules/table/test.lua | 30 | ||||
| -rw-r--r-- | xmake/core/base/interpreter.lua | 28 | ||||
| -rw-r--r-- | xmake/core/base/scopeinfo.lua | 18 | ||||
| -rw-r--r-- | xmake/core/base/table.lua | 2 |
4 files changed, 59 insertions, 19 deletions
diff --git a/tests/modules/table/test.lua b/tests/modules/table/test.lua index 426dfe516..156e619f9 100644 --- a/tests/modules/table/test.lua +++ b/tests/modules/table/test.lua @@ -10,18 +10,24 @@ function test_find_if(t) 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({{}}), {{}}) +function test_wrap(t) + t:are_equal(table.wrap(1), {1}) + t:are_equal(table.wrap(nil), {}) + t:are_equal(table.wrap({}), {}) + t:are_equal(table.wrap({1}), {1}) + t:are_equal(table.wrap({{}}), {{}}) + local a = {1} + debug.setmetatable(a, {}) + t:are_equal(table.wrap(a), {a}) 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({{}}), {}) +function test_unwrap(t) + t:are_equal(table.unwrap(1), 1) + t:are_equal(table.unwrap(nil), nil) + t:are_equal(table.unwrap({}), {}) + t:are_equal(table.unwrap({1}), 1) + t:are_equal(table.unwrap({{}}), {}) + local a = {1} + debug.setmetatable(a, {}) + t:are_equal(table.unwrap(a), a) end diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 8a4ed906c..a226d3db2 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -1059,8 +1059,18 @@ function interpreter:api_register_set_values(scope_kind, ...) extra_config = nil end - -- expand values - if not extra_config or extra_config.expand ~= false then + -- @note we need mark table value as meta object to avoid wrap/unwrap + -- if these values cannot be expanded, especially when there is only one value + -- + -- e.g. set_shflags({"-Wl,-exported_symbols_list", exportfile}, {force = true, expand = false}) + 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 @@ -1101,8 +1111,18 @@ function interpreter:api_register_add_values(scope_kind, ...) extra_config = nil end - -- expand values - if not extra_config or extra_config.expand ~= false then + -- @note we need mark table value as meta object to avoid wrap/unwrap + -- if these values cannot be expanded, especially when there is only one value + -- + -- e.g. add_shflags({"-Wl,-exported_symbols_list", exportfile}, {force = true, expand = false}) + 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/scopeinfo.lua b/xmake/core/base/scopeinfo.lua index e3d9a8500..02fb89299 100644 --- a/xmake/core/base/scopeinfo.lua +++ b/xmake/core/base/scopeinfo.lua @@ -113,8 +113,18 @@ function _instance:_api_set_values(name, ...) extra_config = nil end - -- expand values - if not extra_config or extra_config.expand ~= false then + -- @note we need mark table value as meta object to avoid wrap/unwrap + -- if these values cannot be expanded, especially when there is only one value + -- + -- e.g. target:set("shflags", {"-Wl,-exported_symbols_list", exportfile}, {force = true, expand = false}) + 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 @@ -154,6 +164,10 @@ function _instance:_api_add_values(name, ...) extra_config = nil end + -- @note we need mark table value as meta object to avoid wrap/unwrap + -- if these values cannot be expanded, especially when there is only one value + -- + -- e.g. target:add("shflags", {"-Wl,-exported_symbols_list", exportfile}, {force = true, expand = false}) if extra_config and extra_config.expand == false then for _, value in ipairs(values) do if type(value) == "table" then diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua index 1c19f588d..73556c9fc 100644 --- a/xmake/core/base/table.lua +++ b/xmake/core/base/table.lua @@ -310,7 +310,7 @@ end -- unwrap object if be only one function table.unwrap(object) - if type(object) == "table" then + if type(object) == "table" and not getmetatable(object) then if #object == 1 then return object[1] end |
