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