blob: bfe5386dc8e011a1cf8ecf3c3e20de52da3048fd [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8/*
9 * if_perl.xs: Main code for Perl interface support.
10 * Mostly written by Sven Verdoolaege.
11 */
12
13#define _memory_h /* avoid memset redeclaration */
14#define IN_PERL_FILE /* don't include if_perl.pro from proto.h */
15
Bram Moolenaaraee1f4a2013-08-02 16:10:32 +020016/*
Bram Moolenaar6b107212013-12-11 15:06:40 +010017 * Currently 32-bit version of ActivePerl is built with VC6 (or MinGW since
18 * ActivePerl 5.18).
Bram Moolenaaraee1f4a2013-08-02 16:10:32 +020019 * (http://community.activestate.com/faq/windows-compilers-perl-modules)
20 * It means that time_t should be 32-bit. However the default size of
21 * time_t is 64-bit since VC8. So we have to define _USE_32BIT_TIME_T.
22 */
23#if defined(WIN32) && !defined(_WIN64)
24# define _USE_32BIT_TIME_T
25#endif
26
Bram Moolenaar6b107212013-12-11 15:06:40 +010027/*
28 * Prevent including winsock.h. perl.h tries to detect whether winsock.h is
29 * already included before including winsock2.h, because winsock2.h isn't
30 * compatible with winsock.h. However the detection doesn't work with some
31 * versions of MinGW. If WIN32_LEAN_AND_MEAN is defined, windows.h will not
32 * include winsock.h.
33 */
34#ifdef WIN32
35# define WIN32_LEAN_AND_MEAN
36#endif
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#include "vim.h"
39
Bram Moolenaar7c0daf02013-12-14 11:46:08 +010040/* Work around for perl-5.18.
41 * Don't include "perl\lib\CORE\inline.h" for now,
42 * include it after Perl_sv_free2 is defined. */
K.Takata32f586e2023-08-13 10:15:05 +020043#ifdef DYNAMIC_PERL
Bram Moolenaar7c0daf02013-12-14 11:46:08 +010044# define PERL_NO_INLINE_FUNCTIONS
45#endif
46
Bram Moolenaar207fd752013-12-14 11:50:35 +010047#ifdef _MSC_VER
Bram Moolenaar02890652020-10-31 13:05:11 +010048// Work around for using MSVC and ActivePerl 5.18.
Bram Moolenaar207fd752013-12-14 11:50:35 +010049# define __inline__ __inline
Bram Moolenaare73b38f2020-01-06 21:22:09 +010050// Work around for using MSVC and Strawberry Perl 5.30.
51# define __builtin_expect(expr, val) (expr)
Bram Moolenaar02890652020-10-31 13:05:11 +010052// Work around for using MSVC and Strawberry Perl 5.32.
53# define NO_THREAD_SAFE_LOCALE
Bram Moolenaar207fd752013-12-14 11:50:35 +010054#endif
55
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010056#ifdef __GNUC__
57# pragma GCC diagnostic push
58# pragma GCC diagnostic ignored "-Wunused-variable"
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010059#endif
60
Bram Moolenaaraee1f4a2013-08-02 16:10:32 +020061#include <EXTERN.h>
62#include <perl.h>
63#include <XSUB.h>
Bram Moolenaar6244a0f2016-04-14 14:09:25 +020064#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
65# include <perliol.h>
66#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
Bram Moolenaarcf190c62016-06-02 11:54:06 +020068/* Workaround for perl < 5.8.7 */
69#ifndef PERLIO_FUNCS_DECL
70# ifdef PERLIO_FUNCS_CONST
71# define PERLIO_FUNCS_DECL(funcs) const PerlIO_funcs funcs
72# define PERLIO_FUNCS_CAST(funcs) (PerlIO_funcs*)(funcs)
73# else
74# define PERLIO_FUNCS_DECL(funcs) PerlIO_funcs funcs
75# define PERLIO_FUNCS_CAST(funcs) (funcs)
76# endif
77#endif
Bram Moolenaarc4bc0e62016-06-02 13:54:49 +020078#ifndef SvREFCNT_inc_void_NN
79# define SvREFCNT_inc_void_NN SvREFCNT_inc
80#endif
Bram Moolenaarcf190c62016-06-02 11:54:06 +020081
Bram Moolenaar071d4272004-06-13 20:20:40 +000082/*
83 * Work around clashes between Perl and Vim namespace. proto.h doesn't
84 * include if_perl.pro and perlsfio.pro when IN_PERL_FILE is defined, because
85 * we need the CV typedef. proto.h can't be moved to after including
86 * if_perl.h, because we get all sorts of name clashes then.
87 */
88#ifndef PROTO
Bram Moolenaareeb50ab2016-06-26 17:19:46 +020089# ifndef __MINGW32__
90# include "proto/if_perl.pro"
91# include "proto/if_perlsfio.pro"
92# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#endif
94
Bram Moolenaar1f402802018-09-21 14:01:27 +020095// Perl compatibility stuff. This should ensure compatibility with older
96// versions of Perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#ifndef PERL_VERSION
Bram Moolenaareeb50ab2016-06-26 17:19:46 +020098# include <patchlevel.h>
99# define PERL_REVISION 5
100# define PERL_VERSION PATCHLEVEL
101# define PERL_SUBVERSION SUBVERSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#endif
103
Bram Moolenaar1f402802018-09-21 14:01:27 +0200104
105// Work around for ActivePerl 5.20.3+: Avoid generating (g)vim.lib.
106#if defined(ACTIVEPERL_VERSION) && (ACTIVEPERL_VERSION >= 2003) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100107 && defined(MSWIN) && defined(USE_DYNAMIC_LOADING)
Bram Moolenaar1f402802018-09-21 14:01:27 +0200108# undef XS_EXTERNAL
109# define XS_EXTERNAL(name) XSPROTO(name)
110#endif
111
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000112/*
113 * Quoting Jan Dubois of Active State:
114 * ActivePerl build 822 still identifies itself as 5.8.8 but already
115 * contains many of the changes from the upcoming Perl 5.8.9 release.
116 *
117 * The changes include addition of two symbols (Perl_sv_2iv_flags,
118 * Perl_newXS_flags) not present in earlier releases.
119 *
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +0000120 * Jan Dubois suggested the following guarding scheme.
121 *
122 * Active State defined ACTIVEPERL_VERSION as a string in versions before
123 * 5.8.8; and so the comparison to 822 below needs to be guarded.
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000124 */
Bram Moolenaar3b9b13e2007-09-15 12:49:35 +0000125#if (PERL_REVISION == 5) && (PERL_VERSION == 8) && (PERL_SUBVERSION >= 8)
126# if (ACTIVEPERL_VERSION >= 822) || (PERL_SUBVERSION >= 9)
127# define PERL589_OR_LATER
128# endif
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000129#endif
130#if (PERL_REVISION == 5) && (PERL_VERSION >= 9)
131# define PERL589_OR_LATER
132#endif
133
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100134#if (PERL_REVISION == 5) && ((PERL_VERSION > 10) || \
135 (PERL_VERSION == 10) && (PERL_SUBVERSION >= 1))
136# define PERL5101_OR_LATER
137#endif
138
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#ifndef pTHX
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200140# define pTHX void
141# define pTHX_
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#endif
143
144#ifndef EXTERN_C
145# define EXTERN_C
146#endif
147
Philip H53752052022-11-04 22:32:21 +0000148// Suppress Infinite warnings when compiling XS modules under macOS 12 Monterey.
149#if defined(__clang__) && defined(__clang_major__) && __clang_major__ > 11
K.Takata161b6ac2022-11-14 15:31:07 +0000150# pragma clang diagnostic ignored "-Wcompound-token-split-by-macro"
Philip H53752052022-11-04 22:32:21 +0000151#endif
152
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153/* Compatibility hacks over */
154
155static PerlInterpreter *perl_interp = NULL;
Bram Moolenaard99df422016-01-29 23:20:40 +0100156static void xs_init(pTHX);
157static void VIM_init(void);
158EXTERN_C void boot_DynaLoader(pTHX_ CV*);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
160/*
Bram Moolenaare06c1882010-07-21 22:05:20 +0200161 * For dynamic linked perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 */
163#if defined(DYNAMIC_PERL) || defined(PROTO)
Bram Moolenaare06c1882010-07-21 22:05:20 +0200164
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200165# ifndef DYNAMIC_PERL /* just generating prototypes */
Bram Moolenaar4f974752019-02-17 17:44:42 +0100166# ifdef MSWIN
Bram Moolenaare06c1882010-07-21 22:05:20 +0200167typedef int HANDLE;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200168# endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200169typedef int XSINIT_t;
170typedef int XSUBADDR_t;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200171# endif
172# ifndef USE_ITHREADS
Bram Moolenaare06c1882010-07-21 22:05:20 +0200173typedef int perl_key;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200174# endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200175
Bram Moolenaar4f974752019-02-17 17:44:42 +0100176# ifndef MSWIN
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200177# include <dlfcn.h>
178# define HANDLE void*
179# define PERL_PROC void*
180# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
181# define symbol_from_dll dlsym
182# define close_dll dlclose
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200183# define load_dll_error dlerror
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200184# else
185# define PERL_PROC FARPROC
186# define load_dll vimLoadLib
187# define symbol_from_dll GetProcAddress
188# define close_dll FreeLibrary
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200189# define load_dll_error GetWin32Error
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200190# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191/*
192 * Wrapper defines
193 */
194# define perl_alloc dll_perl_alloc
195# define perl_construct dll_perl_construct
196# define perl_parse dll_perl_parse
197# define perl_run dll_perl_run
198# define perl_destruct dll_perl_destruct
199# define perl_free dll_perl_free
200# define Perl_get_context dll_Perl_get_context
201# define Perl_croak dll_Perl_croak
Bram Moolenaar58cb0892010-03-02 15:14:33 +0100202# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100203# define Perl_croak_xs_usage dll_Perl_croak_xs_usage
204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205# ifndef PROTO
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200206# ifdef PERL_IMPLICIT_CONTEXT
207# define Perl_croak_nocontext dll_Perl_croak_nocontext
208# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209# define Perl_call_argv dll_Perl_call_argv
210# define Perl_call_pv dll_Perl_call_pv
211# define Perl_eval_sv dll_Perl_eval_sv
212# define Perl_get_sv dll_Perl_get_sv
213# define Perl_eval_pv dll_Perl_eval_pv
214# define Perl_call_method dll_Perl_call_method
215# endif
216# define Perl_dowantarray dll_Perl_dowantarray
217# define Perl_free_tmps dll_Perl_free_tmps
218# define Perl_gv_stashpv dll_Perl_gv_stashpv
219# define Perl_markstack_grow dll_Perl_markstack_grow
220# define Perl_mg_find dll_Perl_mg_find
Bram Moolenaar578333b2018-07-22 07:31:09 +0200221# if (PERL_REVISION == 5) && (PERL_VERSION >= 28)
222# define Perl_mg_get dll_Perl_mg_get
223# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224# define Perl_newXS dll_Perl_newXS
225# define Perl_newSV dll_Perl_newSV
226# define Perl_newSViv dll_Perl_newSViv
227# define Perl_newSVpv dll_Perl_newSVpv
228# define Perl_pop_scope dll_Perl_pop_scope
229# define Perl_push_scope dll_Perl_push_scope
230# define Perl_save_int dll_Perl_save_int
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200231# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
232# define Perl_save_strlen dll_Perl_save_strlen
233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# define Perl_stack_grow dll_Perl_stack_grow
235# define Perl_set_context dll_Perl_set_context
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200236# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200237# define Perl_sv_2bool_flags dll_Perl_sv_2bool_flags
238# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
239# define Perl_xs_apiversion_bootcheck dll_Perl_xs_apiversion_bootcheck
240# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200241# else
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200242# define Perl_sv_2bool dll_Perl_sv_2bool
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244# define Perl_sv_2iv dll_Perl_sv_2iv
245# define Perl_sv_2mortal dll_Perl_sv_2mortal
246# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
247# define Perl_sv_2pv_flags dll_Perl_sv_2pv_flags
248# define Perl_sv_2pv_nolen dll_Perl_sv_2pv_nolen
249# else
250# define Perl_sv_2pv dll_Perl_sv_2pv
251# endif
Bram Moolenaar895a7a42020-09-10 21:36:11 +0200252# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
253# define Perl_sv_2pvbyte_flags dll_Perl_sv_2pvbyte_flags
254# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100255# define Perl_sv_2pvbyte dll_Perl_sv_2pvbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256# define Perl_sv_bless dll_Perl_sv_bless
257# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
258# define Perl_sv_catpvn_flags dll_Perl_sv_catpvn_flags
259# else
260# define Perl_sv_catpvn dll_Perl_sv_catpvn
261# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200262# ifdef PERL589_OR_LATER
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000263# define Perl_sv_2iv_flags dll_Perl_sv_2iv_flags
264# define Perl_newXS_flags dll_Perl_newXS_flags
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266# define Perl_sv_free dll_Perl_sv_free
Bram Moolenaarccf22172008-09-01 15:56:45 +0000267# if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
268# define Perl_sv_free2 dll_Perl_sv_free2
269# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270# define Perl_sv_isa dll_Perl_sv_isa
271# define Perl_sv_magic dll_Perl_sv_magic
272# define Perl_sv_setiv dll_Perl_sv_setiv
273# define Perl_sv_setpv dll_Perl_sv_setpv
274# define Perl_sv_setpvn dll_Perl_sv_setpvn
275# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
276# define Perl_sv_setsv_flags dll_Perl_sv_setsv_flags
277# else
278# define Perl_sv_setsv dll_Perl_sv_setsv
279# endif
280# define Perl_sv_upgrade dll_Perl_sv_upgrade
281# define Perl_Tstack_sp_ptr dll_Perl_Tstack_sp_ptr
282# define Perl_Top_ptr dll_Perl_Top_ptr
283# define Perl_Tstack_base_ptr dll_Perl_Tstack_base_ptr
284# define Perl_Tstack_max_ptr dll_Perl_Tstack_max_ptr
285# define Perl_Ttmps_ix_ptr dll_Perl_Ttmps_ix_ptr
286# define Perl_Ttmps_floor_ptr dll_Perl_Ttmps_floor_ptr
287# define Perl_Tmarkstack_ptr_ptr dll_Perl_Tmarkstack_ptr_ptr
288# define Perl_Tmarkstack_max_ptr dll_Perl_Tmarkstack_max_ptr
289# define Perl_TSv_ptr dll_Perl_TSv_ptr
290# define Perl_TXpv_ptr dll_Perl_TXpv_ptr
291# define Perl_Tna_ptr dll_Perl_Tna_ptr
292# define Perl_Idefgv_ptr dll_Perl_Idefgv_ptr
293# define Perl_Ierrgv_ptr dll_Perl_Ierrgv_ptr
294# define Perl_Isv_yes_ptr dll_Perl_Isv_yes_ptr
295# define boot_DynaLoader dll_boot_DynaLoader
Bram Moolenaare06c1882010-07-21 22:05:20 +0200296# define Perl_Gthr_key_ptr dll_Perl_Gthr_key_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000298# define Perl_sys_init dll_Perl_sys_init
Bram Moolenaarc236c162008-07-13 17:41:49 +0000299# define Perl_sys_term dll_Perl_sys_term
300# define Perl_ISv_ptr dll_Perl_ISv_ptr
301# define Perl_Istack_max_ptr dll_Perl_Istack_max_ptr
302# define Perl_Istack_base_ptr dll_Perl_Istack_base_ptr
303# define Perl_Itmps_ix_ptr dll_Perl_Itmps_ix_ptr
304# define Perl_Itmps_floor_ptr dll_Perl_Itmps_floor_ptr
305# define Perl_IXpv_ptr dll_Perl_IXpv_ptr
306# define Perl_Ina_ptr dll_Perl_Ina_ptr
307# define Perl_Imarkstack_ptr_ptr dll_Perl_Imarkstack_ptr_ptr
308# define Perl_Imarkstack_max_ptr dll_Perl_Imarkstack_max_ptr
309# define Perl_Istack_sp_ptr dll_Perl_Istack_sp_ptr
310# define Perl_Iop_ptr dll_Perl_Iop_ptr
311# define Perl_call_list dll_Perl_call_list
312# define Perl_Iscopestack_ix_ptr dll_Perl_Iscopestack_ix_ptr
313# define Perl_Iunitcheckav_ptr dll_Perl_Iunitcheckav_ptr
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200314# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
315# define Perl_xs_handshake dll_Perl_xs_handshake
316# define Perl_xs_boot_epilog dll_Perl_xs_boot_epilog
317# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200318# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100319# ifdef USE_ITHREADS
320# define PL_thr_key *dll_PL_thr_key
321# endif
Bram Moolenaar01c10522012-09-21 12:50:51 +0200322# endif
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100323# define Perl_hv_iternext_flags dll_Perl_hv_iternext_flags
324# define Perl_hv_iterinit dll_Perl_hv_iterinit
325# define Perl_hv_iterkey dll_Perl_hv_iterkey
326# define Perl_hv_iterval dll_Perl_hv_iterval
327# define Perl_av_fetch dll_Perl_av_fetch
328# define Perl_av_len dll_Perl_av_len
329# define Perl_sv_2nv_flags dll_Perl_sv_2nv_flags
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200330# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
331# define PerlIOBase_pushed dll_PerlIOBase_pushed
332# define PerlIO_define_layer dll_PerlIO_define_layer
333# endif
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200334# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
335# define Perl_savetmps dll_Perl_savetmps
336# endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000337
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338/*
339 * Declare HANDLE for perl.dll and function pointers.
340 */
341static HANDLE hPerlLib = NULL;
342
343static PerlInterpreter* (*perl_alloc)();
344static void (*perl_construct)(PerlInterpreter*);
345static void (*perl_destruct)(PerlInterpreter*);
346static void (*perl_free)(PerlInterpreter*);
347static int (*perl_run)(PerlInterpreter*);
348static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
349static void* (*Perl_get_context)(void);
Bram Moolenaar864733a2016-04-02 14:18:01 +0200350static void (*Perl_croak)(pTHX_ const char*, ...) __attribute__noreturn__;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200351# ifdef PERL5101_OR_LATER
Bram Moolenaar6b107212013-12-11 15:06:40 +0100352/* Perl-5.18 has a different Perl_croak_xs_usage signature. */
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200353# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar864733a2016-04-02 14:18:01 +0200354static void (*Perl_croak_xs_usage)(const CV *const, const char *const params)
355 __attribute__noreturn__;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200356# else
Bram Moolenaar864733a2016-04-02 14:18:01 +0200357static void (*Perl_croak_xs_usage)(pTHX_ const CV *const, const char *const params)
358 __attribute__noreturn__;
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200359# endif
Bram Moolenaar6b107212013-12-11 15:06:40 +0100360# endif
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200361# ifdef PERL_IMPLICIT_CONTEXT
Bram Moolenaar864733a2016-04-02 14:18:01 +0200362static void (*Perl_croak_nocontext)(const char*, ...) __attribute__noreturn__;
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200363# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364static I32 (*Perl_dowantarray)(pTHX);
365static void (*Perl_free_tmps)(pTHX);
366static HV* (*Perl_gv_stashpv)(pTHX_ const char*, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200367# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200368static I32* (*Perl_markstack_grow)(pTHX);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200369# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370static void (*Perl_markstack_grow)(pTHX);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200371# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372static MAGIC* (*Perl_mg_find)(pTHX_ SV*, int);
Bram Moolenaar578333b2018-07-22 07:31:09 +0200373# if (PERL_REVISION == 5) && (PERL_VERSION >= 28)
374static int (*Perl_mg_get)(pTHX_ SV*);
375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376static CV* (*Perl_newXS)(pTHX_ char*, XSUBADDR_t, char*);
377static SV* (*Perl_newSV)(pTHX_ STRLEN);
378static SV* (*Perl_newSViv)(pTHX_ IV);
379static SV* (*Perl_newSVpv)(pTHX_ const char*, STRLEN);
380static I32 (*Perl_call_argv)(pTHX_ const char*, I32, char**);
381static I32 (*Perl_call_pv)(pTHX_ const char*, I32);
382static I32 (*Perl_eval_sv)(pTHX_ SV*, I32);
383static SV* (*Perl_get_sv)(pTHX_ const char*, I32);
384static SV* (*Perl_eval_pv)(pTHX_ const char*, I32);
385static SV* (*Perl_call_method)(pTHX_ const char*, I32);
386static void (*Perl_pop_scope)(pTHX);
387static void (*Perl_push_scope)(pTHX);
388static void (*Perl_save_int)(pTHX_ int*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200389# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200390static void (*Perl_save_strlen)(pTHX_ STRLEN* ptr);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200391# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392static SV** (*Perl_stack_grow)(pTHX_ SV**, SV**p, int);
393static SV** (*Perl_set_context)(void*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200394# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200395static bool (*Perl_sv_2bool_flags)(pTHX_ SV*, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200396# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200397static void (*Perl_xs_apiversion_bootcheck)(pTHX_ SV *module, const char *api_p, STRLEN api_len);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200398# endif
399# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400static bool (*Perl_sv_2bool)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402static IV (*Perl_sv_2iv)(pTHX_ SV*);
403static SV* (*Perl_sv_2mortal)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200404# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Philip H1d7caa52023-06-22 08:55:47 +0200405static char* (*Perl_sv_2pv_flags)(pTHX_ SV*, STRLEN* const, const U32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406static char* (*Perl_sv_2pv_nolen)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200407# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408static char* (*Perl_sv_2pv)(pTHX_ SV*, STRLEN*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200409# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100410static char* (*Perl_sv_2pvbyte)(pTHX_ SV*, STRLEN*);
Bram Moolenaar895a7a42020-09-10 21:36:11 +0200411# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
Philip H1d7caa52023-06-22 08:55:47 +0200412static char* (*Perl_sv_2pvbyte_flags)(pTHX_ SV*, STRLEN* const, const U32);
Bram Moolenaar895a7a42020-09-10 21:36:11 +0200413# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414static SV* (*Perl_sv_bless)(pTHX_ SV*, HV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200415# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416static void (*Perl_sv_catpvn_flags)(pTHX_ SV* , const char*, STRLEN, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200417# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418static void (*Perl_sv_catpvn)(pTHX_ SV*, const char*, STRLEN);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200419# endif
420# ifdef PERL589_OR_LATER
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000421static IV (*Perl_sv_2iv_flags)(pTHX_ SV* sv, I32 flags);
422static CV * (*Perl_newXS_flags)(pTHX_ const char *name, XSUBADDR_t subaddr, const char *const filename, const char *const proto, U32 flags);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424static void (*Perl_sv_free)(pTHX_ SV*);
425static int (*Perl_sv_isa)(pTHX_ SV*, const char*);
426static void (*Perl_sv_magic)(pTHX_ SV*, SV*, int, const char*, I32);
427static void (*Perl_sv_setiv)(pTHX_ SV*, IV);
428static void (*Perl_sv_setpv)(pTHX_ SV*, const char*);
429static void (*Perl_sv_setpvn)(pTHX_ SV*, const char*, STRLEN);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200430# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431static void (*Perl_sv_setsv_flags)(pTHX_ SV*, SV*, I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200432# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433static void (*Perl_sv_setsv)(pTHX_ SV*, SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435static bool (*Perl_sv_upgrade)(pTHX_ SV*, U32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200436# if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437static SV*** (*Perl_Tstack_sp_ptr)(register PerlInterpreter*);
438static OP** (*Perl_Top_ptr)(register PerlInterpreter*);
439static SV*** (*Perl_Tstack_base_ptr)(register PerlInterpreter*);
440static SV*** (*Perl_Tstack_max_ptr)(register PerlInterpreter*);
441static I32* (*Perl_Ttmps_ix_ptr)(register PerlInterpreter*);
442static I32* (*Perl_Ttmps_floor_ptr)(register PerlInterpreter*);
443static I32** (*Perl_Tmarkstack_ptr_ptr)(register PerlInterpreter*);
444static I32** (*Perl_Tmarkstack_max_ptr)(register PerlInterpreter*);
445static SV** (*Perl_TSv_ptr)(register PerlInterpreter*);
446static XPV** (*Perl_TXpv_ptr)(register PerlInterpreter*);
447static STRLEN* (*Perl_Tna_ptr)(register PerlInterpreter*);
Bram Moolenaar6b107212013-12-11 15:06:40 +0100448# else
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200449/* Perl-5.18 has a different Perl_sv_free2 signature. */
450# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
451static void (*Perl_sv_free2)(pTHX_ SV*, const U32);
452# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000453static void (*Perl_sv_free2)(pTHX_ SV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200454# endif
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000455static void (*Perl_sys_init)(int* argc, char*** argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000456static void (*Perl_sys_term)(void);
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200457static void (*Perl_call_list)(pTHX_ I32, AV*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200458# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
459# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000460static SV** (*Perl_ISv_ptr)(register PerlInterpreter*);
461static SV*** (*Perl_Istack_max_ptr)(register PerlInterpreter*);
462static SV*** (*Perl_Istack_base_ptr)(register PerlInterpreter*);
463static XPV** (*Perl_IXpv_ptr)(register PerlInterpreter*);
464static I32* (*Perl_Itmps_ix_ptr)(register PerlInterpreter*);
465static I32* (*Perl_Itmps_floor_ptr)(register PerlInterpreter*);
466static STRLEN* (*Perl_Ina_ptr)(register PerlInterpreter*);
467static I32** (*Perl_Imarkstack_ptr_ptr)(register PerlInterpreter*);
468static I32** (*Perl_Imarkstack_max_ptr)(register PerlInterpreter*);
469static SV*** (*Perl_Istack_sp_ptr)(register PerlInterpreter*);
470static OP** (*Perl_Iop_ptr)(register PerlInterpreter*);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000471static I32* (*Perl_Iscopestack_ix_ptr)(register PerlInterpreter*);
472static AV** (*Perl_Iunitcheckav_ptr)(register PerlInterpreter*);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200473# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200474# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200475# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200476static I32 (*Perl_xs_handshake)(const U32, void *, const char *, ...);
477static void (*Perl_xs_boot_epilog)(pTHX_ const U32);
Bram Moolenaard8619992014-03-12 17:08:05 +0100478# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200479
480# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
481# ifdef USE_ITHREADS
482static perl_key* dll_PL_thr_key;
483# endif
484# else
Bram Moolenaare06c1882010-07-21 22:05:20 +0200485static GV** (*Perl_Idefgv_ptr)(register PerlInterpreter*);
486static GV** (*Perl_Ierrgv_ptr)(register PerlInterpreter*);
487static SV* (*Perl_Isv_yes_ptr)(register PerlInterpreter*);
Bram Moolenaare06c1882010-07-21 22:05:20 +0200488static perl_key* (*Perl_Gthr_key_ptr)_((pTHX));
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200489# endif
Bram Moolenaarf5fe79a2012-09-21 12:42:44 +0200490static void (*boot_DynaLoader)_((pTHX_ CV*));
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100491static HE * (*Perl_hv_iternext_flags)(pTHX_ HV *, I32);
492static I32 (*Perl_hv_iterinit)(pTHX_ HV *);
493static char * (*Perl_hv_iterkey)(pTHX_ HE *, I32 *);
494static SV * (*Perl_hv_iterval)(pTHX_ HV *, HE *);
495static SV** (*Perl_av_fetch)(pTHX_ AV *, SSize_t, I32);
496static SSize_t (*Perl_av_len)(pTHX_ AV *);
497static NV (*Perl_sv_2nv_flags)(pTHX_ SV *const, const I32);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200498# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200499static IV (*PerlIOBase_pushed)(pTHX_ PerlIO *, const char *, SV *, PerlIO_funcs *);
500static void (*PerlIO_define_layer)(pTHX_ PerlIO_funcs *);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200501# endif
502# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200503static void (*Perl_savetmps)(pTHX);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200504# endif
Bram Moolenaare06c1882010-07-21 22:05:20 +0200505
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506/*
507 * Table of name to function pointer of perl.
508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509static struct {
510 char* name;
511 PERL_PROC* ptr;
512} perl_funcname_table[] = {
513 {"perl_alloc", (PERL_PROC*)&perl_alloc},
514 {"perl_construct", (PERL_PROC*)&perl_construct},
515 {"perl_destruct", (PERL_PROC*)&perl_destruct},
516 {"perl_free", (PERL_PROC*)&perl_free},
517 {"perl_run", (PERL_PROC*)&perl_run},
518 {"perl_parse", (PERL_PROC*)&perl_parse},
519 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
520 {"Perl_croak", (PERL_PROC*)&Perl_croak},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200521# ifdef PERL5101_OR_LATER
Bram Moolenaar3a0573a2010-02-17 16:40:58 +0100522 {"Perl_croak_xs_usage", (PERL_PROC*)&Perl_croak_xs_usage},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200523# endif
524# ifdef PERL_IMPLICIT_CONTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
528 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
529 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
530 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
531 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
Bram Moolenaar578333b2018-07-22 07:31:09 +0200532# if (PERL_REVISION == 5) && (PERL_VERSION >= 28)
533 {"Perl_mg_get", (PERL_PROC*)&Perl_mg_get},
534# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
536 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
537 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
538 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
539 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
540 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
541 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
542 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
543 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
544 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
545 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
546 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
547 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200548# if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
Bram Moolenaar0e6c5ef2014-06-12 16:03:28 +0200549 {"Perl_save_strlen", (PERL_PROC*)&Perl_save_strlen},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
552 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200553# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200554 {"Perl_sv_2bool_flags", (PERL_PROC*)&Perl_sv_2bool_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200555# if (PERL_REVISION == 5) && (PERL_VERSION < 22)
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200556 {"Perl_xs_apiversion_bootcheck",(PERL_PROC*)&Perl_xs_apiversion_bootcheck},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200557# endif
558# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200560# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
562 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200563# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
565 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200566# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200568# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100569 {"Perl_sv_2pvbyte", (PERL_PROC*)&Perl_sv_2pvbyte},
Bram Moolenaar895a7a42020-09-10 21:36:11 +0200570# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
571 {"Perl_sv_2pvbyte_flags", (PERL_PROC*)&Perl_sv_2pvbyte_flags},
572# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200573# ifdef PERL589_OR_LATER
Bram Moolenaar700d1d72007-09-13 13:20:16 +0000574 {"Perl_sv_2iv_flags", (PERL_PROC*)&Perl_sv_2iv_flags},
575 {"Perl_newXS_flags", (PERL_PROC*)&Perl_newXS_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200576# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200578# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200580# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200582# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
584 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
585 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
586 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
587 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
588 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200589# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200591# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200593# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200595# if (PERL_REVISION == 5) && (PERL_VERSION < 10)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
597 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
598 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
599 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
600 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
601 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
602 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
603 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
604 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
605 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
606 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200607# else
Bram Moolenaarccf22172008-09-01 15:56:45 +0000608 {"Perl_sv_free2", (PERL_PROC*)&Perl_sv_free2},
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000609 {"Perl_sys_init", (PERL_PROC*)&Perl_sys_init},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000610 {"Perl_sys_term", (PERL_PROC*)&Perl_sys_term},
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200611 {"Perl_call_list", (PERL_PROC*)&Perl_call_list},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200612# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
613# else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000614 {"Perl_ISv_ptr", (PERL_PROC*)&Perl_ISv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000615 {"Perl_Istack_max_ptr", (PERL_PROC*)&Perl_Istack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200616 {"Perl_Istack_base_ptr", (PERL_PROC*)&Perl_Istack_base_ptr},
617 {"Perl_IXpv_ptr", (PERL_PROC*)&Perl_IXpv_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000618 {"Perl_Itmps_ix_ptr", (PERL_PROC*)&Perl_Itmps_ix_ptr},
619 {"Perl_Itmps_floor_ptr", (PERL_PROC*)&Perl_Itmps_floor_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200620 {"Perl_Ina_ptr", (PERL_PROC*)&Perl_Ina_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000621 {"Perl_Imarkstack_ptr_ptr", (PERL_PROC*)&Perl_Imarkstack_ptr_ptr},
622 {"Perl_Imarkstack_max_ptr", (PERL_PROC*)&Perl_Imarkstack_max_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200623 {"Perl_Istack_sp_ptr", (PERL_PROC*)&Perl_Istack_sp_ptr},
624 {"Perl_Iop_ptr", (PERL_PROC*)&Perl_Iop_ptr},
Bram Moolenaarc236c162008-07-13 17:41:49 +0000625 {"Perl_Iscopestack_ix_ptr", (PERL_PROC*)&Perl_Iscopestack_ix_ptr},
626 {"Perl_Iunitcheckav_ptr", (PERL_PROC*)&Perl_Iunitcheckav_ptr},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200627# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200628# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200629# if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
Bram Moolenaar367fbf12015-06-25 16:13:46 +0200630 {"Perl_xs_handshake", (PERL_PROC*)&Perl_xs_handshake},
631 {"Perl_xs_boot_epilog", (PERL_PROC*)&Perl_xs_boot_epilog},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200632# endif
633# if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
Bram Moolenaard8619992014-03-12 17:08:05 +0100634# ifdef USE_ITHREADS
Bram Moolenaar01c10522012-09-21 12:50:51 +0200635 {"PL_thr_key", (PERL_PROC*)&dll_PL_thr_key},
Bram Moolenaard8619992014-03-12 17:08:05 +0100636# endif
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200637# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
639 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
640 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
Bram Moolenaare06c1882010-07-21 22:05:20 +0200641 {"Perl_Gthr_key_ptr", (PERL_PROC*)&Perl_Gthr_key_ptr},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200642# endif
Bram Moolenaar6dfff542011-09-07 18:47:23 +0200643 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100644 {"Perl_hv_iternext_flags", (PERL_PROC*)&Perl_hv_iternext_flags},
645 {"Perl_hv_iterinit", (PERL_PROC*)&Perl_hv_iterinit},
646 {"Perl_hv_iterkey", (PERL_PROC*)&Perl_hv_iterkey},
647 {"Perl_hv_iterval", (PERL_PROC*)&Perl_hv_iterval},
648 {"Perl_av_fetch", (PERL_PROC*)&Perl_av_fetch},
649 {"Perl_av_len", (PERL_PROC*)&Perl_av_len},
650 {"Perl_sv_2nv_flags", (PERL_PROC*)&Perl_sv_2nv_flags},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200651# if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200652 {"PerlIOBase_pushed", (PERL_PROC*)&PerlIOBase_pushed},
653 {"PerlIO_define_layer", (PERL_PROC*)&PerlIO_define_layer},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200654# endif
655# if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200656 {"Perl_savetmps", (PERL_PROC*)&Perl_savetmps},
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200657# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {"", NULL},
659};
660
Bram Moolenaar6b107212013-12-11 15:06:40 +0100661/* Work around for perl-5.18.
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200662 * For now, only the definitions of S_SvREFCNT_dec are needed in
663 * "perl\lib\CORE\inline.h". */
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200664# if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
Bram Moolenaar6727bf82016-05-26 22:10:00 +0200665static void
666S_SvREFCNT_dec(pTHX_ SV *sv)
667{
668 if (LIKELY(sv != NULL)) {
669 U32 rc = SvREFCNT(sv);
670 if (LIKELY(rc > 1))
671 SvREFCNT(sv) = rc - 1;
672 else
673 Perl_sv_free2(aTHX_ sv, rc);
674 }
675}
Bram Moolenaareeb50ab2016-06-26 17:19:46 +0200676# endif
Bram Moolenaar6b107212013-12-11 15:06:40 +0100677
Bram Moolenaarf5433fb2020-06-21 20:06:54 +0200678/* perl-5.32 needs Perl_SvREFCNT_dec */
679# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
680# define Perl_SvREFCNT_dec S_SvREFCNT_dec
681# endif
682
Bram Moolenaarfa4161c2017-06-10 15:46:23 +0200683/* perl-5.26 also needs S_TOPMARK and S_POPMARK. */
684# if (PERL_REVISION == 5) && (PERL_VERSION >= 26)
685PERL_STATIC_INLINE I32
686S_TOPMARK(pTHX)
687{
688 DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
689 "MARK top %p %" IVdf "\n",
690 PL_markstack_ptr,
691 (IV)*PL_markstack_ptr)));
692 return *PL_markstack_ptr;
693}
694
695PERL_STATIC_INLINE I32
696S_POPMARK(pTHX)
697{
698 DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
699 "MARK pop %p %" IVdf "\n",
700 (PL_markstack_ptr-1),
701 (IV)*(PL_markstack_ptr-1))));
702 assert((PL_markstack_ptr > PL_markstack) || !"MARK underflow");
703 return *PL_markstack_ptr--;
704}
705# endif
706
Bram Moolenaarf5433fb2020-06-21 20:06:54 +0200707/* perl-5.32 needs Perl_POPMARK */
708# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
709# define Perl_POPMARK S_POPMARK
ichizok54346712021-05-27 18:05:14 +0200710# endif
711
K.Takata32f586e2023-08-13 10:15:05 +0200712# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
713PERL_STATIC_INLINE U8
714Perl_gimme_V(pTHX)
715{
716 I32 cxix;
717 U8 gimme = (PL_op->op_flags & OPf_WANT);
718
719 if (gimme)
720 return gimme;
721 cxix = PL_curstackinfo->si_cxsubix;
722 if (cxix < 0)
723 return
724# if (PERL_REVISION == 5) && (PERL_VERSION >= 34)
725 PL_curstackinfo->si_type == PERLSI_SORT ? G_SCALAR:
726# endif
727 G_VOID;
728 assert(cxstack[cxix].blk_gimme & G_WANT);
729 return (cxstack[cxix].blk_gimme & G_WANT);
730}
731# endif
732
733# if (PERL_REVISION == 5) && (PERL_VERSION >= 38)
734# define PERL_ARGS_ASSERT_SVPVXTRUE \
735 assert(sv)
736PERL_STATIC_INLINE bool
737Perl_SvPVXtrue(pTHX_ SV *sv)
738{
739 PERL_ARGS_ASSERT_SVPVXTRUE;
740
741 if (! (XPV *) SvANY(sv)) {
742 return false;
743 }
744
745 if ( ((XPV *) SvANY(sv))->xpv_cur > 1) { /* length > 1 */
746 return true;
747 }
748
749 if (( (XPV *) SvANY(sv))->xpv_cur == 0) {
750 return false;
751 }
752
753 return *sv->sv_u.svu_pv != '0';
754}
755
756# define PERL_ARGS_ASSERT_SVGETMAGIC \
757 assert(sv)
758PERL_STATIC_INLINE void
759Perl_SvGETMAGIC(pTHX_ SV *sv)
760{
761 PERL_ARGS_ASSERT_SVGETMAGIC;
762
763 if (UNLIKELY(SvGMAGICAL(sv))) {
764 mg_get(sv);
765 }
766}
767
768PERL_STATIC_INLINE char *
769Perl_SvPV_helper(pTHX_
770 SV * const sv,
771 STRLEN * const lp,
772 const U32 flags,
773 const PL_SvPVtype type,
774 char * (*non_trivial)(pTHX_ SV *, STRLEN * const, const U32),
775 const bool or_null,
776 const U32 return_flags
777 )
778{
779 /* 'type' should be known at compile time, so this is reduced to a single
780 * conditional at runtime */
781 if ( (type == SvPVbyte_type_ && SvPOK_byte_nog(sv))
782 || (type == SvPVforce_type_ && SvPOK_pure_nogthink(sv))
783 || (type == SvPVutf8_type_ && SvPOK_utf8_nog(sv))
784 || (type == SvPVnormal_type_ && SvPOK_nog(sv))
785 || (type == SvPVutf8_pure_type_ && SvPOK_utf8_pure_nogthink(sv))
786 || (type == SvPVbyte_pure_type_ && SvPOK_byte_pure_nogthink(sv))
787 ) {
788 if (lp) {
789 *lp = SvCUR(sv);
790 }
791
792 /* Similarly 'return_flags is known at compile time, so this becomes
793 * branchless */
794 if (return_flags & SV_MUTABLE_RETURN) {
795 return SvPVX_mutable(sv);
796 }
797 else if(return_flags & SV_CONST_RETURN) {
798 return (char *) SvPVX_const(sv);
799 }
800 else {
801 return SvPVX(sv);
802 }
803 }
804
805 if (or_null) { /* This is also known at compile time */
806 if (flags & SV_GMAGIC) { /* As is this */
807 SvGETMAGIC(sv);
808 }
809
810 if (! SvOK(sv)) {
811 if (lp) { /* As is this */
812 *lp = 0;
813 }
814
815 return NULL;
816 }
817 }
818
819 /* Can't trivially handle this, call the function */
820 return non_trivial(aTHX_ sv, lp, (flags|return_flags));
821}
822
823# define PERL_ARGS_ASSERT_SVNV \
824 assert(sv)
825PERL_STATIC_INLINE NV
826Perl_SvNV(pTHX_ SV *sv) {
827 PERL_ARGS_ASSERT_SVNV;
828
829 if (SvNOK_nog(sv))
830 return SvNVX(sv);
831 return sv_2nv(sv);
832}
833
834# define PERL_ARGS_ASSERT_SVIV \
835 assert(sv)
836PERL_STATIC_INLINE IV
837Perl_SvIV(pTHX_ SV *sv) {
838 PERL_ARGS_ASSERT_SVIV;
839
840 if (SvIOK_nog(sv))
841 return SvIVX(sv);
842 return sv_2iv(sv);
843}
844# endif
845
ichizok54346712021-05-27 18:05:14 +0200846/* perl-5.34 needs Perl_SvTRUE_common; used in SvTRUE_nomg_NN */
K.Takata32f586e2023-08-13 10:15:05 +0200847# if (PERL_REVISION == 5) && (PERL_VERSION >= 34)
ichizok54346712021-05-27 18:05:14 +0200848PERL_STATIC_INLINE bool
849Perl_SvTRUE_common(pTHX_ SV * sv, const bool sv_2bool_is_fallback)
850{
851 if (UNLIKELY(SvIMMORTAL_INTERP(sv)))
852 return SvIMMORTAL_TRUE(sv);
853
854 if (! SvOK(sv))
855 return FALSE;
856
857 if (SvPOK(sv))
858 return SvPVXtrue(sv);
859
860 if (SvIOK(sv))
861 return SvIVX(sv) != 0; /* casts to bool */
862
863 if (SvROK(sv) && !(SvOBJECT(SvRV(sv)) && HvAMAGIC(SvSTASH(SvRV(sv)))))
864 return TRUE;
865
866 if (sv_2bool_is_fallback)
867 return sv_2bool_nomg(sv);
868
869 return isGV_with_GP(sv);
870}
871# endif
Bram Moolenaarf5433fb2020-06-21 20:06:54 +0200872
873/* perl-5.32 needs Perl_SvTRUE */
K.Takata32f586e2023-08-13 10:15:05 +0200874# if (PERL_REVISION == 5) && (PERL_VERSION >= 32)
Bram Moolenaarf5433fb2020-06-21 20:06:54 +0200875PERL_STATIC_INLINE bool
876Perl_SvTRUE(pTHX_ SV *sv) {
877 if (!LIKELY(sv))
ichizok54346712021-05-27 18:05:14 +0200878 return FALSE;
Bram Moolenaarf5433fb2020-06-21 20:06:54 +0200879 SvGETMAGIC(sv);
880 return SvTRUE_nomg_NN(sv);
881}
882# endif
883
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884/*
885 * Make all runtime-links of perl.
886 *
Bram Moolenaar364ab2f2013-08-02 20:05:32 +0200887 * 1. Get module handle using dlopen() or vimLoadLib().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 * 2. Get pointer to perl function by GetProcAddress.
889 * 3. Repeat 2, until get all functions will be used.
890 *
891 * Parameter 'libname' provides name of DLL.
892 * Return OK or FAIL.
893 */
894 static int
895perl_runtime_link_init(char *libname, int verbose)
896{
897 int i;
898
899 if (hPerlLib != NULL)
900 return OK;
Bram Moolenaare06c1882010-07-21 22:05:20 +0200901 if ((hPerlLib = load_dll(libname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
903 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100904 semsg(_("E370: Could not load library %s"), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 return FAIL;
906 }
907 for (i = 0; perl_funcname_table[i].ptr; ++i)
908 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200909 if (!(*perl_funcname_table[i].ptr = symbol_from_dll(hPerlLib,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 perl_funcname_table[i].name)))
911 {
Bram Moolenaare06c1882010-07-21 22:05:20 +0200912 close_dll(hPerlLib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 hPerlLib = NULL;
914 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000915 semsg((const char *)_(e_could_not_load_library_function_str), perl_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 return FAIL;
917 }
918 }
919 return OK;
920}
921
922/*
923 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
924 * There were no DLL loaded, return FALSE.
925 */
926 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100927perl_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100929 return perl_runtime_link_init((char *)p_perldll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930}
931#endif /* DYNAMIC_PERL */
932
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200933#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
934static void vim_IOLayer_init(void);
935#endif
936
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937/*
938 * perl_init(): initialize perl interpreter
939 * We have to call perl_parse to initialize some structures,
940 * there's nothing to actually parse.
941 */
942 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100943perl_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944{
Bram Moolenaarc236c162008-07-13 17:41:49 +0000945 char *bootargs[] = { "VI", NULL };
946 int argc = 3;
947 static char *argv[] = { "", "-e", "" };
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
Bram Moolenaarc236c162008-07-13 17:41:49 +0000949#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaar4eac38f2008-12-03 12:18:55 +0000950 Perl_sys_init(&argc, (char***)&argv);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 perl_interp = perl_alloc();
953 perl_construct(perl_interp);
Bram Moolenaarc236c162008-07-13 17:41:49 +0000954 perl_parse(perl_interp, xs_init, argc, argv, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
956 VIM_init();
957#ifdef USE_SFIO
958 sfdisc(PerlIO_stdout(), sfdcnewvim());
959 sfdisc(PerlIO_stderr(), sfdcnewvim());
960 sfsetbuf(PerlIO_stdout(), NULL, 0);
961 sfsetbuf(PerlIO_stderr(), NULL, 0);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +0200962#elif defined(PERLIO_LAYERS)
963 vim_IOLayer_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964#endif
965}
966
967/*
Bram Moolenaar20279732020-03-29 20:51:07 +0200968 * Clean up after ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 */
970 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100971perl_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
973 if (perl_interp)
974 {
975 perl_run(perl_interp);
976 perl_destruct(perl_interp);
977 perl_free(perl_interp);
978 perl_interp = NULL;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000979#if (PERL_REVISION == 5) && (PERL_VERSION >= 10)
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100980 Perl_sys_term();
Bram Moolenaarc236c162008-07-13 17:41:49 +0000981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984
985/*
986 * msg_split(): send a message to the message handling routines
987 * split at '\n' first though.
988 */
989 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100990msg_split(
991 char_u *s,
992 int attr) /* highlighting attributes */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993{
994 char *next;
995 char *token = (char *)s;
996
Bram Moolenaaraa8494a2007-10-09 08:47:27 +0000997 while ((next = strchr(token, '\n')) && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 {
999 *next++ = '\0'; /* replace \n with \0 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01001000 msg_attr(token, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 token = next;
1002 }
Bram Moolenaaraa8494a2007-10-09 08:47:27 +00001003 if (*token && !got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001004 msg_attr(token, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005}
1006
1007#ifndef FEAT_EVAL
1008/*
1009 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
1010 * work properly.
1011 */
1012 char_u *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001013eval_to_string(
1014 char_u *arg UNUSED,
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001015 int convert UNUSED,
1016 int use_simple_function UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017{
1018 return NULL;
1019}
1020#endif
1021
1022/*
1023 * Create a new reference to an SV pointing to the SCR structure
Bram Moolenaare344bea2005-09-01 20:46:49 +00001024 * The b_perl_private/w_perl_private part of the SCR structure points to the
1025 * SV, so there can only be one such SV for a particular SCR structure. When
1026 * the last reference has gone (DESTROY is called),
1027 * b_perl_private/w_perl_private is reset; When the screen goes away before
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 * all references are gone, the value of the SV is reset;
1029 * any subsequent use of any of those reference will produce
1030 * a warning. (see typemap)
1031 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001032
1033 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001034newWINrv(SV *rv, win_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001035{
1036 sv_upgrade(rv, SVt_RV);
1037 if (ptr->w_perl_private == NULL)
1038 {
1039 ptr->w_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +01001040 sv_setiv(ptr->w_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +00001041 }
Bram Moolenaar41c363a2018-08-02 21:46:51 +02001042 SvREFCNT_inc_void_NN(ptr->w_perl_private);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001043 SvRV(rv) = ptr->w_perl_private;
1044 SvROK_on(rv);
1045 return sv_bless(rv, gv_stashpv("VIWIN", TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046}
1047
Bram Moolenaare344bea2005-09-01 20:46:49 +00001048 static SV *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001049newBUFrv(SV *rv, buf_T *ptr)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001050{
1051 sv_upgrade(rv, SVt_RV);
1052 if (ptr->b_perl_private == NULL)
1053 {
1054 ptr->b_perl_private = newSV(0);
Bram Moolenaarbe747342012-02-12 00:31:52 +01001055 sv_setiv(ptr->b_perl_private, PTR2IV(ptr));
Bram Moolenaare344bea2005-09-01 20:46:49 +00001056 }
Bram Moolenaar41c363a2018-08-02 21:46:51 +02001057 SvREFCNT_inc_void_NN(ptr->b_perl_private);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001058 SvRV(rv) = ptr->b_perl_private;
1059 SvROK_on(rv);
1060 return sv_bless(rv, gv_stashpv("VIBUF", TRUE));
1061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001063#if 0
1064SV *__sv_save[1024];
1065int __sv_save_ix;
1066# define D_Save_Sv(sv) do { if (__sv_save_ix < 1024) __sv_save[__sv_save_ix++] = (sv); } while (0)
1067#else
1068# define D_Save_Sv(sv) NOOP
1069#endif
1070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071/*
1072 * perl_win_free
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001073 * Remove all references to the window to be destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 */
1075 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001076perl_win_free(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001078 if (wp->w_perl_private && perl_interp != NULL)
1079 {
Bram Moolenaar578333b2018-07-22 07:31:09 +02001080 SV *sv = (SV*)wp->w_perl_private;
1081 D_Save_Sv(sv);
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001082 sv_setiv(sv, 0);
1083 SvREFCNT_dec(sv);
1084 }
1085 wp->w_perl_private = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086}
1087
1088 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001089perl_buf_free(buf_T *bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001091 if (bp->b_perl_private && perl_interp != NULL)
1092 {
Bram Moolenaar578333b2018-07-22 07:31:09 +02001093 SV *sv = (SV *)bp->b_perl_private;
1094 D_Save_Sv(sv);
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001095 sv_setiv(sv, 0);
1096 SvREFCNT_dec(sv);
1097 }
1098 bp->b_perl_private = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099}
1100
1101#ifndef PROTO
1102# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
1103I32 cur_val(pTHX_ IV iv, SV *sv);
1104# else
1105I32 cur_val(IV iv, SV *sv);
Bram Moolenaareeb50ab2016-06-26 17:19:46 +02001106# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107
1108/*
1109 * Handler for the magic variables $main::curwin and $main::curbuf.
1110 * The handler is put into the magic vtbl for these variables.
1111 * (This is effectively a C-level equivalent of a tied variable).
1112 * There is no "set" function as the variables are read-only.
1113 */
1114# if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
1115I32 cur_val(pTHX_ IV iv, SV *sv)
1116# else
1117I32 cur_val(IV iv, SV *sv)
1118# endif
1119{
1120 SV *rv;
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001121
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 if (iv == 0)
1123 rv = newWINrv(newSV(0), curwin);
1124 else
1125 rv = newBUFrv(newSV(0), curbuf);
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001126
Bram Moolenaar41c363a2018-08-02 21:46:51 +02001127 if (SvRV(sv) != SvRV(rv))
1128 // XXX: This magic variable is a bit confusing...
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001129 // Is currently refcounted ?
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001130 sv_setsv(sv, rv);
1131
Bram Moolenaar41c363a2018-08-02 21:46:51 +02001132 SvREFCNT_dec(rv);
1133
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 return 0;
1135}
1136#endif /* !PROTO */
1137
1138struct ufuncs cw_funcs = { cur_val, 0, 0 };
1139struct ufuncs cb_funcs = { cur_val, 0, 1 };
1140
1141/*
1142 * VIM_init(): Vim-specific initialisation.
1143 * Make the magical main::curwin and main::curbuf variables
1144 */
1145 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001146VIM_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147{
1148 static char cw[] = "main::curwin";
1149 static char cb[] = "main::curbuf";
1150 SV *sv;
1151
1152 sv = perl_get_sv(cw, TRUE);
1153 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
1154 SvREADONLY_on(sv);
1155
1156 sv = perl_get_sv(cb, TRUE);
1157 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
1158 SvREADONLY_on(sv);
1159
1160 /*
1161 * Setup the Safe compartment.
1162 * It shouldn't be a fatal error if the Safe module is missing.
1163 * XXX: Only shares the 'Msg' routine (which has to be called
1164 * like 'Msg(...)').
1165 */
1166 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
1167
1168}
1169
1170#ifdef DYNAMIC_PERL
1171static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
1172#endif
1173
1174/*
1175 * ":perl"
1176 */
1177 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001178ex_perl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179{
1180 char *err;
1181 char *script;
1182 STRLEN length;
1183 SV *sv;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +02001184#ifdef HAVE_SANDBOX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 SV *safe;
Bram Moolenaar9d6650f2010-06-06 23:04:47 +02001186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
1188 script = (char *)script_get(eap, eap->arg);
1189 if (eap->skip)
1190 {
1191 vim_free(script);
1192 return;
1193 }
1194
1195 if (perl_interp == NULL)
1196 {
1197#ifdef DYNAMIC_PERL
1198 if (!perl_enabled(TRUE))
1199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 emsg(_(e_noperl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 vim_free(script);
1202 return;
1203 }
1204#endif
1205 perl_init();
1206 }
1207
1208 {
1209 dSP;
1210 ENTER;
1211 SAVETMPS;
1212
1213 if (script == NULL)
1214 sv = newSVpv((char *)eap->arg, 0);
1215 else
1216 {
1217 sv = newSVpv(script, 0);
1218 vim_free(script);
1219 }
1220
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001221 if (sandbox || secure)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
Bram Moolenaara1711622011-07-27 14:15:46 +02001223 safe = perl_get_sv("VIM::safe", FALSE);
Bram Moolenaar3f947ea2009-07-14 14:04:54 +00001224# ifndef MAKE_TEST /* avoid a warning for unreachable code */
Bram Moolenaar954e8c52009-11-11 13:45:33 +00001225 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaare96eea72022-01-28 21:00:51 +00001226 emsg(_(e_perl_evaluation_forbidden_in_sandbox_without_safe_module));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 else
Bram Moolenaar3f947ea2009-07-14 14:04:54 +00001228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
1230 PUSHMARK(SP);
1231 XPUSHs(safe);
1232 XPUSHs(sv);
1233 PUTBACK;
1234 perl_call_method("reval", G_DISCARD);
1235 }
1236 }
1237 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1239
1240 SvREFCNT_dec(sv);
1241
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001242 err = SvPV(GvSV(PL_errgv), length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243
1244 FREETMPS;
1245 LEAVE;
1246
1247 if (!length)
1248 return;
1249
1250 msg_split((char_u *)err, highlight_attr[HLF_E]);
1251 return;
1252 }
1253}
1254
1255 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001256replace_line(linenr_T *line, linenr_T *end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257{
1258 char *str;
1259
1260 if (SvOK(GvSV(PL_defgv)))
1261 {
1262 str = SvPV(GvSV(PL_defgv), PL_na);
1263 ml_replace(*line, (char_u *)str, 1);
1264 changed_bytes(*line, 0);
1265 }
1266 else
1267 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001268 ml_delete(*line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 deleted_lines_mark(*line, 1L);
1270 --(*end);
1271 --(*line);
1272 }
1273 return OK;
1274}
1275
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001276static struct ref_map_S {
1277 void *vim_ref;
1278 SV *perl_ref;
1279 struct ref_map_S *next;
1280} *ref_map = NULL;
1281
1282 static void
1283ref_map_free(void)
1284{
1285 struct ref_map_S *tofree;
1286 struct ref_map_S *refs = ref_map;
1287
1288 while (refs) {
1289 tofree = refs;
1290 refs = refs->next;
1291 vim_free(tofree);
1292 }
1293 ref_map = NULL;
1294}
1295
1296 static struct ref_map_S *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001297ref_map_find_SV(SV *const sv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001298{
1299 struct ref_map_S *refs = ref_map;
1300 int count = 350;
1301
1302 while (refs) {
1303 if (refs->perl_ref == sv)
1304 break;
1305 refs = refs->next;
1306 count--;
1307 }
1308
1309 if (!refs && count > 0) {
1310 refs = (struct ref_map_S *)alloc(sizeof(struct ref_map_S));
1311 if (!refs)
1312 return NULL;
1313 refs->perl_ref = sv;
1314 refs->vim_ref = NULL;
1315 refs->next = ref_map;
1316 ref_map = refs;
1317 }
1318
1319 return refs;
1320}
1321
1322 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001323perl_to_vim(SV *sv, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001324{
1325 if (SvROK(sv))
1326 sv = SvRV(sv);
1327
1328 switch (SvTYPE(sv)) {
1329 case SVt_NULL:
1330 break;
1331 case SVt_NV: /* float */
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001332 rettv->v_type = VAR_FLOAT;
1333 rettv->vval.v_float = SvNV(sv);
1334 break;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001335 case SVt_IV: /* integer */
1336 if (!SvROK(sv)) { /* references should be string */
1337 rettv->vval.v_number = SvIV(sv);
1338 break;
1339 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001340 /* FALLTHROUGH */
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001341 case SVt_PV: /* string */
1342 {
1343 size_t len = 0;
1344 char * str_from = SvPV(sv, len);
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001345 char_u *str_to = (char_u*)alloc(
1346 (unsigned)(sizeof(char_u) * (len + 1)));
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001347
1348 if (str_to) {
1349 str_to[len] = '\0';
1350
1351 while (len--) {
1352 if (str_from[len] == '\0')
1353 str_to[len] = '\n';
1354 else
1355 str_to[len] = str_from[len];
1356 }
1357 }
1358
1359 rettv->v_type = VAR_STRING;
1360 rettv->vval.v_string = str_to;
1361 break;
1362 }
1363 case SVt_PVAV: /* list */
1364 {
1365 SSize_t size;
1366 listitem_T * item;
1367 SV ** item2;
1368 list_T * list;
1369 struct ref_map_S * refs;
1370
1371 if ((refs = ref_map_find_SV(sv)) == NULL)
1372 return FAIL;
1373
1374 if (refs->vim_ref)
1375 list = (list_T *) refs->vim_ref;
1376 else
1377 {
1378 if ((list = list_alloc()) == NULL)
1379 return FAIL;
1380 refs->vim_ref = list;
1381
1382 for (size = av_len((AV*)sv); size >= 0; size--)
1383 {
1384 if ((item = listitem_alloc()) == NULL)
1385 break;
1386
1387 item->li_tv.v_type = VAR_NUMBER;
1388 item->li_tv.v_lock = 0;
1389 item->li_tv.vval.v_number = 0;
1390 list_insert(list, item, list->lv_first);
1391
1392 item2 = av_fetch((AV *)sv, size, 0);
1393
1394 if (item2 == NULL || *item2 == NULL ||
Bram Moolenaard99df422016-01-29 23:20:40 +01001395 perl_to_vim(*item2, &item->li_tv) == FAIL)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001396 break;
1397 }
1398 }
1399
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001400 rettv_list_set(rettv, list);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001401 break;
1402 }
1403 case SVt_PVHV: /* dictionary */
1404 {
1405 HE * entry;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001406 I32 key_len;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001407 char * key;
1408 dictitem_T * item;
1409 SV * item2;
1410 dict_T * dict;
1411 struct ref_map_S * refs;
1412
1413 if ((refs = ref_map_find_SV(sv)) == NULL)
1414 return FAIL;
1415
1416 if (refs->vim_ref)
1417 dict = (dict_T *) refs->vim_ref;
1418 else
1419 {
1420
1421 if ((dict = dict_alloc()) == NULL)
1422 return FAIL;
1423 refs->vim_ref = dict;
1424
1425 hv_iterinit((HV *)sv);
1426
1427 for (entry = hv_iternext((HV *)sv); entry; entry = hv_iternext((HV *)sv))
1428 {
1429 key_len = 0;
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001430 key = hv_iterkey(entry, &key_len);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001431
Bram Moolenaar254ebaf2016-02-23 16:06:28 +01001432 if (!key || !key_len || strlen(key) < (size_t)key_len) {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001433 semsg("Malformed key Dictionary '%s'", key && *key ? key : "(empty)");
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001434 break;
1435 }
1436
1437 if ((item = dictitem_alloc((char_u *)key)) == NULL)
1438 break;
Bram Moolenaarc89d4b32018-07-08 17:19:02 +02001439 item->di_tv.v_type = VAR_NUMBER;
1440 item->di_tv.vval.v_number = 0;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001441
1442 if (dict_add(dict, item) == FAIL) {
1443 dictitem_free(item);
1444 break;
1445 }
1446 item2 = hv_iterval((HV *)sv, entry);
1447 if (item2 == NULL || perl_to_vim(item2, &item->di_tv) == FAIL)
1448 break;
1449 }
1450 }
1451
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001452 rettv_dict_set(rettv, dict);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001453 break;
1454 }
1455 default: /* not convertible */
1456 {
1457 char *val = SvPV_nolen(sv);
1458 rettv->v_type = VAR_STRING;
1459 rettv->vval.v_string = val ? vim_strsave((char_u *)val) : NULL;
1460 break;
1461 }
1462 }
1463 return OK;
1464}
1465
1466/*
1467 * "perleval()"
1468 */
1469 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001470do_perleval(char_u *str, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001471{
1472 char *err = NULL;
1473 STRLEN err_len = 0;
1474 SV *sv = NULL;
1475#ifdef HAVE_SANDBOX
1476 SV *safe;
1477#endif
1478
1479 if (perl_interp == NULL)
1480 {
1481#ifdef DYNAMIC_PERL
1482 if (!perl_enabled(TRUE))
1483 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001484 emsg(_(e_noperl));
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001485 return;
1486 }
1487#endif
1488 perl_init();
1489 }
1490
1491 {
1492 dSP;
1493 ENTER;
1494 SAVETMPS;
1495
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001496 if (sandbox || secure)
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001497 {
1498 safe = get_sv("VIM::safe", FALSE);
1499# ifndef MAKE_TEST /* avoid a warning for unreachable code */
1500 if (safe == NULL || !SvTRUE(safe))
Bram Moolenaare96eea72022-01-28 21:00:51 +00001501 emsg(_(e_perl_evaluation_forbidden_in_sandbox_without_safe_module));
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001502 else
1503# endif
1504 {
1505 sv = newSVpv((char *)str, 0);
1506 PUSHMARK(SP);
1507 XPUSHs(safe);
1508 XPUSHs(sv);
1509 PUTBACK;
1510 call_method("reval", G_SCALAR);
1511 SPAGAIN;
1512 SvREFCNT_dec(sv);
1513 sv = POPs;
Bram Moolenaar0f267622022-05-10 13:32:24 +01001514 PUTBACK;
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001515 }
1516 }
1517 else
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001518 sv = eval_pv((char *)str, 0);
1519
1520 if (sv) {
1521 perl_to_vim(sv, rettv);
1522 ref_map_free();
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001523 err = SvPV(GvSV(PL_errgv), err_len);
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001524 }
Bram Moolenaare9b892e2016-01-17 21:15:58 +01001525 FREETMPS;
1526 LEAVE;
1527 }
1528 if (err_len)
1529 msg_split((char_u *)err, highlight_attr[HLF_E]);
1530}
1531
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532/*
1533 * ":perldo".
1534 */
1535 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01001536ex_perldo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
1538 STRLEN length;
1539 SV *sv;
1540 char *str;
1541 linenr_T i;
Bram Moolenaar85b57432017-01-29 22:59:12 +01001542 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001544 if (BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 return;
1546
1547 if (perl_interp == NULL)
1548 {
1549#ifdef DYNAMIC_PERL
1550 if (!perl_enabled(TRUE))
1551 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001552 emsg(_(e_noperl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 return;
1554 }
1555#endif
1556 perl_init();
1557 }
1558 {
1559 dSP;
1560 length = strlen((char *)eap->arg);
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001561 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
1562 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 sv_catpvn(sv, (char *)eap->arg, length);
1564 sv_catpvn(sv, "}", 1);
1565 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
1566 SvREFCNT_dec(sv);
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001567 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 if (length)
1569 goto err;
1570
1571 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
1572 return;
1573
1574 ENTER;
1575 SAVETMPS;
1576 for (i = eap->line1; i <= eap->line2; i++)
1577 {
Bram Moolenaar85b57432017-01-29 22:59:12 +01001578 /* Check the line number, the command my have deleted lines. */
1579 if (i > curbuf->b_ml.ml_line_count)
1580 break;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00001581 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 PUSHMARK(sp);
1583 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
Bram Moolenaar7b61bf12016-06-26 17:16:51 +02001584 str = SvPV(GvSV(PL_errgv), length);
Bram Moolenaar85b57432017-01-29 22:59:12 +01001585 if (length || curbuf != was_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 break;
1587 SPAGAIN;
1588 if (SvTRUEx(POPs))
1589 {
1590 if (replace_line(&i, &eap->line2) != OK)
1591 {
1592 PUTBACK;
1593 break;
1594 }
1595 }
1596 PUTBACK;
1597 }
1598 FREETMPS;
1599 LEAVE;
1600 check_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001601 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 if (!length)
1603 return;
1604
1605err:
1606 msg_split((char_u *)str, highlight_attr[HLF_E]);
1607 return;
1608 }
1609}
1610
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001611#if defined(PERLIO_LAYERS) && !defined(USE_SFIO)
1612typedef struct {
1613 struct _PerlIO base;
1614 int attr;
1615} PerlIOVim;
1616
1617 static IV
1618PerlIOVim_pushed(pTHX_ PerlIO *f, const char *mode,
1619 SV *arg, PerlIO_funcs *tab)
1620{
1621 PerlIOVim *s = PerlIOSelf(f, PerlIOVim);
1622 s->attr = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001623 if (arg && SvPOK(arg))
1624 s->attr = syn_name2attr((char_u *)SvPV_nolen(arg));
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001625 return PerlIOBase_pushed(aTHX_ f, mode, (SV *)NULL, tab);
1626}
1627
1628 static SSize_t
1629PerlIOVim_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1630{
1631 char_u *str;
1632 PerlIOVim * s = PerlIOSelf(f, PerlIOVim);
1633
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001634 str = vim_strnsave((char_u *)vbuf, count);
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001635 if (str == NULL)
1636 return 0;
1637 msg_split((char_u *)str, s->attr);
1638 vim_free(str);
1639
Bram Moolenaar9b0ac222016-06-01 20:31:43 +02001640 return (SSize_t)count;
Bram Moolenaar6244a0f2016-04-14 14:09:25 +02001641}
1642
1643static PERLIO_FUNCS_DECL(PerlIO_Vim) = {
1644 sizeof(PerlIO_funcs),
1645 "Vim",
1646 sizeof(PerlIOVim),
1647 PERLIO_K_DUMMY, /* flags */
1648 PerlIOVim_pushed,
1649 NULL, /* popped */
1650 NULL, /* open */
1651 NULL, /* binmode */
1652 NULL, /* arg */
1653 NULL, /* fileno */
1654 NULL, /* dup */
1655 NULL, /* read */
1656 NULL, /* unread */
1657 PerlIOVim_write,
1658 NULL, /* seek */
1659 NULL, /* tell */
1660 NULL, /* close */
1661 NULL, /* flush */
1662 NULL, /* fill */
1663 NULL, /* eof */
1664 NULL, /* error */
1665 NULL, /* clearerr */
1666 NULL, /* setlinebuf */
1667 NULL, /* get_base */
1668 NULL, /* get_bufsiz */
1669 NULL, /* get_ptr */
1670 NULL, /* get_cnt */
1671 NULL /* set_ptrcnt */
1672};
1673
1674/* Use Vim routine for print operator */
1675 static void
1676vim_IOLayer_init(void)
1677{
1678 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_Vim));
1679 (void)eval_pv( "binmode(STDOUT, ':Vim')"
1680 " && binmode(STDERR, ':Vim(ErrorMsg)');", 0);
1681}
1682#endif /* PERLIO_LAYERS && !USE_SFIO */
1683
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684XS(boot_VIM);
1685
1686 static void
1687xs_init(pTHX)
1688{
1689 char *file = __FILE__;
1690
1691 /* DynaLoader is a special case */
1692 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1693 newXS("VIM::bootstrap", boot_VIM, file);
1694}
1695
1696typedef win_T * VIWIN;
1697typedef buf_T * VIBUF;
1698
1699MODULE = VIM PACKAGE = VIM
1700
1701void
1702Msg(text, hl=NULL)
1703 char *text;
1704 char *hl;
1705
1706 PREINIT:
1707 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708
1709 PPCODE:
1710 if (text != NULL)
1711 {
1712 attr = 0;
1713 if (hl != NULL)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001714 attr = syn_name2attr((char_u *)hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 msg_split((char_u *)text, attr);
1716 }
1717
1718void
1719SetOption(line)
1720 char *line;
1721
1722 PPCODE:
1723 if (line != NULL)
1724 do_set((char_u *)line, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001725 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
1727void
1728DoCommand(line)
1729 char *line;
1730
1731 PPCODE:
1732 if (line != NULL)
1733 do_cmdline_cmd((char_u *)line);
1734
1735void
1736Eval(str)
1737 char *str;
1738
1739 PREINIT:
1740 char_u *value;
1741 PPCODE:
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001742 value = eval_to_string((char_u *)str, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if (value == NULL)
1744 {
1745 XPUSHs(sv_2mortal(newSViv(0)));
1746 XPUSHs(sv_2mortal(newSVpv("", 0)));
1747 }
1748 else
1749 {
1750 XPUSHs(sv_2mortal(newSViv(1)));
1751 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
1752 vim_free(value);
1753 }
1754
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001755SV*
1756Blob(SV* sv)
1757 PREINIT:
Bram Moolenaarb1443b42019-01-13 23:51:14 +01001758 STRLEN len;
1759 char *s;
1760 unsigned i;
1761 char buf[3];
1762 SV* newsv;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001763
1764 CODE:
1765 s = SvPVbyte(sv, len);
1766 newsv = newSVpv("0z", 2);
1767 for (i = 0; i < len; i++)
1768 {
Bram Moolenaar2472ae82019-02-23 15:04:17 +01001769 sprintf(buf, "%02X", (unsigned char)(s[i]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001770 sv_catpvn(newsv, buf, 2);
1771 }
1772 RETVAL = newsv;
1773 OUTPUT:
1774 RETVAL
1775
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776void
1777Buffers(...)
1778
1779 PREINIT:
1780 buf_T *vimbuf;
1781 int i, b;
1782
1783 PPCODE:
1784 if (items == 0)
1785 {
Philip H1d7caa52023-06-22 08:55:47 +02001786 if (GIMME_V == G_SCALAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 {
1788 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001789 FOR_ALL_BUFFERS(vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 ++i;
1791
1792 XPUSHs(sv_2mortal(newSViv(i)));
1793 }
1794 else
1795 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001796 FOR_ALL_BUFFERS(vimbuf)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001797 XPUSHs(sv_2mortal(newBUFrv(newSV(0), vimbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 }
1799 }
1800 else
1801 {
1802 for (i = 0; i < items; i++)
1803 {
1804 SV *sv = ST(i);
1805 if (SvIOK(sv))
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001806 b = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 else
1808 {
1809 char_u *pat;
1810 STRLEN len;
1811
1812 pat = (char_u *)SvPV(sv, len);
1813 ++emsg_off;
Bram Moolenaar16896a12018-03-06 12:25:56 +01001814 b = buflist_findpat(pat, pat + len, TRUE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 --emsg_off;
1816 }
1817
1818 if (b >= 0)
1819 {
1820 vimbuf = buflist_findnr(b);
1821 if (vimbuf)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001822 XPUSHs(sv_2mortal(newBUFrv(newSV(0), vimbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 }
1824 }
1825 }
1826
1827void
1828Windows(...)
1829
1830 PREINIT:
1831 win_T *vimwin;
1832 int i, w;
1833
1834 PPCODE:
1835 if (items == 0)
1836 {
Philip H1d7caa52023-06-22 08:55:47 +02001837 if (GIMME_V == G_SCALAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 XPUSHs(sv_2mortal(newSViv(win_count())));
1839 else
1840 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001841 FOR_ALL_WINDOWS(vimwin)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001842 XPUSHs(sv_2mortal(newWINrv(newSV(0), vimwin)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
1844 }
1845 else
1846 {
1847 for (i = 0; i < items; i++)
1848 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001849 w = (int) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 vimwin = win_find_nr(w);
1851 if (vimwin)
Bram Moolenaar18c4f1b2018-07-16 17:45:38 +02001852 XPUSHs(sv_2mortal(newWINrv(newSV(0), vimwin)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854 }
1855
1856MODULE = VIM PACKAGE = VIWIN
1857
1858void
1859DESTROY(win)
1860 VIWIN win
1861
1862 CODE:
1863 if (win_valid(win))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001864 win->w_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
1866SV *
1867Buffer(win)
1868 VIWIN win
1869
1870 CODE:
1871 if (!win_valid(win))
1872 win = curwin;
1873 RETVAL = newBUFrv(newSV(0), win->w_buffer);
1874 OUTPUT:
1875 RETVAL
1876
1877void
1878SetHeight(win, height)
1879 VIWIN win
1880 int height;
1881
1882 PREINIT:
1883 win_T *savewin;
1884
1885 PPCODE:
1886 if (!win_valid(win))
1887 win = curwin;
1888 savewin = curwin;
1889 curwin = win;
1890 win_setheight(height);
1891 curwin = savewin;
1892
1893void
Bram Moolenaar864733a2016-04-02 14:18:01 +02001894Cursor(win, ...)
1895 VIWIN win
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
1897 PPCODE:
Bram Moolenaara1711622011-07-27 14:15:46 +02001898 if (items == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 {
1900 EXTEND(sp, 2);
1901 if (!win_valid(win))
1902 win = curwin;
1903 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
1904 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
1905 }
Bram Moolenaara1711622011-07-27 14:15:46 +02001906 else if (items == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
1908 int lnum, col;
1909
1910 if (!win_valid(win))
1911 win = curwin;
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001912 lnum = (int) SvIV(ST(1));
1913 col = (int) SvIV(ST(2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 win->w_cursor.lnum = lnum;
1915 win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02001916 win->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 check_cursor(); /* put cursor on an existing line */
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001918 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 }
1920
1921MODULE = VIM PACKAGE = VIBUF
1922
1923void
1924DESTROY(vimbuf)
1925 VIBUF vimbuf;
1926
1927 CODE:
1928 if (buf_valid(vimbuf))
Bram Moolenaare344bea2005-09-01 20:46:49 +00001929 vimbuf->b_perl_private = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
1931void
1932Name(vimbuf)
1933 VIBUF vimbuf;
1934
1935 PPCODE:
1936 if (!buf_valid(vimbuf))
1937 vimbuf = curbuf;
1938 /* No file name returns an empty string */
1939 if (vimbuf->b_fname == NULL)
1940 XPUSHs(sv_2mortal(newSVpv("", 0)));
1941 else
1942 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
1943
1944void
1945Number(vimbuf)
1946 VIBUF vimbuf;
1947
1948 PPCODE:
1949 if (!buf_valid(vimbuf))
1950 vimbuf = curbuf;
1951 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
1952
1953void
1954Count(vimbuf)
1955 VIBUF vimbuf;
1956
1957 PPCODE:
1958 if (!buf_valid(vimbuf))
1959 vimbuf = curbuf;
1960 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
1961
1962void
1963Get(vimbuf, ...)
1964 VIBUF vimbuf;
1965
1966 PREINIT:
1967 char_u *line;
1968 int i;
1969 long lnum;
1970 PPCODE:
1971 if (buf_valid(vimbuf))
1972 {
1973 for (i = 1; i < items; i++)
1974 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001975 lnum = (long) SvIV(ST(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
1977 {
1978 line = ml_get_buf(vimbuf, lnum, FALSE);
1979 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
1980 }
1981 }
1982 }
1983
1984void
1985Set(vimbuf, ...)
1986 VIBUF vimbuf;
1987
1988 PREINIT:
1989 int i;
1990 long lnum;
1991 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 PPCODE:
1993 if (buf_valid(vimbuf))
1994 {
1995 if (items < 3)
1996 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
1997
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01001998 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 for(i = 2; i < items; i++, lnum++)
2000 {
2001 line = SvPV(ST(i),PL_na);
2002 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
2003 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00002004 aco_save_T aco;
2005
Bram Moolenaare76062c2022-11-28 18:51:43 +00002006 /* Set curwin/curbuf for "vimbuf" and save some things. */
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00002007 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002008 if (curbuf == vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00002010 /* Only when a window was found. */
2011 if (u_savesub(lnum) == OK)
2012 {
2013 ml_replace(lnum, (char_u *)line, TRUE);
2014 changed_bytes(lnum, 0);
2015 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00002016
Bram Moolenaare76062c2022-11-28 18:51:43 +00002017 /* restore curwin/curbuf and a few other things */
2018 aucmd_restbuf(&aco);
2019 /* Careful: autocommands may have made "vimbuf" invalid! */
2020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 }
2022 }
2023 }
2024
2025void
2026Delete(vimbuf, ...)
2027 VIBUF vimbuf;
2028
2029 PREINIT:
2030 long i, lnum = 0, count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 PPCODE:
2032 if (buf_valid(vimbuf))
2033 {
2034 if (items == 2)
2035 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01002036 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 count = 1;
2038 }
2039 else if (items == 3)
2040 {
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01002041 lnum = (long) SvIV(ST(1));
2042 count = (long) 1 + SvIV(ST(2)) - lnum;
Bram Moolenaara1711622011-07-27 14:15:46 +02002043 if (count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 count = 1;
Bram Moolenaara1711622011-07-27 14:15:46 +02002045 if (count < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
2047 lnum -= count;
2048 count = -count;
2049 }
2050 }
2051 if (items >= 2)
2052 {
2053 for (i = 0; i < count; i++)
2054 {
2055 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
2056 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00002057 aco_save_T aco;
2058
2059 /* set curwin/curbuf for "vimbuf" and save some things */
2060 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002061 if (curbuf == vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00002063 /* Only when a window was found. */
2064 if (u_savedel(lnum, 1) == OK)
2065 {
2066 ml_delete(lnum);
2067 check_cursor();
2068 deleted_lines_mark(lnum, 1L);
2069 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00002070
Bram Moolenaare76062c2022-11-28 18:51:43 +00002071 /* restore curwin/curbuf and a few other things */
2072 aucmd_restbuf(&aco);
2073 /* Careful: autocommands may have made "vimbuf"
2074 * invalid! */
2075 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00002076
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002077 update_curbuf(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 }
2079 }
2080 }
2081 }
2082
2083void
2084Append(vimbuf, ...)
2085 VIBUF vimbuf;
2086
2087 PREINIT:
2088 int i;
2089 long lnum;
2090 char *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 PPCODE:
2092 if (buf_valid(vimbuf))
2093 {
2094 if (items < 3)
2095 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
2096
Bram Moolenaare9d47cd2013-02-06 19:58:43 +01002097 lnum = (long) SvIV(ST(1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 for (i = 2; i < items; i++, lnum++)
2099 {
2100 line = SvPV(ST(i),PL_na);
2101 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
2102 {
Bram Moolenaar334a3bf2006-08-08 14:45:44 +00002103 aco_save_T aco;
2104
2105 /* set curwin/curbuf for "vimbuf" and save some things */
2106 aucmd_prepbuf(&aco, vimbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002107 if (curbuf == vimbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00002109 /* Only when a window for "vimbuf" was found. */
2110 if (u_inssub(lnum + 1) == OK)
2111 {
2112 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
2113 appended_lines_mark(lnum, 1L);
2114 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00002115
Bram Moolenaare76062c2022-11-28 18:51:43 +00002116 /* restore curwin/curbuf and a few other things */
2117 aucmd_restbuf(&aco);
2118 /* Careful: autocommands may have made "vimbuf" invalid! */
2119 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00002120
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002121 update_curbuf(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
2123 }
2124 }
2125
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002126#ifdef __GNUC__
2127# pragma GCC diagnostic pop
2128#endif