-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathif_lua.c
2800 lines (2555 loc) · 73.3 KB
/
if_lua.c
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
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Lua interface by Luis Carvalho
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
#include "vim.h"
#include "version.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
// Only do the following when the feature is enabled. Needed for "make
// depend".
#if defined(FEAT_LUA) || defined(PROTO)
#define LUAVIM_CHUNKNAME "vim chunk"
#define LUAVIM_NAME "vim"
#define LUAVIM_EVALNAME "luaeval"
#define LUAVIM_EVALHEADER "local _A=select(1,...) return "
#ifdef LUA_RELEASE
# define LUAVIM_VERSION LUA_RELEASE
#else
# define LUAVIM_VERSION LUA_VERSION
#endif
typedef buf_T *luaV_Buffer;
typedef win_T *luaV_Window;
typedef dict_T *luaV_Dict;
typedef list_T *luaV_List;
typedef blob_T *luaV_Blob;
typedef struct {
char_u *name; // funcref
dict_T *self; // selfdict
} luaV_Funcref;
typedef int (*msgfunc_T)(char *);
typedef struct {
int lua_funcref; // ref to a lua func
int lua_tableref; // ref to a lua table if metatable else LUA_NOREF. used
// for __call
lua_State *L;
} luaV_CFuncState;
static const char LUAVIM_DICT[] = "dict";
static const char LUAVIM_LIST[] = "list";
static const char LUAVIM_BLOB[] = "blob";
static const char LUAVIM_FUNCREF[] = "funcref";
static const char LUAVIM_BUFFER[] = "buffer";
static const char LUAVIM_WINDOW[] = "window";
static const char LUAVIM_FREE[] = "luaV_free";
static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
static const char LUAVIM_SETREF[] = "luaV_setref";
static const char LUA___CALL[] = "__call";
// most functions are closures with a cache table as first upvalue;
// get/setudata manage references to vim userdata in cache table through
// object pointers (light userdata)
#define luaV_getudata(L, v) \
lua_pushlightuserdata((L), (void *) (v)); \
lua_rawget((L), lua_upvalueindex(1))
#define luaV_setudata(L, v) \
lua_pushlightuserdata((L), (void *) (v)); \
lua_pushvalue((L), -2); \
lua_rawset((L), lua_upvalueindex(1))
#define luaV_getfield(L, s) \
lua_pushlightuserdata((L), (void *)(s)); \
lua_rawget((L), LUA_REGISTRYINDEX)
#define luaV_checksandbox(L) \
if (sandbox) luaL_error((L), "not allowed in sandbox")
#define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
#define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
#define luaV_checktypval(L, a, v, msg) \
do { \
if (luaV_totypval(L, a, v) == FAIL) \
luaL_error(L, msg ": cannot convert value"); \
} while (0)
static luaV_List *luaV_pushlist(lua_State *L, list_T *lis);
static luaV_Dict *luaV_pushdict(lua_State *L, dict_T *dic);
static luaV_Blob *luaV_pushblob(lua_State *L, blob_T *blo);
static luaV_Funcref *luaV_pushfuncref(lua_State *L, char_u *name);
static int luaV_call_lua_func(int argcount, typval_T *argvars, typval_T *rettv, void *state);
static void luaV_call_lua_func_free(void *state);
#if LUA_VERSION_NUM <= 501
#define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n)
#define luaL_typeerror luaL_typerror
#else
#define luaV_openlib luaL_setfuncs
#endif
#ifdef DYNAMIC_LUA
#ifndef MSWIN
# include <dlfcn.h>
# define HANDLE void*
# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
# define symbol_from_dll dlsym
# define close_dll dlclose
# define load_dll_error dlerror
#else
# define load_dll vimLoadLib
# define symbol_from_dll GetProcAddress
# define close_dll FreeLibrary
# define load_dll_error GetWin32Error
#endif
// lauxlib
#if LUA_VERSION_NUM <= 501
#define luaL_register dll_luaL_register
#define luaL_prepbuffer dll_luaL_prepbuffer
#define luaL_openlib dll_luaL_openlib
#define luaL_typerror dll_luaL_typerror
#define luaL_loadfile dll_luaL_loadfile
#define luaL_loadbuffer dll_luaL_loadbuffer
#else
#define luaL_prepbuffsize dll_luaL_prepbuffsize
#define luaL_setfuncs dll_luaL_setfuncs
#define luaL_loadfilex dll_luaL_loadfilex
#define luaL_loadbufferx dll_luaL_loadbufferx
#define luaL_argerror dll_luaL_argerror
#endif
#if LUA_VERSION_NUM >= 504
#define luaL_typeerror dll_luaL_typeerror
#endif
#define luaL_checkany dll_luaL_checkany
#define luaL_checklstring dll_luaL_checklstring
#define luaL_checkinteger dll_luaL_checkinteger
#define luaL_optinteger dll_luaL_optinteger
#define luaL_checktype dll_luaL_checktype
#define luaL_error dll_luaL_error
#define luaL_newstate dll_luaL_newstate
#define luaL_buffinit dll_luaL_buffinit
#define luaL_addlstring dll_luaL_addlstring
#define luaL_pushresult dll_luaL_pushresult
#define luaL_loadstring dll_luaL_loadstring
#define luaL_ref dll_luaL_ref
#define luaL_unref dll_luaL_unref
// lua
#if LUA_VERSION_NUM <= 501
#define lua_tonumber dll_lua_tonumber
#define lua_tointeger dll_lua_tointeger
#define lua_call dll_lua_call
#define lua_pcall dll_lua_pcall
#else
#define lua_tonumberx dll_lua_tonumberx
#define lua_tointegerx dll_lua_tointegerx
#define lua_callk dll_lua_callk
#define lua_pcallk dll_lua_pcallk
#define lua_getglobal dll_lua_getglobal
#define lua_setglobal dll_lua_setglobal
#endif
#if LUA_VERSION_NUM <= 502
#define lua_replace dll_lua_replace
#define lua_remove dll_lua_remove
#endif
#if LUA_VERSION_NUM >= 503
#define lua_rotate dll_lua_rotate
#define lua_copy dll_lua_copy
#endif
#define lua_typename dll_lua_typename
#define lua_close dll_lua_close
#define lua_gettop dll_lua_gettop
#define lua_settop dll_lua_settop
#define lua_pushvalue dll_lua_pushvalue
#define lua_isnumber dll_lua_isnumber
#define lua_isstring dll_lua_isstring
#define lua_type dll_lua_type
#define lua_rawequal dll_lua_rawequal
#define lua_toboolean dll_lua_toboolean
#define lua_tolstring dll_lua_tolstring
#define lua_touserdata dll_lua_touserdata
#define lua_pushnil dll_lua_pushnil
#define lua_pushnumber dll_lua_pushnumber
#define lua_pushinteger dll_lua_pushinteger
#define lua_pushlstring dll_lua_pushlstring
#define lua_pushstring dll_lua_pushstring
#define lua_pushfstring dll_lua_pushfstring
#define lua_pushcclosure dll_lua_pushcclosure
#define lua_pushboolean dll_lua_pushboolean
#define lua_pushlightuserdata dll_lua_pushlightuserdata
#define lua_getfield dll_lua_getfield
#define lua_rawget dll_lua_rawget
#define lua_rawgeti dll_lua_rawgeti
#define lua_createtable dll_lua_createtable
#define lua_settable dll_lua_settable
#if LUA_VERSION_NUM >= 504
#define lua_newuserdatauv dll_lua_newuserdatauv
#else
#define lua_newuserdata dll_lua_newuserdata
#endif
#define lua_getmetatable dll_lua_getmetatable
#define lua_setfield dll_lua_setfield
#define lua_rawset dll_lua_rawset
#define lua_rawseti dll_lua_rawseti
#define lua_setmetatable dll_lua_setmetatable
#define lua_next dll_lua_next
// libs
#define luaopen_base dll_luaopen_base
#define luaopen_table dll_luaopen_table
#define luaopen_string dll_luaopen_string
#define luaopen_math dll_luaopen_math
#define luaopen_io dll_luaopen_io
#define luaopen_os dll_luaopen_os
#define luaopen_package dll_luaopen_package
#define luaopen_debug dll_luaopen_debug
#define luaL_openlibs dll_luaL_openlibs
// lauxlib
#if LUA_VERSION_NUM <= 501
void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
#else
char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
#endif
#if LUA_VERSION_NUM >= 504
int (*dll_luaL_typeerror) (lua_State *L, int narg, const char *tname);
#endif
void (*dll_luaL_checkany) (lua_State *L, int narg);
const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
lua_State *(*dll_luaL_newstate) (void);
void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
void (*dll_luaL_pushresult) (luaL_Buffer *B);
int (*dll_luaL_loadstring) (lua_State *L, const char *s);
int (*dll_luaL_ref) (lua_State *L, int idx);
#if LUA_VERSION_NUM <= 502
void (*dll_luaL_unref) (lua_State *L, int idx, int n);
#else
void (*dll_luaL_unref) (lua_State *L, int idx, lua_Integer n);
#endif
// lua
#if LUA_VERSION_NUM <= 501
lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
#else
lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
lua_CFunction k);
int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
int ctx, lua_CFunction k);
void (*dll_lua_getglobal) (lua_State *L, const char *var);
void (*dll_lua_setglobal) (lua_State *L, const char *var);
#endif
#if LUA_VERSION_NUM <= 502
void (*dll_lua_replace) (lua_State *L, int idx);
void (*dll_lua_remove) (lua_State *L, int idx);
#endif
#if LUA_VERSION_NUM >= 503
void (*dll_lua_rotate) (lua_State *L, int idx, int n);
void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
#endif
const char *(*dll_lua_typename) (lua_State *L, int tp);
void (*dll_lua_close) (lua_State *L);
int (*dll_lua_gettop) (lua_State *L);
void (*dll_lua_settop) (lua_State *L, int idx);
void (*dll_lua_pushvalue) (lua_State *L, int idx);
int (*dll_lua_isnumber) (lua_State *L, int idx);
int (*dll_lua_isstring) (lua_State *L, int idx);
int (*dll_lua_type) (lua_State *L, int idx);
int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
int (*dll_lua_toboolean) (lua_State *L, int idx);
const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
void *(*dll_lua_touserdata) (lua_State *L, int idx);
void (*dll_lua_pushnil) (lua_State *L);
void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
void (*dll_lua_pushstring) (lua_State *L, const char *s);
const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
void (*dll_lua_pushboolean) (lua_State *L, int b);
void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
#if LUA_VERSION_NUM <= 502
void (*dll_lua_rawget) (lua_State *L, int idx);
void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
#else
int (*dll_lua_rawget) (lua_State *L, int idx);
int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
#endif
void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
void (*dll_lua_settable) (lua_State *L, int idx);
#if LUA_VERSION_NUM >= 504
void *(*dll_lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
#else
void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
#endif
int (*dll_lua_getmetatable) (lua_State *L, int objindex);
void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
void (*dll_lua_rawset) (lua_State *L, int idx);
#if LUA_VERSION_NUM <= 502
void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
#else
void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n);
#endif
int (*dll_lua_setmetatable) (lua_State *L, int objindex);
int (*dll_lua_next) (lua_State *L, int idx);
// libs
int (*dll_luaopen_base) (lua_State *L);
int (*dll_luaopen_table) (lua_State *L);
int (*dll_luaopen_string) (lua_State *L);
int (*dll_luaopen_math) (lua_State *L);
int (*dll_luaopen_io) (lua_State *L);
int (*dll_luaopen_os) (lua_State *L);
int (*dll_luaopen_package) (lua_State *L);
int (*dll_luaopen_debug) (lua_State *L);
void (*dll_luaL_openlibs) (lua_State *L);
typedef void **luaV_function;
typedef struct {
const char *name;
luaV_function func;
} luaV_Reg;
static const luaV_Reg luaV_dll[] = {
// lauxlib
#if LUA_VERSION_NUM <= 501
{"luaL_register", (luaV_function) &dll_luaL_register},
{"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
{"luaL_openlib", (luaV_function) &dll_luaL_openlib},
{"luaL_typerror", (luaV_function) &dll_luaL_typerror},
{"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
{"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
#else
{"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
{"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
{"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
{"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
{"luaL_argerror", (luaV_function) &dll_luaL_argerror},
#endif
#if LUA_VERSION_NUM >= 504
{"luaL_typeerror", (luaV_function) &dll_luaL_typeerror},
#endif
{"luaL_checkany", (luaV_function) &dll_luaL_checkany},
{"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
{"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
{"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
{"luaL_checktype", (luaV_function) &dll_luaL_checktype},
{"luaL_error", (luaV_function) &dll_luaL_error},
{"luaL_newstate", (luaV_function) &dll_luaL_newstate},
{"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
{"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
{"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
{"luaL_loadstring", (luaV_function) &dll_luaL_loadstring},
{"luaL_ref", (luaV_function) &dll_luaL_ref},
{"luaL_unref", (luaV_function) &dll_luaL_unref},
// lua
#if LUA_VERSION_NUM <= 501
{"lua_tonumber", (luaV_function) &dll_lua_tonumber},
{"lua_tointeger", (luaV_function) &dll_lua_tointeger},
{"lua_call", (luaV_function) &dll_lua_call},
{"lua_pcall", (luaV_function) &dll_lua_pcall},
#else
{"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
{"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
{"lua_callk", (luaV_function) &dll_lua_callk},
{"lua_pcallk", (luaV_function) &dll_lua_pcallk},
{"lua_getglobal", (luaV_function) &dll_lua_getglobal},
{"lua_setglobal", (luaV_function) &dll_lua_setglobal},
#endif
#if LUA_VERSION_NUM <= 502
{"lua_replace", (luaV_function) &dll_lua_replace},
{"lua_remove", (luaV_function) &dll_lua_remove},
#endif
#if LUA_VERSION_NUM >= 503
{"lua_rotate", (luaV_function) &dll_lua_rotate},
{"lua_copy", (luaV_function) &dll_lua_copy},
#endif
{"lua_typename", (luaV_function) &dll_lua_typename},
{"lua_close", (luaV_function) &dll_lua_close},
{"lua_gettop", (luaV_function) &dll_lua_gettop},
{"lua_settop", (luaV_function) &dll_lua_settop},
{"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
{"lua_isnumber", (luaV_function) &dll_lua_isnumber},
{"lua_isstring", (luaV_function) &dll_lua_isstring},
{"lua_type", (luaV_function) &dll_lua_type},
{"lua_rawequal", (luaV_function) &dll_lua_rawequal},
{"lua_toboolean", (luaV_function) &dll_lua_toboolean},
{"lua_tolstring", (luaV_function) &dll_lua_tolstring},
{"lua_touserdata", (luaV_function) &dll_lua_touserdata},
{"lua_pushnil", (luaV_function) &dll_lua_pushnil},
{"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
{"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
{"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
{"lua_pushstring", (luaV_function) &dll_lua_pushstring},
{"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
{"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
{"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
{"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
{"lua_getfield", (luaV_function) &dll_lua_getfield},
{"lua_rawget", (luaV_function) &dll_lua_rawget},
{"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
{"lua_createtable", (luaV_function) &dll_lua_createtable},
{"lua_settable", (luaV_function) &dll_lua_settable},
#if LUA_VERSION_NUM >= 504
{"lua_newuserdatauv", (luaV_function) &dll_lua_newuserdatauv},
#else
{"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
#endif
{"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
{"lua_setfield", (luaV_function) &dll_lua_setfield},
{"lua_rawset", (luaV_function) &dll_lua_rawset},
{"lua_rawseti", (luaV_function) &dll_lua_rawseti},
{"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
{"lua_next", (luaV_function) &dll_lua_next},
// libs
{"luaopen_base", (luaV_function) &dll_luaopen_base},
{"luaopen_table", (luaV_function) &dll_luaopen_table},
{"luaopen_string", (luaV_function) &dll_luaopen_string},
{"luaopen_math", (luaV_function) &dll_luaopen_math},
{"luaopen_io", (luaV_function) &dll_luaopen_io},
{"luaopen_os", (luaV_function) &dll_luaopen_os},
{"luaopen_package", (luaV_function) &dll_luaopen_package},
{"luaopen_debug", (luaV_function) &dll_luaopen_debug},
{"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
{NULL, NULL}
};
static HANDLE hinstLua = NULL;
static int
lua_link_init(char *libname, int verbose)
{
const luaV_Reg *reg;
if (hinstLua) return OK;
hinstLua = load_dll(libname);
if (!hinstLua)
{
if (verbose)
semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
return FAIL;
}
for (reg = luaV_dll; reg->func; reg++)
{
if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
{
close_dll(hinstLua);
hinstLua = 0;
if (verbose)
semsg(_(e_could_not_load_library_function_str), reg->name);
return FAIL;
}
}
return OK;
}
#endif // DYNAMIC_LUA
#if defined(DYNAMIC_LUA) || defined(PROTO)
int
lua_enabled(int verbose)
{
return lua_link_init((char *)p_luadll, verbose) == OK;
}
#endif
#if LUA_VERSION_NUM > 501 && LUA_VERSION_NUM < 504
static int
luaL_typeerror(lua_State *L, int narg, const char *tname)
{
const char *msg = lua_pushfstring(L, "%s expected, got %s",
tname, luaL_typename(L, narg));
return luaL_argerror(L, narg, msg);
}
#endif
// ======= Internal =======
static void
luaV_newmetatable(lua_State *L, const char *tname)
{
lua_newtable(L);
lua_pushlightuserdata(L, (void *) tname);
lua_pushvalue(L, -2);
lua_rawset(L, LUA_REGISTRYINDEX);
}
static void *
luaV_toudata(lua_State *L, int ud, const char *tname)
{
void *p = lua_touserdata(L, ud);
if (p == NULL)
return NULL;
// value is userdata
if (lua_getmetatable(L, ud)) // does it have a metatable?
{
luaV_getfield(L, tname); // get metatable
if (lua_rawequal(L, -1, -2)) // MTs match?
{
lua_pop(L, 2); // MTs
return p;
}
}
return NULL;
}
static void *
luaV_checkcache(lua_State *L, void *p)
{
luaV_getudata(L, p);
if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
lua_pop(L, 1);
return p;
}
#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
#define luaV_checkvalid(L,luatyp,ud) \
luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
static void *
luaV_checkudata(lua_State *L, int ud, const char *tname)
{
void *p = luaV_toudata(L, ud, tname);
if (p == NULL) luaL_typeerror(L, ud, tname);
return p;
}
static void
luaV_pushtypval(lua_State *L, typval_T *tv)
{
if (tv == NULL)
{
lua_pushnil(L);
return;
}
switch (tv->v_type)
{
case VAR_STRING:
lua_pushstring(L, tv->vval.v_string == NULL
? "" : (char *)tv->vval.v_string);
break;
case VAR_NUMBER:
lua_pushinteger(L, (int) tv->vval.v_number);
break;
case VAR_FLOAT:
lua_pushnumber(L, (lua_Number) tv->vval.v_float);
break;
case VAR_LIST:
luaV_pushlist(L, tv->vval.v_list);
break;
case VAR_DICT:
luaV_pushdict(L, tv->vval.v_dict);
break;
case VAR_BOOL:
case VAR_SPECIAL:
if (tv->vval.v_number <= VVAL_TRUE)
lua_pushinteger(L, (int) tv->vval.v_number);
else
lua_pushnil(L);
break;
case VAR_FUNC:
luaV_pushfuncref(L, tv->vval.v_string);
break;
case VAR_PARTIAL:
// TODO: handle partial arguments
luaV_pushfuncref(L, partial_name(tv->vval.v_partial));
break;
case VAR_BLOB:
luaV_pushblob(L, tv->vval.v_blob);
break;
default:
lua_pushnil(L);
}
}
/*
* Converts lua value at 'pos' to typval 'tv'.
* Returns OK or FAIL.
*/
static int
luaV_totypval(lua_State *L, int pos, typval_T *tv)
{
int status = OK;
tv->v_lock = 0;
switch (lua_type(L, pos))
{
case LUA_TBOOLEAN:
tv->v_type = VAR_BOOL;
tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
break;
case LUA_TNIL:
tv->v_type = VAR_SPECIAL;
tv->vval.v_number = VVAL_NULL;
break;
case LUA_TSTRING:
tv->v_type = VAR_STRING;
tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
break;
case LUA_TNUMBER:
{
const lua_Number n = lua_tonumber(L, pos);
if (n > (lua_Number)INT64_MAX || n < (lua_Number)INT64_MIN
|| ((lua_Number)((varnumber_T)n)) != n)
{
tv->v_type = VAR_FLOAT;
tv->vval.v_float = (float_T)n;
}
else
{
tv->v_type = VAR_NUMBER;
tv->vval.v_number = (varnumber_T)n;
}
}
break;
case LUA_TFUNCTION:
{
char_u *name;
luaV_CFuncState *state;
lua_pushvalue(L, pos);
state = ALLOC_CLEAR_ONE(luaV_CFuncState);
state->lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
state->L = L;
state->lua_tableref = LUA_NOREF;
name = register_cfunc(&luaV_call_lua_func,
&luaV_call_lua_func_free, state);
tv->v_type = VAR_FUNC;
tv->vval.v_string = vim_strsave(name);
break;
}
case LUA_TTABLE:
{
int lua_tableref;
lua_pushvalue(L, pos);
lua_tableref = luaL_ref(L, LUA_REGISTRYINDEX);
if (lua_getmetatable(L, pos))
{
lua_getfield(L, -1, LUA___CALL);
if (lua_isfunction(L, -1))
{
char_u *name;
int lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState);
state->lua_funcref = lua_funcref;
state->L = L;
state->lua_tableref = lua_tableref;
name = register_cfunc(&luaV_call_lua_func,
&luaV_call_lua_func_free, state);
tv->v_type = VAR_FUNC;
tv->vval.v_string = vim_strsave(name);
break;
}
}
tv->v_type = VAR_NUMBER;
tv->vval.v_number = 0;
status = FAIL;
break;
}
case LUA_TUSERDATA:
{
void *p = lua_touserdata(L, pos);
if (lua_getmetatable(L, pos)) // has metatable?
{
// check list
luaV_getfield(L, LUAVIM_LIST);
if (lua_rawequal(L, -1, -2))
{
tv->v_type = VAR_LIST;
tv->vval.v_list = *((luaV_List *) p);
++tv->vval.v_list->lv_refcount;
lua_pop(L, 2); // MTs
break;
}
// check dict
luaV_getfield(L, LUAVIM_DICT);
if (lua_rawequal(L, -1, -3))
{
tv->v_type = VAR_DICT;
tv->vval.v_dict = *((luaV_Dict *) p);
++tv->vval.v_dict->dv_refcount;
lua_pop(L, 3); // MTs
break;
}
// check blob
luaV_getfield(L, LUAVIM_BLOB);
if (lua_rawequal(L, -1, -4))
{
tv->v_type = VAR_BLOB;
tv->vval.v_blob = *((luaV_Blob *) p);
++tv->vval.v_blob->bv_refcount;
lua_pop(L, 4); // MTs
break;
}
// check funcref
luaV_getfield(L, LUAVIM_FUNCREF);
if (lua_rawequal(L, -1, -5))
{
luaV_Funcref *f = (luaV_Funcref *) p;
func_ref(f->name);
tv->v_type = VAR_FUNC;
tv->vval.v_string = vim_strsave(f->name);
lua_pop(L, 5); // MTs
break;
}
lua_pop(L, 4); // MTs
}
}
// FALLTHROUGH
default:
tv->v_type = VAR_NUMBER;
tv->vval.v_number = 0;
status = FAIL;
}
return status;
}
/*
* similar to luaL_addlstring, but replaces \0 with \n if toline and
* \n with \0 otherwise
*/
static void
luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
{
while (l--)
{
if (*s == '\0' && toline)
luaL_addchar(b, '\n');
else if (*s == '\n' && !toline)
luaL_addchar(b, '\0');
else
luaL_addchar(b, *s);
s++;
}
}
static void
luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
{
const char *s = (const char *) ml_get_buf(buf, n, FALSE);
luaL_Buffer b;
luaL_buffinit(L, &b);
luaV_addlstring(&b, s, strlen(s), 0);
luaL_pushresult(&b);
}
static char_u *
luaV_toline(lua_State *L, int pos)
{
size_t l;
const char *s = lua_tolstring(L, pos, &l);
luaL_Buffer b;
luaL_buffinit(L, &b);
luaV_addlstring(&b, s, l, 1);
luaL_pushresult(&b);
return (char_u *) lua_tostring(L, -1);
}
/*
* pops a string s from the top of the stack and calls mf(t) for pieces t of
* s separated by newlines
*/
static void
luaV_msgfunc(lua_State *L, msgfunc_T mf)
{
luaL_Buffer b;
size_t l;
const char *p, *s = lua_tolstring(L, -1, &l);
luaL_buffinit(L, &b);
luaV_addlstring(&b, s, l, 0);
luaL_pushresult(&b);
// break string
p = s = lua_tolstring(L, -1, &l);
while (l--)
{
if (*p++ == '\0') // break?
{
mf((char *)s);
s = p;
}
}
mf((char *)s);
lua_pop(L, 2); // original and modified strings
}
#define luaV_newtype(typ,tname,luatyp,luatname) \
static luatyp * \
luaV_new##tname(lua_State *L, typ *obj) \
{ \
luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
*o = obj; \
luaV_setudata(L, obj); /* cache[obj] = udata */ \
luaV_getfield(L, luatname); \
lua_setmetatable(L, -2); \
return o; \
}
#define luaV_pushtype(typ,tname,luatyp) \
static luatyp * \
luaV_push##tname(lua_State *L, typ *obj) \
{ \
luatyp *o = NULL; \
if (obj == NULL) \
lua_pushnil(L); \
else \
{ \
luaV_getudata(L, obj); \
if (lua_isnil(L, -1)) /* not interned? */ \
{ \
lua_pop(L, 1); \
o = luaV_new##tname(L, obj); \
} \
else \
o = (luatyp *) lua_touserdata(L, -1); \
} \
return o; \
}
#define luaV_type_tostring(tname,luatname) \
static int \
luaV_##tname##_tostring(lua_State *L) \
{ \
lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
return 1; \
}
// ======= List type =======
static luaV_List *
luaV_newlist(lua_State *L, list_T *lis)
{
luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
*l = lis;
lis->lv_refcount++; // reference in Lua
luaV_setudata(L, lis); // cache[lis] = udata
luaV_getfield(L, LUAVIM_LIST);
lua_setmetatable(L, -2);
return l;
}
luaV_pushtype(list_T, list, luaV_List)
luaV_type_tostring(list, LUAVIM_LIST)
static int
luaV_list_len(lua_State *L)
{
list_T *l = luaV_unbox(L, luaV_List, 1);
lua_pushinteger(L, (int) list_len(l));
return 1;
}
static int
luaV_list_iter(lua_State *L)
{
listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
if (li == NULL) return 0;
luaV_pushtypval(L, &li->li_tv);
lua_pushlightuserdata(L, (void *) li->li_next);
lua_replace(L, lua_upvalueindex(2));
return 1;
}
static int
luaV_list_call(lua_State *L)
{
list_T *l = luaV_unbox(L, luaV_List, 1);
lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
lua_pushlightuserdata(L, (void *) l->lv_first);
lua_pushcclosure(L, luaV_list_iter, 2);
return 1;
}
static int
luaV_list_index(lua_State *L)
{
list_T *l = luaV_unbox(L, luaV_List, 1);
if (lua_isnumber(L, 2)) // list item?
{
long n = (long) luaL_checkinteger(L, 2);
listitem_T *li;
// Lua array index starts with 1 while Vim uses 0, subtract 1 to
// normalize.
n -= 1;
li = list_find(l, n);
if (li == NULL)
lua_pushnil(L);
else
luaV_pushtypval(L, &li->li_tv);
}
else if (lua_isstring(L, 2)) // method?
{
const char *s = lua_tostring(L, 2);
if (strncmp(s, "add", 3) == 0
|| strncmp(s, "insert", 6) == 0)
{
lua_getmetatable(L, 1);
lua_getfield(L, -1, s);
}
else
lua_pushnil(L);
}
else
lua_pushnil(L);
return 1;
}
static int
luaV_list_newindex(lua_State *L)
{
list_T *l = luaV_unbox(L, luaV_List, 1);
long n = (long) luaL_checkinteger(L, 2);
listitem_T *li;
// Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize.
n -= 1;
if (l->lv_lock)
luaL_error(L, "list is locked");
li = list_find(l, n);
if (li == NULL)
{
if (!lua_isnil(L, 3))
{
typval_T v;
luaV_checktypval(L, 3, &v, "inserting list item");
if (list_insert_tv(l, &v, li) == FAIL)
luaL_error(L, "failed to add item to list");
clear_tv(&v);
}
}
else
{
if (lua_isnil(L, 3)) // remove?
{
vimlist_remove(l, li, li);
listitem_free(l, li);
}
else
{
typval_T v;
luaV_checktypval(L, 3, &v, "setting list item");
clear_tv(&li->li_tv);
li->li_tv = v;
}
}
return 0;
}
static int
luaV_list_add(lua_State *L)
{
luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
typval_T v;
if (l->lv_lock)
luaL_error(L, "list is locked");
lua_settop(L, 2);
luaV_checktypval(L, 2, &v, "adding list item");
if (list_append_tv(l, &v) == FAIL)
luaL_error(L, "failed to add item to list");
clear_tv(&v);
lua_settop(L, 1);
return 1;
}
static int
luaV_list_insert(lua_State *L)
{
luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
long pos = (long) luaL_optinteger(L, 3, 0);
listitem_T *li = NULL;