Ruby  3.1.4p223 (2023-03-30 revision HEAD)
symbol.c
1 /**********************************************************************
2 
3  symbol.h -
4 
5  $Author$
6  created at: Tue Jul 8 15:49:54 JST 2014
7 
8  Copyright (C) 2014 Yukihiro Matsumoto
9 
10 **********************************************************************/
11 
12 #include "gc.h"
13 #include "internal.h"
14 #include "internal/error.h"
15 #include "internal/gc.h"
16 #include "internal/hash.h"
17 #include "internal/object.h"
18 #include "internal/symbol.h"
19 #include "internal/vm.h"
20 #include "probes.h"
21 #include "ruby/encoding.h"
22 #include "ruby/st.h"
23 #include "symbol.h"
24 #include "vm_sync.h"
25 
26 #ifndef USE_SYMBOL_GC
27 # define USE_SYMBOL_GC 1
28 #endif
29 #ifndef SYMBOL_DEBUG
30 # define SYMBOL_DEBUG 0
31 #endif
32 #ifndef CHECK_ID_SERIAL
33 # define CHECK_ID_SERIAL SYMBOL_DEBUG
34 #endif
35 
36 #define SYMBOL_PINNED_P(sym) (RSYMBOL(sym)->id&~ID_SCOPE_MASK)
37 
38 #define STATIC_SYM2ID(sym) RSHIFT((VALUE)(sym), RUBY_SPECIAL_SHIFT)
39 
40 static ID register_static_symid(ID, const char *, long, rb_encoding *);
41 static ID register_static_symid_str(ID, VALUE);
42 #define REGISTER_SYMID(id, name) register_static_symid((id), (name), strlen(name), enc)
43 #include "id.c"
44 
45 #define is_identchar(p,e,enc) (ISALNUM((unsigned char)*(p)) || (*(p)) == '_' || !ISASCII(*(p)))
46 
47 #define op_tbl_count numberof(op_tbl)
48 STATIC_ASSERT(op_tbl_name_size, sizeof(op_tbl[0].name) == 3);
49 #define op_tbl_len(i) (!op_tbl[i].name[1] ? 1 : !op_tbl[i].name[2] ? 2 : 3)
50 
51 static void
52 Init_op_tbl(void)
53 {
54  int i;
55  rb_encoding *const enc = rb_usascii_encoding();
56 
57  for (i = '!'; i <= '~'; ++i) {
58  if (!ISALNUM(i) && i != '_') {
59  char c = (char)i;
60  register_static_symid(i, &c, 1, enc);
61  }
62  }
63  for (i = 0; i < op_tbl_count; ++i) {
64  register_static_symid(op_tbl[i].token, op_tbl[i].name, op_tbl_len(i), enc);
65  }
66 }
67 
68 static const int ID_ENTRY_UNIT = 512;
69 
70 enum id_entry_type {
71  ID_ENTRY_STR,
72  ID_ENTRY_SYM,
73  ID_ENTRY_SIZE
74 };
75 
76 rb_symbols_t ruby_global_symbols = {tNEXT_ID-1};
77 
78 static const struct st_hash_type symhash = {
81 };
82 
83 void
84 Init_sym(void)
85 {
86  rb_symbols_t *symbols = &ruby_global_symbols;
87 
88  VALUE dsym_fstrs = rb_ident_hash_new();
89  symbols->dsymbol_fstr_hash = dsym_fstrs;
90  rb_gc_register_mark_object(dsym_fstrs);
91  rb_obj_hide(dsym_fstrs);
92 
93  symbols->str_sym = st_init_table_with_size(&symhash, 1000);
94  symbols->ids = rb_ary_tmp_new(0);
95  rb_gc_register_mark_object(symbols->ids);
96 
97  Init_op_tbl();
98  Init_id();
99 }
100 
101 WARN_UNUSED_RESULT(static VALUE dsymbol_alloc(rb_symbols_t *symbols, const VALUE klass, const VALUE str, rb_encoding *const enc, const ID type));
102 WARN_UNUSED_RESULT(static VALUE dsymbol_check(rb_symbols_t *symbols, const VALUE sym));
103 WARN_UNUSED_RESULT(static ID lookup_str_id(VALUE str));
104 WARN_UNUSED_RESULT(static VALUE lookup_str_sym_with_lock(rb_symbols_t *symbols, const VALUE str));
105 WARN_UNUSED_RESULT(static VALUE lookup_str_sym(const VALUE str));
106 WARN_UNUSED_RESULT(static VALUE lookup_id_str(ID id));
107 WARN_UNUSED_RESULT(static ID intern_str(VALUE str, int mutable));
108 
109 #define GLOBAL_SYMBOLS_ENTER(symbols) rb_symbols_t *symbols = &ruby_global_symbols; RB_VM_LOCK_ENTER()
110 #define GLOBAL_SYMBOLS_LEAVE() RB_VM_LOCK_LEAVE()
111 
112 ID
114 {
115  VALUE str, sym;
116  int scope;
117 
118  if (!is_notop_id(id)) {
119  switch (id) {
120  case tAREF: case tASET:
121  return tASET; /* only exception */
122  }
123  rb_name_error(id, "cannot make operator ID :%"PRIsVALUE" attrset",
124  rb_id2str(id));
125  }
126  else {
127  scope = id_type(id);
128  switch (scope) {
129  case ID_LOCAL: case ID_INSTANCE: case ID_GLOBAL:
130  case ID_CONST: case ID_CLASS: case ID_JUNK:
131  break;
132  case ID_ATTRSET:
133  return id;
134  default:
135  {
136  if ((str = lookup_id_str(id)) != 0) {
137  rb_name_error(id, "cannot make unknown type ID %d:%"PRIsVALUE" attrset",
138  scope, str);
139  }
140  else {
141  rb_name_error_str(Qnil, "cannot make unknown type anonymous ID %d:%"PRIxVALUE" attrset",
142  scope, (VALUE)id);
143  }
144  }
145  }
146  }
147 
148  /* make new symbol and ID */
149  if (!(str = lookup_id_str(id))) {
150  static const char id_types[][8] = {
151  "local",
152  "instance",
153  "invalid",
154  "global",
155  "attrset",
156  "const",
157  "class",
158  "junk",
159  };
160  rb_name_error(id, "cannot make anonymous %.*s ID %"PRIxVALUE" attrset",
161  (int)sizeof(id_types[0]), id_types[scope], (VALUE)id);
162  }
163  str = rb_str_dup(str);
164  rb_str_cat(str, "=", 1);
165  sym = lookup_str_sym(str);
166  id = sym ? rb_sym2id(sym) : intern_str(str, 1);
167  return id;
168 }
169 
170 static int
171 is_special_global_name(const char *m, const char *e, rb_encoding *enc)
172 {
173  int mb = 0;
174 
175  if (m >= e) return 0;
176  if (is_global_name_punct(*m)) {
177  ++m;
178  }
179  else if (*m == '-') {
180  if (++m >= e) return 0;
181  if (is_identchar(m, e, enc)) {
182  if (!ISASCII(*m)) mb = 1;
183  m += rb_enc_mbclen(m, e, enc);
184  }
185  }
186  else {
187  if (!ISDIGIT(*m)) return 0;
188  do {
189  if (!ISASCII(*m)) mb = 1;
190  ++m;
191  } while (m < e && ISDIGIT(*m));
192  }
193  return m == e ? mb + 1 : 0;
194 }
195 
196 int
197 rb_symname_p(const char *name)
198 {
199  return rb_enc_symname_p(name, rb_ascii8bit_encoding());
200 }
201 
202 int
203 rb_enc_symname_p(const char *name, rb_encoding *enc)
204 {
205  return rb_enc_symname2_p(name, strlen(name), enc);
206 }
207 
208 static int
209 rb_sym_constant_char_p(const char *name, long nlen, rb_encoding *enc)
210 {
211  int c, len;
212  const char *end = name + nlen;
213 
214  if (nlen < 1) return FALSE;
215  if (ISASCII(*name)) return ISUPPER(*name);
216  c = rb_enc_precise_mbclen(name, end, enc);
217  if (!MBCLEN_CHARFOUND_P(c)) return FALSE;
218  len = MBCLEN_CHARFOUND_LEN(c);
219  c = rb_enc_mbc_to_codepoint(name, end, enc);
220  if (ONIGENC_IS_UNICODE(enc)) {
221  static int ctype_titlecase = 0;
222  if (rb_enc_isupper(c, enc)) return TRUE;
223  if (rb_enc_islower(c, enc)) return FALSE;
224  if (!ctype_titlecase) {
225  static const UChar cname[] = "titlecaseletter";
226  static const UChar *const end = cname + sizeof(cname) - 1;
227  ctype_titlecase = ONIGENC_PROPERTY_NAME_TO_CTYPE(enc, cname, end);
228  }
229  if (rb_enc_isctype(c, ctype_titlecase, enc)) return TRUE;
230  }
231  else {
232  /* fallback to case-folding */
233  OnigUChar fold[ONIGENC_GET_CASE_FOLD_CODES_MAX_NUM];
234  const OnigUChar *beg = (const OnigUChar *)name;
235  int r = enc->mbc_case_fold(ONIGENC_CASE_FOLD,
236  &beg, (const OnigUChar *)end,
237  fold, enc);
238  if (r > 0 && (r != len || memcmp(fold, name, r)))
239  return TRUE;
240  }
241  return FALSE;
242 }
243 
244 #define IDSET_ATTRSET_FOR_SYNTAX ((1U<<ID_LOCAL)|(1U<<ID_CONST))
245 #define IDSET_ATTRSET_FOR_INTERN (~(~0U<<(1<<ID_SCOPE_SHIFT)) & ~(1U<<ID_ATTRSET))
246 
248  const enum { invalid, stophere, needmore, } kind;
249  const enum ruby_id_types type;
250  const long nread;
251 };
252 
253 #define t struct enc_synmane_type_leading_chars_tag
254 
256 enc_synmane_type_leading_chars(const char *name, long len, rb_encoding *enc, int allowed_attrset)
257 {
258  const char *m = name;
259  const char *e = m + len;
260 
261  if (! rb_enc_asciicompat(enc)) {
262  return (t) { invalid, 0, 0, };
263  }
264  else if (! m) {
265  return (t) { invalid, 0, 0, };
266  }
267  else if ( len <= 0 ) {
268  return (t) { invalid, 0, 0, };
269  }
270  switch (*m) {
271  case '\0':
272  return (t) { invalid, 0, 0, };
273 
274  case '$':
275  if (is_special_global_name(++m, e, enc)) {
276  return (t) { stophere, ID_GLOBAL, len, };
277  }
278  else {
279  return (t) { needmore, ID_GLOBAL, 1, };
280  }
281 
282  case '@':
283  switch (*++m) {
284  default: return (t) { needmore, ID_INSTANCE, 1, };
285  case '@': return (t) { needmore, ID_CLASS, 2, };
286  }
287 
288  case '<':
289  switch (*++m) {
290  default: return (t) { stophere, ID_JUNK, 1, };
291  case '<': return (t) { stophere, ID_JUNK, 2, };
292  case '=':
293  switch (*++m) {
294  default: return (t) { stophere, ID_JUNK, 2, };
295  case '>': return (t) { stophere, ID_JUNK, 3, };
296  }
297  }
298 
299  case '>':
300  switch (*++m) {
301  default: return (t) { stophere, ID_JUNK, 1, };
302  case '>': case '=': return (t) { stophere, ID_JUNK, 2, };
303  }
304 
305  case '=':
306  switch (*++m) {
307  default: return (t) { invalid, 0, 1, };
308  case '~': return (t) { stophere, ID_JUNK, 2, };
309  case '=':
310  switch (*++m) {
311  default: return (t) { stophere, ID_JUNK, 2, };
312  case '=': return (t) { stophere, ID_JUNK, 3, };
313  }
314  }
315 
316  case '*':
317  switch (*++m) {
318  default: return (t) { stophere, ID_JUNK, 1, };
319  case '*': return (t) { stophere, ID_JUNK, 2, };
320  }
321 
322  case '+': case '-':
323  switch (*++m) {
324  default: return (t) { stophere, ID_JUNK, 1, };
325  case '@': return (t) { stophere, ID_JUNK, 2, };
326  }
327 
328  case '|': case '^': case '&': case '/': case '%': case '~': case '`':
329  return (t) { stophere, ID_JUNK, 1, };
330 
331  case '[':
332  switch (*++m) {
333  default: return (t) { needmore, ID_JUNK, 0, };
334  case ']':
335  switch (*++m) {
336  default: return (t) { stophere, ID_JUNK, 2, };
337  case '=': return (t) { stophere, ID_JUNK, 3, };
338  }
339  }
340 
341  case '!':
342  switch (*++m) {
343  case '=': case '~': return (t) { stophere, ID_JUNK, 2, };
344  default:
345  if (allowed_attrset & (1U << ID_JUNK)) {
346  return (t) { needmore, ID_JUNK, 1, };
347  }
348  else {
349  return (t) { stophere, ID_JUNK, 1, };
350  }
351  }
352 
353  default:
354  if (rb_sym_constant_char_p(name, len, enc)) {
355  return (t) { needmore, ID_CONST, 0, };
356  }
357  else {
358  return (t) { needmore, ID_LOCAL, 0, };
359  }
360  }
361 }
362 #undef t
363 
364 int
365 rb_enc_symname_type(const char *name, long len, rb_encoding *enc, unsigned int allowed_attrset)
366 {
367  const struct enc_synmane_type_leading_chars_tag f =
368  enc_synmane_type_leading_chars(name, len, enc, allowed_attrset);
369  const char *m = name + f.nread;
370  const char *e = name + len;
371  int type = (int)f.type;
372 
373  switch (f.kind) {
374  case invalid: return -1;
375  case stophere: break;
376  case needmore:
377 
378  if (m >= e || (*m != '_' && !ISALPHA(*m) && ISASCII(*m))) {
379  if (len > 1 && *(e-1) == '=') {
380  type = rb_enc_symname_type(name, len-1, enc, allowed_attrset);
381  if (type != ID_ATTRSET) return ID_ATTRSET;
382  }
383  return -1;
384  }
385  while (m < e && is_identchar(m, e, enc)) m += rb_enc_mbclen(m, e, enc);
386  if (m >= e) break;
387  switch (*m) {
388  case '!': case '?':
389  if (type == ID_GLOBAL || type == ID_CLASS || type == ID_INSTANCE) return -1;
390  type = ID_JUNK;
391  ++m;
392  if (m + 1 < e || *m != '=') break;
393  /* fall through */
394  case '=':
395  if (!(allowed_attrset & (1U << type))) return -1;
396  type = ID_ATTRSET;
397  ++m;
398  break;
399  }
400  }
401 
402  return m == e ? type : -1;
403 }
404 
405 int
406 rb_enc_symname2_p(const char *name, long len, rb_encoding *enc)
407 {
408  return rb_enc_symname_type(name, len, enc, IDSET_ATTRSET_FOR_SYNTAX) != -1;
409 }
410 
411 static int
412 rb_str_symname_type(VALUE name, unsigned int allowed_attrset)
413 {
414  const char *ptr = StringValuePtr(name);
415  long len = RSTRING_LEN(name);
416  int type = rb_enc_symname_type(ptr, len, rb_enc_get(name), allowed_attrset);
417  RB_GC_GUARD(name);
418  return type;
419 }
420 
421 static void
422 set_id_entry(rb_symbols_t *symbols, rb_id_serial_t num, VALUE str, VALUE sym)
423 {
424  ASSERT_vm_locking();
425  size_t idx = num / ID_ENTRY_UNIT;
426 
427  VALUE ary, ids = symbols->ids;
428  if (idx >= (size_t)RARRAY_LEN(ids) || NIL_P(ary = rb_ary_entry(ids, (long)idx))) {
429  ary = rb_ary_tmp_new(ID_ENTRY_UNIT * ID_ENTRY_SIZE);
430  rb_ary_store(ids, (long)idx, ary);
431  }
432  idx = (num % ID_ENTRY_UNIT) * ID_ENTRY_SIZE;
433  rb_ary_store(ary, (long)idx + ID_ENTRY_STR, str);
434  rb_ary_store(ary, (long)idx + ID_ENTRY_SYM, sym);
435 }
436 
437 static VALUE
438 get_id_serial_entry(rb_id_serial_t num, ID id, const enum id_entry_type t)
439 {
440  VALUE result = 0;
441 
442  GLOBAL_SYMBOLS_ENTER(symbols);
443  {
444  if (num && num <= symbols->last_id) {
445  size_t idx = num / ID_ENTRY_UNIT;
446  VALUE ids = symbols->ids;
447  VALUE ary;
448  if (idx < (size_t)RARRAY_LEN(ids) && !NIL_P(ary = rb_ary_entry(ids, (long)idx))) {
449  long pos = (long)(num % ID_ENTRY_UNIT) * ID_ENTRY_SIZE;
450  result = rb_ary_entry(ary, pos + t);
451 
452  if (NIL_P(result)) {
453  result = 0;
454  }
455  else {
456 #if CHECK_ID_SERIAL
457  if (id) {
458  VALUE sym = result;
459  if (t != ID_ENTRY_SYM)
460  sym = rb_ary_entry(ary, pos + ID_ENTRY_SYM);
461  if (STATIC_SYM_P(sym)) {
462  if (STATIC_SYM2ID(sym) != id) result = 0;
463  }
464  else {
465  if (RSYMBOL(sym)->id != id) result = 0;
466  }
467  }
468 #endif
469  }
470  }
471  }
472  }
473  GLOBAL_SYMBOLS_LEAVE();
474 
475  return result;
476 }
477 
478 static VALUE
479 get_id_entry(ID id, const enum id_entry_type t)
480 {
481  return get_id_serial_entry(rb_id_to_serial(id), id, t);
482 }
483 
484 static inline ID
485 rb_id_serial_to_id(rb_id_serial_t num)
486 {
487  if (is_notop_id((ID)num)) {
488  VALUE sym = get_id_serial_entry(num, 0, ID_ENTRY_SYM);
489  if (sym) return SYM2ID(sym);
490  return ((ID)num << ID_SCOPE_SHIFT) | ID_INTERNAL | ID_STATIC_SYM;
491  }
492  else {
493  return (ID)num;
494  }
495 }
496 
497 #if SYMBOL_DEBUG
498 static int
499 register_sym_update_callback(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
500 {
501  if (existing) {
502  rb_fatal("symbol :% "PRIsVALUE" is already registered with %"PRIxVALUE,
503  (VALUE)*key, (VALUE)*value);
504  }
505  *value = arg;
506  return ST_CONTINUE;
507 }
508 #endif
509 
510 static void
511 register_sym(rb_symbols_t *symbols, VALUE str, VALUE sym)
512 {
513  ASSERT_vm_locking();
514 
515 #if SYMBOL_DEBUG
516  st_update(symbols->str_sym, (st_data_t)str,
517  register_sym_update_callback, (st_data_t)sym);
518 #else
519  st_add_direct(symbols->str_sym, (st_data_t)str, (st_data_t)sym);
520 #endif
521 }
522 
523 static void
524 unregister_sym(rb_symbols_t *symbols, VALUE str, VALUE sym)
525 {
526  ASSERT_vm_locking();
527 
528  st_data_t str_data = (st_data_t)str;
529  if (!st_delete(symbols->str_sym, &str_data, NULL)) {
530  rb_bug("%p can't remove str from str_id (%s)", (void *)sym, RSTRING_PTR(str));
531  }
532 }
533 
534 static ID
535 register_static_symid(ID id, const char *name, long len, rb_encoding *enc)
536 {
537  VALUE str = rb_enc_str_new(name, len, enc);
538  return register_static_symid_str(id, str);
539 }
540 
541 static ID
542 register_static_symid_str(ID id, VALUE str)
543 {
544  rb_id_serial_t num = rb_id_to_serial(id);
545  VALUE sym = STATIC_ID2SYM(id);
546 
547  OBJ_FREEZE(str);
548  str = rb_fstring(str);
549 
550  RUBY_DTRACE_CREATE_HOOK(SYMBOL, RSTRING_PTR(str));
551 
552  GLOBAL_SYMBOLS_ENTER(symbols)
553  {
554  register_sym(symbols, str, sym);
555  set_id_entry(symbols, num, str, sym);
556  }
557  GLOBAL_SYMBOLS_LEAVE();
558 
559  return id;
560 }
561 
562 static int
563 sym_check_asciionly(VALUE str)
564 {
565  if (!rb_enc_asciicompat(rb_enc_get(str))) return FALSE;
566  switch (rb_enc_str_coderange(str)) {
568  rb_raise(rb_eEncodingError, "invalid symbol in encoding %s :%+"PRIsVALUE,
569  rb_enc_name(rb_enc_get(str)), str);
570  case ENC_CODERANGE_7BIT:
571  return TRUE;
572  }
573  return FALSE;
574 }
575 
576 #if 0
577 /*
578  * _str_ itself will be registered at the global symbol table. _str_
579  * can be modified before the registration, since the encoding will be
580  * set to ASCII-8BIT if it is a special global name.
581  */
582 
583 static inline void
584 must_be_dynamic_symbol(VALUE x)
585 {
586  if (UNLIKELY(!DYNAMIC_SYM_P(x))) {
587  if (STATIC_SYM_P(x)) {
588  VALUE str = lookup_id_str(RSHIFT((unsigned long)(x),RUBY_SPECIAL_SHIFT));
589 
590  if (str) {
591  rb_bug("wrong argument: %s (inappropriate Symbol)", RSTRING_PTR(str));
592  }
593  else {
594  rb_bug("wrong argument: inappropriate Symbol (%p)", (void *)x);
595  }
596  }
597  else {
598  rb_bug("wrong argument type %s (expected Symbol)", rb_builtin_class_name(x));
599  }
600  }
601 }
602 #endif
603 
604 static VALUE
605 dsymbol_alloc(rb_symbols_t *symbols, const VALUE klass, const VALUE str, rb_encoding * const enc, const ID type)
606 {
607  ASSERT_vm_locking();
608 
609  const VALUE dsym = rb_newobj_of(klass, T_SYMBOL | FL_WB_PROTECTED);
610  long hashval;
611 
612  rb_enc_set_index(dsym, rb_enc_to_index(enc));
613  OBJ_FREEZE(dsym);
614  RB_OBJ_WRITE(dsym, &RSYMBOL(dsym)->fstr, str);
615  RSYMBOL(dsym)->id = type;
616 
617  /* we want hashval to be in Fixnum range [ruby-core:15713] r15672 */
618  hashval = (long)rb_str_hash(str);
619  RSYMBOL(dsym)->hashval = RSHIFT((long)hashval, 1);
620  register_sym(symbols, str, dsym);
621  rb_hash_aset(symbols->dsymbol_fstr_hash, str, Qtrue);
622  RUBY_DTRACE_CREATE_HOOK(SYMBOL, RSTRING_PTR(RSYMBOL(dsym)->fstr));
623 
624  return dsym;
625 }
626 
627 static inline VALUE
628 dsymbol_check(rb_symbols_t *symbols, const VALUE sym)
629 {
630  ASSERT_vm_locking();
631 
632  if (UNLIKELY(rb_objspace_garbage_object_p(sym))) {
633  const VALUE fstr = RSYMBOL(sym)->fstr;
634  const ID type = RSYMBOL(sym)->id & ID_SCOPE_MASK;
635  RSYMBOL(sym)->fstr = 0;
636  unregister_sym(symbols, fstr, sym);
637  return dsymbol_alloc(symbols, rb_cSymbol, fstr, rb_enc_get(fstr), type);
638  }
639  else {
640  return sym;
641  }
642 }
643 
644 static ID
645 lookup_str_id(VALUE str)
646 {
647  st_data_t sym_data;
648  int found;
649 
650  GLOBAL_SYMBOLS_ENTER(symbols);
651  {
652  found = st_lookup(symbols->str_sym, (st_data_t)str, &sym_data);
653  }
654  GLOBAL_SYMBOLS_LEAVE();
655 
656  if (found) {
657  const VALUE sym = (VALUE)sym_data;
658 
659  if (STATIC_SYM_P(sym)) {
660  return STATIC_SYM2ID(sym);
661  }
662  else if (DYNAMIC_SYM_P(sym)) {
663  ID id = RSYMBOL(sym)->id;
664  if (id & ~ID_SCOPE_MASK) return id;
665  }
666  else {
667  rb_bug("non-symbol object %s:%"PRIxVALUE" for %"PRIsVALUE" in symbol table",
668  rb_builtin_class_name(sym), sym, str);
669  }
670  }
671  return (ID)0;
672 }
673 
674 static VALUE
675 lookup_str_sym_with_lock(rb_symbols_t *symbols, const VALUE str)
676 {
677  st_data_t sym_data;
678  if (st_lookup(symbols->str_sym, (st_data_t)str, &sym_data)) {
679  VALUE sym = (VALUE)sym_data;
680  if (DYNAMIC_SYM_P(sym)) {
681  sym = dsymbol_check(symbols, sym);
682  }
683  return sym;
684  }
685  else {
686  return Qfalse;
687  }
688 }
689 
690 static VALUE
691 lookup_str_sym(const VALUE str)
692 {
693  VALUE sym;
694 
695  GLOBAL_SYMBOLS_ENTER(symbols);
696  {
697  sym = lookup_str_sym_with_lock(symbols, str);
698  }
699  GLOBAL_SYMBOLS_LEAVE();
700 
701  return sym;
702 }
703 
704 static VALUE
705 lookup_id_str(ID id)
706 {
707  return get_id_entry(id, ID_ENTRY_STR);
708 }
709 
710 ID
711 rb_intern3(const char *name, long len, rb_encoding *enc)
712 {
713  VALUE sym;
714  struct RString fake_str;
715  VALUE str = rb_setup_fake_str(&fake_str, name, len, enc);
716  OBJ_FREEZE(str);
717  sym = lookup_str_sym(str);
718  if (sym) return rb_sym2id(sym);
719  str = rb_enc_str_new(name, len, enc); /* make true string */
720  return intern_str(str, 1);
721 }
722 
723 static ID
724 next_id_base_with_lock(rb_symbols_t *symbols)
725 {
726  ID id;
727  rb_id_serial_t next_serial = symbols->last_id + 1;
728 
729  if (next_serial == 0) {
730  id = (ID)-1;
731  }
732  else {
733  const size_t num = ++symbols->last_id;
734  id = num << ID_SCOPE_SHIFT;
735  }
736 
737  return id;
738 }
739 
740 static ID
741 next_id_base(void)
742 {
743  ID id;
744  GLOBAL_SYMBOLS_ENTER(symbols);
745  {
746  id = next_id_base_with_lock(symbols);
747  }
748  GLOBAL_SYMBOLS_LEAVE();
749  return id;
750 }
751 
752 static ID
753 intern_str(VALUE str, int mutable)
754 {
755  ID id;
756  ID nid;
757 
758  id = rb_str_symname_type(str, IDSET_ATTRSET_FOR_INTERN);
759  if (id == (ID)-1) id = ID_JUNK;
760  if (sym_check_asciionly(str)) {
761  if (!mutable) str = rb_str_dup(str);
763  }
764  if ((nid = next_id_base()) == (ID)-1) {
765  str = rb_str_ellipsize(str, 20);
766  rb_raise(rb_eRuntimeError, "symbol table overflow (symbol %"PRIsVALUE")",
767  str);
768  }
769  id |= nid;
770  id |= ID_STATIC_SYM;
771  return register_static_symid_str(id, str);
772 }
773 
774 ID
775 rb_intern2(const char *name, long len)
776 {
777  return rb_intern3(name, len, rb_usascii_encoding());
778 }
779 
780 #undef rb_intern
781 ID
782 rb_intern(const char *name)
783 {
784  return rb_intern2(name, strlen(name));
785 }
786 
787 ID
789 {
790  VALUE sym = lookup_str_sym(str);
791 
792  if (sym) {
793  return SYM2ID(sym);
794  }
795 
796  return intern_str(str, 0);
797 }
798 
799 void
800 rb_gc_free_dsymbol(VALUE sym)
801 {
802  VALUE str = RSYMBOL(sym)->fstr;
803 
804  if (str) {
805  RSYMBOL(sym)->fstr = 0;
806 
807  GLOBAL_SYMBOLS_ENTER(symbols);
808  {
809  unregister_sym(symbols, str, sym);
810  rb_hash_delete_entry(symbols->dsymbol_fstr_hash, str);
811  }
812  GLOBAL_SYMBOLS_LEAVE();
813  }
814 }
815 
816 /*
817  * call-seq:
818  * str.intern -> symbol
819  * str.to_sym -> symbol
820  *
821  * Returns the Symbol corresponding to <i>str</i>, creating the
822  * symbol if it did not previously exist. See Symbol#id2name.
823  *
824  * "Koala".intern #=> :Koala
825  * s = 'cat'.to_sym #=> :cat
826  * s == :cat #=> true
827  * s = '@cat'.to_sym #=> :@cat
828  * s == :@cat #=> true
829  *
830  * This can also be used to create symbols that cannot be represented using the
831  * <code>:xxx</code> notation.
832  *
833  * 'cat and dog'.to_sym #=> :"cat and dog"
834  */
835 
836 VALUE
838 {
839  VALUE sym;
840 #if USE_SYMBOL_GC
841  rb_encoding *enc, *ascii;
842  int type;
843 #else
844  ID id;
845 #endif
846  GLOBAL_SYMBOLS_ENTER(symbols);
847  {
848  sym = lookup_str_sym_with_lock(symbols, str);
849 
850  if (sym) {
851  // ok
852  }
853  else {
854 #if USE_SYMBOL_GC
855  enc = rb_enc_get(str);
856  ascii = rb_usascii_encoding();
857  if (enc != ascii && sym_check_asciionly(str)) {
858  str = rb_str_dup(str);
859  rb_enc_associate(str, ascii);
860  OBJ_FREEZE(str);
861  enc = ascii;
862  }
863  else {
864  str = rb_str_dup(str);
865  OBJ_FREEZE(str);
866  }
867  str = rb_fstring(str);
868  type = rb_str_symname_type(str, IDSET_ATTRSET_FOR_INTERN);
869  if (type < 0) type = ID_JUNK;
870  sym = dsymbol_alloc(symbols, rb_cSymbol, str, enc, type);
871 #else
872  id = intern_str(str, 0);
873  sym = ID2SYM(id);
874 #endif
875  }
876  }
877  GLOBAL_SYMBOLS_LEAVE();
878  return sym;
879 }
880 
881 ID
883 {
884  ID id;
885  if (STATIC_SYM_P(sym)) {
886  id = STATIC_SYM2ID(sym);
887  }
888  else if (DYNAMIC_SYM_P(sym)) {
889  GLOBAL_SYMBOLS_ENTER(symbols);
890  {
891  sym = dsymbol_check(symbols, sym);
892  id = RSYMBOL(sym)->id;
893 
894  if (UNLIKELY(!(id & ~ID_SCOPE_MASK))) {
895  VALUE fstr = RSYMBOL(sym)->fstr;
896  ID num = next_id_base_with_lock(symbols);
897 
898  RSYMBOL(sym)->id = id |= num;
899  /* make it permanent object */
900 
901  set_id_entry(symbols, rb_id_to_serial(num), fstr, sym);
902  rb_hash_delete_entry(symbols->dsymbol_fstr_hash, fstr);
903  }
904  }
905  GLOBAL_SYMBOLS_LEAVE();
906  }
907  else {
908  rb_raise(rb_eTypeError, "wrong argument type %s (expected Symbol)",
909  rb_builtin_class_name(sym));
910  }
911  return id;
912 }
913 
914 #undef rb_id2sym
915 VALUE
917 {
918  if (!DYNAMIC_ID_P(x)) return STATIC_ID2SYM(x);
919  return get_id_entry(x, ID_ENTRY_SYM);
920 }
921 
922 
923 VALUE
925 {
926  if (DYNAMIC_SYM_P(sym)) {
927  return RSYMBOL(sym)->fstr;
928  }
929  else {
930  return rb_id2str(STATIC_SYM2ID(sym));
931  }
932 }
933 
934 VALUE
936 {
937  return lookup_id_str(id);
938 }
939 
940 const char *
942 {
943  VALUE str = rb_id2str(id);
944 
945  if (!str) return 0;
946  return RSTRING_PTR(str);
947 }
948 
949 ID
950 rb_make_internal_id(void)
951 {
952  return next_id_base() | ID_INTERNAL | ID_STATIC_SYM;
953 }
954 
955 ID
956 rb_make_temporary_id(size_t n)
957 {
958  const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
959  const ID id = max_id - (ID)n;
960  if (id <= ruby_global_symbols.last_id) {
961  rb_raise(rb_eRuntimeError, "too big to make temporary ID: %" PRIdSIZE, n);
962  }
963  return (id << ID_SCOPE_SHIFT) | ID_STATIC_SYM | ID_INTERNAL;
964 }
965 
966 static int
967 symbols_i(st_data_t key, st_data_t value, st_data_t arg)
968 {
969  VALUE ary = (VALUE)arg;
970  VALUE sym = (VALUE)value;
971 
972  if (STATIC_SYM_P(sym)) {
973  rb_ary_push(ary, sym);
974  return ST_CONTINUE;
975  }
976  else if (!DYNAMIC_SYM_P(sym)) {
977  rb_bug("invalid symbol: %s", RSTRING_PTR((VALUE)key));
978  }
979  else if (!SYMBOL_PINNED_P(sym) && rb_objspace_garbage_object_p(sym)) {
980  RSYMBOL(sym)->fstr = 0;
981  return ST_DELETE;
982  }
983  else {
984  rb_ary_push(ary, sym);
985  return ST_CONTINUE;
986  }
987 
988 }
989 
990 VALUE
992 {
993  VALUE ary;
994 
995  GLOBAL_SYMBOLS_ENTER(symbols);
996  {
997  ary = rb_ary_new2(symbols->str_sym->num_entries);
998  st_foreach(symbols->str_sym, symbols_i, ary);
999  }
1000  GLOBAL_SYMBOLS_LEAVE();
1001 
1002  return ary;
1003 }
1004 
1005 size_t
1006 rb_sym_immortal_count(void)
1007 {
1008  return (size_t)ruby_global_symbols.last_id;
1009 }
1010 
1011 int
1013 {
1014  return is_const_id(id);
1015 }
1016 
1017 int
1019 {
1020  return is_class_id(id);
1021 }
1022 
1023 int
1025 {
1026  return is_global_id(id);
1027 }
1028 
1029 int
1031 {
1032  return is_instance_id(id);
1033 }
1034 
1035 int
1037 {
1038  return is_attrset_id(id);
1039 }
1040 
1041 int
1043 {
1044  return is_local_id(id);
1045 }
1046 
1047 int
1049 {
1050  return is_junk_id(id);
1051 }
1052 
1053 int
1054 rb_is_const_sym(VALUE sym)
1055 {
1056  return is_const_sym(sym);
1057 }
1058 
1059 int
1060 rb_is_attrset_sym(VALUE sym)
1061 {
1062  return is_attrset_sym(sym);
1063 }
1064 
1065 ID
1066 rb_check_id(volatile VALUE *namep)
1067 {
1068  VALUE tmp;
1069  VALUE name = *namep;
1070 
1071  if (STATIC_SYM_P(name)) {
1072  return STATIC_SYM2ID(name);
1073  }
1074  else if (DYNAMIC_SYM_P(name)) {
1075  if (SYMBOL_PINNED_P(name)) {
1076  return RSYMBOL(name)->id;
1077  }
1078  else {
1079  *namep = RSYMBOL(name)->fstr;
1080  return 0;
1081  }
1082  }
1083  else if (!RB_TYPE_P(name, T_STRING)) {
1084  tmp = rb_check_string_type(name);
1085  if (NIL_P(tmp)) {
1086  rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol nor a string",
1087  name);
1088  }
1089  name = tmp;
1090  *namep = name;
1091  }
1092 
1093  sym_check_asciionly(name);
1094 
1095  return lookup_str_id(name);
1096 }
1097 
1098 VALUE
1099 rb_check_symbol(volatile VALUE *namep)
1100 {
1101  VALUE sym;
1102  VALUE tmp;
1103  VALUE name = *namep;
1104 
1105  if (STATIC_SYM_P(name)) {
1106  return name;
1107  }
1108  else if (DYNAMIC_SYM_P(name)) {
1109  if (!SYMBOL_PINNED_P(name)) {
1110  GLOBAL_SYMBOLS_ENTER(symbols);
1111  {
1112  name = dsymbol_check(symbols, name);
1113  }
1114  GLOBAL_SYMBOLS_LEAVE();
1115 
1116  *namep = name;
1117  }
1118  return name;
1119  }
1120  else if (!RB_TYPE_P(name, T_STRING)) {
1121  tmp = rb_check_string_type(name);
1122  if (NIL_P(tmp)) {
1123  rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol nor a string",
1124  name);
1125  }
1126  name = tmp;
1127  *namep = name;
1128  }
1129 
1130  sym_check_asciionly(name);
1131 
1132  if ((sym = lookup_str_sym(name)) != 0) {
1133  return sym;
1134  }
1135 
1136  return Qnil;
1137 }
1138 
1139 ID
1140 rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
1141 {
1142  struct RString fake_str;
1143  const VALUE name = rb_setup_fake_str(&fake_str, ptr, len, enc);
1144 
1145  sym_check_asciionly(name);
1146 
1147  return lookup_str_id(name);
1148 }
1149 
1150 VALUE
1151 rb_check_symbol_cstr(const char *ptr, long len, rb_encoding *enc)
1152 {
1153  VALUE sym;
1154  struct RString fake_str;
1155  const VALUE name = rb_setup_fake_str(&fake_str, ptr, len, enc);
1156 
1157  sym_check_asciionly(name);
1158 
1159  if ((sym = lookup_str_sym(name)) != 0) {
1160  return sym;
1161  }
1162 
1163  return Qnil;
1164 }
1165 
1166 #undef rb_sym_intern_ascii_cstr
1167 #ifdef __clang__
1168 NOINLINE(VALUE rb_sym_intern(const char *ptr, long len, rb_encoding *enc));
1169 #else
1170 FUNC_MINIMIZED(VALUE rb_sym_intern(const char *ptr, long len, rb_encoding *enc));
1171 FUNC_MINIMIZED(VALUE rb_sym_intern_ascii(const char *ptr, long len));
1172 FUNC_MINIMIZED(VALUE rb_sym_intern_ascii_cstr(const char *ptr));
1173 #endif
1174 
1175 VALUE
1176 rb_sym_intern(const char *ptr, long len, rb_encoding *enc)
1177 {
1178  struct RString fake_str;
1179  const VALUE name = rb_setup_fake_str(&fake_str, ptr, len, enc);
1180  return rb_str_intern(name);
1181 }
1182 
1183 VALUE
1184 rb_sym_intern_ascii(const char *ptr, long len)
1185 {
1186  return rb_sym_intern(ptr, len, rb_usascii_encoding());
1187 }
1188 
1189 VALUE
1190 rb_sym_intern_ascii_cstr(const char *ptr)
1191 {
1192  return rb_sym_intern_ascii(ptr, strlen(ptr));
1193 }
1194 
1195 VALUE
1196 rb_to_symbol_type(VALUE obj)
1197 {
1198  return rb_convert_type_with_id(obj, T_SYMBOL, "Symbol", idTo_sym);
1199 }
1200 
1201 int
1202 rb_is_const_name(VALUE name)
1203 {
1204  return rb_str_symname_type(name, 0) == ID_CONST;
1205 }
1206 
1207 int
1208 rb_is_class_name(VALUE name)
1209 {
1210  return rb_str_symname_type(name, 0) == ID_CLASS;
1211 }
1212 
1213 int
1214 rb_is_instance_name(VALUE name)
1215 {
1216  return rb_str_symname_type(name, 0) == ID_INSTANCE;
1217 }
1218 
1219 int
1220 rb_is_local_name(VALUE name)
1221 {
1222  return rb_str_symname_type(name, 0) == ID_LOCAL;
1223 }
1224 
1225 #include "id_table.c"
static bool rb_enc_isupper(OnigCodePoint c, rb_encoding *enc)
Identical to rb_isupper(), except it additionally takes an encoding.
Definition: ctype.h:124
static bool rb_enc_isctype(OnigCodePoint c, OnigCtype t, rb_encoding *enc)
Queries if the passed code point is of passed character type in the passed encoding.
Definition: ctype.h:63
static bool rb_enc_islower(OnigCodePoint c, rb_encoding *enc)
Identical to rb_islower(), except it additionally takes an encoding.
Definition: ctype.h:110
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
Definition: coderange.h:180
#define T_STRING
Old name of RUBY_T_STRING.
Definition: value_type.h:78
#define ISUPPER
Old name of rb_isupper.
Definition: ctype.h:89
#define ID2SYM
Old name of RB_ID2SYM.
Definition: symbol.h:44
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition: fl_type.h:143
#define SYM2ID
Old name of RB_SYM2ID.
Definition: symbol.h:45
#define ISDIGIT
Old name of rb_isdigit.
Definition: ctype.h:93
#define STATIC_SYM_P
Old name of RB_STATIC_SYM_P.
#define MBCLEN_CHARFOUND_LEN(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_LEN.
Definition: encoding.h:533
#define ISALPHA
Old name of rb_isalpha.
Definition: ctype.h:92
#define ISASCII
Old name of rb_isascii.
Definition: ctype.h:85
#define Qtrue
Old name of RUBY_Qtrue.
#define DYNAMIC_SYM_P
Old name of RB_DYNAMIC_SYM_P.
Definition: value_type.h:86
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define ENC_CODERANGE_BROKEN
Old name of RUBY_ENC_CODERANGE_BROKEN.
Definition: coderange.h:182
#define NIL_P
Old name of RB_NIL_P.
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
Definition: encoding.h:532
#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 rb_ary_new2
Old name of rb_ary_new_capa.
Definition: array.h:651
#define ISALNUM
Old name of rb_isalnum.
Definition: ctype.h:91
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition: error.c:3025
void rb_bug(const char *fmt,...)
Interpreter panic switch.
Definition: error.c:802
void rb_name_error(ID id, const char *fmt,...)
Raises an instance of rb_eNameError.
Definition: error.c:1687
VALUE rb_eTypeError
TypeError exception.
Definition: error.c:1099
void rb_name_error_str(VALUE str, const char *fmt,...)
Identical to rb_name_error(), except it takes a VALUE instead of ID.
Definition: error.c:1702
void rb_fatal(const char *fmt,...)
Raises the unsung "fatal" exception.
Definition: error.c:3076
VALUE rb_eRuntimeError
RuntimeError exception.
Definition: error.c:1097
VALUE rb_eEncodingError
EncodingError exception.
Definition: error.c:1105
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition: object.c:82
VALUE rb_cSymbol
Sumbol class.
Definition: string.c:81
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition: rgengc.h:220
Encoding relates APIs.
int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
Queries the number of bytes of the character at the passed pointer.
Definition: encoding.c:1234
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
Identical to rb_enc_associate(), except it takes an encoding itself instead of its index.
Definition: encoding.c:1066
rb_encoding * rb_usascii_encoding(void)
Queries the encoding that represents US-ASCII.
Definition: encoding.c:1539
int rb_enc_to_index(rb_encoding *enc)
Queries the index of the encoding.
Definition: encoding.c:197
void rb_enc_set_index(VALUE obj, int encindex)
Destructively assigns an encoding (via its index) to an object.
Definition: encoding.c:1030
rb_encoding * rb_ascii8bit_encoding(void)
Queries the encoding that represents ASCII-8BIT a.k.a.
Definition: encoding.c:1515
static bool rb_enc_asciicompat(rb_encoding *enc)
Queries if the passed encoding is in some sense compatible with ASCII.
Definition: encoding.h:782
int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
Queries the number of bytes of the character at the passed pointer.
Definition: encoding.c:1222
rb_encoding * rb_enc_get(VALUE obj)
Identical to rb_enc_get_index(), except the return type.
Definition: encoding.c:1072
static OnigCodePoint rb_enc_mbc_to_codepoint(const char *p, const char *e, rb_encoding *enc)
Identical to rb_enc_codepoint(), except it assumes the passed character is not broken.
Definition: encoding.h:607
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
Definition: encoding.h:433
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
Definition: string.c:776
VALUE rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it additionally takes an encoding.
Definition: string.c:940
int rb_enc_symname_p(const char *str, rb_encoding *enc)
Identical to rb_symname_p(), except it additionally takes an encoding.
Definition: symbol.c:203
ID rb_intern3(const char *name, long len, rb_encoding *enc)
Identical to rb_intern2(), except it additionally takes an encoding.
Definition: symbol.c:711
VALUE rb_check_symbol_cstr(const char *ptr, long len, rb_encoding *enc)
Identical to rb_check_id_cstr(), except for the return type.
Definition: symbol.c:1151
int rb_enc_symname2_p(const char *name, long len, rb_encoding *enc)
Identical to rb_enc_symname_p(), except it additionally takes the passed string's length.
Definition: symbol.c:406
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:1140
void rb_gc_register_mark_object(VALUE object)
Inform the garbage collector that object is a live Ruby object that should not be moved.
Definition: gc.c:8687
VALUE rb_ary_tmp_new(long capa)
Allocates a "temporary" array.
Definition: array.c:847
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
Definition: array.c:1308
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
Definition: array.c:1679
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
Definition: array.c:1148
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:2903
VALUE rb_sym_all_symbols(void)
Collects every single bits of symbols that have ever interned in the entire history of the current pr...
Definition: symbol.c:991
int rb_is_global_id(ID id)
Classifies the given ID, then sees if it is a global variable.
Definition: symbol.c:1024
int rb_is_instance_id(ID id)
Classifies the given ID, then sees if it is an instance variable.
Definition: symbol.c:1030
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
Definition: symbol.c:1012
int rb_is_junk_id(ID)
Classifies the given ID, then sees if it is a junk ID.
Definition: symbol.c:1048
int rb_symname_p(const char *str)
Sees if the passed C string constructs a valid syntactic symbol.
Definition: symbol.c:197
ID rb_id_attrset(ID id)
Calculates an ID of attribute writer.
Definition: symbol.c:113
int rb_is_class_id(ID id)
Classifies the given ID, then sees if it is a class variable.
Definition: symbol.c:1018
int rb_is_attrset_id(ID id)
Classifies the given ID, then sees if it is an attribute writer.
Definition: symbol.c:1036
int rb_is_local_id(ID id)
Classifies the given ID, then sees if it is a local variable.
Definition: symbol.c:1042
int rb_str_hash_cmp(VALUE str1, VALUE str2)
Compares two strings.
Definition: string.c:3526
VALUE rb_str_ellipsize(VALUE str, long len)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
Definition: string.c:10802
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition: string.c:1808
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
Definition: string.c:3516
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
Definition: string.c:3161
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition: string.c:2659
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition: symbol.c:837
ID rb_intern2(const char *name, long len)
Identical to rb_intern(), except it additionally takes the length of the string.
Definition: symbol.c:775
VALUE rb_check_symbol(volatile VALUE *namep)
Identical to rb_check_id(), except it returns an instance of rb_cSymbol instead.
Definition: symbol.c:1099
VALUE rb_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
Definition: symbol.c:916
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition: symbol.c:1066
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
Definition: symbol.c:941
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
Definition: symbol.c:782
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
Definition: symbol.c:924
ID rb_sym2id(VALUE obj)
Converts an instance of rb_cSymbol into an ID.
Definition: symbol.c:882
ID rb_intern_str(VALUE str)
Identical to rb_intern(), except it takes an instance of rb_cString.
Definition: symbol.c:788
VALUE rb_id2str(ID id)
Identical to rb_id2name(), except it returns a Ruby's String instead of C's.
Definition: symbol.c:935
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition: memory.h:161
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
Definition: cxxanyargs.hpp:432
VALUE rb_newobj_of(VALUE klass, VALUE flags)
This is the implementation detail of RB_NEWOBJ_OF.
Definition: gc.c:2601
#define RARRAY_LEN
Just another name of rb_array_len.
Definition: rarray.h:68
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
Definition: rstring.h:82
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition: rstring.h:497
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
Definition: rstring.h:483
@ RUBY_SPECIAL_SHIFT
Least significant 8 bits are reserved.
Ruby's String.
Definition: rstring.h:231
long len
Length of the string, not including terminating NUL character.
Definition: rstring.h:250
char ary[RSTRING_EMBED_LEN_MAX+1]
When a string is short enough, it uses this area to store the contents themselves.
Definition: rstring.h:298
char * ptr
Pointer to the contents of the string.
Definition: rstring.h:258
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_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition: value_type.h:375