blob: 2041f5bccc178c16df4ad4047925aba2ef97a624 [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
Bram Moolenaar0ba04292010-07-14 23:23:17 +020076#define luaV_getfield(L, s) \
77 lua_pushlightuserdata((L), (void *)(s)); \
78 lua_rawget((L), LUA_REGISTRYINDEX)
79#define luaV_checksandbox(L) \
80 if (sandbox) luaL_error((L), "not allowed in sandbox")
81#define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
82#define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
Bram Moolenaarca06da92018-07-01 15:12:05 +020083#define luaV_checktypval(L, a, v, msg) \
84 do { \
Bram Moolenaar801ab062020-06-25 19:27:56 +020085 if (luaV_totypval(L, a, v) == FAIL) \
Bram Moolenaarca06da92018-07-01 15:12:05 +020086 luaL_error(L, msg ": cannot convert value"); \
87 } while (0)
Bram Moolenaar0ba04292010-07-14 23:23:17 +020088
Bram Moolenaarca06da92018-07-01 15:12:05 +020089static luaV_List *luaV_pushlist(lua_State *L, list_T *lis);
90static luaV_Dict *luaV_pushdict(lua_State *L, dict_T *dic);
Bram Moolenaarb7828692019-03-23 13:57:02 +010091static luaV_Blob *luaV_pushblob(lua_State *L, blob_T *blo);
Bram Moolenaar4eefe472019-03-19 21:59:19 +010092static luaV_Funcref *luaV_pushfuncref(lua_State *L, char_u *name);
Bram Moolenaar801ab062020-06-25 19:27:56 +020093static int luaV_call_lua_func(int argcount, typval_T *argvars, typval_T *rettv, void *state);
94static void luaV_call_lua_func_free(void *state);
Bram Moolenaar1dced572012-04-05 16:54:08 +020095
96#if LUA_VERSION_NUM <= 501
Jesse Pavel8a350332023-08-13 22:05:45 -040097#define luaV_register(L, l) luaL_register(L, NULL, l)
Bram Moolenaar1dced572012-04-05 16:54:08 +020098#define luaL_typeerror luaL_typerror
99#else
Jesse Pavel8a350332023-08-13 22:05:45 -0400100#define luaV_register(L, l) luaL_setfuncs(L, l, 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200101#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200102
103#ifdef DYNAMIC_LUA
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200104
Bram Moolenaar4f974752019-02-17 17:44:42 +0100105#ifndef MSWIN
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200106# include <dlfcn.h>
107# define HANDLE void*
108# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
109# define symbol_from_dll dlsym
110# define close_dll dlclose
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200111# define load_dll_error dlerror
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200112#else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200113# define load_dll vimLoadLib
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200114# define symbol_from_dll GetProcAddress
115# define close_dll FreeLibrary
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200116# define load_dll_error GetWin32Error
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200117#endif
118
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100119// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200120#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200121#define luaL_register dll_luaL_register
Bram Moolenaar1dced572012-04-05 16:54:08 +0200122#define luaL_prepbuffer dll_luaL_prepbuffer
123#define luaL_openlib dll_luaL_openlib
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200124#define luaL_typerror dll_luaL_typerror
Bram Moolenaar1dced572012-04-05 16:54:08 +0200125#define luaL_loadfile dll_luaL_loadfile
126#define luaL_loadbuffer dll_luaL_loadbuffer
127#else
128#define luaL_prepbuffsize dll_luaL_prepbuffsize
129#define luaL_setfuncs dll_luaL_setfuncs
130#define luaL_loadfilex dll_luaL_loadfilex
131#define luaL_loadbufferx dll_luaL_loadbufferx
132#define luaL_argerror dll_luaL_argerror
133#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200134#if LUA_VERSION_NUM >= 504
135#define luaL_typeerror dll_luaL_typeerror
136#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200137#define luaL_checkany dll_luaL_checkany
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200138#define luaL_checklstring dll_luaL_checklstring
139#define luaL_checkinteger dll_luaL_checkinteger
140#define luaL_optinteger dll_luaL_optinteger
141#define luaL_checktype dll_luaL_checktype
142#define luaL_error dll_luaL_error
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200143#define luaL_newstate dll_luaL_newstate
144#define luaL_buffinit dll_luaL_buffinit
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200145#define luaL_addlstring dll_luaL_addlstring
146#define luaL_pushresult dll_luaL_pushresult
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200147#define luaL_loadstring dll_luaL_loadstring
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200148#define luaL_ref dll_luaL_ref
149#define luaL_unref dll_luaL_unref
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100150// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200151#if LUA_VERSION_NUM <= 501
152#define lua_tonumber dll_lua_tonumber
153#define lua_tointeger dll_lua_tointeger
154#define lua_call dll_lua_call
155#define lua_pcall dll_lua_pcall
156#else
157#define lua_tonumberx dll_lua_tonumberx
158#define lua_tointegerx dll_lua_tointegerx
159#define lua_callk dll_lua_callk
160#define lua_pcallk dll_lua_pcallk
161#define lua_getglobal dll_lua_getglobal
162#define lua_setglobal dll_lua_setglobal
Bram Moolenaar1dced572012-04-05 16:54:08 +0200163#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200164#if LUA_VERSION_NUM <= 502
165#define lua_replace dll_lua_replace
166#define lua_remove dll_lua_remove
167#endif
168#if LUA_VERSION_NUM >= 503
169#define lua_rotate dll_lua_rotate
170#define lua_copy dll_lua_copy
171#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200172#define lua_typename dll_lua_typename
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200173#define lua_close dll_lua_close
174#define lua_gettop dll_lua_gettop
175#define lua_settop dll_lua_settop
176#define lua_pushvalue dll_lua_pushvalue
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200177#define lua_isnumber dll_lua_isnumber
178#define lua_isstring dll_lua_isstring
179#define lua_type dll_lua_type
180#define lua_rawequal dll_lua_rawequal
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200181#define lua_toboolean dll_lua_toboolean
182#define lua_tolstring dll_lua_tolstring
183#define lua_touserdata dll_lua_touserdata
184#define lua_pushnil dll_lua_pushnil
185#define lua_pushnumber dll_lua_pushnumber
186#define lua_pushinteger dll_lua_pushinteger
187#define lua_pushlstring dll_lua_pushlstring
188#define lua_pushstring dll_lua_pushstring
189#define lua_pushfstring dll_lua_pushfstring
190#define lua_pushcclosure dll_lua_pushcclosure
191#define lua_pushboolean dll_lua_pushboolean
192#define lua_pushlightuserdata dll_lua_pushlightuserdata
193#define lua_getfield dll_lua_getfield
194#define lua_rawget dll_lua_rawget
Bram Moolenaar1dced572012-04-05 16:54:08 +0200195#define lua_rawgeti dll_lua_rawgeti
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200196#define lua_createtable dll_lua_createtable
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +0200197#define lua_settable dll_lua_settable
Bram Moolenaar830e3582018-08-21 14:23:35 +0200198#if LUA_VERSION_NUM >= 504
199 #define lua_newuserdatauv dll_lua_newuserdatauv
200#else
201 #define lua_newuserdata dll_lua_newuserdata
202#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200203#define lua_getmetatable dll_lua_getmetatable
204#define lua_setfield dll_lua_setfield
205#define lua_rawset dll_lua_rawset
206#define lua_rawseti dll_lua_rawseti
207#define lua_setmetatable dll_lua_setmetatable
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200208#define lua_next dll_lua_next
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100209// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200210#define luaopen_base dll_luaopen_base
211#define luaopen_table dll_luaopen_table
212#define luaopen_string dll_luaopen_string
213#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200214#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200215#define luaopen_os dll_luaopen_os
216#define luaopen_package dll_luaopen_package
217#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200218#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200219
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100220// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200221#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200222void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200223char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
224void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200225int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200226int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
227int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
228#else
229char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
230void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
231int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
232int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
233int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
234#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200235#if LUA_VERSION_NUM >= 504
236int (*dll_luaL_typeerror) (lua_State *L, int narg, const char *tname);
237#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200238void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200239const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
240lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
241lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
242void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
243int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200244lua_State *(*dll_luaL_newstate) (void);
245void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200246void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
247void (*dll_luaL_pushresult) (luaL_Buffer *B);
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200248int (*dll_luaL_loadstring) (lua_State *L, const char *s);
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200249int (*dll_luaL_ref) (lua_State *L, int idx);
250#if LUA_VERSION_NUM <= 502
251void (*dll_luaL_unref) (lua_State *L, int idx, int n);
252#else
253void (*dll_luaL_unref) (lua_State *L, int idx, lua_Integer n);
254#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100255// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200256#if LUA_VERSION_NUM <= 501
257lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
258lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
259void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
260int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
261#else
262lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
263lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
264void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
Bram Moolenaardb913952012-06-29 12:54:53 +0200265 lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200266int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
Bram Moolenaardb913952012-06-29 12:54:53 +0200267 int ctx, lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200268void (*dll_lua_getglobal) (lua_State *L, const char *var);
269void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200270#endif
271#if LUA_VERSION_NUM <= 502
272void (*dll_lua_replace) (lua_State *L, int idx);
273void (*dll_lua_remove) (lua_State *L, int idx);
274#endif
275#if LUA_VERSION_NUM >= 503
276void (*dll_lua_rotate) (lua_State *L, int idx, int n);
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200277void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200278#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200279const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200280void (*dll_lua_close) (lua_State *L);
281int (*dll_lua_gettop) (lua_State *L);
282void (*dll_lua_settop) (lua_State *L, int idx);
283void (*dll_lua_pushvalue) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200284int (*dll_lua_isnumber) (lua_State *L, int idx);
285int (*dll_lua_isstring) (lua_State *L, int idx);
286int (*dll_lua_type) (lua_State *L, int idx);
287int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200288int (*dll_lua_toboolean) (lua_State *L, int idx);
289const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
290void *(*dll_lua_touserdata) (lua_State *L, int idx);
291void (*dll_lua_pushnil) (lua_State *L);
292void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
293void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
294void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
295void (*dll_lua_pushstring) (lua_State *L, const char *s);
296const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
297void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
298void (*dll_lua_pushboolean) (lua_State *L, int b);
299void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
300void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
Bram Moolenaar17413672018-07-14 20:49:42 +0200301#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200302void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200303void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200304#else
305int (*dll_lua_rawget) (lua_State *L, int idx);
306int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
307#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200308void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +0200309void (*dll_lua_settable) (lua_State *L, int idx);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200310#if LUA_VERSION_NUM >= 504
311void *(*dll_lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
312#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200313void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200314#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200315int (*dll_lua_getmetatable) (lua_State *L, int objindex);
316void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
317void (*dll_lua_rawset) (lua_State *L, int idx);
Bram Moolenaar17413672018-07-14 20:49:42 +0200318#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200319void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200320#else
321void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n);
322#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200323int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200324int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100325// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200326int (*dll_luaopen_base) (lua_State *L);
327int (*dll_luaopen_table) (lua_State *L);
328int (*dll_luaopen_string) (lua_State *L);
329int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200330int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200331int (*dll_luaopen_os) (lua_State *L);
332int (*dll_luaopen_package) (lua_State *L);
333int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200334void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200335
336typedef void **luaV_function;
337typedef struct {
338 const char *name;
339 luaV_function func;
340} luaV_Reg;
341
342static const luaV_Reg luaV_dll[] = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100343 // lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200344#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200345 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200346 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
347 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200348 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200349 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
350 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
351#else
352 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
353 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
354 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
355 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
356 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
357#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200358#if LUA_VERSION_NUM >= 504
359 {"luaL_typeerror", (luaV_function) &dll_luaL_typeerror},
360#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200361 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200362 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
363 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
364 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
365 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
366 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200367 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
368 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200369 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
370 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200371 {"luaL_loadstring", (luaV_function) &dll_luaL_loadstring},
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200372 {"luaL_ref", (luaV_function) &dll_luaL_ref},
373 {"luaL_unref", (luaV_function) &dll_luaL_unref},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100374 // lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200375#if LUA_VERSION_NUM <= 501
376 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
377 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
378 {"lua_call", (luaV_function) &dll_lua_call},
379 {"lua_pcall", (luaV_function) &dll_lua_pcall},
380#else
381 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
382 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
383 {"lua_callk", (luaV_function) &dll_lua_callk},
384 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
385 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
386 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200387#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200388#if LUA_VERSION_NUM <= 502
389 {"lua_replace", (luaV_function) &dll_lua_replace},
390 {"lua_remove", (luaV_function) &dll_lua_remove},
391#endif
392#if LUA_VERSION_NUM >= 503
393 {"lua_rotate", (luaV_function) &dll_lua_rotate},
394 {"lua_copy", (luaV_function) &dll_lua_copy},
395#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200396 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200397 {"lua_close", (luaV_function) &dll_lua_close},
398 {"lua_gettop", (luaV_function) &dll_lua_gettop},
399 {"lua_settop", (luaV_function) &dll_lua_settop},
400 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200401 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
402 {"lua_isstring", (luaV_function) &dll_lua_isstring},
403 {"lua_type", (luaV_function) &dll_lua_type},
404 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200405 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
406 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
407 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
408 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
409 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
410 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
411 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
412 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
413 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
414 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
415 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
416 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
417 {"lua_getfield", (luaV_function) &dll_lua_getfield},
418 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200419 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200420 {"lua_createtable", (luaV_function) &dll_lua_createtable},
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +0200421 {"lua_settable", (luaV_function) &dll_lua_settable},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200422#if LUA_VERSION_NUM >= 504
423 {"lua_newuserdatauv", (luaV_function) &dll_lua_newuserdatauv},
424#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200425 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200426#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200427 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
428 {"lua_setfield", (luaV_function) &dll_lua_setfield},
429 {"lua_rawset", (luaV_function) &dll_lua_rawset},
430 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
431 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200432 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100433 // libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200434 {"luaopen_base", (luaV_function) &dll_luaopen_base},
435 {"luaopen_table", (luaV_function) &dll_luaopen_table},
436 {"luaopen_string", (luaV_function) &dll_luaopen_string},
437 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200438 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200439 {"luaopen_os", (luaV_function) &dll_luaopen_os},
440 {"luaopen_package", (luaV_function) &dll_luaopen_package},
441 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200442 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200443 {NULL, NULL}
444};
445
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200446static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200447
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200448 static int
449lua_link_init(char *libname, int verbose)
450{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200451 const luaV_Reg *reg;
452 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200453 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200454 if (!hinstLua)
455 {
456 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000457 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200458 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200459 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200460 for (reg = luaV_dll; reg->func; reg++)
461 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200462 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
463 {
464 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200465 hinstLua = 0;
466 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000467 semsg(_(e_could_not_load_library_function_str), reg->name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200468 return FAIL;
469 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200470 }
471 return OK;
472}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100473#endif // DYNAMIC_LUA
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200474
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200475#if defined(DYNAMIC_LUA) || defined(PROTO)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200476 int
477lua_enabled(int verbose)
478{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100479 return lua_link_init((char *)p_luadll, verbose) == OK;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200480}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200481#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200482
Bram Moolenaar5551b132020-07-14 21:54:28 +0200483#if LUA_VERSION_NUM > 501 && LUA_VERSION_NUM < 504
Bram Moolenaar1dced572012-04-05 16:54:08 +0200484 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100485luaL_typeerror(lua_State *L, int narg, const char *tname)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200486{
487 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200488 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200489 return luaL_argerror(L, narg, msg);
490}
491#endif
492
zeertzjq4112aca2023-08-16 07:29:28 +0800493 static LUAV_INLINE void
494luaV_getudata(lua_State *L, void *v)
495{
496 lua_pushlightuserdata(L, (void *) LUAVIM_UDATA_CACHE);
497 lua_rawget(L, LUA_REGISTRYINDEX); // now the cache table is at the top of the stack
498 lua_pushlightuserdata(L, v);
499 lua_rawget(L, -2);
500 lua_remove(L, -2); // remove the cache table from the stack
501}
502
503 static LUAV_INLINE void
504luaV_setudata(lua_State *L, void *v)
505{
506 lua_pushlightuserdata(L, (void *) LUAVIM_UDATA_CACHE);
507 lua_rawget(L, LUA_REGISTRYINDEX); // cache table is at -1
508 lua_pushlightuserdata(L, v); // ...now at -2
509 lua_pushvalue(L, -3); // copy the userdata (cache at -3)
510 lua_rawset(L, -3); // consumes two stack items
511 lua_pop(L, 1); // and remove the cache table
512}
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200513
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100514// ======= Internal =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200515
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200516 static void
517luaV_newmetatable(lua_State *L, const char *tname)
518{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200519 lua_newtable(L);
520 lua_pushlightuserdata(L, (void *) tname);
521 lua_pushvalue(L, -2);
522 lua_rawset(L, LUA_REGISTRYINDEX);
523}
524
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200525 static void *
526luaV_toudata(lua_State *L, int ud, const char *tname)
527{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200528 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200529
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000530 if (p == NULL)
531 return NULL;
532
533 // value is userdata
534 if (lua_getmetatable(L, ud)) // does it have a metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200535 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000536 luaV_getfield(L, tname); // get metatable
537 if (lua_rawequal(L, -1, -2)) // MTs match?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200538 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000539 lua_pop(L, 2); // MTs
540 return p;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200541 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200542 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000543
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200544 return NULL;
545}
546
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200547 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200548luaV_checkcache(lua_State *L, void *p)
549{
550 luaV_getudata(L, p);
551 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
552 lua_pop(L, 1);
553 return p;
554}
555
556#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
557
558#define luaV_checkvalid(L,luatyp,ud) \
559 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
560
561 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200562luaV_checkudata(lua_State *L, int ud, const char *tname)
563{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200564 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200565 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200566 return p;
567}
568
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200569 static void
570luaV_pushtypval(lua_State *L, typval_T *tv)
571{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200572 if (tv == NULL)
573 {
574 lua_pushnil(L);
575 return;
576 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200577 switch (tv->v_type)
578 {
579 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200580 lua_pushstring(L, tv->vval.v_string == NULL
581 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200582 break;
583 case VAR_NUMBER:
584 lua_pushinteger(L, (int) tv->vval.v_number);
585 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200586 case VAR_FLOAT:
587 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
588 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200589 case VAR_LIST:
590 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200591 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200592 case VAR_DICT:
593 luaV_pushdict(L, tv->vval.v_dict);
594 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100595 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100596 case VAR_SPECIAL:
597 if (tv->vval.v_number <= VVAL_TRUE)
598 lua_pushinteger(L, (int) tv->vval.v_number);
599 else
600 lua_pushnil(L);
601 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200602 case VAR_FUNC:
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100603 luaV_pushfuncref(L, tv->vval.v_string);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200604 break;
Bram Moolenaar86c3a212021-03-08 19:50:24 +0100605 case VAR_PARTIAL:
606 // TODO: handle partial arguments
607 luaV_pushfuncref(L, partial_name(tv->vval.v_partial));
608 break;
609
Bram Moolenaarb7828692019-03-23 13:57:02 +0100610 case VAR_BLOB:
611 luaV_pushblob(L, tv->vval.v_blob);
612 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200613 default:
614 lua_pushnil(L);
615 }
616}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200617
Bram Moolenaarca06da92018-07-01 15:12:05 +0200618/*
619 * Converts lua value at 'pos' to typval 'tv'.
620 * Returns OK or FAIL.
621 */
622 static int
623luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200624{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200625 int status = OK;
626
Bram Moolenaara9a8e5f2020-07-02 21:17:57 +0200627 tv->v_lock = 0;
628
Bram Moolenaarca06da92018-07-01 15:12:05 +0200629 switch (lua_type(L, pos))
630 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200631 case LUA_TBOOLEAN:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100632 tv->v_type = VAR_BOOL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200633 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
634 break;
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100635 case LUA_TNIL:
636 tv->v_type = VAR_SPECIAL;
637 tv->vval.v_number = VVAL_NULL;
638 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200639 case LUA_TSTRING:
640 tv->v_type = VAR_STRING;
641 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
642 break;
643 case LUA_TNUMBER:
Bram Moolenaareb04f082020-05-17 14:32:35 +0200644 {
645 const lua_Number n = lua_tonumber(L, pos);
646
647 if (n > (lua_Number)INT64_MAX || n < (lua_Number)INT64_MIN
648 || ((lua_Number)((varnumber_T)n)) != n)
649 {
650 tv->v_type = VAR_FLOAT;
651 tv->vval.v_float = (float_T)n;
652 }
653 else
654 {
655 tv->v_type = VAR_NUMBER;
656 tv->vval.v_number = (varnumber_T)n;
657 }
658 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200659 break;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200660 case LUA_TFUNCTION:
661 {
662 char_u *name;
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200663 luaV_CFuncState *state;
664
Bram Moolenaar801ab062020-06-25 19:27:56 +0200665 lua_pushvalue(L, pos);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200666 state = ALLOC_CLEAR_ONE(luaV_CFuncState);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200667 state->lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
668 state->L = L;
669 state->lua_tableref = LUA_NOREF;
670 name = register_cfunc(&luaV_call_lua_func,
671 &luaV_call_lua_func_free, state);
672 tv->v_type = VAR_FUNC;
673 tv->vval.v_string = vim_strsave(name);
674 break;
675 }
676 case LUA_TTABLE:
677 {
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200678 int lua_tableref;
679
Bram Moolenaar801ab062020-06-25 19:27:56 +0200680 lua_pushvalue(L, pos);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200681 lua_tableref = luaL_ref(L, LUA_REGISTRYINDEX);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000682 if (lua_getmetatable(L, pos))
683 {
Bram Moolenaar801ab062020-06-25 19:27:56 +0200684 lua_getfield(L, -1, LUA___CALL);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000685 if (lua_isfunction(L, -1))
686 {
Bram Moolenaar801ab062020-06-25 19:27:56 +0200687 char_u *name;
688 int lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
689 luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200690
Bram Moolenaar801ab062020-06-25 19:27:56 +0200691 state->lua_funcref = lua_funcref;
692 state->L = L;
693 state->lua_tableref = lua_tableref;
694 name = register_cfunc(&luaV_call_lua_func,
695 &luaV_call_lua_func_free, state);
696 tv->v_type = VAR_FUNC;
697 tv->vval.v_string = vim_strsave(name);
698 break;
699 }
700 }
701 tv->v_type = VAR_NUMBER;
702 tv->vval.v_number = 0;
703 status = FAIL;
704 break;
705 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200706 case LUA_TUSERDATA:
707 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200708 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200709
Bram Moolenaarb7828692019-03-23 13:57:02 +0100710 if (lua_getmetatable(L, pos)) // has metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200711 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100712 // check list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200713 luaV_getfield(L, LUAVIM_LIST);
714 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200715 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200716 tv->v_type = VAR_LIST;
717 tv->vval.v_list = *((luaV_List *) p);
718 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100719 lua_pop(L, 2); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200720 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200721 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100722 // check dict
Bram Moolenaar1dced572012-04-05 16:54:08 +0200723 luaV_getfield(L, LUAVIM_DICT);
724 if (lua_rawequal(L, -1, -3))
725 {
726 tv->v_type = VAR_DICT;
727 tv->vval.v_dict = *((luaV_Dict *) p);
728 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100729 lua_pop(L, 3); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200730 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200731 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100732 // check blob
733 luaV_getfield(L, LUAVIM_BLOB);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200734 if (lua_rawequal(L, -1, -4))
735 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100736 tv->v_type = VAR_BLOB;
737 tv->vval.v_blob = *((luaV_Blob *) p);
738 ++tv->vval.v_blob->bv_refcount;
739 lua_pop(L, 4); // MTs
740 break;
741 }
742 // check funcref
743 luaV_getfield(L, LUAVIM_FUNCREF);
744 if (lua_rawequal(L, -1, -5))
745 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200746 luaV_Funcref *f = (luaV_Funcref *) p;
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200747
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100748 func_ref(f->name);
749 tv->v_type = VAR_FUNC;
750 tv->vval.v_string = vim_strsave(f->name);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100751 lua_pop(L, 5); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200752 break;
753 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100754 lua_pop(L, 4); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200755 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200756 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100757 // FALLTHROUGH
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200758 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200759 tv->v_type = VAR_NUMBER;
760 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200761 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200762 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200763 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200764}
765
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100766/*
767 * similar to luaL_addlstring, but replaces \0 with \n if toline and
768 * \n with \0 otherwise
769 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200770 static void
771luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
772{
773 while (l--)
774 {
775 if (*s == '\0' && toline)
776 luaL_addchar(b, '\n');
777 else if (*s == '\n' && !toline)
778 luaL_addchar(b, '\0');
779 else
780 luaL_addchar(b, *s);
781 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200782 }
783}
784
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200785 static void
786luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
787{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200788 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
789 luaL_Buffer b;
790 luaL_buffinit(L, &b);
791 luaV_addlstring(&b, s, strlen(s), 0);
792 luaL_pushresult(&b);
793}
794
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200795 static char_u *
796luaV_toline(lua_State *L, int pos)
797{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200798 size_t l;
799 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200800
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200801 luaL_Buffer b;
802 luaL_buffinit(L, &b);
803 luaV_addlstring(&b, s, l, 1);
804 luaL_pushresult(&b);
805 return (char_u *) lua_tostring(L, -1);
806}
807
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100808/*
809 * pops a string s from the top of the stack and calls mf(t) for pieces t of
810 * s separated by newlines
811 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200812 static void
813luaV_msgfunc(lua_State *L, msgfunc_T mf)
814{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200815 luaL_Buffer b;
816 size_t l;
817 const char *p, *s = lua_tolstring(L, -1, &l);
818 luaL_buffinit(L, &b);
819 luaV_addlstring(&b, s, l, 0);
820 luaL_pushresult(&b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100821 // break string
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200822 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200823 while (l--)
824 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100825 if (*p++ == '\0') // break?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200826 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100827 mf((char *)s);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200828 s = p;
829 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200830 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100831 mf((char *)s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100832 lua_pop(L, 2); // original and modified strings
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200833}
834
Bram Moolenaar1dced572012-04-05 16:54:08 +0200835#define luaV_newtype(typ,tname,luatyp,luatname) \
836 static luatyp * \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100837 luaV_new##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200838 { \
839 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
840 *o = obj; \
841 luaV_setudata(L, obj); /* cache[obj] = udata */ \
842 luaV_getfield(L, luatname); \
843 lua_setmetatable(L, -2); \
844 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200845 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200846
847#define luaV_pushtype(typ,tname,luatyp) \
848 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200849 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200850 { \
851 luatyp *o = NULL; \
852 if (obj == NULL) \
853 lua_pushnil(L); \
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000854 else \
855 { \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200856 luaV_getudata(L, obj); \
857 if (lua_isnil(L, -1)) /* not interned? */ \
858 { \
859 lua_pop(L, 1); \
860 o = luaV_new##tname(L, obj); \
861 } \
862 else \
863 o = (luatyp *) lua_touserdata(L, -1); \
864 } \
865 return o; \
866 }
867
868#define luaV_type_tostring(tname,luatname) \
869 static int \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100870 luaV_##tname##_tostring(lua_State *L) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200871 { \
872 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
873 return 1; \
874 }
875
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100876// ======= List type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200877
878 static luaV_List *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100879luaV_newlist(lua_State *L, list_T *lis)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200880{
881 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
882 *l = lis;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100883 lis->lv_refcount++; // reference in Lua
884 luaV_setudata(L, lis); // cache[lis] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200885 luaV_getfield(L, LUAVIM_LIST);
886 lua_setmetatable(L, -2);
887 return l;
888}
889
890luaV_pushtype(list_T, list, luaV_List)
891luaV_type_tostring(list, LUAVIM_LIST)
892
893 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100894luaV_list_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200895{
896 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100897 lua_pushinteger(L, (int) list_len(l));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200898 return 1;
899}
900
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200901 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100902luaV_list_iter(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200903{
Jesse Pavel8a350332023-08-13 22:05:45 -0400904 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(1));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200905 if (li == NULL) return 0;
906 luaV_pushtypval(L, &li->li_tv);
907 lua_pushlightuserdata(L, (void *) li->li_next);
Jesse Pavel8a350332023-08-13 22:05:45 -0400908 lua_replace(L, lua_upvalueindex(1));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200909 return 1;
910}
911
912 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100913luaV_list_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200914{
915 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200916 lua_pushlightuserdata(L, (void *) l->lv_first);
Jesse Pavel8a350332023-08-13 22:05:45 -0400917 lua_pushcclosure(L, luaV_list_iter, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200918 return 1;
919}
920
921 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100922luaV_list_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200923{
924 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100925 if (lua_isnumber(L, 2)) // list item?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200926 {
Bram Moolenaarbd846172020-06-27 12:32:57 +0200927 long n = (long) luaL_checkinteger(L, 2);
928 listitem_T *li;
929
930 // Lua array index starts with 1 while Vim uses 0, subtract 1 to
931 // normalize.
932 n -= 1;
933 li = list_find(l, n);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200934 if (li == NULL)
935 lua_pushnil(L);
936 else
937 luaV_pushtypval(L, &li->li_tv);
938 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100939 else if (lua_isstring(L, 2)) // method?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200940 {
941 const char *s = lua_tostring(L, 2);
942 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200943 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200944 {
945 lua_getmetatable(L, 1);
946 lua_getfield(L, -1, s);
947 }
948 else
949 lua_pushnil(L);
950 }
951 else
952 lua_pushnil(L);
953 return 1;
954}
955
956 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100957luaV_list_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200958{
959 list_T *l = luaV_unbox(L, luaV_List, 1);
960 long n = (long) luaL_checkinteger(L, 2);
961 listitem_T *li;
Bram Moolenaarbd846172020-06-27 12:32:57 +0200962
963 // Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize.
964 n -= 1;
965
Bram Moolenaar1dced572012-04-05 16:54:08 +0200966 if (l->lv_lock)
967 luaL_error(L, "list is locked");
968 li = list_find(l, n);
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200969 if (li == NULL)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200970 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100971 if (!lua_isnil(L, 3))
972 {
973 typval_T v;
974 luaV_checktypval(L, 3, &v, "inserting list item");
975 if (list_insert_tv(l, &v, li) == FAIL)
976 luaL_error(L, "failed to add item to list");
977 clear_tv(&v);
978 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200979 }
980 else
981 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100982 if (lua_isnil(L, 3)) // remove?
983 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200984 vimlist_remove(l, li, li);
985 listitem_free(l, li);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100986 }
987 else
988 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200989 typval_T v;
990 luaV_checktypval(L, 3, &v, "setting list item");
991 clear_tv(&li->li_tv);
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200992 li->li_tv = v;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100993 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200994 }
995 return 0;
996}
997
998 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100999luaV_list_add(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001000{
1001 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
1002 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001003 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001004 if (l->lv_lock)
1005 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001006 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001007 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001008 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001009 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001010 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001011 lua_settop(L, 1);
1012 return 1;
1013}
1014
1015 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001016luaV_list_insert(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001017{
1018 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
1019 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +01001020 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001021 listitem_T *li = NULL;
1022 typval_T v;
1023 if (l->lv_lock)
1024 luaL_error(L, "list is locked");
1025 if (pos < l->lv_len)
1026 {
1027 li = list_find(l, pos);
1028 if (li == NULL)
1029 luaL_error(L, "invalid position");
1030 }
1031 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001032 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001033 if (list_insert_tv(l, &v, li) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001034 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001035 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001036 lua_settop(L, 1);
1037 return 1;
1038}
1039
1040static const luaL_Reg luaV_List_mt[] = {
1041 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001042 {"__len", luaV_list_len},
1043 {"__call", luaV_list_call},
1044 {"__index", luaV_list_index},
1045 {"__newindex", luaV_list_newindex},
1046 {"add", luaV_list_add},
1047 {"insert", luaV_list_insert},
1048 {NULL, NULL}
1049};
1050
1051
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001052// ======= Dict type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001053
1054 static luaV_Dict *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001055luaV_newdict(lua_State *L, dict_T *dic)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001056{
1057 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
1058 *d = dic;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001059 dic->dv_refcount++; // reference in Lua
1060 luaV_setudata(L, dic); // cache[dic] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +02001061 luaV_getfield(L, LUAVIM_DICT);
1062 lua_setmetatable(L, -2);
1063 return d;
1064}
1065
1066luaV_pushtype(dict_T, dict, luaV_Dict)
1067luaV_type_tostring(dict, LUAVIM_DICT)
1068
1069 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001070luaV_dict_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001071{
1072 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001073 lua_pushinteger(L, (int) dict_len(d));
Bram Moolenaar1dced572012-04-05 16:54:08 +02001074 return 1;
1075}
1076
1077 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001078luaV_dict_iter(lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001079{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001080#ifdef FEAT_EVAL
Jesse Pavel8a350332023-08-13 22:05:45 -04001081 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(1));
1082 int n = lua_tointeger(L, lua_upvalueindex(2));
Bram Moolenaar1dced572012-04-05 16:54:08 +02001083 dictitem_T *di;
1084 if (n <= 0) return 0;
1085 while (HASHITEM_EMPTY(hi)) hi++;
1086 di = dict_lookup(hi);
1087 lua_pushstring(L, (char *) hi->hi_key);
1088 luaV_pushtypval(L, &di->di_tv);
1089 lua_pushlightuserdata(L, (void *) (hi + 1));
Jesse Pavel8a350332023-08-13 22:05:45 -04001090 lua_replace(L, lua_upvalueindex(1));
Bram Moolenaar1dced572012-04-05 16:54:08 +02001091 lua_pushinteger(L, n - 1);
Jesse Pavel8a350332023-08-13 22:05:45 -04001092 lua_replace(L, lua_upvalueindex(2));
Bram Moolenaar1dced572012-04-05 16:54:08 +02001093 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001094#else
1095 return 0;
1096#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001097}
1098
1099 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001100luaV_dict_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001101{
1102 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1103 hashtab_T *ht = &d->dv_hashtab;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001104 lua_pushlightuserdata(L, (void *) ht->ht_array);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001105 lua_pushinteger(L, ht->ht_used); // # remaining items
Jesse Pavel8a350332023-08-13 22:05:45 -04001106 lua_pushcclosure(L, luaV_dict_iter, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001107 return 1;
1108}
1109
1110 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001111luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001112{
1113 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1114 char_u *key = (char_u *) luaL_checkstring(L, 2);
1115 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001116
Bram Moolenaar1dced572012-04-05 16:54:08 +02001117 if (di == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001118 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001119 lua_pushnil(L);
1120 return 1;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001121 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001122
1123 luaV_pushtypval(L, &di->di_tv);
1124 if (di->di_tv.v_type == VAR_FUNC) // funcref?
1125 {
1126 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
1127 f->self = d; // keep "self" reference
1128 d->dv_refcount++;
1129 }
1130
Bram Moolenaar1dced572012-04-05 16:54:08 +02001131 return 1;
1132}
1133
1134 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001135luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001136{
1137 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1138 char_u *key = (char_u *) luaL_checkstring(L, 2);
1139 dictitem_T *di;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001140 typval_T tv;
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001141
Bram Moolenaar1dced572012-04-05 16:54:08 +02001142 if (d->dv_lock)
1143 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001144 if (key == NULL)
1145 return 0;
1146 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001147 luaL_error(L, "empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001148 if (!lua_isnil(L, 3)) // read value?
Bram Moolenaar17413672018-07-14 20:49:42 +02001149 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001150 luaV_checktypval(L, 3, &tv, "setting dict item");
1151 if (d->dv_scope == VAR_DEF_SCOPE && tv.v_type == VAR_FUNC)
1152 {
1153 clear_tv(&tv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001154 luaL_error(L, "cannot assign funcref to builtin scope");
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001155 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001156 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001157 di = dict_find(d, key, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001158 if (di == NULL) // non-existing key?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001159 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001160 if (lua_isnil(L, 3))
1161 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001162 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001163 if (di == NULL)
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001164 {
1165 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001166 return 0;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001167 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001168 if (dict_add(d, di) == FAIL)
1169 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001170 vim_free(di);
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001171 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001172 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001173 }
1174 }
1175 else
1176 clear_tv(&di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001177 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001178 {
1179 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001180 hash_remove(&d->dv_hashtab, hi, "Lua new index");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001181 dictitem_free(di);
1182 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001183 else
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001184 di->di_tv = tv;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001185 return 0;
1186}
1187
1188static const luaL_Reg luaV_Dict_mt[] = {
1189 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001190 {"__len", luaV_dict_len},
1191 {"__call", luaV_dict_call},
1192 {"__index", luaV_dict_index},
1193 {"__newindex", luaV_dict_newindex},
1194 {NULL, NULL}
1195};
1196
1197
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001198// ======= Blob type =======
Bram Moolenaarb7828692019-03-23 13:57:02 +01001199
1200 static luaV_Blob *
1201luaV_newblob(lua_State *L, blob_T *blo)
1202{
1203 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1204 *b = blo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001205 blo->bv_refcount++; // reference in Lua
1206 luaV_setudata(L, blo); // cache[blo] = udata
Bram Moolenaarb7828692019-03-23 13:57:02 +01001207 luaV_getfield(L, LUAVIM_BLOB);
1208 lua_setmetatable(L, -2);
1209 return b;
1210}
1211
1212luaV_pushtype(blob_T, blob, luaV_Blob)
1213luaV_type_tostring(blob, LUAVIM_BLOB)
1214
1215 static int
1216luaV_blob_gc(lua_State *L)
1217{
1218 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1219 blob_unref(b);
1220 return 0;
1221}
1222
1223 static int
1224luaV_blob_len(lua_State *L)
1225{
1226 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1227 lua_pushinteger(L, (int) blob_len(b));
1228 return 1;
1229}
1230
1231 static int
1232luaV_blob_index(lua_State *L)
1233{
1234 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1235 if (lua_isnumber(L, 2))
1236 {
1237 int idx = luaL_checkinteger(L, 2);
1238 if (idx < blob_len(b))
1239 lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1240 else
1241 lua_pushnil(L);
1242 }
1243 else if (lua_isstring(L, 2))
1244 {
1245 const char *s = lua_tostring(L, 2);
1246 if (strncmp(s, "add", 3) == 0)
1247 {
1248 lua_getmetatable(L, 1);
1249 lua_getfield(L, -1, s);
1250 }
1251 else
1252 lua_pushnil(L);
1253 }
1254 else
1255 lua_pushnil(L);
1256 return 1;
1257}
1258
1259 static int
1260luaV_blob_newindex(lua_State *L)
1261{
1262 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1263 if (b->bv_lock)
1264 luaL_error(L, "blob is locked");
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001265
1266 if (!lua_isnumber(L, 2))
1267 return 0;
1268
1269 long len = blob_len(b);
1270 int idx = luaL_checkinteger(L, 2);
1271 int val = luaL_checkinteger(L, 3);
1272 if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
Bram Moolenaarb7828692019-03-23 13:57:02 +01001273 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001274 blob_set(b, idx, (char_u) val);
1275 if (idx == len)
1276 ++b->bv_ga.ga_len;
Bram Moolenaarb7828692019-03-23 13:57:02 +01001277 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001278 else
1279 luaL_error(L, "index out of range");
1280
Bram Moolenaarb7828692019-03-23 13:57:02 +01001281 return 0;
1282}
1283
1284 static int
1285luaV_blob_add(lua_State *L)
1286{
1287 luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1288 blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1289 if (b->bv_lock)
1290 luaL_error(L, "blob is locked");
1291 lua_settop(L, 2);
1292 if (!lua_isstring(L, 2))
1293 luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1294 else
1295 {
1296 size_t i, l = 0;
1297 const char *s = lua_tolstring(L, 2, &l);
1298
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001299 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001300 for (i = 0; i < l; ++i)
1301 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001302 }
1303 lua_settop(L, 1);
1304 return 1;
1305}
1306
1307static const luaL_Reg luaV_Blob_mt[] = {
1308 {"__tostring", luaV_blob_tostring},
1309 {"__gc", luaV_blob_gc},
1310 {"__len", luaV_blob_len},
1311 {"__index", luaV_blob_index},
1312 {"__newindex", luaV_blob_newindex},
1313 {"add", luaV_blob_add},
1314 {NULL, NULL}
1315};
1316
1317
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001318// ======= Funcref type =======
Bram Moolenaarca06da92018-07-01 15:12:05 +02001319
1320 static luaV_Funcref *
1321luaV_newfuncref(lua_State *L, char_u *name)
1322{
1323 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1324
1325 if (name != NULL)
1326 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001327 func_ref(name);
1328 f->name = vim_strsave(name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001329 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001330 f->self = NULL;
1331 luaV_getfield(L, LUAVIM_FUNCREF);
1332 lua_setmetatable(L, -2);
1333 return f;
1334}
1335
1336 static luaV_Funcref *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001337luaV_pushfuncref(lua_State *L, char_u *name)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001338{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001339 return luaV_newfuncref(L, name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001340}
1341
1342
1343luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1344
1345 static int
1346luaV_funcref_gc(lua_State *L)
1347{
1348 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1349
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001350 func_unref(f->name);
1351 vim_free(f->name);
1352 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1353 // will be (or has been already) freed by Vim's garbage collection.
Bram Moolenaarca06da92018-07-01 15:12:05 +02001354 return 0;
1355}
1356
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001357// equivalent to string(funcref)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001358 static int
1359luaV_funcref_len(lua_State *L)
1360{
1361 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1362
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001363 lua_pushstring(L, (const char *) f->name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001364 return 1;
1365}
1366
1367 static int
1368luaV_funcref_call(lua_State *L)
1369{
1370 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001371 int i, n = lua_gettop(L) - 1; // #args
1372 int status = FAIL;
1373 typval_T args;
1374 typval_T rettv;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001375
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001376 args.v_type = VAR_LIST;
1377 args.vval.v_list = list_alloc();
1378 rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1379 if (args.vval.v_list != NULL)
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001380 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001381 typval_T v;
1382
Bram Moolenaar17413672018-07-14 20:49:42 +02001383 for (i = 0; i < n; i++)
1384 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001385 luaV_checktypval(L, i + 2, &v, "calling funcref");
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001386 list_append_tv(args.vval.v_list, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001387 clear_tv(&v);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001388 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001389 status = func_call(f->name, &args, NULL, f->self, &rettv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001390 if (status == OK)
1391 luaV_pushtypval(L, &rettv);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001392 clear_tv(&args);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001393 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001394 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001395 if (status != OK)
1396 luaL_error(L, "cannot call funcref");
1397 return 1;
1398}
1399
1400static const luaL_Reg luaV_Funcref_mt[] = {
1401 {"__tostring", luaV_funcref_tostring},
1402 {"__gc", luaV_funcref_gc},
1403 {"__len", luaV_funcref_len},
1404 {"__call", luaV_funcref_call},
1405 {NULL, NULL}
1406};
1407
1408
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001409// ======= Buffer type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001410
1411luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1412luaV_pushtype(buf_T, buffer, luaV_Buffer)
1413luaV_type_tostring(buffer, LUAVIM_BUFFER)
1414
1415 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001416luaV_buffer_len(lua_State *L)
1417{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001418 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1419 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001420 return 1;
1421}
1422
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001423 static int
1424luaV_buffer_call(lua_State *L)
1425{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001426 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001427 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001428 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001429 return 1;
1430}
1431
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001432 static int
1433luaV_buffer_index(lua_State *L)
1434{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001435 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001436 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001437 if (n > 0 && n <= b->b_ml.ml_line_count)
1438 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001439 else if (lua_isstring(L, 2))
1440 {
1441 const char *s = lua_tostring(L, 2);
1442 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001443 lua_pushstring(L, (b->b_sfname == NULL)
1444 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001445 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001446 lua_pushstring(L, (b->b_ffname == NULL)
1447 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001448 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001449 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001450 // methods
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001451 else if (strncmp(s, "insert", 6) == 0
1452 || strncmp(s, "next", 4) == 0
1453 || strncmp(s, "previous", 8) == 0
1454 || strncmp(s, "isvalid", 7) == 0)
1455 {
1456 lua_getmetatable(L, 1);
1457 lua_getfield(L, -1, s);
1458 }
1459 else
1460 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001461 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001462 else
1463 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001464 return 1;
1465}
1466
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001467 static int
1468luaV_buffer_newindex(lua_State *L)
1469{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001470 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001471 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1472#ifdef HAVE_SANDBOX
1473 luaV_checksandbox(L);
1474#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001475 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001476 luaL_error(L, "invalid line number");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001477 if (lua_isnil(L, 3)) // delete line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001478 {
1479 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001480 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001481 if (u_savedel(n, 1L) == FAIL)
1482 {
1483 curbuf = buf;
1484 luaL_error(L, "cannot save undo information");
1485 }
Bram Moolenaarca70c072020-05-30 20:30:46 +02001486 else if (ml_delete(n) == FAIL)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001487 {
1488 curbuf = buf;
1489 luaL_error(L, "cannot delete line");
1490 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001491 else
1492 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001493 deleted_lines_mark(n, 1L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001494 if (b == curwin->w_buffer) // fix cursor in current window?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001495 {
1496 if (curwin->w_cursor.lnum >= n)
1497 {
1498 if (curwin->w_cursor.lnum > n)
1499 {
1500 curwin->w_cursor.lnum -= 1;
1501 check_cursor_col();
1502 }
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001503 else
1504 check_cursor();
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001505 changed_cline_bef_curs();
1506 }
1507 invalidate_botline();
1508 }
1509 }
1510 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001511 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001512 else if (lua_isstring(L, 3)) // update line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001513 {
1514 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001515 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001516 if (u_savesub(n) == FAIL)
1517 {
1518 curbuf = buf;
1519 luaL_error(L, "cannot save undo information");
1520 }
1521 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1522 {
1523 curbuf = buf;
1524 luaL_error(L, "cannot replace line");
1525 }
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001526 else
1527 changed_bytes(n, 0);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001528 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001529 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001530 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001531 }
1532 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001533 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001534 return 0;
1535}
1536
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001537 static int
1538luaV_buffer_insert(lua_State *L)
1539{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001540 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1541 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1542 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001543 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1544 buf_T *buf;
1545 luaL_checktype(L, 2, LUA_TSTRING);
1546#ifdef HAVE_SANDBOX
1547 luaV_checksandbox(L);
1548#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001549 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001550 if (n < 0) n = 0;
1551 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001552 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001553 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001554 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001555 if (u_save(n, n + 1) == FAIL)
1556 {
1557 curbuf = buf;
1558 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001559 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001560 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1561 {
1562 curbuf = buf;
1563 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001564 }
1565 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001566 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001567 curbuf = buf;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001568 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001569 return 0;
1570}
1571
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001572 static int
1573luaV_buffer_next(lua_State *L)
1574{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001575 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001576 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1577 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001578 return 1;
1579}
1580
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001581 static int
1582luaV_buffer_previous(lua_State *L)
1583{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001584 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001585 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1586 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001587 return 1;
1588}
1589
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001590 static int
1591luaV_buffer_isvalid(lua_State *L)
1592{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001593 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001594 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001595 lua_pushboolean(L, !lua_isnil(L, -1));
1596 return 1;
1597}
1598
1599static const luaL_Reg luaV_Buffer_mt[] = {
1600 {"__tostring", luaV_buffer_tostring},
1601 {"__len", luaV_buffer_len},
1602 {"__call", luaV_buffer_call},
1603 {"__index", luaV_buffer_index},
1604 {"__newindex", luaV_buffer_newindex},
1605 {"insert", luaV_buffer_insert},
1606 {"next", luaV_buffer_next},
1607 {"previous", luaV_buffer_previous},
1608 {"isvalid", luaV_buffer_isvalid},
1609 {NULL, NULL}
1610};
1611
1612
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001613// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001614
Bram Moolenaar1dced572012-04-05 16:54:08 +02001615luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1616luaV_pushtype(win_T, window, luaV_Window)
1617luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001618
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001619 static int
1620luaV_window_call(lua_State *L)
1621{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001622 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001623 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001624 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001625 return 1;
1626}
1627
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001628 static int
1629luaV_window_index(lua_State *L)
1630{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001631 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001632 const char *s = luaL_checkstring(L, 2);
1633 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001634 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001635 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001636 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001637 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001638 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001639 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001640 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001641 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001642 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001643 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001644 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001645 || strncmp(s, "previous", 8) == 0
1646 || strncmp(s, "isvalid", 7) == 0)
1647 {
1648 lua_getmetatable(L, 1);
1649 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001650 }
1651 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001652 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001653 return 1;
1654}
1655
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001656 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001657luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001658{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001659 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001660 const char *s = luaL_checkstring(L, 2);
1661 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001662 if (strncmp(s, "line", 4) == 0)
1663 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001664#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001665 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001666#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001667 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001668 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001669 w->w_cursor.lnum = v;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001670 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001671 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001672 else if (strncmp(s, "col", 3) == 0)
1673 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001674#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001675 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001676#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001677 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001678 w->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001679 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001680 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001681 else if (strncmp(s, "width", 5) == 0)
1682 {
1683 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001684#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001685 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001686#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001687 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001688 win_setwidth(v);
1689 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001690 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001691 else if (strncmp(s, "height", 6) == 0)
1692 {
1693 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001694#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001695 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001696#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001697 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001698 win_setheight(v);
1699 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001700 }
1701 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001702 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001703 return 0;
1704}
1705
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001706 static int
1707luaV_window_next(lua_State *L)
1708{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001709 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001710 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1711 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001712 return 1;
1713}
1714
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001715 static int
1716luaV_window_previous(lua_State *L)
1717{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001718 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001719 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1720 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001721 return 1;
1722}
1723
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001724 static int
1725luaV_window_isvalid(lua_State *L)
1726{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001727 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001728 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001729 lua_pushboolean(L, !lua_isnil(L, -1));
1730 return 1;
1731}
1732
1733static const luaL_Reg luaV_Window_mt[] = {
1734 {"__tostring", luaV_window_tostring},
1735 {"__call", luaV_window_call},
1736 {"__index", luaV_window_index},
1737 {"__newindex", luaV_window_newindex},
1738 {"next", luaV_window_next},
1739 {"previous", luaV_window_previous},
1740 {"isvalid", luaV_window_isvalid},
1741 {NULL, NULL}
1742};
1743
1744
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001745// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001746
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001747 static int
1748luaV_print(lua_State *L)
1749{
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001750 int i, n = lua_gettop(L); // nargs
1751 const char *s;
1752 size_t l;
1753 garray_T msg_ga;
1754
1755 ga_init2(&msg_ga, 1, 128);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001756 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001757 for (i = 1; i <= n; i++)
1758 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001759 lua_pushvalue(L, -1); // tostring
1760 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001761 lua_call(L, 1, 1);
1762 s = lua_tolstring(L, -1, &l);
1763 if (s == NULL)
1764 return luaL_error(L, "cannot convert to string");
Bram Moolenaar78e006b2021-07-28 15:07:01 +02001765 if (i > 1)
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001766 ga_append(&msg_ga, ' '); // use space instead of tab
1767 ga_concat_len(&msg_ga, (char_u *)s, l);
Bram Moolenaar2a4bd002021-07-28 21:48:59 +02001768 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001769 }
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001770 // Replace any "\n" with "\0"
1771 for (i = 0; i < msg_ga.ga_len; i++)
1772 if (((char *)msg_ga.ga_data)[i] == '\n')
1773 ((char *)msg_ga.ga_data)[i] = '\0';
1774 lua_pushlstring(L, msg_ga.ga_data, msg_ga.ga_len);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001775 if (!got_int)
1776 luaV_msg(L);
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001777
1778 ga_clear(&msg_ga);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001779 return 0;
1780}
1781
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001782 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001783luaV_debug(lua_State *L)
1784{
1785 lua_settop(L, 0);
1786 lua_getglobal(L, "vim");
1787 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001788 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001789 for (;;)
1790 {
1791 const char *input;
1792 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001793 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001794 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001795 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001796 input = lua_tolstring(L, -1, &l);
1797 if (l == 0 || strcmp(input, "cont") == 0)
1798 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001799 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001800 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1801 || lua_pcall(L, 0, 0, 0))
1802 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001803 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001804 }
1805}
1806
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001807 static dict_T *
1808luaV_get_var_scope(lua_State *L)
1809{
1810 const char *scope = luaL_checkstring(L, 1);
1811 dict_T *dict = NULL;
1812
1813 if (STRICMP((char *)scope, "g") == 0)
1814 dict = get_globvar_dict();
1815 else if (STRICMP((char *)scope, "v") == 0)
1816 dict = get_vimvar_dict();
1817 else if (STRICMP((char *)scope, "b") == 0)
1818 dict = curbuf->b_vars;
1819 else if (STRICMP((char *)scope, "w") == 0)
1820 dict = curwin->w_vars;
1821 else if (STRICMP((char *)scope, "t") == 0)
1822 dict = curtab->tp_vars;
1823 else
1824 {
1825 luaL_error(L, "invalid scope %s", scope);
1826 return NULL;
1827 }
1828
1829 return dict;
1830}
1831
1832 static int
1833luaV_setvar(lua_State *L)
1834{
1835 dict_T *dict;
1836 dictitem_T *di;
1837 size_t len;
1838 char *name;
1839 int del;
1840 char *error = NULL;
1841
1842 name = (char *)luaL_checklstring(L, 3, &len);
1843 del = (lua_gettop(L) < 4) || lua_isnil(L, 4);
1844
1845 dict = luaV_get_var_scope(L);
1846 if (dict == NULL)
1847 return 0;
1848
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001849 di = dict_find(dict, (char_u *)name, (int)len);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001850 if (di != NULL)
1851 {
1852 if (di->di_flags & DI_FLAGS_RO)
1853 error = "variable is read-only";
1854 else if (di->di_flags & DI_FLAGS_LOCK)
1855 error = "variable is locked";
1856 else if (del && di->di_flags & DI_FLAGS_FIX)
1857 error = "variable is fixed";
1858 if (error != NULL)
1859 return luaL_error(L, error);
1860 }
1861 else if (dict->dv_lock)
1862 return luaL_error(L, "Dictionary is locked");
1863
1864 if (del)
1865 {
1866 // Delete the key
1867 if (di == NULL)
1868 // Doesn't exist, nothing to do
1869 return 0;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001870 // Delete the entry
1871 dictitem_remove(dict, di, "Lua delete variable");
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001872 }
1873 else
1874 {
1875 // Update the key
1876 typval_T tv;
1877
h-east965d2ed2021-10-04 21:51:57 +01001878 // Convert the lua value to a Vim script type in the temporary variable
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001879 lua_pushvalue(L, 4);
1880 if (luaV_totypval(L, -1, &tv) == FAIL)
1881 return luaL_error(L, "Couldn't convert lua value");
1882
1883 if (di == NULL)
1884 {
1885 // Need to create an entry
1886 di = dictitem_alloc((char_u *)name);
1887 if (di == NULL)
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001888 {
1889 clear_tv(&tv);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001890 return 0;
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001891 }
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001892 // Update the value
1893 copy_tv(&tv, &di->di_tv);
Bram Moolenaar4a011592021-08-05 15:11:08 +02001894 if (dict_add(dict, di) == FAIL)
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001895 {
1896 dictitem_free(di);
1897 clear_tv(&tv);
Bram Moolenaar4a011592021-08-05 15:11:08 +02001898 return luaL_error(L, "Couldn't add to dictionary");
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001899 }
1900 }
1901 else
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001902 {
1903 // Clear the old value
1904 clear_tv(&di->di_tv);
1905 // Update the value
1906 copy_tv(&tv, &di->di_tv);
1907 }
1908
1909 // Clear the temporary variable
1910 clear_tv(&tv);
1911 }
1912
1913 return 0;
1914}
1915
1916 static int
1917luaV_getvar(lua_State *L)
1918{
1919 dict_T *dict = luaV_get_var_scope(L);
1920 size_t len;
1921 const char *name = luaL_checklstring(L, 3, &len);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001922 dictitem_T *di = dict_find(dict, (char_u *)name, (int)len);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001923
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001924 if (di == NULL)
1925 return 0; // nil
1926
1927 luaV_pushtypval(L, &di->di_tv);
1928 return 1;
1929}
1930
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001931 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001932luaV_command(lua_State *L)
1933{
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001934 char_u *s = vim_strsave((char_u *)luaL_checkstring(L, 1));
1935
1936 execute_cmds_from_string(s);
1937 vim_free(s);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001938 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001939 return 0;
1940}
1941
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001942 static int
1943luaV_eval(lua_State *L)
1944{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001945 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001946
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001947 if (tv == NULL) luaL_error(L, "invalid expression");
1948 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001949 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001950 return 1;
1951}
1952
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001953 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001954luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001955{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001956 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001957 return 0;
1958}
1959
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001960 static int
1961luaV_line(lua_State *L)
1962{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001963 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1964 return 1;
1965}
1966
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001967 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001968luaV_list(lua_State *L)
1969{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001970 list_T *l;
1971 int initarg = !lua_isnoneornil(L, 1);
1972
1973 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1974 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001975
Bram Moolenaarca06da92018-07-01 15:12:05 +02001976 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001977 if (l == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001978 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001979 lua_pushnil(L);
1980 return 1;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001981 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001982
1983 luaV_newlist(L, l);
1984 if (!initarg)
1985 return 1;
1986
1987 // traverse table to init list
1988 int notnil, i = 0;
1989 typval_T v;
1990 do
1991 {
1992 lua_rawgeti(L, 1, ++i);
1993 notnil = !lua_isnil(L, -1);
1994 if (notnil)
1995 {
1996 luaV_checktypval(L, -1, &v, "vim.list");
1997 list_append_tv(l, &v);
1998 clear_tv(&v);
1999 }
2000 lua_pop(L, 1); // value
2001 } while (notnil);
2002
Bram Moolenaar1dced572012-04-05 16:54:08 +02002003 return 1;
2004}
2005
2006 static int
2007luaV_dict(lua_State *L)
2008{
Bram Moolenaarca06da92018-07-01 15:12:05 +02002009 dict_T *d;
2010 int initarg = !lua_isnoneornil(L, 1);
2011
2012 if (initarg && lua_type(L, 1) != LUA_TTABLE)
2013 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002014
Bram Moolenaarca06da92018-07-01 15:12:05 +02002015 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02002016 if (d == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002017 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002018 lua_pushnil(L);
2019 return 1;
2020 }
2021
2022 luaV_newdict(L, d);
2023 if (!initarg)
2024 return 1;
2025
2026 // traverse table to init dict
2027 lua_pushnil(L);
2028 while (lua_next(L, 1))
2029 {
2030 char_u *key;
2031 dictitem_T *di;
2032 typval_T v;
2033
2034 lua_pushvalue(L, -2); // dup key in case it's a number
2035 key = (char_u *) lua_tostring(L, -1);
2036 if (key == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002037 {
2038 lua_pushnil(L);
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002039 return 1;
Bram Moolenaarca06da92018-07-01 15:12:05 +02002040 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002041 if (*key == NUL)
2042 luaL_error(L, "table has empty key");
2043 luaV_checktypval(L, -2, &v, "vim.dict"); // value
2044 di = dictitem_alloc(key);
2045 if (di == NULL || dict_add(d, di) == FAIL)
2046 {
2047 vim_free(di);
2048 lua_pushnil(L);
2049 return 1;
2050 }
2051 di->di_tv = v;
2052 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02002053 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002054
Bram Moolenaarca06da92018-07-01 15:12:05 +02002055 return 1;
2056}
2057
2058 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01002059luaV_blob(lua_State *L)
2060{
2061 blob_T *b;
2062 int initarg = !lua_isnoneornil(L, 1);
2063
2064 if (initarg && !lua_isstring(L, 1))
2065 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002066
Bram Moolenaarb7828692019-03-23 13:57:02 +01002067 b = blob_alloc();
2068 if (b == NULL)
Bram Moolenaarb7828692019-03-23 13:57:02 +01002069 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002070 lua_pushnil(L);
2071 return 1;
Bram Moolenaarb7828692019-03-23 13:57:02 +01002072 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002073
2074 luaV_newblob(L, b);
2075 if (!initarg)
2076 return 1;
2077
2078 // traverse table to init blob
2079 size_t i, l = 0;
2080 const char *s = lua_tolstring(L, 1, &l);
2081
2082 if (ga_grow(&b->bv_ga, (int)l) == OK)
2083 for (i = 0; i < l; ++i)
2084 ga_append(&b->bv_ga, s[i]);
2085
Bram Moolenaarb7828692019-03-23 13:57:02 +01002086 return 1;
2087}
2088
2089 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02002090luaV_funcref(lua_State *L)
2091{
2092 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002093 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002094 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
2095 luaL_error(L, "invalid function name: %s", name);
2096 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002097 return 1;
2098}
2099
2100 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002101luaV_buffer(lua_State *L)
2102{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002103 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002104 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002105 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002106 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002107 {
2108 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02002109 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002110 if (buf->b_fnum == n) break;
2111 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002112 else // by name
2113 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002114 size_t l;
2115 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02002116 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002117 {
2118 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
2119 {
2120 if (l == 0) break;
2121 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02002122 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
2123 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002124 break;
2125 }
2126 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002127 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002128 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002129 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002130 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002131 return 1;
2132}
2133
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002134 static int
2135luaV_window(lua_State *L)
2136{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002137 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002138 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002139 {
2140 int n = lua_tointeger(L, 1);
2141 for (win = firstwin; win != NULL; win = win->w_next, n--)
2142 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002143 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002144 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002145 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002146 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002147 return 1;
2148}
2149
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002150 static int
2151luaV_open(lua_State *L)
2152{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002153 char_u *s = NULL;
2154#ifdef HAVE_SANDBOX
2155 luaV_checksandbox(L);
2156#endif
2157 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01002158 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002159 return 1;
2160}
2161
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002162 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002163luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002164{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002165 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002166 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002167 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02002168 lua_settop(L, 1);
2169 if (lua_getmetatable(L, 1))
2170 {
2171 luaV_getfield(L, LUAVIM_LIST);
2172 if (lua_rawequal(L, -1, 2))
2173 {
2174 lua_pushstring(L, "list");
2175 return 1;
2176 }
2177 luaV_getfield(L, LUAVIM_DICT);
2178 if (lua_rawequal(L, -1, 2))
2179 {
2180 lua_pushstring(L, "dict");
2181 return 1;
2182 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01002183 luaV_getfield(L, LUAVIM_BLOB);
2184 if (lua_rawequal(L, -1, 2))
2185 {
2186 lua_pushstring(L, "blob");
2187 return 1;
2188 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002189 luaV_getfield(L, LUAVIM_FUNCREF);
2190 if (lua_rawequal(L, -1, 2))
2191 {
2192 lua_pushstring(L, "funcref");
2193 return 1;
2194 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002195 luaV_getfield(L, LUAVIM_BUFFER);
2196 if (lua_rawequal(L, -1, 2))
2197 {
2198 lua_pushstring(L, "buffer");
2199 return 1;
2200 }
2201 luaV_getfield(L, LUAVIM_WINDOW);
2202 if (lua_rawequal(L, -1, 2))
2203 {
2204 lua_pushstring(L, "window");
2205 return 1;
2206 }
2207 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002208 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002209 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02002210 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002211}
2212
Bram Moolenaareb04f082020-05-17 14:32:35 +02002213 static int
2214luaV_call(lua_State *L)
2215{
2216 int argc = lua_gettop(L) - 1;
2217 size_t funcname_len;
2218 char_u *funcname;
2219 char *error = NULL;
2220 typval_T rettv;
2221 typval_T argv[MAX_FUNC_ARGS + 1];
2222 int i = 0;
2223
2224 if (argc > MAX_FUNC_ARGS)
2225 return luaL_error(L, "Function called with too many arguments");
2226
2227 funcname = (char_u *)luaL_checklstring(L, 1, &funcname_len);
2228
2229 for (; i < argc; i++)
2230 {
2231 if (luaV_totypval(L, i + 2, &argv[i]) == FAIL)
2232 {
2233 error = "lua: cannot convert value";
2234 goto free_vim_args;
2235 }
2236 }
2237
2238 argv[argc].v_type = VAR_UNKNOWN;
2239
2240 if (call_vim_function(funcname, argc, argv, &rettv) == FAIL)
2241 {
2242 error = "lua: call_vim_function failed";
2243 goto free_vim_args;
2244 }
2245
2246 luaV_pushtypval(L, &rettv);
2247 clear_tv(&rettv);
2248
2249free_vim_args:
2250 while (i > 0)
2251 clear_tv(&argv[--i]);
2252
2253 if (error == NULL)
2254 return 1;
2255 else
2256 return luaL_error(L, error);
2257}
2258
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02002259/*
2260 * Return the Vim version as a Lua table
2261 */
2262 static int
2263luaV_version(lua_State *L)
2264{
2265 lua_newtable(L);
2266 lua_pushstring(L, "major");
2267 lua_pushinteger(L, VIM_VERSION_MAJOR);
2268 lua_settable(L, -3);
2269 lua_pushstring(L, "minor");
2270 lua_pushinteger(L, VIM_VERSION_MINOR);
2271 lua_settable(L, -3);
2272 lua_pushstring(L, "patch");
2273 lua_pushinteger(L, highest_patch());
2274 lua_settable(L, -3);
2275 return 1;
2276}
2277
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002278static const luaL_Reg luaV_module[] = {
2279 {"command", luaV_command},
2280 {"eval", luaV_eval},
2281 {"beep", luaV_beep},
2282 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002283 {"list", luaV_list},
2284 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01002285 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02002286 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002287 {"buffer", luaV_buffer},
2288 {"window", luaV_window},
2289 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002290 {"type", luaV_type},
Bram Moolenaareb04f082020-05-17 14:32:35 +02002291 {"call", luaV_call},
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002292 {"_getvar", luaV_getvar},
2293 {"_setvar", luaV_setvar},
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02002294 {"version", luaV_version},
Bram Moolenaar125ed272021-04-07 20:11:12 +02002295 {"lua_version", NULL},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002296 {NULL, NULL}
2297};
2298
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002299/*
2300 * for freeing list, dict, buffer and window objects; lightuserdata as arg
2301 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02002302 static int
2303luaV_free(lua_State *L)
2304{
2305 lua_pushnil(L);
2306 luaV_setudata(L, lua_touserdata(L, 1));
2307 return 0;
2308}
2309
2310 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002311luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002312{
2313 luaL_Buffer b;
2314 size_t l;
2315 const char *str = lua_tolstring(L, 1, &l);
2316 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
2317 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
2318 luaL_buffinit(L, &b);
2319 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
2320 luaL_addlstring(&b, str, l);
2321 luaL_pushresult(&b);
2322 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002323 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002324 {
2325 luaV_emsg(L);
2326 return 0;
2327 }
2328 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002329 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002330 {
2331 luaV_emsg(L);
2332 return 0;
2333 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002334 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002335 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01002336 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002337}
2338
2339 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002340luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002341{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002342 int copyID = lua_tointeger(L, 1);
2343 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002344
Jesse Pavel8a350332023-08-13 22:05:45 -04002345 lua_pushlightuserdata(L, (void *) LUAVIM_UDATA_CACHE);
2346 lua_rawget(L, LUA_REGISTRYINDEX); // the cache table
2347
Bram Moolenaar1dced572012-04-05 16:54:08 +02002348 luaV_getfield(L, LUAVIM_LIST);
2349 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002350 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002351 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002352 // traverse cache table
Jesse Pavel8a350332023-08-13 22:05:45 -04002353 while (!abort && lua_next(L, 2) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002354 {
2355 lua_getmetatable(L, -1);
Jesse Pavel8a350332023-08-13 22:05:45 -04002356 if (lua_rawequal(L, -1, 3)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002357 {
Jesse Pavel8a350332023-08-13 22:05:45 -04002358 list_T *l = (list_T *)lua_touserdata(L, 6); // key
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002359
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002360 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002361 }
Jesse Pavel8a350332023-08-13 22:05:45 -04002362 else if (lua_rawequal(L, -1, 4)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002363 {
Jesse Pavel8a350332023-08-13 22:05:45 -04002364 dict_T *d = (dict_T *)lua_touserdata(L, 6); // key
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002365
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002366 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002367 }
Jesse Pavel8a350332023-08-13 22:05:45 -04002368 else if (lua_rawequal(L, -1, 5)) // funcref?
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002369 {
Jesse Pavel8a350332023-08-13 22:05:45 -04002370 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 6); // key
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002371
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002372 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002373 }
2374 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02002375 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002376 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01002377 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002378}
2379
Bram Moolenaar125ed272021-04-07 20:11:12 +02002380 static int
2381luaV_pushversion(lua_State *L)
2382{
2383 int major = 0;
2384 int minor = 0;
2385 int patch = 0;
2386 char s[16];
2387
2388 sscanf(LUAVIM_VERSION, "Lua %d.%d.%d", &major, &minor, &patch);
2389 vim_snprintf(s, sizeof(s), "%d.%d.%d", major, minor, patch);
2390 lua_pushstring(L, s);
2391 return 0;
2392}
2393
Bram Moolenaareb04f082020-05-17 14:32:35 +02002394#define LUA_VIM_FN_CODE \
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002395 "vim.fn = setmetatable({}, {\n"\
2396 " __index = function (t, key)\n"\
2397 " local function _fn(...)\n"\
2398 " return vim.call(key, ...)\n"\
2399 " end\n"\
2400 " t[key] = _fn\n"\
2401 " return _fn\n"\
2402 " end\n"\
2403 " })"
2404
2405#define LUA_VIM_UPDATE_PACKAGE_PATHS \
2406 "local last_vim_paths = {}\n"\
2407 "vim._update_package_paths = function ()\n"\
2408 " local cur_vim_paths = {}\n"\
2409 " local function split(s, delimiter)\n"\
2410 " result = {}\n"\
2411 " for match in (s..delimiter):gmatch(\"(.-)\"..delimiter) do\n"\
2412 " table.insert(result, match)\n"\
2413 " end\n"\
2414 " return result\n"\
2415 " end\n"\
2416 " local rtps = split(vim.eval('&runtimepath'), ',')\n"\
2417 " local sep = package.config:sub(1, 1)\n"\
2418 " for _, key in ipairs({'path', 'cpath'}) do\n"\
2419 " local orig_str = package[key] .. ';'\n"\
2420 " local pathtrails_ordered = {}\n"\
2421 " -- Note: ignores trailing item without trailing `;`. Not using something\n"\
2422 " -- simpler in order to preserve empty items (stand for default path).\n"\
2423 " local orig = {}\n"\
2424 " for s in orig_str:gmatch('[^;]*;') do\n"\
2425 " s = s:sub(1, -2) -- Strip trailing semicolon\n"\
2426 " orig[#orig + 1] = s\n"\
2427 " end\n"\
2428 " if key == 'path' then\n"\
2429 " -- /?.lua and /?/init.lua\n"\
2430 " pathtrails_ordered = {sep .. '?.lua', sep .. '?' .. sep .. 'init.lua'}\n"\
2431 " else\n"\
2432 " local pathtrails = {}\n"\
2433 " for _, s in ipairs(orig) do\n"\
2434 " -- Find out path patterns. pathtrail should contain something like\n"\
2435 " -- /?.so, \?.dll. This allows not to bother determining what correct\n"\
2436 " -- suffixes are.\n"\
2437 " local pathtrail = s:match('[/\\\\][^/\\\\]*%?.*$')\n"\
2438 " if pathtrail and not pathtrails[pathtrail] then\n"\
2439 " pathtrails[pathtrail] = true\n"\
2440 " pathtrails_ordered[#pathtrails_ordered + 1] = pathtrail\n"\
2441 " end\n"\
2442 " end\n"\
2443 " end\n"\
2444 " local new = {}\n"\
2445 " for _, rtp in ipairs(rtps) do\n"\
2446 " if not rtp:match(';') then\n"\
2447 " for _, pathtrail in pairs(pathtrails_ordered) do\n"\
2448 " local new_path = rtp .. sep .. 'lua' .. pathtrail\n"\
2449 " -- Always keep paths from &runtimepath at the start:\n"\
2450 " -- append them here disregarding orig possibly containing one of them.\n"\
2451 " new[#new + 1] = new_path\n"\
2452 " cur_vim_paths[new_path] = true\n"\
2453 " end\n"\
2454 " end\n"\
2455 " end\n"\
2456 " for _, orig_path in ipairs(orig) do\n"\
2457 " -- Handle removing obsolete paths originating from &runtimepath: such\n"\
2458 " -- paths either belong to cur_nvim_paths and were already added above or\n"\
2459 " -- to last_nvim_paths and should not be added at all if corresponding\n"\
2460 " -- entry was removed from &runtimepath list.\n"\
2461 " if not (cur_vim_paths[orig_path] or last_vim_paths[orig_path]) then\n"\
2462 " new[#new + 1] = orig_path\n"\
2463 " end\n"\
2464 " end\n"\
2465 " package[key] = table.concat(new, ';')\n"\
2466 " end\n"\
2467 " last_vim_paths = cur_vim_paths\n"\
2468 "end"
Bram Moolenaareb04f082020-05-17 14:32:35 +02002469
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002470#define LUA_VIM_SETUP_VARIABLE_DICTS \
2471 "do\n"\
2472 " local function make_dict_accessor(scope)\n"\
2473 " local mt = {}\n"\
2474 " function mt:__newindex(k, v)\n"\
2475 " return vim._setvar(scope, 0, k, v)\n"\
2476 " end\n"\
2477 " function mt:__index(k)\n"\
2478 " return vim._getvar(scope, 0, k)\n"\
2479 " end\n"\
2480 " return setmetatable({}, mt)\n"\
2481 " end\n"\
2482 " vim.g = make_dict_accessor('g')\n"\
2483 " vim.v = make_dict_accessor('v')\n"\
2484 " vim.b = make_dict_accessor('b')\n"\
2485 " vim.w = make_dict_accessor('w')\n"\
2486 " vim.t = make_dict_accessor('t')\n"\
2487 "end"
2488
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002489 static int
2490luaopen_vim(lua_State *L)
2491{
Jesse Pavel8a350332023-08-13 22:05:45 -04002492 lua_newtable(L); // cache table
2493 lua_newtable(L); // cache table's metatable
Bram Moolenaar1dced572012-04-05 16:54:08 +02002494 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002495 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002496 lua_setmetatable(L, -2); // cache is weak-valued
Jesse Pavel8a350332023-08-13 22:05:45 -04002497 // put the cache table in the registry for luaV_get/setudata()
2498 lua_pushlightuserdata(L, (void *) LUAVIM_UDATA_CACHE);
2499 lua_pushvalue(L, -2);
2500 lua_rawset(L, LUA_REGISTRYINDEX);
2501 lua_pop(L, 1); // we don't need the cache table here anymore
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002502 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002503 lua_pushcfunction(L, luaV_print);
2504 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002505 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002506 lua_getglobal(L, "debug");
2507 lua_pushcfunction(L, luaV_debug);
2508 lua_setfield(L, -2, "debug");
2509 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002510 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002511 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Jesse Pavel8a350332023-08-13 22:05:45 -04002512 lua_pushcfunction(L, luaV_free);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002513 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002514 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002515 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Jesse Pavel8a350332023-08-13 22:05:45 -04002516 lua_pushcfunction(L, luaV_luaeval);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002517 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002518 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002519 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Jesse Pavel8a350332023-08-13 22:05:45 -04002520 lua_pushcfunction(L, luaV_setref);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002521 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002522 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002523 luaV_newmetatable(L, LUAVIM_LIST);
Jesse Pavel8a350332023-08-13 22:05:45 -04002524 luaV_register(L, luaV_List_mt);
2525 lua_pop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002526 luaV_newmetatable(L, LUAVIM_DICT);
Jesse Pavel8a350332023-08-13 22:05:45 -04002527 luaV_register(L, luaV_Dict_mt);
2528 lua_pop(L, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002529 luaV_newmetatable(L, LUAVIM_BLOB);
Jesse Pavel8a350332023-08-13 22:05:45 -04002530 luaV_register(L, luaV_Blob_mt);
2531 lua_pop(L, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002532 luaV_newmetatable(L, LUAVIM_FUNCREF);
Jesse Pavel8a350332023-08-13 22:05:45 -04002533 luaV_register(L, luaV_Funcref_mt);
2534 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002535 luaV_newmetatable(L, LUAVIM_BUFFER);
Jesse Pavel8a350332023-08-13 22:05:45 -04002536 luaV_register(L, luaV_Buffer_mt);
2537 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002538 luaV_newmetatable(L, LUAVIM_WINDOW);
Jesse Pavel8a350332023-08-13 22:05:45 -04002539 luaV_register(L, luaV_Window_mt);
2540 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002541 lua_newtable(L); // vim table
Jesse Pavel8a350332023-08-13 22:05:45 -04002542 luaV_register(L, luaV_module);
Bram Moolenaar125ed272021-04-07 20:11:12 +02002543 luaV_pushversion(L);
2544 lua_setfield(L, -2, "lua_version");
Bram Moolenaar1dced572012-04-05 16:54:08 +02002545 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaareb04f082020-05-17 14:32:35 +02002546 // custom code
Bram Moolenaar9309eb22020-05-17 16:53:56 +02002547 (void)luaL_dostring(L, LUA_VIM_FN_CODE);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002548 (void)luaL_dostring(L, LUA_VIM_UPDATE_PACKAGE_PATHS);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002549 (void)luaL_dostring(L, LUA_VIM_SETUP_VARIABLE_DICTS);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002550
Jesse Pavel8a350332023-08-13 22:05:45 -04002551 lua_getglobal(L, LUAVIM_NAME);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002552 lua_getfield(L, -1, "_update_package_paths");
2553
2554 if (lua_pcall(L, 0, 0, 0))
2555 luaV_emsg(L);
2556
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002557 return 0;
2558}
2559
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002560 static lua_State *
2561luaV_newstate(void)
2562{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002563 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002564 luaL_openlibs(L); // core libs
2565 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002566 lua_call(L, 0, 0);
2567 return L;
2568}
2569
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002570 static void
2571luaV_setrange(lua_State *L, int line1, int line2)
2572{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002573 lua_getglobal(L, LUAVIM_NAME);
2574 lua_pushinteger(L, line1);
2575 lua_setfield(L, -2, "firstline");
2576 lua_pushinteger(L, line2);
2577 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002578 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002579}
2580
2581
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002582// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002583
2584static lua_State *L = NULL;
2585
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002586 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002587lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002588{
2589 return L != NULL;
2590}
2591
2592 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002593lua_init(void)
2594{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002595 if (lua_isopen())
2596 return OK;
2597
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002598#ifdef DYNAMIC_LUA
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002599 if (!lua_enabled(TRUE))
2600 {
2601 emsg(_("Lua library cannot be loaded."));
2602 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002603 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002604#endif
2605 L = luaV_newstate();
2606
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002607 return OK;
2608}
2609
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002610 void
2611lua_end(void)
2612{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002613 if (!lua_isopen())
2614 return;
2615
2616 lua_close(L);
2617 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002618}
2619
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002620/*
2621 * ex commands
2622 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002623 void
2624ex_lua(exarg_T *eap)
2625{
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002626 char *script = (char *)script_get(eap, eap->arg);
2627
2628 if (!eap->skip && lua_init() == OK)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002629 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002630 char *s = script != NULL ? script : (char *)eap->arg;
2631
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002632 luaV_setrange(L, eap->line1, eap->line2);
2633 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2634 || lua_pcall(L, 0, 0, 0))
2635 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002636 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002637 if (script != NULL)
2638 vim_free(script);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002639}
2640
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002641 void
2642ex_luado(exarg_T *eap)
2643{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002644 linenr_T l;
2645 const char *s = (const char *) eap->arg;
2646 luaL_Buffer b;
2647 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002648 buf_T *was_curbuf = curbuf;
2649
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002650 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002651 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2652 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002653 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002654 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002655 }
2656 luaV_setrange(L, eap->line1, eap->line2);
2657 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002658 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002659 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002660 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002661 luaL_pushresult(&b);
2662 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002663 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2664 {
2665 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002666 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002667 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002668 }
2669 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002670 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002671 for (l = eap->line1; l <= eap->line2; l++)
2672 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02002673 // Check the line number, the command may have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002674 if (l > curbuf->b_ml.ml_line_count)
2675 break;
2676
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002677 lua_pushvalue(L, -1); // function
2678 luaV_pushline(L, curbuf, l); // current line as arg
2679 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002680 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002681 {
2682 luaV_emsg(L);
2683 break;
2684 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002685 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002686 if (curbuf != was_curbuf)
2687 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002688 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002689 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002690#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002691 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002692#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002693 ml_replace(l, luaV_toline(L, -1), TRUE);
2694 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002695 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002696 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002697 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002698 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002699 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002700 check_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002701 update_screen(UPD_NOT_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002702}
2703
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002704 void
2705ex_luafile(exarg_T *eap)
2706{
2707 if (lua_init() == FAIL)
2708 return;
2709 if (!eap->skip)
2710 {
2711 luaV_setrange(L, eap->line1, eap->line2);
2712 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2713 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002714 }
2715}
2716
Bram Moolenaar1dced572012-04-05 16:54:08 +02002717#define luaV_freetype(typ,tname) \
2718 void \
2719 lua_##tname##_free(typ *o) \
2720 { \
2721 if (!lua_isopen()) return; \
2722 luaV_getfield(L, LUAVIM_FREE); \
2723 lua_pushlightuserdata(L, (void *) o); \
2724 lua_call(L, 1, 0); \
2725 }
2726
2727luaV_freetype(buf_T, buffer)
2728luaV_freetype(win_T, window)
2729
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002730 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002731do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002732{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002733 lua_init();
2734 luaV_getfield(L, LUAVIM_LUAEVAL);
2735 lua_pushstring(L, (char *) str);
2736 lua_pushlightuserdata(L, (void *) arg);
2737 lua_pushlightuserdata(L, (void *) rettv);
2738 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002739}
2740
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002741 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002742set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002743{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002744 int aborted = 0;
2745
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002746 if (!lua_isopen())
2747 return 0;
2748
2749 luaV_getfield(L, LUAVIM_SETREF);
2750 // call the function with 1 arg, getting 1 result back
2751 lua_pushinteger(L, copyID);
2752 lua_call(L, 1, 1);
2753 // get the result
2754 aborted = lua_tointeger(L, -1);
2755 // pop result off the stack
2756 lua_pop(L, 1);
2757
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002758 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002759}
2760
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002761 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002762update_package_paths_in_lua(void)
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002763{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002764 if (!lua_isopen())
2765 return;
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002766
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002767 lua_getglobal(L, "vim");
2768 lua_getfield(L, -1, "_update_package_paths");
2769
2770 if (lua_pcall(L, 0, 0, 0))
2771 luaV_emsg(L);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002772}
2773
Bram Moolenaar801ab062020-06-25 19:27:56 +02002774/*
2775 * Native C function callback
2776 */
2777 static int
2778luaV_call_lua_func(
2779 int argcount,
2780 typval_T *argvars,
2781 typval_T *rettv,
2782 void *state)
2783{
2784 int i;
2785 int luaargcount = argcount;
2786 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2787 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2788
2789 if (funcstate->lua_tableref != LUA_NOREF)
2790 {
2791 // First arg for metatable __call method is a table
2792 luaargcount += 1;
2793 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2794 }
2795
2796 for (i = 0; i < argcount; ++i)
2797 luaV_pushtypval(funcstate->L, &argvars[i]);
2798
2799 if (lua_pcall(funcstate->L, luaargcount, 1, 0))
2800 {
2801 luaV_emsg(funcstate->L);
Bram Moolenaar0917e862023-02-18 14:42:44 +00002802 return (int)FCERR_OTHER;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002803 }
2804
2805 luaV_checktypval(funcstate->L, -1, rettv, "get return value");
Bram Moolenaar0917e862023-02-18 14:42:44 +00002806 return (int)FCERR_NONE;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002807}
2808
2809/*
2810 * Free up any lua references held by the func state.
2811 */
2812 static void
2813luaV_call_lua_func_free(void *state)
2814{
2815 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2816 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2817 funcstate->L = NULL;
2818 if (funcstate->lua_tableref != LUA_NOREF)
2819 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2820 VIM_CLEAR(funcstate);
2821}
2822
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002823#endif