aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/eval/encode.c
blob: 03d48625506abdac7badfbbc1001fc7ed9bc3205 (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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com

/// @file encode.c
///
/// File containing functions for encoding and decoding VimL values.
///
/// Split out from eval.c.

#include <assert.h>
#include <inttypes.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include "klib/kvec.h"
#include "msgpack/pack.h"
#include "nvim/ascii.h"
#include "nvim/eval.h"
#include "nvim/eval/encode.h"
#include "nvim/eval/typval.h"
#include "nvim/eval/typval_encode.h"
#include "nvim/garray.h"
#include "nvim/gettext.h"
#include "nvim/hashtab.h"
#include "nvim/macros.h"
#include "nvim/math.h"
#include "nvim/mbyte.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/strings.h"
#include "nvim/types.h"
#include "nvim/vim.h"  // For _()

const char *const encode_bool_var_names[] = {
  [kBoolVarTrue] = "v:true",
  [kBoolVarFalse] = "v:false",
};

const char *const encode_special_var_names[] = {
  [kSpecialVarNull] = "v:null",
};

#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "eval/encode.c.generated.h"
#endif

/// Msgpack callback for writing to a Blob
int encode_blob_write(void *const data, const char *const buf, const size_t len)
  FUNC_ATTR_NONNULL_ARG(1)
{
  ga_concat_len(&((blob_T *)data)->bv_ga, buf, len);
  return (int)len;
}

/// Msgpack callback for writing to readfile()-style list
int encode_list_write(void *const data, const char *const buf, const size_t len)
  FUNC_ATTR_NONNULL_ARG(1)
{
  if (len == 0) {
    return 0;
  }
  list_T *const list = (list_T *)data;
  const char *const end = buf + len;
  const char *line_end = buf;
  listitem_T *li = tv_list_last(list);

  // Continue the last list element
  if (li != NULL) {
    line_end = xmemscan(buf, NL, len);
    if (line_end != buf) {
      const size_t line_length = (size_t)(line_end - buf);
      char *str = TV_LIST_ITEM_TV(li)->vval.v_string;
      const size_t li_len = (str == NULL ? 0 : strlen(str));
      TV_LIST_ITEM_TV(li)->vval.v_string = xrealloc(str, li_len + line_length + 1);
      str = TV_LIST_ITEM_TV(li)->vval.v_string + li_len;
      memcpy(str, buf, line_length);
      str[line_length] = 0;
      memchrsub(str, NUL, NL, line_length);
    }
    line_end++;
  }

  while (line_end < end) {
    const char *line_start = line_end;
    line_end = xmemscan(line_start, NL, (size_t)(end - line_start));
    char *str = NULL;
    if (line_end != line_start) {
      const size_t line_length = (size_t)(line_end - line_start);
      str = xmemdupz(line_start, line_length);
      memchrsub(str, NUL, NL, line_length);
    }
    tv_list_append_allocated_string(list, str);
    line_end++;
  }
  if (line_end == end) {
    tv_list_append_allocated_string(list, NULL);
  }
  return 0;
}

/// Abort conversion to string after a recursion error.
static bool did_echo_string_emsg = false;

/// Show a error message when converting to msgpack value
///
/// @param[in]  msg  Error message to dump. Must contain exactly two %s that
///                  will be replaced with what was being dumped: first with
///                  something like “F” or “function argument”, second with path
///                  to the failed value.
/// @param[in]  mpstack  Path to the failed value.
/// @param[in]  objname  Dumped object name.
///
/// @return FAIL.
static int conv_error(const char *const msg, const MPConvStack *const mpstack,
                      const char *const objname)
  FUNC_ATTR_NONNULL_ALL
{
  garray_T msg_ga;
  ga_init(&msg_ga, (int)sizeof(char), 80);
  const char *const key_msg = _("key %s");
  const char *const key_pair_msg = _("key %s at index %i from special map");
  const char *const idx_msg = _("index %i");
  const char *const partial_arg_msg = _("partial");
  const char *const partial_arg_i_msg = _("argument %i");
  const char *const partial_self_msg = _("partial self dictionary");
  for (size_t i = 0; i < kv_size(*mpstack); i++) {
    if (i != 0) {
      ga_concat(&msg_ga, ", ");
    }
    MPConvStackVal v = kv_A(*mpstack, i);
    switch (v.type) {
    case kMPConvDict: {
      typval_T key_tv = {
        .v_type = VAR_STRING,
        .vval = { .v_string =
                    (v.data.d.hi ==
                     NULL ? v.data.d.dict->dv_hashtab.ht_array : (v.data.d.hi -
                                                                  1))->hi_key },
      };
      char *const key = encode_tv2string(&key_tv, NULL);
      vim_snprintf(IObuff, IOSIZE, key_msg, key);
      xfree(key);
      ga_concat(&msg_ga, IObuff);
      break;
    }
    case kMPConvPairs:
    case kMPConvList: {
      const int idx = (v.data.l.li == tv_list_first(v.data.l.list)
                       ? 0
                       : (v.data.l.li == NULL
                          ? tv_list_len(v.data.l.list) - 1
                          : (int)tv_list_idx_of_item(v.data.l.list,
                                                     TV_LIST_ITEM_PREV(v.data.l.list,
                                                                       v.data.l.li))));
      const listitem_T *const li = (v.data.l.li == NULL
                                    ? tv_list_last(v.data.l.list)
                                    : TV_LIST_ITEM_PREV(v.data.l.list,
                                                        v.data.l.li));
      if (v.type == kMPConvList
          || li == NULL
          || (TV_LIST_ITEM_TV(li)->v_type != VAR_LIST
              && tv_list_len(TV_LIST_ITEM_TV(li)->vval.v_list) <= 0)) {
        vim_snprintf(IObuff, IOSIZE, idx_msg, idx);
        ga_concat(&msg_ga, IObuff);
      } else {
        assert(li != NULL);
        listitem_T *const first_item =
          tv_list_first(TV_LIST_ITEM_TV(li)->vval.v_list);
        assert(first_item != NULL);
        typval_T key_tv = *TV_LIST_ITEM_TV(first_item);
        char *const key = encode_tv2echo(&key_tv, NULL);
        vim_snprintf(IObuff, IOSIZE, key_pair_msg, key, idx);
        xfree(key);
        ga_concat(&msg_ga, IObuff);
      }
      break;
    }
    case kMPConvPartial:
      switch (v.data.p.stage) {
      case kMPConvPartialArgs:
        abort();
        break;
      case kMPConvPartialSelf:
        ga_concat(&msg_ga, partial_arg_msg);
        break;
      case kMPConvPartialEnd:
        ga_concat(&msg_ga, partial_self_msg);
        break;
      }
      break;
    case kMPConvPartialList: {
      const int idx = (int)(v.data.a.arg - v.data.a.argv) - 1;
      vim_snprintf(IObuff, IOSIZE, partial_arg_i_msg, idx);
      ga_concat(&msg_ga, IObuff);
      break;
    }
    }
  }
  semsg(msg, _(objname), (kv_size(*mpstack) == 0
                          ? _("itself")
                          : (char *)msg_ga.ga_data));
  ga_clear(&msg_ga);
  return FAIL;
}

/// Convert readfile()-style list to a char * buffer with length
///
/// @param[in]  list  Converted list.
/// @param[out]  ret_len  Resulting buffer length.
/// @param[out]  ret_buf  Allocated buffer with the result or NULL if ret_len is
///                       zero.
///
/// @return true in case of success, false in case of failure.
bool encode_vim_list_to_buf(const list_T *const list, size_t *const ret_len, char **const ret_buf)
  FUNC_ATTR_NONNULL_ARG(2, 3) FUNC_ATTR_WARN_UNUSED_RESULT
{
  size_t len = 0;
  TV_LIST_ITER_CONST(list, li, {
    if (TV_LIST_ITEM_TV(li)->v_type != VAR_STRING) {
      return false;
    }
    len++;
    if (TV_LIST_ITEM_TV(li)->vval.v_string != NULL) {
      len += strlen(TV_LIST_ITEM_TV(li)->vval.v_string);
    }
  });
  if (len) {
    len--;
  }
  *ret_len = len;
  if (len == 0) {
    *ret_buf = NULL;
    return true;
  }
  ListReaderState lrstate = encode_init_lrstate(list);
  char *const buf = xmalloc(len);
  size_t read_bytes;
  if (encode_read_from_list(&lrstate, buf, len, &read_bytes) != OK) {
    abort();
  }
  assert(len == read_bytes);
  *ret_buf = buf;
  return true;
}

/// Read bytes from list
///
/// @param[in,out]  state  Structure describing position in list from which
///                        reading should start. Is updated to reflect position
///                        at which reading ended.
/// @param[out]  buf  Buffer to write to.
/// @param[in]  nbuf  Buffer length.
/// @param[out]  read_bytes  Is set to amount of bytes read.
///
/// @return OK when reading was finished, FAIL in case of error (i.e. list item
///         was not a string), NOTDONE if reading was successful, but there are
///         more bytes to read.
int encode_read_from_list(ListReaderState *const state, char *const buf, const size_t nbuf,
                          size_t *const read_bytes)
  FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
  char *const buf_end = buf + nbuf;
  char *p = buf;
  while (p < buf_end) {
    assert(state->li_length == 0
           || TV_LIST_ITEM_TV(state->li)->vval.v_string != NULL);
    for (size_t i = state->offset; i < state->li_length && p < buf_end; i++) {
      assert(TV_LIST_ITEM_TV(state->li)->vval.v_string != NULL);
      const char ch = TV_LIST_ITEM_TV(state->li)->vval.v_string[state->offset++];
      *p++ = (char)(ch == (char)NL ? (char)NUL : ch);
    }
    if (p < buf_end) {
      state->li = TV_LIST_ITEM_NEXT(state->list, state->li);
      if (state->li == NULL) {
        *read_bytes = (size_t)(p - buf);
        return OK;
      }
      *p++ = NL;
      if (TV_LIST_ITEM_TV(state->li)->v_type != VAR_STRING) {
        *read_bytes = (size_t)(p - buf);
        return FAIL;
      }
      state->offset = 0;
      state->li_length = (TV_LIST_ITEM_TV(state->li)->vval.v_string == NULL
                          ? 0
                          : strlen(TV_LIST_ITEM_TV(state->li)->vval.v_string));
    }
  }
  *read_bytes = nbuf;
  return ((state->offset < state->li_length
           || TV_LIST_ITEM_NEXT(state->list, state->li) != NULL)
          ? NOTDONE
          : OK);
}

#define TYPVAL_ENCODE_CONV_STRING(tv, buf, len) \
  do { \
    const char *const buf_ = (const char *)(buf); \
    if ((buf) == NULL) { \
      ga_concat(gap, "''"); \
    } else { \
      const size_t len_ = (len); \
      ga_grow(gap, (int)(2 + len_ + memcnt(buf_, '\'', len_))); \
      ga_append(gap, '\''); \
      for (size_t i_ = 0; i_ < len_; i_++) { \
        if (buf_[i_] == '\'') { \
          ga_append(gap, '\''); \
        } \
        ga_append(gap, buf_[i_]); \
      } \
      ga_append(gap, '\''); \
    } \
  } while (0)

#define TYPVAL_ENCODE_CONV_STR_STRING(tv, buf, len) \
  TYPVAL_ENCODE_CONV_STRING(tv, buf, len)

#define TYPVAL_ENCODE_CONV_EXT_STRING(tv, buf, len, type)

#define TYPVAL_ENCODE_CONV_BLOB(tv, blob, len) \
  do { \
    const blob_T *const blob_ = (blob); \
    const int len_ = (len); \
    if (len_ == 0) { \
      ga_concat(gap, "0z"); \
    } else { \
      /* Allocate space for "0z", the two hex chars per byte, and a */ \
      /* "." separator after every eight hex chars. */ \
      /* Example: "0z00112233.44556677.8899" */ \
      ga_grow(gap, 2 + 2 * len_ + (len_ - 1) / 4); \
      ga_concat(gap, "0z"); \
      char numbuf[NUMBUFLEN]; \
      for (int i_ = 0; i_ < len_; i_++) { \
        if (i_ > 0 && (i_ & 3) == 0) { \
          ga_append(gap, '.'); \
        } \
        vim_snprintf((char *)numbuf, ARRAY_SIZE(numbuf), "%02X", \
                     (int)tv_blob_get(blob_, i_)); \
        ga_concat(gap, numbuf); \
      } \
    } \
  } while (0)

#define TYPVAL_ENCODE_CONV_NUMBER(tv, num) \
  do { \
    char numbuf[NUMBUFLEN]; \
    vim_snprintf(numbuf, ARRAY_SIZE(numbuf), "%" PRId64, (int64_t)(num)); \
    ga_concat(gap, numbuf); \
  } while (0)

#define TYPVAL_ENCODE_CONV_FLOAT(tv, flt) \
  do { \
    const float_T flt_ = (flt); \
    switch (xfpclassify(flt_)) { \
    case FP_NAN: { \
        ga_concat(gap, "str2float('nan')"); \
        break; \
    } \
    case FP_INFINITE: { \
        if (flt_ < 0) { \
          ga_append(gap, '-'); \
        } \
        ga_concat(gap, "str2float('inf')"); \
        break; \
    } \
    default: { \
        char numbuf[NUMBUFLEN]; \
        vim_snprintf(numbuf, ARRAY_SIZE(numbuf), "%g", flt_); \
        ga_concat(gap, numbuf); \
    } \
    } \
  } while (0)

#define TYPVAL_ENCODE_CONV_FUNC_START(tv, fun) \
  do { \
    const char *const fun_ = (const char *)(fun); \
    if (fun_ == NULL) { \
      internal_error("string(): NULL function name"); \
      ga_concat(gap, "function(NULL"); \
    } else { \
      ga_concat(gap, "function("); \
      TYPVAL_ENCODE_CONV_STRING(tv, fun_, strlen(fun_)); \
    } \
  } while (0)

#define TYPVAL_ENCODE_CONV_FUNC_BEFORE_ARGS(tv, len) \
  do { \
    if ((len) != 0) { \
      ga_concat(gap, ", "); \
    } \
  } while (0)

#define TYPVAL_ENCODE_CONV_FUNC_BEFORE_SELF(tv, len) \
  do { \
    if ((ptrdiff_t)(len) != -1) { \
      ga_concat(gap, ", "); \
    } \
  } while (0)

#define TYPVAL_ENCODE_CONV_FUNC_END(tv) \
  ga_append(gap, ')')

#define TYPVAL_ENCODE_CONV_EMPTY_LIST(tv) \
  ga_concat(gap, "[]")

#define TYPVAL_ENCODE_CONV_LIST_START(tv, len) \
  ga_append(gap, '[')

#define TYPVAL_ENCODE_CONV_REAL_LIST_AFTER_START(tv, mpsv)

#define TYPVAL_ENCODE_CONV_EMPTY_DICT(tv, dict) \
  ga_concat(gap, "{}")

#define TYPVAL_ENCODE_CONV_NIL(tv) \
  ga_concat(gap, "v:null")

#define TYPVAL_ENCODE_CONV_BOOL(tv, num) \
  ga_concat(gap, ((num)? "v:true": "v:false"))

#define TYPVAL_ENCODE_CONV_UNSIGNED_NUMBER(tv, num)

#define TYPVAL_ENCODE_CONV_DICT_START(tv, dict, len) \
  ga_append(gap, '{')

#define TYPVAL_ENCODE_CONV_REAL_DICT_AFTER_START(tv, dict, mpsv)

#define TYPVAL_ENCODE_CONV_DICT_END(tv, dict) \
  ga_append(gap, '}')

#define TYPVAL_ENCODE_CONV_DICT_AFTER_KEY(tv, dict) \
  ga_concat(gap, ": ")

#define TYPVAL_ENCODE_CONV_DICT_BETWEEN_ITEMS(tv, dict) \
  ga_concat(gap, ", ")

