Debian Codegraph @

include/ruby/internal/intern/vm.h

raw
1 #ifndef RBIMPL_INTERN_VM_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_INTERN_VM_H
3 /**
4 * @file
5 * @author Ruby developers <[email protected]>
6 * @copyright This file is a part of the programming language Ruby.
7 * Permission is hereby granted, to either redistribute and/or
8 * modify this file, provided that the conditions mentioned in the
9 * file COPYING are met. Consult the file for details.
10 * @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
11 * implementation details. Don't take them as canon. They could
12 * rapidly appear then vanish. The name (path) of this header file
13 * is also an implementation detail. Do not expect it to persist
14 * at the place it is now. Developers are free to move it anywhere
15 * anytime at will.
16 * @note To ruby-core: remember that this header can be possibly
17 * recursively included from extension libraries written in C++.
18 * Do not expect for instance `__VA_ARGS__` is always available.
19 * We assume C99 for ruby itself but we don't assume languages of
20 * extension libraries. They could be written in C++98.
21 * @brief Public APIs related to rb_cRubyVM.
22 */
23 #include "ruby/internal/attr/nonnull.h"
24 #include "ruby/internal/attr/noreturn.h"
25 #include "ruby/internal/dllexport.h"
26 #include "ruby/internal/value.h"
27
28 RBIMPL_SYMBOL_EXPORT_BEGIN()
29
30 /* vm.c */
31
32 /**
33 * Resembles `__LINE__`.
34 *
35 * @retval 0 Current execution context not in a ruby method.
36 * @retval otherwise The current line number of the current thread of the
37 * current ractor of the current execution context.
38 */
39 int rb_sourceline(void);
40
41 /**
42 * Resembles `__FILE__`.
43 *
44 * @retval 0 Current execution context not in a ruby method.
45 * @retval otherwise The current source path of the current thread of the
46 * current ractor of the current execution context.
47 * @note This may or may not be an absolute path.
48 */
49 const char *rb_sourcefile(void);
50
51 /**
52 * Resembles `__method__`.
53 *
54 * @param[out] idp Return buffer for method id.
55 * @param[out] klassp Return buffer for class.
56 * @retval 0 Current execution context not in a method.
57 * @retval 1 Successful return.
58 * @post Upon successful return `*idp` and `*klassp` are updated to have
59 * the current method name and its defined class respectively.
60 * @note Both parameters can be `NULL`.
61 */
62 int rb_frame_method_id_and_class(ID *idp, VALUE *klassp);
63
64 /* vm_eval.c */
65
66 /**
67 * Identical to rb_funcallv(), except it returns ::RUBY_Qundef instead of
68 * raising ::rb_eNoMethodError.
69 *
70 * @param[in,out] recv Receiver of the method.
71 * @param[in] mid Name of the method to call.
72 * @param[in] argc Number of arguments.
73 * @param[in] argv Arbitrary number of method arguments.
74 * @retval RUBY_Qundef `recv` doesn't respond to `mid`.
75 * @retval otherwise What the method evaluates to.
76 */
77 VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv);
78
79 /**
80 * Identical to rb_check_funcall(), except you can specify how to handle the
81 * last element of the given array. It can also be seen as a routine identical
82 * to rb_funcallv_kw(), except it returns ::RUBY_Qundef instead of raising
83 * ::rb_eNoMethodError.
84 *
85 * @param[in,out] recv Receiver of the method.
86 * @param[in] mid Name of the method to call.
87 * @param[in] argc Number of arguments.
88 * @param[in] argv Arbitrary number of method arguments.
89 * @param[in] kw_splat Handling of keyword parameters:
90 * - RB_NO_KEYWORDS `argv`'s last is not a keyword argument.
91 * - RB_PASS_KEYWORDS `argv`'s last is a keyword argument.
92 * - RB_PASS_CALLED_KEYWORDS it depends if there is a passed block.
93 * @retval RUBY_Qundef `recv` doesn't respond to `mid`.
94 * @retval otherwise What the method evaluates to.
95 */
96 VALUE rb_check_funcall_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat);
97
98 /**
99 * This API is practically a variant of rb_proc_call_kw() now. Historically
100 * when there still was a concept called `$SAFE`, this was an API for that.
101 * But we no longer have that. This function basically ended its role. It
102 * just remains here because of no harm.
103 *
104 * @param[in] cmd A string, or something callable.
105 * @param[in] arg Argument passed to the call.
106 * @param[in] kw_splat Handling of keyword parameters:
107 * - RB_NO_KEYWORDS `arg`'s last is not a keyword argument.
108 * - RB_PASS_KEYWORDS `arg`'s last is a keyword argument.
109 * - RB_PASS_CALLED_KEYWORDS it depends if there is a passed block.
110 * @return What the command evaluates to.
111 */
112 VALUE rb_eval_cmd_kw(VALUE cmd, VALUE arg, int kw_splat);
113
114 /**
115 * Identical to rb_funcallv(), except it takes Ruby's array instead of C's.
116 * @param[in,out] recv Receiver of the method.
117 * @param[in] mid Name of the method to call.
118 * @param[in] args An instance of ::RArray.
119 * @exception rb_eNoMethodError No such method.
120 * @exception rb_eException Any exceptions happen inside.
121 * @return What the method evaluates to.
122 * @pre `args` must be an ::RArray. Call `to_ary` beforehand when
123 * necessary.
124 */
125 VALUE rb_apply(VALUE recv, ID mid, VALUE args);
126
127 /**
128 * Evaluates a string containing Ruby source code, or the given block, within
129 * the context of the receiver. In order to set the context, the variable
130 * `self` is set to `recv` while the code is executing, giving the code access
131 * to `recv`'s instance variables and private methods.
132 *
133 * When given a block, `recv` is also passed in as the block's only argument.
134 *
135 * When given a string, the optional second and third parameters supply a
136 * filename and starting line number that are used when reporting compilation
137 * errors.
138 *
139 * @param[in] argc Number of objects in `argv`
140 * @param[in] argv C array of 0 up to 3 elements.
141 * @param[in] recv The object in question.
142 * @return What was evaluated.
143 */
144 VALUE rb_obj_instance_eval(int argc, const VALUE *argv, VALUE recv);
145
146 /**
147 * Executes the given block within the context of the receiver. In order to
148 * set the context, the variable `self` is set to `recv` while the code is
149 * executing, giving the code access to `recv`'s instance variables. Arguments
150 * are passed as block parameters.
151 *
152 * @param[in] argc Number of objects in `argv`
153 * @param[in] argv Arbitrary parameters to be passed to the block.
154 * @param[in] recv The object in question.
155 * @return What was evaluated.
156 * @note Don't confuse this with rb_obj_instance_eval(). The key
157 * difference is whether you can pass arbitrary parameters to the
158 * block, like this:
159 *
160 * ```ruby
161 * class Foo
162 * def initialize
163 * @foo = 5
164 * end
165 * end
166 * Foo.new.instance_exec(7) {|i| @foo + i } # => 12
167 * ```
168 */
169 VALUE rb_obj_instance_exec(int argc, const VALUE *argv, VALUE recv);
170
171 /**
172 * Identical to rb_obj_instance_eval(), except it evaluates within the context
173 * of module.
174 *
175 * @param[in] argc Number of objects in `argv`
176 * @param[in] argv C array of 0 up to 3 elements.
177 * @param[in] mod The module in question.
178 * @pre `mod` must be a Module.
179 * @return What was evaluated.
180 */
181 VALUE rb_mod_module_eval(int argc, const VALUE *argv, VALUE mod);
182
183 /**
184 * Identical to rb_obj_instance_exec(), except it evaluates within the context
185 * of module.
186 *
187 * @param[in] argc Number of objects in `argv`
188 * @param[in] argv Arbitrary parameters to be passed to the block.
189 * @param[in] mod The module in question.
190 * @pre `mod` must be a Module.
191 * @return What was evaluated.
192 */
193 VALUE rb_mod_module_exec(int argc, const VALUE *argv, VALUE mod);
194
195 /* vm_method.c */
196
197 /**
198 * @private
199 *
200 * @deprecated This macro once was a thing in the old days, but makes no sense
201 * any longer today. Exists here for backwards compatibility
202 * only. You can safely forget about it.
203 */
204 #define HAVE_RB_DEFINE_ALLOC_FUNC 1
205
206 /**
207 * This is the type of functions that ruby calls when trying to allocate an
208 * object. It is sometimes necessary to allocate extra memory regions for an
209 * object. When you define a class that uses ::RTypedData, it is typically the
210 * case. On such situations define a function of this type and pass it to
211 * rb_define_alloc_func().
212 *
213 * @param[in] klass The class that this function is registered.
214 * @return A newly allocated instance of `klass`.
215 */
216 typedef VALUE (*rb_alloc_func_t)(VALUE klass);
217
218 /**
219 * Sets the allocator function of a class.
220 *
221 * @param[out] klass The class to modify.
222 * @param[in] func An allocator function for the class.
223 * @pre `klass` must be an instance of Class.
224 */
225 void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func);
226
227 /**
228 * Deletes the allocator function of a class. It is sometimes desirable to
229 * restrict creation of an instance of a class. For example it rarely makes
230 * sense for a DB adaptor class to allow programmers creating DB row objects
231 * without querying the DB itself. You can kill sporadic creation of such
232 * objects then, by nullifying the allocator function using this API. Your
233 * object shall be allocated using #RB_NEWOBJ_OF() directly.
234 *
235 * @param[out] klass The class to modify.
236 * @pre `klass` must be an instance of Class.
237 */
238 void rb_undef_alloc_func(VALUE klass);
239
240 /**
241 * Queries the allocator function of a class.
242 *
243 * @param[in] klass The class in question.
244 * @pre `klass` must be an instance of Class.
245 * @retval 0 No allocator function is registered.
246 * @retval otherwise The allocator function.
247 *
248 * @internal
249 *
250 * Who cares? @shyouhei finds no practical usage of the return value. Maybe we
251 * need KonMari.
252 */
253 rb_alloc_func_t rb_get_alloc_func(VALUE klass);
254
255 /**
256 * Clears the inline constant caches associated with a particular ID. Extension
257 * libraries should not bother with such things. Just forget about this API (or
258 * even, the presence of constant caches).
259 */
260 void rb_clear_constant_cache_for_id(ID id);
261
262 /**
263 * Resembles `alias`.
264 *
265 * @param[out] klass Where to define an alias.
266 * @param[in] dst New name.
267 * @param[in] src Existing name.
268 * @exception rb_eTypeError `klass` is not a class.
269 * @exception rb_eFrozenError `klass` is frozen.
270 * @exception rb_eNameError No such method named `src`.
271 * @post `klass` has a method named `dst`, which is the identical to its
272 * method named `src`.
273 */
274 void rb_alias(VALUE klass, ID dst, ID src);
275
276 /**
277 * This function resembles now-deprecated `Module#attr`.
278 *
279 * @param[out] klass Where to define an attribute.
280 * @param[in] name Name of an instance variable.
281 * @param[in] need_reader Whether attr_reader is needed.
282 * @param[in] need_writer Whether attr_writer is needed.
283 * @param[in] honour_visibility Whether to use the current visibility.
284 * @exception rb_eTypeError `klass` is not a class.
285 * @exception rb_eFrozenError `klass` is frozen.
286 * @post If `need_reader` is set `klass` has a method named `name`.
287 * @post If `need_writer` is set `klass` has a method named `name=`.
288 *
289 * @internal
290 *
291 * The three `int` arguments should have been bool, but there was no such thing
292 * like a bool when K&R was used in this project.
293 */
294 void rb_attr(VALUE klass, ID name, int need_reader, int need_writer, int honour_visibility);
295
296 RBIMPL_ATTR_NONNULL(())
297 /**
298 * Removes a method. Don't confuse this to rb_undef_method(), which doesn't
299 * remove a method. This one resembles `Module#remove_method`.
300 *
301 * @param[out] klass The class to remove a method.
302 * @param[in] name Name of a method to be removed.
303 * @exception rb_eTypeError `klass` is a non-module.
304 * @exception rb_eFrozenError `klass` is frozen.
305 * @exception rb_eNameError No such method.
306 * @see rb_undef_method
307 */
308 void rb_remove_method(VALUE klass, const char *name);
309
310 /**
311 * Identical to rb_remove_method(), except it accepts the method name as ::ID.
312 *
313 * @param[out] klass The class to remove a method.
314 * @param[in] mid Name of a method to be removed.
315 * @exception rb_eTypeError `klass` is a non-module.
316 * @exception rb_eFrozenError `klass` is frozen.
317 * @exception rb_eNameError No such method.
318 * @see rb_undef
319 */
320 void rb_remove_method_id(VALUE klass, ID mid);
321
322 /**
323 * Queries if the klass has this method. This function has only one line of
324 * document in the implementation that states "// deprecated". Don't know what
325 * that means though.
326 *
327 * @param[in] klass The class in question.
328 * @param[in] id The method name to query.
329 * @param[in] ex Undocumented magic value.
330 * @retval false Method not found.
331 * @retval true There is a method.
332 * @pre `klass` must be a module.
333 *
334 * @internal
335 *
336 * @shyouhei has no motivation to describe what should be passed to `ex`. It
337 * seems this function should just be trashed.
338 */
339 int rb_method_boundp(VALUE klass, ID id, int ex);
340
341 /**
342 * Well... Let us hesitate from describing what a "basic definition" is. This
343 * nuanced concept should have been kept private. Just please. Don't touch
344 * it. This function is a badly distributed random number generator. Right?
345 *
346 * @param[in] klass The class in question.
347 * @param[in] mid The method name in question.
348 * @retval 1 It is.
349 * @retval 0 It isn't.
350 */
351 int rb_method_basic_definition_p(VALUE klass, ID mid);
352
353 /**
354 * Identical to rb_respond_to(), except it additionally takes the visibility
355 * parameter. This does not make difference unless the object has
356 * `respond_to?` undefined, but has `respond_to_missing?` defined. That case
357 * the passed argument becomes the second argument of `respond_to_missing?`.
358 *
359 * @param[in] obj The object in question.
360 * @param[in] mid The method name in question.
361 * @param[in] private_p This is the second argument of `obj`'s
362 * `respond_to_missing?`.
363 * @retval 1 Yes it does.
364 * @retval 0 No it doesn't.
365 */
366 int rb_obj_respond_to(VALUE obj, ID mid, int private_p);
367
368 /**
369 * Queries if the object responds to the method. This involves calling the
370 * object's `respond_to?` method.
371 *
372 * @param[in] obj The object in question.
373 * @param[in] mid The method name in question.
374 * @retval 1 Yes it does.
375 * @retval 0 No it doesn't.
376 */
377 int rb_respond_to(VALUE obj, ID mid);
378
379 RBIMPL_ATTR_NORETURN()
380 /**
381 * Raises ::rb_eNotImpError. This function is used as an argument to
382 * rb_define_method() etc.
383 *
384 * ```CXX
385 * rb_define_method(rb_cFoo, "foo", rb_f_notimplement, -1);
386 * ```
387 *
388 * @param argc Unused parameter.
389 * @param argv Unused parameter.
390 * @param obj Unused parameter.
391 * @param marker Unused parameter.
392 * @exception rb_eNotImpError Always.
393 * @return Never returns.
394 *
395 * @internal
396 *
397 * See also the Q&A section of include/ruby/internal/anyargs.h.
398 */
399 VALUE rb_f_notimplement(int argc, const VALUE *argv, VALUE obj, VALUE marker);
400 #if !defined(RUBY_EXPORT) && defined(_WIN32)
401 RUBY_EXTERN VALUE (*const rb_f_notimplement_)(int, const VALUE *, VALUE, VALUE marker);
402 #define rb_f_notimplement (*rb_f_notimplement_)
403 #endif
404
405 /* vm_backtrace.c */
406
407 /**
408 * Prints the backtrace out to the standard error. This just confuses people
409 * for no reason. Evil souls must only use it.
410 *
411 * @internal
412 *
413 * Actually it is very useful when called from an interactive GDB session.
414 */
415 void rb_backtrace(void);
416
417 /**
418 * Creates the good old fashioned array-of-strings style backtrace info.
419 *
420 * @return An array which contains strings, which are the textual
421 * representations of the backtrace locations of the current thread of
422 * the current ractor of the current execution context.
423 * @note Ruby scripts can access more sophisticated
424 * `Thread::Backtrace::Location`. But it seems there is no way for C
425 * extensions to use that API.
426 */
427 VALUE rb_make_backtrace(void);
428
429 RBIMPL_SYMBOL_EXPORT_END()
430
431 #endif /* RBIMPL_INTERN_VM_H */