mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-27 16:49:31 +00:00
Serialize function scope
This commit is contained in:
parent
b004946266
commit
f198286870
4 changed files with 21 additions and 6 deletions
|
|
@ -123,23 +123,26 @@ Function = Overloadable {
|
||||||
|
|
||||||
-- Note: when serializing and reloading a function, its upvalues will not be linked anymore to their original definition.
|
-- Note: when serializing and reloading a function, its upvalues will not be linked anymore to their original definition.
|
||||||
-- The reloaded function will not be able to affect variables defined outside its body.
|
-- The reloaded function will not be able to affect variables defined outside its body.
|
||||||
-- Only the upvalues that explicitely appear in the function body will be saved, so we don't have to keep a copy of the whole environment.
|
-- Only the upvalues that explicitely appear in the function body and variables directly defined in the function scope will be saved, so we don't have to keep a full copy of the whole environment.
|
||||||
-- TODO: we should also store variables that have been defined in the function scope, even if they are not referred directly in the body
|
|
||||||
_serialize = function(self)
|
_serialize = function(self)
|
||||||
return { parameters = self.parameters, expression = self.expression, upvalues = self.upvalues }
|
local state = require("anselme.serializer_state")
|
||||||
|
return { parameters = self.parameters, expression = self.expression, upvalues = self.upvalues, scope = self.scope.variables:to_struct(state) }
|
||||||
end,
|
end,
|
||||||
_deserialize = function(self)
|
_deserialize = function(self)
|
||||||
local state = require("anselme.serializer_state")
|
local state = require("anselme.serializer_state")
|
||||||
local scope
|
local scope
|
||||||
if self.upvalues then
|
if self.upvalues then
|
||||||
-- rebuild scope: exported + normal layer so any upvalue that happen to be exported stay there
|
-- rebuild scope: exported + normal layer so any upvalue that happen to be exported stay there
|
||||||
-- (and link again to current scope to allow internal vars that are not considered explicit upvalues to still work, like _translations)
|
-- (and link again to current scope to allow internal vars that are not serialized to still work, like _translations)
|
||||||
scope = Environment:new(state, Environment:new(state, state.scope:capture(), nil, true))
|
scope = Environment:new(state, Environment:new(state, state.scope:capture(), nil, true))
|
||||||
for _, var in pairs(self.upvalues) do
|
for _, var in pairs(self.upvalues) do
|
||||||
scope:define(state, var:get_symbol(), var:get(state))
|
scope:define(state, var:get_symbol(), var:get(state))
|
||||||
end
|
end
|
||||||
|
for _, var in self.scope:iter() do
|
||||||
|
scope:define(state, var:get_symbol(), var:get(state))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return Function:new(self.parameters, self.expression, Environment:new(state, scope), self.upvalues)
|
return Function:new(self.parameters, self.expression, Environment:new(state, scope), self.upvalues):set_source("saved")
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,6 @@ traverse = {
|
||||||
local Node
|
local Node
|
||||||
Node = class {
|
Node = class {
|
||||||
type = "node",
|
type = "node",
|
||||||
source = "?",
|
|
||||||
mutable = false,
|
mutable = false,
|
||||||
hide_in_stacktrace = false,
|
hide_in_stacktrace = false,
|
||||||
|
|
||||||
|
|
@ -79,6 +78,7 @@ Node = class {
|
||||||
end
|
end
|
||||||
return self
|
return self
|
||||||
end,
|
end,
|
||||||
|
source = "?",
|
||||||
|
|
||||||
-- call function callback with args ... on the children Nodes of this Node
|
-- call function callback with args ... on the children Nodes of this Node
|
||||||
-- by default, assumes no children Nodes
|
-- by default, assumes no children Nodes
|
||||||
|
|
|
||||||
7
test/results/serialize function env.ans
Normal file
7
test/results/serialize function env.ans
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
--# run #--
|
||||||
|
--- text ---
|
||||||
|
| {}"" {}"5" {}"" |
|
||||||
|
--- return ---
|
||||||
|
()
|
||||||
|
--# saved #--
|
||||||
|
{}
|
||||||
5
test/tests/serialize function env.ans
Normal file
5
test/tests/serialize function env.ans
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
:x = $
|
||||||
|
2+z
|
||||||
|
x.:z = 3
|
||||||
|
|
||||||
|
|{x!serialize!deserialize!}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue