Debian Codegraph @

vendor/mruby-source/doc/guides/symbol.md

raw
1 # Symbols
2
3 Symbols in `mruby` C source code is represented by `mrb_sym` which is alias of
4 `uint32_t`. Lower 30 bits are used for symbols so that higher 2 bits can be
5 used as flags, e.g. `struct mt_elem` in `class.c`.
6
7 ```c
8 struct mt_elem {
9 union mt_ptr ptr;
10 size_t func_p:1;
11 size_t noarg_p:1;
12 mrb_sym key:sizeof(mrb_sym)*8-2;
13 };
14 ```
15
16 ## C API
17
18 We provide following C API for symbols.
19
20 ### Generate Symbols
21
22 #### `mrb_sym mrb_intern(mrb_state*,const char*,size_t)`
23
24 Get a symbol from a string.
25
26 #### `mrb_sym mrb_intern_check_cstr(mrb_state*,const char*)`
27
28 Get a symbol from a NULL terminated (C) string.
29
30 #### `mrb_sym mrb_intern_str(mrb_state*,mrb_value)`
31
32 Get a symbol from a Ruby string object.
33
34 #### `mrb_intern_lit(mrb_state*,const char*)`
35
36 Get a symbol from a C string literal. The second argument should be a C string
37 literal, otherwise you will get a compilation error. It does not copy C string
38 given the fact it's a literal.
39
40 #### `mrb_sym mrb_intern_check(mrb_state*,const char*,size_t)`
41
42 Get a symbol from a string if the string has been already registered as a
43 symbol, otherwise return `0`. We also provide variants `mrb_intern_check_str()`
44 (from Ruby string) and `mrb_intern_check_cstr()` (from C string).
45
46 #### `const char *mrb_sym_name(mrb_state*,mrb_sym)`
47
48 Get a string representation of a symbol as a C string.
49
50 #### `const char *mrb_sym_name_len(mrb_state*,mrb_sym,mrb_int*)`
51
52 Get a string representation of a symbol, and its length.
53
54 ## Preallocate Symbols
55
56 To save RAM, `mruby` can use compile-time allocation of some symbols. You can
57 use following macros to get preallocated symbols by including `mruby/presym.h`
58 header.
59
60 - `MRB_SYM(xor)` //=> xor (Word characters)
61 - `MRB_SYM_B(xor)` //=> xor! (Method with Bang)
62 - `MRB_SYM_Q(xor)` //=> xor? (Method with Question mark)
63 - `MRB_SYM_E(xor)` //=> xor= (Method with Equal)
64 - `MRB_CVSYM(xor)` //=> @@xor (Class Variable)
65 - `MRB_IVSYM(xor)` //=> @xor (Instance Variable)
66 - `MRB_OPSYM(xor)` //=> ^ (Operator)
67
68 For `MRB_OPSYM()`, specify the names corresponding to operators (see
69 `MRuby::Presym::OPERATORS` in `lib/mruby/presym.rb` for the names that
70 can be specified for it). Other than that, describe only word characters
71 excluding leading and ending punctuations.
72
73 These macros are converted to static symbol IDs at compile time, unless
74 preallocate symbols are disabled by `conf.disable_presym`. In that case,
75 these macros are expanded to `mrb_intern_lit` calls, therefore the mruby state
76 variable is required. The above macros assume the variable name is `mrb`. If
77 its name is not `mrb`, you need to use macros with `_2` suffix, such as
78 `MRB_SYM_2` to specify `mrb_state*` variable.
79
80 ### Disabling Preallocated Symbols
81
82 You can disable preallocated symbols by specifying `conf.disable_presym` in the
83 configuration file.