blob: 9b6d388a3c22fa7c29b3d7a2d134c8ebae0921a6 [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
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004 *
5 * Ruby interface by Shugo Maeda
6 * with improvements by SegPhault (Ryan Paul)
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +02007 * with improvements by Jon Maken
Bram Moolenaar071d4272004-06-13 20:20:40 +00008 *
9 * Do ":help uganda" in Vim to read copying and usage conditions.
10 * Do ":help credits" in Vim to see a list of people who contributed.
11 * See README.txt for an overview of the Vim source code.
12 */
13
Bram Moolenaar32d19c12018-09-13 17:26:54 +020014#include "protodef.h"
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020015#ifdef HAVE_CONFIG_H
16# include "auto/config.h"
17#endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020018
Bram Moolenaare2793352011-01-17 19:53:27 +010019#include <stdio.h>
20#include <string.h>
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef _WIN32
Bram Moolenaar41a41412020-01-07 21:32:19 +010023# if !defined(DYNAMIC_RUBY) || (RUBY_VERSION < 18)
Bram Moolenaar49c99fc2020-02-11 23:01:39 +010024# define NT
Bram Moolenaar071d4272004-06-13 20:20:40 +000025# endif
26# ifndef DYNAMIC_RUBY
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010027# define IMPORT // For static dll usage __declspec(dllimport)
Bram Moolenaar071d4272004-06-13 20:20:40 +000028# define RUBYEXTERN __declspec(dllimport)
29# endif
30#endif
31#ifndef RUBYEXTERN
32# define RUBYEXTERN extern
33#endif
34
Bram Moolenaar41a41412020-01-07 21:32:19 +010035#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 24
Bram Moolenaar2016ae52016-06-13 20:08:43 +020036# define USE_RUBY_INTEGER
Bram Moolenaar06469e92016-06-11 22:26:53 +020037#endif
38
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020039#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000040/*
41 * This is tricky. In ruby.h there is (inline) function rb_class_of()
42 * definition. This function use these variables. But we want function to
43 * use dll_* variables.
44 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045# define rb_cFalseClass (*dll_rb_cFalseClass)
46# define rb_cFixnum (*dll_rb_cFixnum)
Bram Moolenaar2016ae52016-06-13 20:08:43 +020047# if defined(USE_RUBY_INTEGER)
48# define rb_cInteger (*dll_rb_cInteger)
Bram Moolenaar06469e92016-06-11 22:26:53 +020049# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +010050# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +010051# define rb_cFloat (*dll_rb_cFloat)
52# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000053# define rb_cNilClass (*dll_rb_cNilClass)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010054# define rb_cString (*dll_rb_cString)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055# define rb_cSymbol (*dll_rb_cSymbol)
56# define rb_cTrueClass (*dll_rb_cTrueClass)
Bram Moolenaar41a41412020-01-07 21:32:19 +010057# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +000058/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010059 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
60 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 * defined in this file. When defined this RUBY_EXPORT it modified to
62 * "extern" and be able to avoid this problem.
63 */
64# define RUBY_EXPORT
65# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020066
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +010067#endif // ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +010069// suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +010070#if (_MSC_VER == 1200)
71# undef _WIN32_WINNT
72#endif
73
Bram Moolenaar41a41412020-01-07 21:32:19 +010074#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 19
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010075// Ruby 1.9 defines a number of static functions which use rb_num2long and
76// rb_int2big
Bram Moolenaar42d57f02010-03-10 12:47:00 +010077# define rb_num2long rb_num2long_stub
78# define rb_int2big rb_int2big_stub
79#endif
80
Bram Moolenaar41a41412020-01-07 21:32:19 +010081#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 19 \
Bram Moolenaar498af702014-03-28 21:58:21 +010082 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010083// Ruby 1.9 defines a number of static functions which use rb_fix2int and
84// rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +020085# define rb_fix2int rb_fix2int_stub
86# define rb_num2int rb_num2int_stub
87#endif
88
Bram Moolenaar41a41412020-01-07 21:32:19 +010089#if defined(DYNAMIC_RUBY) && RUBY_VERSION == 21
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010090// Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
91// rb_gc_writebarrier_unprotect_promoted if USE_RGENGC
Bram Moolenaar90140742014-11-27 17:44:08 +010092# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
93#endif
Bram Moolenaar41a41412020-01-07 21:32:19 +010094#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 22
Bram Moolenaar0c7485f2015-01-14 14:04:10 +010095# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaar0c7485f2015-01-14 14:04:10 +010096#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +010097
Bram Moolenaar41a41412020-01-07 21:32:19 +010098#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 26
Bram Moolenaarf62fc312019-01-08 20:29:32 +010099# define rb_ary_detransient rb_ary_detransient_stub
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100100#endif
101
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#include <ruby.h>
Bram Moolenaar41a41412020-01-07 21:32:19 +0100103#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100104# include <ruby/encoding.h>
105#endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100106#if RUBY_VERSION <= 18
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200107# include <st.h> // for ST_STOP and ST_CONTINUE
108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200110#undef off_t // ruby defines off_t as _int64, Mingw uses long
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#undef EXTERN
112#undef _
113
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100114// T_DATA defined both by Ruby and Mac header files, hack around it...
Bram Moolenaard0573012017-10-28 21:11:06 +0200115#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116# define __OPENTRANSPORT__
117# define __OPENTRANSPORTPROTOCOL__
118# define __OPENTRANSPORTPROVIDERS__
119#endif
120
Bram Moolenaar165641d2010-02-17 16:23:09 +0100121/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100122 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
123 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
124 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100125 * Use TypedData_XXX if available.
126 */
Bram Moolenaar41a41412020-01-07 21:32:19 +0100127#if defined(TypedData_Wrap_Struct) && (RUBY_VERSION >= 20)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100128# define USE_TYPEDDATA 1
129#endif
130
131/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200132 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100133 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
134 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
135 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
136 */
137#ifndef StringValuePtr
138# define StringValuePtr(s) STR2CSTR(s)
139#endif
140#ifndef RARRAY_LEN
141# define RARRAY_LEN(s) RARRAY(s)->len
142#endif
143#ifndef RARRAY_PTR
144# define RARRAY_PTR(s) RARRAY(s)->ptr
145#endif
146#ifndef RSTRING_LEN
147# define RSTRING_LEN(s) RSTRING(s)->len
148#endif
149#ifndef RSTRING_PTR
150# define RSTRING_PTR(s) RSTRING(s)->ptr
151#endif
152
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100153#ifdef HAVE_DUP
154# undef HAVE_DUP
155#endif
156
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#include "vim.h"
158#include "version.h"
159
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100160#ifdef DYNAMIC_RUBY
161# if !defined(MSWIN) // must come after including vim.h, where it is defined
162# include <dlfcn.h>
163# define HINSTANCE void*
164# define RUBY_PROC void*
165# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
166# define symbol_from_dll dlsym
167# define close_dll dlclose
168# else
169# define RUBY_PROC FARPROC
170# define load_dll vimLoadLib
171# define symbol_from_dll GetProcAddress
172# define close_dll FreeLibrary
173# endif
174#endif
175
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#if defined(PROTO) && !defined(FEAT_RUBY)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100177// Define these to be able to generate the function prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178# define VALUE int
179# define RUBY_DATA_FUNC int
180#endif
181
182static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200183static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184static VALUE objtbl;
185
186static VALUE mVIM;
187static VALUE cBuffer;
188static VALUE cVimWindow;
189static VALUE eDeletedBufferError;
190static VALUE eDeletedWindowError;
191
192static int ensure_ruby_initialized(void);
193static void error_print(int);
194static void ruby_io_init(void);
195static void ruby_vim_init(void);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100196static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaar41a41412020-01-07 21:32:19 +0100198#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200199# if defined(__ia64) && !defined(ruby_init_stack)
200# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
201# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#endif
203
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200204#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100205# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100206# define HINSTANCE int // for generating prototypes
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200207# endif
208
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209/*
210 * Wrapper defines
211 */
Bram Moolenaar8b430b42020-02-22 15:01:00 +0100212// Ruby 2.7 actually expands the following symbols as macro.
213# if RUBY_VERSION >= 27
214# undef rb_define_global_function
215# undef rb_define_method
216# undef rb_define_module_function
217# undef rb_define_singleton_method
218# endif
219
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200220# define rb_assoc_new dll_rb_assoc_new
221# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100222# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100223# define rb_check_type dll_rb_check_type
224# ifdef USE_TYPEDDATA
225# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100226# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200227# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100228# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100229# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100230# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
231# else
232# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
233# endif
234# else
235# define rb_data_object_alloc dll_rb_data_object_alloc
236# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200237# define rb_define_class_under dll_rb_define_class_under
238# define rb_define_const dll_rb_define_const
239# define rb_define_global_function dll_rb_define_global_function
240# define rb_define_method dll_rb_define_method
241# define rb_define_module dll_rb_define_module
242# define rb_define_module_function dll_rb_define_module_function
243# define rb_define_singleton_method dll_rb_define_singleton_method
244# define rb_define_virtual_variable dll_rb_define_virtual_variable
245# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200246# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200247# define rb_eArgError (*dll_rb_eArgError)
248# define rb_eIndexError (*dll_rb_eIndexError)
249# define rb_eRuntimeError (*dll_rb_eRuntimeError)
250# define rb_eStandardError (*dll_rb_eStandardError)
251# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaar41a41412020-01-07 21:32:19 +0100252# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200253# define rb_funcallv dll_rb_funcallv
254# else
255# define rb_funcall2 dll_rb_funcall2
256# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200257# define rb_global_variable dll_rb_global_variable
258# define rb_hash_aset dll_rb_hash_aset
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100259# define rb_hash_foreach dll_rb_hash_foreach
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200260# define rb_hash_new dll_rb_hash_new
261# define rb_inspect dll_rb_inspect
262# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200263
264// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
265// work. Not using the cache appears to be the best solution.
266# undef rb_intern
267# define rb_intern dll_rb_intern
268
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100269# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar41a41412020-01-07 21:32:19 +0100270# if RUBY_VERSION <= 18
Bram Moolenaar498af702014-03-28 21:58:21 +0100271# define rb_fix2int dll_rb_fix2int
272# define rb_num2int dll_rb_num2int
273# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200274# define rb_num2uint dll_rb_num2uint
275# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100276# define rb_num2dbl dll_rb_num2dbl
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200277# define rb_lastline_get dll_rb_lastline_get
278# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200279# define rb_protect dll_rb_protect
280# define rb_load dll_rb_load
Bram Moolenaar41a41412020-01-07 21:32:19 +0100281# if RUBY_VERSION <= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200282# define rb_num2long dll_rb_num2long
283# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100284# if RUBY_VERSION <= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200285# define rb_num2ulong dll_rb_num2ulong
286# endif
287# define rb_obj_alloc dll_rb_obj_alloc
288# define rb_obj_as_string dll_rb_obj_as_string
289# define rb_obj_id dll_rb_obj_id
290# define rb_raise dll_rb_raise
291# define rb_str_cat dll_rb_str_cat
292# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100293# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200294# define rb_str_new dll_rb_str_new
295# ifdef rb_str_new2
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100296// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200297# define need_rb_str_new_cstr 1
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100298// Ruby's headers #define rb_str_new_cstr to make use of GCC's
299// __builtin_constant_p extension.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200300# undef rb_str_new_cstr
301# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200302# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200303# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200304# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100305# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200306# define rb_string_value dll_rb_string_value
307# define rb_string_value_ptr dll_rb_string_value_ptr
308# define rb_float_new dll_rb_float_new
309# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200310# ifdef rb_ary_new4
Bram Moolenaar49c99fc2020-02-11 23:01:39 +0100311# define RB_ARY_NEW4_MACRO 1
312# undef rb_ary_new4
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200313# endif
314# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200315# define rb_ary_push dll_rb_ary_push
Bram Moolenaar41a41412020-01-07 21:32:19 +0100316# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200317# ifdef __ia64
318# define rb_ia64_bsp dll_rb_ia64_bsp
319# undef ruby_init_stack
320# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
321# else
322# define ruby_init_stack dll_ruby_init_stack
323# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200324# endif
325# else
326# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200327# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100328# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200329# define rb_errinfo dll_rb_errinfo
330# else
331# define ruby_errinfo (*dll_ruby_errinfo)
332# endif
333# define ruby_init dll_ruby_init
334# define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar4f974752019-02-17 17:44:42 +0100335# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100336# if RUBY_VERSION >= 19
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100337# define ruby_sysinit dll_ruby_sysinit
338# else
339# define NtInitialize dll_NtInitialize
340# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100341# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200342# define rb_w32_snprintf dll_rb_w32_snprintf
343# endif
344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
Bram Moolenaar41a41412020-01-07 21:32:19 +0100346# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200347# define ruby_script dll_ruby_script
348# define rb_enc_find_index dll_rb_enc_find_index
349# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100350# undef rb_enc_str_new
351# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200352# define rb_sprintf dll_rb_sprintf
353# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100354# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200355# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100356
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357/*
358 * Pointers for dynamic link
359 */
360static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200361VALUE *dll_rb_cFalseClass;
362VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200363# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200364VALUE *dll_rb_cInteger;
365# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100366# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100367VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200368# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200369VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100371VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200372VALUE *dll_rb_cSymbol;
373VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100374static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100376# ifdef USE_TYPEDDATA
377static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100380# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100381# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100382static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
383# else
384static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
385# endif
386# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
390static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
391static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
392static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
393static VALUE (*dll_rb_define_module) (const char*);
394static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
395static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
396static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
397static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200398static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399static VALUE *dll_rb_eArgError;
400static VALUE *dll_rb_eIndexError;
401static VALUE *dll_rb_eRuntimeError;
402static VALUE *dll_rb_eStandardError;
403static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100404# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200405static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
406# else
407static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409static void (*dll_rb_global_variable) (VALUE*);
410static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100411static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412static VALUE (*dll_rb_hash_new) (void);
413static VALUE (*dll_rb_inspect) (VALUE);
414static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200415static ID (*dll_rb_intern) (const char*);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100416# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200417static long (*dll_rb_fix2int) (VALUE);
418static long (*dll_rb_num2int) (VALUE);
419static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200420# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100421static double (*dll_rb_num2dbl) (VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422static VALUE (*dll_rb_lastline_get) (void);
423static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100424static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200425static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426static long (*dll_rb_num2long) (VALUE);
427static unsigned long (*dll_rb_num2ulong) (VALUE);
428static VALUE (*dll_rb_obj_alloc) (VALUE);
429static VALUE (*dll_rb_obj_as_string) (VALUE);
430static VALUE (*dll_rb_obj_id) (VALUE);
431static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100432# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200433static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200434# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
438static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
439static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200440# ifdef need_rb_str_new_cstr
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100441// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200442static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200443# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200445# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100446# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100447static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200448# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451static void (*dll_ruby_init) (void);
452static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100453# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100454# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100455static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100456# else
457static void (*dll_NtInitialize) (int*, char***);
458# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100459# if RUBY_VERSION >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200460static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200461# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200462# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100463# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100464static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
465static VALUE (*dll_rb_float_new) (double);
466static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200467static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100468static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100469# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100470static void (*dll_rb_ary_detransient) (VALUE);
471# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100472# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200473# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200474static void * (*dll_rb_ia64_bsp) (void);
475static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200476# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200477static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200478# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200479# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200480# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100481# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100482static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200483# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484
Bram Moolenaar41a41412020-01-07 21:32:19 +0100485# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100486static void (*dll_ruby_script) (const char*);
487static int (*dll_rb_enc_find_index) (const char*);
488static rb_encoding* (*dll_rb_enc_find) (const char*);
489static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
490static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100491static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100492static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200493# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100494
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100495# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100496# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100497static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100498# else
499static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
500# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100501# endif
502
Bram Moolenaar41a41412020-01-07 21:32:19 +0100503# if (RUBY_VERSION >= 19) && !defined(PROTO)
504# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100505 long
506rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200507# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100508 SIGNED_VALUE
509rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200510# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100511{
512 return dll_rb_num2long(x);
513}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100514# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100515 VALUE
516rb_int2big_stub(intptr_t x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100517# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100518 VALUE
519rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100520# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100521{
522 return dll_rb_int2big(x);
523}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100524# if (RUBY_VERSION >= 19) && (VIM_SIZEOF_INT < VIM_SIZEOF_LONG)
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100525 long
526rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200527{
528 return dll_rb_fix2int(x);
529}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100530 long
531rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200532{
533 return dll_rb_num2int(x);
534}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200535# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100536# if RUBY_VERSION >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100537 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100538rb_float_new_in_heap(double d)
539{
540 return dll_rb_float_new(d);
541}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100542# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100543 unsigned long
544rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200545# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100546 VALUE
547rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200548# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100549{
550 return (long)RSHIFT((SIGNED_VALUE)(x),1);
551}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200552# endif
553# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100554
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100555 // Do not generate a prototype here, VALUE isn't always defined.
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100556# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar41a41412020-01-07 21:32:19 +0100557# if RUBY_VERSION == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100558 void
559rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100560{
Bram Moolenaar90140742014-11-27 17:44:08 +0100561 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100562}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100563# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100564 void
565rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100566{
567 dll_rb_gc_writebarrier_unprotect(obj);
568}
569# endif
570# endif
571
Bram Moolenaar41a41412020-01-07 21:32:19 +0100572# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100573 void
574rb_ary_detransient_stub(VALUE x)
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100575{
576 dll_rb_ary_detransient(x);
577}
578# endif
579
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100580static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
582/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000583 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585static struct
586{
587 char *name;
588 RUBY_PROC *ptr;
589} ruby_funcname_table[] =
590{
591 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
592 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200593# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200594 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100595# else
596 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200597# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100598# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100599 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200600# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
602 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100603 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
605 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100606 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100608# ifdef USE_TYPEDDATA
609 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
610# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100612# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100613# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100614 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
615# else
616 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
617# endif
618# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100620# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
622 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
623 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
624 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
625 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
626 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
627 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
628 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
629 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200630 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
632 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
633 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
634 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
635 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100636# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200637 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
638# else
639 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
640# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
642 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100643 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
645 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
646 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200647 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100648# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200649 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
650 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
651 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200652# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100653 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
655 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200656 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
657 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
659 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
660 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
661 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
662 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
663 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100664# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200665 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200666# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
670 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
671 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200672# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200673 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200674# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200676# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100677# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100678 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200679# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200681# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
683 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100684# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100685# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100686 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100687# else
688 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200689# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100690# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200692# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200693# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100694# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100695 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100696# if RUBY_VERSION <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100697 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200698# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100699 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200700# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100701 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200702# ifdef RB_ARY_NEW4_MACRO
703 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
704# else
705 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
706# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100707 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100708# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100709 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
710# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200711# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100712# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100713 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100714 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
715 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
716 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
717 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
718 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100719 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100720 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200721# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100722# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200723# ifdef __ia64
724 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
725# endif
726 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
727# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100728# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100729# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100730 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100731# else
732 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
733# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100734# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 {"", NULL},
736};
737
738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 * Load library and get all pointers.
740 * Parameter 'libname' provides name of DLL.
741 * Return OK or FAIL.
742 */
743 static int
744ruby_runtime_link_init(char *libname, int verbose)
745{
746 int i;
747
748 if (hinstRuby)
749 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200750 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 if (!hinstRuby)
752 {
753 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100754 semsg(_(e_loadlib), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 return FAIL;
756 }
757
758 for (i = 0; ruby_funcname_table[i].ptr; ++i)
759 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200760 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 ruby_funcname_table[i].name)))
762 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200763 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200764 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100766 semsg(_(e_loadfunc), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 return FAIL;
768 }
769 }
770 return OK;
771}
772
773/*
774 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
775 * else FALSE.
776 */
777 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100778ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100780 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100782#endif // defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
784 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100785ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787}
788
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100789 void
790ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
792 int state;
793 char *script = NULL;
794
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000795 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 if (!eap->skip && ensure_ruby_initialized())
797 {
798 if (script == NULL)
799 rb_eval_string_protect((char *)eap->arg, &state);
800 else
801 rb_eval_string_protect(script, &state);
802 if (state)
803 error_print(state);
804 }
805 vim_free(script);
806}
807
Bram Moolenaar165641d2010-02-17 16:23:09 +0100808/*
809 * In Ruby 1.9 or later, ruby String object has encoding.
810 * conversion buffer string of vim to ruby String object using
811 * VIM encoding option.
812 */
813 static VALUE
814vim_str2rb_enc_str(const char *s)
815{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100816#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100817 int isnum;
818 long lval;
819 char_u *sval;
820 rb_encoding *enc;
821
822 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
823 if (isnum == 0)
824 {
825 enc = rb_enc_find((char *)sval);
826 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200827 if (enc)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200828 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100829 }
830#endif
831 return rb_str_new2(s);
832}
833
834 static VALUE
835eval_enc_string_protect(const char *str, int *state)
836{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100837#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100838 int isnum;
839 long lval;
840 char_u *sval;
841 rb_encoding *enc;
842 VALUE v;
843
844 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
845 if (isnum == 0)
846 {
847 enc = rb_enc_find((char *)sval);
848 vim_free(sval);
849 if (enc)
850 {
851 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
852 return rb_eval_string_protect(StringValuePtr(v), state);
853 }
854 }
855#endif
856 return rb_eval_string_protect(str, state);
857}
858
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100859 void
860ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861{
862 int state;
863 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100864 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 if (ensure_ruby_initialized())
867 {
868 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
869 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200870 for (i = eap->line1; i <= eap->line2; i++)
871 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100872 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100874 if (i > curbuf->b_ml.ml_line_count)
875 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100876 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100878 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200879 if (state)
880 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 error_print(state);
882 break;
883 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100884 if (was_curbuf != curbuf)
885 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200887 if (!NIL_P(line))
888 {
889 if (TYPE(line) != T_STRING)
890 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100891 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 return;
893 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100894 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 changed();
896#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100897 syn_changed(i); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898#endif
899 }
900 }
901 check_cursor();
902 update_curbuf(NOT_VALID);
903 }
904}
905
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100906 static VALUE
907rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100908{
909 rb_load(file_to_load, 0);
910 return Qnil;
911}
912
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100913 void
914ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915{
916 int state;
917
918 if (ensure_ruby_initialized())
919 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100920 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
921 rb_protect(rb_load_wrap, file_to_load, &state);
922 if (state)
923 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
925}
926
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100927 void
928ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000930 if (buf->b_ruby_ref)
931 {
932 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
933 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
935}
936
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100937 void
938ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000940 if (win->w_ruby_ref)
941 {
942 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
943 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
945}
946
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100947 static int
948ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949{
950 if (!ruby_initialized)
951 {
952#ifdef DYNAMIC_RUBY
953 if (ruby_enabled(TRUE))
954 {
955#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100956#ifdef MSWIN
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100957 // suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100958 int argc = 1;
959 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100960 char **argvp = argv;
Bram Moolenaar41a41412020-01-07 21:32:19 +0100961# if RUBY_VERSION >= 19
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100962 ruby_sysinit(&argc, &argvp);
963# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100964 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100965# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100966#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200967 {
Bram Moolenaar41a41412020-01-07 21:32:19 +0100968#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200969 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100970#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200971 ruby_init();
972 }
Bram Moolenaar41a41412020-01-07 21:32:19 +0100973#if RUBY_VERSION >= 19
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100974 {
975 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200976 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100977 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100978 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100979 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100980#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100982#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100983 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 ruby_vim_init();
985 ruby_initialized = 1;
986#ifdef DYNAMIC_RUBY
987 }
988 else
989 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100990 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 return 0;
992 }
993#endif
994 }
995 return ruby_initialized;
996}
997
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100998 static void
999error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000{
Bram Moolenaar41a41412020-01-07 21:32:19 +01001001#if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 RUBYEXTERN VALUE ruby_errinfo;
1003#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001004 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 VALUE eclass;
1006 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001007 VALUE bt;
1008 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001010 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
1012#define TAG_RETURN 0x1
1013#define TAG_BREAK 0x2
1014#define TAG_NEXT 0x3
1015#define TAG_RETRY 0x4
1016#define TAG_REDO 0x5
1017#define TAG_RAISE 0x6
1018#define TAG_THROW 0x7
1019#define TAG_FATAL 0x8
1020#define TAG_MASK 0xf
1021
Bram Moolenaar758535a2016-03-30 22:06:16 +02001022 switch (state)
1023 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001024 case TAG_RETURN:
1025 emsg(_("E267: unexpected return"));
1026 break;
1027 case TAG_NEXT:
1028 emsg(_("E268: unexpected next"));
1029 break;
1030 case TAG_BREAK:
1031 emsg(_("E269: unexpected break"));
1032 break;
1033 case TAG_REDO:
1034 emsg(_("E270: unexpected redo"));
1035 break;
1036 case TAG_RETRY:
1037 emsg(_("E271: retry outside of rescue clause"));
1038 break;
1039 case TAG_RAISE:
1040 case TAG_FATAL:
Bram Moolenaar41a41412020-01-07 21:32:19 +01001041#if RUBY_VERSION >= 19
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001042 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001043#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001044 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001045#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001046 eclass = CLASS_OF(error);
1047 einfo = rb_obj_as_string(error);
1048 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1049 {
1050 emsg(_("E272: unhandled exception"));
1051 }
1052 else
1053 {
1054 VALUE epath;
1055 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001057 epath = rb_class_path(eclass);
1058 vim_snprintf(buff, BUFSIZ, "%s: %s",
1059 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1060 p = strchr(buff, '\n');
1061 if (p) *p = '\0';
1062 emsg(buff);
1063 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001064
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001065 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001066#if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001067 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1068 for (i = 0; i < RARRAY_LEN(bt); i++)
1069 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001070#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001071 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1072 for (i = 0; i < RARRAY_LEN(bt); i++)
1073 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001074#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001075 break;
1076 default:
1077 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1078 emsg(buff);
1079 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 }
1081}
1082
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001083 static VALUE
1084vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
1086 char *buff, *p;
1087
1088 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001089 if (RSTRING_LEN(str) > 0)
1090 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001091 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001092 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001093 strcpy(buff, RSTRING_PTR(str));
1094 p = strchr(buff, '\n');
1095 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001096 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001097 }
1098 else
1099 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001100 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 return Qnil;
1103}
1104
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001105 static VALUE
1106vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001108 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 update_screen(NOT_VALID);
1110 return Qnil;
1111}
1112
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001113 static VALUE
1114vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001116 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 return Qnil;
1118}
1119
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001120#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001121 static VALUE
1122vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001123{
1124 VALUE result = Qnil;
1125
1126 if (tv->v_type == VAR_STRING)
1127 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001128 result = rb_str_new2(tv->vval.v_string == NULL
1129 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001130 }
1131 else if (tv->v_type == VAR_NUMBER)
1132 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001133 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001134 }
1135# ifdef FEAT_FLOAT
1136 else if (tv->v_type == VAR_FLOAT)
1137 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001138 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001139 }
1140# endif
1141 else if (tv->v_type == VAR_LIST)
1142 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001143 list_T *list = tv->vval.v_list;
1144 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001145
Bram Moolenaar639a2552010-03-17 18:15:23 +01001146 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001147
Bram Moolenaar639a2552010-03-17 18:15:23 +01001148 if (list != NULL)
1149 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001150 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001151 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001152 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001153 }
1154 else if (tv->v_type == VAR_DICT)
1155 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001156 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001157
Bram Moolenaar639a2552010-03-17 18:15:23 +01001158 if (tv->vval.v_dict != NULL)
1159 {
1160 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1161 long_u todo = ht->ht_used;
1162 hashitem_T *hi;
1163 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001164
Bram Moolenaar639a2552010-03-17 18:15:23 +01001165 for (hi = ht->ht_array; todo > 0; ++hi)
1166 {
1167 if (!HASHITEM_EMPTY(hi))
1168 {
1169 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001170
Bram Moolenaar639a2552010-03-17 18:15:23 +01001171 di = dict_lookup(hi);
1172 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001173 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001174 }
1175 }
1176 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001177 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001178 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001179 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001180 if (tv->vval.v_number == VVAL_TRUE)
1181 result = Qtrue;
1182 else if (tv->vval.v_number == VVAL_FALSE)
1183 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001184 }
1185 else if (tv->v_type == VAR_BLOB)
1186 {
1187 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1188 tv->vval.v_blob->bv_ga.ga_len);
1189 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001190 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001191
1192 return result;
1193}
1194#endif
1195
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001196 static VALUE
1197vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198{
1199#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001200 typval_T *tv;
1201 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001203 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1204 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001205 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001206 result = vim_to_ruby(tv);
1207
1208 free_tv(tv);
1209
1210 return result;
1211#else
1212 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214}
1215
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001216#ifdef USE_TYPEDDATA
1217static size_t buffer_dsize(const void *buf);
1218
1219static const rb_data_type_t buffer_type = {
1220 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001221 {0, 0, buffer_dsize,
1222# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001223 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001224# else
1225 {0, 0}
1226# endif
1227 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001228 0, 0,
1229# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1230 0,
1231# endif
1232};
1233
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001234 static size_t
1235buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001236{
1237 return sizeof(buf_T);
1238}
1239#endif
1240
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001241 static VALUE
1242buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001244 if (buf->b_ruby_ref)
1245 {
1246 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001248 else
1249 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001250#ifdef USE_TYPEDDATA
1251 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1252#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001254#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001255 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1257 return obj;
1258 }
1259}
1260
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001261 static buf_T *
1262get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263{
1264 buf_T *buf;
1265
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001266#ifdef USE_TYPEDDATA
1267 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1268#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 if (buf == NULL)
1272 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1273 return buf;
1274}
1275
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001276 static VALUE
1277vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001278{
1279 VALUE result = rb_str_new("0z", 2);
1280 char buf[4];
1281 int i;
1282 for (i = 0; i < RSTRING_LEN(str); i++)
1283 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001284 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001285 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001286 }
1287 return result;
1288}
1289
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001290 static VALUE
1291buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292{
1293 return buffer_new(curbuf);
1294}
1295
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001296 static VALUE
1297buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298{
1299 buf_T *b;
1300 int n = 0;
1301
Bram Moolenaar29323592016-07-24 22:04:11 +02001302 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001303 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001304 // Deleted buffers should not be counted
1305 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001306 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001307 n++;
1308 }
1309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 return INT2NUM(n);
1311}
1312
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001313 static VALUE
1314buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315{
1316 buf_T *b;
1317 int n = NUM2INT(num);
1318
Bram Moolenaar29323592016-07-24 22:04:11 +02001319 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001320 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001321 // Deleted buffers should not be counted
1322 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001323 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001324 continue;
1325
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001326 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001328
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001329 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 }
1331 return Qnil;
1332}
1333
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001334 static VALUE
1335buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
1337 buf_T *buf = get_buf(self);
1338
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001339 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340}
1341
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001342 static VALUE
1343buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
1345 buf_T *buf = get_buf(self);
1346
1347 return INT2NUM(buf->b_fnum);
1348}
1349
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001350 static VALUE
1351buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
1353 buf_T *buf = get_buf(self);
1354
1355 return INT2NUM(buf->b_ml.ml_line_count);
1356}
1357
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001358 static VALUE
1359get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001361 if (n <= 0 || n > buf->b_ml.ml_line_count)
1362 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1363 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364}
1365
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001366 static VALUE
1367buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
1369 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001370
1371 if (buf != NULL)
1372 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001373 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001374}
1375
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001376 static VALUE
1377set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001378{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001379 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001380 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001382 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1383 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001384 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001385 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001386
Bram Moolenaar758535a2016-03-30 22:06:16 +02001387 if (u_savesub(n) == OK)
1388 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001389 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 changed();
1391#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001392 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393#endif
1394 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001395
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001396 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001397 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001398 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001399
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 update_curbuf(NOT_VALID);
1401 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001402 else
1403 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001404 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 }
1406 return str;
1407}
1408
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001409 static VALUE
1410buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001411{
1412 buf_T *buf = get_buf(self);
1413
1414 if (buf != NULL)
1415 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1416 return str;
1417}
1418
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001419 static VALUE
1420buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001422 buf_T *buf = get_buf(self);
1423 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001424 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001426 if (n > 0 && n <= buf->b_ml.ml_line_count)
1427 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001428 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001429 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001430
Bram Moolenaar758535a2016-03-30 22:06:16 +02001431 if (u_savedel(n, 1) == OK)
1432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001434
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001435 // Changes to non-active buffers should properly refresh
1436 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001437 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001438
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 changed();
1440 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001441
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001442 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001443 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001444 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001445
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 update_curbuf(NOT_VALID);
1447 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001448 else
1449 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001450 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452 return Qnil;
1453}
1454
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001455 static VALUE
1456buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001458 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001459 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001460 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001461 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462
Bram Moolenaar3c531602010-11-16 14:46:19 +01001463 if (line == NULL)
1464 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001465 rb_raise(rb_eIndexError, "NULL line");
1466 }
1467 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001468 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001469 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001470 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001471
Bram Moolenaar758535a2016-03-30 22:06:16 +02001472 if (u_inssub(n + 1) == OK)
1473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001475
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001476 // Changes to non-active buffers should properly refresh screen
1477 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001478 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001479
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001480 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001482
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001483 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001484 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001485 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001486
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 update_curbuf(NOT_VALID);
1488 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001489 else
1490 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001491 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 }
1493 return str;
1494}
1495
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001496#ifdef USE_TYPEDDATA
1497static size_t window_dsize(const void *buf);
1498
1499static const rb_data_type_t window_type = {
1500 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001501 {0, 0, window_dsize,
1502# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001503 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001504# else
1505 {0, 0}
1506# endif
1507 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001508 0, 0,
1509# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1510 0,
1511# endif
1512};
1513
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001514 static size_t
1515window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001516{
1517 return sizeof(win_T);
1518}
1519#endif
1520
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001521 static VALUE
1522window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001524 if (win->w_ruby_ref)
1525 {
1526 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001528 else
1529 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001530#ifdef USE_TYPEDDATA
1531 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1532#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001534#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001535 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1537 return obj;
1538 }
1539}
1540
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001541 static win_T *
1542get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543{
1544 win_T *win;
1545
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001546#ifdef USE_TYPEDDATA
1547 TypedData_Get_Struct(obj, win_T, &window_type, win);
1548#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 if (win == NULL)
1552 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1553 return win;
1554}
1555
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001556 static VALUE
1557window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
1559 return window_new(curwin);
1560}
1561
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001562/*
1563 * Added line manipulation functions
1564 * SegPhault - 03/07/05
1565 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001566 static VALUE
1567line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001568{
1569 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1570}
1571
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001572 static VALUE
1573set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001574{
1575 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1576}
1577
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001578 static VALUE
1579current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001580{
1581 return INT2FIX((int)curwin->w_cursor.lnum);
1582}
1583
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001584 static VALUE
1585window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 win_T *w;
1588 int n = 0;
1589
Bram Moolenaar29323592016-07-24 22:04:11 +02001590 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 n++;
1592 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593}
1594
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001595 static VALUE
1596window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597{
1598 win_T *w;
1599 int n = NUM2INT(num);
1600
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 if (n == 0)
1603 return window_new(w);
1604 return Qnil;
1605}
1606
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001607 static VALUE
1608window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609{
1610 win_T *win = get_win(self);
1611
1612 return buffer_new(win->w_buffer);
1613}
1614
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001615 static VALUE
1616window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617{
1618 win_T *win = get_win(self);
1619
1620 return INT2NUM(win->w_height);
1621}
1622
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001623 static VALUE
1624window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
1626 win_T *win = get_win(self);
1627 win_T *savewin = curwin;
1628
1629 curwin = win;
1630 win_setheight(NUM2INT(height));
1631 curwin = savewin;
1632 return height;
1633}
1634
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001635 static VALUE
1636window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001637{
Bram Moolenaare745d752017-09-22 16:56:22 +02001638 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001639}
1640
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001641 static VALUE
1642window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001643{
1644 win_T *win = get_win(self);
1645 win_T *savewin = curwin;
1646
1647 curwin = win;
1648 win_setwidth(NUM2INT(width));
1649 curwin = savewin;
1650 return width;
1651}
1652
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001653 static VALUE
1654window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655{
1656 win_T *win = get_win(self);
1657
1658 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1659}
1660
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001661 static VALUE
1662window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
1664 VALUE lnum, col;
1665 win_T *win = get_win(self);
1666
1667 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001668 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001670 lnum = RARRAY_PTR(pos)[0];
1671 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 win->w_cursor.lnum = NUM2LONG(lnum);
1673 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001674 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001675 check_cursor(); // put cursor on an existing line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 update_screen(NOT_VALID);
1677 return Qnil;
1678}
1679
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001680 static VALUE
1681f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001682{
1683 return Qnil;
1684}
1685
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001686 static VALUE
1687f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688{
1689 int i;
1690 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001691 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
Bram Moolenaar758535a2016-03-30 22:06:16 +02001693 for (i = 0; i < argc; i++)
1694 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 if (i > 0) rb_str_cat(str, ", ", 2);
1696 rb_str_concat(str, rb_inspect(argv[i]));
1697 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001698 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001699
1700 if (argc == 1)
1701 ret = argv[0];
1702 else if (argc > 1)
1703 ret = rb_ary_new4(argc, argv);
1704 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705}
1706
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001707 static void
1708ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710#ifndef DYNAMIC_RUBY
1711 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001712 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713#endif
1714
1715 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001716 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001718 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001719 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1720 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 rb_define_global_function("p", f_p, -1);
1722}
1723
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001724 static void
1725ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726{
1727 objtbl = rb_hash_new();
1728 rb_global_variable(&objtbl);
1729
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001730 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1731 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001732 mVIM = rb_define_module("Vim");
1733 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1735 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1736 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1737 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1738 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1739 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1740 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1741 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1742 rb_define_module_function(mVIM, "message", vim_message, 1);
1743 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1744 rb_define_module_function(mVIM, "command", vim_command, 1);
1745 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001746 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
1748 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1749 rb_eStandardError);
1750 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1751 rb_eStandardError);
1752
1753 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1754 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1755 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1756 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1757 rb_define_method(cBuffer, "name", buffer_name, 0);
1758 rb_define_method(cBuffer, "number", buffer_number, 0);
1759 rb_define_method(cBuffer, "count", buffer_count, 0);
1760 rb_define_method(cBuffer, "length", buffer_count, 0);
1761 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1762 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1763 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1764 rb_define_method(cBuffer, "append", buffer_append, 2);
1765
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001766 // Added line manipulation functions
1767 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001768 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1769 rb_define_method(cBuffer, "line", line_s_current, 0);
1770 rb_define_method(cBuffer, "line=", set_current_line, 1);
1771
1772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1774 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1775 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1776 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1777 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1778 rb_define_method(cVimWindow, "height", window_height, 0);
1779 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001780 rb_define_method(cVimWindow, "width", window_width, 0);
1781 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1783 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1784
1785 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1786 rb_define_virtual_variable("$curwin", window_s_current, 0);
1787}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001788
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001789 void
1790vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001791{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001792 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001793 ruby_stack_start = stack_start;
1794}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001795
1796 static int
1797convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1798{
1799 dict_T *d = (dict_T *)arg;
1800 dictitem_T *di;
1801
1802 di = dictitem_alloc((char_u *)RSTRING_PTR(RSTRING(rb_obj_as_string(key))));
1803 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1804 || dict_add(d, di) != OK)
1805 {
1806 d->dv_hashtab.ht_error = TRUE;
1807 return ST_STOP;
1808 }
1809 return ST_CONTINUE;
1810}
1811
1812 static int
1813ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1814{
1815 switch (TYPE(val))
1816 {
1817 case T_NIL:
1818 rettv->v_type = VAR_SPECIAL;
1819 rettv->vval.v_number = VVAL_NULL;
1820 break;
1821 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001822 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001823 rettv->vval.v_number = VVAL_TRUE;
1824 break;
1825 case T_FALSE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001826 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001827 rettv->vval.v_number = VVAL_FALSE;
1828 break;
1829 case T_BIGNUM:
1830 case T_FIXNUM:
1831 rettv->v_type = VAR_NUMBER;
1832 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1833 break;
1834#ifdef FEAT_FLOAT
1835 case T_FLOAT:
1836 rettv->v_type = VAR_FLOAT;
1837 rettv->vval.v_float = (float_T)NUM2DBL(val);
1838 break;
1839#endif
1840 default:
1841 val = rb_obj_as_string(val);
1842 // FALLTHROUGH
1843 case T_STRING:
1844 {
1845 VALUE str = (VALUE)RSTRING(val);
1846
1847 rettv->v_type = VAR_STRING;
1848 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
1849 (int)RSTRING_LEN(str));
1850 }
1851 break;
1852 case T_ARRAY:
1853 {
1854 list_T *l;
1855 long i;
1856 typval_T v;
1857
1858 l = list_alloc();
1859 if (l == NULL)
1860 return FAIL;
1861
1862 for (i = 0; i < RARRAY_LEN(val); ++i)
1863 {
1864 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1865 &v) != OK)
1866 {
1867 list_unref(l);
1868 return FAIL;
1869 }
1870 list_append_tv(l, &v);
1871 clear_tv(&v);
1872 }
1873
1874 rettv->v_type = VAR_LIST;
1875 rettv->vval.v_list = l;
1876 ++l->lv_refcount;
1877 }
1878 break;
1879 case T_HASH:
1880 {
1881 dict_T *d;
1882
1883 d = dict_alloc();
1884 if (d == NULL)
1885 return FAIL;
1886
1887 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1888 if (d->dv_hashtab.ht_error)
1889 {
1890 dict_unref(d);
1891 return FAIL;
1892 }
1893
1894 rettv->v_type = VAR_DICT;
1895 rettv->vval.v_dict = d;
1896 ++d->dv_refcount;
1897 }
1898 break;
1899 }
1900 return OK;
1901}
1902
1903 void
1904do_rubyeval(char_u *str, typval_T *rettv)
1905{
1906 int retval = FAIL;
1907
1908 if (ensure_ruby_initialized())
1909 {
1910 int state;
1911 VALUE obj;
1912
1913 obj = rb_eval_string_protect((const char *)str, &state);
1914 if (state)
1915 error_print(state);
1916 else
1917 retval = ruby_convert_to_vim_value(obj, rettv);
1918 }
1919 if (retval == FAIL)
1920 {
1921 rettv->v_type = VAR_NUMBER;
1922 rettv->vval.v_number = 0;
1923 }
1924}