Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for finalisers using the __gc metamethod #5

Open
mjanicek opened this issue Oct 7, 2016 · 2 comments
Open

Add support for finalisers using the __gc metamethod #5

mjanicek opened this issue Oct 7, 2016 · 2 comments

Comments

@mjanicek
Copy link
Owner

mjanicek commented Oct 7, 2016

In Lua, tables and userdata may be marked for finalisation using the __gc metamethod. (For details, see §2.5.1 of the Lua Reference Manual). This is currently not possible in Rembulan.

@bensku
Copy link

bensku commented Oct 13, 2016

I'm interested to hear how this could be implemented. Java does have finalizers/destructors, but they are not reliable, while Lua metamethod should be.

@mjanicek
Copy link
Owner Author

The tricky part here is to execute the finalisers (which may be arbitrary Lua functions) within an execution context. But I think it's doable: the main idea would be to use a ReferenceQueue to register objects marked for finalisation, and pull them out of the queue once they have been GC'd.

There are quite a few degrees of freedom the implementation has when it comes to deciding when the finaliser is then actually run. Per the Lua Reference Manual, the finaliser is not called immediately once its object is GC'd, but "at some point" after -- and I would see that as compatible with Java's behaviour.

Of course, the problem is that the GC behaviour is unpredictable on the JVM, but not entirely unpredictable in PUC-Lua: PUC-Lua's standard library function collectgarbage allows you to run a full GC cycle. On the (generic) JVM we don't have that. (System.gc() only guarantees that "a best effort" will be made.)

However, I would see this as a limitation of Rembulan's standard library, and argue that it does not really get in the way of implementing the Lua programming language with the same semantics. Besides, Lua programs that depend on the GC behaviour as fundamentally broken anyway. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants