blob: e989c01336ced796b608047aff566d6ca43a6375 [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 Moolenaar81ea1df2020-04-11 18:01:41 +0200102// On macOS pre-installed Ruby defines "SIZEOF_TIME_T" as "SIZEOF_LONG" so it
103// conflicts with the definition in config.h then causes macro-redifned warning.
104#ifdef SIZEOF_TIME_T
105# undef SIZEOF_TIME_T
106#endif
107
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#include <ruby.h>
Bram Moolenaar41a41412020-01-07 21:32:19 +0100109#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100110# include <ruby/encoding.h>
111#endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100112#if RUBY_VERSION <= 18
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200113# include <st.h> // for ST_STOP and ST_CONTINUE
114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200116#undef off_t // ruby defines off_t as _int64, Mingw uses long
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#undef EXTERN
118#undef _
119
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100120// T_DATA defined both by Ruby and Mac header files, hack around it...
Bram Moolenaard0573012017-10-28 21:11:06 +0200121#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122# define __OPENTRANSPORT__
123# define __OPENTRANSPORTPROTOCOL__
124# define __OPENTRANSPORTPROVIDERS__
125#endif
126
Bram Moolenaar165641d2010-02-17 16:23:09 +0100127/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100128 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
129 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
130 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100131 * Use TypedData_XXX if available.
132 */
Bram Moolenaar41a41412020-01-07 21:32:19 +0100133#if defined(TypedData_Wrap_Struct) && (RUBY_VERSION >= 20)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100134# define USE_TYPEDDATA 1
135#endif
136
137/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200138 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100139 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
140 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
141 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
142 */
143#ifndef StringValuePtr
144# define StringValuePtr(s) STR2CSTR(s)
145#endif
146#ifndef RARRAY_LEN
147# define RARRAY_LEN(s) RARRAY(s)->len
148#endif
149#ifndef RARRAY_PTR
150# define RARRAY_PTR(s) RARRAY(s)->ptr
151#endif
152#ifndef RSTRING_LEN
153# define RSTRING_LEN(s) RSTRING(s)->len
154#endif
155#ifndef RSTRING_PTR
156# define RSTRING_PTR(s) RSTRING(s)->ptr
157#endif
158
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100159#ifdef HAVE_DUP
160# undef HAVE_DUP
161#endif
162
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#include "vim.h"
164#include "version.h"
165
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100166#ifdef DYNAMIC_RUBY
167# if !defined(MSWIN) // must come after including vim.h, where it is defined
168# include <dlfcn.h>
169# define HINSTANCE void*
170# define RUBY_PROC void*
171# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
172# define symbol_from_dll dlsym
173# define close_dll dlclose
174# else
175# define RUBY_PROC FARPROC
176# define load_dll vimLoadLib
177# define symbol_from_dll GetProcAddress
178# define close_dll FreeLibrary
179# endif
180#endif
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#if defined(PROTO) && !defined(FEAT_RUBY)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100183// Define these to be able to generate the function prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184# define VALUE int
185# define RUBY_DATA_FUNC int
186#endif
187
188static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200189static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190static VALUE objtbl;
191
192static VALUE mVIM;
193static VALUE cBuffer;
194static VALUE cVimWindow;
195static VALUE eDeletedBufferError;
196static VALUE eDeletedWindowError;
197
198static int ensure_ruby_initialized(void);
199static void error_print(int);
200static void ruby_io_init(void);
201static void ruby_vim_init(void);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100202static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
Bram Moolenaar41a41412020-01-07 21:32:19 +0100204#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200205# if defined(__ia64) && !defined(ruby_init_stack)
206# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208#endif
209
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200210#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100211# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100212# define HINSTANCE int // for generating prototypes
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200213# endif
214
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215/*
216 * Wrapper defines
217 */
Bram Moolenaar8b430b42020-02-22 15:01:00 +0100218// Ruby 2.7 actually expands the following symbols as macro.
219# if RUBY_VERSION >= 27
220# undef rb_define_global_function
221# undef rb_define_method
222# undef rb_define_module_function
223# undef rb_define_singleton_method
224# endif
225
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200226# define rb_assoc_new dll_rb_assoc_new
227# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100228# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100229# define rb_check_type dll_rb_check_type
230# ifdef USE_TYPEDDATA
231# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100232# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200233# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100234# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100235# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100236# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
237# else
238# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
239# endif
240# else
241# define rb_data_object_alloc dll_rb_data_object_alloc
242# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200243# define rb_define_class_under dll_rb_define_class_under
244# define rb_define_const dll_rb_define_const
245# define rb_define_global_function dll_rb_define_global_function
246# define rb_define_method dll_rb_define_method
247# define rb_define_module dll_rb_define_module
248# define rb_define_module_function dll_rb_define_module_function
249# define rb_define_singleton_method dll_rb_define_singleton_method
250# define rb_define_virtual_variable dll_rb_define_virtual_variable
251# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200252# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200253# define rb_eArgError (*dll_rb_eArgError)
254# define rb_eIndexError (*dll_rb_eIndexError)
255# define rb_eRuntimeError (*dll_rb_eRuntimeError)
256# define rb_eStandardError (*dll_rb_eStandardError)
257# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaar41a41412020-01-07 21:32:19 +0100258# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200259# define rb_funcallv dll_rb_funcallv
260# else
261# define rb_funcall2 dll_rb_funcall2
262# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200263# define rb_global_variable dll_rb_global_variable
264# define rb_hash_aset dll_rb_hash_aset
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100265# define rb_hash_foreach dll_rb_hash_foreach
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200266# define rb_hash_new dll_rb_hash_new
267# define rb_inspect dll_rb_inspect
268# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200269
270// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
271// work. Not using the cache appears to be the best solution.
272# undef rb_intern
273# define rb_intern dll_rb_intern
274
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100275# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar41a41412020-01-07 21:32:19 +0100276# if RUBY_VERSION <= 18
Bram Moolenaar498af702014-03-28 21:58:21 +0100277# define rb_fix2int dll_rb_fix2int
278# define rb_num2int dll_rb_num2int
279# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200280# define rb_num2uint dll_rb_num2uint
281# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100282# define rb_num2dbl dll_rb_num2dbl
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200283# define rb_lastline_get dll_rb_lastline_get
284# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200285# define rb_protect dll_rb_protect
286# define rb_load dll_rb_load
Bram Moolenaar41a41412020-01-07 21:32:19 +0100287# if RUBY_VERSION <= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200288# define rb_num2long dll_rb_num2long
289# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100290# if RUBY_VERSION <= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200291# define rb_num2ulong dll_rb_num2ulong
292# endif
293# define rb_obj_alloc dll_rb_obj_alloc
294# define rb_obj_as_string dll_rb_obj_as_string
295# define rb_obj_id dll_rb_obj_id
296# define rb_raise dll_rb_raise
297# define rb_str_cat dll_rb_str_cat
298# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100299# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200300# define rb_str_new dll_rb_str_new
301# ifdef rb_str_new2
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100302// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200303# define need_rb_str_new_cstr 1
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100304// Ruby's headers #define rb_str_new_cstr to make use of GCC's
305// __builtin_constant_p extension.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200306# undef rb_str_new_cstr
307# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200308# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200309# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200310# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100311# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200312# define rb_string_value dll_rb_string_value
313# define rb_string_value_ptr dll_rb_string_value_ptr
314# define rb_float_new dll_rb_float_new
315# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200316# ifdef rb_ary_new4
Bram Moolenaar49c99fc2020-02-11 23:01:39 +0100317# define RB_ARY_NEW4_MACRO 1
318# undef rb_ary_new4
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200319# endif
320# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200321# define rb_ary_push dll_rb_ary_push
Bram Moolenaar41a41412020-01-07 21:32:19 +0100322# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200323# ifdef __ia64
324# define rb_ia64_bsp dll_rb_ia64_bsp
325# undef ruby_init_stack
326# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
327# else
328# define ruby_init_stack dll_ruby_init_stack
329# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200330# endif
331# else
332# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200333# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100334# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200335# define rb_errinfo dll_rb_errinfo
336# else
337# define ruby_errinfo (*dll_ruby_errinfo)
338# endif
339# define ruby_init dll_ruby_init
340# define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar4f974752019-02-17 17:44:42 +0100341# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100342# if RUBY_VERSION >= 19
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100343# define ruby_sysinit dll_ruby_sysinit
344# else
345# define NtInitialize dll_NtInitialize
346# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100347# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200348# define rb_w32_snprintf dll_rb_w32_snprintf
349# endif
350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351
Bram Moolenaar41a41412020-01-07 21:32:19 +0100352# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200353# define ruby_script dll_ruby_script
354# define rb_enc_find_index dll_rb_enc_find_index
355# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100356# undef rb_enc_str_new
357# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200358# define rb_sprintf dll_rb_sprintf
359# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100360# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200361# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363/*
364 * Pointers for dynamic link
365 */
366static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200367VALUE *dll_rb_cFalseClass;
368VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200369# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200370VALUE *dll_rb_cInteger;
371# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100372# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100373VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200374# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200375VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100377VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200378VALUE *dll_rb_cSymbol;
379VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100380static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100382# ifdef USE_TYPEDDATA
383static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100386# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100387# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100388static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
389# else
390static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
391# endif
392# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
396static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
397static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
398static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
399static VALUE (*dll_rb_define_module) (const char*);
400static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
401static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
402static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
403static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200404static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405static VALUE *dll_rb_eArgError;
406static VALUE *dll_rb_eIndexError;
407static VALUE *dll_rb_eRuntimeError;
408static VALUE *dll_rb_eStandardError;
409static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100410# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200411static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
412# else
413static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
414# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415static void (*dll_rb_global_variable) (VALUE*);
416static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100417static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418static VALUE (*dll_rb_hash_new) (void);
419static VALUE (*dll_rb_inspect) (VALUE);
420static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200421static ID (*dll_rb_intern) (const char*);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100422# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200423static long (*dll_rb_fix2int) (VALUE);
424static long (*dll_rb_num2int) (VALUE);
425static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200426# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100427static double (*dll_rb_num2dbl) (VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428static VALUE (*dll_rb_lastline_get) (void);
429static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100430static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200431static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432static long (*dll_rb_num2long) (VALUE);
433static unsigned long (*dll_rb_num2ulong) (VALUE);
434static VALUE (*dll_rb_obj_alloc) (VALUE);
435static VALUE (*dll_rb_obj_as_string) (VALUE);
436static VALUE (*dll_rb_obj_id) (VALUE);
437static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100438# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200439static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200440# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200442# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
444static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
445static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200446# ifdef need_rb_str_new_cstr
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100447// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200448static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200449# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200451# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100452# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100453static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200454# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457static void (*dll_ruby_init) (void);
458static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100459# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100460# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100461static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100462# else
463static void (*dll_NtInitialize) (int*, char***);
464# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100465# if RUBY_VERSION >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200466static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200467# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200468# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100469# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100470static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
471static VALUE (*dll_rb_float_new) (double);
472static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200473static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100474static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100475# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100476static void (*dll_rb_ary_detransient) (VALUE);
477# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100478# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200479# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200480static void * (*dll_rb_ia64_bsp) (void);
481static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200482# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200483static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200484# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200485# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200486# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100487# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100488static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200489# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490
Bram Moolenaar41a41412020-01-07 21:32:19 +0100491# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100492static void (*dll_ruby_script) (const char*);
493static int (*dll_rb_enc_find_index) (const char*);
494static rb_encoding* (*dll_rb_enc_find) (const char*);
495static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
496static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100497static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100498static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200499# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100500
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100501# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100502# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100503static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100504# else
505static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
506# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100507# endif
508
Bram Moolenaar41a41412020-01-07 21:32:19 +0100509# if (RUBY_VERSION >= 19) && !defined(PROTO)
510# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100511 long
512rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200513# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100514 SIGNED_VALUE
515rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200516# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100517{
518 return dll_rb_num2long(x);
519}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100520# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100521 VALUE
522rb_int2big_stub(intptr_t x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100523# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100524 VALUE
525rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100526# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100527{
528 return dll_rb_int2big(x);
529}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100530# if (RUBY_VERSION >= 19) && (VIM_SIZEOF_INT < VIM_SIZEOF_LONG)
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100531 long
532rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200533{
534 return dll_rb_fix2int(x);
535}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100536 long
537rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200538{
539 return dll_rb_num2int(x);
540}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200541# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100542# if RUBY_VERSION >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100543 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100544rb_float_new_in_heap(double d)
545{
546 return dll_rb_float_new(d);
547}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100548# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100549 unsigned long
550rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200551# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100552 VALUE
553rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200554# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100555{
556 return (long)RSHIFT((SIGNED_VALUE)(x),1);
557}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200558# endif
559# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100560
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100561 // Do not generate a prototype here, VALUE isn't always defined.
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100562# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar41a41412020-01-07 21:32:19 +0100563# if RUBY_VERSION == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100564 void
565rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100566{
Bram Moolenaar90140742014-11-27 17:44:08 +0100567 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100568}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100569# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100570 void
571rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100572{
573 dll_rb_gc_writebarrier_unprotect(obj);
574}
575# endif
576# endif
577
Bram Moolenaar41a41412020-01-07 21:32:19 +0100578# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100579 void
580rb_ary_detransient_stub(VALUE x)
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100581{
582 dll_rb_ary_detransient(x);
583}
584# endif
585
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100586static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
588/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000589 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591static struct
592{
593 char *name;
594 RUBY_PROC *ptr;
595} ruby_funcname_table[] =
596{
597 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
598 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200599# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200600 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100601# else
602 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200603# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100604# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100605 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200606# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
608 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100609 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
611 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100612 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100614# ifdef USE_TYPEDDATA
615 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
616# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100618# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100619# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100620 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
621# else
622 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
623# endif
624# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100626# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
628 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
629 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
630 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
631 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
632 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
633 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
634 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
635 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200636 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
638 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
639 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
640 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
641 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100642# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200643 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
644# else
645 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
648 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100649 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
651 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
652 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200653 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100654# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200655 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
656 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
657 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200658# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100659 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
661 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200662 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
663 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
665 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
666 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
667 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
668 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
669 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100670# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200671 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200672# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
676 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
677 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200678# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200679 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200680# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200682# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100683# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100684 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200685# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200687# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
689 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100690# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100691# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100692 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100693# else
694 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200695# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100696# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200698# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200699# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100700# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100701 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100702# if RUBY_VERSION <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100703 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200704# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100705 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200706# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100707 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200708# ifdef RB_ARY_NEW4_MACRO
709 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
710# else
711 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
712# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100713 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100714# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100715 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
716# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200717# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100718# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100719 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100720 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
721 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
722 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
723 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
724 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100725 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100726 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200727# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100728# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200729# ifdef __ia64
730 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
731# endif
732 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
733# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100734# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100735# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100736 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100737# else
738 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
739# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100740# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 {"", NULL},
742};
743
744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 * Load library and get all pointers.
746 * Parameter 'libname' provides name of DLL.
747 * Return OK or FAIL.
748 */
749 static int
750ruby_runtime_link_init(char *libname, int verbose)
751{
752 int i;
753
754 if (hinstRuby)
755 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200756 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 if (!hinstRuby)
758 {
759 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100760 semsg(_(e_loadlib), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 return FAIL;
762 }
763
764 for (i = 0; ruby_funcname_table[i].ptr; ++i)
765 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200766 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 ruby_funcname_table[i].name)))
768 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200769 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200770 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100772 semsg(_(e_loadfunc), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 return FAIL;
774 }
775 }
776 return OK;
777}
778
779/*
780 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
781 * else FALSE.
782 */
783 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100784ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100786 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100788#endif // defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
790 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100791ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793}
794
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100795 void
796ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
798 int state;
799 char *script = NULL;
800
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000801 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (!eap->skip && ensure_ruby_initialized())
803 {
804 if (script == NULL)
805 rb_eval_string_protect((char *)eap->arg, &state);
806 else
807 rb_eval_string_protect(script, &state);
808 if (state)
809 error_print(state);
810 }
811 vim_free(script);
812}
813
Bram Moolenaar165641d2010-02-17 16:23:09 +0100814/*
815 * In Ruby 1.9 or later, ruby String object has encoding.
816 * conversion buffer string of vim to ruby String object using
817 * VIM encoding option.
818 */
819 static VALUE
820vim_str2rb_enc_str(const char *s)
821{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100822#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100823 int isnum;
824 long lval;
825 char_u *sval;
826 rb_encoding *enc;
827
828 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
829 if (isnum == 0)
830 {
831 enc = rb_enc_find((char *)sval);
832 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200833 if (enc)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200834 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100835 }
836#endif
837 return rb_str_new2(s);
838}
839
840 static VALUE
841eval_enc_string_protect(const char *str, int *state)
842{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100843#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100844 int isnum;
845 long lval;
846 char_u *sval;
847 rb_encoding *enc;
848 VALUE v;
849
850 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
851 if (isnum == 0)
852 {
853 enc = rb_enc_find((char *)sval);
854 vim_free(sval);
855 if (enc)
856 {
857 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
858 return rb_eval_string_protect(StringValuePtr(v), state);
859 }
860 }
861#endif
862 return rb_eval_string_protect(str, state);
863}
864
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100865 void
866ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867{
868 int state;
869 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100870 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
872 if (ensure_ruby_initialized())
873 {
874 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
875 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200876 for (i = eap->line1; i <= eap->line2; i++)
877 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100878 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100880 if (i > curbuf->b_ml.ml_line_count)
881 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100882 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100884 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200885 if (state)
886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 error_print(state);
888 break;
889 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100890 if (was_curbuf != curbuf)
891 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200893 if (!NIL_P(line))
894 {
895 if (TYPE(line) != T_STRING)
896 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100897 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 return;
899 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100900 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 changed();
902#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100903 syn_changed(i); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904#endif
905 }
906 }
907 check_cursor();
908 update_curbuf(NOT_VALID);
909 }
910}
911
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100912 static VALUE
913rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100914{
915 rb_load(file_to_load, 0);
916 return Qnil;
917}
918
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100919 void
920ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921{
922 int state;
923
924 if (ensure_ruby_initialized())
925 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100926 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
927 rb_protect(rb_load_wrap, file_to_load, &state);
928 if (state)
929 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 }
931}
932
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100933 void
934ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000936 if (buf->b_ruby_ref)
937 {
938 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
939 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 }
941}
942
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100943 void
944ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000946 if (win->w_ruby_ref)
947 {
948 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
949 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951}
952
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100953 static int
954ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955{
956 if (!ruby_initialized)
957 {
958#ifdef DYNAMIC_RUBY
959 if (ruby_enabled(TRUE))
960 {
961#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100962#ifdef MSWIN
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100963 // suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100964 int argc = 1;
965 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100966 char **argvp = argv;
Bram Moolenaar41a41412020-01-07 21:32:19 +0100967# if RUBY_VERSION >= 19
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100968 ruby_sysinit(&argc, &argvp);
969# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100970 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100971# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100972#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200973 {
Bram Moolenaar41a41412020-01-07 21:32:19 +0100974#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200975 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100976#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200977 ruby_init();
978 }
Bram Moolenaar41a41412020-01-07 21:32:19 +0100979#if RUBY_VERSION >= 19
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100980 {
981 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200982 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100983 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100984 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100985 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100986#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100988#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100989 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 ruby_vim_init();
991 ruby_initialized = 1;
992#ifdef DYNAMIC_RUBY
993 }
994 else
995 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100996 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 return 0;
998 }
999#endif
1000 }
1001 return ruby_initialized;
1002}
1003
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001004 static void
1005error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
Bram Moolenaar41a41412020-01-07 21:32:19 +01001007#if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 RUBYEXTERN VALUE ruby_errinfo;
1009#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001010 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 VALUE eclass;
1012 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001013 VALUE bt;
1014 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001016 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
1018#define TAG_RETURN 0x1
1019#define TAG_BREAK 0x2
1020#define TAG_NEXT 0x3
1021#define TAG_RETRY 0x4
1022#define TAG_REDO 0x5
1023#define TAG_RAISE 0x6
1024#define TAG_THROW 0x7
1025#define TAG_FATAL 0x8
1026#define TAG_MASK 0xf
1027
Bram Moolenaar758535a2016-03-30 22:06:16 +02001028 switch (state)
1029 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001030 case TAG_RETURN:
1031 emsg(_("E267: unexpected return"));
1032 break;
1033 case TAG_NEXT:
1034 emsg(_("E268: unexpected next"));
1035 break;
1036 case TAG_BREAK:
1037 emsg(_("E269: unexpected break"));
1038 break;
1039 case TAG_REDO:
1040 emsg(_("E270: unexpected redo"));
1041 break;
1042 case TAG_RETRY:
1043 emsg(_("E271: retry outside of rescue clause"));
1044 break;
1045 case TAG_RAISE:
1046 case TAG_FATAL:
Bram Moolenaar41a41412020-01-07 21:32:19 +01001047#if RUBY_VERSION >= 19
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001048 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001049#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001050 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001051#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001052 eclass = CLASS_OF(error);
1053 einfo = rb_obj_as_string(error);
1054 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1055 {
1056 emsg(_("E272: unhandled exception"));
1057 }
1058 else
1059 {
1060 VALUE epath;
1061 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001063 epath = rb_class_path(eclass);
1064 vim_snprintf(buff, BUFSIZ, "%s: %s",
1065 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1066 p = strchr(buff, '\n');
1067 if (p) *p = '\0';
1068 emsg(buff);
1069 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001070
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001071 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001072#if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001073 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1074 for (i = 0; i < RARRAY_LEN(bt); i++)
1075 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001076#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001077 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1078 for (i = 0; i < RARRAY_LEN(bt); i++)
1079 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001080#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001081 break;
1082 default:
1083 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1084 emsg(buff);
1085 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087}
1088
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001089 static VALUE
1090vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091{
1092 char *buff, *p;
1093
1094 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001095 if (RSTRING_LEN(str) > 0)
1096 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001097 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001098 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001099 strcpy(buff, RSTRING_PTR(str));
1100 p = strchr(buff, '\n');
1101 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001102 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001103 }
1104 else
1105 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001106 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 return Qnil;
1109}
1110
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001111 static VALUE
1112vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001114 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 update_screen(NOT_VALID);
1116 return Qnil;
1117}
1118
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001119 static VALUE
1120vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001122 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 return Qnil;
1124}
1125
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001126#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001127 static VALUE
1128vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001129{
1130 VALUE result = Qnil;
1131
1132 if (tv->v_type == VAR_STRING)
1133 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001134 result = rb_str_new2(tv->vval.v_string == NULL
1135 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001136 }
1137 else if (tv->v_type == VAR_NUMBER)
1138 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001139 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001140 }
1141# ifdef FEAT_FLOAT
1142 else if (tv->v_type == VAR_FLOAT)
1143 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001144 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001145 }
1146# endif
1147 else if (tv->v_type == VAR_LIST)
1148 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001149 list_T *list = tv->vval.v_list;
1150 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001151
Bram Moolenaar639a2552010-03-17 18:15:23 +01001152 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001153
Bram Moolenaar639a2552010-03-17 18:15:23 +01001154 if (list != NULL)
1155 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001156 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001157 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001158 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001159 }
1160 else if (tv->v_type == VAR_DICT)
1161 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001162 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001163
Bram Moolenaar639a2552010-03-17 18:15:23 +01001164 if (tv->vval.v_dict != NULL)
1165 {
1166 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1167 long_u todo = ht->ht_used;
1168 hashitem_T *hi;
1169 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001170
Bram Moolenaar639a2552010-03-17 18:15:23 +01001171 for (hi = ht->ht_array; todo > 0; ++hi)
1172 {
1173 if (!HASHITEM_EMPTY(hi))
1174 {
1175 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001176
Bram Moolenaar639a2552010-03-17 18:15:23 +01001177 di = dict_lookup(hi);
1178 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001179 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001180 }
1181 }
1182 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001183 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001184 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001185 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001186 if (tv->vval.v_number == VVAL_TRUE)
1187 result = Qtrue;
1188 else if (tv->vval.v_number == VVAL_FALSE)
1189 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001190 }
1191 else if (tv->v_type == VAR_BLOB)
1192 {
1193 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1194 tv->vval.v_blob->bv_ga.ga_len);
1195 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001196 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001197
1198 return result;
1199}
1200#endif
1201
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001202 static VALUE
1203vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204{
1205#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001206 typval_T *tv;
1207 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001209 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1210 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001211 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001212 result = vim_to_ruby(tv);
1213
1214 free_tv(tv);
1215
1216 return result;
1217#else
1218 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220}
1221
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001222#ifdef USE_TYPEDDATA
1223static size_t buffer_dsize(const void *buf);
1224
1225static const rb_data_type_t buffer_type = {
1226 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001227 {0, 0, buffer_dsize,
1228# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001229 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001230# else
1231 {0, 0}
1232# endif
1233 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001234 0, 0,
1235# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1236 0,
1237# endif
1238};
1239
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001240 static size_t
1241buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001242{
1243 return sizeof(buf_T);
1244}
1245#endif
1246
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001247 static VALUE
1248buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001250 if (buf->b_ruby_ref)
1251 {
1252 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001254 else
1255 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001256#ifdef USE_TYPEDDATA
1257 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1258#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001260#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001261 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1263 return obj;
1264 }
1265}
1266
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001267 static buf_T *
1268get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269{
1270 buf_T *buf;
1271
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001272#ifdef USE_TYPEDDATA
1273 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1274#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 if (buf == NULL)
1278 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1279 return buf;
1280}
1281
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001282 static VALUE
1283vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001284{
1285 VALUE result = rb_str_new("0z", 2);
1286 char buf[4];
1287 int i;
1288 for (i = 0; i < RSTRING_LEN(str); i++)
1289 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001290 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001291 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001292 }
1293 return result;
1294}
1295
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001296 static VALUE
1297buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298{
1299 return buffer_new(curbuf);
1300}
1301
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001302 static VALUE
1303buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304{
1305 buf_T *b;
1306 int n = 0;
1307
Bram Moolenaar29323592016-07-24 22:04:11 +02001308 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001309 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001310 // Deleted buffers should not be counted
1311 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001312 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001313 n++;
1314 }
1315
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 return INT2NUM(n);
1317}
1318
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001319 static VALUE
1320buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321{
1322 buf_T *b;
1323 int n = NUM2INT(num);
1324
Bram Moolenaar29323592016-07-24 22:04:11 +02001325 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001326 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001327 // Deleted buffers should not be counted
1328 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001329 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001330 continue;
1331
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001332 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001334
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001335 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
1337 return Qnil;
1338}
1339
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001340 static VALUE
1341buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342{
1343 buf_T *buf = get_buf(self);
1344
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001345 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346}
1347
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001348 static VALUE
1349buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350{
1351 buf_T *buf = get_buf(self);
1352
1353 return INT2NUM(buf->b_fnum);
1354}
1355
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001356 static VALUE
1357buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358{
1359 buf_T *buf = get_buf(self);
1360
1361 return INT2NUM(buf->b_ml.ml_line_count);
1362}
1363
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001364 static VALUE
1365get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001367 if (n <= 0 || n > buf->b_ml.ml_line_count)
1368 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1369 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370}
1371
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001372 static VALUE
1373buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
1375 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001376
1377 if (buf != NULL)
1378 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001379 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001380}
1381
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001382 static VALUE
1383set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001384{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001385 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001386 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001388 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1389 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001390 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001391 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001392
Bram Moolenaar758535a2016-03-30 22:06:16 +02001393 if (u_savesub(n) == OK)
1394 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001395 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 changed();
1397#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001398 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399#endif
1400 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001401
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001402 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001403 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001404 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001405
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 update_curbuf(NOT_VALID);
1407 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001408 else
1409 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001410 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 }
1412 return str;
1413}
1414
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001415 static VALUE
1416buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001417{
1418 buf_T *buf = get_buf(self);
1419
1420 if (buf != NULL)
1421 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1422 return str;
1423}
1424
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001425 static VALUE
1426buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001428 buf_T *buf = get_buf(self);
1429 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001430 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001432 if (n > 0 && n <= buf->b_ml.ml_line_count)
1433 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001434 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001435 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001436
Bram Moolenaar758535a2016-03-30 22:06:16 +02001437 if (u_savedel(n, 1) == OK)
1438 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001440
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001441 // Changes to non-active buffers should properly refresh
1442 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001443 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001444
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 changed();
1446 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001447
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001448 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001449 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001450 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001451
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 update_curbuf(NOT_VALID);
1453 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001454 else
1455 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001456 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 }
1458 return Qnil;
1459}
1460
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001461 static VALUE
1462buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001464 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001465 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001466 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001467 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
Bram Moolenaar3c531602010-11-16 14:46:19 +01001469 if (line == NULL)
1470 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001471 rb_raise(rb_eIndexError, "NULL line");
1472 }
1473 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001474 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001475 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001476 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001477
Bram Moolenaar758535a2016-03-30 22:06:16 +02001478 if (u_inssub(n + 1) == OK)
1479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001481
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001482 // Changes to non-active buffers should properly refresh screen
1483 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001484 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001485
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001486 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001488
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001489 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001490 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001491 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001492
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 update_curbuf(NOT_VALID);
1494 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001495 else
1496 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001497 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
1499 return str;
1500}
1501
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001502#ifdef USE_TYPEDDATA
1503static size_t window_dsize(const void *buf);
1504
1505static const rb_data_type_t window_type = {
1506 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001507 {0, 0, window_dsize,
1508# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001509 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001510# else
1511 {0, 0}
1512# endif
1513 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001514 0, 0,
1515# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1516 0,
1517# endif
1518};
1519
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001520 static size_t
1521window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001522{
1523 return sizeof(win_T);
1524}
1525#endif
1526
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001527 static VALUE
1528window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001530 if (win->w_ruby_ref)
1531 {
1532 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001534 else
1535 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001536#ifdef USE_TYPEDDATA
1537 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1538#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001540#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001541 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1543 return obj;
1544 }
1545}
1546
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001547 static win_T *
1548get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
1550 win_T *win;
1551
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001552#ifdef USE_TYPEDDATA
1553 TypedData_Get_Struct(obj, win_T, &window_type, win);
1554#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 if (win == NULL)
1558 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1559 return win;
1560}
1561
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001562 static VALUE
1563window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565 return window_new(curwin);
1566}
1567
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001568/*
1569 * Added line manipulation functions
1570 * SegPhault - 03/07/05
1571 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001572 static VALUE
1573line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001574{
1575 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1576}
1577
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001578 static VALUE
1579set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001580{
1581 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1582}
1583
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001584 static VALUE
1585current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001586{
1587 return INT2FIX((int)curwin->w_cursor.lnum);
1588}
1589
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001590 static VALUE
1591window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 win_T *w;
1594 int n = 0;
1595
Bram Moolenaar29323592016-07-24 22:04:11 +02001596 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 n++;
1598 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599}
1600
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001601 static VALUE
1602window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604 win_T *w;
1605 int n = NUM2INT(num);
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (n == 0)
1609 return window_new(w);
1610 return Qnil;
1611}
1612
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001613 static VALUE
1614window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615{
1616 win_T *win = get_win(self);
1617
1618 return buffer_new(win->w_buffer);
1619}
1620
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001621 static VALUE
1622window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
1624 win_T *win = get_win(self);
1625
1626 return INT2NUM(win->w_height);
1627}
1628
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001629 static VALUE
1630window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
1632 win_T *win = get_win(self);
1633 win_T *savewin = curwin;
1634
1635 curwin = win;
1636 win_setheight(NUM2INT(height));
1637 curwin = savewin;
1638 return height;
1639}
1640
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001641 static VALUE
1642window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001643{
Bram Moolenaare745d752017-09-22 16:56:22 +02001644 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001645}
1646
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001647 static VALUE
1648window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001649{
1650 win_T *win = get_win(self);
1651 win_T *savewin = curwin;
1652
1653 curwin = win;
1654 win_setwidth(NUM2INT(width));
1655 curwin = savewin;
1656 return width;
1657}
1658
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001659 static VALUE
1660window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661{
1662 win_T *win = get_win(self);
1663
1664 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1665}
1666
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001667 static VALUE
1668window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
1670 VALUE lnum, col;
1671 win_T *win = get_win(self);
1672
1673 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001674 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001676 lnum = RARRAY_PTR(pos)[0];
1677 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 win->w_cursor.lnum = NUM2LONG(lnum);
1679 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001680 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001681 check_cursor(); // put cursor on an existing line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 update_screen(NOT_VALID);
1683 return Qnil;
1684}
1685
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001686 static VALUE
1687f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001688{
1689 return Qnil;
1690}
1691
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001692 static VALUE
1693f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694{
1695 int i;
1696 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001697 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
Bram Moolenaar758535a2016-03-30 22:06:16 +02001699 for (i = 0; i < argc; i++)
1700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if (i > 0) rb_str_cat(str, ", ", 2);
1702 rb_str_concat(str, rb_inspect(argv[i]));
1703 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001704 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001705
1706 if (argc == 1)
1707 ret = argv[0];
1708 else if (argc > 1)
1709 ret = rb_ary_new4(argc, argv);
1710 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711}
1712
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001713 static void
1714ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
1716#ifndef DYNAMIC_RUBY
1717 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001718 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719#endif
1720
1721 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001722 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001724 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001725 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1726 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 rb_define_global_function("p", f_p, -1);
1728}
1729
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001730 static void
1731ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
1733 objtbl = rb_hash_new();
1734 rb_global_variable(&objtbl);
1735
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001736 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1737 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001738 mVIM = rb_define_module("Vim");
1739 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1741 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1742 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1743 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1744 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1745 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1746 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1747 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1748 rb_define_module_function(mVIM, "message", vim_message, 1);
1749 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1750 rb_define_module_function(mVIM, "command", vim_command, 1);
1751 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001752 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
1754 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1755 rb_eStandardError);
1756 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1757 rb_eStandardError);
1758
1759 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1760 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1761 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1762 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1763 rb_define_method(cBuffer, "name", buffer_name, 0);
1764 rb_define_method(cBuffer, "number", buffer_number, 0);
1765 rb_define_method(cBuffer, "count", buffer_count, 0);
1766 rb_define_method(cBuffer, "length", buffer_count, 0);
1767 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1768 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1769 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1770 rb_define_method(cBuffer, "append", buffer_append, 2);
1771
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001772 // Added line manipulation functions
1773 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001774 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1775 rb_define_method(cBuffer, "line", line_s_current, 0);
1776 rb_define_method(cBuffer, "line=", set_current_line, 1);
1777
1778
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1780 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1781 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1782 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1783 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1784 rb_define_method(cVimWindow, "height", window_height, 0);
1785 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001786 rb_define_method(cVimWindow, "width", window_width, 0);
1787 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1789 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1790
1791 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1792 rb_define_virtual_variable("$curwin", window_s_current, 0);
1793}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001794
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001795 void
1796vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001797{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001798 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001799 ruby_stack_start = stack_start;
1800}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001801
1802 static int
1803convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1804{
1805 dict_T *d = (dict_T *)arg;
1806 dictitem_T *di;
1807
1808 di = dictitem_alloc((char_u *)RSTRING_PTR(RSTRING(rb_obj_as_string(key))));
1809 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1810 || dict_add(d, di) != OK)
1811 {
1812 d->dv_hashtab.ht_error = TRUE;
1813 return ST_STOP;
1814 }
1815 return ST_CONTINUE;
1816}
1817
1818 static int
1819ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1820{
1821 switch (TYPE(val))
1822 {
1823 case T_NIL:
1824 rettv->v_type = VAR_SPECIAL;
1825 rettv->vval.v_number = VVAL_NULL;
1826 break;
1827 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001828 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001829 rettv->vval.v_number = VVAL_TRUE;
1830 break;
1831 case T_FALSE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001832 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001833 rettv->vval.v_number = VVAL_FALSE;
1834 break;
1835 case T_BIGNUM:
1836 case T_FIXNUM:
1837 rettv->v_type = VAR_NUMBER;
1838 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1839 break;
1840#ifdef FEAT_FLOAT
1841 case T_FLOAT:
1842 rettv->v_type = VAR_FLOAT;
1843 rettv->vval.v_float = (float_T)NUM2DBL(val);
1844 break;
1845#endif
1846 default:
1847 val = rb_obj_as_string(val);
1848 // FALLTHROUGH
1849 case T_STRING:
1850 {
1851 VALUE str = (VALUE)RSTRING(val);
1852
1853 rettv->v_type = VAR_STRING;
1854 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
1855 (int)RSTRING_LEN(str));
1856 }
1857 break;
1858 case T_ARRAY:
1859 {
1860 list_T *l;
1861 long i;
1862 typval_T v;
1863
1864 l = list_alloc();
1865 if (l == NULL)
1866 return FAIL;
1867
1868 for (i = 0; i < RARRAY_LEN(val); ++i)
1869 {
1870 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1871 &v) != OK)
1872 {
1873 list_unref(l);
1874 return FAIL;
1875 }
1876 list_append_tv(l, &v);
1877 clear_tv(&v);
1878 }
1879
1880 rettv->v_type = VAR_LIST;
1881 rettv->vval.v_list = l;
1882 ++l->lv_refcount;
1883 }
1884 break;
1885 case T_HASH:
1886 {
1887 dict_T *d;
1888
1889 d = dict_alloc();
1890 if (d == NULL)
1891 return FAIL;
1892
1893 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1894 if (d->dv_hashtab.ht_error)
1895 {
1896 dict_unref(d);
1897 return FAIL;
1898 }
1899
1900 rettv->v_type = VAR_DICT;
1901 rettv->vval.v_dict = d;
1902 ++d->dv_refcount;
1903 }
1904 break;
1905 }
1906 return OK;
1907}
1908
1909 void
1910do_rubyeval(char_u *str, typval_T *rettv)
1911{
1912 int retval = FAIL;
1913
1914 if (ensure_ruby_initialized())
1915 {
1916 int state;
1917 VALUE obj;
1918
1919 obj = rb_eval_string_protect((const char *)str, &state);
1920 if (state)
1921 error_print(state);
1922 else
1923 retval = ruby_convert_to_vim_value(obj, rettv);
1924 }
1925 if (retval == FAIL)
1926 {
1927 rettv->v_type = VAR_NUMBER;
1928 rettv->vval.v_number = 0;
1929 }
1930}