xref: /vim-8.2.3635/src/if_lua.c (revision 3b0ef8cf)
1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *
5  * Lua interface by Luis Carvalho
6  *
7  * Do ":help uganda"  in Vim to read copying and usage conditions.
8  * Do ":help credits" in Vim to see a list of people who contributed.
9  * See README.txt for an overview of the Vim source code.
10  */
11 
12 #include "vim.h"
13 
14 #include <lua.h>
15 #include <lualib.h>
16 #include <lauxlib.h>
17 
18 // Only do the following when the feature is enabled.  Needed for "make
19 // depend".
20 #if defined(FEAT_LUA) || defined(PROTO)
21 
22 #define LUAVIM_CHUNKNAME "vim chunk"
23 #define LUAVIM_NAME "vim"
24 #define LUAVIM_EVALNAME "luaeval"
25 #define LUAVIM_EVALHEADER "local _A=select(1,...) return "
26 
27 typedef buf_T *luaV_Buffer;
28 typedef win_T *luaV_Window;
29 typedef dict_T *luaV_Dict;
30 typedef list_T *luaV_List;
31 typedef blob_T *luaV_Blob;
32 typedef struct {
33     char_u	*name;	// funcref
34     dict_T	*self;	// selfdict
35 } luaV_Funcref;
36 typedef void (*msgfunc_T)(char_u *);
37 
38 static const char LUAVIM_DICT[] = "dict";
39 static const char LUAVIM_LIST[] = "list";
40 static const char LUAVIM_BLOB[] = "blob";
41 static const char LUAVIM_FUNCREF[] = "funcref";
42 static const char LUAVIM_BUFFER[] = "buffer";
43 static const char LUAVIM_WINDOW[] = "window";
44 static const char LUAVIM_FREE[] = "luaV_free";
45 static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
46 static const char LUAVIM_SETREF[] = "luaV_setref";
47 
48 // most functions are closures with a cache table as first upvalue;
49 // get/setudata manage references to vim userdata in cache table through
50 // object pointers (light userdata)
51 #define luaV_getudata(L, v) \
52     lua_pushlightuserdata((L), (void *) (v)); \
53     lua_rawget((L), lua_upvalueindex(1))
54 #define luaV_setudata(L, v) \
55     lua_pushlightuserdata((L), (void *) (v)); \
56     lua_pushvalue((L), -2); \
57     lua_rawset((L), lua_upvalueindex(1))
58 #define luaV_getfield(L, s) \
59     lua_pushlightuserdata((L), (void *)(s)); \
60     lua_rawget((L), LUA_REGISTRYINDEX)
61 #define luaV_checksandbox(L) \
62     if (sandbox) luaL_error((L), "not allowed in sandbox")
63 #define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
64 #define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
65 #define luaV_checktypval(L, a, v, msg) \
66     do { \
67         if (luaV_totypval(L, a, v) == FAIL) \
68 	    luaL_error(L, msg ": cannot convert value"); \
69     } while (0)
70 
71 static luaV_List *luaV_pushlist(lua_State *L, list_T *lis);
72 static luaV_Dict *luaV_pushdict(lua_State *L, dict_T *dic);
73 static luaV_Blob *luaV_pushblob(lua_State *L, blob_T *blo);
74 static luaV_Funcref *luaV_pushfuncref(lua_State *L, char_u *name);
75 
76 #if LUA_VERSION_NUM <= 501
77 #define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n)
78 #define luaL_typeerror luaL_typerror
79 #else
80 #define luaV_openlib luaL_setfuncs
81 #endif
82 
83 #ifdef DYNAMIC_LUA
84 
85 #ifndef MSWIN
86 # include <dlfcn.h>
87 # define HANDLE void*
88 # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
89 # define symbol_from_dll dlsym
90 # define close_dll dlclose
91 #else
92 # define load_dll vimLoadLib
93 # define symbol_from_dll GetProcAddress
94 # define close_dll FreeLibrary
95 #endif
96 
97 // lauxlib
98 #if LUA_VERSION_NUM <= 501
99 #define luaL_register dll_luaL_register
100 #define luaL_prepbuffer dll_luaL_prepbuffer
101 #define luaL_openlib dll_luaL_openlib
102 #define luaL_typerror dll_luaL_typerror
103 #define luaL_loadfile dll_luaL_loadfile
104 #define luaL_loadbuffer dll_luaL_loadbuffer
105 #else
106 #define luaL_prepbuffsize dll_luaL_prepbuffsize
107 #define luaL_setfuncs dll_luaL_setfuncs
108 #define luaL_loadfilex dll_luaL_loadfilex
109 #define luaL_loadbufferx dll_luaL_loadbufferx
110 #define luaL_argerror dll_luaL_argerror
111 #endif
112 #define luaL_checkany dll_luaL_checkany
113 #define luaL_checklstring dll_luaL_checklstring
114 #define luaL_checkinteger dll_luaL_checkinteger
115 #define luaL_optinteger dll_luaL_optinteger
116 #define luaL_checktype dll_luaL_checktype
117 #define luaL_error dll_luaL_error
118 #define luaL_newstate dll_luaL_newstate
119 #define luaL_buffinit dll_luaL_buffinit
120 #define luaL_addlstring dll_luaL_addlstring
121 #define luaL_pushresult dll_luaL_pushresult
122 // lua
123 #if LUA_VERSION_NUM <= 501
124 #define lua_tonumber dll_lua_tonumber
125 #define lua_tointeger dll_lua_tointeger
126 #define lua_call dll_lua_call
127 #define lua_pcall dll_lua_pcall
128 #else
129 #define lua_tonumberx dll_lua_tonumberx
130 #define lua_tointegerx dll_lua_tointegerx
131 #define lua_callk dll_lua_callk
132 #define lua_pcallk dll_lua_pcallk
133 #define lua_getglobal dll_lua_getglobal
134 #define lua_setglobal dll_lua_setglobal
135 #endif
136 #if LUA_VERSION_NUM <= 502
137 #define lua_replace dll_lua_replace
138 #define lua_remove dll_lua_remove
139 #endif
140 #if LUA_VERSION_NUM >= 503
141 #define lua_rotate dll_lua_rotate
142 #define lua_copy dll_lua_copy
143 #endif
144 #define lua_typename dll_lua_typename
145 #define lua_close dll_lua_close
146 #define lua_gettop dll_lua_gettop
147 #define lua_settop dll_lua_settop
148 #define lua_pushvalue dll_lua_pushvalue
149 #define lua_isnumber dll_lua_isnumber
150 #define lua_isstring dll_lua_isstring
151 #define lua_type dll_lua_type
152 #define lua_rawequal dll_lua_rawequal
153 #define lua_toboolean dll_lua_toboolean
154 #define lua_tolstring dll_lua_tolstring
155 #define lua_touserdata dll_lua_touserdata
156 #define lua_pushnil dll_lua_pushnil
157 #define lua_pushnumber dll_lua_pushnumber
158 #define lua_pushinteger dll_lua_pushinteger
159 #define lua_pushlstring dll_lua_pushlstring
160 #define lua_pushstring dll_lua_pushstring
161 #define lua_pushfstring dll_lua_pushfstring
162 #define lua_pushcclosure dll_lua_pushcclosure
163 #define lua_pushboolean dll_lua_pushboolean
164 #define lua_pushlightuserdata dll_lua_pushlightuserdata
165 #define lua_getfield dll_lua_getfield
166 #define lua_rawget dll_lua_rawget
167 #define lua_rawgeti dll_lua_rawgeti
168 #define lua_createtable dll_lua_createtable
169 #if LUA_VERSION_NUM >= 504
170  #define lua_newuserdatauv dll_lua_newuserdatauv
171 #else
172  #define lua_newuserdata dll_lua_newuserdata
173 #endif
174 #define lua_getmetatable dll_lua_getmetatable
175 #define lua_setfield dll_lua_setfield
176 #define lua_rawset dll_lua_rawset
177 #define lua_rawseti dll_lua_rawseti
178 #define lua_setmetatable dll_lua_setmetatable
179 #define lua_next dll_lua_next
180 // libs
181 #define luaopen_base dll_luaopen_base
182 #define luaopen_table dll_luaopen_table
183 #define luaopen_string dll_luaopen_string
184 #define luaopen_math dll_luaopen_math
185 #define luaopen_io dll_luaopen_io
186 #define luaopen_os dll_luaopen_os
187 #define luaopen_package dll_luaopen_package
188 #define luaopen_debug dll_luaopen_debug
189 #define luaL_openlibs dll_luaL_openlibs
190 
191 // lauxlib
192 #if LUA_VERSION_NUM <= 501
193 void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
194 char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
195 void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
196 int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
197 int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
198 int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
199 #else
200 char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
201 void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
202 int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
203 int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
204 int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
205 #endif
206 void (*dll_luaL_checkany) (lua_State *L, int narg);
207 const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
208 lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
209 lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
210 void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
211 int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
212 lua_State *(*dll_luaL_newstate) (void);
213 void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
214 void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
215 void (*dll_luaL_pushresult) (luaL_Buffer *B);
216 // lua
217 #if LUA_VERSION_NUM <= 501
218 lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
219 lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
220 void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
221 int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
222 #else
223 lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
224 lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
225 void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
226 	lua_CFunction k);
227 int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
228 	int ctx, lua_CFunction k);
229 void (*dll_lua_getglobal) (lua_State *L, const char *var);
230 void (*dll_lua_setglobal) (lua_State *L, const char *var);
231 #endif
232 #if LUA_VERSION_NUM <= 502
233 void (*dll_lua_replace) (lua_State *L, int idx);
234 void (*dll_lua_remove) (lua_State *L, int idx);
235 #endif
236 #if LUA_VERSION_NUM >= 503
237 void  (*dll_lua_rotate) (lua_State *L, int idx, int n);
238 void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
239 #endif
240 const char *(*dll_lua_typename) (lua_State *L, int tp);
241 void       (*dll_lua_close) (lua_State *L);
242 int (*dll_lua_gettop) (lua_State *L);
243 void (*dll_lua_settop) (lua_State *L, int idx);
244 void (*dll_lua_pushvalue) (lua_State *L, int idx);
245 int (*dll_lua_isnumber) (lua_State *L, int idx);
246 int (*dll_lua_isstring) (lua_State *L, int idx);
247 int (*dll_lua_type) (lua_State *L, int idx);
248 int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
249 int (*dll_lua_toboolean) (lua_State *L, int idx);
250 const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
251 void *(*dll_lua_touserdata) (lua_State *L, int idx);
252 void (*dll_lua_pushnil) (lua_State *L);
253 void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
254 void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
255 void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
256 void (*dll_lua_pushstring) (lua_State *L, const char *s);
257 const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
258 void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
259 void (*dll_lua_pushboolean) (lua_State *L, int b);
260 void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
261 void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
262 #if LUA_VERSION_NUM <= 502
263 void (*dll_lua_rawget) (lua_State *L, int idx);
264 void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
265 #else
266 int (*dll_lua_rawget) (lua_State *L, int idx);
267 int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
268 #endif
269 void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
270 #if LUA_VERSION_NUM >= 504
271 void *(*dll_lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
272 #else
273 void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
274 #endif
275 int (*dll_lua_getmetatable) (lua_State *L, int objindex);
276 void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
277 void (*dll_lua_rawset) (lua_State *L, int idx);
278 #if LUA_VERSION_NUM <= 502
279 void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
280 #else
281 void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n);
282 #endif
283 int (*dll_lua_setmetatable) (lua_State *L, int objindex);
284 int (*dll_lua_next) (lua_State *L, int idx);
285 // libs
286 int (*dll_luaopen_base) (lua_State *L);
287 int (*dll_luaopen_table) (lua_State *L);
288 int (*dll_luaopen_string) (lua_State *L);
289 int (*dll_luaopen_math) (lua_State *L);
290 int (*dll_luaopen_io) (lua_State *L);
291 int (*dll_luaopen_os) (lua_State *L);
292 int (*dll_luaopen_package) (lua_State *L);
293 int (*dll_luaopen_debug) (lua_State *L);
294 void (*dll_luaL_openlibs) (lua_State *L);
295 
296 typedef void **luaV_function;
297 typedef struct {
298     const char *name;
299     luaV_function func;
300 } luaV_Reg;
301 
302 static const luaV_Reg luaV_dll[] = {
303     // lauxlib
304 #if LUA_VERSION_NUM <= 501
305     {"luaL_register", (luaV_function) &dll_luaL_register},
306     {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
307     {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
308     {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
309     {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
310     {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
311 #else
312     {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
313     {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
314     {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
315     {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
316     {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
317 #endif
318     {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
319     {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
320     {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
321     {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
322     {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
323     {"luaL_error", (luaV_function) &dll_luaL_error},
324     {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
325     {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
326     {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
327     {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
328     // lua
329 #if LUA_VERSION_NUM <= 501
330     {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
331     {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
332     {"lua_call", (luaV_function) &dll_lua_call},
333     {"lua_pcall", (luaV_function) &dll_lua_pcall},
334 #else
335     {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
336     {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
337     {"lua_callk", (luaV_function) &dll_lua_callk},
338     {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
339     {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
340     {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
341 #endif
342 #if LUA_VERSION_NUM <= 502
343     {"lua_replace", (luaV_function) &dll_lua_replace},
344     {"lua_remove", (luaV_function) &dll_lua_remove},
345 #endif
346 #if LUA_VERSION_NUM >= 503
347     {"lua_rotate", (luaV_function) &dll_lua_rotate},
348     {"lua_copy", (luaV_function) &dll_lua_copy},
349 #endif
350     {"lua_typename", (luaV_function) &dll_lua_typename},
351     {"lua_close", (luaV_function) &dll_lua_close},
352     {"lua_gettop", (luaV_function) &dll_lua_gettop},
353     {"lua_settop", (luaV_function) &dll_lua_settop},
354     {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
355     {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
356     {"lua_isstring", (luaV_function) &dll_lua_isstring},
357     {"lua_type", (luaV_function) &dll_lua_type},
358     {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
359     {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
360     {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
361     {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
362     {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
363     {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
364     {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
365     {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
366     {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
367     {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
368     {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
369     {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
370     {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
371     {"lua_getfield", (luaV_function) &dll_lua_getfield},
372     {"lua_rawget", (luaV_function) &dll_lua_rawget},
373     {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
374     {"lua_createtable", (luaV_function) &dll_lua_createtable},
375 #if LUA_VERSION_NUM >= 504
376     {"lua_newuserdatauv", (luaV_function) &dll_lua_newuserdatauv},
377 #else
378     {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
379 #endif
380     {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
381     {"lua_setfield", (luaV_function) &dll_lua_setfield},
382     {"lua_rawset", (luaV_function) &dll_lua_rawset},
383     {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
384     {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
385     {"lua_next", (luaV_function) &dll_lua_next},
386     // libs
387     {"luaopen_base", (luaV_function) &dll_luaopen_base},
388     {"luaopen_table", (luaV_function) &dll_luaopen_table},
389     {"luaopen_string", (luaV_function) &dll_luaopen_string},
390     {"luaopen_math", (luaV_function) &dll_luaopen_math},
391     {"luaopen_io", (luaV_function) &dll_luaopen_io},
392     {"luaopen_os", (luaV_function) &dll_luaopen_os},
393     {"luaopen_package", (luaV_function) &dll_luaopen_package},
394     {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
395     {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
396     {NULL, NULL}
397 };
398 
399 static HANDLE hinstLua = NULL;
400 
401     static void
402 end_dynamic_lua(void)
403 {
404     if (hinstLua)
405     {
406 	close_dll(hinstLua);
407 	hinstLua = 0;
408     }
409 }
410 
411     static int
412 lua_link_init(char *libname, int verbose)
413 {
414     const luaV_Reg *reg;
415     if (hinstLua) return OK;
416     hinstLua = load_dll(libname);
417     if (!hinstLua)
418     {
419 	if (verbose)
420 	    semsg(_(e_loadlib), libname);
421 	return FAIL;
422     }
423     for (reg = luaV_dll; reg->func; reg++)
424     {
425 	if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
426 	{
427 	    close_dll(hinstLua);
428 	    hinstLua = 0;
429 	    if (verbose)
430 		semsg(_(e_loadfunc), reg->name);
431 	    return FAIL;
432 	}
433     }
434     return OK;
435 }
436 #endif // DYNAMIC_LUA
437 
438 #if defined(DYNAMIC_LUA) || defined(PROTO)
439     int
440 lua_enabled(int verbose)
441 {
442     return lua_link_init((char *)p_luadll, verbose) == OK;
443 }
444 #endif
445 
446 #if LUA_VERSION_NUM > 501
447     static int
448 luaL_typeerror(lua_State *L, int narg, const char *tname)
449 {
450     const char *msg = lua_pushfstring(L, "%s expected, got %s",
451 	    tname, luaL_typename(L, narg));
452     return luaL_argerror(L, narg, msg);
453 }
454 #endif
455 
456 
457 // =======   Internal   =======
458 
459     static void
460 luaV_newmetatable(lua_State *L, const char *tname)
461 {
462     lua_newtable(L);
463     lua_pushlightuserdata(L, (void *) tname);
464     lua_pushvalue(L, -2);
465     lua_rawset(L, LUA_REGISTRYINDEX);
466 }
467 
468     static void *
469 luaV_toudata(lua_State *L, int ud, const char *tname)
470 {
471     void *p = lua_touserdata(L, ud);
472 
473     if (p != NULL) // value is userdata?
474     {
475 	if (lua_getmetatable(L, ud)) // does it have a metatable?
476 	{
477 	    luaV_getfield(L, tname); // get metatable
478 	    if (lua_rawequal(L, -1, -2)) // MTs match?
479 	    {
480 		lua_pop(L, 2); // MTs
481 		return p;
482 	    }
483 	}
484     }
485     return NULL;
486 }
487 
488     static void *
489 luaV_checkcache(lua_State *L, void *p)
490 {
491     luaV_getudata(L, p);
492     if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
493     lua_pop(L, 1);
494     return p;
495 }
496 
497 #define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
498 
499 #define luaV_checkvalid(L,luatyp,ud) \
500     luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
501 
502     static void *
503 luaV_checkudata(lua_State *L, int ud, const char *tname)
504 {
505     void *p = luaV_toudata(L, ud, tname);
506     if (p == NULL) luaL_typeerror(L, ud, tname);
507     return p;
508 }
509 
510     static void
511 luaV_pushtypval(lua_State *L, typval_T *tv)
512 {
513     if (tv == NULL)
514     {
515 	lua_pushnil(L);
516 	return;
517     }
518     switch (tv->v_type)
519     {
520 	case VAR_STRING:
521 	    lua_pushstring(L, tv->vval.v_string == NULL
522 					    ? "" : (char *)tv->vval.v_string);
523 	    break;
524 	case VAR_NUMBER:
525 	    lua_pushinteger(L, (int) tv->vval.v_number);
526 	    break;
527 #ifdef FEAT_FLOAT
528 	case VAR_FLOAT:
529 	    lua_pushnumber(L, (lua_Number) tv->vval.v_float);
530 	    break;
531 #endif
532 	case VAR_LIST:
533 	    luaV_pushlist(L, tv->vval.v_list);
534 	    break;
535 	case VAR_DICT:
536 	    luaV_pushdict(L, tv->vval.v_dict);
537 	    break;
538 	case VAR_BOOL:
539 	case VAR_SPECIAL:
540 	    if (tv->vval.v_number <= VVAL_TRUE)
541 		lua_pushinteger(L, (int) tv->vval.v_number);
542 	    else
543 		lua_pushnil(L);
544 	    break;
545 	case VAR_FUNC:
546 	    luaV_pushfuncref(L, tv->vval.v_string);
547 	    break;
548 	case VAR_BLOB:
549 	    luaV_pushblob(L, tv->vval.v_blob);
550 	    break;
551 	default:
552 	    lua_pushnil(L);
553     }
554 }
555 
556 /*
557  * Converts lua value at 'pos' to typval 'tv'.
558  * Returns OK or FAIL.
559  */
560     static int
561 luaV_totypval(lua_State *L, int pos, typval_T *tv)
562 {
563     int status = OK;
564 
565     switch (lua_type(L, pos))
566     {
567 	case LUA_TBOOLEAN:
568 	    tv->v_type = VAR_BOOL;
569 	    tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
570 	    break;
571 	case LUA_TNIL:
572 	    tv->v_type = VAR_SPECIAL;
573 	    tv->vval.v_number = VVAL_NULL;
574 	    break;
575 	case LUA_TSTRING:
576 	    tv->v_type = VAR_STRING;
577 	    tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
578 	    break;
579 	case LUA_TNUMBER:
580 #ifdef FEAT_FLOAT
581 	    tv->v_type = VAR_FLOAT;
582 	    tv->vval.v_float = (float_T) lua_tonumber(L, pos);
583 #else
584 	    tv->v_type = VAR_NUMBER;
585 	    tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
586 #endif
587 	    break;
588 	case LUA_TUSERDATA:
589 	{
590 	    void *p = lua_touserdata(L, pos);
591 
592 	    if (lua_getmetatable(L, pos)) // has metatable?
593 	    {
594 		// check list
595 		luaV_getfield(L, LUAVIM_LIST);
596 		if (lua_rawequal(L, -1, -2))
597 		{
598 		    tv->v_type = VAR_LIST;
599 		    tv->vval.v_list = *((luaV_List *) p);
600 		    ++tv->vval.v_list->lv_refcount;
601 		    lua_pop(L, 2); // MTs
602 		    break;
603 		}
604 		// check dict
605 		luaV_getfield(L, LUAVIM_DICT);
606 		if (lua_rawequal(L, -1, -3))
607 		{
608 		    tv->v_type = VAR_DICT;
609 		    tv->vval.v_dict = *((luaV_Dict *) p);
610 		    ++tv->vval.v_dict->dv_refcount;
611 		    lua_pop(L, 3); // MTs
612 		    break;
613 		}
614 		// check blob
615 		luaV_getfield(L, LUAVIM_BLOB);
616 		if (lua_rawequal(L, -1, -4))
617 		{
618 		    tv->v_type = VAR_BLOB;
619 		    tv->vval.v_blob = *((luaV_Blob *) p);
620 		    ++tv->vval.v_blob->bv_refcount;
621 		    lua_pop(L, 4); // MTs
622 		    break;
623 		}
624 		// check funcref
625 		luaV_getfield(L, LUAVIM_FUNCREF);
626 		if (lua_rawequal(L, -1, -5))
627 		{
628 		    luaV_Funcref *f = (luaV_Funcref *) p;
629 		    func_ref(f->name);
630 		    tv->v_type = VAR_FUNC;
631 		    tv->vval.v_string = vim_strsave(f->name);
632 		    lua_pop(L, 5); // MTs
633 		    break;
634 		}
635 		lua_pop(L, 4); // MTs
636 	    }
637 	}
638 	// FALLTHROUGH
639 	default:
640 	    tv->v_type = VAR_NUMBER;
641 	    tv->vval.v_number = 0;
642 	    status = FAIL;
643     }
644     return status;
645 }
646 
647 /*
648  * similar to luaL_addlstring, but replaces \0 with \n if toline and
649  * \n with \0 otherwise
650  */
651     static void
652 luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
653 {
654     while (l--)
655     {
656 	if (*s == '\0' && toline)
657 	    luaL_addchar(b, '\n');
658 	else if (*s == '\n' && !toline)
659 	    luaL_addchar(b, '\0');
660 	else
661 	    luaL_addchar(b, *s);
662 	s++;
663     }
664 }
665 
666     static void
667 luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
668 {
669     const char *s = (const char *) ml_get_buf(buf, n, FALSE);
670     luaL_Buffer b;
671     luaL_buffinit(L, &b);
672     luaV_addlstring(&b, s, strlen(s), 0);
673     luaL_pushresult(&b);
674 }
675 
676     static char_u *
677 luaV_toline(lua_State *L, int pos)
678 {
679     size_t l;
680     const char *s = lua_tolstring(L, pos, &l);
681 
682     luaL_Buffer b;
683     luaL_buffinit(L, &b);
684     luaV_addlstring(&b, s, l, 1);
685     luaL_pushresult(&b);
686     return (char_u *) lua_tostring(L, -1);
687 }
688 
689 /*
690  * pops a string s from the top of the stack and calls mf(t) for pieces t of
691  * s separated by newlines
692  */
693     static void
694 luaV_msgfunc(lua_State *L, msgfunc_T mf)
695 {
696     luaL_Buffer b;
697     size_t l;
698     const char *p, *s = lua_tolstring(L, -1, &l);
699     luaL_buffinit(L, &b);
700     luaV_addlstring(&b, s, l, 0);
701     luaL_pushresult(&b);
702     // break string
703     p = s = lua_tolstring(L, -1, &l);
704     while (l--)
705     {
706 	if (*p++ == '\0') // break?
707 	{
708 	    mf((char_u *) s);
709 	    s = p;
710 	}
711     }
712     mf((char_u *) s);
713     lua_pop(L, 2); // original and modified strings
714 }
715 
716 #define luaV_newtype(typ,tname,luatyp,luatname) \
717 	static luatyp * \
718     luaV_new##tname(lua_State *L, typ *obj) \
719     { \
720 	luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
721 	*o = obj; \
722 	luaV_setudata(L, obj); /* cache[obj] = udata */ \
723 	luaV_getfield(L, luatname); \
724 	lua_setmetatable(L, -2); \
725 	return o; \
726     }
727 
728 #define luaV_pushtype(typ,tname,luatyp) \
729 	static luatyp * \
730     luaV_push##tname(lua_State *L, typ *obj) \
731     { \
732 	luatyp *o = NULL; \
733 	if (obj == NULL) \
734 	    lua_pushnil(L); \
735 	else { \
736 	    luaV_getudata(L, obj); \
737 	    if (lua_isnil(L, -1)) /* not interned? */ \
738 	    { \
739 		lua_pop(L, 1); \
740 		o = luaV_new##tname(L, obj); \
741 	    } \
742 	    else \
743 		o = (luatyp *) lua_touserdata(L, -1); \
744 	} \
745 	return o; \
746     }
747 
748 #define luaV_type_tostring(tname,luatname) \
749 	static int \
750     luaV_##tname##_tostring(lua_State *L) \
751     { \
752 	lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
753 	return 1; \
754     }
755 
756 // =======   List type   =======
757 
758     static luaV_List *
759 luaV_newlist(lua_State *L, list_T *lis)
760 {
761     luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
762     *l = lis;
763     lis->lv_refcount++; // reference in Lua
764     luaV_setudata(L, lis); // cache[lis] = udata
765     luaV_getfield(L, LUAVIM_LIST);
766     lua_setmetatable(L, -2);
767     return l;
768 }
769 
770 luaV_pushtype(list_T, list, luaV_List)
771 luaV_type_tostring(list, LUAVIM_LIST)
772 
773     static int
774 luaV_list_len(lua_State *L)
775 {
776     list_T *l = luaV_unbox(L, luaV_List, 1);
777     lua_pushinteger(L, (int) list_len(l));
778     return 1;
779 }
780 
781     static int
782 luaV_list_iter(lua_State *L)
783 {
784     listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
785     if (li == NULL) return 0;
786     luaV_pushtypval(L, &li->li_tv);
787     lua_pushlightuserdata(L, (void *) li->li_next);
788     lua_replace(L, lua_upvalueindex(2));
789     return 1;
790 }
791 
792     static int
793 luaV_list_call(lua_State *L)
794 {
795     list_T *l = luaV_unbox(L, luaV_List, 1);
796     lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
797     lua_pushlightuserdata(L, (void *) l->lv_first);
798     lua_pushcclosure(L, luaV_list_iter, 2);
799     return 1;
800 }
801 
802     static int
803 luaV_list_index(lua_State *L)
804 {
805     list_T *l = luaV_unbox(L, luaV_List, 1);
806     if (lua_isnumber(L, 2)) // list item?
807     {
808 	listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
809 	if (li == NULL)
810 	    lua_pushnil(L);
811 	else
812 	    luaV_pushtypval(L, &li->li_tv);
813     }
814     else if (lua_isstring(L, 2)) // method?
815     {
816 	const char *s = lua_tostring(L, 2);
817 	if (strncmp(s, "add", 3) == 0
818 		|| strncmp(s, "insert", 6) == 0)
819 	{
820 	    lua_getmetatable(L, 1);
821 	    lua_getfield(L, -1, s);
822 	}
823 	else
824 	    lua_pushnil(L);
825     }
826     else
827 	lua_pushnil(L);
828     return 1;
829 }
830 
831     static int
832 luaV_list_newindex(lua_State *L)
833 {
834     list_T *l = luaV_unbox(L, luaV_List, 1);
835     long n = (long) luaL_checkinteger(L, 2);
836     listitem_T *li;
837     if (l->lv_lock)
838 	luaL_error(L, "list is locked");
839     li = list_find(l, n);
840     if (li == NULL) return 0;
841     if (lua_isnil(L, 3)) // remove?
842     {
843 	vimlist_remove(l, li, li);
844 	listitem_free(l, li);
845     }
846     else
847     {
848 	typval_T v;
849 	luaV_checktypval(L, 3, &v, "setting list item");
850 	clear_tv(&li->li_tv);
851 	copy_tv(&v, &li->li_tv);
852 	clear_tv(&v);
853     }
854     return 0;
855 }
856 
857     static int
858 luaV_list_add(lua_State *L)
859 {
860     luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
861     list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
862     typval_T v;
863     if (l->lv_lock)
864 	luaL_error(L, "list is locked");
865     lua_settop(L, 2);
866     luaV_checktypval(L, 2, &v, "adding list item");
867     if (list_append_tv(l, &v) == FAIL)
868 	luaL_error(L, "failed to add item to list");
869     clear_tv(&v);
870     lua_settop(L, 1);
871     return 1;
872 }
873 
874     static int
875 luaV_list_insert(lua_State *L)
876 {
877     luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
878     list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
879     long pos = (long) luaL_optinteger(L, 3, 0);
880     listitem_T *li = NULL;
881     typval_T v;
882     if (l->lv_lock)
883 	luaL_error(L, "list is locked");
884     if (pos < l->lv_len)
885     {
886 	li = list_find(l, pos);
887 	if (li == NULL)
888 	    luaL_error(L, "invalid position");
889     }
890     lua_settop(L, 2);
891     luaV_checktypval(L, 2, &v, "inserting list item");
892     if (list_insert_tv(l, &v, li) == FAIL)
893 	luaL_error(L, "failed to add item to list");
894     clear_tv(&v);
895     lua_settop(L, 1);
896     return 1;
897 }
898 
899 static const luaL_Reg luaV_List_mt[] = {
900     {"__tostring", luaV_list_tostring},
901     {"__len", luaV_list_len},
902     {"__call", luaV_list_call},
903     {"__index", luaV_list_index},
904     {"__newindex", luaV_list_newindex},
905     {"add", luaV_list_add},
906     {"insert", luaV_list_insert},
907     {NULL, NULL}
908 };
909 
910 
911 // =======   Dict type   =======
912 
913     static luaV_Dict *
914 luaV_newdict(lua_State *L, dict_T *dic)
915 {
916     luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
917     *d = dic;
918     dic->dv_refcount++; // reference in Lua
919     luaV_setudata(L, dic); // cache[dic] = udata
920     luaV_getfield(L, LUAVIM_DICT);
921     lua_setmetatable(L, -2);
922     return d;
923 }
924 
925 luaV_pushtype(dict_T, dict, luaV_Dict)
926 luaV_type_tostring(dict, LUAVIM_DICT)
927 
928     static int
929 luaV_dict_len(lua_State *L)
930 {
931     dict_T *d = luaV_unbox(L, luaV_Dict, 1);
932     lua_pushinteger(L, (int) dict_len(d));
933     return 1;
934 }
935 
936     static int
937 luaV_dict_iter(lua_State *L UNUSED)
938 {
939 #ifdef FEAT_EVAL
940     hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
941     int n = lua_tointeger(L, lua_upvalueindex(3));
942     dictitem_T *di;
943     if (n <= 0) return 0;
944     while (HASHITEM_EMPTY(hi)) hi++;
945     di = dict_lookup(hi);
946     lua_pushstring(L, (char *) hi->hi_key);
947     luaV_pushtypval(L, &di->di_tv);
948     lua_pushlightuserdata(L, (void *) (hi + 1));
949     lua_replace(L, lua_upvalueindex(2));
950     lua_pushinteger(L, n - 1);
951     lua_replace(L, lua_upvalueindex(3));
952     return 2;
953 #else
954     return 0;
955 #endif
956 }
957 
958     static int
959 luaV_dict_call(lua_State *L)
960 {
961     dict_T *d = luaV_unbox(L, luaV_Dict, 1);
962     hashtab_T *ht = &d->dv_hashtab;
963     lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
964     lua_pushlightuserdata(L, (void *) ht->ht_array);
965     lua_pushinteger(L, ht->ht_used); // # remaining items
966     lua_pushcclosure(L, luaV_dict_iter, 3);
967     return 1;
968 }
969 
970     static int
971 luaV_dict_index(lua_State *L)
972 {
973     dict_T *d = luaV_unbox(L, luaV_Dict, 1);
974     char_u *key = (char_u *) luaL_checkstring(L, 2);
975     dictitem_T *di = dict_find(d, key, -1);
976 
977     if (di == NULL)
978 	lua_pushnil(L);
979     else
980     {
981 	luaV_pushtypval(L, &di->di_tv);
982 	if (di->di_tv.v_type == VAR_FUNC) // funcref?
983 	{
984 	    luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
985 	    f->self = d; // keep "self" reference
986 	    d->dv_refcount++;
987 	}
988     }
989     return 1;
990 }
991 
992     static int
993 luaV_dict_newindex(lua_State *L)
994 {
995     dict_T *d = luaV_unbox(L, luaV_Dict, 1);
996     char_u *key = (char_u *) luaL_checkstring(L, 2);
997     dictitem_T *di;
998     typval_T v;
999 
1000     if (d->dv_lock)
1001 	luaL_error(L, "dict is locked");
1002     if (key == NULL)
1003 	return 0;
1004     if (*key == NUL)
1005 	luaL_error(L, "empty key");
1006     if (!lua_isnil(L, 3)) // read value?
1007     {
1008 	luaV_checktypval(L, 3, &v, "setting dict item");
1009 	if (d->dv_scope == VAR_DEF_SCOPE && v.v_type == VAR_FUNC)
1010 	    luaL_error(L, "cannot assign funcref to builtin scope");
1011     }
1012     di = dict_find(d, key, -1);
1013     if (di == NULL) // non-existing key?
1014     {
1015 	if (lua_isnil(L, 3))
1016 	    return 0;
1017 	di = dictitem_alloc(key);
1018 	if (di == NULL)
1019 	    return 0;
1020 	if (dict_add(d, di) == FAIL)
1021 	{
1022 	    vim_free(di);
1023 	    return 0;
1024 	}
1025     }
1026     else
1027 	clear_tv(&di->di_tv);
1028     if (lua_isnil(L, 3)) // remove?
1029     {
1030 	hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1031 	hash_remove(&d->dv_hashtab, hi);
1032 	dictitem_free(di);
1033     }
1034     else
1035     {
1036 	copy_tv(&v, &di->di_tv);
1037 	clear_tv(&v);
1038     }
1039     return 0;
1040 }
1041 
1042 static const luaL_Reg luaV_Dict_mt[] = {
1043     {"__tostring", luaV_dict_tostring},
1044     {"__len", luaV_dict_len},
1045     {"__call", luaV_dict_call},
1046     {"__index", luaV_dict_index},
1047     {"__newindex", luaV_dict_newindex},
1048     {NULL, NULL}
1049 };
1050 
1051 
1052 // =======   Blob type   =======
1053 
1054     static luaV_Blob *
1055 luaV_newblob(lua_State *L, blob_T *blo)
1056 {
1057     luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1058     *b = blo;
1059     blo->bv_refcount++; // reference in Lua
1060     luaV_setudata(L, blo); // cache[blo] = udata
1061     luaV_getfield(L, LUAVIM_BLOB);
1062     lua_setmetatable(L, -2);
1063     return b;
1064 }
1065 
1066 luaV_pushtype(blob_T, blob, luaV_Blob)
1067 luaV_type_tostring(blob, LUAVIM_BLOB)
1068 
1069     static int
1070 luaV_blob_gc(lua_State *L)
1071 {
1072     blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1073     blob_unref(b);
1074     return 0;
1075 }
1076 
1077     static int
1078 luaV_blob_len(lua_State *L)
1079 {
1080     blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1081     lua_pushinteger(L, (int) blob_len(b));
1082     return 1;
1083 }
1084 
1085     static int
1086 luaV_blob_index(lua_State *L)
1087 {
1088     blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1089     if (lua_isnumber(L, 2))
1090     {
1091 	int idx = luaL_checkinteger(L, 2);
1092 	if (idx < blob_len(b))
1093 	    lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1094 	else
1095 	    lua_pushnil(L);
1096     }
1097     else if (lua_isstring(L, 2))
1098     {
1099 	const char *s = lua_tostring(L, 2);
1100 	if (strncmp(s, "add", 3) == 0)
1101 	{
1102 	    lua_getmetatable(L, 1);
1103 	    lua_getfield(L, -1, s);
1104 	}
1105 	else
1106 	    lua_pushnil(L);
1107     }
1108     else
1109 	lua_pushnil(L);
1110     return 1;
1111 }
1112 
1113     static int
1114 luaV_blob_newindex(lua_State *L)
1115 {
1116     blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1117     if (b->bv_lock)
1118 	luaL_error(L, "blob is locked");
1119     if (lua_isnumber(L, 2))
1120     {
1121 	long len = blob_len(b);
1122 	int idx = luaL_checkinteger(L, 2);
1123 	int val = luaL_checkinteger(L, 3);
1124 	if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
1125 	{
1126 	    blob_set(b, idx, (char_u) val);
1127 	    if (idx == len)
1128 		++b->bv_ga.ga_len;
1129 	}
1130 	else
1131 	    luaL_error(L, "index out of range");
1132     }
1133     return 0;
1134 }
1135 
1136     static int
1137 luaV_blob_add(lua_State *L)
1138 {
1139     luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1140     blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1141     if (b->bv_lock)
1142 	luaL_error(L, "blob is locked");
1143     lua_settop(L, 2);
1144     if (!lua_isstring(L, 2))
1145 	luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1146     else
1147     {
1148 	size_t i, l = 0;
1149 	const char *s = lua_tolstring(L, 2, &l);
1150 
1151 	if (ga_grow(&b->bv_ga, (int)l) == OK)
1152 	    for (i = 0; i < l; ++i)
1153 		ga_append(&b->bv_ga, s[i]);
1154     }
1155     lua_settop(L, 1);
1156     return 1;
1157 }
1158 
1159 static const luaL_Reg luaV_Blob_mt[] = {
1160     {"__tostring", luaV_blob_tostring},
1161     {"__gc", luaV_blob_gc},
1162     {"__len", luaV_blob_len},
1163     {"__index", luaV_blob_index},
1164     {"__newindex", luaV_blob_newindex},
1165     {"add", luaV_blob_add},
1166     {NULL, NULL}
1167 };
1168 
1169 
1170 // =======   Funcref type   =======
1171 
1172     static luaV_Funcref *
1173 luaV_newfuncref(lua_State *L, char_u *name)
1174 {
1175     luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1176 
1177     if (name != NULL)
1178     {
1179 	func_ref(name);
1180 	f->name = vim_strsave(name);
1181     }
1182     f->self = NULL;
1183     luaV_getfield(L, LUAVIM_FUNCREF);
1184     lua_setmetatable(L, -2);
1185     return f;
1186 }
1187 
1188     static luaV_Funcref *
1189 luaV_pushfuncref(lua_State *L, char_u *name)
1190 {
1191     return luaV_newfuncref(L, name);
1192 }
1193 
1194 
1195 luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1196 
1197     static int
1198 luaV_funcref_gc(lua_State *L)
1199 {
1200     luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1201 
1202     func_unref(f->name);
1203     vim_free(f->name);
1204     // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1205     // will be (or has been already) freed by Vim's garbage collection.
1206     return 0;
1207 }
1208 
1209 // equivalent to string(funcref)
1210     static int
1211 luaV_funcref_len(lua_State *L)
1212 {
1213     luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1214 
1215     lua_pushstring(L, (const char *) f->name);
1216     return 1;
1217 }
1218 
1219     static int
1220 luaV_funcref_call(lua_State *L)
1221 {
1222     luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1223     int i, n = lua_gettop(L) - 1; // #args
1224     int status = FAIL;
1225     typval_T args;
1226     typval_T rettv;
1227 
1228     args.v_type = VAR_LIST;
1229     args.vval.v_list = list_alloc();
1230     rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1231     if (args.vval.v_list != NULL)
1232     {
1233 	typval_T v;
1234 
1235 	for (i = 0; i < n; i++)
1236 	{
1237 	    luaV_checktypval(L, i + 2, &v, "calling funcref");
1238 	    list_append_tv(args.vval.v_list, &v);
1239 	    clear_tv(&v);
1240 	}
1241 	status = func_call(f->name, &args, NULL, f->self, &rettv);
1242 	if (status == OK)
1243 	    luaV_pushtypval(L, &rettv);
1244 	clear_tv(&args);
1245 	clear_tv(&rettv);
1246     }
1247     if (status != OK)
1248 	luaL_error(L, "cannot call funcref");
1249     return 1;
1250 }
1251 
1252 static const luaL_Reg luaV_Funcref_mt[] = {
1253     {"__tostring", luaV_funcref_tostring},
1254     {"__gc", luaV_funcref_gc},
1255     {"__len", luaV_funcref_len},
1256     {"__call", luaV_funcref_call},
1257     {NULL, NULL}
1258 };
1259 
1260 
1261 // =======   Buffer type   =======
1262 
1263 luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1264 luaV_pushtype(buf_T, buffer, luaV_Buffer)
1265 luaV_type_tostring(buffer, LUAVIM_BUFFER)
1266 
1267     static int
1268 luaV_buffer_len(lua_State *L)
1269 {
1270     buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1271     lua_pushinteger(L, b->b_ml.ml_line_count);
1272     return 1;
1273 }
1274 
1275     static int
1276 luaV_buffer_call(lua_State *L)
1277 {
1278     buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1279     lua_settop(L, 1);
1280     set_curbuf(b, DOBUF_SPLIT);
1281     return 1;
1282 }
1283 
1284     static int
1285 luaV_buffer_index(lua_State *L)
1286 {
1287     buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1288     linenr_T n = (linenr_T) lua_tointeger(L, 2);
1289     if (n > 0 && n <= b->b_ml.ml_line_count)
1290 	luaV_pushline(L, b, n);
1291     else if (lua_isstring(L, 2))
1292     {
1293 	const char *s = lua_tostring(L, 2);
1294 	if (strncmp(s, "name", 4) == 0)
1295 	    lua_pushstring(L, (b->b_sfname == NULL)
1296 					? "" : (char *) b->b_sfname);
1297 	else if (strncmp(s, "fname", 5) == 0)
1298 	    lua_pushstring(L, (b->b_ffname == NULL)
1299 					? "" : (char *) b->b_ffname);
1300 	else if (strncmp(s, "number", 6) == 0)
1301 	    lua_pushinteger(L, b->b_fnum);
1302 	// methods
1303 	else if (strncmp(s,   "insert", 6) == 0
1304 		|| strncmp(s, "next", 4) == 0
1305 		|| strncmp(s, "previous", 8) == 0
1306 		|| strncmp(s, "isvalid", 7) == 0)
1307 	{
1308 	    lua_getmetatable(L, 1);
1309 	    lua_getfield(L, -1, s);
1310 	}
1311 	else
1312 	    lua_pushnil(L);
1313     }
1314     else
1315 	lua_pushnil(L);
1316     return 1;
1317 }
1318 
1319     static int
1320 luaV_buffer_newindex(lua_State *L)
1321 {
1322     buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1323     linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1324 #ifdef HAVE_SANDBOX
1325     luaV_checksandbox(L);
1326 #endif
1327     if (n < 1 || n > b->b_ml.ml_line_count)
1328 	luaL_error(L, "invalid line number");
1329     if (lua_isnil(L, 3)) // delete line
1330     {
1331 	buf_T *buf = curbuf;
1332 	curbuf = b;
1333 	if (u_savedel(n, 1L) == FAIL)
1334 	{
1335 	    curbuf = buf;
1336 	    luaL_error(L, "cannot save undo information");
1337 	}
1338 	else if (ml_delete(n, FALSE) == FAIL)
1339 	{
1340 	    curbuf = buf;
1341 	    luaL_error(L, "cannot delete line");
1342 	}
1343 	else
1344 	{
1345 	    deleted_lines_mark(n, 1L);
1346 	    if (b == curwin->w_buffer) // fix cursor in current window?
1347 	    {
1348 		if (curwin->w_cursor.lnum >= n)
1349 		{
1350 		    if (curwin->w_cursor.lnum > n)
1351 		    {
1352 			curwin->w_cursor.lnum -= 1;
1353 			check_cursor_col();
1354 		    }
1355 		    else check_cursor();
1356 		    changed_cline_bef_curs();
1357 		}
1358 		invalidate_botline();
1359 	    }
1360 	}
1361 	curbuf = buf;
1362     }
1363     else if (lua_isstring(L, 3)) // update line
1364     {
1365 	buf_T *buf = curbuf;
1366 	curbuf = b;
1367 	if (u_savesub(n) == FAIL)
1368 	{
1369 	    curbuf = buf;
1370 	    luaL_error(L, "cannot save undo information");
1371 	}
1372 	else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1373 	{
1374 	    curbuf = buf;
1375 	    luaL_error(L, "cannot replace line");
1376 	}
1377 	else changed_bytes(n, 0);
1378 	curbuf = buf;
1379 	if (b == curwin->w_buffer)
1380 	    check_cursor_col();
1381     }
1382     else
1383 	luaL_error(L, "wrong argument to change line");
1384     return 0;
1385 }
1386 
1387     static int
1388 luaV_buffer_insert(lua_State *L)
1389 {
1390     luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1391     buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1392     linenr_T last = b->b_ml.ml_line_count;
1393     linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1394     buf_T *buf;
1395     luaL_checktype(L, 2, LUA_TSTRING);
1396 #ifdef HAVE_SANDBOX
1397     luaV_checksandbox(L);
1398 #endif
1399     // fix insertion line
1400     if (n < 0) n = 0;
1401     if (n > last) n = last;
1402     // insert
1403     buf = curbuf;
1404     curbuf = b;
1405     if (u_save(n, n + 1) == FAIL)
1406     {
1407 	curbuf = buf;
1408 	luaL_error(L, "cannot save undo information");
1409     }
1410     else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1411     {
1412 	curbuf = buf;
1413 	luaL_error(L, "cannot insert line");
1414     }
1415     else
1416 	appended_lines_mark(n, 1L);
1417     curbuf = buf;
1418     update_screen(VALID);
1419     return 0;
1420 }
1421 
1422     static int
1423 luaV_buffer_next(lua_State *L)
1424 {
1425     luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1426     buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1427     luaV_pushbuffer(L, buf->b_next);
1428     return 1;
1429 }
1430 
1431     static int
1432 luaV_buffer_previous(lua_State *L)
1433 {
1434     luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1435     buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1436     luaV_pushbuffer(L, buf->b_prev);
1437     return 1;
1438 }
1439 
1440     static int
1441 luaV_buffer_isvalid(lua_State *L)
1442 {
1443     luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1444     luaV_getudata(L, *b);
1445     lua_pushboolean(L, !lua_isnil(L, -1));
1446     return 1;
1447 }
1448 
1449 static const luaL_Reg luaV_Buffer_mt[] = {
1450     {"__tostring", luaV_buffer_tostring},
1451     {"__len", luaV_buffer_len},
1452     {"__call", luaV_buffer_call},
1453     {"__index", luaV_buffer_index},
1454     {"__newindex", luaV_buffer_newindex},
1455     {"insert", luaV_buffer_insert},
1456     {"next", luaV_buffer_next},
1457     {"previous", luaV_buffer_previous},
1458     {"isvalid", luaV_buffer_isvalid},
1459     {NULL, NULL}
1460 };
1461 
1462 
1463 // =======   Window type   =======
1464 
1465 luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1466 luaV_pushtype(win_T, window, luaV_Window)
1467 luaV_type_tostring(window, LUAVIM_WINDOW)
1468 
1469     static int
1470 luaV_window_call(lua_State *L)
1471 {
1472     win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
1473     lua_settop(L, 1);
1474     win_goto(w);
1475     return 1;
1476 }
1477 
1478     static int
1479 luaV_window_index(lua_State *L)
1480 {
1481     win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
1482     const char *s = luaL_checkstring(L, 2);
1483     if (strncmp(s, "buffer", 6) == 0)
1484 	luaV_pushbuffer(L, w->w_buffer);
1485     else if (strncmp(s, "line", 4) == 0)
1486 	lua_pushinteger(L, w->w_cursor.lnum);
1487     else if (strncmp(s, "col", 3) == 0)
1488 	lua_pushinteger(L, w->w_cursor.col + 1);
1489     else if (strncmp(s, "width", 5) == 0)
1490 	lua_pushinteger(L, w->w_width);
1491     else if (strncmp(s, "height", 6) == 0)
1492 	lua_pushinteger(L, w->w_height);
1493     // methods
1494     else if (strncmp(s,   "next", 4) == 0
1495 	    || strncmp(s, "previous", 8) == 0
1496 	    || strncmp(s, "isvalid", 7) == 0)
1497     {
1498 	lua_getmetatable(L, 1);
1499 	lua_getfield(L, -1, s);
1500     }
1501     else
1502 	lua_pushnil(L);
1503     return 1;
1504 }
1505 
1506     static int
1507 luaV_window_newindex(lua_State *L)
1508 {
1509     win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
1510     const char *s = luaL_checkstring(L, 2);
1511     int v = luaL_checkinteger(L, 3);
1512     if (strncmp(s, "line", 4) == 0)
1513     {
1514 #ifdef HAVE_SANDBOX
1515 	luaV_checksandbox(L);
1516 #endif
1517 	if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
1518 	    luaL_error(L, "line out of range");
1519 	w->w_cursor.lnum = v;
1520 	update_screen(VALID);
1521     }
1522     else if (strncmp(s, "col", 3) == 0)
1523     {
1524 #ifdef HAVE_SANDBOX
1525 	luaV_checksandbox(L);
1526 #endif
1527 	w->w_cursor.col = v - 1;
1528 	w->w_set_curswant = TRUE;
1529 	update_screen(VALID);
1530     }
1531     else if (strncmp(s, "width", 5) == 0)
1532     {
1533 	win_T *win = curwin;
1534 #ifdef FEAT_GUI
1535 	need_mouse_correct = TRUE;
1536 #endif
1537 	curwin = w;
1538 	win_setwidth(v);
1539 	curwin = win;
1540     }
1541     else if (strncmp(s, "height", 6) == 0)
1542     {
1543 	win_T *win = curwin;
1544 #ifdef FEAT_GUI
1545 	need_mouse_correct = TRUE;
1546 #endif
1547 	curwin = w;
1548 	win_setheight(v);
1549 	curwin = win;
1550     }
1551     else
1552 	luaL_error(L, "invalid window property: `%s'", s);
1553     return 0;
1554 }
1555 
1556     static int
1557 luaV_window_next(lua_State *L)
1558 {
1559     luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
1560     win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1561     luaV_pushwindow(L, win->w_next);
1562     return 1;
1563 }
1564 
1565     static int
1566 luaV_window_previous(lua_State *L)
1567 {
1568     luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
1569     win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1570     luaV_pushwindow(L, win->w_prev);
1571     return 1;
1572 }
1573 
1574     static int
1575 luaV_window_isvalid(lua_State *L)
1576 {
1577     luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
1578     luaV_getudata(L, *w);
1579     lua_pushboolean(L, !lua_isnil(L, -1));
1580     return 1;
1581 }
1582 
1583 static const luaL_Reg luaV_Window_mt[] = {
1584     {"__tostring", luaV_window_tostring},
1585     {"__call", luaV_window_call},
1586     {"__index", luaV_window_index},
1587     {"__newindex", luaV_window_newindex},
1588     {"next", luaV_window_next},
1589     {"previous", luaV_window_previous},
1590     {"isvalid", luaV_window_isvalid},
1591     {NULL, NULL}
1592 };
1593 
1594 
1595 // =======   Vim module   =======
1596 
1597     static int
1598 luaV_print(lua_State *L)
1599 {
1600     int i, n = lua_gettop(L); // nargs
1601     const char *s;
1602     size_t l;
1603     luaL_Buffer b;
1604     luaL_buffinit(L, &b);
1605     lua_getglobal(L, "tostring");
1606     for (i = 1; i <= n; i++)
1607     {
1608 	lua_pushvalue(L, -1); // tostring
1609 	lua_pushvalue(L, i); // arg
1610 	lua_call(L, 1, 1);
1611 	s = lua_tolstring(L, -1, &l);
1612 	if (s == NULL)
1613 	    return luaL_error(L, "cannot convert to string");
1614 	if (i > 1) luaL_addchar(&b, ' '); // use space instead of tab
1615 	luaV_addlstring(&b, s, l, 0);
1616 	lua_pop(L, 1);
1617     }
1618     luaL_pushresult(&b);
1619     if (!got_int)
1620 	luaV_msg(L);
1621     return 0;
1622 }
1623 
1624     static int
1625 luaV_debug(lua_State *L)
1626 {
1627     lua_settop(L, 0);
1628     lua_getglobal(L, "vim");
1629     lua_getfield(L, -1, "eval");
1630     lua_remove(L, -2); // vim.eval at position 1
1631     for (;;)
1632     {
1633 	const char *input;
1634 	size_t l;
1635 	lua_pushvalue(L, 1); // vim.eval
1636 	lua_pushliteral(L, "input('lua_debug> ')");
1637 	lua_call(L, 1, 1); // return string
1638 	input = lua_tolstring(L, -1, &l);
1639 	if (l == 0 || strcmp(input, "cont") == 0)
1640 	    return 0;
1641 	msg_putchar('\n'); // avoid outputting on input line
1642 	if (luaL_loadbuffer(L, input, l, "=(debug command)")
1643 		|| lua_pcall(L, 0, 0, 0))
1644 	    luaV_emsg(L);
1645 	lua_settop(L, 1); // remove eventual returns, but keep vim.eval
1646     }
1647 }
1648 
1649     static int
1650 luaV_command(lua_State *L)
1651 {
1652     do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1653     update_screen(VALID);
1654     return 0;
1655 }
1656 
1657     static int
1658 luaV_eval(lua_State *L)
1659 {
1660     typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1661     if (tv == NULL) luaL_error(L, "invalid expression");
1662     luaV_pushtypval(L, tv);
1663     free_tv(tv);
1664     return 1;
1665 }
1666 
1667     static int
1668 luaV_beep(lua_State *L UNUSED)
1669 {
1670     vim_beep(BO_LANG);
1671     return 0;
1672 }
1673 
1674     static int
1675 luaV_line(lua_State *L)
1676 {
1677     luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1678     return 1;
1679 }
1680 
1681     static int
1682 luaV_list(lua_State *L)
1683 {
1684     list_T *l;
1685     int initarg = !lua_isnoneornil(L, 1);
1686 
1687     if (initarg && lua_type(L, 1) != LUA_TTABLE)
1688 	luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1689     l = list_alloc();
1690     if (l == NULL)
1691 	lua_pushnil(L);
1692     else
1693     {
1694 	luaV_newlist(L, l);
1695 	if (initarg) // traverse table to init list
1696 	{
1697 	    int notnil, i = 0;
1698 	    typval_T v;
1699 	    do
1700 	    {
1701 		lua_rawgeti(L, 1, ++i);
1702 		notnil = !lua_isnil(L, -1);
1703 		if (notnil)
1704 		{
1705 		    luaV_checktypval(L, -1, &v, "vim.list");
1706 		    list_append_tv(l, &v);
1707 		    clear_tv(&v);
1708 		}
1709 		lua_pop(L, 1); // value
1710 	    } while (notnil);
1711 	}
1712     }
1713     return 1;
1714 }
1715 
1716     static int
1717 luaV_dict(lua_State *L)
1718 {
1719     dict_T *d;
1720     int initarg = !lua_isnoneornil(L, 1);
1721 
1722     if (initarg && lua_type(L, 1) != LUA_TTABLE)
1723 	luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1724     d = dict_alloc();
1725     if (d == NULL)
1726 	lua_pushnil(L);
1727     else
1728     {
1729 	luaV_newdict(L, d);
1730 	if (initarg) // traverse table to init dict
1731 	{
1732 	    lua_pushnil(L);
1733 	    while (lua_next(L, 1))
1734 	    {
1735 		char_u *key;
1736 		dictitem_T *di;
1737 		typval_T v;
1738 
1739 		lua_pushvalue(L, -2); // dup key in case it's a number
1740 		key = (char_u *) lua_tostring(L, -1);
1741 		if (key == NULL)
1742 		{
1743 		    lua_pushnil(L);
1744 		    return 1;
1745 		}
1746 		if (*key == NUL)
1747 		    luaL_error(L, "table has empty key");
1748 		luaV_checktypval(L, -2, &v, "vim.dict"); // value
1749 		di = dictitem_alloc(key);
1750 		if (di == NULL || dict_add(d, di) == FAIL)
1751 		{
1752 		    vim_free(di);
1753 		    lua_pushnil(L);
1754 		    return 1;
1755 		}
1756 		copy_tv(&v, &di->di_tv);
1757 		clear_tv(&v);
1758 		lua_pop(L, 2); // key copy and value
1759 	    }
1760 	}
1761     }
1762     return 1;
1763 }
1764 
1765     static int
1766 luaV_blob(lua_State *L)
1767 {
1768     blob_T *b;
1769     int initarg = !lua_isnoneornil(L, 1);
1770 
1771     if (initarg && !lua_isstring(L, 1))
1772 	luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
1773     b = blob_alloc();
1774     if (b == NULL)
1775 	lua_pushnil(L);
1776     else
1777     {
1778 	luaV_newblob(L, b);
1779 	if (initarg)
1780 	{
1781 	    size_t i, l = 0;
1782 	    const char *s = lua_tolstring(L, 1, &l);
1783 
1784 	    if (ga_grow(&b->bv_ga, (int)l) == OK)
1785 		for (i = 0; i < l; ++i)
1786 		    ga_append(&b->bv_ga, s[i]);
1787 	}
1788     }
1789     return 1;
1790 }
1791 
1792     static int
1793 luaV_funcref(lua_State *L)
1794 {
1795     const char *name = luaL_checkstring(L, 1);
1796     // note: not checking if function exists (needs function_exists)
1797     if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1798 	luaL_error(L, "invalid function name: %s", name);
1799     luaV_newfuncref(L, (char_u *) name);
1800     return 1;
1801 }
1802 
1803     static int
1804 luaV_buffer(lua_State *L)
1805 {
1806     buf_T *buf;
1807     if (lua_isstring(L, 1)) // get by number or name?
1808     {
1809 	if (lua_isnumber(L, 1)) // by number?
1810 	{
1811 	    int n = lua_tointeger(L, 1);
1812 	    FOR_ALL_BUFFERS(buf)
1813 		if (buf->b_fnum == n) break;
1814 	}
1815 	else // by name
1816 	{
1817 	    size_t l;
1818 	    const char *s = lua_tolstring(L, 1, &l);
1819 	    FOR_ALL_BUFFERS(buf)
1820 	    {
1821 		if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1822 		{
1823 		    if (l == 0) break;
1824 		}
1825 		else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1826 			|| strncmp(s, (char *)buf->b_sfname, l) == 0)
1827 		    break;
1828 	    }
1829 	}
1830     }
1831     else
1832 	buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
1833     luaV_pushbuffer(L, buf);
1834     return 1;
1835 }
1836 
1837     static int
1838 luaV_window(lua_State *L)
1839 {
1840     win_T *win;
1841     if (lua_isnumber(L, 1)) // get by number?
1842     {
1843 	int n = lua_tointeger(L, 1);
1844 	for (win = firstwin; win != NULL; win = win->w_next, n--)
1845 	    if (n == 1) break;
1846     }
1847     else
1848 	win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
1849     luaV_pushwindow(L, win);
1850     return 1;
1851 }
1852 
1853     static int
1854 luaV_open(lua_State *L)
1855 {
1856     char_u *s = NULL;
1857 #ifdef HAVE_SANDBOX
1858     luaV_checksandbox(L);
1859 #endif
1860     if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
1861     luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
1862     return 1;
1863 }
1864 
1865     static int
1866 luaV_type(lua_State *L)
1867 {
1868     luaL_checkany(L, 1);
1869     if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
1870     {
1871 	lua_settop(L, 1);
1872 	if (lua_getmetatable(L, 1))
1873 	{
1874 	    luaV_getfield(L, LUAVIM_LIST);
1875 	    if (lua_rawequal(L, -1, 2))
1876 	    {
1877 		lua_pushstring(L, "list");
1878 		return 1;
1879 	    }
1880 	    luaV_getfield(L, LUAVIM_DICT);
1881 	    if (lua_rawequal(L, -1, 2))
1882 	    {
1883 		lua_pushstring(L, "dict");
1884 		return 1;
1885 	    }
1886 	    luaV_getfield(L, LUAVIM_BLOB);
1887 	    if (lua_rawequal(L, -1, 2))
1888 	    {
1889 		lua_pushstring(L, "blob");
1890 		return 1;
1891 	    }
1892 	    luaV_getfield(L, LUAVIM_FUNCREF);
1893 	    if (lua_rawequal(L, -1, 2))
1894 	    {
1895 		lua_pushstring(L, "funcref");
1896 		return 1;
1897 	    }
1898 	    luaV_getfield(L, LUAVIM_BUFFER);
1899 	    if (lua_rawequal(L, -1, 2))
1900 	    {
1901 		lua_pushstring(L, "buffer");
1902 		return 1;
1903 	    }
1904 	    luaV_getfield(L, LUAVIM_WINDOW);
1905 	    if (lua_rawequal(L, -1, 2))
1906 	    {
1907 		lua_pushstring(L, "window");
1908 		return 1;
1909 	    }
1910 	}
1911     }
1912     lua_pushstring(L, luaL_typename(L, 1)); // fallback
1913     return 1;
1914 }
1915 
1916 static const luaL_Reg luaV_module[] = {
1917     {"command", luaV_command},
1918     {"eval", luaV_eval},
1919     {"beep", luaV_beep},
1920     {"line", luaV_line},
1921     {"list", luaV_list},
1922     {"dict", luaV_dict},
1923     {"blob", luaV_blob},
1924     {"funcref", luaV_funcref},
1925     {"buffer", luaV_buffer},
1926     {"window", luaV_window},
1927     {"open", luaV_open},
1928     {"type", luaV_type},
1929     {NULL, NULL}
1930 };
1931 
1932 /*
1933  * for freeing list, dict, buffer and window objects; lightuserdata as arg
1934  */
1935     static int
1936 luaV_free(lua_State *L)
1937 {
1938     lua_pushnil(L);
1939     luaV_setudata(L, lua_touserdata(L, 1));
1940     return 0;
1941 }
1942 
1943     static int
1944 luaV_luaeval(lua_State *L)
1945 {
1946     luaL_Buffer b;
1947     size_t l;
1948     const char *str = lua_tolstring(L, 1, &l);
1949     typval_T *arg = (typval_T *) lua_touserdata(L, 2);
1950     typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
1951     luaL_buffinit(L, &b);
1952     luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
1953     luaL_addlstring(&b, str, l);
1954     luaL_pushresult(&b);
1955     str = lua_tolstring(L, -1, &l);
1956     if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
1957     {
1958 	luaV_emsg(L);
1959 	return 0;
1960     }
1961     luaV_pushtypval(L, arg);
1962     if (lua_pcall(L, 1, 1, 0)) // running error?
1963     {
1964 	luaV_emsg(L);
1965 	return 0;
1966     }
1967     if (luaV_totypval(L, -1, rettv) == FAIL)
1968 	emsg("luaeval: cannot convert value");
1969     return 0;
1970 }
1971 
1972     static int
1973 luaV_setref(lua_State *L)
1974 {
1975     int copyID = lua_tointeger(L, 1);
1976     int abort = FALSE;
1977 
1978     luaV_getfield(L, LUAVIM_LIST);
1979     luaV_getfield(L, LUAVIM_DICT);
1980     luaV_getfield(L, LUAVIM_FUNCREF);
1981     lua_pushnil(L);
1982     // traverse cache table
1983     while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
1984     {
1985 	lua_getmetatable(L, -1);
1986 	if (lua_rawequal(L, -1, 2)) // list?
1987 	{
1988 	    list_T *l = (list_T *)lua_touserdata(L, 5); // key
1989 
1990 	    abort = set_ref_in_list(l, copyID);
1991 	}
1992 	else if (lua_rawequal(L, -1, 3)) // dict?
1993 	{
1994 	    dict_T *d = (dict_T *)lua_touserdata(L, 5); // key
1995 
1996 	    abort = set_ref_in_dict(d, copyID);
1997 	}
1998 	else if (lua_rawequal(L, -1, 4)) // funcref?
1999 	{
2000 	    luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key
2001 
2002 	    abort = set_ref_in_dict(f->self, copyID);
2003 	}
2004 	lua_pop(L, 2); // metatable and value
2005     }
2006     lua_pushinteger(L, abort);
2007     return 1;
2008 }
2009 
2010     static int
2011 luaopen_vim(lua_State *L)
2012 {
2013     // set cache table
2014     lua_newtable(L);
2015     lua_newtable(L);
2016     lua_pushstring(L, "v");
2017     lua_setfield(L, -2, "__mode");
2018     lua_setmetatable(L, -2); // cache is weak-valued
2019     // print
2020     lua_pushcfunction(L, luaV_print);
2021     lua_setglobal(L, "print");
2022     // debug.debug
2023     lua_getglobal(L, "debug");
2024     lua_pushcfunction(L, luaV_debug);
2025     lua_setfield(L, -2, "debug");
2026     lua_pop(L, 1);
2027     // free
2028     lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
2029     lua_pushvalue(L, 1); // cache table
2030     lua_pushcclosure(L, luaV_free, 1);
2031     lua_rawset(L, LUA_REGISTRYINDEX);
2032     // luaeval
2033     lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
2034     lua_pushvalue(L, 1); // cache table
2035     lua_pushcclosure(L, luaV_luaeval, 1);
2036     lua_rawset(L, LUA_REGISTRYINDEX);
2037     // setref
2038     lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
2039     lua_pushvalue(L, 1); // cache table
2040     lua_pushcclosure(L, luaV_setref, 1);
2041     lua_rawset(L, LUA_REGISTRYINDEX);
2042     // register
2043     luaV_newmetatable(L, LUAVIM_LIST);
2044     lua_pushvalue(L, 1);
2045     luaV_openlib(L, luaV_List_mt, 1);
2046     luaV_newmetatable(L, LUAVIM_DICT);
2047     lua_pushvalue(L, 1);
2048     luaV_openlib(L, luaV_Dict_mt, 1);
2049     luaV_newmetatable(L, LUAVIM_BLOB);
2050     lua_pushvalue(L, 1);
2051     luaV_openlib(L, luaV_Blob_mt, 1);
2052     luaV_newmetatable(L, LUAVIM_FUNCREF);
2053     lua_pushvalue(L, 1);
2054     luaV_openlib(L, luaV_Funcref_mt, 1);
2055     luaV_newmetatable(L, LUAVIM_BUFFER);
2056     lua_pushvalue(L, 1); // cache table
2057     luaV_openlib(L, luaV_Buffer_mt, 1);
2058     luaV_newmetatable(L, LUAVIM_WINDOW);
2059     lua_pushvalue(L, 1); // cache table
2060     luaV_openlib(L, luaV_Window_mt, 1);
2061     lua_newtable(L); // vim table
2062     lua_pushvalue(L, 1); // cache table
2063     luaV_openlib(L, luaV_module, 1);
2064     lua_setglobal(L, LUAVIM_NAME);
2065     return 0;
2066 }
2067 
2068     static lua_State *
2069 luaV_newstate(void)
2070 {
2071     lua_State *L = luaL_newstate();
2072     luaL_openlibs(L); // core libs
2073     lua_pushcfunction(L, luaopen_vim); // vim
2074     lua_call(L, 0, 0);
2075     return L;
2076 }
2077 
2078     static void
2079 luaV_setrange(lua_State *L, int line1, int line2)
2080 {
2081     lua_getglobal(L, LUAVIM_NAME);
2082     lua_pushinteger(L, line1);
2083     lua_setfield(L, -2, "firstline");
2084     lua_pushinteger(L, line2);
2085     lua_setfield(L, -2, "lastline");
2086     lua_pop(L, 1); // vim table
2087 }
2088 
2089 
2090 // =======   Interface   =======
2091 
2092 static lua_State *L = NULL;
2093 
2094     static int
2095 lua_isopen(void)
2096 {
2097     return L != NULL;
2098 }
2099 
2100     static int
2101 lua_init(void)
2102 {
2103     if (!lua_isopen())
2104     {
2105 #ifdef DYNAMIC_LUA
2106 	if (!lua_enabled(TRUE))
2107 	{
2108 	    emsg(_("Lua library cannot be loaded."));
2109 	    return FAIL;
2110 	}
2111 #endif
2112 	L = luaV_newstate();
2113     }
2114     return OK;
2115 }
2116 
2117     void
2118 lua_end(void)
2119 {
2120     if (lua_isopen())
2121     {
2122 	lua_close(L);
2123 	L = NULL;
2124 #ifdef DYNAMIC_LUA
2125 	end_dynamic_lua();
2126 #endif
2127     }
2128 }
2129 
2130 /*
2131  * ex commands
2132  */
2133     void
2134 ex_lua(exarg_T *eap)
2135 {
2136     char *script;
2137     if (lua_init() == FAIL) return;
2138     script = (char *) script_get(eap, eap->arg);
2139     if (!eap->skip)
2140     {
2141 	char *s = (script) ? script :  (char *) eap->arg;
2142 	luaV_setrange(L, eap->line1, eap->line2);
2143 	if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2144 		|| lua_pcall(L, 0, 0, 0))
2145 	    luaV_emsg(L);
2146     }
2147     if (script != NULL) vim_free(script);
2148 }
2149 
2150     void
2151 ex_luado(exarg_T *eap)
2152 {
2153     linenr_T l;
2154     const char *s = (const char *) eap->arg;
2155     luaL_Buffer b;
2156     size_t len;
2157     buf_T *was_curbuf = curbuf;
2158 
2159     if (lua_init() == FAIL) return;
2160     if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2161     {
2162 	emsg(_("cannot save undo information"));
2163 	return;
2164     }
2165     luaV_setrange(L, eap->line1, eap->line2);
2166     luaL_buffinit(L, &b);
2167     luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
2168     luaL_addlstring(&b, s, strlen(s));
2169     luaL_addlstring(&b, " end", 4); // footer
2170     luaL_pushresult(&b);
2171     s = lua_tolstring(L, -1, &len);
2172     if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2173     {
2174 	luaV_emsg(L);
2175 	lua_pop(L, 1); // function body
2176 	return;
2177     }
2178     lua_call(L, 0, 1);
2179     lua_replace(L, -2); // function -> body
2180     for (l = eap->line1; l <= eap->line2; l++)
2181     {
2182 	// Check the line number, the command my have deleted lines.
2183 	if (l > curbuf->b_ml.ml_line_count)
2184 	    break;
2185 
2186 	lua_pushvalue(L, -1); // function
2187 	luaV_pushline(L, curbuf, l); // current line as arg
2188 	lua_pushinteger(L, l); // current line number as arg
2189 	if (lua_pcall(L, 2, 1, 0))
2190 	{
2191 	    luaV_emsg(L);
2192 	    break;
2193 	}
2194 	// Catch the command switching to another buffer.
2195 	if (curbuf != was_curbuf)
2196 	    break;
2197 	if (lua_isstring(L, -1)) // update line?
2198 	{
2199 #ifdef HAVE_SANDBOX
2200 	    luaV_checksandbox(L);
2201 #endif
2202 	    ml_replace(l, luaV_toline(L, -1), TRUE);
2203 	    changed_bytes(l, 0);
2204 	    lua_pop(L, 1); // result from luaV_toline
2205 	}
2206 	lua_pop(L, 1); // line
2207     }
2208     lua_pop(L, 1); // function
2209     check_cursor();
2210     update_screen(NOT_VALID);
2211 }
2212 
2213     void
2214 ex_luafile(exarg_T *eap)
2215 {
2216     if (lua_init() == FAIL)
2217 	return;
2218     if (!eap->skip)
2219     {
2220 	luaV_setrange(L, eap->line1, eap->line2);
2221 	if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2222 	    luaV_emsg(L);
2223     }
2224 }
2225 
2226 #define luaV_freetype(typ,tname) \
2227 	void \
2228     lua_##tname##_free(typ *o) \
2229     { \
2230 	if (!lua_isopen()) return; \
2231 	luaV_getfield(L, LUAVIM_FREE); \
2232 	lua_pushlightuserdata(L, (void *) o); \
2233 	lua_call(L, 1, 0); \
2234     }
2235 
2236 luaV_freetype(buf_T, buffer)
2237 luaV_freetype(win_T, window)
2238 
2239     void
2240 do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
2241 {
2242     lua_init();
2243     luaV_getfield(L, LUAVIM_LUAEVAL);
2244     lua_pushstring(L, (char *) str);
2245     lua_pushlightuserdata(L, (void *) arg);
2246     lua_pushlightuserdata(L, (void *) rettv);
2247     lua_call(L, 3, 0);
2248 }
2249 
2250     int
2251 set_ref_in_lua(int copyID)
2252 {
2253     int aborted = 0;
2254 
2255     if (lua_isopen())
2256     {
2257 	luaV_getfield(L, LUAVIM_SETREF);
2258 	// call the function with 1 arg, getting 1 result back
2259 	lua_pushinteger(L, copyID);
2260 	lua_call(L, 1, 1);
2261 	// get the result
2262 	aborted = lua_tointeger(L, -1);
2263 	// pop result off the stack
2264 	lua_pop(L, 1);
2265     }
2266     return aborted;
2267 }
2268 
2269 #endif
2270