blob: 250da02ae2285343cf556de44c5bcb7d84a195c0 [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 }
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001504 else
1505 changed_bytes(n, 0);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001506 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001507 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001508 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001509 }
1510 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001511 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001512 return 0;
1513}
1514
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001515 static int
1516luaV_buffer_insert(lua_State *L)
1517{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001518 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1519 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1520 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001521 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1522 buf_T *buf;
1523 luaL_checktype(L, 2, LUA_TSTRING);
1524#ifdef HAVE_SANDBOX
1525 luaV_checksandbox(L);
1526#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001527 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001528 if (n < 0) n = 0;
1529 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001530 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001531 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001532 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001533 if (u_save(n, n + 1) == FAIL)
1534 {
1535 curbuf = buf;
1536 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001537 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001538 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1539 {
1540 curbuf = buf;
1541 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001542 }
1543 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001544 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001545 curbuf = buf;
1546 update_screen(VALID);
1547 return 0;
1548}
1549
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001550 static int
1551luaV_buffer_next(lua_State *L)
1552{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001553 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001554 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1555 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001556 return 1;
1557}
1558
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001559 static int
1560luaV_buffer_previous(lua_State *L)
1561{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001562 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001563 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1564 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001565 return 1;
1566}
1567
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001568 static int
1569luaV_buffer_isvalid(lua_State *L)
1570{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001571 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001572 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001573 lua_pushboolean(L, !lua_isnil(L, -1));
1574 return 1;
1575}
1576
1577static const luaL_Reg luaV_Buffer_mt[] = {
1578 {"__tostring", luaV_buffer_tostring},
1579 {"__len", luaV_buffer_len},
1580 {"__call", luaV_buffer_call},
1581 {"__index", luaV_buffer_index},
1582 {"__newindex", luaV_buffer_newindex},
1583 {"insert", luaV_buffer_insert},
1584 {"next", luaV_buffer_next},
1585 {"previous", luaV_buffer_previous},
1586 {"isvalid", luaV_buffer_isvalid},
1587 {NULL, NULL}
1588};
1589
1590
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001591// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001592
Bram Moolenaar1dced572012-04-05 16:54:08 +02001593luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1594luaV_pushtype(win_T, window, luaV_Window)
1595luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001596
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001597 static int
1598luaV_window_call(lua_State *L)
1599{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001600 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001601 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001602 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001603 return 1;
1604}
1605
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001606 static int
1607luaV_window_index(lua_State *L)
1608{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001609 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001610 const char *s = luaL_checkstring(L, 2);
1611 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001612 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001613 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001614 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001615 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001616 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001617 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001618 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001619 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001620 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001621 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001622 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001623 || strncmp(s, "previous", 8) == 0
1624 || strncmp(s, "isvalid", 7) == 0)
1625 {
1626 lua_getmetatable(L, 1);
1627 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001628 }
1629 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001630 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001631 return 1;
1632}
1633
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001634 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001635luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001636{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001637 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001638 const char *s = luaL_checkstring(L, 2);
1639 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001640 if (strncmp(s, "line", 4) == 0)
1641 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001642#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001643 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001644#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001645 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001646 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001647 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001648 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001649 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001650 else if (strncmp(s, "col", 3) == 0)
1651 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001652#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001653 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001654#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001655 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001656 w->w_set_curswant = TRUE;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001657 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001658 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001659 else if (strncmp(s, "width", 5) == 0)
1660 {
1661 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001662#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001663 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001664#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001665 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001666 win_setwidth(v);
1667 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001668 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001669 else if (strncmp(s, "height", 6) == 0)
1670 {
1671 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001672#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001673 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001674#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001675 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001676 win_setheight(v);
1677 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001678 }
1679 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001680 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001681 return 0;
1682}
1683
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001684 static int
1685luaV_window_next(lua_State *L)
1686{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001687 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001688 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1689 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001690 return 1;
1691}
1692
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001693 static int
1694luaV_window_previous(lua_State *L)
1695{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001696 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001697 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1698 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001699 return 1;
1700}
1701
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001702 static int
1703luaV_window_isvalid(lua_State *L)
1704{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001705 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001706 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001707 lua_pushboolean(L, !lua_isnil(L, -1));
1708 return 1;
1709}
1710
1711static const luaL_Reg luaV_Window_mt[] = {
1712 {"__tostring", luaV_window_tostring},
1713 {"__call", luaV_window_call},
1714 {"__index", luaV_window_index},
1715 {"__newindex", luaV_window_newindex},
1716 {"next", luaV_window_next},
1717 {"previous", luaV_window_previous},
1718 {"isvalid", luaV_window_isvalid},
1719 {NULL, NULL}
1720};
1721
1722
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001723// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001724
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001725 static int
1726luaV_print(lua_State *L)
1727{
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001728 int i, n = lua_gettop(L); // nargs
1729 const char *s;
1730 size_t l;
1731 garray_T msg_ga;
1732
1733 ga_init2(&msg_ga, 1, 128);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001734 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001735 for (i = 1; i <= n; i++)
1736 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001737 lua_pushvalue(L, -1); // tostring
1738 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001739 lua_call(L, 1, 1);
1740 s = lua_tolstring(L, -1, &l);
1741 if (s == NULL)
1742 return luaL_error(L, "cannot convert to string");
Bram Moolenaar78e006b2021-07-28 15:07:01 +02001743 if (i > 1)
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001744 ga_append(&msg_ga, ' '); // use space instead of tab
1745 ga_concat_len(&msg_ga, (char_u *)s, l);
Bram Moolenaar2a4bd002021-07-28 21:48:59 +02001746 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001747 }
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001748 // Replace any "\n" with "\0"
1749 for (i = 0; i < msg_ga.ga_len; i++)
1750 if (((char *)msg_ga.ga_data)[i] == '\n')
1751 ((char *)msg_ga.ga_data)[i] = '\0';
1752 lua_pushlstring(L, msg_ga.ga_data, msg_ga.ga_len);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001753 if (!got_int)
1754 luaV_msg(L);
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001755
1756 ga_clear(&msg_ga);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001757 return 0;
1758}
1759
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001760 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001761luaV_debug(lua_State *L)
1762{
1763 lua_settop(L, 0);
1764 lua_getglobal(L, "vim");
1765 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001766 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001767 for (;;)
1768 {
1769 const char *input;
1770 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001771 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001772 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001773 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001774 input = lua_tolstring(L, -1, &l);
1775 if (l == 0 || strcmp(input, "cont") == 0)
1776 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001777 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001778 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1779 || lua_pcall(L, 0, 0, 0))
1780 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001781 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001782 }
1783}
1784
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001785 static dict_T *
1786luaV_get_var_scope(lua_State *L)
1787{
1788 const char *scope = luaL_checkstring(L, 1);
1789 dict_T *dict = NULL;
1790
1791 if (STRICMP((char *)scope, "g") == 0)
1792 dict = get_globvar_dict();
1793 else if (STRICMP((char *)scope, "v") == 0)
1794 dict = get_vimvar_dict();
1795 else if (STRICMP((char *)scope, "b") == 0)
1796 dict = curbuf->b_vars;
1797 else if (STRICMP((char *)scope, "w") == 0)
1798 dict = curwin->w_vars;
1799 else if (STRICMP((char *)scope, "t") == 0)
1800 dict = curtab->tp_vars;
1801 else
1802 {
1803 luaL_error(L, "invalid scope %s", scope);
1804 return NULL;
1805 }
1806
1807 return dict;
1808}
1809
1810 static int
1811luaV_setvar(lua_State *L)
1812{
1813 dict_T *dict;
1814 dictitem_T *di;
1815 size_t len;
1816 char *name;
1817 int del;
1818 char *error = NULL;
1819
1820 name = (char *)luaL_checklstring(L, 3, &len);
1821 del = (lua_gettop(L) < 4) || lua_isnil(L, 4);
1822
1823 dict = luaV_get_var_scope(L);
1824 if (dict == NULL)
1825 return 0;
1826
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001827 di = dict_find(dict, (char_u *)name, (int)len);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001828 if (di != NULL)
1829 {
1830 if (di->di_flags & DI_FLAGS_RO)
1831 error = "variable is read-only";
1832 else if (di->di_flags & DI_FLAGS_LOCK)
1833 error = "variable is locked";
1834 else if (del && di->di_flags & DI_FLAGS_FIX)
1835 error = "variable is fixed";
1836 if (error != NULL)
1837 return luaL_error(L, error);
1838 }
1839 else if (dict->dv_lock)
1840 return luaL_error(L, "Dictionary is locked");
1841
1842 if (del)
1843 {
1844 // Delete the key
1845 if (di == NULL)
1846 // Doesn't exist, nothing to do
1847 return 0;
1848 else
1849 // Delete the entry
1850 dictitem_remove(dict, di);
1851 }
1852 else
1853 {
1854 // Update the key
1855 typval_T tv;
1856
h-east965d2ed2021-10-04 21:51:57 +01001857 // Convert the lua value to a Vim script type in the temporary variable
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001858 lua_pushvalue(L, 4);
1859 if (luaV_totypval(L, -1, &tv) == FAIL)
1860 return luaL_error(L, "Couldn't convert lua value");
1861
1862 if (di == NULL)
1863 {
1864 // Need to create an entry
1865 di = dictitem_alloc((char_u *)name);
1866 if (di == NULL)
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001867 {
1868 clear_tv(&tv);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001869 return 0;
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001870 }
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001871 // Update the value
1872 copy_tv(&tv, &di->di_tv);
Bram Moolenaar4a011592021-08-05 15:11:08 +02001873 if (dict_add(dict, di) == FAIL)
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001874 {
1875 dictitem_free(di);
1876 clear_tv(&tv);
Bram Moolenaar4a011592021-08-05 15:11:08 +02001877 return luaL_error(L, "Couldn't add to dictionary");
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001878 }
1879 }
1880 else
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001881 {
1882 // Clear the old value
1883 clear_tv(&di->di_tv);
1884 // Update the value
1885 copy_tv(&tv, &di->di_tv);
1886 }
1887
1888 // Clear the temporary variable
1889 clear_tv(&tv);
1890 }
1891
1892 return 0;
1893}
1894
1895 static int
1896luaV_getvar(lua_State *L)
1897{
1898 dict_T *dict = luaV_get_var_scope(L);
1899 size_t len;
1900 const char *name = luaL_checklstring(L, 3, &len);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001901 dictitem_T *di = dict_find(dict, (char_u *)name, (int)len);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001902
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001903 if (di == NULL)
1904 return 0; // nil
1905
1906 luaV_pushtypval(L, &di->di_tv);
1907 return 1;
1908}
1909
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001910 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001911luaV_command(lua_State *L)
1912{
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001913 char_u *s = vim_strsave((char_u *)luaL_checkstring(L, 1));
1914
1915 execute_cmds_from_string(s);
1916 vim_free(s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001917 update_screen(VALID);
1918 return 0;
1919}
1920
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001921 static int
1922luaV_eval(lua_State *L)
1923{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001924 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001925
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001926 if (tv == NULL) luaL_error(L, "invalid expression");
1927 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001928 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001929 return 1;
1930}
1931
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001932 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001933luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001934{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001935 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001936 return 0;
1937}
1938
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001939 static int
1940luaV_line(lua_State *L)
1941{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001942 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1943 return 1;
1944}
1945
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001946 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001947luaV_list(lua_State *L)
1948{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001949 list_T *l;
1950 int initarg = !lua_isnoneornil(L, 1);
1951
1952 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1953 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1954 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001955 if (l == NULL)
1956 lua_pushnil(L);
1957 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001958 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001959 luaV_newlist(L, l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001960 if (initarg) // traverse table to init list
Bram Moolenaar17413672018-07-14 20:49:42 +02001961 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001962 int notnil, i = 0;
1963 typval_T v;
Bram Moolenaar17413672018-07-14 20:49:42 +02001964 do
1965 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001966 lua_rawgeti(L, 1, ++i);
1967 notnil = !lua_isnil(L, -1);
Bram Moolenaar17413672018-07-14 20:49:42 +02001968 if (notnil)
1969 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001970 luaV_checktypval(L, -1, &v, "vim.list");
1971 list_append_tv(l, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001972 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001973 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001974 lua_pop(L, 1); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001975 } while (notnil);
1976 }
1977 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001978 return 1;
1979}
1980
1981 static int
1982luaV_dict(lua_State *L)
1983{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001984 dict_T *d;
1985 int initarg = !lua_isnoneornil(L, 1);
1986
1987 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1988 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1989 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001990 if (d == NULL)
1991 lua_pushnil(L);
1992 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001993 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001994 luaV_newdict(L, d);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001995 if (initarg) // traverse table to init dict
Bram Moolenaarca06da92018-07-01 15:12:05 +02001996 {
1997 lua_pushnil(L);
1998 while (lua_next(L, 1))
1999 {
2000 char_u *key;
2001 dictitem_T *di;
2002 typval_T v;
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02002003
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002004 lua_pushvalue(L, -2); // dup key in case it's a number
Bram Moolenaarca06da92018-07-01 15:12:05 +02002005 key = (char_u *) lua_tostring(L, -1);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02002006 if (key == NULL)
2007 {
2008 lua_pushnil(L);
2009 return 1;
2010 }
2011 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002012 luaL_error(L, "table has empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002013 luaV_checktypval(L, -2, &v, "vim.dict"); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02002014 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02002015 if (di == NULL || dict_add(d, di) == FAIL)
2016 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02002017 vim_free(di);
2018 lua_pushnil(L);
2019 return 1;
2020 }
Bram Moolenaare49b8e82020-07-01 13:52:55 +02002021 di->di_tv = v;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002022 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02002023 }
2024 }
2025 }
2026 return 1;
2027}
2028
2029 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01002030luaV_blob(lua_State *L)
2031{
2032 blob_T *b;
2033 int initarg = !lua_isnoneornil(L, 1);
2034
2035 if (initarg && !lua_isstring(L, 1))
2036 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
2037 b = blob_alloc();
2038 if (b == NULL)
2039 lua_pushnil(L);
2040 else
2041 {
2042 luaV_newblob(L, b);
2043 if (initarg)
2044 {
2045 size_t i, l = 0;
2046 const char *s = lua_tolstring(L, 1, &l);
2047
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01002048 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01002049 for (i = 0; i < l; ++i)
2050 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002051 }
2052 }
2053 return 1;
2054}
2055
2056 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02002057luaV_funcref(lua_State *L)
2058{
2059 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002060 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002061 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
2062 luaL_error(L, "invalid function name: %s", name);
2063 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002064 return 1;
2065}
2066
2067 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002068luaV_buffer(lua_State *L)
2069{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002070 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002071 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002072 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002073 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002074 {
2075 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02002076 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002077 if (buf->b_fnum == n) break;
2078 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002079 else // by name
2080 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002081 size_t l;
2082 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02002083 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002084 {
2085 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
2086 {
2087 if (l == 0) break;
2088 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02002089 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
2090 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002091 break;
2092 }
2093 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002094 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002095 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002096 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002097 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002098 return 1;
2099}
2100
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002101 static int
2102luaV_window(lua_State *L)
2103{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002104 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002105 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002106 {
2107 int n = lua_tointeger(L, 1);
2108 for (win = firstwin; win != NULL; win = win->w_next, n--)
2109 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002110 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002111 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002112 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002113 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002114 return 1;
2115}
2116
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002117 static int
2118luaV_open(lua_State *L)
2119{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002120 char_u *s = NULL;
2121#ifdef HAVE_SANDBOX
2122 luaV_checksandbox(L);
2123#endif
2124 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01002125 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002126 return 1;
2127}
2128
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002129 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002130luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002131{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002132 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002133 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002134 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02002135 lua_settop(L, 1);
2136 if (lua_getmetatable(L, 1))
2137 {
2138 luaV_getfield(L, LUAVIM_LIST);
2139 if (lua_rawequal(L, -1, 2))
2140 {
2141 lua_pushstring(L, "list");
2142 return 1;
2143 }
2144 luaV_getfield(L, LUAVIM_DICT);
2145 if (lua_rawequal(L, -1, 2))
2146 {
2147 lua_pushstring(L, "dict");
2148 return 1;
2149 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01002150 luaV_getfield(L, LUAVIM_BLOB);
2151 if (lua_rawequal(L, -1, 2))
2152 {
2153 lua_pushstring(L, "blob");
2154 return 1;
2155 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002156 luaV_getfield(L, LUAVIM_FUNCREF);
2157 if (lua_rawequal(L, -1, 2))
2158 {
2159 lua_pushstring(L, "funcref");
2160 return 1;
2161 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002162 luaV_getfield(L, LUAVIM_BUFFER);
2163 if (lua_rawequal(L, -1, 2))
2164 {
2165 lua_pushstring(L, "buffer");
2166 return 1;
2167 }
2168 luaV_getfield(L, LUAVIM_WINDOW);
2169 if (lua_rawequal(L, -1, 2))
2170 {
2171 lua_pushstring(L, "window");
2172 return 1;
2173 }
2174 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002175 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002176 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02002177 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002178}
2179
Bram Moolenaareb04f082020-05-17 14:32:35 +02002180 static int
2181luaV_call(lua_State *L)
2182{
2183 int argc = lua_gettop(L) - 1;
2184 size_t funcname_len;
2185 char_u *funcname;
2186 char *error = NULL;
2187 typval_T rettv;
2188 typval_T argv[MAX_FUNC_ARGS + 1];
2189 int i = 0;
2190
2191 if (argc > MAX_FUNC_ARGS)
2192 return luaL_error(L, "Function called with too many arguments");
2193
2194 funcname = (char_u *)luaL_checklstring(L, 1, &funcname_len);
2195
2196 for (; i < argc; i++)
2197 {
2198 if (luaV_totypval(L, i + 2, &argv[i]) == FAIL)
2199 {
2200 error = "lua: cannot convert value";
2201 goto free_vim_args;
2202 }
2203 }
2204
2205 argv[argc].v_type = VAR_UNKNOWN;
2206
2207 if (call_vim_function(funcname, argc, argv, &rettv) == FAIL)
2208 {
2209 error = "lua: call_vim_function failed";
2210 goto free_vim_args;
2211 }
2212
2213 luaV_pushtypval(L, &rettv);
2214 clear_tv(&rettv);
2215
2216free_vim_args:
2217 while (i > 0)
2218 clear_tv(&argv[--i]);
2219
2220 if (error == NULL)
2221 return 1;
2222 else
2223 return luaL_error(L, error);
2224}
2225
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02002226/*
2227 * Return the Vim version as a Lua table
2228 */
2229 static int
2230luaV_version(lua_State *L)
2231{
2232 lua_newtable(L);
2233 lua_pushstring(L, "major");
2234 lua_pushinteger(L, VIM_VERSION_MAJOR);
2235 lua_settable(L, -3);
2236 lua_pushstring(L, "minor");
2237 lua_pushinteger(L, VIM_VERSION_MINOR);
2238 lua_settable(L, -3);
2239 lua_pushstring(L, "patch");
2240 lua_pushinteger(L, highest_patch());
2241 lua_settable(L, -3);
2242 return 1;
2243}
2244
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002245static const luaL_Reg luaV_module[] = {
2246 {"command", luaV_command},
2247 {"eval", luaV_eval},
2248 {"beep", luaV_beep},
2249 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002250 {"list", luaV_list},
2251 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01002252 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02002253 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002254 {"buffer", luaV_buffer},
2255 {"window", luaV_window},
2256 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002257 {"type", luaV_type},
Bram Moolenaareb04f082020-05-17 14:32:35 +02002258 {"call", luaV_call},
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002259 {"_getvar", luaV_getvar},
2260 {"_setvar", luaV_setvar},
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02002261 {"version", luaV_version},
Bram Moolenaar125ed272021-04-07 20:11:12 +02002262 {"lua_version", NULL},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002263 {NULL, NULL}
2264};
2265
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002266/*
2267 * for freeing list, dict, buffer and window objects; lightuserdata as arg
2268 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02002269 static int
2270luaV_free(lua_State *L)
2271{
2272 lua_pushnil(L);
2273 luaV_setudata(L, lua_touserdata(L, 1));
2274 return 0;
2275}
2276
2277 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002278luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002279{
2280 luaL_Buffer b;
2281 size_t l;
2282 const char *str = lua_tolstring(L, 1, &l);
2283 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
2284 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
2285 luaL_buffinit(L, &b);
2286 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
2287 luaL_addlstring(&b, str, l);
2288 luaL_pushresult(&b);
2289 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002290 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002291 {
2292 luaV_emsg(L);
2293 return 0;
2294 }
2295 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002296 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002297 {
2298 luaV_emsg(L);
2299 return 0;
2300 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002301 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002302 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01002303 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002304}
2305
2306 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002307luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002308{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002309 int copyID = lua_tointeger(L, 1);
2310 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002311
Bram Moolenaar1dced572012-04-05 16:54:08 +02002312 luaV_getfield(L, LUAVIM_LIST);
2313 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002314 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002315 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002316 // traverse cache table
Bram Moolenaarb84634d2015-02-04 22:02:37 +01002317 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002318 {
2319 lua_getmetatable(L, -1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002320 if (lua_rawequal(L, -1, 2)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002321 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002322 list_T *l = (list_T *)lua_touserdata(L, 5); // key
2323
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002324 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002325 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002326 else if (lua_rawequal(L, -1, 3)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002327 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002328 dict_T *d = (dict_T *)lua_touserdata(L, 5); // key
2329
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002330 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002331 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002332 else if (lua_rawequal(L, -1, 4)) // funcref?
2333 {
2334 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key
2335
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002336 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002337 }
2338 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02002339 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002340 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01002341 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002342}
2343
Bram Moolenaar125ed272021-04-07 20:11:12 +02002344 static int
2345luaV_pushversion(lua_State *L)
2346{
2347 int major = 0;
2348 int minor = 0;
2349 int patch = 0;
2350 char s[16];
2351
2352 sscanf(LUAVIM_VERSION, "Lua %d.%d.%d", &major, &minor, &patch);
2353 vim_snprintf(s, sizeof(s), "%d.%d.%d", major, minor, patch);
2354 lua_pushstring(L, s);
2355 return 0;
2356}
2357
Bram Moolenaareb04f082020-05-17 14:32:35 +02002358#define LUA_VIM_FN_CODE \
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002359 "vim.fn = setmetatable({}, {\n"\
2360 " __index = function (t, key)\n"\
2361 " local function _fn(...)\n"\
2362 " return vim.call(key, ...)\n"\
2363 " end\n"\
2364 " t[key] = _fn\n"\
2365 " return _fn\n"\
2366 " end\n"\
2367 " })"
2368
2369#define LUA_VIM_UPDATE_PACKAGE_PATHS \
2370 "local last_vim_paths = {}\n"\
2371 "vim._update_package_paths = function ()\n"\
2372 " local cur_vim_paths = {}\n"\
2373 " local function split(s, delimiter)\n"\
2374 " result = {}\n"\
2375 " for match in (s..delimiter):gmatch(\"(.-)\"..delimiter) do\n"\
2376 " table.insert(result, match)\n"\
2377 " end\n"\
2378 " return result\n"\
2379 " end\n"\
2380 " local rtps = split(vim.eval('&runtimepath'), ',')\n"\
2381 " local sep = package.config:sub(1, 1)\n"\
2382 " for _, key in ipairs({'path', 'cpath'}) do\n"\
2383 " local orig_str = package[key] .. ';'\n"\
2384 " local pathtrails_ordered = {}\n"\
2385 " -- Note: ignores trailing item without trailing `;`. Not using something\n"\
2386 " -- simpler in order to preserve empty items (stand for default path).\n"\
2387 " local orig = {}\n"\
2388 " for s in orig_str:gmatch('[^;]*;') do\n"\
2389 " s = s:sub(1, -2) -- Strip trailing semicolon\n"\
2390 " orig[#orig + 1] = s\n"\
2391 " end\n"\
2392 " if key == 'path' then\n"\
2393 " -- /?.lua and /?/init.lua\n"\
2394 " pathtrails_ordered = {sep .. '?.lua', sep .. '?' .. sep .. 'init.lua'}\n"\
2395 " else\n"\
2396 " local pathtrails = {}\n"\
2397 " for _, s in ipairs(orig) do\n"\
2398 " -- Find out path patterns. pathtrail should contain something like\n"\
2399 " -- /?.so, \?.dll. This allows not to bother determining what correct\n"\
2400 " -- suffixes are.\n"\
2401 " local pathtrail = s:match('[/\\\\][^/\\\\]*%?.*$')\n"\
2402 " if pathtrail and not pathtrails[pathtrail] then\n"\
2403 " pathtrails[pathtrail] = true\n"\
2404 " pathtrails_ordered[#pathtrails_ordered + 1] = pathtrail\n"\
2405 " end\n"\
2406 " end\n"\
2407 " end\n"\
2408 " local new = {}\n"\
2409 " for _, rtp in ipairs(rtps) do\n"\
2410 " if not rtp:match(';') then\n"\
2411 " for _, pathtrail in pairs(pathtrails_ordered) do\n"\
2412 " local new_path = rtp .. sep .. 'lua' .. pathtrail\n"\
2413 " -- Always keep paths from &runtimepath at the start:\n"\
2414 " -- append them here disregarding orig possibly containing one of them.\n"\
2415 " new[#new + 1] = new_path\n"\
2416 " cur_vim_paths[new_path] = true\n"\
2417 " end\n"\
2418 " end\n"\
2419 " end\n"\
2420 " for _, orig_path in ipairs(orig) do\n"\
2421 " -- Handle removing obsolete paths originating from &runtimepath: such\n"\
2422 " -- paths either belong to cur_nvim_paths and were already added above or\n"\
2423 " -- to last_nvim_paths and should not be added at all if corresponding\n"\
2424 " -- entry was removed from &runtimepath list.\n"\
2425 " if not (cur_vim_paths[orig_path] or last_vim_paths[orig_path]) then\n"\
2426 " new[#new + 1] = orig_path\n"\
2427 " end\n"\
2428 " end\n"\
2429 " package[key] = table.concat(new, ';')\n"\
2430 " end\n"\
2431 " last_vim_paths = cur_vim_paths\n"\
2432 "end"
Bram Moolenaareb04f082020-05-17 14:32:35 +02002433
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002434#define LUA_VIM_SETUP_VARIABLE_DICTS \
2435 "do\n"\
2436 " local function make_dict_accessor(scope)\n"\
2437 " local mt = {}\n"\
2438 " function mt:__newindex(k, v)\n"\
2439 " return vim._setvar(scope, 0, k, v)\n"\
2440 " end\n"\
2441 " function mt:__index(k)\n"\
2442 " return vim._getvar(scope, 0, k)\n"\
2443 " end\n"\
2444 " return setmetatable({}, mt)\n"\
2445 " end\n"\
2446 " vim.g = make_dict_accessor('g')\n"\
2447 " vim.v = make_dict_accessor('v')\n"\
2448 " vim.b = make_dict_accessor('b')\n"\
2449 " vim.w = make_dict_accessor('w')\n"\
2450 " vim.t = make_dict_accessor('t')\n"\
2451 "end"
2452
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002453 static int
2454luaopen_vim(lua_State *L)
2455{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002456 // set cache table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002457 lua_newtable(L);
2458 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002459 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002460 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002461 lua_setmetatable(L, -2); // cache is weak-valued
2462 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002463 lua_pushcfunction(L, luaV_print);
2464 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002465 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002466 lua_getglobal(L, "debug");
2467 lua_pushcfunction(L, luaV_debug);
2468 lua_setfield(L, -2, "debug");
2469 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002470 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002471 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002472 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002473 lua_pushcclosure(L, luaV_free, 1);
2474 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002475 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002476 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002477 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002478 lua_pushcclosure(L, luaV_luaeval, 1);
2479 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002480 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002481 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002482 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002483 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002484 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002485 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002486 luaV_newmetatable(L, LUAVIM_LIST);
2487 lua_pushvalue(L, 1);
2488 luaV_openlib(L, luaV_List_mt, 1);
2489 luaV_newmetatable(L, LUAVIM_DICT);
2490 lua_pushvalue(L, 1);
2491 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002492 luaV_newmetatable(L, LUAVIM_BLOB);
2493 lua_pushvalue(L, 1);
2494 luaV_openlib(L, luaV_Blob_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002495 luaV_newmetatable(L, LUAVIM_FUNCREF);
2496 lua_pushvalue(L, 1);
2497 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002498 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002499 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002500 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002501 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002502 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002503 luaV_openlib(L, luaV_Window_mt, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002504 lua_newtable(L); // vim table
2505 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002506 luaV_openlib(L, luaV_module, 1);
Bram Moolenaar125ed272021-04-07 20:11:12 +02002507 luaV_pushversion(L);
2508 lua_setfield(L, -2, "lua_version");
Bram Moolenaar1dced572012-04-05 16:54:08 +02002509 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaareb04f082020-05-17 14:32:35 +02002510 // custom code
Bram Moolenaar9309eb22020-05-17 16:53:56 +02002511 (void)luaL_dostring(L, LUA_VIM_FN_CODE);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002512 (void)luaL_dostring(L, LUA_VIM_UPDATE_PACKAGE_PATHS);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002513 (void)luaL_dostring(L, LUA_VIM_SETUP_VARIABLE_DICTS);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002514
2515 lua_getglobal(L, "vim");
2516 lua_getfield(L, -1, "_update_package_paths");
2517
2518 if (lua_pcall(L, 0, 0, 0))
2519 luaV_emsg(L);
2520
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002521 return 0;
2522}
2523
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002524 static lua_State *
2525luaV_newstate(void)
2526{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002527 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002528 luaL_openlibs(L); // core libs
2529 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002530 lua_call(L, 0, 0);
2531 return L;
2532}
2533
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002534 static void
2535luaV_setrange(lua_State *L, int line1, int line2)
2536{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002537 lua_getglobal(L, LUAVIM_NAME);
2538 lua_pushinteger(L, line1);
2539 lua_setfield(L, -2, "firstline");
2540 lua_pushinteger(L, line2);
2541 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002542 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002543}
2544
2545
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002546// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002547
2548static lua_State *L = NULL;
2549
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002550 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002551lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002552{
2553 return L != NULL;
2554}
2555
2556 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002557lua_init(void)
2558{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002559 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002560 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002561#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002562 if (!lua_enabled(TRUE))
2563 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002564 emsg(_("Lua library cannot be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002565 return FAIL;
2566 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002567#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002568 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002569 }
2570 return OK;
2571}
2572
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002573 void
2574lua_end(void)
2575{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002576 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002577 {
2578 lua_close(L);
2579 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002580 }
2581}
2582
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002583/*
2584 * ex commands
2585 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002586 void
2587ex_lua(exarg_T *eap)
2588{
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002589 char *script = (char *)script_get(eap, eap->arg);
2590
2591 if (!eap->skip && lua_init() == OK)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002592 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002593 char *s = script != NULL ? script : (char *)eap->arg;
2594
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002595 luaV_setrange(L, eap->line1, eap->line2);
2596 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2597 || lua_pcall(L, 0, 0, 0))
2598 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002599 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002600 if (script != NULL)
2601 vim_free(script);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002602}
2603
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002604 void
2605ex_luado(exarg_T *eap)
2606{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002607 linenr_T l;
2608 const char *s = (const char *) eap->arg;
2609 luaL_Buffer b;
2610 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002611 buf_T *was_curbuf = curbuf;
2612
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002613 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002614 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2615 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002616 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002617 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002618 }
2619 luaV_setrange(L, eap->line1, eap->line2);
2620 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002621 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002622 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002623 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002624 luaL_pushresult(&b);
2625 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002626 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2627 {
2628 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002629 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002630 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002631 }
2632 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002633 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002634 for (l = eap->line1; l <= eap->line2; l++)
2635 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02002636 // Check the line number, the command may have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002637 if (l > curbuf->b_ml.ml_line_count)
2638 break;
2639
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002640 lua_pushvalue(L, -1); // function
2641 luaV_pushline(L, curbuf, l); // current line as arg
2642 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002643 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002644 {
2645 luaV_emsg(L);
2646 break;
2647 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002648 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002649 if (curbuf != was_curbuf)
2650 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002651 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002652 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002653#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002654 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002655#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002656 ml_replace(l, luaV_toline(L, -1), TRUE);
2657 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002658 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002659 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002660 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002661 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002662 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002663 check_cursor();
2664 update_screen(NOT_VALID);
2665}
2666
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002667 void
2668ex_luafile(exarg_T *eap)
2669{
2670 if (lua_init() == FAIL)
2671 return;
2672 if (!eap->skip)
2673 {
2674 luaV_setrange(L, eap->line1, eap->line2);
2675 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2676 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002677 }
2678}
2679
Bram Moolenaar1dced572012-04-05 16:54:08 +02002680#define luaV_freetype(typ,tname) \
2681 void \
2682 lua_##tname##_free(typ *o) \
2683 { \
2684 if (!lua_isopen()) return; \
2685 luaV_getfield(L, LUAVIM_FREE); \
2686 lua_pushlightuserdata(L, (void *) o); \
2687 lua_call(L, 1, 0); \
2688 }
2689
2690luaV_freetype(buf_T, buffer)
2691luaV_freetype(win_T, window)
2692
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002693 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002694do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002695{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002696 lua_init();
2697 luaV_getfield(L, LUAVIM_LUAEVAL);
2698 lua_pushstring(L, (char *) str);
2699 lua_pushlightuserdata(L, (void *) arg);
2700 lua_pushlightuserdata(L, (void *) rettv);
2701 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002702}
2703
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002704 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002705set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002706{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002707 int aborted = 0;
2708
2709 if (lua_isopen())
2710 {
2711 luaV_getfield(L, LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002712 // call the function with 1 arg, getting 1 result back
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002713 lua_pushinteger(L, copyID);
2714 lua_call(L, 1, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002715 // get the result
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002716 aborted = lua_tointeger(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002717 // pop result off the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002718 lua_pop(L, 1);
2719 }
2720 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002721}
2722
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002723 void
2724update_package_paths_in_lua()
2725{
2726 if (lua_isopen())
2727 {
2728 lua_getglobal(L, "vim");
2729 lua_getfield(L, -1, "_update_package_paths");
2730
2731 if (lua_pcall(L, 0, 0, 0))
2732 luaV_emsg(L);
2733 }
2734}
2735
Bram Moolenaar801ab062020-06-25 19:27:56 +02002736/*
2737 * Native C function callback
2738 */
2739 static int
2740luaV_call_lua_func(
2741 int argcount,
2742 typval_T *argvars,
2743 typval_T *rettv,
2744 void *state)
2745{
2746 int i;
2747 int luaargcount = argcount;
2748 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2749 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2750
2751 if (funcstate->lua_tableref != LUA_NOREF)
2752 {
2753 // First arg for metatable __call method is a table
2754 luaargcount += 1;
2755 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2756 }
2757
2758 for (i = 0; i < argcount; ++i)
2759 luaV_pushtypval(funcstate->L, &argvars[i]);
2760
2761 if (lua_pcall(funcstate->L, luaargcount, 1, 0))
2762 {
2763 luaV_emsg(funcstate->L);
2764 return FCERR_OTHER;
2765 }
2766
2767 luaV_checktypval(funcstate->L, -1, rettv, "get return value");
2768 return FCERR_NONE;
2769}
2770
2771/*
2772 * Free up any lua references held by the func state.
2773 */
2774 static void
2775luaV_call_lua_func_free(void *state)
2776{
2777 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2778 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2779 funcstate->L = NULL;
2780 if (funcstate->lua_tableref != LUA_NOREF)
2781 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2782 VIM_CLEAR(funcstate);
2783}
2784
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002785#endif