refactor(api): do not allocate temporaries for internal events

This commit is contained in:
bfredl 2023-01-03 15:24:41 +01:00
parent 5b22b32e50
commit b2295ac4ec
8 changed files with 47 additions and 48 deletions

View File

@ -88,6 +88,7 @@ bool buf_updates_register(buf_T *buf, uint64_t channel_id, BufUpdateCallbacks cb
args.items[5] = BOOLEAN_OBJ(false);
rpc_send_event(channel_id, "nvim_buf_lines_event", args);
api_free_array(args); // TODO(bfredl): no
} else {
buf_updates_changedtick_single(buf, channel_id);
}
@ -103,10 +104,8 @@ bool buf_updates_active(buf_T *buf)
void buf_updates_send_end(buf_T *buf, uint64_t channelid)
{
Array args = ARRAY_DICT_INIT;
args.size = 1;
args.items = xcalloc(args.size, sizeof(Object));
args.items[0] = BUFFER_OBJ(buf->handle);
MAXSIZE_TEMP_ARRAY(args, 1);
ADD_C(args, BUFFER_OBJ(buf->handle));
rpc_send_event(channelid, "nvim_buf_detach_event", args);
}
@ -262,6 +261,7 @@ void buf_updates_send_changes(buf_T *buf, linenr_T firstline, int64_t num_added,
// the end.
badchannelid = channelid;
}
api_free_array(args); // TODO(bfredl): no
}
// We can only ever remove one dead channel at a time. This is OK because the
@ -408,15 +408,13 @@ void buf_updates_changedtick(buf_T *buf)
void buf_updates_changedtick_single(buf_T *buf, uint64_t channel_id)
{
Array args = ARRAY_DICT_INIT;
args.size = 2;
args.items = xcalloc(args.size, sizeof(Object));
MAXSIZE_TEMP_ARRAY(args, 2);
// the first argument is always the buffer handle
args.items[0] = BUFFER_OBJ(buf->handle);
ADD_C(args, BUFFER_OBJ(buf->handle));
// next argument is b:changedtick
args.items[1] = INTEGER_OBJ(buf_get_changedtick(buf));
ADD_C(args, INTEGER_OBJ(buf_get_changedtick(buf)));
// don't try and clean up dead channels here
rpc_send_event(channel_id, "nvim_buf_changedtick_event", args);

View File

@ -6322,12 +6322,15 @@ static void f_rpcnotify(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
ADD(args, vim_to_object(tv));
}
if (!rpc_send_event((uint64_t)argvars[0].vval.v_number,
tv_get_string(&argvars[1]), args)) {
bool ok = rpc_send_event((uint64_t)argvars[0].vval.v_number,
tv_get_string(&argvars[1]), args);
if (!ok) {
semsg(_(e_invarg2), "Channel doesn't exist");
return;
}
api_free_array(args);
rettv->vval.v_number = 1;
}

View File

@ -1190,6 +1190,7 @@ static int nlua_rpc(lua_State *lstate, bool request)
api_set_error(&err, kErrorTypeValidation,
"Invalid channel: %" PRIu64, chan_id);
}
api_free_array(args); // TODO(bfredl): no
}
check_err:

View File

@ -136,6 +136,7 @@ Object rpc_send_call(uint64_t id, const char *method_name, Array args, ArenaMem
uint32_t request_id = rpc->next_request_id++;
// Send the msgpack-rpc request
send_request(channel, request_id, method_name, args);
api_free_array(args);
// Push the frame
ChannelCallFrame frame = { request_id, false, false, NIL, NULL };
@ -491,7 +492,6 @@ static void broadcast_event(const char *name, Array args)
});
if (!kv_size(subscribed)) {
api_free_array(args);
goto end;
}
@ -593,7 +593,6 @@ static WBuffer *serialize_request(uint64_t channel_id, uint32_t request_id, cons
refcount,
xfree);
msgpack_sbuffer_clear(sbuffer);
api_free_array(args);
return rv;
}

View File

