@@ -19,7 +19,7 @@ extern "C" {
19
19
#endif
20
20
21
21
#define rb_sprintf (format , ...) \
22
- (VALUE) polyglot_invoke(RUBY_CEXT, "rb_sprintf", rb_str_new_cstr(format), ##__VA_ARGS__)
22
+ rb_tr_wrap( polyglot_invoke(RUBY_CEXT, "rb_sprintf", rb_tr_unwrap( rb_str_new_cstr(format)) , ##__VA_ARGS__) )
23
23
24
24
NORETURN (VALUE rb_f_notimplement (int args_count , const VALUE * args , VALUE object ));
25
25
@@ -90,22 +90,17 @@ int rb_tr_writable(int mode);
90
90
91
91
typedef void * (* gvl_call )(void * );
92
92
93
- // Exceptions
94
-
95
- #define rb_raise (EXCEPTION , FORMAT , ...) \
96
- rb_exc_raise(rb_exc_new_str(EXCEPTION, (VALUE) polyglot_invoke(RUBY_CEXT, "rb_sprintf", rb_str_new_cstr(FORMAT), ##__VA_ARGS__)))
97
-
98
93
// Utilities
99
94
100
95
#define rb_warn (FORMAT , ...) do { \
101
96
if (polyglot_as_boolean(polyglot_invoke(RUBY_CEXT, "warn?"))) { \
102
- polyglot_invoke(rb_mKernel, "warn", (VALUE) polyglot_invoke(rb_mKernel, "sprintf", rb_str_new_cstr(FORMAT), ##__VA_ARGS__)); \
97
+ polyglot_invoke(rb_tr_unwrap( rb_mKernel) , "warn", (VALUE) polyglot_invoke(rb_tr_unwrap( rb_mKernel) , "sprintf", rb_tr_unwrap( rb_str_new_cstr(FORMAT) ), ##__VA_ARGS__)); \
103
98
} \
104
99
} while (0);
105
100
106
101
#define rb_warning (FORMAT , ...) do { \
107
102
if (polyglot_as_boolean(polyglot_invoke(RUBY_CEXT, "warning?"))) { \
108
- polyglot_invoke(rb_mKernel, "warn", (VALUE) polyglot_invoke(rb_mKernel, "sprintf", rb_str_new_cstr(FORMAT), ##__VA_ARGS__)); \
103
+ polyglot_invoke(rb_tr_unwrap( rb_mKernel) , "warn", (VALUE) polyglot_invoke(rb_tr_unwrap( rb_mKernel) , "sprintf", rb_tr_unwrap( rb_str_new_cstr(FORMAT) ), ##__VA_ARGS__)); \
109
104
} \
110
105
} while (0);
111
106
@@ -125,9 +120,51 @@ MUST_INLINE int rb_tr_scan_args(int argc, VALUE *argv, const char *format, VALUE
125
120
#define SCAN_ARGS_IMPL (_1 , _2 , _3 , _4 , _5 , _6 , _7 , _8 , _9 , _10 , NAME , ...) NAME
126
121
#define rb_scan_args (ARGC , ARGV , FORMAT , ...) SCAN_ARGS_IMPL(__VA_ARGS__, rb_tr_scan_args_10, rb_tr_scan_args_9, rb_tr_scan_args_8, rb_tr_scan_args_7, rb_tr_scan_args_6, rb_tr_scan_args_5, rb_tr_scan_args_4, rb_tr_scan_args_3, rb_tr_scan_args_2, rb_tr_scan_args_1)(ARGC, ARGV, FORMAT, __VA_ARGS__)
127
122
123
+ // Invoking ruby methods.
124
+
125
+ // These macros implement ways to call the methods on Truffle::CExt
126
+ // (RUBY_CEXT_INVOKE) and other ruby objects (RUBY_INVOKE) and handle
127
+ // all the unwrapping of arguments. They also come in _NO_WRAP
128
+ // variants which will not attempt to wrap the result. This is
129
+ // important if it is not an actual ruby object being returned as an
130
+ // error will be raised when attempting to wrap such objects.
131
+
132
+ // Internal macros for the implementation
133
+ #define RUBY_INVOKE_IMPL_0 (recv , name ) polyglot_invoke(recv, name)
134
+ #define RUBY_INVOKE_IMPL_1 (recv , name , V1 ) polyglot_invoke(recv, name, rb_tr_unwrap(V1))
135
+ #define RUBY_INVOKE_IMPL_2 (recv , name , V1 , V2 ) polyglot_invoke(recv, name, rb_tr_unwrap(V1), rb_tr_unwrap(V2))
136
+ #define RUBY_INVOKE_IMPL_3 (recv , name , V1 , V2 , V3 ) polyglot_invoke(recv, name, rb_tr_unwrap(V1), rb_tr_unwrap(V2), rb_tr_unwrap(V3))
137
+ #define RUBY_INVOKE_IMPL_4 (recv , name , V1 , V2 , V3 , V4 ) polyglot_invoke(recv, name, rb_tr_unwrap(V1), rb_tr_unwrap(V2), rb_tr_unwrap(V3), rb_tr_unwrap(V4))
138
+ #define RUBY_INVOKE_IMPL_5 (recv , name , V1 , V2 , V3 , V4 , V5 ) polyglot_invoke(recv, name, rb_tr_unwrap(V1), rb_tr_unwrap(V2), rb_tr_unwrap(V3), rb_tr_unwrap(V4), rb_tr_unwrap(V5))
139
+ #define RUBY_INVOKE_IMPL_6 (recv , name , V1 , V2 , V3 , V4 , V5 , V6 ) polyglot_invoke(recv, name, rb_tr_unwrap(V1), rb_tr_unwrap(V2), rb_tr_unwrap(V3), rb_tr_unwrap(V4), rb_tr_unwrap(V5), rb_tr_unwrap(V6))
140
+ #define RUBY_INVOKE_IMPL_7 (recv , name , V1 , V2 , V3 , V4 , V5 , V6 , V7 ) polyglot_invoke(recv, name, rb_tr_unwrap(V1), rb_tr_unwrap(V2), rb_tr_unwrap(V3), rb_tr_unwrap(V4), rb_tr_unwrap(V5), rb_tr_unwrap(V6), rb_tr_unwrap(V7))
141
+ #define RUBY_INVOKE_IMPL_8 (recv , name , V1 , V2 , V3 , V4 , V5 , V6 , V7 , V8 ) polyglot_invoke(recv, name, rb_tr_unwrap(V1), rb_tr_unwrap(V2), rb_tr_unwrap(V3), rb_tr_unwrap(V4), rb_tr_unwrap(V5), rb_tr_unwrap(V6), rb_tr_unwrap(V7), rb_tr_unwrap(V8))
142
+ #define RUBY_INVOKE_IMPL_9 (recv , name , V1 , V2 , V3 , V4 , V5 , V6 , V7 , V8 , V9 ) polyglot_invoke(recv, name, rb_tr_unwrap(V1), rb_tr_unwrap(V2), rb_tr_unwrap(V3), rb_tr_unwrap(V4), rb_tr_unwrap(V5), rb_tr_unwrap(V6), rb_tr_unwrap(V7), rb_tr_unwrap(V8), rb_tr_unwrap(V9))
143
+ #define RUBY_INVOKE_IMPL_10 (recv , name , V1 , V2 , V3 , V4 , V5 , V6 , V7 , V8 , V9 , V10 ) polyglot_invoke(recv, name, rb_tr_unwrap(V1), rb_tr_unwrap(V2), rb_tr_unwrap(V3), rb_tr_unwrap(V4), rb_tr_unwrap(V5), rb_tr_unwrap(V6), rb_tr_unwrap(V7), rb_tr_unwrap(V8), rb_tr_unwrap(V9), rb_tr_unwrap(V10))
144
+ #define INVOKE_IMPL (RECV , MESG , _1 , _2 , _3 , _4 , _5 , _6 , _7 , _8 , _9 , _10 , NAME , ...) NAME
145
+ #define RUBY_INVOKE_IMPL_NO_WRAP (RECV , NAME , ...) INVOKE_IMPL(RECV, NAME, ##__VA_ARGS__, RUBY_INVOKE_IMPL_10, RUBY_INVOKE_IMPL_9, RUBY_INVOKE_IMPL_8, RUBY_INVOKE_IMPL_7, RUBY_INVOKE_IMPL_6, RUBY_INVOKE_IMPL_5, RUBY_INVOKE_IMPL_4, RUBY_INVOKE_IMPL_3, RUBY_INVOKE_IMPL_2, RUBY_INVOKE_IMPL_1, RUBY_INVOKE_IMPL_0)(RECV, NAME, ##__VA_ARGS__)
146
+ #define RUBY_INVOKE_IMPL (RECV , NAME , ...) rb_tr_wrap(RUBY_INVOKE_IMPL_NO_WRAP(RECV, NAME, ##__VA_ARGS__))
147
+
148
+ // Public macros used in this header and ruby.c
149
+ #define RUBY_INVOKE (RECV , NAME , ...) RUBY_INVOKE_IMPL(rb_tr_unwrap(RECV), NAME, ##__VA_ARGS__)
150
+ #define RUBY_INVOKE_NO_WRAP (RECV , NAME , ...) RUBY_INVOKE_IMPL_NO_WRAP(rb_tr_unwrap(RECV), NAME, ##__VA_ARGS__)
151
+
152
+ #define RUBY_CEXT_INVOKE (NAME , ...) RUBY_INVOKE_IMPL(RUBY_CEXT, NAME, ##__VA_ARGS__)
153
+ #define RUBY_CEXT_INVOKE_NO_WRAP (NAME , ...) RUBY_INVOKE_IMPL_NO_WRAP(RUBY_CEXT, NAME, ##__VA_ARGS__)
154
+
128
155
// Calls
129
156
130
- #define rb_funcall (object , ...) polyglot_invoke(RUBY_CEXT, "rb_funcall", (void *) object, __VA_ARGS__)
157
+ // We use this pair of macros because ##__VA_ARGS__ args will not
158
+ // have macro substitution done on them at the right point in
159
+ // preprocessing and will prevent rb_funcall(..., rb_funcall(...))
160
+ // from being expanded correctly.
161
+ #define rb_tr_funcall (object , method , n ,...) RUBY_CEXT_INVOKE("rb_funcall", object, method, INT2FIX(n), ##__VA_ARGS__)
162
+ #define rb_funcall (object , method , ...) rb_tr_funcall(object, method, __VA_ARGS__)
163
+
164
+ // Exceptions
165
+
166
+ #define rb_raise (EXCEPTION , FORMAT , ...) \
167
+ rb_exc_raise(rb_exc_new_str(EXCEPTION, (VALUE) rb_tr_wrap(polyglot_invoke(RUBY_CEXT, "rb_sprintf", rb_tr_unwrap(rb_str_new_cstr(FORMAT)), ##__VA_ARGS__))))
131
168
132
169
// Additional non-standard
133
170
VALUE rb_java_class_of (VALUE val );
@@ -139,39 +176,34 @@ VALUE rb_ivar_lookup(VALUE object, const char *name, VALUE default_value);
139
176
// Inline implementations
140
177
141
178
MUST_INLINE int rb_nativethread_lock_initialize (rb_nativethread_lock_t * lock ) {
142
- * lock = polyglot_invoke ( RUBY_CEXT , "rb_nativethread_lock_initialize" );
179
+ * lock = RUBY_CEXT_INVOKE ( "rb_nativethread_lock_initialize" );
143
180
return 0 ;
144
181
}
145
182
146
183
MUST_INLINE int rb_nativethread_lock_destroy (rb_nativethread_lock_t * lock ) {
147
- * lock = NULL ;
184
+ * lock = RUBY_CEXT_INVOKE ( "rb_nativethread_lock_destroy" , * lock ) ;
148
185
return 0 ;
149
186
}
150
187
151
188
MUST_INLINE int rb_nativethread_lock_lock (rb_nativethread_lock_t * lock ) {
152
- polyglot_invoke (* lock , "lock" );
189
+ RUBY_INVOKE_NO_WRAP (* lock , "lock" );
153
190
return 0 ;
154
191
}
155
192
156
193
MUST_INLINE int rb_nativethread_lock_unlock (rb_nativethread_lock_t * lock ) {
157
- polyglot_invoke (* lock , "unlock" );
194
+ RUBY_INVOKE_NO_WRAP (* lock , "unlock" );
158
195
return 0 ;
159
196
}
160
197
161
198
MUST_INLINE int rb_range_values (VALUE range , VALUE * begp , VALUE * endp , int * exclp ) {
162
- if (rb_obj_is_kind_of (range , rb_cRange )) {
163
- * begp = (VALUE ) polyglot_invoke (range , "begin" );
164
- * endp = (VALUE ) polyglot_invoke (range , "end" );
165
- * exclp = (int ) polyglot_as_boolean (polyglot_invoke (range , "exclude_end?" ));
199
+ if (!rb_obj_is_kind_of (range , rb_cRange )) {
200
+ if (!RTEST (RUBY_INVOKE (range , "respond_to?" , rb_intern ("begin" )))) return Qfalse_int_const ;
201
+ if (!RTEST (RUBY_INVOKE (range , "respond_to?" , rb_intern ("end" )))) return Qfalse_int_const ;
166
202
}
167
- else {
168
- if (!polyglot_as_boolean (polyglot_invoke (range , "respond_to?" , rb_intern ("begin" )))) return Qfalse_int_const ;
169
- if (!polyglot_as_boolean (polyglot_invoke (range , "respond_to?" , rb_intern ("end" )))) return Qfalse_int_const ;
170
203
171
- * begp = polyglot_invoke (range , "begin" );
172
- * endp = polyglot_invoke (range , "end" );
173
- * exclp = (int ) RTEST (polyglot_invoke (range , "exclude_end?" ));
174
- }
204
+ * begp = RUBY_INVOKE (range , "begin" );
205
+ * endp = RUBY_INVOKE (range , "end" );
206
+ * exclp = (int ) RTEST (RUBY_INVOKE (range , "exclude_end?" ));
175
207
return Qtrue_int_const ;
176
208
}
177
209
@@ -194,7 +226,7 @@ MUST_INLINE char *rb_string_value_ptr(VALUE *value_pointer) {
194
226
MUST_INLINE char * rb_string_value_cstr (VALUE * value_pointer ) {
195
227
VALUE string = rb_string_value (value_pointer );
196
228
197
- polyglot_invoke ( RUBY_CEXT , "rb_string_value_cstr_check" , string );
229
+ RUBY_CEXT_INVOKE ( "rb_string_value_cstr_check" , string );
198
230
199
231
return RSTRING_PTR (string );
200
232
}
@@ -270,7 +302,7 @@ MUST_INLINE int rb_tr_scan_args(int argc, VALUE *argv, const char *format, VALUE
270
302
bool erased_kwargs = false;
271
303
bool found_kwargs = false;
272
304
273
- if (rest && kwargs && !polyglot_as_boolean (polyglot_invoke ( RUBY_CEXT , "test_kwargs" , argv [argc - 1 ], Qfalse ))) {
305
+ if (rest && kwargs && !polyglot_as_boolean (RUBY_CEXT_INVOKE_NO_WRAP ( "test_kwargs" , argv [argc - 1 ], Qfalse ))) {
274
306
kwargs = false;
275
307
erased_kwargs = true;
276
308
}
@@ -313,7 +345,7 @@ MUST_INLINE int rb_tr_scan_args(int argc, VALUE *argv, const char *format, VALUE
313
345
} else if (kwargs && !taken_kwargs ) {
314
346
if (argn < argc ) {
315
347
arg = argv [argn ];
316
- polyglot_invoke ( RUBY_CEXT , "test_kwargs" , arg , Qtrue );
348
+ RUBY_CEXT_INVOKE_NO_WRAP ( "test_kwargs" , arg , Qtrue );
317
349
argn ++ ;
318
350
found_kwargs = true;
319
351
} else {
0 commit comments