#define TYPVAL_ENCODE_SPECIAL_DICT_KEY_CHECK(label, key)

#define TYPVAL_ENCODE_CONV_LIST_END(tv) \
  ga_append(gap, ']')

#define TYPVAL_ENCODE_CONV_LIST_BETWEEN_ITEMS(tv) \
  TYPVAL_ENCODE_CONV_DICT_BETWEEN_ITEMS(tv, NULL)

#define TYPVAL_ENCODE_CONV_RECURSE(val, conv_type) \
  do { \
    if (!did_echo_string_emsg) { \
      /* Only give this message once for a recursive call to avoid */ \
      /* flooding the user with errors. */ \
      did_echo_string_emsg = true; \
      emsg(_("E724: unable to correctly dump variable " \
             "with self-referencing container")); \
    } \
    char ebuf[NUMBUFLEN + 7]; \
    size_t backref = 0; \
    for (; backref < kv_size(*mpstack); backref++) { \
      const MPConvStackVal mpval = kv_A(*mpstack, backref); \
      if (mpval.type == (conv_type)) { \
        if ((conv_type) == kMPConvDict) { \
          if ((void *)mpval.data.d.dict == (void *)(val)) { \
            break; \
          } \
        } else if ((conv_type) == kMPConvList) { \
          if ((void *)mpval.data.l.list == (void *)(val)) { \
            break; \
          } \
        } \
      } \
    } \
    vim_snprintf(ebuf, ARRAY_SIZE(ebuf), "{E724@%zu}", backref); \
    ga_concat(gap, &ebuf[0]); \
  } while (0)

#define TYPVAL_ENCODE_ALLOW_SPECIALS false

#define TYPVAL_ENCODE_SCOPE static
#define TYPVAL_ENCODE_NAME string
#define TYPVAL_ENCODE_FIRST_ARG_TYPE garray_T *const
#define TYPVAL_ENCODE_FIRST_ARG_NAME gap
#include "nvim/eval/typval_encode.c.h"
#undef TYPVAL_ENCODE_SCOPE
#undef TYPVAL_ENCODE_NAME
#undef TYPVAL_ENCODE_FIRST_ARG_TYPE
#undef TYPVAL_ENCODE_FIRST_ARG_NAME

#undef TYPVAL_ENCODE_CONV_RECURSE
#define TYPVAL_ENCODE_CONV_RECURSE(val, conv_type) \
  do { \
    char ebuf[NUMBUFLEN + 7]; \
    size_t backref = 0; \
    for (; backref < kv_size(*mpstack); backref++) { \
      const MPConvStackVal mpval = kv_A(*mpstack, backref); \
      if (mpval.type == (conv_type)) { \
        if ((conv_type) == kMPConvDict) { \
          if ((void *)mpval.data.d.dict == (void *)(val)) { \
            break; \
          } \
        } else if ((conv_type) == kMPConvList) { \
          if ((void *)mpval.data.l.list == (void *)(val)) { \
            break; \
          } \
        } \
      } \
    } \
    if ((conv_type) == kMPConvDict) { \
      vim_snprintf(ebuf, ARRAY_SIZE(ebuf), "{...@%zu}", backref); \
    } else { \
      vim_snprintf(ebuf, ARRAY_SIZE(ebuf), "[...@%zu]", backref); \
    } \
    ga_concat(gap, &ebuf[0]); \
    return OK; \
  } while (0)

#define TYPVAL_ENCODE_SCOPE
#define TYPVAL_ENCODE_NAME echo
#define TYPVAL_ENCODE_FIRST_ARG_TYPE garray_T *const
#define TYPVAL_ENCODE_FIRST_ARG_NAME gap
#include "nvim/eval/typval_encode.c.h"
#undef TYPVAL_ENCODE_SCOPE
#undef TYPVAL_ENCODE_NAME
#undef TYPVAL_ENCODE_FIRST_ARG_TYPE
#undef TYPVAL_ENCODE_FIRST_ARG_NAME

#undef TYPVAL_ENCODE_CONV_RECURSE
#define TYPVAL_ENCODE_CONV_RECURSE(val, conv_type) \
  do { \
    if (!did_echo_string_emsg) { \
      /* Only give this message once for a recursive call to avoid */ \
      /* flooding the user with errors. */ \
      did_echo_string_emsg = true; \
      emsg(_("E724: unable to correctly dump variable " \
             "with self-referencing container")); \
    } \
  } while (0)

#undef TYPVAL_ENCODE_ALLOW_SPECIALS
#define TYPVAL_ENCODE_ALLOW_SPECIALS true

#undef TYPVAL_ENCODE_CONV_NIL
#define TYPVAL_ENCODE_CONV_NIL(tv) \
  ga_concat(gap, "null")

#undef TYPVAL_ENCODE_CONV_BOOL
#define TYPVAL_ENCODE_CONV_BOOL(tv, num) \
  ga_concat(gap, ((num)? "true": "false"))

#undef TYPVAL_ENCODE_CONV_UNSIGNED_NUMBER
#define TYPVAL_ENCODE_CONV_UNSIGNED_NUMBER(tv, num) \
  do { \
    char numbuf[NUMBUFLEN]; \
    vim_snprintf(numbuf, ARRAY_SIZE(numbuf), "%" PRIu64, (num)); \
    ga_concat(gap, numbuf); \
  } while (0)

