xref: /vim-8.2.3635/src/if_ruby.c (revision a0122dcd)
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 // suggested by Ariya Mizutani
36 #if (_MSC_VER == 1200)
37 # undef _WIN32_WINNT
38 #endif
39 
40 #ifdef DYNAMIC_RUBY
41 /*
42  * This is tricky.  In ruby.h there is (inline) function rb_class_of()
43  * definition.  This function use these variables.  But we want function to
44  * use dll_* variables.
45  */
46 # if RUBY_VERSION >= 24
47 #  define USE_RUBY_INTEGER
48 # endif
49 
50 # define rb_cFalseClass		(*dll_rb_cFalseClass)
51 # define rb_cFixnum		(*dll_rb_cFixnum)
52 # if defined(USE_RUBY_INTEGER)
53 #  define rb_cInteger		(*dll_rb_cInteger)
54 # endif
55 # if RUBY_VERSION >= 20
56 #  define rb_cFloat		(*dll_rb_cFloat)
57 # endif
58 # define rb_cNilClass		(*dll_rb_cNilClass)
59 # define rb_cString		(*dll_rb_cString)
60 # define rb_cSymbol		(*dll_rb_cSymbol)
61 # define rb_cTrueClass		(*dll_rb_cTrueClass)
62 
63 # if RUBY_VERSION >= 18
64 /*
65  * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
66  * in ruby.h.  But it causes trouble for these variables, because it is
67  * defined in this file.  When defined this RUBY_EXPORT it modified to
68  * "extern" and be able to avoid this problem.
69  */
70 #  define RUBY_EXPORT
71 # endif
72 
73 # if RUBY_VERSION >= 19
74 // Ruby 1.9 defines a number of static functions which use rb_num2long and
75 // rb_int2big
76 #  define rb_num2long rb_num2long_stub
77 #  define rb_int2big rb_int2big_stub
78 
79 #  if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
80 // Ruby 1.9 defines a number of static functions which use rb_fix2int and
81 // rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit)
82 #   define rb_fix2int rb_fix2int_stub
83 #   define rb_num2int rb_num2int_stub
84 #  endif
85 # endif
86 
87 # if RUBY_VERSION == 21
88 // Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
89 // rb_gc_writebarrier_unprotect_promoted if USE_RGENGC
90 #  define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
91 # endif
92 
93 # if RUBY_VERSION >= 22
94 #  define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
95 # endif
96 
97 # if RUBY_VERSION >= 26
98 #  define rb_ary_detransient rb_ary_detransient_stub
99 # endif
100 
101 # if RUBY_VERSION >= 30
102 #  define rb_check_type rb_check_type_stub
103 #  define rb_num2uint rb_num2uint_stub
104 #  define ruby_malloc_size_overflow ruby_malloc_size_overflow_stub
105 # endif
106 
107 #endif  // ifdef DYNAMIC_RUBY
108 
109 // On macOS pre-installed Ruby defines "SIZEOF_TIME_T" as "SIZEOF_LONG" so it
110 // conflicts with the definition in config.h then causes a macro-redefined
111 // warning.
112 #ifdef SIZEOF_TIME_T
113 # undef SIZEOF_TIME_T
114 #endif
115 
116 #include <ruby.h>
117 #if RUBY_VERSION >= 19
118 # include <ruby/encoding.h>
119 #endif
120 #if RUBY_VERSION <= 18
121 # include <st.h>  // for ST_STOP and ST_CONTINUE
122 #endif
123 
124 // See above.
125 #ifdef SIZEOF_TIME_T
126 # undef SIZEOF_TIME_T
127 #endif
128 
129 #undef off_t	// ruby defines off_t as _int64, Mingw uses long
130 #undef EXTERN
131 #undef _
132 
133 // T_DATA defined both by Ruby and Mac header files, hack around it...
134 #if defined(MACOS_X)
135 # define __OPENTRANSPORT__
136 # define __OPENTRANSPORTPROTOCOL__
137 # define __OPENTRANSPORTPROVIDERS__
138 #endif
139 
140 /*
141  * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
142  * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
143  * The old Data_XXX macro family was deprecated on Ruby 2.2.
144  * Use TypedData_XXX if available.
145  */
146 #if defined(TypedData_Wrap_Struct) && (RUBY_VERSION >= 20)
147 # define USE_TYPEDDATA	1
148 #endif
149 
150 /*
151  * Backward compatibility for Ruby 1.8 and earlier.
152  * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
153  * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
154  * RXXX_LEN(s) and RXXX_PTR(s) are provided.
155  */
156 #ifndef StringValuePtr
157 # define StringValuePtr(s) STR2CSTR(s)
158 #endif
159 #ifndef RARRAY_LEN
160 # define RARRAY_LEN(s) RARRAY(s)->len
161 #endif
162 #ifndef RARRAY_PTR
163 # define RARRAY_PTR(s) RARRAY(s)->ptr
164 #endif
165 #ifndef RSTRING_LEN
166 # define RSTRING_LEN(s) RSTRING(s)->len
167 #endif
168 #ifndef RSTRING_PTR
169 # define RSTRING_PTR(s) RSTRING(s)->ptr
170 #endif
171 
172 #ifdef HAVE_DUP
173 # undef HAVE_DUP
174 #endif
175 
176 #include "vim.h"
177 #include "version.h"
178 
179 #ifdef DYNAMIC_RUBY
180 # if !defined(MSWIN)  // must come after including vim.h, where it is defined
181 #  include <dlfcn.h>
182 #  define HINSTANCE void*
183 #  define RUBY_PROC void*
184 #  define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
185 #  define symbol_from_dll dlsym
186 #  define close_dll dlclose
187 # else
188 #  define RUBY_PROC FARPROC
189 #  define load_dll vimLoadLib
190 #  define symbol_from_dll GetProcAddress
191 #  define close_dll FreeLibrary
192 # endif
193 #endif
194 
195 #if defined(PROTO) && !defined(FEAT_RUBY)
196 // Define these to be able to generate the function prototypes.
197 # define VALUE int
198 # define RUBY_DATA_FUNC int
199 #endif
200 
201 static int ruby_initialized = 0;
202 static void *ruby_stack_start;
203 static VALUE objtbl;
204 
205 static VALUE mVIM;
206 static VALUE cBuffer;
207 static VALUE cVimWindow;
208 static VALUE eDeletedBufferError;
209 static VALUE eDeletedWindowError;
210 
211 static int ensure_ruby_initialized(void);
212 static void error_print(int);
213 static void ruby_io_init(void);
214 static void ruby_vim_init(void);
215 static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
216 
217 #if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
218 # if defined(__ia64) && !defined(ruby_init_stack)
219 #  define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
220 # endif
221 #endif
222 
223 #if defined(DYNAMIC_RUBY) || defined(PROTO)
224 # if defined(PROTO) && !defined(HINSTANCE)
225 #  define HINSTANCE int		// for generating prototypes
226 # endif
227 
228 /*
229  * Wrapper defines
230  */
231 // Ruby 2.7 actually expands the following symbols as macro.
232 # if RUBY_VERSION >= 27
233 #  undef rb_define_global_function
234 #  undef rb_define_method
235 #  undef rb_define_module_function
236 #  undef rb_define_singleton_method
237 # endif
238 
239 # define rb_assoc_new			dll_rb_assoc_new
240 # define rb_cObject			(*dll_rb_cObject)
241 # define rb_class_new_instance		dll_rb_class_new_instance
242 # if RUBY_VERSION < 30
243 #  define rb_check_type			dll_rb_check_type
244 # endif
245 # ifdef USE_TYPEDDATA
246 #  define rb_check_typeddata		dll_rb_check_typeddata
247 # endif
248 # define rb_class_path			dll_rb_class_path
249 # ifdef USE_TYPEDDATA
250 #  if RUBY_VERSION >= 23
251 #   define rb_data_typed_object_wrap	dll_rb_data_typed_object_wrap
252 #  else
253 #   define rb_data_typed_object_alloc	dll_rb_data_typed_object_alloc
254 #  endif
255 # else
256 #  define rb_data_object_alloc		dll_rb_data_object_alloc
257 # endif
258 # define rb_define_class_under		dll_rb_define_class_under
259 # define rb_define_const			dll_rb_define_const
260 # define rb_define_global_function	dll_rb_define_global_function
261 # define rb_define_method		dll_rb_define_method
262 # define rb_define_module		dll_rb_define_module
263 # define rb_define_module_function	dll_rb_define_module_function
264 # define rb_define_singleton_method	dll_rb_define_singleton_method
265 # define rb_define_virtual_variable	dll_rb_define_virtual_variable
266 # define rb_stdout			(*dll_rb_stdout)
267 # define rb_stderr			(*dll_rb_stderr)
268 # define rb_eArgError			(*dll_rb_eArgError)
269 # define rb_eIndexError			(*dll_rb_eIndexError)
270 # define rb_eRuntimeError		(*dll_rb_eRuntimeError)
271 # define rb_eStandardError		(*dll_rb_eStandardError)
272 # define rb_eval_string_protect		dll_rb_eval_string_protect
273 # if RUBY_VERSION >= 21
274 #  define rb_funcallv			dll_rb_funcallv
275 # else
276 #  define rb_funcall2			dll_rb_funcall2
277 # endif
278 # define rb_global_variable		dll_rb_global_variable
279 # define rb_hash_aset			dll_rb_hash_aset
280 # define rb_hash_foreach		dll_rb_hash_foreach
281 # define rb_hash_new			dll_rb_hash_new
282 # define rb_inspect			dll_rb_inspect
283 # define rb_int2inum			dll_rb_int2inum
284 
285 // ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
286 // work.  Not using the cache appears to be the best solution.
287 # undef rb_intern
288 # define rb_intern			dll_rb_intern
289 
290 # if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
291 #  if RUBY_VERSION <= 18
292 #   define rb_fix2int			dll_rb_fix2int
293 #   define rb_num2int			dll_rb_num2int
294 #  endif
295 #  if RUBY_VERSION < 30
296 #   define rb_num2uint			dll_rb_num2uint
297 #  endif
298 # endif
299 # define rb_num2dbl			dll_rb_num2dbl
300 # define rb_lastline_get			dll_rb_lastline_get
301 # define rb_lastline_set			dll_rb_lastline_set
302 # define rb_protect			dll_rb_protect
303 # define rb_load			dll_rb_load
304 # if RUBY_VERSION <= 18
305 #  define rb_num2long			dll_rb_num2long
306 # endif
307 # if RUBY_VERSION <= 19
308 #  define rb_num2ulong			dll_rb_num2ulong
309 # endif
310 # define rb_obj_alloc			dll_rb_obj_alloc
311 # define rb_obj_as_string		dll_rb_obj_as_string
312 # define rb_obj_id			dll_rb_obj_id
313 # define rb_raise			dll_rb_raise
314 # define rb_str_cat			dll_rb_str_cat
315 # define rb_str_concat			dll_rb_str_concat
316 # undef rb_str_new
317 # define rb_str_new			dll_rb_str_new
318 # ifdef rb_str_new2
319 // Ruby may #define rb_str_new2 to use rb_str_new_cstr.
320 #  define need_rb_str_new_cstr 1
321 // Ruby's headers #define rb_str_new_cstr to make use of GCC's
322 // __builtin_constant_p extension.
323 #  undef rb_str_new_cstr
324 #  define rb_str_new_cstr		dll_rb_str_new_cstr
325 # else
326 #  define rb_str_new2			dll_rb_str_new2
327 # endif
328 # if RUBY_VERSION >= 18
329 #  define rb_string_value		dll_rb_string_value
330 #  define rb_string_value_ptr		dll_rb_string_value_ptr
331 #  define rb_float_new			dll_rb_float_new
332 #  define rb_ary_new			dll_rb_ary_new
333 #  ifdef rb_ary_new4
334 #   define RB_ARY_NEW4_MACRO 1
335 #   undef rb_ary_new4
336 #  endif
337 #  define rb_ary_new4			dll_rb_ary_new4
338 #  define rb_ary_push			dll_rb_ary_push
339 #  if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
340 #   ifdef __ia64
341 #    define rb_ia64_bsp			dll_rb_ia64_bsp
342 #    undef ruby_init_stack
343 #    define ruby_init_stack(addr)	dll_ruby_init_stack((addr), rb_ia64_bsp())
344 #   else
345 #    define ruby_init_stack		dll_ruby_init_stack
346 #   endif
347 #  endif
348 # else
349 #  define rb_str2cstr			dll_rb_str2cstr
350 # endif
351 # if RUBY_VERSION >= 19
352 #  define rb_errinfo			dll_rb_errinfo
353 # else
354 #  define ruby_errinfo			(*dll_ruby_errinfo)
355 # endif
356 # define ruby_init			dll_ruby_init
357 # define ruby_init_loadpath		dll_ruby_init_loadpath
358 # ifdef MSWIN
359 #  if RUBY_VERSION >= 19
360 #   define ruby_sysinit			dll_ruby_sysinit
361 #  else
362 #   define NtInitialize			dll_NtInitialize
363 #  endif
364 #  if RUBY_VERSION >= 18
365 #   define rb_w32_snprintf		dll_rb_w32_snprintf
366 #  endif
367 # endif
368 
369 # if RUBY_VERSION >= 19
370 #  define ruby_script			dll_ruby_script
371 #  define rb_enc_find_index		dll_rb_enc_find_index
372 #  define rb_enc_find			dll_rb_enc_find
373 #  undef rb_enc_str_new
374 #  define rb_enc_str_new		dll_rb_enc_str_new
375 #  define rb_sprintf			dll_rb_sprintf
376 #  define rb_require			dll_rb_require
377 #  define ruby_options			dll_ruby_options
378 # endif
379 
380 /*
381  * Pointers for dynamic link
382  */
383 static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
384 VALUE *dll_rb_cFalseClass;
385 VALUE *dll_rb_cFixnum;
386 # if defined(USE_RUBY_INTEGER)
387 VALUE *dll_rb_cInteger;
388 # endif
389 # if RUBY_VERSION >= 20
390 VALUE *dll_rb_cFloat;
391 # endif
392 VALUE *dll_rb_cNilClass;
393 static VALUE *dll_rb_cObject;
394 VALUE *dll_rb_cString;
395 VALUE *dll_rb_cSymbol;
396 VALUE *dll_rb_cTrueClass;
397 static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
398 static void (*dll_rb_check_type) (VALUE,int);
399 # ifdef USE_TYPEDDATA
400 static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
401 # endif
402 static VALUE (*dll_rb_class_path) (VALUE);
403 # ifdef USE_TYPEDDATA
404 #  if RUBY_VERSION >= 23
405 static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
406 #  else
407 static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
408 #  endif
409 # else
410 static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
411 # endif
412 static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
413 static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
414 static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
415 static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
416 static VALUE (*dll_rb_define_module) (const char*);
417 static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
418 static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
419 static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
420 static VALUE *dll_rb_stdout;
421 static VALUE *dll_rb_stderr;
422 static VALUE *dll_rb_eArgError;
423 static VALUE *dll_rb_eIndexError;
424 static VALUE *dll_rb_eRuntimeError;
425 static VALUE *dll_rb_eStandardError;
426 static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
427 # if RUBY_VERSION >= 21
428 static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
429 # else
430 static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
431 # endif
432 static void (*dll_rb_global_variable) (VALUE*);
433 static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
434 static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
435 static VALUE (*dll_rb_hash_new) (void);
436 static VALUE (*dll_rb_inspect) (VALUE);
437 static VALUE (*dll_rb_int2inum) (long);
438 static ID (*dll_rb_intern) (const char*);
439 # if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
440 static long (*dll_rb_fix2int) (VALUE);
441 static long (*dll_rb_num2int) (VALUE);
442 static unsigned long (*dll_rb_num2uint) (VALUE);
443 # endif
444 static double (*dll_rb_num2dbl) (VALUE);
445 static VALUE (*dll_rb_lastline_get) (void);
446 static void (*dll_rb_lastline_set) (VALUE);
447 static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
448 static void (*dll_rb_load) (VALUE, int);
449 static long (*dll_rb_num2long) (VALUE);
450 static unsigned long (*dll_rb_num2ulong) (VALUE);
451 static VALUE (*dll_rb_obj_alloc) (VALUE);
452 static VALUE (*dll_rb_obj_as_string) (VALUE);
453 static VALUE (*dll_rb_obj_id) (VALUE);
454 static void (*dll_rb_raise) (VALUE, const char*, ...);
455 # if RUBY_VERSION >= 18
456 static VALUE (*dll_rb_string_value) (volatile VALUE*);
457 # else
458 static char *(*dll_rb_str2cstr) (VALUE,int*);
459 # endif
460 static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
461 static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
462 static VALUE (*dll_rb_str_new) (const char*, long);
463 # ifdef need_rb_str_new_cstr
464 // Ruby may #define rb_str_new2 to use rb_str_new_cstr.
465 static VALUE (*dll_rb_str_new_cstr) (const char*);
466 # else
467 static VALUE (*dll_rb_str_new2) (const char*);
468 # endif
469 # if RUBY_VERSION >= 19
470 static VALUE (*dll_rb_errinfo) (void);
471 # else
472 static VALUE *dll_ruby_errinfo;
473 # endif
474 static void (*dll_ruby_init) (void);
475 static void (*dll_ruby_init_loadpath) (void);
476 # ifdef MSWIN
477 #  if RUBY_VERSION >= 19
478 static void (*dll_ruby_sysinit) (int*, char***);
479 #  else
480 static void (*dll_NtInitialize) (int*, char***);
481 #  endif
482 #  if RUBY_VERSION >= 18
483 static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
484 #  endif
485 # endif
486 # if RUBY_VERSION >= 18
487 static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
488 static VALUE (*dll_rb_float_new) (double);
489 static VALUE (*dll_rb_ary_new) (void);
490 static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
491 static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
492 #  if RUBY_VERSION >= 26
493 static void (*dll_rb_ary_detransient) (VALUE);
494 #  endif
495 #  if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
496 #   ifdef __ia64
497 static void * (*dll_rb_ia64_bsp) (void);
498 static void (*dll_ruby_init_stack)(VALUE*, void*);
499 #   else
500 static void (*dll_ruby_init_stack)(VALUE*);
501 #   endif
502 #  endif
503 # endif
504 # if RUBY_VERSION >= 19
505 static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
506 # endif
507 
508 # if RUBY_VERSION >= 19
509 static void (*dll_ruby_script) (const char*);
510 static int (*dll_rb_enc_find_index) (const char*);
511 static rb_encoding* (*dll_rb_enc_find) (const char*);
512 static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
513 static VALUE (*dll_rb_sprintf) (const char*, ...);
514 static VALUE (*dll_rb_require) (const char*);
515 static void* (*dll_ruby_options)(int, char**);
516 # endif
517 
518 # if defined(USE_RGENGC) && USE_RGENGC
519 #  if RUBY_VERSION == 21
520 static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
521 #  else
522 static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
523 #  endif
524 # endif
525 
526 # if RUBY_VERSION >= 30
527 NORETURN(static void (*dll_ruby_malloc_size_overflow)(size_t, size_t));
528 # endif
529 
530 # if RUBY_VERSION >= 26
531 void rb_ary_detransient_stub(VALUE x);
532 # endif
533 
534 // Do not generate a prototype here, VALUE isn't always defined.
535 # ifndef PROTO
536 #  if RUBY_VERSION >= 19
537 #   if RUBY_VERSION >= 22
538     long
539 rb_num2long_stub(VALUE x)
540 #   else
541     SIGNED_VALUE
542 rb_num2long_stub(VALUE x)
543 #   endif
544 {
545     return dll_rb_num2long(x);
546 }
547 #   if RUBY_VERSION >= 26
548     VALUE
549 rb_int2big_stub(intptr_t x)
550 #   else
551     VALUE
552 rb_int2big_stub(SIGNED_VALUE x)
553 #   endif
554 {
555     return dll_rb_int2big(x);
556 }
557 #   if VIM_SIZEOF_INT < VIM_SIZEOF_LONG
558     long
559 rb_fix2int_stub(VALUE x)
560 {
561     return dll_rb_fix2int(x);
562 }
563     long
564 rb_num2int_stub(VALUE x)
565 {
566     return dll_rb_num2int(x);
567 }
568 #   endif
569 #   if RUBY_VERSION >= 20
570     VALUE
571 rb_float_new_in_heap(double d)
572 {
573     return dll_rb_float_new(d);
574 }
575 #    if RUBY_VERSION >= 22
576     unsigned long
577 rb_num2ulong(VALUE x)
578 #    else
579     VALUE
580 rb_num2ulong(VALUE x)
581 #    endif
582 {
583     return (long)RSHIFT((SIGNED_VALUE)(x),1);
584 }
585 #   endif
586 #  endif
587 #  if defined(USE_RGENGC) && USE_RGENGC
588 #   if RUBY_VERSION == 21
589     void
590 rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
591 {
592     dll_rb_gc_writebarrier_unprotect_promoted(obj);
593 }
594 #   else
595     void
596 rb_gc_writebarrier_unprotect_stub(VALUE obj)
597 {
598     dll_rb_gc_writebarrier_unprotect(obj);
599 }
600 #   endif
601 #  endif
602 #  if RUBY_VERSION >= 26
603     void
604 rb_ary_detransient_stub(VALUE x)
605 {
606     dll_rb_ary_detransient(x);
607 }
608 #  endif
609 #  if RUBY_VERSION >= 30
610     void
611 rb_check_type_stub(VALUE obj, int t)
612 {
613     dll_rb_check_type(obj, t);
614 }
615 #   if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
616     unsigned long
617 rb_num2uint_stub(VALUE x)
618 {
619     return dll_rb_num2uint(x);
620 }
621 #   endif
622     void
623 ruby_malloc_size_overflow_stub(size_t x, size_t y)
624 {
625     dll_ruby_malloc_size_overflow(x, y);
626 }
627 #  endif
628 # endif // ifndef PROTO
629 
630 static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
631 
632 /*
633  * Table of name to function pointer of ruby.
634  */
635 static struct
636 {
637     char *name;
638     RUBY_PROC *ptr;
639 } ruby_funcname_table[] =
640 {
641     {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
642     {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
643 # if defined(USE_RUBY_INTEGER)
644     {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
645 # else
646     {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
647 # endif
648 # if RUBY_VERSION >= 20
649     {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
650 # endif
651     {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
652     {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
653     {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
654     {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
655     {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
656     {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
657     {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
658 # ifdef USE_TYPEDDATA
659     {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
660 # endif
661     {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
662 # ifdef USE_TYPEDDATA
663 #  if RUBY_VERSION >= 23
664     {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
665 #  else
666     {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
667 #  endif
668 # else
669     {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
670 # endif
671     {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
672     {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
673     {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
674     {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
675     {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
676     {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
677     {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
678     {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
679     {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
680     {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
681     {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
682     {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
683     {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
684     {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
685     {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
686 # if RUBY_VERSION >= 21
687     {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
688 # else
689     {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
690 # endif
691     {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
692     {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
693     {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
694     {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
695     {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
696     {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
697     {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
698 # if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
699     {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
700     {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
701     {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
702 # endif
703     {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
704     {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
705     {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
706     {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
707     {"rb_load", (RUBY_PROC*)&dll_rb_load},
708     {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
709     {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
710     {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
711     {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
712     {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
713     {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
714 # if RUBY_VERSION >= 18
715     {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
716 # else
717     {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
718 # endif
719     {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
720     {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
721     {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
722 # ifdef need_rb_str_new_cstr
723     {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
724 # else
725     {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
726 # endif
727 # if RUBY_VERSION >= 19
728     {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
729 # else
730     {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
731 # endif
732     {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
733     {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
734 # ifdef MSWIN
735 #  if RUBY_VERSION >= 19
736     {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
737 #  else
738     {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
739 #  endif
740 #  if RUBY_VERSION >= 18
741     {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
742 #  endif
743 # endif
744 # if RUBY_VERSION >= 18
745     {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
746 #  if RUBY_VERSION <= 19
747     {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
748 #  else
749     {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
750 #  endif
751     {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
752 #  ifdef RB_ARY_NEW4_MACRO
753     {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
754 #  else
755     {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
756 #  endif
757     {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
758 #  if RUBY_VERSION >= 26
759     {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
760 #  endif
761 # endif
762 # if RUBY_VERSION >= 19
763     {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
764     {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
765     {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
766     {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
767     {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
768     {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
769     {"rb_require", (RUBY_PROC*)&dll_rb_require},
770     {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
771 # endif
772 # if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
773 #  ifdef __ia64
774     {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
775 #  endif
776     {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
777 # endif
778 # if defined(USE_RGENGC) && USE_RGENGC
779 #  if RUBY_VERSION == 21
780     {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
781 #  else
782     {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
783 #  endif
784 # endif
785 # if RUBY_VERSION >= 30
786     {"ruby_malloc_size_overflow", (RUBY_PROC*)&dll_ruby_malloc_size_overflow},
787 # endif
788     {"", NULL},
789 };
790 
791 /*
792  * Load library and get all pointers.
793  * Parameter 'libname' provides name of DLL.
794  * Return OK or FAIL.
795  */
796     static int
797 ruby_runtime_link_init(char *libname, int verbose)
798 {
799     int i;
800 
801     if (hinstRuby)
802 	return OK;
803     hinstRuby = load_dll(libname);
804     if (!hinstRuby)
805     {
806 	if (verbose)
807 	    semsg(_(e_loadlib), libname);
808 	return FAIL;
809     }
810 
811     for (i = 0; ruby_funcname_table[i].ptr; ++i)
812     {
813 	if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
814 			ruby_funcname_table[i].name)))
815 	{
816 	    close_dll(hinstRuby);
817 	    hinstRuby = NULL;
818 	    if (verbose)
819 		semsg(_(e_loadfunc), ruby_funcname_table[i].name);
820 	    return FAIL;
821 	}
822     }
823     return OK;
824 }
825 
826 /*
827  * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
828  * else FALSE.
829  */
830     int
831 ruby_enabled(int verbose)
832 {
833     return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
834 }
835 #endif // defined(DYNAMIC_RUBY) || defined(PROTO)
836 
837     void
838 ruby_end(void)
839 {
840 }
841 
842     void
843 ex_ruby(exarg_T *eap)
844 {
845     int state;
846     char *script = NULL;
847 
848     script = (char *)script_get(eap, eap->arg);
849     if (!eap->skip && ensure_ruby_initialized())
850     {
851 	if (script == NULL)
852 	    rb_eval_string_protect((char *)eap->arg, &state);
853 	else
854 	    rb_eval_string_protect(script, &state);
855 	if (state)
856 	    error_print(state);
857     }
858     vim_free(script);
859 }
860 
861 /*
862  *  In Ruby 1.9 or later, ruby String object has encoding.
863  *  conversion buffer string of vim to ruby String object using
864  *  VIM encoding option.
865  */
866     static VALUE
867 vim_str2rb_enc_str(const char *s)
868 {
869 #if RUBY_VERSION >= 19
870     long lval;
871     char_u *sval;
872     rb_encoding *enc;
873 
874     if (get_option_value((char_u *)"enc", &lval, &sval, 0) == gov_string)
875     {
876 	enc = rb_enc_find((char *)sval);
877 	vim_free(sval);
878 	if (enc)
879 	    return rb_enc_str_new(s, (long)strlen(s), enc);
880     }
881 #endif
882     return rb_str_new2(s);
883 }
884 
885     static VALUE
886 eval_enc_string_protect(const char *str, int *state)
887 {
888 #if RUBY_VERSION >= 19
889     long lval;
890     char_u *sval;
891     rb_encoding *enc;
892     VALUE v;
893 
894     if (get_option_value((char_u *)"enc", &lval, &sval, 0) == gov_string)
895     {
896 	enc = rb_enc_find((char *)sval);
897 	vim_free(sval);
898 	if (enc)
899 	{
900 	    v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
901 	    return rb_eval_string_protect(StringValuePtr(v), state);
902 	}
903     }
904 #endif
905     return rb_eval_string_protect(str, state);
906 }
907 
908     void
909 ex_rubydo(exarg_T *eap)
910 {
911     int state;
912     linenr_T i;
913     buf_T   *was_curbuf = curbuf;
914 
915     if (ensure_ruby_initialized())
916     {
917 	if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
918 	    return;
919 	for (i = eap->line1; i <= eap->line2; i++)
920 	{
921 	    VALUE line;
922 
923 	    if (i > curbuf->b_ml.ml_line_count)
924 		break;
925 	    line = vim_str2rb_enc_str((char *)ml_get(i));
926 	    rb_lastline_set(line);
927 	    eval_enc_string_protect((char *) eap->arg, &state);
928 	    if (state)
929 	    {
930 		error_print(state);
931 		break;
932 	    }
933 	    if (was_curbuf != curbuf)
934 		break;
935 	    line = rb_lastline_get();
936 	    if (!NIL_P(line))
937 	    {
938 		if (TYPE(line) != T_STRING)
939 		{
940 		    emsg(_("E265: $_ must be an instance of String"));
941 		    return;
942 		}
943 		ml_replace(i, (char_u *) StringValuePtr(line), 1);
944 		changed();
945 #ifdef SYNTAX_HL
946 		syn_changed(i); // recompute syntax hl. for this line
947 #endif
948 	    }
949 	}
950 	check_cursor();
951 	update_curbuf(NOT_VALID);
952     }
953 }
954 
955     static VALUE
956 rb_load_wrap(VALUE file_to_load)
957 {
958     rb_load(file_to_load, 0);
959     return Qnil;
960 }
961 
962     void
963 ex_rubyfile(exarg_T *eap)
964 {
965     int state;
966 
967     if (ensure_ruby_initialized())
968     {
969 	VALUE file_to_load = rb_str_new2((const char *)eap->arg);
970 	rb_protect(rb_load_wrap, file_to_load, &state);
971 	if (state)
972 	    error_print(state);
973     }
974 }
975 
976     void
977 ruby_buffer_free(buf_T *buf)
978 {
979     if (buf->b_ruby_ref)
980     {
981 	rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
982 	RDATA(buf->b_ruby_ref)->data = NULL;
983     }
984 }
985 
986     void
987 ruby_window_free(win_T *win)
988 {
989     if (win->w_ruby_ref)
990     {
991 	rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
992 	RDATA(win->w_ruby_ref)->data = NULL;
993     }
994 }
995 
996     static int
997 ensure_ruby_initialized(void)
998 {
999     if (!ruby_initialized)
1000     {
1001 #ifdef DYNAMIC_RUBY
1002 	if (ruby_enabled(TRUE))
1003 	{
1004 #endif
1005 #ifdef MSWIN
1006 	    // suggested by Ariya Mizutani
1007 	    int argc = 1;
1008 	    char *argv[] = {"gvim.exe"};
1009 	    char **argvp = argv;
1010 # if RUBY_VERSION >= 19
1011 	    ruby_sysinit(&argc, &argvp);
1012 # else
1013 	    NtInitialize(&argc, &argvp);
1014 # endif
1015 #endif
1016 	    {
1017 #if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
1018 		ruby_init_stack(ruby_stack_start);
1019 #endif
1020 		ruby_init();
1021 	    }
1022 #if RUBY_VERSION >= 19
1023 	    {
1024 		int dummy_argc = 2;
1025 		char *dummy_argv[] = {"vim-ruby", "-e_=0"};
1026 		ruby_options(dummy_argc, dummy_argv);
1027 	    }
1028 	    ruby_script("vim-ruby");
1029 #else
1030 	    ruby_init_loadpath();
1031 #endif
1032 	    ruby_io_init();
1033 	    ruby_vim_init();
1034 	    ruby_initialized = 1;
1035 #ifdef DYNAMIC_RUBY
1036 	}
1037 	else
1038 	{
1039 	    emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
1040 	    return 0;
1041 	}
1042 #endif
1043     }
1044     return ruby_initialized;
1045 }
1046 
1047     static void
1048 error_print(int state)
1049 {
1050 #if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
1051     RUBYEXTERN VALUE ruby_errinfo;
1052 #endif
1053     VALUE error;
1054     VALUE eclass;
1055     VALUE einfo;
1056     VALUE bt;
1057     int attr;
1058     char buff[BUFSIZ];
1059     long i;
1060 
1061 #define TAG_RETURN	0x1
1062 #define TAG_BREAK	0x2
1063 #define TAG_NEXT	0x3
1064 #define TAG_RETRY	0x4
1065 #define TAG_REDO	0x5
1066 #define TAG_RAISE	0x6
1067 #define TAG_THROW	0x7
1068 #define TAG_FATAL	0x8
1069 #define TAG_MASK	0xf
1070 
1071     switch (state)
1072     {
1073 	case TAG_RETURN:
1074 	    emsg(_("E267: unexpected return"));
1075 	    break;
1076 	case TAG_NEXT:
1077 	    emsg(_("E268: unexpected next"));
1078 	    break;
1079 	case TAG_BREAK:
1080 	    emsg(_("E269: unexpected break"));
1081 	    break;
1082 	case TAG_REDO:
1083 	    emsg(_("E270: unexpected redo"));
1084 	    break;
1085 	case TAG_RETRY:
1086 	    emsg(_("E271: retry outside of rescue clause"));
1087 	    break;
1088 	case TAG_RAISE:
1089 	case TAG_FATAL:
1090 #if RUBY_VERSION >= 19
1091 	    error = rb_errinfo();
1092 #else
1093 	    error = ruby_errinfo;
1094 #endif
1095 	    eclass = CLASS_OF(error);
1096 	    einfo = rb_obj_as_string(error);
1097 	    if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1098 	    {
1099 		emsg(_("E272: unhandled exception"));
1100 	    }
1101 	    else
1102 	    {
1103 		VALUE epath;
1104 		char *p;
1105 
1106 		epath = rb_class_path(eclass);
1107 		vim_snprintf(buff, BUFSIZ, "%s: %s",
1108 			 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1109 		p = strchr(buff, '\n');
1110 		if (p) *p = '\0';
1111 		emsg(buff);
1112 	    }
1113 
1114 	    attr = syn_name2attr((char_u *)"Error");
1115 #if RUBY_VERSION >= 21
1116 	    bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1117 	    for (i = 0; i < RARRAY_LEN(bt); i++)
1118 		msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
1119 #else
1120 	    bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1121 	    for (i = 0; i < RARRAY_LEN(bt); i++)
1122 		msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
1123 #endif
1124 	    break;
1125 	default:
1126 	    vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1127 	    emsg(buff);
1128 	    break;
1129     }
1130 }
1131 
1132     static VALUE
1133 vim_message(VALUE self UNUSED, VALUE str)
1134 {
1135     char *buff, *p;
1136 
1137     str = rb_obj_as_string(str);
1138     if (RSTRING_LEN(str) > 0)
1139     {
1140 	// Only do this when the string isn't empty, alloc(0) causes trouble.
1141 	buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
1142 	strcpy(buff, RSTRING_PTR(str));
1143 	p = strchr(buff, '\n');
1144 	if (p) *p = '\0';
1145 	msg(buff);
1146     }
1147     else
1148     {
1149 	msg("");
1150     }
1151     return Qnil;
1152 }
1153 
1154     static VALUE
1155 vim_set_option(VALUE self UNUSED, VALUE str)
1156 {
1157     do_set((char_u *)StringValuePtr(str), 0);
1158     update_screen(NOT_VALID);
1159     return Qnil;
1160 }
1161 
1162     static VALUE
1163 vim_command(VALUE self UNUSED, VALUE str)
1164 {
1165     do_cmdline_cmd((char_u *)StringValuePtr(str));
1166     return Qnil;
1167 }
1168 
1169 #ifdef FEAT_EVAL
1170     static VALUE
1171 vim_to_ruby(typval_T *tv)
1172 {
1173     VALUE result = Qnil;
1174 
1175     if (tv->v_type == VAR_STRING)
1176     {
1177 	result = rb_str_new2(tv->vval.v_string == NULL
1178 					  ? "" : (char *)(tv->vval.v_string));
1179     }
1180     else if (tv->v_type == VAR_NUMBER)
1181     {
1182 	result = INT2NUM(tv->vval.v_number);
1183     }
1184 # ifdef FEAT_FLOAT
1185     else if (tv->v_type == VAR_FLOAT)
1186     {
1187 	result = rb_float_new(tv->vval.v_float);
1188     }
1189 # endif
1190     else if (tv->v_type == VAR_LIST)
1191     {
1192 	list_T      *list = tv->vval.v_list;
1193 	listitem_T  *curr;
1194 
1195 	result = rb_ary_new();
1196 
1197 	if (list != NULL)
1198 	{
1199 	    FOR_ALL_LIST_ITEMS(list, curr)
1200 		rb_ary_push(result, vim_to_ruby(&curr->li_tv));
1201 	}
1202     }
1203     else if (tv->v_type == VAR_DICT)
1204     {
1205 	result = rb_hash_new();
1206 
1207 	if (tv->vval.v_dict != NULL)
1208 	{
1209 	    hashtab_T   *ht = &tv->vval.v_dict->dv_hashtab;
1210 	    long_u      todo = ht->ht_used;
1211 	    hashitem_T  *hi;
1212 	    dictitem_T  *di;
1213 
1214 	    for (hi = ht->ht_array; todo > 0; ++hi)
1215 	    {
1216 		if (!HASHITEM_EMPTY(hi))
1217 		{
1218 		    --todo;
1219 
1220 		    di = dict_lookup(hi);
1221 		    rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
1222 						     vim_to_ruby(&di->di_tv));
1223 		}
1224 	    }
1225 	}
1226     }
1227     else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
1228     {
1229 	if (tv->vval.v_number == VVAL_TRUE)
1230 	    result = Qtrue;
1231 	else if (tv->vval.v_number == VVAL_FALSE)
1232 	    result = Qfalse;
1233     }
1234     else if (tv->v_type == VAR_BLOB)
1235     {
1236 	result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1237 		tv->vval.v_blob->bv_ga.ga_len);
1238     }
1239     // else return Qnil;
1240 
1241     return result;
1242 }
1243 #endif
1244 
1245     static VALUE
1246 vim_evaluate(VALUE self UNUSED, VALUE str)
1247 {
1248 #ifdef FEAT_EVAL
1249     typval_T    *tv;
1250     VALUE       result;
1251 
1252     tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1253     if (tv == NULL)
1254 	return Qnil;
1255     result = vim_to_ruby(tv);
1256 
1257     free_tv(tv);
1258 
1259     return result;
1260 #else
1261     return Qnil;
1262 #endif
1263 }
1264 
1265 #ifdef USE_TYPEDDATA
1266 static size_t buffer_dsize(const void *buf);
1267 
1268 static const rb_data_type_t buffer_type = {
1269     "vim_buffer",
1270     {0, 0, buffer_dsize,
1271 # if RUBY_VERSION >= 27
1272 	0, {0}
1273 # else
1274 	{0, 0}
1275 # endif
1276     },
1277     0, 0,
1278 # ifdef RUBY_TYPED_FREE_IMMEDIATELY
1279     0,
1280 # endif
1281 };
1282 
1283     static size_t
1284 buffer_dsize(const void *buf UNUSED)
1285 {
1286     return sizeof(buf_T);
1287 }
1288 #endif
1289 
1290     static VALUE
1291 buffer_new(buf_T *buf)
1292 {
1293     if (buf->b_ruby_ref)
1294     {
1295 	return (VALUE) buf->b_ruby_ref;
1296     }
1297     else
1298     {
1299 #ifdef USE_TYPEDDATA
1300 	VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1301 #else
1302 	VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
1303 #endif
1304 	buf->b_ruby_ref = (void *) obj;
1305 	rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1306 	return obj;
1307     }
1308 }
1309 
1310     static buf_T *
1311 get_buf(VALUE obj)
1312 {
1313     buf_T *buf;
1314 
1315 #ifdef USE_TYPEDDATA
1316     TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1317 #else
1318     Data_Get_Struct(obj, buf_T, buf);
1319 #endif
1320     if (buf == NULL)
1321 	rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1322     return buf;
1323 }
1324 
1325     static VALUE
1326 vim_blob(VALUE self UNUSED, VALUE str)
1327 {
1328     VALUE result = rb_str_new("0z", 2);
1329     char    buf[4];
1330     int	i;
1331     for (i = 0; i < RSTRING_LEN(str); i++)
1332     {
1333 	sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
1334 	rb_str_concat(result, rb_str_new2(buf));
1335     }
1336     return result;
1337 }
1338 
1339     static VALUE
1340 buffer_s_current(VALUE self UNUSED)
1341 {
1342     return buffer_new(curbuf);
1343 }
1344 
1345     static VALUE
1346 buffer_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
1347 {
1348     return buffer_new(curbuf);
1349 }
1350 
1351     static VALUE
1352 buffer_s_count(VALUE self UNUSED)
1353 {
1354     buf_T *b;
1355     int n = 0;
1356 
1357     FOR_ALL_BUFFERS(b)
1358     {
1359 	//  Deleted buffers should not be counted
1360 	//    SegPhault - 01/07/05
1361 	if (b->b_p_bl)
1362 	    n++;
1363     }
1364 
1365     return INT2NUM(n);
1366 }
1367 
1368     static VALUE
1369 buffer_s_aref(VALUE self UNUSED, VALUE num)
1370 {
1371     buf_T *b;
1372     int n = NUM2INT(num);
1373 
1374     FOR_ALL_BUFFERS(b)
1375     {
1376 	//  Deleted buffers should not be counted
1377 	//    SegPhault - 01/07/05
1378 	if (!b->b_p_bl)
1379 	    continue;
1380 
1381 	if (n == 0)
1382 	    return buffer_new(b);
1383 
1384 	n--;
1385     }
1386     return Qnil;
1387 }
1388 
1389     static VALUE
1390 buffer_name(VALUE self)
1391 {
1392     buf_T *buf = get_buf(self);
1393 
1394     return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
1395 }
1396 
1397     static VALUE
1398 buffer_number(VALUE self)
1399 {
1400     buf_T *buf = get_buf(self);
1401 
1402     return INT2NUM(buf->b_fnum);
1403 }
1404 
1405     static VALUE
1406 buffer_count(VALUE self)
1407 {
1408     buf_T *buf = get_buf(self);
1409 
1410     return INT2NUM(buf->b_ml.ml_line_count);
1411 }
1412 
1413     static VALUE
1414 get_buffer_line(buf_T *buf, linenr_T n)
1415 {
1416     if (n <= 0 || n > buf->b_ml.ml_line_count)
1417 	rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1418     return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
1419 }
1420 
1421     static VALUE
1422 buffer_aref(VALUE self, VALUE num)
1423 {
1424     buf_T *buf = get_buf(self);
1425 
1426     if (buf != NULL)
1427 	return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1428     return Qnil; // For stop warning
1429 }
1430 
1431     static VALUE
1432 set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1433 {
1434     char	*line = StringValuePtr(str);
1435     aco_save_T	aco;
1436 
1437     if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1438     {
1439 	// set curwin/curbuf for "buf" and save some things
1440 	aucmd_prepbuf(&aco, buf);
1441 
1442 	if (u_savesub(n) == OK)
1443 	{
1444 	    ml_replace(n, (char_u *)line, TRUE);
1445 	    changed();
1446 #ifdef SYNTAX_HL
1447 	    syn_changed(n); // recompute syntax hl. for this line
1448 #endif
1449 	}
1450 
1451 	// restore curwin/curbuf and a few other things
1452 	aucmd_restbuf(&aco);
1453 	// Careful: autocommands may have made "buf" invalid!
1454 
1455 	update_curbuf(NOT_VALID);
1456     }
1457     else
1458     {
1459 	rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1460     }
1461     return str;
1462 }
1463 
1464     static VALUE
1465 buffer_aset(VALUE self, VALUE num, VALUE str)
1466 {
1467     buf_T *buf = get_buf(self);
1468 
1469     if (buf != NULL)
1470 	return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1471     return str;
1472 }
1473 
1474     static VALUE
1475 buffer_delete(VALUE self, VALUE num)
1476 {
1477     buf_T	*buf = get_buf(self);
1478     long	n = NUM2LONG(num);
1479     aco_save_T	aco;
1480 
1481     if (n > 0 && n <= buf->b_ml.ml_line_count)
1482     {
1483 	// set curwin/curbuf for "buf" and save some things
1484 	aucmd_prepbuf(&aco, buf);
1485 
1486 	if (u_savedel(n, 1) == OK)
1487 	{
1488 	    ml_delete(n);
1489 
1490 	    // Changes to non-active buffers should properly refresh
1491 	    //   SegPhault - 01/09/05
1492 	    deleted_lines_mark(n, 1L);
1493 
1494 	    changed();
1495 	}
1496 
1497 	// restore curwin/curbuf and a few other things
1498 	aucmd_restbuf(&aco);
1499 	// Careful: autocommands may have made "buf" invalid!
1500 
1501 	update_curbuf(NOT_VALID);
1502     }
1503     else
1504     {
1505 	rb_raise(rb_eIndexError, "line number %ld out of range", n);
1506     }
1507     return Qnil;
1508 }
1509 
1510     static VALUE
1511 buffer_append(VALUE self, VALUE num, VALUE str)
1512 {
1513     buf_T	*buf = get_buf(self);
1514     char	*line = StringValuePtr(str);
1515     long	n = NUM2LONG(num);
1516     aco_save_T	aco;
1517 
1518     if (line == NULL)
1519     {
1520 	rb_raise(rb_eIndexError, "NULL line");
1521     }
1522     else if (n >= 0 && n <= buf->b_ml.ml_line_count)
1523     {
1524 	// set curwin/curbuf for "buf" and save some things
1525 	aucmd_prepbuf(&aco, buf);
1526 
1527 	if (u_inssub(n + 1) == OK)
1528 	{
1529 	    ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
1530 
1531 	    //  Changes to non-active buffers should properly refresh screen
1532 	    //    SegPhault - 12/20/04
1533 	    appended_lines_mark(n, 1L);
1534 
1535 	    changed();
1536 	}
1537 
1538 	// restore curwin/curbuf and a few other things
1539 	aucmd_restbuf(&aco);
1540 	// Careful: autocommands may have made "buf" invalid!
1541 
1542 	update_curbuf(NOT_VALID);
1543     }
1544     else
1545     {
1546 	rb_raise(rb_eIndexError, "line number %ld out of range", n);
1547     }
1548     return str;
1549 }
1550 
1551 #ifdef USE_TYPEDDATA
1552 static size_t window_dsize(const void *buf);
1553 
1554 static const rb_data_type_t window_type = {
1555     "vim_window",
1556     {0, 0, window_dsize,
1557 # if RUBY_VERSION >= 27
1558 	0, {0}
1559 # else
1560 	{0, 0}
1561 # endif
1562     },
1563     0, 0,
1564 # ifdef RUBY_TYPED_FREE_IMMEDIATELY
1565     0,
1566 # endif
1567 };
1568 
1569     static size_t
1570 window_dsize(const void *win UNUSED)
1571 {
1572     return sizeof(win_T);
1573 }
1574 #endif
1575 
1576     static VALUE
1577 window_new(win_T *win)
1578 {
1579     if (win->w_ruby_ref)
1580     {
1581 	return (VALUE) win->w_ruby_ref;
1582     }
1583     else
1584     {
1585 #ifdef USE_TYPEDDATA
1586 	VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1587 #else
1588 	VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
1589 #endif
1590 	win->w_ruby_ref = (void *) obj;
1591 	rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1592 	return obj;
1593     }
1594 }
1595 
1596     static win_T *
1597 get_win(VALUE obj)
1598 {
1599     win_T *win;
1600 
1601 #ifdef USE_TYPEDDATA
1602     TypedData_Get_Struct(obj, win_T, &window_type, win);
1603 #else
1604     Data_Get_Struct(obj, win_T, win);
1605 #endif
1606     if (win == NULL)
1607 	rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1608     return win;
1609 }
1610 
1611     static VALUE
1612 window_s_current(VALUE self UNUSED)
1613 {
1614     return window_new(curwin);
1615 }
1616 
1617     static VALUE
1618 window_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
1619 {
1620     return window_new(curwin);
1621 }
1622 
1623 /*
1624  * Added line manipulation functions
1625  *    SegPhault - 03/07/05
1626  */
1627     static VALUE
1628 line_s_current(VALUE self UNUSED)
1629 {
1630     return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1631 }
1632 
1633     static VALUE
1634 set_current_line(VALUE self UNUSED, VALUE str)
1635 {
1636     return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1637 }
1638 
1639     static VALUE
1640 current_line_number(VALUE self UNUSED)
1641 {
1642     return INT2FIX((int)curwin->w_cursor.lnum);
1643 }
1644 
1645     static VALUE
1646 window_s_count(VALUE self UNUSED)
1647 {
1648     win_T	*w;
1649     int n = 0;
1650 
1651     FOR_ALL_WINDOWS(w)
1652 	n++;
1653     return INT2NUM(n);
1654 }
1655 
1656     static VALUE
1657 window_s_aref(VALUE self UNUSED, VALUE num)
1658 {
1659     win_T *w;
1660     int n = NUM2INT(num);
1661 
1662     for (w = firstwin; w != NULL; w = w->w_next, --n)
1663 	if (n == 0)
1664 	    return window_new(w);
1665     return Qnil;
1666 }
1667 
1668     static VALUE
1669 window_buffer(VALUE self)
1670 {
1671     win_T *win = get_win(self);
1672 
1673     return buffer_new(win->w_buffer);
1674 }
1675 
1676     static VALUE
1677 window_height(VALUE self)
1678 {
1679     win_T *win = get_win(self);
1680 
1681     return INT2NUM(win->w_height);
1682 }
1683 
1684     static VALUE
1685 window_set_height(VALUE self, VALUE height)
1686 {
1687     win_T *win = get_win(self);
1688     win_T *savewin = curwin;
1689 
1690     curwin = win;
1691     win_setheight(NUM2INT(height));
1692     curwin = savewin;
1693     return height;
1694 }
1695 
1696     static VALUE
1697 window_width(VALUE self UNUSED)
1698 {
1699     return INT2NUM(get_win(self)->w_width);
1700 }
1701 
1702     static VALUE
1703 window_set_width(VALUE self UNUSED, VALUE width)
1704 {
1705     win_T *win = get_win(self);
1706     win_T *savewin = curwin;
1707 
1708     curwin = win;
1709     win_setwidth(NUM2INT(width));
1710     curwin = savewin;
1711     return width;
1712 }
1713 
1714     static VALUE
1715 window_cursor(VALUE self)
1716 {
1717     win_T *win = get_win(self);
1718 
1719     return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1720 }
1721 
1722     static VALUE
1723 window_set_cursor(VALUE self, VALUE pos)
1724 {
1725     VALUE lnum, col;
1726     win_T *win = get_win(self);
1727 
1728     Check_Type(pos, T_ARRAY);
1729     if (RARRAY_LEN(pos) != 2)
1730 	rb_raise(rb_eArgError, "array length must be 2");
1731     lnum = RARRAY_PTR(pos)[0];
1732     col = RARRAY_PTR(pos)[1];
1733     win->w_cursor.lnum = NUM2LONG(lnum);
1734     win->w_cursor.col = NUM2UINT(col);
1735     win->w_set_curswant = TRUE;
1736     check_cursor();		    // put cursor on an existing line
1737     update_screen(NOT_VALID);
1738     return Qnil;
1739 }
1740 
1741     static VALUE
1742 f_nop(VALUE self UNUSED)
1743 {
1744     return Qnil;
1745 }
1746 
1747     static VALUE
1748 f_p(int argc, VALUE *argv, VALUE self UNUSED)
1749 {
1750     int i;
1751     VALUE str = rb_str_new("", 0);
1752     VALUE ret = Qnil;
1753 
1754     for (i = 0; i < argc; i++)
1755     {
1756 	if (i > 0) rb_str_cat(str, ", ", 2);
1757 	rb_str_concat(str, rb_inspect(argv[i]));
1758     }
1759     msg(RSTRING_PTR(str));
1760 
1761     if (argc == 1)
1762 	ret = argv[0];
1763     else if (argc > 1)
1764 	ret = rb_ary_new4(argc, argv);
1765     return ret;
1766 }
1767 
1768     static void
1769 ruby_io_init(void)
1770 {
1771 #ifndef DYNAMIC_RUBY
1772     RUBYEXTERN VALUE rb_stdout;
1773     RUBYEXTERN VALUE rb_stderr;
1774 #endif
1775 
1776     rb_stdout = rb_obj_alloc(rb_cObject);
1777     rb_stderr = rb_obj_alloc(rb_cObject);
1778     rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
1779     rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
1780     rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1781     rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
1782     rb_define_global_function("p", f_p, -1);
1783 }
1784 
1785     static void
1786 ruby_vim_init(void)
1787 {
1788     objtbl = rb_hash_new();
1789     rb_global_variable(&objtbl);
1790 
1791     // The Vim module used to be called "VIM", but "Vim" is better.  Make an
1792     // alias "VIM" for backwards compatibility.
1793     mVIM = rb_define_module("Vim");
1794     rb_define_const(rb_cObject, "VIM", mVIM);
1795     rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1796     rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1797     rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1798     rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1799     rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1800     rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1801     rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1802     rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1803     rb_define_module_function(mVIM, "message", vim_message, 1);
1804     rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1805     rb_define_module_function(mVIM, "command", vim_command, 1);
1806     rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1807     rb_define_module_function(mVIM, "blob", vim_blob, 1);
1808 
1809     eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1810 						rb_eStandardError);
1811     eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1812 						rb_eStandardError);
1813 
1814     cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1815     rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1816     rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1817     rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1818     rb_define_method(cBuffer, "name", buffer_name, 0);
1819     rb_define_method(cBuffer, "number", buffer_number, 0);
1820     rb_define_method(cBuffer, "count", buffer_count, 0);
1821     rb_define_method(cBuffer, "length", buffer_count, 0);
1822     rb_define_method(cBuffer, "[]", buffer_aref, 1);
1823     rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1824     rb_define_method(cBuffer, "delete", buffer_delete, 1);
1825     rb_define_method(cBuffer, "append", buffer_append, 2);
1826 
1827     // Added line manipulation functions
1828     //   SegPhault - 03/07/05
1829     rb_define_method(cBuffer, "line_number", current_line_number, 0);
1830     rb_define_method(cBuffer, "line", line_s_current, 0);
1831     rb_define_method(cBuffer, "line=", set_current_line, 1);
1832 
1833 
1834     cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1835     rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1836     rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1837     rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1838     rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1839     rb_define_method(cVimWindow, "height", window_height, 0);
1840     rb_define_method(cVimWindow, "height=", window_set_height, 1);
1841     rb_define_method(cVimWindow, "width", window_width, 0);
1842     rb_define_method(cVimWindow, "width=", window_set_width, 1);
1843     rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1844     rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1845 
1846     rb_define_virtual_variable("$curbuf", buffer_s_current_getter, 0);
1847     rb_define_virtual_variable("$curwin", window_s_current_getter, 0);
1848 }
1849 
1850     void
1851 vim_ruby_init(void *stack_start)
1852 {
1853     // should get machine stack start address early in main function
1854     ruby_stack_start = stack_start;
1855 }
1856 
1857     static int
1858 convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1859 {
1860     dict_T *d = (dict_T *)arg;
1861     dictitem_T *di;
1862 
1863     di = dictitem_alloc((char_u *)RSTRING_PTR(rb_obj_as_string(key)));
1864     if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1865 						     || dict_add(d, di) != OK)
1866     {
1867 	d->dv_hashtab.ht_error = TRUE;
1868 	return ST_STOP;
1869     }
1870     return ST_CONTINUE;
1871 }
1872 
1873     static int
1874 ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1875 {
1876     switch (TYPE(val))
1877     {
1878 	case T_NIL:
1879 	    rettv->v_type = VAR_SPECIAL;
1880 	    rettv->vval.v_number = VVAL_NULL;
1881 	    break;
1882 	case T_TRUE:
1883 	    rettv->v_type = VAR_BOOL;
1884 	    rettv->vval.v_number = VVAL_TRUE;
1885 	    break;
1886 	case T_FALSE:
1887 	    rettv->v_type = VAR_BOOL;
1888 	    rettv->vval.v_number = VVAL_FALSE;
1889 	    break;
1890 	case T_BIGNUM:
1891 	case T_FIXNUM:
1892 	    rettv->v_type = VAR_NUMBER;
1893 	    rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1894 	    break;
1895 #ifdef FEAT_FLOAT
1896 	case T_FLOAT:
1897 	    rettv->v_type = VAR_FLOAT;
1898 	    rettv->vval.v_float = (float_T)NUM2DBL(val);
1899 	    break;
1900 #endif
1901 	default:
1902 	    val = rb_obj_as_string(val);
1903 	    // FALLTHROUGH
1904 	case T_STRING:
1905 	    {
1906 		VALUE str = (VALUE)RSTRING(val);
1907 
1908 		rettv->v_type = VAR_STRING;
1909 		rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
1910 							     RSTRING_LEN(str));
1911 	    }
1912 	    break;
1913 	case T_ARRAY:
1914 	    {
1915 		list_T *l;
1916 		long i;
1917 		typval_T v;
1918 
1919 		l = list_alloc();
1920 		if (l == NULL)
1921 		    return FAIL;
1922 
1923 		for (i = 0; i < RARRAY_LEN(val); ++i)
1924 		{
1925 		    if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1926 									&v) != OK)
1927 		    {
1928 			list_unref(l);
1929 			return FAIL;
1930 		    }
1931 		    list_append_tv(l, &v);
1932 		    clear_tv(&v);
1933 		}
1934 
1935 		rettv->v_type = VAR_LIST;
1936 		rettv->vval.v_list = l;
1937 		++l->lv_refcount;
1938 	    }
1939 	    break;
1940 	case T_HASH:
1941 	    {
1942 		dict_T *d;
1943 
1944 		d = dict_alloc();
1945 		if (d == NULL)
1946 		    return FAIL;
1947 
1948 		rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1949 		if (d->dv_hashtab.ht_error)
1950 		{
1951 		    dict_unref(d);
1952 		    return FAIL;
1953 		}
1954 
1955 		rettv->v_type = VAR_DICT;
1956 		rettv->vval.v_dict = d;
1957 		++d->dv_refcount;
1958 	    }
1959 	    break;
1960     }
1961     return OK;
1962 }
1963 
1964     void
1965 do_rubyeval(char_u *str, typval_T *rettv)
1966 {
1967     int retval = FAIL;
1968 
1969     if (ensure_ruby_initialized())
1970     {
1971 	int state;
1972 	VALUE obj;
1973 
1974 	obj = rb_eval_string_protect((const char *)str, &state);
1975 	if (state)
1976 	    error_print(state);
1977 	else
1978 	    retval = ruby_convert_to_vim_value(obj, rettv);
1979     }
1980     if (retval == FAIL)
1981     {
1982 	rettv->v_type = VAR_NUMBER;
1983 	rettv->vval.v_number = 0;
1984     }
1985 }
1986