aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/os/msgpack_rpc.c
blob: 3ce0d7b63c434f8cce4031597f951978d30fa559 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <msgpack.h>

#include "nvim/os/msgpack_rpc.h"
#include "nvim/vim.h"
#include "nvim/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;
}