#undef TYPVAL_ENCODE_CONV_FLOAT
#define TYPVAL_ENCODE_CONV_FLOAT(tv, flt) \
  do { \
    const float_T flt_ = (flt); \
    switch (xfpclassify(flt_)) { \
    case FP_NAN: { \
        emsg(_("E474: Unable to represent NaN value in JSON")); \
        return FAIL; \
    } \
    case FP_INFINITE: { \
        emsg(_("E474: Unable to represent infinity in JSON")); \
        return FAIL; \
    } \
    default: { \
        char numbuf[NUMBUFLEN]; \
        vim_snprintf(numbuf, ARRAY_SIZE(numbuf), "%g", flt_); \
        ga_concat(gap, numbuf); \
        break; \
    } \
    } \
  } while (0)

/// Escape sequences used in JSON
static const char escapes[][3] = {
  [BS] = "\\b",
  [TAB] = "\\t",
  [NL] = "\\n",
  [CAR] = "\\r",
  ['"'] = "\\\"",
  ['\\'] = "\\\\",
  [FF] = "\\f",
};

static const char xdigits[] = "0123456789ABCDEF";

/// Convert given string to JSON string
///
/// @param[out]  gap  Garray where result will be saved.
/// @param[in]  buf  Converted string.
/// @param[in]  len  Converted string length.
///
/// @return OK in case of success, FAIL otherwise.
static inline int convert_to_json_string(garray_T *const gap, const char *const buf,
                                         const size_t len)
  FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_ALWAYS_INLINE
{
  const char *utf_buf = buf;
  if (utf_buf == NULL) {
    ga_concat(gap, "\"\"");
  } else {
    size_t utf_len = len;
    char *tofree = NULL;
    size_t str_len = 0;
    // Encode character as \uNNNN if
    // 1. It is an ASCII control character (0x0 .. 0x1F; 0x7F not
    //    utf_printable and thus not checked specially).
    // 2. Code point is not printable according to utf_printable().
    // This is done to make resulting values displayable on screen also not from
    // Neovim.
#define ENCODE_RAW(ch) \
  ((ch) >= 0x20 && utf_printable(ch))
    for (size_t i = 0; i < utf_len;) {
      const int ch = utf_ptr2char(utf_buf + i);
      const size_t shift = (ch == 0 ? 1 : ((size_t)utf_ptr2len(utf_buf + i)));
      assert(shift > 0);
      i += shift;
      switch (ch) {
      case BS:
      case TAB:
      case NL:
      case FF:
      case CAR:
      case '"':
      case '\\':
        str_len += 2;
        break;
      default:
        if (ch > 0x7F && shift == 1) {
          semsg(_("E474: String \"%.*s\" contains byte that does not start "
                  "any UTF-8 character"),
                (int)(utf_len - (i - shift)), utf_buf + i - shift);
          xfree(tofree);
          return FAIL;
        } else if ((SURROGATE_HI_START <= ch && ch <= SURROGATE_HI_END)
                   || (SURROGATE_LO_START <= ch && ch <= SURROGATE_LO_END)) {
          semsg(_("E474: UTF-8 string contains code point which belongs "
                  "to a surrogate pair: %.*s"),
                (int)(utf_len - (i - shift)), utf_buf + i - shift);
          xfree(tofree);
          return FAIL;
        } else if (ENCODE_RAW(ch)) {
          str_len += shift;
        } else {
          str_len += ((sizeof("\\u1234") - 1)
                      * (size_t)(1 + (ch >= SURROGATE_FIRST_CHAR)));
        }
        break;
      }
    }
    ga_append(gap, '"');
    ga_grow(gap, (int)str_len);
    for (size_t i = 0; i < utf_len;) {
      const int ch = utf_ptr2char(utf_buf + i);
      const size_t shift = (ch == 0 ? 1 : ((size_t)utf_char2len(ch)));
      assert(shift > 0);
      // Is false on invalid unicode, but this should already be handled.
      assert(ch == 0 || shift == ((size_t)utf_ptr2len(utf_buf + i)));
      switch (ch) {
      case BS:
      case TAB:
      case NL:
      case FF:
      case CAR:
      case '"':
      case '\\':
        ga_concat_len(gap, escapes[ch], 2);
        break;
      default:
        if (ENCODE_RAW(ch)) {
          ga_concat_len(gap, utf_buf + i, shift);
        } else if (ch < SURROGATE_FIRST_CHAR) {
          ga_concat_len(gap, ((const char[]) {
            '\\', 'u',
            xdigits[(ch >> (4 * 3)) & 0xF],
            xdigits[(ch >> (4 * 2)) & 0xF],
            xdigits[(ch >> (4 * 1)) & 0xF],
            xdigits[(ch >> (4 * 0)) & 0xF],
          }), sizeof("\\u1234") - 1);
        } else {
          const int tmp = ch - SURROGATE_FIRST_CHAR;
          const int hi = SURROGATE_HI_START + ((tmp >> 10) & ((1 << 10) - 1));
          const int lo = SURROGATE_LO_END + ((tmp >>  0) & ((1 << 10) - 1));
          ga_concat_len(gap, ((const char[]) {
            '\\', 'u',
            xdigits[(hi >> (4 * 3)) & 0xF],
            xdigits[(hi >> (4 * 2)) & 0xF],
            xdigits[(hi >> (4 * 1)) & 0xF],
            xdigits[(hi >> (4 * 0)) & 0xF],
            '\\', 'u',
            xdigits[(lo >> (4 * 3)) & 0xF],
            xdigits[(lo >> (4 * 2)) & 0xF],
            xdigits[(lo >> (4 * 1)) & 0xF],
            xdigits[(lo >> (4 * 0)) & 0xF],
          }), (sizeof("\\u1234") - 1) * 2);
        }
        break;
      }
      i += shift;
    }
    ga_append(gap, '"');
    xfree(tofree);
  }
  return OK;
}

