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
|
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
#include "nvim/func_attr.h"
#include "nvim/memory.h"
#include "nvim/assert.h"
// FIXME: vim.h is not actually needed, but otherwise it states MAXPATHL is
// redefined
#include "nvim/vim.h"
#include "nvim/globals.h"
#include "nvim/message.h"
#include "nvim/eval_defs.h"
#include "nvim/ascii.h"
#include "nvim/lib/kvec.h"
#include "nvim/eval/decode.h"
#include "nvim/viml/executor/converter.h"
#include "nvim/viml/executor/executor.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "viml/executor/converter.c.generated.h"
#endif
/// Helper structure for nlua_pop_typval
typedef struct {
typval_T *tv; ///< Location where conversion result is saved.
bool container; ///< True if tv is a container.
bool special; ///< If true then tv is a _VAL part of special dictionary
///< that represents mapping.
} PopStackItem;
/// Convert lua object to VimL typval_T
///
/// Should pop exactly one value from lua stack.
///
/// @param lstate Lua state.
/// @param[out] ret_tv Where to put the result.
///
/// @return `true` in case of success, `false` in case of failure. Error is
/// reported automatically.
bool nlua_pop_typval(lua_State *lstate, typval_T *ret_tv)
{
bool ret = true;
#ifndef NDEBUG
const int initial_size = lua_gettop(lstate);
#endif
kvec_t(PopStackItem) stack = KV_INITIAL_VALUE;
kv_push(stack, ((PopStackItem) { ret_tv, false, false }));
while (ret && kv_size(stack)) {
if (!lua_checkstack(lstate, lua_gettop(lstate) + 3)) {
emsgf(_("E1502: Lua failed to grow stack to %i"), lua_gettop(lstate) + 3);
ret = false;
break;
}
PopStackItem cur = kv_pop(stack);
if (cur.container) {
if (cur.special || cur.tv->v_type == VAR_DICT) {
assert(cur.tv->v_type == (cur.special ? VAR_LIST : VAR_DICT));
if (lua_next(lstate, -2)) {
assert(lua_type(lstate, -2) == LUA_TSTRING);
size_t len;
const char *s = lua_tolstring(lstate, -2, &len);
if (cur.special) {
list_T *const kv_pair = list_alloc();
list_append_list(cur.tv->vval.v_list, kv_pair);
listitem_T *const key = listitem_alloc();
key->li_tv = decode_string(s, len, kTrue, false);
list_append(kv_pair, key);
if (key->li_tv.v_type == VAR_UNKNOWN) {
ret = false;
list_unref(kv_pair);
continue;
}
listitem_T *const val = listitem_alloc();
list_append(kv_pair, val);
kv_push(stack, cur);
cur = (PopStackItem) { &val->li_tv, false, false };
} else {
dictitem_T *const di = dictitem_alloc_len(s, len);
if (dict_add(cur.tv->vval.v_dict, di) == FAIL) {
assert(false);
}
kv_push(stack, cur);
cur = (PopStackItem) { &di->di_tv, false, false };
}
} else {
lua_pop(lstate, 1);
continue;
}
} else {
assert(cur.tv->v_type == VAR_LIST);
lua_rawgeti(lstate, -1, cur.tv->vval.v_list->lv_len + 1);
if (lua_isnil(lstate, -1)) {
lua_pop(lstate, 1);
lua_pop(lstate, 1);
continue;
}
listitem_T *li = listitem_alloc();
list_append(cur.tv->vval.v_list, li);
kv_push(stack, cur);
cur = (PopStackItem) { &li->li_tv, false, false };
}
}
assert(!cur.container);
memset(cur.tv, 0, sizeof(*cur.tv));
switch (lua_type(lstate, -1)) {
case LUA_TNIL: {
cur.tv->v_type = VAR_SPECIAL;
cur.tv->vval.v_special = kSpecialVarNull;
break;
}
case LUA_TBOOLEAN: {
cur.tv->v_type = VAR_SPECIAL;
cur.tv->vval.v_special = (lua_toboolean(lstate, -1)
? kSpecialVarTrue
: kSpecialVarFalse);
break;
}
case LUA_TSTRING: {
size_t len;
const char *s = lua_tolstring(lstate, -1, &len);
*cur.tv = decode_string(s, len, kNone, true);
if (cur.tv->v_type == VAR_UNKNOWN) {
ret = false;
}
break;
}
case LUA_TNUMBER: {
const lua_Number n = lua_tonumber(lstate, -1);
if (n > (lua_Number)VARNUMBER_MAX || n < (lua_Number)VARNUMBER_MIN
|| ((lua_Number)((varnumber_T)n)) != n) {
cur.tv->v_type = VAR_FLOAT;
cur.tv->vval.v_float = (float_T)n;
} else {
cur.tv->v_type = VAR_NUMBER;
cur.tv->vval.v_number = (varnumber_T)n;
}
break;
}
case LUA_TTABLE: {
bool has_string = false;
bool has_string_with_nul = false;
bool has_other = false;
size_t maxidx = 0;
size_t tsize = 0;
lua_pushnil(lstate);
while (lua_next(lstate, -2)) {
switch (lua_type(lstate, -2)) {
case LUA_TSTRING: {
size_t len;
const char *s = lua_tolstring(lstate, -2, &len);
if (memchr(s, NUL, len) != NULL) {
has_string_with_nul = true;
}
has_string = true;
break;
}
case LUA_TNUMBER: {
const lua_Number n = lua_tonumber(lstate, -2);
if (n > (lua_Number)SIZE_MAX || n <= 0
|| ((lua_Number)((size_t)n)) != n) {
has_other = true;
} else {
const size_t idx = (size_t)n;
if (idx > maxidx) {
maxidx = idx;
}
}
break;
}
default: {
has_other = true;
break;
}
}
tsize++;
lua_pop(lstate, 1);
}
if (tsize == 0) {
// Assuming empty list
cur.tv->v_type = VAR_LIST;
cur.tv->vval.v_list = list_alloc();
cur.tv->vval.v_list->lv_refcount++;
} else if (tsize == maxidx && !has_other && !has_string) {
// Assuming array
cur.tv->v_type = VAR_LIST;
cur.tv->vval.v_list = list_alloc();
cur.tv->vval.v_list->lv_refcount++;
cur.container = true;
kv_push(stack, cur);
} else if (has_string && !has_other && maxidx == 0) {
// Assuming dictionary
cur.special = has_string_with_nul;
if (has_string_with_nul) {
decode_create_map_special_dict(cur.tv);
assert(cur.tv->v_type = VAR_DICT);
dictitem_T *const val_di = dict_find(cur.tv->vval.v_dict,
(char_u *)"_VAL", 4);
assert(val_di != NULL);
cur.tv = &val_di->di_tv;
assert(cur.tv->v_type == VAR_LIST);
} else {
cur.tv->v_type = VAR_DICT;
cur.tv->vval.v_dict = dict_alloc();
cur.tv->vval.v_dict->dv_refcount++;
}
cur.container = true;
kv_push(stack, cur);
lua_pushnil(lstate);
} else {
EMSG(_("E5100: Cannot convert given lua table: table "
"should either have a sequence of positive integer keys "
"or contain only string keys"));
ret = false;
}
break;
}
default: {
EMSG(_("E5101: Cannot convert given lua type"));
ret = false;
break;
}
}
if (!cur.container) {
lua_pop(lstate, 1);
}
}
kv_destroy(stack);
if (!ret) {
clear_tv(ret_tv);
memset(ret_tv, 0, sizeof(*ret_tv));
lua_pop(lstate, lua_gettop(lstate) - initial_size + 1);
}
assert(lua_gettop(lstate) == initial_size - 1);
return ret;
}
#define TYPVAL_ENCODE_ALLOW_SPECIALS true
#define TYPVAL_ENCODE_CONV_NIL(tv) \
lua_pushnil(lstate)
#define TYPVAL_ENCODE_CONV_BOOL(tv, num) \
lua_pushboolean(lstate, (bool)(num))
#define TYPVAL_ENCODE_CONV_NUMBER(tv, num) \
lua_pushnumber(lstate, (lua_Number)(num))
#define TYPVAL_ENCODE_CONV_UNSIGNED_NUMBER TYPVAL_ENCODE_CONV_NUMBER
#define TYPVAL_ENCODE_CONV_FLOAT(tv, flt) \
TYPVAL_ENCODE_CONV_NUMBER(tv, flt)
#define TYPVAL_ENCODE_CONV_STRING(tv, str, len) \
lua_pushlstring(lstate, (const char *)(str), (len))
#define TYPVAL_ENCODE_CONV_STR_STRING TYPVAL_ENCODE_CONV_STRING
#define TYPVAL_ENCODE_CONV_EXT_STRING(tv, str, len, type) \
TYPVAL_ENCODE_CONV_NIL()
#define TYPVAL_ENCODE_CONV_FUNC_START(tv, fun) \
do { \
TYPVAL_ENCODE_CONV_NIL(tv); \
goto typval_encode_stop_converting_one_item; \
} while (0)
#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) \
lua_createtable(lstate, 0, 0)
#define TYPVAL_ENCODE_CONV_EMPTY_DICT(tv, dict) \
TYPVAL_ENCODE_CONV_EMPTY_LIST()
#define TYPVAL_ENCODE_CONV_LIST_START(tv, len) \
do { \
if (!lua_checkstack(lstate, lua_gettop(lstate) + 3)) { \
emsgf(_("E5102: Lua failed to grow stack to %i"), \
lua_gettop(lstate) + 3); \
return false; \
} \
lua_createtable(lstate, (int)(len), 0); \
lua_pushnumber(lstate, 1); \
} while (0)
#define TYPVAL_ENCODE_CONV_REAL_LIST_AFTER_START(tv, mpsv)
#define TYPVAL_ENCODE_CONV_LIST_BETWEEN_ITEMS(tv) \
do { \
lua_Number idx = lua_tonumber(lstate, -2); \
lua_rawset(lstate, -3); \
lua_pushnumber(lstate, idx + 1); \
} while (0)
#define TYPVAL_ENCODE_CONV_LIST_END(tv) \
lua_rawset(lstate, -3)
#define TYPVAL_ENCODE_CONV_DICT_START(tv, dict, len) \
do { \
if (!lua_checkstack(lstate, lua_gettop(lstate) + 3)) { \
emsgf(_("E5102: Lua failed to grow stack to %i"), \
lua_gettop(lstate) + 3); \
return false; \
} \
lua_createtable(lstate, 0, (int)(len)); \
} while (0)
#define TYPVAL_ENCODE_SPECIAL_DICT_KEY_CHECK(label, kv_pair)
#define TYPVAL_ENCODE_CONV_REAL_DICT_AFTER_START(tv, dict, mpsv)
#define TYPVAL_ENCODE_CONV_DICT_AFTER_KEY(tv, dict)
#define TYPVAL_ENCODE_CONV_DICT_BETWEEN_ITEMS(tv, dict) \
lua_rawset(lstate, -3)
#define TYPVAL_ENCODE_CONV_DICT_END(tv, dict) \
TYPVAL_ENCODE_CONV_DICT_BETWEEN_ITEMS(tv, dict)
#define TYPVAL_ENCODE_CONV_RECURSE(val, conv_type) \
do { \
for (size_t backref = kv_size(*mpstack); backref; backref--) { \
const MPConvStackVal mpval = kv_A(*mpstack, backref - 1); \
if (mpval.type == conv_type) { \
if (conv_type == kMPConvDict \
? (void *)mpval.data.d.dict == (void *)(val) \
: (void *)mpval.data.l.list == (void *)(val)) { \
lua_pushvalue(lstate, \
1 - ((int)((kv_size(*mpstack) - backref + 1) * 2))); \
break; \
} \
} \
} \
} while (0)
#define TYPVAL_ENCODE_SCOPE static
#define TYPVAL_ENCODE_NAME lua
#define TYPVAL_ENCODE_FIRST_ARG_TYPE lua_State *const
#define TYPVAL_ENCODE_FIRST_ARG_NAME lstate
#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_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
/// Convert VimL typval_T to lua value
///
/// Should leave single value in lua stack. May only fail if lua failed to grow
/// stack.
///
/// @param lstate Lua interpreter state.
/// @param[in] tv typval_T to convert.
///
/// @return true in case of success, false otherwise.
bool nlua_push_typval(lua_State *lstate, typval_T *const tv)
{
const int initial_size = lua_gettop(lstate);
if (!lua_checkstack(lstate, initial_size + 1)) {
emsgf(_("E1502: Lua failed to grow stack to %i"), initial_size + 4);
return false;
}
if (encode_vim_to_lua(lstate, tv, "nlua_push_typval argument") == FAIL) {
return false;
}
assert(lua_gettop(lstate) == initial_size + 1);
return true;
}
#define NLUA_PUSH_IDX(lstate, type, idx) \
do { \
STATIC_ASSERT(sizeof(type) <= sizeof(lua_Number), \
"Number sizes do not match"); \
const type src = idx; \
lua_Number tgt; \
memset(&tgt, 0, sizeof(tgt)); \
memcpy(&tgt, &src, sizeof(src)); \
lua_pushnumber(lstate, tgt); \
} while (0)
#define NLUA_POP_IDX(lstate, type, stack_idx, idx) \
do { \
STATIC_ASSERT(sizeof(type) <= sizeof(lua_Number), \
"Number sizes do not match"); \
const lua_Number src = lua_tonumber(lstate, stack_idx); \
type tgt; \
memcpy(&tgt, &src, sizeof(tgt)); \
idx = tgt; \
} while (0)
/// Push value which is a type index
///
/// Used for all “typed” tables: i.e. for all tables which represent VimL
/// values.
static inline void nlua_push_type_idx(lua_State *lstate)
FUNC_ATTR_NONNULL_ALL
{
lua_pushboolean(lstate, true);
}
/// Push value which is a locks index
///
/// Used for containers tables.
static inline void nlua_push_locks_idx(lua_State *lstate)
FUNC_ATTR_NONNULL_ALL
{
lua_pushboolean(lstate, false);
}
/// Push value which is a value index
///
/// Used for tables which represent scalar values, like float value.
static inline void nlua_push_val_idx(lua_State *lstate)
FUNC_ATTR_NONNULL_ALL
{
lua_pushnumber(lstate, (lua_Number)0);
}
/// Push type
///
/// Type is a value in vim.types table.
///
/// @param[out] lstate Lua state.
/// @param[in] type Type to push (key in vim.types table).
static inline void nlua_push_type(lua_State *lstate, const char *const type)
{
lua_getglobal(lstate, "vim");
lua_getfield(lstate, -1, "types");
lua_remove(lstate, -2);
lua_getfield(lstate, -1, type);
lua_remove(lstate, -2);
}
/// Create lua table which has an entry that determines its VimL type
///
/// @param[out] lstate Lua state.
/// @param[in] narr Number of “array” entries to be populated later.
/// @param[in] nrec Number of “dictionary” entries to be populated later.
/// @param[in] type Type of the table.
static inline void nlua_create_typed_table(lua_State *lstate,
const size_t narr,
const size_t nrec,
const char *const type)
FUNC_ATTR_NONNULL_ALL
{
lua_createtable(lstate, (int)narr, (int)(1 + nrec));
nlua_push_type_idx(lstate);
nlua_push_type(lstate, type);
lua_rawset(lstate, -3);
}
/// Convert given String to lua string
///
/// Leaves converted string on top of the stack.
void nlua_push_String(lua_State *lstate, const String s)
FUNC_ATTR_NONNULL_ALL
{
lua_pushlstring(lstate, s.data, s.size);
}
/// Convert given Integer to lua number
///
/// Leaves converted number on top of the stack.
void nlua_push_Integer(lua_State *lstate, const Integer n)
FUNC_ATTR_NONNULL_ALL
{
lua_pushnumber(lstate, (lua_Number)n);
}
/// Convert given Float to lua table
///
/// Leaves converted table on top of the stack.
void nlua_push_Float(lua_State *lstate, const Float f)
FUNC_ATTR_NONNULL_ALL
{
nlua_create_typed_table(lstate, 0, 1, "float");
nlua_push_val_idx(lstate);
lua_pushnumber(lstate, (lua_Number)f);
lua_rawset(lstate, -3);
}
/// Convert given Float to lua boolean
///
/// Leaves converted value on top of the stack.
void nlua_push_Boolean(lua_State *lstate, const Boolean b)
FUNC_ATTR_NONNULL_ALL
{
lua_pushboolean(lstate, b);
}
static inline void nlua_add_locks_table(lua_State *lstate)
{
nlua_push_locks_idx(lstate);
lua_newtable(lstate);
lua_rawset(lstate, -3);
}
/// Convert given Dictionary to lua table
///
/// Leaves converted table on top of the stack.
void nlua_push_Dictionary(lua_State *lstate, const Dictionary dict)
FUNC_ATTR_NONNULL_ALL
{
nlua_create_typed_table(lstate, 0, 1 + dict.size, "dict");
nlua_add_locks_table(lstate);
for (size_t i = 0; i < dict.size; i++) {
nlua_push_String(lstate, dict.items[i].key);
nlua_push_Object(lstate, dict.items[i].value);
lua_rawset(lstate, -3);
}
}
/// Convert given Array to lua table
///
/// Leaves converted table on top of the stack.
void nlua_push_Array(lua_State *lstate, const Array array)
FUNC_ATTR_NONNULL_ALL
{
nlua_create_typed_table(lstate, array.size, 1, "float");
nlua_add_locks_table(lstate);
for (size_t i = 0; i < array.size; i++) {
nlua_push_Object(lstate, array.items[i]);
lua_rawseti(lstate, -3, (int)i + 1);
}
}
#define GENERATE_INDEX_FUNCTION(type) \
void nlua_push_##type(lua_State *lstate, const type item) \
FUNC_ATTR_NONNULL_ALL \
{ \
NLUA_PUSH_IDX(lstate, type, item); \
}
GENERATE_INDEX_FUNCTION(Buffer)
GENERATE_INDEX_FUNCTION(Window)
GENERATE_INDEX_FUNCTION(Tabpage)
#undef GENERATE_INDEX_FUNCTION
/// Convert given Object to lua value
///
/// Leaves converted value on top of the stack.
void nlua_push_Object(lua_State *lstate, const Object obj)
FUNC_ATTR_NONNULL_ALL
{
switch (obj.type) {
case kObjectTypeNil: {
lua_pushnil(lstate);
break;
}
#define ADD_TYPE(type, data_key) \
case kObjectType##type: { \
nlua_push_##type(lstate, obj.data.data_key); \
break; \
}
ADD_TYPE(Boolean, boolean)
ADD_TYPE(Integer, integer)
ADD_TYPE(Float, floating)
ADD_TYPE(String, string)
ADD_TYPE(Array, array)
ADD_TYPE(Dictionary, dictionary)
#undef ADD_TYPE
#define ADD_REMOTE_TYPE(type) \
case kObjectType##type: { \
nlua_push_##type(lstate, (type)obj.data.integer); \
break; \
}
ADD_REMOTE_TYPE(Buffer)
ADD_REMOTE_TYPE(Window)
ADD_REMOTE_TYPE(Tabpage)
#undef ADD_REMOTE_TYPE
}
}
/// Convert lua value to string
///
/// Always pops one value from the stack.
String nlua_pop_String(lua_State *lstate, Error *err)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
String ret;
ret.data = (char *)lua_tolstring(lstate, -1, &(ret.size));
if (ret.data == NULL) {
lua_pop(lstate, 1);
set_api_error("Expected lua string", err);
return (String) { .size = 0, .data = NULL };
}
ret.data = xmemdupz(ret.data, ret.size);
lua_pop(lstate, 1);
return ret;
}
/// Convert lua value to integer
///
/// Always pops one value from the stack.
Integer nlua_pop_Integer(lua_State *lstate, Error *err)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
Integer ret = 0;
if (!lua_isnumber(lstate, -1)) {
lua_pop(lstate, 1);
set_api_error("Expected lua integer", err);
return ret;
}
ret = (Integer)lua_tonumber(lstate, -1);
lua_pop(lstate, 1);
return ret;
}
/// Convert lua value to boolean
///
/// Always pops one value from the stack.
Boolean nlua_pop_Boolean(lua_State *lstate, Error *err)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
Boolean ret = lua_toboolean(lstate, -1);
lua_pop(lstate, 1);
return ret;
}
static inline bool nlua_check_type(lua_State *lstate, Error *err,
const char *const type)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
if (lua_type(lstate, -1) != LUA_TTABLE) {
set_api_error("Expected lua table", err);
return true;
}
nlua_push_type_idx(lstate);
lua_rawget(lstate, -2);
nlua_push_type(lstate, type);
if (!lua_rawequal(lstate, -2, -1)) {
lua_pop(lstate, 2);
set_api_error("Expected lua table with float type", err);
return true;
}
lua_pop(lstate, 2);
return false;
}
/// Convert lua table to float
///
/// Always pops one value from the stack.
Float nlua_pop_Float(lua_State *lstate, Error *err)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
Float ret = 0;
if (nlua_check_type(lstate, err, "float")) {
lua_pop(lstate, 1);
return 0;
}
nlua_push_val_idx(lstate);
lua_rawget(lstate, -2);
if (!lua_isnumber(lstate, -1)) {
lua_pop(lstate, 2);
set_api_error("Value field should be lua number", err);
return ret;
}
ret = lua_tonumber(lstate, -1);
lua_pop(lstate, 2);
return ret;
}
/// Convert lua table to array
///
/// Always pops one value from the stack.
Array nlua_pop_Array(lua_State *lstate, Error *err)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
Array ret = { .size = 0, .items = NULL };
if (nlua_check_type(lstate, err, "list")) {
lua_pop(lstate, 1);
return ret;
}
for (int i = 1; ; i++, ret.size++) {
lua_rawgeti(lstate, -1, i);
if (lua_isnil(lstate, -1)) {
lua_pop(lstate, 1);
break;
}
lua_pop(lstate, 1);
}
if (ret.size == 0) {
lua_pop(lstate, 1);
return ret;
}
ret.items = xcalloc(ret.size, sizeof(*ret.items));
for (size_t i = 1; i <= ret.size; i++) {
Object val;
lua_rawgeti(lstate, -1, (int)i);
val = nlua_pop_Object(lstate, err);
if (err->set) {
ret.size = i;
lua_pop(lstate, 1);
api_free_array(ret);
return (Array) { .size = 0, .items = NULL };
}
ret.items[i - 1] = val;
}
lua_pop(lstate, 1);
return ret;
}
/// Convert lua table to dictionary
///
/// Always pops one value from the stack. Does not check whether
/// `vim.is_dict(table[type_idx])` or whether topmost value on the stack is
/// a table.
Dictionary nlua_pop_Dictionary_unchecked(lua_State *lstate, Error *err)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
Dictionary ret = { .size = 0, .items = NULL };
lua_pushnil(lstate);
while (lua_next(lstate, -2)) {
if (lua_type(lstate, -2) == LUA_TSTRING) {
ret.size++;
}
lua_pop(lstate, 1);
}
if (ret.size == 0) {
lua_pop(lstate, 1);
return ret;
}
ret.items = xcalloc(ret.size, sizeof(*ret.items));
lua_pushnil(lstate);
for (size_t i = 0; lua_next(lstate, -2);) {
// stack: dict, key, value
if (lua_type(lstate, -2) == LUA_TSTRING) {
lua_pushvalue(lstate, -2);
// stack: dict, key, value, key
ret.items[i].key = nlua_pop_String(lstate, err);
// stack: dict, key, value
if (!err->set) {
ret.items[i].value = nlua_pop_Object(lstate, err);
// stack: dict, key
} else {
lua_pop(lstate, 1);
// stack: dict, key
}
if (err->set) {
ret.size = i;
api_free_dictionary(ret);
lua_pop(lstate, 2);
// stack:
return (Dictionary) { .size = 0, .items = NULL };
}
i++;
} else {
lua_pop(lstate, 1);
// stack: dict, key
}
}
lua_pop(lstate, 1);
return ret;
}
/// Convert lua table to dictionary
///
/// Always pops one value from the stack.
Dictionary nlua_pop_Dictionary(lua_State *lstate, Error *err)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
if (nlua_check_type(lstate, err, "dict")) {
lua_pop(lstate, 1);
return (Dictionary) { .size = 0, .items = NULL };
}
return nlua_pop_Dictionary_unchecked(lstate, err);
}
/// Convert lua table to object
///
/// Always pops one value from the stack.
Object nlua_pop_Object(lua_State *lstate, Error *err)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
Object ret = { .type = kObjectTypeNil };
switch (lua_type(lstate, -1)) {
case LUA_TNIL: {
ret.type = kObjectTypeNil;
lua_pop(lstate, 1);
break;
}
case LUA_TSTRING: {
ret.type = kObjectTypeString;
ret.data.string = nlua_pop_String(lstate, err);
break;
}
case LUA_TNUMBER: {
ret.type = kObjectTypeInteger;
ret.data.integer = nlua_pop_Integer(lstate, err);
break;
}
case LUA_TBOOLEAN: {
ret.type = kObjectTypeBoolean;
ret.data.boolean = nlua_pop_Boolean(lstate, err);
break;
}
case LUA_TTABLE: {
lua_getglobal(lstate, "vim");
// stack: obj, vim
#define CHECK_TYPE(Type, key, vim_type) \
lua_getfield(lstate, -1, "is_" #vim_type); \
/* stack: obj, vim, checker */ \
lua_pushvalue(lstate, -3); \
/* stack: obj, vim, checker, obj */ \
lua_call(lstate, 1, 1); \
/* stack: obj, vim, result */ \
if (lua_toboolean(lstate, -1)) { \
lua_pop(lstate, 2); \
/* stack: obj */ \
ret.type = kObjectType##Type; \
ret.data.key = nlua_pop_##Type(lstate, err); \
/* stack: */ \
break; \
} \
lua_pop(lstate, 1); \
// stack: obj, vim
CHECK_TYPE(Float, floating, float)
CHECK_TYPE(Array, array, list)
CHECK_TYPE(Dictionary, dictionary, dict)
#undef CHECK_TYPE
lua_pop(lstate, 1);
// stack: obj
ret.type = kObjectTypeDictionary;
ret.data.dictionary = nlua_pop_Dictionary_unchecked(lstate, err);
break;
}
default: {
lua_pop(lstate, 1);
set_api_error("Cannot convert given lua type", err);
break;
}
}
if (err->set) {
ret.type = kObjectTypeNil;
}
return ret;
}
#define GENERATE_INDEX_FUNCTION(type) \
type nlua_pop_##type(lua_State *lstate, Error *err) \
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT \
{ \
type ret; \
NLUA_POP_IDX(lstate, type, -1, ret); \
lua_pop(lstate, 1); \
return ret; \
}
GENERATE_INDEX_FUNCTION(Buffer)
GENERATE_INDEX_FUNCTION(Window)
GENERATE_INDEX_FUNCTION(Tabpage)
#undef GENERATE_INDEX_FUNCTION
|