blob: 6779c234389a12beaad4f459e563c803cd135340 [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
23# if !defined(DYNAMIC_RUBY_VER) || (DYNAMIC_RUBY_VER < 18)
24# define NT
25# endif
26# ifndef DYNAMIC_RUBY
27# define IMPORT /* For static dll usage __declspec(dllimport) */
28# define RUBYEXTERN __declspec(dllimport)
29# endif
30#endif
31#ifndef RUBYEXTERN
32# define RUBYEXTERN extern
33#endif
34
Bram Moolenaar2016ae52016-06-13 20:08:43 +020035#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 24
36# 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 Moolenaardb3fbe52013-03-07 15:16:21 +010050# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
51# 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)
57# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
58/*
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 Moolenaar639a2552010-03-17 18:15:23 +010074#if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
75 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
76# define RUBY19_OR_LATER 1
77#endif
78
Bram Moolenaar0d27f642015-12-28 22:05:28 +010079#if (defined(RUBY_VERSION) && RUBY_VERSION >= 20) \
80 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20)
81# define RUBY20_OR_LATER 1
82#endif
83
Bram Moolenaarf711cb22018-08-01 18:42:13 +020084#if (defined(RUBY_VERSION) && RUBY_VERSION >= 21) \
85 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 21)
86# define RUBY21_OR_LATER 1
87#endif
88
Bram Moolenaar42d57f02010-03-10 12:47:00 +010089#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
90/* Ruby 1.9 defines a number of static functions which use rb_num2long and
91 * rb_int2big */
92# define rb_num2long rb_num2long_stub
93# define rb_int2big rb_int2big_stub
94#endif
95
Bram Moolenaar498af702014-03-28 21:58:21 +010096#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
97 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
98/* Ruby 1.9 defines a number of static functions which use rb_fix2int and
Bram Moolenaara2aa31a2014-02-23 22:52:40 +010099 * rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit) */
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200100# define rb_fix2int rb_fix2int_stub
101# define rb_num2int rb_num2int_stub
102#endif
103
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100104#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100105/* Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
106 * rb_gc_writebarrier_unprotect_promoted if USE_RGENGC */
Bram Moolenaar90140742014-11-27 17:44:08 +0100107# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
108#endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100109#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
110# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100111#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100112
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100113#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100114# define rb_ary_detransient rb_ary_detransient_stub
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100115#endif
116
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100118#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100119# include <ruby/encoding.h>
120#endif
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200121#ifndef RUBY19_OR_LATER
122# include <st.h> // for ST_STOP and ST_CONTINUE
123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200125#undef off_t // ruby defines off_t as _int64, Mingw uses long
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#undef EXTERN
127#undef _
128
129/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaard0573012017-10-28 21:11:06 +0200130#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# define __OPENTRANSPORT__
132# define __OPENTRANSPORTPROTOCOL__
133# define __OPENTRANSPORTPROVIDERS__
134#endif
135
Bram Moolenaar165641d2010-02-17 16:23:09 +0100136/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100137 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
138 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
139 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100140 * Use TypedData_XXX if available.
141 */
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100142#if defined(TypedData_Wrap_Struct) && defined(RUBY20_OR_LATER)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100143# define USE_TYPEDDATA 1
144#endif
145
146/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200147 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100148 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
149 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
150 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
151 */
152#ifndef StringValuePtr
153# define StringValuePtr(s) STR2CSTR(s)
154#endif
155#ifndef RARRAY_LEN
156# define RARRAY_LEN(s) RARRAY(s)->len
157#endif
158#ifndef RARRAY_PTR
159# define RARRAY_PTR(s) RARRAY(s)->ptr
160#endif
161#ifndef RSTRING_LEN
162# define RSTRING_LEN(s) RSTRING(s)->len
163#endif
164#ifndef RSTRING_PTR
165# define RSTRING_PTR(s) RSTRING(s)->ptr
166#endif
167
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100168#ifdef HAVE_DUP
169# undef HAVE_DUP
170#endif
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#include "vim.h"
173#include "version.h"
174
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100175#ifdef DYNAMIC_RUBY
176# if !defined(MSWIN) // must come after including vim.h, where it is defined
177# include <dlfcn.h>
178# define HINSTANCE void*
179# define RUBY_PROC void*
180# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
181# define symbol_from_dll dlsym
182# define close_dll dlclose
183# else
184# define RUBY_PROC FARPROC
185# define load_dll vimLoadLib
186# define symbol_from_dll GetProcAddress
187# define close_dll FreeLibrary
188# endif
189#endif
190
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#if defined(PROTO) && !defined(FEAT_RUBY)
192/* Define these to be able to generate the function prototypes. */
193# define VALUE int
194# define RUBY_DATA_FUNC int
195#endif
196
197static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200198static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199static VALUE objtbl;
200
201static VALUE mVIM;
202static VALUE cBuffer;
203static VALUE cVimWindow;
204static VALUE eDeletedBufferError;
205static VALUE eDeletedWindowError;
206
207static int ensure_ruby_initialized(void);
208static void error_print(int);
209static void ruby_io_init(void);
210static void ruby_vim_init(void);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100211static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200213#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
214# if defined(__ia64) && !defined(ruby_init_stack)
215# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
216# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217#endif
218
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200219#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100220# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200221# define HINSTANCE int /* for generating prototypes */
222# endif
223
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224/*
225 * Wrapper defines
226 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200227# define rb_assoc_new dll_rb_assoc_new
228# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100229# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100230# define rb_check_type dll_rb_check_type
231# ifdef USE_TYPEDDATA
232# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100233# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200234# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100235# ifdef USE_TYPEDDATA
236# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
237# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
238# else
239# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
240# endif
241# else
242# define rb_data_object_alloc dll_rb_data_object_alloc
243# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200244# define rb_define_class_under dll_rb_define_class_under
245# define rb_define_const dll_rb_define_const
246# define rb_define_global_function dll_rb_define_global_function
247# define rb_define_method dll_rb_define_method
248# define rb_define_module dll_rb_define_module
249# define rb_define_module_function dll_rb_define_module_function
250# define rb_define_singleton_method dll_rb_define_singleton_method
251# define rb_define_virtual_variable dll_rb_define_virtual_variable
252# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200253# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200254# define rb_eArgError (*dll_rb_eArgError)
255# define rb_eIndexError (*dll_rb_eIndexError)
256# define rb_eRuntimeError (*dll_rb_eRuntimeError)
257# define rb_eStandardError (*dll_rb_eStandardError)
258# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200259# ifdef RUBY21_OR_LATER
260# define rb_funcallv dll_rb_funcallv
261# else
262# define rb_funcall2 dll_rb_funcall2
263# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200264# define rb_global_variable dll_rb_global_variable
265# define rb_hash_aset dll_rb_hash_aset
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100266# define rb_hash_foreach dll_rb_hash_foreach
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200267# define rb_hash_new dll_rb_hash_new
268# define rb_inspect dll_rb_inspect
269# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200270
271// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
272// work. Not using the cache appears to be the best solution.
273# undef rb_intern
274# define rb_intern dll_rb_intern
275
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100276# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar498af702014-03-28 21:58:21 +0100277# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 18
278# define rb_fix2int dll_rb_fix2int
279# define rb_num2int dll_rb_num2int
280# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200281# define rb_num2uint dll_rb_num2uint
282# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100283# define rb_num2dbl dll_rb_num2dbl
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200284# define rb_lastline_get dll_rb_lastline_get
285# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200286# define rb_protect dll_rb_protect
287# define rb_load dll_rb_load
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200288# ifndef RUBY19_OR_LATER
289# define rb_num2long dll_rb_num2long
290# endif
291# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
292# define rb_num2ulong dll_rb_num2ulong
293# endif
294# define rb_obj_alloc dll_rb_obj_alloc
295# define rb_obj_as_string dll_rb_obj_as_string
296# define rb_obj_id dll_rb_obj_id
297# define rb_raise dll_rb_raise
298# define rb_str_cat dll_rb_str_cat
299# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100300# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200301# define rb_str_new dll_rb_str_new
302# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200303/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200304# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200305/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
306 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200307# undef rb_str_new_cstr
308# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200309# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200310# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200311# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200312# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200313# define rb_string_value dll_rb_string_value
314# define rb_string_value_ptr dll_rb_string_value_ptr
315# define rb_float_new dll_rb_float_new
316# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200317# ifdef rb_ary_new4
318# define RB_ARY_NEW4_MACRO 1
319# undef rb_ary_new4
320# endif
321# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200322# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200323# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
324# ifdef __ia64
325# define rb_ia64_bsp dll_rb_ia64_bsp
326# undef ruby_init_stack
327# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
328# else
329# define ruby_init_stack dll_ruby_init_stack
330# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200331# endif
332# else
333# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200334# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200335# ifdef RUBY19_OR_LATER
336# define rb_errinfo dll_rb_errinfo
337# else
338# define ruby_errinfo (*dll_ruby_errinfo)
339# endif
340# define ruby_init dll_ruby_init
341# define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar4f974752019-02-17 17:44:42 +0100342# ifdef MSWIN
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100343# ifdef RUBY19_OR_LATER
344# define ruby_sysinit dll_ruby_sysinit
345# else
346# define NtInitialize dll_NtInitialize
347# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200348# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
349# define rb_w32_snprintf dll_rb_w32_snprintf
350# endif
351# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200353# ifdef RUBY19_OR_LATER
354# define ruby_script dll_ruby_script
355# define rb_enc_find_index dll_rb_enc_find_index
356# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100357# undef rb_enc_str_new
358# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200359# define rb_sprintf dll_rb_sprintf
360# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100361# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200362# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100363
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364/*
365 * Pointers for dynamic link
366 */
367static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200368VALUE *dll_rb_cFalseClass;
369VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200370# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200371VALUE *dll_rb_cInteger;
372# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200373# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100374VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200375# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200376VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100378VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200379VALUE *dll_rb_cSymbol;
380VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100381static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100383# ifdef USE_TYPEDDATA
384static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100387# ifdef USE_TYPEDDATA
388# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
389static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
390# else
391static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
392# endif
393# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
397static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
398static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
399static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
400static VALUE (*dll_rb_define_module) (const char*);
401static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
402static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
403static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
404static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200405static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406static VALUE *dll_rb_eArgError;
407static VALUE *dll_rb_eIndexError;
408static VALUE *dll_rb_eRuntimeError;
409static VALUE *dll_rb_eStandardError;
410static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200411# ifdef RUBY21_OR_LATER
412static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
413# else
414static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416static void (*dll_rb_global_variable) (VALUE*);
417static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100418static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419static VALUE (*dll_rb_hash_new) (void);
420static VALUE (*dll_rb_inspect) (VALUE);
421static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200422static ID (*dll_rb_intern) (const char*);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100423# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200424static long (*dll_rb_fix2int) (VALUE);
425static long (*dll_rb_num2int) (VALUE);
426static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200427# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100428static double (*dll_rb_num2dbl) (VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429static VALUE (*dll_rb_lastline_get) (void);
430static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100431static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200432static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433static long (*dll_rb_num2long) (VALUE);
434static unsigned long (*dll_rb_num2ulong) (VALUE);
435static VALUE (*dll_rb_obj_alloc) (VALUE);
436static VALUE (*dll_rb_obj_as_string) (VALUE);
437static VALUE (*dll_rb_obj_id) (VALUE);
438static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200439# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200440static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200441# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
445static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
446static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200447# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200448/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
449static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200450# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200452# endif
453# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100454static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200455# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200457# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458static void (*dll_ruby_init) (void);
459static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100460# ifdef MSWIN
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100461# ifdef RUBY19_OR_LATER
Bram Moolenaar4f391792017-01-15 16:59:07 +0100462static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100463# else
464static void (*dll_NtInitialize) (int*, char***);
465# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200466# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200467static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200468# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200469# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200470# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100471static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
472static VALUE (*dll_rb_float_new) (double);
473static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200474static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100475static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100476# if DYNAMIC_RUBY_VER >= 26
477static void (*dll_rb_ary_detransient) (VALUE);
478# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200479# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
480# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200481static void * (*dll_rb_ia64_bsp) (void);
482static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200483# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200484static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200485# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200486# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200487# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200488# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100489static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200490# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200492# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100493static void (*dll_ruby_script) (const char*);
494static int (*dll_rb_enc_find_index) (const char*);
495static rb_encoding* (*dll_rb_enc_find) (const char*);
496static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
497static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100498static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100499static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200500# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100501
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100502# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100503# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100504static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100505# else
506static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
507# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100508# endif
509
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200510# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200511# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100512 long
513rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200514# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100515 SIGNED_VALUE
516rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200517# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100518{
519 return dll_rb_num2long(x);
520}
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100521# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100522 VALUE
523rb_int2big_stub(intptr_t x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100524# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100525 VALUE
526rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100527# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100528{
529 return dll_rb_int2big(x);
530}
Bram Moolenaar498af702014-03-28 21:58:21 +0100531# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
532 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100533 long
534rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200535{
536 return dll_rb_fix2int(x);
537}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100538 long
539rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200540{
541 return dll_rb_num2int(x);
542}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200543# endif
544# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100545 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100546rb_float_new_in_heap(double d)
547{
548 return dll_rb_float_new(d);
549}
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200550# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100551 unsigned long
552rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200553# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100554 VALUE
555rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200556# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100557{
558 return (long)RSHIFT((SIGNED_VALUE)(x),1);
559}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200560# endif
561# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100562
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100563 /* Do not generate a prototype here, VALUE isn't always defined. */
564# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100565# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100566 void
567rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100568{
Bram Moolenaar90140742014-11-27 17:44:08 +0100569 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100570}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100571# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100572 void
573rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100574{
575 dll_rb_gc_writebarrier_unprotect(obj);
576}
577# endif
578# endif
579
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100580# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100581 void
582rb_ary_detransient_stub(VALUE x)
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100583{
584 dll_rb_ary_detransient(x);
585}
586# endif
587
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200588static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
590/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000591 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593static struct
594{
595 char *name;
596 RUBY_PROC *ptr;
597} ruby_funcname_table[] =
598{
599 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
600 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200601# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200602 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100603# else
604 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200605# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200606# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100607 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
610 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100611 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
613 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100614 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100616# ifdef USE_TYPEDDATA
617 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100620# ifdef USE_TYPEDDATA
621# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
622 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
623# else
624 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
625# endif
626# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
630 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
631 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
632 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
633 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
634 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
635 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
636 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
637 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200638 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
640 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
641 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
642 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
643 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200644# ifdef RUBY21_OR_LATER
645 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
646# else
647 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
648# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
650 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100651 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
653 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
654 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200655 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100656# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200657 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
658 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
659 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200660# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100661 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
663 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200664 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
665 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
667 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
668 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
669 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
670 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
671 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200672# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200673 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200674# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200676# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
678 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
679 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200680# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200681 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200682# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200684# endif
685# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100686 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200687# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200689# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
691 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100692# ifdef MSWIN
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100693# ifdef RUBY19_OR_LATER
Bram Moolenaar4f391792017-01-15 16:59:07 +0100694 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100695# else
696 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200697# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200698# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200700# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200701# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200702# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100703 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200704# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100705 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200706# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100707 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200708# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100709 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200710# ifdef RB_ARY_NEW4_MACRO
711 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
712# else
713 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
714# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100715 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100716# if DYNAMIC_RUBY_VER >= 26
717 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
718# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200719# endif
720# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100721 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100722 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
723 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
724 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
725 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
726 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100727 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100728 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200729# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200730# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
731# ifdef __ia64
732 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
733# endif
734 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
735# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100736# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100737# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 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 Moolenaar071d4272004-06-13 20:20:40 +0000743 {"", NULL},
744};
745
746/*
747 * Free ruby.dll
748 */
749 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100750end_dynamic_ruby(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
752 if (hinstRuby)
753 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200754 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200755 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 }
757}
758
759/*
760 * Load library and get all pointers.
761 * Parameter 'libname' provides name of DLL.
762 * Return OK or FAIL.
763 */
764 static int
765ruby_runtime_link_init(char *libname, int verbose)
766{
767 int i;
768
769 if (hinstRuby)
770 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200771 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (!hinstRuby)
773 {
774 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100775 semsg(_(e_loadlib), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 return FAIL;
777 }
778
779 for (i = 0; ruby_funcname_table[i].ptr; ++i)
780 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200781 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 ruby_funcname_table[i].name)))
783 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200784 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200785 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100787 semsg(_(e_loadfunc), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 return FAIL;
789 }
790 }
791 return OK;
792}
793
794/*
795 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
796 * else FALSE.
797 */
798 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100799ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100801 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802}
803#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
804
805 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100806ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807{
808#ifdef DYNAMIC_RUBY
809 end_dynamic_ruby();
810#endif
811}
812
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100813 void
814ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815{
816 int state;
817 char *script = NULL;
818
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000819 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 if (!eap->skip && ensure_ruby_initialized())
821 {
822 if (script == NULL)
823 rb_eval_string_protect((char *)eap->arg, &state);
824 else
825 rb_eval_string_protect(script, &state);
826 if (state)
827 error_print(state);
828 }
829 vim_free(script);
830}
831
Bram Moolenaar165641d2010-02-17 16:23:09 +0100832/*
833 * In Ruby 1.9 or later, ruby String object has encoding.
834 * conversion buffer string of vim to ruby String object using
835 * VIM encoding option.
836 */
837 static VALUE
838vim_str2rb_enc_str(const char *s)
839{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100840#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100841 int isnum;
842 long lval;
843 char_u *sval;
844 rb_encoding *enc;
845
846 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
847 if (isnum == 0)
848 {
849 enc = rb_enc_find((char *)sval);
850 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200851 if (enc)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200852 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100853 }
854#endif
855 return rb_str_new2(s);
856}
857
858 static VALUE
859eval_enc_string_protect(const char *str, int *state)
860{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100861#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100862 int isnum;
863 long lval;
864 char_u *sval;
865 rb_encoding *enc;
866 VALUE v;
867
868 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
869 if (isnum == 0)
870 {
871 enc = rb_enc_find((char *)sval);
872 vim_free(sval);
873 if (enc)
874 {
875 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
876 return rb_eval_string_protect(StringValuePtr(v), state);
877 }
878 }
879#endif
880 return rb_eval_string_protect(str, state);
881}
882
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100883 void
884ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885{
886 int state;
887 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100888 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
890 if (ensure_ruby_initialized())
891 {
892 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
893 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200894 for (i = eap->line1; i <= eap->line2; i++)
895 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100896 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100898 if (i > curbuf->b_ml.ml_line_count)
899 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100900 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100902 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200903 if (state)
904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 error_print(state);
906 break;
907 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100908 if (was_curbuf != curbuf)
909 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200911 if (!NIL_P(line))
912 {
913 if (TYPE(line) != T_STRING)
914 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100915 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 return;
917 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100918 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 changed();
920#ifdef SYNTAX_HL
921 syn_changed(i); /* recompute syntax hl. for this line */
922#endif
923 }
924 }
925 check_cursor();
926 update_curbuf(NOT_VALID);
927 }
928}
929
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100930 static VALUE
931rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100932{
933 rb_load(file_to_load, 0);
934 return Qnil;
935}
936
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100937 void
938ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939{
940 int state;
941
942 if (ensure_ruby_initialized())
943 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100944 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
945 rb_protect(rb_load_wrap, file_to_load, &state);
946 if (state)
947 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 }
949}
950
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100951 void
952ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000954 if (buf->b_ruby_ref)
955 {
956 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
957 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959}
960
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100961 void
962ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000964 if (win->w_ruby_ref)
965 {
966 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
967 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 }
969}
970
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100971 static int
972ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973{
974 if (!ruby_initialized)
975 {
976#ifdef DYNAMIC_RUBY
977 if (ruby_enabled(TRUE))
978 {
979#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100980#ifdef MSWIN
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100981 /* suggested by Ariya Mizutani */
982 int argc = 1;
983 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100984 char **argvp = argv;
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100985# ifdef RUBY19_OR_LATER
986 ruby_sysinit(&argc, &argvp);
987# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100988 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100989# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100990#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200991 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200992#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200993 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100994#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200995 ruby_init();
996 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100997#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100998 {
999 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +02001000 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +01001001 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001002 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001003 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001004#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001006#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001007 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 ruby_vim_init();
1009 ruby_initialized = 1;
1010#ifdef DYNAMIC_RUBY
1011 }
1012 else
1013 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001014 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 return 0;
1016 }
1017#endif
1018 }
1019 return ruby_initialized;
1020}
1021
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001022 static void
1023error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024{
Bram Moolenaarb09c6842018-12-27 22:11:01 +01001025#if !defined(DYNAMIC_RUBY) && !defined(RUBY19_OR_LATER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 RUBYEXTERN VALUE ruby_errinfo;
1027#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001028 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 VALUE eclass;
1030 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001031 VALUE bt;
1032 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001034 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
1036#define TAG_RETURN 0x1
1037#define TAG_BREAK 0x2
1038#define TAG_NEXT 0x3
1039#define TAG_RETRY 0x4
1040#define TAG_REDO 0x5
1041#define TAG_RAISE 0x6
1042#define TAG_THROW 0x7
1043#define TAG_FATAL 0x8
1044#define TAG_MASK 0xf
1045
Bram Moolenaar758535a2016-03-30 22:06:16 +02001046 switch (state)
1047 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001048 case TAG_RETURN:
1049 emsg(_("E267: unexpected return"));
1050 break;
1051 case TAG_NEXT:
1052 emsg(_("E268: unexpected next"));
1053 break;
1054 case TAG_BREAK:
1055 emsg(_("E269: unexpected break"));
1056 break;
1057 case TAG_REDO:
1058 emsg(_("E270: unexpected redo"));
1059 break;
1060 case TAG_RETRY:
1061 emsg(_("E271: retry outside of rescue clause"));
1062 break;
1063 case TAG_RAISE:
1064 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +01001065#ifdef RUBY19_OR_LATER
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001066 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001067#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001068 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001069#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001070 eclass = CLASS_OF(error);
1071 einfo = rb_obj_as_string(error);
1072 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1073 {
1074 emsg(_("E272: unhandled exception"));
1075 }
1076 else
1077 {
1078 VALUE epath;
1079 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001081 epath = rb_class_path(eclass);
1082 vim_snprintf(buff, BUFSIZ, "%s: %s",
1083 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1084 p = strchr(buff, '\n');
1085 if (p) *p = '\0';
1086 emsg(buff);
1087 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001088
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001089 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001090# ifdef RUBY21_OR_LATER
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001091 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1092 for (i = 0; i < RARRAY_LEN(bt); i++)
1093 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001094# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001095 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1096 for (i = 0; i < RARRAY_LEN(bt); i++)
1097 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001098# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001099 break;
1100 default:
1101 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1102 emsg(buff);
1103 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 }
1105}
1106
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001107 static VALUE
1108vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
1110 char *buff, *p;
1111
1112 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001113 if (RSTRING_LEN(str) > 0)
1114 {
1115 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001116 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001117 strcpy(buff, RSTRING_PTR(str));
1118 p = strchr(buff, '\n');
1119 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001120 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001121 }
1122 else
1123 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001124 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 return Qnil;
1127}
1128
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001129 static VALUE
1130vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001132 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 update_screen(NOT_VALID);
1134 return Qnil;
1135}
1136
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001137 static VALUE
1138vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001140 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 return Qnil;
1142}
1143
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001144#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001145 static VALUE
1146vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001147{
1148 VALUE result = Qnil;
1149
1150 if (tv->v_type == VAR_STRING)
1151 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001152 result = rb_str_new2(tv->vval.v_string == NULL
1153 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001154 }
1155 else if (tv->v_type == VAR_NUMBER)
1156 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001157 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001158 }
1159# ifdef FEAT_FLOAT
1160 else if (tv->v_type == VAR_FLOAT)
1161 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001162 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001163 }
1164# endif
1165 else if (tv->v_type == VAR_LIST)
1166 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001167 list_T *list = tv->vval.v_list;
1168 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001169
Bram Moolenaar639a2552010-03-17 18:15:23 +01001170 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001171
Bram Moolenaar639a2552010-03-17 18:15:23 +01001172 if (list != NULL)
1173 {
1174 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001175 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001176 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001177 }
1178 else if (tv->v_type == VAR_DICT)
1179 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001180 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001181
Bram Moolenaar639a2552010-03-17 18:15:23 +01001182 if (tv->vval.v_dict != NULL)
1183 {
1184 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1185 long_u todo = ht->ht_used;
1186 hashitem_T *hi;
1187 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001188
Bram Moolenaar639a2552010-03-17 18:15:23 +01001189 for (hi = ht->ht_array; todo > 0; ++hi)
1190 {
1191 if (!HASHITEM_EMPTY(hi))
1192 {
1193 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001194
Bram Moolenaar639a2552010-03-17 18:15:23 +01001195 di = dict_lookup(hi);
1196 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001197 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001198 }
1199 }
1200 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001201 }
1202 else if (tv->v_type == VAR_SPECIAL)
1203 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001204 if (tv->vval.v_number == VVAL_TRUE)
1205 result = Qtrue;
1206 else if (tv->vval.v_number == VVAL_FALSE)
1207 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001208 }
1209 else if (tv->v_type == VAR_BLOB)
1210 {
1211 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1212 tv->vval.v_blob->bv_ga.ga_len);
1213 }
1214 /* else return Qnil; */
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001215
1216 return result;
1217}
1218#endif
1219
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001220 static VALUE
1221vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222{
1223#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001224 typval_T *tv;
1225 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001227 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1228 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001229 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001230 result = vim_to_ruby(tv);
1231
1232 free_tv(tv);
1233
1234 return result;
1235#else
1236 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238}
1239
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001240#ifdef USE_TYPEDDATA
1241static size_t buffer_dsize(const void *buf);
1242
1243static const rb_data_type_t buffer_type = {
1244 "vim_buffer",
1245 {0, 0, buffer_dsize, {0, 0}},
1246 0, 0,
1247# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1248 0,
1249# endif
1250};
1251
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001252 static size_t
1253buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001254{
1255 return sizeof(buf_T);
1256}
1257#endif
1258
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001259 static VALUE
1260buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001262 if (buf->b_ruby_ref)
1263 {
1264 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001266 else
1267 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001268#ifdef USE_TYPEDDATA
1269 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1270#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001272#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001273 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1275 return obj;
1276 }
1277}
1278
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001279 static buf_T *
1280get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281{
1282 buf_T *buf;
1283
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001284#ifdef USE_TYPEDDATA
1285 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1286#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (buf == NULL)
1290 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1291 return buf;
1292}
1293
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001294 static VALUE
1295vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001296{
1297 VALUE result = rb_str_new("0z", 2);
1298 char buf[4];
1299 int i;
1300 for (i = 0; i < RSTRING_LEN(str); i++)
1301 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001302 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001303 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001304 }
1305 return result;
1306}
1307
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001308 static VALUE
1309buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
1311 return buffer_new(curbuf);
1312}
1313
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001314 static VALUE
1315buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316{
1317 buf_T *b;
1318 int n = 0;
1319
Bram Moolenaar29323592016-07-24 22:04:11 +02001320 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001321 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001322 /* Deleted buffers should not be counted
1323 * SegPhault - 01/07/05 */
1324 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001325 n++;
1326 }
1327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 return INT2NUM(n);
1329}
1330
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001331 static VALUE
1332buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
1334 buf_T *b;
1335 int n = NUM2INT(num);
1336
Bram Moolenaar29323592016-07-24 22:04:11 +02001337 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001338 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001339 /* Deleted buffers should not be counted
1340 * SegPhault - 01/07/05 */
1341 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001342 continue;
1343
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001344 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001346
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001347 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349 return Qnil;
1350}
1351
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001352 static VALUE
1353buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354{
1355 buf_T *buf = get_buf(self);
1356
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001357 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358}
1359
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001360 static VALUE
1361buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362{
1363 buf_T *buf = get_buf(self);
1364
1365 return INT2NUM(buf->b_fnum);
1366}
1367
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001368 static VALUE
1369buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
1371 buf_T *buf = get_buf(self);
1372
1373 return INT2NUM(buf->b_ml.ml_line_count);
1374}
1375
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001376 static VALUE
1377get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001379 if (n <= 0 || n > buf->b_ml.ml_line_count)
1380 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1381 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382}
1383
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001384 static VALUE
1385buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001388
1389 if (buf != NULL)
1390 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1391 return Qnil; /* For stop warning */
1392}
1393
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001394 static VALUE
1395set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001396{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001397 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001398 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001400 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1401 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001402 /* set curwin/curbuf for "buf" and save some things */
1403 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001404
Bram Moolenaar758535a2016-03-30 22:06:16 +02001405 if (u_savesub(n) == OK)
1406 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001407 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 changed();
1409#ifdef SYNTAX_HL
1410 syn_changed(n); /* recompute syntax hl. for this line */
1411#endif
1412 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001413
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001414 /* restore curwin/curbuf and a few other things */
1415 aucmd_restbuf(&aco);
1416 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001417
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 update_curbuf(NOT_VALID);
1419 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001420 else
1421 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001422 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
1424 return str;
1425}
1426
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001427 static VALUE
1428buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001429{
1430 buf_T *buf = get_buf(self);
1431
1432 if (buf != NULL)
1433 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1434 return str;
1435}
1436
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001437 static VALUE
1438buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001440 buf_T *buf = get_buf(self);
1441 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001442 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001444 if (n > 0 && n <= buf->b_ml.ml_line_count)
1445 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001446 /* set curwin/curbuf for "buf" and save some things */
1447 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001448
Bram Moolenaar758535a2016-03-30 22:06:16 +02001449 if (u_savedel(n, 1) == OK)
1450 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001452
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001453 /* Changes to non-active buffers should properly refresh
1454 * SegPhault - 01/09/05 */
1455 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 changed();
1458 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001459
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001460 /* restore curwin/curbuf and a few other things */
1461 aucmd_restbuf(&aco);
1462 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001463
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 update_curbuf(NOT_VALID);
1465 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001466 else
1467 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001468 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
1470 return Qnil;
1471}
1472
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001473 static VALUE
1474buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001476 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001477 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001478 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001479 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480
Bram Moolenaar3c531602010-11-16 14:46:19 +01001481 if (line == NULL)
1482 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001483 rb_raise(rb_eIndexError, "NULL line");
1484 }
1485 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001486 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001487 /* set curwin/curbuf for "buf" and save some things */
1488 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001489
Bram Moolenaar758535a2016-03-30 22:06:16 +02001490 if (u_inssub(n + 1) == OK)
1491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001493
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001494 /* Changes to non-active buffers should properly refresh screen
1495 * SegPhault - 12/20/04 */
1496 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001497
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001498 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001500
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001501 /* restore curwin/curbuf and a few other things */
1502 aucmd_restbuf(&aco);
1503 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 update_curbuf(NOT_VALID);
1506 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001507 else
1508 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001509 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
1511 return str;
1512}
1513
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001514#ifdef USE_TYPEDDATA
1515static size_t window_dsize(const void *buf);
1516
1517static const rb_data_type_t window_type = {
1518 "vim_window",
1519 {0, 0, window_dsize, {0, 0}},
1520 0, 0,
1521# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1522 0,
1523# endif
1524};
1525
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001526 static size_t
1527window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001528{
1529 return sizeof(win_T);
1530}
1531#endif
1532
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001533 static VALUE
1534window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001536 if (win->w_ruby_ref)
1537 {
1538 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001540 else
1541 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001542#ifdef USE_TYPEDDATA
1543 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1544#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001546#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001547 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1549 return obj;
1550 }
1551}
1552
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001553 static win_T *
1554get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555{
1556 win_T *win;
1557
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001558#ifdef USE_TYPEDDATA
1559 TypedData_Get_Struct(obj, win_T, &window_type, win);
1560#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 if (win == NULL)
1564 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1565 return win;
1566}
1567
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001568 static VALUE
1569window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570{
1571 return window_new(curwin);
1572}
1573
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001574/*
1575 * Added line manipulation functions
1576 * SegPhault - 03/07/05
1577 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001578 static VALUE
1579line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001580{
1581 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1582}
1583
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001584 static VALUE
1585set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001586{
1587 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1588}
1589
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001590 static VALUE
1591current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001592{
1593 return INT2FIX((int)curwin->w_cursor.lnum);
1594}
1595
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001596 static VALUE
1597window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 win_T *w;
1600 int n = 0;
1601
Bram Moolenaar29323592016-07-24 22:04:11 +02001602 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 n++;
1604 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605}
1606
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001607 static VALUE
1608window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609{
1610 win_T *w;
1611 int n = NUM2INT(num);
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (n == 0)
1615 return window_new(w);
1616 return Qnil;
1617}
1618
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001619 static VALUE
1620window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
1622 win_T *win = get_win(self);
1623
1624 return buffer_new(win->w_buffer);
1625}
1626
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001627 static VALUE
1628window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629{
1630 win_T *win = get_win(self);
1631
1632 return INT2NUM(win->w_height);
1633}
1634
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001635 static VALUE
1636window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 win_T *win = get_win(self);
1639 win_T *savewin = curwin;
1640
1641 curwin = win;
1642 win_setheight(NUM2INT(height));
1643 curwin = savewin;
1644 return height;
1645}
1646
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001647 static VALUE
1648window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001649{
Bram Moolenaare745d752017-09-22 16:56:22 +02001650 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001651}
1652
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001653 static VALUE
1654window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001655{
1656 win_T *win = get_win(self);
1657 win_T *savewin = curwin;
1658
1659 curwin = win;
1660 win_setwidth(NUM2INT(width));
1661 curwin = savewin;
1662 return width;
1663}
1664
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001665 static VALUE
1666window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667{
1668 win_T *win = get_win(self);
1669
1670 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1671}
1672
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001673 static VALUE
1674window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
1676 VALUE lnum, col;
1677 win_T *win = get_win(self);
1678
1679 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001680 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001682 lnum = RARRAY_PTR(pos)[0];
1683 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 win->w_cursor.lnum = NUM2LONG(lnum);
1685 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001686 win->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 check_cursor(); /* put cursor on an existing line */
1688 update_screen(NOT_VALID);
1689 return Qnil;
1690}
1691
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001692 static VALUE
1693f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001694{
1695 return Qnil;
1696}
1697
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001698 static VALUE
1699f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700{
1701 int i;
1702 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001703 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaar758535a2016-03-30 22:06:16 +02001705 for (i = 0; i < argc; i++)
1706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 if (i > 0) rb_str_cat(str, ", ", 2);
1708 rb_str_concat(str, rb_inspect(argv[i]));
1709 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001710 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001711
1712 if (argc == 1)
1713 ret = argv[0];
1714 else if (argc > 1)
1715 ret = rb_ary_new4(argc, argv);
1716 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717}
1718
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001719 static void
1720ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721{
1722#ifndef DYNAMIC_RUBY
1723 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001724 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#endif
1726
1727 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001728 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001730 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001731 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1732 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 rb_define_global_function("p", f_p, -1);
1734}
1735
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001736 static void
1737ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
1739 objtbl = rb_hash_new();
1740 rb_global_variable(&objtbl);
1741
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001742 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001743 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001744 mVIM = rb_define_module("Vim");
1745 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1747 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1748 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1749 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1750 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1751 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1752 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1753 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1754 rb_define_module_function(mVIM, "message", vim_message, 1);
1755 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1756 rb_define_module_function(mVIM, "command", vim_command, 1);
1757 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001758 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
1760 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1761 rb_eStandardError);
1762 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1763 rb_eStandardError);
1764
1765 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1766 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1767 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1768 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1769 rb_define_method(cBuffer, "name", buffer_name, 0);
1770 rb_define_method(cBuffer, "number", buffer_number, 0);
1771 rb_define_method(cBuffer, "count", buffer_count, 0);
1772 rb_define_method(cBuffer, "length", buffer_count, 0);
1773 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1774 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1775 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1776 rb_define_method(cBuffer, "append", buffer_append, 2);
1777
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001778 /* Added line manipulation functions
1779 * SegPhault - 03/07/05 */
1780 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1781 rb_define_method(cBuffer, "line", line_s_current, 0);
1782 rb_define_method(cBuffer, "line=", set_current_line, 1);
1783
1784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1786 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1787 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1788 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1789 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1790 rb_define_method(cVimWindow, "height", window_height, 0);
1791 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001792 rb_define_method(cVimWindow, "width", window_width, 0);
1793 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1795 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1796
1797 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1798 rb_define_virtual_variable("$curwin", window_s_current, 0);
1799}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001800
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001801 void
1802vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001803{
1804 /* should get machine stack start address early in main function */
1805 ruby_stack_start = stack_start;
1806}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001807
1808 static int
1809convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1810{
1811 dict_T *d = (dict_T *)arg;
1812 dictitem_T *di;
1813
1814 di = dictitem_alloc((char_u *)RSTRING_PTR(RSTRING(rb_obj_as_string(key))));
1815 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1816 || dict_add(d, di) != OK)
1817 {
1818 d->dv_hashtab.ht_error = TRUE;
1819 return ST_STOP;
1820 }
1821 return ST_CONTINUE;
1822}
1823
1824 static int
1825ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1826{
1827 switch (TYPE(val))
1828 {
1829 case T_NIL:
1830 rettv->v_type = VAR_SPECIAL;
1831 rettv->vval.v_number = VVAL_NULL;
1832 break;
1833 case T_TRUE:
1834 rettv->v_type = VAR_SPECIAL;
1835 rettv->vval.v_number = VVAL_TRUE;
1836 break;
1837 case T_FALSE:
1838 rettv->v_type = VAR_SPECIAL;
1839 rettv->vval.v_number = VVAL_FALSE;
1840 break;
1841 case T_BIGNUM:
1842 case T_FIXNUM:
1843 rettv->v_type = VAR_NUMBER;
1844 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1845 break;
1846#ifdef FEAT_FLOAT
1847 case T_FLOAT:
1848 rettv->v_type = VAR_FLOAT;
1849 rettv->vval.v_float = (float_T)NUM2DBL(val);
1850 break;
1851#endif
1852 default:
1853 val = rb_obj_as_string(val);
1854 // FALLTHROUGH
1855 case T_STRING:
1856 {
1857 VALUE str = (VALUE)RSTRING(val);
1858
1859 rettv->v_type = VAR_STRING;
1860 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
1861 (int)RSTRING_LEN(str));
1862 }
1863 break;
1864 case T_ARRAY:
1865 {
1866 list_T *l;
1867 long i;
1868 typval_T v;
1869
1870 l = list_alloc();
1871 if (l == NULL)
1872 return FAIL;
1873
1874 for (i = 0; i < RARRAY_LEN(val); ++i)
1875 {
1876 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1877 &v) != OK)
1878 {
1879 list_unref(l);
1880 return FAIL;
1881 }
1882 list_append_tv(l, &v);
1883 clear_tv(&v);
1884 }
1885
1886 rettv->v_type = VAR_LIST;
1887 rettv->vval.v_list = l;
1888 ++l->lv_refcount;
1889 }
1890 break;
1891 case T_HASH:
1892 {
1893 dict_T *d;
1894
1895 d = dict_alloc();
1896 if (d == NULL)
1897 return FAIL;
1898
1899 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1900 if (d->dv_hashtab.ht_error)
1901 {
1902 dict_unref(d);
1903 return FAIL;
1904 }
1905
1906 rettv->v_type = VAR_DICT;
1907 rettv->vval.v_dict = d;
1908 ++d->dv_refcount;
1909 }
1910 break;
1911 }
1912 return OK;
1913}
1914
1915 void
1916do_rubyeval(char_u *str, typval_T *rettv)
1917{
1918 int retval = FAIL;
1919
1920 if (ensure_ruby_initialized())
1921 {
1922 int state;
1923 VALUE obj;
1924
1925 obj = rb_eval_string_protect((const char *)str, &state);
1926 if (state)
1927 error_print(state);
1928 else
1929 retval = ruby_convert_to_vim_value(obj, rettv);
1930 }
1931 if (retval == FAIL)
1932 {
1933 rettv->v_type = VAR_NUMBER;
1934 rettv->vval.v_number = 0;
1935 }
1936}