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