blob: 63294f4e5eb3dfc7d676d1851816a48582b823ed [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004 *
5 * Ruby interface by Shugo Maeda
6 * with improvements by SegPhault (Ryan Paul)
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +02007 * with improvements by Jon Maken
Bram Moolenaar071d4272004-06-13 20:20:40 +00008 *
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
Bram Moolenaar32d19c12018-09-13 17:26:54 +020014#include "protodef.h"
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020015#ifdef HAVE_CONFIG_H
16# include "auto/config.h"
17#endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020018
Bram Moolenaare2793352011-01-17 19:53:27 +010019#include <stdio.h>
20#include <string.h>
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef _WIN32
23# if !defined(DYNAMIC_RUBY_VER) || (DYNAMIC_RUBY_VER < 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
Bram Moolenaar2016ae52016-06-13 20:08:43 +020035#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 24
36# define USE_RUBY_INTEGER
Bram Moolenaar06469e92016-06-11 22:26:53 +020037#endif
38
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020039#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000040/*
41 * This is tricky. In ruby.h there is (inline) function rb_class_of()
42 * definition. This function use these variables. But we want function to
43 * use dll_* variables.
44 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045# define rb_cFalseClass (*dll_rb_cFalseClass)
46# define rb_cFixnum (*dll_rb_cFixnum)
Bram Moolenaar2016ae52016-06-13 20:08:43 +020047# if defined(USE_RUBY_INTEGER)
48# define rb_cInteger (*dll_rb_cInteger)
Bram Moolenaar06469e92016-06-11 22:26:53 +020049# endif
Bram Moolenaardb3fbe52013-03-07 15:16:21 +010050# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
51# define rb_cFloat (*dll_rb_cFloat)
52# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000053# define rb_cNilClass (*dll_rb_cNilClass)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010054# define rb_cString (*dll_rb_cString)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055# define rb_cSymbol (*dll_rb_cSymbol)
56# define rb_cTrueClass (*dll_rb_cTrueClass)
57# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
58/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010059 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
60 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 * defined in this file. When defined this RUBY_EXPORT it modified to
62 * "extern" and be able to avoid this problem.
63 */
64# define RUBY_EXPORT
65# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020066
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +010067#endif // ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +010069// suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +010070#if (_MSC_VER == 1200)
71# undef _WIN32_WINNT
72#endif
73
Bram Moolenaar639a2552010-03-17 18:15:23 +010074#if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
75 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
76# define RUBY19_OR_LATER 1
77#endif
78
Bram Moolenaar0d27f642015-12-28 22:05:28 +010079#if (defined(RUBY_VERSION) && RUBY_VERSION >= 20) \
80 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20)
81# define RUBY20_OR_LATER 1
82#endif
83
Bram Moolenaarf711cb22018-08-01 18:42:13 +020084#if (defined(RUBY_VERSION) && RUBY_VERSION >= 21) \
85 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 21)
86# define RUBY21_OR_LATER 1
87#endif
88
Bram Moolenaar42d57f02010-03-10 12:47:00 +010089#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
90/* Ruby 1.9 defines a number of static functions which use rb_num2long and
91 * rb_int2big */
92# define rb_num2long rb_num2long_stub
93# define rb_int2big rb_int2big_stub
94#endif
95
Bram Moolenaar498af702014-03-28 21:58:21 +010096#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
97 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
98/* Ruby 1.9 defines a number of static functions which use rb_fix2int and
Bram Moolenaara2aa31a2014-02-23 22:52:40 +010099 * rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit) */
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200100# define rb_fix2int rb_fix2int_stub
101# define rb_num2int rb_num2int_stub
102#endif
103
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100104#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100105/* Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
106 * rb_gc_writebarrier_unprotect_promoted if USE_RGENGC */
Bram Moolenaar90140742014-11-27 17:44:08 +0100107# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
108#endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100109#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
110# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100111#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100112
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100113#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100114# define rb_ary_detransient rb_ary_detransient_stub
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100115#endif
116
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100118#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100119# include <ruby/encoding.h>
120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100122#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#undef EXTERN
124#undef _
125
126/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaard0573012017-10-28 21:11:06 +0200127#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128# define __OPENTRANSPORT__
129# define __OPENTRANSPORTPROTOCOL__
130# define __OPENTRANSPORTPROVIDERS__
131#endif
132
Bram Moolenaar165641d2010-02-17 16:23:09 +0100133/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100134 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
135 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
136 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100137 * Use TypedData_XXX if available.
138 */
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100139#if defined(TypedData_Wrap_Struct) && defined(RUBY20_OR_LATER)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100140# define USE_TYPEDDATA 1
141#endif
142
143/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200144 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100145 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
146 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
147 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
148 */
149#ifndef StringValuePtr
150# define StringValuePtr(s) STR2CSTR(s)
151#endif
152#ifndef RARRAY_LEN
153# define RARRAY_LEN(s) RARRAY(s)->len
154#endif
155#ifndef RARRAY_PTR
156# define RARRAY_PTR(s) RARRAY(s)->ptr
157#endif
158#ifndef RSTRING_LEN
159# define RSTRING_LEN(s) RSTRING(s)->len
160#endif
161#ifndef RSTRING_PTR
162# define RSTRING_PTR(s) RSTRING(s)->ptr
163#endif
164
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100165#ifdef HAVE_DUP
166# undef HAVE_DUP
167#endif
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#include "vim.h"
170#include "version.h"
171
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100172#ifdef DYNAMIC_RUBY
173# if !defined(MSWIN) // must come after including vim.h, where it is defined
174# include <dlfcn.h>
175# define HINSTANCE void*
176# define RUBY_PROC void*
177# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
178# define symbol_from_dll dlsym
179# define close_dll dlclose
180# else
181# define RUBY_PROC FARPROC
182# define load_dll vimLoadLib
183# define symbol_from_dll GetProcAddress
184# define close_dll FreeLibrary
185# endif
186#endif
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#if defined(PROTO) && !defined(FEAT_RUBY)
189/* Define these to be able to generate the function prototypes. */
190# define VALUE int
191# define RUBY_DATA_FUNC int
192#endif
193
194static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200195static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196static VALUE objtbl;
197
198static VALUE mVIM;
199static VALUE cBuffer;
200static VALUE cVimWindow;
201static VALUE eDeletedBufferError;
202static VALUE eDeletedWindowError;
203
204static int ensure_ruby_initialized(void);
205static void error_print(int);
206static void ruby_io_init(void);
207static void ruby_vim_init(void);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100208static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200210#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
211# if defined(__ia64) && !defined(ruby_init_stack)
212# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#endif
215
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200216#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100217# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200218# define HINSTANCE int /* for generating prototypes */
219# endif
220
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221/*
222 * Wrapper defines
223 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200224# define rb_assoc_new dll_rb_assoc_new
225# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100226# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100227# define rb_check_type dll_rb_check_type
228# ifdef USE_TYPEDDATA
229# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100230# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200231# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100232# ifdef USE_TYPEDDATA
233# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
234# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
235# else
236# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
237# endif
238# else
239# define rb_data_object_alloc dll_rb_data_object_alloc
240# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200241# define rb_define_class_under dll_rb_define_class_under
242# define rb_define_const dll_rb_define_const
243# define rb_define_global_function dll_rb_define_global_function
244# define rb_define_method dll_rb_define_method
245# define rb_define_module dll_rb_define_module
246# define rb_define_module_function dll_rb_define_module_function
247# define rb_define_singleton_method dll_rb_define_singleton_method
248# define rb_define_virtual_variable dll_rb_define_virtual_variable
249# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200250# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200251# define rb_eArgError (*dll_rb_eArgError)
252# define rb_eIndexError (*dll_rb_eIndexError)
253# define rb_eRuntimeError (*dll_rb_eRuntimeError)
254# define rb_eStandardError (*dll_rb_eStandardError)
255# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200256# ifdef RUBY21_OR_LATER
257# define rb_funcallv dll_rb_funcallv
258# else
259# define rb_funcall2 dll_rb_funcall2
260# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200261# define rb_global_variable dll_rb_global_variable
262# define rb_hash_aset dll_rb_hash_aset
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100263# define rb_hash_foreach dll_rb_hash_foreach
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200264# define rb_hash_new dll_rb_hash_new
265# define rb_inspect dll_rb_inspect
266# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200267
268// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
269// work. Not using the cache appears to be the best solution.
270# undef rb_intern
271# define rb_intern dll_rb_intern
272
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100273# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar498af702014-03-28 21:58:21 +0100274# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 18
275# define rb_fix2int dll_rb_fix2int
276# define rb_num2int dll_rb_num2int
277# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200278# define rb_num2uint dll_rb_num2uint
279# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100280# define rb_num2dbl dll_rb_num2dbl
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200281# define rb_lastline_get dll_rb_lastline_get
282# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200283# define rb_protect dll_rb_protect
284# define rb_load dll_rb_load
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200285# ifndef RUBY19_OR_LATER
286# define rb_num2long dll_rb_num2long
287# endif
288# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
289# define rb_num2ulong dll_rb_num2ulong
290# endif
291# define rb_obj_alloc dll_rb_obj_alloc
292# define rb_obj_as_string dll_rb_obj_as_string
293# define rb_obj_id dll_rb_obj_id
294# define rb_raise dll_rb_raise
295# define rb_str_cat dll_rb_str_cat
296# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100297# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200298# define rb_str_new dll_rb_str_new
299# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200300/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200301# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200302/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
303 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200304# undef rb_str_new_cstr
305# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200306# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200307# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200308# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200309# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200310# define rb_string_value dll_rb_string_value
311# define rb_string_value_ptr dll_rb_string_value_ptr
312# define rb_float_new dll_rb_float_new
313# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200314# ifdef rb_ary_new4
315# define RB_ARY_NEW4_MACRO 1
316# undef rb_ary_new4
317# endif
318# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200319# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200320# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
321# ifdef __ia64
322# define rb_ia64_bsp dll_rb_ia64_bsp
323# undef ruby_init_stack
324# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
325# else
326# define ruby_init_stack dll_ruby_init_stack
327# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200328# endif
329# else
330# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200331# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200332# ifdef RUBY19_OR_LATER
333# define rb_errinfo dll_rb_errinfo
334# else
335# define ruby_errinfo (*dll_ruby_errinfo)
336# endif
337# define ruby_init dll_ruby_init
338# define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar4f974752019-02-17 17:44:42 +0100339# ifdef MSWIN
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100340# ifdef RUBY19_OR_LATER
341# define ruby_sysinit dll_ruby_sysinit
342# else
343# define NtInitialize dll_NtInitialize
344# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200345# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
346# define rb_w32_snprintf dll_rb_w32_snprintf
347# endif
348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200350# ifdef RUBY19_OR_LATER
351# define ruby_script dll_ruby_script
352# define rb_enc_find_index dll_rb_enc_find_index
353# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100354# undef rb_enc_str_new
355# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200356# define rb_sprintf dll_rb_sprintf
357# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100358# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200359# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100360
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361/*
362 * Pointers for dynamic link
363 */
364static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200365VALUE *dll_rb_cFalseClass;
366VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200367# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200368VALUE *dll_rb_cInteger;
369# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200370# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100371VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200372# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200373VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100375VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200376VALUE *dll_rb_cSymbol;
377VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100378static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100380# ifdef USE_TYPEDDATA
381static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100384# ifdef USE_TYPEDDATA
385# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
386static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
387# else
388static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
389# endif
390# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
394static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
395static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
396static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
397static VALUE (*dll_rb_define_module) (const char*);
398static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
399static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
400static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
401static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200402static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403static VALUE *dll_rb_eArgError;
404static VALUE *dll_rb_eIndexError;
405static VALUE *dll_rb_eRuntimeError;
406static VALUE *dll_rb_eStandardError;
407static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200408# ifdef RUBY21_OR_LATER
409static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
410# else
411static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413static void (*dll_rb_global_variable) (VALUE*);
414static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100415static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416static VALUE (*dll_rb_hash_new) (void);
417static VALUE (*dll_rb_inspect) (VALUE);
418static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200419static ID (*dll_rb_intern) (const char*);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100420# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200421static long (*dll_rb_fix2int) (VALUE);
422static long (*dll_rb_num2int) (VALUE);
423static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200424# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100425static double (*dll_rb_num2dbl) (VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426static VALUE (*dll_rb_lastline_get) (void);
427static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100428static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200429static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430static long (*dll_rb_num2long) (VALUE);
431static unsigned long (*dll_rb_num2ulong) (VALUE);
432static VALUE (*dll_rb_obj_alloc) (VALUE);
433static VALUE (*dll_rb_obj_as_string) (VALUE);
434static VALUE (*dll_rb_obj_id) (VALUE);
435static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200436# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200437static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200438# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200440# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
442static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
443static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200444# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200445/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
446static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200447# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200449# endif
450# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100451static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200452# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455static void (*dll_ruby_init) (void);
456static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100457# ifdef MSWIN
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100458# ifdef RUBY19_OR_LATER
Bram Moolenaar4f391792017-01-15 16:59:07 +0100459static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100460# else
461static void (*dll_NtInitialize) (int*, char***);
462# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200463# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200464static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200465# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200466# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200467# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100468static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
469static VALUE (*dll_rb_float_new) (double);
470static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200471static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100472static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100473# if DYNAMIC_RUBY_VER >= 26
474static void (*dll_rb_ary_detransient) (VALUE);
475# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200476# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
477# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200478static void * (*dll_rb_ia64_bsp) (void);
479static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200480# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200481static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200482# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200483# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200484# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200485# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100486static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200487# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200489# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100490static void (*dll_ruby_script) (const char*);
491static int (*dll_rb_enc_find_index) (const char*);
492static rb_encoding* (*dll_rb_enc_find) (const char*);
493static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
494static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100495static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100496static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200497# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100498
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100499# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100500# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100501static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100502# else
503static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
504# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100505# endif
506
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200507# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200508# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100509 long
510rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200511# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100512 SIGNED_VALUE
513rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200514# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100515{
516 return dll_rb_num2long(x);
517}
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100518# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100519 VALUE
520rb_int2big_stub(intptr_t x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100521# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100522 VALUE
523rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100524# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100525{
526 return dll_rb_int2big(x);
527}
Bram Moolenaar498af702014-03-28 21:58:21 +0100528# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
529 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100530 long
531rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200532{
533 return dll_rb_fix2int(x);
534}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100535 long
536rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200537{
538 return dll_rb_num2int(x);
539}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200540# endif
541# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100542 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100543rb_float_new_in_heap(double d)
544{
545 return dll_rb_float_new(d);
546}
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200547# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100548 unsigned long
549rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200550# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100551 VALUE
552rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200553# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100554{
555 return (long)RSHIFT((SIGNED_VALUE)(x),1);
556}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200557# endif
558# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100559
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100560 /* Do not generate a prototype here, VALUE isn't always defined. */
561# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100562# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100563 void
564rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100565{
Bram Moolenaar90140742014-11-27 17:44:08 +0100566 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100567}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100568# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100569 void
570rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100571{
572 dll_rb_gc_writebarrier_unprotect(obj);
573}
574# endif
575# endif
576
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100577# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100578 void
579rb_ary_detransient_stub(VALUE x)
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100580{
581 dll_rb_ary_detransient(x);
582}
583# endif
584
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200585static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
587/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000588 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590static struct
591{
592 char *name;
593 RUBY_PROC *ptr;
594} ruby_funcname_table[] =
595{
596 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
597 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200598# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200599 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100600# else
601 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200602# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200603# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100604 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200605# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
607 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100608 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
610 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100611 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100613# ifdef USE_TYPEDDATA
614 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
615# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100617# ifdef USE_TYPEDDATA
618# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
619 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
620# else
621 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
622# endif
623# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100625# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
627 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
628 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
629 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
630 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
631 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
632 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
633 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
634 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200635 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
637 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
638 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
639 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
640 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200641# ifdef RUBY21_OR_LATER
642 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
643# else
644 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
647 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100648 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
650 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
651 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200652 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100653# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200654 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
655 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
656 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200657# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100658 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
660 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200661 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
662 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
664 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
665 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
666 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
667 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
668 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200669# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200670 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200671# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200673# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
675 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
676 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200677# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200678 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200679# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200681# endif
682# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100683 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200684# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200686# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
688 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100689# ifdef MSWIN
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100690# ifdef RUBY19_OR_LATER
Bram Moolenaar4f391792017-01-15 16:59:07 +0100691 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100692# else
693 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200694# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200695# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200697# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200698# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200699# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100700 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200701# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100702 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200703# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100704 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200705# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100706 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200707# ifdef RB_ARY_NEW4_MACRO
708 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
709# else
710 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
711# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100712 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100713# if DYNAMIC_RUBY_VER >= 26
714 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
715# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200716# endif
717# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100718 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100719 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
720 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
721 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
722 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
723 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100724 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100725 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200726# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200727# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
728# ifdef __ia64
729 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
730# endif
731 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
732# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100733# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100734# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100735 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100736# else
737 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
738# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100739# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 {"", NULL},
741};
742
743/*
744 * Free ruby.dll
745 */
746 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100747end_dynamic_ruby(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
749 if (hinstRuby)
750 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200751 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200752 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 }
754}
755
756/*
757 * Load library and get all pointers.
758 * Parameter 'libname' provides name of DLL.
759 * Return OK or FAIL.
760 */
761 static int
762ruby_runtime_link_init(char *libname, int verbose)
763{
764 int i;
765
766 if (hinstRuby)
767 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200768 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 if (!hinstRuby)
770 {
771 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100772 semsg(_(e_loadlib), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 return FAIL;
774 }
775
776 for (i = 0; ruby_funcname_table[i].ptr; ++i)
777 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200778 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 ruby_funcname_table[i].name)))
780 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200781 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200782 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100784 semsg(_(e_loadfunc), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 return FAIL;
786 }
787 }
788 return OK;
789}
790
791/*
792 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
793 * else FALSE.
794 */
795 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100796ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100798 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799}
800#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
801
802 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100803ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
805#ifdef DYNAMIC_RUBY
806 end_dynamic_ruby();
807#endif
808}
809
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100810 void
811ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
813 int state;
814 char *script = NULL;
815
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000816 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 if (!eap->skip && ensure_ruby_initialized())
818 {
819 if (script == NULL)
820 rb_eval_string_protect((char *)eap->arg, &state);
821 else
822 rb_eval_string_protect(script, &state);
823 if (state)
824 error_print(state);
825 }
826 vim_free(script);
827}
828
Bram Moolenaar165641d2010-02-17 16:23:09 +0100829/*
830 * In Ruby 1.9 or later, ruby String object has encoding.
831 * conversion buffer string of vim to ruby String object using
832 * VIM encoding option.
833 */
834 static VALUE
835vim_str2rb_enc_str(const char *s)
836{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100837#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100838 int isnum;
839 long lval;
840 char_u *sval;
841 rb_encoding *enc;
842
843 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
844 if (isnum == 0)
845 {
846 enc = rb_enc_find((char *)sval);
847 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200848 if (enc)
849 {
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200850 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100851 }
852 }
853#endif
854 return rb_str_new2(s);
855}
856
857 static VALUE
858eval_enc_string_protect(const char *str, int *state)
859{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100860#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100861 int isnum;
862 long lval;
863 char_u *sval;
864 rb_encoding *enc;
865 VALUE v;
866
867 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
868 if (isnum == 0)
869 {
870 enc = rb_enc_find((char *)sval);
871 vim_free(sval);
872 if (enc)
873 {
874 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
875 return rb_eval_string_protect(StringValuePtr(v), state);
876 }
877 }
878#endif
879 return rb_eval_string_protect(str, state);
880}
881
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100882 void
883ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884{
885 int state;
886 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100887 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
889 if (ensure_ruby_initialized())
890 {
891 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
892 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200893 for (i = eap->line1; i <= eap->line2; i++)
894 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100895 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100897 if (i > curbuf->b_ml.ml_line_count)
898 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100899 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100901 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200902 if (state)
903 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 error_print(state);
905 break;
906 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100907 if (was_curbuf != curbuf)
908 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200910 if (!NIL_P(line))
911 {
912 if (TYPE(line) != T_STRING)
913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100914 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 return;
916 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100917 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 changed();
919#ifdef SYNTAX_HL
920 syn_changed(i); /* recompute syntax hl. for this line */
921#endif
922 }
923 }
924 check_cursor();
925 update_curbuf(NOT_VALID);
926 }
927}
928
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100929 static VALUE
930rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100931{
932 rb_load(file_to_load, 0);
933 return Qnil;
934}
935
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100936 void
937ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938{
939 int state;
940
941 if (ensure_ruby_initialized())
942 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100943 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
944 rb_protect(rb_load_wrap, file_to_load, &state);
945 if (state)
946 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 }
948}
949
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100950 void
951ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000953 if (buf->b_ruby_ref)
954 {
955 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
956 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 }
958}
959
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100960 void
961ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000963 if (win->w_ruby_ref)
964 {
965 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
966 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968}
969
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100970 static int
971ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
973 if (!ruby_initialized)
974 {
975#ifdef DYNAMIC_RUBY
976 if (ruby_enabled(TRUE))
977 {
978#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100979#ifdef MSWIN
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100980 /* suggested by Ariya Mizutani */
981 int argc = 1;
982 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100983 char **argvp = argv;
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100984# ifdef RUBY19_OR_LATER
985 ruby_sysinit(&argc, &argvp);
986# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100987 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100988# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100989#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200990 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200991#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200992 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100993#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200994 ruby_init();
995 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100996#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100997 {
998 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200999 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +01001000 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001001 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001002 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001003#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001005#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001006 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 ruby_vim_init();
1008 ruby_initialized = 1;
1009#ifdef DYNAMIC_RUBY
1010 }
1011 else
1012 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001013 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 return 0;
1015 }
1016#endif
1017 }
1018 return ruby_initialized;
1019}
1020
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001021 static void
1022error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023{
Bram Moolenaarb09c6842018-12-27 22:11:01 +01001024#if !defined(DYNAMIC_RUBY) && !defined(RUBY19_OR_LATER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 RUBYEXTERN VALUE ruby_errinfo;
1026#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001027 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 VALUE eclass;
1029 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001030 VALUE bt;
1031 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001033 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
1035#define TAG_RETURN 0x1
1036#define TAG_BREAK 0x2
1037#define TAG_NEXT 0x3
1038#define TAG_RETRY 0x4
1039#define TAG_REDO 0x5
1040#define TAG_RAISE 0x6
1041#define TAG_THROW 0x7
1042#define TAG_FATAL 0x8
1043#define TAG_MASK 0xf
1044
Bram Moolenaar758535a2016-03-30 22:06:16 +02001045 switch (state)
1046 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001047 case TAG_RETURN:
1048 emsg(_("E267: unexpected return"));
1049 break;
1050 case TAG_NEXT:
1051 emsg(_("E268: unexpected next"));
1052 break;
1053 case TAG_BREAK:
1054 emsg(_("E269: unexpected break"));
1055 break;
1056 case TAG_REDO:
1057 emsg(_("E270: unexpected redo"));
1058 break;
1059 case TAG_RETRY:
1060 emsg(_("E271: retry outside of rescue clause"));
1061 break;
1062 case TAG_RAISE:
1063 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +01001064#ifdef RUBY19_OR_LATER
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001065 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001066#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001067 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001068#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001069 eclass = CLASS_OF(error);
1070 einfo = rb_obj_as_string(error);
1071 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1072 {
1073 emsg(_("E272: unhandled exception"));
1074 }
1075 else
1076 {
1077 VALUE epath;
1078 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001080 epath = rb_class_path(eclass);
1081 vim_snprintf(buff, BUFSIZ, "%s: %s",
1082 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1083 p = strchr(buff, '\n');
1084 if (p) *p = '\0';
1085 emsg(buff);
1086 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001087
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001088 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001089# ifdef RUBY21_OR_LATER
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001090 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1091 for (i = 0; i < RARRAY_LEN(bt); i++)
1092 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001093# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001094 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1095 for (i = 0; i < RARRAY_LEN(bt); i++)
1096 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001097# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001098 break;
1099 default:
1100 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1101 emsg(buff);
1102 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 }
1104}
1105
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001106 static VALUE
1107vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108{
1109 char *buff, *p;
1110
1111 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001112 if (RSTRING_LEN(str) > 0)
1113 {
1114 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001115 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001116 strcpy(buff, RSTRING_PTR(str));
1117 p = strchr(buff, '\n');
1118 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001119 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001120 }
1121 else
1122 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001123 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 return Qnil;
1126}
1127
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001128 static VALUE
1129vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001131 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 update_screen(NOT_VALID);
1133 return Qnil;
1134}
1135
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001136 static VALUE
1137vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001139 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 return Qnil;
1141}
1142
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001143#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001144 static VALUE
1145vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001146{
1147 VALUE result = Qnil;
1148
1149 if (tv->v_type == VAR_STRING)
1150 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001151 result = rb_str_new2(tv->vval.v_string == NULL
1152 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001153 }
1154 else if (tv->v_type == VAR_NUMBER)
1155 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001156 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001157 }
1158# ifdef FEAT_FLOAT
1159 else if (tv->v_type == VAR_FLOAT)
1160 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001161 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001162 }
1163# endif
1164 else if (tv->v_type == VAR_LIST)
1165 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001166 list_T *list = tv->vval.v_list;
1167 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001168
Bram Moolenaar639a2552010-03-17 18:15:23 +01001169 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001170
Bram Moolenaar639a2552010-03-17 18:15:23 +01001171 if (list != NULL)
1172 {
1173 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1174 {
1175 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
1176 }
1177 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001178 }
1179 else if (tv->v_type == VAR_DICT)
1180 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001181 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001182
Bram Moolenaar639a2552010-03-17 18:15:23 +01001183 if (tv->vval.v_dict != NULL)
1184 {
1185 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1186 long_u todo = ht->ht_used;
1187 hashitem_T *hi;
1188 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001189
Bram Moolenaar639a2552010-03-17 18:15:23 +01001190 for (hi = ht->ht_array; todo > 0; ++hi)
1191 {
1192 if (!HASHITEM_EMPTY(hi))
1193 {
1194 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001195
Bram Moolenaar639a2552010-03-17 18:15:23 +01001196 di = dict_lookup(hi);
1197 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001198 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001199 }
1200 }
1201 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001202 }
1203 else if (tv->v_type == VAR_SPECIAL)
1204 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001205 if (tv->vval.v_number == VVAL_TRUE)
1206 result = Qtrue;
1207 else if (tv->vval.v_number == VVAL_FALSE)
1208 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001209 }
1210 else if (tv->v_type == VAR_BLOB)
1211 {
1212 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1213 tv->vval.v_blob->bv_ga.ga_len);
1214 }
1215 /* else return Qnil; */
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001216
1217 return result;
1218}
1219#endif
1220
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001221 static VALUE
1222vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223{
1224#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001225 typval_T *tv;
1226 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001228 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1229 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001231 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001233 result = vim_to_ruby(tv);
1234
1235 free_tv(tv);
1236
1237 return result;
1238#else
1239 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241}
1242
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001243#ifdef USE_TYPEDDATA
1244static size_t buffer_dsize(const void *buf);
1245
1246static const rb_data_type_t buffer_type = {
1247 "vim_buffer",
1248 {0, 0, buffer_dsize, {0, 0}},
1249 0, 0,
1250# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1251 0,
1252# endif
1253};
1254
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001255 static size_t
1256buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001257{
1258 return sizeof(buf_T);
1259}
1260#endif
1261
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001262 static VALUE
1263buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001265 if (buf->b_ruby_ref)
1266 {
1267 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001269 else
1270 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001271#ifdef USE_TYPEDDATA
1272 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1273#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001275#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001276 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1278 return obj;
1279 }
1280}
1281
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001282 static buf_T *
1283get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284{
1285 buf_T *buf;
1286
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001287#ifdef USE_TYPEDDATA
1288 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1289#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 if (buf == NULL)
1293 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1294 return buf;
1295}
1296
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001297 static VALUE
1298vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001299{
1300 VALUE result = rb_str_new("0z", 2);
1301 char buf[4];
1302 int i;
1303 for (i = 0; i < RSTRING_LEN(str); i++)
1304 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001305 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001306 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001307 }
1308 return result;
1309}
1310
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001311 static VALUE
1312buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
1314 return buffer_new(curbuf);
1315}
1316
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001317 static VALUE
1318buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 buf_T *b;
1321 int n = 0;
1322
Bram Moolenaar29323592016-07-24 22:04:11 +02001323 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001324 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001325 /* Deleted buffers should not be counted
1326 * SegPhault - 01/07/05 */
1327 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001328 n++;
1329 }
1330
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 return INT2NUM(n);
1332}
1333
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001334 static VALUE
1335buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
1337 buf_T *b;
1338 int n = NUM2INT(num);
1339
Bram Moolenaar29323592016-07-24 22:04:11 +02001340 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001341 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001342 /* Deleted buffers should not be counted
1343 * SegPhault - 01/07/05 */
1344 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001345 continue;
1346
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001347 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001349
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001350 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 }
1352 return Qnil;
1353}
1354
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001355 static VALUE
1356buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357{
1358 buf_T *buf = get_buf(self);
1359
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001360 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361}
1362
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001363 static VALUE
1364buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 buf_T *buf = get_buf(self);
1367
1368 return INT2NUM(buf->b_fnum);
1369}
1370
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001371 static VALUE
1372buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
1374 buf_T *buf = get_buf(self);
1375
1376 return INT2NUM(buf->b_ml.ml_line_count);
1377}
1378
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001379 static VALUE
1380get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001382 if (n <= 0 || n > buf->b_ml.ml_line_count)
1383 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1384 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385}
1386
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001387 static VALUE
1388buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
1390 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001391
1392 if (buf != NULL)
1393 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1394 return Qnil; /* For stop warning */
1395}
1396
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001397 static VALUE
1398set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001399{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001400 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001401 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001403 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1404 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001405 /* set curwin/curbuf for "buf" and save some things */
1406 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001407
Bram Moolenaar758535a2016-03-30 22:06:16 +02001408 if (u_savesub(n) == OK)
1409 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001410 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 changed();
1412#ifdef SYNTAX_HL
1413 syn_changed(n); /* recompute syntax hl. for this line */
1414#endif
1415 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001416
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001417 /* restore curwin/curbuf and a few other things */
1418 aucmd_restbuf(&aco);
1419 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 update_curbuf(NOT_VALID);
1422 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001423 else
1424 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001425 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 }
1427 return str;
1428}
1429
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001430 static VALUE
1431buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001432{
1433 buf_T *buf = get_buf(self);
1434
1435 if (buf != NULL)
1436 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1437 return str;
1438}
1439
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001440 static VALUE
1441buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001443 buf_T *buf = get_buf(self);
1444 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001445 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001447 if (n > 0 && n <= buf->b_ml.ml_line_count)
1448 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001449 /* set curwin/curbuf for "buf" and save some things */
1450 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001451
Bram Moolenaar758535a2016-03-30 22:06:16 +02001452 if (u_savedel(n, 1) == OK)
1453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001455
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001456 /* Changes to non-active buffers should properly refresh
1457 * SegPhault - 01/09/05 */
1458 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001459
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 changed();
1461 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001462
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001463 /* restore curwin/curbuf and a few other things */
1464 aucmd_restbuf(&aco);
1465 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001466
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 update_curbuf(NOT_VALID);
1468 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001469 else
1470 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001471 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
1473 return Qnil;
1474}
1475
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001476 static VALUE
1477buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001479 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001480 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001481 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001482 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
Bram Moolenaar3c531602010-11-16 14:46:19 +01001484 if (line == NULL)
1485 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001486 rb_raise(rb_eIndexError, "NULL line");
1487 }
1488 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001489 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001490 /* set curwin/curbuf for "buf" and save some things */
1491 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001492
Bram Moolenaar758535a2016-03-30 22:06:16 +02001493 if (u_inssub(n + 1) == OK)
1494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001496
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001497 /* Changes to non-active buffers should properly refresh screen
1498 * SegPhault - 12/20/04 */
1499 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001500
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001501 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001503
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001504 /* restore curwin/curbuf and a few other things */
1505 aucmd_restbuf(&aco);
1506 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 update_curbuf(NOT_VALID);
1509 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001510 else
1511 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001512 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
1514 return str;
1515}
1516
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001517#ifdef USE_TYPEDDATA
1518static size_t window_dsize(const void *buf);
1519
1520static const rb_data_type_t window_type = {
1521 "vim_window",
1522 {0, 0, window_dsize, {0, 0}},
1523 0, 0,
1524# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1525 0,
1526# endif
1527};
1528
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001529 static size_t
1530window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001531{
1532 return sizeof(win_T);
1533}
1534#endif
1535
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001536 static VALUE
1537window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001539 if (win->w_ruby_ref)
1540 {
1541 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001543 else
1544 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001545#ifdef USE_TYPEDDATA
1546 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1547#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001549#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001550 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1552 return obj;
1553 }
1554}
1555
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001556 static win_T *
1557get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
1559 win_T *win;
1560
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001561#ifdef USE_TYPEDDATA
1562 TypedData_Get_Struct(obj, win_T, &window_type, win);
1563#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 if (win == NULL)
1567 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1568 return win;
1569}
1570
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001571 static VALUE
1572window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573{
1574 return window_new(curwin);
1575}
1576
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001577/*
1578 * Added line manipulation functions
1579 * SegPhault - 03/07/05
1580 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001581 static VALUE
1582line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001583{
1584 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1585}
1586
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001587 static VALUE
1588set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001589{
1590 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1591}
1592
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001593 static VALUE
1594current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001595{
1596 return INT2FIX((int)curwin->w_cursor.lnum);
1597}
1598
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001599 static VALUE
1600window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 win_T *w;
1603 int n = 0;
1604
Bram Moolenaar29323592016-07-24 22:04:11 +02001605 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 n++;
1607 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608}
1609
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001610 static VALUE
1611window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
1613 win_T *w;
1614 int n = NUM2INT(num);
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 if (n == 0)
1618 return window_new(w);
1619 return Qnil;
1620}
1621
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001622 static VALUE
1623window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624{
1625 win_T *win = get_win(self);
1626
1627 return buffer_new(win->w_buffer);
1628}
1629
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001630 static VALUE
1631window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632{
1633 win_T *win = get_win(self);
1634
1635 return INT2NUM(win->w_height);
1636}
1637
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001638 static VALUE
1639window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640{
1641 win_T *win = get_win(self);
1642 win_T *savewin = curwin;
1643
1644 curwin = win;
1645 win_setheight(NUM2INT(height));
1646 curwin = savewin;
1647 return height;
1648}
1649
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001650 static VALUE
1651window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001652{
Bram Moolenaare745d752017-09-22 16:56:22 +02001653 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001654}
1655
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001656 static VALUE
1657window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001658{
1659 win_T *win = get_win(self);
1660 win_T *savewin = curwin;
1661
1662 curwin = win;
1663 win_setwidth(NUM2INT(width));
1664 curwin = savewin;
1665 return width;
1666}
1667
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001668 static VALUE
1669window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670{
1671 win_T *win = get_win(self);
1672
1673 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1674}
1675
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001676 static VALUE
1677window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
1679 VALUE lnum, col;
1680 win_T *win = get_win(self);
1681
1682 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001683 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001685 lnum = RARRAY_PTR(pos)[0];
1686 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 win->w_cursor.lnum = NUM2LONG(lnum);
1688 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001689 win->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 check_cursor(); /* put cursor on an existing line */
1691 update_screen(NOT_VALID);
1692 return Qnil;
1693}
1694
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001695 static VALUE
1696f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001697{
1698 return Qnil;
1699}
1700
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001701 static VALUE
1702f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703{
1704 int i;
1705 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001706 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
Bram Moolenaar758535a2016-03-30 22:06:16 +02001708 for (i = 0; i < argc; i++)
1709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 if (i > 0) rb_str_cat(str, ", ", 2);
1711 rb_str_concat(str, rb_inspect(argv[i]));
1712 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001713 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001714
1715 if (argc == 1)
1716 ret = argv[0];
1717 else if (argc > 1)
1718 ret = rb_ary_new4(argc, argv);
1719 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720}
1721
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001722 static void
1723ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725#ifndef DYNAMIC_RUBY
1726 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001727 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728#endif
1729
1730 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001731 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001733 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001734 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1735 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 rb_define_global_function("p", f_p, -1);
1737}
1738
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001739 static void
1740ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
1742 objtbl = rb_hash_new();
1743 rb_global_variable(&objtbl);
1744
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001745 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001746 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001747 mVIM = rb_define_module("Vim");
1748 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1750 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1751 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1752 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1753 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1754 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1755 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1756 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1757 rb_define_module_function(mVIM, "message", vim_message, 1);
1758 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1759 rb_define_module_function(mVIM, "command", vim_command, 1);
1760 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001761 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762
1763 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1764 rb_eStandardError);
1765 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1766 rb_eStandardError);
1767
1768 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1769 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1770 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1771 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1772 rb_define_method(cBuffer, "name", buffer_name, 0);
1773 rb_define_method(cBuffer, "number", buffer_number, 0);
1774 rb_define_method(cBuffer, "count", buffer_count, 0);
1775 rb_define_method(cBuffer, "length", buffer_count, 0);
1776 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1777 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1778 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1779 rb_define_method(cBuffer, "append", buffer_append, 2);
1780
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001781 /* Added line manipulation functions
1782 * SegPhault - 03/07/05 */
1783 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1784 rb_define_method(cBuffer, "line", line_s_current, 0);
1785 rb_define_method(cBuffer, "line=", set_current_line, 1);
1786
1787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1789 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1790 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1791 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1792 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1793 rb_define_method(cVimWindow, "height", window_height, 0);
1794 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001795 rb_define_method(cVimWindow, "width", window_width, 0);
1796 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1798 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1799
1800 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1801 rb_define_virtual_variable("$curwin", window_s_current, 0);
1802}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001803
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001804 void
1805vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001806{
1807 /* should get machine stack start address early in main function */
1808 ruby_stack_start = stack_start;
1809}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001810
1811 static int
1812convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1813{
1814 dict_T *d = (dict_T *)arg;
1815 dictitem_T *di;
1816
1817 di = dictitem_alloc((char_u *)RSTRING_PTR(RSTRING(rb_obj_as_string(key))));
1818 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1819 || dict_add(d, di) != OK)
1820 {
1821 d->dv_hashtab.ht_error = TRUE;
1822 return ST_STOP;
1823 }
1824 return ST_CONTINUE;
1825}
1826
1827 static int
1828ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1829{
1830 switch (TYPE(val))
1831 {
1832 case T_NIL:
1833 rettv->v_type = VAR_SPECIAL;
1834 rettv->vval.v_number = VVAL_NULL;
1835 break;
1836 case T_TRUE:
1837 rettv->v_type = VAR_SPECIAL;
1838 rettv->vval.v_number = VVAL_TRUE;
1839 break;
1840 case T_FALSE:
1841 rettv->v_type = VAR_SPECIAL;
1842 rettv->vval.v_number = VVAL_FALSE;
1843 break;
1844 case T_BIGNUM:
1845 case T_FIXNUM:
1846 rettv->v_type = VAR_NUMBER;
1847 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1848 break;
1849#ifdef FEAT_FLOAT
1850 case T_FLOAT:
1851 rettv->v_type = VAR_FLOAT;
1852 rettv->vval.v_float = (float_T)NUM2DBL(val);
1853 break;
1854#endif
1855 default:
1856 val = rb_obj_as_string(val);
1857 // FALLTHROUGH
1858 case T_STRING:
1859 {
1860 VALUE str = (VALUE)RSTRING(val);
1861
1862 rettv->v_type = VAR_STRING;
1863 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
1864 (int)RSTRING_LEN(str));
1865 }
1866 break;
1867 case T_ARRAY:
1868 {
1869 list_T *l;
1870 long i;
1871 typval_T v;
1872
1873 l = list_alloc();
1874 if (l == NULL)
1875 return FAIL;
1876
1877 for (i = 0; i < RARRAY_LEN(val); ++i)
1878 {
1879 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1880 &v) != OK)
1881 {
1882 list_unref(l);
1883 return FAIL;
1884 }
1885 list_append_tv(l, &v);
1886 clear_tv(&v);
1887 }
1888
1889 rettv->v_type = VAR_LIST;
1890 rettv->vval.v_list = l;
1891 ++l->lv_refcount;
1892 }
1893 break;
1894 case T_HASH:
1895 {
1896 dict_T *d;
1897
1898 d = dict_alloc();
1899 if (d == NULL)
1900 return FAIL;
1901
1902 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1903 if (d->dv_hashtab.ht_error)
1904 {
1905 dict_unref(d);
1906 return FAIL;
1907 }
1908
1909 rettv->v_type = VAR_DICT;
1910 rettv->vval.v_dict = d;
1911 ++d->dv_refcount;
1912 }
1913 break;
1914 }
1915 return OK;
1916}
1917
1918 void
1919do_rubyeval(char_u *str, typval_T *rettv)
1920{
1921 int retval = FAIL;
1922
1923 if (ensure_ruby_initialized())
1924 {
1925 int state;
1926 VALUE obj;
1927
1928 obj = rb_eval_string_protect((const char *)str, &state);
1929 if (state)
1930 error_print(state);
1931 else
1932 retval = ruby_convert_to_vim_value(obj, rettv);
1933 }
1934 if (retval == FAIL)
1935 {
1936 rettv->v_type = VAR_NUMBER;
1937 rettv->vval.v_number = 0;
1938 }
1939}