hchunhui / librime-lua

Extending RIME with Lua scripts
BSD 3-Clause "New" or "Revised" License
309 stars 43 forks source link

tie the lifetime of the lua state to components #353

Open ksqsf opened 3 months ago

ksqsf commented 3 months ago

Currently, the lifetime of the lua state is not destroyed even all sessions are dead. This is because the Lua state is tied to the registration, not the real live components.

This creates a rather weird problem, that the Lua state will not be destroyed when all sessions are destroyed with rime_api->cleanup_all_sessions. A known problem is that this can affect the usability of synchronization, if the lua script author forgets to set lua Components to nil and/or call collectgarbage manually.

This PR defers the creation of the lua state until a lua gear is actually created. Then, the ownership of the created lua state is shared globally, and the lua state will be destroyed when no one is referring to the lua state. Finally, if it is requested again, a new lua state will be created.

This is probably a breaking change for some users.

hchunhui commented 3 months ago

原先的考虑是 lua script 可能会保存一些全局状态,比如启动以来打了几个字,最近的候选是哪些等,所以做成了这种不与组件绑定的形式。

这个 PR 吸引人的地方是可以比较明显地降低 script 的编写难度,但需要评估一下影响。我对 rime session 的不是很了解,不太清楚是否会经常性地重建,导致 lua 状态丢失。

hchunhui commented 3 months ago

另外 Component 只能用 gc 来销毁,我认为应该类似 https://github.com/hchunhui/librime-lua/issues/335#issuecomment-2105673050 这里也提供一个手动释放的方法。

ksqsf commented 3 months ago

按目前的写法,sync 和 deploy 的时候一定会重建状态。自然使用时 session 全部消失似乎可能性很低,不过切到别的输入法的时候应该就没了。