Lua的关机/程序执行回调结束 [英] Lua shutdown/End of the program execution callback

查看:421
本文介绍了Lua的关机/程序执行回调结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的Lua的模块。在关闭LUA间preTER它必须运行清理程序,即使用户忘记隐式调用shutdown程序。

I am writing a module for Lua. On closing the lua interpreter it must run clean up routines even if user forgets to call shutdown routine implicitly.

该模块主要是用C写的

我应该用在Lua C API回调什么检测程序执行的结束?我来了,唯一的想法是使用__gc元方法对表重新presenting我的模块。任何想法?

What callback in Lua C Api should I use to detect end of program execution? The only idea I have come with is using __gc metamethod on table representing my module. Any ideas?

推荐答案

从C模块,简单的事情做的是创建一个完整的用户数据元表 __ GC 元方法。在模块的环境中的字段存储它,直到在模块卸载它不是由GC的收集

From a C module, the simple thing to do is to create a full userdata with a metatable with a __gc metamethod. Store it in a field in the module's environment so it isn't collected by the GC until the module is unloaded.

根据手动,只有用户数据得到他们的 __ GC 元方法的收集器调用,所以你不能用一个表来保存模块的终结。

According to the manual, only userdata get their __gc metamethod called by the collector, so you can't use a table to hold the module's finalizer.

有关用纯Lua的一个模块需要一个终结,你仍然需要有一个用户数据来阻止它。不受支持和无证,但广为人知的功能 newproxy() 可以用来创建一个空的,否则用户数据与元表用于此目的。称其为 newproxy(真)来得到一个带有元表,并使用 getmetatable()来检索的元表等等您可以添加 __ GC 元方法吧。

For a module written in pure Lua that needs a finalizer, you still need to have a userdata to hold it up. The unsupported and undocumented but widely known function newproxy() can be used to create an otherwise empty userdata with a metatable to use for this purpose. Call it as newproxy(true) to get one with a metatable, and use getmetatable() to retrieve the metatable so you can add the __gc metamethod to it.

这篇关于Lua的关机/程序执行回调结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