#undef TYPVAL_ENCODE_CONV_STRING
#define TYPVAL_ENCODE_CONV_STRING(tv, buf, len) \
  do { \
    if (convert_to_json_string(gap, (const char *)(buf), (len)) != OK) { \
      return FAIL; \
    } \
  } while (0)

#undef TYPVAL_ENCODE_CONV_EXT_STRING
#define TYPVAL_ENCODE_CONV_EXT_STRING(tv, buf, len, type) \
  do { \
    xfree(buf); \
    emsg(_("E474: Unable to convert EXT string to JSON")); \
    return FAIL; \
  } while (0)

#undef TYPVAL_ENCODE_CONV_BLOB
#define TYPVAL_ENCODE_CONV_BLOB(tv, blob, len) \
  do { \
    const blob_T *const blob_ = (blob); \
    const int len_ = (len); \
    if (len_ == 0) { \
      ga_concat(gap, "[]"); \
    } else { \
      ga_append(gap, '['); \
      char numbuf[NUMBUFLEN]; \
      for (int i_ = 0; i_ < len_; i_++) { \
        if (i_ > 0) { \
          ga_concat(gap, ", "); \
        } \
        vim_snprintf((char *)numbuf, ARRAY_SIZE(numbuf), "%d", \
                     (int)tv_blob_get(blob_, i_)); \
        ga_concat(gap, numbuf); \
      } \
      ga_append(gap, ']'); \
    } \
  } while (0)

#undef TYPVAL_ENCODE_CONV_FUNC_START
#define TYPVAL_ENCODE_CONV_FUNC_START(tv, fun) \
  return conv_error(_("E474: Error while dumping %s, %s: " \
                      "attempt to dump function reference"), \
                    mpstack, objname)

/// Check whether given key can be used in json_encode()
///
/// @param[in]  tv  Key to check.
bool encode_check_json_key(const typval_T *const tv)
  FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE
{
  if (tv->v_type == VAR_STRING) {
    return true;
  }
  if (tv->v_type != VAR_DICT) {
    return false;
  }
  const dict_T *const spdict = tv->vval.v_dict;
  if (spdict->dv_hashtab.ht_used != 2) {
    return false;
  }
  const dictitem_T *type_di;
  const dictitem_T *val_di;
  if ((type_di = tv_dict_find(spdict, S_LEN("_TYPE"))) == NULL
      || type_di->di_tv.v_type != VAR_LIST
      || (type_di->di_tv.vval.v_list != eval_msgpack_type_lists[kMPString]
          && type_di->di_tv.vval.v_list != eval_msgpack_type_lists[kMPBinary])
      || (val_di = tv_dict_find(spdict, S_LEN("_VAL"))) == NULL
      || val_di->di_tv.v_type != VAR_LIST) {
    return false;
  }
  if (val_di->di_tv.vval.v_list == NULL) {
    return true;
  }
  TV_LIST_ITER_CONST(val_di->di_tv.vval.v_list, li, {
    if (TV_LIST_ITEM_TV(li)->v_type != VAR_STRING) {
      return false;
    }
  });
  return true;
}

#undef TYPVAL_ENCODE_SPECIAL_DICT_KEY_CHECK
#define TYPVAL_ENCODE_SPECIAL_DICT_KEY_CHECK(label, key) \
  do { \
    if (!encode_check_json_key(&(key))) { \
      emsg(_("E474: Invalid key in special dictionary")); \
      goto label; \
    } \
  } while (0)

#define TYPVAL_ENCODE_SCOPE static
#define TYPVAL_ENCODE_NAME json
#define TYPVAL_ENCODE_FIRST_ARG_TYPE garray_T *const
#define TYPVAL_ENCODE_FIRST_ARG_NAME gap
#include "nvim/eval/typval_encode.c.h"
#undef TYPVAL_ENCODE_SCOPE
#undef TYPVAL_ENCODE_NAME
#undef TYPVAL_ENCODE_FIRST_ARG_TYPE
#undef TYPVAL_ENCODE_FIRST_ARG_NAME

