diff options
author | Peng Wu <alexepico@gmail.com> | 2021-01-27 15:29:36 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2021-01-27 15:40:33 +0800 |
commit | b8d700607ea7a6d27cc9c7ab9d4f9901bd40997c (patch) | |
tree | a8816c2eafab8b2dbb04d0fcc5c921a44cb9cd5e | |
parent | dcf8e13c6921e4d8a39ec9bf33263fd37616560c (diff) | |
download | ibus-libpinyin-b8d700607ea7a6d27cc9c7ab9d4f9901bd40997c.tar.gz |
Update Lua Plugin API
-rw-r--r-- | lua/lua-ext-console.c | 8 | ||||
-rw-r--r-- | lua/lua-plugin.c | 39 | ||||
-rw-r--r-- | lua/lua-plugin.h | 14 |
3 files changed, 53 insertions, 8 deletions
diff --git a/lua/lua-ext-console.c b/lua/lua-ext-console.c index 3f6e3cc..80bf291 100644 --- a/lua/lua-ext-console.c +++ b/lua/lua-ext-console.c @@ -102,8 +102,14 @@ int do_lua_call(IBusEnginePlugin * plugin, const char * command_name, const char } int do_simple_lua_call(IBusEnginePlugin * plugin, const char * lua_function_name, const char * string){ + int i; int num = ibus_engine_plugin_call(plugin, lua_function_name, string); - printf("result: %s.\n", ibus_engine_plugin_get_first_result(plugin)); + g_assert(num == ibus_engine_plugin_get_n_result(plugin)); + for (i = 0; i < num ; ++i){ + printf("%d.%s >\t", i, ibus_engine_plugin_get_nth_result(plugin, i)); + } + printf("\n"); + ibus_engine_plugin_clear_results(plugin); return 0; } diff --git a/lua/lua-plugin.c b/lua/lua-plugin.c index 2f14156..0fa4c96 100644 --- a/lua/lua-plugin.c +++ b/lua/lua-plugin.c @@ -411,30 +411,59 @@ static const lua_command_candidate_t * ibus_engine_plugin_get_candidate(lua_Stat } /** - * retrieve the first string value. (value has been copied.) + * retrieve the number of string values. */ -gchar * ibus_engine_plugin_get_first_result(IBusEnginePlugin * plugin){ +gint ibus_engine_plugin_get_n_result(IBusEnginePlugin * plugin){ + IBusEnginePluginPrivate * priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE(plugin); + int type; + lua_State * L = priv->L; + + type = lua_type(L ,-1); + if ( LUA_TNUMBER == type || LUA_TBOOLEAN == type || LUA_TSTRING == type) + return 1; + else if( LUA_TTABLE == type ) + return lua_objlen (L, -1); + + return 0; +} + +/** + * retrieve the nth string value. (value has been copied.) + */ +gchar * ibus_engine_plugin_get_nth_result(IBusEnginePlugin * plugin, gint index){ IBusEnginePluginPrivate * priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE(plugin); const char * result = NULL; int type; lua_State * L = priv->L; type = lua_type(L ,-1); if ( LUA_TNUMBER == type || LUA_TBOOLEAN == type || LUA_TSTRING == type) { + /* check index value */ + g_assert(0 == index); result = g_strdup(lua_tostring(L, -1)); - lua_pop(L, 1); } else if( LUA_TTABLE == type ){ - lua_pushinteger(L, 1); + /* check index value */ + g_assert(index < lua_objlen (L, -1)); + lua_pushinteger(L, (guint) index + 1); lua_gettable(L, -2); int type = lua_type(L, -1); if ( LUA_TNUMBER == type || LUA_TBOOLEAN == type || LUA_TSTRING == type ) result = g_strdup(lua_tostring(L, -1)); - lua_pop(L, 2); + lua_pop(L, 1); } return (char *)result; } /** + * clear the string values from the stack. + */ +void ibus_engine_plugin_clear_results(IBusEnginePlugin * plugin){ + IBusEnginePluginPrivate * priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE(plugin); + lua_State * L = priv->L; + lua_pop(L, 1); +} + +/** * retrieve the retval string value. (value has been copied.) */ const lua_command_candidate_t * ibus_engine_plugin_get_retval(IBusEnginePlugin * plugin){ diff --git a/lua/lua-plugin.h b/lua/lua-plugin.h index bed2f9a..46631dc 100644 --- a/lua/lua-plugin.h +++ b/lua/lua-plugin.h @@ -175,9 +175,19 @@ const lua_command_t * ibus_engine_plugin_lookup_command(IBusEnginePlugin * plugi int ibus_engine_plugin_call(IBusEnginePlugin * plugin, const char * lua_function_name, const char * argument /*optional, maybe NULL.*/); /** - * retrieve the first string value. (value has been copied.) + * retrieve the number of string values. */ -gchar * ibus_engine_plugin_get_first_result(IBusEnginePlugin * plugin); +gint ibus_engine_plugin_get_n_result(IBusEnginePlugin * plugin); + +/** + * retrieve the nth string value. (value has been copied.) + */ +gchar * ibus_engine_plugin_get_nth_result(IBusEnginePlugin * plugin, gint index); + +/** + * clear the string values from the stack. + */ +void ibus_engine_plugin_clear_results(IBusEnginePlugin * plugin); /** * retrieve the retval string value. (value has been copied.) |