@ -220,11 +220,12 @@ static void tinput_wait_enqueue(void **argv)
const size_t len = rbuffer_size(input->key_buffer);
String keys = { .data = xmallocz(len), .size = len };
rbuffer_read(input->key_buffer, keys.data, len);
Array args = ARRAY_DICT_INIT;
ADD(args, STRING_OBJ(keys)); // 'data'
ADD(args, BOOLEAN_OBJ(true)); // 'crlf'
ADD(args, INTEGER_OBJ(input->paste)); // 'phase'
MAXSIZE_TEMP_ARRAY(args, 3);
ADD_C(args, STRING_OBJ(keys)); // 'data'
ADD_C(args, BOOLEAN_OBJ(true)); // 'crlf'
ADD_C(args, INTEGER_OBJ(input->paste)); // 'phase'
rpc_send_event(ui_client_channel_id, "nvim_paste", args);
api_free_string(keys);
if (input->paste == 1) {
// Paste phase: "continue"
input->paste = 2;
@ -233,8 +234,8 @@ static void tinput_wait_enqueue(void **argv)
} else { // enqueue input for the main thread or Nvim server
RBUFFER_UNTIL_EMPTY(input->key_buffer, buf, len) {
const String keys = { .data = buf, .size = len };
Array args = ARRAY_DICT_INIT;
ADD(args, STRING_OBJ(copy_string(keys, NULL)));
MAXSIZE_TEMP_ARRAY(args, 1);
ADD_C(args, STRING_OBJ(keys));
// NOTE: This is non-blocking and won't check partially processed input,
// but should be fine as all big sends are handled with nvim_paste, not nvim_input
rpc_send_event(ui_client_channel_id, "nvim_input", args);
@ -529,8 +530,8 @@ static bool handle_focus_event(TermInput *input)
// Advance past the sequence
rbuffer_consumed(input->read_stream.buffer, 3);
Array args = ARRAY_DICT_INIT;
ADD(args, BOOLEAN_OBJ(focus_gained));
MAXSIZE_TEMP_ARRAY(args, 1);
ADD_C(args, BOOLEAN_OBJ(focus_gained));
rpc_send_event(ui_client_channel_id, "nvim_ui_set_focus", args);
return true;
}
@ -581,10 +582,9 @@ static HandleState handle_bracketed_paste(TermInput *input)
static void set_bg(char *bgvalue)
{
if (ui_client_attached) {
Array args = ARRAY_DICT_INIT;
ADD(args, STRING_OBJ(cstr_to_string("term_background")));
ADD(args, STRING_OBJ(cstr_as_string(xstrdup(bgvalue))));
MAXSIZE_TEMP_ARRAY(args, 2);
ADD_C(args, STRING_OBJ(cstr_as_string("term_background")));
ADD_C(args, STRING_OBJ(cstr_as_string(bgvalue)));
rpc_send_event(ui_client_channel_id, "nvim_ui_set_option", args);
}
}

View File

@ -1401,6 +1401,7 @@ static void show_verbose_terminfo(TUIData *data)
PUT(opts, "verbose", BOOLEAN_OBJ(true));
ADD(args, DICTIONARY_OBJ(opts));
rpc_send_event(ui_client_channel_id, "nvim_echo", args);
api_free_array(args);
}
#ifdef UNIX
@ -1493,9 +1494,9 @@ static void tui_option_set(UI *ui, String name, Object value)
invalidate(ui, 0, data->grid.height, 0, data->grid.width);
if (ui_client_channel_id) {
Array args = ARRAY_DICT_INIT;
ADD(args, STRING_OBJ(cstr_as_string(xstrdup("rgb"))));
ADD(args, BOOLEAN_OBJ(value.data.boolean));
MAXSIZE_TEMP_ARRAY(args, 2);
ADD_C(args, STRING_OBJ(cstr_as_string("rgb")));
ADD_C(args, BOOLEAN_OBJ(value.data.boolean));
rpc_send_event(ui_client_channel_id, "nvim_ui_set_option", args);
}
} else if (strequal(name.data, "ttimeout")) {

View File

@ -227,9 +227,9 @@ void ui_refresh(void)
if (ui_client_attached) {
// TODO(bfredl): ui_refresh() should only be used on the server
// we are in the client process. forward the resize
Array args = ARRAY_DICT_INIT;
ADD(args, INTEGER_OBJ((int)width));
ADD(args, INTEGER_OBJ((int)height));
MAXSIZE_TEMP_ARRAY(args, 2);
ADD_C(args, INTEGER_OBJ((int)width));
ADD_C(args, INTEGER_OBJ((int)height));
rpc_send_event(ui_client_channel_id, "nvim_ui_try_resize", args);
} else {
/// TODO(bfredl): Messy! The screen does not yet exist, but we need to

View File

@ -56,33 +56,30 @@ void ui_client_run(bool remote_ui)
loop_poll_events(&main_loop, 1);
Array args = ARRAY_DICT_INIT;
Dictionary opts = ARRAY_DICT_INIT;
PUT(opts, "rgb", BOOLEAN_OBJ(true));
PUT(opts, "ext_linegrid", BOOLEAN_OBJ(true));
PUT(opts, "ext_termcolors", BOOLEAN_OBJ(true));
MAXSIZE_TEMP_ARRAY(args, 3);
ADD_C(args, INTEGER_OBJ(Columns));
ADD_C(args, INTEGER_OBJ(Rows));
MAXSIZE_TEMP_DICT(opts, 9);
PUT_C(opts, "rgb", BOOLEAN_OBJ(true));
PUT_C(opts, "ext_linegrid", BOOLEAN_OBJ(true));
PUT_C(opts, "ext_termcolors", BOOLEAN_OBJ(true));
if (ui_client_termname) {
PUT(opts, "term_name", STRING_OBJ(cstr_as_string(ui_client_termname)));
PUT_C(opts, "term_name", STRING_OBJ(cstr_as_string(ui_client_termname)));
}
if (ui_client_bg_respose != kNone) {
bool is_dark = (ui_client_bg_respose == kTrue);
PUT(opts, "term_background", STRING_OBJ(cstr_as_string(is_dark ? "dark" : "light")));
PUT_C(opts, "term_background", STRING_OBJ(cstr_as_string(is_dark ? "dark" : "light")));
}
PUT(opts, "term_colors", INTEGER_OBJ(t_colors));
PUT_C(opts, "term_colors", INTEGER_OBJ(t_colors));
if (!remote_ui) {
PUT(opts, "stdin_tty", BOOLEAN_OBJ(stdin_isatty));
PUT(opts, "stdout_tty", BOOLEAN_OBJ(stdout_isatty));
PUT_C(opts, "stdin_tty", BOOLEAN_OBJ(stdin_isatty));
PUT_C(opts, "stdout_tty", BOOLEAN_OBJ(stdout_isatty));
if (ui_client_forward_stdin) {
PUT(opts, "stdin_fd", INTEGER_OBJ(UI_CLIENT_STDIN_FD));
PUT_C(opts, "stdin_fd", INTEGER_OBJ(UI_CLIENT_STDIN_FD));
}
}
ADD(args, INTEGER_OBJ(Columns));
ADD(args, INTEGER_OBJ(Rows));
ADD(args, DICTIONARY_OBJ(opts));
ADD_C(args, DICTIONARY_OBJ(opts));
rpc_send_event(ui_client_channel_id, "nvim_ui_attach", args);
ui_client_attached = true;