diff options
| author | ruki <[email protected]> | 2022-03-02 23:29:35 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-03-02 23:29:35 +0800 |
| commit | f6a48a7481bffa3f8b97ac9cd509eb7f4c202559 (patch) | |
| tree | 77e3c441cb5d344e6037300a35ece91fa0c5f6a0 | |
| parent | aeb97fe6d7d7385fddb494dc4f4a892993e03876 (diff) | |
add wraplock
| -rw-r--r-- | tests/modules/table/test.lua | 4 | ||||
| -rw-r--r-- | xmake/core/base/interpreter.lua | 8 | ||||
| -rw-r--r-- | xmake/core/base/scopeinfo.lua | 8 | ||||
| -rw-r--r-- | xmake/core/base/table.lua | 34 |
4 files changed, 28 insertions, 26 deletions
diff --git a/tests/modules/table/test.lua b/tests/modules/table/test.lua index 5133e778f..569f18e50 100644 --- a/tests/modules/table/test.lua +++ b/tests/modules/table/test.lua @@ -16,7 +16,7 @@ function test_wrap(t) t:are_equal(table.wrap({}), {}) t:are_equal(table.wrap({1}), {1}) t:are_equal(table.wrap({{}}), {{}}) - local a = {1, __object__ = true} + local a = table.wraplock({1}) t:are_equal(table.wrap({a}), {a}) end @@ -26,6 +26,6 @@ function test_unwrap(t) t:are_equal(table.unwrap({}), {}) t:are_equal(table.unwrap({1}), 1) t:are_equal(table.unwrap({{}}), {}) - local a = {1, __object__ = true} + local a = table.wraplock({1}) t:are_equal(table.unwrap(a), a) end diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 50d7f3149..07aa701dc 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -1065,9 +1065,7 @@ function interpreter:api_register_set_values(scope_kind, ...) -- 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 - value.__object__ = true - end + table.wraplock(value) end else -- expand values @@ -1117,9 +1115,7 @@ function interpreter:api_register_add_values(scope_kind, ...) -- 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 - value.__object__ = true - end + table.wraplock(value) end else -- expand values diff --git a/xmake/core/base/scopeinfo.lua b/xmake/core/base/scopeinfo.lua index 4e0d5a73c..b826ac648 100644 --- a/xmake/core/base/scopeinfo.lua +++ b/xmake/core/base/scopeinfo.lua @@ -119,9 +119,7 @@ function _instance:_api_set_values(name, ...) -- 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 - value.__object__ = true - end + table.wraplock(value) end else -- expand values @@ -170,9 +168,7 @@ function _instance:_api_add_values(name, ...) -- 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 - value.__object__ = true - end + table.wraplock(value) end else -- expand values diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua index 232f6962a..6c2b3aaf4 100644 --- a/xmake/core/base/table.lua +++ b/xmake/core/base/table.lua @@ -308,25 +308,35 @@ function table.to_array(iterator, state, var) return result, count end --- unwrap object if be only one -function table.unwrap(object) - if type(object) == "table" and not object.__object__ then - if #object == 1 then - return object[1] +-- unwrap array if be only one value +function table.unwrap(array) + if type(array) == "table" and not array.__wraplocked__ then + if #array == 1 then + return array[1] end end - return object + return array end --- wrap object to table -function table.wrap(object) - if nil == object then +-- wrap value to array +function table.wrap(value) + if nil == value then return {} end - if type(object) ~= "table" or object.__object__ then - return {object} + if type(value) ~= "table" or value.__wraplocked__ then + return {value} + end + return value +end + +-- lock table value to avoid unwrap +-- +-- a = {1}, wrap(a): {1}, unwrap(a): 1 +-- a = wraplock({1}), wrap(a): {a}, unwrap(a): a +function table.wraplock(value) + if type(value) == "table" then + value.__wraplocked__ = true end - return object end -- remove repeat from the given array |
