aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/generators/gen_api_dispatch.lua
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2022-11-14 18:04:36 +0000
committerGitHub <noreply@github.com>2022-11-14 18:04:36 +0000
commitf8c671827710c6e9cca3bfd60c32098b2be8239a (patch)
treea5d949663a1b061ca82ecd89bb90f083f9a2bbb8 /src/nvim/generators/gen_api_dispatch.lua
parent30604320072335122aea0f37890f136b2ba0e445 (diff)
downloadrneovim-f8c671827710c6e9cca3bfd60c32098b2be8239a.tar.gz
rneovim-f8c671827710c6e9cca3bfd60c32098b2be8239a.tar.bz2
rneovim-f8c671827710c6e9cca3bfd60c32098b2be8239a.zip
feat(lua-api): avoid unnecessary allocations (#19877)
Lua makes (or reuses) an internal copy of strings, so we can safely push buf pointers onto the stack.
Diffstat (limited to 'src/nvim/generators/gen_api_dispatch.lua')
-rw-r--r--src/nvim/generators/gen_api_dispatch.lua34
1 files changed, 32 insertions, 2 deletions
diff --git a/src/nvim/generators/gen_api_dispatch.lua b/src/nvim/generators/gen_api_dispatch.lua
index 67b8f5f0f5..6894b7a6df 100644
--- a/src/nvim/generators/gen_api_dispatch.lua
+++ b/src/nvim/generators/gen_api_dispatch.lua
@@ -78,6 +78,10 @@ for i = 6, #arg do
fn.arena_return = true
fn.parameters[#fn.parameters] = nil
end
+ if #fn.parameters ~= 0 and fn.parameters[#fn.parameters][1] == 'lstate' then
+ fn.has_lua_imp = true
+ fn.parameters[#fn.parameters] = nil
+ end
end
end
input:close()
@@ -329,6 +333,14 @@ for i = 1, #functions do
output:write(', arena')
end
+ if fn.has_lua_imp then
+ if #args > 0 then
+ output:write(', NULL')
+ else
+ output:write('NULL')
+ end
+ end
+
if fn.can_fail then
-- if the function can fail, also pass a pointer to the local error object
if #args > 0 then
@@ -497,6 +509,10 @@ local function process_function(fn)
]])
end
+ if fn.has_lua_imp then
+ cparams = cparams .. 'lstate, '
+ end
+
if fn.can_fail then
cparams = cparams .. '&err'
else
@@ -539,13 +555,27 @@ local function process_function(fn)
end
write_shifted_output(output, string.format([[
const %s ret = %s(%s);
+ ]], fn.return_type, fn.name, cparams))
+
+ if fn.has_lua_imp then
+ -- only push onto the Lua stack if we haven't already
+ write_shifted_output(output, string.format([[
+ if (lua_gettop(lstate) == 0) {
+ nlua_push_%s(lstate, ret, true);
+ }
+ ]], return_type))
+ else
+ write_shifted_output(output, string.format([[
nlua_push_%s(lstate, ret, true);
+ ]], return_type))
+ end
+
+ write_shifted_output(output, string.format([[
%s
%s
%s
return 1;
- ]], fn.return_type, fn.name, cparams, return_type,
- free_retval, free_at_exit_code, err_throw_code))
+ ]], free_retval, free_at_exit_code, err_throw_code))
else
write_shifted_output(output, string.format([[
%s(%s);