blob: 120ed7bbdd0b581dd6e51e32f8a794deaf49c864 [file] [log] [blame]
Bram Moolenaar1dced572012-04-05 16:54:08 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002 *
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
Bram Moolenaare2793352011-01-17 19:53:27 +010012#include "vim.h"
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +020013#include "version.h"
Bram Moolenaare2793352011-01-17 19:53:27 +010014
Bram Moolenaar0ba04292010-07-14 23:23:17 +020015#include <lua.h>
16#include <lualib.h>
17#include <lauxlib.h>
Bram Moolenaar0ba04292010-07-14 23:23:17 +020018
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010019// Only do the following when the feature is enabled. Needed for "make
20// depend".
Bram Moolenaar0ba04292010-07-14 23:23:17 +020021#if defined(FEAT_LUA) || defined(PROTO)
22
23#define LUAVIM_CHUNKNAME "vim chunk"
24#define LUAVIM_NAME "vim"
Bram Moolenaar1dced572012-04-05 16:54:08 +020025#define LUAVIM_EVALNAME "luaeval"
26#define LUAVIM_EVALHEADER "local _A=select(1,...) return "
Bram Moolenaar0ba04292010-07-14 23:23:17 +020027
Bram Moolenaar125ed272021-04-07 20:11:12 +020028#ifdef LUA_RELEASE
29# define LUAVIM_VERSION LUA_RELEASE
30#else
31# define LUAVIM_VERSION LUA_VERSION
32#endif
33
Bram Moolenaar0ba04292010-07-14 23:23:17 +020034typedef buf_T *luaV_Buffer;
35typedef win_T *luaV_Window;
Bram Moolenaar1dced572012-04-05 16:54:08 +020036typedef dict_T *luaV_Dict;
37typedef list_T *luaV_List;
Bram Moolenaarb7828692019-03-23 13:57:02 +010038typedef blob_T *luaV_Blob;
Bram Moolenaarca06da92018-07-01 15:12:05 +020039typedef struct {
Bram Moolenaar4eefe472019-03-19 21:59:19 +010040 char_u *name; // funcref
Bram Moolenaarca06da92018-07-01 15:12:05 +020041 dict_T *self; // selfdict
42} luaV_Funcref;
Bram Moolenaarc8970b92020-10-26 20:18:08 +010043typedef int (*msgfunc_T)(char *);
Bram Moolenaar0ba04292010-07-14 23:23:17 +020044
Bram Moolenaar801ab062020-06-25 19:27:56 +020045typedef struct {
46 int lua_funcref; // ref to a lua func
47 int lua_tableref; // ref to a lua table if metatable else LUA_NOREF. used
48 // for __call
49 lua_State *L;
50} luaV_CFuncState;
51
Bram Moolenaar1dced572012-04-05 16:54:08 +020052static const char LUAVIM_DICT[] = "dict";
53static const char LUAVIM_LIST[] = "list";
Bram Moolenaarb7828692019-03-23 13:57:02 +010054static const char LUAVIM_BLOB[] = "blob";
Bram Moolenaarca06da92018-07-01 15:12:05 +020055static const char LUAVIM_FUNCREF[] = "funcref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020056static const char LUAVIM_BUFFER[] = "buffer";
57static const char LUAVIM_WINDOW[] = "window";
58static const char LUAVIM_FREE[] = "luaV_free";
Bram Moolenaar1dced572012-04-05 16:54:08 +020059static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
60static const char LUAVIM_SETREF[] = "luaV_setref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020061
Bram Moolenaar801ab062020-06-25 19:27:56 +020062static const char LUA___CALL[] = "__call";
63
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010064// most functions are closures with a cache table as first upvalue;
65// get/setudata manage references to vim userdata in cache table through
66// object pointers (light userdata)
Bram Moolenaar1dced572012-04-05 16:54:08 +020067#define luaV_getudata(L, v) \
68 lua_pushlightuserdata((L), (void *) (v)); \
69 lua_rawget((L), lua_upvalueindex(1))
70#define luaV_setudata(L, v) \
71 lua_pushlightuserdata((L), (void *) (v)); \
72 lua_pushvalue((L), -2); \
73 lua_rawset((L), lua_upvalueindex(1))
Bram Moolenaar0ba04292010-07-14 23:23:17 +020074#define luaV_getfield(L, s) \
75 lua_pushlightuserdata((L), (void *)(s)); \
76 lua_rawget((L), LUA_REGISTRYINDEX)
77#define luaV_checksandbox(L) \
78 if (sandbox) luaL_error((L), "not allowed in sandbox")
79#define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
80#define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
Bram Moolenaarca06da92018-07-01 15:12:05 +020081#define luaV_checktypval(L, a, v, msg) \
82 do { \
Bram Moolenaar801ab062020-06-25 19:27:56 +020083 if (luaV_totypval(L, a, v) == FAIL) \
Bram Moolenaarca06da92018-07-01 15:12:05 +020084 luaL_error(L, msg ": cannot convert value"); \
85 } while (0)
Bram Moolenaar0ba04292010-07-14 23:23:17 +020086
Bram Moolenaarca06da92018-07-01 15:12:05 +020087static luaV_List *luaV_pushlist(lua_State *L, list_T *lis);
88static luaV_Dict *luaV_pushdict(lua_State *L, dict_T *dic);
Bram Moolenaarb7828692019-03-23 13:57:02 +010089static luaV_Blob *luaV_pushblob(lua_State *L, blob_T *blo);
Bram Moolenaar4eefe472019-03-19 21:59:19 +010090static luaV_Funcref *luaV_pushfuncref(lua_State *L, char_u *name);
Bram Moolenaar801ab062020-06-25 19:27:56 +020091static int luaV_call_lua_func(int argcount, typval_T *argvars, typval_T *rettv, void *state);
92static void luaV_call_lua_func_free(void *state);
Bram Moolenaar1dced572012-04-05 16:54:08 +020093
94#if LUA_VERSION_NUM <= 501
95#define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n)
96#define luaL_typeerror luaL_typerror
97#else
98#define luaV_openlib luaL_setfuncs
99#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200100
101#ifdef DYNAMIC_LUA
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200102
Bram Moolenaar4f974752019-02-17 17:44:42 +0100103#ifndef MSWIN
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200104# include <dlfcn.h>
105# define HANDLE void*
106# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
107# define symbol_from_dll dlsym
108# define close_dll dlclose
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200109# define load_dll_error dlerror
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200110#else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200111# define load_dll vimLoadLib
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200112# define symbol_from_dll GetProcAddress
113# define close_dll FreeLibrary
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200114# define load_dll_error GetWin32Error
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200115#endif
116
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100117// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200118#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200119#define luaL_register dll_luaL_register
Bram Moolenaar1dced572012-04-05 16:54:08 +0200120#define luaL_prepbuffer dll_luaL_prepbuffer
121#define luaL_openlib dll_luaL_openlib
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200122#define luaL_typerror dll_luaL_typerror
Bram Moolenaar1dced572012-04-05 16:54:08 +0200123#define luaL_loadfile dll_luaL_loadfile
124#define luaL_loadbuffer dll_luaL_loadbuffer
125#else
126#define luaL_prepbuffsize dll_luaL_prepbuffsize
127#define luaL_setfuncs dll_luaL_setfuncs
128#define luaL_loadfilex dll_luaL_loadfilex
129#define luaL_loadbufferx dll_luaL_loadbufferx
130#define luaL_argerror dll_luaL_argerror
131#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200132#if LUA_VERSION_NUM >= 504
133#define luaL_typeerror dll_luaL_typeerror
134#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200135#define luaL_checkany dll_luaL_checkany
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200136#define luaL_checklstring dll_luaL_checklstring
137#define luaL_checkinteger dll_luaL_checkinteger
138#define luaL_optinteger dll_luaL_optinteger
139#define luaL_checktype dll_luaL_checktype
140#define luaL_error dll_luaL_error
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200141#define luaL_newstate dll_luaL_newstate
142#define luaL_buffinit dll_luaL_buffinit
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200143#define luaL_addlstring dll_luaL_addlstring
144#define luaL_pushresult dll_luaL_pushresult
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200145#define luaL_loadstring dll_luaL_loadstring
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200146#define luaL_ref dll_luaL_ref
147#define luaL_unref dll_luaL_unref
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100148// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200149#if LUA_VERSION_NUM <= 501
150#define lua_tonumber dll_lua_tonumber
151#define lua_tointeger dll_lua_tointeger
152#define lua_call dll_lua_call
153#define lua_pcall dll_lua_pcall
154#else
155#define lua_tonumberx dll_lua_tonumberx
156#define lua_tointegerx dll_lua_tointegerx
157#define lua_callk dll_lua_callk
158#define lua_pcallk dll_lua_pcallk
159#define lua_getglobal dll_lua_getglobal
160#define lua_setglobal dll_lua_setglobal
Bram Moolenaar1dced572012-04-05 16:54:08 +0200161#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200162#if LUA_VERSION_NUM <= 502
163#define lua_replace dll_lua_replace
164#define lua_remove dll_lua_remove
165#endif
166#if LUA_VERSION_NUM >= 503
167#define lua_rotate dll_lua_rotate
168#define lua_copy dll_lua_copy
169#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200170#define lua_typename dll_lua_typename
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200171#define lua_close dll_lua_close
172#define lua_gettop dll_lua_gettop
173#define lua_settop dll_lua_settop
174#define lua_pushvalue dll_lua_pushvalue
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200175#define lua_isnumber dll_lua_isnumber
176#define lua_isstring dll_lua_isstring
177#define lua_type dll_lua_type
178#define lua_rawequal dll_lua_rawequal
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200179#define lua_toboolean dll_lua_toboolean
180#define lua_tolstring dll_lua_tolstring
181#define lua_touserdata dll_lua_touserdata
182#define lua_pushnil dll_lua_pushnil
183#define lua_pushnumber dll_lua_pushnumber
184#define lua_pushinteger dll_lua_pushinteger
185#define lua_pushlstring dll_lua_pushlstring
186#define lua_pushstring dll_lua_pushstring
187#define lua_pushfstring dll_lua_pushfstring
188#define lua_pushcclosure dll_lua_pushcclosure
189#define lua_pushboolean dll_lua_pushboolean
190#define lua_pushlightuserdata dll_lua_pushlightuserdata
191#define lua_getfield dll_lua_getfield
192#define lua_rawget dll_lua_rawget
Bram Moolenaar1dced572012-04-05 16:54:08 +0200193#define lua_rawgeti dll_lua_rawgeti
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200194#define lua_createtable dll_lua_createtable
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +0200195#define lua_settable dll_lua_settable
Bram Moolenaar830e3582018-08-21 14:23:35 +0200196#if LUA_VERSION_NUM >= 504
197 #define lua_newuserdatauv dll_lua_newuserdatauv
198#else
199 #define lua_newuserdata dll_lua_newuserdata
200#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200201#define lua_getmetatable dll_lua_getmetatable
202#define lua_setfield dll_lua_setfield
203#define lua_rawset dll_lua_rawset
204#define lua_rawseti dll_lua_rawseti
205#define lua_setmetatable dll_lua_setmetatable
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200206#define lua_next dll_lua_next
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100207// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200208#define luaopen_base dll_luaopen_base
209#define luaopen_table dll_luaopen_table
210#define luaopen_string dll_luaopen_string
211#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200212#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200213#define luaopen_os dll_luaopen_os
214#define luaopen_package dll_luaopen_package
215#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200216#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200217
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100218// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200219#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200220void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200221char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
222void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200223int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200224int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
225int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
226#else
227char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
228void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
229int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
230int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
231int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
232#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200233#if LUA_VERSION_NUM >= 504
234int (*dll_luaL_typeerror) (lua_State *L, int narg, const char *tname);
235#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200236void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200237const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
238lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
239lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
240void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
241int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200242lua_State *(*dll_luaL_newstate) (void);
243void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200244void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
245void (*dll_luaL_pushresult) (luaL_Buffer *B);
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200246int (*dll_luaL_loadstring) (lua_State *L, const char *s);
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200247int (*dll_luaL_ref) (lua_State *L, int idx);
248#if LUA_VERSION_NUM <= 502
249void (*dll_luaL_unref) (lua_State *L, int idx, int n);
250#else
251void (*dll_luaL_unref) (lua_State *L, int idx, lua_Integer n);
252#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100253// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200254#if LUA_VERSION_NUM <= 501
255lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
256lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
257void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
258int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
259#else
260lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
261lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
262void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
Bram Moolenaardb913952012-06-29 12:54:53 +0200263 lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200264int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
Bram Moolenaardb913952012-06-29 12:54:53 +0200265 int ctx, lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200266void (*dll_lua_getglobal) (lua_State *L, const char *var);
267void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200268#endif
269#if LUA_VERSION_NUM <= 502
270void (*dll_lua_replace) (lua_State *L, int idx);
271void (*dll_lua_remove) (lua_State *L, int idx);
272#endif
273#if LUA_VERSION_NUM >= 503
274void (*dll_lua_rotate) (lua_State *L, int idx, int n);
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200275void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200276#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200277const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200278void (*dll_lua_close) (lua_State *L);
279int (*dll_lua_gettop) (lua_State *L);
280void (*dll_lua_settop) (lua_State *L, int idx);
281void (*dll_lua_pushvalue) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200282int (*dll_lua_isnumber) (lua_State *L, int idx);
283int (*dll_lua_isstring) (lua_State *L, int idx);
284int (*dll_lua_type) (lua_State *L, int idx);
285int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200286int (*dll_lua_toboolean) (lua_State *L, int idx);
287const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
288void *(*dll_lua_touserdata) (lua_State *L, int idx);
289void (*dll_lua_pushnil) (lua_State *L);
290void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
291void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
292void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
293void (*dll_lua_pushstring) (lua_State *L, const char *s);
294const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
295void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
296void (*dll_lua_pushboolean) (lua_State *L, int b);
297void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
298void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
Bram Moolenaar17413672018-07-14 20:49:42 +0200299#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200300void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200301void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200302#else
303int (*dll_lua_rawget) (lua_State *L, int idx);
304int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
305#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200306void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +0200307void (*dll_lua_settable) (lua_State *L, int idx);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200308#if LUA_VERSION_NUM >= 504
309void *(*dll_lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
310#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200311void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200312#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200313int (*dll_lua_getmetatable) (lua_State *L, int objindex);
314void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
315void (*dll_lua_rawset) (lua_State *L, int idx);
Bram Moolenaar17413672018-07-14 20:49:42 +0200316#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200317void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200318#else
319void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n);
320#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200321int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200322int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100323// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200324int (*dll_luaopen_base) (lua_State *L);
325int (*dll_luaopen_table) (lua_State *L);
326int (*dll_luaopen_string) (lua_State *L);
327int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200328int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200329int (*dll_luaopen_os) (lua_State *L);
330int (*dll_luaopen_package) (lua_State *L);
331int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200332void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200333
334typedef void **luaV_function;
335typedef struct {
336 const char *name;
337 luaV_function func;
338} luaV_Reg;
339
340static const luaV_Reg luaV_dll[] = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100341 // lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200342#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200343 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200344 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
345 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200346 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200347 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
348 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
349#else
350 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
351 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
352 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
353 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
354 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
355#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200356#if LUA_VERSION_NUM >= 504
357 {"luaL_typeerror", (luaV_function) &dll_luaL_typeerror},
358#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200359 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200360 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
361 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
362 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
363 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
364 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200365 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
366 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200367 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
368 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200369 {"luaL_loadstring", (luaV_function) &dll_luaL_loadstring},
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200370 {"luaL_ref", (luaV_function) &dll_luaL_ref},
371 {"luaL_unref", (luaV_function) &dll_luaL_unref},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100372 // lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200373#if LUA_VERSION_NUM <= 501
374 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
375 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
376 {"lua_call", (luaV_function) &dll_lua_call},
377 {"lua_pcall", (luaV_function) &dll_lua_pcall},
378#else
379 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
380 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
381 {"lua_callk", (luaV_function) &dll_lua_callk},
382 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
383 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
384 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200385#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200386#if LUA_VERSION_NUM <= 502
387 {"lua_replace", (luaV_function) &dll_lua_replace},
388 {"lua_remove", (luaV_function) &dll_lua_remove},
389#endif
390#if LUA_VERSION_NUM >= 503
391 {"lua_rotate", (luaV_function) &dll_lua_rotate},
392 {"lua_copy", (luaV_function) &dll_lua_copy},
393#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200394 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200395 {"lua_close", (luaV_function) &dll_lua_close},
396 {"lua_gettop", (luaV_function) &dll_lua_gettop},
397 {"lua_settop", (luaV_function) &dll_lua_settop},
398 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200399 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
400 {"lua_isstring", (luaV_function) &dll_lua_isstring},
401 {"lua_type", (luaV_function) &dll_lua_type},
402 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200403 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
404 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
405 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
406 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
407 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
408 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
409 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
410 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
411 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
412 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
413 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
414 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
415 {"lua_getfield", (luaV_function) &dll_lua_getfield},
416 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200417 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200418 {"lua_createtable", (luaV_function) &dll_lua_createtable},
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +0200419 {"lua_settable", (luaV_function) &dll_lua_settable},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200420#if LUA_VERSION_NUM >= 504
421 {"lua_newuserdatauv", (luaV_function) &dll_lua_newuserdatauv},
422#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200423 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200424#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200425 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
426 {"lua_setfield", (luaV_function) &dll_lua_setfield},
427 {"lua_rawset", (luaV_function) &dll_lua_rawset},
428 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
429 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200430 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100431 // libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200432 {"luaopen_base", (luaV_function) &dll_luaopen_base},
433 {"luaopen_table", (luaV_function) &dll_luaopen_table},
434 {"luaopen_string", (luaV_function) &dll_luaopen_string},
435 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200436 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200437 {"luaopen_os", (luaV_function) &dll_luaopen_os},
438 {"luaopen_package", (luaV_function) &dll_luaopen_package},
439 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200440 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200441 {NULL, NULL}
442};
443
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200444static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200445
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200446 static int
447lua_link_init(char *libname, int verbose)
448{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200449 const luaV_Reg *reg;
450 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200451 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200452 if (!hinstLua)
453 {
454 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000455 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200456 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200457 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200458 for (reg = luaV_dll; reg->func; reg++)
459 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200460 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
461 {
462 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200463 hinstLua = 0;
464 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000465 semsg(_(e_could_not_load_library_function_str), reg->name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200466 return FAIL;
467 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200468 }
469 return OK;
470}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100471#endif // DYNAMIC_LUA
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200472
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200473#if defined(DYNAMIC_LUA) || defined(PROTO)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200474 int
475lua_enabled(int verbose)
476{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100477 return lua_link_init((char *)p_luadll, verbose) == OK;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200478}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200479#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200480
Bram Moolenaar5551b132020-07-14 21:54:28 +0200481#if LUA_VERSION_NUM > 501 && LUA_VERSION_NUM < 504
Bram Moolenaar1dced572012-04-05 16:54:08 +0200482 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100483luaL_typeerror(lua_State *L, int narg, const char *tname)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200484{
485 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200486 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200487 return luaL_argerror(L, narg, msg);
488}
489#endif
490
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200491
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100492// ======= Internal =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200493
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200494 static void
495luaV_newmetatable(lua_State *L, const char *tname)
496{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200497 lua_newtable(L);
498 lua_pushlightuserdata(L, (void *) tname);
499 lua_pushvalue(L, -2);
500 lua_rawset(L, LUA_REGISTRYINDEX);
501}
502
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200503 static void *
504luaV_toudata(lua_State *L, int ud, const char *tname)
505{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200506 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200507
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100508 if (p != NULL) // value is userdata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200509 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100510 if (lua_getmetatable(L, ud)) // does it have a metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200511 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100512 luaV_getfield(L, tname); // get metatable
513 if (lua_rawequal(L, -1, -2)) // MTs match?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200514 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100515 lua_pop(L, 2); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200516 return p;
517 }
518 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200519 }
520 return NULL;
521}
522
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200523 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200524luaV_checkcache(lua_State *L, void *p)
525{
526 luaV_getudata(L, p);
527 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
528 lua_pop(L, 1);
529 return p;
530}
531
532#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
533
534#define luaV_checkvalid(L,luatyp,ud) \
535 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
536
537 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200538luaV_checkudata(lua_State *L, int ud, const char *tname)
539{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200540 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200541 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200542 return p;
543}
544
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200545 static void
546luaV_pushtypval(lua_State *L, typval_T *tv)
547{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200548 if (tv == NULL)
549 {
550 lua_pushnil(L);
551 return;
552 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200553 switch (tv->v_type)
554 {
555 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200556 lua_pushstring(L, tv->vval.v_string == NULL
557 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200558 break;
559 case VAR_NUMBER:
560 lua_pushinteger(L, (int) tv->vval.v_number);
561 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200562#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200563 case VAR_FLOAT:
564 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
565 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200566#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200567 case VAR_LIST:
568 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200569 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200570 case VAR_DICT:
571 luaV_pushdict(L, tv->vval.v_dict);
572 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100573 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100574 case VAR_SPECIAL:
575 if (tv->vval.v_number <= VVAL_TRUE)
576 lua_pushinteger(L, (int) tv->vval.v_number);
577 else
578 lua_pushnil(L);
579 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200580 case VAR_FUNC:
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100581 luaV_pushfuncref(L, tv->vval.v_string);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200582 break;
Bram Moolenaar86c3a212021-03-08 19:50:24 +0100583 case VAR_PARTIAL:
584 // TODO: handle partial arguments
585 luaV_pushfuncref(L, partial_name(tv->vval.v_partial));
586 break;
587
Bram Moolenaarb7828692019-03-23 13:57:02 +0100588 case VAR_BLOB:
589 luaV_pushblob(L, tv->vval.v_blob);
590 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200591 default:
592 lua_pushnil(L);
593 }
594}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200595
Bram Moolenaarca06da92018-07-01 15:12:05 +0200596/*
597 * Converts lua value at 'pos' to typval 'tv'.
598 * Returns OK or FAIL.
599 */
600 static int
601luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200602{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200603 int status = OK;
604
Bram Moolenaara9a8e5f2020-07-02 21:17:57 +0200605 tv->v_lock = 0;
606
Bram Moolenaarca06da92018-07-01 15:12:05 +0200607 switch (lua_type(L, pos))
608 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200609 case LUA_TBOOLEAN:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100610 tv->v_type = VAR_BOOL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200611 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
612 break;
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100613 case LUA_TNIL:
614 tv->v_type = VAR_SPECIAL;
615 tv->vval.v_number = VVAL_NULL;
616 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200617 case LUA_TSTRING:
618 tv->v_type = VAR_STRING;
619 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
620 break;
621 case LUA_TNUMBER:
622#ifdef FEAT_FLOAT
Bram Moolenaareb04f082020-05-17 14:32:35 +0200623 {
624 const lua_Number n = lua_tonumber(L, pos);
625
626 if (n > (lua_Number)INT64_MAX || n < (lua_Number)INT64_MIN
627 || ((lua_Number)((varnumber_T)n)) != n)
628 {
629 tv->v_type = VAR_FLOAT;
630 tv->vval.v_float = (float_T)n;
631 }
632 else
633 {
634 tv->v_type = VAR_NUMBER;
635 tv->vval.v_number = (varnumber_T)n;
636 }
637 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200638#else
639 tv->v_type = VAR_NUMBER;
640 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
641#endif
642 break;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200643 case LUA_TFUNCTION:
644 {
645 char_u *name;
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200646 luaV_CFuncState *state;
647
Bram Moolenaar801ab062020-06-25 19:27:56 +0200648 lua_pushvalue(L, pos);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200649 state = ALLOC_CLEAR_ONE(luaV_CFuncState);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200650 state->lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
651 state->L = L;
652 state->lua_tableref = LUA_NOREF;
653 name = register_cfunc(&luaV_call_lua_func,
654 &luaV_call_lua_func_free, state);
655 tv->v_type = VAR_FUNC;
656 tv->vval.v_string = vim_strsave(name);
657 break;
658 }
659 case LUA_TTABLE:
660 {
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200661 int lua_tableref;
662
Bram Moolenaar801ab062020-06-25 19:27:56 +0200663 lua_pushvalue(L, pos);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200664 lua_tableref = luaL_ref(L, LUA_REGISTRYINDEX);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200665 if (lua_getmetatable(L, pos)) {
666 lua_getfield(L, -1, LUA___CALL);
667 if (lua_isfunction(L, -1)) {
668 char_u *name;
669 int lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
670 luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200671
Bram Moolenaar801ab062020-06-25 19:27:56 +0200672 state->lua_funcref = lua_funcref;
673 state->L = L;
674 state->lua_tableref = lua_tableref;
675 name = register_cfunc(&luaV_call_lua_func,
676 &luaV_call_lua_func_free, state);
677 tv->v_type = VAR_FUNC;
678 tv->vval.v_string = vim_strsave(name);
679 break;
680 }
681 }
682 tv->v_type = VAR_NUMBER;
683 tv->vval.v_number = 0;
684 status = FAIL;
685 break;
686 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200687 case LUA_TUSERDATA:
688 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200689 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200690
Bram Moolenaarb7828692019-03-23 13:57:02 +0100691 if (lua_getmetatable(L, pos)) // has metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200692 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100693 // check list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200694 luaV_getfield(L, LUAVIM_LIST);
695 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200696 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200697 tv->v_type = VAR_LIST;
698 tv->vval.v_list = *((luaV_List *) p);
699 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100700 lua_pop(L, 2); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200701 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200702 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100703 // check dict
Bram Moolenaar1dced572012-04-05 16:54:08 +0200704 luaV_getfield(L, LUAVIM_DICT);
705 if (lua_rawequal(L, -1, -3))
706 {
707 tv->v_type = VAR_DICT;
708 tv->vval.v_dict = *((luaV_Dict *) p);
709 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100710 lua_pop(L, 3); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200711 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200712 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100713 // check blob
714 luaV_getfield(L, LUAVIM_BLOB);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200715 if (lua_rawequal(L, -1, -4))
716 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100717 tv->v_type = VAR_BLOB;
718 tv->vval.v_blob = *((luaV_Blob *) p);
719 ++tv->vval.v_blob->bv_refcount;
720 lua_pop(L, 4); // MTs
721 break;
722 }
723 // check funcref
724 luaV_getfield(L, LUAVIM_FUNCREF);
725 if (lua_rawequal(L, -1, -5))
726 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200727 luaV_Funcref *f = (luaV_Funcref *) p;
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200728
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100729 func_ref(f->name);
730 tv->v_type = VAR_FUNC;
731 tv->vval.v_string = vim_strsave(f->name);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100732 lua_pop(L, 5); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200733 break;
734 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100735 lua_pop(L, 4); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200736 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200737 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100738 // FALLTHROUGH
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200739 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200740 tv->v_type = VAR_NUMBER;
741 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200742 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200743 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200744 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200745}
746
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100747/*
748 * similar to luaL_addlstring, but replaces \0 with \n if toline and
749 * \n with \0 otherwise
750 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200751 static void
752luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
753{
754 while (l--)
755 {
756 if (*s == '\0' && toline)
757 luaL_addchar(b, '\n');
758 else if (*s == '\n' && !toline)
759 luaL_addchar(b, '\0');
760 else
761 luaL_addchar(b, *s);
762 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200763 }
764}
765
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200766 static void
767luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
768{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200769 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
770 luaL_Buffer b;
771 luaL_buffinit(L, &b);
772 luaV_addlstring(&b, s, strlen(s), 0);
773 luaL_pushresult(&b);
774}
775
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200776 static char_u *
777luaV_toline(lua_State *L, int pos)
778{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200779 size_t l;
780 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200781
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200782 luaL_Buffer b;
783 luaL_buffinit(L, &b);
784 luaV_addlstring(&b, s, l, 1);
785 luaL_pushresult(&b);
786 return (char_u *) lua_tostring(L, -1);
787}
788
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100789/*
790 * pops a string s from the top of the stack and calls mf(t) for pieces t of
791 * s separated by newlines
792 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200793 static void
794luaV_msgfunc(lua_State *L, msgfunc_T mf)
795{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200796 luaL_Buffer b;
797 size_t l;
798 const char *p, *s = lua_tolstring(L, -1, &l);
799 luaL_buffinit(L, &b);
800 luaV_addlstring(&b, s, l, 0);
801 luaL_pushresult(&b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100802 // break string
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200803 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200804 while (l--)
805 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100806 if (*p++ == '\0') // break?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200807 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100808 mf((char *)s);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200809 s = p;
810 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200811 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100812 mf((char *)s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100813 lua_pop(L, 2); // original and modified strings
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200814}
815
Bram Moolenaar1dced572012-04-05 16:54:08 +0200816#define luaV_newtype(typ,tname,luatyp,luatname) \
817 static luatyp * \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100818 luaV_new##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200819 { \
820 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
821 *o = obj; \
822 luaV_setudata(L, obj); /* cache[obj] = udata */ \
823 luaV_getfield(L, luatname); \
824 lua_setmetatable(L, -2); \
825 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200826 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200827
828#define luaV_pushtype(typ,tname,luatyp) \
829 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200830 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200831 { \
832 luatyp *o = NULL; \
833 if (obj == NULL) \
834 lua_pushnil(L); \
835 else { \
836 luaV_getudata(L, obj); \
837 if (lua_isnil(L, -1)) /* not interned? */ \
838 { \
839 lua_pop(L, 1); \
840 o = luaV_new##tname(L, obj); \
841 } \
842 else \
843 o = (luatyp *) lua_touserdata(L, -1); \
844 } \
845 return o; \
846 }
847
848#define luaV_type_tostring(tname,luatname) \
849 static int \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100850 luaV_##tname##_tostring(lua_State *L) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200851 { \
852 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
853 return 1; \
854 }
855
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100856// ======= List type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200857
858 static luaV_List *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100859luaV_newlist(lua_State *L, list_T *lis)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200860{
861 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
862 *l = lis;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100863 lis->lv_refcount++; // reference in Lua
864 luaV_setudata(L, lis); // cache[lis] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200865 luaV_getfield(L, LUAVIM_LIST);
866 lua_setmetatable(L, -2);
867 return l;
868}
869
870luaV_pushtype(list_T, list, luaV_List)
871luaV_type_tostring(list, LUAVIM_LIST)
872
873 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100874luaV_list_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200875{
876 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100877 lua_pushinteger(L, (int) list_len(l));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200878 return 1;
879}
880
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200881 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100882luaV_list_iter(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200883{
884 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
885 if (li == NULL) return 0;
886 luaV_pushtypval(L, &li->li_tv);
887 lua_pushlightuserdata(L, (void *) li->li_next);
888 lua_replace(L, lua_upvalueindex(2));
889 return 1;
890}
891
892 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100893luaV_list_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200894{
895 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100896 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200897 lua_pushlightuserdata(L, (void *) l->lv_first);
898 lua_pushcclosure(L, luaV_list_iter, 2);
899 return 1;
900}
901
902 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100903luaV_list_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200904{
905 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100906 if (lua_isnumber(L, 2)) // list item?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200907 {
Bram Moolenaarbd846172020-06-27 12:32:57 +0200908 long n = (long) luaL_checkinteger(L, 2);
909 listitem_T *li;
910
911 // Lua array index starts with 1 while Vim uses 0, subtract 1 to
912 // normalize.
913 n -= 1;
914 li = list_find(l, n);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200915 if (li == NULL)
916 lua_pushnil(L);
917 else
918 luaV_pushtypval(L, &li->li_tv);
919 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100920 else if (lua_isstring(L, 2)) // method?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200921 {
922 const char *s = lua_tostring(L, 2);
923 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200924 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200925 {
926 lua_getmetatable(L, 1);
927 lua_getfield(L, -1, s);
928 }
929 else
930 lua_pushnil(L);
931 }
932 else
933 lua_pushnil(L);
934 return 1;
935}
936
937 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100938luaV_list_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200939{
940 list_T *l = luaV_unbox(L, luaV_List, 1);
941 long n = (long) luaL_checkinteger(L, 2);
942 listitem_T *li;
Bram Moolenaarbd846172020-06-27 12:32:57 +0200943
944 // Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize.
945 n -= 1;
946
Bram Moolenaar1dced572012-04-05 16:54:08 +0200947 if (l->lv_lock)
948 luaL_error(L, "list is locked");
949 li = list_find(l, n);
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200950 if (li == NULL)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200951 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100952 if (!lua_isnil(L, 3))
953 {
954 typval_T v;
955 luaV_checktypval(L, 3, &v, "inserting list item");
956 if (list_insert_tv(l, &v, li) == FAIL)
957 luaL_error(L, "failed to add item to list");
958 clear_tv(&v);
959 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200960 }
961 else
962 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100963 if (lua_isnil(L, 3)) // remove?
964 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200965 vimlist_remove(l, li, li);
966 listitem_free(l, li);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100967 }
968 else
969 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200970 typval_T v;
971 luaV_checktypval(L, 3, &v, "setting list item");
972 clear_tv(&li->li_tv);
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200973 li->li_tv = v;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100974 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200975 }
976 return 0;
977}
978
979 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100980luaV_list_add(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200981{
982 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
983 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200984 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200985 if (l->lv_lock)
986 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200987 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200988 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200989 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200990 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200991 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200992 lua_settop(L, 1);
993 return 1;
994}
995
996 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100997luaV_list_insert(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200998{
999 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
1000 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +01001001 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001002 listitem_T *li = NULL;
1003 typval_T v;
1004 if (l->lv_lock)
1005 luaL_error(L, "list is locked");
1006 if (pos < l->lv_len)
1007 {
1008 li = list_find(l, pos);
1009 if (li == NULL)
1010 luaL_error(L, "invalid position");
1011 }
1012 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001013 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001014 if (list_insert_tv(l, &v, li) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001015 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001016 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001017 lua_settop(L, 1);
1018 return 1;
1019}
1020
1021static const luaL_Reg luaV_List_mt[] = {
1022 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001023 {"__len", luaV_list_len},
1024 {"__call", luaV_list_call},
1025 {"__index", luaV_list_index},
1026 {"__newindex", luaV_list_newindex},
1027 {"add", luaV_list_add},
1028 {"insert", luaV_list_insert},
1029 {NULL, NULL}
1030};
1031
1032
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001033// ======= Dict type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001034
1035 static luaV_Dict *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001036luaV_newdict(lua_State *L, dict_T *dic)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001037{
1038 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
1039 *d = dic;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001040 dic->dv_refcount++; // reference in Lua
1041 luaV_setudata(L, dic); // cache[dic] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +02001042 luaV_getfield(L, LUAVIM_DICT);
1043 lua_setmetatable(L, -2);
1044 return d;
1045}
1046
1047luaV_pushtype(dict_T, dict, luaV_Dict)
1048luaV_type_tostring(dict, LUAVIM_DICT)
1049
1050 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001051luaV_dict_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001052{
1053 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001054 lua_pushinteger(L, (int) dict_len(d));
Bram Moolenaar1dced572012-04-05 16:54:08 +02001055 return 1;
1056}
1057
1058 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001059luaV_dict_iter(lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001060{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001061#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +02001062 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
1063 int n = lua_tointeger(L, lua_upvalueindex(3));
1064 dictitem_T *di;
1065 if (n <= 0) return 0;
1066 while (HASHITEM_EMPTY(hi)) hi++;
1067 di = dict_lookup(hi);
1068 lua_pushstring(L, (char *) hi->hi_key);
1069 luaV_pushtypval(L, &di->di_tv);
1070 lua_pushlightuserdata(L, (void *) (hi + 1));
1071 lua_replace(L, lua_upvalueindex(2));
1072 lua_pushinteger(L, n - 1);
1073 lua_replace(L, lua_upvalueindex(3));
1074 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001075#else
1076 return 0;
1077#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001078}
1079
1080 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001081luaV_dict_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001082{
1083 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1084 hashtab_T *ht = &d->dv_hashtab;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001085 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +02001086 lua_pushlightuserdata(L, (void *) ht->ht_array);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001087 lua_pushinteger(L, ht->ht_used); // # remaining items
Bram Moolenaar1dced572012-04-05 16:54:08 +02001088 lua_pushcclosure(L, luaV_dict_iter, 3);
1089 return 1;
1090}
1091
1092 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001093luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001094{
1095 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1096 char_u *key = (char_u *) luaL_checkstring(L, 2);
1097 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001098
Bram Moolenaar1dced572012-04-05 16:54:08 +02001099 if (di == NULL)
1100 lua_pushnil(L);
1101 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001102 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001103 luaV_pushtypval(L, &di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001104 if (di->di_tv.v_type == VAR_FUNC) // funcref?
Bram Moolenaarca06da92018-07-01 15:12:05 +02001105 {
1106 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001107 f->self = d; // keep "self" reference
Bram Moolenaarca06da92018-07-01 15:12:05 +02001108 d->dv_refcount++;
1109 }
1110 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001111 return 1;
1112}
1113
1114 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001115luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001116{
1117 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1118 char_u *key = (char_u *) luaL_checkstring(L, 2);
1119 dictitem_T *di;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001120 typval_T tv;
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001121
Bram Moolenaar1dced572012-04-05 16:54:08 +02001122 if (d->dv_lock)
1123 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001124 if (key == NULL)
1125 return 0;
1126 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001127 luaL_error(L, "empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001128 if (!lua_isnil(L, 3)) // read value?
Bram Moolenaar17413672018-07-14 20:49:42 +02001129 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001130 luaV_checktypval(L, 3, &tv, "setting dict item");
1131 if (d->dv_scope == VAR_DEF_SCOPE && tv.v_type == VAR_FUNC)
1132 {
1133 clear_tv(&tv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001134 luaL_error(L, "cannot assign funcref to builtin scope");
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001135 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001136 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001137 di = dict_find(d, key, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001138 if (di == NULL) // non-existing key?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001139 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001140 if (lua_isnil(L, 3))
1141 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001142 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001143 if (di == NULL)
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001144 {
1145 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001146 return 0;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001147 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001148 if (dict_add(d, di) == FAIL)
1149 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001150 vim_free(di);
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001151 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001152 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001153 }
1154 }
1155 else
1156 clear_tv(&di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001157 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001158 {
1159 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1160 hash_remove(&d->dv_hashtab, hi);
1161 dictitem_free(di);
1162 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001163 else
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001164 di->di_tv = tv;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001165 return 0;
1166}
1167
1168static const luaL_Reg luaV_Dict_mt[] = {
1169 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001170 {"__len", luaV_dict_len},
1171 {"__call", luaV_dict_call},
1172 {"__index", luaV_dict_index},
1173 {"__newindex", luaV_dict_newindex},
1174 {NULL, NULL}
1175};
1176
1177
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001178// ======= Blob type =======
Bram Moolenaarb7828692019-03-23 13:57:02 +01001179
1180 static luaV_Blob *
1181luaV_newblob(lua_State *L, blob_T *blo)
1182{
1183 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1184 *b = blo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001185 blo->bv_refcount++; // reference in Lua
1186 luaV_setudata(L, blo); // cache[blo] = udata
Bram Moolenaarb7828692019-03-23 13:57:02 +01001187 luaV_getfield(L, LUAVIM_BLOB);
1188 lua_setmetatable(L, -2);
1189 return b;
1190}
1191
1192luaV_pushtype(blob_T, blob, luaV_Blob)
1193luaV_type_tostring(blob, LUAVIM_BLOB)
1194
1195 static int
1196luaV_blob_gc(lua_State *L)
1197{
1198 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1199 blob_unref(b);
1200 return 0;
1201}
1202
1203 static int
1204luaV_blob_len(lua_State *L)
1205{
1206 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1207 lua_pushinteger(L, (int) blob_len(b));
1208 return 1;
1209}
1210
1211 static int
1212luaV_blob_index(lua_State *L)
1213{
1214 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1215 if (lua_isnumber(L, 2))
1216 {
1217 int idx = luaL_checkinteger(L, 2);
1218 if (idx < blob_len(b))
1219 lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1220 else
1221 lua_pushnil(L);
1222 }
1223 else if (lua_isstring(L, 2))
1224 {
1225 const char *s = lua_tostring(L, 2);
1226 if (strncmp(s, "add", 3) == 0)
1227 {
1228 lua_getmetatable(L, 1);
1229 lua_getfield(L, -1, s);
1230 }
1231 else
1232 lua_pushnil(L);
1233 }
1234 else
1235 lua_pushnil(L);
1236 return 1;
1237}
1238
1239 static int
1240luaV_blob_newindex(lua_State *L)
1241{
1242 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1243 if (b->bv_lock)
1244 luaL_error(L, "blob is locked");
1245 if (lua_isnumber(L, 2))
1246 {
1247 long len = blob_len(b);
1248 int idx = luaL_checkinteger(L, 2);
1249 int val = luaL_checkinteger(L, 3);
1250 if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
1251 {
1252 blob_set(b, idx, (char_u) val);
1253 if (idx == len)
1254 ++b->bv_ga.ga_len;
1255 }
1256 else
1257 luaL_error(L, "index out of range");
1258 }
1259 return 0;
1260}
1261
1262 static int
1263luaV_blob_add(lua_State *L)
1264{
1265 luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1266 blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1267 if (b->bv_lock)
1268 luaL_error(L, "blob is locked");
1269 lua_settop(L, 2);
1270 if (!lua_isstring(L, 2))
1271 luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1272 else
1273 {
1274 size_t i, l = 0;
1275 const char *s = lua_tolstring(L, 2, &l);
1276
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001277 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001278 for (i = 0; i < l; ++i)
1279 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001280 }
1281 lua_settop(L, 1);
1282 return 1;
1283}
1284
1285static const luaL_Reg luaV_Blob_mt[] = {
1286 {"__tostring", luaV_blob_tostring},
1287 {"__gc", luaV_blob_gc},
1288 {"__len", luaV_blob_len},
1289 {"__index", luaV_blob_index},
1290 {"__newindex", luaV_blob_newindex},
1291 {"add", luaV_blob_add},
1292 {NULL, NULL}
1293};
1294
1295
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001296// ======= Funcref type =======
Bram Moolenaarca06da92018-07-01 15:12:05 +02001297
1298 static luaV_Funcref *
1299luaV_newfuncref(lua_State *L, char_u *name)
1300{
1301 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1302
1303 if (name != NULL)
1304 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001305 func_ref(name);
1306 f->name = vim_strsave(name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001307 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001308 f->self = NULL;
1309 luaV_getfield(L, LUAVIM_FUNCREF);
1310 lua_setmetatable(L, -2);
1311 return f;
1312}
1313
1314 static luaV_Funcref *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001315luaV_pushfuncref(lua_State *L, char_u *name)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001316{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001317 return luaV_newfuncref(L, name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001318}
1319
1320
1321luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1322
1323 static int
1324luaV_funcref_gc(lua_State *L)
1325{
1326 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1327
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001328 func_unref(f->name);
1329 vim_free(f->name);
1330 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1331 // will be (or has been already) freed by Vim's garbage collection.
Bram Moolenaarca06da92018-07-01 15:12:05 +02001332 return 0;
1333}
1334
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001335// equivalent to string(funcref)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001336 static int
1337luaV_funcref_len(lua_State *L)
1338{
1339 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1340
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001341 lua_pushstring(L, (const char *) f->name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001342 return 1;
1343}
1344
1345 static int
1346luaV_funcref_call(lua_State *L)
1347{
1348 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001349 int i, n = lua_gettop(L) - 1; // #args
1350 int status = FAIL;
1351 typval_T args;
1352 typval_T rettv;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001353
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001354 args.v_type = VAR_LIST;
1355 args.vval.v_list = list_alloc();
1356 rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1357 if (args.vval.v_list != NULL)
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001358 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001359 typval_T v;
1360
Bram Moolenaar17413672018-07-14 20:49:42 +02001361 for (i = 0; i < n; i++)
1362 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001363 luaV_checktypval(L, i + 2, &v, "calling funcref");
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001364 list_append_tv(args.vval.v_list, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001365 clear_tv(&v);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001366 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001367 status = func_call(f->name, &args, NULL, f->self, &rettv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001368 if (status == OK)
1369 luaV_pushtypval(L, &rettv);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001370 clear_tv(&args);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001371 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001372 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001373 if (status != OK)
1374 luaL_error(L, "cannot call funcref");
1375 return 1;
1376}
1377
1378static const luaL_Reg luaV_Funcref_mt[] = {
1379 {"__tostring", luaV_funcref_tostring},
1380 {"__gc", luaV_funcref_gc},
1381 {"__len", luaV_funcref_len},
1382 {"__call", luaV_funcref_call},
1383 {NULL, NULL}
1384};
1385
1386
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001387// ======= Buffer type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001388
1389luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1390luaV_pushtype(buf_T, buffer, luaV_Buffer)
1391luaV_type_tostring(buffer, LUAVIM_BUFFER)
1392
1393 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001394luaV_buffer_len(lua_State *L)
1395{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001396 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1397 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001398 return 1;
1399}
1400
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001401 static int
1402luaV_buffer_call(lua_State *L)
1403{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001404 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001405 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001406 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001407 return 1;
1408}
1409
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001410 static int
1411luaV_buffer_index(lua_State *L)
1412{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001413 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001414 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001415 if (n > 0 && n <= b->b_ml.ml_line_count)
1416 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001417 else if (lua_isstring(L, 2))
1418 {
1419 const char *s = lua_tostring(L, 2);
1420 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001421 lua_pushstring(L, (b->b_sfname == NULL)
1422 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001423 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001424 lua_pushstring(L, (b->b_ffname == NULL)
1425 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001426 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001427 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001428 // methods
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001429 else if (strncmp(s, "insert", 6) == 0
1430 || strncmp(s, "next", 4) == 0
1431 || strncmp(s, "previous", 8) == 0
1432 || strncmp(s, "isvalid", 7) == 0)
1433 {
1434 lua_getmetatable(L, 1);
1435 lua_getfield(L, -1, s);
1436 }
1437 else
1438 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001439 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001440 else
1441 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001442 return 1;
1443}
1444
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001445 static int
1446luaV_buffer_newindex(lua_State *L)
1447{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001448 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001449 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1450#ifdef HAVE_SANDBOX
1451 luaV_checksandbox(L);
1452#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001453 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001454 luaL_error(L, "invalid line number");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001455 if (lua_isnil(L, 3)) // delete line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001456 {
1457 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001458 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001459 if (u_savedel(n, 1L) == FAIL)
1460 {
1461 curbuf = buf;
1462 luaL_error(L, "cannot save undo information");
1463 }
Bram Moolenaarca70c072020-05-30 20:30:46 +02001464 else if (ml_delete(n) == FAIL)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001465 {
1466 curbuf = buf;
1467 luaL_error(L, "cannot delete line");
1468 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001469 else
1470 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001471 deleted_lines_mark(n, 1L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001472 if (b == curwin->w_buffer) // fix cursor in current window?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001473 {
1474 if (curwin->w_cursor.lnum >= n)
1475 {
1476 if (curwin->w_cursor.lnum > n)
1477 {
1478 curwin->w_cursor.lnum -= 1;
1479 check_cursor_col();
1480 }
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001481 else
1482 check_cursor();
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001483 changed_cline_bef_curs();
1484 }
1485 invalidate_botline();
1486 }
1487 }
1488 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001489 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001490 else if (lua_isstring(L, 3)) // update line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001491 {
1492 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001493 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001494 if (u_savesub(n) == FAIL)
1495 {
1496 curbuf = buf;
1497 luaL_error(L, "cannot save undo information");
1498 }
1499 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1500 {
1501 curbuf = buf;
1502 luaL_error(L, "cannot replace line");
1503 }
1504 else changed_bytes(n, 0);
1505 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001506 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001507 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001508 }
1509 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001510 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001511 return 0;
1512}
1513
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001514 static int
1515luaV_buffer_insert(lua_State *L)
1516{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001517 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1518 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1519 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001520 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1521 buf_T *buf;
1522 luaL_checktype(L, 2, LUA_TSTRING);
1523#ifdef HAVE_SANDBOX
1524 luaV_checksandbox(L);
1525#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001526 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001527 if (n < 0) n = 0;
1528 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001529 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001530 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001531 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001532 if (u_save(n, n + 1) == FAIL)
1533 {
1534 curbuf = buf;
1535 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001536 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001537 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1538 {
1539 curbuf = buf;
1540 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001541 }
1542 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001543 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001544 curbuf = buf;
1545 update_screen(VALID);
1546 return 0;
1547}
1548
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001549 static int
1550luaV_buffer_next(lua_State *L)
1551{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001552 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001553 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1554 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001555 return 1;
1556}
1557
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001558 static int
1559luaV_buffer_previous(lua_State *L)
1560{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001561 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001562 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1563 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001564 return 1;
1565}
1566
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001567 static int
1568luaV_buffer_isvalid(lua_State *L)
1569{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001570 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001571 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001572 lua_pushboolean(L, !lua_isnil(L, -1));
1573 return 1;
1574}
1575
1576static const luaL_Reg luaV_Buffer_mt[] = {
1577 {"__tostring", luaV_buffer_tostring},
1578 {"__len", luaV_buffer_len},
1579 {"__call", luaV_buffer_call},
1580 {"__index", luaV_buffer_index},
1581 {"__newindex", luaV_buffer_newindex},
1582 {"insert", luaV_buffer_insert},
1583 {"next", luaV_buffer_next},
1584 {"previous", luaV_buffer_previous},
1585 {"isvalid", luaV_buffer_isvalid},
1586 {NULL, NULL}
1587};
1588
1589
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001590// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001591
Bram Moolenaar1dced572012-04-05 16:54:08 +02001592luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1593luaV_pushtype(win_T, window, luaV_Window)
1594luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001595
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001596 static int
1597luaV_window_call(lua_State *L)
1598{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001599 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001600 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001601 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001602 return 1;
1603}
1604
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001605 static int
1606luaV_window_index(lua_State *L)
1607{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001608 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001609 const char *s = luaL_checkstring(L, 2);
1610 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001611 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001612 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001613 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001614 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001615 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001616 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001617 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001618 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001619 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001620 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001621 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001622 || strncmp(s, "previous", 8) == 0
1623 || strncmp(s, "isvalid", 7) == 0)
1624 {
1625 lua_getmetatable(L, 1);
1626 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001627 }
1628 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001629 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001630 return 1;
1631}
1632
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001633 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001634luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001635{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001636 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001637 const char *s = luaL_checkstring(L, 2);
1638 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001639 if (strncmp(s, "line", 4) == 0)
1640 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001641#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001642 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001643#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001644 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001645 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001646 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001647 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001648 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001649 else if (strncmp(s, "col", 3) == 0)
1650 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001651#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001652 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001653#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001654 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001655 w->w_set_curswant = TRUE;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001656 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001657 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001658 else if (strncmp(s, "width", 5) == 0)
1659 {
1660 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001661#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001662 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001663#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001664 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001665 win_setwidth(v);
1666 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001667 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001668 else if (strncmp(s, "height", 6) == 0)
1669 {
1670 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001671#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001672 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001673#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001674 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001675 win_setheight(v);
1676 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001677 }
1678 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001679 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001680 return 0;
1681}
1682
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001683 static int
1684luaV_window_next(lua_State *L)
1685{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001686 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001687 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1688 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001689 return 1;
1690}
1691
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001692 static int
1693luaV_window_previous(lua_State *L)
1694{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001695 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001696 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1697 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001698 return 1;
1699}
1700
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001701 static int
1702luaV_window_isvalid(lua_State *L)
1703{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001704 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001705 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001706 lua_pushboolean(L, !lua_isnil(L, -1));
1707 return 1;
1708}
1709
1710static const luaL_Reg luaV_Window_mt[] = {
1711 {"__tostring", luaV_window_tostring},
1712 {"__call", luaV_window_call},
1713 {"__index", luaV_window_index},
1714 {"__newindex", luaV_window_newindex},
1715 {"next", luaV_window_next},
1716 {"previous", luaV_window_previous},
1717 {"isvalid", luaV_window_isvalid},
1718 {NULL, NULL}
1719};
1720
1721
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001722// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001723
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001724 static int
1725luaV_print(lua_State *L)
1726{
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001727 int i, n = lua_gettop(L); // nargs
1728 const char *s;
1729 size_t l;
1730 garray_T msg_ga;
1731
1732 ga_init2(&msg_ga, 1, 128);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001733 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001734 for (i = 1; i <= n; i++)
1735 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001736 lua_pushvalue(L, -1); // tostring
1737 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001738 lua_call(L, 1, 1);
1739 s = lua_tolstring(L, -1, &l);
1740 if (s == NULL)
1741 return luaL_error(L, "cannot convert to string");
Bram Moolenaar78e006b2021-07-28 15:07:01 +02001742 if (i > 1)
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001743 ga_append(&msg_ga, ' '); // use space instead of tab
1744 ga_concat_len(&msg_ga, (char_u *)s, l);
Bram Moolenaar2a4bd002021-07-28 21:48:59 +02001745 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001746 }
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001747 // Replace any "\n" with "\0"
1748 for (i = 0; i < msg_ga.ga_len; i++)
1749 if (((char *)msg_ga.ga_data)[i] == '\n')
1750 ((char *)msg_ga.ga_data)[i] = '\0';
1751 lua_pushlstring(L, msg_ga.ga_data, msg_ga.ga_len);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001752 if (!got_int)
1753 luaV_msg(L);
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001754
1755 ga_clear(&msg_ga);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001756 return 0;
1757}
1758
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001759 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001760luaV_debug(lua_State *L)
1761{
1762 lua_settop(L, 0);
1763 lua_getglobal(L, "vim");
1764 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001765 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001766 for (;;)
1767 {
1768 const char *input;
1769 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001770 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001771 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001772 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001773 input = lua_tolstring(L, -1, &l);
1774 if (l == 0 || strcmp(input, "cont") == 0)
1775 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001776 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001777 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1778 || lua_pcall(L, 0, 0, 0))
1779 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001780 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001781 }
1782}
1783
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001784 static dict_T *
1785luaV_get_var_scope(lua_State *L)
1786{
1787 const char *scope = luaL_checkstring(L, 1);
1788 dict_T *dict = NULL;
1789
1790 if (STRICMP((char *)scope, "g") == 0)
1791 dict = get_globvar_dict();
1792 else if (STRICMP((char *)scope, "v") == 0)
1793 dict = get_vimvar_dict();
1794 else if (STRICMP((char *)scope, "b") == 0)
1795 dict = curbuf->b_vars;
1796 else if (STRICMP((char *)scope, "w") == 0)
1797 dict = curwin->w_vars;
1798 else if (STRICMP((char *)scope, "t") == 0)
1799 dict = curtab->tp_vars;
1800 else
1801 {
1802 luaL_error(L, "invalid scope %s", scope);
1803 return NULL;
1804 }
1805
1806 return dict;
1807}
1808
1809 static int
1810luaV_setvar(lua_State *L)
1811{
1812 dict_T *dict;
1813 dictitem_T *di;
1814 size_t len;
1815 char *name;
1816 int del;
1817 char *error = NULL;
1818
1819 name = (char *)luaL_checklstring(L, 3, &len);
1820 del = (lua_gettop(L) < 4) || lua_isnil(L, 4);
1821
1822 dict = luaV_get_var_scope(L);
1823 if (dict == NULL)
1824 return 0;
1825
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001826 di = dict_find(dict, (char_u *)name, (int)len);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001827 if (di != NULL)
1828 {
1829 if (di->di_flags & DI_FLAGS_RO)
1830 error = "variable is read-only";
1831 else if (di->di_flags & DI_FLAGS_LOCK)
1832 error = "variable is locked";
1833 else if (del && di->di_flags & DI_FLAGS_FIX)
1834 error = "variable is fixed";
1835 if (error != NULL)
1836 return luaL_error(L, error);
1837 }
1838 else if (dict->dv_lock)
1839 return luaL_error(L, "Dictionary is locked");
1840
1841 if (del)
1842 {
1843 // Delete the key
1844 if (di == NULL)
1845 // Doesn't exist, nothing to do
1846 return 0;
1847 else
1848 // Delete the entry
1849 dictitem_remove(dict, di);
1850 }
1851 else
1852 {
1853 // Update the key
1854 typval_T tv;
1855
h-east965d2ed2021-10-04 21:51:57 +01001856 // Convert the lua value to a Vim script type in the temporary variable
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001857 lua_pushvalue(L, 4);
1858 if (luaV_totypval(L, -1, &tv) == FAIL)
1859 return luaL_error(L, "Couldn't convert lua value");
1860
1861 if (di == NULL)
1862 {
1863 // Need to create an entry
1864 di = dictitem_alloc((char_u *)name);
1865 if (di == NULL)
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001866 {
1867 clear_tv(&tv);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001868 return 0;
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001869 }
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001870 // Update the value
1871 copy_tv(&tv, &di->di_tv);
Bram Moolenaar4a011592021-08-05 15:11:08 +02001872 if (dict_add(dict, di) == FAIL)
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001873 {
1874 dictitem_free(di);
1875 clear_tv(&tv);
Bram Moolenaar4a011592021-08-05 15:11:08 +02001876 return luaL_error(L, "Couldn't add to dictionary");
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001877 }
1878 }
1879 else
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001880 {
1881 // Clear the old value
1882 clear_tv(&di->di_tv);
1883 // Update the value
1884 copy_tv(&tv, &di->di_tv);
1885 }
1886
1887 // Clear the temporary variable
1888 clear_tv(&tv);
1889 }
1890
1891 return 0;
1892}
1893
1894 static int
1895luaV_getvar(lua_State *L)
1896{
1897 dict_T *dict = luaV_get_var_scope(L);
1898 size_t len;
1899 const char *name = luaL_checklstring(L, 3, &len);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001900 dictitem_T *di = dict_find(dict, (char_u *)name, (int)len);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001901
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001902 if (di == NULL)
1903 return 0; // nil
1904
1905 luaV_pushtypval(L, &di->di_tv);
1906 return 1;
1907}
1908
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001909 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001910luaV_command(lua_State *L)
1911{
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001912 char_u *s = vim_strsave((char_u *)luaL_checkstring(L, 1));
1913
1914 execute_cmds_from_string(s);
1915 vim_free(s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001916 update_screen(VALID);
1917 return 0;
1918}
1919
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001920 static int
1921luaV_eval(lua_State *L)
1922{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001923 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001924
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001925 if (tv == NULL) luaL_error(L, "invalid expression");
1926 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001927 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001928 return 1;
1929}
1930
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001931 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001932luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001933{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001934 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001935 return 0;
1936}
1937
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001938 static int
1939luaV_line(lua_State *L)
1940{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001941 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1942 return 1;
1943}
1944
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001945 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001946luaV_list(lua_State *L)
1947{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001948 list_T *l;
1949 int initarg = !lua_isnoneornil(L, 1);
1950
1951 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1952 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1953 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001954 if (l == NULL)
1955 lua_pushnil(L);
1956 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001957 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001958 luaV_newlist(L, l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001959 if (initarg) // traverse table to init list
Bram Moolenaar17413672018-07-14 20:49:42 +02001960 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001961 int notnil, i = 0;
1962 typval_T v;
Bram Moolenaar17413672018-07-14 20:49:42 +02001963 do
1964 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001965 lua_rawgeti(L, 1, ++i);
1966 notnil = !lua_isnil(L, -1);
Bram Moolenaar17413672018-07-14 20:49:42 +02001967 if (notnil)
1968 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001969 luaV_checktypval(L, -1, &v, "vim.list");
1970 list_append_tv(l, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001971 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001972 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001973 lua_pop(L, 1); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001974 } while (notnil);
1975 }
1976 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001977 return 1;
1978}
1979
1980 static int
1981luaV_dict(lua_State *L)
1982{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001983 dict_T *d;
1984 int initarg = !lua_isnoneornil(L, 1);
1985
1986 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1987 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1988 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001989 if (d == NULL)
1990 lua_pushnil(L);
1991 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001992 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001993 luaV_newdict(L, d);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001994 if (initarg) // traverse table to init dict
Bram Moolenaarca06da92018-07-01 15:12:05 +02001995 {
1996 lua_pushnil(L);
1997 while (lua_next(L, 1))
1998 {
1999 char_u *key;
2000 dictitem_T *di;
2001 typval_T v;
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02002002
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002003 lua_pushvalue(L, -2); // dup key in case it's a number
Bram Moolenaarca06da92018-07-01 15:12:05 +02002004 key = (char_u *) lua_tostring(L, -1);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02002005 if (key == NULL)
2006 {
2007 lua_pushnil(L);
2008 return 1;
2009 }
2010 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002011 luaL_error(L, "table has empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002012 luaV_checktypval(L, -2, &v, "vim.dict"); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02002013 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02002014 if (di == NULL || dict_add(d, di) == FAIL)
2015 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02002016 vim_free(di);
2017 lua_pushnil(L);
2018 return 1;
2019 }
Bram Moolenaare49b8e82020-07-01 13:52:55 +02002020 di->di_tv = v;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002021 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02002022 }
2023 }
2024 }
2025 return 1;
2026}
2027
2028 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01002029luaV_blob(lua_State *L)
2030{
2031 blob_T *b;
2032 int initarg = !lua_isnoneornil(L, 1);
2033
2034 if (initarg && !lua_isstring(L, 1))
2035 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
2036 b = blob_alloc();
2037 if (b == NULL)
2038 lua_pushnil(L);
2039 else
2040 {
2041 luaV_newblob(L, b);
2042 if (initarg)
2043 {
2044 size_t i, l = 0;
2045 const char *s = lua_tolstring(L, 1, &l);
2046
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01002047 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01002048 for (i = 0; i < l; ++i)
2049 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002050 }
2051 }
2052 return 1;
2053}
2054
2055 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02002056luaV_funcref(lua_State *L)
2057{
2058 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002059 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002060 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
2061 luaL_error(L, "invalid function name: %s", name);
2062 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002063 return 1;
2064}
2065
2066 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002067luaV_buffer(lua_State *L)
2068{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002069 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002070 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002071 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002072 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002073 {
2074 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02002075 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002076 if (buf->b_fnum == n) break;
2077 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002078 else // by name
2079 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002080 size_t l;
2081 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02002082 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002083 {
2084 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
2085 {
2086 if (l == 0) break;
2087 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02002088 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
2089 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002090 break;
2091 }
2092 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002093 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002094 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002095 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002096 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002097 return 1;
2098}
2099
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002100 static int
2101luaV_window(lua_State *L)
2102{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002103 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002104 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002105 {
2106 int n = lua_tointeger(L, 1);
2107 for (win = firstwin; win != NULL; win = win->w_next, n--)
2108 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002109 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002110 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002111 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002112 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002113 return 1;
2114}
2115
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002116 static int
2117luaV_open(lua_State *L)
2118{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002119 char_u *s = NULL;
2120#ifdef HAVE_SANDBOX
2121 luaV_checksandbox(L);
2122#endif
2123 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01002124 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002125 return 1;
2126}
2127
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002128 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002129luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002130{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002131 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002132 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002133 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02002134 lua_settop(L, 1);
2135 if (lua_getmetatable(L, 1))
2136 {
2137 luaV_getfield(L, LUAVIM_LIST);
2138 if (lua_rawequal(L, -1, 2))
2139 {
2140 lua_pushstring(L, "list");
2141 return 1;
2142 }
2143 luaV_getfield(L, LUAVIM_DICT);
2144 if (lua_rawequal(L, -1, 2))
2145 {
2146 lua_pushstring(L, "dict");
2147 return 1;
2148 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01002149 luaV_getfield(L, LUAVIM_BLOB);
2150 if (lua_rawequal(L, -1, 2))
2151 {
2152 lua_pushstring(L, "blob");
2153 return 1;
2154 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002155 luaV_getfield(L, LUAVIM_FUNCREF);
2156 if (lua_rawequal(L, -1, 2))
2157 {
2158 lua_pushstring(L, "funcref");
2159 return 1;
2160 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002161 luaV_getfield(L, LUAVIM_BUFFER);
2162 if (lua_rawequal(L, -1, 2))
2163 {
2164 lua_pushstring(L, "buffer");
2165 return 1;
2166 }
2167 luaV_getfield(L, LUAVIM_WINDOW);
2168 if (lua_rawequal(L, -1, 2))
2169 {
2170 lua_pushstring(L, "window");
2171 return 1;
2172 }
2173 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002174 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002175 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02002176 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002177}
2178
Bram Moolenaareb04f082020-05-17 14:32:35 +02002179 static int
2180luaV_call(lua_State *L)
2181{
2182 int argc = lua_gettop(L) - 1;
2183 size_t funcname_len;
2184 char_u *funcname;
2185 char *error = NULL;
2186 typval_T rettv;
2187 typval_T argv[MAX_FUNC_ARGS + 1];
2188 int i = 0;
2189
2190 if (argc > MAX_FUNC_ARGS)
2191 return luaL_error(L, "Function called with too many arguments");
2192
2193 funcname = (char_u *)luaL_checklstring(L, 1, &funcname_len);
2194
2195 for (; i < argc; i++)
2196 {
2197 if (luaV_totypval(L, i + 2, &argv[i]) == FAIL)
2198 {
2199 error = "lua: cannot convert value";
2200 goto free_vim_args;
2201 }
2202 }
2203
2204 argv[argc].v_type = VAR_UNKNOWN;
2205
2206 if (call_vim_function(funcname, argc, argv, &rettv) == FAIL)
2207 {
2208 error = "lua: call_vim_function failed";
2209 goto free_vim_args;
2210 }
2211
2212 luaV_pushtypval(L, &rettv);
2213 clear_tv(&rettv);
2214
2215free_vim_args:
2216 while (i > 0)
2217 clear_tv(&argv[--i]);
2218
2219 if (error == NULL)
2220 return 1;
2221 else
2222 return luaL_error(L, error);
2223}
2224
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02002225/*
2226 * Return the Vim version as a Lua table
2227 */
2228 static int
2229luaV_version(lua_State *L)
2230{
2231 lua_newtable(L);
2232 lua_pushstring(L, "major");
2233 lua_pushinteger(L, VIM_VERSION_MAJOR);
2234 lua_settable(L, -3);
2235 lua_pushstring(L, "minor");
2236 lua_pushinteger(L, VIM_VERSION_MINOR);
2237 lua_settable(L, -3);
2238 lua_pushstring(L, "patch");
2239 lua_pushinteger(L, highest_patch());
2240 lua_settable(L, -3);
2241 return 1;
2242}
2243
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002244static const luaL_Reg luaV_module[] = {
2245 {"command", luaV_command},
2246 {"eval", luaV_eval},
2247 {"beep", luaV_beep},
2248 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002249 {"list", luaV_list},
2250 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01002251 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02002252 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002253 {"buffer", luaV_buffer},
2254 {"window", luaV_window},
2255 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002256 {"type", luaV_type},
Bram Moolenaareb04f082020-05-17 14:32:35 +02002257 {"call", luaV_call},
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002258 {"_getvar", luaV_getvar},
2259 {"_setvar", luaV_setvar},
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02002260 {"version", luaV_version},
Bram Moolenaar125ed272021-04-07 20:11:12 +02002261 {"lua_version", NULL},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002262 {NULL, NULL}
2263};
2264
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002265/*
2266 * for freeing list, dict, buffer and window objects; lightuserdata as arg
2267 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02002268 static int
2269luaV_free(lua_State *L)
2270{
2271 lua_pushnil(L);
2272 luaV_setudata(L, lua_touserdata(L, 1));
2273 return 0;
2274}
2275
2276 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002277luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002278{
2279 luaL_Buffer b;
2280 size_t l;
2281 const char *str = lua_tolstring(L, 1, &l);
2282 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
2283 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
2284 luaL_buffinit(L, &b);
2285 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
2286 luaL_addlstring(&b, str, l);
2287 luaL_pushresult(&b);
2288 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002289 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002290 {
2291 luaV_emsg(L);
2292 return 0;
2293 }
2294 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002295 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002296 {
2297 luaV_emsg(L);
2298 return 0;
2299 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002300 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002301 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01002302 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002303}
2304
2305 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002306luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002307{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002308 int copyID = lua_tointeger(L, 1);
2309 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002310
Bram Moolenaar1dced572012-04-05 16:54:08 +02002311 luaV_getfield(L, LUAVIM_LIST);
2312 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002313 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002314 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002315 // traverse cache table
Bram Moolenaarb84634d2015-02-04 22:02:37 +01002316 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002317 {
2318 lua_getmetatable(L, -1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002319 if (lua_rawequal(L, -1, 2)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002320 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002321 list_T *l = (list_T *)lua_touserdata(L, 5); // key
2322
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002323 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002324 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002325 else if (lua_rawequal(L, -1, 3)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002326 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002327 dict_T *d = (dict_T *)lua_touserdata(L, 5); // key
2328
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002329 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002330 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002331 else if (lua_rawequal(L, -1, 4)) // funcref?
2332 {
2333 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key
2334
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002335 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002336 }
2337 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02002338 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002339 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01002340 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002341}
2342
Bram Moolenaar125ed272021-04-07 20:11:12 +02002343 static int
2344luaV_pushversion(lua_State *L)
2345{
2346 int major = 0;
2347 int minor = 0;
2348 int patch = 0;
2349 char s[16];
2350
2351 sscanf(LUAVIM_VERSION, "Lua %d.%d.%d", &major, &minor, &patch);
2352 vim_snprintf(s, sizeof(s), "%d.%d.%d", major, minor, patch);
2353 lua_pushstring(L, s);
2354 return 0;
2355}
2356
Bram Moolenaareb04f082020-05-17 14:32:35 +02002357#define LUA_VIM_FN_CODE \
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002358 "vim.fn = setmetatable({}, {\n"\
2359 " __index = function (t, key)\n"\
2360 " local function _fn(...)\n"\
2361 " return vim.call(key, ...)\n"\
2362 " end\n"\
2363 " t[key] = _fn\n"\
2364 " return _fn\n"\
2365 " end\n"\
2366 " })"
2367
2368#define LUA_VIM_UPDATE_PACKAGE_PATHS \
2369 "local last_vim_paths = {}\n"\
2370 "vim._update_package_paths = function ()\n"\
2371 " local cur_vim_paths = {}\n"\
2372 " local function split(s, delimiter)\n"\
2373 " result = {}\n"\
2374 " for match in (s..delimiter):gmatch(\"(.-)\"..delimiter) do\n"\
2375 " table.insert(result, match)\n"\
2376 " end\n"\
2377 " return result\n"\
2378 " end\n"\
2379 " local rtps = split(vim.eval('&runtimepath'), ',')\n"\
2380 " local sep = package.config:sub(1, 1)\n"\
2381 " for _, key in ipairs({'path', 'cpath'}) do\n"\
2382 " local orig_str = package[key] .. ';'\n"\
2383 " local pathtrails_ordered = {}\n"\
2384 " -- Note: ignores trailing item without trailing `;`. Not using something\n"\
2385 " -- simpler in order to preserve empty items (stand for default path).\n"\
2386 " local orig = {}\n"\
2387 " for s in orig_str:gmatch('[^;]*;') do\n"\
2388 " s = s:sub(1, -2) -- Strip trailing semicolon\n"\
2389 " orig[#orig + 1] = s\n"\
2390 " end\n"\
2391 " if key == 'path' then\n"\
2392 " -- /?.lua and /?/init.lua\n"\
2393 " pathtrails_ordered = {sep .. '?.lua', sep .. '?' .. sep .. 'init.lua'}\n"\
2394 " else\n"\
2395 " local pathtrails = {}\n"\
2396 " for _, s in ipairs(orig) do\n"\
2397 " -- Find out path patterns. pathtrail should contain something like\n"\
2398 " -- /?.so, \?.dll. This allows not to bother determining what correct\n"\
2399 " -- suffixes are.\n"\
2400 " local pathtrail = s:match('[/\\\\][^/\\\\]*%?.*$')\n"\
2401 " if pathtrail and not pathtrails[pathtrail] then\n"\
2402 " pathtrails[pathtrail] = true\n"\
2403 " pathtrails_ordered[#pathtrails_ordered + 1] = pathtrail\n"\
2404 " end\n"\
2405 " end\n"\
2406 " end\n"\
2407 " local new = {}\n"\
2408 " for _, rtp in ipairs(rtps) do\n"\
2409 " if not rtp:match(';') then\n"\
2410 " for _, pathtrail in pairs(pathtrails_ordered) do\n"\
2411 " local new_path = rtp .. sep .. 'lua' .. pathtrail\n"\
2412 " -- Always keep paths from &runtimepath at the start:\n"\
2413 " -- append them here disregarding orig possibly containing one of them.\n"\
2414 " new[#new + 1] = new_path\n"\
2415 " cur_vim_paths[new_path] = true\n"\
2416 " end\n"\
2417 " end\n"\
2418 " end\n"\
2419 " for _, orig_path in ipairs(orig) do\n"\
2420 " -- Handle removing obsolete paths originating from &runtimepath: such\n"\
2421 " -- paths either belong to cur_nvim_paths and were already added above or\n"\
2422 " -- to last_nvim_paths and should not be added at all if corresponding\n"\
2423 " -- entry was removed from &runtimepath list.\n"\
2424 " if not (cur_vim_paths[orig_path] or last_vim_paths[orig_path]) then\n"\
2425 " new[#new + 1] = orig_path\n"\
2426 " end\n"\
2427 " end\n"\
2428 " package[key] = table.concat(new, ';')\n"\
2429 " end\n"\
2430 " last_vim_paths = cur_vim_paths\n"\
2431 "end"
Bram Moolenaareb04f082020-05-17 14:32:35 +02002432
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002433#define LUA_VIM_SETUP_VARIABLE_DICTS \
2434 "do\n"\
2435 " local function make_dict_accessor(scope)\n"\
2436 " local mt = {}\n"\
2437 " function mt:__newindex(k, v)\n"\
2438 " return vim._setvar(scope, 0, k, v)\n"\
2439 " end\n"\
2440 " function mt:__index(k)\n"\
2441 " return vim._getvar(scope, 0, k)\n"\
2442 " end\n"\
2443 " return setmetatable({}, mt)\n"\
2444 " end\n"\
2445 " vim.g = make_dict_accessor('g')\n"\
2446 " vim.v = make_dict_accessor('v')\n"\
2447 " vim.b = make_dict_accessor('b')\n"\
2448 " vim.w = make_dict_accessor('w')\n"\
2449 " vim.t = make_dict_accessor('t')\n"\
2450 "end"
2451
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002452 static int
2453luaopen_vim(lua_State *L)
2454{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002455 // set cache table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002456 lua_newtable(L);
2457 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002458 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002459 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002460 lua_setmetatable(L, -2); // cache is weak-valued
2461 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002462 lua_pushcfunction(L, luaV_print);
2463 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002464 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002465 lua_getglobal(L, "debug");
2466 lua_pushcfunction(L, luaV_debug);
2467 lua_setfield(L, -2, "debug");
2468 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002469 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002470 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002471 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002472 lua_pushcclosure(L, luaV_free, 1);
2473 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002474 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002475 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002476 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002477 lua_pushcclosure(L, luaV_luaeval, 1);
2478 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002479 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002480 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002481 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002482 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002483 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002484 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002485 luaV_newmetatable(L, LUAVIM_LIST);
2486 lua_pushvalue(L, 1);
2487 luaV_openlib(L, luaV_List_mt, 1);
2488 luaV_newmetatable(L, LUAVIM_DICT);
2489 lua_pushvalue(L, 1);
2490 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002491 luaV_newmetatable(L, LUAVIM_BLOB);
2492 lua_pushvalue(L, 1);
2493 luaV_openlib(L, luaV_Blob_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002494 luaV_newmetatable(L, LUAVIM_FUNCREF);
2495 lua_pushvalue(L, 1);
2496 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002497 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002498 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002499 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002500 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002501 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002502 luaV_openlib(L, luaV_Window_mt, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002503 lua_newtable(L); // vim table
2504 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002505 luaV_openlib(L, luaV_module, 1);
Bram Moolenaar125ed272021-04-07 20:11:12 +02002506 luaV_pushversion(L);
2507 lua_setfield(L, -2, "lua_version");
Bram Moolenaar1dced572012-04-05 16:54:08 +02002508 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaareb04f082020-05-17 14:32:35 +02002509 // custom code
Bram Moolenaar9309eb22020-05-17 16:53:56 +02002510 (void)luaL_dostring(L, LUA_VIM_FN_CODE);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002511 (void)luaL_dostring(L, LUA_VIM_UPDATE_PACKAGE_PATHS);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002512 (void)luaL_dostring(L, LUA_VIM_SETUP_VARIABLE_DICTS);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002513
2514 lua_getglobal(L, "vim");
2515 lua_getfield(L, -1, "_update_package_paths");
2516
2517 if (lua_pcall(L, 0, 0, 0))
2518 luaV_emsg(L);
2519
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002520 return 0;
2521}
2522
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002523 static lua_State *
2524luaV_newstate(void)
2525{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002526 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002527 luaL_openlibs(L); // core libs
2528 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002529 lua_call(L, 0, 0);
2530 return L;
2531}
2532
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002533 static void
2534luaV_setrange(lua_State *L, int line1, int line2)
2535{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002536 lua_getglobal(L, LUAVIM_NAME);
2537 lua_pushinteger(L, line1);
2538 lua_setfield(L, -2, "firstline");
2539 lua_pushinteger(L, line2);
2540 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002541 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002542}
2543
2544
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002545// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002546
2547static lua_State *L = NULL;
2548
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002549 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002550lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002551{
2552 return L != NULL;
2553}
2554
2555 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002556lua_init(void)
2557{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002558 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002559 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002560#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002561 if (!lua_enabled(TRUE))
2562 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002563 emsg(_("Lua library cannot be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002564 return FAIL;
2565 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002566#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002567 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002568 }
2569 return OK;
2570}
2571
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002572 void
2573lua_end(void)
2574{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002575 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002576 {
2577 lua_close(L);
2578 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002579 }
2580}
2581
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002582/*
2583 * ex commands
2584 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002585 void
2586ex_lua(exarg_T *eap)
2587{
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002588 char *script = (char *)script_get(eap, eap->arg);
2589
2590 if (!eap->skip && lua_init() == OK)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002591 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002592 char *s = script != NULL ? script : (char *)eap->arg;
2593
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002594 luaV_setrange(L, eap->line1, eap->line2);
2595 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2596 || lua_pcall(L, 0, 0, 0))
2597 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002598 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002599 if (script != NULL)
2600 vim_free(script);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002601}
2602
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002603 void
2604ex_luado(exarg_T *eap)
2605{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002606 linenr_T l;
2607 const char *s = (const char *) eap->arg;
2608 luaL_Buffer b;
2609 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002610 buf_T *was_curbuf = curbuf;
2611
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002612 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002613 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2614 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002615 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002616 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002617 }
2618 luaV_setrange(L, eap->line1, eap->line2);
2619 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002620 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002621 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002622 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002623 luaL_pushresult(&b);
2624 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002625 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2626 {
2627 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002628 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002629 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002630 }
2631 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002632 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002633 for (l = eap->line1; l <= eap->line2; l++)
2634 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02002635 // Check the line number, the command may have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002636 if (l > curbuf->b_ml.ml_line_count)
2637 break;
2638
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002639 lua_pushvalue(L, -1); // function
2640 luaV_pushline(L, curbuf, l); // current line as arg
2641 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002642 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002643 {
2644 luaV_emsg(L);
2645 break;
2646 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002647 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002648 if (curbuf != was_curbuf)
2649 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002650 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002651 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002652#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002653 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002654#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002655 ml_replace(l, luaV_toline(L, -1), TRUE);
2656 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002657 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002658 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002659 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002660 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002661 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002662 check_cursor();
2663 update_screen(NOT_VALID);
2664}
2665
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002666 void
2667ex_luafile(exarg_T *eap)
2668{
2669 if (lua_init() == FAIL)
2670 return;
2671 if (!eap->skip)
2672 {
2673 luaV_setrange(L, eap->line1, eap->line2);
2674 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2675 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002676 }
2677}
2678
Bram Moolenaar1dced572012-04-05 16:54:08 +02002679#define luaV_freetype(typ,tname) \
2680 void \
2681 lua_##tname##_free(typ *o) \
2682 { \
2683 if (!lua_isopen()) return; \
2684 luaV_getfield(L, LUAVIM_FREE); \
2685 lua_pushlightuserdata(L, (void *) o); \
2686 lua_call(L, 1, 0); \
2687 }
2688
2689luaV_freetype(buf_T, buffer)
2690luaV_freetype(win_T, window)
2691
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002692 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002693do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002694{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002695 lua_init();
2696 luaV_getfield(L, LUAVIM_LUAEVAL);
2697 lua_pushstring(L, (char *) str);
2698 lua_pushlightuserdata(L, (void *) arg);
2699 lua_pushlightuserdata(L, (void *) rettv);
2700 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002701}
2702
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002703 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002704set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002705{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002706 int aborted = 0;
2707
2708 if (lua_isopen())
2709 {
2710 luaV_getfield(L, LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002711 // call the function with 1 arg, getting 1 result back
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002712 lua_pushinteger(L, copyID);
2713 lua_call(L, 1, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002714 // get the result
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002715 aborted = lua_tointeger(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002716 // pop result off the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002717 lua_pop(L, 1);
2718 }
2719 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002720}
2721
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002722 void
2723update_package_paths_in_lua()
2724{
2725 if (lua_isopen())
2726 {
2727 lua_getglobal(L, "vim");
2728 lua_getfield(L, -1, "_update_package_paths");
2729
2730 if (lua_pcall(L, 0, 0, 0))
2731 luaV_emsg(L);
2732 }
2733}
2734
Bram Moolenaar801ab062020-06-25 19:27:56 +02002735/*
2736 * Native C function callback
2737 */
2738 static int
2739luaV_call_lua_func(
2740 int argcount,
2741 typval_T *argvars,
2742 typval_T *rettv,
2743 void *state)
2744{
2745 int i;
2746 int luaargcount = argcount;
2747 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2748 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2749
2750 if (funcstate->lua_tableref != LUA_NOREF)
2751 {
2752 // First arg for metatable __call method is a table
2753 luaargcount += 1;
2754 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2755 }
2756
2757 for (i = 0; i < argcount; ++i)
2758 luaV_pushtypval(funcstate->L, &argvars[i]);
2759
2760 if (lua_pcall(funcstate->L, luaargcount, 1, 0))
2761 {
2762 luaV_emsg(funcstate->L);
2763 return FCERR_OTHER;
2764 }
2765
2766 luaV_checktypval(funcstate->L, -1, rettv, "get return value");
2767 return FCERR_NONE;
2768}
2769
2770/*
2771 * Free up any lua references held by the func state.
2772 */
2773 static void
2774luaV_call_lua_func_free(void *state)
2775{
2776 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2777 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2778 funcstate->L = NULL;
2779 if (funcstate->lua_tableref != LUA_NOREF)
2780 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2781 VIM_CLEAR(funcstate);
2782}
2783
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002784#endif