xref: /vim-8.2.3635/src/if_ruby.c (revision 8024f936)
1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *
5  * Ruby interface by Shugo Maeda
6  *   with improvements by SegPhault (Ryan Paul)
7  *   with improvements by Jon Maken
8  *
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 
14 #include "protodef.h"
15 #ifdef HAVE_CONFIG_H
16 # include "auto/config.h"
17 #endif
18 
19 #include <stdio.h>
20 #include <string.h>
21 
22 #ifdef _WIN32
23 # if !defined(DYNAMIC_RUBY) || (RUBY_VERSION < 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 
35 #if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 24
36 # define USE_RUBY_INTEGER
37 #endif
38 
39 #ifdef DYNAMIC_RUBY
40 /*
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  */
45 # define rb_cFalseClass		(*dll_rb_cFalseClass)
46 # define rb_cFixnum		(*dll_rb_cFixnum)
47 # if defined(USE_RUBY_INTEGER)
48 #  define rb_cInteger		(*dll_rb_cInteger)
49 # endif
50 # if RUBY_VERSION >= 20
51 #  define rb_cFloat		(*dll_rb_cFloat)
52 # endif
53 # define rb_cNilClass		(*dll_rb_cNilClass)
54 # define rb_cString		(*dll_rb_cString)
55 # define rb_cSymbol		(*dll_rb_cSymbol)
56 # define rb_cTrueClass		(*dll_rb_cTrueClass)
57 # if RUBY_VERSION >= 18
58 /*
59  * 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
61  * 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
66 
67 #endif  // ifdef DYNAMIC_RUBY
68 
69 // suggested by Ariya Mizutani
70 #if (_MSC_VER == 1200)
71 # undef _WIN32_WINNT
72 #endif
73 
74 #if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 19
75 // Ruby 1.9 defines a number of static functions which use rb_num2long and
76 // rb_int2big
77 # define rb_num2long rb_num2long_stub
78 # define rb_int2big rb_int2big_stub
79 #endif
80 
81 #if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 19 \
82 	&& VIM_SIZEOF_INT < VIM_SIZEOF_LONG
83 // 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)
85 # define rb_fix2int rb_fix2int_stub
86 # define rb_num2int rb_num2int_stub
87 #endif
88 
89 #if defined(DYNAMIC_RUBY) && RUBY_VERSION == 21
90 // Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
91 // rb_gc_writebarrier_unprotect_promoted if USE_RGENGC
92 # define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
93 #endif
94 #if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 22
95 # define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
96 #endif
97 
98 #if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 26
99 # define rb_ary_detransient rb_ary_detransient_stub
100 #endif
101 
102 #include <ruby.h>
103 #if RUBY_VERSION >= 19
104 # include <ruby/encoding.h>
105 #endif
106 #if RUBY_VERSION <= 18
107 # include <st.h>  // for ST_STOP and ST_CONTINUE
108 #endif
109 
110 #undef off_t	// ruby defines off_t as _int64, Mingw uses long
111 #undef EXTERN
112 #undef _
113 
114 // T_DATA defined both by Ruby and Mac header files, hack around it...
115 #if defined(MACOS_X)
116 # define __OPENTRANSPORT__
117 # define __OPENTRANSPORTPROTOCOL__
118 # define __OPENTRANSPORTPROVIDERS__
119 #endif
120 
121 /*
122  * 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.
125  * Use TypedData_XXX if available.
126  */
127 #if defined(TypedData_Wrap_Struct) && (RUBY_VERSION >= 20)
128 # define USE_TYPEDDATA	1
129 #endif
130 
131 /*
132  * Backward compatibility for Ruby 1.8 and earlier.
133  * 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 
153 #ifdef HAVE_DUP
154 # undef HAVE_DUP
155 #endif
156 
157 #include "vim.h"
158 #include "version.h"
159 
160 #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 
176 #if defined(PROTO) && !defined(FEAT_RUBY)
177 // Define these to be able to generate the function prototypes.
178 # define VALUE int
179 # define RUBY_DATA_FUNC int
180 #endif
181 
182 static int ruby_initialized = 0;
183 static void *ruby_stack_start;
184 static VALUE objtbl;
185 
186 static VALUE mVIM;
187 static VALUE cBuffer;
188 static VALUE cVimWindow;
189 static VALUE eDeletedBufferError;
190 static VALUE eDeletedWindowError;
191 
192 static int ensure_ruby_initialized(void);
193 static void error_print(int);
194 static void ruby_io_init(void);
195 static void ruby_vim_init(void);
196 static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
197 
198 #if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
199 # if defined(__ia64) && !defined(ruby_init_stack)
200 #  define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
201 # endif
202 #endif
203 
204 #if defined(DYNAMIC_RUBY) || defined(PROTO)
205 # if defined(PROTO) && !defined(HINSTANCE)
206 #  define HINSTANCE int		// for generating prototypes
207 # endif
208 
209 /*
210  * Wrapper defines
211  */
212 # define rb_assoc_new			dll_rb_assoc_new
213 # define rb_cObject			(*dll_rb_cObject)
214 # define rb_class_new_instance		dll_rb_class_new_instance
215 # define rb_check_type			dll_rb_check_type
216 # ifdef USE_TYPEDDATA
217 #  define rb_check_typeddata		dll_rb_check_typeddata
218 # endif
219 # define rb_class_path			dll_rb_class_path
220 # ifdef USE_TYPEDDATA
221 #  if RUBY_VERSION >= 23
222 #   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
229 # 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)
238 # define rb_stderr			(*dll_rb_stderr)
239 # 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
244 # if RUBY_VERSION >= 21
245 #  define rb_funcallv			dll_rb_funcallv
246 # else
247 #  define rb_funcall2			dll_rb_funcall2
248 # endif
249 # define rb_global_variable		dll_rb_global_variable
250 # define rb_hash_aset			dll_rb_hash_aset
251 # define rb_hash_foreach		dll_rb_hash_foreach
252 # define rb_hash_new			dll_rb_hash_new
253 # define rb_inspect			dll_rb_inspect
254 # define rb_int2inum			dll_rb_int2inum
255 
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 
261 # if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
262 #  if RUBY_VERSION <= 18
263 #   define rb_fix2int			dll_rb_fix2int
264 #   define rb_num2int			dll_rb_num2int
265 #  endif
266 #  define rb_num2uint			dll_rb_num2uint
267 # endif
268 # define rb_num2dbl			dll_rb_num2dbl
269 # define rb_lastline_get			dll_rb_lastline_get
270 # define rb_lastline_set			dll_rb_lastline_set
271 # define rb_protect			dll_rb_protect
272 # define rb_load			dll_rb_load
273 # if RUBY_VERSION <= 18
274 #  define rb_num2long			dll_rb_num2long
275 # endif
276 # if RUBY_VERSION <= 19
277 #  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
285 # undef rb_str_new
286 # define rb_str_new			dll_rb_str_new
287 # ifdef rb_str_new2
288 // Ruby may #define rb_str_new2 to use rb_str_new_cstr.
289 #  define need_rb_str_new_cstr 1
290 // Ruby's headers #define rb_str_new_cstr to make use of GCC's
291 // __builtin_constant_p extension.
292 #  undef rb_str_new_cstr
293 #  define rb_str_new_cstr		dll_rb_str_new_cstr
294 # else
295 #  define rb_str_new2			dll_rb_str_new2
296 # endif
297 # if RUBY_VERSION >= 18
298 #  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
302 #  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
307 #  define rb_ary_push			dll_rb_ary_push
308 #  if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
309 #   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
316 #  endif
317 # else
318 #  define rb_str2cstr			dll_rb_str2cstr
319 # endif
320 # if RUBY_VERSION >= 19
321 #  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
327 # ifdef MSWIN
328 #  if RUBY_VERSION >= 19
329 #   define ruby_sysinit			dll_ruby_sysinit
330 #  else
331 #   define NtInitialize			dll_NtInitialize
332 #  endif
333 #  if RUBY_VERSION >= 18
334 #   define rb_w32_snprintf		dll_rb_w32_snprintf
335 #  endif
336 # endif
337 
338 # if RUBY_VERSION >= 19
339 #  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
342 #  undef rb_enc_str_new
343 #  define rb_enc_str_new		dll_rb_enc_str_new
344 #  define rb_sprintf			dll_rb_sprintf
345 #  define rb_require			dll_rb_require
346 #  define ruby_options			dll_ruby_options
347 # endif
348 
349 /*
350  * Pointers for dynamic link
351  */
352 static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
353 VALUE *dll_rb_cFalseClass;
354 VALUE *dll_rb_cFixnum;
355 # if defined(USE_RUBY_INTEGER)
356 VALUE *dll_rb_cInteger;
357 # endif
358 # if RUBY_VERSION >= 20
359 VALUE *dll_rb_cFloat;
360 # endif
361 VALUE *dll_rb_cNilClass;
362 static VALUE *dll_rb_cObject;
363 VALUE *dll_rb_cString;
364 VALUE *dll_rb_cSymbol;
365 VALUE *dll_rb_cTrueClass;
366 static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
367 static void (*dll_rb_check_type) (VALUE,int);
368 # ifdef USE_TYPEDDATA
369 static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
370 # endif
371 static VALUE (*dll_rb_class_path) (VALUE);
372 # ifdef USE_TYPEDDATA
373 #  if RUBY_VERSION >= 23
374 static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
375 #  else
376 static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
377 #  endif
378 # else
379 static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
380 # endif
381 static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
382 static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
383 static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
384 static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
385 static VALUE (*dll_rb_define_module) (const char*);
386 static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
387 static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
388 static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
389 static VALUE *dll_rb_stdout;
390 static VALUE *dll_rb_stderr;
391 static VALUE *dll_rb_eArgError;
392 static VALUE *dll_rb_eIndexError;
393 static VALUE *dll_rb_eRuntimeError;
394 static VALUE *dll_rb_eStandardError;
395 static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
396 # if RUBY_VERSION >= 21
397 static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
398 # else
399 static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
400 # endif
401 static void (*dll_rb_global_variable) (VALUE*);
402 static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
403 static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
404 static VALUE (*dll_rb_hash_new) (void);
405 static VALUE (*dll_rb_inspect) (VALUE);
406 static VALUE (*dll_rb_int2inum) (long);
407 static ID (*dll_rb_intern) (const char*);
408 # if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
409 static long (*dll_rb_fix2int) (VALUE);
410 static long (*dll_rb_num2int) (VALUE);
411 static unsigned long (*dll_rb_num2uint) (VALUE);
412 # endif
413 static double (*dll_rb_num2dbl) (VALUE);
414 static VALUE (*dll_rb_lastline_get) (void);
415 static void (*dll_rb_lastline_set) (VALUE);
416 static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
417 static void (*dll_rb_load) (VALUE, int);
418 static long (*dll_rb_num2long) (VALUE);
419 static unsigned long (*dll_rb_num2ulong) (VALUE);
420 static VALUE (*dll_rb_obj_alloc) (VALUE);
421 static VALUE (*dll_rb_obj_as_string) (VALUE);
422 static VALUE (*dll_rb_obj_id) (VALUE);
423 static void (*dll_rb_raise) (VALUE, const char*, ...);
424 # if RUBY_VERSION >= 18
425 static VALUE (*dll_rb_string_value) (volatile VALUE*);
426 # else
427 static char *(*dll_rb_str2cstr) (VALUE,int*);
428 # endif
429 static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
430 static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
431 static VALUE (*dll_rb_str_new) (const char*, long);
432 # ifdef need_rb_str_new_cstr
433 // Ruby may #define rb_str_new2 to use rb_str_new_cstr.
434 static VALUE (*dll_rb_str_new_cstr) (const char*);
435 # else
436 static VALUE (*dll_rb_str_new2) (const char*);
437 # endif
438 # if RUBY_VERSION >= 19
439 static VALUE (*dll_rb_errinfo) (void);
440 # else
441 static VALUE *dll_ruby_errinfo;
442 # endif
443 static void (*dll_ruby_init) (void);
444 static void (*dll_ruby_init_loadpath) (void);
445 # ifdef MSWIN
446 #  if RUBY_VERSION >= 19
447 static void (*dll_ruby_sysinit) (int*, char***);
448 #  else
449 static void (*dll_NtInitialize) (int*, char***);
450 #  endif
451 #  if RUBY_VERSION >= 18
452 static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
453 #  endif
454 # endif
455 # if RUBY_VERSION >= 18
456 static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
457 static VALUE (*dll_rb_float_new) (double);
458 static VALUE (*dll_rb_ary_new) (void);
459 static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
460 static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
461 #  if RUBY_VERSION >= 26
462 static void (*dll_rb_ary_detransient) (VALUE);
463 #  endif
464 #  if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
465 #   ifdef __ia64
466 static void * (*dll_rb_ia64_bsp) (void);
467 static void (*dll_ruby_init_stack)(VALUE*, void*);
468 #   else
469 static void (*dll_ruby_init_stack)(VALUE*);
470 #   endif
471 #  endif
472 # endif
473 # if RUBY_VERSION >= 19
474 static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
475 # endif
476 
477 # if RUBY_VERSION >= 19
478 static void (*dll_ruby_script) (const char*);
479 static int (*dll_rb_enc_find_index) (const char*);
480 static rb_encoding* (*dll_rb_enc_find) (const char*);
481 static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
482 static VALUE (*dll_rb_sprintf) (const char*, ...);
483 static VALUE (*dll_rb_require) (const char*);
484 static void* (*ruby_options)(int, char**);
485 # endif
486 
487 # if defined(USE_RGENGC) && USE_RGENGC
488 #  if RUBY_VERSION == 21
489 static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
490 #  else
491 static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
492 #  endif
493 # endif
494 
495 # if (RUBY_VERSION >= 19) && !defined(PROTO)
496 #  if RUBY_VERSION >= 22
497     long
498 rb_num2long_stub(VALUE x)
499 #  else
500     SIGNED_VALUE
501 rb_num2long_stub(VALUE x)
502 #  endif
503 {
504     return dll_rb_num2long(x);
505 }
506 #  if RUBY_VERSION >= 26
507     VALUE
508 rb_int2big_stub(intptr_t x)
509 #  else
510     VALUE
511 rb_int2big_stub(SIGNED_VALUE x)
512 #  endif
513 {
514     return dll_rb_int2big(x);
515 }
516 #  if (RUBY_VERSION >= 19) && (VIM_SIZEOF_INT < VIM_SIZEOF_LONG)
517     long
518 rb_fix2int_stub(VALUE x)
519 {
520     return dll_rb_fix2int(x);
521 }
522     long
523 rb_num2int_stub(VALUE x)
524 {
525     return dll_rb_num2int(x);
526 }
527 #  endif
528 #  if RUBY_VERSION >= 20
529     VALUE
530 rb_float_new_in_heap(double d)
531 {
532     return dll_rb_float_new(d);
533 }
534 #   if RUBY_VERSION >= 22
535     unsigned long
536 rb_num2ulong(VALUE x)
537 #   else
538     VALUE
539 rb_num2ulong(VALUE x)
540 #   endif
541 {
542     return (long)RSHIFT((SIGNED_VALUE)(x),1);
543 }
544 #  endif
545 # endif
546 
547    // Do not generate a prototype here, VALUE isn't always defined.
548 # if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
549 #  if RUBY_VERSION == 21
550     void
551 rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
552 {
553     dll_rb_gc_writebarrier_unprotect_promoted(obj);
554 }
555 #  else
556     void
557 rb_gc_writebarrier_unprotect_stub(VALUE obj)
558 {
559     dll_rb_gc_writebarrier_unprotect(obj);
560 }
561 #  endif
562 # endif
563 
564 # if RUBY_VERSION >= 26
565     void
566 rb_ary_detransient_stub(VALUE x)
567 {
568     dll_rb_ary_detransient(x);
569 }
570 # endif
571 
572 static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
573 
574 /*
575  * Table of name to function pointer of ruby.
576  */
577 static 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},
585 # if defined(USE_RUBY_INTEGER)
586     {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
587 # else
588     {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
589 # endif
590 # if RUBY_VERSION >= 20
591     {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
592 # endif
593     {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
594     {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
595     {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
596     {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
597     {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
598     {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
599     {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
600 # ifdef USE_TYPEDDATA
601     {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
602 # endif
603     {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
604 # ifdef USE_TYPEDDATA
605 #  if RUBY_VERSION >= 23
606     {"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
611     {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
612 # endif
613     {"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},
622     {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
623     {"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},
628 # if RUBY_VERSION >= 21
629     {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
630 # else
631     {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
632 # endif
633     {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
634     {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
635     {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
636     {"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},
639     {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
640 # if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
641     {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
642     {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
643     {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
644 # endif
645     {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
646     {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
647     {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
648     {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
649     {"rb_load", (RUBY_PROC*)&dll_rb_load},
650     {"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},
656 # if RUBY_VERSION >= 18
657     {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
658 # else
659     {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
660 # endif
661     {"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},
664 # ifdef need_rb_str_new_cstr
665     {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
666 # else
667     {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
668 # endif
669 # if RUBY_VERSION >= 19
670     {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
671 # else
672     {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
673 # endif
674     {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
675     {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
676 # ifdef MSWIN
677 #  if RUBY_VERSION >= 19
678     {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
679 #  else
680     {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
681 #  endif
682 #  if RUBY_VERSION >= 18
683     {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
684 #  endif
685 # endif
686 # if RUBY_VERSION >= 18
687     {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
688 #  if RUBY_VERSION <= 19
689     {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
690 #  else
691     {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
692 #  endif
693     {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
694 #  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
699     {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
700 #  if RUBY_VERSION >= 26
701     {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
702 #  endif
703 # endif
704 # if RUBY_VERSION >= 19
705     {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
706     {"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},
711     {"rb_require", (RUBY_PROC*)&dll_rb_require},
712     {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
713 # endif
714 # if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
715 #  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
720 # if defined(USE_RGENGC) && USE_RGENGC
721 #  if RUBY_VERSION == 21
722     {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
723 #  else
724     {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
725 #  endif
726 # endif
727     {"", NULL},
728 };
729 
730 /*
731  * Free ruby.dll
732  */
733     static void
734 end_dynamic_ruby(void)
735 {
736     if (hinstRuby)
737     {
738 	close_dll(hinstRuby);
739 	hinstRuby = NULL;
740     }
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
749 ruby_runtime_link_init(char *libname, int verbose)
750 {
751     int i;
752 
753     if (hinstRuby)
754 	return OK;
755     hinstRuby = load_dll(libname);
756     if (!hinstRuby)
757     {
758 	if (verbose)
759 	    semsg(_(e_loadlib), libname);
760 	return FAIL;
761     }
762 
763     for (i = 0; ruby_funcname_table[i].ptr; ++i)
764     {
765 	if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
766 			ruby_funcname_table[i].name)))
767 	{
768 	    close_dll(hinstRuby);
769 	    hinstRuby = NULL;
770 	    if (verbose)
771 		semsg(_(e_loadfunc), ruby_funcname_table[i].name);
772 	    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
783 ruby_enabled(int verbose)
784 {
785     return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
786 }
787 #endif // defined(DYNAMIC_RUBY) || defined(PROTO)
788 
789     void
790 ruby_end(void)
791 {
792 #ifdef DYNAMIC_RUBY
793     end_dynamic_ruby();
794 #endif
795 }
796 
797     void
798 ex_ruby(exarg_T *eap)
799 {
800     int state;
801     char *script = NULL;
802 
803     script = (char *)script_get(eap, eap->arg);
804     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 
816 /*
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
822 vim_str2rb_enc_str(const char *s)
823 {
824 #if RUBY_VERSION >= 19
825     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);
835 	if (enc)
836 	    return rb_enc_str_new(s, (long)strlen(s), enc);
837     }
838 #endif
839     return rb_str_new2(s);
840 }
841 
842     static VALUE
843 eval_enc_string_protect(const char *str, int *state)
844 {
845 #if RUBY_VERSION >= 19
846     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 
867     void
868 ex_rubydo(exarg_T *eap)
869 {
870     int state;
871     linenr_T i;
872     buf_T   *was_curbuf = curbuf;
873 
874     if (ensure_ruby_initialized())
875     {
876 	if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
877 	    return;
878 	for (i = eap->line1; i <= eap->line2; i++)
879 	{
880 	    VALUE line;
881 
882 	    if (i > curbuf->b_ml.ml_line_count)
883 		break;
884 	    line = vim_str2rb_enc_str((char *)ml_get(i));
885 	    rb_lastline_set(line);
886 	    eval_enc_string_protect((char *) eap->arg, &state);
887 	    if (state)
888 	    {
889 		error_print(state);
890 		break;
891 	    }
892 	    if (was_curbuf != curbuf)
893 		break;
894 	    line = rb_lastline_get();
895 	    if (!NIL_P(line))
896 	    {
897 		if (TYPE(line) != T_STRING)
898 		{
899 		    emsg(_("E265: $_ must be an instance of String"));
900 		    return;
901 		}
902 		ml_replace(i, (char_u *) StringValuePtr(line), 1);
903 		changed();
904 #ifdef SYNTAX_HL
905 		syn_changed(i); // recompute syntax hl. for this line
906 #endif
907 	    }
908 	}
909 	check_cursor();
910 	update_curbuf(NOT_VALID);
911     }
912 }
913 
914     static VALUE
915 rb_load_wrap(VALUE file_to_load)
916 {
917     rb_load(file_to_load, 0);
918     return Qnil;
919 }
920 
921     void
922 ex_rubyfile(exarg_T *eap)
923 {
924     int state;
925 
926     if (ensure_ruby_initialized())
927     {
928 	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);
932     }
933 }
934 
935     void
936 ruby_buffer_free(buf_T *buf)
937 {
938     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;
942     }
943 }
944 
945     void
946 ruby_window_free(win_T *win)
947 {
948     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;
952     }
953 }
954 
955     static int
956 ensure_ruby_initialized(void)
957 {
958     if (!ruby_initialized)
959     {
960 #ifdef DYNAMIC_RUBY
961 	if (ruby_enabled(TRUE))
962 	{
963 #endif
964 #ifdef MSWIN
965 	    // suggested by Ariya Mizutani
966 	    int argc = 1;
967 	    char *argv[] = {"gvim.exe"};
968 	    char **argvp = argv;
969 # if RUBY_VERSION >= 19
970 	    ruby_sysinit(&argc, &argvp);
971 # else
972 	    NtInitialize(&argc, &argvp);
973 # endif
974 #endif
975 	    {
976 #if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
977 		ruby_init_stack(ruby_stack_start);
978 #endif
979 		ruby_init();
980 	    }
981 #if RUBY_VERSION >= 19
982 	    {
983 		int dummy_argc = 2;
984 		char *dummy_argv[] = {"vim-ruby", "-e_=0"};
985 		ruby_options(dummy_argc, dummy_argv);
986 	    }
987 	    ruby_script("vim-ruby");
988 #else
989 	    ruby_init_loadpath();
990 #endif
991 	    ruby_io_init();
992 	    ruby_vim_init();
993 	    ruby_initialized = 1;
994 #ifdef DYNAMIC_RUBY
995 	}
996 	else
997 	{
998 	    emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
999 	    return 0;
1000 	}
1001 #endif
1002     }
1003     return ruby_initialized;
1004 }
1005 
1006     static void
1007 error_print(int state)
1008 {
1009 #if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
1010     RUBYEXTERN VALUE ruby_errinfo;
1011 #endif
1012     VALUE error;
1013     VALUE eclass;
1014     VALUE einfo;
1015     VALUE bt;
1016     int attr;
1017     char buff[BUFSIZ];
1018     long i;
1019 
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 
1030     switch (state)
1031     {
1032 	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:
1049 #if RUBY_VERSION >= 19
1050 	    error = rb_errinfo();
1051 #else
1052 	    error = ruby_errinfo;
1053 #endif
1054 	    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;
1064 
1065 		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 	    }
1072 
1073 	    attr = syn_name2attr((char_u *)"Error");
1074 # if RUBY_VERSION >= 21
1075 	    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);
1078 # else
1079 	    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);
1082 # endif
1083 	    break;
1084 	default:
1085 	    vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1086 	    emsg(buff);
1087 	    break;
1088     }
1089 }
1090 
1091     static VALUE
1092 vim_message(VALUE self UNUSED, VALUE str)
1093 {
1094     char *buff, *p;
1095 
1096     str = rb_obj_as_string(str);
1097     if (RSTRING_LEN(str) > 0)
1098     {
1099 	// Only do this when the string isn't empty, alloc(0) causes trouble.
1100 	buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
1101 	strcpy(buff, RSTRING_PTR(str));
1102 	p = strchr(buff, '\n');
1103 	if (p) *p = '\0';
1104 	msg(buff);
1105     }
1106     else
1107     {
1108 	msg("");
1109     }
1110     return Qnil;
1111 }
1112 
1113     static VALUE
1114 vim_set_option(VALUE self UNUSED, VALUE str)
1115 {
1116     do_set((char_u *)StringValuePtr(str), 0);
1117     update_screen(NOT_VALID);
1118     return Qnil;
1119 }
1120 
1121     static VALUE
1122 vim_command(VALUE self UNUSED, VALUE str)
1123 {
1124     do_cmdline_cmd((char_u *)StringValuePtr(str));
1125     return Qnil;
1126 }
1127 
1128 #ifdef FEAT_EVAL
1129     static VALUE
1130 vim_to_ruby(typval_T *tv)
1131 {
1132     VALUE result = Qnil;
1133 
1134     if (tv->v_type == VAR_STRING)
1135     {
1136 	result = rb_str_new2(tv->vval.v_string == NULL
1137 					  ? "" : (char *)(tv->vval.v_string));
1138     }
1139     else if (tv->v_type == VAR_NUMBER)
1140     {
1141 	result = INT2NUM(tv->vval.v_number);
1142     }
1143 # ifdef FEAT_FLOAT
1144     else if (tv->v_type == VAR_FLOAT)
1145     {
1146 	result = rb_float_new(tv->vval.v_float);
1147     }
1148 # endif
1149     else if (tv->v_type == VAR_LIST)
1150     {
1151 	list_T      *list = tv->vval.v_list;
1152 	listitem_T  *curr;
1153 
1154 	result = rb_ary_new();
1155 
1156 	if (list != NULL)
1157 	{
1158 	    for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1159 		rb_ary_push(result, vim_to_ruby(&curr->li_tv));
1160 	}
1161     }
1162     else if (tv->v_type == VAR_DICT)
1163     {
1164 	result = rb_hash_new();
1165 
1166 	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;
1172 
1173 	    for (hi = ht->ht_array; todo > 0; ++hi)
1174 	    {
1175 		if (!HASHITEM_EMPTY(hi))
1176 		{
1177 		    --todo;
1178 
1179 		    di = dict_lookup(hi);
1180 		    rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
1181 						     vim_to_ruby(&di->di_tv));
1182 		}
1183 	    }
1184 	}
1185     }
1186     else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
1187     {
1188 	if (tv->vval.v_number == VVAL_TRUE)
1189 	    result = Qtrue;
1190 	else if (tv->vval.v_number == VVAL_FALSE)
1191 	    result = Qfalse;
1192     }
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     }
1198     // else return Qnil;
1199 
1200     return result;
1201 }
1202 #endif
1203 
1204     static VALUE
1205 vim_evaluate(VALUE self UNUSED, VALUE str)
1206 {
1207 #ifdef FEAT_EVAL
1208     typval_T    *tv;
1209     VALUE       result;
1210 
1211     tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1212     if (tv == NULL)
1213 	return Qnil;
1214     result = vim_to_ruby(tv);
1215 
1216     free_tv(tv);
1217 
1218     return result;
1219 #else
1220     return Qnil;
1221 #endif
1222 }
1223 
1224 #ifdef USE_TYPEDDATA
1225 static size_t buffer_dsize(const void *buf);
1226 
1227 static const rb_data_type_t buffer_type = {
1228     "vim_buffer",
1229     {0, 0, buffer_dsize,
1230 # if RUBY_VERSION >= 27
1231 	0, 0
1232 # else
1233 	{0, 0}
1234 # endif
1235     },
1236     0, 0,
1237 # ifdef RUBY_TYPED_FREE_IMMEDIATELY
1238     0,
1239 # endif
1240 };
1241 
1242     static size_t
1243 buffer_dsize(const void *buf UNUSED)
1244 {
1245     return sizeof(buf_T);
1246 }
1247 #endif
1248 
1249     static VALUE
1250 buffer_new(buf_T *buf)
1251 {
1252     if (buf->b_ruby_ref)
1253     {
1254 	return (VALUE) buf->b_ruby_ref;
1255     }
1256     else
1257     {
1258 #ifdef USE_TYPEDDATA
1259 	VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1260 #else
1261 	VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
1262 #endif
1263 	buf->b_ruby_ref = (void *) obj;
1264 	rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1265 	return obj;
1266     }
1267 }
1268 
1269     static buf_T *
1270 get_buf(VALUE obj)
1271 {
1272     buf_T *buf;
1273 
1274 #ifdef USE_TYPEDDATA
1275     TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1276 #else
1277     Data_Get_Struct(obj, buf_T, buf);
1278 #endif
1279     if (buf == NULL)
1280 	rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1281     return buf;
1282 }
1283 
1284     static VALUE
1285 vim_blob(VALUE self UNUSED, VALUE str)
1286 {
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     {
1292 	sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
1293 	rb_str_concat(result, rb_str_new2(buf));
1294     }
1295     return result;
1296 }
1297 
1298     static VALUE
1299 buffer_s_current(void)
1300 {
1301     return buffer_new(curbuf);
1302 }
1303 
1304     static VALUE
1305 buffer_s_count(void)
1306 {
1307     buf_T *b;
1308     int n = 0;
1309 
1310     FOR_ALL_BUFFERS(b)
1311     {
1312 	//  Deleted buffers should not be counted
1313 	//    SegPhault - 01/07/05
1314 	if (b->b_p_bl)
1315 	    n++;
1316     }
1317 
1318     return INT2NUM(n);
1319 }
1320 
1321     static VALUE
1322 buffer_s_aref(VALUE self UNUSED, VALUE num)
1323 {
1324     buf_T *b;
1325     int n = NUM2INT(num);
1326 
1327     FOR_ALL_BUFFERS(b)
1328     {
1329 	//  Deleted buffers should not be counted
1330 	//    SegPhault - 01/07/05
1331 	if (!b->b_p_bl)
1332 	    continue;
1333 
1334 	if (n == 0)
1335 	    return buffer_new(b);
1336 
1337 	n--;
1338     }
1339     return Qnil;
1340 }
1341 
1342     static VALUE
1343 buffer_name(VALUE self)
1344 {
1345     buf_T *buf = get_buf(self);
1346 
1347     return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
1348 }
1349 
1350     static VALUE
1351 buffer_number(VALUE self)
1352 {
1353     buf_T *buf = get_buf(self);
1354 
1355     return INT2NUM(buf->b_fnum);
1356 }
1357 
1358     static VALUE
1359 buffer_count(VALUE self)
1360 {
1361     buf_T *buf = get_buf(self);
1362 
1363     return INT2NUM(buf->b_ml.ml_line_count);
1364 }
1365 
1366     static VALUE
1367 get_buffer_line(buf_T *buf, linenr_T n)
1368 {
1369     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));
1372 }
1373 
1374     static VALUE
1375 buffer_aref(VALUE self, VALUE num)
1376 {
1377     buf_T *buf = get_buf(self);
1378 
1379     if (buf != NULL)
1380 	return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1381     return Qnil; // For stop warning
1382 }
1383 
1384     static VALUE
1385 set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1386 {
1387     char	*line = StringValuePtr(str);
1388     aco_save_T	aco;
1389 
1390     if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1391     {
1392 	// set curwin/curbuf for "buf" and save some things
1393 	aucmd_prepbuf(&aco, buf);
1394 
1395 	if (u_savesub(n) == OK)
1396 	{
1397 	    ml_replace(n, (char_u *)line, TRUE);
1398 	    changed();
1399 #ifdef SYNTAX_HL
1400 	    syn_changed(n); // recompute syntax hl. for this line
1401 #endif
1402 	}
1403 
1404 	// restore curwin/curbuf and a few other things
1405 	aucmd_restbuf(&aco);
1406 	// Careful: autocommands may have made "buf" invalid!
1407 
1408 	update_curbuf(NOT_VALID);
1409     }
1410     else
1411     {
1412 	rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1413     }
1414     return str;
1415 }
1416 
1417     static VALUE
1418 buffer_aset(VALUE self, VALUE num, VALUE str)
1419 {
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 
1427     static VALUE
1428 buffer_delete(VALUE self, VALUE num)
1429 {
1430     buf_T	*buf = get_buf(self);
1431     long	n = NUM2LONG(num);
1432     aco_save_T	aco;
1433 
1434     if (n > 0 && n <= buf->b_ml.ml_line_count)
1435     {
1436 	// set curwin/curbuf for "buf" and save some things
1437 	aucmd_prepbuf(&aco, buf);
1438 
1439 	if (u_savedel(n, 1) == OK)
1440 	{
1441 	    ml_delete(n, 0);
1442 
1443 	    // Changes to non-active buffers should properly refresh
1444 	    //   SegPhault - 01/09/05
1445 	    deleted_lines_mark(n, 1L);
1446 
1447 	    changed();
1448 	}
1449 
1450 	// restore curwin/curbuf and a few other things
1451 	aucmd_restbuf(&aco);
1452 	// Careful: autocommands may have made "buf" invalid!
1453 
1454 	update_curbuf(NOT_VALID);
1455     }
1456     else
1457     {
1458 	rb_raise(rb_eIndexError, "line number %ld out of range", n);
1459     }
1460     return Qnil;
1461 }
1462 
1463     static VALUE
1464 buffer_append(VALUE self, VALUE num, VALUE str)
1465 {
1466     buf_T	*buf = get_buf(self);
1467     char	*line = StringValuePtr(str);
1468     long	n = NUM2LONG(num);
1469     aco_save_T	aco;
1470 
1471     if (line == NULL)
1472     {
1473 	rb_raise(rb_eIndexError, "NULL line");
1474     }
1475     else if (n >= 0 && n <= buf->b_ml.ml_line_count)
1476     {
1477 	// set curwin/curbuf for "buf" and save some things
1478 	aucmd_prepbuf(&aco, buf);
1479 
1480 	if (u_inssub(n + 1) == OK)
1481 	{
1482 	    ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
1483 
1484 	    //  Changes to non-active buffers should properly refresh screen
1485 	    //    SegPhault - 12/20/04
1486 	    appended_lines_mark(n, 1L);
1487 
1488 	    changed();
1489 	}
1490 
1491 	// restore curwin/curbuf and a few other things
1492 	aucmd_restbuf(&aco);
1493 	// Careful: autocommands may have made "buf" invalid!
1494 
1495 	update_curbuf(NOT_VALID);
1496     }
1497     else
1498     {
1499 	rb_raise(rb_eIndexError, "line number %ld out of range", n);
1500     }
1501     return str;
1502 }
1503 
1504 #ifdef USE_TYPEDDATA
1505 static size_t window_dsize(const void *buf);
1506 
1507 static const rb_data_type_t window_type = {
1508     "vim_window",
1509     {0, 0, window_dsize,
1510 # if RUBY_VERSION >= 27
1511 	0, 0
1512 # else
1513 	{0, 0}
1514 # endif
1515     },
1516     0, 0,
1517 # ifdef RUBY_TYPED_FREE_IMMEDIATELY
1518     0,
1519 # endif
1520 };
1521 
1522     static size_t
1523 window_dsize(const void *win UNUSED)
1524 {
1525     return sizeof(win_T);
1526 }
1527 #endif
1528 
1529     static VALUE
1530 window_new(win_T *win)
1531 {
1532     if (win->w_ruby_ref)
1533     {
1534 	return (VALUE) win->w_ruby_ref;
1535     }
1536     else
1537     {
1538 #ifdef USE_TYPEDDATA
1539 	VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1540 #else
1541 	VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
1542 #endif
1543 	win->w_ruby_ref = (void *) obj;
1544 	rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1545 	return obj;
1546     }
1547 }
1548 
1549     static win_T *
1550 get_win(VALUE obj)
1551 {
1552     win_T *win;
1553 
1554 #ifdef USE_TYPEDDATA
1555     TypedData_Get_Struct(obj, win_T, &window_type, win);
1556 #else
1557     Data_Get_Struct(obj, win_T, win);
1558 #endif
1559     if (win == NULL)
1560 	rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1561     return win;
1562 }
1563 
1564     static VALUE
1565 window_s_current(void)
1566 {
1567     return window_new(curwin);
1568 }
1569 
1570 /*
1571  * Added line manipulation functions
1572  *    SegPhault - 03/07/05
1573  */
1574     static VALUE
1575 line_s_current(void)
1576 {
1577     return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1578 }
1579 
1580     static VALUE
1581 set_current_line(VALUE self UNUSED, VALUE str)
1582 {
1583     return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1584 }
1585 
1586     static VALUE
1587 current_line_number(void)
1588 {
1589     return INT2FIX((int)curwin->w_cursor.lnum);
1590 }
1591 
1592     static VALUE
1593 window_s_count(void)
1594 {
1595     win_T	*w;
1596     int n = 0;
1597 
1598     FOR_ALL_WINDOWS(w)
1599 	n++;
1600     return INT2NUM(n);
1601 }
1602 
1603     static VALUE
1604 window_s_aref(VALUE self UNUSED, VALUE num)
1605 {
1606     win_T *w;
1607     int n = NUM2INT(num);
1608 
1609     for (w = firstwin; w != NULL; w = w->w_next, --n)
1610 	if (n == 0)
1611 	    return window_new(w);
1612     return Qnil;
1613 }
1614 
1615     static VALUE
1616 window_buffer(VALUE self)
1617 {
1618     win_T *win = get_win(self);
1619 
1620     return buffer_new(win->w_buffer);
1621 }
1622 
1623     static VALUE
1624 window_height(VALUE self)
1625 {
1626     win_T *win = get_win(self);
1627 
1628     return INT2NUM(win->w_height);
1629 }
1630 
1631     static VALUE
1632 window_set_height(VALUE self, VALUE height)
1633 {
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 
1643     static VALUE
1644 window_width(VALUE self UNUSED)
1645 {
1646     return INT2NUM(get_win(self)->w_width);
1647 }
1648 
1649     static VALUE
1650 window_set_width(VALUE self UNUSED, VALUE width)
1651 {
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 
1661     static VALUE
1662 window_cursor(VALUE self)
1663 {
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 
1669     static VALUE
1670 window_set_cursor(VALUE self, VALUE pos)
1671 {
1672     VALUE lnum, col;
1673     win_T *win = get_win(self);
1674 
1675     Check_Type(pos, T_ARRAY);
1676     if (RARRAY_LEN(pos) != 2)
1677 	rb_raise(rb_eArgError, "array length must be 2");
1678     lnum = RARRAY_PTR(pos)[0];
1679     col = RARRAY_PTR(pos)[1];
1680     win->w_cursor.lnum = NUM2LONG(lnum);
1681     win->w_cursor.col = NUM2UINT(col);
1682     win->w_set_curswant = TRUE;
1683     check_cursor();		    // put cursor on an existing line
1684     update_screen(NOT_VALID);
1685     return Qnil;
1686 }
1687 
1688     static VALUE
1689 f_nop(VALUE self UNUSED)
1690 {
1691     return Qnil;
1692 }
1693 
1694     static VALUE
1695 f_p(int argc, VALUE *argv, VALUE self UNUSED)
1696 {
1697     int i;
1698     VALUE str = rb_str_new("", 0);
1699     VALUE ret = Qnil;
1700 
1701     for (i = 0; i < argc; i++)
1702     {
1703 	if (i > 0) rb_str_cat(str, ", ", 2);
1704 	rb_str_concat(str, rb_inspect(argv[i]));
1705     }
1706     msg(RSTRING_PTR(str));
1707 
1708     if (argc == 1)
1709 	ret = argv[0];
1710     else if (argc > 1)
1711 	ret = rb_ary_new4(argc, argv);
1712     return ret;
1713 }
1714 
1715     static void
1716 ruby_io_init(void)
1717 {
1718 #ifndef DYNAMIC_RUBY
1719     RUBYEXTERN VALUE rb_stdout;
1720     RUBYEXTERN VALUE rb_stderr;
1721 #endif
1722 
1723     rb_stdout = rb_obj_alloc(rb_cObject);
1724     rb_stderr = rb_obj_alloc(rb_cObject);
1725     rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
1726     rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
1727     rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1728     rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
1729     rb_define_global_function("p", f_p, -1);
1730 }
1731 
1732     static void
1733 ruby_vim_init(void)
1734 {
1735     objtbl = rb_hash_new();
1736     rb_global_variable(&objtbl);
1737 
1738     // The Vim module used to be called "VIM", but "Vim" is better.  Make an
1739     // alias "VIM" for backwards compatibility.
1740     mVIM = rb_define_module("Vim");
1741     rb_define_const(rb_cObject, "VIM", mVIM);
1742     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);
1754     rb_define_module_function(mVIM, "blob", vim_blob, 1);
1755 
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 
1774     // Added line manipulation functions
1775     //   SegPhault - 03/07/05
1776     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 
1781     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);
1788     rb_define_method(cVimWindow, "width", window_width, 0);
1789     rb_define_method(cVimWindow, "width=", window_set_width, 1);
1790     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 }
1796 
1797     void
1798 vim_ruby_init(void *stack_start)
1799 {
1800     // should get machine stack start address early in main function
1801     ruby_stack_start = stack_start;
1802 }
1803 
1804     static int
1805 convert_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
1821 ruby_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:
1830 	    rettv->v_type = VAR_BOOL;
1831 	    rettv->vval.v_number = VVAL_TRUE;
1832 	    break;
1833 	case T_FALSE:
1834 	    rettv->v_type = VAR_BOOL;
1835 	    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
1912 do_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 }
1933