blob: 1eda2bf264c0dd448a3d581cb3657d16c0dab44c [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
Jesse Pavel8a350332023-08-13 22:05:45 -040019#if __STDC_VERSION__ >= 199901L
20# define LUAV_INLINE inline
21#else
22# define LUAV_INLINE
23#endif
24
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010025// Only do the following when the feature is enabled. Needed for "make
26// depend".
Bram Moolenaar0ba04292010-07-14 23:23:17 +020027#if defined(FEAT_LUA) || defined(PROTO)
28
29#define LUAVIM_CHUNKNAME "vim chunk"
30#define LUAVIM_NAME "vim"
Bram Moolenaar1dced572012-04-05 16:54:08 +020031#define LUAVIM_EVALNAME "luaeval"
32#define LUAVIM_EVALHEADER "local _A=select(1,...) return "
Bram Moolenaar0ba04292010-07-14 23:23:17 +020033
Bram Moolenaar125ed272021-04-07 20:11:12 +020034#ifdef LUA_RELEASE
35# define LUAVIM_VERSION LUA_RELEASE
36#else
37# define LUAVIM_VERSION LUA_VERSION
38#endif
39
Bram Moolenaar0ba04292010-07-14 23:23:17 +020040typedef buf_T *luaV_Buffer;
41typedef win_T *luaV_Window;
Bram Moolenaar1dced572012-04-05 16:54:08 +020042typedef dict_T *luaV_Dict;
43typedef list_T *luaV_List;
Bram Moolenaarb7828692019-03-23 13:57:02 +010044typedef blob_T *luaV_Blob;
Bram Moolenaarca06da92018-07-01 15:12:05 +020045typedef struct {
Bram Moolenaar4eefe472019-03-19 21:59:19 +010046 char_u *name; // funcref
Bram Moolenaarca06da92018-07-01 15:12:05 +020047 dict_T *self; // selfdict
48} luaV_Funcref;
Bram Moolenaarc8970b92020-10-26 20:18:08 +010049typedef int (*msgfunc_T)(char *);
Bram Moolenaar0ba04292010-07-14 23:23:17 +020050
Bram Moolenaar801ab062020-06-25 19:27:56 +020051typedef struct {
52 int lua_funcref; // ref to a lua func
53 int lua_tableref; // ref to a lua table if metatable else LUA_NOREF. used
54 // for __call
55 lua_State *L;
56} luaV_CFuncState;
57
Bram Moolenaar1dced572012-04-05 16:54:08 +020058static const char LUAVIM_DICT[] = "dict";
59static const char LUAVIM_LIST[] = "list";
Bram Moolenaarb7828692019-03-23 13:57:02 +010060static const char LUAVIM_BLOB[] = "blob";
Bram Moolenaarca06da92018-07-01 15:12:05 +020061static const char LUAVIM_FUNCREF[] = "funcref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020062static const char LUAVIM_BUFFER[] = "buffer";
63static const char LUAVIM_WINDOW[] = "window";
64static const char LUAVIM_FREE[] = "luaV_free";
Bram Moolenaar1dced572012-04-05 16:54:08 +020065static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
66static const char LUAVIM_SETREF[] = "luaV_setref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020067
Bram Moolenaar801ab062020-06-25 19:27:56 +020068static const char LUA___CALL[] = "__call";
69
Jesse Pavel8a350332023-08-13 22:05:45 -040070// get/setudata manage references to vim userdata in a cache table through
71// object pointers (light userdata). The cache table itself is retrieved
72// from the registry.
73
74static const char LUAVIM_UDATA_CACHE[] = "luaV_udata_cache";
75
76 static void LUAV_INLINE
77luaV_getudata(lua_State *L, void *v)
78{
79 lua_pushlightuserdata(L, (void *) LUAVIM_UDATA_CACHE);
80 lua_rawget(L, LUA_REGISTRYINDEX); // now the cache table is at the top of the stack
81 lua_pushlightuserdata(L, v);
82 lua_rawget(L, -2);
83 lua_remove(L, -2); // remove the cache table from the stack
84}
85
86 static void LUAV_INLINE
87luaV_setudata(lua_State *L, void *v)
88{
89 lua_pushlightuserdata(L, (void *) LUAVIM_UDATA_CACHE);
90 lua_rawget(L, LUA_REGISTRYINDEX); // cache table is at -1
91 lua_pushlightuserdata(L, v); // ...now at -2
92 lua_pushvalue(L, -3); // copy the userdata (cache at -3)
93 lua_rawset(L, -3); // consumes two stack items
94 lua_pop(L, 1); // and remove the cache table
95}
96
Bram Moolenaar0ba04292010-07-14 23:23:17 +020097#define luaV_getfield(L, s) \
98 lua_pushlightuserdata((L), (void *)(s)); \
99 lua_rawget((L), LUA_REGISTRYINDEX)
100#define luaV_checksandbox(L) \
101 if (sandbox) luaL_error((L), "not allowed in sandbox")
102#define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
103#define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200104#define luaV_checktypval(L, a, v, msg) \
105 do { \
Bram Moolenaar801ab062020-06-25 19:27:56 +0200106 if (luaV_totypval(L, a, v) == FAIL) \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200107 luaL_error(L, msg ": cannot convert value"); \
108 } while (0)
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200109
Bram Moolenaarca06da92018-07-01 15:12:05 +0200110static luaV_List *luaV_pushlist(lua_State *L, list_T *lis);
111static luaV_Dict *luaV_pushdict(lua_State *L, dict_T *dic);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100112static luaV_Blob *luaV_pushblob(lua_State *L, blob_T *blo);
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100113static luaV_Funcref *luaV_pushfuncref(lua_State *L, char_u *name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200114static int luaV_call_lua_func(int argcount, typval_T *argvars, typval_T *rettv, void *state);
115static void luaV_call_lua_func_free(void *state);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200116
117#if LUA_VERSION_NUM <= 501
Jesse Pavel8a350332023-08-13 22:05:45 -0400118#define luaV_register(L, l) luaL_register(L, NULL, l)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200119#define luaL_typeerror luaL_typerror
120#else
Jesse Pavel8a350332023-08-13 22:05:45 -0400121#define luaV_register(L, l) luaL_setfuncs(L, l, 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200122#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200123
124#ifdef DYNAMIC_LUA
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200125
Bram Moolenaar4f974752019-02-17 17:44:42 +0100126#ifndef MSWIN
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200127# include <dlfcn.h>
128# define HANDLE void*
129# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
130# define symbol_from_dll dlsym
131# define close_dll dlclose
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200132# define load_dll_error dlerror
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200133#else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200134# define load_dll vimLoadLib
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200135# define symbol_from_dll GetProcAddress
136# define close_dll FreeLibrary
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200137# define load_dll_error GetWin32Error
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200138#endif
139
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100140// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200141#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200142#define luaL_register dll_luaL_register
Bram Moolenaar1dced572012-04-05 16:54:08 +0200143#define luaL_prepbuffer dll_luaL_prepbuffer
144#define luaL_openlib dll_luaL_openlib
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200145#define luaL_typerror dll_luaL_typerror
Bram Moolenaar1dced572012-04-05 16:54:08 +0200146#define luaL_loadfile dll_luaL_loadfile
147#define luaL_loadbuffer dll_luaL_loadbuffer
148#else
149#define luaL_prepbuffsize dll_luaL_prepbuffsize
150#define luaL_setfuncs dll_luaL_setfuncs
151#define luaL_loadfilex dll_luaL_loadfilex
152#define luaL_loadbufferx dll_luaL_loadbufferx
153#define luaL_argerror dll_luaL_argerror
154#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200155#if LUA_VERSION_NUM >= 504
156#define luaL_typeerror dll_luaL_typeerror
157#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200158#define luaL_checkany dll_luaL_checkany
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200159#define luaL_checklstring dll_luaL_checklstring
160#define luaL_checkinteger dll_luaL_checkinteger
161#define luaL_optinteger dll_luaL_optinteger
162#define luaL_checktype dll_luaL_checktype
163#define luaL_error dll_luaL_error
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200164#define luaL_newstate dll_luaL_newstate
165#define luaL_buffinit dll_luaL_buffinit
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200166#define luaL_addlstring dll_luaL_addlstring
167#define luaL_pushresult dll_luaL_pushresult
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200168#define luaL_loadstring dll_luaL_loadstring
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200169#define luaL_ref dll_luaL_ref
170#define luaL_unref dll_luaL_unref
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100171// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200172#if LUA_VERSION_NUM <= 501
173#define lua_tonumber dll_lua_tonumber
174#define lua_tointeger dll_lua_tointeger
175#define lua_call dll_lua_call
176#define lua_pcall dll_lua_pcall
177#else
178#define lua_tonumberx dll_lua_tonumberx
179#define lua_tointegerx dll_lua_tointegerx
180#define lua_callk dll_lua_callk
181#define lua_pcallk dll_lua_pcallk
182#define lua_getglobal dll_lua_getglobal
183#define lua_setglobal dll_lua_setglobal
Bram Moolenaar1dced572012-04-05 16:54:08 +0200184#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200185#if LUA_VERSION_NUM <= 502
186#define lua_replace dll_lua_replace
187#define lua_remove dll_lua_remove
188#endif
189#if LUA_VERSION_NUM >= 503
190#define lua_rotate dll_lua_rotate
191#define lua_copy dll_lua_copy
192#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200193#define lua_typename dll_lua_typename
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200194#define lua_close dll_lua_close
195#define lua_gettop dll_lua_gettop
196#define lua_settop dll_lua_settop
197#define lua_pushvalue dll_lua_pushvalue
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200198#define lua_isnumber dll_lua_isnumber
199#define lua_isstring dll_lua_isstring
200#define lua_type dll_lua_type
201#define lua_rawequal dll_lua_rawequal
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200202#define lua_toboolean dll_lua_toboolean
203#define lua_tolstring dll_lua_tolstring
204#define lua_touserdata dll_lua_touserdata
205#define lua_pushnil dll_lua_pushnil
206#define lua_pushnumber dll_lua_pushnumber
207#define lua_pushinteger dll_lua_pushinteger
208#define lua_pushlstring dll_lua_pushlstring
209#define lua_pushstring dll_lua_pushstring
210#define lua_pushfstring dll_lua_pushfstring
211#define lua_pushcclosure dll_lua_pushcclosure
212#define lua_pushboolean dll_lua_pushboolean
213#define lua_pushlightuserdata dll_lua_pushlightuserdata
214#define lua_getfield dll_lua_getfield
215#define lua_rawget dll_lua_rawget
Bram Moolenaar1dced572012-04-05 16:54:08 +0200216#define lua_rawgeti dll_lua_rawgeti
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200217#define lua_createtable dll_lua_createtable
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +0200218#define lua_settable dll_lua_settable
Bram Moolenaar830e3582018-08-21 14:23:35 +0200219#if LUA_VERSION_NUM >= 504
220 #define lua_newuserdatauv dll_lua_newuserdatauv
221#else
222 #define lua_newuserdata dll_lua_newuserdata
223#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200224#define lua_getmetatable dll_lua_getmetatable
225#define lua_setfield dll_lua_setfield
226#define lua_rawset dll_lua_rawset
227#define lua_rawseti dll_lua_rawseti
228#define lua_setmetatable dll_lua_setmetatable
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200229#define lua_next dll_lua_next
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100230// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200231#define luaopen_base dll_luaopen_base
232#define luaopen_table dll_luaopen_table
233#define luaopen_string dll_luaopen_string
234#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200235#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200236#define luaopen_os dll_luaopen_os
237#define luaopen_package dll_luaopen_package
238#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200239#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200240
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100241// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200242#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200243void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200244char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
245void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200246int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200247int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
248int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
249#else
250char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
251void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
252int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
253int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
254int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
255#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200256#if LUA_VERSION_NUM >= 504
257int (*dll_luaL_typeerror) (lua_State *L, int narg, const char *tname);
258#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200259void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200260const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
261lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
262lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
263void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
264int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200265lua_State *(*dll_luaL_newstate) (void);
266void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200267void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
268void (*dll_luaL_pushresult) (luaL_Buffer *B);
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200269int (*dll_luaL_loadstring) (lua_State *L, const char *s);
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200270int (*dll_luaL_ref) (lua_State *L, int idx);
271#if LUA_VERSION_NUM <= 502
272void (*dll_luaL_unref) (lua_State *L, int idx, int n);
273#else
274void (*dll_luaL_unref) (lua_State *L, int idx, lua_Integer n);
275#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100276// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200277#if LUA_VERSION_NUM <= 501
278lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
279lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
280void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
281int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
282#else
283lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
284lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
285void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
Bram Moolenaardb913952012-06-29 12:54:53 +0200286 lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200287int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
Bram Moolenaardb913952012-06-29 12:54:53 +0200288 int ctx, lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200289void (*dll_lua_getglobal) (lua_State *L, const char *var);
290void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200291#endif
292#if LUA_VERSION_NUM <= 502
293void (*dll_lua_replace) (lua_State *L, int idx);
294void (*dll_lua_remove) (lua_State *L, int idx);
295#endif
296#if LUA_VERSION_NUM >= 503
297void (*dll_lua_rotate) (lua_State *L, int idx, int n);
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200298void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200299#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200300const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200301void (*dll_lua_close) (lua_State *L);
302int (*dll_lua_gettop) (lua_State *L);
303void (*dll_lua_settop) (lua_State *L, int idx);
304void (*dll_lua_pushvalue) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200305int (*dll_lua_isnumber) (lua_State *L, int idx);
306int (*dll_lua_isstring) (lua_State *L, int idx);
307int (*dll_lua_type) (lua_State *L, int idx);
308int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200309int (*dll_lua_toboolean) (lua_State *L, int idx);
310const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
311void *(*dll_lua_touserdata) (lua_State *L, int idx);
312void (*dll_lua_pushnil) (lua_State *L);
313void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
314void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
315void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
316void (*dll_lua_pushstring) (lua_State *L, const char *s);
317const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
318void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
319void (*dll_lua_pushboolean) (lua_State *L, int b);
320void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
321void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
Bram Moolenaar17413672018-07-14 20:49:42 +0200322#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200323void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200324void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200325#else
326int (*dll_lua_rawget) (lua_State *L, int idx);
327int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
328#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200329void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +0200330void (*dll_lua_settable) (lua_State *L, int idx);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200331#if LUA_VERSION_NUM >= 504
332void *(*dll_lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
333#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200334void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200335#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200336int (*dll_lua_getmetatable) (lua_State *L, int objindex);
337void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
338void (*dll_lua_rawset) (lua_State *L, int idx);
Bram Moolenaar17413672018-07-14 20:49:42 +0200339#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200340void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200341#else
342void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n);
343#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200344int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200345int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100346// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200347int (*dll_luaopen_base) (lua_State *L);
348int (*dll_luaopen_table) (lua_State *L);
349int (*dll_luaopen_string) (lua_State *L);
350int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200351int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200352int (*dll_luaopen_os) (lua_State *L);
353int (*dll_luaopen_package) (lua_State *L);
354int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200355void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200356
357typedef void **luaV_function;
358typedef struct {
359 const char *name;
360 luaV_function func;
361} luaV_Reg;
362
363static const luaV_Reg luaV_dll[] = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100364 // lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200365#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200366 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200367 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
368 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200369 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200370 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
371 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
372#else
373 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
374 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
375 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
376 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
377 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
378#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200379#if LUA_VERSION_NUM >= 504
380 {"luaL_typeerror", (luaV_function) &dll_luaL_typeerror},
381#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200382 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200383 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
384 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
385 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
386 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
387 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200388 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
389 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200390 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
391 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200392 {"luaL_loadstring", (luaV_function) &dll_luaL_loadstring},
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200393 {"luaL_ref", (luaV_function) &dll_luaL_ref},
394 {"luaL_unref", (luaV_function) &dll_luaL_unref},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100395 // lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200396#if LUA_VERSION_NUM <= 501
397 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
398 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
399 {"lua_call", (luaV_function) &dll_lua_call},
400 {"lua_pcall", (luaV_function) &dll_lua_pcall},
401#else
402 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
403 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
404 {"lua_callk", (luaV_function) &dll_lua_callk},
405 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
406 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
407 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200408#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200409#if LUA_VERSION_NUM <= 502
410 {"lua_replace", (luaV_function) &dll_lua_replace},
411 {"lua_remove", (luaV_function) &dll_lua_remove},
412#endif
413#if LUA_VERSION_NUM >= 503
414 {"lua_rotate", (luaV_function) &dll_lua_rotate},
415 {"lua_copy", (luaV_function) &dll_lua_copy},
416#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200417 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200418 {"lua_close", (luaV_function) &dll_lua_close},
419 {"lua_gettop", (luaV_function) &dll_lua_gettop},
420 {"lua_settop", (luaV_function) &dll_lua_settop},
421 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200422 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
423 {"lua_isstring", (luaV_function) &dll_lua_isstring},
424 {"lua_type", (luaV_function) &dll_lua_type},
425 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200426 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
427 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
428 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
429 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
430 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
431 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
432 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
433 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
434 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
435 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
436 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
437 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
438 {"lua_getfield", (luaV_function) &dll_lua_getfield},
439 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200440 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200441 {"lua_createtable", (luaV_function) &dll_lua_createtable},
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +0200442 {"lua_settable", (luaV_function) &dll_lua_settable},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200443#if LUA_VERSION_NUM >= 504
444 {"lua_newuserdatauv", (luaV_function) &dll_lua_newuserdatauv},
445#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200446 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200447#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200448 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
449 {"lua_setfield", (luaV_function) &dll_lua_setfield},
450 {"lua_rawset", (luaV_function) &dll_lua_rawset},
451 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
452 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200453 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100454 // libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200455 {"luaopen_base", (luaV_function) &dll_luaopen_base},
456 {"luaopen_table", (luaV_function) &dll_luaopen_table},
457 {"luaopen_string", (luaV_function) &dll_luaopen_string},
458 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200459 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200460 {"luaopen_os", (luaV_function) &dll_luaopen_os},
461 {"luaopen_package", (luaV_function) &dll_luaopen_package},
462 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200463 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200464 {NULL, NULL}
465};
466
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200467static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200468
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200469 static int
470lua_link_init(char *libname, int verbose)
471{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200472 const luaV_Reg *reg;
473 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200474 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200475 if (!hinstLua)
476 {
477 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000478 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200479 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200480 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200481 for (reg = luaV_dll; reg->func; reg++)
482 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200483 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
484 {
485 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200486 hinstLua = 0;
487 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000488 semsg(_(e_could_not_load_library_function_str), reg->name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200489 return FAIL;
490 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200491 }
492 return OK;
493}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100494#endif // DYNAMIC_LUA
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200495
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200496#if defined(DYNAMIC_LUA) || defined(PROTO)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200497 int
498lua_enabled(int verbose)
499{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100500 return lua_link_init((char *)p_luadll, verbose) == OK;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200501}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200502#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200503
Bram Moolenaar5551b132020-07-14 21:54:28 +0200504#if LUA_VERSION_NUM > 501 && LUA_VERSION_NUM < 504
Bram Moolenaar1dced572012-04-05 16:54:08 +0200505 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100506luaL_typeerror(lua_State *L, int narg, const char *tname)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200507{
508 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200509 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200510 return luaL_argerror(L, narg, msg);
511}
512#endif
513
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200514
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100515// ======= Internal =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200516
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200517 static void
518luaV_newmetatable(lua_State *L, const char *tname)
519{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200520 lua_newtable(L);
521 lua_pushlightuserdata(L, (void *) tname);
522 lua_pushvalue(L, -2);
523 lua_rawset(L, LUA_REGISTRYINDEX);
524}
525
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200526 static void *
527luaV_toudata(lua_State *L, int ud, const char *tname)
528{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200529 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200530
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000531 if (p == NULL)
532 return NULL;
533
534 // value is userdata
535 if (lua_getmetatable(L, ud)) // does it have a metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200536 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000537 luaV_getfield(L, tname); // get metatable
538 if (lua_rawequal(L, -1, -2)) // MTs match?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200539 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000540 lua_pop(L, 2); // MTs
541 return p;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200542 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200543 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000544
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200545 return NULL;
546}
547
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200548 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200549luaV_checkcache(lua_State *L, void *p)
550{
551 luaV_getudata(L, p);
552 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
553 lua_pop(L, 1);
554 return p;
555}
556
557#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
558
559#define luaV_checkvalid(L,luatyp,ud) \
560 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
561
562 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200563luaV_checkudata(lua_State *L, int ud, const char *tname)
564{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200565 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200566 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200567 return p;
568}
569
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200570 static void
571luaV_pushtypval(lua_State *L, typval_T *tv)
572{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200573 if (tv == NULL)
574 {
575 lua_pushnil(L);
576 return;
577 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200578 switch (tv->v_type)
579 {
580 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200581 lua_pushstring(L, tv->vval.v_string == NULL
582 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200583 break;
584 case VAR_NUMBER:
585 lua_pushinteger(L, (int) tv->vval.v_number);
586 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200587 case VAR_FLOAT:
588 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
589 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200590 case VAR_LIST:
591 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200592 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200593 case VAR_DICT:
594 luaV_pushdict(L, tv->vval.v_dict);
595 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100596 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100597 case VAR_SPECIAL:
598 if (tv->vval.v_number <= VVAL_TRUE)
599 lua_pushinteger(L, (int) tv->vval.v_number);
600 else
601 lua_pushnil(L);
602 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200603 case VAR_FUNC:
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100604 luaV_pushfuncref(L, tv->vval.v_string);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200605 break;
Bram Moolenaar86c3a212021-03-08 19:50:24 +0100606 case VAR_PARTIAL:
607 // TODO: handle partial arguments
608 luaV_pushfuncref(L, partial_name(tv->vval.v_partial));
609 break;
610
Bram Moolenaarb7828692019-03-23 13:57:02 +0100611 case VAR_BLOB:
612 luaV_pushblob(L, tv->vval.v_blob);
613 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200614 default:
615 lua_pushnil(L);
616 }
617}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200618
Bram Moolenaarca06da92018-07-01 15:12:05 +0200619/*
620 * Converts lua value at 'pos' to typval 'tv'.
621 * Returns OK or FAIL.
622 */
623 static int
624luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200625{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200626 int status = OK;
627
Bram Moolenaara9a8e5f2020-07-02 21:17:57 +0200628 tv->v_lock = 0;
629
Bram Moolenaarca06da92018-07-01 15:12:05 +0200630 switch (lua_type(L, pos))
631 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200632 case LUA_TBOOLEAN:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100633 tv->v_type = VAR_BOOL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200634 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
635 break;
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100636 case LUA_TNIL:
637 tv->v_type = VAR_SPECIAL;
638 tv->vval.v_number = VVAL_NULL;
639 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200640 case LUA_TSTRING:
641 tv->v_type = VAR_STRING;
642 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
643 break;
644 case LUA_TNUMBER:
Bram Moolenaareb04f082020-05-17 14:32:35 +0200645 {
646 const lua_Number n = lua_tonumber(L, pos);
647
648 if (n > (lua_Number)INT64_MAX || n < (lua_Number)INT64_MIN
649 || ((lua_Number)((varnumber_T)n)) != n)
650 {
651 tv->v_type = VAR_FLOAT;
652 tv->vval.v_float = (float_T)n;
653 }
654 else
655 {
656 tv->v_type = VAR_NUMBER;
657 tv->vval.v_number = (varnumber_T)n;
658 }
659 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200660 break;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200661 case LUA_TFUNCTION:
662 {
663 char_u *name;
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200664 luaV_CFuncState *state;
665
Bram Moolenaar801ab062020-06-25 19:27:56 +0200666 lua_pushvalue(L, pos);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200667 state = ALLOC_CLEAR_ONE(luaV_CFuncState);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200668 state->lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
669 state->L = L;
670 state->lua_tableref = LUA_NOREF;
671 name = register_cfunc(&luaV_call_lua_func,
672 &luaV_call_lua_func_free, state);
673 tv->v_type = VAR_FUNC;
674 tv->vval.v_string = vim_strsave(name);
675 break;
676 }
677 case LUA_TTABLE:
678 {
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200679 int lua_tableref;
680
Bram Moolenaar801ab062020-06-25 19:27:56 +0200681 lua_pushvalue(L, pos);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200682 lua_tableref = luaL_ref(L, LUA_REGISTRYINDEX);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000683 if (lua_getmetatable(L, pos))
684 {
Bram Moolenaar801ab062020-06-25 19:27:56 +0200685 lua_getfield(L, -1, LUA___CALL);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000686 if (lua_isfunction(L, -1))
687 {
Bram Moolenaar801ab062020-06-25 19:27:56 +0200688 char_u *name;
689 int lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
690 luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200691
Bram Moolenaar801ab062020-06-25 19:27:56 +0200692 state->lua_funcref = lua_funcref;
693 state->L = L;
694 state->lua_tableref = lua_tableref;
695 name = register_cfunc(&luaV_call_lua_func,
696 &luaV_call_lua_func_free, state);
697 tv->v_type = VAR_FUNC;
698 tv->vval.v_string = vim_strsave(name);
699 break;
700 }
701 }
702 tv->v_type = VAR_NUMBER;
703 tv->vval.v_number = 0;
704 status = FAIL;
705 break;
706 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200707 case LUA_TUSERDATA:
708 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200709 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200710
Bram Moolenaarb7828692019-03-23 13:57:02 +0100711 if (lua_getmetatable(L, pos)) // has metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200712 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100713 // check list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200714 luaV_getfield(L, LUAVIM_LIST);
715 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200716 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200717 tv->v_type = VAR_LIST;
718 tv->vval.v_list = *((luaV_List *) p);
719 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100720 lua_pop(L, 2); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200721 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200722 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100723 // check dict
Bram Moolenaar1dced572012-04-05 16:54:08 +0200724 luaV_getfield(L, LUAVIM_DICT);
725 if (lua_rawequal(L, -1, -3))
726 {
727 tv->v_type = VAR_DICT;
728 tv->vval.v_dict = *((luaV_Dict *) p);
729 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100730 lua_pop(L, 3); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200731 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200732 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100733 // check blob
734 luaV_getfield(L, LUAVIM_BLOB);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200735 if (lua_rawequal(L, -1, -4))
736 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100737 tv->v_type = VAR_BLOB;
738 tv->vval.v_blob = *((luaV_Blob *) p);
739 ++tv->vval.v_blob->bv_refcount;
740 lua_pop(L, 4); // MTs
741 break;
742 }
743 // check funcref
744 luaV_getfield(L, LUAVIM_FUNCREF);
745 if (lua_rawequal(L, -1, -5))
746 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200747 luaV_Funcref *f = (luaV_Funcref *) p;
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200748
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100749 func_ref(f->name);
750 tv->v_type = VAR_FUNC;
751 tv->vval.v_string = vim_strsave(f->name);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100752 lua_pop(L, 5); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200753 break;
754 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100755 lua_pop(L, 4); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200756 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200757 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100758 // FALLTHROUGH
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200759 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200760 tv->v_type = VAR_NUMBER;
761 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200762 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200763 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200764 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200765}
766
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100767/*
768 * similar to luaL_addlstring, but replaces \0 with \n if toline and
769 * \n with \0 otherwise
770 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200771 static void
772luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
773{
774 while (l--)
775 {
776 if (*s == '\0' && toline)
777 luaL_addchar(b, '\n');
778 else if (*s == '\n' && !toline)
779 luaL_addchar(b, '\0');
780 else
781 luaL_addchar(b, *s);
782 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200783 }
784}
785
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200786 static void
787luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
788{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200789 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
790 luaL_Buffer b;
791 luaL_buffinit(L, &b);
792 luaV_addlstring(&b, s, strlen(s), 0);
793 luaL_pushresult(&b);
794}
795
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200796 static char_u *
797luaV_toline(lua_State *L, int pos)
798{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200799 size_t l;
800 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200801
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200802 luaL_Buffer b;
803 luaL_buffinit(L, &b);
804 luaV_addlstring(&b, s, l, 1);
805 luaL_pushresult(&b);
806 return (char_u *) lua_tostring(L, -1);
807}
808
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100809/*
810 * pops a string s from the top of the stack and calls mf(t) for pieces t of
811 * s separated by newlines
812 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200813 static void
814luaV_msgfunc(lua_State *L, msgfunc_T mf)
815{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200816 luaL_Buffer b;
817 size_t l;
818 const char *p, *s = lua_tolstring(L, -1, &l);
819 luaL_buffinit(L, &b);
820 luaV_addlstring(&b, s, l, 0);
821 luaL_pushresult(&b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100822 // break string
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200823 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200824 while (l--)
825 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100826 if (*p++ == '\0') // break?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200827 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100828 mf((char *)s);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200829 s = p;
830 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200831 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100832 mf((char *)s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100833 lua_pop(L, 2); // original and modified strings
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200834}
835
Bram Moolenaar1dced572012-04-05 16:54:08 +0200836#define luaV_newtype(typ,tname,luatyp,luatname) \
837 static luatyp * \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100838 luaV_new##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200839 { \
840 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
841 *o = obj; \
842 luaV_setudata(L, obj); /* cache[obj] = udata */ \
843 luaV_getfield(L, luatname); \
844 lua_setmetatable(L, -2); \
845 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200846 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200847
848#define luaV_pushtype(typ,tname,luatyp) \
849 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200850 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200851 { \
852 luatyp *o = NULL; \
853 if (obj == NULL) \
854 lua_pushnil(L); \
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000855 else \
856 { \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200857 luaV_getudata(L, obj); \
858 if (lua_isnil(L, -1)) /* not interned? */ \
859 { \
860 lua_pop(L, 1); \
861 o = luaV_new##tname(L, obj); \
862 } \
863 else \
864 o = (luatyp *) lua_touserdata(L, -1); \
865 } \
866 return o; \
867 }
868
869#define luaV_type_tostring(tname,luatname) \
870 static int \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100871 luaV_##tname##_tostring(lua_State *L) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200872 { \
873 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
874 return 1; \
875 }
876
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100877// ======= List type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200878
879 static luaV_List *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100880luaV_newlist(lua_State *L, list_T *lis)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200881{
882 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
883 *l = lis;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100884 lis->lv_refcount++; // reference in Lua
885 luaV_setudata(L, lis); // cache[lis] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200886 luaV_getfield(L, LUAVIM_LIST);
887 lua_setmetatable(L, -2);
888 return l;
889}
890
891luaV_pushtype(list_T, list, luaV_List)
892luaV_type_tostring(list, LUAVIM_LIST)
893
894 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100895luaV_list_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200896{
897 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100898 lua_pushinteger(L, (int) list_len(l));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200899 return 1;
900}
901
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200902 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100903luaV_list_iter(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200904{
Jesse Pavel8a350332023-08-13 22:05:45 -0400905 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(1));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200906 if (li == NULL) return 0;
907 luaV_pushtypval(L, &li->li_tv);
908 lua_pushlightuserdata(L, (void *) li->li_next);
Jesse Pavel8a350332023-08-13 22:05:45 -0400909 lua_replace(L, lua_upvalueindex(1));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200910 return 1;
911}
912
913 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100914luaV_list_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200915{
916 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200917 lua_pushlightuserdata(L, (void *) l->lv_first);
Jesse Pavel8a350332023-08-13 22:05:45 -0400918 lua_pushcclosure(L, luaV_list_iter, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200919 return 1;
920}
921
922 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100923luaV_list_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200924{
925 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100926 if (lua_isnumber(L, 2)) // list item?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200927 {
Bram Moolenaarbd846172020-06-27 12:32:57 +0200928 long n = (long) luaL_checkinteger(L, 2);
929 listitem_T *li;
930
931 // Lua array index starts with 1 while Vim uses 0, subtract 1 to
932 // normalize.
933 n -= 1;
934 li = list_find(l, n);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200935 if (li == NULL)
936 lua_pushnil(L);
937 else
938 luaV_pushtypval(L, &li->li_tv);
939 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100940 else if (lua_isstring(L, 2)) // method?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200941 {
942 const char *s = lua_tostring(L, 2);
943 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200944 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200945 {
946 lua_getmetatable(L, 1);
947 lua_getfield(L, -1, s);
948 }
949 else
950 lua_pushnil(L);
951 }
952 else
953 lua_pushnil(L);
954 return 1;
955}
956
957 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100958luaV_list_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200959{
960 list_T *l = luaV_unbox(L, luaV_List, 1);
961 long n = (long) luaL_checkinteger(L, 2);
962 listitem_T *li;
Bram Moolenaarbd846172020-06-27 12:32:57 +0200963
964 // Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize.
965 n -= 1;
966
Bram Moolenaar1dced572012-04-05 16:54:08 +0200967 if (l->lv_lock)
968 luaL_error(L, "list is locked");
969 li = list_find(l, n);
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200970 if (li == NULL)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200971 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100972 if (!lua_isnil(L, 3))
973 {
974 typval_T v;
975 luaV_checktypval(L, 3, &v, "inserting list item");
976 if (list_insert_tv(l, &v, li) == FAIL)
977 luaL_error(L, "failed to add item to list");
978 clear_tv(&v);
979 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200980 }
981 else
982 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100983 if (lua_isnil(L, 3)) // remove?
984 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200985 vimlist_remove(l, li, li);
986 listitem_free(l, li);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100987 }
988 else
989 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200990 typval_T v;
991 luaV_checktypval(L, 3, &v, "setting list item");
992 clear_tv(&li->li_tv);
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200993 li->li_tv = v;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100994 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200995 }
996 return 0;
997}
998
999 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001000luaV_list_add(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001001{
1002 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
1003 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001004 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001005 if (l->lv_lock)
1006 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001007 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001008 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001009 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001010 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001011 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001012 lua_settop(L, 1);
1013 return 1;
1014}
1015
1016 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001017luaV_list_insert(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001018{
1019 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
1020 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +01001021 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001022 listitem_T *li = NULL;
1023 typval_T v;
1024 if (l->lv_lock)
1025 luaL_error(L, "list is locked");
1026 if (pos < l->lv_len)
1027 {
1028 li = list_find(l, pos);
1029 if (li == NULL)
1030 luaL_error(L, "invalid position");
1031 }
1032 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001033 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001034 if (list_insert_tv(l, &v, li) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001035 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001036 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001037 lua_settop(L, 1);
1038 return 1;
1039}
1040
1041static const luaL_Reg luaV_List_mt[] = {
1042 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001043 {"__len", luaV_list_len},
1044 {"__call", luaV_list_call},
1045 {"__index", luaV_list_index},
1046 {"__newindex", luaV_list_newindex},
1047 {"add", luaV_list_add},
1048 {"insert", luaV_list_insert},
1049 {NULL, NULL}
1050};
1051
1052
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001053// ======= Dict type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001054
1055 static luaV_Dict *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001056luaV_newdict(lua_State *L, dict_T *dic)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001057{
1058 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
1059 *d = dic;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001060 dic->dv_refcount++; // reference in Lua
1061 luaV_setudata(L, dic); // cache[dic] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +02001062 luaV_getfield(L, LUAVIM_DICT);
1063 lua_setmetatable(L, -2);
1064 return d;
1065}
1066
1067luaV_pushtype(dict_T, dict, luaV_Dict)
1068luaV_type_tostring(dict, LUAVIM_DICT)
1069
1070 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001071luaV_dict_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001072{
1073 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001074 lua_pushinteger(L, (int) dict_len(d));
Bram Moolenaar1dced572012-04-05 16:54:08 +02001075 return 1;
1076}
1077
1078 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001079luaV_dict_iter(lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001080{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001081#ifdef FEAT_EVAL
Jesse Pavel8a350332023-08-13 22:05:45 -04001082 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(1));
1083 int n = lua_tointeger(L, lua_upvalueindex(2));
Bram Moolenaar1dced572012-04-05 16:54:08 +02001084 dictitem_T *di;
1085 if (n <= 0) return 0;
1086 while (HASHITEM_EMPTY(hi)) hi++;
1087 di = dict_lookup(hi);
1088 lua_pushstring(L, (char *) hi->hi_key);
1089 luaV_pushtypval(L, &di->di_tv);
1090 lua_pushlightuserdata(L, (void *) (hi + 1));
Jesse Pavel8a350332023-08-13 22:05:45 -04001091 lua_replace(L, lua_upvalueindex(1));
Bram Moolenaar1dced572012-04-05 16:54:08 +02001092 lua_pushinteger(L, n - 1);
Jesse Pavel8a350332023-08-13 22:05:45 -04001093 lua_replace(L, lua_upvalueindex(2));
Bram Moolenaar1dced572012-04-05 16:54:08 +02001094 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001095#else
1096 return 0;
1097#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001098}
1099
1100 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001101luaV_dict_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001102{
1103 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1104 hashtab_T *ht = &d->dv_hashtab;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001105 lua_pushlightuserdata(L, (void *) ht->ht_array);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001106 lua_pushinteger(L, ht->ht_used); // # remaining items
Jesse Pavel8a350332023-08-13 22:05:45 -04001107 lua_pushcclosure(L, luaV_dict_iter, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001108 return 1;
1109}
1110
1111 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001112luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001113{
1114 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1115 char_u *key = (char_u *) luaL_checkstring(L, 2);
1116 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001117
Bram Moolenaar1dced572012-04-05 16:54:08 +02001118 if (di == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001119 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001120 lua_pushnil(L);
1121 return 1;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001122 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001123
1124 luaV_pushtypval(L, &di->di_tv);
1125 if (di->di_tv.v_type == VAR_FUNC) // funcref?
1126 {
1127 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
1128 f->self = d; // keep "self" reference
1129 d->dv_refcount++;
1130 }
1131
Bram Moolenaar1dced572012-04-05 16:54:08 +02001132 return 1;
1133}
1134
1135 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001136luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001137{
1138 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1139 char_u *key = (char_u *) luaL_checkstring(L, 2);
1140 dictitem_T *di;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001141 typval_T tv;
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001142
Bram Moolenaar1dced572012-04-05 16:54:08 +02001143 if (d->dv_lock)
1144 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001145 if (key == NULL)
1146 return 0;
1147 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001148 luaL_error(L, "empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001149 if (!lua_isnil(L, 3)) // read value?
Bram Moolenaar17413672018-07-14 20:49:42 +02001150 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001151 luaV_checktypval(L, 3, &tv, "setting dict item");
1152 if (d->dv_scope == VAR_DEF_SCOPE && tv.v_type == VAR_FUNC)
1153 {
1154 clear_tv(&tv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001155 luaL_error(L, "cannot assign funcref to builtin scope");
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001156 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001157 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001158 di = dict_find(d, key, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001159 if (di == NULL) // non-existing key?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001160 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001161 if (lua_isnil(L, 3))
1162 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001163 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001164 if (di == NULL)
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001165 {
1166 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001167 return 0;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001168 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001169 if (dict_add(d, di) == FAIL)
1170 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001171 vim_free(di);
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001172 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001173 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001174 }
1175 }
1176 else
1177 clear_tv(&di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001178 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001179 {
1180 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001181 hash_remove(&d->dv_hashtab, hi, "Lua new index");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001182 dictitem_free(di);
1183 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001184 else
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001185 di->di_tv = tv;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001186 return 0;
1187}
1188
1189static const luaL_Reg luaV_Dict_mt[] = {
1190 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001191 {"__len", luaV_dict_len},
1192 {"__call", luaV_dict_call},
1193 {"__index", luaV_dict_index},
1194 {"__newindex", luaV_dict_newindex},
1195 {NULL, NULL}
1196};
1197
1198
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001199// ======= Blob type =======
Bram Moolenaarb7828692019-03-23 13:57:02 +01001200
1201 static luaV_Blob *
1202luaV_newblob(lua_State *L, blob_T *blo)
1203{
1204 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1205 *b = blo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001206 blo->bv_refcount++; // reference in Lua
1207 luaV_setudata(L, blo); // cache[blo] = udata
Bram Moolenaarb7828692019-03-23 13:57:02 +01001208 luaV_getfield(L, LUAVIM_BLOB);
1209 lua_setmetatable(L, -2);
1210 return b;
1211}
1212
1213luaV_pushtype(blob_T, blob, luaV_Blob)
1214luaV_type_tostring(blob, LUAVIM_BLOB)
1215
1216 static int
1217luaV_blob_gc(lua_State *L)
1218{
1219 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1220 blob_unref(b);
1221 return 0;
1222}
1223
1224 static int
1225luaV_blob_len(lua_State *L)
1226{
1227 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1228 lua_pushinteger(L, (int) blob_len(b));
1229 return 1;
1230}
1231
1232 static int
1233luaV_blob_index(lua_State *L)
1234{
1235 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1236 if (lua_isnumber(L, 2))
1237 {
1238 int idx = luaL_checkinteger(L, 2);
1239 if (idx < blob_len(b))
1240 lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1241 else
1242 lua_pushnil(L);
1243 }
1244 else if (lua_isstring(L, 2))
1245 {
1246 const char *s = lua_tostring(L, 2);
1247 if (strncmp(s, "add", 3) == 0)
1248 {
1249 lua_getmetatable(L, 1);
1250 lua_getfield(L, -1, s);
1251 }
1252 else
1253 lua_pushnil(L);
1254 }
1255 else
1256 lua_pushnil(L);
1257 return 1;
1258}
1259
1260 static int
1261luaV_blob_newindex(lua_State *L)
1262{
1263 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1264 if (b->bv_lock)
1265 luaL_error(L, "blob is locked");
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001266
1267 if (!lua_isnumber(L, 2))
1268 return 0;
1269
1270 long len = blob_len(b);
1271 int idx = luaL_checkinteger(L, 2);
1272 int val = luaL_checkinteger(L, 3);
1273 if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
Bram Moolenaarb7828692019-03-23 13:57:02 +01001274 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001275 blob_set(b, idx, (char_u) val);
1276 if (idx == len)
1277 ++b->bv_ga.ga_len;
Bram Moolenaarb7828692019-03-23 13:57:02 +01001278 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001279 else
1280 luaL_error(L, "index out of range");
1281
Bram Moolenaarb7828692019-03-23 13:57:02 +01001282 return 0;
1283}
1284
1285 static int
1286luaV_blob_add(lua_State *L)
1287{
1288 luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1289 blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1290 if (b->bv_lock)
1291 luaL_error(L, "blob is locked");
1292 lua_settop(L, 2);
1293 if (!lua_isstring(L, 2))
1294 luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1295 else
1296 {
1297 size_t i, l = 0;
1298 const char *s = lua_tolstring(L, 2, &l);
1299
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001300 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001301 for (i = 0; i < l; ++i)
1302 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001303 }
1304 lua_settop(L, 1);
1305 return 1;
1306}
1307
1308static const luaL_Reg luaV_Blob_mt[] = {
1309 {"__tostring", luaV_blob_tostring},
1310 {"__gc", luaV_blob_gc},
1311 {"__len", luaV_blob_len},
1312 {"__index", luaV_blob_index},
1313 {"__newindex", luaV_blob_newindex},
1314 {"add", luaV_blob_add},
1315 {NULL, NULL}
1316};
1317
1318
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001319// ======= Funcref type =======
Bram Moolenaarca06da92018-07-01 15:12:05 +02001320
1321 static luaV_Funcref *
1322luaV_newfuncref(lua_State *L, char_u *name)
1323{
1324 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1325
1326 if (name != NULL)
1327 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001328 func_ref(name);
1329 f->name = vim_strsave(name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001330 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001331 f->self = NULL;
1332 luaV_getfield(L, LUAVIM_FUNCREF);
1333 lua_setmetatable(L, -2);
1334 return f;
1335}
1336
1337 static luaV_Funcref *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001338luaV_pushfuncref(lua_State *L, char_u *name)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001339{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001340 return luaV_newfuncref(L, name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001341}
1342
1343
1344luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1345
1346 static int
1347luaV_funcref_gc(lua_State *L)
1348{
1349 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1350
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001351 func_unref(f->name);
1352 vim_free(f->name);
1353 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1354 // will be (or has been already) freed by Vim's garbage collection.
Bram Moolenaarca06da92018-07-01 15:12:05 +02001355 return 0;
1356}
1357
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001358// equivalent to string(funcref)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001359 static int
1360luaV_funcref_len(lua_State *L)
1361{
1362 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1363
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001364 lua_pushstring(L, (const char *) f->name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001365 return 1;
1366}
1367
1368 static int
1369luaV_funcref_call(lua_State *L)
1370{
1371 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001372 int i, n = lua_gettop(L) - 1; // #args
1373 int status = FAIL;
1374 typval_T args;
1375 typval_T rettv;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001376
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001377 args.v_type = VAR_LIST;
1378 args.vval.v_list = list_alloc();
1379 rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1380 if (args.vval.v_list != NULL)
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001381 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001382 typval_T v;
1383
Bram Moolenaar17413672018-07-14 20:49:42 +02001384 for (i = 0; i < n; i++)
1385 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001386 luaV_checktypval(L, i + 2, &v, "calling funcref");
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001387 list_append_tv(args.vval.v_list, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001388 clear_tv(&v);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001389 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001390 status = func_call(f->name, &args, NULL, f->self, &rettv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001391 if (status == OK)
1392 luaV_pushtypval(L, &rettv);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001393 clear_tv(&args);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001394 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001395 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001396 if (status != OK)
1397 luaL_error(L, "cannot call funcref");
1398 return 1;
1399}
1400
1401static const luaL_Reg luaV_Funcref_mt[] = {
1402 {"__tostring", luaV_funcref_tostring},
1403 {"__gc", luaV_funcref_gc},
1404 {"__len", luaV_funcref_len},
1405 {"__call", luaV_funcref_call},
1406 {NULL, NULL}
1407};
1408
1409
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001410// ======= Buffer type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001411
1412luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1413luaV_pushtype(buf_T, buffer, luaV_Buffer)
1414luaV_type_tostring(buffer, LUAVIM_BUFFER)
1415
1416 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001417luaV_buffer_len(lua_State *L)
1418{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001419 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1420 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001421 return 1;
1422}
1423
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001424 static int
1425luaV_buffer_call(lua_State *L)
1426{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001427 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001428 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001429 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001430 return 1;
1431}
1432
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001433 static int
1434luaV_buffer_index(lua_State *L)
1435{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001436 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001437 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001438 if (n > 0 && n <= b->b_ml.ml_line_count)
1439 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001440 else if (lua_isstring(L, 2))
1441 {
1442 const char *s = lua_tostring(L, 2);
1443 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001444 lua_pushstring(L, (b->b_sfname == NULL)
1445 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001446 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001447 lua_pushstring(L, (b->b_ffname == NULL)
1448 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001449 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001450 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001451 // methods
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001452 else if (strncmp(s, "insert", 6) == 0
1453 || strncmp(s, "next", 4) == 0
1454 || strncmp(s, "previous", 8) == 0
1455 || strncmp(s, "isvalid", 7) == 0)
1456 {
1457 lua_getmetatable(L, 1);
1458 lua_getfield(L, -1, s);
1459 }
1460 else
1461 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001462 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001463 else
1464 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001465 return 1;
1466}
1467
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001468 static int
1469luaV_buffer_newindex(lua_State *L)
1470{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001471 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001472 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1473#ifdef HAVE_SANDBOX
1474 luaV_checksandbox(L);
1475#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001476 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001477 luaL_error(L, "invalid line number");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001478 if (lua_isnil(L, 3)) // delete line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001479 {
1480 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001481 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001482 if (u_savedel(n, 1L) == FAIL)
1483 {
1484 curbuf = buf;
1485 luaL_error(L, "cannot save undo information");
1486 }
Bram Moolenaarca70c072020-05-30 20:30:46 +02001487 else if (ml_delete(n) == FAIL)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001488 {
1489 curbuf = buf;
1490 luaL_error(L, "cannot delete line");
1491 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001492 else
1493 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001494 deleted_lines_mark(n, 1L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001495 if (b == curwin->w_buffer) // fix cursor in current window?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001496 {
1497 if (curwin->w_cursor.lnum >= n)
1498 {
1499 if (curwin->w_cursor.lnum > n)
1500 {
1501 curwin->w_cursor.lnum -= 1;
1502 check_cursor_col();
1503 }
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001504 else
1505 check_cursor();
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001506 changed_cline_bef_curs();
1507 }
1508 invalidate_botline();
1509 }
1510 }
1511 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001512 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001513 else if (lua_isstring(L, 3)) // update line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001514 {
1515 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001516 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001517 if (u_savesub(n) == FAIL)
1518 {
1519 curbuf = buf;
1520 luaL_error(L, "cannot save undo information");
1521 }
1522 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1523 {
1524 curbuf = buf;
1525 luaL_error(L, "cannot replace line");
1526 }
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001527 else
1528 changed_bytes(n, 0);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001529 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001530 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001531 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001532 }
1533 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001534 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001535 return 0;
1536}
1537
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001538 static int
1539luaV_buffer_insert(lua_State *L)
1540{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001541 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1542 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1543 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001544 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1545 buf_T *buf;
1546 luaL_checktype(L, 2, LUA_TSTRING);
1547#ifdef HAVE_SANDBOX
1548 luaV_checksandbox(L);
1549#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001550 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001551 if (n < 0) n = 0;
1552 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001553 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001554 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001555 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001556 if (u_save(n, n + 1) == FAIL)
1557 {
1558 curbuf = buf;
1559 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001560 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001561 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1562 {
1563 curbuf = buf;
1564 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001565 }
1566 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001567 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001568 curbuf = buf;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001569 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001570 return 0;
1571}
1572
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001573 static int
1574luaV_buffer_next(lua_State *L)
1575{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001576 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001577 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1578 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001579 return 1;
1580}
1581
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001582 static int
1583luaV_buffer_previous(lua_State *L)
1584{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001585 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001586 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1587 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001588 return 1;
1589}
1590
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001591 static int
1592luaV_buffer_isvalid(lua_State *L)
1593{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001594 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001595 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001596 lua_pushboolean(L, !lua_isnil(L, -1));
1597 return 1;
1598}
1599
1600static const luaL_Reg luaV_Buffer_mt[] = {
1601 {"__tostring", luaV_buffer_tostring},
1602 {"__len", luaV_buffer_len},
1603 {"__call", luaV_buffer_call},
1604 {"__index", luaV_buffer_index},
1605 {"__newindex", luaV_buffer_newindex},
1606 {"insert", luaV_buffer_insert},
1607 {"next", luaV_buffer_next},
1608 {"previous", luaV_buffer_previous},
1609 {"isvalid", luaV_buffer_isvalid},
1610 {NULL, NULL}
1611};
1612
1613
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001614// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001615
Bram Moolenaar1dced572012-04-05 16:54:08 +02001616luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1617luaV_pushtype(win_T, window, luaV_Window)
1618luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001619
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001620 static int
1621luaV_window_call(lua_State *L)
1622{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001623 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001624 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001625 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001626 return 1;
1627}
1628
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001629 static int
1630luaV_window_index(lua_State *L)
1631{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001632 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001633 const char *s = luaL_checkstring(L, 2);
1634 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001635 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001636 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001637 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001638 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001639 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001640 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001641 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001642 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001643 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001644 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001645 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001646 || strncmp(s, "previous", 8) == 0
1647 || strncmp(s, "isvalid", 7) == 0)
1648 {
1649 lua_getmetatable(L, 1);
1650 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001651 }
1652 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001653 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001654 return 1;
1655}
1656
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001657 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001658luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001659{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001660 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001661 const char *s = luaL_checkstring(L, 2);
1662 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001663 if (strncmp(s, "line", 4) == 0)
1664 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001665#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001666 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001667#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001668 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001669 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001670 w->w_cursor.lnum = v;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001671 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001672 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001673 else if (strncmp(s, "col", 3) == 0)
1674 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001675#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001676 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001677#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001678 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001679 w->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001680 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001681 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001682 else if (strncmp(s, "width", 5) == 0)
1683 {
1684 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001685#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001686 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001687#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001688 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001689 win_setwidth(v);
1690 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001691 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001692 else if (strncmp(s, "height", 6) == 0)
1693 {
1694 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001695#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001696 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001697#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001698 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001699 win_setheight(v);
1700 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001701 }
1702 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001703 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001704 return 0;
1705}
1706
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001707 static int
1708luaV_window_next(lua_State *L)
1709{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001710 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001711 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1712 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001713 return 1;
1714}
1715
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001716 static int
1717luaV_window_previous(lua_State *L)
1718{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001719 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001720 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1721 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001722 return 1;
1723}
1724
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001725 static int
1726luaV_window_isvalid(lua_State *L)
1727{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001728 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001729 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001730 lua_pushboolean(L, !lua_isnil(L, -1));
1731 return 1;
1732}
1733
1734static const luaL_Reg luaV_Window_mt[] = {
1735 {"__tostring", luaV_window_tostring},
1736 {"__call", luaV_window_call},
1737 {"__index", luaV_window_index},
1738 {"__newindex", luaV_window_newindex},
1739 {"next", luaV_window_next},
1740 {"previous", luaV_window_previous},
1741 {"isvalid", luaV_window_isvalid},
1742 {NULL, NULL}
1743};
1744
1745
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001746// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001747
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001748 static int
1749luaV_print(lua_State *L)
1750{
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001751 int i, n = lua_gettop(L); // nargs
1752 const char *s;
1753 size_t l;
1754 garray_T msg_ga;
1755
1756 ga_init2(&msg_ga, 1, 128);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001757 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001758 for (i = 1; i <= n; i++)
1759 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001760 lua_pushvalue(L, -1); // tostring
1761 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001762 lua_call(L, 1, 1);
1763 s = lua_tolstring(L, -1, &l);
1764 if (s == NULL)
1765 return luaL_error(L, "cannot convert to string");
Bram Moolenaar78e006b2021-07-28 15:07:01 +02001766 if (i > 1)
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001767 ga_append(&msg_ga, ' '); // use space instead of tab
1768 ga_concat_len(&msg_ga, (char_u *)s, l);
Bram Moolenaar2a4bd002021-07-28 21:48:59 +02001769 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001770 }
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001771 // Replace any "\n" with "\0"
1772 for (i = 0; i < msg_ga.ga_len; i++)
1773 if (((char *)msg_ga.ga_data)[i] == '\n')
1774 ((char *)msg_ga.ga_data)[i] = '\0';
1775 lua_pushlstring(L, msg_ga.ga_data, msg_ga.ga_len);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001776 if (!got_int)
1777 luaV_msg(L);
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001778
1779 ga_clear(&msg_ga);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001780 return 0;
1781}
1782
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001783 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001784luaV_debug(lua_State *L)
1785{
1786 lua_settop(L, 0);
1787 lua_getglobal(L, "vim");
1788 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001789 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001790 for (;;)
1791 {
1792 const char *input;
1793 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001794 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001795 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001796 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001797 input = lua_tolstring(L, -1, &l);
1798 if (l == 0 || strcmp(input, "cont") == 0)
1799 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001800 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001801 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1802 || lua_pcall(L, 0, 0, 0))
1803 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001804 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001805 }
1806}
1807
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001808 static dict_T *
1809luaV_get_var_scope(lua_State *L)
1810{
1811 const char *scope = luaL_checkstring(L, 1);
1812 dict_T *dict = NULL;
1813
1814 if (STRICMP((char *)scope, "g") == 0)
1815 dict = get_globvar_dict();
1816 else if (STRICMP((char *)scope, "v") == 0)
1817 dict = get_vimvar_dict();
1818 else if (STRICMP((char *)scope, "b") == 0)
1819 dict = curbuf->b_vars;
1820 else if (STRICMP((char *)scope, "w") == 0)
1821 dict = curwin->w_vars;
1822 else if (STRICMP((char *)scope, "t") == 0)
1823 dict = curtab->tp_vars;
1824 else
1825 {
1826 luaL_error(L, "invalid scope %s", scope);
1827 return NULL;
1828 }
1829
1830 return dict;
1831}
1832
1833 static int
1834luaV_setvar(lua_State *L)
1835{
1836 dict_T *dict;
1837 dictitem_T *di;
1838 size_t len;
1839 char *name;
1840 int del;
1841 char *error = NULL;
1842
1843 name = (char *)luaL_checklstring(L, 3, &len);
1844 del = (lua_gettop(L) < 4) || lua_isnil(L, 4);
1845
1846 dict = luaV_get_var_scope(L);
1847 if (dict == NULL)
1848 return 0;
1849
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001850 di = dict_find(dict, (char_u *)name, (int)len);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001851 if (di != NULL)
1852 {
1853 if (di->di_flags & DI_FLAGS_RO)
1854 error = "variable is read-only";
1855 else if (di->di_flags & DI_FLAGS_LOCK)
1856 error = "variable is locked";
1857 else if (del && di->di_flags & DI_FLAGS_FIX)
1858 error = "variable is fixed";
1859 if (error != NULL)
1860 return luaL_error(L, error);
1861 }
1862 else if (dict->dv_lock)
1863 return luaL_error(L, "Dictionary is locked");
1864
1865 if (del)
1866 {
1867 // Delete the key
1868 if (di == NULL)
1869 // Doesn't exist, nothing to do
1870 return 0;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001871 // Delete the entry
1872 dictitem_remove(dict, di, "Lua delete variable");
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001873 }
1874 else
1875 {
1876 // Update the key
1877 typval_T tv;
1878
h-east965d2ed2021-10-04 21:51:57 +01001879 // Convert the lua value to a Vim script type in the temporary variable
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001880 lua_pushvalue(L, 4);
1881 if (luaV_totypval(L, -1, &tv) == FAIL)
1882 return luaL_error(L, "Couldn't convert lua value");
1883
1884 if (di == NULL)
1885 {
1886 // Need to create an entry
1887 di = dictitem_alloc((char_u *)name);
1888 if (di == NULL)
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001889 {
1890 clear_tv(&tv);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001891 return 0;
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001892 }
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001893 // Update the value
1894 copy_tv(&tv, &di->di_tv);
Bram Moolenaar4a011592021-08-05 15:11:08 +02001895 if (dict_add(dict, di) == FAIL)
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001896 {
1897 dictitem_free(di);
1898 clear_tv(&tv);
Bram Moolenaar4a011592021-08-05 15:11:08 +02001899 return luaL_error(L, "Couldn't add to dictionary");
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001900 }
1901 }
1902 else
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001903 {
1904 // Clear the old value
1905 clear_tv(&di->di_tv);
1906 // Update the value
1907 copy_tv(&tv, &di->di_tv);
1908 }
1909
1910 // Clear the temporary variable
1911 clear_tv(&tv);
1912 }
1913
1914 return 0;
1915}
1916
1917 static int
1918luaV_getvar(lua_State *L)
1919{
1920 dict_T *dict = luaV_get_var_scope(L);
1921 size_t len;
1922 const char *name = luaL_checklstring(L, 3, &len);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001923 dictitem_T *di = dict_find(dict, (char_u *)name, (int)len);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001924
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001925 if (di == NULL)
1926 return 0; // nil
1927
1928 luaV_pushtypval(L, &di->di_tv);
1929 return 1;
1930}
1931
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001932 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001933luaV_command(lua_State *L)
1934{
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001935 char_u *s = vim_strsave((char_u *)luaL_checkstring(L, 1));
1936
1937 execute_cmds_from_string(s);
1938 vim_free(s);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001939 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001940 return 0;
1941}
1942
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001943 static int
1944luaV_eval(lua_State *L)
1945{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001946 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001947
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001948 if (tv == NULL) luaL_error(L, "invalid expression");
1949 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001950 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001951 return 1;
1952}
1953
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001954 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001955luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001956{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001957 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001958 return 0;
1959}
1960
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001961 static int
1962luaV_line(lua_State *L)
1963{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001964 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1965 return 1;
1966}
1967
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001968 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001969luaV_list(lua_State *L)
1970{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001971 list_T *l;
1972 int initarg = !lua_isnoneornil(L, 1);
1973
1974 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1975 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001976
Bram Moolenaarca06da92018-07-01 15:12:05 +02001977 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001978 if (l == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001979 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001980 lua_pushnil(L);
1981 return 1;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001982 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001983
1984 luaV_newlist(L, l);
1985 if (!initarg)
1986 return 1;
1987
1988 // traverse table to init list
1989 int notnil, i = 0;
1990 typval_T v;
1991 do
1992 {
1993 lua_rawgeti(L, 1, ++i);
1994 notnil = !lua_isnil(L, -1);
1995 if (notnil)
1996 {
1997 luaV_checktypval(L, -1, &v, "vim.list");
1998 list_append_tv(l, &v);
1999 clear_tv(&v);
2000 }
2001 lua_pop(L, 1); // value
2002 } while (notnil);
2003
Bram Moolenaar1dced572012-04-05 16:54:08 +02002004 return 1;
2005}
2006
2007 static int
2008luaV_dict(lua_State *L)
2009{
Bram Moolenaarca06da92018-07-01 15:12:05 +02002010 dict_T *d;
2011 int initarg = !lua_isnoneornil(L, 1);
2012
2013 if (initarg && lua_type(L, 1) != LUA_TTABLE)
2014 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002015
Bram Moolenaarca06da92018-07-01 15:12:05 +02002016 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02002017 if (d == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002018 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002019 lua_pushnil(L);
2020 return 1;
2021 }
2022
2023 luaV_newdict(L, d);
2024 if (!initarg)
2025 return 1;
2026
2027 // traverse table to init dict
2028 lua_pushnil(L);
2029 while (lua_next(L, 1))
2030 {
2031 char_u *key;
2032 dictitem_T *di;
2033 typval_T v;
2034
2035 lua_pushvalue(L, -2); // dup key in case it's a number
2036 key = (char_u *) lua_tostring(L, -1);
2037 if (key == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002038 {
2039 lua_pushnil(L);
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002040 return 1;
Bram Moolenaarca06da92018-07-01 15:12:05 +02002041 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002042 if (*key == NUL)
2043 luaL_error(L, "table has empty key");
2044 luaV_checktypval(L, -2, &v, "vim.dict"); // value
2045 di = dictitem_alloc(key);
2046 if (di == NULL || dict_add(d, di) == FAIL)
2047 {
2048 vim_free(di);
2049 lua_pushnil(L);
2050 return 1;
2051 }
2052 di->di_tv = v;
2053 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02002054 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002055
Bram Moolenaarca06da92018-07-01 15:12:05 +02002056 return 1;
2057}
2058
2059 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01002060luaV_blob(lua_State *L)
2061{
2062 blob_T *b;
2063 int initarg = !lua_isnoneornil(L, 1);
2064
2065 if (initarg && !lua_isstring(L, 1))
2066 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002067
Bram Moolenaarb7828692019-03-23 13:57:02 +01002068 b = blob_alloc();
2069 if (b == NULL)
Bram Moolenaarb7828692019-03-23 13:57:02 +01002070 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002071 lua_pushnil(L);
2072 return 1;
Bram Moolenaarb7828692019-03-23 13:57:02 +01002073 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002074
2075 luaV_newblob(L, b);
2076 if (!initarg)
2077 return 1;
2078
2079 // traverse table to init blob
2080 size_t i, l = 0;
2081 const char *s = lua_tolstring(L, 1, &l);
2082
2083 if (ga_grow(&b->bv_ga, (int)l) == OK)
2084 for (i = 0; i < l; ++i)
2085 ga_append(&b->bv_ga, s[i]);
2086
Bram Moolenaarb7828692019-03-23 13:57:02 +01002087 return 1;
2088}
2089
2090 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02002091luaV_funcref(lua_State *L)
2092{
2093 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002094 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002095 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
2096 luaL_error(L, "invalid function name: %s", name);
2097 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002098 return 1;
2099}
2100
2101 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002102luaV_buffer(lua_State *L)
2103{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002104 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002105 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002106 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002107 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002108 {
2109 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02002110 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002111 if (buf->b_fnum == n) break;
2112 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002113 else // by name
2114 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002115 size_t l;
2116 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02002117 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002118 {
2119 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
2120 {
2121 if (l == 0) break;
2122 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02002123 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
2124 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002125 break;
2126 }
2127 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002128 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002129 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002130 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002131 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002132 return 1;
2133}
2134
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002135 static int
2136luaV_window(lua_State *L)
2137{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002138 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002139 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002140 {
2141 int n = lua_tointeger(L, 1);
2142 for (win = firstwin; win != NULL; win = win->w_next, n--)
2143 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002144 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002145 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002146 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002147 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002148 return 1;
2149}
2150
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002151 static int
2152luaV_open(lua_State *L)
2153{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002154 char_u *s = NULL;
2155#ifdef HAVE_SANDBOX
2156 luaV_checksandbox(L);
2157#endif
2158 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01002159 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002160 return 1;
2161}
2162
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002163 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002164luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002165{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002166 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002167 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002168 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02002169 lua_settop(L, 1);
2170 if (lua_getmetatable(L, 1))
2171 {
2172 luaV_getfield(L, LUAVIM_LIST);
2173 if (lua_rawequal(L, -1, 2))
2174 {
2175 lua_pushstring(L, "list");
2176 return 1;
2177 }
2178 luaV_getfield(L, LUAVIM_DICT);
2179 if (lua_rawequal(L, -1, 2))
2180 {
2181 lua_pushstring(L, "dict");
2182 return 1;
2183 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01002184 luaV_getfield(L, LUAVIM_BLOB);
2185 if (lua_rawequal(L, -1, 2))
2186 {
2187 lua_pushstring(L, "blob");
2188 return 1;
2189 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002190 luaV_getfield(L, LUAVIM_FUNCREF);
2191 if (lua_rawequal(L, -1, 2))
2192 {
2193 lua_pushstring(L, "funcref");
2194 return 1;
2195 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002196 luaV_getfield(L, LUAVIM_BUFFER);
2197 if (lua_rawequal(L, -1, 2))
2198 {
2199 lua_pushstring(L, "buffer");
2200 return 1;
2201 }
2202 luaV_getfield(L, LUAVIM_WINDOW);
2203 if (lua_rawequal(L, -1, 2))
2204 {
2205 lua_pushstring(L, "window");
2206 return 1;
2207 }
2208 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002209 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002210 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02002211 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002212}
2213
Bram Moolenaareb04f082020-05-17 14:32:35 +02002214 static int
2215luaV_call(lua_State *L)
2216{
2217 int argc = lua_gettop(L) - 1;
2218 size_t funcname_len;
2219 char_u *funcname;
2220 char *error = NULL;
2221 typval_T rettv;
2222 typval_T argv[MAX_FUNC_ARGS + 1];
2223 int i = 0;
2224
2225 if (argc > MAX_FUNC_ARGS)
2226 return luaL_error(L, "Function called with too many arguments");
2227
2228 funcname = (char_u *)luaL_checklstring(L, 1, &funcname_len);
2229
2230 for (; i < argc; i++)
2231 {
2232 if (luaV_totypval(L, i + 2, &argv[i]) == FAIL)
2233 {
2234 error = "lua: cannot convert value";
2235 goto free_vim_args;
2236 }
2237 }
2238
2239 argv[argc].v_type = VAR_UNKNOWN;
2240
2241 if (call_vim_function(funcname, argc, argv, &rettv) == FAIL)
2242 {
2243 error = "lua: call_vim_function failed";
2244 goto free_vim_args;
2245 }
2246
2247 luaV_pushtypval(L, &rettv);
2248 clear_tv(&rettv);
2249
2250free_vim_args:
2251 while (i > 0)
2252 clear_tv(&argv[--i]);
2253
2254 if (error == NULL)
2255 return 1;
2256 else
2257 return luaL_error(L, error);
2258}
2259
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02002260/*
2261 * Return the Vim version as a Lua table
2262 */
2263 static int
2264luaV_version(lua_State *L)
2265{
2266 lua_newtable(L);
2267 lua_pushstring(L, "major");
2268 lua_pushinteger(L, VIM_VERSION_MAJOR);
2269 lua_settable(L, -3);
2270 lua_pushstring(L, "minor");
2271 lua_pushinteger(L, VIM_VERSION_MINOR);
2272 lua_settable(L, -3);
2273 lua_pushstring(L, "patch");
2274 lua_pushinteger(L, highest_patch());
2275 lua_settable(L, -3);
2276 return 1;
2277}
2278
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002279static const luaL_Reg luaV_module[] = {
2280 {"command", luaV_command},
2281 {"eval", luaV_eval},
2282 {"beep", luaV_beep},
2283 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002284 {"list", luaV_list},
2285 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01002286 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02002287 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002288 {"buffer", luaV_buffer},
2289 {"window", luaV_window},
2290 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002291 {"type", luaV_type},
Bram Moolenaareb04f082020-05-17 14:32:35 +02002292 {"call", luaV_call},
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002293 {"_getvar", luaV_getvar},
2294 {"_setvar", luaV_setvar},
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02002295 {"version", luaV_version},
Bram Moolenaar125ed272021-04-07 20:11:12 +02002296 {"lua_version", NULL},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002297 {NULL, NULL}
2298};
2299
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002300/*
2301 * for freeing list, dict, buffer and window objects; lightuserdata as arg
2302 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02002303 static int
2304luaV_free(lua_State *L)
2305{
2306 lua_pushnil(L);
2307 luaV_setudata(L, lua_touserdata(L, 1));
2308 return 0;
2309}
2310
2311 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002312luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002313{
2314 luaL_Buffer b;
2315 size_t l;
2316 const char *str = lua_tolstring(L, 1, &l);
2317 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
2318 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
2319 luaL_buffinit(L, &b);
2320 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
2321 luaL_addlstring(&b, str, l);
2322 luaL_pushresult(&b);
2323 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002324 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002325 {
2326 luaV_emsg(L);
2327 return 0;
2328 }
2329 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002330 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002331 {
2332 luaV_emsg(L);
2333 return 0;
2334 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002335 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002336 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01002337 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002338}
2339
2340 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002341luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002342{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002343 int copyID = lua_tointeger(L, 1);
2344 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002345
Jesse Pavel8a350332023-08-13 22:05:45 -04002346 lua_pushlightuserdata(L, (void *) LUAVIM_UDATA_CACHE);
2347 lua_rawget(L, LUA_REGISTRYINDEX); // the cache table
2348
Bram Moolenaar1dced572012-04-05 16:54:08 +02002349 luaV_getfield(L, LUAVIM_LIST);
2350 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002351 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002352 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002353 // traverse cache table
Jesse Pavel8a350332023-08-13 22:05:45 -04002354 while (!abort && lua_next(L, 2) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002355 {
2356 lua_getmetatable(L, -1);
Jesse Pavel8a350332023-08-13 22:05:45 -04002357 if (lua_rawequal(L, -1, 3)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002358 {
Jesse Pavel8a350332023-08-13 22:05:45 -04002359 list_T *l = (list_T *)lua_touserdata(L, 6); // key
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002360
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002361 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002362 }
Jesse Pavel8a350332023-08-13 22:05:45 -04002363 else if (lua_rawequal(L, -1, 4)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002364 {
Jesse Pavel8a350332023-08-13 22:05:45 -04002365 dict_T *d = (dict_T *)lua_touserdata(L, 6); // key
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002366
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002367 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002368 }
Jesse Pavel8a350332023-08-13 22:05:45 -04002369 else if (lua_rawequal(L, -1, 5)) // funcref?
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002370 {
Jesse Pavel8a350332023-08-13 22:05:45 -04002371 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 6); // key
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002372
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002373 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002374 }
2375 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02002376 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002377 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01002378 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002379}
2380
Bram Moolenaar125ed272021-04-07 20:11:12 +02002381 static int
2382luaV_pushversion(lua_State *L)
2383{
2384 int major = 0;
2385 int minor = 0;
2386 int patch = 0;
2387 char s[16];
2388
2389 sscanf(LUAVIM_VERSION, "Lua %d.%d.%d", &major, &minor, &patch);
2390 vim_snprintf(s, sizeof(s), "%d.%d.%d", major, minor, patch);
2391 lua_pushstring(L, s);
2392 return 0;
2393}
2394
Bram Moolenaareb04f082020-05-17 14:32:35 +02002395#define LUA_VIM_FN_CODE \
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002396 "vim.fn = setmetatable({}, {\n"\
2397 " __index = function (t, key)\n"\
2398 " local function _fn(...)\n"\
2399 " return vim.call(key, ...)\n"\
2400 " end\n"\
2401 " t[key] = _fn\n"\
2402 " return _fn\n"\
2403 " end\n"\
2404 " })"
2405
2406#define LUA_VIM_UPDATE_PACKAGE_PATHS \
2407 "local last_vim_paths = {}\n"\
2408 "vim._update_package_paths = function ()\n"\
2409 " local cur_vim_paths = {}\n"\
2410 " local function split(s, delimiter)\n"\
2411 " result = {}\n"\
2412 " for match in (s..delimiter):gmatch(\"(.-)\"..delimiter) do\n"\
2413 " table.insert(result, match)\n"\
2414 " end\n"\
2415 " return result\n"\
2416 " end\n"\
2417 " local rtps = split(vim.eval('&runtimepath'), ',')\n"\
2418 " local sep = package.config:sub(1, 1)\n"\
2419 " for _, key in ipairs({'path', 'cpath'}) do\n"\
2420 " local orig_str = package[key] .. ';'\n"\
2421 " local pathtrails_ordered = {}\n"\
2422 " -- Note: ignores trailing item without trailing `;`. Not using something\n"\
2423 " -- simpler in order to preserve empty items (stand for default path).\n"\
2424 " local orig = {}\n"\
2425 " for s in orig_str:gmatch('[^;]*;') do\n"\
2426 " s = s:sub(1, -2) -- Strip trailing semicolon\n"\
2427 " orig[#orig + 1] = s\n"\
2428 " end\n"\
2429 " if key == 'path' then\n"\
2430 " -- /?.lua and /?/init.lua\n"\
2431 " pathtrails_ordered = {sep .. '?.lua', sep .. '?' .. sep .. 'init.lua'}\n"\
2432 " else\n"\
2433 " local pathtrails = {}\n"\
2434 " for _, s in ipairs(orig) do\n"\
2435 " -- Find out path patterns. pathtrail should contain something like\n"\
2436 " -- /?.so, \?.dll. This allows not to bother determining what correct\n"\
2437 " -- suffixes are.\n"\
2438 " local pathtrail = s:match('[/\\\\][^/\\\\]*%?.*$')\n"\
2439 " if pathtrail and not pathtrails[pathtrail] then\n"\
2440 " pathtrails[pathtrail] = true\n"\
2441 " pathtrails_ordered[#pathtrails_ordered + 1] = pathtrail\n"\
2442 " end\n"\
2443 " end\n"\
2444 " end\n"\
2445 " local new = {}\n"\
2446 " for _, rtp in ipairs(rtps) do\n"\
2447 " if not rtp:match(';') then\n"\
2448 " for _, pathtrail in pairs(pathtrails_ordered) do\n"\
2449 " local new_path = rtp .. sep .. 'lua' .. pathtrail\n"\
2450 " -- Always keep paths from &runtimepath at the start:\n"\
2451 " -- append them here disregarding orig possibly containing one of them.\n"\
2452 " new[#new + 1] = new_path\n"\
2453 " cur_vim_paths[new_path] = true\n"\
2454 " end\n"\
2455 " end\n"\
2456 " end\n"\
2457 " for _, orig_path in ipairs(orig) do\n"\
2458 " -- Handle removing obsolete paths originating from &runtimepath: such\n"\
2459 " -- paths either belong to cur_nvim_paths and were already added above or\n"\
2460 " -- to last_nvim_paths and should not be added at all if corresponding\n"\
2461 " -- entry was removed from &runtimepath list.\n"\
2462 " if not (cur_vim_paths[orig_path] or last_vim_paths[orig_path]) then\n"\
2463 " new[#new + 1] = orig_path\n"\
2464 " end\n"\
2465 " end\n"\
2466 " package[key] = table.concat(new, ';')\n"\
2467 " end\n"\
2468 " last_vim_paths = cur_vim_paths\n"\
2469 "end"
Bram Moolenaareb04f082020-05-17 14:32:35 +02002470
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002471#define LUA_VIM_SETUP_VARIABLE_DICTS \
2472 "do\n"\
2473 " local function make_dict_accessor(scope)\n"\
2474 " local mt = {}\n"\
2475 " function mt:__newindex(k, v)\n"\
2476 " return vim._setvar(scope, 0, k, v)\n"\
2477 " end\n"\
2478 " function mt:__index(k)\n"\
2479 " return vim._getvar(scope, 0, k)\n"\
2480 " end\n"\
2481 " return setmetatable({}, mt)\n"\
2482 " end\n"\
2483 " vim.g = make_dict_accessor('g')\n"\
2484 " vim.v = make_dict_accessor('v')\n"\
2485 " vim.b = make_dict_accessor('b')\n"\
2486 " vim.w = make_dict_accessor('w')\n"\
2487 " vim.t = make_dict_accessor('t')\n"\
2488 "end"
2489
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002490 static int
2491luaopen_vim(lua_State *L)
2492{
Jesse Pavel8a350332023-08-13 22:05:45 -04002493 lua_newtable(L); // cache table
2494 lua_newtable(L); // cache table's metatable
Bram Moolenaar1dced572012-04-05 16:54:08 +02002495 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002496 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002497 lua_setmetatable(L, -2); // cache is weak-valued
Jesse Pavel8a350332023-08-13 22:05:45 -04002498 // put the cache table in the registry for luaV_get/setudata()
2499 lua_pushlightuserdata(L, (void *) LUAVIM_UDATA_CACHE);
2500 lua_pushvalue(L, -2);
2501 lua_rawset(L, LUA_REGISTRYINDEX);
2502 lua_pop(L, 1); // we don't need the cache table here anymore
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002503 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002504 lua_pushcfunction(L, luaV_print);
2505 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002506 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002507 lua_getglobal(L, "debug");
2508 lua_pushcfunction(L, luaV_debug);
2509 lua_setfield(L, -2, "debug");
2510 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002511 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002512 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Jesse Pavel8a350332023-08-13 22:05:45 -04002513 lua_pushcfunction(L, luaV_free);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002514 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002515 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002516 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Jesse Pavel8a350332023-08-13 22:05:45 -04002517 lua_pushcfunction(L, luaV_luaeval);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002518 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002519 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002520 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Jesse Pavel8a350332023-08-13 22:05:45 -04002521 lua_pushcfunction(L, luaV_setref);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002522 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002523 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002524 luaV_newmetatable(L, LUAVIM_LIST);
Jesse Pavel8a350332023-08-13 22:05:45 -04002525 luaV_register(L, luaV_List_mt);
2526 lua_pop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002527 luaV_newmetatable(L, LUAVIM_DICT);
Jesse Pavel8a350332023-08-13 22:05:45 -04002528 luaV_register(L, luaV_Dict_mt);
2529 lua_pop(L, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002530 luaV_newmetatable(L, LUAVIM_BLOB);
Jesse Pavel8a350332023-08-13 22:05:45 -04002531 luaV_register(L, luaV_Blob_mt);
2532 lua_pop(L, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002533 luaV_newmetatable(L, LUAVIM_FUNCREF);
Jesse Pavel8a350332023-08-13 22:05:45 -04002534 luaV_register(L, luaV_Funcref_mt);
2535 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002536 luaV_newmetatable(L, LUAVIM_BUFFER);
Jesse Pavel8a350332023-08-13 22:05:45 -04002537 luaV_register(L, luaV_Buffer_mt);
2538 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002539 luaV_newmetatable(L, LUAVIM_WINDOW);
Jesse Pavel8a350332023-08-13 22:05:45 -04002540 luaV_register(L, luaV_Window_mt);
2541 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002542 lua_newtable(L); // vim table
Jesse Pavel8a350332023-08-13 22:05:45 -04002543 luaV_register(L, luaV_module);
Bram Moolenaar125ed272021-04-07 20:11:12 +02002544 luaV_pushversion(L);
2545 lua_setfield(L, -2, "lua_version");
Bram Moolenaar1dced572012-04-05 16:54:08 +02002546 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaareb04f082020-05-17 14:32:35 +02002547 // custom code
Bram Moolenaar9309eb22020-05-17 16:53:56 +02002548 (void)luaL_dostring(L, LUA_VIM_FN_CODE);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002549 (void)luaL_dostring(L, LUA_VIM_UPDATE_PACKAGE_PATHS);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002550 (void)luaL_dostring(L, LUA_VIM_SETUP_VARIABLE_DICTS);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002551
Jesse Pavel8a350332023-08-13 22:05:45 -04002552 lua_getglobal(L, LUAVIM_NAME);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002553 lua_getfield(L, -1, "_update_package_paths");
2554
2555 if (lua_pcall(L, 0, 0, 0))
2556 luaV_emsg(L);
2557
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002558 return 0;
2559}
2560
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002561 static lua_State *
2562luaV_newstate(void)
2563{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002564 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002565 luaL_openlibs(L); // core libs
2566 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002567 lua_call(L, 0, 0);
2568 return L;
2569}
2570
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002571 static void
2572luaV_setrange(lua_State *L, int line1, int line2)
2573{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002574 lua_getglobal(L, LUAVIM_NAME);
2575 lua_pushinteger(L, line1);
2576 lua_setfield(L, -2, "firstline");
2577 lua_pushinteger(L, line2);
2578 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002579 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002580}
2581
2582
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002583// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002584
2585static lua_State *L = NULL;
2586
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002587 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002588lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002589{
2590 return L != NULL;
2591}
2592
2593 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002594lua_init(void)
2595{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002596 if (lua_isopen())
2597 return OK;
2598
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002599#ifdef DYNAMIC_LUA
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002600 if (!lua_enabled(TRUE))
2601 {
2602 emsg(_("Lua library cannot be loaded."));
2603 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002604 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002605#endif
2606 L = luaV_newstate();
2607
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002608 return OK;
2609}
2610
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002611 void
2612lua_end(void)
2613{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002614 if (!lua_isopen())
2615 return;
2616
2617 lua_close(L);
2618 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002619}
2620
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002621/*
2622 * ex commands
2623 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002624 void
2625ex_lua(exarg_T *eap)
2626{
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002627 char *script = (char *)script_get(eap, eap->arg);
2628
2629 if (!eap->skip && lua_init() == OK)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002630 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002631 char *s = script != NULL ? script : (char *)eap->arg;
2632
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002633 luaV_setrange(L, eap->line1, eap->line2);
2634 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2635 || lua_pcall(L, 0, 0, 0))
2636 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002637 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002638 if (script != NULL)
2639 vim_free(script);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002640}
2641
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002642 void
2643ex_luado(exarg_T *eap)
2644{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002645 linenr_T l;
2646 const char *s = (const char *) eap->arg;
2647 luaL_Buffer b;
2648 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002649 buf_T *was_curbuf = curbuf;
2650
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002651 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002652 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2653 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002654 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002655 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002656 }
2657 luaV_setrange(L, eap->line1, eap->line2);
2658 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002659 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002660 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002661 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002662 luaL_pushresult(&b);
2663 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002664 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2665 {
2666 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002667 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002668 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002669 }
2670 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002671 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002672 for (l = eap->line1; l <= eap->line2; l++)
2673 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02002674 // Check the line number, the command may have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002675 if (l > curbuf->b_ml.ml_line_count)
2676 break;
2677
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002678 lua_pushvalue(L, -1); // function
2679 luaV_pushline(L, curbuf, l); // current line as arg
2680 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002681 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002682 {
2683 luaV_emsg(L);
2684 break;
2685 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002686 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002687 if (curbuf != was_curbuf)
2688 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002689 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002690 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002691#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002692 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002693#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002694 ml_replace(l, luaV_toline(L, -1), TRUE);
2695 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002696 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002697 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002698 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002699 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002700 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002701 check_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002702 update_screen(UPD_NOT_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002703}
2704
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002705 void
2706ex_luafile(exarg_T *eap)
2707{
2708 if (lua_init() == FAIL)
2709 return;
2710 if (!eap->skip)
2711 {
2712 luaV_setrange(L, eap->line1, eap->line2);
2713 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2714 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002715 }
2716}
2717
Bram Moolenaar1dced572012-04-05 16:54:08 +02002718#define luaV_freetype(typ,tname) \
2719 void \
2720 lua_##tname##_free(typ *o) \
2721 { \
2722 if (!lua_isopen()) return; \
2723 luaV_getfield(L, LUAVIM_FREE); \
2724 lua_pushlightuserdata(L, (void *) o); \
2725 lua_call(L, 1, 0); \
2726 }
2727
2728luaV_freetype(buf_T, buffer)
2729luaV_freetype(win_T, window)
2730
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002731 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002732do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002733{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002734 lua_init();
2735 luaV_getfield(L, LUAVIM_LUAEVAL);
2736 lua_pushstring(L, (char *) str);
2737 lua_pushlightuserdata(L, (void *) arg);
2738 lua_pushlightuserdata(L, (void *) rettv);
2739 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002740}
2741
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002742 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002743set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002744{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002745 int aborted = 0;
2746
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002747 if (!lua_isopen())
2748 return 0;
2749
2750 luaV_getfield(L, LUAVIM_SETREF);
2751 // call the function with 1 arg, getting 1 result back
2752 lua_pushinteger(L, copyID);
2753 lua_call(L, 1, 1);
2754 // get the result
2755 aborted = lua_tointeger(L, -1);
2756 // pop result off the stack
2757 lua_pop(L, 1);
2758
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002759 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002760}
2761
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002762 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002763update_package_paths_in_lua(void)
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002764{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002765 if (!lua_isopen())
2766 return;
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002767
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002768 lua_getglobal(L, "vim");
2769 lua_getfield(L, -1, "_update_package_paths");
2770
2771 if (lua_pcall(L, 0, 0, 0))
2772 luaV_emsg(L);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002773}
2774
Bram Moolenaar801ab062020-06-25 19:27:56 +02002775/*
2776 * Native C function callback
2777 */
2778 static int
2779luaV_call_lua_func(
2780 int argcount,
2781 typval_T *argvars,
2782 typval_T *rettv,
2783 void *state)
2784{
2785 int i;
2786 int luaargcount = argcount;
2787 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2788 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2789
2790 if (funcstate->lua_tableref != LUA_NOREF)
2791 {
2792 // First arg for metatable __call method is a table
2793 luaargcount += 1;
2794 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2795 }
2796
2797 for (i = 0; i < argcount; ++i)
2798 luaV_pushtypval(funcstate->L, &argvars[i]);
2799
2800 if (lua_pcall(funcstate->L, luaargcount, 1, 0))
2801 {
2802 luaV_emsg(funcstate->L);
Bram Moolenaar0917e862023-02-18 14:42:44 +00002803 return (int)FCERR_OTHER;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002804 }
2805
2806 luaV_checktypval(funcstate->L, -1, rettv, "get return value");
Bram Moolenaar0917e862023-02-18 14:42:44 +00002807 return (int)FCERR_NONE;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002808}
2809
2810/*
2811 * Free up any lua references held by the func state.
2812 */
2813 static void
2814luaV_call_lua_func_free(void *state)
2815{
2816 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2817 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2818 funcstate->L = NULL;
2819 if (funcstate->lua_tableref != LUA_NOREF)
2820 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2821 VIM_CLEAR(funcstate);
2822}
2823
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002824#endif