blob: 2727524f89a70e7dd13b95a31b64e992ef59b709 [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)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200849 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100850 }
851#endif
852 return rb_str_new2(s);
853}
854
855 static VALUE
856eval_enc_string_protect(const char *str, int *state)
857{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100858#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100859 int isnum;
860 long lval;
861 char_u *sval;
862 rb_encoding *enc;
863 VALUE v;
864
865 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
866 if (isnum == 0)
867 {
868 enc = rb_enc_find((char *)sval);
869 vim_free(sval);
870 if (enc)
871 {
872 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
873 return rb_eval_string_protect(StringValuePtr(v), state);
874 }
875 }
876#endif
877 return rb_eval_string_protect(str, state);
878}
879
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100880 void
881ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882{
883 int state;
884 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100885 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886
887 if (ensure_ruby_initialized())
888 {
889 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
890 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200891 for (i = eap->line1; i <= eap->line2; i++)
892 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100893 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100895 if (i > curbuf->b_ml.ml_line_count)
896 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100897 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100899 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200900 if (state)
901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 error_print(state);
903 break;
904 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100905 if (was_curbuf != curbuf)
906 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200908 if (!NIL_P(line))
909 {
910 if (TYPE(line) != T_STRING)
911 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100912 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 return;
914 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100915 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 changed();
917#ifdef SYNTAX_HL
918 syn_changed(i); /* recompute syntax hl. for this line */
919#endif
920 }
921 }
922 check_cursor();
923 update_curbuf(NOT_VALID);
924 }
925}
926
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100927 static VALUE
928rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100929{
930 rb_load(file_to_load, 0);
931 return Qnil;
932}
933
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100934 void
935ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936{
937 int state;
938
939 if (ensure_ruby_initialized())
940 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100941 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
942 rb_protect(rb_load_wrap, file_to_load, &state);
943 if (state)
944 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 }
946}
947
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100948 void
949ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000951 if (buf->b_ruby_ref)
952 {
953 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
954 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 }
956}
957
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100958 void
959ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000961 if (win->w_ruby_ref)
962 {
963 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
964 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966}
967
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100968 static int
969ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
971 if (!ruby_initialized)
972 {
973#ifdef DYNAMIC_RUBY
974 if (ruby_enabled(TRUE))
975 {
976#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100977#ifdef MSWIN
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100978 /* suggested by Ariya Mizutani */
979 int argc = 1;
980 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100981 char **argvp = argv;
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100982# ifdef RUBY19_OR_LATER
983 ruby_sysinit(&argc, &argvp);
984# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100985 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100986# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100987#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200988 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200989#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200990 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100991#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200992 ruby_init();
993 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100994#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100995 {
996 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200997 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100998 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100999 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001000 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001001#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001003#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001004 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 ruby_vim_init();
1006 ruby_initialized = 1;
1007#ifdef DYNAMIC_RUBY
1008 }
1009 else
1010 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001011 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 return 0;
1013 }
1014#endif
1015 }
1016 return ruby_initialized;
1017}
1018
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001019 static void
1020error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
Bram Moolenaarb09c6842018-12-27 22:11:01 +01001022#if !defined(DYNAMIC_RUBY) && !defined(RUBY19_OR_LATER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 RUBYEXTERN VALUE ruby_errinfo;
1024#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001025 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 VALUE eclass;
1027 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001028 VALUE bt;
1029 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001031 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033#define TAG_RETURN 0x1
1034#define TAG_BREAK 0x2
1035#define TAG_NEXT 0x3
1036#define TAG_RETRY 0x4
1037#define TAG_REDO 0x5
1038#define TAG_RAISE 0x6
1039#define TAG_THROW 0x7
1040#define TAG_FATAL 0x8
1041#define TAG_MASK 0xf
1042
Bram Moolenaar758535a2016-03-30 22:06:16 +02001043 switch (state)
1044 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001045 case TAG_RETURN:
1046 emsg(_("E267: unexpected return"));
1047 break;
1048 case TAG_NEXT:
1049 emsg(_("E268: unexpected next"));
1050 break;
1051 case TAG_BREAK:
1052 emsg(_("E269: unexpected break"));
1053 break;
1054 case TAG_REDO:
1055 emsg(_("E270: unexpected redo"));
1056 break;
1057 case TAG_RETRY:
1058 emsg(_("E271: retry outside of rescue clause"));
1059 break;
1060 case TAG_RAISE:
1061 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +01001062#ifdef RUBY19_OR_LATER
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001063 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001064#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001065 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001066#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001067 eclass = CLASS_OF(error);
1068 einfo = rb_obj_as_string(error);
1069 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1070 {
1071 emsg(_("E272: unhandled exception"));
1072 }
1073 else
1074 {
1075 VALUE epath;
1076 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001078 epath = rb_class_path(eclass);
1079 vim_snprintf(buff, BUFSIZ, "%s: %s",
1080 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1081 p = strchr(buff, '\n');
1082 if (p) *p = '\0';
1083 emsg(buff);
1084 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001085
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001086 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001087# ifdef RUBY21_OR_LATER
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001088 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1089 for (i = 0; i < RARRAY_LEN(bt); i++)
1090 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001091# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001092 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1093 for (i = 0; i < RARRAY_LEN(bt); i++)
1094 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001095# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001096 break;
1097 default:
1098 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1099 emsg(buff);
1100 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 }
1102}
1103
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001104 static VALUE
1105vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106{
1107 char *buff, *p;
1108
1109 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001110 if (RSTRING_LEN(str) > 0)
1111 {
1112 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001113 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001114 strcpy(buff, RSTRING_PTR(str));
1115 p = strchr(buff, '\n');
1116 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001117 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001118 }
1119 else
1120 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001121 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 return Qnil;
1124}
1125
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001126 static VALUE
1127vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001129 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 update_screen(NOT_VALID);
1131 return Qnil;
1132}
1133
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001134 static VALUE
1135vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001137 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 return Qnil;
1139}
1140
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001141#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001142 static VALUE
1143vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001144{
1145 VALUE result = Qnil;
1146
1147 if (tv->v_type == VAR_STRING)
1148 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001149 result = rb_str_new2(tv->vval.v_string == NULL
1150 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001151 }
1152 else if (tv->v_type == VAR_NUMBER)
1153 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001154 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001155 }
1156# ifdef FEAT_FLOAT
1157 else if (tv->v_type == VAR_FLOAT)
1158 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001159 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001160 }
1161# endif
1162 else if (tv->v_type == VAR_LIST)
1163 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001164 list_T *list = tv->vval.v_list;
1165 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001166
Bram Moolenaar639a2552010-03-17 18:15:23 +01001167 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001168
Bram Moolenaar639a2552010-03-17 18:15:23 +01001169 if (list != NULL)
1170 {
1171 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001172 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001173 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001174 }
1175 else if (tv->v_type == VAR_DICT)
1176 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001177 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001178
Bram Moolenaar639a2552010-03-17 18:15:23 +01001179 if (tv->vval.v_dict != NULL)
1180 {
1181 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1182 long_u todo = ht->ht_used;
1183 hashitem_T *hi;
1184 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001185
Bram Moolenaar639a2552010-03-17 18:15:23 +01001186 for (hi = ht->ht_array; todo > 0; ++hi)
1187 {
1188 if (!HASHITEM_EMPTY(hi))
1189 {
1190 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001191
Bram Moolenaar639a2552010-03-17 18:15:23 +01001192 di = dict_lookup(hi);
1193 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001194 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001195 }
1196 }
1197 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001198 }
1199 else if (tv->v_type == VAR_SPECIAL)
1200 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001201 if (tv->vval.v_number == VVAL_TRUE)
1202 result = Qtrue;
1203 else if (tv->vval.v_number == VVAL_FALSE)
1204 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001205 }
1206 else if (tv->v_type == VAR_BLOB)
1207 {
1208 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1209 tv->vval.v_blob->bv_ga.ga_len);
1210 }
1211 /* else return Qnil; */
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001212
1213 return result;
1214}
1215#endif
1216
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001217 static VALUE
1218vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219{
1220#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001221 typval_T *tv;
1222 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001224 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1225 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001226 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001227 result = vim_to_ruby(tv);
1228
1229 free_tv(tv);
1230
1231 return result;
1232#else
1233 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235}
1236
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001237#ifdef USE_TYPEDDATA
1238static size_t buffer_dsize(const void *buf);
1239
1240static const rb_data_type_t buffer_type = {
1241 "vim_buffer",
1242 {0, 0, buffer_dsize, {0, 0}},
1243 0, 0,
1244# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1245 0,
1246# endif
1247};
1248
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001249 static size_t
1250buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001251{
1252 return sizeof(buf_T);
1253}
1254#endif
1255
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001256 static VALUE
1257buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001259 if (buf->b_ruby_ref)
1260 {
1261 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001263 else
1264 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001265#ifdef USE_TYPEDDATA
1266 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1267#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001269#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001270 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1272 return obj;
1273 }
1274}
1275
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001276 static buf_T *
1277get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278{
1279 buf_T *buf;
1280
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001281#ifdef USE_TYPEDDATA
1282 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1283#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (buf == NULL)
1287 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1288 return buf;
1289}
1290
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001291 static VALUE
1292vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001293{
1294 VALUE result = rb_str_new("0z", 2);
1295 char buf[4];
1296 int i;
1297 for (i = 0; i < RSTRING_LEN(str); i++)
1298 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001299 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001300 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001301 }
1302 return result;
1303}
1304
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001305 static VALUE
1306buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307{
1308 return buffer_new(curbuf);
1309}
1310
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001311 static VALUE
1312buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
1314 buf_T *b;
1315 int n = 0;
1316
Bram Moolenaar29323592016-07-24 22:04:11 +02001317 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001318 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001319 /* Deleted buffers should not be counted
1320 * SegPhault - 01/07/05 */
1321 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001322 n++;
1323 }
1324
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 return INT2NUM(n);
1326}
1327
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001328 static VALUE
1329buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330{
1331 buf_T *b;
1332 int n = NUM2INT(num);
1333
Bram Moolenaar29323592016-07-24 22:04:11 +02001334 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001335 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001336 /* Deleted buffers should not be counted
1337 * SegPhault - 01/07/05 */
1338 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001339 continue;
1340
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001341 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001343
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001344 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
1346 return Qnil;
1347}
1348
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001349 static VALUE
1350buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351{
1352 buf_T *buf = get_buf(self);
1353
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001354 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355}
1356
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001357 static VALUE
1358buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359{
1360 buf_T *buf = get_buf(self);
1361
1362 return INT2NUM(buf->b_fnum);
1363}
1364
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001365 static VALUE
1366buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367{
1368 buf_T *buf = get_buf(self);
1369
1370 return INT2NUM(buf->b_ml.ml_line_count);
1371}
1372
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001373 static VALUE
1374get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001376 if (n <= 0 || n > buf->b_ml.ml_line_count)
1377 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1378 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379}
1380
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001381 static VALUE
1382buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
1384 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001385
1386 if (buf != NULL)
1387 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1388 return Qnil; /* For stop warning */
1389}
1390
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001391 static VALUE
1392set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001393{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001394 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001395 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001397 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1398 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001399 /* set curwin/curbuf for "buf" and save some things */
1400 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001401
Bram Moolenaar758535a2016-03-30 22:06:16 +02001402 if (u_savesub(n) == OK)
1403 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001404 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 changed();
1406#ifdef SYNTAX_HL
1407 syn_changed(n); /* recompute syntax hl. for this line */
1408#endif
1409 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001410
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001411 /* restore curwin/curbuf and a few other things */
1412 aucmd_restbuf(&aco);
1413 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 update_curbuf(NOT_VALID);
1416 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001417 else
1418 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001419 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
1421 return str;
1422}
1423
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001424 static VALUE
1425buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001426{
1427 buf_T *buf = get_buf(self);
1428
1429 if (buf != NULL)
1430 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1431 return str;
1432}
1433
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001434 static VALUE
1435buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001437 buf_T *buf = get_buf(self);
1438 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001439 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001441 if (n > 0 && n <= buf->b_ml.ml_line_count)
1442 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001443 /* set curwin/curbuf for "buf" and save some things */
1444 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001445
Bram Moolenaar758535a2016-03-30 22:06:16 +02001446 if (u_savedel(n, 1) == OK)
1447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001449
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001450 /* Changes to non-active buffers should properly refresh
1451 * SegPhault - 01/09/05 */
1452 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 changed();
1455 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001456
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001457 /* restore curwin/curbuf and a few other things */
1458 aucmd_restbuf(&aco);
1459 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001460
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 update_curbuf(NOT_VALID);
1462 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001463 else
1464 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001465 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 }
1467 return Qnil;
1468}
1469
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001470 static VALUE
1471buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001473 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001474 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001475 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001476 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
Bram Moolenaar3c531602010-11-16 14:46:19 +01001478 if (line == NULL)
1479 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001480 rb_raise(rb_eIndexError, "NULL line");
1481 }
1482 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001483 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001484 /* set curwin/curbuf for "buf" and save some things */
1485 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001486
Bram Moolenaar758535a2016-03-30 22:06:16 +02001487 if (u_inssub(n + 1) == OK)
1488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001490
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001491 /* Changes to non-active buffers should properly refresh screen
1492 * SegPhault - 12/20/04 */
1493 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001494
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001495 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001497
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001498 /* restore curwin/curbuf and a few other things */
1499 aucmd_restbuf(&aco);
1500 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001501
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 update_curbuf(NOT_VALID);
1503 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001504 else
1505 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001506 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 }
1508 return str;
1509}
1510
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001511#ifdef USE_TYPEDDATA
1512static size_t window_dsize(const void *buf);
1513
1514static const rb_data_type_t window_type = {
1515 "vim_window",
1516 {0, 0, window_dsize, {0, 0}},
1517 0, 0,
1518# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1519 0,
1520# endif
1521};
1522
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001523 static size_t
1524window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001525{
1526 return sizeof(win_T);
1527}
1528#endif
1529
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001530 static VALUE
1531window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001533 if (win->w_ruby_ref)
1534 {
1535 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001537 else
1538 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001539#ifdef USE_TYPEDDATA
1540 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1541#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001543#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001544 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1546 return obj;
1547 }
1548}
1549
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001550 static win_T *
1551get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 win_T *win;
1554
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001555#ifdef USE_TYPEDDATA
1556 TypedData_Get_Struct(obj, win_T, &window_type, win);
1557#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 if (win == NULL)
1561 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1562 return win;
1563}
1564
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001565 static VALUE
1566window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567{
1568 return window_new(curwin);
1569}
1570
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001571/*
1572 * Added line manipulation functions
1573 * SegPhault - 03/07/05
1574 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001575 static VALUE
1576line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001577{
1578 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1579}
1580
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001581 static VALUE
1582set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001583{
1584 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1585}
1586
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001587 static VALUE
1588current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001589{
1590 return INT2FIX((int)curwin->w_cursor.lnum);
1591}
1592
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001593 static VALUE
1594window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 win_T *w;
1597 int n = 0;
1598
Bram Moolenaar29323592016-07-24 22:04:11 +02001599 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 n++;
1601 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602}
1603
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001604 static VALUE
1605window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
1607 win_T *w;
1608 int n = NUM2INT(num);
1609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 if (n == 0)
1612 return window_new(w);
1613 return Qnil;
1614}
1615
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001616 static VALUE
1617window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
1619 win_T *win = get_win(self);
1620
1621 return buffer_new(win->w_buffer);
1622}
1623
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001624 static VALUE
1625window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626{
1627 win_T *win = get_win(self);
1628
1629 return INT2NUM(win->w_height);
1630}
1631
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001632 static VALUE
1633window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
1635 win_T *win = get_win(self);
1636 win_T *savewin = curwin;
1637
1638 curwin = win;
1639 win_setheight(NUM2INT(height));
1640 curwin = savewin;
1641 return height;
1642}
1643
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001644 static VALUE
1645window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001646{
Bram Moolenaare745d752017-09-22 16:56:22 +02001647 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001648}
1649
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001650 static VALUE
1651window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001652{
1653 win_T *win = get_win(self);
1654 win_T *savewin = curwin;
1655
1656 curwin = win;
1657 win_setwidth(NUM2INT(width));
1658 curwin = savewin;
1659 return width;
1660}
1661
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001662 static VALUE
1663window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664{
1665 win_T *win = get_win(self);
1666
1667 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1668}
1669
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001670 static VALUE
1671window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672{
1673 VALUE lnum, col;
1674 win_T *win = get_win(self);
1675
1676 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001677 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001679 lnum = RARRAY_PTR(pos)[0];
1680 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 win->w_cursor.lnum = NUM2LONG(lnum);
1682 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001683 win->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 check_cursor(); /* put cursor on an existing line */
1685 update_screen(NOT_VALID);
1686 return Qnil;
1687}
1688
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001689 static VALUE
1690f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001691{
1692 return Qnil;
1693}
1694
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001695 static VALUE
1696f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
1698 int i;
1699 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001700 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaar758535a2016-03-30 22:06:16 +02001702 for (i = 0; i < argc; i++)
1703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 if (i > 0) rb_str_cat(str, ", ", 2);
1705 rb_str_concat(str, rb_inspect(argv[i]));
1706 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001707 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001708
1709 if (argc == 1)
1710 ret = argv[0];
1711 else if (argc > 1)
1712 ret = rb_ary_new4(argc, argv);
1713 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714}
1715
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001716 static void
1717ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719#ifndef DYNAMIC_RUBY
1720 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001721 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722#endif
1723
1724 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001725 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001727 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001728 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1729 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 rb_define_global_function("p", f_p, -1);
1731}
1732
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001733 static void
1734ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735{
1736 objtbl = rb_hash_new();
1737 rb_global_variable(&objtbl);
1738
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001739 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001740 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001741 mVIM = rb_define_module("Vim");
1742 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1744 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1745 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1746 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1747 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1748 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1749 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1750 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1751 rb_define_module_function(mVIM, "message", vim_message, 1);
1752 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1753 rb_define_module_function(mVIM, "command", vim_command, 1);
1754 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001755 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756
1757 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1758 rb_eStandardError);
1759 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1760 rb_eStandardError);
1761
1762 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1763 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1764 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1765 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1766 rb_define_method(cBuffer, "name", buffer_name, 0);
1767 rb_define_method(cBuffer, "number", buffer_number, 0);
1768 rb_define_method(cBuffer, "count", buffer_count, 0);
1769 rb_define_method(cBuffer, "length", buffer_count, 0);
1770 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1771 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1772 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1773 rb_define_method(cBuffer, "append", buffer_append, 2);
1774
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001775 /* Added line manipulation functions
1776 * SegPhault - 03/07/05 */
1777 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1778 rb_define_method(cBuffer, "line", line_s_current, 0);
1779 rb_define_method(cBuffer, "line=", set_current_line, 1);
1780
1781
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1783 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1784 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1785 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1786 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1787 rb_define_method(cVimWindow, "height", window_height, 0);
1788 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001789 rb_define_method(cVimWindow, "width", window_width, 0);
1790 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1792 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1793
1794 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1795 rb_define_virtual_variable("$curwin", window_s_current, 0);
1796}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001797
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001798 void
1799vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001800{
1801 /* should get machine stack start address early in main function */
1802 ruby_stack_start = stack_start;
1803}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001804
1805 static int
1806convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1807{
1808 dict_T *d = (dict_T *)arg;
1809 dictitem_T *di;
1810
1811 di = dictitem_alloc((char_u *)RSTRING_PTR(RSTRING(rb_obj_as_string(key))));
1812 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1813 || dict_add(d, di) != OK)
1814 {
1815 d->dv_hashtab.ht_error = TRUE;
1816 return ST_STOP;
1817 }
1818 return ST_CONTINUE;
1819}
1820
1821 static int
1822ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1823{
1824 switch (TYPE(val))
1825 {
1826 case T_NIL:
1827 rettv->v_type = VAR_SPECIAL;
1828 rettv->vval.v_number = VVAL_NULL;
1829 break;
1830 case T_TRUE:
1831 rettv->v_type = VAR_SPECIAL;
1832 rettv->vval.v_number = VVAL_TRUE;
1833 break;
1834 case T_FALSE:
1835 rettv->v_type = VAR_SPECIAL;
1836 rettv->vval.v_number = VVAL_FALSE;
1837 break;
1838 case T_BIGNUM:
1839 case T_FIXNUM:
1840 rettv->v_type = VAR_NUMBER;
1841 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1842 break;
1843#ifdef FEAT_FLOAT
1844 case T_FLOAT:
1845 rettv->v_type = VAR_FLOAT;
1846 rettv->vval.v_float = (float_T)NUM2DBL(val);
1847 break;
1848#endif
1849 default:
1850 val = rb_obj_as_string(val);
1851 // FALLTHROUGH
1852 case T_STRING:
1853 {
1854 VALUE str = (VALUE)RSTRING(val);
1855
1856 rettv->v_type = VAR_STRING;
1857 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
1858 (int)RSTRING_LEN(str));
1859 }
1860 break;
1861 case T_ARRAY:
1862 {
1863 list_T *l;
1864 long i;
1865 typval_T v;
1866
1867 l = list_alloc();
1868 if (l == NULL)
1869 return FAIL;
1870
1871 for (i = 0; i < RARRAY_LEN(val); ++i)
1872 {
1873 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1874 &v) != OK)
1875 {
1876 list_unref(l);
1877 return FAIL;
1878 }
1879 list_append_tv(l, &v);
1880 clear_tv(&v);
1881 }
1882
1883 rettv->v_type = VAR_LIST;
1884 rettv->vval.v_list = l;
1885 ++l->lv_refcount;
1886 }
1887 break;
1888 case T_HASH:
1889 {
1890 dict_T *d;
1891
1892 d = dict_alloc();
1893 if (d == NULL)
1894 return FAIL;
1895
1896 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1897 if (d->dv_hashtab.ht_error)
1898 {
1899 dict_unref(d);
1900 return FAIL;
1901 }
1902
1903 rettv->v_type = VAR_DICT;
1904 rettv->vval.v_dict = d;
1905 ++d->dv_refcount;
1906 }
1907 break;
1908 }
1909 return OK;
1910}
1911
1912 void
1913do_rubyeval(char_u *str, typval_T *rettv)
1914{
1915 int retval = FAIL;
1916
1917 if (ensure_ruby_initialized())
1918 {
1919 int state;
1920 VALUE obj;
1921
1922 obj = rb_eval_string_protect((const char *)str, &state);
1923 if (state)
1924 error_print(state);
1925 else
1926 retval = ruby_convert_to_vim_value(obj, rettv);
1927 }
1928 if (retval == FAIL)
1929 {
1930 rettv->v_type = VAR_NUMBER;
1931 rettv->vval.v_number = 0;
1932 }
1933}