blob: 7f2a7d265e19d35e6fa5272ba6b8d587d5f0df74 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004 *
5 * Ruby interface by Shugo Maeda
6 * with improvements by SegPhault (Ryan Paul)
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +02007 * with improvements by Jon Maken
Bram Moolenaar071d4272004-06-13 20:20:40 +00008 *
9 * Do ":help uganda" in Vim to read copying and usage conditions.
10 * Do ":help credits" in Vim to see a list of people who contributed.
11 * See README.txt for an overview of the Vim source code.
12 */
13
Bram Moolenaar32d19c12018-09-13 17:26:54 +020014#include "protodef.h"
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020015#ifdef HAVE_CONFIG_H
16# include "auto/config.h"
17#endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020018
Bram Moolenaare2793352011-01-17 19:53:27 +010019#include <stdio.h>
20#include <string.h>
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef _WIN32
Bram Moolenaar41a41412020-01-07 21:32:19 +010023# if !defined(DYNAMIC_RUBY) || (RUBY_VERSION < 18)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024# define NT
25# endif
26# ifndef DYNAMIC_RUBY
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010027# define IMPORT // For static dll usage __declspec(dllimport)
Bram Moolenaar071d4272004-06-13 20:20:40 +000028# define RUBYEXTERN __declspec(dllimport)
29# endif
30#endif
31#ifndef RUBYEXTERN
32# define RUBYEXTERN extern
33#endif
34
Bram Moolenaar41a41412020-01-07 21:32:19 +010035#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 24
Bram Moolenaar2016ae52016-06-13 20:08:43 +020036# define USE_RUBY_INTEGER
Bram Moolenaar06469e92016-06-11 22:26:53 +020037#endif
38
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020039#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000040/*
41 * This is tricky. In ruby.h there is (inline) function rb_class_of()
42 * definition. This function use these variables. But we want function to
43 * use dll_* variables.
44 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045# define rb_cFalseClass (*dll_rb_cFalseClass)
46# define rb_cFixnum (*dll_rb_cFixnum)
Bram Moolenaar2016ae52016-06-13 20:08:43 +020047# if defined(USE_RUBY_INTEGER)
48# define rb_cInteger (*dll_rb_cInteger)
Bram Moolenaar06469e92016-06-11 22:26:53 +020049# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +010050# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +010051# define rb_cFloat (*dll_rb_cFloat)
52# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000053# define rb_cNilClass (*dll_rb_cNilClass)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010054# define rb_cString (*dll_rb_cString)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055# define rb_cSymbol (*dll_rb_cSymbol)
56# define rb_cTrueClass (*dll_rb_cTrueClass)
Bram Moolenaar41a41412020-01-07 21:32:19 +010057# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +000058/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010059 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
60 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 * defined in this file. When defined this RUBY_EXPORT it modified to
62 * "extern" and be able to avoid this problem.
63 */
64# define RUBY_EXPORT
65# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020066
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +010067#endif // ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +010069// suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +010070#if (_MSC_VER == 1200)
71# undef _WIN32_WINNT
72#endif
73
Bram Moolenaar41a41412020-01-07 21:32:19 +010074#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 19
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010075// Ruby 1.9 defines a number of static functions which use rb_num2long and
76// rb_int2big
Bram Moolenaar42d57f02010-03-10 12:47:00 +010077# define rb_num2long rb_num2long_stub
78# define rb_int2big rb_int2big_stub
79#endif
80
Bram Moolenaar41a41412020-01-07 21:32:19 +010081#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 19 \
Bram Moolenaar498af702014-03-28 21:58:21 +010082 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010083// Ruby 1.9 defines a number of static functions which use rb_fix2int and
84// rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +020085# define rb_fix2int rb_fix2int_stub
86# define rb_num2int rb_num2int_stub
87#endif
88
Bram Moolenaar41a41412020-01-07 21:32:19 +010089#if defined(DYNAMIC_RUBY) && RUBY_VERSION == 21
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010090// Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
91// rb_gc_writebarrier_unprotect_promoted if USE_RGENGC
Bram Moolenaar90140742014-11-27 17:44:08 +010092# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
93#endif
Bram Moolenaar41a41412020-01-07 21:32:19 +010094#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 22
Bram Moolenaar0c7485f2015-01-14 14:04:10 +010095# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaar0c7485f2015-01-14 14:04:10 +010096#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +010097
Bram Moolenaar41a41412020-01-07 21:32:19 +010098#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 26
Bram Moolenaarf62fc312019-01-08 20:29:32 +010099# define rb_ary_detransient rb_ary_detransient_stub
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100100#endif
101
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#include <ruby.h>
Bram Moolenaar41a41412020-01-07 21:32:19 +0100103#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100104# include <ruby/encoding.h>
105#endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100106#if RUBY_VERSION <= 18
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200107# include <st.h> // for ST_STOP and ST_CONTINUE
108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200110#undef off_t // ruby defines off_t as _int64, Mingw uses long
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#undef EXTERN
112#undef _
113
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100114// T_DATA defined both by Ruby and Mac header files, hack around it...
Bram Moolenaard0573012017-10-28 21:11:06 +0200115#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116# define __OPENTRANSPORT__
117# define __OPENTRANSPORTPROTOCOL__
118# define __OPENTRANSPORTPROVIDERS__
119#endif
120
Bram Moolenaar165641d2010-02-17 16:23:09 +0100121/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100122 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
123 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
124 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100125 * Use TypedData_XXX if available.
126 */
Bram Moolenaar41a41412020-01-07 21:32:19 +0100127#if defined(TypedData_Wrap_Struct) && (RUBY_VERSION >= 20)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100128# define USE_TYPEDDATA 1
129#endif
130
131/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200132 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100133 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
134 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
135 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
136 */
137#ifndef StringValuePtr
138# define StringValuePtr(s) STR2CSTR(s)
139#endif
140#ifndef RARRAY_LEN
141# define RARRAY_LEN(s) RARRAY(s)->len
142#endif
143#ifndef RARRAY_PTR
144# define RARRAY_PTR(s) RARRAY(s)->ptr
145#endif
146#ifndef RSTRING_LEN
147# define RSTRING_LEN(s) RSTRING(s)->len
148#endif
149#ifndef RSTRING_PTR
150# define RSTRING_PTR(s) RSTRING(s)->ptr
151#endif
152
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100153#ifdef HAVE_DUP
154# undef HAVE_DUP
155#endif
156
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#include "vim.h"
158#include "version.h"
159
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100160#ifdef DYNAMIC_RUBY
161# if !defined(MSWIN) // must come after including vim.h, where it is defined
162# include <dlfcn.h>
163# define HINSTANCE void*
164# define RUBY_PROC void*
165# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
166# define symbol_from_dll dlsym
167# define close_dll dlclose
168# else
169# define RUBY_PROC FARPROC
170# define load_dll vimLoadLib
171# define symbol_from_dll GetProcAddress
172# define close_dll FreeLibrary
173# endif
174#endif
175
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#if defined(PROTO) && !defined(FEAT_RUBY)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100177// Define these to be able to generate the function prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178# define VALUE int
179# define RUBY_DATA_FUNC int
180#endif
181
182static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200183static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184static VALUE objtbl;
185
186static VALUE mVIM;
187static VALUE cBuffer;
188static VALUE cVimWindow;
189static VALUE eDeletedBufferError;
190static VALUE eDeletedWindowError;
191
192static int ensure_ruby_initialized(void);
193static void error_print(int);
194static void ruby_io_init(void);
195static void ruby_vim_init(void);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100196static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaar41a41412020-01-07 21:32:19 +0100198#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200199# if defined(__ia64) && !defined(ruby_init_stack)
200# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
201# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#endif
203
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200204#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100205# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100206# define HINSTANCE int // for generating prototypes
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200207# endif
208
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209/*
210 * Wrapper defines
211 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200212# define rb_assoc_new dll_rb_assoc_new
213# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100214# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100215# define rb_check_type dll_rb_check_type
216# ifdef USE_TYPEDDATA
217# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100218# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200219# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100220# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100221# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100222# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
223# else
224# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
225# endif
226# else
227# define rb_data_object_alloc dll_rb_data_object_alloc
228# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200229# define rb_define_class_under dll_rb_define_class_under
230# define rb_define_const dll_rb_define_const
231# define rb_define_global_function dll_rb_define_global_function
232# define rb_define_method dll_rb_define_method
233# define rb_define_module dll_rb_define_module
234# define rb_define_module_function dll_rb_define_module_function
235# define rb_define_singleton_method dll_rb_define_singleton_method
236# define rb_define_virtual_variable dll_rb_define_virtual_variable
237# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200238# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200239# define rb_eArgError (*dll_rb_eArgError)
240# define rb_eIndexError (*dll_rb_eIndexError)
241# define rb_eRuntimeError (*dll_rb_eRuntimeError)
242# define rb_eStandardError (*dll_rb_eStandardError)
243# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaar41a41412020-01-07 21:32:19 +0100244# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200245# define rb_funcallv dll_rb_funcallv
246# else
247# define rb_funcall2 dll_rb_funcall2
248# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200249# define rb_global_variable dll_rb_global_variable
250# define rb_hash_aset dll_rb_hash_aset
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100251# define rb_hash_foreach dll_rb_hash_foreach
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200252# define rb_hash_new dll_rb_hash_new
253# define rb_inspect dll_rb_inspect
254# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200255
256// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
257// work. Not using the cache appears to be the best solution.
258# undef rb_intern
259# define rb_intern dll_rb_intern
260
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100261# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar41a41412020-01-07 21:32:19 +0100262# if RUBY_VERSION <= 18
Bram Moolenaar498af702014-03-28 21:58:21 +0100263# define rb_fix2int dll_rb_fix2int
264# define rb_num2int dll_rb_num2int
265# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200266# define rb_num2uint dll_rb_num2uint
267# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100268# define rb_num2dbl dll_rb_num2dbl
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200269# define rb_lastline_get dll_rb_lastline_get
270# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200271# define rb_protect dll_rb_protect
272# define rb_load dll_rb_load
Bram Moolenaar41a41412020-01-07 21:32:19 +0100273# if RUBY_VERSION <= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200274# define rb_num2long dll_rb_num2long
275# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100276# if RUBY_VERSION <= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200277# define rb_num2ulong dll_rb_num2ulong
278# endif
279# define rb_obj_alloc dll_rb_obj_alloc
280# define rb_obj_as_string dll_rb_obj_as_string
281# define rb_obj_id dll_rb_obj_id
282# define rb_raise dll_rb_raise
283# define rb_str_cat dll_rb_str_cat
284# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100285# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200286# define rb_str_new dll_rb_str_new
287# ifdef rb_str_new2
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100288// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200289# define need_rb_str_new_cstr 1
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100290// Ruby's headers #define rb_str_new_cstr to make use of GCC's
291// __builtin_constant_p extension.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200292# undef rb_str_new_cstr
293# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200294# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200295# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200296# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100297# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200298# define rb_string_value dll_rb_string_value
299# define rb_string_value_ptr dll_rb_string_value_ptr
300# define rb_float_new dll_rb_float_new
301# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200302# ifdef rb_ary_new4
303# define RB_ARY_NEW4_MACRO 1
304# undef rb_ary_new4
305# endif
306# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200307# define rb_ary_push dll_rb_ary_push
Bram Moolenaar41a41412020-01-07 21:32:19 +0100308# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200309# ifdef __ia64
310# define rb_ia64_bsp dll_rb_ia64_bsp
311# undef ruby_init_stack
312# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
313# else
314# define ruby_init_stack dll_ruby_init_stack
315# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200316# endif
317# else
318# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200319# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100320# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200321# define rb_errinfo dll_rb_errinfo
322# else
323# define ruby_errinfo (*dll_ruby_errinfo)
324# endif
325# define ruby_init dll_ruby_init
326# define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar4f974752019-02-17 17:44:42 +0100327# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100328# if RUBY_VERSION >= 19
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100329# define ruby_sysinit dll_ruby_sysinit
330# else
331# define NtInitialize dll_NtInitialize
332# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100333# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200334# define rb_w32_snprintf dll_rb_w32_snprintf
335# endif
336# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337
Bram Moolenaar41a41412020-01-07 21:32:19 +0100338# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200339# define ruby_script dll_ruby_script
340# define rb_enc_find_index dll_rb_enc_find_index
341# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100342# undef rb_enc_str_new
343# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200344# define rb_sprintf dll_rb_sprintf
345# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100346# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200347# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100348
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349/*
350 * Pointers for dynamic link
351 */
352static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200353VALUE *dll_rb_cFalseClass;
354VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200355# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200356VALUE *dll_rb_cInteger;
357# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100358# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100359VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200360# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200361VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100363VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200364VALUE *dll_rb_cSymbol;
365VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100366static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100368# ifdef USE_TYPEDDATA
369static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
370# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100372# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100373# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100374static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
375# else
376static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
377# endif
378# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
382static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
383static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
384static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
385static VALUE (*dll_rb_define_module) (const char*);
386static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
387static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
388static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
389static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200390static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391static VALUE *dll_rb_eArgError;
392static VALUE *dll_rb_eIndexError;
393static VALUE *dll_rb_eRuntimeError;
394static VALUE *dll_rb_eStandardError;
395static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100396# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200397static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
398# else
399static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401static void (*dll_rb_global_variable) (VALUE*);
402static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100403static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404static VALUE (*dll_rb_hash_new) (void);
405static VALUE (*dll_rb_inspect) (VALUE);
406static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200407static ID (*dll_rb_intern) (const char*);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100408# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200409static long (*dll_rb_fix2int) (VALUE);
410static long (*dll_rb_num2int) (VALUE);
411static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200412# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100413static double (*dll_rb_num2dbl) (VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414static VALUE (*dll_rb_lastline_get) (void);
415static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100416static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200417static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418static long (*dll_rb_num2long) (VALUE);
419static unsigned long (*dll_rb_num2ulong) (VALUE);
420static VALUE (*dll_rb_obj_alloc) (VALUE);
421static VALUE (*dll_rb_obj_as_string) (VALUE);
422static VALUE (*dll_rb_obj_id) (VALUE);
423static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100424# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200425static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200426# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
430static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
431static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200432# ifdef need_rb_str_new_cstr
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100433// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200434static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200435# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200437# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100438# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100439static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200440# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200442# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443static void (*dll_ruby_init) (void);
444static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100445# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100446# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100447static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100448# else
449static void (*dll_NtInitialize) (int*, char***);
450# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100451# if RUBY_VERSION >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200452static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200453# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200454# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100455# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100456static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
457static VALUE (*dll_rb_float_new) (double);
458static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200459static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100460static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100461# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100462static void (*dll_rb_ary_detransient) (VALUE);
463# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100464# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200465# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200466static void * (*dll_rb_ia64_bsp) (void);
467static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200468# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200469static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200470# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200471# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200472# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100473# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100474static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200475# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476
Bram Moolenaar41a41412020-01-07 21:32:19 +0100477# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100478static void (*dll_ruby_script) (const char*);
479static int (*dll_rb_enc_find_index) (const char*);
480static rb_encoding* (*dll_rb_enc_find) (const char*);
481static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
482static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100483static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100484static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200485# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100486
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100487# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100488# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100489static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100490# else
491static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
492# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100493# endif
494
Bram Moolenaar41a41412020-01-07 21:32:19 +0100495# if (RUBY_VERSION >= 19) && !defined(PROTO)
496# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100497 long
498rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200499# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100500 SIGNED_VALUE
501rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200502# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100503{
504 return dll_rb_num2long(x);
505}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100506# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100507 VALUE
508rb_int2big_stub(intptr_t x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100509# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100510 VALUE
511rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100512# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100513{
514 return dll_rb_int2big(x);
515}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100516# if (RUBY_VERSION >= 19) && (VIM_SIZEOF_INT < VIM_SIZEOF_LONG)
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100517 long
518rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200519{
520 return dll_rb_fix2int(x);
521}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100522 long
523rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200524{
525 return dll_rb_num2int(x);
526}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200527# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100528# if RUBY_VERSION >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100529 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100530rb_float_new_in_heap(double d)
531{
532 return dll_rb_float_new(d);
533}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100534# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100535 unsigned long
536rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200537# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100538 VALUE
539rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200540# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100541{
542 return (long)RSHIFT((SIGNED_VALUE)(x),1);
543}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200544# endif
545# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100546
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100547 // Do not generate a prototype here, VALUE isn't always defined.
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100548# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar41a41412020-01-07 21:32:19 +0100549# if RUBY_VERSION == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100550 void
551rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100552{
Bram Moolenaar90140742014-11-27 17:44:08 +0100553 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100554}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100555# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100556 void
557rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100558{
559 dll_rb_gc_writebarrier_unprotect(obj);
560}
561# endif
562# endif
563
Bram Moolenaar41a41412020-01-07 21:32:19 +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}
570# endif
571
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100572static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000575 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577static struct
578{
579 char *name;
580 RUBY_PROC *ptr;
581} ruby_funcname_table[] =
582{
583 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
584 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200585# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200586 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100587# else
588 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200589# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100590# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100591 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
594 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100595 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
597 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100598 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100600# ifdef USE_TYPEDDATA
601 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
602# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100604# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100605# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100606 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
607# else
608 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
609# endif
610# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
614 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
615 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
616 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
617 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
618 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
619 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
620 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
621 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200622 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
624 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
625 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
626 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
627 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100628# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200629 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
630# else
631 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
634 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100635 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
637 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
638 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200639 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100640# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200641 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
642 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
643 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200644# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100645 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
647 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200648 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
649 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
651 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
652 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
653 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
654 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
655 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100656# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200657 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200658# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200660# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
662 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
663 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200664# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200665 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200666# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200668# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100669# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100670 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200671# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200673# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
675 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100676# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100677# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100678 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100679# else
680 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200681# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100682# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200684# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200685# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100686# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100687 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100688# if RUBY_VERSION <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100689 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200690# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100691 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200692# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100693 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200694# ifdef RB_ARY_NEW4_MACRO
695 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
696# else
697 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
698# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100699 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100700# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100701 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
702# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200703# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100704# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100705 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100706 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
707 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
708 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
709 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
710 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100711 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100712 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200713# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100714# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200715# ifdef __ia64
716 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
717# endif
718 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
719# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100720# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100721# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100722 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100723# else
724 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
725# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100726# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {"", NULL},
728};
729
730/*
731 * Free ruby.dll
732 */
733 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100734end_dynamic_ruby(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735{
736 if (hinstRuby)
737 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200738 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200739 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 }
741}
742
743/*
744 * Load library and get all pointers.
745 * Parameter 'libname' provides name of DLL.
746 * Return OK or FAIL.
747 */
748 static int
749ruby_runtime_link_init(char *libname, int verbose)
750{
751 int i;
752
753 if (hinstRuby)
754 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200755 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 if (!hinstRuby)
757 {
758 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100759 semsg(_(e_loadlib), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 return FAIL;
761 }
762
763 for (i = 0; ruby_funcname_table[i].ptr; ++i)
764 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200765 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 ruby_funcname_table[i].name)))
767 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200768 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200769 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100771 semsg(_(e_loadfunc), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 return FAIL;
773 }
774 }
775 return OK;
776}
777
778/*
779 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
780 * else FALSE.
781 */
782 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100783ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100785 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100787#endif // defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
789 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100790ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
792#ifdef DYNAMIC_RUBY
793 end_dynamic_ruby();
794#endif
795}
796
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100797 void
798ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799{
800 int state;
801 char *script = NULL;
802
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000803 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 if (!eap->skip && ensure_ruby_initialized())
805 {
806 if (script == NULL)
807 rb_eval_string_protect((char *)eap->arg, &state);
808 else
809 rb_eval_string_protect(script, &state);
810 if (state)
811 error_print(state);
812 }
813 vim_free(script);
814}
815
Bram Moolenaar165641d2010-02-17 16:23:09 +0100816/*
817 * In Ruby 1.9 or later, ruby String object has encoding.
818 * conversion buffer string of vim to ruby String object using
819 * VIM encoding option.
820 */
821 static VALUE
822vim_str2rb_enc_str(const char *s)
823{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100824#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100825 int isnum;
826 long lval;
827 char_u *sval;
828 rb_encoding *enc;
829
830 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
831 if (isnum == 0)
832 {
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 }
838#endif
839 return rb_str_new2(s);
840}
841
842 static VALUE
843eval_enc_string_protect(const char *str, int *state)
844{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100845#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100846 int isnum;
847 long lval;
848 char_u *sval;
849 rb_encoding *enc;
850 VALUE v;
851
852 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
853 if (isnum == 0)
854 {
855 enc = rb_enc_find((char *)sval);
856 vim_free(sval);
857 if (enc)
858 {
859 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
860 return rb_eval_string_protect(StringValuePtr(v), state);
861 }
862 }
863#endif
864 return rb_eval_string_protect(str, state);
865}
866
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100867 void
868ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869{
870 int state;
871 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100872 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873
874 if (ensure_ruby_initialized())
875 {
876 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
877 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200878 for (i = eap->line1; i <= eap->line2; i++)
879 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100880 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100882 if (i > curbuf->b_ml.ml_line_count)
883 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100884 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100886 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200887 if (state)
888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 error_print(state);
890 break;
891 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100892 if (was_curbuf != curbuf)
893 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200895 if (!NIL_P(line))
896 {
897 if (TYPE(line) != T_STRING)
898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100899 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 return;
901 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100902 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 changed();
904#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100905 syn_changed(i); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906#endif
907 }
908 }
909 check_cursor();
910 update_curbuf(NOT_VALID);
911 }
912}
913
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100914 static VALUE
915rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100916{
917 rb_load(file_to_load, 0);
918 return Qnil;
919}
920
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100921 void
922ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923{
924 int state;
925
926 if (ensure_ruby_initialized())
927 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100928 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
929 rb_protect(rb_load_wrap, file_to_load, &state);
930 if (state)
931 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 }
933}
934
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100935 void
936ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000938 if (buf->b_ruby_ref)
939 {
940 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
941 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
943}
944
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100945 void
946ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000948 if (win->w_ruby_ref)
949 {
950 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
951 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 }
953}
954
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100955 static int
956ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957{
958 if (!ruby_initialized)
959 {
960#ifdef DYNAMIC_RUBY
961 if (ruby_enabled(TRUE))
962 {
963#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100964#ifdef MSWIN
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100965 // suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100966 int argc = 1;
967 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100968 char **argvp = argv;
Bram Moolenaar41a41412020-01-07 21:32:19 +0100969# if RUBY_VERSION >= 19
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100970 ruby_sysinit(&argc, &argvp);
971# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100972 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100973# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100974#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200975 {
Bram Moolenaar41a41412020-01-07 21:32:19 +0100976#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200977 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100978#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200979 ruby_init();
980 }
Bram Moolenaar41a41412020-01-07 21:32:19 +0100981#if RUBY_VERSION >= 19
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100982 {
983 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200984 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100985 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100986 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100987 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100988#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100990#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100991 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 ruby_vim_init();
993 ruby_initialized = 1;
994#ifdef DYNAMIC_RUBY
995 }
996 else
997 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100998 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 return 0;
1000 }
1001#endif
1002 }
1003 return ruby_initialized;
1004}
1005
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001006 static void
1007error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
Bram Moolenaar41a41412020-01-07 21:32:19 +01001009#if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 RUBYEXTERN VALUE ruby_errinfo;
1011#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001012 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 VALUE eclass;
1014 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001015 VALUE bt;
1016 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001018 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019
1020#define TAG_RETURN 0x1
1021#define TAG_BREAK 0x2
1022#define TAG_NEXT 0x3
1023#define TAG_RETRY 0x4
1024#define TAG_REDO 0x5
1025#define TAG_RAISE 0x6
1026#define TAG_THROW 0x7
1027#define TAG_FATAL 0x8
1028#define TAG_MASK 0xf
1029
Bram Moolenaar758535a2016-03-30 22:06:16 +02001030 switch (state)
1031 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001032 case TAG_RETURN:
1033 emsg(_("E267: unexpected return"));
1034 break;
1035 case TAG_NEXT:
1036 emsg(_("E268: unexpected next"));
1037 break;
1038 case TAG_BREAK:
1039 emsg(_("E269: unexpected break"));
1040 break;
1041 case TAG_REDO:
1042 emsg(_("E270: unexpected redo"));
1043 break;
1044 case TAG_RETRY:
1045 emsg(_("E271: retry outside of rescue clause"));
1046 break;
1047 case TAG_RAISE:
1048 case TAG_FATAL:
Bram Moolenaar41a41412020-01-07 21:32:19 +01001049#if RUBY_VERSION >= 19
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001050 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001051#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001052 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001053#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001054 eclass = CLASS_OF(error);
1055 einfo = rb_obj_as_string(error);
1056 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1057 {
1058 emsg(_("E272: unhandled exception"));
1059 }
1060 else
1061 {
1062 VALUE epath;
1063 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001065 epath = rb_class_path(eclass);
1066 vim_snprintf(buff, BUFSIZ, "%s: %s",
1067 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1068 p = strchr(buff, '\n');
1069 if (p) *p = '\0';
1070 emsg(buff);
1071 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001072
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001073 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar41a41412020-01-07 21:32:19 +01001074# if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001075 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1076 for (i = 0; i < RARRAY_LEN(bt); i++)
1077 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001078# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001079 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1080 for (i = 0; i < RARRAY_LEN(bt); i++)
1081 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001082# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001083 break;
1084 default:
1085 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1086 emsg(buff);
1087 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 }
1089}
1090
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001091 static VALUE
1092vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093{
1094 char *buff, *p;
1095
1096 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001097 if (RSTRING_LEN(str) > 0)
1098 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001099 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001100 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001101 strcpy(buff, RSTRING_PTR(str));
1102 p = strchr(buff, '\n');
1103 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001104 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001105 }
1106 else
1107 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001108 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 return Qnil;
1111}
1112
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001113 static VALUE
1114vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001116 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 update_screen(NOT_VALID);
1118 return Qnil;
1119}
1120
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001121 static VALUE
1122vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001124 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 return Qnil;
1126}
1127
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001128#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001129 static VALUE
1130vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001131{
1132 VALUE result = Qnil;
1133
1134 if (tv->v_type == VAR_STRING)
1135 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001136 result = rb_str_new2(tv->vval.v_string == NULL
1137 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001138 }
1139 else if (tv->v_type == VAR_NUMBER)
1140 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001141 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001142 }
1143# ifdef FEAT_FLOAT
1144 else if (tv->v_type == VAR_FLOAT)
1145 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001146 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001147 }
1148# endif
1149 else if (tv->v_type == VAR_LIST)
1150 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001151 list_T *list = tv->vval.v_list;
1152 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001153
Bram Moolenaar639a2552010-03-17 18:15:23 +01001154 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001155
Bram Moolenaar639a2552010-03-17 18:15:23 +01001156 if (list != NULL)
1157 {
1158 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001159 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001160 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001161 }
1162 else if (tv->v_type == VAR_DICT)
1163 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001164 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001165
Bram Moolenaar639a2552010-03-17 18:15:23 +01001166 if (tv->vval.v_dict != NULL)
1167 {
1168 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1169 long_u todo = ht->ht_used;
1170 hashitem_T *hi;
1171 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001172
Bram Moolenaar639a2552010-03-17 18:15:23 +01001173 for (hi = ht->ht_array; todo > 0; ++hi)
1174 {
1175 if (!HASHITEM_EMPTY(hi))
1176 {
1177 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001178
Bram Moolenaar639a2552010-03-17 18:15:23 +01001179 di = dict_lookup(hi);
1180 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001181 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001182 }
1183 }
1184 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001185 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001186 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001187 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001188 if (tv->vval.v_number == VVAL_TRUE)
1189 result = Qtrue;
1190 else if (tv->vval.v_number == VVAL_FALSE)
1191 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001192 }
1193 else if (tv->v_type == VAR_BLOB)
1194 {
1195 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1196 tv->vval.v_blob->bv_ga.ga_len);
1197 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001198 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001199
1200 return result;
1201}
1202#endif
1203
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001204 static VALUE
1205vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206{
1207#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001208 typval_T *tv;
1209 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001211 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1212 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001213 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001214 result = vim_to_ruby(tv);
1215
1216 free_tv(tv);
1217
1218 return result;
1219#else
1220 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222}
1223
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001224#ifdef USE_TYPEDDATA
1225static size_t buffer_dsize(const void *buf);
1226
1227static const rb_data_type_t buffer_type = {
1228 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001229 {0, 0, buffer_dsize,
1230# if RUBY_VERSION >= 27
1231 0, 0
1232# else
1233 {0, 0}
1234# endif
1235 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001236 0, 0,
1237# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1238 0,
1239# endif
1240};
1241
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001242 static size_t
1243buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001244{
1245 return sizeof(buf_T);
1246}
1247#endif
1248
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001249 static VALUE
1250buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001252 if (buf->b_ruby_ref)
1253 {
1254 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001256 else
1257 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001258#ifdef USE_TYPEDDATA
1259 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1260#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001262#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001263 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1265 return obj;
1266 }
1267}
1268
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001269 static buf_T *
1270get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271{
1272 buf_T *buf;
1273
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001274#ifdef USE_TYPEDDATA
1275 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1276#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (buf == NULL)
1280 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1281 return buf;
1282}
1283
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001284 static VALUE
1285vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001286{
1287 VALUE result = rb_str_new("0z", 2);
1288 char buf[4];
1289 int i;
1290 for (i = 0; i < RSTRING_LEN(str); i++)
1291 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001292 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001293 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294 }
1295 return result;
1296}
1297
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001298 static VALUE
1299buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 return buffer_new(curbuf);
1302}
1303
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001304 static VALUE
1305buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306{
1307 buf_T *b;
1308 int n = 0;
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 n++;
1316 }
1317
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 return INT2NUM(n);
1319}
1320
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001321 static VALUE
1322buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
1324 buf_T *b;
1325 int n = NUM2INT(num);
1326
Bram Moolenaar29323592016-07-24 22:04:11 +02001327 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001328 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001329 // Deleted buffers should not be counted
1330 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001331 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001332 continue;
1333
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001334 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001336
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001337 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 }
1339 return Qnil;
1340}
1341
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001342 static VALUE
1343buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
1345 buf_T *buf = get_buf(self);
1346
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001347 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348}
1349
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001350 static VALUE
1351buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
1353 buf_T *buf = get_buf(self);
1354
1355 return INT2NUM(buf->b_fnum);
1356}
1357
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001358 static VALUE
1359buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
1361 buf_T *buf = get_buf(self);
1362
1363 return INT2NUM(buf->b_ml.ml_line_count);
1364}
1365
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001366 static VALUE
1367get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001369 if (n <= 0 || n > buf->b_ml.ml_line_count)
1370 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1371 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372}
1373
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001374 static VALUE
1375buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
1377 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001378
1379 if (buf != NULL)
1380 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001381 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001382}
1383
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001384 static VALUE
1385set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001386{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001387 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001388 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001390 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1391 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001392 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001393 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001394
Bram Moolenaar758535a2016-03-30 22:06:16 +02001395 if (u_savesub(n) == OK)
1396 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001397 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 changed();
1399#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001400 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401#endif
1402 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001403
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001404 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001405 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001406 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001407
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 update_curbuf(NOT_VALID);
1409 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001410 else
1411 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001412 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 }
1414 return str;
1415}
1416
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001417 static VALUE
1418buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001419{
1420 buf_T *buf = get_buf(self);
1421
1422 if (buf != NULL)
1423 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1424 return str;
1425}
1426
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001427 static VALUE
1428buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001430 buf_T *buf = get_buf(self);
1431 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001432 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001434 if (n > 0 && n <= buf->b_ml.ml_line_count)
1435 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001436 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001437 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001438
Bram Moolenaar758535a2016-03-30 22:06:16 +02001439 if (u_savedel(n, 1) == OK)
1440 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001442
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001443 // Changes to non-active buffers should properly refresh
1444 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001445 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001446
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 changed();
1448 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001449
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001450 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001451 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001452 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 update_curbuf(NOT_VALID);
1455 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001456 else
1457 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001458 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 }
1460 return Qnil;
1461}
1462
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001463 static VALUE
1464buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001466 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001467 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001468 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001469 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470
Bram Moolenaar3c531602010-11-16 14:46:19 +01001471 if (line == NULL)
1472 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001473 rb_raise(rb_eIndexError, "NULL line");
1474 }
1475 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001476 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001477 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001478 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001479
Bram Moolenaar758535a2016-03-30 22:06:16 +02001480 if (u_inssub(n + 1) == OK)
1481 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001483
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001484 // Changes to non-active buffers should properly refresh screen
1485 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001486 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001487
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001488 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001490
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001491 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001492 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001493 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001494
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 update_curbuf(NOT_VALID);
1496 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001497 else
1498 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001499 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 }
1501 return str;
1502}
1503
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001504#ifdef USE_TYPEDDATA
1505static size_t window_dsize(const void *buf);
1506
1507static const rb_data_type_t window_type = {
1508 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001509 {0, 0, window_dsize,
1510# if RUBY_VERSION >= 27
1511 0, 0
1512# else
1513 {0, 0}
1514# endif
1515 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001516 0, 0,
1517# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1518 0,
1519# endif
1520};
1521
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001522 static size_t
1523window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001524{
1525 return sizeof(win_T);
1526}
1527#endif
1528
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001529 static VALUE
1530window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001532 if (win->w_ruby_ref)
1533 {
1534 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001536 else
1537 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001538#ifdef USE_TYPEDDATA
1539 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1540#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001542#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001543 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1545 return obj;
1546 }
1547}
1548
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001549 static win_T *
1550get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 win_T *win;
1553
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001554#ifdef USE_TYPEDDATA
1555 TypedData_Get_Struct(obj, win_T, &window_type, win);
1556#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 if (win == NULL)
1560 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1561 return win;
1562}
1563
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001564 static VALUE
1565window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566{
1567 return window_new(curwin);
1568}
1569
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001570/*
1571 * Added line manipulation functions
1572 * SegPhault - 03/07/05
1573 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001574 static VALUE
1575line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001576{
1577 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1578}
1579
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001580 static VALUE
1581set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001582{
1583 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1584}
1585
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001586 static VALUE
1587current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001588{
1589 return INT2FIX((int)curwin->w_cursor.lnum);
1590}
1591
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001592 static VALUE
1593window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 win_T *w;
1596 int n = 0;
1597
Bram Moolenaar29323592016-07-24 22:04:11 +02001598 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 n++;
1600 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601}
1602
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001603 static VALUE
1604window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605{
1606 win_T *w;
1607 int n = NUM2INT(num);
1608
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (n == 0)
1611 return window_new(w);
1612 return Qnil;
1613}
1614
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001615 static VALUE
1616window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617{
1618 win_T *win = get_win(self);
1619
1620 return buffer_new(win->w_buffer);
1621}
1622
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001623 static VALUE
1624window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
1626 win_T *win = get_win(self);
1627
1628 return INT2NUM(win->w_height);
1629}
1630
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001631 static VALUE
1632window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
1634 win_T *win = get_win(self);
1635 win_T *savewin = curwin;
1636
1637 curwin = win;
1638 win_setheight(NUM2INT(height));
1639 curwin = savewin;
1640 return height;
1641}
1642
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001643 static VALUE
1644window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001645{
Bram Moolenaare745d752017-09-22 16:56:22 +02001646 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001647}
1648
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001649 static VALUE
1650window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001651{
1652 win_T *win = get_win(self);
1653 win_T *savewin = curwin;
1654
1655 curwin = win;
1656 win_setwidth(NUM2INT(width));
1657 curwin = savewin;
1658 return width;
1659}
1660
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001661 static VALUE
1662window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
1664 win_T *win = get_win(self);
1665
1666 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1667}
1668
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001669 static VALUE
1670window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671{
1672 VALUE lnum, col;
1673 win_T *win = get_win(self);
1674
1675 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001676 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001678 lnum = RARRAY_PTR(pos)[0];
1679 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 win->w_cursor.lnum = NUM2LONG(lnum);
1681 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001682 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001683 check_cursor(); // put cursor on an existing line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 update_screen(NOT_VALID);
1685 return Qnil;
1686}
1687
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001688 static VALUE
1689f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001690{
1691 return Qnil;
1692}
1693
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001694 static VALUE
1695f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
1697 int i;
1698 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001699 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
Bram Moolenaar758535a2016-03-30 22:06:16 +02001701 for (i = 0; i < argc; i++)
1702 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 if (i > 0) rb_str_cat(str, ", ", 2);
1704 rb_str_concat(str, rb_inspect(argv[i]));
1705 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001706 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001707
1708 if (argc == 1)
1709 ret = argv[0];
1710 else if (argc > 1)
1711 ret = rb_ary_new4(argc, argv);
1712 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713}
1714
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001715 static void
1716ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
1718#ifndef DYNAMIC_RUBY
1719 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001720 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721#endif
1722
1723 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001724 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001726 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001727 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1728 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 rb_define_global_function("p", f_p, -1);
1730}
1731
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001732 static void
1733ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
1735 objtbl = rb_hash_new();
1736 rb_global_variable(&objtbl);
1737
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001738 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1739 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001740 mVIM = rb_define_module("Vim");
1741 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1743 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1744 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1745 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1746 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1747 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1748 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1749 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1750 rb_define_module_function(mVIM, "message", vim_message, 1);
1751 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1752 rb_define_module_function(mVIM, "command", vim_command, 1);
1753 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001754 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
1756 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1757 rb_eStandardError);
1758 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1759 rb_eStandardError);
1760
1761 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1762 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1763 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1764 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1765 rb_define_method(cBuffer, "name", buffer_name, 0);
1766 rb_define_method(cBuffer, "number", buffer_number, 0);
1767 rb_define_method(cBuffer, "count", buffer_count, 0);
1768 rb_define_method(cBuffer, "length", buffer_count, 0);
1769 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1770 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1771 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1772 rb_define_method(cBuffer, "append", buffer_append, 2);
1773
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001774 // Added line manipulation functions
1775 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001776 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1777 rb_define_method(cBuffer, "line", line_s_current, 0);
1778 rb_define_method(cBuffer, "line=", set_current_line, 1);
1779
1780
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1782 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1783 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1784 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1785 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1786 rb_define_method(cVimWindow, "height", window_height, 0);
1787 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001788 rb_define_method(cVimWindow, "width", window_width, 0);
1789 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1791 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1792
1793 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1794 rb_define_virtual_variable("$curwin", window_s_current, 0);
1795}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001796
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001797 void
1798vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001799{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001800 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001801 ruby_stack_start = stack_start;
1802}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001803
1804 static int
1805convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1806{
1807 dict_T *d = (dict_T *)arg;
1808 dictitem_T *di;
1809
1810 di = dictitem_alloc((char_u *)RSTRING_PTR(RSTRING(rb_obj_as_string(key))));
1811 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1812 || dict_add(d, di) != OK)
1813 {
1814 d->dv_hashtab.ht_error = TRUE;
1815 return ST_STOP;
1816 }
1817 return ST_CONTINUE;
1818}
1819
1820 static int
1821ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1822{
1823 switch (TYPE(val))
1824 {
1825 case T_NIL:
1826 rettv->v_type = VAR_SPECIAL;
1827 rettv->vval.v_number = VVAL_NULL;
1828 break;
1829 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001830 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001831 rettv->vval.v_number = VVAL_TRUE;
1832 break;
1833 case T_FALSE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001834 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001835 rettv->vval.v_number = VVAL_FALSE;
1836 break;
1837 case T_BIGNUM:
1838 case T_FIXNUM:
1839 rettv->v_type = VAR_NUMBER;
1840 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1841 break;
1842#ifdef FEAT_FLOAT
1843 case T_FLOAT:
1844 rettv->v_type = VAR_FLOAT;
1845 rettv->vval.v_float = (float_T)NUM2DBL(val);
1846 break;
1847#endif
1848 default:
1849 val = rb_obj_as_string(val);
1850 // FALLTHROUGH
1851 case T_STRING:
1852 {
1853 VALUE str = (VALUE)RSTRING(val);
1854
1855 rettv->v_type = VAR_STRING;
1856 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
1857 (int)RSTRING_LEN(str));
1858 }
1859 break;
1860 case T_ARRAY:
1861 {
1862 list_T *l;
1863 long i;
1864 typval_T v;
1865
1866 l = list_alloc();
1867 if (l == NULL)
1868 return FAIL;
1869
1870 for (i = 0; i < RARRAY_LEN(val); ++i)
1871 {
1872 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1873 &v) != OK)
1874 {
1875 list_unref(l);
1876 return FAIL;
1877 }
1878 list_append_tv(l, &v);
1879 clear_tv(&v);
1880 }
1881
1882 rettv->v_type = VAR_LIST;
1883 rettv->vval.v_list = l;
1884 ++l->lv_refcount;
1885 }
1886 break;
1887 case T_HASH:
1888 {
1889 dict_T *d;
1890
1891 d = dict_alloc();
1892 if (d == NULL)
1893 return FAIL;
1894
1895 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1896 if (d->dv_hashtab.ht_error)
1897 {
1898 dict_unref(d);
1899 return FAIL;
1900 }
1901
1902 rettv->v_type = VAR_DICT;
1903 rettv->vval.v_dict = d;
1904 ++d->dv_refcount;
1905 }
1906 break;
1907 }
1908 return OK;
1909}
1910
1911 void
1912do_rubyeval(char_u *str, typval_T *rettv)
1913{
1914 int retval = FAIL;
1915
1916 if (ensure_ruby_initialized())
1917 {
1918 int state;
1919 VALUE obj;
1920
1921 obj = rb_eval_string_protect((const char *)str, &state);
1922 if (state)
1923 error_print(state);
1924 else
1925 retval = ruby_convert_to_vim_value(obj, rettv);
1926 }
1927 if (retval == FAIL)
1928 {
1929 rettv->v_type = VAR_NUMBER;
1930 rettv->vval.v_number = 0;
1931 }
1932}