[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: What happens when you try to serialize a closure
- From: Finn Wilcox <finnw@...>
- Date: Fri, 25 Jan 2013 00:59:05 +0000
I just noticed this question on stackoverflow
(http://stackoverflow.com/questions/14509814)
The poster was trying to serialize a closure (using string.dump.)
This is their code sample:
> function ffactory(x) return function() return x end end
> f1 = ffactory(5)
> print(f1())
5 <-- so far so good
> s = string.dump(f1)
> f2 = load(s)
> print(f2())
table: 00000000002F7BA0 <-- expected the integer 5
> print(f2()==_ENV)
true <-- definitely didn't expect this!
I understand that f2 has no way to access the original upvalue (which is
why one should not pass a function with upvalues to string.dump.)
But still, I agree with the "definitely didn't expect this!" remark.
Shouldn't f2() return nil rather than _ENV?
Is this a bug?