Ruby 4.1.0dev (2026-12-01 revision 289ea2d4a1c4b83f24a63829ae817f7866b37944)
symbol.c (289ea2d4a1c4b83f24a63829ae817f7866b37944)
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 "darray.h"
13#include "internal.h"
14#include "internal/concurrent_set.h"
15#include "internal/error.h"
16#include "internal/gc.h"
17#include "internal/hash.h"
18#include "internal/object.h"
19#include "internal/symbol.h"
20#include "internal/vm.h"
21#include "probes.h"
22#include "ruby/atomic.h"
23#include "ruby/encoding.h"
24#include "ruby/ractor.h"
25#include "ruby/st.h"
26#include "symbol.h"
27#include "vm_core.h"
28#include "vm_sync.h"
29#include "builtin.h"
31
32#if defined(SYMBOL_DEBUG) && (SYMBOL_DEBUG+0)
33# undef SYMBOL_DEBUG
34# define SYMBOL_DEBUG 1
35#else
36# undef SYMBOL_DEBUG
37# define SYMBOL_DEBUG 0
38#endif
39#ifndef CHECK_ID_SERIAL
40# define CHECK_ID_SERIAL SYMBOL_DEBUG
41#endif
42
43#define IDSET_ATTRSET_FOR_SYNTAX ((1U<<ID_LOCAL)|(1U<<ID_CONST))
44#define IDSET_ATTRSET_FOR_INTERN (~(~0U<<(1<<ID_SCOPE_SHIFT)) & ~(1U<<ID_ATTRSET))
45
46#define STATIC_SYM2ID(sym) RSHIFT((VALUE)(sym), RUBY_SPECIAL_SHIFT)
47
48static ID register_static_symid(ID, const char *, rb_len_t, rb_encoding *);
49#define REGISTER_SYMID(id, name) register_static_symid((id), (name), strlen(name), enc)
50#include "id.c"
51
52#define is_identchar(p,e,enc) (ISALNUM((unsigned char)*(p)) || (*(p)) == '_' || !ISASCII(*(p)))
53
54#define op_tbl_count numberof(op_tbl)
55STATIC_ASSERT(op_tbl_name_size, sizeof(op_tbl[0].name) == 3);
56#define op_tbl_len(i) (!op_tbl[i].name[1] ? 1 : !op_tbl[i].name[2] ? 2 : 3)
57
58
59#define GLOBAL_SYMBOLS_LOCKING(symbols) \
60 for (rb_symbols_t *symbols = &ruby_global_symbols, **locking = &symbols; \
61 locking; \
62 locking = NULL) \
63 RB_VM_LOCKING()
64
65static void
66Init_op_tbl(void)
67{
68 int i;
69 rb_encoding *const enc = rb_usascii_encoding();
70
71 for (i = '!'; i <= '~'; ++i) {
72 if (!ISALNUM(i) && i != '_') {
73 char c = (char)i;
74 register_static_symid(i, &c, 1, enc);
75 }
76 }
77 for (i = 0; i < op_tbl_count; ++i) {
78 register_static_symid(op_tbl[i].token, op_tbl[i].name, op_tbl_len(i), enc);
79 }
80}
81
82static const int ID_ENTRY_UNIT = 512;
83
84typedef struct {
85 rb_atomic_t next_id;
86 VALUE sym_set;
87
88 VALUE ids;
90
91rb_symbols_t ruby_global_symbols = {
92 .next_id = tNEXT_ID,
93};
94RUBY_DEBUG_EXPORT(ruby_debug_global_symbols, ruby_global_symbols)
95
97 VALUE sym;
98 VALUE str;
99};
100
101#define SYM_SET_SYM_STATIC_TAG 1
102
103static bool
104sym_set_sym_static_p(VALUE sym)
105{
106 return sym & SYM_SET_SYM_STATIC_TAG;
107}
108
109static VALUE
110sym_set_static_sym_tag(struct sym_set_static_sym_entry *sym)
111{
112 VALUE value = (VALUE)sym | SYM_SET_SYM_STATIC_TAG;
113 RUBY_ASSERT(IMMEDIATE_P(value));
114 RUBY_ASSERT(sym_set_sym_static_p(value));
115
116 return value;
117}
118
119static struct sym_set_static_sym_entry *
120sym_set_static_sym_untag(VALUE sym)
121{
122 RUBY_ASSERT(sym_set_sym_static_p(sym));
123
124 return (struct sym_set_static_sym_entry *)(sym & ~((VALUE)SYM_SET_SYM_STATIC_TAG));
125}
126
127static VALUE
128sym_set_sym_get_str(VALUE sym)
129{
130 VALUE str;
131 if (sym_set_sym_static_p(sym)) {
132 str = sym_set_static_sym_untag(sym)->str;
133 }
134 else {
136 str = RSYMBOL(sym)->fstr;
137 }
138
140
141 return str;
142}
143
144static VALUE
145sym_set_hash(VALUE sym)
146{
147 if (sym_set_sym_static_p(sym)) {
148 return (VALUE)rb_str_hash(sym_set_static_sym_untag(sym)->str);
149 }
150 else {
151 return (VALUE)RSYMBOL(sym)->hashval;
152 }
153}
154
155static bool
156sym_set_cmp(VALUE a, VALUE b)
157{
158 return rb_str_hash_cmp(sym_set_sym_get_str(a), sym_set_sym_get_str(b)) == false;
159}
160
162 VALUE sym;
163 VALUE str;
164};
165
166static void
167sym_id_entry_list_mark(void *ptr)
168{
169 rb_darray(struct sym_id_entry) ary = ptr;
170
171 struct sym_id_entry *entry;
172 rb_darray_foreach(ary, i, entry) {
173 // sym must be pinned because it may be used in places that don't
174 // support compaction
175 rb_gc_mark(entry->sym);
176 rb_gc_mark_movable(entry->str);
177 }
178}
179
180static void
181sym_id_entry_list_free(void *ptr)
182{
183 rb_darray_free_sized(ptr, struct sym_id_entry);
184}
185
186static size_t
187sym_id_entry_list_memsize(const void *ptr)
188{
189 const rb_darray(struct sym_id_entry) ary = ptr;
190
191 return rb_darray_memsize(ary);
192}
193
194static void
195sym_id_entry_list_compact(void *ptr)
196{
197 rb_darray(struct sym_id_entry) ary = ptr;
198
199 struct sym_id_entry *entry;
200 rb_darray_foreach(ary, i, entry) {
201 rb_gc_update_moved(&entry->str);
202 }
203}
204
205static const rb_data_type_t sym_id_entry_list_type = {
206 "symbol_id_entry_list",
207 {
208 sym_id_entry_list_mark,
209 sym_id_entry_list_free,
210 sym_id_entry_list_memsize,
211 sym_id_entry_list_compact,
212 },
213 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED
214};
215
216/* Directory mapping an ID serial (via idx = serial / ID_ENTRY_UNIT) to its
217 * sym_id_entry bucket. */
219 long capa;
220 VALUE *entries;
221};
222
223static void
224id_entry_dir_mark(void *ptr)
225{
226 struct id_entry_dir *dir = ptr;
227 for (long i = 0; i < dir->capa; i++) {
228 if (dir->entries[i]) {
229 rb_gc_mark_movable(dir->entries[i]);
230 }
231 }
232}
233
234static void
235id_entry_dir_free(void *ptr)
236{
237 struct id_entry_dir *dir = ptr;
238 SIZED_FREE_N(dir->entries, dir->capa);
239 xfree(dir);
240}
241
242static size_t
243id_entry_dir_memsize(const void *ptr)
244{
245 const struct id_entry_dir *dir = ptr;
246 return sizeof(struct id_entry_dir) + dir->capa * sizeof(VALUE);
247}
248
249static void
250id_entry_dir_compact(void *ptr)
251{
252 struct id_entry_dir *dir = ptr;
253 for (long i = 0; i < dir->capa; i++) {
254 if (dir->entries[i]) {
255 rb_gc_update_moved(&dir->entries[i]);
256 }
257 }
258}
259
260static const rb_data_type_t id_entry_dir_type = {
261 "symbol_id_entry_directory",
262 {
263 id_entry_dir_mark,
264 id_entry_dir_free,
265 id_entry_dir_memsize,
266 id_entry_dir_compact,
267 },
268 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED
269};
270
271static const long ID_ENTRY_DIR_INITIAL_CAPA = 16;
272
273static VALUE
274id_entry_dir_new(long capa)
275{
276 struct id_entry_dir *dir;
277 VALUE obj = TypedData_Make_Struct(0, struct id_entry_dir, &id_entry_dir_type, dir);
279 dir->entries = ZALLOC_N(VALUE, capa);
280 dir->capa = capa; // must be set after the allocation in case of GC
281 return obj;
282}
283
284static int
285sym_check_asciionly(VALUE str, bool fake_str)
286{
287 if (!rb_enc_asciicompat(rb_enc_get(str))) return FALSE;
288 switch (rb_enc_str_coderange(str)) {
290 if (fake_str) {
291 str = rb_enc_str_new(RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
292 }
293 rb_raise(rb_eEncodingError, "invalid symbol in encoding %s :%+"PRIsVALUE,
294 rb_enc_name(rb_enc_get(str)), str);
296 return TRUE;
297 }
298 return FALSE;
299}
300
301static VALUE
302dup_string_for_create(VALUE str)
303{
304 rb_encoding *enc = rb_enc_get(str);
305
306 str = rb_enc_str_new(RSTRING_PTR(str), RSTRING_LEN(str), enc);
307
308 rb_encoding *ascii = rb_usascii_encoding();
309 if (enc != ascii && sym_check_asciionly(str, false)) {
310 rb_enc_associate(str, ascii);
311 }
312 OBJ_FREEZE(str);
313
314 str = rb_fstring(str);
315 return str;
316}
317
318static int
319rb_str_symname_type(VALUE name, unsigned int allowed_attrset)
320{
321 const char *ptr = StringValuePtr(name);
322 rb_len_t len = RSTRING_LEN(name);
323 int type = rb_enc_symname_type(ptr, len, rb_enc_get(name), allowed_attrset);
324 RB_GC_GUARD(name);
325 return type;
326}
327
328static ID
329next_id_base(void)
330{
331 rb_atomic_t serial = RUBY_ATOMIC_FETCH_ADD(ruby_global_symbols.next_id, 1);
332
333 return (ID)serial << ID_SCOPE_SHIFT;
334}
335
336static struct id_entry_dir *
337id_entry_dir_grow(rb_symbols_t *symbols, struct id_entry_dir *old_dir, size_t min_idx)
338{
339 ASSERT_vm_locking();
340
341 long new_capa = old_dir->capa ? old_dir->capa * 2 : ID_ENTRY_DIR_INITIAL_CAPA;
342 while ((size_t)new_capa <= min_idx) new_capa *= 2;
343
344 // id_entry_dir_new may trigger GC, but the old directory is still reachable
345 // via symbols->ids until we publish below, so it and its buckets stay marked.
346 VALUE new_obj = id_entry_dir_new(new_capa);
347 struct id_entry_dir *new_dir = RTYPEDDATA_GET_DATA(new_obj);
348
349 memcpy(new_dir->entries, old_dir->entries, old_dir->capa * sizeof(VALUE));
350 rb_gc_writebarrier_remember(new_obj);
351
352 // Publish: this release pairs with the acquire load in get_id_serial_entry.
353 // The old directory becomes unreachable from roots, but is kept alive for any
354 // in-flight lock-free reader by that reader's own machine stack
355 rbimpl_atomic_value_store(&symbols->ids, new_obj, RBIMPL_ATOMIC_RELEASE);
356
357 return new_dir;
358}
359
360static void
361set_id_entry(rb_symbols_t *symbols, rb_id_serial_t num, VALUE str, VALUE sym)
362{
363 ASSERT_vm_locking();
368
369 size_t idx = num / ID_ENTRY_UNIT;
370
371 struct id_entry_dir *dir = RTYPEDDATA_GET_DATA(symbols->ids);
372 if (idx >= (size_t)dir->capa) {
373 dir = id_entry_dir_grow(symbols, dir, idx);
374 }
375
376 VALUE bucket = dir->entries[idx];
377 rb_darray(struct sym_id_entry) entries;
378 if (!bucket) {
379 rb_darray_make(&entries, ID_ENTRY_UNIT);
380 bucket = TypedData_Wrap_Struct(0, &sym_id_entry_list_type, entries);
381 /* Reachable from every Ractor via the global symbol table, so mark it shareable. */
382 RB_OBJ_SET_SHAREABLE(bucket);
383 // Publish the (calloc-zeroed) bucket; release pairs with the reader's acquire load.
384 rbimpl_atomic_value_store(&dir->entries[idx], bucket, RBIMPL_ATOMIC_RELEASE);
385 RB_OBJ_WRITTEN(symbols->ids, Qundef, bucket);
386 }
387 else {
388 entries = RTYPEDDATA_GET_DATA(bucket);
389 }
390
391 idx = num % ID_ENTRY_UNIT;
392 struct sym_id_entry *entry = rb_darray_ref(entries, idx);
393 RUBY_ASSERT(entry->str == 0);
394 RUBY_ASSERT(entry->sym == 0);
395
396 RB_OBJ_WRITE(bucket, &entry->str, str);
397 RB_OBJ_WRITE(bucket, &entry->sym, sym);
398}
399
400static VALUE
401sym_set_create(VALUE sym, void *data)
402{
403 bool create_dynamic_symbol = (bool)data;
404
405 struct sym_set_static_sym_entry *static_sym_entry = sym_set_static_sym_untag(sym);
406
407 VALUE str = dup_string_for_create(static_sym_entry->str);
408
409 if (create_dynamic_symbol) {
410 NEWOBJ_OF(obj, struct RSymbol, rb_cSymbol, T_SYMBOL, sizeof(struct RSymbol));
411
412 rb_encoding *enc = rb_enc_get(str);
413 rb_enc_set_index((VALUE)obj, rb_enc_to_index(enc));
414 RB_OBJ_WRITE((VALUE)obj, &obj->fstr, str);
416
417 int id = rb_str_symname_type(str, IDSET_ATTRSET_FOR_INTERN);
418 if (id < 0) id = ID_INTERNAL;
419 obj->id = id;
420
421 obj->hashval = rb_str_hash(str);
422 RUBY_DTRACE_CREATE_HOOK(SYMBOL, RSTRING_PTR(obj->fstr));
423
424 return (VALUE)obj;
425 }
426 else {
427 struct sym_set_static_sym_entry *new_static_sym_entry = xmalloc(sizeof(struct sym_set_static_sym_entry));
428 new_static_sym_entry->str = str;
429
430 VALUE static_sym = static_sym_entry->sym;
431 if (static_sym == 0) {
432 ID id = rb_str_symname_type(str, IDSET_ATTRSET_FOR_INTERN);
433 if (id == (ID)-1) id = ID_INTERNAL;
434
435 id |= next_id_base();
436 id |= ID_STATIC_SYM;
437
438 static_sym = STATIC_ID2SYM(id);
439 }
440 new_static_sym_entry->sym = static_sym;
441
442 RB_VM_LOCKING() {
443 set_id_entry(&ruby_global_symbols, rb_id_to_serial(STATIC_SYM2ID(static_sym)), str, static_sym);
444 }
445
446 return sym_set_static_sym_tag(new_static_sym_entry);
447 }
448}
449
450static void
451sym_set_free(VALUE sym)
452{
453 if (sym_set_sym_static_p(sym)) {
454 xfree(sym_set_static_sym_untag(sym));
455 }
456}
457
458static const struct rb_concurrent_set_funcs sym_set_funcs = {
459 .hash = sym_set_hash,
460 .cmp = sym_set_cmp,
461 .create = sym_set_create,
462 .free = sym_set_free,
463};
464
465static VALUE
466sym_set_entry_to_sym(VALUE entry)
467{
468 if (sym_set_sym_static_p(entry)) {
469 RUBY_ASSERT(STATIC_SYM_P(sym_set_static_sym_untag(entry)->sym));
470
471 if (!STATIC_SYM_P(sym_set_static_sym_untag(entry)->sym)) rb_bug("not sym");
472
473 return sym_set_static_sym_untag(entry)->sym;
474 }
475 else {
477 if (!DYNAMIC_SYM_P(entry)) rb_bug("not sym");
478
479 return entry;
480 }
481}
482
483static VALUE
484sym_find_or_insert_dynamic_symbol(rb_symbols_t *symbols, const VALUE str)
485{
486 struct sym_set_static_sym_entry static_sym = {
487 .str = str
488 };
489 return sym_set_entry_to_sym(
490 rb_concurrent_set_find_or_insert(&symbols->sym_set, sym_set_static_sym_tag(&static_sym), (void *)true)
491 );
492}
493
494static VALUE
495sym_find_or_insert_static_symbol(rb_symbols_t *symbols, const VALUE str)
496{
497 struct sym_set_static_sym_entry static_sym = {
498 .str = str
499 };
500 return sym_set_entry_to_sym(
501 rb_concurrent_set_find_or_insert(&symbols->sym_set, sym_set_static_sym_tag(&static_sym), (void *)false)
502 );
503}
504
505static VALUE
506sym_find_or_insert_static_symbol_id(rb_symbols_t *symbols, const VALUE str, ID id)
507{
508 struct sym_set_static_sym_entry static_sym = {
509 .sym = STATIC_ID2SYM(id),
510 .str = str,
511 };
512 return sym_set_entry_to_sym(
513 rb_concurrent_set_find_or_insert(&symbols->sym_set, sym_set_static_sym_tag(&static_sym), (void *)false)
514 );
515}
516
517void
518Init_sym(void)
519{
520 rb_symbols_t *symbols = &ruby_global_symbols;
521
522 symbols->sym_set = rb_concurrent_set_new(&sym_set_funcs, 1024);
523 symbols->ids = id_entry_dir_new(ID_ENTRY_DIR_INITIAL_CAPA);
524
525 Init_op_tbl();
526 Init_id();
527}
528
529void
530rb_sym_global_symbols_mark_and_move(void)
531{
532 rb_symbols_t *symbols = &ruby_global_symbols;
533
534 rb_gc_mark_and_move(&symbols->sym_set);
535 rb_gc_mark_and_move(&symbols->ids);
536}
537
538static int
539rb_free_global_symbol_table_i(VALUE *sym_ptr, void *data)
540{
541 sym_set_free(*sym_ptr);
542
543 return ST_DELETE;
544}
545
546void
547rb_free_global_symbol_table(void)
548{
549 rb_concurrent_set_foreach_with_replace(ruby_global_symbols.sym_set, rb_free_global_symbol_table_i, NULL);
550}
551
552WARN_UNUSED_RESULT(static ID lookup_str_id(VALUE str));
553WARN_UNUSED_RESULT(static VALUE get_id_str(ID id));
554
555ID
556rb_id_attrset(ID id)
557{
558 int scope;
559
560 if (!is_notop_id(id)) {
561 switch (id) {
562 case tAREF: case tASET:
563 return tASET; /* only exception */
564 }
565 rb_name_error(id, "cannot make operator ID :%"PRIsVALUE" attrset",
566 rb_id2str(id));
567 }
568 else {
569 scope = id_type(id);
570 switch (scope) {
571 case ID_LOCAL: case ID_INSTANCE: case ID_GLOBAL:
572 case ID_CONST: case ID_CLASS: case ID_INTERNAL:
573 break;
574 case ID_ATTRSET:
575 return id;
576 default:
577 {
578 VALUE str = get_id_str(id);
579 if (str != 0) {
580 rb_name_error(id, "cannot make unknown type ID %d:%"PRIsVALUE" attrset",
581 scope, str);
582 }
583 else {
584 rb_name_error_str(Qnil, "cannot make unknown type anonymous ID %d:%"PRIxVALUE" attrset",
585 scope, (VALUE)id);
586 }
587 }
588 }
589 }
590
591 bool error = false;
592 /* make new symbol and ID */
593 VALUE str = get_id_str(id);
594 if (str) {
595 str = rb_str_dup(str);
596 rb_str_cat(str, "=", 1);
597 if (sym_check_asciionly(str, false)) {
598 rb_enc_associate(str, rb_usascii_encoding());
599 }
600
601 VALUE sym = sym_find_or_insert_static_symbol(&ruby_global_symbols, str);
602 id = rb_sym2id(sym);
603 }
604 else {
605 error = true;
606 }
607
608 if (error) {
609 RBIMPL_ATTR_NONSTRING_ARRAY() static const char id_types[][8] = {
610 "local",
611 "instance",
612 "invalid",
613 "global",
614 "attrset",
615 "const",
616 "class",
617 "internal",
618 };
619 rb_name_error(id, "cannot make anonymous %.*s ID %"PRIxVALUE" attrset",
620 (int)sizeof(id_types[0]), id_types[scope], (VALUE)id);
621 }
622
623 return id;
624}
625
626static int
627is_special_global_name(const char *m, const char *e, rb_encoding *enc)
628{
629 int mb = 0;
630
631 if (m >= e) return 0;
632 if (is_global_name_punct(*m)) {
633 ++m;
634 }
635 else if (*m == '-') {
636 if (++m >= e) return 0;
637 if (is_identchar(m, e, enc)) {
638 if (!ISASCII(*m)) mb = 1;
639 m += rb_enc_mbclen(m, e, enc);
640 }
641 }
642 else {
643 if (!ISDIGIT(*m)) return 0;
644 do {
645 if (!ISASCII(*m)) mb = 1;
646 ++m;
647 } while (m < e && ISDIGIT(*m));
648 }
649 return m == e ? mb + 1 : 0;
650}
651
652int
653rb_symname_p(const char *name)
654{
656}
657
658int
659rb_enc_symname_p(const char *name, rb_encoding *enc)
660{
661 return rb_enc_symname2_p(name, strlen(name), enc);
662}
663
664static int
665rb_sym_constant_char_p(const char *name, rb_len_t nlen, rb_encoding *enc)
666{
667 int c, len;
668 const char *end = name + nlen;
669
670 if (nlen < 1) return FALSE;
671 if (ISASCII(*name)) return ISUPPER(*name);
672 c = rb_enc_precise_mbclen(name, end, enc);
673 if (!MBCLEN_CHARFOUND_P(c)) return FALSE;
675 c = rb_enc_mbc_to_codepoint(name, end, enc);
676 if (rb_enc_isupper(c, enc)) return TRUE;
677 if (rb_enc_islower(c, enc)) return FALSE;
678 if (ONIGENC_IS_UNICODE(enc)) {
679 static int ctype_titlecase = 0;
680 if (!ctype_titlecase) {
681 static const UChar cname[] = "titlecaseletter";
682 static const UChar *const end = cname + sizeof(cname) - 1;
683 ctype_titlecase = ONIGENC_PROPERTY_NAME_TO_CTYPE(enc, cname, end);
684 }
685 if (rb_enc_isctype(c, ctype_titlecase, enc)) return TRUE;
686 }
687 else {
688 /* fallback to case-folding */
689 OnigUChar fold[ONIGENC_GET_CASE_FOLD_CODES_MAX_NUM];
690 const OnigUChar *beg = (const OnigUChar *)name;
691 int r = enc->mbc_case_fold(ONIGENC_CASE_FOLD,
692 &beg, (const OnigUChar *)end,
693 fold, enc);
694 if (r > 0 && (r != len || memcmp(fold, name, r)))
695 return TRUE;
696 }
697 return FALSE;
698}
699
701 const enum { invalid, stophere, needmore, } kind;
702 const enum ruby_id_types type;
703 const rb_len_t nread;
704};
705
706#define t struct enc_synmane_type_leading_chars_tag
707
709enc_synmane_type_leading_chars(const char *name, rb_len_t len, rb_encoding *enc, int allowed_attrset)
710{
711 const char *m = name;
712 const char *e = m + len;
713
714 if (! rb_enc_asciicompat(enc)) {
715 return (t) { invalid, 0, 0, };
716 }
717 else if (! m) {
718 return (t) { invalid, 0, 0, };
719 }
720 else if ( len <= 0 ) {
721 return (t) { invalid, 0, 0, };
722 }
723 switch (*m) {
724 case '\0':
725 return (t) { invalid, 0, 0, };
726
727 case '$':
728 if (is_special_global_name(++m, e, enc)) {
729 return (t) { stophere, ID_GLOBAL, len, };
730 }
731 else {
732 return (t) { needmore, ID_GLOBAL, 1, };
733 }
734
735 case '@':
736 switch (*++m) {
737 default: return (t) { needmore, ID_INSTANCE, 1, };
738 case '@': return (t) { needmore, ID_CLASS, 2, };
739 }
740
741 case '<':
742 switch (*++m) {
743 default: return (t) { stophere, ID_INTERNAL, 1, };
744 case '<': return (t) { stophere, ID_INTERNAL, 2, };
745 case '=':
746 switch (*++m) {
747 default: return (t) { stophere, ID_INTERNAL, 2, };
748 case '>': return (t) { stophere, ID_INTERNAL, 3, };
749 }
750 }
751
752 case '>':
753 switch (*++m) {
754 default: return (t) { stophere, ID_INTERNAL, 1, };
755 case '>': case '=': return (t) { stophere, ID_INTERNAL, 2, };
756 }
757
758 case '=':
759 switch (*++m) {
760 default: return (t) { invalid, 0, 1, };
761 case '~': return (t) { stophere, ID_INTERNAL, 2, };
762 case '=':
763 switch (*++m) {
764 default: return (t) { stophere, ID_INTERNAL, 2, };
765 case '=': return (t) { stophere, ID_INTERNAL, 3, };
766 }
767 }
768
769 case '*':
770 switch (*++m) {
771 default: return (t) { stophere, ID_INTERNAL, 1, };
772 case '*': return (t) { stophere, ID_INTERNAL, 2, };
773 }
774
775 case '+': case '-':
776 switch (*++m) {
777 default: return (t) { stophere, ID_INTERNAL, 1, };
778 case '@': return (t) { stophere, ID_INTERNAL, 2, };
779 }
780
781 case '|': case '^': case '&': case '/': case '%': case '~': case '`':
782 return (t) { stophere, ID_INTERNAL, 1, };
783
784 case '[':
785 switch (*++m) {
786 default: return (t) { needmore, ID_INTERNAL, 0, };
787 case ']':
788 switch (*++m) {
789 default: return (t) { stophere, ID_INTERNAL, 2, };
790 case '=': return (t) { stophere, ID_INTERNAL, 3, };
791 }
792 }
793
794 case '!':
795 switch (*++m) {
796 case '=': case '~': return (t) { stophere, ID_INTERNAL, 2, };
797 default:
798 if (allowed_attrset & (1U << ID_INTERNAL)) {
799 return (t) { needmore, ID_INTERNAL, 1, };
800 }
801 else {
802 return (t) { stophere, ID_INTERNAL, 1, };
803 }
804 }
805
806 default:
807 if (rb_sym_constant_char_p(name, len, enc)) {
808 return (t) { needmore, ID_CONST, 0, };
809 }
810 else {
811 return (t) { needmore, ID_LOCAL, 0, };
812 }
813 }
814}
815#undef t
816
817int
818rb_enc_symname_type(const char *name, rb_len_t len, rb_encoding *enc, unsigned int allowed_attrset)
819{
821 enc_synmane_type_leading_chars(name, len, enc, allowed_attrset);
822 const char *m = name + f.nread;
823 const char *e = name + len;
824 int type = (int)f.type;
825
826 switch (f.kind) {
827 case invalid: return -1;
828 case stophere: break;
829 case needmore:
830
831 if (m >= e || (*m != '_' && !ISALPHA(*m) && ISASCII(*m))) {
832 if (len > 1 && *(e-1) == '=') {
833 type = rb_enc_symname_type(name, len-1, enc, allowed_attrset);
834 if (type != -1 && (allowed_attrset & (1U << type))) return ID_ATTRSET;
835 }
836 return -1;
837 }
838 while (m < e && is_identchar(m, e, enc)) m += rb_enc_mbclen(m, e, enc);
839 if (m >= e) break;
840 switch (*m) {
841 case '!': case '?':
842 if (type == ID_GLOBAL || type == ID_CLASS || type == ID_INSTANCE) return -1;
843 type = ID_INTERNAL;
844 ++m;
845 if (m + 1 < e || *m != '=') break;
846 /* fall through */
847 case '=':
848 if (!(allowed_attrset & (1U << type))) return -1;
849 type = ID_ATTRSET;
850 ++m;
851 break;
852 }
853 }
854
855 return m == e ? type : -1;
856}
857
858int
859rb_enc_symname2_p(const char *name, rb_len_t len, rb_encoding *enc)
860{
861 return rb_enc_symname_type(name, len, enc, IDSET_ATTRSET_FOR_SYNTAX) != -1;
862}
863
864static struct sym_id_entry *
865get_id_serial_entry(rb_id_serial_t num)
866{
867 struct sym_id_entry *entry = NULL;
868
869 if (num && num < RUBY_ATOMIC_LOAD(ruby_global_symbols.next_id)) {
870 size_t idx = num / ID_ENTRY_UNIT;
871
872 VALUE dir_obj = rbimpl_atomic_value_load(&ruby_global_symbols.ids, RBIMPL_ATOMIC_ACQUIRE);
873 struct id_entry_dir *dir = RTYPEDDATA_GET_DATA(dir_obj);
874
875 if (idx < (size_t)dir->capa) {
876 VALUE bucket = rbimpl_atomic_value_load(&dir->entries[idx], RBIMPL_ATOMIC_ACQUIRE);
877 if (bucket) {
878 rb_darray(struct sym_id_entry) entries = RTYPEDDATA_GET_DATA(bucket);
879
880 size_t pos = (size_t)(num % ID_ENTRY_UNIT);
881 RUBY_ASSERT(pos < rb_darray_size(entries));
882 entry = rb_darray_ref(entries, pos);
883 }
884 }
885
886 RB_GC_GUARD(dir_obj);
887 }
888
889 return entry;
890}
891
892static VALUE
893get_id_sym(ID id)
894{
895 struct sym_id_entry *entry = get_id_serial_entry(rb_id_to_serial(id));
896 return entry ? entry->sym : 0;
897}
898
899static VALUE
900get_id_str(ID id)
901{
902 struct sym_id_entry *entry = get_id_serial_entry(rb_id_to_serial(id));
903 return entry ? entry->str : 0;
904}
905
906int
907rb_static_id_valid_p(ID id)
908{
909 return STATIC_ID2SYM(id) == get_id_sym(id);
910}
911
912static inline ID
913id_serial_to_id(rb_id_serial_t num)
914{
915 if (is_notop_id((ID)num)) {
916 struct sym_id_entry *entry = get_id_serial_entry(num);
917 if (entry && entry->sym != 0) {
918 return SYM2ID(entry->sym);
919 }
920 else {
921 return ((ID)num << ID_SCOPE_SHIFT) | ID_INTERNAL | ID_STATIC_SYM;
922 }
923 }
924 else {
925 return (ID)num;
926 }
927}
928
929ID
930rb_id_serial_to_id(rb_id_serial_t num)
931{
932 return id_serial_to_id(num);
933}
934
935static ID
936register_static_symid(ID id, const char *name, rb_len_t len, rb_encoding *enc)
937{
938 VALUE str = rb_enc_str_new(name, len, enc);
939 OBJ_FREEZE(str);
940 str = rb_fstring(str);
941
942 RUBY_DTRACE_CREATE_HOOK(SYMBOL, RSTRING_PTR(str));
943
944 sym_find_or_insert_static_symbol_id(&ruby_global_symbols, str, id);
945
946 return id;
947}
948
949static VALUE
950sym_find(VALUE str)
951{
952 VALUE sym;
953
954 struct sym_set_static_sym_entry static_sym = {
955 .str = str
956 };
957 sym = rb_concurrent_set_find(&ruby_global_symbols.sym_set, sym_set_static_sym_tag(&static_sym));
958
959 if (sym) {
960 return sym_set_entry_to_sym(sym);
961 }
962 else {
963 return 0;
964 }
965}
966
967/* A dynamic symbol's id is assigned lazily by rb_sym2id, which performs a release store of
968 * id after set_id_entry has populated the serial->{str,sym} entry. */
969STATIC_ASSERT(value_id_same_width, sizeof(VALUE) == sizeof(ID));
970static inline ID
971sym_id_load_acquire(VALUE sym)
972{
973 return (ID)rbimpl_atomic_value_load((VALUE *)&RSYMBOL(sym)->id, RBIMPL_ATOMIC_ACQUIRE);
974}
975
976static ID
977lookup_str_id(VALUE str)
978{
979 VALUE sym = sym_find(str);
980
981 if (sym == 0) {
982 return (ID)0;
983 }
984
985 if (STATIC_SYM_P(sym)) {
986 return STATIC_SYM2ID(sym);
987 }
988 else if (DYNAMIC_SYM_P(sym)) {
989 ID id = sym_id_load_acquire(sym);
990 if (id & ~ID_SCOPE_MASK) return id;
991 }
992 else {
993 rb_bug("non-symbol object %s:%"PRIxVALUE" for %"PRIsVALUE" in symbol table",
994 rb_builtin_class_name(sym), sym, str);
995 }
996
997 return (ID)0;
998}
999
1000ID
1001rb_intern3(const char *name, rb_len_t len, rb_encoding *enc)
1002{
1003 struct RString fake_str = {RBASIC_INIT};
1004 VALUE str = rb_setup_fake_str(&fake_str, name, len, enc);
1005 OBJ_FREEZE(str);
1006
1007 VALUE sym = sym_find_or_insert_static_symbol(&ruby_global_symbols, str);
1008 return rb_sym2id(sym);
1009}
1010
1011ID
1012rb_intern2(const char *name, rb_len_t len)
1013{
1014 return rb_intern3(name, len, rb_usascii_encoding());
1015}
1016
1017#undef rb_intern
1018ID
1019rb_intern(const char *name)
1020{
1021 return rb_intern2(name, strlen(name));
1022}
1023
1024ID
1025rb_intern_str(VALUE str)
1026{
1027 VALUE sym = sym_find_or_insert_static_symbol(&ruby_global_symbols, str);
1028 return SYM2ID(sym);
1029}
1030
1031bool
1032rb_obj_is_symbol_table(VALUE obj)
1033{
1034 return obj == ruby_global_symbols.sym_set;
1035}
1036
1038 int (*callback)(VALUE *key, void *data);
1039 void *data;
1040};
1041
1042static int
1043rb_sym_global_symbol_table_foreach_weak_reference_i(VALUE *key, void *d)
1044{
1046 VALUE sym = *key;
1047
1048 if (sym_set_sym_static_p(sym)) {
1049 struct sym_set_static_sym_entry *static_sym = sym_set_static_sym_untag(sym);
1050
1051 return data->callback(&static_sym->str, data->data);
1052 }
1053 else {
1054 return data->callback(key, data->data);
1055 }
1056}
1057
1058void
1059rb_sym_global_symbol_table_foreach_weak_reference(int (*callback)(VALUE *key, void *data), void *data)
1060{
1061 if (!ruby_global_symbols.sym_set) return;
1062
1064 .callback = callback,
1065 .data = data,
1066 };
1067
1068 rb_concurrent_set_foreach_with_replace(ruby_global_symbols.sym_set, rb_sym_global_symbol_table_foreach_weak_reference_i, &foreach_data);
1069}
1070
1071void
1072rb_gc_free_dsymbol(VALUE sym)
1073{
1074 VALUE str = RSYMBOL(sym)->fstr;
1075
1076 if (str) {
1077 rb_concurrent_set_delete_by_identity(ruby_global_symbols.sym_set, sym);
1078
1079 RSYMBOL(sym)->fstr = 0;
1080 }
1081}
1082
1083/*
1084 * call-seq:
1085 * intern -> symbol
1086 *
1087 * Returns the Symbol object derived from +self+,
1088 * creating it if it did not already exist:
1089 *
1090 * 'foo'.intern # => :foo
1091 * 'こんにちは'.intern # => :こんにちは
1092 *
1093 * Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non-String].
1094 *
1095 */
1096
1097VALUE
1099{
1100 return sym_find_or_insert_dynamic_symbol(&ruby_global_symbols, str);
1101}
1102
1103ID
1105{
1106 ID id = 0;
1107 if (STATIC_SYM_P(sym)) {
1108 id = STATIC_SYM2ID(sym);
1109 }
1110 else if (DYNAMIC_SYM_P(sym)) {
1111 id = sym_id_load_acquire(sym);
1112 if (LIKELY(id & ~ID_SCOPE_MASK)) {
1113 return id;
1114 }
1115
1116 GLOBAL_SYMBOLS_LOCKING(symbols) {
1117 RUBY_ASSERT(!rb_objspace_garbage_object_p(sym));
1118 id = RSYMBOL(sym)->id;
1119
1120 if (UNLIKELY(!(id & ~ID_SCOPE_MASK))) { // double-checked
1121 VALUE fstr = RSYMBOL(sym)->fstr;
1122 ID num = next_id_base();
1123 id |= num;
1124
1125 /* Populate the serial->{str,sym} entry before publishing the pinned id below.
1126 * The publish is a release store paired with the acquire in sym_id_load_acquire
1127 * so a lock-free reader that observes the pinned id is guaranteed to see this entry. */
1128 set_id_entry(symbols, rb_id_to_serial(num), fstr, sym);
1129 rbimpl_atomic_value_store((VALUE *)&RSYMBOL(sym)->id, (VALUE)id, RBIMPL_ATOMIC_RELEASE);
1130 }
1131 }
1132 }
1133 else {
1134 rb_raise(rb_eTypeError, "wrong argument type %s (expected Symbol)",
1135 rb_builtin_class_name(sym));
1136 }
1137 return id;
1138}
1139
1140#undef rb_id2sym
1141VALUE
1143{
1144 RUBY_ASSERT(x != 0);
1145 if (!DYNAMIC_ID_P(x)) return STATIC_ID2SYM(x);
1146 return get_id_sym(x);
1147}
1148
1149/*
1150 * call-seq:
1151 * name -> string
1152 *
1153 * Returns a frozen string representation of +self+ (not including the leading colon):
1154 *
1155 * :foo.name # => "foo"
1156 * :foo.name.frozen? # => true
1157 *
1158 * Related: Symbol#to_s, Symbol#inspect.
1159 */
1160
1161VALUE
1163{
1164 VALUE str;
1165 if (DYNAMIC_SYM_P(sym)) {
1166 str = RSYMBOL(sym)->fstr;
1168 }
1169 else {
1170 str = rb_id2str(STATIC_SYM2ID(sym));
1171 if (str) RUBY_ASSERT_BUILTIN_TYPE(str, T_STRING);
1172 }
1173
1174 return str;
1175}
1176
1177VALUE
1178rb_id2str(ID id)
1179{
1180 return get_id_str(id);
1181}
1182
1183const char *
1184rb_id2name(ID id)
1185{
1186 VALUE str = rb_id2str(id);
1187
1188 if (!str) return 0;
1189 return RSTRING_PTR(str);
1190}
1191
1192ID
1193rb_make_internal_id(void)
1194{
1195 return next_id_base() | ID_INTERNAL | ID_STATIC_SYM;
1196}
1197
1198ID
1199rb_make_temporary_id(size_t n)
1200{
1201 const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
1202 const ID id = max_id - (ID)n;
1203 if (id < RUBY_ATOMIC_LOAD(ruby_global_symbols.next_id)) {
1204 rb_raise(rb_eRuntimeError, "too big to make temporary ID: %" PRIdSIZE, n);
1205 }
1206 return (id << ID_SCOPE_SHIFT) | ID_STATIC_SYM | ID_INTERNAL;
1207}
1208
1209static int
1210symbols_i(VALUE *key, void *data)
1211{
1212 VALUE ary = (VALUE)data;
1213 VALUE sym = (VALUE)*key;
1214
1215 if (sym_set_sym_static_p(sym)) {
1216 rb_ary_push(ary, sym_set_static_sym_untag(sym)->sym);
1217 }
1218 else if (rb_objspace_garbage_object_p(sym)) {
1219 return ST_DELETE;
1220 }
1221 else {
1222 rb_ary_push(ary, sym);
1223 }
1224
1225 return ST_CONTINUE;
1226}
1227
1228VALUE
1230{
1231 VALUE ary;
1232
1233 GLOBAL_SYMBOLS_LOCKING(symbols) {
1234 rb_vm_barrier();
1235 ary = rb_ary_new2(rb_concurrent_set_size(symbols->sym_set));
1236 rb_concurrent_set_foreach_with_replace(symbols->sym_set, symbols_i, (void *)ary);
1237 }
1238
1239 return ary;
1240}
1241
1242size_t
1243rb_sym_immortal_count(void)
1244{
1245 return (size_t)(RUBY_ATOMIC_LOAD(ruby_global_symbols.next_id) - 1);
1246}
1247
1248int
1250{
1251 return is_const_id(id);
1252}
1253
1254int
1256{
1257 return is_class_id(id);
1258}
1259
1260int
1262{
1263 return is_global_id(id);
1264}
1265
1266int
1268{
1269 return is_instance_id(id);
1270}
1271
1272int
1274{
1275 return is_attrset_id(id);
1276}
1277
1278int
1280{
1281 return is_local_id(id);
1282}
1283
1284int
1286{
1287 return is_internal_id(id);
1288}
1289
1290int
1291rb_is_const_sym(VALUE sym)
1292{
1293 return is_const_sym(sym);
1294}
1295
1296int
1297rb_is_attrset_sym(VALUE sym)
1298{
1299 return is_attrset_sym(sym);
1300}
1301
1302ID
1303rb_check_id(volatile VALUE *namep)
1304{
1305 VALUE tmp;
1306 VALUE name = *namep;
1307
1308 if (STATIC_SYM_P(name)) {
1309 return STATIC_SYM2ID(name);
1310 }
1311 else if (DYNAMIC_SYM_P(name)) {
1312 ID id = sym_id_load_acquire(name);
1313 if (id & ~ID_SCOPE_MASK) {
1314 return id;
1315 }
1316 else {
1317 *namep = RSYMBOL(name)->fstr;
1318 return 0;
1319 }
1320 }
1321 else if (!RB_TYPE_P(name, T_STRING)) {
1322 tmp = rb_check_string_type(name);
1323 if (NIL_P(tmp)) {
1324 rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol nor a string",
1325 name);
1326 }
1327 name = tmp;
1328 *namep = name;
1329 }
1330
1331 sym_check_asciionly(name, false);
1332
1333 return lookup_str_id(name);
1334}
1335
1336// Used by yjit for handling .send without throwing exceptions
1337ID
1338rb_get_symbol_id(VALUE name)
1339{
1340 if (STATIC_SYM_P(name)) {
1341 return STATIC_SYM2ID(name);
1342 }
1343 else if (DYNAMIC_SYM_P(name)) {
1344 ID id = sym_id_load_acquire(name);
1345 if (id & ~ID_SCOPE_MASK) {
1346 return id;
1347 }
1348 else {
1349 return 0;
1350 }
1351 }
1352 else if (RB_TYPE_P(name, T_STRING)) {
1353 return lookup_str_id(name);
1354 }
1355 else {
1356 return 0;
1357 }
1358}
1359
1360
1361VALUE
1362rb_check_symbol(volatile VALUE *namep)
1363{
1364 VALUE sym;
1365 VALUE tmp;
1366 VALUE name = *namep;
1367
1368 if (STATIC_SYM_P(name)) {
1369 return name;
1370 }
1371 else if (DYNAMIC_SYM_P(name)) {
1372 RUBY_ASSERT(!rb_objspace_garbage_object_p(name));
1373 return name;
1374 }
1375 else if (!RB_TYPE_P(name, T_STRING)) {
1376 tmp = rb_check_string_type(name);
1377 if (NIL_P(tmp)) {
1378 rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol nor a string",
1379 name);
1380 }
1381 name = tmp;
1382 *namep = name;
1383 }
1384
1385 sym_check_asciionly(name, false);
1386
1387 if ((sym = sym_find(name)) != 0) {
1388 return sym;
1389 }
1390
1391 return Qnil;
1392}
1393
1394ID
1395rb_check_id_cstr(const char *ptr, rb_len_t len, rb_encoding *enc)
1396{
1397 struct RString fake_str = {RBASIC_INIT};
1398 const VALUE name = rb_setup_fake_str(&fake_str, ptr, len, enc);
1399
1400 sym_check_asciionly(name, true);
1401
1402 return lookup_str_id(name);
1403}
1404
1405VALUE
1406rb_check_symbol_cstr(const char *ptr, rb_len_t len, rb_encoding *enc)
1407{
1408 VALUE sym;
1409 struct RString fake_str = {RBASIC_INIT};
1410 const VALUE name = rb_setup_fake_str(&fake_str, ptr, len, enc);
1411
1412 sym_check_asciionly(name, true);
1413
1414 if ((sym = sym_find(name)) != 0) {
1415 return sym;
1416 }
1417
1418 return Qnil;
1419}
1420
1421#undef rb_sym_intern_ascii_cstr
1422#ifdef __clang__
1423NOINLINE(VALUE rb_sym_intern(const char *ptr, rb_len_t len, rb_encoding *enc));
1424#else
1425FUNC_MINIMIZED(VALUE rb_sym_intern(const char *ptr, rb_len_t len, rb_encoding *enc));
1426FUNC_MINIMIZED(VALUE rb_sym_intern_ascii(const char *ptr, rb_len_t len));
1427FUNC_MINIMIZED(VALUE rb_sym_intern_ascii_cstr(const char *ptr));
1428#endif
1429
1430VALUE
1431rb_sym_intern(const char *ptr, rb_len_t len, rb_encoding *enc)
1432{
1433 struct RString fake_str = {RBASIC_INIT};
1434 const VALUE name = rb_setup_fake_str(&fake_str, ptr, len, enc);
1435 return rb_str_intern(name);
1436}
1437
1438VALUE
1439rb_sym_intern_ascii(const char *ptr, rb_len_t len)
1440{
1441 return rb_sym_intern(ptr, len, rb_usascii_encoding());
1442}
1443
1444VALUE
1445rb_sym_intern_ascii_cstr(const char *ptr)
1446{
1447 return rb_sym_intern_ascii(ptr, strlen(ptr));
1448}
1449
1450VALUE
1451rb_to_symbol_type(VALUE obj)
1452{
1453 return rb_convert_type_with_id(obj, T_SYMBOL, "Symbol", idTo_sym);
1454}
1455
1456int
1457rb_is_const_name(VALUE name)
1458{
1459 return rb_str_symname_type(name, 0) == ID_CONST;
1460}
1461
1462int
1463rb_is_class_name(VALUE name)
1464{
1465 return rb_str_symname_type(name, 0) == ID_CLASS;
1466}
1467
1468int
1469rb_is_instance_name(VALUE name)
1470{
1471 return rb_str_symname_type(name, 0) == ID_INSTANCE;
1472}
1473
1474int
1475rb_is_local_name(VALUE name)
1476{
1477 return rb_str_symname_type(name, 0) == ID_LOCAL;
1478}
1479
1480#include "id_table.c"
1481#include "symbol.rbinc"
Atomic operations.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
Definition atomic.h:118
#define RUBY_ATOMIC_LOAD(var)
Atomic load.
Definition atomic.h:175
#define ISUPPER
@old{rb_isupper}
Definition ctype.h:89
#define ISDIGIT
@old{rb_isdigit}
Definition ctype.h:93
#define ISALPHA
@old{rb_isalpha}
Definition ctype.h:92
#define ISALNUM
@old{rb_isalnum}
Definition ctype.h:91
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 xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define Qundef
Old name of RUBY_Qundef.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:131
#define SYM2ID
Old name of RB_SYM2ID.
Definition symbol.h:45
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
#define ZALLOC_N
Old name of RB_ZALLOC_N.
Definition memory.h:404
#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:517
#define DYNAMIC_SYM_P
Old name of RB_DYNAMIC_SYM_P.
Definition value_type.h:86
#define Qnil
Old name of RUBY_Qnil.
#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:516
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
#define IMMEDIATE_P
Old name of RB_IMMEDIATE_P.
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:657
void rb_name_error(ID id, const char *fmt,...)
Raises an instance of rb_eNameError.
Definition error.c:2425
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1457
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:2440
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1455
VALUE rb_eEncodingError
EncodingError exception.
Definition error.c:1463
VALUE rb_cSymbol
Symbol class.
Definition string.c:86
#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:504
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:492
#define RUBY_ASSERT_BUILTIN_TYPE(obj, type)
A variant of RUBY_ASSERT that asserts when either RUBY_DEBUG or built-in type of obj is type.
Definition assert.h:291
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
Encoding relates APIs.
rb_encoding * rb_ascii8bit_encoding(void)
Queries the encoding that represents ASCII-8BIT a.k.a.
Definition encoding.c:1626
rb_encoding * rb_usascii_encoding(void)
Queries the encoding that represents US-ASCII.
Definition encoding.c:1650
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:659
ID rb_check_id_cstr(const char *ptr, rb_len_t 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:1395
VALUE rb_check_symbol_cstr(const char *ptr, rb_len_t len, rb_encoding *enc)
Identical to rb_check_id_cstr(), except for the return type.
Definition symbol.c:1406
int rb_enc_symname2_p(const char *name, rb_len_t len, rb_encoding *enc)
Identical to rb_enc_symname_p(), except it additionally takes the passed string's length.
Definition symbol.c:859
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
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:1229
int rb_is_global_id(ID id)
Classifies the given ID, then sees if it is a global variable.
Definition symbol.c:1261
int rb_is_instance_id(ID id)
Classifies the given ID, then sees if it is an instance variable.
Definition symbol.c:1267
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
Definition symbol.c:1249
int rb_is_junk_id(ID)
Classifies the given ID, then sees if it is a junk ID.
Definition symbol.c:1285
int rb_symname_p(const char *str)
Sees if the passed C string constructs a valid syntactic symbol.
Definition symbol.c:653
int rb_is_class_id(ID id)
Classifies the given ID, then sees if it is a class variable.
Definition symbol.c:1255
int rb_is_attrset_id(ID id)
Classifies the given ID, then sees if it is an attribute writer.
Definition symbol.c:1273
int rb_is_local_id(ID id)
Classifies the given ID, then sees if it is a local variable.
Definition symbol.c:1279
int rb_str_hash_cmp(VALUE str1, VALUE str2)
Compares two strings.
Definition string.c:4379
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:2057
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
Definition string.c:4365
VALUE rb_str_cat(VALUE dst, const char *src, rb_len_t srclen)
Destructively appends the passed contents to the string.
Definition string.c:3762
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:3075
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition symbol.c:1098
VALUE rb_check_symbol(volatile VALUE *namep)
Identical to rb_check_id(), except it returns an instance of rb_cSymbol instead.
Definition symbol.c:1362
VALUE rb_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
Definition symbol.c:1142
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition symbol.c:1303
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:1162
ID rb_sym2id(VALUE obj)
Converts an instance of rb_cSymbol into an ID.
Definition symbol.c:1104
int capa
Designed capacity of the buffer.
Definition io.h:11
int len
Length of the buffer.
Definition io.h:8
#define RB_OBJ_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
Definition ractor.h:290
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
Definition ractor.h:255
static VALUE RB_OBJ_SET_FROZEN_SHAREABLE(VALUE obj)
Freezes and marks the object as shareable.
Definition ractor.h:301
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:170
VALUE type(ANYARGS)
ANYARGS-ed function type.
Defines RBIMPL_ATTR_NONSTRING.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
Definition rstring.h:76
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:557
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:239
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
Definition rtypeddata.h:604
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
Ruby's String.
Definition rstring.h:196
Definition symbol.c:218
Definition symbol.c:161
Definition symbol.c:96
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:142
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:376