blob: 8f9b35623106122e2d65b8b786def9c92afc160d [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 Moolenaar071d4272004-06-13 20:20:40 +000023# ifndef DYNAMIC_RUBY
K.Takata236ccbf2022-09-22 16:12:06 +010024# define NT
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010025# define IMPORT // For static dll usage __declspec(dllimport)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026# define RUBYEXTERN __declspec(dllimport)
27# endif
28#endif
29#ifndef RUBYEXTERN
30# define RUBYEXTERN extern
31#endif
32
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020033#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000034/*
35 * This is tricky. In ruby.h there is (inline) function rb_class_of()
36 * definition. This function use these variables. But we want function to
37 * use dll_* variables.
38 */
Bram Moolenaardace9f72020-12-28 15:07:45 +010039# if RUBY_VERSION >= 24
40# define USE_RUBY_INTEGER
41# endif
42
Bram Moolenaar071d4272004-06-13 20:20:40 +000043# define rb_cFalseClass (*dll_rb_cFalseClass)
44# define rb_cFixnum (*dll_rb_cFixnum)
Bram Moolenaar2016ae52016-06-13 20:08:43 +020045# if defined(USE_RUBY_INTEGER)
46# define rb_cInteger (*dll_rb_cInteger)
Bram Moolenaar06469e92016-06-11 22:26:53 +020047# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +010048# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +010049# define rb_cFloat (*dll_rb_cFloat)
50# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000051# define rb_cNilClass (*dll_rb_cNilClass)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010052# define rb_cString (*dll_rb_cString)
Bram Moolenaar071d4272004-06-13 20:20:40 +000053# define rb_cSymbol (*dll_rb_cSymbol)
54# define rb_cTrueClass (*dll_rb_cTrueClass)
Bram Moolenaardace9f72020-12-28 15:07:45 +010055
Bram Moolenaar071d4272004-06-13 20:20:40 +000056/*
K.Takata236ccbf2022-09-22 16:12:06 +010057 * All Ruby functions are exported with "__declspec(dllimport)" in ruby.h.
58 * But it causes trouble for these variables, because it is defined in this
59 * file. When defined this RUBY_EXPORT it modified to "extern" and be able
60 * to avoid this problem.
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 */
K.Takata236ccbf2022-09-22 16:12:06 +010062# define RUBY_EXPORT
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020063
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010064// Ruby 1.9 defines a number of static functions which use rb_num2long and
65// rb_int2big
K.Takata236ccbf2022-09-22 16:12:06 +010066# define rb_num2long rb_num2long_stub
67# define rb_int2big rb_int2big_stub
Bram Moolenaar42d57f02010-03-10 12:47:00 +010068
K.Takata236ccbf2022-09-22 16:12:06 +010069# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010070// Ruby 1.9 defines a number of static functions which use rb_fix2int and
71// rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit)
K.Takata236ccbf2022-09-22 16:12:06 +010072# define rb_fix2int rb_fix2int_stub
73# define rb_num2int rb_num2int_stub
Bram Moolenaardace9f72020-12-28 15:07:45 +010074# endif
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +020075
Bram Moolenaardace9f72020-12-28 15:07:45 +010076# if RUBY_VERSION == 21
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010077// Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
78// rb_gc_writebarrier_unprotect_promoted if USE_RGENGC
ichizok8bb3fe42021-12-28 15:51:45 +000079# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
Bram Moolenaardace9f72020-12-28 15:07:45 +010080# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +010081
Bram Moolenaardace9f72020-12-28 15:07:45 +010082# if RUBY_VERSION >= 22
ichizok8bb3fe42021-12-28 15:51:45 +000083# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaardace9f72020-12-28 15:07:45 +010084# endif
85
86# if RUBY_VERSION >= 26
ichizok8bb3fe42021-12-28 15:51:45 +000087# define rb_ary_detransient rb_ary_detransient_stub
Bram Moolenaardace9f72020-12-28 15:07:45 +010088# endif
89
90# if RUBY_VERSION >= 30
ichizok8bb3fe42021-12-28 15:51:45 +000091# define rb_check_type rb_check_type_stub
92# define rb_num2uint rb_num2uint_stub
93# define ruby_malloc_size_overflow ruby_malloc_size_overflow_stub
94# endif
95
96# if RUBY_VERSION >= 31
97# define rb_debug_rstring_null_ptr rb_debug_rstring_null_ptr_stub
98# define rb_unexpected_type rb_unexpected_type_stub
Bram Moolenaardace9f72020-12-28 15:07:45 +010099# endif
100
101#endif // ifdef DYNAMIC_RUBY
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100102
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200103// On macOS pre-installed Ruby defines "SIZEOF_TIME_T" as "SIZEOF_LONG" so it
Bram Moolenaar92c098d2020-05-26 20:09:11 +0200104// conflicts with the definition in config.h then causes a macro-redefined
105// warning.
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200106#ifdef SIZEOF_TIME_T
107# undef SIZEOF_TIME_T
108#endif
109
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110#include <ruby.h>
K.Takata236ccbf2022-09-22 16:12:06 +0100111#include <ruby/encoding.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
Bram Moolenaar92c098d2020-05-26 20:09:11 +0200113// See above.
114#ifdef SIZEOF_TIME_T
115# undef SIZEOF_TIME_T
116#endif
117
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200118#undef off_t // ruby defines off_t as _int64, Mingw uses long
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#undef EXTERN
120#undef _
121
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100122// T_DATA defined both by Ruby and Mac header files, hack around it...
Bram Moolenaard0573012017-10-28 21:11:06 +0200123#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124# define __OPENTRANSPORT__
125# define __OPENTRANSPORTPROTOCOL__
126# define __OPENTRANSPORTPROVIDERS__
127#endif
128
Bram Moolenaar165641d2010-02-17 16:23:09 +0100129/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100130 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
131 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
132 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100133 * Use TypedData_XXX if available.
134 */
Bram Moolenaar41a41412020-01-07 21:32:19 +0100135#if defined(TypedData_Wrap_Struct) && (RUBY_VERSION >= 20)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100136# define USE_TYPEDDATA 1
137#endif
138
139/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200140 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100141 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
142 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
143 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
144 */
145#ifndef StringValuePtr
146# define StringValuePtr(s) STR2CSTR(s)
147#endif
148#ifndef RARRAY_LEN
149# define RARRAY_LEN(s) RARRAY(s)->len
150#endif
151#ifndef RARRAY_PTR
152# define RARRAY_PTR(s) RARRAY(s)->ptr
153#endif
154#ifndef RSTRING_LEN
155# define RSTRING_LEN(s) RSTRING(s)->len
156#endif
157#ifndef RSTRING_PTR
158# define RSTRING_PTR(s) RSTRING(s)->ptr
159#endif
160
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100161#ifdef HAVE_DUP
162# undef HAVE_DUP
163#endif
164
ichizok8bb3fe42021-12-28 15:51:45 +0000165// Avoid redefining TRUE/FALSE in vterm.h.
166#ifdef TRUE
167# undef TRUE
168#endif
169#ifdef FALSE
170# undef FALSE
171#endif
172
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#include "vim.h"
174#include "version.h"
175
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100176#ifdef DYNAMIC_RUBY
177# if !defined(MSWIN) // must come after including vim.h, where it is defined
178# include <dlfcn.h>
179# define HINSTANCE void*
180# define RUBY_PROC void*
181# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
182# define symbol_from_dll dlsym
183# define close_dll dlclose
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200184# define load_dll_error dlerror
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100185# else
186# define RUBY_PROC FARPROC
187# define load_dll vimLoadLib
188# define symbol_from_dll GetProcAddress
189# define close_dll FreeLibrary
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200190# define load_dll_error GetWin32Error
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100191# endif
192#endif
193
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194#if defined(PROTO) && !defined(FEAT_RUBY)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100195// Define these to be able to generate the function prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196# define VALUE int
197# define RUBY_DATA_FUNC int
198#endif
199
200static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200201static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202static VALUE objtbl;
203
204static VALUE mVIM;
205static VALUE cBuffer;
206static VALUE cVimWindow;
207static VALUE eDeletedBufferError;
208static VALUE eDeletedWindowError;
209
210static int ensure_ruby_initialized(void);
211static void error_print(int);
212static void ruby_io_init(void);
213static void ruby_vim_init(void);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100214static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215
K.Takata236ccbf2022-09-22 16:12:06 +0100216#if defined(__ia64) && !defined(ruby_init_stack)
217# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#endif
219
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200220#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100221# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100222# define HINSTANCE int // for generating prototypes
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200223# endif
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225/*
226 * Wrapper defines
227 */
Bram Moolenaar8b430b42020-02-22 15:01:00 +0100228// Ruby 2.7 actually expands the following symbols as macro.
229# if RUBY_VERSION >= 27
230# undef rb_define_global_function
231# undef rb_define_method
232# undef rb_define_module_function
233# undef rb_define_singleton_method
234# endif
235
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200236# define rb_assoc_new dll_rb_assoc_new
237# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100238# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaardace9f72020-12-28 15:07:45 +0100239# if RUBY_VERSION < 30
240# define rb_check_type dll_rb_check_type
241# endif
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100242# ifdef USE_TYPEDDATA
243# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100244# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200245# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100246# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100247# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100248# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
249# else
250# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
251# endif
252# else
253# define rb_data_object_alloc dll_rb_data_object_alloc
254# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200255# define rb_define_class_under dll_rb_define_class_under
256# define rb_define_const dll_rb_define_const
257# define rb_define_global_function dll_rb_define_global_function
258# define rb_define_method dll_rb_define_method
259# define rb_define_module dll_rb_define_module
260# define rb_define_module_function dll_rb_define_module_function
261# define rb_define_singleton_method dll_rb_define_singleton_method
262# define rb_define_virtual_variable dll_rb_define_virtual_variable
263# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200264# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200265# define rb_eArgError (*dll_rb_eArgError)
266# define rb_eIndexError (*dll_rb_eIndexError)
267# define rb_eRuntimeError (*dll_rb_eRuntimeError)
268# define rb_eStandardError (*dll_rb_eStandardError)
269# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaar41a41412020-01-07 21:32:19 +0100270# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200271# define rb_funcallv dll_rb_funcallv
272# else
273# define rb_funcall2 dll_rb_funcall2
274# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200275# define rb_global_variable dll_rb_global_variable
276# define rb_hash_aset dll_rb_hash_aset
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100277# define rb_hash_foreach dll_rb_hash_foreach
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200278# define rb_hash_new dll_rb_hash_new
279# define rb_inspect dll_rb_inspect
280# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200281
282// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
283// work. Not using the cache appears to be the best solution.
284# undef rb_intern
285# define rb_intern dll_rb_intern
286
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100287# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaardace9f72020-12-28 15:07:45 +0100288# if RUBY_VERSION < 30
289# define rb_num2uint dll_rb_num2uint
290# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200291# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100292# define rb_num2dbl dll_rb_num2dbl
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200293# define rb_lastline_get dll_rb_lastline_get
294# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200295# define rb_protect dll_rb_protect
296# define rb_load dll_rb_load
K.Takata236ccbf2022-09-22 16:12:06 +0100297# if RUBY_VERSION < 20
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200298# define rb_num2ulong dll_rb_num2ulong
299# endif
300# define rb_obj_alloc dll_rb_obj_alloc
301# define rb_obj_as_string dll_rb_obj_as_string
302# define rb_obj_id dll_rb_obj_id
303# define rb_raise dll_rb_raise
304# define rb_str_cat dll_rb_str_cat
305# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100306# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200307# define rb_str_new dll_rb_str_new
308# ifdef rb_str_new2
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100309// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200310# define need_rb_str_new_cstr 1
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100311// Ruby's headers #define rb_str_new_cstr to make use of GCC's
312// __builtin_constant_p extension.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200313# undef rb_str_new_cstr
314# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200315# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200316# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200317# endif
K.Takata236ccbf2022-09-22 16:12:06 +0100318# 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
322# ifdef rb_ary_new4
323# define RB_ARY_NEW4_MACRO 1
324# undef rb_ary_new4
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200325# endif
K.Takata236ccbf2022-09-22 16:12:06 +0100326# define rb_ary_new4 dll_rb_ary_new4
327# define rb_ary_push dll_rb_ary_push
328# ifdef __ia64
329# define rb_ia64_bsp dll_rb_ia64_bsp
330# undef ruby_init_stack
331# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200332# else
K.Takata236ccbf2022-09-22 16:12:06 +0100333# define ruby_init_stack dll_ruby_init_stack
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200334# endif
K.Takata236ccbf2022-09-22 16:12:06 +0100335# define rb_errinfo dll_rb_errinfo
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200336# define ruby_init dll_ruby_init
337# define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar4f974752019-02-17 17:44:42 +0100338# ifdef MSWIN
K.Takata236ccbf2022-09-22 16:12:06 +0100339# define ruby_sysinit dll_ruby_sysinit
340# define rb_w32_snprintf dll_rb_w32_snprintf
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342
K.Takata236ccbf2022-09-22 16:12:06 +0100343# define ruby_script dll_ruby_script
344# define rb_enc_find_index dll_rb_enc_find_index
345# define rb_enc_find dll_rb_enc_find
346# undef rb_enc_str_new
347# define rb_enc_str_new dll_rb_enc_str_new
348# define rb_sprintf dll_rb_sprintf
349# define rb_require dll_rb_require
350# define ruby_options dll_ruby_options
Bram Moolenaar165641d2010-02-17 16:23:09 +0100351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352/*
353 * Pointers for dynamic link
354 */
355static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200356VALUE *dll_rb_cFalseClass;
357VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200358# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200359VALUE *dll_rb_cInteger;
360# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100361# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100362VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200363# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200364VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100366VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200367VALUE *dll_rb_cSymbol;
368VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100369static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100371# ifdef USE_TYPEDDATA
372static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
373# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100375# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100376# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100377static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
378# else
379static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
380# endif
381# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100383# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000384# if RUBY_VERSION >= 31
385static void (*dll_rb_debug_rstring_null_ptr) (const char*);
386# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
388static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
389static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
390static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
391static VALUE (*dll_rb_define_module) (const char*);
392static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
393static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
394static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
395static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200396static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397static VALUE *dll_rb_eArgError;
398static VALUE *dll_rb_eIndexError;
399static VALUE *dll_rb_eRuntimeError;
400static VALUE *dll_rb_eStandardError;
401static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100402# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200403static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
404# else
405static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407static void (*dll_rb_global_variable) (VALUE*);
408static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100409static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410static VALUE (*dll_rb_hash_new) (void);
411static VALUE (*dll_rb_inspect) (VALUE);
412static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200413static ID (*dll_rb_intern) (const char*);
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100414# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200415static long (*dll_rb_fix2int) (VALUE);
416static long (*dll_rb_num2int) (VALUE);
417static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200418# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100419static double (*dll_rb_num2dbl) (VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420static VALUE (*dll_rb_lastline_get) (void);
421static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100422static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200423static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424static long (*dll_rb_num2long) (VALUE);
425static unsigned long (*dll_rb_num2ulong) (VALUE);
426static VALUE (*dll_rb_obj_alloc) (VALUE);
427static VALUE (*dll_rb_obj_as_string) (VALUE);
428static VALUE (*dll_rb_obj_id) (VALUE);
429static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200430static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
432static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
433static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200434# ifdef need_rb_str_new_cstr
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100435// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200436static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200437# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200439# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100440static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441static void (*dll_ruby_init) (void);
442static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100443# ifdef MSWIN
Bram Moolenaar4f391792017-01-15 16:59:07 +0100444static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200445static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
446# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000447# if RUBY_VERSION >= 31
ichizoke1240652022-01-07 20:01:07 +0000448# ifdef _MSC_VER
449static void (*dll_rb_unexpected_type) (VALUE, int);
450# else
451NORETURN(static void (*dll_rb_unexpected_type) (VALUE, int));
452# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000453# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100454static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
455static VALUE (*dll_rb_float_new) (double);
456static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200457static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100458static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
K.Takata236ccbf2022-09-22 16:12:06 +0100459# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100460static void (*dll_rb_ary_detransient) (VALUE);
K.Takata236ccbf2022-09-22 16:12:06 +0100461# endif
462# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200463static void * (*dll_rb_ia64_bsp) (void);
464static void (*dll_ruby_init_stack)(VALUE*, void*);
K.Takata236ccbf2022-09-22 16:12:06 +0100465# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200466static void (*dll_ruby_init_stack)(VALUE*);
467# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100468static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469
Bram Moolenaar165641d2010-02-17 16:23:09 +0100470static void (*dll_ruby_script) (const char*);
471static int (*dll_rb_enc_find_index) (const char*);
472static rb_encoding* (*dll_rb_enc_find) (const char*);
473static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
474static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100475static VALUE (*dll_rb_require) (const char*);
Bram Moolenaardace9f72020-12-28 15:07:45 +0100476static void* (*dll_ruby_options)(int, char**);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100477
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100478# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100479# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100480static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100481# else
482static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
483# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100484# endif
485
Bram Moolenaardace9f72020-12-28 15:07:45 +0100486# if RUBY_VERSION >= 30
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100487# ifdef _MSC_VER
488static void (*dll_ruby_malloc_size_overflow)(size_t, size_t);
489# else
Bram Moolenaardace9f72020-12-28 15:07:45 +0100490NORETURN(static void (*dll_ruby_malloc_size_overflow)(size_t, size_t));
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100491# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100492# endif
493
Bram Moolenaar0e121402020-12-10 20:50:34 +0100494# if RUBY_VERSION >= 26
495void rb_ary_detransient_stub(VALUE x);
496# endif
497
Bram Moolenaardace9f72020-12-28 15:07:45 +0100498// Do not generate a prototype here, VALUE isn't always defined.
499# ifndef PROTO
K.Takata236ccbf2022-09-22 16:12:06 +0100500# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100501 long
502rb_num2long_stub(VALUE x)
K.Takata236ccbf2022-09-22 16:12:06 +0100503# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100504 SIGNED_VALUE
505rb_num2long_stub(VALUE x)
K.Takata236ccbf2022-09-22 16:12:06 +0100506# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100507{
508 return dll_rb_num2long(x);
509}
K.Takata236ccbf2022-09-22 16:12:06 +0100510# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100511 VALUE
512rb_int2big_stub(intptr_t x)
K.Takata236ccbf2022-09-22 16:12:06 +0100513# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100514 VALUE
515rb_int2big_stub(SIGNED_VALUE x)
K.Takata236ccbf2022-09-22 16:12:06 +0100516# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100517{
518 return dll_rb_int2big(x);
519}
K.Takata236ccbf2022-09-22 16:12:06 +0100520# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100521 long
522rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200523{
524 return dll_rb_fix2int(x);
525}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100526 long
527rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200528{
529 return dll_rb_num2int(x);
530}
K.Takata236ccbf2022-09-22 16:12:06 +0100531# endif
532# if RUBY_VERSION >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100533 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100534rb_float_new_in_heap(double d)
535{
536 return dll_rb_float_new(d);
537}
K.Takata236ccbf2022-09-22 16:12:06 +0100538# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100539 unsigned long
540rb_num2ulong(VALUE x)
K.Takata236ccbf2022-09-22 16:12:06 +0100541# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100542 VALUE
543rb_num2ulong(VALUE x)
K.Takata236ccbf2022-09-22 16:12:06 +0100544# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100545{
546 return (long)RSHIFT((SIGNED_VALUE)(x),1);
547}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200548# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100549# if defined(USE_RGENGC) && USE_RGENGC
550# if RUBY_VERSION == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100551 void
552rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100553{
Bram Moolenaar90140742014-11-27 17:44:08 +0100554 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100555}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100556# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100557 void
558rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100559{
560 dll_rb_gc_writebarrier_unprotect(obj);
561}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100562# endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100563# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100564# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100565 void
566rb_ary_detransient_stub(VALUE x)
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100567{
568 dll_rb_ary_detransient(x);
569}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100570# endif
571# if RUBY_VERSION >= 30
572 void
573rb_check_type_stub(VALUE obj, int t)
574{
575 dll_rb_check_type(obj, t);
576}
577 unsigned long
578rb_num2uint_stub(VALUE x)
579{
580 return dll_rb_num2uint(x);
581}
582 void
583ruby_malloc_size_overflow_stub(size_t x, size_t y)
584{
585 dll_ruby_malloc_size_overflow(x, y);
586}
587# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000588# if RUBY_VERSION >= 31
589 void
590rb_debug_rstring_null_ptr_stub(const char *func)
591{
592 dll_rb_debug_rstring_null_ptr(func);
593}
594 void
595rb_unexpected_type_stub(VALUE self, int t)
596{
597 dll_rb_unexpected_type(self, t);
598}
599# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100600# endif // ifndef PROTO
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100601
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100602static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603
604/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000605 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607static struct
608{
609 char *name;
610 RUBY_PROC *ptr;
611} ruby_funcname_table[] =
612{
613 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
614 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200615# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200616 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100617# else
618 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200619# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100620# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100621 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
624 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100625 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
627 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100628 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100630# ifdef USE_TYPEDDATA
631 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100634# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100635# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100636 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
637# else
638 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
639# endif
640# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100642# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000643# if RUBY_VERSION >= 31
644 {"rb_debug_rstring_null_ptr", (RUBY_PROC*)&dll_rb_debug_rstring_null_ptr},
645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
647 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
648 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
649 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
650 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
651 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
652 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
653 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
654 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200655 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
657 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
658 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
659 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
660 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100661# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200662 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
663# else
664 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
665# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
667 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100668 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
670 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
671 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200672 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100673# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200674 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
675 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
676 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200677# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100678 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
680 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200681 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
682 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
684 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
685 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
686 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
687 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
688 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200689 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
691 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
692 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200693# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200694 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200695# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200697# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100698 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
700 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100701# ifdef MSWIN
Bram Moolenaar4f391792017-01-15 16:59:07 +0100702 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200704# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000705# if RUBY_VERSION >= 31
706 {"rb_unexpected_type", (RUBY_PROC*)&dll_rb_unexpected_type},
707# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100708 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
K.Takata236ccbf2022-09-22 16:12:06 +0100709# if RUBY_VERSION >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100710 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
K.Takata236ccbf2022-09-22 16:12:06 +0100711# else
712 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200713# endif
K.Takata236ccbf2022-09-22 16:12:06 +0100714 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
715# ifdef RB_ARY_NEW4_MACRO
716 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
717# else
718 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
719# endif
720 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
721# if RUBY_VERSION >= 26
722 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
723# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100724 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100725 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
726 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
727 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
728 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
729 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100730 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100731 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
K.Takata236ccbf2022-09-22 16:12:06 +0100732# ifdef __ia64
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200733 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200734# endif
K.Takata236ccbf2022-09-22 16:12:06 +0100735 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100736# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100737# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100738 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100739# else
740 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
741# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100742# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100743# if RUBY_VERSION >= 30
744 {"ruby_malloc_size_overflow", (RUBY_PROC*)&dll_ruby_malloc_size_overflow},
745# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {"", NULL},
747};
748
749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 * Load library and get all pointers.
751 * Parameter 'libname' provides name of DLL.
752 * Return OK or FAIL.
753 */
754 static int
755ruby_runtime_link_init(char *libname, int verbose)
756{
757 int i;
758
759 if (hinstRuby)
760 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200761 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 if (!hinstRuby)
763 {
764 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000765 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 return FAIL;
767 }
768
769 for (i = 0; ruby_funcname_table[i].ptr; ++i)
770 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200771 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 ruby_funcname_table[i].name)))
773 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200774 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200775 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000777 semsg(_(e_could_not_load_library_function_str), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 return FAIL;
779 }
780 }
781 return OK;
782}
783
784/*
785 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
786 * else FALSE.
787 */
788 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100789ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100791 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100793#endif // defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
795 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100796ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798}
799
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100800 void
801ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802{
803 int state;
804 char *script = NULL;
805
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000806 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 if (!eap->skip && ensure_ruby_initialized())
808 {
809 if (script == NULL)
810 rb_eval_string_protect((char *)eap->arg, &state);
811 else
812 rb_eval_string_protect(script, &state);
813 if (state)
814 error_print(state);
815 }
816 vim_free(script);
817}
818
Bram Moolenaar165641d2010-02-17 16:23:09 +0100819/*
820 * In Ruby 1.9 or later, ruby String object has encoding.
821 * conversion buffer string of vim to ruby String object using
822 * VIM encoding option.
823 */
824 static VALUE
825vim_str2rb_enc_str(const char *s)
826{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100827 long lval;
828 char_u *sval;
829 rb_encoding *enc;
830
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000831 if (get_option_value((char_u *)"enc", &lval, &sval, NULL, 0) == gov_string)
Bram Moolenaar165641d2010-02-17 16:23:09 +0100832 {
833 enc = rb_enc_find((char *)sval);
834 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200835 if (enc)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200836 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100837 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100838 return rb_str_new2(s);
839}
840
841 static VALUE
842eval_enc_string_protect(const char *str, int *state)
843{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100844 long lval;
845 char_u *sval;
846 rb_encoding *enc;
847 VALUE v;
848
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000849 if (get_option_value((char_u *)"enc", &lval, &sval, NULL, 0) == gov_string)
Bram Moolenaar165641d2010-02-17 16:23:09 +0100850 {
851 enc = rb_enc_find((char *)sval);
852 vim_free(sval);
853 if (enc)
854 {
855 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
856 return rb_eval_string_protect(StringValuePtr(v), state);
857 }
858 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100859 return rb_eval_string_protect(str, state);
860}
861
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100862 void
863ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864{
865 int state;
866 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100867 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000869 if (!ensure_ruby_initialized())
870 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000872 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
873 return;
874 for (i = eap->line1; i <= eap->line2; i++)
875 {
876 VALUE line;
877
878 if (i > curbuf->b_ml.ml_line_count)
879 break;
880 line = vim_str2rb_enc_str((char *)ml_get(i));
881 rb_lastline_set(line);
882 eval_enc_string_protect((char *) eap->arg, &state);
883 if (state)
884 {
885 error_print(state);
886 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000888 if (was_curbuf != curbuf)
889 break;
890 line = rb_lastline_get();
891 if (!NIL_P(line))
892 {
893 if (TYPE(line) != T_STRING)
894 {
895 emsg(_(e_dollar_must_be_an_instance_of_string));
896 return;
897 }
898 ml_replace(i, (char_u *) StringValuePtr(line), 1);
899 changed();
900#ifdef SYNTAX_HL
901 syn_changed(i); // recompute syntax hl. for this line
902#endif
903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000905 check_cursor();
906 update_curbuf(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907}
908
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100909 static VALUE
910rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100911{
912 rb_load(file_to_load, 0);
913 return Qnil;
914}
915
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100916 void
917ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918{
919 int state;
920
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000921 if (!ensure_ruby_initialized())
922 return;
923
924 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
925 rb_protect(rb_load_wrap, file_to_load, &state);
926 if (state)
927 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928}
929
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100930 void
931ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000933 if (buf->b_ruby_ref == NULL)
934 return;
935
936 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
937 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938}
939
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100940 void
941ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000943 if (win->w_ruby_ref == NULL)
944 return;
945
946 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
947 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948}
949
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100950 static int
951ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000953 if (ruby_initialized)
954 return ruby_initialized;
955
956#ifdef DYNAMIC_RUBY
957 if (ruby_enabled(TRUE))
958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100960#ifdef MSWIN
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000961 // suggested by Ariya Mizutani
962 int argc = 1;
963 char *argv[] = {"gvim.exe"};
964 char **argvp = argv;
965 ruby_sysinit(&argc, &argvp);
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000968 ruby_init_stack(ruby_stack_start);
969 ruby_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000971 {
972 int dummy_argc = 2;
973 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
974 ruby_options(dummy_argc, dummy_argv);
975 }
976 ruby_script("vim-ruby");
977 ruby_io_init();
978 ruby_vim_init();
979 ruby_initialized = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000981#ifdef DYNAMIC_RUBY
982 else
983 {
984 emsg(_(e_sorry_this_command_is_disabled_the_ruby_library_could_not_be_loaded));
985 return 0;
986 }
987#endif
988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 return ruby_initialized;
990}
991
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100992 static void
993error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994{
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200995 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 VALUE eclass;
997 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200998 VALUE bt;
999 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001001 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002
1003#define TAG_RETURN 0x1
1004#define TAG_BREAK 0x2
1005#define TAG_NEXT 0x3
1006#define TAG_RETRY 0x4
1007#define TAG_REDO 0x5
1008#define TAG_RAISE 0x6
1009#define TAG_THROW 0x7
1010#define TAG_FATAL 0x8
1011#define TAG_MASK 0xf
1012
Bram Moolenaar758535a2016-03-30 22:06:16 +02001013 switch (state)
1014 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001015 case TAG_RETURN:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001016 emsg(_(e_unexpected_return));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001017 break;
1018 case TAG_NEXT:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001019 emsg(_(e_unexpected_next));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001020 break;
1021 case TAG_BREAK:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001022 emsg(_(e_unexpected_break));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001023 break;
1024 case TAG_REDO:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001025 emsg(_(e_unexpected_redo));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001026 break;
1027 case TAG_RETRY:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001028 emsg(_(e_retry_outside_of_rescue_clause));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001029 break;
1030 case TAG_RAISE:
1031 case TAG_FATAL:
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001032 error = rb_errinfo();
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001033 eclass = CLASS_OF(error);
1034 einfo = rb_obj_as_string(error);
1035 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1036 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001037 emsg(_(e_unhandled_exception));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001038 }
1039 else
1040 {
1041 VALUE epath;
1042 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001044 epath = rb_class_path(eclass);
1045 vim_snprintf(buff, BUFSIZ, "%s: %s",
1046 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1047 p = strchr(buff, '\n');
1048 if (p) *p = '\0';
1049 emsg(buff);
1050 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001051
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001052 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001053#if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001054 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1055 for (i = 0; i < RARRAY_LEN(bt); i++)
1056 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001057#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001058 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1059 for (i = 0; i < RARRAY_LEN(bt); i++)
1060 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001061#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001062 break;
1063 default:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001064 vim_snprintf(buff, BUFSIZ, _(e_unknown_longjmp_status_nr), state);
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001065 emsg(buff);
1066 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 }
1068}
1069
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001070 static VALUE
1071vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072{
1073 char *buff, *p;
1074
1075 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001076 if (RSTRING_LEN(str) > 0)
1077 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001078 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001079 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001080 strcpy(buff, RSTRING_PTR(str));
1081 p = strchr(buff, '\n');
1082 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001083 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001084 }
1085 else
1086 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001087 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 return Qnil;
1090}
1091
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001092 static VALUE
1093vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001095 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001096 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 return Qnil;
1098}
1099
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001100 static VALUE
1101vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001103 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 return Qnil;
1105}
1106
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001107#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001108 static VALUE
1109vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001110{
1111 VALUE result = Qnil;
1112
1113 if (tv->v_type == VAR_STRING)
1114 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001115 result = rb_str_new2(tv->vval.v_string == NULL
1116 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001117 }
1118 else if (tv->v_type == VAR_NUMBER)
1119 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001120 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001121 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001122 else if (tv->v_type == VAR_FLOAT)
1123 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001124 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001125 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001126 else if (tv->v_type == VAR_LIST)
1127 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001128 list_T *list = tv->vval.v_list;
1129 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001130
Bram Moolenaar639a2552010-03-17 18:15:23 +01001131 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001132
Bram Moolenaar639a2552010-03-17 18:15:23 +01001133 if (list != NULL)
1134 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001135 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001136 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001137 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001138 }
1139 else if (tv->v_type == VAR_DICT)
1140 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001141 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001142
Bram Moolenaar639a2552010-03-17 18:15:23 +01001143 if (tv->vval.v_dict != NULL)
1144 {
1145 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1146 long_u todo = ht->ht_used;
1147 hashitem_T *hi;
1148 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001149
Bram Moolenaar639a2552010-03-17 18:15:23 +01001150 for (hi = ht->ht_array; todo > 0; ++hi)
1151 {
1152 if (!HASHITEM_EMPTY(hi))
1153 {
1154 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001155
Bram Moolenaar639a2552010-03-17 18:15:23 +01001156 di = dict_lookup(hi);
1157 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001158 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001159 }
1160 }
1161 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001162 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001163 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001164 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001165 if (tv->vval.v_number == VVAL_TRUE)
1166 result = Qtrue;
1167 else if (tv->vval.v_number == VVAL_FALSE)
1168 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001169 }
1170 else if (tv->v_type == VAR_BLOB)
1171 {
1172 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1173 tv->vval.v_blob->bv_ga.ga_len);
1174 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001175 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001176
1177 return result;
1178}
1179#endif
1180
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001181 static VALUE
1182vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
1184#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001185 typval_T *tv;
1186 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001188 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1189 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001190 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001191 result = vim_to_ruby(tv);
1192
1193 free_tv(tv);
1194
1195 return result;
1196#else
1197 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199}
1200
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001201#ifdef USE_TYPEDDATA
1202static size_t buffer_dsize(const void *buf);
1203
1204static const rb_data_type_t buffer_type = {
1205 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001206 {0, 0, buffer_dsize,
1207# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001208 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001209# else
1210 {0, 0}
1211# endif
1212 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001213 0, 0,
1214# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1215 0,
1216# endif
1217};
1218
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001219 static size_t
1220buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001221{
1222 return sizeof(buf_T);
1223}
1224#endif
1225
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001226 static VALUE
1227buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001229 if (buf->b_ruby_ref)
1230 {
1231 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001233 else
1234 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001235#ifdef USE_TYPEDDATA
1236 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1237#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001239#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001240 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1242 return obj;
1243 }
1244}
1245
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001246 static buf_T *
1247get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
1249 buf_T *buf;
1250
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001251#ifdef USE_TYPEDDATA
1252 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1253#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (buf == NULL)
1257 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1258 return buf;
1259}
1260
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001261 static VALUE
1262vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001263{
1264 VALUE result = rb_str_new("0z", 2);
1265 char buf[4];
1266 int i;
1267 for (i = 0; i < RSTRING_LEN(str); i++)
1268 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001269 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001270 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001271 }
1272 return result;
1273}
1274
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001275 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001276buffer_s_current(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277{
1278 return buffer_new(curbuf);
1279}
1280
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001281 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001282buffer_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
1283{
1284 return buffer_new(curbuf);
1285}
1286
1287 static VALUE
1288buffer_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289{
1290 buf_T *b;
1291 int n = 0;
1292
Bram Moolenaar29323592016-07-24 22:04:11 +02001293 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001294 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001295 // Deleted buffers should not be counted
1296 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001297 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001298 n++;
1299 }
1300
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 return INT2NUM(n);
1302}
1303
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001304 static VALUE
1305buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306{
1307 buf_T *b;
1308 int n = NUM2INT(num);
1309
Bram Moolenaar29323592016-07-24 22:04:11 +02001310 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001311 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001312 // Deleted buffers should not be counted
1313 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001314 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001315 continue;
1316
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001317 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001319
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001320 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 return Qnil;
1323}
1324
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001325 static VALUE
1326buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327{
1328 buf_T *buf = get_buf(self);
1329
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001330 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331}
1332
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001333 static VALUE
1334buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
1336 buf_T *buf = get_buf(self);
1337
1338 return INT2NUM(buf->b_fnum);
1339}
1340
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001341 static VALUE
1342buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
1344 buf_T *buf = get_buf(self);
1345
1346 return INT2NUM(buf->b_ml.ml_line_count);
1347}
1348
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001349 static VALUE
1350get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001352 if (n <= 0 || n > buf->b_ml.ml_line_count)
1353 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1354 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355}
1356
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001357 static VALUE
1358buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359{
1360 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001361
1362 if (buf != NULL)
1363 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001364 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001365}
1366
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001367 static VALUE
1368set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001369{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001370 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001371 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001373 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1374 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001375 // Set curwin/curbuf for "buf" and save some things.
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001376 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00001377 if (curbuf == buf)
Bram Moolenaar758535a2016-03-30 22:06:16 +02001378 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001379 // Only when it worked to set "curbuf".
1380 if (u_savesub(n) == OK)
1381 {
1382 ml_replace(n, (char_u *)line, TRUE);
1383 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384#ifdef SYNTAX_HL
Bram Moolenaare76062c2022-11-28 18:51:43 +00001385 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386#endif
Bram Moolenaare76062c2022-11-28 18:51:43 +00001387 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001388
Bram Moolenaare76062c2022-11-28 18:51:43 +00001389 // restore curwin/curbuf and a few other things
1390 aucmd_restbuf(&aco);
1391 // Careful: autocommands may have made "buf" invalid!
1392 }
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001393
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001394 update_curbuf(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001396 else
1397 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001398 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 }
1400 return str;
1401}
1402
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001403 static VALUE
1404buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001405{
1406 buf_T *buf = get_buf(self);
1407
1408 if (buf != NULL)
1409 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1410 return str;
1411}
1412
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001413 static VALUE
1414buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001416 buf_T *buf = get_buf(self);
1417 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001418 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001420 if (n > 0 && n <= buf->b_ml.ml_line_count)
1421 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001422 // Set curwin/curbuf for "buf" and save some things.
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001423 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00001424 if (curbuf == buf)
Bram Moolenaar758535a2016-03-30 22:06:16 +02001425 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001426 // Only when it worked to set "curbuf".
1427 if (u_savedel(n, 1) == OK)
1428 {
1429 ml_delete(n);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001430
Bram Moolenaare76062c2022-11-28 18:51:43 +00001431 // Changes to non-active buffers should properly refresh
1432 // SegPhault - 01/09/05
1433 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001434
Bram Moolenaare76062c2022-11-28 18:51:43 +00001435 changed();
1436 }
1437
1438 // restore curwin/curbuf and a few other things
1439 aucmd_restbuf(&aco);
1440 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001442
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001443 update_curbuf(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001445 else
1446 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001447 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 }
1449 return Qnil;
1450}
1451
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001452 static VALUE
1453buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001455 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001456 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001457 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001458 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459
Bram Moolenaar3c531602010-11-16 14:46:19 +01001460 if (line == NULL)
1461 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001462 rb_raise(rb_eIndexError, "NULL line");
1463 }
1464 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001465 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001466 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001467 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00001468 if (curbuf == buf)
Bram Moolenaar758535a2016-03-30 22:06:16 +02001469 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001470 // Only when it worked to set "curbuf".
1471 if (u_inssub(n + 1) == OK)
1472 {
1473 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001474
Bram Moolenaare76062c2022-11-28 18:51:43 +00001475 // Changes to non-active buffers should properly refresh screen
1476 // SegPhault - 12/20/04
1477 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001478
Bram Moolenaare76062c2022-11-28 18:51:43 +00001479 changed();
1480 }
1481
1482 // restore curwin/curbuf and a few other things
1483 aucmd_restbuf(&aco);
1484 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001486
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001487 update_curbuf(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 }
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
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001557window_s_current(VALUE self UNUSED)
1558{
1559 return window_new(curwin);
1560}
1561
1562 static VALUE
1563window_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565 return window_new(curwin);
1566}
1567
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001568/*
1569 * Added line manipulation functions
1570 * SegPhault - 03/07/05
1571 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001572 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001573line_s_current(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001574{
1575 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1576}
1577
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001578 static VALUE
1579set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001580{
1581 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1582}
1583
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001584 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001585current_line_number(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001586{
1587 return INT2FIX((int)curwin->w_cursor.lnum);
1588}
1589
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001590 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001591window_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 win_T *w;
1594 int n = 0;
1595
Bram Moolenaar29323592016-07-24 22:04:11 +02001596 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 n++;
1598 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599}
1600
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001601 static VALUE
1602window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604 win_T *w;
1605 int n = NUM2INT(num);
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (n == 0)
1609 return window_new(w);
1610 return Qnil;
1611}
1612
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001613 static VALUE
1614window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615{
1616 win_T *win = get_win(self);
1617
1618 return buffer_new(win->w_buffer);
1619}
1620
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001621 static VALUE
1622window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
1624 win_T *win = get_win(self);
1625
1626 return INT2NUM(win->w_height);
1627}
1628
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001629 static VALUE
1630window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
1632 win_T *win = get_win(self);
1633 win_T *savewin = curwin;
1634
1635 curwin = win;
1636 win_setheight(NUM2INT(height));
1637 curwin = savewin;
1638 return height;
1639}
1640
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001641 static VALUE
1642window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001643{
Bram Moolenaare745d752017-09-22 16:56:22 +02001644 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001645}
1646
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001647 static VALUE
1648window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001649{
1650 win_T *win = get_win(self);
1651 win_T *savewin = curwin;
1652
1653 curwin = win;
1654 win_setwidth(NUM2INT(width));
1655 curwin = savewin;
1656 return width;
1657}
1658
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001659 static VALUE
1660window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661{
1662 win_T *win = get_win(self);
1663
1664 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1665}
1666
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001667 static VALUE
1668window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
1670 VALUE lnum, col;
1671 win_T *win = get_win(self);
1672
1673 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001674 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001676 lnum = RARRAY_PTR(pos)[0];
1677 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 win->w_cursor.lnum = NUM2LONG(lnum);
1679 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001680 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001681 check_cursor(); // put cursor on an existing line
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001682 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 return Qnil;
1684}
1685
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001686 static VALUE
1687f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001688{
1689 return Qnil;
1690}
1691
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001692 static VALUE
1693f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694{
1695 int i;
1696 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001697 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
Bram Moolenaar758535a2016-03-30 22:06:16 +02001699 for (i = 0; i < argc; i++)
1700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if (i > 0) rb_str_cat(str, ", ", 2);
1702 rb_str_concat(str, rb_inspect(argv[i]));
1703 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001704 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001705
1706 if (argc == 1)
1707 ret = argv[0];
1708 else if (argc > 1)
1709 ret = rb_ary_new4(argc, argv);
1710 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711}
1712
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001713 static void
1714ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
1716#ifndef DYNAMIC_RUBY
1717 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001718 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719#endif
1720
1721 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001722 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001724 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001725 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1726 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 rb_define_global_function("p", f_p, -1);
1728}
1729
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001730 static void
1731ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
1733 objtbl = rb_hash_new();
1734 rb_global_variable(&objtbl);
1735
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001736 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1737 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001738 mVIM = rb_define_module("Vim");
1739 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1741 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1742 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1743 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1744 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1745 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1746 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1747 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1748 rb_define_module_function(mVIM, "message", vim_message, 1);
1749 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1750 rb_define_module_function(mVIM, "command", vim_command, 1);
1751 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001752 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
1754 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1755 rb_eStandardError);
1756 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1757 rb_eStandardError);
1758
1759 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1760 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1761 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1762 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1763 rb_define_method(cBuffer, "name", buffer_name, 0);
1764 rb_define_method(cBuffer, "number", buffer_number, 0);
1765 rb_define_method(cBuffer, "count", buffer_count, 0);
1766 rb_define_method(cBuffer, "length", buffer_count, 0);
1767 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1768 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1769 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1770 rb_define_method(cBuffer, "append", buffer_append, 2);
1771
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001772 // Added line manipulation functions
1773 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001774 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1775 rb_define_method(cBuffer, "line", line_s_current, 0);
1776 rb_define_method(cBuffer, "line=", set_current_line, 1);
1777
1778
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1780 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1781 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1782 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1783 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1784 rb_define_method(cVimWindow, "height", window_height, 0);
1785 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001786 rb_define_method(cVimWindow, "width", window_width, 0);
1787 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1789 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1790
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001791 rb_define_virtual_variable("$curbuf", buffer_s_current_getter, 0);
1792 rb_define_virtual_variable("$curwin", window_s_current_getter, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001794
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001795 void
1796vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001797{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001798 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001799 ruby_stack_start = stack_start;
1800}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001801
1802 static int
1803convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1804{
1805 dict_T *d = (dict_T *)arg;
1806 dictitem_T *di;
1807
Bram Moolenaardace9f72020-12-28 15:07:45 +01001808 di = dictitem_alloc((char_u *)RSTRING_PTR(rb_obj_as_string(key)));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001809 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1810 || dict_add(d, di) != OK)
1811 {
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001812 d->dv_hashtab.ht_flags |= HTFLAGS_ERROR;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001813 return ST_STOP;
1814 }
1815 return ST_CONTINUE;
1816}
1817
1818 static int
1819ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1820{
1821 switch (TYPE(val))
1822 {
1823 case T_NIL:
1824 rettv->v_type = VAR_SPECIAL;
1825 rettv->vval.v_number = VVAL_NULL;
1826 break;
1827 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001828 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001829 rettv->vval.v_number = VVAL_TRUE;
1830 break;
1831 case T_FALSE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001832 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001833 rettv->vval.v_number = VVAL_FALSE;
1834 break;
1835 case T_BIGNUM:
1836 case T_FIXNUM:
1837 rettv->v_type = VAR_NUMBER;
1838 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1839 break;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001840 case T_FLOAT:
1841 rettv->v_type = VAR_FLOAT;
1842 rettv->vval.v_float = (float_T)NUM2DBL(val);
1843 break;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001844 default:
1845 val = rb_obj_as_string(val);
1846 // FALLTHROUGH
1847 case T_STRING:
1848 {
1849 VALUE str = (VALUE)RSTRING(val);
1850
1851 rettv->v_type = VAR_STRING;
1852 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001853 RSTRING_LEN(str));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001854 }
1855 break;
1856 case T_ARRAY:
1857 {
1858 list_T *l;
1859 long i;
1860 typval_T v;
1861
1862 l = list_alloc();
1863 if (l == NULL)
1864 return FAIL;
1865
1866 for (i = 0; i < RARRAY_LEN(val); ++i)
1867 {
1868 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1869 &v) != OK)
1870 {
1871 list_unref(l);
1872 return FAIL;
1873 }
1874 list_append_tv(l, &v);
1875 clear_tv(&v);
1876 }
1877
1878 rettv->v_type = VAR_LIST;
1879 rettv->vval.v_list = l;
1880 ++l->lv_refcount;
1881 }
1882 break;
1883 case T_HASH:
1884 {
1885 dict_T *d;
1886
1887 d = dict_alloc();
1888 if (d == NULL)
1889 return FAIL;
1890
1891 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001892 if (d->dv_hashtab.ht_flags & HTFLAGS_ERROR)
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001893 {
1894 dict_unref(d);
1895 return FAIL;
1896 }
1897
1898 rettv->v_type = VAR_DICT;
1899 rettv->vval.v_dict = d;
1900 ++d->dv_refcount;
1901 }
1902 break;
1903 }
1904 return OK;
1905}
1906
1907 void
1908do_rubyeval(char_u *str, typval_T *rettv)
1909{
1910 int retval = FAIL;
1911
1912 if (ensure_ruby_initialized())
1913 {
1914 int state;
1915 VALUE obj;
1916
1917 obj = rb_eval_string_protect((const char *)str, &state);
1918 if (state)
1919 error_print(state);
1920 else
1921 retval = ruby_convert_to_vim_value(obj, rettv);
1922 }
1923 if (retval == FAIL)
1924 {
1925 rettv->v_type = VAR_NUMBER;
1926 rettv->vval.v_number = 0;
1927 }
1928}