diff options
| author | bfredl <bjorn.linse@gmail.com> | 2022-05-30 00:59:06 +0200 |
|---|---|---|
| committer | bfredl <bjorn.linse@gmail.com> | 2022-05-30 14:11:01 +0200 |
| commit | 1f63052b682a6019d7f092553747272195fbfffd (patch) | |
| tree | bf1d011b37bc1e61e75c6d8859c830e09d5c0285 /src/nvim/generators | |
| parent | e9803e1de6497ee21f77f45cf2670c2fe4e8ab22 (diff) | |
| download | rneovim-1f63052b682a6019d7f092553747272195fbfffd.tar.gz rneovim-1f63052b682a6019d7f092553747272195fbfffd.tar.bz2 rneovim-1f63052b682a6019d7f092553747272195fbfffd.zip | |
refactor(api): use hashy hash for looking up api method and event names
This avoids generating khash tables at runtime, and is consistent with
how evalfuncs lookup work.
Diffstat (limited to 'src/nvim/generators')
| -rw-r--r-- | src/nvim/generators/gen_api_dispatch.lua | 35 | ||||
| -rwxr-xr-x | src/nvim/generators/gen_api_ui_events.lua | 32 |
2 files changed, 39 insertions, 28 deletions
diff --git a/src/nvim/generators/gen_api_dispatch.lua b/src/nvim/generators/gen_api_dispatch.lua index c6dd25154b..38aca32f33 100644 --- a/src/nvim/generators/gen_api_dispatch.lua +++ b/src/nvim/generators/gen_api_dispatch.lua @@ -16,6 +16,10 @@ local functions = {} local nvimdir = arg[1] package.path = nvimdir .. '/?.lua;' .. package.path +_G.vim = loadfile(nvimdir..'/../../runtime/lua/vim/shared.lua')() + +local hashy = require'generators.hashy' + -- names of all headers relative to the source root (for inclusion in the -- generated file) local headers = {} @@ -339,24 +343,27 @@ for i = 1, #functions do end end --- Generate a function that initializes method names with handler functions -output:write([[ -void msgpack_rpc_init_method_table(void) -{ -]]) - -for i = 1, #functions do - local fn = functions[i] +local remote_fns = {} +for _,fn in ipairs(functions) do if fn.remote then - output:write(' msgpack_rpc_add_method_handler('.. - '(String) {.data = "'..fn.name..'", '.. - '.size = sizeof("'..fn.name..'") - 1}, '.. - '(MsgpackRpcRequestHandler) {.fn = handle_'.. (fn.impl_name or fn.name).. - ', .fast = '..tostring(fn.fast)..'});\n') + remote_fns[fn.name] = fn end end +remote_fns.redraw = {impl_name="ui_client_redraw", fast=true} + +local hashorder, hashfun = hashy.hashy_hash("msgpack_rpc_get_handler_for", vim.tbl_keys(remote_fns), function (idx) + return "method_handlers["..idx.."].name" +end) + +output:write("static const MsgpackRpcRequestHandler method_handlers[] = {\n") +for _, name in ipairs(hashorder) do + local fn = remote_fns[name] + output:write(' { .name = "'..name..'", .fn = handle_'.. (fn.impl_name or fn.name).. + ', .fast = '..tostring(fn.fast)..'},\n') +end +output:write("};\n\n") +output:write(hashfun) -output:write('\n}\n\n') output:close() local mpack_output = io.open(mpack_outputf, 'wb') diff --git a/src/nvim/generators/gen_api_ui_events.lua b/src/nvim/generators/gen_api_ui_events.lua index 5e70442dce..99dfac05e8 100755 --- a/src/nvim/generators/gen_api_ui_events.lua +++ b/src/nvim/generators/gen_api_ui_events.lua @@ -15,6 +15,9 @@ local client_output = io.open(arg[8], 'wb') local c_grammar = require('generators.c_grammar') local events = c_grammar.grammar:match(input:read('*all')) +_G.vim = loadfile(nvimdir..'/../../runtime/lua/vim/shared.lua')() +local hashy = require'generators.hashy' + local function write_signature(output, ev, prefix, notype) output:write('('..prefix) if prefix == "" and #ev.parameters == 0 then @@ -213,24 +216,25 @@ for i = 1, #events do end end --- Generate the map_init method for client handlers -client_output:write([[ -void ui_client_methods_table_init(void) -{ +local client_events = {} +for _,ev in ipairs(events) do + if (not ev.noexport) and ((not ev.remote_only) or ev.client_impl) then + client_events[ev.name] = ev + end +end -]]) +local hashorder, hashfun = hashy.hashy_hash("ui_client_handler", vim.tbl_keys(client_events), function (idx) + return "event_handlers["..idx.."].name" +end) -for i = 1, #events do - local fn = events[i] - if (not fn.noexport) and ((not fn.remote_only) or fn.client_impl) then - client_output:write(' add_ui_client_event_handler('.. - '(String) {.data = "'..fn.name..'", '.. - '.size = sizeof("'..fn.name..'") - 1}, '.. - '(UIClientHandler) ui_client_event_'..fn.name..');\n') - end +client_output:write("static const UIClientHandler event_handlers[] = {\n") + +for _, name in ipairs(hashorder) do + client_output:write(' { .name = "'..name..'", .fn = ui_client_event_'..name..'},\n') end -client_output:write('\n}\n\n') +client_output:write('\n};\n\n') +client_output:write(hashfun) proto_output:close() call_output:close() |