#undef TYPVAL_ENCODE_CONV_STRING
#undef TYPVAL_ENCODE_CONV_STR_STRING
#undef TYPVAL_ENCODE_CONV_EXT_STRING
#undef TYPVAL_ENCODE_CONV_BLOB
#undef TYPVAL_ENCODE_CONV_NUMBER
#undef TYPVAL_ENCODE_CONV_FLOAT
#undef TYPVAL_ENCODE_CONV_FUNC_START
#undef TYPVAL_ENCODE_CONV_FUNC_BEFORE_ARGS
#undef TYPVAL_ENCODE_CONV_FUNC_BEFORE_SELF
#undef TYPVAL_ENCODE_CONV_FUNC_END
#undef TYPVAL_ENCODE_CONV_EMPTY_LIST
#undef TYPVAL_ENCODE_CONV_LIST_START
#undef TYPVAL_ENCODE_CONV_REAL_LIST_AFTER_START
#undef TYPVAL_ENCODE_CONV_EMPTY_DICT
#undef TYPVAL_ENCODE_CONV_NIL
#undef TYPVAL_ENCODE_CONV_BOOL
#undef TYPVAL_ENCODE_CONV_UNSIGNED_NUMBER
#undef TYPVAL_ENCODE_CONV_DICT_START
#undef TYPVAL_ENCODE_CONV_REAL_DICT_AFTER_START
#undef TYPVAL_ENCODE_CONV_DICT_END
#undef TYPVAL_ENCODE_CONV_DICT_AFTER_KEY
#undef TYPVAL_ENCODE_CONV_DICT_BETWEEN_ITEMS
#undef TYPVAL_ENCODE_SPECIAL_DICT_KEY_CHECK
#undef TYPVAL_ENCODE_CONV_LIST_END
#undef TYPVAL_ENCODE_CONV_LIST_BETWEEN_ITEMS
#undef TYPVAL_ENCODE_CONV_RECURSE
#undef TYPVAL_ENCODE_ALLOW_SPECIALS

/// Return a string with the string representation of a variable.
/// Puts quotes around strings, so that they can be parsed back by eval().
///
/// @param[in]  tv  typval_T to convert.
/// @param[out]  len  Location where length of the result will be saved.
///
/// @return String representation of the variable or NULL.
char *encode_tv2string(typval_T *tv, size_t *len)
  FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_MALLOC
{
  garray_T ga;
  ga_init(&ga, (int)sizeof(char), 80);
  const int evs_ret = encode_vim_to_string(&ga, tv,
                                           N_("encode_tv2string() argument"));
  (void)evs_ret;
  assert(evs_ret == OK);
  did_echo_string_emsg = false;
  if (len != NULL) {
    *len = (size_t)ga.ga_len;
  }
  ga_append(&ga, '\0');
  return (char *)ga.ga_data;
}

/// Return a string with the string representation of a variable.
/// Does not put quotes around strings, as ":echo" displays values.
///
/// @param[in]  tv  typval_T to convert.
/// @param[out]  len  Location where length of the result will be saved.
///
/// @return String representation of the variable or NULL.
char *encode_tv2echo(typval_T *tv, size_t *len)
  FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_MALLOC
{
  garray_T ga;
  ga_init(&ga, (int)sizeof(char), 80);
  if (tv->v_type == VAR_STRING || tv->v_type == VAR_FUNC) {
    if (tv->vval.v_string != NULL) {
      ga_concat(&ga, tv->vval.v_string);
    }
  } else {
    const int eve_ret = encode_vim_to_echo(&ga, tv, N_(":echo argument"));
    (void)eve_ret;
    assert(eve_ret == OK);
  }
  if (len != NULL) {
    *len = (size_t)ga.ga_len;
  }
  ga_append(&ga, '\0');
  return (char *)ga.ga_data;
}

/// Return a string with the string representation of a variable.
/// Puts quotes around strings, so that they can be parsed back by eval().
///
/// @param[in]  tv  typval_T to convert.
/// @param[out]  len  Location where length of the result will be saved.
///
/// @return String representation of the variable or NULL.
char *encode_tv2json(typval_T *tv, size_t *len)
  FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_MALLOC
{
  garray_T ga;
  ga_init(&ga, (int)sizeof(char), 80);
  const int evj_ret = encode_vim_to_json(&ga, tv,
                                         N_("encode_tv2json() argument"));
  if (!evj_ret) {
    ga_clear(&ga);
  }
  did_echo_string_emsg = false;
  if (len != NULL) {
    *len = (size_t)ga.ga_len;
  }
  ga_append(&ga, '\0');
  return (char *)ga.ga_data;
}

#define TYPVAL_ENCODE_CONV_STRING(tv, buf, len) \
  do { \
    if ((buf) == NULL) { \
      msgpack_pack_bin(packer, 0); \
    } else { \
      const size_t len_ = (len); \
      msgpack_pack_bin(packer, len_); \
      msgpack_pack_bin_body(packer, buf, len_); \
    } \
  } while (0)

#define TYPVAL_ENCODE_CONV_STR_STRING(tv, buf, len) \
  do { \
    if ((buf) == NULL) { \
      msgpack_pack_str(packer, 0); \
    } else { \
      const size_t len_ = (len); \
      msgpack_pack_str(packer, len_); \
      msgpack_pack_str_body(packer, buf, len_); \
    } \
  } while (0)

#define TYPVAL_ENCODE_CONV_EXT_STRING(tv, buf, len, type) \
  do { \
    if ((buf) == NULL) { \
      msgpack_pack_ext(packer, 0, (int8_t)(type)); \
    } else { \
      const size_t len_ = (len); \
      msgpack_pack_ext(packer, len_, (int8_t)(type)); \
      msgpack_pack_ext_body(packer, buf, len_); \
    } \
  } while (0)

#define TYPVAL_ENCODE_CONV_BLOB(tv, blob, len) \
  do { \
    const size_t len_ = (size_t)(len); \
    msgpack_pack_bin(packer, len_); \
    if (len_ > 0) { \
      msgpack_pack_bin_body(packer, (blob)->bv_ga.ga_data, len_); \
    } \
  } while (0)

#define TYPVAL_ENCODE_CONV_NUMBER(tv, num) \
  msgpack_pack_int64(packer, (int64_t)(num))

