aboutsummaryrefslogtreecommitdiff
path: root/src/os
diff options
context:
space:
mode:
authorThiago de Arruda <tpadilha84@gmail.com>2014-05-07 17:59:16 -0300
committerThiago de Arruda <tpadilha84@gmail.com>2014-05-12 19:28:30 -0300
commitb3268d071277c8967b3e3ecb60430718e1f36472 (patch)
tree37fbdeb52835cd3fff0648301292aec12f25bb12 /src/os
parentfc22317389fa8713f27f5a754ee142ae003f5871 (diff)
downloadrneovim-b3268d071277c8967b3e3ecb60430718e1f36472.tar.gz
rneovim-b3268d071277c8967b3e3ecb60430718e1f36472.tar.bz2
rneovim-b3268d071277c8967b3e3ecb60430718e1f36472.zip
Refactor API types and prototypes
- Split functions with multiple files in the 'api' subdirectory - Move/Add more types in the 'api/defs.h' header - Add more prototypes - Refactor scripts/msgpack-gen.lua - Move msgpack modules to 'os' subdirectory
Diffstat (limited to 'src/os')
-rw-r--r--src/os/msgpack_rpc.c409
-rw-r--r--src/os/msgpack_rpc.h107
2 files changed, 516 insertions, 0 deletions
diff --git a/src/os/msgpack_rpc.c b/src/os/msgpack_rpc.c
new file mode 100644
index 0000000000..0d5e3ef5da
--- /dev/null
+++ b/src/os/msgpack_rpc.c
@@ -0,0 +1,409 @@
+#include <msgpack.h>
+
+#include "msgpack_rpc.h"
+#include "vim.h"
+#include "memory.h"
+
+static bool msgpack_rpc_to_uint16_t(msgpack_object *obj, uint16_t *arg);
+
+void msgpack_rpc_call(msgpack_object *req, msgpack_packer *res)
+{
+ // The initial response structure is the same no matter what happens,
+ // we set it up here
+ // Array of size 4
+ msgpack_pack_array(res, 4);
+ // Response type is 1
+ msgpack_pack_int(res, 1);
+
+ // Validate the basic structure of the msgpack-rpc payload
+ if (req->type != MSGPACK_OBJECT_ARRAY) {
+ msgpack_pack_int(res, 0); // no message id yet
+ msgpack_rpc_error("Request is not an array", res);
+ return;
+ }
+
+ if (req->via.array.size != 4) {
+ msgpack_pack_int(res, 0); // no message id yet
+ char error_msg[256];
+ snprintf(error_msg,
+ sizeof(error_msg),
+ "Request array size is %u, it should be 4",
+ req->via.array.size);
+ msgpack_rpc_error(error_msg, res);
+ }
+
+ if (req->via.array.ptr[1].type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
+ msgpack_pack_int(res, 0); // no message id yet
+ msgpack_rpc_error("Id must be a positive integer", res);
+ }
+
+ // Set the response id, which is the same as the request
+ msgpack_pack_int(res, req->via.array.ptr[1].via.u64);
+
+ if (req->via.array.ptr[0].type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
+ msgpack_rpc_error("Message type must be an integer", res);
+ return;
+ }
+
+ if (req->via.array.ptr[0].via.u64 != 0) {
+ msgpack_rpc_error("Message type must be 0", res);
+ return;
+ }
+
+ if (req->via.array.ptr[2].type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
+ msgpack_rpc_error("Method id must be a positive integer", res);
+ return;
+ }
+
+ if (req->via.array.ptr[3].type != MSGPACK_OBJECT_ARRAY) {
+ msgpack_rpc_error("Paremeters must be an array", res);
+ return;
+ }
+
+ // dispatch the message
+ msgpack_rpc_dispatch(req, res);
+}
+
+void msgpack_rpc_error(char *msg, msgpack_packer *res)
+{
+ size_t len = strlen(msg);
+
+ // error message
+ msgpack_pack_raw(res, len);
+ msgpack_pack_raw_body(res, msg, len);
+ // Nil result
+ msgpack_pack_nil(res);
+}
+
+bool msgpack_rpc_to_bool(msgpack_object *obj, bool *arg)
+{
+ *arg = obj->via.boolean;
+ return obj->type == MSGPACK_OBJECT_BOOLEAN;
+}
+
+bool msgpack_rpc_to_int64_t(msgpack_object *obj, int64_t *arg)
+{
+ *arg = obj->via.i64;
+ return obj->type == MSGPACK_OBJECT_POSITIVE_INTEGER
+ || obj->type == MSGPACK_OBJECT_NEGATIVE_INTEGER;
+}
+
+bool msgpack_rpc_to_double(msgpack_object *obj, double *arg)
+{
+ *arg = obj->via.dec;
+ return obj->type == MSGPACK_OBJECT_DOUBLE;
+}
+
+bool msgpack_rpc_to_string(msgpack_object *obj, String *arg)
+{
+ arg->data = (char *)obj->via.raw.ptr;
+ arg->size = obj->via.raw.size;
+ return obj->type == MSGPACK_OBJECT_RAW;
+}
+
+bool msgpack_rpc_to_buffer(msgpack_object *obj, Buffer *arg)
+{
+ return msgpack_rpc_to_uint16_t(obj, arg);
+}
+
+bool msgpack_rpc_to_window(msgpack_object *obj, Window *arg)
+{
+ return msgpack_rpc_to_uint16_t(obj, arg);
+}
+
+bool msgpack_rpc_to_tabpage(msgpack_object *obj, Tabpage *arg)
+{
+ return msgpack_rpc_to_uint16_t(obj, arg);
+}
+
+bool msgpack_rpc_to_object(msgpack_object *obj, Object *arg)
+{
+ switch (obj->type) {
+ case MSGPACK_OBJECT_NIL:
+ arg->type = kObjectTypeNil;
+ return true;
+
+ case MSGPACK_OBJECT_BOOLEAN:
+ arg->type = kObjectTypeBool;
+ return msgpack_rpc_to_bool(obj, &arg->data.boolean);
+
+ case MSGPACK_OBJECT_POSITIVE_INTEGER:
+ case MSGPACK_OBJECT_NEGATIVE_INTEGER:
+ arg->type = kObjectTypeInt;
+ return msgpack_rpc_to_int64_t(obj, &arg->data.integer);
+
+ case MSGPACK_OBJECT_DOUBLE:
+ arg->type = kObjectTypeFloat;
+ return msgpack_rpc_to_double(obj, &arg->data.floating_point);
+
+ case MSGPACK_OBJECT_RAW:
+ arg->type = kObjectTypeString;
+ return msgpack_rpc_to_string(obj, &arg->data.string);
+
+ case MSGPACK_OBJECT_ARRAY:
+ arg->type = kObjectTypeArray;
+ return msgpack_rpc_to_array(obj, &arg->data.array);
+
+ case MSGPACK_OBJECT_MAP:
+ arg->type = kObjectTypeDictionary;
+ return msgpack_rpc_to_dictionary(obj, &arg->data.dictionary);
+
+ default:
+ return false;
+ }
+}
+
+bool msgpack_rpc_to_stringarray(msgpack_object *obj, StringArray *arg)
+{
+ if (obj->type != MSGPACK_OBJECT_ARRAY) {
+ return false;
+ }
+
+ arg->size = obj->via.array.size;
+ arg->items = xcalloc(obj->via.array.size, sizeof(String));
+
+ for (uint32_t i = 0; i < obj->via.array.size; i++) {
+ if (!msgpack_rpc_to_string(obj->via.array.ptr + i, &arg->items[i])) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+bool msgpack_rpc_to_position(msgpack_object *obj, Position *arg)
+{
+ // positions are represented by integer arrays of size 2
+ if (obj->type != MSGPACK_OBJECT_ARRAY
+ || obj->via.array.size != 2
+ || obj->via.array.ptr[0].type != MSGPACK_OBJECT_POSITIVE_INTEGER
+ || obj->via.array.ptr[1].type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
+ return false;
+ }
+
+ arg->row = obj->via.array.ptr[0].via.u64;
+ arg->col = obj->via.array.ptr[1].via.u64;
+
+ return true;
+}
+
+
+bool msgpack_rpc_to_array(msgpack_object *obj, Array *arg)
+{
+ if (obj->type != MSGPACK_OBJECT_ARRAY) {
+ return false;
+ }
+
+ arg->size = obj->via.array.size;
+ arg->items = xcalloc(obj->via.array.size, sizeof(Object));
+
+ for (uint32_t i = 0; i < obj->via.array.size; i++) {
+ if (!msgpack_rpc_to_object(obj->via.array.ptr + i, &arg->items[i])) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+bool msgpack_rpc_to_dictionary(msgpack_object *obj, Dictionary *arg)
+{
+ if (obj->type != MSGPACK_OBJECT_MAP) {
+ return false;
+ }
+
+ arg->size = obj->via.array.size;
+ arg->items = xcalloc(obj->via.map.size, sizeof(KeyValuePair));
+
+
+ for (uint32_t i = 0; i < obj->via.map.size; i++) {
+ if (!msgpack_rpc_to_string(&obj->via.map.ptr[i].key,
+ &arg->items[i].key)) {
+ return false;
+ }
+
+ if (!msgpack_rpc_to_object(&obj->via.map.ptr[i].val,
+ &arg->items[i].value)) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+void msgpack_rpc_from_bool(bool result, msgpack_packer *res)
+{
+ if (result) {
+ msgpack_pack_true(res);
+ } else {
+ msgpack_pack_false(res);
+ }
+}
+
+void msgpack_rpc_from_int64_t(int64_t result, msgpack_packer *res)
+{
+ msgpack_pack_int64(res, result);
+}
+
+void msgpack_rpc_from_uint64_t(uint64_t result, msgpack_packer *res)
+{
+ msgpack_pack_uint64(res, result);
+}
+
+void msgpack_rpc_from_double(double result, msgpack_packer *res)
+{
+ msgpack_pack_double(res, result);
+}
+
+void msgpack_rpc_from_string(String result, msgpack_packer *res)
+{
+ msgpack_pack_raw(res, result.size);
+ msgpack_pack_raw_body(res, result.data, result.size);
+}
+
+void msgpack_rpc_from_buffer(Buffer result, msgpack_packer *res)
+{
+ msgpack_rpc_from_uint64_t(result, res);
+}
+
+void msgpack_rpc_from_window(Window result, msgpack_packer *res)
+{
+ msgpack_rpc_from_uint64_t(result, res);
+}
+
+void msgpack_rpc_from_tabpage(Tabpage result, msgpack_packer *res)
+{
+ msgpack_rpc_from_uint64_t(result, res);
+}
+
+void msgpack_rpc_from_object(Object result, msgpack_packer *res)
+{
+
+ switch (result.type) {
+ case kObjectTypeNil:
+ msgpack_pack_nil(res);
+ break;
+
+ case kObjectTypeBool:
+ msgpack_rpc_from_bool(result.data.boolean, res);
+ break;
+
+ case kObjectTypeInt:
+ msgpack_rpc_from_int64_t(result.data.integer, res);
+ break;
+
+ case kObjectTypeFloat:
+ msgpack_rpc_from_double(result.data.floating_point, res);
+ break;
+
+ case kObjectTypeString:
+ msgpack_rpc_from_string(result.data.string, res);
+ break;
+
+ case kObjectTypeArray:
+ msgpack_rpc_from_array(result.data.array, res);
+ break;
+
+ case kObjectTypeDictionary:
+ msgpack_rpc_from_dictionary(result.data.dictionary, res);
+ break;
+
+ default:
+ abort();
+ }
+}
+
+void msgpack_rpc_from_stringarray(StringArray result, msgpack_packer *res)
+{
+ msgpack_pack_array(res, result.size);
+
+ for (size_t i = 0; i < result.size; i++) {
+ msgpack_rpc_from_string(result.items[i], res);
+ }
+}
+
+void msgpack_rpc_from_position(Position result, msgpack_packer *res)
+{
+ msgpack_pack_array(res, 2);;
+ msgpack_pack_uint16(res, result.row);
+ msgpack_pack_uint16(res, result.col);
+}
+
+void msgpack_rpc_from_array(Array result, msgpack_packer *res)
+{
+ msgpack_pack_array(res, result.size);
+
+ for (size_t i = 0; i < result.size; i++) {
+ msgpack_rpc_from_object(result.items[i], res);
+ }
+}
+
+void msgpack_rpc_from_dictionary(Dictionary result, msgpack_packer *res)
+{
+ msgpack_pack_map(res, result.size);
+
+ for (size_t i = 0; i < result.size; i++) {
+ msgpack_rpc_from_string(result.items[i].key, res);
+ msgpack_rpc_from_object(result.items[i].value, res);
+ }
+}
+
+void msgpack_rpc_free_object(Object value)
+{
+ switch (value.type) {
+ case kObjectTypeNil:
+ case kObjectTypeBool:
+ case kObjectTypeInt:
+ case kObjectTypeFloat:
+ break;
+
+ case kObjectTypeString:
+ msgpack_rpc_free_string(value.data.string);
+ break;
+
+ case kObjectTypeArray:
+ msgpack_rpc_free_array(value.data.array);
+ break;
+
+ case kObjectTypeDictionary:
+ msgpack_rpc_free_dictionary(value.data.dictionary);
+ break;
+
+ default:
+ abort();
+ }
+}
+
+void msgpack_rpc_free_stringarray(StringArray value) {
+ for (uint32_t i = 0; i < value.size; i++) {
+ msgpack_rpc_free_string(value.items[i]);
+ }
+
+ free(value.items);
+}
+
+void msgpack_rpc_free_array(Array value)
+{
+ for (uint32_t i = 0; i < value.size; i++) {
+ msgpack_rpc_free_object(value.items[i]);
+ }
+
+ free(value.items);
+}
+
+void msgpack_rpc_free_dictionary(Dictionary value)
+{
+ for (uint32_t i = 0; i < value.size; i++) {
+ msgpack_rpc_free_string(value.items[i].key);
+ msgpack_rpc_free_object(value.items[i].value);
+ }
+
+ free(value.items);
+}
+
+static bool msgpack_rpc_to_uint16_t(msgpack_object *obj, uint16_t *arg)
+{
+ *arg = obj->via.u64;
+ return obj->type == MSGPACK_OBJECT_POSITIVE_INTEGER;
+}
+
diff --git a/src/os/msgpack_rpc.h b/src/os/msgpack_rpc.h
new file mode 100644
index 0000000000..8d2c6e767b
--- /dev/null
+++ b/src/os/msgpack_rpc.h
@@ -0,0 +1,107 @@
+#ifndef NEOVIM_MSGPACK_RPC_H
+#define NEOVIM_MSGPACK_RPC_H
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#include <msgpack.h>
+
+#include "api/defs.h"
+
+/// Validates the basic structure of the msgpack-rpc call and fills `res`
+/// with the basic response structure.
+///
+/// @param req The parsed request object
+/// @param res A packer that contains the response
+void msgpack_rpc_call(msgpack_object *req, msgpack_packer *res);
+
+/// Dispatches to the actual API function after basic payload validation by
+/// `msgpack_rpc_call`. It is responsible for validating/converting arguments
+/// to C types, and converting the return value back to msgpack types.
+/// The implementation is generated at compile time with metadata extracted
+/// from the api/*.h headers,
+///
+/// @param req The parsed request object
+/// @param res A packer that contains the response
+void msgpack_rpc_dispatch(msgpack_object *req, msgpack_packer *res);
+
+/// Finishes the msgpack-rpc call with an error message.
+///
+/// @param msg The error message
+/// @param res A packer that contains the response
+void msgpack_rpc_error(char *msg, msgpack_packer *res);
+
+/// Functions for validating and converting from msgpack types to C types.
+/// These are used by `msgpack_rpc_dispatch` to validate and convert each
+/// argument.
+///
+/// @param obj The object to convert
+/// @param[out] arg A pointer to the avalue
+/// @return true if the convertion succeeded, false otherwise
+bool msgpack_rpc_to_bool(msgpack_object *obj, bool *arg);
+bool msgpack_rpc_to_int64_t(msgpack_object *obj, int64_t *arg);
+bool msgpack_rpc_to_double(msgpack_object *obj, double *arg);
+bool msgpack_rpc_to_position(msgpack_object *obj, Position *arg);
+bool msgpack_rpc_to_string(msgpack_object *obj, String *arg);
+bool msgpack_rpc_to_buffer(msgpack_object *obj, Buffer *arg);
+bool msgpack_rpc_to_window(msgpack_object *obj, Window *arg);
+bool msgpack_rpc_to_tabpage(msgpack_object *obj, Tabpage *arg);
+bool msgpack_rpc_to_object(msgpack_object *obj, Object *arg);
+bool msgpack_rpc_to_stringarray(msgpack_object *obj, StringArray *arg);
+bool msgpack_rpc_to_array(msgpack_object *obj, Array *arg);
+bool msgpack_rpc_to_dictionary(msgpack_object *obj, Dictionary *arg);
+
+/// Functions for converting from C types to msgpack types.
+/// These are used by `msgpack_rpc_dispatch` to convert return values
+/// from the API
+///
+/// @param result A pointer to the result
+/// @param res A packer that contains the response
+void msgpack_rpc_from_bool(bool result, msgpack_packer *res);
+void msgpack_rpc_from_int64_t(int64_t result, msgpack_packer *res);
+void msgpack_rpc_from_double(double result, msgpack_packer *res);
+void msgpack_rpc_from_position(Position result, msgpack_packer *res);
+void msgpack_rpc_from_string(String result, msgpack_packer *res);
+void msgpack_rpc_from_buffer(Buffer result, msgpack_packer *res);
+void msgpack_rpc_from_window(Window result, msgpack_packer *res);
+void msgpack_rpc_from_tabpage(Tabpage result, msgpack_packer *res);
+void msgpack_rpc_from_object(Object result, msgpack_packer *res);
+void msgpack_rpc_from_stringarray(StringArray result, msgpack_packer *res);
+void msgpack_rpc_from_array(Array result, msgpack_packer *res);
+void msgpack_rpc_from_dictionary(Dictionary result, msgpack_packer *res);
+
+/// Helpers for initializing types that may be freed later
+#define msgpack_rpc_init_bool
+#define msgpack_rpc_init_int64_t
+#define msgpack_rpc_init_double
+#define msgpack_rpc_init_position
+#define msgpack_rpc_init_string
+#define msgpack_rpc_init_buffer
+#define msgpack_rpc_init_window
+#define msgpack_rpc_init_tabpage
+#define msgpack_rpc_init_object = {.type = kObjectTypeNil}
+#define msgpack_rpc_init_stringarray = {.items = NULL, .size = 0}
+#define msgpack_rpc_init_array = {.items = NULL, .size = 0}
+#define msgpack_rpc_init_dictionary = {.items = NULL, .size = 0}
+
+/// Helpers for freeing arguments/return value
+///
+/// @param value The value to be freed
+#define msgpack_rpc_free_bool(value)
+#define msgpack_rpc_free_int64_t(value)
+#define msgpack_rpc_free_double(value)
+#define msgpack_rpc_free_position(value)
+// Strings are not copied from msgpack and so don't need to be freed(they
+// probably "live" in the msgpack streaming buffer)
+#define msgpack_rpc_free_string(value)
+#define msgpack_rpc_free_buffer(value)
+#define msgpack_rpc_free_window(value)
+#define msgpack_rpc_free_tabpage(value)
+void msgpack_rpc_free_object(Object value);
+void msgpack_rpc_free_stringarray(StringArray value);
+void msgpack_rpc_free_array(Array value);
+void msgpack_rpc_free_dictionary(Dictionary value);
+
+
+#endif // NEOVIM_MSGPACK_RPC_H
+