summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2019-07-26 16:25:52 +0800
committerOpportunityLiu <[email protected]>2019-07-28 14:45:01 +0800
commitd3667d71c86861ffb910af8ba8ecd930f75bf9e7 (patch)
tree5a0014b413aae30fc60d2d084aac98c9575eddf7
parentcff42b3aeb348d0a193c24f244ddec003aded06f (diff)
fix up value
-rw-r--r--tests/modules/string/serialize/test.lua3
-rw-r--r--tests/test_utils/test_assert.lua2
-rw-r--r--xmake/core/base/serialize.lua20
3 files changed, 15 insertions, 10 deletions
diff --git a/tests/modules/string/serialize/test.lua b/tests/modules/string/serialize/test.lua
index ab8986867..66b216566 100644
--- a/tests/modules/string/serialize/test.lua
+++ b/tests/modules/string/serialize/test.lua
@@ -42,6 +42,9 @@ function test_function(t)
t:are_equal(roundtrip(function() return {{1, 2, 3, nil, 4}} end)(), {{1, 2, 3, nil, 4}})
t:are_equal(roundtrip({function() return {{1, 2, 3, nil, 4}} end})[1](), {{1, 2, 3, nil, 4}})
t:are_equal(roundtrip({{function() return {{1, 2, 3, nil, 4}} end}})[1][1](), {{1, 2, 3, nil, 4}})
+
+ t:will_raise(function() string.serialize({io.open}):deserialize()[1]("test.lua") end, "attempt to call global 'setmetatable' (a nil value)")
+ t:will_raise(function() string.serialize({io.open}):deserialize()[1]("not/a/file") end, "failed to open file: not/a/file")
end
function test_refloop(t)
diff --git a/tests/test_utils/test_assert.lua b/tests/test_utils/test_assert.lua
index a964fcfb3..1cf0e9386 100644
--- a/tests/test_utils/test_assert.lua
+++ b/tests/test_utils/test_assert.lua
@@ -57,7 +57,7 @@ function test_assert:will_raise(func, message_pattern)
local funcs = { func, unpack(self._will_raise_stack) }
if ok then
self:print_error("expected raise but finished successfully", funcs)
- elseif message_pattern and not string.find(error, message_pattern) then
+ elseif message_pattern and not error:find(message_pattern, 1, true) and not error:find(message_pattern) then
self:print_error(format("expected raise with message ${green}%s${reset} but got ${red}%s${reset}", message_pattern, error), funcs)
end
end
diff --git a/xmake/core/base/serialize.lua b/xmake/core/base/serialize.lua
index 6e652384a..f5e078da0 100644
--- a/xmake/core/base/serialize.lua
+++ b/xmake/core/base/serialize.lua
@@ -50,7 +50,7 @@ function serialize._resolvestub(object, root, fenv)
end
for k, v in pairs(object) do
- local result, errors = serialize._resolvestub(v, root, env)
+ local result, errors = serialize._resolvestub(v, root, fenv)
if errors ~= nil then
return nil, errors
end
@@ -179,25 +179,27 @@ function serialize._makefunction(func, opt)
return string.format("func%q", funccode)
end
-function serialize._resolvefunction(root, env, funccode)
+function serialize._resolvefunction(root, fenv, funccode)
-- check
if type(funccode) ~= "string" then
return nil, "func should called with a string"
end
-- resolve funccode
- local func, err = load(funccode, "=(deserialized code)", "b", env)
+ local func, err = load(funccode, "=(deserialized code)", "b", fenv)
if err ~= nil then
return nil, err
end
-- try restore upvalues
- for i = 1, math.huge do
- local upname = debug.getupvalue(func, i)
- if upname == nil or upname == "" then
- break
+ if fenv then
+ for i = 1, math.huge do
+ local upname = debug.getupvalue(func, i)
+ if upname == nil or upname == "" then
+ break
+ end
+ debug.setupvalue(func, i, fenv[upname])
end
- debug.setupvalue(func, i, env[upname])
end
return func
end
@@ -217,7 +219,7 @@ function serialize._makeref(path, opt)
return "ref(" .. table.concat(ppath, opt.indent and ", " or ",") .. ")"
end
-function serialize._resolveref(root, env, ...)
+function serialize._resolveref(root, fenv, ...)
local pos = root
for i, v in ipairs({...}) do
if type(v) ~= "string" and type(v) ~= "number" then