fnuecke / eris

Heavy Duty Persistence for Lua 5.2 and 5.3
Other
169 stars 23 forks source link

attempt to persist a light C function? #8

Closed krisr closed 10 years ago

krisr commented 10 years ago

Thank you for all your hard work on this library. I'm really excited about using it, I can see it saving me TONS of time. However, I'm having some issues understanding how I'm supposed to work around the "attempt to persist a light C function".

I'm not sure I understand what is meant by "light C function" anymore.

For example, why does this fail? I don't see any use of a C function:

a = 3
eris:persist(function() return a end)

while this works:

local a = 3
eris:persist(function() return a end)

It's also worth noting that I expected this to work as well:

eris:perist({[cfunc]: 1}, function() return cfunc end)

But it didn't. This did work as expected though:

eris:perist({[cfunc]: 1}, cfunc)
fnuecke commented 10 years ago

I'm glad if it helps!

What you're probably seeing is the environment your closures are created in being stored as an upvalue of that closure - that environment probably being _G, and therefore containing a bunch of C functions. A quick solution for this (and in most cases a good idea anyway) is to simply throw _G into the permanent values.

In general, I'd recommend eris.settings("path", true) when debugging, in which case it'll tell you exactly where it failed (see the last section in the readme).

Since it can happen that C functions from the global environment are stored somewhere else, directly (e.g. local variables or as upvalues), it can make sense to automatically populate the permanent value table with whatever functions there are in the global environment (directly or indirectly). Here's a snippet that does this, lines 34-67, if you're interested. I've been using this approach (though implemented on the 'host' side) very successfully.

krisr commented 10 years ago

Thanks so much for the quick reply. Yes, I worked around this exactly as you suggested, but I didn't know about eris.settings("path..., that will be very helpful. I will play around with your snippet as well.

I'm using eris to build a stateful UI library on top of cocoa for my game development tools. It's really neat to be able to launch a handful of new 3D views and slider panels, quit to tweak some C++ rendering code, and relaunch with the exact same UI and views I had.

fnuecke commented 10 years ago

Happy to help. That's a pretty cool use-case, good luck with it!