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