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