Debian Codegraph @

include/ruby/internal/intern/object.h

raw
1 #ifndef RBIMPL_INTERN_OBJECT_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_INTERN_OBJECT_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_cObject.
22 */
23 #include "ruby/internal/attr/const.h"
24 #include "ruby/internal/attr/deprecated.h"
25 #include "ruby/internal/attr/nonnull.h"
26 #include "ruby/internal/attr/pure.h"
27 #include "ruby/internal/dllexport.h"
28 #include "ruby/internal/value.h"
29
30 RBIMPL_SYMBOL_EXPORT_BEGIN()
31
32 /**
33 * This macro is (used but) mysterious. Why on earth do we need this?
34 *
35 * - `obj != orig` check is done anyways inside of rb_obj_init_copy().
36 * - rb_obj_init_copy() returns something. No need are there to add `, 1`.
37 */
38 #define RB_OBJ_INIT_COPY(obj, orig) \
39 ((obj) != (orig) && (rb_obj_init_copy((obj), (orig)), 1))
40 /** @old{RB_OBJ_INIT_COPY} */
41 #define OBJ_INIT_COPY(obj, orig) RB_OBJ_INIT_COPY(obj, orig)
42
43 /* object.c */
44
45 /**
46 * Identical to rb_class_new_instance(), except it passes the passed keywords
47 * if any to the `#initialize` method.
48 *
49 * @param[in] argc Number of objects of `argv`.
50 * @param[in] argv Arbitrary number of method arguments.
51 * @param[in] klass An instance of ::rb_cClass.
52 * @exception rb_eTypeError `klass`'s allocator is undefined.
53 * @exception rb_eException Any exceptions can happen inside.
54 * @return An allocated new instance of `klass`.
55 * @note This is _the_ implementation of `Object.new`.
56 */
57 VALUE rb_class_new_instance_pass_kw(int argc, const VALUE *argv, VALUE klass);
58
59 /**
60 * Allocates, then initialises an instance of the given class. It first calls
61 * the passed class' allocator to obtain an uninitialised object, then calls
62 * its initialiser with the remaining arguments.
63 *
64 * @param[in] argc Number of objects of `argv`.
65 * @param[in] argv Arguments passed to `#initialize`.
66 * @param[in] klass An instance of ::rb_cClass.
67 * @exception rb_eTypeError `klass`'s allocator is undefined.
68 * @exception rb_eException Any exceptions can happen inside.
69 * @return An allocated new instance of `klass`.
70 */
71 VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass);
72
73 /**
74 * Identical to rb_class_new_instance(), except you can specify how to handle
75 * the last element of the given array.
76 *
77 * @param[in] argc Number of objects of `argv`.
78 * @param[in] argv Arbitrary number of method arguments.
79 * @param[in] klass An instance of ::rb_cClass.
80 * @param[in] kw_splat Handling of keyword parameters:
81 * - RB_NO_KEYWORDS `argv`'s last is not a keyword argument.
82 * - RB_PASS_KEYWORDS `argv`'s last is a keyword argument.
83 * - RB_PASS_CALLED_KEYWORDS it depends if there is a passed block.
84 * @exception rb_eTypeError `klass`'s allocator is undefined.
85 * @exception rb_eException Any exceptions can happen inside.
86 * @return An allocated new instance of `klass`.
87 */
88 VALUE rb_class_new_instance_kw(int argc, const VALUE *argv, VALUE klass, int kw_splat);
89
90 /**
91 * Checks for equality of the passed objects, in terms of `Object#eql?`.
92 *
93 * @param[in] lhs Comparison left hand side.
94 * @param[in] rhs Comparison right hand side.
95 * @retval non-zero They are equal.
96 * @retval 0 Otherwise.
97 * @note This function actually calls `lhs.eql?(rhs)` so you cannot
98 * implement your class' `#eql?` method using it.
99 */
100 int rb_eql(VALUE lhs, VALUE rhs);
101
102 /**
103 * Generates a textual representation of the given object.
104 *
105 * @param[in] obj Arbitrary ruby object.
106 * @return An instance of ::rb_cString that represents `obj`.
107 * @note This is the default implementation of `Object#to_s` that each
108 * subclasses want to override.
109 */
110 VALUE rb_any_to_s(VALUE obj);
111
112 /**
113 * Generates a human-readable textual representation of the given object. This
114 * is largely similar to Ruby level `Object#inspect` but not the same; it
115 * additionally escapes the inspection result so that the string be compatible
116 * with that of default internal (or default external, if absent).
117 *
118 * @param[in] obj Arbitrary ruby object.
119 * @return An instance of ::rb_cString that represents `obj`.
120 */
121 VALUE rb_inspect(VALUE obj);
122
123 /**
124 * Queries if the given object is a direct instance of the given class.
125 *
126 * @param[in] obj Arbitrary ruby object.
127 * @param[in] klass An instance of ::rb_cModule.
128 * @exception rb_eTypeError `klass` is neither module nor class.
129 * @retval RUBY_Qtrue `obj` is an instance of `klass`.
130 * @retval RUBY_Qfalse Otherwise.
131 */
132 VALUE rb_obj_is_instance_of(VALUE obj, VALUE klass);
133
134 /**
135 * Queries if the given object is an instance (of possibly descendants) of the
136 * given class.
137 *
138 * @param[in] obj Arbitrary ruby object.
139 * @param[in] klass An instance of ::rb_cModule.
140 * @exception rb_eTypeError `klass` is neither module nor class.
141 * @retval RUBY_Qtrue `obj` is a `klass`.
142 * @retval RUBY_Qfalse Otherwise.
143 */
144 VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass);
145
146 /**
147 * Allocates an instance of the given class.
148 *
149 * @param[in] klass A class to instantiate.
150 * @exception rb_eTypeError `klass` is not a class.
151 * @return An allocated, not yet initialised instance of `klass`.
152 * @note It calls the allocator defined by rb_define_alloc_func(). You
153 * cannot use this function to define an allocator. Use
154 * rb_newobj_of(), #TypedData_Make_Struct or others, instead.
155 * @note Usually prefer rb_class_new_instance() to rb_obj_alloc() and
156 * rb_obj_call_init().
157 * @see rb_class_new_instance()
158 * @see rb_obj_call_init()
159 * @see rb_define_alloc_func()
160 * @see rb_newobj_of()
161 * @see #TypedData_Make_Struct
162 */
163 VALUE rb_obj_alloc(VALUE klass);
164
165 /**
166 * Produces a shallow copy of the given object. Its list of instance variables
167 * are copied, but not the objects they reference. It also copies the frozen
168 * value state.
169 *
170 * @param[in] obj Arbitrary ruby object.
171 * @exception rb_eException `#initialize_copy` can raise anything.
172 * @return A "clone" of `obj`.
173 *
174 * @internal
175 *
176 * Unlike ruby-level `Object#clone`, there is no way to control the frozen-ness
177 * of the return value.
178 */
179 VALUE rb_obj_clone(VALUE obj);
180
181 /**
182 * Duplicates the given object. This does almost the same thing as
183 * rb_obj_clone() do. However it does not copy the singleton class (if any).
184 * It also doesn't copy frozen-ness.
185 *
186 * @param[in] obj Arbitrary ruby object.
187 * @exception rb_eException `#initialize_copy` can raise anything.
188 * @return A shallow copy of `obj`.
189 */
190 VALUE rb_obj_dup(VALUE obj);
191
192 /**
193 * Default implementation of `#initialize_copy`, `#initialize_dup` and
194 * `#initialize_clone`. It does almost nothing. Just raises exceptions for
195 * checks.
196 *
197 * @param[in] dst The destination object.
198 * @param[in] src The source object.
199 * @exception rb_eFrozenError `dst` is frozen.
200 * @exception rb_eTypeError `dst` and `src` have different classes.
201 * @return Always returns `dst`.
202 */
203 VALUE rb_obj_init_copy(VALUE src, VALUE dst);
204
205 /**
206 * Just calls rb_obj_freeze_inline() inside. Does this make any sens to
207 * extension libraries?
208 *
209 * @param[out] obj Object to freeze.
210 * @return Verbatim `obj`.
211 */
212 VALUE rb_obj_freeze(VALUE obj);
213
214 RBIMPL_ATTR_PURE()
215 /**
216 * Just calls RB_OBJ_FROZEN() inside. Does this make any sens to extension
217 * libraries?
218 *
219 * @param[in] obj Object in question.
220 * @retval RUBY_Qtrue Yes it is.
221 * @retval RUBY_Qfalse No it isn't.
222 */
223 VALUE rb_obj_frozen_p(VALUE obj);
224
225 /* gc.c */
226
227 /**
228 * Finds or creates an integer primary key of the given object. In the old
229 * days this function was a purely arithmetic operation that maps the
230 * underlying memory address where the object resides into a Ruby's integer.
231 * Some time around 2.x this changed. It no longer relates its return values
232 * to C level pointers. This function assigns some random number to the given
233 * object if absent. The same number will be returned on all subsequent
234 * requests. No two active objects share a number.
235 *
236 * @param[in] obj Arbitrary ruby object.
237 * @return An instance of ::rb_cInteger which is an "identifier" of `obj`.
238 *
239 * @internal
240 *
241 * The "some random number" is in fact a monotonic-increasing process-global
242 * unique integer, much like an `INTEGER AUTO_INCREMENT PRIMARY KEY` column in
243 * a MySQL table.
244 */
245 VALUE rb_obj_id(VALUE obj);
246
247 RBIMPL_ATTR_CONST()
248 /**
249 * Identical to rb_obj_id(), except it hesitates from allocating a new instance
250 * of ::rb_cInteger. rb_obj_id() could allocate ::RUBY_T_BIGNUM objects. That
251 * allocation might perhaps impact negatively. On such situations, this
252 * function instead returns one-shot temporary small integers that need no
253 * allocations at all. The values are guaranteed unique at the moment, but no
254 * future promise is made; could be reused. Use of this API should be very
255 * instant. It is a failure to store the returned integer to somewhere else.
256 *
257 * In short it is difficult to use.
258 *
259 * @param[in] obj Arbitrary ruby object.
260 * @return An instance of ::rb_cInteger unique at the moment.
261 *
262 * @internal
263 *
264 * This is roughly the old behaviour of rb_obj_id().
265 */
266 VALUE rb_memory_id(VALUE obj);
267
268 /* object.c */
269
270 RBIMPL_ATTR_PURE()
271 /**
272 * Finds a "real" class. As the name implies there are class objects that are
273 * surreal. This function takes a class, traverses its ancestry tree, and
274 * returns its nearest ancestor which is neither a module nor a singleton
275 * class.
276 *
277 * @param[in] klass An instance of ::rb_cClass.
278 * @retval RUBY_Qfalse No real class in `klass`' ancestry tree.
279 * @retval klass `klass` itself is a real class.
280 * @retval otherwise Nearest ancestor of `klass` who is real.
281 */
282 VALUE rb_class_real(VALUE klass);
283
284 RBIMPL_ATTR_PURE()
285 /**
286 * Determines if the given two modules are relatives.
287 *
288 * @param[in] scion Possible subclass.
289 * @param[in] ascendant Possible superclass.
290 * @exception rb_eTypeError `ascendant` is not a module.
291 * @retval RUBY_Qtrue `scion` inherits, or is equal to `ascendant`.
292 * @retval RUBY_Qfalse `ascendant` inherits `scion`.
293 * @retval RUBY_Qnil They are not relatives.
294 */
295 VALUE rb_class_inherited_p(VALUE scion, VALUE ascendant);
296
297 RBIMPL_ATTR_PURE()
298 /**
299 * Queries the parent of the given class.
300 *
301 * @param[in] klass A child class.
302 * @exception rb_eTypeError `klass` is a `Class.allocate`.
303 * @retval RUBY_Qfalse `klass` has no superclass.
304 * @retval otherwise `klass`' superclass.
305 *
306 * @internal
307 *
308 * Is there any class except ::rb_cBasicObject, that has no superclass?
309 */
310 VALUE rb_class_superclass(VALUE klass);
311
312 RBIMPL_ATTR_NONNULL(())
313 /**
314 * Converts an object into another type. Calls the specified conversion method
315 * if necessary.
316 *
317 * @param[in] val An object to convert.
318 * @param[in] type A value of enum ::ruby_value_type.
319 * @param[in] name Name to display on error (e.g. "Array").
320 * @param[in] mid Conversion method (e.g. "to_ary").
321 * @exception rb_eTypeError Failed to convert.
322 * @return An object of the specified type.
323 */
324 VALUE rb_convert_type(VALUE val, int type, const char *name, const char *mid);
325
326 RBIMPL_ATTR_NONNULL(())
327 /**
328 * Identical to rb_convert_type(), except it returns ::RUBY_Qnil instead of
329 * raising exceptions, in case of conversion failure. It still raises
330 * exceptions for various reasons, like when the conversion method itself
331 * raises, though.
332 *
333 * @param[in] val An object to convert.
334 * @param[in] type A value of enum ::ruby_value_type.
335 * @param[in] name Name to display on error (e.g. "Array").
336 * @param[in] mid Conversion method (e.g. "to_ary").
337 * @exception rb_eTypeError The `mid` does not generate `type`.
338 * @retval RUBY_Qnil No conversion defined.
339 * @retval otherwise An object of the specified type.
340 */
341 VALUE rb_check_convert_type(VALUE val, int type, const char *name, const char *mid);
342
343 RBIMPL_ATTR_NONNULL(())
344 /**
345 * Identical to rb_check_convert_type(), except the return value type is fixed
346 * to ::rb_cInteger.
347 *
348 * @param[in] val An object to convert.
349 * @param[in] mid Conversion method (e.g. "to_ary").
350 * @exception rb_eTypeError The `mid` does not generate an integer.
351 * @retval RUBY_Qnil No conversion defined.
352 * @retval otherwise An instance of ::rb_cInteger.
353 */
354 VALUE rb_check_to_integer(VALUE val, const char *mid);
355
356 /**
357 * This is complicated.
358 *
359 * - When the passed object is already an instance of ::rb_cFloat, just
360 * returns it as-is.
361 *
362 * - When the passed object is something numeric, the function tries to
363 * convert it using `#to_f` method.
364 *
365 * - If that conversion fails (this happens for instance when the numeric
366 * is a complex) it returns ::RUBY_Qnil.
367 *
368 * - Otherwise returns the conversion result.
369 *
370 * - Otherwise it also returns ::RUBY_Qnil.
371 *
372 * @param[in] val An object to convert.
373 * @retval RUBY_Qnil Conversion from `val` to float is undefined.
374 * @retval otherwise Converted result.
375 */
376 VALUE rb_check_to_float(VALUE val);
377
378 /**
379 * Identical to rb_check_to_int(), except it raises in case of conversion
380 * mismatch.
381 *
382 * @param[in] val An object to convert.
383 * @exception rb_eTypeError `#to_int` does not generate an integer.
384 * @return An instance of ::rb_cInteger.
385 */
386 VALUE rb_to_int(VALUE val);
387
388 /**
389 * Identical to rb_check_to_integer(), except it uses `#to_int` for conversion.
390 *
391 * @param[in] val An object to convert.
392 * @exception rb_eTypeError `#to_int` does not return an integer.
393 * @retval RUBY_Qnil No conversion defined.
394 * @retval otherwise An instance of ::rb_cInteger.
395 */
396 VALUE rb_check_to_int(VALUE val);
397
398 /**
399 * This is the logic behind `Kernel#Integer`. Numeric types are converted
400 * directly, with floating point numbers being truncated. Strings are
401 * interpreted strictly; only leading/trailing whitespaces, plus/minus sign,
402 * radix indicators such as `0x`, digits, and underscores are allowed.
403 * Anything else are converted by first trying `#to_int`, then `#to_i`.
404 *
405 * This is slightly stricter than `String#to_i`.
406 *
407 * @param[in] val An object to convert.
408 * @exception rb_eArgError Malformed `val` passed.
409 * @exception rb_eTypeError No conversion defined.
410 * @return An instance of ::rb_cInteger.
411 */
412 VALUE rb_Integer(VALUE val);
413
414 /**
415 * Identical to rb_check_to_float(), except it raises on error.
416 *
417 * @param[in] val An object to convert.
418 * @exception rb_eTypeError No conversion defined.
419 * @return An instance of ::rb_cFloat.
420 */
421 VALUE rb_to_float(VALUE val);
422
423 /**
424 * This is the logic behind `Kernel#Float`. Numeric types are converted
425 * directly to the nearest value that a Float can represent. Strings are
426 * interpreted strictly; only leading/trailing whitespaces are allowed except
427 * what `strtod` understands. Anything else are converted using `#to_f`.
428 *
429 * This is slightly stricter than `String#to_f`.
430 *
431 * @param[in] val An object to convert.
432 * @exception rb_eArgError Malformed `val` passed.
433 * @exception rb_eTypeError No conversion defined.
434 * @return An instance of ::rb_cFloat.
435 */
436 VALUE rb_Float(VALUE val);
437
438 /**
439 * This is the logic behind `Kernel#String`. Arguments are converted by first
440 * trying `#to_str`, then `#to_s`.
441 *
442 * @param[in] val An object to convert.
443 * @exception rb_eTypeError No conversion defined.
444 * @return An instance of ::rb_cString.
445 */
446 VALUE rb_String(VALUE val);
447
448 /**
449 * This is the logic behind `Kernel#Array`. Arguments are converted by first
450 * trying `#to_ary`, then `#to_a`, and if both failed, returns an array of
451 * length 1 that contains the passed argument as the sole contents.
452 *
453 * @param[in] val An object to convert.
454 * @return An instance of ::rb_cArray.
455 */
456 VALUE rb_Array(VALUE val);
457
458 /**
459 * This is the logic behind `Kernel#Hash`. Arguments are converted by first
460 * trying `#to_hash`. if it failed, and the argument is either ::RUBY_Qnil or
461 * an empty array, returns an empty hash. Otherwise an exception is raised.
462 *
463 * @param[in] val An object to convert.
464 * @exception rb_eTypeError No conversion defined.
465 * @return An instance of ::rb_cHash.
466 */
467 VALUE rb_Hash(VALUE val);
468
469 RBIMPL_ATTR_NONNULL(())
470 /**
471 * Converts a textual representation of a real number into a numeric, which is
472 * the nearest value that the return type can represent, of the value that the
473 * argument represents. This is in fact a 2-in-1 function whose behaviour can
474 * be controlled using the second (mode) argument. If the mode is zero, this
475 * function is in "historical" mode which only understands "floating-constant"
476 * defined at ISO/IEC 9899:1990 section 6.1.3.1. If the mode is nonzero, it is
477 * in "extended" mode, which also accepts "hexadecimal-floating-constant"
478 * defined at ISO/IEC 9899:2018 section 6.4.4.2.
479 *
480 * @param[in] str A textual representation of a real number.
481 * @param[in] mode Conversion mode, as described above.
482 * @exception rb_eArgError Malformed `str` passed.
483 * @see https://bugs.ruby-lang.org/issues/2969
484 * @note Null pointers are allowed, and it returns 0.0 then.
485 */
486 double rb_cstr_to_dbl(const char *str, int mode);
487
488 /**
489 * Identical to rb_cstr_to_dbl(), except it accepts a Ruby's string instead of
490 * C's.
491 *
492 * @param[in] str A textual representation of a real number.
493 * @param[in] mode Conversion mode, as described in rb_cstr_to_dbl().
494 * @exception rb_eArgError Malformed `str` passed.
495 * @see https://bugs.ruby-lang.org/issues/2969
496 */
497 double rb_str_to_dbl(VALUE str, int mode);
498
499 RBIMPL_SYMBOL_EXPORT_END()
500
501 #endif /* RBIMPL_INTERN_OBJECT_H */