mirror of
https://github.com/neovim/neovim.git
synced 2024-12-29 14:41:06 -07:00
channel.c: refactor spaghetti code
channel.c: WIP remove redundant method check and added FUNC_ATTR_NONNULL_ALL macro channel.c channel_defs.h helpers.c: added Error field to RequestEvent, added no_op handler func channel.c: use const char* instead of string and cleanup channel.c; channel_defs.h; helpers.c: removed error from event again; send errors directly to the channel without using handlers and events channel.c: fixed memory leak and lint errors api/private/dispatch.c; api/vim.c; msgpack_rpc/channel.c msgpack_rpc/helpers.c added Error* field to msgpack_get_handler_for; further refactored channel.c channel.c:323 changed order of evaluation in if statement channel.c: removed superflous whitespace dispatch.c: review comment
This commit is contained in:
parent
4874214139
commit
ed02278e42
@ -32,16 +32,19 @@ static void msgpack_rpc_add_method_handler(String method,
|
|||||||
/// @param name API method name
|
/// @param name API method name
|
||||||
/// @param name_len name size (includes terminating NUL)
|
/// @param name_len name size (includes terminating NUL)
|
||||||
MsgpackRpcRequestHandler msgpack_rpc_get_handler_for(const char *name,
|
MsgpackRpcRequestHandler msgpack_rpc_get_handler_for(const char *name,
|
||||||
size_t name_len)
|
size_t name_len,
|
||||||
|
Error *error)
|
||||||
{
|
{
|
||||||
String m = { .data = (char *)name, .size = name_len };
|
String m = { .data = (char *)name, .size = name_len };
|
||||||
MsgpackRpcRequestHandler rv =
|
MsgpackRpcRequestHandler rv =
|
||||||
map_get(String, MsgpackRpcRequestHandler)(methods, m);
|
map_get(String, MsgpackRpcRequestHandler)(methods, m);
|
||||||
|
|
||||||
if (!rv.fn) {
|
if (!rv.fn) {
|
||||||
rv.fn = msgpack_rpc_handle_missing_method;
|
String method_name = m.size > 0 ?
|
||||||
|
m : cstr_as_string("<empty>");
|
||||||
|
api_set_error(error, kErrorTypeException, "Invalid method: %s",
|
||||||
|
method_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1162,11 +1162,12 @@ Array nvim_call_atomic(uint64_t channel_id, Array calls, Error *err)
|
|||||||
}
|
}
|
||||||
Array args = call.items[1].data.array;
|
Array args = call.items[1].data.array;
|
||||||
|
|
||||||
MsgpackRpcRequestHandler handler = msgpack_rpc_get_handler_for(name.data,
|
MsgpackRpcRequestHandler handler =
|
||||||
name.size);
|
msgpack_rpc_get_handler_for(name.data,
|
||||||
if (handler.fn == msgpack_rpc_handle_missing_method) {
|
name.size,
|
||||||
api_set_error(&nested_error, kErrorTypeException, "Invalid method: %s",
|
&nested_error);
|
||||||
name.size > 0 ? name.data : "<empty>");
|
|
||||||
|
if (ERROR_SET(&nested_error)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Object result = handler.fn(channel_id, args, &nested_error);
|
Object result = handler.fn(channel_id, args, &nested_error);
|
||||||
|
@ -287,7 +287,6 @@ static void parse_msgpack(Channel *channel)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void handle_request(Channel *channel, msgpack_object *request)
|
static void handle_request(Channel *channel, msgpack_object *request)
|
||||||
FUNC_ATTR_NONNULL_ALL
|
FUNC_ATTR_NONNULL_ALL
|
||||||
{
|
{
|
||||||
@ -313,27 +312,24 @@ static void handle_request(Channel *channel, msgpack_object *request)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve the request handler
|
|
||||||
MsgpackRpcRequestHandler handler;
|
MsgpackRpcRequestHandler handler;
|
||||||
Array args = ARRAY_DICT_INIT;
|
|
||||||
msgpack_object *method = msgpack_rpc_method(request);
|
msgpack_object *method = msgpack_rpc_method(request);
|
||||||
|
|
||||||
if (method) {
|
|
||||||
handler = msgpack_rpc_get_handler_for(method->via.bin.ptr,
|
handler = msgpack_rpc_get_handler_for(method->via.bin.ptr,
|
||||||
method->via.bin.size);
|
method->via.bin.size,
|
||||||
if (handler.fn == msgpack_rpc_handle_missing_method) {
|
&error);
|
||||||
String m = method->via.bin.size > 0
|
|
||||||
? cbuf_to_string(method->via.bin.ptr, method->via.bin.size)
|
// check method arguments
|
||||||
: cstr_to_string("<empty>");
|
Array args = ARRAY_DICT_INIT;
|
||||||
ADD(args, STRING_OBJ(m));
|
if (!ERROR_SET(&error)
|
||||||
handler.async = true;
|
&& !msgpack_rpc_to_array(msgpack_rpc_args(request), &args)) {
|
||||||
} else if (!msgpack_rpc_to_array(msgpack_rpc_args(request), &args)) {
|
api_set_error(&error, kErrorTypeException, "Invalid method arguments");
|
||||||
handler.fn = msgpack_rpc_handle_invalid_arguments;
|
|
||||||
handler.async = true;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
handler.fn = msgpack_rpc_handle_missing_method;
|
if (ERROR_SET(&error)) {
|
||||||
handler.async = true;
|
send_error(channel, request_id, error.msg);
|
||||||
|
api_clear_error(&error);
|
||||||
|
api_free_array(args);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
RequestEvent *evdata = xmalloc(sizeof(RequestEvent));
|
RequestEvent *evdata = xmalloc(sizeof(RequestEvent));
|
||||||
|
@ -488,25 +488,6 @@ void msgpack_rpc_from_dictionary(Dictionary result, msgpack_packer *res)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handler executed when an invalid method name is passed
|
|
||||||
Object msgpack_rpc_handle_missing_method(uint64_t channel_id,
|
|
||||||
Array args,
|
|
||||||
Error *error)
|
|
||||||
{
|
|
||||||
api_set_error(error, kErrorTypeException, "Invalid method: %s",
|
|
||||||
args.size > 0 ? args.items[0].data.string.data : "?");
|
|
||||||
return NIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Handler executed when malformated arguments are passed
|
|
||||||
Object msgpack_rpc_handle_invalid_arguments(uint64_t channel_id,
|
|
||||||
Array args,
|
|
||||||
Error *error)
|
|
||||||
{
|
|
||||||
api_set_error(error, kErrorTypeException, "Invalid method arguments");
|
|
||||||
return NIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Serializes a msgpack-rpc request or notification(id == 0)
|
/// Serializes a msgpack-rpc request or notification(id == 0)
|
||||||
void msgpack_rpc_serialize_request(uint64_t request_id,
|
void msgpack_rpc_serialize_request(uint64_t request_id,
|
||||||
const String method,
|
const String method,
|
||||||
|
Loading…
Reference in New Issue
Block a user