blob: feb1d500960fc3c380f62d571fb1e089d249c9f9 [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
869 if (ensure_ruby_initialized())
870 {
871 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
872 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200873 for (i = eap->line1; i <= eap->line2; i++)
874 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100875 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100877 if (i > curbuf->b_ml.ml_line_count)
878 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100879 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100881 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200882 if (state)
883 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 error_print(state);
885 break;
886 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100887 if (was_curbuf != curbuf)
888 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200890 if (!NIL_P(line))
891 {
892 if (TYPE(line) != T_STRING)
893 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000894 emsg(_(e_dollar_must_be_an_instance_of_string));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 return;
896 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100897 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 changed();
899#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100900 syn_changed(i); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901#endif
902 }
903 }
904 check_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100905 update_curbuf(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 }
907}
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
921 if (ensure_ruby_initialized())
922 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100923 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
924 rb_protect(rb_load_wrap, file_to_load, &state);
925 if (state)
926 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 }
928}
929
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100930 void
931ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000933 if (buf->b_ruby_ref)
934 {
935 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
936 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 }
938}
939
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100940 void
941ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000943 if (win->w_ruby_ref)
944 {
945 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
946 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 }
948}
949
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100950 static int
951ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952{
953 if (!ruby_initialized)
954 {
955#ifdef DYNAMIC_RUBY
956 if (ruby_enabled(TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957#endif
K.Takata236ccbf2022-09-22 16:12:06 +0100958 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100959#ifdef MSWIN
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100960 // suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100961 int argc = 1;
962 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100963 char **argvp = argv;
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100964 ruby_sysinit(&argc, &argvp);
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100965#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200966 {
Bram Moolenaar99685e62013-05-11 13:56:18 +0200967 ruby_init_stack(ruby_stack_start);
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200968 ruby_init();
969 }
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100970 {
971 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200972 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100973 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100974 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100975 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100976 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 ruby_vim_init();
978 ruby_initialized = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
K.Takata236ccbf2022-09-22 16:12:06 +0100980#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 else
982 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000983 emsg(_(e_sorry_this_command_is_disabled_the_ruby_library_could_not_be_loaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 return 0;
985 }
986#endif
987 }
988 return ruby_initialized;
989}
990
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100991 static void
992error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993{
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200994 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 VALUE eclass;
996 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200997 VALUE bt;
998 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001000 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
1002#define TAG_RETURN 0x1
1003#define TAG_BREAK 0x2
1004#define TAG_NEXT 0x3
1005#define TAG_RETRY 0x4
1006#define TAG_REDO 0x5
1007#define TAG_RAISE 0x6
1008#define TAG_THROW 0x7
1009#define TAG_FATAL 0x8
1010#define TAG_MASK 0xf
1011
Bram Moolenaar758535a2016-03-30 22:06:16 +02001012 switch (state)
1013 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001014 case TAG_RETURN:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001015 emsg(_(e_unexpected_return));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001016 break;
1017 case TAG_NEXT:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001018 emsg(_(e_unexpected_next));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001019 break;
1020 case TAG_BREAK:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001021 emsg(_(e_unexpected_break));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001022 break;
1023 case TAG_REDO:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001024 emsg(_(e_unexpected_redo));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001025 break;
1026 case TAG_RETRY:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001027 emsg(_(e_retry_outside_of_rescue_clause));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001028 break;
1029 case TAG_RAISE:
1030 case TAG_FATAL:
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001031 error = rb_errinfo();
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001032 eclass = CLASS_OF(error);
1033 einfo = rb_obj_as_string(error);
1034 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1035 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001036 emsg(_(e_unhandled_exception));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001037 }
1038 else
1039 {
1040 VALUE epath;
1041 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001043 epath = rb_class_path(eclass);
1044 vim_snprintf(buff, BUFSIZ, "%s: %s",
1045 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1046 p = strchr(buff, '\n');
1047 if (p) *p = '\0';
1048 emsg(buff);
1049 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001050
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001051 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001052#if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001053 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1054 for (i = 0; i < RARRAY_LEN(bt); i++)
1055 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001056#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001057 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1058 for (i = 0; i < RARRAY_LEN(bt); i++)
1059 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001060#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001061 break;
1062 default:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001063 vim_snprintf(buff, BUFSIZ, _(e_unknown_longjmp_status_nr), state);
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001064 emsg(buff);
1065 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 }
1067}
1068
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001069 static VALUE
1070vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
1072 char *buff, *p;
1073
1074 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001075 if (RSTRING_LEN(str) > 0)
1076 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001077 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001078 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001079 strcpy(buff, RSTRING_PTR(str));
1080 p = strchr(buff, '\n');
1081 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001082 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001083 }
1084 else
1085 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001086 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 return Qnil;
1089}
1090
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001091 static VALUE
1092vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001094 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001095 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 return Qnil;
1097}
1098
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001099 static VALUE
1100vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001102 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 return Qnil;
1104}
1105
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001106#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001107 static VALUE
1108vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001109{
1110 VALUE result = Qnil;
1111
1112 if (tv->v_type == VAR_STRING)
1113 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001114 result = rb_str_new2(tv->vval.v_string == NULL
1115 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001116 }
1117 else if (tv->v_type == VAR_NUMBER)
1118 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001119 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001120 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001121 else if (tv->v_type == VAR_FLOAT)
1122 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001123 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001124 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001125 else if (tv->v_type == VAR_LIST)
1126 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001127 list_T *list = tv->vval.v_list;
1128 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001129
Bram Moolenaar639a2552010-03-17 18:15:23 +01001130 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001131
Bram Moolenaar639a2552010-03-17 18:15:23 +01001132 if (list != NULL)
1133 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001134 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001135 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001136 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001137 }
1138 else if (tv->v_type == VAR_DICT)
1139 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001140 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001141
Bram Moolenaar639a2552010-03-17 18:15:23 +01001142 if (tv->vval.v_dict != NULL)
1143 {
1144 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1145 long_u todo = ht->ht_used;
1146 hashitem_T *hi;
1147 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001148
Bram Moolenaar639a2552010-03-17 18:15:23 +01001149 for (hi = ht->ht_array; todo > 0; ++hi)
1150 {
1151 if (!HASHITEM_EMPTY(hi))
1152 {
1153 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001154
Bram Moolenaar639a2552010-03-17 18:15:23 +01001155 di = dict_lookup(hi);
1156 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001157 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001158 }
1159 }
1160 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001161 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001162 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001163 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001164 if (tv->vval.v_number == VVAL_TRUE)
1165 result = Qtrue;
1166 else if (tv->vval.v_number == VVAL_FALSE)
1167 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001168 }
1169 else if (tv->v_type == VAR_BLOB)
1170 {
1171 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1172 tv->vval.v_blob->bv_ga.ga_len);
1173 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001174 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001175
1176 return result;
1177}
1178#endif
1179
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001180 static VALUE
1181vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182{
1183#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001184 typval_T *tv;
1185 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001187 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1188 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001189 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001190 result = vim_to_ruby(tv);
1191
1192 free_tv(tv);
1193
1194 return result;
1195#else
1196 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198}
1199
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001200#ifdef USE_TYPEDDATA
1201static size_t buffer_dsize(const void *buf);
1202
1203static const rb_data_type_t buffer_type = {
1204 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001205 {0, 0, buffer_dsize,
1206# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001207 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001208# else
1209 {0, 0}
1210# endif
1211 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001212 0, 0,
1213# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1214 0,
1215# endif
1216};
1217
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001218 static size_t
1219buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001220{
1221 return sizeof(buf_T);
1222}
1223#endif
1224
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001225 static VALUE
1226buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001228 if (buf->b_ruby_ref)
1229 {
1230 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001232 else
1233 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001234#ifdef USE_TYPEDDATA
1235 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1236#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001238#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001239 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1241 return obj;
1242 }
1243}
1244
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001245 static buf_T *
1246get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247{
1248 buf_T *buf;
1249
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001250#ifdef USE_TYPEDDATA
1251 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1252#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 if (buf == NULL)
1256 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1257 return buf;
1258}
1259
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001260 static VALUE
1261vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001262{
1263 VALUE result = rb_str_new("0z", 2);
1264 char buf[4];
1265 int i;
1266 for (i = 0; i < RSTRING_LEN(str); i++)
1267 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001268 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001269 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001270 }
1271 return result;
1272}
1273
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001274 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001275buffer_s_current(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276{
1277 return buffer_new(curbuf);
1278}
1279
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001280 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001281buffer_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
1282{
1283 return buffer_new(curbuf);
1284}
1285
1286 static VALUE
1287buffer_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288{
1289 buf_T *b;
1290 int n = 0;
1291
Bram Moolenaar29323592016-07-24 22:04:11 +02001292 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001293 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001294 // Deleted buffers should not be counted
1295 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001296 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001297 n++;
1298 }
1299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 return INT2NUM(n);
1301}
1302
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001303 static VALUE
1304buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305{
1306 buf_T *b;
1307 int n = NUM2INT(num);
1308
Bram Moolenaar29323592016-07-24 22:04:11 +02001309 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001310 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001311 // Deleted buffers should not be counted
1312 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001313 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001314 continue;
1315
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001316 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001318
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001319 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321 return Qnil;
1322}
1323
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001324 static VALUE
1325buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326{
1327 buf_T *buf = get_buf(self);
1328
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001329 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330}
1331
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001332 static VALUE
1333buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
1335 buf_T *buf = get_buf(self);
1336
1337 return INT2NUM(buf->b_fnum);
1338}
1339
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001340 static VALUE
1341buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342{
1343 buf_T *buf = get_buf(self);
1344
1345 return INT2NUM(buf->b_ml.ml_line_count);
1346}
1347
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001348 static VALUE
1349get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001351 if (n <= 0 || n > buf->b_ml.ml_line_count)
1352 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1353 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354}
1355
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001356 static VALUE
1357buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358{
1359 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001360
1361 if (buf != NULL)
1362 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001363 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001364}
1365
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001366 static VALUE
1367set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001368{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001369 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001370 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001372 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1373 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001374 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001375 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001376
Bram Moolenaar758535a2016-03-30 22:06:16 +02001377 if (u_savesub(n) == OK)
1378 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001379 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 changed();
1381#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001382 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383#endif
1384 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001385
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001386 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001387 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001388 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001389
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001390 update_curbuf(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001392 else
1393 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001394 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 return str;
1397}
1398
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001399 static VALUE
1400buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001401{
1402 buf_T *buf = get_buf(self);
1403
1404 if (buf != NULL)
1405 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1406 return str;
1407}
1408
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001409 static VALUE
1410buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001412 buf_T *buf = get_buf(self);
1413 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001414 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001416 if (n > 0 && n <= buf->b_ml.ml_line_count)
1417 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001418 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001419 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001420
Bram Moolenaar758535a2016-03-30 22:06:16 +02001421 if (u_savedel(n, 1) == OK)
1422 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001423 ml_delete(n);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001424
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001425 // Changes to non-active buffers should properly refresh
1426 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001427 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001428
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 changed();
1430 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001431
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001432 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001433 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001434 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001435
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001436 update_curbuf(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001438 else
1439 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001440 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
1442 return Qnil;
1443}
1444
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001445 static VALUE
1446buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001448 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001449 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001450 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001451 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
Bram Moolenaar3c531602010-11-16 14:46:19 +01001453 if (line == NULL)
1454 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001455 rb_raise(rb_eIndexError, "NULL line");
1456 }
1457 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001458 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001459 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001460 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001461
Bram Moolenaar758535a2016-03-30 22:06:16 +02001462 if (u_inssub(n + 1) == OK)
1463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001465
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001466 // Changes to non-active buffers should properly refresh screen
1467 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001468 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001469
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001470 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001472
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001473 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001474 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001475 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001476
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001477 update_curbuf(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001479 else
1480 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001481 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 }
1483 return str;
1484}
1485
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001486#ifdef USE_TYPEDDATA
1487static size_t window_dsize(const void *buf);
1488
1489static const rb_data_type_t window_type = {
1490 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001491 {0, 0, window_dsize,
1492# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001493 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001494# else
1495 {0, 0}
1496# endif
1497 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001498 0, 0,
1499# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1500 0,
1501# endif
1502};
1503
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001504 static size_t
1505window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001506{
1507 return sizeof(win_T);
1508}
1509#endif
1510
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001511 static VALUE
1512window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001514 if (win->w_ruby_ref)
1515 {
1516 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001518 else
1519 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001520#ifdef USE_TYPEDDATA
1521 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1522#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001524#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001525 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1527 return obj;
1528 }
1529}
1530
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001531 static win_T *
1532get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533{
1534 win_T *win;
1535
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001536#ifdef USE_TYPEDDATA
1537 TypedData_Get_Struct(obj, win_T, &window_type, win);
1538#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 if (win == NULL)
1542 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1543 return win;
1544}
1545
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001546 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001547window_s_current(VALUE self UNUSED)
1548{
1549 return window_new(curwin);
1550}
1551
1552 static VALUE
1553window_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
1555 return window_new(curwin);
1556}
1557
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001558/*
1559 * Added line manipulation functions
1560 * SegPhault - 03/07/05
1561 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001562 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001563line_s_current(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001564{
1565 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1566}
1567
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001568 static VALUE
1569set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001570{
1571 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1572}
1573
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001574 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001575current_line_number(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001576{
1577 return INT2FIX((int)curwin->w_cursor.lnum);
1578}
1579
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001580 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001581window_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 win_T *w;
1584 int n = 0;
1585
Bram Moolenaar29323592016-07-24 22:04:11 +02001586 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 n++;
1588 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589}
1590
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001591 static VALUE
1592window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593{
1594 win_T *w;
1595 int n = NUM2INT(num);
1596
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (n == 0)
1599 return window_new(w);
1600 return Qnil;
1601}
1602
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001603 static VALUE
1604window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605{
1606 win_T *win = get_win(self);
1607
1608 return buffer_new(win->w_buffer);
1609}
1610
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001611 static VALUE
1612window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613{
1614 win_T *win = get_win(self);
1615
1616 return INT2NUM(win->w_height);
1617}
1618
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001619 static VALUE
1620window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
1622 win_T *win = get_win(self);
1623 win_T *savewin = curwin;
1624
1625 curwin = win;
1626 win_setheight(NUM2INT(height));
1627 curwin = savewin;
1628 return height;
1629}
1630
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001631 static VALUE
1632window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001633{
Bram Moolenaare745d752017-09-22 16:56:22 +02001634 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001635}
1636
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001637 static VALUE
1638window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001639{
1640 win_T *win = get_win(self);
1641 win_T *savewin = curwin;
1642
1643 curwin = win;
1644 win_setwidth(NUM2INT(width));
1645 curwin = savewin;
1646 return width;
1647}
1648
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001649 static VALUE
1650window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651{
1652 win_T *win = get_win(self);
1653
1654 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1655}
1656
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001657 static VALUE
1658window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659{
1660 VALUE lnum, col;
1661 win_T *win = get_win(self);
1662
1663 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001664 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001666 lnum = RARRAY_PTR(pos)[0];
1667 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 win->w_cursor.lnum = NUM2LONG(lnum);
1669 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001670 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001671 check_cursor(); // put cursor on an existing line
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001672 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 return Qnil;
1674}
1675
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001676 static VALUE
1677f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001678{
1679 return Qnil;
1680}
1681
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001682 static VALUE
1683f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684{
1685 int i;
1686 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001687 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688
Bram Moolenaar758535a2016-03-30 22:06:16 +02001689 for (i = 0; i < argc; i++)
1690 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 if (i > 0) rb_str_cat(str, ", ", 2);
1692 rb_str_concat(str, rb_inspect(argv[i]));
1693 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001694 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001695
1696 if (argc == 1)
1697 ret = argv[0];
1698 else if (argc > 1)
1699 ret = rb_ary_new4(argc, argv);
1700 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701}
1702
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001703 static void
1704ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705{
1706#ifndef DYNAMIC_RUBY
1707 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001708 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709#endif
1710
1711 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001712 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001714 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001715 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1716 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 rb_define_global_function("p", f_p, -1);
1718}
1719
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001720 static void
1721ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
1723 objtbl = rb_hash_new();
1724 rb_global_variable(&objtbl);
1725
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001726 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1727 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001728 mVIM = rb_define_module("Vim");
1729 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1731 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1732 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1733 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1734 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1735 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1736 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1737 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1738 rb_define_module_function(mVIM, "message", vim_message, 1);
1739 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1740 rb_define_module_function(mVIM, "command", vim_command, 1);
1741 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001742 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743
1744 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1745 rb_eStandardError);
1746 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1747 rb_eStandardError);
1748
1749 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1750 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1751 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1752 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1753 rb_define_method(cBuffer, "name", buffer_name, 0);
1754 rb_define_method(cBuffer, "number", buffer_number, 0);
1755 rb_define_method(cBuffer, "count", buffer_count, 0);
1756 rb_define_method(cBuffer, "length", buffer_count, 0);
1757 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1758 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1759 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1760 rb_define_method(cBuffer, "append", buffer_append, 2);
1761
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001762 // Added line manipulation functions
1763 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001764 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1765 rb_define_method(cBuffer, "line", line_s_current, 0);
1766 rb_define_method(cBuffer, "line=", set_current_line, 1);
1767
1768
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1770 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1771 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1772 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1773 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1774 rb_define_method(cVimWindow, "height", window_height, 0);
1775 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001776 rb_define_method(cVimWindow, "width", window_width, 0);
1777 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1779 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1780
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001781 rb_define_virtual_variable("$curbuf", buffer_s_current_getter, 0);
1782 rb_define_virtual_variable("$curwin", window_s_current_getter, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001784
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001785 void
1786vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001787{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001788 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001789 ruby_stack_start = stack_start;
1790}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001791
1792 static int
1793convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1794{
1795 dict_T *d = (dict_T *)arg;
1796 dictitem_T *di;
1797
Bram Moolenaardace9f72020-12-28 15:07:45 +01001798 di = dictitem_alloc((char_u *)RSTRING_PTR(rb_obj_as_string(key)));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001799 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1800 || dict_add(d, di) != OK)
1801 {
1802 d->dv_hashtab.ht_error = TRUE;
1803 return ST_STOP;
1804 }
1805 return ST_CONTINUE;
1806}
1807
1808 static int
1809ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1810{
1811 switch (TYPE(val))
1812 {
1813 case T_NIL:
1814 rettv->v_type = VAR_SPECIAL;
1815 rettv->vval.v_number = VVAL_NULL;
1816 break;
1817 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001818 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001819 rettv->vval.v_number = VVAL_TRUE;
1820 break;
1821 case T_FALSE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001822 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001823 rettv->vval.v_number = VVAL_FALSE;
1824 break;
1825 case T_BIGNUM:
1826 case T_FIXNUM:
1827 rettv->v_type = VAR_NUMBER;
1828 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1829 break;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001830 case T_FLOAT:
1831 rettv->v_type = VAR_FLOAT;
1832 rettv->vval.v_float = (float_T)NUM2DBL(val);
1833 break;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001834 default:
1835 val = rb_obj_as_string(val);
1836 // FALLTHROUGH
1837 case T_STRING:
1838 {
1839 VALUE str = (VALUE)RSTRING(val);
1840
1841 rettv->v_type = VAR_STRING;
1842 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001843 RSTRING_LEN(str));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001844 }
1845 break;
1846 case T_ARRAY:
1847 {
1848 list_T *l;
1849 long i;
1850 typval_T v;
1851
1852 l = list_alloc();
1853 if (l == NULL)
1854 return FAIL;
1855
1856 for (i = 0; i < RARRAY_LEN(val); ++i)
1857 {
1858 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1859 &v) != OK)
1860 {
1861 list_unref(l);
1862 return FAIL;
1863 }
1864 list_append_tv(l, &v);
1865 clear_tv(&v);
1866 }
1867
1868 rettv->v_type = VAR_LIST;
1869 rettv->vval.v_list = l;
1870 ++l->lv_refcount;
1871 }
1872 break;
1873 case T_HASH:
1874 {
1875 dict_T *d;
1876
1877 d = dict_alloc();
1878 if (d == NULL)
1879 return FAIL;
1880
1881 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1882 if (d->dv_hashtab.ht_error)
1883 {
1884 dict_unref(d);
1885 return FAIL;
1886 }
1887
1888 rettv->v_type = VAR_DICT;
1889 rettv->vval.v_dict = d;
1890 ++d->dv_refcount;
1891 }
1892 break;
1893 }
1894 return OK;
1895}
1896
1897 void
1898do_rubyeval(char_u *str, typval_T *rettv)
1899{
1900 int retval = FAIL;
1901
1902 if (ensure_ruby_initialized())
1903 {
1904 int state;
1905 VALUE obj;
1906
1907 obj = rb_eval_string_protect((const char *)str, &state);
1908 if (state)
1909 error_print(state);
1910 else
1911 retval = ruby_convert_to_vim_value(obj, rettv);
1912 }
1913 if (retval == FAIL)
1914 {
1915 rettv->v_type = VAR_NUMBER;
1916 rettv->vval.v_number = 0;
1917 }
1918}