blob: 2480ee4d12149080d79d5299f322ddd1c7c6931a [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 Moolenaar81ea1df2020-04-11 18:01:41 +0200102// On macOS pre-installed Ruby defines "SIZEOF_TIME_T" as "SIZEOF_LONG" so it
Bram Moolenaar92c098d2020-05-26 20:09:11 +0200103// conflicts with the definition in config.h then causes a macro-redefined
104// warning.
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200105#ifdef SIZEOF_TIME_T
106# undef SIZEOF_TIME_T
107#endif
108
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#include <ruby.h>
Bram Moolenaar41a41412020-01-07 21:32:19 +0100110#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100111# include <ruby/encoding.h>
112#endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100113#if RUBY_VERSION <= 18
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200114# include <st.h> // for ST_STOP and ST_CONTINUE
115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
Bram Moolenaar92c098d2020-05-26 20:09:11 +0200117// See above.
118#ifdef SIZEOF_TIME_T
119# undef SIZEOF_TIME_T
120#endif
121
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200122#undef off_t // ruby defines off_t as _int64, Mingw uses long
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#undef EXTERN
124#undef _
125
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100126// T_DATA defined both by Ruby and Mac header files, hack around it...
Bram Moolenaard0573012017-10-28 21:11:06 +0200127#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128# define __OPENTRANSPORT__
129# define __OPENTRANSPORTPROTOCOL__
130# define __OPENTRANSPORTPROVIDERS__
131#endif
132
Bram Moolenaar165641d2010-02-17 16:23:09 +0100133/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100134 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
135 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
136 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100137 * Use TypedData_XXX if available.
138 */
Bram Moolenaar41a41412020-01-07 21:32:19 +0100139#if defined(TypedData_Wrap_Struct) && (RUBY_VERSION >= 20)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100140# define USE_TYPEDDATA 1
141#endif
142
143/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200144 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100145 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
146 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
147 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
148 */
149#ifndef StringValuePtr
150# define StringValuePtr(s) STR2CSTR(s)
151#endif
152#ifndef RARRAY_LEN
153# define RARRAY_LEN(s) RARRAY(s)->len
154#endif
155#ifndef RARRAY_PTR
156# define RARRAY_PTR(s) RARRAY(s)->ptr
157#endif
158#ifndef RSTRING_LEN
159# define RSTRING_LEN(s) RSTRING(s)->len
160#endif
161#ifndef RSTRING_PTR
162# define RSTRING_PTR(s) RSTRING(s)->ptr
163#endif
164
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100165#ifdef HAVE_DUP
166# undef HAVE_DUP
167#endif
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#include "vim.h"
170#include "version.h"
171
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100172#ifdef DYNAMIC_RUBY
173# if !defined(MSWIN) // must come after including vim.h, where it is defined
174# include <dlfcn.h>
175# define HINSTANCE void*
176# define RUBY_PROC void*
177# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
178# define symbol_from_dll dlsym
179# define close_dll dlclose
180# else
181# define RUBY_PROC FARPROC
182# define load_dll vimLoadLib
183# define symbol_from_dll GetProcAddress
184# define close_dll FreeLibrary
185# endif
186#endif
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#if defined(PROTO) && !defined(FEAT_RUBY)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100189// Define these to be able to generate the function prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190# define VALUE int
191# define RUBY_DATA_FUNC int
192#endif
193
194static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200195static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196static VALUE objtbl;
197
198static VALUE mVIM;
199static VALUE cBuffer;
200static VALUE cVimWindow;
201static VALUE eDeletedBufferError;
202static VALUE eDeletedWindowError;
203
204static int ensure_ruby_initialized(void);
205static void error_print(int);
206static void ruby_io_init(void);
207static void ruby_vim_init(void);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100208static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209
Bram Moolenaar41a41412020-01-07 21:32:19 +0100210#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200211# if defined(__ia64) && !defined(ruby_init_stack)
212# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#endif
215
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200216#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100217# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100218# define HINSTANCE int // for generating prototypes
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200219# endif
220
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221/*
222 * Wrapper defines
223 */
Bram Moolenaar8b430b42020-02-22 15:01:00 +0100224// Ruby 2.7 actually expands the following symbols as macro.
225# if RUBY_VERSION >= 27
226# undef rb_define_global_function
227# undef rb_define_method
228# undef rb_define_module_function
229# undef rb_define_singleton_method
230# endif
231
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200232# define rb_assoc_new dll_rb_assoc_new
233# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100234# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100235# define rb_check_type dll_rb_check_type
236# ifdef USE_TYPEDDATA
237# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100238# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200239# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100240# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100241# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100242# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
243# else
244# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
245# endif
246# else
247# define rb_data_object_alloc dll_rb_data_object_alloc
248# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200249# define rb_define_class_under dll_rb_define_class_under
250# define rb_define_const dll_rb_define_const
251# define rb_define_global_function dll_rb_define_global_function
252# define rb_define_method dll_rb_define_method
253# define rb_define_module dll_rb_define_module
254# define rb_define_module_function dll_rb_define_module_function
255# define rb_define_singleton_method dll_rb_define_singleton_method
256# define rb_define_virtual_variable dll_rb_define_virtual_variable
257# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200258# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200259# define rb_eArgError (*dll_rb_eArgError)
260# define rb_eIndexError (*dll_rb_eIndexError)
261# define rb_eRuntimeError (*dll_rb_eRuntimeError)
262# define rb_eStandardError (*dll_rb_eStandardError)
263# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaar41a41412020-01-07 21:32:19 +0100264# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200265# define rb_funcallv dll_rb_funcallv
266# else
267# define rb_funcall2 dll_rb_funcall2
268# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200269# define rb_global_variable dll_rb_global_variable
270# define rb_hash_aset dll_rb_hash_aset
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100271# define rb_hash_foreach dll_rb_hash_foreach
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200272# define rb_hash_new dll_rb_hash_new
273# define rb_inspect dll_rb_inspect
274# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200275
276// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
277// work. Not using the cache appears to be the best solution.
278# undef rb_intern
279# define rb_intern dll_rb_intern
280
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100281# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar41a41412020-01-07 21:32:19 +0100282# if RUBY_VERSION <= 18
Bram Moolenaar498af702014-03-28 21:58:21 +0100283# define rb_fix2int dll_rb_fix2int
284# define rb_num2int dll_rb_num2int
285# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200286# define rb_num2uint dll_rb_num2uint
287# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100288# define rb_num2dbl dll_rb_num2dbl
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200289# define rb_lastline_get dll_rb_lastline_get
290# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200291# define rb_protect dll_rb_protect
292# define rb_load dll_rb_load
Bram Moolenaar41a41412020-01-07 21:32:19 +0100293# if RUBY_VERSION <= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200294# define rb_num2long dll_rb_num2long
295# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100296# if RUBY_VERSION <= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200297# define rb_num2ulong dll_rb_num2ulong
298# endif
299# define rb_obj_alloc dll_rb_obj_alloc
300# define rb_obj_as_string dll_rb_obj_as_string
301# define rb_obj_id dll_rb_obj_id
302# define rb_raise dll_rb_raise
303# define rb_str_cat dll_rb_str_cat
304# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100305# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200306# define rb_str_new dll_rb_str_new
307# ifdef rb_str_new2
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100308// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200309# define need_rb_str_new_cstr 1
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100310// Ruby's headers #define rb_str_new_cstr to make use of GCC's
311// __builtin_constant_p extension.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200312# undef rb_str_new_cstr
313# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200314# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200315# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200316# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100317# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200318# define rb_string_value dll_rb_string_value
319# define rb_string_value_ptr dll_rb_string_value_ptr
320# define rb_float_new dll_rb_float_new
321# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200322# ifdef rb_ary_new4
Bram Moolenaar49c99fc2020-02-11 23:01:39 +0100323# define RB_ARY_NEW4_MACRO 1
324# undef rb_ary_new4
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200325# endif
326# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200327# define rb_ary_push dll_rb_ary_push
Bram Moolenaar41a41412020-01-07 21:32:19 +0100328# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200329# ifdef __ia64
330# define rb_ia64_bsp dll_rb_ia64_bsp
331# undef ruby_init_stack
332# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
333# else
334# define ruby_init_stack dll_ruby_init_stack
335# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200336# endif
337# else
338# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200339# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100340# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200341# define rb_errinfo dll_rb_errinfo
342# else
343# define ruby_errinfo (*dll_ruby_errinfo)
344# endif
345# define ruby_init dll_ruby_init
346# define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar4f974752019-02-17 17:44:42 +0100347# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100348# if RUBY_VERSION >= 19
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100349# define ruby_sysinit dll_ruby_sysinit
350# else
351# define NtInitialize dll_NtInitialize
352# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100353# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200354# define rb_w32_snprintf dll_rb_w32_snprintf
355# endif
356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
Bram Moolenaar41a41412020-01-07 21:32:19 +0100358# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200359# define ruby_script dll_ruby_script
360# define rb_enc_find_index dll_rb_enc_find_index
361# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100362# undef rb_enc_str_new
363# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200364# define rb_sprintf dll_rb_sprintf
365# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100366# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200367# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100368
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369/*
370 * Pointers for dynamic link
371 */
372static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200373VALUE *dll_rb_cFalseClass;
374VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200375# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200376VALUE *dll_rb_cInteger;
377# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100378# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100379VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200380# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200381VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100383VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200384VALUE *dll_rb_cSymbol;
385VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100386static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100388# ifdef USE_TYPEDDATA
389static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100392# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100393# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100394static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
395# else
396static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
397# endif
398# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
402static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
403static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
404static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
405static VALUE (*dll_rb_define_module) (const char*);
406static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
407static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
408static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
409static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200410static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411static VALUE *dll_rb_eArgError;
412static VALUE *dll_rb_eIndexError;
413static VALUE *dll_rb_eRuntimeError;
414static VALUE *dll_rb_eStandardError;
415static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100416# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200417static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
418# else
419static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
420# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421static void (*dll_rb_global_variable) (VALUE*);
422static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100423static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424static VALUE (*dll_rb_hash_new) (void);
425static VALUE (*dll_rb_inspect) (VALUE);
426static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200427static ID (*dll_rb_intern) (const char*);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100428# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200429static long (*dll_rb_fix2int) (VALUE);
430static long (*dll_rb_num2int) (VALUE);
431static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200432# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100433static double (*dll_rb_num2dbl) (VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434static VALUE (*dll_rb_lastline_get) (void);
435static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100436static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200437static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438static long (*dll_rb_num2long) (VALUE);
439static unsigned long (*dll_rb_num2ulong) (VALUE);
440static VALUE (*dll_rb_obj_alloc) (VALUE);
441static VALUE (*dll_rb_obj_as_string) (VALUE);
442static VALUE (*dll_rb_obj_id) (VALUE);
443static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100444# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200445static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200446# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200448# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
450static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
451static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200452# ifdef need_rb_str_new_cstr
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100453// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200454static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200455# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200457# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100458# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100459static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200460# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463static void (*dll_ruby_init) (void);
464static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100465# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100466# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100467static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100468# else
469static void (*dll_NtInitialize) (int*, char***);
470# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100471# if RUBY_VERSION >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200472static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200473# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200474# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100475# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100476static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
477static VALUE (*dll_rb_float_new) (double);
478static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200479static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100480static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100481# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100482static void (*dll_rb_ary_detransient) (VALUE);
483# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100484# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200485# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200486static void * (*dll_rb_ia64_bsp) (void);
487static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200488# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200489static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200490# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200491# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200492# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100493# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100494static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200495# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496
Bram Moolenaar41a41412020-01-07 21:32:19 +0100497# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100498static void (*dll_ruby_script) (const char*);
499static int (*dll_rb_enc_find_index) (const char*);
500static rb_encoding* (*dll_rb_enc_find) (const char*);
501static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
502static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100503static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100504static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200505# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100506
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100507# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100508# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100509static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100510# else
511static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
512# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100513# endif
514
Bram Moolenaar41a41412020-01-07 21:32:19 +0100515# if (RUBY_VERSION >= 19) && !defined(PROTO)
516# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100517 long
518rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200519# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100520 SIGNED_VALUE
521rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200522# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100523{
524 return dll_rb_num2long(x);
525}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100526# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100527 VALUE
528rb_int2big_stub(intptr_t x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100529# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100530 VALUE
531rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100532# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100533{
534 return dll_rb_int2big(x);
535}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100536# if (RUBY_VERSION >= 19) && (VIM_SIZEOF_INT < VIM_SIZEOF_LONG)
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100537 long
538rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200539{
540 return dll_rb_fix2int(x);
541}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100542 long
543rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200544{
545 return dll_rb_num2int(x);
546}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200547# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100548# if RUBY_VERSION >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100549 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100550rb_float_new_in_heap(double d)
551{
552 return dll_rb_float_new(d);
553}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100554# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100555 unsigned long
556rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200557# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100558 VALUE
559rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200560# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100561{
562 return (long)RSHIFT((SIGNED_VALUE)(x),1);
563}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200564# endif
565# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100566
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100567 // Do not generate a prototype here, VALUE isn't always defined.
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100568# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar41a41412020-01-07 21:32:19 +0100569# if RUBY_VERSION == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100570 void
571rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100572{
Bram Moolenaar90140742014-11-27 17:44:08 +0100573 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100574}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100575# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100576 void
577rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100578{
579 dll_rb_gc_writebarrier_unprotect(obj);
580}
581# endif
582# endif
583
Bram Moolenaar41a41412020-01-07 21:32:19 +0100584# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100585 void
586rb_ary_detransient_stub(VALUE x)
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100587{
588 dll_rb_ary_detransient(x);
589}
590# endif
591
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100592static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
594/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000595 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597static struct
598{
599 char *name;
600 RUBY_PROC *ptr;
601} ruby_funcname_table[] =
602{
603 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
604 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200605# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200606 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100607# else
608 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200609# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100610# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100611 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
614 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100615 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
617 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100618 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100620# ifdef USE_TYPEDDATA
621 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100624# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100625# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100626 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
627# else
628 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
629# endif
630# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
634 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
635 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
636 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
637 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
638 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
639 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
640 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
641 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200642 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
644 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
645 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
646 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
647 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100648# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200649 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
650# else
651 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
652# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
654 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100655 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
657 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
658 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200659 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100660# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200661 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
662 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
663 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200664# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100665 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
667 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200668 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
669 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
671 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
672 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
673 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
674 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
675 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100676# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200677 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200678# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200680# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
682 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
683 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200684# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200685 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200686# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200688# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100689# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100690 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200691# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200693# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
695 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100696# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100697# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100698 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100699# else
700 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200701# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100702# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200704# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200705# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100706# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100707 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100708# if RUBY_VERSION <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100709 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200710# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100711 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200712# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100713 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200714# ifdef RB_ARY_NEW4_MACRO
715 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
716# else
717 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
718# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100719 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100720# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100721 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
722# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200723# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100724# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100725 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100726 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
727 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
728 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
729 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
730 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100731 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100732 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200733# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100734# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200735# ifdef __ia64
736 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
737# endif
738 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
739# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100740# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100741# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100742 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100743# else
744 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
745# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100746# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 {"", NULL},
748};
749
750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 * Load library and get all pointers.
752 * Parameter 'libname' provides name of DLL.
753 * Return OK or FAIL.
754 */
755 static int
756ruby_runtime_link_init(char *libname, int verbose)
757{
758 int i;
759
760 if (hinstRuby)
761 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200762 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 if (!hinstRuby)
764 {
765 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100766 semsg(_(e_loadlib), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 return FAIL;
768 }
769
770 for (i = 0; ruby_funcname_table[i].ptr; ++i)
771 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200772 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 ruby_funcname_table[i].name)))
774 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200775 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200776 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100778 semsg(_(e_loadfunc), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 return FAIL;
780 }
781 }
782 return OK;
783}
784
785/*
786 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
787 * else FALSE.
788 */
789 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100790ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100792 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100794#endif // defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100797ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799}
800
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100801 void
802ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
804 int state;
805 char *script = NULL;
806
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000807 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 if (!eap->skip && ensure_ruby_initialized())
809 {
810 if (script == NULL)
811 rb_eval_string_protect((char *)eap->arg, &state);
812 else
813 rb_eval_string_protect(script, &state);
814 if (state)
815 error_print(state);
816 }
817 vim_free(script);
818}
819
Bram Moolenaar165641d2010-02-17 16:23:09 +0100820/*
821 * In Ruby 1.9 or later, ruby String object has encoding.
822 * conversion buffer string of vim to ruby String object using
823 * VIM encoding option.
824 */
825 static VALUE
826vim_str2rb_enc_str(const char *s)
827{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100828#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100829 int isnum;
830 long lval;
831 char_u *sval;
832 rb_encoding *enc;
833
834 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
835 if (isnum == 0)
836 {
837 enc = rb_enc_find((char *)sval);
838 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200839 if (enc)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200840 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100841 }
842#endif
843 return rb_str_new2(s);
844}
845
846 static VALUE
847eval_enc_string_protect(const char *str, int *state)
848{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100849#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100850 int isnum;
851 long lval;
852 char_u *sval;
853 rb_encoding *enc;
854 VALUE v;
855
856 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
857 if (isnum == 0)
858 {
859 enc = rb_enc_find((char *)sval);
860 vim_free(sval);
861 if (enc)
862 {
863 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
864 return rb_eval_string_protect(StringValuePtr(v), state);
865 }
866 }
867#endif
868 return rb_eval_string_protect(str, state);
869}
870
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100871 void
872ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873{
874 int state;
875 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100876 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877
878 if (ensure_ruby_initialized())
879 {
880 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
881 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200882 for (i = eap->line1; i <= eap->line2; i++)
883 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100884 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100886 if (i > curbuf->b_ml.ml_line_count)
887 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100888 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100890 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200891 if (state)
892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 error_print(state);
894 break;
895 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100896 if (was_curbuf != curbuf)
897 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200899 if (!NIL_P(line))
900 {
901 if (TYPE(line) != T_STRING)
902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100903 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 return;
905 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100906 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 changed();
908#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100909 syn_changed(i); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#endif
911 }
912 }
913 check_cursor();
914 update_curbuf(NOT_VALID);
915 }
916}
917
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100918 static VALUE
919rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100920{
921 rb_load(file_to_load, 0);
922 return Qnil;
923}
924
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100925 void
926ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928 int state;
929
930 if (ensure_ruby_initialized())
931 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100932 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
933 rb_protect(rb_load_wrap, file_to_load, &state);
934 if (state)
935 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
937}
938
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100939 void
940ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000942 if (buf->b_ruby_ref)
943 {
944 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
945 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947}
948
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100949 void
950ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000952 if (win->w_ruby_ref)
953 {
954 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
955 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957}
958
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100959 static int
960ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961{
962 if (!ruby_initialized)
963 {
964#ifdef DYNAMIC_RUBY
965 if (ruby_enabled(TRUE))
966 {
967#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100968#ifdef MSWIN
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100969 // suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100970 int argc = 1;
971 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100972 char **argvp = argv;
Bram Moolenaar41a41412020-01-07 21:32:19 +0100973# if RUBY_VERSION >= 19
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100974 ruby_sysinit(&argc, &argvp);
975# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100976 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100977# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100978#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200979 {
Bram Moolenaar41a41412020-01-07 21:32:19 +0100980#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200981 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100982#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200983 ruby_init();
984 }
Bram Moolenaar41a41412020-01-07 21:32:19 +0100985#if RUBY_VERSION >= 19
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100986 {
987 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200988 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100989 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100990 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100991 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100992#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100994#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100995 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 ruby_vim_init();
997 ruby_initialized = 1;
998#ifdef DYNAMIC_RUBY
999 }
1000 else
1001 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001002 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 return 0;
1004 }
1005#endif
1006 }
1007 return ruby_initialized;
1008}
1009
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001010 static void
1011error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012{
Bram Moolenaar41a41412020-01-07 21:32:19 +01001013#if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 RUBYEXTERN VALUE ruby_errinfo;
1015#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001016 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 VALUE eclass;
1018 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001019 VALUE bt;
1020 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001022 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024#define TAG_RETURN 0x1
1025#define TAG_BREAK 0x2
1026#define TAG_NEXT 0x3
1027#define TAG_RETRY 0x4
1028#define TAG_REDO 0x5
1029#define TAG_RAISE 0x6
1030#define TAG_THROW 0x7
1031#define TAG_FATAL 0x8
1032#define TAG_MASK 0xf
1033
Bram Moolenaar758535a2016-03-30 22:06:16 +02001034 switch (state)
1035 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001036 case TAG_RETURN:
1037 emsg(_("E267: unexpected return"));
1038 break;
1039 case TAG_NEXT:
1040 emsg(_("E268: unexpected next"));
1041 break;
1042 case TAG_BREAK:
1043 emsg(_("E269: unexpected break"));
1044 break;
1045 case TAG_REDO:
1046 emsg(_("E270: unexpected redo"));
1047 break;
1048 case TAG_RETRY:
1049 emsg(_("E271: retry outside of rescue clause"));
1050 break;
1051 case TAG_RAISE:
1052 case TAG_FATAL:
Bram Moolenaar41a41412020-01-07 21:32:19 +01001053#if RUBY_VERSION >= 19
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001054 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001055#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001056 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001057#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001058 eclass = CLASS_OF(error);
1059 einfo = rb_obj_as_string(error);
1060 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1061 {
1062 emsg(_("E272: unhandled exception"));
1063 }
1064 else
1065 {
1066 VALUE epath;
1067 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001069 epath = rb_class_path(eclass);
1070 vim_snprintf(buff, BUFSIZ, "%s: %s",
1071 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1072 p = strchr(buff, '\n');
1073 if (p) *p = '\0';
1074 emsg(buff);
1075 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001076
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001077 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001078#if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001079 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1080 for (i = 0; i < RARRAY_LEN(bt); i++)
1081 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001082#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001083 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1084 for (i = 0; i < RARRAY_LEN(bt); i++)
1085 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001086#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001087 break;
1088 default:
1089 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1090 emsg(buff);
1091 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 }
1093}
1094
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001095 static VALUE
1096vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097{
1098 char *buff, *p;
1099
1100 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001101 if (RSTRING_LEN(str) > 0)
1102 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001103 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001104 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001105 strcpy(buff, RSTRING_PTR(str));
1106 p = strchr(buff, '\n');
1107 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001108 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001109 }
1110 else
1111 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001112 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 return Qnil;
1115}
1116
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001117 static VALUE
1118vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001120 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 update_screen(NOT_VALID);
1122 return Qnil;
1123}
1124
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001125 static VALUE
1126vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001128 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 return Qnil;
1130}
1131
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001132#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001133 static VALUE
1134vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001135{
1136 VALUE result = Qnil;
1137
1138 if (tv->v_type == VAR_STRING)
1139 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001140 result = rb_str_new2(tv->vval.v_string == NULL
1141 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001142 }
1143 else if (tv->v_type == VAR_NUMBER)
1144 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001145 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001146 }
1147# ifdef FEAT_FLOAT
1148 else if (tv->v_type == VAR_FLOAT)
1149 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001150 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001151 }
1152# endif
1153 else if (tv->v_type == VAR_LIST)
1154 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001155 list_T *list = tv->vval.v_list;
1156 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001157
Bram Moolenaar639a2552010-03-17 18:15:23 +01001158 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001159
Bram Moolenaar639a2552010-03-17 18:15:23 +01001160 if (list != NULL)
1161 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001162 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001163 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001164 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001165 }
1166 else if (tv->v_type == VAR_DICT)
1167 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001168 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001169
Bram Moolenaar639a2552010-03-17 18:15:23 +01001170 if (tv->vval.v_dict != NULL)
1171 {
1172 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1173 long_u todo = ht->ht_used;
1174 hashitem_T *hi;
1175 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001176
Bram Moolenaar639a2552010-03-17 18:15:23 +01001177 for (hi = ht->ht_array; todo > 0; ++hi)
1178 {
1179 if (!HASHITEM_EMPTY(hi))
1180 {
1181 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001182
Bram Moolenaar639a2552010-03-17 18:15:23 +01001183 di = dict_lookup(hi);
1184 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001185 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001186 }
1187 }
1188 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001189 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001190 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001191 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001192 if (tv->vval.v_number == VVAL_TRUE)
1193 result = Qtrue;
1194 else if (tv->vval.v_number == VVAL_FALSE)
1195 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001196 }
1197 else if (tv->v_type == VAR_BLOB)
1198 {
1199 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1200 tv->vval.v_blob->bv_ga.ga_len);
1201 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001202 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001203
1204 return result;
1205}
1206#endif
1207
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001208 static VALUE
1209vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210{
1211#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001212 typval_T *tv;
1213 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001215 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1216 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001217 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001218 result = vim_to_ruby(tv);
1219
1220 free_tv(tv);
1221
1222 return result;
1223#else
1224 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226}
1227
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001228#ifdef USE_TYPEDDATA
1229static size_t buffer_dsize(const void *buf);
1230
1231static const rb_data_type_t buffer_type = {
1232 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001233 {0, 0, buffer_dsize,
1234# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001235 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001236# else
1237 {0, 0}
1238# endif
1239 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001240 0, 0,
1241# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1242 0,
1243# endif
1244};
1245
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001246 static size_t
1247buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001248{
1249 return sizeof(buf_T);
1250}
1251#endif
1252
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001253 static VALUE
1254buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001256 if (buf->b_ruby_ref)
1257 {
1258 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001260 else
1261 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001262#ifdef USE_TYPEDDATA
1263 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1264#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001266#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001267 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1269 return obj;
1270 }
1271}
1272
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001273 static buf_T *
1274get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275{
1276 buf_T *buf;
1277
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001278#ifdef USE_TYPEDDATA
1279 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1280#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 if (buf == NULL)
1284 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1285 return buf;
1286}
1287
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001288 static VALUE
1289vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001290{
1291 VALUE result = rb_str_new("0z", 2);
1292 char buf[4];
1293 int i;
1294 for (i = 0; i < RSTRING_LEN(str); i++)
1295 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001296 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001297 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001298 }
1299 return result;
1300}
1301
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001302 static VALUE
1303buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304{
1305 return buffer_new(curbuf);
1306}
1307
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001308 static VALUE
1309buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
1311 buf_T *b;
1312 int n = 0;
1313
Bram Moolenaar29323592016-07-24 22:04:11 +02001314 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001315 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001316 // Deleted buffers should not be counted
1317 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001318 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001319 n++;
1320 }
1321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 return INT2NUM(n);
1323}
1324
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001325 static VALUE
1326buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327{
1328 buf_T *b;
1329 int n = NUM2INT(num);
1330
Bram Moolenaar29323592016-07-24 22:04:11 +02001331 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001332 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001333 // Deleted buffers should not be counted
1334 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001335 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001336 continue;
1337
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001338 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001340
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001341 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 }
1343 return Qnil;
1344}
1345
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001346 static VALUE
1347buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 buf_T *buf = get_buf(self);
1350
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001351 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352}
1353
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001354 static VALUE
1355buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356{
1357 buf_T *buf = get_buf(self);
1358
1359 return INT2NUM(buf->b_fnum);
1360}
1361
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001362 static VALUE
1363buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
1365 buf_T *buf = get_buf(self);
1366
1367 return INT2NUM(buf->b_ml.ml_line_count);
1368}
1369
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001370 static VALUE
1371get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001373 if (n <= 0 || n > buf->b_ml.ml_line_count)
1374 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1375 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376}
1377
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001378 static VALUE
1379buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380{
1381 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001382
1383 if (buf != NULL)
1384 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001385 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001386}
1387
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001388 static VALUE
1389set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001390{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001391 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001392 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001394 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1395 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001396 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001397 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001398
Bram Moolenaar758535a2016-03-30 22:06:16 +02001399 if (u_savesub(n) == OK)
1400 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001401 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 changed();
1403#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001404 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405#endif
1406 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001407
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001408 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001409 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001410 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 update_curbuf(NOT_VALID);
1413 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001414 else
1415 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001416 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418 return str;
1419}
1420
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001421 static VALUE
1422buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001423{
1424 buf_T *buf = get_buf(self);
1425
1426 if (buf != NULL)
1427 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1428 return str;
1429}
1430
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001431 static VALUE
1432buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001434 buf_T *buf = get_buf(self);
1435 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001436 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001438 if (n > 0 && n <= buf->b_ml.ml_line_count)
1439 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001440 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001441 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001442
Bram Moolenaar758535a2016-03-30 22:06:16 +02001443 if (u_savedel(n, 1) == OK)
1444 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001445 ml_delete(n);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001446
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001447 // Changes to non-active buffers should properly refresh
1448 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001449 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 changed();
1452 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001453
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001454 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001455 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001456 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001457
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 update_curbuf(NOT_VALID);
1459 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001460 else
1461 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001462 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 }
1464 return Qnil;
1465}
1466
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001467 static VALUE
1468buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001470 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001471 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001472 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001473 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474
Bram Moolenaar3c531602010-11-16 14:46:19 +01001475 if (line == NULL)
1476 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001477 rb_raise(rb_eIndexError, "NULL line");
1478 }
1479 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001480 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001481 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001482 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001483
Bram Moolenaar758535a2016-03-30 22:06:16 +02001484 if (u_inssub(n + 1) == OK)
1485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001487
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001488 // Changes to non-active buffers should properly refresh screen
1489 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001490 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001491
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001492 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001494
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001495 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001496 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001497 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001498
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 update_curbuf(NOT_VALID);
1500 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001501 else
1502 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001503 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 }
1505 return str;
1506}
1507
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001508#ifdef USE_TYPEDDATA
1509static size_t window_dsize(const void *buf);
1510
1511static const rb_data_type_t window_type = {
1512 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001513 {0, 0, window_dsize,
1514# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001515 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001516# else
1517 {0, 0}
1518# endif
1519 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001520 0, 0,
1521# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1522 0,
1523# endif
1524};
1525
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001526 static size_t
1527window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001528{
1529 return sizeof(win_T);
1530}
1531#endif
1532
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001533 static VALUE
1534window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001536 if (win->w_ruby_ref)
1537 {
1538 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001540 else
1541 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001542#ifdef USE_TYPEDDATA
1543 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1544#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001546#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001547 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1549 return obj;
1550 }
1551}
1552
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001553 static win_T *
1554get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555{
1556 win_T *win;
1557
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001558#ifdef USE_TYPEDDATA
1559 TypedData_Get_Struct(obj, win_T, &window_type, win);
1560#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 if (win == NULL)
1564 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1565 return win;
1566}
1567
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001568 static VALUE
1569window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570{
1571 return window_new(curwin);
1572}
1573
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001574/*
1575 * Added line manipulation functions
1576 * SegPhault - 03/07/05
1577 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001578 static VALUE
1579line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001580{
1581 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1582}
1583
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001584 static VALUE
1585set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001586{
1587 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1588}
1589
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001590 static VALUE
1591current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001592{
1593 return INT2FIX((int)curwin->w_cursor.lnum);
1594}
1595
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001596 static VALUE
1597window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 win_T *w;
1600 int n = 0;
1601
Bram Moolenaar29323592016-07-24 22:04:11 +02001602 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 n++;
1604 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605}
1606
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001607 static VALUE
1608window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609{
1610 win_T *w;
1611 int n = NUM2INT(num);
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (n == 0)
1615 return window_new(w);
1616 return Qnil;
1617}
1618
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001619 static VALUE
1620window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
1622 win_T *win = get_win(self);
1623
1624 return buffer_new(win->w_buffer);
1625}
1626
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001627 static VALUE
1628window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629{
1630 win_T *win = get_win(self);
1631
1632 return INT2NUM(win->w_height);
1633}
1634
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001635 static VALUE
1636window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 win_T *win = get_win(self);
1639 win_T *savewin = curwin;
1640
1641 curwin = win;
1642 win_setheight(NUM2INT(height));
1643 curwin = savewin;
1644 return height;
1645}
1646
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001647 static VALUE
1648window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001649{
Bram Moolenaare745d752017-09-22 16:56:22 +02001650 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001651}
1652
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001653 static VALUE
1654window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001655{
1656 win_T *win = get_win(self);
1657 win_T *savewin = curwin;
1658
1659 curwin = win;
1660 win_setwidth(NUM2INT(width));
1661 curwin = savewin;
1662 return width;
1663}
1664
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001665 static VALUE
1666window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667{
1668 win_T *win = get_win(self);
1669
1670 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1671}
1672
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001673 static VALUE
1674window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
1676 VALUE lnum, col;
1677 win_T *win = get_win(self);
1678
1679 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001680 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001682 lnum = RARRAY_PTR(pos)[0];
1683 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 win->w_cursor.lnum = NUM2LONG(lnum);
1685 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001686 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001687 check_cursor(); // put cursor on an existing line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 update_screen(NOT_VALID);
1689 return Qnil;
1690}
1691
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001692 static VALUE
1693f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001694{
1695 return Qnil;
1696}
1697
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001698 static VALUE
1699f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700{
1701 int i;
1702 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001703 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaar758535a2016-03-30 22:06:16 +02001705 for (i = 0; i < argc; i++)
1706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 if (i > 0) rb_str_cat(str, ", ", 2);
1708 rb_str_concat(str, rb_inspect(argv[i]));
1709 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001710 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001711
1712 if (argc == 1)
1713 ret = argv[0];
1714 else if (argc > 1)
1715 ret = rb_ary_new4(argc, argv);
1716 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717}
1718
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001719 static void
1720ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721{
1722#ifndef DYNAMIC_RUBY
1723 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001724 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#endif
1726
1727 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001728 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001730 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001731 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1732 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 rb_define_global_function("p", f_p, -1);
1734}
1735
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001736 static void
1737ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
1739 objtbl = rb_hash_new();
1740 rb_global_variable(&objtbl);
1741
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001742 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1743 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001744 mVIM = rb_define_module("Vim");
1745 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1747 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1748 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1749 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1750 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1751 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1752 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1753 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1754 rb_define_module_function(mVIM, "message", vim_message, 1);
1755 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1756 rb_define_module_function(mVIM, "command", vim_command, 1);
1757 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001758 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
1760 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1761 rb_eStandardError);
1762 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1763 rb_eStandardError);
1764
1765 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1766 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1767 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1768 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1769 rb_define_method(cBuffer, "name", buffer_name, 0);
1770 rb_define_method(cBuffer, "number", buffer_number, 0);
1771 rb_define_method(cBuffer, "count", buffer_count, 0);
1772 rb_define_method(cBuffer, "length", buffer_count, 0);
1773 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1774 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1775 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1776 rb_define_method(cBuffer, "append", buffer_append, 2);
1777
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001778 // Added line manipulation functions
1779 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001780 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1781 rb_define_method(cBuffer, "line", line_s_current, 0);
1782 rb_define_method(cBuffer, "line=", set_current_line, 1);
1783
1784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1786 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1787 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1788 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1789 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1790 rb_define_method(cVimWindow, "height", window_height, 0);
1791 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001792 rb_define_method(cVimWindow, "width", window_width, 0);
1793 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1795 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1796
1797 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1798 rb_define_virtual_variable("$curwin", window_s_current, 0);
1799}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001800
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001801 void
1802vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001803{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001804 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001805 ruby_stack_start = stack_start;
1806}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001807
1808 static int
1809convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1810{
1811 dict_T *d = (dict_T *)arg;
1812 dictitem_T *di;
1813
1814 di = dictitem_alloc((char_u *)RSTRING_PTR(RSTRING(rb_obj_as_string(key))));
1815 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1816 || dict_add(d, di) != OK)
1817 {
1818 d->dv_hashtab.ht_error = TRUE;
1819 return ST_STOP;
1820 }
1821 return ST_CONTINUE;
1822}
1823
1824 static int
1825ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1826{
1827 switch (TYPE(val))
1828 {
1829 case T_NIL:
1830 rettv->v_type = VAR_SPECIAL;
1831 rettv->vval.v_number = VVAL_NULL;
1832 break;
1833 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001834 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001835 rettv->vval.v_number = VVAL_TRUE;
1836 break;
1837 case T_FALSE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001838 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001839 rettv->vval.v_number = VVAL_FALSE;
1840 break;
1841 case T_BIGNUM:
1842 case T_FIXNUM:
1843 rettv->v_type = VAR_NUMBER;
1844 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1845 break;
1846#ifdef FEAT_FLOAT
1847 case T_FLOAT:
1848 rettv->v_type = VAR_FLOAT;
1849 rettv->vval.v_float = (float_T)NUM2DBL(val);
1850 break;
1851#endif
1852 default:
1853 val = rb_obj_as_string(val);
1854 // FALLTHROUGH
1855 case T_STRING:
1856 {
1857 VALUE str = (VALUE)RSTRING(val);
1858
1859 rettv->v_type = VAR_STRING;
1860 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
1861 (int)RSTRING_LEN(str));
1862 }
1863 break;
1864 case T_ARRAY:
1865 {
1866 list_T *l;
1867 long i;
1868 typval_T v;
1869
1870 l = list_alloc();
1871 if (l == NULL)
1872 return FAIL;
1873
1874 for (i = 0; i < RARRAY_LEN(val); ++i)
1875 {
1876 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1877 &v) != OK)
1878 {
1879 list_unref(l);
1880 return FAIL;
1881 }
1882 list_append_tv(l, &v);
1883 clear_tv(&v);
1884 }
1885
1886 rettv->v_type = VAR_LIST;
1887 rettv->vval.v_list = l;
1888 ++l->lv_refcount;
1889 }
1890 break;
1891 case T_HASH:
1892 {
1893 dict_T *d;
1894
1895 d = dict_alloc();
1896 if (d == NULL)
1897 return FAIL;
1898
1899 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1900 if (d->dv_hashtab.ht_error)
1901 {
1902 dict_unref(d);
1903 return FAIL;
1904 }
1905
1906 rettv->v_type = VAR_DICT;
1907 rettv->vval.v_dict = d;
1908 ++d->dv_refcount;
1909 }
1910 break;
1911 }
1912 return OK;
1913}
1914
1915 void
1916do_rubyeval(char_u *str, typval_T *rettv)
1917{
1918 int retval = FAIL;
1919
1920 if (ensure_ruby_initialized())
1921 {
1922 int state;
1923 VALUE obj;
1924
1925 obj = rb_eval_string_protect((const char *)str, &state);
1926 if (state)
1927 error_print(state);
1928 else
1929 retval = ruby_convert_to_vim_value(obj, rettv);
1930 }
1931 if (retval == FAIL)
1932 {
1933 rettv->v_type = VAR_NUMBER;
1934 rettv->vval.v_number = 0;
1935 }
1936}