From dfd3fcada6a6c3c87e25aef9b5583676778832fb Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 4 Oct 2024 14:30:38 +0100 Subject: [PATCH] library: api_close --- src/library/plugin/plugin.c | 13 ++++++++++++- src/library/plugin/plugin_api.h | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/library/plugin/plugin.c b/src/library/plugin/plugin.c index ab9d952..e35f942 100644 --- a/src/library/plugin/plugin.c +++ b/src/library/plugin/plugin.c @@ -275,9 +275,10 @@ void _bolt_plugin_init(const struct PluginManagedFunctions* functions) { } static int _bolt_api_init(lua_State* state) { - lua_createtable(state, 0, 18); + lua_createtable(state, 0, 19); API_ADD(apiversion) API_ADD(checkversion) + API_ADD(close) API_ADD(time) API_ADD(datetime) API_ADD(weekday) @@ -1009,6 +1010,16 @@ static int api_checkversion(lua_State* state) { return 2; } +static int api_close(lua_State* state) { + lua_getfield(state, LUA_REGISTRYINDEX, PLUGIN_REGISTRYNAME); + const struct Plugin* plugin = lua_touserdata(state, -1); + const uint64_t id = plugin->id; + lua_pop(state, 1); + _bolt_plugin_stop(id); + _bolt_plugin_notify_stopped(id); + return 0; +} + static int api_time(lua_State* state) { _bolt_check_argc(state, 0, "time"); #if defined(_WIN32) diff --git a/src/library/plugin/plugin_api.h b/src/library/plugin/plugin_api.h index 9b75140..5780395 100644 --- a/src/library/plugin/plugin_api.h +++ b/src/library/plugin/plugin_api.h @@ -55,6 +55,11 @@ static int api_apiversion(lua_State*); /// For compatibility reasons, there will never be a breaking change to this function. static int api_checkversion(lua_State*); +/// [-0, +0, -] +/// Stops this instance of this plugin. Any resources will be cleaned up and destroyed. Do not use +/// any API functions after this one. +static int api_close(lua_State*); + /// [-0, +1, -] /// Returns a monotonic time as an integer, in microseconds. ///