Ruby  3.4.0dev (2024-11-05 revision 348a53415339076afc4a02fcd09f3ae36e9c4c61)
object.c (348a53415339076afc4a02fcd09f3ae36e9c4c61)
1 /**********************************************************************
2 
3  object.c -
4 
5  $Author$
6  created at: Thu Jul 15 12:01:24 JST 1993
7 
8  Copyright (C) 1993-2007 Yukihiro Matsumoto
9  Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10  Copyright (C) 2000 Information-technology Promotion Agency, Japan
11 
12 **********************************************************************/
13 
14 #include "ruby/internal/config.h"
15 
16 #include <ctype.h>
17 #include <errno.h>
18 #include <float.h>
19 #include <math.h>
20 #include <stdio.h>
21 
22 #include "constant.h"
23 #include "id.h"
24 #include "internal.h"
25 #include "internal/array.h"
26 #include "internal/class.h"
27 #include "internal/error.h"
28 #include "internal/eval.h"
29 #include "internal/inits.h"
30 #include "internal/numeric.h"
31 #include "internal/object.h"
32 #include "internal/struct.h"
33 #include "internal/string.h"
34 #include "internal/st.h"
35 #include "internal/symbol.h"
36 #include "internal/variable.h"
37 #include "variable.h"
38 #include "probes.h"
39 #include "ruby/encoding.h"
40 #include "ruby/st.h"
41 #include "ruby/util.h"
42 #include "ruby/assert.h"
43 #include "builtin.h"
44 #include "shape.h"
45 #include "yjit.h"
46 
47 /* Flags of RObject
48  *
49  * 1: ROBJECT_EMBED
50  * The object has its instance variables embedded (the array of
51  * instance variables directly follow the object, rather than being
52  * on a separately allocated buffer).
53  * if !SHAPE_IN_BASIC_FLAGS
54  * 4-19: SHAPE_FLAG_MASK
55  * Shape ID for the object.
56  * endif
57  */
58 
70 
74 
75 static VALUE rb_cNilClass_to_s;
76 static VALUE rb_cTrueClass_to_s;
77 static VALUE rb_cFalseClass_to_s;
78 
81 #define id_eq idEq
82 #define id_eql idEqlP
83 #define id_match idEqTilde
84 #define id_inspect idInspect
85 #define id_init_copy idInitialize_copy
86 #define id_init_clone idInitialize_clone
87 #define id_init_dup idInitialize_dup
88 #define id_const_missing idConst_missing
89 #define id_to_f idTo_f
90 
91 #define CLASS_OR_MODULE_P(obj) \
92  (!SPECIAL_CONST_P(obj) && \
93  (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE))
94 
97 size_t
98 rb_obj_embedded_size(uint32_t numiv)
99 {
100  return offsetof(struct RObject, as.ary) + (sizeof(VALUE) * numiv);
101 }
102 
103 VALUE
105 {
106  if (!SPECIAL_CONST_P(obj)) {
107  RBASIC_CLEAR_CLASS(obj);
108  }
109  return obj;
110 }
111 
112 VALUE
114 {
115  if (!SPECIAL_CONST_P(obj)) {
116  RBASIC_SET_CLASS(obj, klass);
117  }
118  return obj;
119 }
120 
121 VALUE
122 rb_class_allocate_instance(VALUE klass)
123 {
124  uint32_t index_tbl_num_entries = RCLASS_EXT(klass)->max_iv_count;
125 
126  size_t size = rb_obj_embedded_size(index_tbl_num_entries);
127  if (!rb_gc_size_allocatable_p(size)) {
128  size = sizeof(struct RObject);
129  }
130 
131  NEWOBJ_OF(o, struct RObject, klass,
132  T_OBJECT | ROBJECT_EMBED | (RGENGC_WB_PROTECTED_OBJECT ? FL_WB_PROTECTED : 0), size, 0);
133  VALUE obj = (VALUE)o;
134 
135  RUBY_ASSERT(rb_shape_get_shape(obj)->type == SHAPE_ROOT);
136 
137  // Set the shape to the specific T_OBJECT shape.
138  ROBJECT_SET_SHAPE_ID(obj, (shape_id_t)(rb_gc_heap_id_for_size(size) + FIRST_T_OBJECT_SHAPE_ID));
139 
140 #if RUBY_DEBUG
141  RUBY_ASSERT(!rb_shape_obj_too_complex(obj));
142  VALUE *ptr = ROBJECT_IVPTR(obj);
143  for (size_t i = 0; i < ROBJECT_IV_CAPACITY(obj); i++) {
144  ptr[i] = Qundef;
145  }
146 #endif
147 
148  return obj;
149 }
150 
151 VALUE
153 {
154  VALUE ignored_flags = RUBY_FL_PROMOTED | RUBY_FL_SEEN_OBJ_ID;
155  RBASIC(obj)->flags = (type & ~ignored_flags) | (RBASIC(obj)->flags & ignored_flags);
156  RBASIC_SET_CLASS(obj, klass);
157  return obj;
158 }
159 
160 /*
161  * call-seq:
162  * true === other -> true or false
163  * false === other -> true or false
164  * nil === other -> true or false
165  *
166  * Returns +true+ or +false+.
167  *
168  * Like Object#==, if +object+ is an instance of Object
169  * (and not an instance of one of its many subclasses).
170  *
171  * This method is commonly overridden by those subclasses,
172  * to provide meaningful semantics in +case+ statements.
173  */
174 #define case_equal rb_equal
175  /* The default implementation of #=== is
176  * to call #== with the rb_equal() optimization. */
177 
178 VALUE
179 rb_equal(VALUE obj1, VALUE obj2)
180 {
181  VALUE result;
182 
183  if (obj1 == obj2) return Qtrue;
184  result = rb_equal_opt(obj1, obj2);
185  if (UNDEF_P(result)) {
186  result = rb_funcall(obj1, id_eq, 1, obj2);
187  }
188  return RBOOL(RTEST(result));
189 }
190 
191 int
192 rb_eql(VALUE obj1, VALUE obj2)
193 {
194  VALUE result;
195 
196  if (obj1 == obj2) return TRUE;
197  result = rb_eql_opt(obj1, obj2);
198  if (UNDEF_P(result)) {
199  result = rb_funcall(obj1, id_eql, 1, obj2);
200  }
201  return RTEST(result);
202 }
203 
207 VALUE
208 rb_obj_equal(VALUE obj1, VALUE obj2)
209 {
210  return RBOOL(obj1 == obj2);
211 }
212 
213 VALUE rb_obj_hash(VALUE obj);
214 
219 VALUE
220 rb_obj_not(VALUE obj)
221 {
222  return RBOOL(!RTEST(obj));
223 }
224 
229 VALUE
230 rb_obj_not_equal(VALUE obj1, VALUE obj2)
231 {
232  VALUE result = rb_funcall(obj1, id_eq, 1, obj2);
233  return rb_obj_not(result);
234 }
235 
236 VALUE
238 {
239  while (cl &&
240  (RCLASS_SINGLETON_P(cl) || BUILTIN_TYPE(cl) == T_ICLASS)) {
241  cl = RCLASS_SUPER(cl);
242  }
243  return cl;
244 }
245 
246 VALUE
248 {
249  return rb_class_real(CLASS_OF(obj));
250 }
251 
252 /*
253  * call-seq:
254  * obj.singleton_class -> class
255  *
256  * Returns the singleton class of <i>obj</i>. This method creates
257  * a new singleton class if <i>obj</i> does not have one.
258  *
259  * If <i>obj</i> is <code>nil</code>, <code>true</code>, or
260  * <code>false</code>, it returns NilClass, TrueClass, or FalseClass,
261  * respectively.
262  * If <i>obj</i> is an Integer, a Float or a Symbol, it raises a TypeError.
263  *
264  * Object.new.singleton_class #=> #<Class:#<Object:0xb7ce1e24>>
265  * String.singleton_class #=> #<Class:String>
266  * nil.singleton_class #=> NilClass
267  */
268 
269 static VALUE
270 rb_obj_singleton_class(VALUE obj)
271 {
272  return rb_singleton_class(obj);
273 }
274 
276 void
277 rb_obj_copy_ivar(VALUE dest, VALUE obj)
278 {
279  RUBY_ASSERT(!RB_TYPE_P(obj, T_CLASS) && !RB_TYPE_P(obj, T_MODULE));
280 
281  RUBY_ASSERT(BUILTIN_TYPE(dest) == BUILTIN_TYPE(obj));
282  rb_shape_t * src_shape = rb_shape_get_shape(obj);
283 
284  if (rb_shape_obj_too_complex(obj)) {
285  // obj is TOO_COMPLEX so we can copy its iv_hash
286  st_table *table = st_copy(ROBJECT_IV_HASH(obj));
287  rb_obj_convert_to_too_complex(dest, table);
288 
289  return;
290  }
291 
292  uint32_t src_num_ivs = RBASIC_IV_COUNT(obj);
293  rb_shape_t * shape_to_set_on_dest = src_shape;
294  VALUE * src_buf;
295  VALUE * dest_buf;
296 
297  if (!src_num_ivs) {
298  return;
299  }
300 
301  // The copy should be mutable, so we don't want the frozen shape
302  if (rb_shape_frozen_shape_p(src_shape)) {
303  shape_to_set_on_dest = rb_shape_get_parent(src_shape);
304  }
305 
306  src_buf = ROBJECT_IVPTR(obj);
307  dest_buf = ROBJECT_IVPTR(dest);
308 
309  rb_shape_t * initial_shape = rb_shape_get_shape(dest);
310 
311  if (initial_shape->heap_index != src_shape->heap_index) {
312  RUBY_ASSERT(initial_shape->type == SHAPE_T_OBJECT);
313 
314  shape_to_set_on_dest = rb_shape_rebuild_shape(initial_shape, src_shape);
315  if (UNLIKELY(rb_shape_id(shape_to_set_on_dest) == OBJ_TOO_COMPLEX_SHAPE_ID)) {
316  st_table * table = rb_st_init_numtable_with_size(src_num_ivs);
317  rb_obj_copy_ivs_to_hash_table(obj, table);
318  rb_obj_convert_to_too_complex(dest, table);
319 
320  return;
321  }
322  }
323 
324  RUBY_ASSERT(src_num_ivs <= shape_to_set_on_dest->capacity || rb_shape_id(shape_to_set_on_dest) == OBJ_TOO_COMPLEX_SHAPE_ID);
325  if (initial_shape->capacity < shape_to_set_on_dest->capacity) {
326  rb_ensure_iv_list_size(dest, initial_shape->capacity, shape_to_set_on_dest->capacity);
327  dest_buf = ROBJECT_IVPTR(dest);
328  }
329 
330  MEMCPY(dest_buf, src_buf, VALUE, src_num_ivs);
331 
332  // Fire write barriers
333  for (uint32_t i = 0; i < src_num_ivs; i++) {
334  RB_OBJ_WRITTEN(dest, Qundef, dest_buf[i]);
335  }
336 
337  rb_shape_set_shape(dest, shape_to_set_on_dest);
338 }
339 
340 static void
341 init_copy(VALUE dest, VALUE obj)
342 {
343  if (OBJ_FROZEN(dest)) {
344  rb_raise(rb_eTypeError, "[bug] frozen object (%s) allocated", rb_obj_classname(dest));
345  }
346  RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
347  // Copies the shape id from obj to dest
348  RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR);
349  rb_gc_copy_attributes(dest, obj);
350  rb_copy_generic_ivar(dest, obj);
351  if (RB_TYPE_P(obj, T_OBJECT)) {
352  rb_obj_copy_ivar(dest, obj);
353  }
354 }
355 
356 static VALUE immutable_obj_clone(VALUE obj, VALUE kwfreeze);
357 static VALUE mutable_obj_clone(VALUE obj, VALUE kwfreeze);
358 PUREFUNC(static inline int special_object_p(VALUE obj));
359 static inline int
360 special_object_p(VALUE obj)
361 {
362  if (SPECIAL_CONST_P(obj)) return TRUE;
363  switch (BUILTIN_TYPE(obj)) {
364  case T_BIGNUM:
365  case T_FLOAT:
366  case T_SYMBOL:
367  case T_RATIONAL:
368  case T_COMPLEX:
369  /* not a comprehensive list */
370  return TRUE;
371  default:
372  return FALSE;
373  }
374 }
375 
376 static VALUE
377 obj_freeze_opt(VALUE freeze)
378 {
379  switch (freeze) {
380  case Qfalse:
381  case Qtrue:
382  case Qnil:
383  break;
384  default:
385  rb_raise(rb_eArgError, "unexpected value for freeze: %"PRIsVALUE, rb_obj_class(freeze));
386  }
387 
388  return freeze;
389 }
390 
391 static VALUE
392 rb_obj_clone2(rb_execution_context_t *ec, VALUE obj, VALUE freeze)
393 {
394  VALUE kwfreeze = obj_freeze_opt(freeze);
395  if (!special_object_p(obj))
396  return mutable_obj_clone(obj, kwfreeze);
397  return immutable_obj_clone(obj, kwfreeze);
398 }
399 
401 VALUE
402 rb_immutable_obj_clone(int argc, VALUE *argv, VALUE obj)
403 {
404  VALUE kwfreeze = rb_get_freeze_opt(argc, argv);
405  return immutable_obj_clone(obj, kwfreeze);
406 }
407 
408 VALUE
409 rb_get_freeze_opt(int argc, VALUE *argv)
410 {
411  static ID keyword_ids[1];
412  VALUE opt;
413  VALUE kwfreeze = Qnil;
414 
415  if (!keyword_ids[0]) {
416  CONST_ID(keyword_ids[0], "freeze");
417  }
418  rb_scan_args(argc, argv, "0:", &opt);
419  if (!NIL_P(opt)) {
420  rb_get_kwargs(opt, keyword_ids, 0, 1, &kwfreeze);
421  if (!UNDEF_P(kwfreeze))
422  kwfreeze = obj_freeze_opt(kwfreeze);
423  }
424  return kwfreeze;
425 }
426 
427 static VALUE
428 immutable_obj_clone(VALUE obj, VALUE kwfreeze)
429 {
430  if (kwfreeze == Qfalse)
431  rb_raise(rb_eArgError, "can't unfreeze %"PRIsVALUE,
432  rb_obj_class(obj));
433  return obj;
434 }
435 
436 VALUE
437 rb_obj_clone_setup(VALUE obj, VALUE clone, VALUE kwfreeze)
438 {
439  VALUE argv[2];
440 
441  VALUE singleton = rb_singleton_class_clone_and_attach(obj, clone);
442  RBASIC_SET_CLASS(clone, singleton);
443  if (RCLASS_SINGLETON_P(singleton)) {
444  rb_singleton_class_attached(singleton, clone);
445  }
446 
447  init_copy(clone, obj);
448 
449  switch (kwfreeze) {
450  case Qnil:
451  rb_funcall(clone, id_init_clone, 1, obj);
452  RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
453  if (CHILLED_STRING_P(obj)) {
454  STR_CHILL_RAW(clone);
455  }
456  else if (RB_OBJ_FROZEN(obj)) {
457  rb_shape_t * next_shape = rb_shape_transition_shape_frozen(clone);
458  if (!rb_shape_obj_too_complex(clone) && next_shape->type == SHAPE_OBJ_TOO_COMPLEX) {
459  rb_evict_ivars_to_hash(clone);
460  }
461  else {
462  rb_shape_set_shape(clone, next_shape);
463  }
464  }
465  break;
466  case Qtrue: {
467  static VALUE freeze_true_hash;
468  if (!freeze_true_hash) {
469  freeze_true_hash = rb_hash_new();
470  rb_vm_register_global_object(freeze_true_hash);
471  rb_hash_aset(freeze_true_hash, ID2SYM(idFreeze), Qtrue);
472  rb_obj_freeze(freeze_true_hash);
473  }
474 
475  argv[0] = obj;
476  argv[1] = freeze_true_hash;
477  rb_funcallv_kw(clone, id_init_clone, 2, argv, RB_PASS_KEYWORDS);
478  RBASIC(clone)->flags |= FL_FREEZE;
479  rb_shape_t * next_shape = rb_shape_transition_shape_frozen(clone);
480  // If we're out of shapes, but we want to freeze, then we need to
481  // evacuate this clone to a hash
482  if (!rb_shape_obj_too_complex(clone) && next_shape->type == SHAPE_OBJ_TOO_COMPLEX) {
483  rb_evict_ivars_to_hash(clone);
484  }
485  else {
486  rb_shape_set_shape(clone, next_shape);
487  }
488  break;
489  }
490  case Qfalse: {
491  static VALUE freeze_false_hash;
492  if (!freeze_false_hash) {
493  freeze_false_hash = rb_hash_new();
494  rb_vm_register_global_object(freeze_false_hash);
495  rb_hash_aset(freeze_false_hash, ID2SYM(idFreeze), Qfalse);
496  rb_obj_freeze(freeze_false_hash);
497  }
498 
499  argv[0] = obj;
500  argv[1] = freeze_false_hash;
501  rb_funcallv_kw(clone, id_init_clone, 2, argv, RB_PASS_KEYWORDS);
502  break;
503  }
504  default:
505  rb_bug("invalid kwfreeze passed to mutable_obj_clone");
506  }
507 
508  return clone;
509 }
510 
511 static VALUE
512 mutable_obj_clone(VALUE obj, VALUE kwfreeze)
513 {
514  VALUE clone = rb_obj_alloc(rb_obj_class(obj));
515  return rb_obj_clone_setup(obj, clone, kwfreeze);
516 }
517 
518 VALUE
520 {
521  if (special_object_p(obj)) return obj;
522  return mutable_obj_clone(obj, Qnil);
523 }
524 
525 VALUE
526 rb_obj_dup_setup(VALUE obj, VALUE dup)
527 {
528  init_copy(dup, obj);
529  rb_funcall(dup, id_init_dup, 1, obj);
530 
531  return dup;
532 }
533 
534 /*
535  * call-seq:
536  * obj.dup -> an_object
537  *
538  * Produces a shallow copy of <i>obj</i>---the instance variables of
539  * <i>obj</i> are copied, but not the objects they reference.
540  *
541  * This method may have class-specific behavior. If so, that
542  * behavior will be documented under the #+initialize_copy+ method of
543  * the class.
544  *
545  * === on dup vs clone
546  *
547  * In general, #clone and #dup may have different semantics in
548  * descendant classes. While #clone is used to duplicate an object,
549  * including its internal state, #dup typically uses the class of the
550  * descendant object to create the new instance.
551  *
552  * When using #dup, any modules that the object has been extended with will not
553  * be copied.
554  *
555  * class Klass
556  * attr_accessor :str
557  * end
558  *
559  * module Foo
560  * def foo; 'foo'; end
561  * end
562  *
563  * s1 = Klass.new #=> #<Klass:0x401b3a38>
564  * s1.extend(Foo) #=> #<Klass:0x401b3a38>
565  * s1.foo #=> "foo"
566  *
567  * s2 = s1.clone #=> #<Klass:0x401be280>
568  * s2.foo #=> "foo"
569  *
570  * s3 = s1.dup #=> #<Klass:0x401c1084>
571  * s3.foo #=> NoMethodError: undefined method `foo' for #<Klass:0x401c1084>
572  */
573 VALUE
575 {
576  VALUE dup;
577 
578  if (special_object_p(obj)) {
579  return obj;
580  }
581  dup = rb_obj_alloc(rb_obj_class(obj));
582  return rb_obj_dup_setup(obj, dup);
583 }
584 
585 /*
586  * call-seq:
587  * obj.itself -> obj
588  *
589  * Returns the receiver.
590  *
591  * string = "my string"
592  * string.itself.object_id == string.object_id #=> true
593  *
594  */
595 
596 static VALUE
597 rb_obj_itself(VALUE obj)
598 {
599  return obj;
600 }
601 
602 VALUE
603 rb_obj_size(VALUE self, VALUE args, VALUE obj)
604 {
605  return LONG2FIX(1);
606 }
607 
613 VALUE
615 {
616  if (obj == orig) return obj;
617  rb_check_frozen(obj);
618  if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
619  rb_raise(rb_eTypeError, "initialize_copy should take same class object");
620  }
621  return obj;
622 }
623 
630 VALUE
632 {
633  rb_funcall(obj, id_init_copy, 1, orig);
634  return obj;
635 }
636 
644 static VALUE
645 rb_obj_init_clone(int argc, VALUE *argv, VALUE obj)
646 {
647  VALUE orig, opts;
648  if (rb_scan_args(argc, argv, "1:", &orig, &opts) < argc) {
649  /* Ignore a freeze keyword */
650  rb_get_freeze_opt(1, &opts);
651  }
652  rb_funcall(obj, id_init_copy, 1, orig);
653  return obj;
654 }
655 
656 /*
657  * call-seq:
658  * obj.to_s -> string
659  *
660  * Returns a string representing <i>obj</i>. The default #to_s prints
661  * the object's class and an encoding of the object id. As a special
662  * case, the top-level object that is the initial execution context
663  * of Ruby programs returns ``main''.
664  *
665  */
666 VALUE
668 {
669  VALUE str;
670  VALUE cname = rb_class_name(CLASS_OF(obj));
671 
672  str = rb_sprintf("#<%"PRIsVALUE":%p>", cname, (void*)obj);
673 
674  return str;
675 }
676 
677 VALUE
679 {
680  VALUE str = rb_obj_as_string(rb_funcallv(obj, id_inspect, 0, 0));
681 
683  if (enc == NULL) enc = rb_default_external_encoding();
684  if (!rb_enc_asciicompat(enc)) {
685  if (!rb_enc_str_asciionly_p(str))
686  return rb_str_escape(str);
687  return str;
688  }
689  if (rb_enc_get(str) != enc && !rb_enc_str_asciionly_p(str))
690  return rb_str_escape(str);
691  return str;
692 }
693 
694 static int
695 inspect_i(ID id, VALUE value, st_data_t a)
696 {
697  VALUE str = (VALUE)a;
698 
699  /* need not to show internal data */
700  if (CLASS_OF(value) == 0) return ST_CONTINUE;
701  if (!rb_is_instance_id(id)) return ST_CONTINUE;
702  if (RSTRING_PTR(str)[0] == '-') { /* first element */
703  RSTRING_PTR(str)[0] = '#';
704  rb_str_cat2(str, " ");
705  }
706  else {
707  rb_str_cat2(str, ", ");
708  }
709  rb_str_catf(str, "%"PRIsVALUE"=", rb_id2str(id));
710  rb_str_buf_append(str, rb_inspect(value));
711 
712  return ST_CONTINUE;
713 }
714 
715 static VALUE
716 inspect_obj(VALUE obj, VALUE str, int recur)
717 {
718  if (recur) {
719  rb_str_cat2(str, " ...");
720  }
721  else {
722  rb_ivar_foreach(obj, inspect_i, str);
723  }
724  rb_str_cat2(str, ">");
725  RSTRING_PTR(str)[0] = '#';
726 
727  return str;
728 }
729 
730 /*
731  * call-seq:
732  * obj.inspect -> string
733  *
734  * Returns a string containing a human-readable representation of <i>obj</i>.
735  * The default #inspect shows the object's class name, an encoding of
736  * its memory address, and a list of the instance variables and their
737  * values (by calling #inspect on each of them). User defined classes
738  * should override this method to provide a better representation of
739  * <i>obj</i>. When overriding this method, it should return a string
740  * whose encoding is compatible with the default external encoding.
741  *
742  * [ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
743  * Time.new.inspect #=> "2008-03-08 19:43:39 +0900"
744  *
745  * class Foo
746  * end
747  * Foo.new.inspect #=> "#<Foo:0x0300c868>"
748  *
749  * class Bar
750  * def initialize
751  * @bar = 1
752  * end
753  * end
754  * Bar.new.inspect #=> "#<Bar:0x0300c868 @bar=1>"
755  */
756 
757 static VALUE
758 rb_obj_inspect(VALUE obj)
759 {
760  if (rb_ivar_count(obj) > 0) {
761  VALUE str;
762  VALUE c = rb_class_name(CLASS_OF(obj));
763 
764  str = rb_sprintf("-<%"PRIsVALUE":%p", c, (void*)obj);
765  return rb_exec_recursive(inspect_obj, obj, str);
766  }
767  else {
768  return rb_any_to_s(obj);
769  }
770 }
771 
772 static VALUE
773 class_or_module_required(VALUE c)
774 {
775  switch (OBJ_BUILTIN_TYPE(c)) {
776  case T_MODULE:
777  case T_CLASS:
778  case T_ICLASS:
779  break;
780 
781  default:
782  rb_raise(rb_eTypeError, "class or module required");
783  }
784  return c;
785 }
786 
787 static VALUE class_search_ancestor(VALUE cl, VALUE c);
788 
789 /*
790  * call-seq:
791  * obj.instance_of?(class) -> true or false
792  *
793  * Returns <code>true</code> if <i>obj</i> is an instance of the given
794  * class. See also Object#kind_of?.
795  *
796  * class A; end
797  * class B < A; end
798  * class C < B; end
799  *
800  * b = B.new
801  * b.instance_of? A #=> false
802  * b.instance_of? B #=> true
803  * b.instance_of? C #=> false
804  */
805 
806 VALUE
808 {
809  c = class_or_module_required(c);
810  return RBOOL(rb_obj_class(obj) == c);
811 }
812 
813 // Returns whether c is a proper (c != cl) superclass of cl
814 // Both c and cl must be T_CLASS
815 static VALUE
816 class_search_class_ancestor(VALUE cl, VALUE c)
817 {
820 
821  size_t c_depth = RCLASS_SUPERCLASS_DEPTH(c);
822  size_t cl_depth = RCLASS_SUPERCLASS_DEPTH(cl);
823  VALUE *classes = RCLASS_SUPERCLASSES(cl);
824 
825  // If c's inheritance chain is longer, it cannot be an ancestor
826  // We are checking for a proper superclass so don't check if they are equal
827  if (cl_depth <= c_depth)
828  return Qfalse;
829 
830  // Otherwise check that c is in cl's inheritance chain
831  return RBOOL(classes[c_depth] == c);
832 }
833 
834 /*
835  * call-seq:
836  * obj.is_a?(class) -> true or false
837  * obj.kind_of?(class) -> true or false
838  *
839  * Returns <code>true</code> if <i>class</i> is the class of
840  * <i>obj</i>, or if <i>class</i> is one of the superclasses of
841  * <i>obj</i> or modules included in <i>obj</i>.
842  *
843  * module M; end
844  * class A
845  * include M
846  * end
847  * class B < A; end
848  * class C < B; end
849  *
850  * b = B.new
851  * b.is_a? A #=> true
852  * b.is_a? B #=> true
853  * b.is_a? C #=> false
854  * b.is_a? M #=> true
855  *
856  * b.kind_of? A #=> true
857  * b.kind_of? B #=> true
858  * b.kind_of? C #=> false
859  * b.kind_of? M #=> true
860  */
861 
862 VALUE
864 {
865  VALUE cl = CLASS_OF(obj);
866 
868 
869  // Fastest path: If the object's class is an exact match we know `c` is a
870  // class without checking type and can return immediately.
871  if (cl == c) return Qtrue;
872 
873  // Note: YJIT needs this function to never allocate and never raise when
874  // `c` is a class or a module.
875 
876  if (LIKELY(RB_TYPE_P(c, T_CLASS))) {
877  // Fast path: Both are T_CLASS
878  return class_search_class_ancestor(cl, c);
879  }
880  else if (RB_TYPE_P(c, T_ICLASS)) {
881  // First check if we inherit the includer
882  // If we do we can return true immediately
883  VALUE includer = RCLASS_INCLUDER(c);
884  if (cl == includer) return Qtrue;
885 
886  // Usually includer is a T_CLASS here, except when including into an
887  // already included Module.
888  // If it is a class, attempt the fast class-to-class check and return
889  // true if there is a match.
890  if (RB_TYPE_P(includer, T_CLASS) && class_search_class_ancestor(cl, includer))
891  return Qtrue;
892 
893  // We don't include the ICLASS directly, so must check if we inherit
894  // the module via another include
895  return RBOOL(class_search_ancestor(cl, RCLASS_ORIGIN(c)));
896  }
897  else if (RB_TYPE_P(c, T_MODULE)) {
898  // Slow path: check each ancestor in the linked list and its method table
899  return RBOOL(class_search_ancestor(cl, RCLASS_ORIGIN(c)));
900  }
901  else {
902  rb_raise(rb_eTypeError, "class or module required");
904  }
905 }
906 
907 
908 static VALUE
909 class_search_ancestor(VALUE cl, VALUE c)
910 {
911  while (cl) {
912  if (cl == c || RCLASS_M_TBL(cl) == RCLASS_M_TBL(c))
913  return cl;
914  cl = RCLASS_SUPER(cl);
915  }
916  return 0;
917 }
918 
920 VALUE
921 rb_class_search_ancestor(VALUE cl, VALUE c)
922 {
923  cl = class_or_module_required(cl);
924  c = class_or_module_required(c);
925  return class_search_ancestor(cl, RCLASS_ORIGIN(c));
926 }
927 
928 
929 /*
930  * Document-method: inherited
931  *
932  * call-seq:
933  * inherited(subclass)
934  *
935  * Callback invoked whenever a subclass of the current class is created.
936  *
937  * Example:
938  *
939  * class Foo
940  * def self.inherited(subclass)
941  * puts "New subclass: #{subclass}"
942  * end
943  * end
944  *
945  * class Bar < Foo
946  * end
947  *
948  * class Baz < Bar
949  * end
950  *
951  * <em>produces:</em>
952  *
953  * New subclass: Bar
954  * New subclass: Baz
955  */
956 #define rb_obj_class_inherited rb_obj_dummy1
957 
958 /* Document-method: method_added
959  *
960  * call-seq:
961  * method_added(method_name)
962  *
963  * Invoked as a callback whenever an instance method is added to the
964  * receiver.
965  *
966  * module Chatty
967  * def self.method_added(method_name)
968  * puts "Adding #{method_name.inspect}"
969  * end
970  * def self.some_class_method() end
971  * def some_instance_method() end
972  * end
973  *
974  * <em>produces:</em>
975  *
976  * Adding :some_instance_method
977  *
978  */
979 #define rb_obj_mod_method_added rb_obj_dummy1
980 
981 /* Document-method: method_removed
982  *
983  * call-seq:
984  * method_removed(method_name)
985  *
986  * Invoked as a callback whenever an instance method is removed from the
987  * receiver.
988  *
989  * module Chatty
990  * def self.method_removed(method_name)
991  * puts "Removing #{method_name.inspect}"
992  * end
993  * def self.some_class_method() end
994  * def some_instance_method() end
995  * class << self
996  * remove_method :some_class_method
997  * end
998  * remove_method :some_instance_method
999  * end
1000  *
1001  * <em>produces:</em>
1002  *
1003  * Removing :some_instance_method
1004  *
1005  */
1006 #define rb_obj_mod_method_removed rb_obj_dummy1
1007 
1008 /* Document-method: method_undefined
1009  *
1010  * call-seq:
1011  * method_undefined(method_name)
1012  *
1013  * Invoked as a callback whenever an instance method is undefined from the
1014  * receiver.
1015  *
1016  * module Chatty
1017  * def self.method_undefined(method_name)
1018  * puts "Undefining #{method_name.inspect}"
1019  * end
1020  * def self.some_class_method() end
1021  * def some_instance_method() end
1022  * class << self
1023  * undef_method :some_class_method
1024  * end
1025  * undef_method :some_instance_method
1026  * end
1027  *
1028  * <em>produces:</em>
1029  *
1030  * Undefining :some_instance_method
1031  *
1032  */
1033 #define rb_obj_mod_method_undefined rb_obj_dummy1
1034 
1035 /*
1036  * Document-method: singleton_method_added
1037  *
1038  * call-seq:
1039  * singleton_method_added(symbol)
1040  *
1041  * Invoked as a callback whenever a singleton method is added to the
1042  * receiver.
1043  *
1044  * module Chatty
1045  * def Chatty.singleton_method_added(id)
1046  * puts "Adding #{id.id2name}"
1047  * end
1048  * def self.one() end
1049  * def two() end
1050  * def Chatty.three() end
1051  * end
1052  *
1053  * <em>produces:</em>
1054  *
1055  * Adding singleton_method_added
1056  * Adding one
1057  * Adding three
1058  *
1059  */
1060 #define rb_obj_singleton_method_added rb_obj_dummy1
1061 
1062 /*
1063  * Document-method: singleton_method_removed
1064  *
1065  * call-seq:
1066  * singleton_method_removed(symbol)
1067  *
1068  * Invoked as a callback whenever a singleton method is removed from
1069  * the receiver.
1070  *
1071  * module Chatty
1072  * def Chatty.singleton_method_removed(id)
1073  * puts "Removing #{id.id2name}"
1074  * end
1075  * def self.one() end
1076  * def two() end
1077  * def Chatty.three() end
1078  * class << self
1079  * remove_method :three
1080  * remove_method :one
1081  * end
1082  * end
1083  *
1084  * <em>produces:</em>
1085  *
1086  * Removing three
1087  * Removing one
1088  */
1089 #define rb_obj_singleton_method_removed rb_obj_dummy1
1090 
1091 /*
1092  * Document-method: singleton_method_undefined
1093  *
1094  * call-seq:
1095  * singleton_method_undefined(symbol)
1096  *
1097  * Invoked as a callback whenever a singleton method is undefined in
1098  * the receiver.
1099  *
1100  * module Chatty
1101  * def Chatty.singleton_method_undefined(id)
1102  * puts "Undefining #{id.id2name}"
1103  * end
1104  * def Chatty.one() end
1105  * class << self
1106  * undef_method(:one)
1107  * end
1108  * end
1109  *
1110  * <em>produces:</em>
1111  *
1112  * Undefining one
1113  */
1114 #define rb_obj_singleton_method_undefined rb_obj_dummy1
1115 
1116 /* Document-method: const_added
1117  *
1118  * call-seq:
1119  * const_added(const_name)
1120  *
1121  * Invoked as a callback whenever a constant is assigned on the receiver
1122  *
1123  * module Chatty
1124  * def self.const_added(const_name)
1125  * super
1126  * puts "Added #{const_name.inspect}"
1127  * end
1128  * FOO = 1
1129  * end
1130  *
1131  * <em>produces:</em>
1132  *
1133  * Added :FOO
1134  *
1135  */
1136 #define rb_obj_mod_const_added rb_obj_dummy1
1137 
1138 /*
1139  * Document-method: extended
1140  *
1141  * call-seq:
1142  * extended(othermod)
1143  *
1144  * The equivalent of <tt>included</tt>, but for extended modules.
1145  *
1146  * module A
1147  * def self.extended(mod)
1148  * puts "#{self} extended in #{mod}"
1149  * end
1150  * end
1151  * module Enumerable
1152  * extend A
1153  * end
1154  * # => prints "A extended in Enumerable"
1155  */
1156 #define rb_obj_mod_extended rb_obj_dummy1
1157 
1158 /*
1159  * Document-method: included
1160  *
1161  * call-seq:
1162  * included(othermod)
1163  *
1164  * Callback invoked whenever the receiver is included in another
1165  * module or class. This should be used in preference to
1166  * <tt>Module.append_features</tt> if your code wants to perform some
1167  * action when a module is included in another.
1168  *
1169  * module A
1170  * def A.included(mod)
1171  * puts "#{self} included in #{mod}"
1172  * end
1173  * end
1174  * module Enumerable
1175  * include A
1176  * end
1177  * # => prints "A included in Enumerable"
1178  */
1179 #define rb_obj_mod_included rb_obj_dummy1
1180 
1181 /*
1182  * Document-method: prepended
1183  *
1184  * call-seq:
1185  * prepended(othermod)
1186  *
1187  * The equivalent of <tt>included</tt>, but for prepended modules.
1188  *
1189  * module A
1190  * def self.prepended(mod)
1191  * puts "#{self} prepended to #{mod}"
1192  * end
1193  * end
1194  * module Enumerable
1195  * prepend A
1196  * end
1197  * # => prints "A prepended to Enumerable"
1198  */
1199 #define rb_obj_mod_prepended rb_obj_dummy1
1200 
1201 /*
1202  * Document-method: initialize
1203  *
1204  * call-seq:
1205  * BasicObject.new
1206  *
1207  * Returns a new BasicObject.
1208  */
1209 #define rb_obj_initialize rb_obj_dummy0
1210 
1211 /*
1212  * Not documented
1213  */
1214 
1215 static VALUE
1216 rb_obj_dummy(void)
1217 {
1218  return Qnil;
1219 }
1220 
1221 static VALUE
1222 rb_obj_dummy0(VALUE _)
1223 {
1224  return rb_obj_dummy();
1225 }
1226 
1227 static VALUE
1228 rb_obj_dummy1(VALUE _x, VALUE _y)
1229 {
1230  return rb_obj_dummy();
1231 }
1232 
1233 /*
1234  * call-seq:
1235  * obj.freeze -> obj
1236  *
1237  * Prevents further modifications to <i>obj</i>. A
1238  * FrozenError will be raised if modification is attempted.
1239  * There is no way to unfreeze a frozen object. See also
1240  * Object#frozen?.
1241  *
1242  * This method returns self.
1243  *
1244  * a = [ "a", "b", "c" ]
1245  * a.freeze
1246  * a << "z"
1247  *
1248  * <em>produces:</em>
1249  *
1250  * prog.rb:3:in `<<': can't modify frozen Array (FrozenError)
1251  * from prog.rb:3
1252  *
1253  * Objects of the following classes are always frozen: Integer,
1254  * Float, Symbol.
1255  */
1256 
1257 VALUE
1259 {
1260  if (!OBJ_FROZEN(obj)) {
1261  OBJ_FREEZE(obj);
1262  if (SPECIAL_CONST_P(obj)) {
1263  rb_bug("special consts should be frozen.");
1264  }
1265  }
1266  return obj;
1267 }
1268 
1269 VALUE
1271 {
1272  return RBOOL(OBJ_FROZEN(obj));
1273 }
1274 
1275 
1276 /*
1277  * Document-class: NilClass
1278  *
1279  * The class of the singleton object +nil+.
1280  *
1281  * Several of its methods act as operators:
1282  *
1283  * - #&
1284  * - #|
1285  * - #===
1286  * - #=~
1287  * - #^
1288  *
1289  * Others act as converters, carrying the concept of _nullity_
1290  * to other classes:
1291  *
1292  * - #rationalize
1293  * - #to_a
1294  * - #to_c
1295  * - #to_h
1296  * - #to_r
1297  * - #to_s
1298  *
1299  * Another method provides inspection:
1300  *
1301  * - #inspect
1302  *
1303  * Finally, there is this query method:
1304  *
1305  * - #nil?
1306  *
1307  */
1308 
1309 /*
1310  * call-seq:
1311  * to_s -> ''
1312  *
1313  * Returns an empty String:
1314  *
1315  * nil.to_s # => ""
1316  *
1317  */
1318 
1319 VALUE
1320 rb_nil_to_s(VALUE obj)
1321 {
1322  return rb_cNilClass_to_s;
1323 }
1324 
1325 /*
1326  * Document-method: to_a
1327  *
1328  * call-seq:
1329  * to_a -> []
1330  *
1331  * Returns an empty Array.
1332  *
1333  * nil.to_a # => []
1334  *
1335  */
1336 
1337 static VALUE
1338 nil_to_a(VALUE obj)
1339 {
1340  return rb_ary_new2(0);
1341 }
1342 
1343 /*
1344  * Document-method: to_h
1345  *
1346  * call-seq:
1347  * to_h -> {}
1348  *
1349  * Returns an empty Hash.
1350  *
1351  * nil.to_h #=> {}
1352  *
1353  */
1354 
1355 static VALUE
1356 nil_to_h(VALUE obj)
1357 {
1358  return rb_hash_new();
1359 }
1360 
1361 /*
1362  * call-seq:
1363  * inspect -> 'nil'
1364  *
1365  * Returns string <tt>'nil'</tt>:
1366  *
1367  * nil.inspect # => "nil"
1368  *
1369  */
1370 
1371 static VALUE
1372 nil_inspect(VALUE obj)
1373 {
1374  return rb_usascii_str_new2("nil");
1375 }
1376 
1377 /*
1378  * call-seq:
1379  * nil =~ object -> nil
1380  *
1381  * Returns +nil+.
1382  *
1383  * This method makes it useful to write:
1384  *
1385  * while gets =~ /re/
1386  * # ...
1387  * end
1388  *
1389  */
1390 
1391 static VALUE
1392 nil_match(VALUE obj1, VALUE obj2)
1393 {
1394  return Qnil;
1395 }
1396 
1397 /*
1398  * Document-class: TrueClass
1399  *
1400  * The class of the singleton object +true+.
1401  *
1402  * Several of its methods act as operators:
1403  *
1404  * - #&
1405  * - #|
1406  * - #===
1407  * - #^
1408  *
1409  * One other method:
1410  *
1411  * - #to_s and its alias #inspect.
1412  *
1413  */
1414 
1415 
1416 /*
1417  * call-seq:
1418  * true.to_s -> 'true'
1419  *
1420  * Returns string <tt>'true'</tt>:
1421  *
1422  * true.to_s # => "true"
1423  *
1424  * TrueClass#inspect is an alias for TrueClass#to_s.
1425  *
1426  */
1427 
1428 VALUE
1429 rb_true_to_s(VALUE obj)
1430 {
1431  return rb_cTrueClass_to_s;
1432 }
1433 
1434 
1435 /*
1436  * call-seq:
1437  * true & object -> true or false
1438  *
1439  * Returns +false+ if +object+ is +false+ or +nil+, +true+ otherwise:
1440  *
1441  * true & Object.new # => true
1442  * true & false # => false
1443  * true & nil # => false
1444  *
1445  */
1446 
1447 static VALUE
1448 true_and(VALUE obj, VALUE obj2)
1449 {
1450  return RBOOL(RTEST(obj2));
1451 }
1452 
1453 /*
1454  * call-seq:
1455  * true | object -> true
1456  *
1457  * Returns +true+:
1458  *
1459  * true | Object.new # => true
1460  * true | false # => true
1461  * true | nil # => true
1462  *
1463  * Argument +object+ is evaluated.
1464  * This is different from +true+ with the short-circuit operator,
1465  * whose operand is evaluated only if necessary:
1466  *
1467  * true | raise # => Raises RuntimeError.
1468  * true || raise # => true
1469  *
1470  */
1471 
1472 static VALUE
1473 true_or(VALUE obj, VALUE obj2)
1474 {
1475  return Qtrue;
1476 }
1477 
1478 
1479 /*
1480  * call-seq:
1481  * true ^ object -> !object
1482  *
1483  * Returns +true+ if +object+ is +false+ or +nil+, +false+ otherwise:
1484  *
1485  * true ^ Object.new # => false
1486  * true ^ false # => true
1487  * true ^ nil # => true
1488  *
1489  */
1490 
1491 static VALUE
1492 true_xor(VALUE obj, VALUE obj2)
1493 {
1494  return rb_obj_not(obj2);
1495 }
1496 
1497 
1498 /*
1499  * Document-class: FalseClass
1500  *
1501  * The global value <code>false</code> is the only instance of class
1502  * FalseClass and represents a logically false value in
1503  * boolean expressions. The class provides operators allowing
1504  * <code>false</code> to participate correctly in logical expressions.
1505  *
1506  */
1507 
1508 /*
1509  * call-seq:
1510  * false.to_s -> "false"
1511  *
1512  * The string representation of <code>false</code> is "false".
1513  */
1514 
1515 VALUE
1516 rb_false_to_s(VALUE obj)
1517 {
1518  return rb_cFalseClass_to_s;
1519 }
1520 
1521 /*
1522  * call-seq:
1523  * false & object -> false
1524  * nil & object -> false
1525  *
1526  * Returns +false+:
1527  *
1528  * false & true # => false
1529  * false & Object.new # => false
1530  *
1531  * Argument +object+ is evaluated:
1532  *
1533  * false & raise # Raises RuntimeError.
1534  *
1535  */
1536 static VALUE
1537 false_and(VALUE obj, VALUE obj2)
1538 {
1539  return Qfalse;
1540 }
1541 
1542 
1543 /*
1544  * call-seq:
1545  * false | object -> true or false
1546  * nil | object -> true or false
1547  *
1548  * Returns +false+ if +object+ is +nil+ or +false+, +true+ otherwise:
1549  *
1550  * nil | nil # => false
1551  * nil | false # => false
1552  * nil | Object.new # => true
1553  *
1554  */
1555 
1556 #define false_or true_and
1557 
1558 /*
1559  * call-seq:
1560  * false ^ object -> true or false
1561  * nil ^ object -> true or false
1562  *
1563  * Returns +false+ if +object+ is +nil+ or +false+, +true+ otherwise:
1564  *
1565  * nil ^ nil # => false
1566  * nil ^ false # => false
1567  * nil ^ Object.new # => true
1568  *
1569  */
1570 
1571 #define false_xor true_and
1572 
1573 /*
1574  * call-seq:
1575  * nil.nil? -> true
1576  *
1577  * Returns +true+.
1578  * For all other objects, method <tt>nil?</tt> returns +false+.
1579  */
1580 
1581 static VALUE
1582 rb_true(VALUE obj)
1583 {
1584  return Qtrue;
1585 }
1586 
1587 /*
1588  * call-seq:
1589  * obj.nil? -> true or false
1590  *
1591  * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
1592  *
1593  * Object.new.nil? #=> false
1594  * nil.nil? #=> true
1595  */
1596 
1597 
1598 VALUE
1599 rb_false(VALUE obj)
1600 {
1601  return Qfalse;
1602 }
1603 
1604 /*
1605  * call-seq:
1606  * obj !~ other -> true or false
1607  *
1608  * Returns true if two objects do not match (using the <i>=~</i>
1609  * method), otherwise false.
1610  */
1611 
1612 static VALUE
1613 rb_obj_not_match(VALUE obj1, VALUE obj2)
1614 {
1615  VALUE result = rb_funcall(obj1, id_match, 1, obj2);
1616  return rb_obj_not(result);
1617 }
1618 
1619 
1620 /*
1621  * call-seq:
1622  * obj <=> other -> 0 or nil
1623  *
1624  * Returns 0 if +obj+ and +other+ are the same object
1625  * or <code>obj == other</code>, otherwise nil.
1626  *
1627  * The #<=> is used by various methods to compare objects, for example
1628  * Enumerable#sort, Enumerable#max etc.
1629  *
1630  * Your implementation of #<=> should return one of the following values: -1, 0,
1631  * 1 or nil. -1 means self is smaller than other. 0 means self is equal to other.
1632  * 1 means self is bigger than other. Nil means the two values could not be
1633  * compared.
1634  *
1635  * When you define #<=>, you can include Comparable to gain the
1636  * methods #<=, #<, #==, #>=, #> and #between?.
1637  */
1638 static VALUE
1639 rb_obj_cmp(VALUE obj1, VALUE obj2)
1640 {
1641  if (rb_equal(obj1, obj2))
1642  return INT2FIX(0);
1643  return Qnil;
1644 }
1645 
1646 /***********************************************************************
1647  *
1648  * Document-class: Module
1649  *
1650  * A Module is a collection of methods and constants. The
1651  * methods in a module may be instance methods or module methods.
1652  * Instance methods appear as methods in a class when the module is
1653  * included, module methods do not. Conversely, module methods may be
1654  * called without creating an encapsulating object, while instance
1655  * methods may not. (See Module#module_function.)
1656  *
1657  * In the descriptions that follow, the parameter <i>sym</i> refers
1658  * to a symbol, which is either a quoted string or a
1659  * Symbol (such as <code>:name</code>).
1660  *
1661  * module Mod
1662  * include Math
1663  * CONST = 1
1664  * def meth
1665  * # ...
1666  * end
1667  * end
1668  * Mod.class #=> Module
1669  * Mod.constants #=> [:CONST, :PI, :E]
1670  * Mod.instance_methods #=> [:meth]
1671  *
1672  */
1673 
1674 /*
1675  * call-seq:
1676  * mod.to_s -> string
1677  *
1678  * Returns a string representing this module or class. For basic
1679  * classes and modules, this is the name. For singletons, we
1680  * show information on the thing we're attached to as well.
1681  */
1682 
1683 VALUE
1684 rb_mod_to_s(VALUE klass)
1685 {
1686  ID id_defined_at;
1687  VALUE refined_class, defined_at;
1688 
1689  if (RCLASS_SINGLETON_P(klass)) {
1690  VALUE s = rb_usascii_str_new2("#<Class:");
1691  VALUE v = RCLASS_ATTACHED_OBJECT(klass);
1692 
1693  if (CLASS_OR_MODULE_P(v)) {
1694  rb_str_append(s, rb_inspect(v));
1695  }
1696  else {
1697  rb_str_append(s, rb_any_to_s(v));
1698  }
1699  rb_str_cat2(s, ">");
1700 
1701  return s;
1702  }
1703  refined_class = rb_refinement_module_get_refined_class(klass);
1704  if (!NIL_P(refined_class)) {
1705  VALUE s = rb_usascii_str_new2("#<refinement:");
1706 
1707  rb_str_concat(s, rb_inspect(refined_class));
1708  rb_str_cat2(s, "@");
1709  CONST_ID(id_defined_at, "__defined_at__");
1710  defined_at = rb_attr_get(klass, id_defined_at);
1711  rb_str_concat(s, rb_inspect(defined_at));
1712  rb_str_cat2(s, ">");
1713  return s;
1714  }
1715  return rb_class_name(klass);
1716 }
1717 
1718 /*
1719  * call-seq:
1720  * mod.freeze -> mod
1721  *
1722  * Prevents further modifications to <i>mod</i>.
1723  *
1724  * This method returns self.
1725  */
1726 
1727 static VALUE
1728 rb_mod_freeze(VALUE mod)
1729 {
1730  rb_class_name(mod);
1731  return rb_obj_freeze(mod);
1732 }
1733 
1734 /*
1735  * call-seq:
1736  * mod === obj -> true or false
1737  *
1738  * Case Equality---Returns <code>true</code> if <i>obj</i> is an
1739  * instance of <i>mod</i> or an instance of one of <i>mod</i>'s descendants.
1740  * Of limited use for modules, but can be used in <code>case</code> statements
1741  * to classify objects by class.
1742  */
1743 
1744 static VALUE
1745 rb_mod_eqq(VALUE mod, VALUE arg)
1746 {
1747  return rb_obj_is_kind_of(arg, mod);
1748 }
1749 
1750 /*
1751  * call-seq:
1752  * mod <= other -> true, false, or nil
1753  *
1754  * Returns true if <i>mod</i> is a subclass of <i>other</i> or
1755  * is the same as <i>other</i>. Returns
1756  * <code>nil</code> if there's no relationship between the two.
1757  * (Think of the relationship in terms of the class definition:
1758  * "class A < B" implies "A < B".)
1759  */
1760 
1761 VALUE
1763 {
1764  if (mod == arg) return Qtrue;
1765 
1766  if (RB_TYPE_P(arg, T_CLASS) && RB_TYPE_P(mod, T_CLASS)) {
1767  // comparison between classes
1768  size_t mod_depth = RCLASS_SUPERCLASS_DEPTH(mod);
1769  size_t arg_depth = RCLASS_SUPERCLASS_DEPTH(arg);
1770  if (arg_depth < mod_depth) {
1771  // check if mod < arg
1772  return RCLASS_SUPERCLASSES(mod)[arg_depth] == arg ?
1773  Qtrue :
1774  Qnil;
1775  }
1776  else if (arg_depth > mod_depth) {
1777  // check if mod > arg
1778  return RCLASS_SUPERCLASSES(arg)[mod_depth] == mod ?
1779  Qfalse :
1780  Qnil;
1781  }
1782  else {
1783  // Depths match, and we know they aren't equal: no relation
1784  return Qnil;
1785  }
1786  }
1787  else {
1788  if (!CLASS_OR_MODULE_P(arg) && !RB_TYPE_P(arg, T_ICLASS)) {
1789  rb_raise(rb_eTypeError, "compared with non class/module");
1790  }
1791  if (class_search_ancestor(mod, RCLASS_ORIGIN(arg))) {
1792  return Qtrue;
1793  }
1794  /* not mod < arg; check if mod > arg */
1795  if (class_search_ancestor(arg, mod)) {
1796  return Qfalse;
1797  }
1798  return Qnil;
1799  }
1800 }
1801 
1802 /*
1803  * call-seq:
1804  * mod < other -> true, false, or nil
1805  *
1806  * Returns true if <i>mod</i> is a subclass of <i>other</i>. Returns
1807  * <code>false</code> if <i>mod</i> is the same as <i>other</i>
1808  * or <i>mod</i> is an ancestor of <i>other</i>.
1809  * Returns <code>nil</code> if there's no relationship between the two.
1810  * (Think of the relationship in terms of the class definition:
1811  * "class A < B" implies "A < B".)
1812  *
1813  */
1814 
1815 static VALUE
1816 rb_mod_lt(VALUE mod, VALUE arg)
1817 {
1818  if (mod == arg) return Qfalse;
1819  return rb_class_inherited_p(mod, arg);
1820 }
1821 
1822 
1823 /*
1824  * call-seq:
1825  * mod >= other -> true, false, or nil
1826  *
1827  * Returns true if <i>mod</i> is an ancestor of <i>other</i>, or the
1828  * two modules are the same. Returns
1829  * <code>nil</code> if there's no relationship between the two.
1830  * (Think of the relationship in terms of the class definition:
1831  * "class A < B" implies "B > A".)
1832  *
1833  */
1834 
1835 static VALUE
1836 rb_mod_ge(VALUE mod, VALUE arg)
1837 {
1838  if (!CLASS_OR_MODULE_P(arg)) {
1839  rb_raise(rb_eTypeError, "compared with non class/module");
1840  }
1841 
1842  return rb_class_inherited_p(arg, mod);
1843 }
1844 
1845 /*
1846  * call-seq:
1847  * mod > other -> true, false, or nil
1848  *
1849  * Returns true if <i>mod</i> is an ancestor of <i>other</i>. Returns
1850  * <code>false</code> if <i>mod</i> is the same as <i>other</i>
1851  * or <i>mod</i> is a descendant of <i>other</i>.
1852  * Returns <code>nil</code> if there's no relationship between the two.
1853  * (Think of the relationship in terms of the class definition:
1854  * "class A < B" implies "B > A".)
1855  *
1856  */
1857 
1858 static VALUE
1859 rb_mod_gt(VALUE mod, VALUE arg)
1860 {
1861  if (mod == arg) return Qfalse;
1862  return rb_mod_ge(mod, arg);
1863 }
1864 
1865 /*
1866  * call-seq:
1867  * module <=> other_module -> -1, 0, +1, or nil
1868  *
1869  * Comparison---Returns -1, 0, +1 or nil depending on whether +module+
1870  * includes +other_module+, they are the same, or if +module+ is included by
1871  * +other_module+.
1872  *
1873  * Returns +nil+ if +module+ has no relationship with +other_module+, if
1874  * +other_module+ is not a module, or if the two values are incomparable.
1875  */
1876 
1877 static VALUE
1878 rb_mod_cmp(VALUE mod, VALUE arg)
1879 {
1880  VALUE cmp;
1881 
1882  if (mod == arg) return INT2FIX(0);
1883  if (!CLASS_OR_MODULE_P(arg)) {
1884  return Qnil;
1885  }
1886 
1887  cmp = rb_class_inherited_p(mod, arg);
1888  if (NIL_P(cmp)) return Qnil;
1889  if (cmp) {
1890  return INT2FIX(-1);
1891  }
1892  return INT2FIX(1);
1893 }
1894 
1895 static VALUE rb_mod_initialize_exec(VALUE module);
1896 
1897 /*
1898  * call-seq:
1899  * Module.new -> mod
1900  * Module.new {|mod| block } -> mod
1901  *
1902  * Creates a new anonymous module. If a block is given, it is passed
1903  * the module object, and the block is evaluated in the context of this
1904  * module like #module_eval.
1905  *
1906  * fred = Module.new do
1907  * def meth1
1908  * "hello"
1909  * end
1910  * def meth2
1911  * "bye"
1912  * end
1913  * end
1914  * a = "my string"
1915  * a.extend(fred) #=> "my string"
1916  * a.meth1 #=> "hello"
1917  * a.meth2 #=> "bye"
1918  *
1919  * Assign the module to a constant (name starting uppercase) if you
1920  * want to treat it like a regular module.
1921  */
1922 
1923 static VALUE
1924 rb_mod_initialize(VALUE module)
1925 {
1926  return rb_mod_initialize_exec(module);
1927 }
1928 
1929 static VALUE
1930 rb_mod_initialize_exec(VALUE module)
1931 {
1932  if (rb_block_given_p()) {
1933  rb_mod_module_exec(1, &module, module);
1934  }
1935  return Qnil;
1936 }
1937 
1938 /* :nodoc: */
1939 static VALUE
1940 rb_mod_initialize_clone(int argc, VALUE* argv, VALUE clone)
1941 {
1942  VALUE ret, orig, opts;
1943  rb_scan_args(argc, argv, "1:", &orig, &opts);
1944  ret = rb_obj_init_clone(argc, argv, clone);
1945  if (OBJ_FROZEN(orig))
1946  rb_class_name(clone);
1947  return ret;
1948 }
1949 
1950 /*
1951  * call-seq:
1952  * Class.new(super_class=Object) -> a_class
1953  * Class.new(super_class=Object) { |mod| ... } -> a_class
1954  *
1955  * Creates a new anonymous (unnamed) class with the given superclass
1956  * (or Object if no parameter is given). You can give a
1957  * class a name by assigning the class object to a constant.
1958  *
1959  * If a block is given, it is passed the class object, and the block
1960  * is evaluated in the context of this class like
1961  * #class_eval.
1962  *
1963  * fred = Class.new do
1964  * def meth1
1965  * "hello"
1966  * end
1967  * def meth2
1968  * "bye"
1969  * end
1970  * end
1971  *
1972  * a = fred.new #=> #<#<Class:0x100381890>:0x100376b98>
1973  * a.meth1 #=> "hello"
1974  * a.meth2 #=> "bye"
1975  *
1976  * Assign the class to a constant (name starting uppercase) if you
1977  * want to treat it like a regular class.
1978  */
1979 
1980 static VALUE
1981 rb_class_initialize(int argc, VALUE *argv, VALUE klass)
1982 {
1983  VALUE super;
1984 
1985  if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) {
1986  rb_raise(rb_eTypeError, "already initialized class");
1987  }
1988  if (rb_check_arity(argc, 0, 1) == 0) {
1989  super = rb_cObject;
1990  }
1991  else {
1992  super = argv[0];
1993  rb_check_inheritable(super);
1994  if (super != rb_cBasicObject && !RCLASS_SUPER(super)) {
1995  rb_raise(rb_eTypeError, "can't inherit uninitialized class");
1996  }
1997  }
1998  RCLASS_SET_SUPER(klass, super);
1999  rb_make_metaclass(klass, RBASIC(super)->klass);
2000  rb_class_inherited(super, klass);
2001  rb_mod_initialize_exec(klass);
2002 
2003  return klass;
2004 }
2005 
2007 void
2008 rb_undefined_alloc(VALUE klass)
2009 {
2010  rb_raise(rb_eTypeError, "allocator undefined for %"PRIsVALUE,
2011  klass);
2012 }
2013 
2014 static rb_alloc_func_t class_get_alloc_func(VALUE klass);
2015 static VALUE class_call_alloc_func(rb_alloc_func_t allocator, VALUE klass);
2016 
2017 /*
2018  * call-seq:
2019  * class.allocate() -> obj
2020  *
2021  * Allocates space for a new object of <i>class</i>'s class and does not
2022  * call initialize on the new instance. The returned object must be an
2023  * instance of <i>class</i>.
2024  *
2025  * klass = Class.new do
2026  * def initialize(*args)
2027  * @initialized = true
2028  * end
2029  *
2030  * def initialized?
2031  * @initialized || false
2032  * end
2033  * end
2034  *
2035  * klass.allocate.initialized? #=> false
2036  *
2037  */
2038 
2039 static VALUE
2040 rb_class_alloc_m(VALUE klass)
2041 {
2042  rb_alloc_func_t allocator = class_get_alloc_func(klass);
2043  if (!rb_obj_respond_to(klass, rb_intern("allocate"), 1)) {
2044  rb_raise(rb_eTypeError, "calling %"PRIsVALUE".allocate is prohibited",
2045  klass);
2046  }
2047  return class_call_alloc_func(allocator, klass);
2048 }
2049 
2050 static VALUE
2051 rb_class_alloc(VALUE klass)
2052 {
2053  rb_alloc_func_t allocator = class_get_alloc_func(klass);
2054  return class_call_alloc_func(allocator, klass);
2055 }
2056 
2057 static rb_alloc_func_t
2058 class_get_alloc_func(VALUE klass)
2059 {
2060  rb_alloc_func_t allocator;
2061 
2062  if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
2063  rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
2064  }
2065  if (RCLASS_SINGLETON_P(klass)) {
2066  rb_raise(rb_eTypeError, "can't create instance of singleton class");
2067  }
2068  allocator = rb_get_alloc_func(klass);
2069  if (!allocator) {
2070  rb_undefined_alloc(klass);
2071  }
2072  return allocator;
2073 }
2074 
2075 static VALUE
2076 class_call_alloc_func(rb_alloc_func_t allocator, VALUE klass)
2077 {
2078  VALUE obj;
2079 
2080  RUBY_DTRACE_CREATE_HOOK(OBJECT, rb_class2name(klass));
2081 
2082  obj = (*allocator)(klass);
2083 
2084  if (rb_obj_class(obj) != rb_class_real(klass)) {
2085  rb_raise(rb_eTypeError, "wrong instance allocation");
2086  }
2087  return obj;
2088 }
2089 
2090 VALUE
2092 {
2093  Check_Type(klass, T_CLASS);
2094  return rb_class_alloc(klass);
2095 }
2096 
2097 /*
2098  * call-seq:
2099  * class.new(args, ...) -> obj
2100  *
2101  * Calls #allocate to create a new object of <i>class</i>'s class,
2102  * then invokes that object's #initialize method, passing it
2103  * <i>args</i>. This is the method that ends up getting called
2104  * whenever an object is constructed using <code>.new</code>.
2105  *
2106  */
2107 
2108 VALUE
2109 rb_class_new_instance_pass_kw(int argc, const VALUE *argv, VALUE klass)
2110 {
2111  VALUE obj;
2112 
2113  obj = rb_class_alloc(klass);
2114  rb_obj_call_init_kw(obj, argc, argv, RB_PASS_CALLED_KEYWORDS);
2115 
2116  return obj;
2117 }
2118 
2119 VALUE
2120 rb_class_new_instance_kw(int argc, const VALUE *argv, VALUE klass, int kw_splat)
2121 {
2122  VALUE obj;
2123  Check_Type(klass, T_CLASS);
2124 
2125  obj = rb_class_alloc(klass);
2126  rb_obj_call_init_kw(obj, argc, argv, kw_splat);
2127 
2128  return obj;
2129 }
2130 
2131 VALUE
2132 rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
2133 {
2134  return rb_class_new_instance_kw(argc, argv, klass, RB_NO_KEYWORDS);
2135 }
2136 
2146 VALUE
2148 {
2149  RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS));
2150 
2151  VALUE super = RCLASS_SUPER(klass);
2152 
2153  if (!super) {
2154  if (klass == rb_cBasicObject) return Qnil;
2155  rb_raise(rb_eTypeError, "uninitialized class");
2156  }
2157 
2158  if (!RCLASS_SUPERCLASS_DEPTH(klass)) {
2159  return Qnil;
2160  }
2161  else {
2162  super = RCLASS_SUPERCLASSES(klass)[RCLASS_SUPERCLASS_DEPTH(klass) - 1];
2163  RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS));
2164  return super;
2165  }
2166 }
2167 
2168 VALUE
2170 {
2171  return RCLASS(klass)->super;
2172 }
2173 
2174 static const char bad_instance_name[] = "'%1$s' is not allowed as an instance variable name";
2175 static const char bad_class_name[] = "'%1$s' is not allowed as a class variable name";
2176 static const char bad_const_name[] = "wrong constant name %1$s";
2177 static const char bad_attr_name[] = "invalid attribute name '%1$s'";
2178 #define wrong_constant_name bad_const_name
2179 
2181 #define id_for_var(obj, name, type) id_for_setter(obj, name, type, bad_##type##_name)
2183 #define id_for_setter(obj, name, type, message) \
2184  check_setter_id(obj, &(name), rb_is_##type##_id, rb_is_##type##_name, message, strlen(message))
2185 static ID
2186 check_setter_id(VALUE obj, VALUE *pname,
2187  int (*valid_id_p)(ID), int (*valid_name_p)(VALUE),
2188  const char *message, size_t message_len)
2189 {
2190  ID id = rb_check_id(pname);
2191  VALUE name = *pname;
2192 
2193  if (id ? !valid_id_p(id) : !valid_name_p(name)) {
2194  rb_name_err_raise_str(rb_fstring_new(message, message_len),
2195  obj, name);
2196  }
2197  return id;
2198 }
2199 
2200 static int
2201 rb_is_attr_name(VALUE name)
2202 {
2203  return rb_is_local_name(name) || rb_is_const_name(name);
2204 }
2205 
2206 static int
2207 rb_is_attr_id(ID id)
2208 {
2209  return rb_is_local_id(id) || rb_is_const_id(id);
2210 }
2211 
2212 static ID
2213 id_for_attr(VALUE obj, VALUE name)
2214 {
2215  ID id = id_for_var(obj, name, attr);
2216  if (!id) id = rb_intern_str(name);
2217  return id;
2218 }
2219 
2220 /*
2221  * call-seq:
2222  * attr_reader(symbol, ...) -> array
2223  * attr(symbol, ...) -> array
2224  * attr_reader(string, ...) -> array
2225  * attr(string, ...) -> array
2226  *
2227  * Creates instance variables and corresponding methods that return the
2228  * value of each instance variable. Equivalent to calling
2229  * ``<code>attr</code><i>:name</i>'' on each name in turn.
2230  * String arguments are converted to symbols.
2231  * Returns an array of defined method names as symbols.
2232  */
2233 
2234 static VALUE
2235 rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
2236 {
2237  int i;
2238  VALUE names = rb_ary_new2(argc);
2239 
2240  for (i=0; i<argc; i++) {
2241  ID id = id_for_attr(klass, argv[i]);
2242  rb_attr(klass, id, TRUE, FALSE, TRUE);
2243  rb_ary_push(names, ID2SYM(id));
2244  }
2245  return names;
2246 }
2247 
2252 VALUE
2253 rb_mod_attr(int argc, VALUE *argv, VALUE klass)
2254 {
2255  if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
2256  ID id = id_for_attr(klass, argv[0]);
2257  VALUE names = rb_ary_new();
2258 
2259  rb_category_warning(RB_WARN_CATEGORY_DEPRECATED, "optional boolean argument is obsoleted");
2260  rb_attr(klass, id, 1, RTEST(argv[1]), TRUE);
2261  rb_ary_push(names, ID2SYM(id));
2262  if (argv[1] == Qtrue) rb_ary_push(names, ID2SYM(rb_id_attrset(id)));
2263  return names;
2264  }
2265  return rb_mod_attr_reader(argc, argv, klass);
2266 }
2267 
2268 /*
2269  * call-seq:
2270  * attr_writer(symbol, ...) -> array
2271  * attr_writer(string, ...) -> array
2272  *
2273  * Creates an accessor method to allow assignment to the attribute
2274  * <i>symbol</i><code>.id2name</code>.
2275  * String arguments are converted to symbols.
2276  * Returns an array of defined method names as symbols.
2277  */
2278 
2279 static VALUE
2280 rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
2281 {
2282  int i;
2283  VALUE names = rb_ary_new2(argc);
2284 
2285  for (i=0; i<argc; i++) {
2286  ID id = id_for_attr(klass, argv[i]);
2287  rb_attr(klass, id, FALSE, TRUE, TRUE);
2288  rb_ary_push(names, ID2SYM(rb_id_attrset(id)));
2289  }
2290  return names;
2291 }
2292 
2293 /*
2294  * call-seq:
2295  * attr_accessor(symbol, ...) -> array
2296  * attr_accessor(string, ...) -> array
2297  *
2298  * Defines a named attribute for this module, where the name is
2299  * <i>symbol.</i><code>id2name</code>, creating an instance variable
2300  * (<code>@name</code>) and a corresponding access method to read it.
2301  * Also creates a method called <code>name=</code> to set the attribute.
2302  * String arguments are converted to symbols.
2303  * Returns an array of defined method names as symbols.
2304  *
2305  * module Mod
2306  * attr_accessor(:one, :two) #=> [:one, :one=, :two, :two=]
2307  * end
2308  * Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]
2309  */
2310 
2311 static VALUE
2312 rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
2313 {
2314  int i;
2315  VALUE names = rb_ary_new2(argc * 2);
2316 
2317  for (i=0; i<argc; i++) {
2318  ID id = id_for_attr(klass, argv[i]);
2319 
2320  rb_attr(klass, id, TRUE, TRUE, TRUE);
2321  rb_ary_push(names, ID2SYM(id));
2322  rb_ary_push(names, ID2SYM(rb_id_attrset(id)));
2323  }
2324  return names;
2325 }
2326 
2327 /*
2328  * call-seq:
2329  * mod.const_get(sym, inherit=true) -> obj
2330  * mod.const_get(str, inherit=true) -> obj
2331  *
2332  * Checks for a constant with the given name in <i>mod</i>.
2333  * If +inherit+ is set, the lookup will also search
2334  * the ancestors (and +Object+ if <i>mod</i> is a +Module+).
2335  *
2336  * The value of the constant is returned if a definition is found,
2337  * otherwise a +NameError+ is raised.
2338  *
2339  * Math.const_get(:PI) #=> 3.14159265358979
2340  *
2341  * This method will recursively look up constant names if a namespaced
2342  * class name is provided. For example:
2343  *
2344  * module Foo; class Bar; end end
2345  * Object.const_get 'Foo::Bar'
2346  *
2347  * The +inherit+ flag is respected on each lookup. For example:
2348  *
2349  * module Foo
2350  * class Bar
2351  * VAL = 10
2352  * end
2353  *
2354  * class Baz < Bar; end
2355  * end
2356  *
2357  * Object.const_get 'Foo::Baz::VAL' # => 10
2358  * Object.const_get 'Foo::Baz::VAL', false # => NameError
2359  *
2360  * If the argument is not a valid constant name a +NameError+ will be
2361  * raised with a warning "wrong constant name".
2362  *
2363  * Object.const_get 'foobar' #=> NameError: wrong constant name foobar
2364  *
2365  */
2366 
2367 static VALUE
2368 rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
2369 {
2370  VALUE name, recur;
2371  rb_encoding *enc;
2372  const char *pbeg, *p, *path, *pend;
2373  ID id;
2374 
2375  rb_check_arity(argc, 1, 2);
2376  name = argv[0];
2377  recur = (argc == 1) ? Qtrue : argv[1];
2378 
2379  if (SYMBOL_P(name)) {
2380  if (!rb_is_const_sym(name)) goto wrong_name;
2381  id = rb_check_id(&name);
2382  if (!id) return rb_const_missing(mod, name);
2383  return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
2384  }
2385 
2386  path = StringValuePtr(name);
2387  enc = rb_enc_get(name);
2388 
2389  if (!rb_enc_asciicompat(enc)) {
2390  rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
2391  }
2392 
2393  pbeg = p = path;
2394  pend = path + RSTRING_LEN(name);
2395 
2396  if (p >= pend || !*p) {
2397  goto wrong_name;
2398  }
2399 
2400  if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
2401  mod = rb_cObject;
2402  p += 2;
2403  pbeg = p;
2404  }
2405 
2406  while (p < pend) {
2407  VALUE part;
2408  long len, beglen;
2409 
2410  while (p < pend && *p != ':') p++;
2411 
2412  if (pbeg == p) goto wrong_name;
2413 
2414  id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
2415  beglen = pbeg-path;
2416 
2417  if (p < pend && p[0] == ':') {
2418  if (p + 2 >= pend || p[1] != ':') goto wrong_name;
2419  p += 2;
2420  pbeg = p;
2421  }
2422 
2423  if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
2424  rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
2425  QUOTE(name));
2426  }
2427 
2428  if (!id) {
2429  part = rb_str_subseq(name, beglen, len);
2430  OBJ_FREEZE(part);
2431  if (!rb_is_const_name(part)) {
2432  name = part;
2433  goto wrong_name;
2434  }
2435  else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) {
2436  part = rb_str_intern(part);
2437  mod = rb_const_missing(mod, part);
2438  continue;
2439  }
2440  else {
2441  rb_mod_const_missing(mod, part);
2442  }
2443  }
2444  if (!rb_is_const_id(id)) {
2445  name = ID2SYM(id);
2446  goto wrong_name;
2447  }
2448 #if 0
2449  mod = rb_const_get_0(mod, id, beglen > 0 || !RTEST(recur), RTEST(recur), FALSE);
2450 #else
2451  if (!RTEST(recur)) {
2452  mod = rb_const_get_at(mod, id);
2453  }
2454  else if (beglen == 0) {
2455  mod = rb_const_get(mod, id);
2456  }
2457  else {
2458  mod = rb_const_get_from(mod, id);
2459  }
2460 #endif
2461  }
2462 
2463  return mod;
2464 
2465  wrong_name:
2466  rb_name_err_raise(wrong_constant_name, mod, name);
2468 }
2469 
2470 /*
2471  * call-seq:
2472  * mod.const_set(sym, obj) -> obj
2473  * mod.const_set(str, obj) -> obj
2474  *
2475  * Sets the named constant to the given object, returning that object.
2476  * Creates a new constant if no constant with the given name previously
2477  * existed.
2478  *
2479  * Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714
2480  * Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968
2481  *
2482  * If +sym+ or +str+ is not a valid constant name a +NameError+ will be
2483  * raised with a warning "wrong constant name".
2484  *
2485  * Object.const_set('foobar', 42) #=> NameError: wrong constant name foobar
2486  *
2487  */
2488 
2489 static VALUE
2490 rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
2491 {
2492  ID id = id_for_var(mod, name, const);
2493  if (!id) id = rb_intern_str(name);
2494  rb_const_set(mod, id, value);
2495 
2496  return value;
2497 }
2498 
2499 /*
2500  * call-seq:
2501  * mod.const_defined?(sym, inherit=true) -> true or false
2502  * mod.const_defined?(str, inherit=true) -> true or false
2503  *
2504  * Says whether _mod_ or its ancestors have a constant with the given name:
2505  *
2506  * Float.const_defined?(:EPSILON) #=> true, found in Float itself
2507  * Float.const_defined?("String") #=> true, found in Object (ancestor)
2508  * BasicObject.const_defined?(:Hash) #=> false
2509  *
2510  * If _mod_ is a +Module+, additionally +Object+ and its ancestors are checked:
2511  *
2512  * Math.const_defined?(:String) #=> true, found in Object
2513  *
2514  * In each of the checked classes or modules, if the constant is not present
2515  * but there is an autoload for it, +true+ is returned directly without
2516  * autoloading:
2517  *
2518  * module Admin
2519  * autoload :User, 'admin/user'
2520  * end
2521  * Admin.const_defined?(:User) #=> true
2522  *
2523  * If the constant is not found the callback +const_missing+ is *not* called
2524  * and the method returns +false+.
2525  *
2526  * If +inherit+ is false, the lookup only checks the constants in the receiver:
2527  *
2528  * IO.const_defined?(:SYNC) #=> true, found in File::Constants (ancestor)
2529  * IO.const_defined?(:SYNC, false) #=> false, not found in IO itself
2530  *
2531  * In this case, the same logic for autoloading applies.
2532  *
2533  * If the argument is not a valid constant name a +NameError+ is raised with the
2534  * message "wrong constant name _name_":
2535  *
2536  * Hash.const_defined? 'foobar' #=> NameError: wrong constant name foobar
2537  *
2538  */
2539 
2540 static VALUE
2541 rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
2542 {
2543  VALUE name, recur;
2544  rb_encoding *enc;
2545  const char *pbeg, *p, *path, *pend;
2546  ID id;
2547 
2548  rb_check_arity(argc, 1, 2);
2549  name = argv[0];
2550  recur = (argc == 1) ? Qtrue : argv[1];
2551 
2552  if (SYMBOL_P(name)) {
2553  if (!rb_is_const_sym(name)) goto wrong_name;
2554  id = rb_check_id(&name);
2555  if (!id) return Qfalse;
2556  return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
2557  }
2558 
2559  path = StringValuePtr(name);
2560  enc = rb_enc_get(name);
2561 
2562  if (!rb_enc_asciicompat(enc)) {
2563  rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
2564  }
2565 
2566  pbeg = p = path;
2567  pend = path + RSTRING_LEN(name);
2568 
2569  if (p >= pend || !*p) {
2570  goto wrong_name;
2571  }
2572 
2573  if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
2574  mod = rb_cObject;
2575  p += 2;
2576  pbeg = p;
2577  }
2578 
2579  while (p < pend) {
2580  VALUE part;
2581  long len, beglen;
2582 
2583  while (p < pend && *p != ':') p++;
2584 
2585  if (pbeg == p) goto wrong_name;
2586 
2587  id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
2588  beglen = pbeg-path;
2589 
2590  if (p < pend && p[0] == ':') {
2591  if (p + 2 >= pend || p[1] != ':') goto wrong_name;
2592  p += 2;
2593  pbeg = p;
2594  }
2595 
2596  if (!id) {
2597  part = rb_str_subseq(name, beglen, len);
2598  OBJ_FREEZE(part);
2599  if (!rb_is_const_name(part)) {
2600  name = part;
2601  goto wrong_name;
2602  }
2603  else {
2604  return Qfalse;
2605  }
2606  }
2607  if (!rb_is_const_id(id)) {
2608  name = ID2SYM(id);
2609  goto wrong_name;
2610  }
2611 
2612 #if 0
2613  mod = rb_const_search(mod, id, beglen > 0 || !RTEST(recur), RTEST(recur), FALSE);
2614  if (UNDEF_P(mod)) return Qfalse;
2615 #else
2616  if (!RTEST(recur)) {
2617  if (!rb_const_defined_at(mod, id))
2618  return Qfalse;
2619  if (p == pend) return Qtrue;
2620  mod = rb_const_get_at(mod, id);
2621  }
2622  else if (beglen == 0) {
2623  if (!rb_const_defined(mod, id))
2624  return Qfalse;
2625  if (p == pend) return Qtrue;
2626  mod = rb_const_get(mod, id);
2627  }
2628  else {
2629  if (!rb_const_defined_from(mod, id))
2630  return Qfalse;
2631  if (p == pend) return Qtrue;
2632  mod = rb_const_get_from(mod, id);
2633  }
2634 #endif
2635 
2636  if (p < pend && !RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
2637  rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
2638  QUOTE(name));
2639  }
2640  }
2641 
2642  return Qtrue;
2643 
2644  wrong_name:
2645  rb_name_err_raise(wrong_constant_name, mod, name);
2647 }
2648 
2649 /*
2650  * call-seq:
2651  * mod.const_source_location(sym, inherit=true) -> [String, Integer]
2652  * mod.const_source_location(str, inherit=true) -> [String, Integer]
2653  *
2654  * Returns the Ruby source filename and line number containing the definition
2655  * of the constant specified. If the named constant is not found, +nil+ is returned.
2656  * If the constant is found, but its source location can not be extracted
2657  * (constant is defined in C code), empty array is returned.
2658  *
2659  * _inherit_ specifies whether to lookup in <code>mod.ancestors</code> (+true+
2660  * by default).
2661  *
2662  * # test.rb:
2663  * class A # line 1
2664  * C1 = 1
2665  * C2 = 2
2666  * end
2667  *
2668  * module M # line 6
2669  * C3 = 3
2670  * end
2671  *
2672  * class B < A # line 10
2673  * include M
2674  * C4 = 4
2675  * end
2676  *
2677  * class A # continuation of A definition
2678  * C2 = 8 # constant redefinition; warned yet allowed
2679  * end
2680  *
2681  * p B.const_source_location('C4') # => ["test.rb", 12]
2682  * p B.const_source_location('C3') # => ["test.rb", 7]
2683  * p B.const_source_location('C1') # => ["test.rb", 2]
2684  *
2685  * p B.const_source_location('C3', false) # => nil -- don't lookup in ancestors
2686  *
2687  * p A.const_source_location('C2') # => ["test.rb", 16] -- actual (last) definition place
2688  *
2689  * p Object.const_source_location('B') # => ["test.rb", 10] -- top-level constant could be looked through Object
2690  * p Object.const_source_location('A') # => ["test.rb", 1] -- class reopening is NOT considered new definition
2691  *
2692  * p B.const_source_location('A') # => ["test.rb", 1] -- because Object is in ancestors
2693  * p M.const_source_location('A') # => ["test.rb", 1] -- Object is not ancestor, but additionally checked for modules
2694  *
2695  * p Object.const_source_location('A::C1') # => ["test.rb", 2] -- nesting is supported
2696  * p Object.const_source_location('String') # => [] -- constant is defined in C code
2697  *
2698  *
2699  */
2700 static VALUE
2701 rb_mod_const_source_location(int argc, VALUE *argv, VALUE mod)
2702 {
2703  VALUE name, recur, loc = Qnil;
2704  rb_encoding *enc;
2705  const char *pbeg, *p, *path, *pend;
2706  ID id;
2707 
2708  rb_check_arity(argc, 1, 2);
2709  name = argv[0];
2710  recur = (argc == 1) ? Qtrue : argv[1];
2711 
2712  if (SYMBOL_P(name)) {
2713  if (!rb_is_const_sym(name)) goto wrong_name;
2714  id = rb_check_id(&name);
2715  if (!id) return Qnil;
2716  return RTEST(recur) ? rb_const_source_location(mod, id) : rb_const_source_location_at(mod, id);
2717  }
2718 
2719  path = StringValuePtr(name);
2720  enc = rb_enc_get(name);
2721 
2722  if (!rb_enc_asciicompat(enc)) {
2723  rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
2724  }
2725 
2726  pbeg = p = path;
2727  pend = path + RSTRING_LEN(name);
2728 
2729  if (p >= pend || !*p) {
2730  goto wrong_name;
2731  }
2732 
2733  if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
2734  mod = rb_cObject;
2735  p += 2;
2736  pbeg = p;
2737  }
2738 
2739  while (p < pend) {
2740  VALUE part;
2741  long len, beglen;
2742 
2743  while (p < pend && *p != ':') p++;
2744 
2745  if (pbeg == p) goto wrong_name;
2746 
2747  id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
2748  beglen = pbeg-path;
2749 
2750  if (p < pend && p[0] == ':') {
2751  if (p + 2 >= pend || p[1] != ':') goto wrong_name;
2752  p += 2;
2753  pbeg = p;
2754  }
2755 
2756  if (!id) {
2757  part = rb_str_subseq(name, beglen, len);
2758  OBJ_FREEZE(part);
2759  if (!rb_is_const_name(part)) {
2760  name = part;
2761  goto wrong_name;
2762  }
2763  else {
2764  return Qnil;
2765  }
2766  }
2767  if (!rb_is_const_id(id)) {
2768  name = ID2SYM(id);
2769  goto wrong_name;
2770  }
2771  if (p < pend) {
2772  if (RTEST(recur)) {
2773  mod = rb_const_get(mod, id);
2774  }
2775  else {
2776  mod = rb_const_get_at(mod, id);
2777  }
2778  if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
2779  rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
2780  QUOTE(name));
2781  }
2782  }
2783  else {
2784  if (RTEST(recur)) {
2785  loc = rb_const_source_location(mod, id);
2786  }
2787  else {
2788  loc = rb_const_source_location_at(mod, id);
2789  }
2790  break;
2791  }
2792  recur = Qfalse;
2793  }
2794 
2795  return loc;
2796 
2797  wrong_name:
2798  rb_name_err_raise(wrong_constant_name, mod, name);
2800 }
2801 
2802 /*
2803  * call-seq:
2804  * obj.instance_variable_get(symbol) -> obj
2805  * obj.instance_variable_get(string) -> obj
2806  *
2807  * Returns the value of the given instance variable, or nil if the
2808  * instance variable is not set. The <code>@</code> part of the
2809  * variable name should be included for regular instance
2810  * variables. Throws a NameError exception if the
2811  * supplied symbol is not valid as an instance variable name.
2812  * String arguments are converted to symbols.
2813  *
2814  * class Fred
2815  * def initialize(p1, p2)
2816  * @a, @b = p1, p2
2817  * end
2818  * end
2819  * fred = Fred.new('cat', 99)
2820  * fred.instance_variable_get(:@a) #=> "cat"
2821  * fred.instance_variable_get("@b") #=> 99
2822  */
2823 
2824 static VALUE
2825 rb_obj_ivar_get(VALUE obj, VALUE iv)
2826 {
2827  ID id = id_for_var(obj, iv, instance);
2828 
2829  if (!id) {
2830  return Qnil;
2831  }
2832  return rb_ivar_get(obj, id);
2833 }
2834 
2835 /*
2836  * call-seq:
2837  * obj.instance_variable_set(symbol, obj) -> obj
2838  * obj.instance_variable_set(string, obj) -> obj
2839  *
2840  * Sets the instance variable named by <i>symbol</i> to the given
2841  * object. This may circumvent the encapsulation intended by
2842  * the author of the class, so it should be used with care.
2843  * The variable does not have to exist prior to this call.
2844  * If the instance variable name is passed as a string, that string
2845  * is converted to a symbol.
2846  *
2847  * class Fred
2848  * def initialize(p1, p2)
2849  * @a, @b = p1, p2
2850  * end
2851  * end
2852  * fred = Fred.new('cat', 99)
2853  * fred.instance_variable_set(:@a, 'dog') #=> "dog"
2854  * fred.instance_variable_set(:@c, 'cat') #=> "cat"
2855  * fred.inspect #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
2856  */
2857 
2858 static VALUE
2859 rb_obj_ivar_set_m(VALUE obj, VALUE iv, VALUE val)
2860 {
2861  ID id = id_for_var(obj, iv, instance);
2862  if (!id) id = rb_intern_str(iv);
2863  return rb_ivar_set(obj, id, val);
2864 }
2865 
2866 /*
2867  * call-seq:
2868  * obj.instance_variable_defined?(symbol) -> true or false
2869  * obj.instance_variable_defined?(string) -> true or false
2870  *
2871  * Returns <code>true</code> if the given instance variable is
2872  * defined in <i>obj</i>.
2873  * String arguments are converted to symbols.
2874  *
2875  * class Fred
2876  * def initialize(p1, p2)
2877  * @a, @b = p1, p2
2878  * end
2879  * end
2880  * fred = Fred.new('cat', 99)
2881  * fred.instance_variable_defined?(:@a) #=> true
2882  * fred.instance_variable_defined?("@b") #=> true
2883  * fred.instance_variable_defined?("@c") #=> false
2884  */
2885 
2886 static VALUE
2887 rb_obj_ivar_defined(VALUE obj, VALUE iv)
2888 {
2889  ID id = id_for_var(obj, iv, instance);
2890 
2891  if (!id) {
2892  return Qfalse;
2893  }
2894  return rb_ivar_defined(obj, id);
2895 }
2896 
2897 /*
2898  * call-seq:
2899  * mod.class_variable_get(symbol) -> obj
2900  * mod.class_variable_get(string) -> obj
2901  *
2902  * Returns the value of the given class variable (or throws a
2903  * NameError exception). The <code>@@</code> part of the
2904  * variable name should be included for regular class variables.
2905  * String arguments are converted to symbols.
2906  *
2907  * class Fred
2908  * @@foo = 99
2909  * end
2910  * Fred.class_variable_get(:@@foo) #=> 99
2911  */
2912 
2913 static VALUE
2914 rb_mod_cvar_get(VALUE obj, VALUE iv)
2915 {
2916  ID id = id_for_var(obj, iv, class);
2917 
2918  if (!id) {
2919  rb_name_err_raise("uninitialized class variable %1$s in %2$s",
2920  obj, iv);
2921  }
2922  return rb_cvar_get(obj, id);
2923 }
2924 
2925 /*
2926  * call-seq:
2927  * obj.class_variable_set(symbol, obj) -> obj
2928  * obj.class_variable_set(string, obj) -> obj
2929  *
2930  * Sets the class variable named by <i>symbol</i> to the given
2931  * object.
2932  * If the class variable name is passed as a string, that string
2933  * is converted to a symbol.
2934  *
2935  * class Fred
2936  * @@foo = 99
2937  * def foo
2938  * @@foo
2939  * end
2940  * end
2941  * Fred.class_variable_set(:@@foo, 101) #=> 101
2942  * Fred.new.foo #=> 101
2943  */
2944 
2945 static VALUE
2946 rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
2947 {
2948  ID id = id_for_var(obj, iv, class);
2949  if (!id) id = rb_intern_str(iv);
2950  rb_cvar_set(obj, id, val);
2951  return val;
2952 }
2953 
2954 /*
2955  * call-seq:
2956  * obj.class_variable_defined?(symbol) -> true or false
2957  * obj.class_variable_defined?(string) -> true or false
2958  *
2959  * Returns <code>true</code> if the given class variable is defined
2960  * in <i>obj</i>.
2961  * String arguments are converted to symbols.
2962  *
2963  * class Fred
2964  * @@foo = 99
2965  * end
2966  * Fred.class_variable_defined?(:@@foo) #=> true
2967  * Fred.class_variable_defined?(:@@bar) #=> false
2968  */
2969 
2970 static VALUE
2971 rb_mod_cvar_defined(VALUE obj, VALUE iv)
2972 {
2973  ID id = id_for_var(obj, iv, class);
2974 
2975  if (!id) {
2976  return Qfalse;
2977  }
2978  return rb_cvar_defined(obj, id);
2979 }
2980 
2981 /*
2982  * call-seq:
2983  * mod.singleton_class? -> true or false
2984  *
2985  * Returns <code>true</code> if <i>mod</i> is a singleton class or
2986  * <code>false</code> if it is an ordinary class or module.
2987  *
2988  * class C
2989  * end
2990  * C.singleton_class? #=> false
2991  * C.singleton_class.singleton_class? #=> true
2992  */
2993 
2994 static VALUE
2995 rb_mod_singleton_p(VALUE klass)
2996 {
2997  return RBOOL(RCLASS_SINGLETON_P(klass));
2998 }
2999 
3001 static const struct conv_method_tbl {
3002  const char method[6];
3003  unsigned short id;
3004 } conv_method_names[] = {
3005 #define M(n) {#n, (unsigned short)idTo_##n}
3006  M(int),
3007  M(ary),
3008  M(str),
3009  M(sym),
3010  M(hash),
3011  M(proc),
3012  M(io),
3013  M(a),
3014  M(s),
3015  M(i),
3016  M(f),
3017  M(r),
3018 #undef M
3019 };
3020 #define IMPLICIT_CONVERSIONS 7
3021 
3022 static int
3023 conv_method_index(const char *method)
3024 {
3025  static const char prefix[] = "to_";
3026 
3027  if (strncmp(prefix, method, sizeof(prefix)-1) == 0) {
3028  const char *const meth = &method[sizeof(prefix)-1];
3029  int i;
3030  for (i=0; i < numberof(conv_method_names); i++) {
3031  if (conv_method_names[i].method[0] == meth[0] &&
3032  strcmp(conv_method_names[i].method, meth) == 0) {
3033  return i;
3034  }
3035  }
3036  }
3037  return numberof(conv_method_names);
3038 }
3039 
3040 static VALUE
3041 convert_type_with_id(VALUE val, const char *tname, ID method, int raise, int index)
3042 {
3043  VALUE r = rb_check_funcall(val, method, 0, 0);
3044  if (UNDEF_P(r)) {
3045  if (raise) {
3046  const char *msg =
3047  ((index < 0 ? conv_method_index(rb_id2name(method)) : index)
3048  < IMPLICIT_CONVERSIONS) ?
3049  "no implicit conversion of" : "can't convert";
3050  const char *cname = NIL_P(val) ? "nil" :
3051  val == Qtrue ? "true" :
3052  val == Qfalse ? "false" :
3053  NULL;
3054  if (cname)
3055  rb_raise(rb_eTypeError, "%s %s into %s", msg, cname, tname);
3056  rb_raise(rb_eTypeError, "%s %"PRIsVALUE" into %s", msg,
3057  rb_obj_class(val),
3058  tname);
3059  }
3060  return Qnil;
3061  }
3062  return r;
3063 }
3064 
3065 static VALUE
3066 convert_type(VALUE val, const char *tname, const char *method, int raise)
3067 {
3068  int i = conv_method_index(method);
3069  ID m = i < numberof(conv_method_names) ?
3070  conv_method_names[i].id : rb_intern(method);
3071  return convert_type_with_id(val, tname, m, raise, i);
3072 }
3073 
3075 NORETURN(static void conversion_mismatch(VALUE, const char *, const char *, VALUE));
3076 static void
3077 conversion_mismatch(VALUE val, const char *tname, const char *method, VALUE result)
3078 {
3079  VALUE cname = rb_obj_class(val);
3081  "can't convert %"PRIsVALUE" to %s (%"PRIsVALUE"#%s gives %"PRIsVALUE")",
3082  cname, tname, cname, method, rb_obj_class(result));
3083 }
3084 
3085 VALUE
3086 rb_convert_type(VALUE val, int type, const char *tname, const char *method)
3087 {
3088  VALUE v;
3089 
3090  if (TYPE(val) == type) return val;
3091  v = convert_type(val, tname, method, TRUE);
3092  if (TYPE(v) != type) {
3093  conversion_mismatch(val, tname, method, v);
3094  }
3095  return v;
3096 }
3097 
3099 VALUE
3100 rb_convert_type_with_id(VALUE val, int type, const char *tname, ID method)
3101 {
3102  VALUE v;
3103 
3104  if (TYPE(val) == type) return val;
3105  v = convert_type_with_id(val, tname, method, TRUE, -1);
3106  if (TYPE(v) != type) {
3107  conversion_mismatch(val, tname, RSTRING_PTR(rb_id2str(method)), v);
3108  }
3109  return v;
3110 }
3111 
3112 VALUE
3113 rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
3114 {
3115  VALUE v;
3116 
3117  /* always convert T_DATA */
3118  if (TYPE(val) == type && type != T_DATA) return val;
3119  v = convert_type(val, tname, method, FALSE);
3120  if (NIL_P(v)) return Qnil;
3121  if (TYPE(v) != type) {
3122  conversion_mismatch(val, tname, method, v);
3123  }
3124  return v;
3125 }
3126 
3128 VALUE
3129 rb_check_convert_type_with_id(VALUE val, int type, const char *tname, ID method)
3130 {
3131  VALUE v;
3132 
3133  /* always convert T_DATA */
3134  if (TYPE(val) == type && type != T_DATA) return val;
3135  v = convert_type_with_id(val, tname, method, FALSE, -1);
3136  if (NIL_P(v)) return Qnil;
3137  if (TYPE(v) != type) {
3138  conversion_mismatch(val, tname, RSTRING_PTR(rb_id2str(method)), v);
3139  }
3140  return v;
3141 }
3142 
3143 #define try_to_int(val, mid, raise) \
3144  convert_type_with_id(val, "Integer", mid, raise, -1)
3145 
3146 ALWAYS_INLINE(static VALUE rb_to_integer_with_id_exception(VALUE val, const char *method, ID mid, int raise));
3147 /* Integer specific rb_check_convert_type_with_id */
3148 static inline VALUE
3149 rb_to_integer_with_id_exception(VALUE val, const char *method, ID mid, int raise)
3150 {
3151  // We need to pop the lazily pushed frame when not raising an exception.
3152  rb_control_frame_t *current_cfp;
3153  VALUE v;
3154 
3155  if (RB_INTEGER_TYPE_P(val)) return val;
3156  current_cfp = GET_EC()->cfp;
3157  rb_yjit_lazy_push_frame(GET_EC()->cfp->pc);
3158  v = try_to_int(val, mid, raise);
3159  if (!raise && NIL_P(v)) {
3160  GET_EC()->cfp = current_cfp;
3161  return Qnil;
3162  }
3163  if (!RB_INTEGER_TYPE_P(v)) {
3164  conversion_mismatch(val, "Integer", method, v);
3165  }
3166  GET_EC()->cfp = current_cfp;
3167  return v;
3168 }
3169 #define rb_to_integer(val, method, mid) \
3170  rb_to_integer_with_id_exception(val, method, mid, TRUE)
3171 
3172 VALUE
3173 rb_check_to_integer(VALUE val, const char *method)
3174 {
3175  VALUE v;
3176 
3177  if (RB_INTEGER_TYPE_P(val)) return val;
3178  v = convert_type(val, "Integer", method, FALSE);
3179  if (!RB_INTEGER_TYPE_P(v)) {
3180  return Qnil;
3181  }
3182  return v;
3183 }
3184 
3185 VALUE
3187 {
3188  return rb_to_integer(val, "to_int", idTo_int);
3189 }
3190 
3191 VALUE
3193 {
3194  if (RB_INTEGER_TYPE_P(val)) return val;
3195  val = try_to_int(val, idTo_int, FALSE);
3196  if (RB_INTEGER_TYPE_P(val)) return val;
3197  return Qnil;
3198 }
3199 
3200 static VALUE
3201 rb_check_to_i(VALUE val)
3202 {
3203  if (RB_INTEGER_TYPE_P(val)) return val;
3204  val = try_to_int(val, idTo_i, FALSE);
3205  if (RB_INTEGER_TYPE_P(val)) return val;
3206  return Qnil;
3207 }
3208 
3209 static VALUE
3210 rb_convert_to_integer(VALUE val, int base, int raise_exception)
3211 {
3212  VALUE tmp;
3213 
3214  if (base) {
3215  tmp = rb_check_string_type(val);
3216 
3217  if (! NIL_P(tmp)) {
3218  val = tmp;
3219  }
3220  else if (! raise_exception) {
3221  return Qnil;
3222  }
3223  else {
3224  rb_raise(rb_eArgError, "base specified for non string value");
3225  }
3226  }
3227  if (RB_FLOAT_TYPE_P(val)) {
3228  double f = RFLOAT_VALUE(val);
3229  if (!raise_exception && !isfinite(f)) return Qnil;
3230  if (FIXABLE(f)) return LONG2FIX((long)f);
3231  return rb_dbl2big(f);
3232  }
3233  else if (RB_INTEGER_TYPE_P(val)) {
3234  return val;
3235  }
3236  else if (RB_TYPE_P(val, T_STRING)) {
3237  return rb_str_convert_to_inum(val, base, TRUE, raise_exception);
3238  }
3239  else if (NIL_P(val)) {
3240  if (!raise_exception) return Qnil;
3241  rb_raise(rb_eTypeError, "can't convert nil into Integer");
3242  }
3243 
3244  tmp = rb_protect(rb_check_to_int, val, NULL);
3245  if (RB_INTEGER_TYPE_P(tmp)) return tmp;
3247  if (!NIL_P(tmp = rb_check_string_type(val))) {
3248  return rb_str_convert_to_inum(tmp, base, TRUE, raise_exception);
3249  }
3250 
3251  if (!raise_exception) {
3252  VALUE result = rb_protect(rb_check_to_i, val, NULL);
3254  return result;
3255  }
3256 
3257  return rb_to_integer(val, "to_i", idTo_i);
3258 }
3259 
3260 VALUE
3262 {
3263  return rb_convert_to_integer(val, 0, TRUE);
3264 }
3265 
3266 VALUE
3267 rb_check_integer_type(VALUE val)
3268 {
3269  return rb_to_integer_with_id_exception(val, "to_int", idTo_int, FALSE);
3270 }
3271 
3272 int
3273 rb_bool_expected(VALUE obj, const char *flagname, int raise)
3274 {
3275  switch (obj) {
3276  case Qtrue:
3277  return TRUE;
3278  case Qfalse:
3279  return FALSE;
3280  default: {
3281  static const char message[] = "expected true or false as %s: %+"PRIsVALUE;
3282  if (raise) {
3283  rb_raise(rb_eArgError, message, flagname, obj);
3284  }
3285  rb_warning(message, flagname, obj);
3286  return !NIL_P(obj);
3287  }
3288  }
3289 }
3290 
3291 int
3292 rb_opts_exception_p(VALUE opts, int default_value)
3293 {
3294  static const ID kwds[1] = {idException};
3295  VALUE exception;
3296  if (rb_get_kwargs(opts, kwds, 0, 1, &exception))
3297  return rb_bool_expected(exception, "exception", TRUE);
3298  return default_value;
3299 }
3300 
3301 static VALUE
3302 rb_f_integer1(rb_execution_context_t *ec, VALUE obj, VALUE arg)
3303 {
3304  return rb_convert_to_integer(arg, 0, TRUE);
3305 }
3306 
3307 static VALUE
3308 rb_f_integer(rb_execution_context_t *ec, VALUE obj, VALUE arg, VALUE base, VALUE exception)
3309 {
3310  int exc = rb_bool_expected(exception, "exception", TRUE);
3311  return rb_convert_to_integer(arg, NUM2INT(base), exc);
3312 }
3313 
3314 static double
3315 rb_cstr_to_dbl_raise(const char *p, rb_encoding *enc, int badcheck, int raise, int *error)
3316 {
3317  const char *q;
3318  char *end;
3319  double d;
3320  const char *ellipsis = "";
3321  int w;
3322  enum {max_width = 20};
3323 #define OutOfRange() ((end - p > max_width) ? \
3324  (w = max_width, ellipsis = "...") : \
3325  (w = (int)(end - p), ellipsis = ""))
3326  /* p...end has been parsed with strtod, should be ASCII-only */
3327 
3328  if (!p) return 0.0;
3329  q = p;
3330  while (ISSPACE(*p)) p++;
3331 
3332  if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
3333  return 0.0;
3334  }
3335 
3336  d = strtod(p, &end);
3337  if (errno == ERANGE) {
3338  OutOfRange();
3339  rb_warning("Float %.*s%s out of range", w, p, ellipsis);
3340  errno = 0;
3341  }
3342  if (p == end) {
3343  if (badcheck) {
3344  goto bad;
3345  }
3346  return d;
3347  }
3348  if (*end) {
3349  char buf[DBL_DIG * 4 + 10];
3350  char *n = buf;
3351  char *const init_e = buf + DBL_DIG * 4;
3352  char *e = init_e;
3353  char prev = 0;
3354  int dot_seen = FALSE;
3355 
3356  switch (*p) {case '+': case '-': prev = *n++ = *p++;}
3357  if (*p == '0') {
3358  prev = *n++ = '0';
3359  while (*++p == '0');
3360  }
3361  while (p < end && n < e) prev = *n++ = *p++;
3362  while (*p) {
3363  if (*p == '_') {
3364  /* remove an underscore between digits */
3365  if (n == buf || !ISDIGIT(prev) || (++p, !ISDIGIT(*p))) {
3366  if (badcheck) goto bad;
3367  break;
3368  }
3369  }
3370  prev = *p++;
3371  if (e == init_e && (prev == 'e' || prev == 'E' || prev == 'p' || prev == 'P')) {
3372  e = buf + sizeof(buf) - 1;
3373  *n++ = prev;
3374  switch (*p) {case '+': case '-': prev = *n++ = *p++;}
3375  if (*p == '0') {
3376  prev = *n++ = '0';
3377  while (*++p == '0');
3378  }
3379  continue;
3380  }
3381  else if (ISSPACE(prev)) {
3382  while (ISSPACE(*p)) ++p;
3383  if (*p) {
3384  if (badcheck) goto bad;
3385  break;
3386  }
3387  }
3388  else if (prev == '.' ? dot_seen++ : !ISDIGIT(prev)) {
3389  if (badcheck) goto bad;
3390  break;
3391  }
3392  if (n < e) *n++ = prev;
3393  }
3394  *n = '\0';
3395  p = buf;
3396 
3397  if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
3398  return 0.0;
3399  }
3400 
3401  d = strtod(p, &end);
3402  if (errno == ERANGE) {
3403  OutOfRange();
3404  rb_warning("Float %.*s%s out of range", w, p, ellipsis);
3405  errno = 0;
3406  }
3407  if (badcheck) {
3408  if (!end || p == end) goto bad;
3409  while (*end && ISSPACE(*end)) end++;
3410  if (*end) goto bad;
3411  }
3412  }
3413  if (errno == ERANGE) {
3414  errno = 0;
3415  OutOfRange();
3416  rb_raise(rb_eArgError, "Float %.*s%s out of range", w, q, ellipsis);
3417  }
3418  return d;
3419 
3420  bad:
3421  if (raise) {
3422  VALUE s = rb_enc_str_new_cstr(q, enc);
3423  rb_raise(rb_eArgError, "invalid value for Float(): %+"PRIsVALUE, s);
3424  UNREACHABLE_RETURN(nan(""));
3425  }
3426  else {
3427  if (error) *error = 1;
3428  return 0.0;
3429  }
3430 }
3431 
3432 double
3433 rb_cstr_to_dbl(const char *p, int badcheck)
3434 {
3435  return rb_cstr_to_dbl_raise(p, NULL, badcheck, TRUE, NULL);
3436 }
3437 
3438 static double
3439 rb_str_to_dbl_raise(VALUE str, int badcheck, int raise, int *error)
3440 {
3441  char *s;
3442  long len;
3443  double ret;
3444  VALUE v = 0;
3445 
3446  StringValue(str);
3447  rb_must_asciicompat(str);
3448  s = RSTRING_PTR(str);
3449  len = RSTRING_LEN(str);
3450  if (s) {
3451  if (badcheck && memchr(s, '\0', len)) {
3452  if (raise)
3453  rb_raise(rb_eArgError, "string for Float contains null byte");
3454  else {
3455  if (error) *error = 1;
3456  return 0.0;
3457  }
3458  }
3459  if (s[len]) { /* no sentinel somehow */
3460  char *p = ALLOCV(v, (size_t)len + 1);
3461  MEMCPY(p, s, char, len);
3462  p[len] = '\0';
3463  s = p;
3464  }
3465  }
3466  ret = rb_cstr_to_dbl_raise(s, rb_enc_get(str), badcheck, raise, error);
3467  if (v)
3468  ALLOCV_END(v);
3469  else
3470  RB_GC_GUARD(str);
3471  return ret;
3472 }
3473 
3474 FUNC_MINIMIZED(double rb_str_to_dbl(VALUE str, int badcheck));
3475 
3476 double
3477 rb_str_to_dbl(VALUE str, int badcheck)
3478 {
3479  return rb_str_to_dbl_raise(str, badcheck, TRUE, NULL);
3480 }
3481 
3483 #define fix2dbl_without_to_f(x) (double)FIX2LONG(x)
3484 #define big2dbl_without_to_f(x) rb_big2dbl(x)
3485 #define int2dbl_without_to_f(x) \
3486  (FIXNUM_P(x) ? fix2dbl_without_to_f(x) : big2dbl_without_to_f(x))
3487 #define num2dbl_without_to_f(x) \
3488  (FIXNUM_P(x) ? fix2dbl_without_to_f(x) : \
3489  RB_BIGNUM_TYPE_P(x) ? big2dbl_without_to_f(x) : \
3490  (Check_Type(x, T_FLOAT), RFLOAT_VALUE(x)))
3491 static inline double
3492 rat2dbl_without_to_f(VALUE x)
3493 {
3494  VALUE num = rb_rational_num(x);
3495  VALUE den = rb_rational_den(x);
3496  return num2dbl_without_to_f(num) / num2dbl_without_to_f(den);
3497 }
3498 
3499 #define special_const_to_float(val, pre, post) \
3500  switch (val) { \
3501  case Qnil: \
3502  rb_raise_static(rb_eTypeError, pre "nil" post); \
3503  case Qtrue: \
3504  rb_raise_static(rb_eTypeError, pre "true" post); \
3505  case Qfalse: \
3506  rb_raise_static(rb_eTypeError, pre "false" post); \
3507  }
3510 static inline void
3511 conversion_to_float(VALUE val)
3512 {
3513  special_const_to_float(val, "can't convert ", " into Float");
3514 }
3515 
3516 static inline void
3517 implicit_conversion_to_float(VALUE val)
3518 {
3519  special_const_to_float(val, "no implicit conversion to float from ", "");
3520 }
3521 
3522 static int
3523 to_float(VALUE *valp, int raise_exception)
3524 {
3525  VALUE val = *valp;
3526  if (SPECIAL_CONST_P(val)) {
3527  if (FIXNUM_P(val)) {
3528  *valp = DBL2NUM(fix2dbl_without_to_f(val));
3529  return T_FLOAT;
3530  }
3531  else if (FLONUM_P(val)) {
3532  return T_FLOAT;
3533  }
3534  else if (raise_exception) {
3535  conversion_to_float(val);
3536  }
3537  }
3538  else {
3539  int type = BUILTIN_TYPE(val);
3540  switch (type) {
3541  case T_FLOAT:
3542  return T_FLOAT;
3543  case T_BIGNUM:
3544  *valp = DBL2NUM(big2dbl_without_to_f(val));
3545  return T_FLOAT;
3546  case T_RATIONAL:
3547  *valp = DBL2NUM(rat2dbl_without_to_f(val));
3548  return T_FLOAT;
3549  case T_STRING:
3550  return T_STRING;
3551  }
3552  }
3553  return T_NONE;
3554 }
3555 
3556 static VALUE
3557 convert_type_to_float_protected(VALUE val)
3558 {
3559  return rb_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
3560 }
3561 
3562 static VALUE
3563 rb_convert_to_float(VALUE val, int raise_exception)
3564 {
3565  switch (to_float(&val, raise_exception)) {
3566  case T_FLOAT:
3567  return val;
3568  case T_STRING:
3569  if (!raise_exception) {
3570  int e = 0;
3571  double x = rb_str_to_dbl_raise(val, TRUE, raise_exception, &e);
3572  return e ? Qnil : DBL2NUM(x);
3573  }
3574  return DBL2NUM(rb_str_to_dbl(val, TRUE));
3575  case T_NONE:
3576  if (SPECIAL_CONST_P(val) && !raise_exception)
3577  return Qnil;
3578  }
3579 
3580  if (!raise_exception) {
3581  int state;
3582  VALUE result = rb_protect(convert_type_to_float_protected, val, &state);
3583  if (state) rb_set_errinfo(Qnil);
3584  return result;
3585  }
3586 
3587  return rb_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
3588 }
3589 
3590 FUNC_MINIMIZED(VALUE rb_Float(VALUE val));
3591 
3592 VALUE
3594 {
3595  return rb_convert_to_float(val, TRUE);
3596 }
3597 
3598 static VALUE
3599 rb_f_float1(rb_execution_context_t *ec, VALUE obj, VALUE arg)
3600 {
3601  return rb_convert_to_float(arg, TRUE);
3602 }
3603 
3604 static VALUE
3605 rb_f_float(rb_execution_context_t *ec, VALUE obj, VALUE arg, VALUE opts)
3606 {
3607  int exception = rb_bool_expected(opts, "exception", TRUE);
3608  return rb_convert_to_float(arg, exception);
3609 }
3610 
3611 static VALUE
3612 numeric_to_float(VALUE val)
3613 {
3614  if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
3615  rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into Float",
3616  rb_obj_class(val));
3617  }
3618  return rb_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
3619 }
3620 
3621 VALUE
3623 {
3624  switch (to_float(&val, TRUE)) {
3625  case T_FLOAT:
3626  return val;
3627  }
3628  return numeric_to_float(val);
3629 }
3630 
3631 VALUE
3633 {
3634  if (RB_FLOAT_TYPE_P(val)) return val;
3635  if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
3636  return Qnil;
3637  }
3638  return rb_check_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
3639 }
3640 
3641 static inline int
3642 basic_to_f_p(VALUE klass)
3643 {
3644  return rb_method_basic_definition_p(klass, id_to_f);
3645 }
3646 
3648 double
3649 rb_num_to_dbl(VALUE val)
3650 {
3651  if (SPECIAL_CONST_P(val)) {
3652  if (FIXNUM_P(val)) {
3653  if (basic_to_f_p(rb_cInteger))
3654  return fix2dbl_without_to_f(val);
3655  }
3656  else if (FLONUM_P(val)) {
3657  return rb_float_flonum_value(val);
3658  }
3659  else {
3660  conversion_to_float(val);
3661  }
3662  }
3663  else {
3664  switch (BUILTIN_TYPE(val)) {
3665  case T_FLOAT:
3666  return rb_float_noflonum_value(val);
3667  case T_BIGNUM:
3668  if (basic_to_f_p(rb_cInteger))
3669  return big2dbl_without_to_f(val);
3670  break;
3671  case T_RATIONAL:
3672  if (basic_to_f_p(rb_cRational))
3673  return rat2dbl_without_to_f(val);
3674  break;
3675  default:
3676  break;
3677  }
3678  }
3679  val = numeric_to_float(val);
3680  return RFLOAT_VALUE(val);
3681 }
3682 
3683 double
3685 {
3686  if (SPECIAL_CONST_P(val)) {
3687  if (FIXNUM_P(val)) {
3688  return fix2dbl_without_to_f(val);
3689  }
3690  else if (FLONUM_P(val)) {
3691  return rb_float_flonum_value(val);
3692  }
3693  else {
3694  implicit_conversion_to_float(val);
3695  }
3696  }
3697  else {
3698  switch (BUILTIN_TYPE(val)) {
3699  case T_FLOAT:
3700  return rb_float_noflonum_value(val);
3701  case T_BIGNUM:
3702  return big2dbl_without_to_f(val);
3703  case T_RATIONAL:
3704  return rat2dbl_without_to_f(val);
3705  case T_STRING:
3706  rb_raise(rb_eTypeError, "no implicit conversion to float from string");
3707  default:
3708  break;
3709  }
3710  }
3711  val = rb_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
3712  return RFLOAT_VALUE(val);
3713 }
3714 
3715 VALUE
3717 {
3718  VALUE tmp = rb_check_string_type(val);
3719  if (NIL_P(tmp))
3720  tmp = rb_convert_type_with_id(val, T_STRING, "String", idTo_s);
3721  return tmp;
3722 }
3723 
3724 
3725 /*
3726  * call-seq:
3727  * String(object) -> object or new_string
3728  *
3729  * Returns a string converted from +object+.
3730  *
3731  * Tries to convert +object+ to a string
3732  * using +to_str+ first and +to_s+ second:
3733  *
3734  * String([0, 1, 2]) # => "[0, 1, 2]"
3735  * String(0..5) # => "0..5"
3736  * String({foo: 0, bar: 1}) # => "{:foo=>0, :bar=>1}"
3737  *
3738  * Raises +TypeError+ if +object+ cannot be converted to a string.
3739  */
3740 
3741 static VALUE
3742 rb_f_string(VALUE obj, VALUE arg)
3743 {
3744  return rb_String(arg);
3745 }
3746 
3747 VALUE
3749 {
3750  VALUE tmp = rb_check_array_type(val);
3751 
3752  if (NIL_P(tmp)) {
3753  tmp = rb_check_to_array(val);
3754  if (NIL_P(tmp)) {
3755  return rb_ary_new3(1, val);
3756  }
3757  }
3758  return tmp;
3759 }
3760 
3761 /*
3762  * call-seq:
3763  * Array(object) -> object or new_array
3764  *
3765  * Returns an array converted from +object+.
3766  *
3767  * Tries to convert +object+ to an array
3768  * using +to_ary+ first and +to_a+ second:
3769  *
3770  * Array([0, 1, 2]) # => [0, 1, 2]
3771  * Array({foo: 0, bar: 1}) # => [[:foo, 0], [:bar, 1]]
3772  * Array(0..4) # => [0, 1, 2, 3, 4]
3773  *
3774  * Returns +object+ in an array, <tt>[object]</tt>,
3775  * if +object+ cannot be converted:
3776  *
3777  * Array(:foo) # => [:foo]
3778  *
3779  */
3780 
3781 static VALUE
3782 rb_f_array(VALUE obj, VALUE arg)
3783 {
3784  return rb_Array(arg);
3785 }
3786 
3790 VALUE
3792 {
3793  VALUE tmp;
3794 
3795  if (NIL_P(val)) return rb_hash_new();
3796  tmp = rb_check_hash_type(val);
3797  if (NIL_P(tmp)) {
3798  if (RB_TYPE_P(val, T_ARRAY) && RARRAY_LEN(val) == 0)
3799  return rb_hash_new();
3800  rb_raise(rb_eTypeError, "can't convert %s into Hash", rb_obj_classname(val));
3801  }
3802  return tmp;
3803 }
3804 
3805 /*
3806  * call-seq:
3807  * Hash(object) -> object or new_hash
3808  *
3809  * Returns a hash converted from +object+.
3810  *
3811  * - If +object+ is:
3812  *
3813  * - A hash, returns +object+.
3814  * - An empty array or +nil+, returns an empty hash.
3815  *
3816  * - Otherwise, if <tt>object.to_hash</tt> returns a hash, returns that hash.
3817  * - Otherwise, returns TypeError.
3818  *
3819  * Examples:
3820  *
3821  * Hash({foo: 0, bar: 1}) # => {:foo=>0, :bar=>1}
3822  * Hash(nil) # => {}
3823  * Hash([]) # => {}
3824  *
3825  */
3826 
3827 static VALUE
3828 rb_f_hash(VALUE obj, VALUE arg)
3829 {
3830  return rb_Hash(arg);
3831 }
3832 
3834 struct dig_method {
3835  VALUE klass;
3836  int basic;
3837 };
3838 
3839 static ID id_dig;
3840 
3841 static int
3842 dig_basic_p(VALUE obj, struct dig_method *cache)
3843 {
3844  VALUE klass = RBASIC_CLASS(obj);
3845  if (klass != cache->klass) {
3846  cache->klass = klass;
3847  cache->basic = rb_method_basic_definition_p(klass, id_dig);
3848  }
3849  return cache->basic;
3850 }
3851 
3852 static void
3853 no_dig_method(int found, VALUE recv, ID mid, int argc, const VALUE *argv, VALUE data)
3854 {
3855  if (!found) {
3856  rb_raise(rb_eTypeError, "%"PRIsVALUE" does not have #dig method",
3857  CLASS_OF(data));
3858  }
3859 }
3860 
3862 VALUE
3863 rb_obj_dig(int argc, VALUE *argv, VALUE obj, VALUE notfound)
3864 {
3865  struct dig_method hash = {Qnil}, ary = {Qnil}, strt = {Qnil};
3866 
3867  for (; argc > 0; ++argv, --argc) {
3868  if (NIL_P(obj)) return notfound;
3869  if (!SPECIAL_CONST_P(obj)) {
3870  switch (BUILTIN_TYPE(obj)) {
3871  case T_HASH:
3872  if (dig_basic_p(obj, &hash)) {
3873  obj = rb_hash_aref(obj, *argv);
3874  continue;
3875  }
3876  break;
3877  case T_ARRAY:
3878  if (dig_basic_p(obj, &ary)) {
3879  obj = rb_ary_at(obj, *argv);
3880  continue;
3881  }
3882  break;
3883  case T_STRUCT:
3884  if (dig_basic_p(obj, &strt)) {
3885  obj = rb_struct_lookup(obj, *argv);
3886  continue;
3887  }
3888  break;
3889  default:
3890  break;
3891  }
3892  }
3893  return rb_check_funcall_with_hook_kw(obj, id_dig, argc, argv,
3894  no_dig_method, obj,
3895  RB_NO_KEYWORDS);
3896  }
3897  return obj;
3898 }
3899 
3900 /*
3901  * call-seq:
3902  * sprintf(format_string *objects) -> string
3903  *
3904  * Returns the string resulting from formatting +objects+
3905  * into +format_string+.
3906  *
3907  * For details on +format_string+, see
3908  * {Format Specifications}[rdoc-ref:format_specifications.rdoc].
3909  */
3910 
3911 static VALUE
3912 f_sprintf(int c, const VALUE *v, VALUE _)
3913 {
3914  return rb_f_sprintf(c, v);
3915 }
3916 
3917 static VALUE
3918 rb_f_loop_size(VALUE self, VALUE args, VALUE eobj)
3919 {
3920  return DBL2NUM(HUGE_VAL);
3921 }
3922 
3923 /*
3924  * Document-class: Class
3925  *
3926  * Classes in Ruby are first-class objects---each is an instance of
3927  * class Class.
3928  *
3929  * Typically, you create a new class by using:
3930  *
3931  * class Name
3932  * # some code describing the class behavior
3933  * end
3934  *
3935  * When a new class is created, an object of type Class is initialized and
3936  * assigned to a global constant (Name in this case).
3937  *
3938  * When <code>Name.new</code> is called to create a new object, the
3939  * #new method in Class is run by default.
3940  * This can be demonstrated by overriding #new in Class:
3941  *
3942  * class Class
3943  * alias old_new new
3944  * def new(*args)
3945  * print "Creating a new ", self.name, "\n"
3946  * old_new(*args)
3947  * end
3948  * end
3949  *
3950  * class Name
3951  * end
3952  *
3953  * n = Name.new
3954  *
3955  * <em>produces:</em>
3956  *
3957  * Creating a new Name
3958  *
3959  * Classes, modules, and objects are interrelated. In the diagram
3960  * that follows, the vertical arrows represent inheritance, and the
3961  * parentheses metaclasses. All metaclasses are instances
3962  * of the class `Class'.
3963  * +---------+ +-...
3964  * | | |
3965  * BasicObject-----|-->(BasicObject)-------|-...
3966  * ^ | ^ |
3967  * | | | |
3968  * Object---------|----->(Object)---------|-...
3969  * ^ | ^ |
3970  * | | | |
3971  * +-------+ | +--------+ |
3972  * | | | | | |
3973  * | Module-|---------|--->(Module)-|-...
3974  * | ^ | | ^ |
3975  * | | | | | |
3976  * | Class-|---------|---->(Class)-|-...
3977  * | ^ | | ^ |
3978  * | +---+ | +----+
3979  * | |
3980  * obj--->OtherClass---------->(OtherClass)-----------...
3981  *
3982  */
3983 
3984 
3985 /*
3986  * Document-class: BasicObject
3987  *
3988  * +BasicObject+ is the parent class of all classes in Ruby.
3989  * In particular, +BasicObject+ is the parent class of class Object,
3990  * which is itself the default parent class of every Ruby class:
3991  *
3992  * class Foo; end
3993  * Foo.superclass # => Object
3994  * Object.superclass # => BasicObject
3995  *
3996  * +BasicObject+ is the only class that has no parent:
3997  *
3998  * BasicObject.superclass # => nil
3999  *
4000  * \Class +BasicObject+ can be used to create an object hierarchy
4001  * (e.g., class Delegator) that is independent of Ruby's object hierarchy.
4002  * Such objects:
4003  *
4004  * - Do not have namespace "pollution" from the many methods
4005  * provided in class Object and its included module Kernel.
4006  * - Do not have definitions of common classes,
4007  * and so references to such common classes must be fully qualified
4008  * (+::String+, not +String+).
4009  *
4010  * A variety of strategies can be used to provide useful portions
4011  * of the Standard Library in subclasses of +BasicObject+:
4012  *
4013  * - The immediate subclass could <tt>include Kernel</tt>,
4014  * which would define methods such as +puts+, +exit+, etc.
4015  * - A custom Kernel-like module could be created and included.
4016  * - Delegation can be used via #method_missing:
4017  *
4018  * class MyObjectSystem < BasicObject
4019  * DELEGATE = [:puts, :p]
4020  *
4021  * def method_missing(name, *args, &block)
4022  * return super unless DELEGATE.include? name
4023  * ::Kernel.send(name, *args, &block)
4024  * end
4025  *
4026  * def respond_to_missing?(name, include_private = false)
4027  * DELEGATE.include?(name)
4028  * end
4029  * end
4030  *
4031  * === What's Here
4032  *
4033  * These are the methods defined for \BasicObject:
4034  *
4035  * - ::new: Returns a new \BasicObject instance.
4036  * - #!: Returns the boolean negation of +self+: +true+ or +false+.
4037  * - #!=: Returns whether +self+ and the given object are _not_ equal.
4038  * - #==: Returns whether +self+ and the given object are equivalent.
4039  * - #__id__: Returns the integer object identifier for +self+.
4040  * - #__send__: Calls the method identified by the given symbol.
4041  * - #equal?: Returns whether +self+ and the given object are the same object.
4042  * - #instance_eval: Evaluates the given string or block in the context of +self+.
4043  * - #instance_exec: Executes the given block in the context of +self+, passing the given arguments.
4044  * - #method_missing: Called when +self+ is called with a method it does not define.
4045  * - #singleton_method_added: Called when a singleton method is added to +self+.
4046  * - #singleton_method_removed: Called when a singleton method is removed from +self+.
4047  * - #singleton_method_undefined: Called when a singleton method is undefined in +self+.
4048  *
4049  */
4050 
4051 /* Document-class: Object
4052  *
4053  * Object is the default root of all Ruby objects. Object inherits from
4054  * BasicObject which allows creating alternate object hierarchies. Methods
4055  * on Object are available to all classes unless explicitly overridden.
4056  *
4057  * Object mixes in the Kernel module, making the built-in kernel functions
4058  * globally accessible. Although the instance methods of Object are defined
4059  * by the Kernel module, we have chosen to document them here for clarity.
4060  *
4061  * When referencing constants in classes inheriting from Object you do not
4062  * need to use the full namespace. For example, referencing +File+ inside
4063  * +YourClass+ will find the top-level File class.
4064  *
4065  * In the descriptions of Object's methods, the parameter <i>symbol</i> refers
4066  * to a symbol, which is either a quoted string or a Symbol (such as
4067  * <code>:name</code>).
4068  *
4069  * == What's Here
4070  *
4071  * First, what's elsewhere. \Class \Object:
4072  *
4073  * - Inherits from {class BasicObject}[rdoc-ref:BasicObject@What-27s+Here].
4074  * - Includes {module Kernel}[rdoc-ref:Kernel@What-27s+Here].
4075  *
4076  * Here, class \Object provides methods for:
4077  *
4078  * - {Querying}[rdoc-ref:Object@Querying]
4079  * - {Instance Variables}[rdoc-ref:Object@Instance+Variables]
4080  * - {Other}[rdoc-ref:Object@Other]
4081  *
4082  * === Querying
4083  *
4084  * - #!~: Returns +true+ if +self+ does not match the given object,
4085  * otherwise +false+.
4086  * - #<=>: Returns 0 if +self+ and the given object +object+ are the same
4087  * object, or if <tt>self == object</tt>; otherwise returns +nil+.
4088  * - #===: Implements case equality, effectively the same as calling #==.
4089  * - #eql?: Implements hash equality, effectively the same as calling #==.
4090  * - #kind_of? (aliased as #is_a?): Returns whether given argument is an ancestor
4091  * of the singleton class of +self+.
4092  * - #instance_of?: Returns whether +self+ is an instance of the given class.
4093  * - #instance_variable_defined?: Returns whether the given instance variable
4094  * is defined in +self+.
4095  * - #method: Returns the Method object for the given method in +self+.
4096  * - #methods: Returns an array of symbol names of public and protected methods
4097  * in +self+.
4098  * - #nil?: Returns +false+. (Only +nil+ responds +true+ to method <tt>nil?</tt>.)
4099  * - #object_id: Returns an integer corresponding to +self+ that is unique
4100  * for the current process
4101  * - #private_methods: Returns an array of the symbol names
4102  * of the private methods in +self+.
4103  * - #protected_methods: Returns an array of the symbol names
4104  * of the protected methods in +self+.
4105  * - #public_method: Returns the Method object for the given public method in +self+.
4106  * - #public_methods: Returns an array of the symbol names
4107  * of the public methods in +self+.
4108  * - #respond_to?: Returns whether +self+ responds to the given method.
4109  * - #singleton_class: Returns the singleton class of +self+.
4110  * - #singleton_method: Returns the Method object for the given singleton method
4111  * in +self+.
4112  * - #singleton_methods: Returns an array of the symbol names
4113  * of the singleton methods in +self+.
4114  *
4115  * - #define_singleton_method: Defines a singleton method in +self+
4116  * for the given symbol method-name and block or proc.
4117  * - #extend: Includes the given modules in the singleton class of +self+.
4118  * - #public_send: Calls the given public method in +self+ with the given argument.
4119  * - #send: Calls the given method in +self+ with the given argument.
4120  *
4121  * === Instance Variables
4122  *
4123  * - #instance_variable_get: Returns the value of the given instance variable
4124  * in +self+, or +nil+ if the instance variable is not set.
4125  * - #instance_variable_set: Sets the value of the given instance variable in +self+
4126  * to the given object.
4127  * - #instance_variables: Returns an array of the symbol names
4128  * of the instance variables in +self+.
4129  * - #remove_instance_variable: Removes the named instance variable from +self+.
4130  *
4131  * === Other
4132  *
4133  * - #clone: Returns a shallow copy of +self+, including singleton class
4134  * and frozen state.
4135  * - #define_singleton_method: Defines a singleton method in +self+
4136  * for the given symbol method-name and block or proc.
4137  * - #display: Prints +self+ to the given IO stream or <tt>$stdout</tt>.
4138  * - #dup: Returns a shallow unfrozen copy of +self+.
4139  * - #enum_for (aliased as #to_enum): Returns an Enumerator for +self+
4140  * using the using the given method, arguments, and block.
4141  * - #extend: Includes the given modules in the singleton class of +self+.
4142  * - #freeze: Prevents further modifications to +self+.
4143  * - #hash: Returns the integer hash value for +self+.
4144  * - #inspect: Returns a human-readable string representation of +self+.
4145  * - #itself: Returns +self+.
4146  * - #method_missing: Method called when an undefined method is called on +self+.
4147  * - #public_send: Calls the given public method in +self+ with the given argument.
4148  * - #send: Calls the given method in +self+ with the given argument.
4149  * - #to_s: Returns a string representation of +self+.
4150  *
4151  */
4152 
4153 void
4154 InitVM_Object(void)
4155 {
4157 
4158 #if 0
4159  // teach RDoc about these classes
4160  rb_cBasicObject = rb_define_class("BasicObject", Qnil);
4162  rb_cModule = rb_define_class("Module", rb_cObject);
4163  rb_cClass = rb_define_class("Class", rb_cModule);
4164  rb_cRefinement = rb_define_class("Refinement", rb_cModule);
4165 #endif
4166 
4167  rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_initialize, 0);
4168  rb_define_alloc_func(rb_cBasicObject, rb_class_allocate_instance);
4169  rb_define_method(rb_cBasicObject, "==", rb_obj_equal, 1);
4170  rb_define_method(rb_cBasicObject, "equal?", rb_obj_equal, 1);
4171  rb_define_method(rb_cBasicObject, "!", rb_obj_not, 0);
4172  rb_define_method(rb_cBasicObject, "!=", rb_obj_not_equal, 1);
4173 
4174  rb_define_private_method(rb_cBasicObject, "singleton_method_added", rb_obj_singleton_method_added, 1);
4175  rb_define_private_method(rb_cBasicObject, "singleton_method_removed", rb_obj_singleton_method_removed, 1);
4176  rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_singleton_method_undefined, 1);
4177 
4178  /* Document-module: Kernel
4179  *
4180  * The Kernel module is included by class Object, so its methods are
4181  * available in every Ruby object.
4182  *
4183  * The Kernel instance methods are documented in class Object while the
4184  * module methods are documented here. These methods are called without a
4185  * receiver and thus can be called in functional form:
4186  *
4187  * sprintf "%.1f", 1.234 #=> "1.2"
4188  *
4189  * == What's Here
4190  *
4191  * \Module \Kernel provides methods that are useful for:
4192  *
4193  * - {Converting}[rdoc-ref:Kernel@Converting]
4194  * - {Querying}[rdoc-ref:Kernel@Querying]
4195  * - {Exiting}[rdoc-ref:Kernel@Exiting]
4196  * - {Exceptions}[rdoc-ref:Kernel@Exceptions]
4197  * - {IO}[rdoc-ref:Kernel@IO]
4198  * - {Procs}[rdoc-ref:Kernel@Procs]
4199  * - {Tracing}[rdoc-ref:Kernel@Tracing]
4200  * - {Subprocesses}[rdoc-ref:Kernel@Subprocesses]
4201  * - {Loading}[rdoc-ref:Kernel@Loading]
4202  * - {Yielding}[rdoc-ref:Kernel@Yielding]
4203  * - {Random Values}[rdoc-ref:Kernel@Random+Values]
4204  * - {Other}[rdoc-ref:Kernel@Other]
4205  *
4206  * === Converting
4207  *
4208  * - #Array: Returns an Array based on the given argument.
4209  * - #Complex: Returns a Complex based on the given arguments.
4210  * - #Float: Returns a Float based on the given arguments.
4211  * - #Hash: Returns a Hash based on the given argument.
4212  * - #Integer: Returns an Integer based on the given arguments.
4213  * - #Rational: Returns a Rational based on the given arguments.
4214  * - #String: Returns a String based on the given argument.
4215  *
4216  * === Querying
4217  *
4218  * - #__callee__: Returns the called name of the current method as a symbol.
4219  * - #__dir__: Returns the path to the directory from which the current
4220  * method is called.
4221  * - #__method__: Returns the name of the current method as a symbol.
4222  * - #autoload?: Returns the file to be loaded when the given module is referenced.
4223  * - #binding: Returns a Binding for the context at the point of call.
4224  * - #block_given?: Returns +true+ if a block was passed to the calling method.
4225  * - #caller: Returns the current execution stack as an array of strings.
4226  * - #caller_locations: Returns the current execution stack as an array
4227  * of Thread::Backtrace::Location objects.
4228  * - #class: Returns the class of +self+.
4229  * - #frozen?: Returns whether +self+ is frozen.
4230  * - #global_variables: Returns an array of global variables as symbols.
4231  * - #local_variables: Returns an array of local variables as symbols.
4232  * - #test: Performs specified tests on the given single file or pair of files.
4233  *
4234  * === Exiting
4235  *
4236  * - #abort: Exits the current process after printing the given arguments.
4237  * - #at_exit: Executes the given block when the process exits.
4238  * - #exit: Exits the current process after calling any registered
4239  * +at_exit+ handlers.
4240  * - #exit!: Exits the current process without calling any registered
4241  * +at_exit+ handlers.
4242  *
4243  * === Exceptions
4244  *
4245  * - #catch: Executes the given block, possibly catching a thrown object.
4246  * - #raise (aliased as #fail): Raises an exception based on the given arguments.
4247  * - #throw: Returns from the active catch block waiting for the given tag.
4248  *
4249  *
4250  * === \IO
4251  *
4252  * - ::pp: Prints the given objects in pretty form.
4253  * - #gets: Returns and assigns to <tt>$_</tt> the next line from the current input.
4254  * - #open: Creates an IO object connected to the given stream, file, or subprocess.
4255  * - #p: Prints the given objects' inspect output to the standard output.
4256  * - #print: Prints the given objects to standard output without a newline.
4257  * - #printf: Prints the string resulting from applying the given format string
4258  * to any additional arguments.
4259  * - #putc: Equivalent to <tt.$stdout.putc(object)</tt> for the given object.
4260  * - #puts: Equivalent to <tt>$stdout.puts(*objects)</tt> for the given objects.
4261  * - #readline: Similar to #gets, but raises an exception at the end of file.
4262  * - #readlines: Returns an array of the remaining lines from the current input.
4263  * - #select: Same as IO.select.
4264  *
4265  * === Procs
4266  *
4267  * - #lambda: Returns a lambda proc for the given block.
4268  * - #proc: Returns a new Proc; equivalent to Proc.new.
4269  *
4270  * === Tracing
4271  *
4272  * - #set_trace_func: Sets the given proc as the handler for tracing,
4273  * or disables tracing if given +nil+.
4274  * - #trace_var: Starts tracing assignments to the given global variable.
4275  * - #untrace_var: Disables tracing of assignments to the given global variable.
4276  *
4277  * === Subprocesses
4278  *
4279  * - {\`command`}[rdoc-ref:Kernel#`]: Returns the standard output of running
4280  * +command+ in a subshell.
4281  * - #exec: Replaces current process with a new process.
4282  * - #fork: Forks the current process into two processes.
4283  * - #spawn: Executes the given command and returns its pid without waiting
4284  * for completion.
4285  * - #system: Executes the given command in a subshell.
4286  *
4287  * === Loading
4288  *
4289  * - #autoload: Registers the given file to be loaded when the given constant
4290  * is first referenced.
4291  * - #load: Loads the given Ruby file.
4292  * - #require: Loads the given Ruby file unless it has already been loaded.
4293  * - #require_relative: Loads the Ruby file path relative to the calling file,
4294  * unless it has already been loaded.
4295  *
4296  * === Yielding
4297  *
4298  * - #tap: Yields +self+ to the given block; returns +self+.
4299  * - #then (aliased as #yield_self): Yields +self+ to the block
4300  * and returns the result of the block.
4301  *
4302  * === \Random Values
4303  *
4304  * - #rand: Returns a pseudo-random floating point number
4305  * strictly between 0.0 and 1.0.
4306  * - #srand: Seeds the pseudo-random number generator with the given number.
4307  *
4308  * === Other
4309  *
4310  * - #eval: Evaluates the given string as Ruby code.
4311  * - #loop: Repeatedly executes the given block.
4312  * - #sleep: Suspends the current thread for the given number of seconds.
4313  * - #sprintf (aliased as #format): Returns the string resulting from applying
4314  * the given format string to any additional arguments.
4315  * - #syscall: Runs an operating system call.
4316  * - #trap: Specifies the handling of system signals.
4317  * - #warn: Issue a warning based on the given messages and options.
4318  *
4319  */
4320  rb_mKernel = rb_define_module("Kernel");
4322  rb_define_private_method(rb_cClass, "inherited", rb_obj_class_inherited, 1);
4323  rb_define_private_method(rb_cModule, "included", rb_obj_mod_included, 1);
4324  rb_define_private_method(rb_cModule, "extended", rb_obj_mod_extended, 1);
4325  rb_define_private_method(rb_cModule, "prepended", rb_obj_mod_prepended, 1);
4326  rb_define_private_method(rb_cModule, "method_added", rb_obj_mod_method_added, 1);
4327  rb_define_private_method(rb_cModule, "const_added", rb_obj_mod_const_added, 1);
4328  rb_define_private_method(rb_cModule, "method_removed", rb_obj_mod_method_removed, 1);
4329  rb_define_private_method(rb_cModule, "method_undefined", rb_obj_mod_method_undefined, 1);
4330 
4331  rb_define_method(rb_mKernel, "nil?", rb_false, 0);
4332  rb_define_method(rb_mKernel, "===", case_equal, 1);
4333  rb_define_method(rb_mKernel, "!~", rb_obj_not_match, 1);
4334  rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
4335  rb_define_method(rb_mKernel, "hash", rb_obj_hash, 0); /* in hash.c */
4336  rb_define_method(rb_mKernel, "<=>", rb_obj_cmp, 1);
4337 
4338  rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
4340  rb_define_method(rb_mKernel, "itself", rb_obj_itself, 0);
4341  rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
4342  rb_define_method(rb_mKernel, "initialize_dup", rb_obj_init_dup_clone, 1);
4343  rb_define_method(rb_mKernel, "initialize_clone", rb_obj_init_clone, -1);
4344 
4345  rb_define_method(rb_mKernel, "freeze", rb_obj_freeze, 0);
4346 
4348  rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
4349  rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1); /* in class.c */
4350  rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1); /* in class.c */
4351  rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1); /* in class.c */
4352  rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1); /* in class.c */
4353  rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1); /* in class.c */
4354  rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); /* in variable.c */
4355  rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
4356  rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set_m, 2);
4357  rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1);
4358  rb_define_method(rb_mKernel, "remove_instance_variable",
4359  rb_obj_remove_instance_variable, 1); /* in variable.c */
4360 
4361  rb_define_method(rb_mKernel, "instance_of?", rb_obj_is_instance_of, 1);
4364 
4365  rb_define_global_function("sprintf", f_sprintf, -1);
4366  rb_define_global_function("format", f_sprintf, -1);
4367 
4368  rb_define_global_function("String", rb_f_string, 1);
4369  rb_define_global_function("Array", rb_f_array, 1);
4370  rb_define_global_function("Hash", rb_f_hash, 1);
4371 
4372  rb_cNilClass = rb_define_class("NilClass", rb_cObject);
4373  rb_cNilClass_to_s = rb_fstring_enc_lit("", rb_usascii_encoding());
4374  rb_vm_register_global_object(rb_cNilClass_to_s);
4375  rb_define_method(rb_cNilClass, "to_s", rb_nil_to_s, 0);
4376  rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
4377  rb_define_method(rb_cNilClass, "to_h", nil_to_h, 0);
4378  rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
4379  rb_define_method(rb_cNilClass, "=~", nil_match, 1);
4380  rb_define_method(rb_cNilClass, "&", false_and, 1);
4381  rb_define_method(rb_cNilClass, "|", false_or, 1);
4382  rb_define_method(rb_cNilClass, "^", false_xor, 1);
4383  rb_define_method(rb_cNilClass, "===", case_equal, 1);
4384 
4385  rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
4388 
4389  rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
4390  rb_define_method(rb_cModule, "===", rb_mod_eqq, 1);
4391  rb_define_method(rb_cModule, "==", rb_obj_equal, 1);
4392  rb_define_method(rb_cModule, "<=>", rb_mod_cmp, 1);
4393  rb_define_method(rb_cModule, "<", rb_mod_lt, 1);
4395  rb_define_method(rb_cModule, ">", rb_mod_gt, 1);
4396  rb_define_method(rb_cModule, ">=", rb_mod_ge, 1);
4397  rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */
4398  rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0);
4399  rb_define_alias(rb_cModule, "inspect", "to_s");
4400  rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0); /* in class.c */
4401  rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1); /* in class.c */
4402  rb_define_method(rb_cModule, "name", rb_mod_name, 0); /* in variable.c */
4403  rb_define_method(rb_cModule, "set_temporary_name", rb_mod_set_temporary_name, 1); /* in variable.c */
4404  rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0); /* in class.c */
4405 
4406  rb_define_method(rb_cModule, "attr", rb_mod_attr, -1);
4407  rb_define_method(rb_cModule, "attr_reader", rb_mod_attr_reader, -1);
4408  rb_define_method(rb_cModule, "attr_writer", rb_mod_attr_writer, -1);
4409  rb_define_method(rb_cModule, "attr_accessor", rb_mod_attr_accessor, -1);
4410 
4411  rb_define_alloc_func(rb_cModule, rb_module_s_alloc);
4413  rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
4414  rb_define_method(rb_cModule, "initialize_clone", rb_mod_initialize_clone, -1);
4415  rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1); /* in class.c */
4416  rb_define_method(rb_cModule, "public_instance_methods",
4417  rb_class_public_instance_methods, -1); /* in class.c */
4418  rb_define_method(rb_cModule, "protected_instance_methods",
4419  rb_class_protected_instance_methods, -1); /* in class.c */
4420  rb_define_method(rb_cModule, "private_instance_methods",
4421  rb_class_private_instance_methods, -1); /* in class.c */
4422  rb_define_method(rb_cModule, "undefined_instance_methods",
4423  rb_class_undefined_instance_methods, 0); /* in class.c */
4424 
4425  rb_define_method(rb_cModule, "constants", rb_mod_constants, -1); /* in variable.c */
4426  rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
4427  rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
4428  rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
4429  rb_define_method(rb_cModule, "const_source_location", rb_mod_const_source_location, -1);
4430  rb_define_private_method(rb_cModule, "remove_const",
4431  rb_mod_remove_const, 1); /* in variable.c */
4432  rb_define_method(rb_cModule, "const_missing",
4433  rb_mod_const_missing, 1); /* in variable.c */
4434  rb_define_method(rb_cModule, "class_variables",
4435  rb_mod_class_variables, -1); /* in variable.c */
4436  rb_define_method(rb_cModule, "remove_class_variable",
4437  rb_mod_remove_cvar, 1); /* in variable.c */
4438  rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
4439  rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
4440  rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
4441  rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1); /* in variable.c */
4442  rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1); /* in variable.c */
4443  rb_define_method(rb_cModule, "deprecate_constant", rb_mod_deprecate_constant, -1); /* in variable.c */
4444  rb_define_method(rb_cModule, "singleton_class?", rb_mod_singleton_p, 0);
4445 
4446  rb_define_method(rb_singleton_class(rb_cClass), "allocate", rb_class_alloc_m, 0);
4447  rb_define_method(rb_cClass, "allocate", rb_class_alloc_m, 0);
4449  rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
4450  rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
4451  rb_define_method(rb_cClass, "subclasses", rb_class_subclasses, 0); /* in class.c */
4452  rb_define_method(rb_cClass, "attached_object", rb_class_attached_object, 0); /* in class.c */
4453  rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
4454  rb_undef_method(rb_cClass, "extend_object");
4455  rb_undef_method(rb_cClass, "append_features");
4456  rb_undef_method(rb_cClass, "prepend_features");
4457 
4458  rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
4459  rb_cTrueClass_to_s = rb_fstring_enc_lit("true", rb_usascii_encoding());
4460  rb_vm_register_global_object(rb_cTrueClass_to_s);
4461  rb_define_method(rb_cTrueClass, "to_s", rb_true_to_s, 0);
4462  rb_define_alias(rb_cTrueClass, "inspect", "to_s");
4463  rb_define_method(rb_cTrueClass, "&", true_and, 1);
4464  rb_define_method(rb_cTrueClass, "|", true_or, 1);
4465  rb_define_method(rb_cTrueClass, "^", true_xor, 1);
4466  rb_define_method(rb_cTrueClass, "===", case_equal, 1);
4469 
4470  rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
4471  rb_cFalseClass_to_s = rb_fstring_enc_lit("false", rb_usascii_encoding());
4472  rb_vm_register_global_object(rb_cFalseClass_to_s);
4473  rb_define_method(rb_cFalseClass, "to_s", rb_false_to_s, 0);
4474  rb_define_alias(rb_cFalseClass, "inspect", "to_s");
4475  rb_define_method(rb_cFalseClass, "&", false_and, 1);
4476  rb_define_method(rb_cFalseClass, "|", false_or, 1);
4477  rb_define_method(rb_cFalseClass, "^", false_xor, 1);
4478  rb_define_method(rb_cFalseClass, "===", case_equal, 1);
4481 }
4482 
4483 #include "kernel.rbinc"
4484 #include "nilclass.rbinc"
4485 
4486 void
4487 Init_Object(void)
4488 {
4489  id_dig = rb_intern_const("dig");
4490  InitVM(Object);
4491 }
4492 
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition: assert.h:219
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
Definition: cxxanyargs.hpp:677
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
Definition: fl_type.h:898
@ RUBY_FL_PROMOTED
Ruby objects are "generational".
Definition: fl_type.h:218
@ RUBY_FL_SEEN_OBJ_ID
This flag has something to do with object IDs.
Definition: fl_type.h:300
VALUE rb_class_protected_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are protected only.
Definition: class.c:1912
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition: class.c:1187
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:980
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
Definition: class.c:1692
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition: class.c:2297
void Init_class_hierarchy(void)
Internal header aggregating init functions.
Definition: class.c:899
VALUE rb_class_attached_object(VALUE klass)
Returns the attached object for a singleton class.
Definition: class.c:1715
VALUE rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
Identical to rb_class_instance_methods(), except it returns names of singleton methods instead of ins...
Definition: class.c:2089
VALUE rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
Generates an array of symbols, which are the list of method names defined in the passed class.
Definition: class.c:1897
void rb_check_inheritable(VALUE super)
Asserts that the given class can derive a child class.
Definition: class.c:344
VALUE rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are public only.
Definition: class.c:1950
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition: class.c:1095
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
Definition: class.c:709
VALUE rb_mod_included_modules(VALUE mod)
Queries the list of included modules.
Definition: class.c:1510
VALUE rb_mod_ancestors(VALUE mod)
Queries the module's ancestors.
Definition: class.c:1578
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition: class.c:971
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Queries if the passed module is included by the module.
Definition: class.c:1546
VALUE rb_class_private_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are private only.
Definition: class.c:1935
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
The comment that comes with this function says :nodoc:.
Definition: class.c:533
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:2345
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition: class.c:2166
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition: class.c:2635
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
Definition: class.c:2142
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:916
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition: class.c:2424
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:2339
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
Definition: value_type.h:59
#define TYPE(_)
Old name of rb_type.
Definition: value_type.h:108
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
Definition: value_type.h:87
#define FL_EXIVAR
Old name of RUBY_FL_EXIVAR.
Definition: fl_type.h:66
#define ALLOCV
Old name of RB_ALLOCV.
Definition: memory.h:399
#define ISSPACE
Old name of rb_isspace.
Definition: ctype.h:88
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition: double.h:28
#define T_STRING
Old name of RUBY_T_STRING.
Definition: value_type.h:78
#define T_MASK
Old name of RUBY_T_MASK.
Definition: value_type.h:68
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition: long.h:48
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
Definition: fl_type.h:137
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition: value_type.h:64
#define ID2SYM
Old name of RB_ID2SYM.
Definition: symbol.h:44
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition: value_type.h:57
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
Definition: value_type.h:79
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition: fl_type.h:135
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition: assume.h:29
#define T_DATA
Old name of RUBY_T_DATA.
Definition: value_type.h:60
#define CLASS_OF
Old name of rb_class_of.
Definition: globals.h:203
#define T_NONE
Old name of RUBY_T_NONE.
Definition: value_type.h:74
#define FIXABLE
Old name of RB_FIXABLE.
Definition: fixnum.h:25
#define LONG2FIX
Old name of RB_INT2FIX.
Definition: long.h:49
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition: value_type.h:70
#define ISDIGIT
Old name of rb_isdigit.
Definition: ctype.h:93
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
Definition: value_type.h:76
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition: value_type.h:66
#define T_HASH
Old name of RUBY_T_HASH.
Definition: value_type.h:65
#define rb_ary_new3
Old name of rb_ary_new_from_args.
Definition: array.h:658
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
Definition: string.h:1680
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
Definition: int.h:44
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition: value_type.h:56
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition: value_type.h:75
#define NIL_P
Old name of RB_NIL_P.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
Definition: fl_type.h:59
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition: value_type.h:80
#define DBL2NUM
Old name of rb_float_new.
Definition: double.h:29
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition: value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition: value_type.h:85
#define FL_FREEZE
Old name of RUBY_FL_FREEZE.
Definition: fl_type.h:67
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition: symbol.h:47
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition: array.h:657
#define ALLOCV_END
Old name of RB_ALLOCV_END.
Definition: memory.h:401
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition: value_type.h:88
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition: error.c:3627
void rb_category_warning(rb_warning_category_t category, const char *fmt,...)
Identical to rb_warning(), except it takes additional "category" parameter.
Definition: error.c:507
void rb_bug(const char *fmt,...)
Interpreter panic switch.
Definition: error.c:1088
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
Definition: eval.c:1941
VALUE rb_eTypeError
TypeError exception.
Definition: error.c:1403
VALUE rb_eArgError
ArgumentError exception.
Definition: error.c:1404
void rb_warning(const char *fmt,...)
Issues a warning.
Definition: error.c:496
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
Definition: error.h:48
VALUE rb_cClass
Class class.
Definition: object.c:68
VALUE rb_cRational
Rational class.
Definition: rational.c:53
VALUE rb_class_superclass(VALUE klass)
Returns the superclass of klass.
Definition: object.c:2147
size_t rb_obj_embedded_size(uint32_t numiv)
Internal header for Object.
Definition: object.c:98
VALUE rb_class_get_superclass(VALUE klass)
Returns the superclass of a class.
Definition: object.c:2169
VALUE rb_convert_type(VALUE val, int type, const char *tname, const char *method)
Converts an object into another type.
Definition: object.c:3086
VALUE rb_Float(VALUE val)
This is the logic behind Kernel#Float.
Definition: object.c:3593
VALUE rb_mKernel
Kernel module.
Definition: object.c:65
VALUE rb_check_to_int(VALUE val)
Identical to rb_check_to_integer(), except it uses #to_int for conversion.
Definition: object.c:3192
VALUE rb_obj_reveal(VALUE obj, VALUE klass)
Make a hidden object visible again.
Definition: object.c:113
VALUE rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
Identical to rb_convert_type(), except it returns RUBY_Qnil instead of raising exceptions,...
Definition: object.c:3113
VALUE rb_cObject
Documented in include/ruby/internal/globals.h.
Definition: object.c:66
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
Definition: object.c:667
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
Definition: object.c:2091
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
Definition: object.c:2132
VALUE rb_class_new_instance_kw(int argc, const VALUE *argv, VALUE klass, int kw_splat)
Identical to rb_class_new_instance(), except you can specify how to handle the last element of the gi...
Definition: object.c:2120
VALUE rb_cRefinement
Refinement class.
Definition: object.c:69
VALUE rb_cInteger
Module class.
Definition: numeric.c:198
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition: object.c:104
VALUE rb_class_new_instance_pass_kw(int argc, const VALUE *argv, VALUE klass)
Identical to rb_class_new_instance(), except it passes the passed keywords if any to the #initialize ...
Definition: object.c:2109
VALUE rb_check_to_float(VALUE val)
This is complicated.
Definition: object.c:3632
static VALUE rb_obj_init_clone(int argc, VALUE *argv, VALUE obj)
Default implementation of #initialize_clone
Definition: object.c:645
VALUE rb_cNilClass
NilClass class.
Definition: object.c:71
VALUE rb_Hash(VALUE val)
Equivalent to Kernel#Hash in Ruby.
Definition: object.c:3791
VALUE rb_obj_frozen_p(VALUE obj)
Just calls RB_OBJ_FROZEN() inside.
Definition: object.c:1270
VALUE rb_obj_init_copy(VALUE obj, VALUE orig)
Default implementation of #initialize_copy
Definition: object.c:614
int rb_eql(VALUE obj1, VALUE obj2)
Checks for equality of the passed objects, in terms of Object#eql?.
Definition: object.c:192
double rb_str_to_dbl(VALUE str, int badcheck)
Identical to rb_cstr_to_dbl(), except it accepts a Ruby's string instead of C's.
Definition: object.c:3477
VALUE rb_Integer(VALUE val)
This is the logic behind Kernel#Integer.
Definition: object.c:3261
VALUE rb_cFalseClass
FalseClass class.
Definition: object.c:73
VALUE rb_cNumeric
Numeric class.
Definition: numeric.c:196
VALUE rb_Array(VALUE val)
This is the logic behind Kernel#Array.
Definition: object.c:3748
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition: object.c:247
VALUE rb_obj_dup(VALUE obj)
Duplicates the given object.
Definition: object.c:574
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition: object.c:678
VALUE rb_cBasicObject
BasicObject class.
Definition: object.c:64
VALUE rb_cModule
Module class.
Definition: object.c:67
VALUE rb_class_inherited_p(VALUE mod, VALUE arg)
Determines if the given two modules are relatives.
Definition: object.c:1762
VALUE rb_obj_is_instance_of(VALUE obj, VALUE c)
Queries if the given object is a direct instance of the given class.
Definition: object.c:807
VALUE rb_class_real(VALUE cl)
Finds a "real" class.
Definition: object.c:237
VALUE rb_obj_init_dup_clone(VALUE obj, VALUE orig)
Default implementation of #initialize_dup
Definition: object.c:631
VALUE rb_to_float(VALUE val)
Identical to rb_check_to_float(), except it raises on error.
Definition: object.c:3622
double rb_num2dbl(VALUE val)
Converts an instance of rb_cNumeric into C's double.
Definition: object.c:3684
VALUE rb_equal(VALUE obj1, VALUE obj2)
This function is an optimised version of calling #==.
Definition: object.c:179
VALUE rb_obj_clone(VALUE obj)
Produces a shallow copy of the given object.
Definition: object.c:519
VALUE rb_obj_is_kind_of(VALUE obj, VALUE c)
Queries if the given object is an instance (of possibly descendants) of the given class.
Definition: object.c:863
double rb_cstr_to_dbl(const char *p, int badcheck)
Converts a textual representation of a real number into a numeric, which is the nearest value that th...
Definition: object.c:3433
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
Definition: object.c:1258
VALUE rb_check_to_integer(VALUE val, const char *method)
Identical to rb_check_convert_type(), except the return value type is fixed to rb_cInteger.
Definition: object.c:3173
VALUE rb_String(VALUE val)
This is the logic behind Kernel#String.
Definition: object.c:3716
VALUE rb_cTrueClass
TrueClass class.
Definition: object.c:72
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
Definition: object.c:3186
VALUE rb_obj_setup(VALUE obj, VALUE klass, VALUE type)
Fills common fields in the object.
Definition: object.c:152
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition: gc.h:615
Encoding relates APIs.
rb_encoding * rb_default_external_encoding(void)
Queries the "default external" encoding.
Definition: encoding.c:1574
rb_encoding * rb_usascii_encoding(void)
Queries the encoding that represents US-ASCII.
Definition: encoding.c:1472
rb_encoding * rb_default_internal_encoding(void)
Queries the "default internal" encoding.
Definition: encoding.c:1661
static bool rb_enc_asciicompat(rb_encoding *enc)
Queries if the passed encoding is in some sense compatible with ASCII.
Definition: encoding.h:768
rb_encoding * rb_enc_get(VALUE obj)
Identical to rb_enc_get_index(), except the return type.
Definition: encoding.c:1013
VALUE rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
Identical to rb_enc_str_new(), except it assumes the passed pointer is a pointer to a C string.
Definition: string.c:1082
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
Definition: string.c:899
ID rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
Identical to rb_check_id(), except it takes a pointer to a memory region instead of Ruby's string.
Definition: symbol.c:1215
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition: vm_eval.c:1099
VALUE rb_funcallv_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
Identical to rb_funcallv(), except you can specify how to handle the last element of the given array.
Definition: vm_eval.c:1066
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
Definition: vm_eval.c:1058
#define RGENGC_WB_PROTECTED_OBJECT
This is a compile-time flag to enable/disable write barrier for struct RObject.
Definition: gc.h:490
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
Definition: array.c:1014
VALUE rb_ary_new(void)
Allocates a new, empty array.
Definition: array.c:747
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
Definition: array.c:1384
VALUE rb_dbl2big(double d)
Converts a C's double into a bignum.
Definition: bignum.c:5285
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition: error.h:284
void rb_obj_call_init_kw(VALUE, int, const VALUE *, int)
Identical to rb_obj_call_init(), except you can specify how to handle the last element of the given a...
Definition: eval.c:1743
VALUE rb_check_hash_type(VALUE obj)
Try converting an object to its hash representation using its to_hash method, if any.
Definition: hash.c:1864
VALUE rb_hash_aref(VALUE hash, VALUE key)
Queries the given key in the given hash table.
Definition: hash.c:2073
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
Definition: hash.c:2893
VALUE rb_hash_new(void)
Creates a new, empty hash object.
Definition: hash.c:1475
int rb_is_instance_id(ID id)
Classifies the given ID, then sees if it is an instance variable.
Definition: symbol.c:1081
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
Definition: symbol.c:1063
ID rb_id_attrset(ID id)
Calculates an ID of attribute writer.
Definition: symbol.c:121
int rb_is_local_id(ID id)
Classifies the given ID, then sees if it is a local variable.
Definition: symbol.c:1093
VALUE rb_protect(VALUE(*func)(VALUE args), VALUE args, int *state)
Protects a function call from potential global escapes from the function.
VALUE rb_rational_num(VALUE rat)
Queries the numerator of the passed Rational.
Definition: rational.c:1990
VALUE rb_rational_den(VALUE rat)
Queries the denominator of the passed Rational.
Definition: rational.c:1996
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition: string.c:3662
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
Definition: string.c:3046
VALUE rb_str_cat2(VALUE, const char *)
Just another name of rb_str_cat_cstr.
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
Definition: string.c:3628
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
Definition: string.c:2694
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
Definition: string.c:3904
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition: string.c:2845
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition: symbol.c:878
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
Definition: string.c:1770
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
VALUE rb_mod_remove_cvar(VALUE mod, VALUE name)
Resembles Module#remove_class_variable.
Definition: variable.c:4177
VALUE rb_obj_instance_variables(VALUE obj)
Resembles Object#instance_variables.
Definition: variable.c:2195
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition: variable.c:3151
VALUE rb_attr_get(VALUE obj, ID name)
Identical to rb_ivar_get()
Definition: variable.c:1358
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition: variable.c:1859
VALUE rb_mod_remove_const(VALUE space, VALUE name)
Resembles Module#remove_const.
Definition: variable.c:3256
void rb_cvar_set(VALUE klass, ID name, VALUE val)
Assigns a value to a class variable.
Definition: variable.c:3942
VALUE rb_cvar_get(VALUE klass, ID name)
Obtains a value from a class variable.
Definition: variable.c:4012
VALUE rb_mod_constants(int argc, const VALUE *argv, VALUE recv)
Resembles Module#constants.
Definition: variable.c:3416
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
Definition: variable.c:1350
void rb_ivar_foreach(VALUE obj, int(*func)(ID name, VALUE val, st_data_t arg), st_data_t arg)
Iterates over an object's instance variables.
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
Definition: variable.c:3619
VALUE rb_mod_name(VALUE mod)
Queries the name of a module.
Definition: variable.c:130
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
Definition: variable.c:412
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
Definition: variable.c:3157
VALUE rb_obj_remove_instance_variable(VALUE obj, VALUE name)
Resembles Object#remove_instance_variable.
Definition: variable.c:2249
st_index_t rb_ivar_count(VALUE obj)
Number of instance variables defined on an object.
Definition: variable.c:2143
VALUE rb_const_get_from(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
Definition: variable.c:3145
VALUE rb_ivar_defined(VALUE obj, ID name)
Queries if the instance variable is defined at the object.
Definition: variable.c:1876
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
Definition: variable.c:3478
VALUE rb_mod_class_variables(int argc, const VALUE *argv, VALUE recv)
Resembles Module#class_variables.
Definition: variable.c:4142
VALUE rb_cvar_defined(VALUE klass, ID name)
Queries if the given class has the given class variable.
Definition: variable.c:4019
int rb_const_defined_from(VALUE space, ID name)
Identical to rb_const_defined(), except it returns false for private constants.
Definition: variable.c:3466
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
Definition: variable.c:3472
VALUE(* rb_alloc_func_t)(VALUE klass)
This is the type of functions that ruby calls when trying to allocate an object.
Definition: vm.h:216
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition: vm_method.c:1286
int rb_method_basic_definition_p(VALUE klass, ID mid)
Well...
Definition: vm_method.c:2833
void rb_attr(VALUE klass, ID name, int need_reader, int need_writer, int honour_visibility)
This function resembles now-deprecated Module#attr.
Definition: vm_method.c:1864
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
Definition: vm_eval.c:668
rb_alloc_func_t rb_get_alloc_func(VALUE klass)
Queries the allocator function of a class.
Definition: vm_method.c:1292
VALUE rb_mod_module_exec(int argc, const VALUE *argv, VALUE mod)
Identical to rb_obj_instance_exec(), except it evaluates within the context of module.
Definition: vm_eval.c:2310
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
Definition: vm_method.c:2939
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition: symbol.h:276
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition: symbol.c:1117
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
Definition: symbol.c:992
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
Definition: symbol.c:823
ID rb_intern_str(VALUE str)
Identical to rb_intern(), except it takes an instance of rb_cString.
Definition: symbol.c:829
VALUE rb_id2str(ID id)
Identical to rb_id2name(), except it returns a Ruby's String instead of C's.
Definition: symbol.c:986
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition: io.h:2
int len
Length of the buffer.
Definition: io.h:8
#define strtod(s, e)
Just another name of ruby_strtod.
Definition: util.h:223
VALUE rb_f_sprintf(int argc, const VALUE *argv)
Identical to rb_str_format(), except how the arguments are arranged.
Definition: sprintf.c:208
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
Definition: sprintf.c:1217
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
Definition: sprintf.c:1240
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition: memory.h:367
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition: memory.h:162
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
Definition: variable.c:2036
#define RARRAY_LEN
Just another name of rb_array_len.
Definition: rarray.h:51
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition: rbasic.h:150
#define RBASIC(obj)
Convenient casting macro.
Definition: rbasic.h:40
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition: rclass.h:44
#define RCLASS(obj)
Convenient casting macro.
Definition: rclass.h:38
static VALUE * ROBJECT_IVPTR(VALUE obj)
Queries the instance variables.
Definition: robject.h:136
#define StringValue(v)
Ensures that the parameter object is a String.
Definition: rstring.h:66
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
Definition: rstring.h:76
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition: rstring.h:416
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
Definition: rstring.h:367
const char * rb_class2name(VALUE klass)
Queries the name of the passed class.
Definition: variable.c:418
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition: variable.c:427
#define errno
Ractor-aware version of errno.
Definition: ruby.h:388
#define InitVM(ext)
This macro is for internal use.
Definition: ruby.h:231
#define RB_PASS_KEYWORDS
Pass keywords, final argument should be a hash of keywords.
Definition: scan_args.h:72
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
Definition: scan_args.h:78
#define RB_NO_KEYWORDS
Do not pass keywords.
Definition: scan_args.h:69
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Definition: stdarg.h:35
Ruby's ordinal objects.
Definition: robject.h:83
Definition: shape.h:44
Definition: st.h:79
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition: value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
Definition: value_type.h:264
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition: value_type.h:433
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition: value_type.h:376