blob: a4a59f6bda762767f463b3b89be814af0251324e [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
Bram Moolenaar41a41412020-01-07 21:32:19 +010023# if !defined(DYNAMIC_RUBY) || (RUBY_VERSION < 18)
Bram Moolenaar49c99fc2020-02-11 23:01:39 +010024# define NT
Bram Moolenaar071d4272004-06-13 20:20:40 +000025# endif
26# ifndef DYNAMIC_RUBY
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010027# define IMPORT // For static dll usage __declspec(dllimport)
Bram Moolenaar071d4272004-06-13 20:20:40 +000028# define RUBYEXTERN __declspec(dllimport)
29# endif
30#endif
31#ifndef RUBYEXTERN
32# define RUBYEXTERN extern
33#endif
34
Bram Moolenaar41a41412020-01-07 21:32:19 +010035#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 24
Bram Moolenaar2016ae52016-06-13 20:08:43 +020036# 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 Moolenaar41a41412020-01-07 21:32:19 +010050# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +010051# 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)
Bram Moolenaar41a41412020-01-07 21:32:19 +010057# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +000058/*
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 Moolenaar41a41412020-01-07 21:32:19 +010074#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 19
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010075// Ruby 1.9 defines a number of static functions which use rb_num2long and
76// rb_int2big
Bram Moolenaar42d57f02010-03-10 12:47:00 +010077# define rb_num2long rb_num2long_stub
78# define rb_int2big rb_int2big_stub
79#endif
80
Bram Moolenaar41a41412020-01-07 21:32:19 +010081#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 19 \
Bram Moolenaar498af702014-03-28 21:58:21 +010082 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010083// Ruby 1.9 defines a number of static functions which use rb_fix2int and
84// rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +020085# define rb_fix2int rb_fix2int_stub
86# define rb_num2int rb_num2int_stub
87#endif
88
Bram Moolenaar41a41412020-01-07 21:32:19 +010089#if defined(DYNAMIC_RUBY) && RUBY_VERSION == 21
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010090// Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
91// rb_gc_writebarrier_unprotect_promoted if USE_RGENGC
Bram Moolenaar90140742014-11-27 17:44:08 +010092# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
93#endif
Bram Moolenaar41a41412020-01-07 21:32:19 +010094#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 22
Bram Moolenaar0c7485f2015-01-14 14:04:10 +010095# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaar0c7485f2015-01-14 14:04:10 +010096#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +010097
Bram Moolenaar41a41412020-01-07 21:32:19 +010098#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 26
Bram Moolenaarf62fc312019-01-08 20:29:32 +010099# define rb_ary_detransient rb_ary_detransient_stub
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100100#endif
101
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#include <ruby.h>
Bram Moolenaar41a41412020-01-07 21:32:19 +0100103#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100104# include <ruby/encoding.h>
105#endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100106#if RUBY_VERSION <= 18
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200107# include <st.h> // for ST_STOP and ST_CONTINUE
108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200110#undef off_t // ruby defines off_t as _int64, Mingw uses long
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#undef EXTERN
112#undef _
113
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100114// T_DATA defined both by Ruby and Mac header files, hack around it...
Bram Moolenaard0573012017-10-28 21:11:06 +0200115#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116# define __OPENTRANSPORT__
117# define __OPENTRANSPORTPROTOCOL__
118# define __OPENTRANSPORTPROVIDERS__
119#endif
120
Bram Moolenaar165641d2010-02-17 16:23:09 +0100121/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100122 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
123 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
124 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100125 * Use TypedData_XXX if available.
126 */
Bram Moolenaar41a41412020-01-07 21:32:19 +0100127#if defined(TypedData_Wrap_Struct) && (RUBY_VERSION >= 20)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100128# define USE_TYPEDDATA 1
129#endif
130
131/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200132 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100133 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
134 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
135 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
136 */
137#ifndef StringValuePtr
138# define StringValuePtr(s) STR2CSTR(s)
139#endif
140#ifndef RARRAY_LEN
141# define RARRAY_LEN(s) RARRAY(s)->len
142#endif
143#ifndef RARRAY_PTR
144# define RARRAY_PTR(s) RARRAY(s)->ptr
145#endif
146#ifndef RSTRING_LEN
147# define RSTRING_LEN(s) RSTRING(s)->len
148#endif
149#ifndef RSTRING_PTR
150# define RSTRING_PTR(s) RSTRING(s)->ptr
151#endif
152
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100153#ifdef HAVE_DUP
154# undef HAVE_DUP
155#endif
156
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#include "vim.h"
158#include "version.h"
159
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100160#ifdef DYNAMIC_RUBY
161# if !defined(MSWIN) // must come after including vim.h, where it is defined
162# include <dlfcn.h>
163# define HINSTANCE void*
164# define RUBY_PROC void*
165# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
166# define symbol_from_dll dlsym
167# define close_dll dlclose
168# else
169# define RUBY_PROC FARPROC
170# define load_dll vimLoadLib
171# define symbol_from_dll GetProcAddress
172# define close_dll FreeLibrary
173# endif
174#endif
175
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#if defined(PROTO) && !defined(FEAT_RUBY)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100177// Define these to be able to generate the function prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178# define VALUE int
179# define RUBY_DATA_FUNC int
180#endif
181
182static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200183static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184static VALUE objtbl;
185
186static VALUE mVIM;
187static VALUE cBuffer;
188static VALUE cVimWindow;
189static VALUE eDeletedBufferError;
190static VALUE eDeletedWindowError;
191
192static int ensure_ruby_initialized(void);
193static void error_print(int);
194static void ruby_io_init(void);
195static void ruby_vim_init(void);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100196static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaar41a41412020-01-07 21:32:19 +0100198#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200199# if defined(__ia64) && !defined(ruby_init_stack)
200# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
201# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#endif
203
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200204#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100205# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100206# define HINSTANCE int // for generating prototypes
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200207# endif
208
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209/*
210 * Wrapper defines
211 */
Bram Moolenaar8b430b42020-02-22 15:01:00 +0100212// Ruby 2.7 actually expands the following symbols as macro.
213# if RUBY_VERSION >= 27
214# undef rb_define_global_function
215# undef rb_define_method
216# undef rb_define_module_function
217# undef rb_define_singleton_method
218# endif
219
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200220# define rb_assoc_new dll_rb_assoc_new
221# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100222# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100223# define rb_check_type dll_rb_check_type
224# ifdef USE_TYPEDDATA
225# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100226# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200227# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100228# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100229# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100230# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
231# else
232# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
233# endif
234# else
235# define rb_data_object_alloc dll_rb_data_object_alloc
236# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200237# define rb_define_class_under dll_rb_define_class_under
238# define rb_define_const dll_rb_define_const
239# define rb_define_global_function dll_rb_define_global_function
240# define rb_define_method dll_rb_define_method
241# define rb_define_module dll_rb_define_module
242# define rb_define_module_function dll_rb_define_module_function
243# define rb_define_singleton_method dll_rb_define_singleton_method
244# define rb_define_virtual_variable dll_rb_define_virtual_variable
245# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200246# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200247# define rb_eArgError (*dll_rb_eArgError)
248# define rb_eIndexError (*dll_rb_eIndexError)
249# define rb_eRuntimeError (*dll_rb_eRuntimeError)
250# define rb_eStandardError (*dll_rb_eStandardError)
251# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaar41a41412020-01-07 21:32:19 +0100252# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200253# define rb_funcallv dll_rb_funcallv
254# else
255# define rb_funcall2 dll_rb_funcall2
256# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200257# define rb_global_variable dll_rb_global_variable
258# define rb_hash_aset dll_rb_hash_aset
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100259# define rb_hash_foreach dll_rb_hash_foreach
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200260# define rb_hash_new dll_rb_hash_new
261# define rb_inspect dll_rb_inspect
262# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200263
264// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
265// work. Not using the cache appears to be the best solution.
266# undef rb_intern
267# define rb_intern dll_rb_intern
268
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100269# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar41a41412020-01-07 21:32:19 +0100270# if RUBY_VERSION <= 18
Bram Moolenaar498af702014-03-28 21:58:21 +0100271# define rb_fix2int dll_rb_fix2int
272# define rb_num2int dll_rb_num2int
273# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200274# define rb_num2uint dll_rb_num2uint
275# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100276# define rb_num2dbl dll_rb_num2dbl
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200277# define rb_lastline_get dll_rb_lastline_get
278# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200279# define rb_protect dll_rb_protect
280# define rb_load dll_rb_load
Bram Moolenaar41a41412020-01-07 21:32:19 +0100281# if RUBY_VERSION <= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200282# define rb_num2long dll_rb_num2long
283# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100284# if RUBY_VERSION <= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200285# define rb_num2ulong dll_rb_num2ulong
286# endif
287# define rb_obj_alloc dll_rb_obj_alloc
288# define rb_obj_as_string dll_rb_obj_as_string
289# define rb_obj_id dll_rb_obj_id
290# define rb_raise dll_rb_raise
291# define rb_str_cat dll_rb_str_cat
292# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100293# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200294# define rb_str_new dll_rb_str_new
295# ifdef rb_str_new2
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100296// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200297# define need_rb_str_new_cstr 1
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100298// Ruby's headers #define rb_str_new_cstr to make use of GCC's
299// __builtin_constant_p extension.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200300# undef rb_str_new_cstr
301# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200302# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200303# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200304# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100305# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200306# define rb_string_value dll_rb_string_value
307# define rb_string_value_ptr dll_rb_string_value_ptr
308# define rb_float_new dll_rb_float_new
309# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200310# ifdef rb_ary_new4
Bram Moolenaar49c99fc2020-02-11 23:01:39 +0100311# define RB_ARY_NEW4_MACRO 1
312# undef rb_ary_new4
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200313# endif
314# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200315# define rb_ary_push dll_rb_ary_push
Bram Moolenaar41a41412020-01-07 21:32:19 +0100316# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200317# ifdef __ia64
318# define rb_ia64_bsp dll_rb_ia64_bsp
319# undef ruby_init_stack
320# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
321# else
322# define ruby_init_stack dll_ruby_init_stack
323# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200324# endif
325# else
326# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200327# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100328# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200329# define rb_errinfo dll_rb_errinfo
330# else
331# define ruby_errinfo (*dll_ruby_errinfo)
332# endif
333# define ruby_init dll_ruby_init
334# define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar4f974752019-02-17 17:44:42 +0100335# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100336# if RUBY_VERSION >= 19
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100337# define ruby_sysinit dll_ruby_sysinit
338# else
339# define NtInitialize dll_NtInitialize
340# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100341# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200342# define rb_w32_snprintf dll_rb_w32_snprintf
343# endif
344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
Bram Moolenaar41a41412020-01-07 21:32:19 +0100346# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200347# define ruby_script dll_ruby_script
348# define rb_enc_find_index dll_rb_enc_find_index
349# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100350# undef rb_enc_str_new
351# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200352# define rb_sprintf dll_rb_sprintf
353# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100354# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200355# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100356
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357/*
358 * Pointers for dynamic link
359 */
360static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200361VALUE *dll_rb_cFalseClass;
362VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200363# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200364VALUE *dll_rb_cInteger;
365# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100366# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100367VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200368# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200369VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100371VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200372VALUE *dll_rb_cSymbol;
373VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100374static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100376# ifdef USE_TYPEDDATA
377static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100380# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100381# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100382static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
383# else
384static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
385# endif
386# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
390static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
391static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
392static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
393static VALUE (*dll_rb_define_module) (const char*);
394static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
395static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
396static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
397static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200398static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399static VALUE *dll_rb_eArgError;
400static VALUE *dll_rb_eIndexError;
401static VALUE *dll_rb_eRuntimeError;
402static VALUE *dll_rb_eStandardError;
403static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100404# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200405static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
406# else
407static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409static void (*dll_rb_global_variable) (VALUE*);
410static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100411static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412static VALUE (*dll_rb_hash_new) (void);
413static VALUE (*dll_rb_inspect) (VALUE);
414static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200415static ID (*dll_rb_intern) (const char*);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100416# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200417static long (*dll_rb_fix2int) (VALUE);
418static long (*dll_rb_num2int) (VALUE);
419static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200420# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100421static double (*dll_rb_num2dbl) (VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422static VALUE (*dll_rb_lastline_get) (void);
423static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100424static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200425static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426static long (*dll_rb_num2long) (VALUE);
427static unsigned long (*dll_rb_num2ulong) (VALUE);
428static VALUE (*dll_rb_obj_alloc) (VALUE);
429static VALUE (*dll_rb_obj_as_string) (VALUE);
430static VALUE (*dll_rb_obj_id) (VALUE);
431static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100432# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200433static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200434# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
438static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
439static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200440# ifdef need_rb_str_new_cstr
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100441// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200442static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200443# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200445# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100446# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100447static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200448# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451static void (*dll_ruby_init) (void);
452static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100453# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100454# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100455static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100456# else
457static void (*dll_NtInitialize) (int*, char***);
458# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100459# if RUBY_VERSION >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200460static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200461# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200462# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100463# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100464static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
465static VALUE (*dll_rb_float_new) (double);
466static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200467static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100468static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100469# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100470static void (*dll_rb_ary_detransient) (VALUE);
471# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100472# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200473# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200474static void * (*dll_rb_ia64_bsp) (void);
475static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200476# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200477static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200478# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200479# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200480# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100481# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100482static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200483# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484
Bram Moolenaar41a41412020-01-07 21:32:19 +0100485# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100486static void (*dll_ruby_script) (const char*);
487static int (*dll_rb_enc_find_index) (const char*);
488static rb_encoding* (*dll_rb_enc_find) (const char*);
489static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
490static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100491static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100492static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200493# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100494
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100495# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100496# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100497static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100498# else
499static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
500# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100501# endif
502
Bram Moolenaar41a41412020-01-07 21:32:19 +0100503# if (RUBY_VERSION >= 19) && !defined(PROTO)
504# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100505 long
506rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200507# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100508 SIGNED_VALUE
509rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200510# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100511{
512 return dll_rb_num2long(x);
513}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100514# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100515 VALUE
516rb_int2big_stub(intptr_t x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100517# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100518 VALUE
519rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100520# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100521{
522 return dll_rb_int2big(x);
523}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100524# if (RUBY_VERSION >= 19) && (VIM_SIZEOF_INT < VIM_SIZEOF_LONG)
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100525 long
526rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200527{
528 return dll_rb_fix2int(x);
529}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100530 long
531rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200532{
533 return dll_rb_num2int(x);
534}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200535# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100536# if RUBY_VERSION >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100537 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100538rb_float_new_in_heap(double d)
539{
540 return dll_rb_float_new(d);
541}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100542# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100543 unsigned long
544rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200545# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100546 VALUE
547rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200548# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100549{
550 return (long)RSHIFT((SIGNED_VALUE)(x),1);
551}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200552# endif
553# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100554
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100555 // Do not generate a prototype here, VALUE isn't always defined.
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100556# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar41a41412020-01-07 21:32:19 +0100557# if RUBY_VERSION == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100558 void
559rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100560{
Bram Moolenaar90140742014-11-27 17:44:08 +0100561 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100562}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100563# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100564 void
565rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100566{
567 dll_rb_gc_writebarrier_unprotect(obj);
568}
569# endif
570# endif
571
Bram Moolenaar41a41412020-01-07 21:32:19 +0100572# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100573 void
574rb_ary_detransient_stub(VALUE x)
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100575{
576 dll_rb_ary_detransient(x);
577}
578# endif
579
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100580static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
582/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000583 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585static struct
586{
587 char *name;
588 RUBY_PROC *ptr;
589} ruby_funcname_table[] =
590{
591 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
592 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200593# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200594 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100595# else
596 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200597# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100598# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100599 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200600# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
602 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100603 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
605 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100606 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100608# ifdef USE_TYPEDDATA
609 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
610# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100612# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100613# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100614 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
615# else
616 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
617# endif
618# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100620# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
622 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
623 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
624 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
625 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
626 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
627 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
628 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
629 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200630 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
632 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
633 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
634 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
635 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100636# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200637 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
638# else
639 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
640# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
642 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100643 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
645 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
646 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200647 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100648# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200649 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
650 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
651 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200652# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100653 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
655 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200656 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
657 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
659 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
660 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
661 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
662 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
663 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100664# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200665 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200666# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
670 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
671 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200672# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200673 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200674# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200676# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100677# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100678 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200679# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200681# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
683 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100684# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100685# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100686 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100687# else
688 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200689# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100690# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200692# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200693# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100694# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100695 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100696# if RUBY_VERSION <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100697 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200698# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100699 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200700# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100701 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200702# ifdef RB_ARY_NEW4_MACRO
703 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
704# else
705 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
706# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100707 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100708# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100709 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
710# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200711# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100712# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100713 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100714 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
715 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
716 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
717 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
718 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100719 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100720 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200721# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100722# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200723# ifdef __ia64
724 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
725# endif
726 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
727# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100728# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100729# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100730 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100731# else
732 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
733# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100734# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 {"", NULL},
736};
737
738/*
739 * Free ruby.dll
740 */
741 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100742end_dynamic_ruby(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
744 if (hinstRuby)
745 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200746 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200747 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 }
749}
750
751/*
752 * Load library and get all pointers.
753 * Parameter 'libname' provides name of DLL.
754 * Return OK or FAIL.
755 */
756 static int
757ruby_runtime_link_init(char *libname, int verbose)
758{
759 int i;
760
761 if (hinstRuby)
762 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200763 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 if (!hinstRuby)
765 {
766 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100767 semsg(_(e_loadlib), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 return FAIL;
769 }
770
771 for (i = 0; ruby_funcname_table[i].ptr; ++i)
772 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200773 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 ruby_funcname_table[i].name)))
775 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200776 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200777 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100779 semsg(_(e_loadfunc), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 return FAIL;
781 }
782 }
783 return OK;
784}
785
786/*
787 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
788 * else FALSE.
789 */
790 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100791ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100793 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100795#endif // defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
797 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100798ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799{
800#ifdef DYNAMIC_RUBY
801 end_dynamic_ruby();
802#endif
803}
804
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100805 void
806ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807{
808 int state;
809 char *script = NULL;
810
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000811 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 if (!eap->skip && ensure_ruby_initialized())
813 {
814 if (script == NULL)
815 rb_eval_string_protect((char *)eap->arg, &state);
816 else
817 rb_eval_string_protect(script, &state);
818 if (state)
819 error_print(state);
820 }
821 vim_free(script);
822}
823
Bram Moolenaar165641d2010-02-17 16:23:09 +0100824/*
825 * In Ruby 1.9 or later, ruby String object has encoding.
826 * conversion buffer string of vim to ruby String object using
827 * VIM encoding option.
828 */
829 static VALUE
830vim_str2rb_enc_str(const char *s)
831{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100832#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100833 int isnum;
834 long lval;
835 char_u *sval;
836 rb_encoding *enc;
837
838 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
839 if (isnum == 0)
840 {
841 enc = rb_enc_find((char *)sval);
842 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200843 if (enc)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200844 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100845 }
846#endif
847 return rb_str_new2(s);
848}
849
850 static VALUE
851eval_enc_string_protect(const char *str, int *state)
852{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100853#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100854 int isnum;
855 long lval;
856 char_u *sval;
857 rb_encoding *enc;
858 VALUE v;
859
860 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
861 if (isnum == 0)
862 {
863 enc = rb_enc_find((char *)sval);
864 vim_free(sval);
865 if (enc)
866 {
867 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
868 return rb_eval_string_protect(StringValuePtr(v), state);
869 }
870 }
871#endif
872 return rb_eval_string_protect(str, state);
873}
874
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100875 void
876ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877{
878 int state;
879 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100880 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
882 if (ensure_ruby_initialized())
883 {
884 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
885 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200886 for (i = eap->line1; i <= eap->line2; i++)
887 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100888 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100890 if (i > curbuf->b_ml.ml_line_count)
891 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100892 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100894 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200895 if (state)
896 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 error_print(state);
898 break;
899 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100900 if (was_curbuf != curbuf)
901 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200903 if (!NIL_P(line))
904 {
905 if (TYPE(line) != T_STRING)
906 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100907 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 return;
909 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100910 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 changed();
912#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100913 syn_changed(i); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914#endif
915 }
916 }
917 check_cursor();
918 update_curbuf(NOT_VALID);
919 }
920}
921
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100922 static VALUE
923rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100924{
925 rb_load(file_to_load, 0);
926 return Qnil;
927}
928
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100929 void
930ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 int state;
933
934 if (ensure_ruby_initialized())
935 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100936 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
937 rb_protect(rb_load_wrap, file_to_load, &state);
938 if (state)
939 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 }
941}
942
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100943 void
944ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000946 if (buf->b_ruby_ref)
947 {
948 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
949 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951}
952
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100953 void
954ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000956 if (win->w_ruby_ref)
957 {
958 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
959 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 }
961}
962
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100963 static int
964ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965{
966 if (!ruby_initialized)
967 {
968#ifdef DYNAMIC_RUBY
969 if (ruby_enabled(TRUE))
970 {
971#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100972#ifdef MSWIN
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100973 // suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100974 int argc = 1;
975 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100976 char **argvp = argv;
Bram Moolenaar41a41412020-01-07 21:32:19 +0100977# if RUBY_VERSION >= 19
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100978 ruby_sysinit(&argc, &argvp);
979# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100980 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100981# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100982#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200983 {
Bram Moolenaar41a41412020-01-07 21:32:19 +0100984#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200985 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100986#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200987 ruby_init();
988 }
Bram Moolenaar41a41412020-01-07 21:32:19 +0100989#if RUBY_VERSION >= 19
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100990 {
991 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200992 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100993 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100994 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100995 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100996#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100998#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100999 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 ruby_vim_init();
1001 ruby_initialized = 1;
1002#ifdef DYNAMIC_RUBY
1003 }
1004 else
1005 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001006 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 return 0;
1008 }
1009#endif
1010 }
1011 return ruby_initialized;
1012}
1013
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001014 static void
1015error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016{
Bram Moolenaar41a41412020-01-07 21:32:19 +01001017#if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 RUBYEXTERN VALUE ruby_errinfo;
1019#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001020 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 VALUE eclass;
1022 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001023 VALUE bt;
1024 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001026 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027
1028#define TAG_RETURN 0x1
1029#define TAG_BREAK 0x2
1030#define TAG_NEXT 0x3
1031#define TAG_RETRY 0x4
1032#define TAG_REDO 0x5
1033#define TAG_RAISE 0x6
1034#define TAG_THROW 0x7
1035#define TAG_FATAL 0x8
1036#define TAG_MASK 0xf
1037
Bram Moolenaar758535a2016-03-30 22:06:16 +02001038 switch (state)
1039 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001040 case TAG_RETURN:
1041 emsg(_("E267: unexpected return"));
1042 break;
1043 case TAG_NEXT:
1044 emsg(_("E268: unexpected next"));
1045 break;
1046 case TAG_BREAK:
1047 emsg(_("E269: unexpected break"));
1048 break;
1049 case TAG_REDO:
1050 emsg(_("E270: unexpected redo"));
1051 break;
1052 case TAG_RETRY:
1053 emsg(_("E271: retry outside of rescue clause"));
1054 break;
1055 case TAG_RAISE:
1056 case TAG_FATAL:
Bram Moolenaar41a41412020-01-07 21:32:19 +01001057#if RUBY_VERSION >= 19
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001058 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001059#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001060 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001061#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001062 eclass = CLASS_OF(error);
1063 einfo = rb_obj_as_string(error);
1064 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1065 {
1066 emsg(_("E272: unhandled exception"));
1067 }
1068 else
1069 {
1070 VALUE epath;
1071 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001073 epath = rb_class_path(eclass);
1074 vim_snprintf(buff, BUFSIZ, "%s: %s",
1075 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1076 p = strchr(buff, '\n');
1077 if (p) *p = '\0';
1078 emsg(buff);
1079 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001080
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001081 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001082#if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001083 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1084 for (i = 0; i < RARRAY_LEN(bt); i++)
1085 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001086#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001087 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1088 for (i = 0; i < RARRAY_LEN(bt); i++)
1089 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001090#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001091 break;
1092 default:
1093 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1094 emsg(buff);
1095 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 }
1097}
1098
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001099 static VALUE
1100vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101{
1102 char *buff, *p;
1103
1104 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001105 if (RSTRING_LEN(str) > 0)
1106 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001107 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001108 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001109 strcpy(buff, RSTRING_PTR(str));
1110 p = strchr(buff, '\n');
1111 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001112 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001113 }
1114 else
1115 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001116 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 return Qnil;
1119}
1120
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001121 static VALUE
1122vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001124 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 update_screen(NOT_VALID);
1126 return Qnil;
1127}
1128
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001129 static VALUE
1130vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001132 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 return Qnil;
1134}
1135
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001136#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001137 static VALUE
1138vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001139{
1140 VALUE result = Qnil;
1141
1142 if (tv->v_type == VAR_STRING)
1143 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001144 result = rb_str_new2(tv->vval.v_string == NULL
1145 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001146 }
1147 else if (tv->v_type == VAR_NUMBER)
1148 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001149 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001150 }
1151# ifdef FEAT_FLOAT
1152 else if (tv->v_type == VAR_FLOAT)
1153 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001154 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001155 }
1156# endif
1157 else if (tv->v_type == VAR_LIST)
1158 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001159 list_T *list = tv->vval.v_list;
1160 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001161
Bram Moolenaar639a2552010-03-17 18:15:23 +01001162 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001163
Bram Moolenaar639a2552010-03-17 18:15:23 +01001164 if (list != NULL)
1165 {
1166 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001167 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001168 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001169 }
1170 else if (tv->v_type == VAR_DICT)
1171 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001172 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001173
Bram Moolenaar639a2552010-03-17 18:15:23 +01001174 if (tv->vval.v_dict != NULL)
1175 {
1176 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1177 long_u todo = ht->ht_used;
1178 hashitem_T *hi;
1179 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001180
Bram Moolenaar639a2552010-03-17 18:15:23 +01001181 for (hi = ht->ht_array; todo > 0; ++hi)
1182 {
1183 if (!HASHITEM_EMPTY(hi))
1184 {
1185 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001186
Bram Moolenaar639a2552010-03-17 18:15:23 +01001187 di = dict_lookup(hi);
1188 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001189 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001190 }
1191 }
1192 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001193 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001194 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001195 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001196 if (tv->vval.v_number == VVAL_TRUE)
1197 result = Qtrue;
1198 else if (tv->vval.v_number == VVAL_FALSE)
1199 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001200 }
1201 else if (tv->v_type == VAR_BLOB)
1202 {
1203 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1204 tv->vval.v_blob->bv_ga.ga_len);
1205 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001206 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001207
1208 return result;
1209}
1210#endif
1211
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001212 static VALUE
1213vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214{
1215#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001216 typval_T *tv;
1217 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001219 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1220 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001221 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001222 result = vim_to_ruby(tv);
1223
1224 free_tv(tv);
1225
1226 return result;
1227#else
1228 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230}
1231
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001232#ifdef USE_TYPEDDATA
1233static size_t buffer_dsize(const void *buf);
1234
1235static const rb_data_type_t buffer_type = {
1236 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001237 {0, 0, buffer_dsize,
1238# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001239 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001240# else
1241 {0, 0}
1242# endif
1243 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001244 0, 0,
1245# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1246 0,
1247# endif
1248};
1249
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001250 static size_t
1251buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001252{
1253 return sizeof(buf_T);
1254}
1255#endif
1256
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001257 static VALUE
1258buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001260 if (buf->b_ruby_ref)
1261 {
1262 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001264 else
1265 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001266#ifdef USE_TYPEDDATA
1267 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1268#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001270#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001271 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1273 return obj;
1274 }
1275}
1276
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001277 static buf_T *
1278get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279{
1280 buf_T *buf;
1281
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001282#ifdef USE_TYPEDDATA
1283 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1284#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 if (buf == NULL)
1288 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1289 return buf;
1290}
1291
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001292 static VALUE
1293vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294{
1295 VALUE result = rb_str_new("0z", 2);
1296 char buf[4];
1297 int i;
1298 for (i = 0; i < RSTRING_LEN(str); i++)
1299 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001300 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001301 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001302 }
1303 return result;
1304}
1305
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001306 static VALUE
1307buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308{
1309 return buffer_new(curbuf);
1310}
1311
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001312 static VALUE
1313buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
1315 buf_T *b;
1316 int n = 0;
1317
Bram Moolenaar29323592016-07-24 22:04:11 +02001318 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001319 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001320 // Deleted buffers should not be counted
1321 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001322 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001323 n++;
1324 }
1325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 return INT2NUM(n);
1327}
1328
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001329 static VALUE
1330buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331{
1332 buf_T *b;
1333 int n = NUM2INT(num);
1334
Bram Moolenaar29323592016-07-24 22:04:11 +02001335 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001336 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001337 // Deleted buffers should not be counted
1338 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001339 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001340 continue;
1341
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001342 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001344
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001345 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 }
1347 return Qnil;
1348}
1349
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001350 static VALUE
1351buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
1353 buf_T *buf = get_buf(self);
1354
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001355 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356}
1357
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001358 static VALUE
1359buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
1361 buf_T *buf = get_buf(self);
1362
1363 return INT2NUM(buf->b_fnum);
1364}
1365
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001366 static VALUE
1367buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
1369 buf_T *buf = get_buf(self);
1370
1371 return INT2NUM(buf->b_ml.ml_line_count);
1372}
1373
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001374 static VALUE
1375get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001377 if (n <= 0 || n > buf->b_ml.ml_line_count)
1378 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1379 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380}
1381
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001382 static VALUE
1383buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
1385 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001386
1387 if (buf != NULL)
1388 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001389 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001390}
1391
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001392 static VALUE
1393set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001394{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001395 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001396 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001398 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1399 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001400 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001401 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001402
Bram Moolenaar758535a2016-03-30 22:06:16 +02001403 if (u_savesub(n) == OK)
1404 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001405 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 changed();
1407#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001408 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409#endif
1410 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001411
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001412 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001413 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001414 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 update_curbuf(NOT_VALID);
1417 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001418 else
1419 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001420 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 }
1422 return str;
1423}
1424
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001425 static VALUE
1426buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001427{
1428 buf_T *buf = get_buf(self);
1429
1430 if (buf != NULL)
1431 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1432 return str;
1433}
1434
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001435 static VALUE
1436buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001438 buf_T *buf = get_buf(self);
1439 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001440 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001442 if (n > 0 && n <= buf->b_ml.ml_line_count)
1443 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001444 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001445 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001446
Bram Moolenaar758535a2016-03-30 22:06:16 +02001447 if (u_savedel(n, 1) == OK)
1448 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001450
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001451 // Changes to non-active buffers should properly refresh
1452 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001453 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 changed();
1456 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001457
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001458 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001459 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001460 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001461
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 update_curbuf(NOT_VALID);
1463 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001464 else
1465 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001466 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 }
1468 return Qnil;
1469}
1470
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001471 static VALUE
1472buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001474 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001475 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001476 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001477 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
Bram Moolenaar3c531602010-11-16 14:46:19 +01001479 if (line == NULL)
1480 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001481 rb_raise(rb_eIndexError, "NULL line");
1482 }
1483 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001484 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001485 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001486 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001487
Bram Moolenaar758535a2016-03-30 22:06:16 +02001488 if (u_inssub(n + 1) == OK)
1489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001491
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001492 // Changes to non-active buffers should properly refresh screen
1493 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001494 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001495
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001496 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001498
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001499 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001500 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001501 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 update_curbuf(NOT_VALID);
1504 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001505 else
1506 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001507 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 }
1509 return str;
1510}
1511
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001512#ifdef USE_TYPEDDATA
1513static size_t window_dsize(const void *buf);
1514
1515static const rb_data_type_t window_type = {
1516 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001517 {0, 0, window_dsize,
1518# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001519 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001520# else
1521 {0, 0}
1522# endif
1523 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001524 0, 0,
1525# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1526 0,
1527# endif
1528};
1529
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001530 static size_t
1531window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001532{
1533 return sizeof(win_T);
1534}
1535#endif
1536
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001537 static VALUE
1538window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001540 if (win->w_ruby_ref)
1541 {
1542 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001544 else
1545 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001546#ifdef USE_TYPEDDATA
1547 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1548#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001550#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001551 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1553 return obj;
1554 }
1555}
1556
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001557 static win_T *
1558get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
1560 win_T *win;
1561
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001562#ifdef USE_TYPEDDATA
1563 TypedData_Get_Struct(obj, win_T, &window_type, win);
1564#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (win == NULL)
1568 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1569 return win;
1570}
1571
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001572 static VALUE
1573window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
1575 return window_new(curwin);
1576}
1577
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001578/*
1579 * Added line manipulation functions
1580 * SegPhault - 03/07/05
1581 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001582 static VALUE
1583line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001584{
1585 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1586}
1587
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001588 static VALUE
1589set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001590{
1591 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1592}
1593
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001594 static VALUE
1595current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001596{
1597 return INT2FIX((int)curwin->w_cursor.lnum);
1598}
1599
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001600 static VALUE
1601window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 win_T *w;
1604 int n = 0;
1605
Bram Moolenaar29323592016-07-24 22:04:11 +02001606 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 n++;
1608 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609}
1610
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001611 static VALUE
1612window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613{
1614 win_T *w;
1615 int n = NUM2INT(num);
1616
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 if (n == 0)
1619 return window_new(w);
1620 return Qnil;
1621}
1622
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001623 static VALUE
1624window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
1626 win_T *win = get_win(self);
1627
1628 return buffer_new(win->w_buffer);
1629}
1630
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001631 static VALUE
1632window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
1634 win_T *win = get_win(self);
1635
1636 return INT2NUM(win->w_height);
1637}
1638
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001639 static VALUE
1640window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641{
1642 win_T *win = get_win(self);
1643 win_T *savewin = curwin;
1644
1645 curwin = win;
1646 win_setheight(NUM2INT(height));
1647 curwin = savewin;
1648 return height;
1649}
1650
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001651 static VALUE
1652window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001653{
Bram Moolenaare745d752017-09-22 16:56:22 +02001654 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001655}
1656
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001657 static VALUE
1658window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001659{
1660 win_T *win = get_win(self);
1661 win_T *savewin = curwin;
1662
1663 curwin = win;
1664 win_setwidth(NUM2INT(width));
1665 curwin = savewin;
1666 return width;
1667}
1668
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001669 static VALUE
1670window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671{
1672 win_T *win = get_win(self);
1673
1674 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1675}
1676
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001677 static VALUE
1678window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
1680 VALUE lnum, col;
1681 win_T *win = get_win(self);
1682
1683 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001684 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001686 lnum = RARRAY_PTR(pos)[0];
1687 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 win->w_cursor.lnum = NUM2LONG(lnum);
1689 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001690 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001691 check_cursor(); // put cursor on an existing line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 update_screen(NOT_VALID);
1693 return Qnil;
1694}
1695
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001696 static VALUE
1697f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001698{
1699 return Qnil;
1700}
1701
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001702 static VALUE
1703f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704{
1705 int i;
1706 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001707 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708
Bram Moolenaar758535a2016-03-30 22:06:16 +02001709 for (i = 0; i < argc; i++)
1710 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 if (i > 0) rb_str_cat(str, ", ", 2);
1712 rb_str_concat(str, rb_inspect(argv[i]));
1713 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001714 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001715
1716 if (argc == 1)
1717 ret = argv[0];
1718 else if (argc > 1)
1719 ret = rb_ary_new4(argc, argv);
1720 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721}
1722
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001723 static void
1724ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
1726#ifndef DYNAMIC_RUBY
1727 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001728 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729#endif
1730
1731 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001732 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001734 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001735 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1736 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 rb_define_global_function("p", f_p, -1);
1738}
1739
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001740 static void
1741ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742{
1743 objtbl = rb_hash_new();
1744 rb_global_variable(&objtbl);
1745
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001746 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1747 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001748 mVIM = rb_define_module("Vim");
1749 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1751 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1752 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1753 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1754 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1755 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1756 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1757 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1758 rb_define_module_function(mVIM, "message", vim_message, 1);
1759 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1760 rb_define_module_function(mVIM, "command", vim_command, 1);
1761 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001762 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763
1764 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1765 rb_eStandardError);
1766 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1767 rb_eStandardError);
1768
1769 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1770 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1771 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1772 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1773 rb_define_method(cBuffer, "name", buffer_name, 0);
1774 rb_define_method(cBuffer, "number", buffer_number, 0);
1775 rb_define_method(cBuffer, "count", buffer_count, 0);
1776 rb_define_method(cBuffer, "length", buffer_count, 0);
1777 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1778 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1779 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1780 rb_define_method(cBuffer, "append", buffer_append, 2);
1781
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001782 // Added line manipulation functions
1783 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001784 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1785 rb_define_method(cBuffer, "line", line_s_current, 0);
1786 rb_define_method(cBuffer, "line=", set_current_line, 1);
1787
1788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1790 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1791 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1792 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1793 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1794 rb_define_method(cVimWindow, "height", window_height, 0);
1795 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001796 rb_define_method(cVimWindow, "width", window_width, 0);
1797 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1799 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1800
1801 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1802 rb_define_virtual_variable("$curwin", window_s_current, 0);
1803}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001804
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001805 void
1806vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001807{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001808 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001809 ruby_stack_start = stack_start;
1810}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001811
1812 static int
1813convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1814{
1815 dict_T *d = (dict_T *)arg;
1816 dictitem_T *di;
1817
1818 di = dictitem_alloc((char_u *)RSTRING_PTR(RSTRING(rb_obj_as_string(key))));
1819 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1820 || dict_add(d, di) != OK)
1821 {
1822 d->dv_hashtab.ht_error = TRUE;
1823 return ST_STOP;
1824 }
1825 return ST_CONTINUE;
1826}
1827
1828 static int
1829ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1830{
1831 switch (TYPE(val))
1832 {
1833 case T_NIL:
1834 rettv->v_type = VAR_SPECIAL;
1835 rettv->vval.v_number = VVAL_NULL;
1836 break;
1837 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001838 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001839 rettv->vval.v_number = VVAL_TRUE;
1840 break;
1841 case T_FALSE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001842 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001843 rettv->vval.v_number = VVAL_FALSE;
1844 break;
1845 case T_BIGNUM:
1846 case T_FIXNUM:
1847 rettv->v_type = VAR_NUMBER;
1848 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1849 break;
1850#ifdef FEAT_FLOAT
1851 case T_FLOAT:
1852 rettv->v_type = VAR_FLOAT;
1853 rettv->vval.v_float = (float_T)NUM2DBL(val);
1854 break;
1855#endif
1856 default:
1857 val = rb_obj_as_string(val);
1858 // FALLTHROUGH
1859 case T_STRING:
1860 {
1861 VALUE str = (VALUE)RSTRING(val);
1862
1863 rettv->v_type = VAR_STRING;
1864 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
1865 (int)RSTRING_LEN(str));
1866 }
1867 break;
1868 case T_ARRAY:
1869 {
1870 list_T *l;
1871 long i;
1872 typval_T v;
1873
1874 l = list_alloc();
1875 if (l == NULL)
1876 return FAIL;
1877
1878 for (i = 0; i < RARRAY_LEN(val); ++i)
1879 {
1880 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1881 &v) != OK)
1882 {
1883 list_unref(l);
1884 return FAIL;
1885 }
1886 list_append_tv(l, &v);
1887 clear_tv(&v);
1888 }
1889
1890 rettv->v_type = VAR_LIST;
1891 rettv->vval.v_list = l;
1892 ++l->lv_refcount;
1893 }
1894 break;
1895 case T_HASH:
1896 {
1897 dict_T *d;
1898
1899 d = dict_alloc();
1900 if (d == NULL)
1901 return FAIL;
1902
1903 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1904 if (d->dv_hashtab.ht_error)
1905 {
1906 dict_unref(d);
1907 return FAIL;
1908 }
1909
1910 rettv->v_type = VAR_DICT;
1911 rettv->vval.v_dict = d;
1912 ++d->dv_refcount;
1913 }
1914 break;
1915 }
1916 return OK;
1917}
1918
1919 void
1920do_rubyeval(char_u *str, typval_T *rettv)
1921{
1922 int retval = FAIL;
1923
1924 if (ensure_ruby_initialized())
1925 {
1926 int state;
1927 VALUE obj;
1928
1929 obj = rb_eval_string_protect((const char *)str, &state);
1930 if (state)
1931 error_print(state);
1932 else
1933 retval = ruby_convert_to_vim_value(obj, rettv);
1934 }
1935 if (retval == FAIL)
1936 {
1937 rettv->v_type = VAR_NUMBER;
1938 rettv->vval.v_number = 0;
1939 }
1940}