diff options
author | Sam Gross <colesbury@gmail.com> | 2015-04-30 22:00:56 +0300 |
---|---|---|
committer | Sam Gross <colesbury@gmail.com> | 2015-06-01 17:14:07 +0300 |
commit | 9cf1907b8d74a102fb7456f8ef9103a7ac1f7582 (patch) | |
tree | 862157b4cf7bd843bd684ba2e4fb815677fbc6f6 /File.lua | |
parent | 18fb209ffdf1e42a73a745617bcbf76f0a6136c3 (diff) |
Fix serialization of closures in Lua 5.2
This fixes test_can_write_a_nil_closure in Lua 5.2.
Added a test case that affected Lua 5.1 and Lua 5.2.
Support serialization of closures that reference the environment.
Diffstat (limited to 'File.lua')
-rw-r--r-- | File.lua | 34 |
1 files changed, 21 insertions, 13 deletions
@@ -19,7 +19,8 @@ local TYPE_TABLE = 3 local TYPE_TORCH = 4 local TYPE_BOOLEAN = 5 local TYPE_FUNCTION = 6 -local TYPE_RECUR_FUNCTION = 7 +local TYPE_RECUR_FUNCTION = 8 +local LEGACY_TYPE_RECUR_FUNCTION = 7 -- Lua 5.2 compatibility local loadstring = loadstring or load @@ -141,7 +142,8 @@ function File:writeObject(object) counter = counter + 1 local name,value = debug.getupvalue(object, counter) if not name then break end - table.insert(upvalues, value) + if name == '_ENV' then value = nil end + table.insert(upvalues, {name=name, value=value}) end local dumped = string.dump(object) local stringStorage = torch.CharStorage():string(dumped) @@ -217,7 +219,7 @@ function File:readObject() debug.setupvalue(func, index, upvalue) end return func - elseif typeidx == TYPE_TABLE or typeidx == TYPE_TORCH or typeidx == TYPE_RECUR_FUNCTION then + elseif typeidx == TYPE_TABLE or typeidx == TYPE_TORCH or typeidx == TYPE_RECUR_FUNCTION or typeidx == LEGACY_TYPE_RECUR_FUNCTION then -- read the index local index = self:readInt() @@ -228,16 +230,22 @@ function File:readObject() end -- otherwise read it - if typeidx == TYPE_RECUR_FUNCTION then - local size = self:readInt() - local dumped = self:readChar(size):string() - local func = loadstring(dumped) - objects[index] = func - local upvalues = self:readObject() - for index,upvalue in ipairs(upvalues) do - debug.setupvalue(func, index, upvalue) - end - return func + if typeidx == TYPE_RECUR_FUNCTION or typeidx == LEGACY_TYPE_RECUR_FUNCTION then + local size = self:readInt() + local dumped = self:readChar(size):string() + local func = loadstring(dumped) + objects[index] = func + local upvalues = self:readObject() + for index,upvalue in ipairs(upvalues) do + if typeidx == LEGACY_TYPE_RECUR_FUNCTION then + debug.setupvalue(func, index, upvalue) + elseif upvalue.name == '_ENV' then + debug.setupvalue(func, index, _ENV) + else + debug.setupvalue(func, index, upvalue.value) + end + end + return func elseif typeidx == TYPE_TORCH then local version, className, versionNumber version = self:readChar(self:readInt()):string() |