#define TYPVAL_ENCODE_CONV_FLOAT(tv, flt) \
  msgpack_pack_double(packer, (double)(flt))

#define TYPVAL_ENCODE_CONV_FUNC_START(tv, fun) \
  return conv_error(_("E5004: Error while dumping %s, %s: " \
                      "attempt to dump function reference"), \
                    mpstack, objname)

#define TYPVAL_ENCODE_CONV_FUNC_BEFORE_ARGS(tv, len)
#define TYPVAL_ENCODE_CONV_FUNC_BEFORE_SELF(tv, len)
#define TYPVAL_ENCODE_CONV_FUNC_END(tv)

#define TYPVAL_ENCODE_CONV_EMPTY_LIST(tv) \
  msgpack_pack_array(packer, 0)

#define TYPVAL_ENCODE_CONV_LIST_START(tv, len) \
  msgpack_pack_array(packer, (size_t)(len))

#define TYPVAL_ENCODE_CONV_REAL_LIST_AFTER_START(tv, mpsv)

#define TYPVAL_ENCODE_CONV_EMPTY_DICT(tv, dict) \
  msgpack_pack_map(packer, 0)

#define TYPVAL_ENCODE_CONV_NIL(tv) \
  msgpack_pack_nil(packer)

#define TYPVAL_ENCODE_CONV_BOOL(tv, num) \
  do { \
    if (num) { \
      msgpack_pack_true(packer); \
    } else { \
      msgpack_pack_false(packer); \
    } \
  } while (0)

#define TYPVAL_ENCODE_CONV_UNSIGNED_NUMBER(tv, num) \
  msgpack_pack_uint64(packer, (num))

#define TYPVAL_ENCODE_CONV_DICT_START(tv, dict, len) \
  msgpack_pack_map(packer, (size_t)(len))

#define TYPVAL_ENCODE_CONV_REAL_DICT_AFTER_START(tv, dict, mpsv)

#define TYPVAL_ENCODE_CONV_DICT_END(tv, dict)

#define TYPVAL_ENCODE_CONV_DICT_AFTER_KEY(tv, dict)

#define TYPVAL_ENCODE_CONV_DICT_BETWEEN_ITEMS(tv, dict)

#define TYPVAL_ENCODE_SPECIAL_DICT_KEY_CHECK(label, key)

#define TYPVAL_ENCODE_CONV_LIST_END(tv)

#define TYPVAL_ENCODE_CONV_LIST_BETWEEN_ITEMS(tv)

#define TYPVAL_ENCODE_CONV_RECURSE(val, conv_type) \
  return conv_error(_("E5005: Unable to dump %s: " \
                      "container references itself in %s"), \
                    mpstack, objname)

#define TYPVAL_ENCODE_ALLOW_SPECIALS true

#define TYPVAL_ENCODE_SCOPE
#define TYPVAL_ENCODE_NAME msgpack
#define TYPVAL_ENCODE_FIRST_ARG_TYPE msgpack_packer *const
#define TYPVAL_ENCODE_FIRST_ARG_NAME packer
#include "nvim/eval/typval_encode.c.h"
#undef TYPVAL_ENCODE_SCOPE
#undef TYPVAL_ENCODE_NAME
#undef TYPVAL_ENCODE_FIRST_ARG_TYPE
#undef TYPVAL_ENCODE_FIRST_ARG_NAME

#undef TYPVAL_ENCODE_CONV_STRING
#undef TYPVAL_ENCODE_CONV_STR_STRING
#undef TYPVAL_ENCODE_CONV_EXT_STRING
#undef TYPVAL_ENCODE_CONV_BLOB
#undef TYPVAL_ENCODE_CONV_NUMBER
#undef TYPVAL_ENCODE_CONV_FLOAT
#undef TYPVAL_ENCODE_CONV_FUNC_START
#undef TYPVAL_ENCODE_CONV_FUNC_BEFORE_ARGS
#undef TYPVAL_ENCODE_CONV_FUNC_BEFORE_SELF
#undef TYPVAL_ENCODE_CONV_FUNC_END
#undef TYPVAL_ENCODE_CONV_EMPTY_LIST
#undef TYPVAL_ENCODE_CONV_LIST_START
#undef TYPVAL_ENCODE_CONV_REAL_LIST_AFTER_START
#undef TYPVAL_ENCODE_CONV_EMPTY_DICT
#undef TYPVAL_ENCODE_CONV_NIL
#undef TYPVAL_ENCODE_CONV_BOOL
#undef TYPVAL_ENCODE_CONV_UNSIGNED_NUMBER
#undef TYPVAL_ENCODE_CONV_DICT_START
#undef TYPVAL_ENCODE_CONV_REAL_DICT_AFTER_START
#undef TYPVAL_ENCODE_CONV_DICT_END
#undef TYPVAL_ENCODE_CONV_DICT_AFTER_KEY
#undef TYPVAL_ENCODE_CONV_DICT_BETWEEN_ITEMS
#undef TYPVAL_ENCODE_SPECIAL_DICT_KEY_CHECK
#undef TYPVAL_ENCODE_CONV_LIST_END
#undef TYPVAL_ENCODE_CONV_LIST_BETWEEN_ITEMS
#undef TYPVAL_ENCODE_CONV_RECURSE
#undef TYPVAL_ENCODE_ALLOW_SPECIALS