mruby/c VM Source Code master (2026/08/06)
Loading...
Searching...
No Matches
global.c
Go to the documentation of this file.
1
14
15/***** Feature test switches ************************************************/
16/***** System headers *******************************************************/
17//@cond
18#include "vm_config.h"
19//@endcond
20
21/***** Local headers ********************************************************/
22#include "mrubyc.h"
23
24/***** Constat values *******************************************************/
25/***** Macros ***************************************************************/
26/***** Typedefs *************************************************************/
27/***** Function prototypes **************************************************/
28/***** Local variables ******************************************************/
31
32/***** Global variables *****************************************************/
33/***** Signal catching functions ********************************************/
34/***** Local functions ******************************************************/
35/***** Global functions *****************************************************/
36
37//================================================================
41{
44#if defined(MRBC_DEBUG)
45 handle_const.data->obj_mark_[0] = 'C'; // "CV"
46#endif
47}
48
49
50//================================================================
58{
59 if( mrbc_kv_get( &handle_const, sym_id ) != NULL ) {
60 mrbc_printf("warning: already initialized constant %s\n",
61 mrbc_symid_to_str(sym_id));
62 }
63
64 return mrbc_kv_set( &handle_const, sym_id, v );
65}
66
67
68//================================================================
77{
79
80 mrbc_make_nested_symbol_s( buf, cls->sym_id, sym_id );
81 mrbc_sym id = mrbc_symbol( mrbc_symbol_new( 0, buf ));
82
83 return mrbc_set_const( id, v );
84}
85
86
87//================================================================
94{
95 return mrbc_kv_get( &handle_const, sym_id );
96}
97
98
99//================================================================
107{
109
110 mrbc_make_nested_symbol_s( buf, cls->sym_id, sym_id );
111 mrbc_sym id = mrbc_search_symid(buf);
112 if( id <= 0 ) return 0;
113
114 return mrbc_kv_get( &handle_const, id );
115}
116
117
118//================================================================
125{
127 int flag_object_class = (cls == MRBC_CLASS(Object));
128
129 while( mrbc_kv_i_has_next( &ite ) ) {
130 const mrbc_kv *kv = mrbc_kv_i_next( &ite );
131
132 if( mrbc_is_nested_symid(kv->sym_id) ) {
133 mrbc_sym id1, id2;
134
135 mrbc_separate_nested_symid( kv->sym_id, &id1, &id2 );
136 if( id1 == cls->sym_id ) {
138 }
139
140 } else if( flag_object_class ) {
142 }
143 }
144}
145
146
147//================================================================
155{
156 return mrbc_kv_set( &handle_global, sym_id, v );
157}
158
159
160//================================================================
167{
168 return mrbc_kv_get( &handle_global, sym_id );
169}
170
171
172#if defined(MRBC_ALLOC_VMID)
173//================================================================
176void mrbc_global_clear_vm_id(void)
177{
178 mrbc_kv_clear_vm_id( &handle_const );
179 mrbc_kv_clear_vm_id( &handle_global );
180}
181#endif
182
183
184#ifdef MRBC_DEBUG
185//================================================================
191void mrbc_debug_dump_const( void )
192{
193 mrbc_print("<< Const table dump. >>\n(symid:identifier = value)\n");
195
196 while( mrbc_kv_i_has_next( &ite ) ) {
197 const mrbc_kv *kv = mrbc_kv_i_next( &ite );
198 const char *s = mrbc_symid_to_str(kv->sym_id);
199
200 if( kv->sym_id < 512 ) continue; // OFFSET_BUILTIN_SYMBOL in symbol.c
201
202 mrbc_printf(" $%04x:\"%s\"", kv->sym_id, s );
203 if( mrbc_is_nested_symid(kv->sym_id) ) {
204 mrbc_printf("(");
206 mrbc_printf(")");
207 }
208
209 if( mrbc_type(kv->value) == MRBC_TT_CLASS ) {
210 const mrbc_class *cls = kv->value.cls;
211 mrbc_printf(" = Class(symid=$%x name=", cls->sym_id);
213 mrbc_printf(")\n");
214 continue;
215 }
216
217 if( mrbc_type(kv->value) == MRBC_TT_MODULE ) {
218 const mrbc_class *cls = kv->value.cls;
219 mrbc_printf(" = Module(symid=$%x name=", cls->sym_id);
221 mrbc_printf(")\n");
222 continue;
223 }
224
225 mrbc_printf(" = ");
226 mrbc_p_sub( &kv->value );
228 mrbc_printf(".tt=%d\n", mrbc_type(kv->value));
229 } else {
230 mrbc_printf(".tt=%d.ref=%d\n", mrbc_type(kv->value), kv->value.obj->ref_count);
231 }
232 }
233}
234
235
236//================================================================
242void mrbc_debug_dump_global( void )
243{
244 mrbc_print("<< Global table dump. >>\n(symid:identifier = value)\n");
245
247 while( mrbc_kv_i_has_next( &ite ) ) {
248 mrbc_kv *kv = mrbc_kv_i_next( &ite );
249
250 mrbc_printf(" $%04x:%s = ", kv->sym_id, mrbc_symid_to_str(kv->sym_id));
251 mrbc_p_sub( &kv->value );
253 mrbc_printf(" .tt=%d\n", mrbc_type(kv->value));
254 } else {
255 mrbc_printf(" .tt=%d refcnt=%d\n", mrbc_type(kv->value), kv->value.obj->ref_count);
256 }
257 }
258}
259#endif
#define mrbc_symbol(o)
Definition boxing_no.h:62
#define mrbc_symbol_value(n)
Definition boxing_no.h:74
#define mrbc_type(o)
Definition boxing_no.h:57
struct RObject mrbc_value
Value object. Default version.
int mrbc_array_push(mrbc_value *ary, mrbc_value *set_val)
Definition c_array.c:243
#define MRBC_CLASS(cls)
Definition class.h:55
struct RClass mrbc_class
Class object.
void mrbc_printf(const char *fstr,...)
Definition console.c:205
int mrbc_p_sub(const mrbc_value *v)
Definition console.c:398
void mrbc_print_symbol(mrbc_sym sym_id)
Definition console.c:152
static void mrbc_print(const char *str)
Definition console.h:110
void mrbc_init_global(void)
Definition global.c:40
int mrbc_set_global(mrbc_sym sym_id, mrbc_value *v)
Definition global.c:154
int mrbc_set_class_const(const mrbc_class *cls, mrbc_sym sym_id, mrbc_value *v)
Definition global.c:76
static mrbc_kv_handle handle_global
for global variables.
Definition global.c:30
void mrbc_get_all_class_const(const mrbc_class *cls, mrbc_value *ret)
Definition global.c:124
mrbc_value * mrbc_get_const(mrbc_sym sym_id)
Definition global.c:93
mrbc_value * mrbc_get_global(mrbc_sym sym_id)
Definition global.c:166
static mrbc_kv_handle handle_const
for global(Object) constants.
Definition global.c:29
int mrbc_set_const(mrbc_sym sym_id, mrbc_value *v)
Definition global.c:57
mrbc_value * mrbc_get_class_const(const mrbc_class *cls, mrbc_sym sym_id)
Definition global.c:106
int mrbc_kv_init_handle(struct VM *vm, mrbc_kv_handle *kvh, int size)
Definition keyvalue.c:128
mrbc_value * mrbc_kv_get(mrbc_kv_handle *kvh, mrbc_sym sym_id)
Definition keyvalue.c:284
int mrbc_kv_set(mrbc_kv_handle *kvh, mrbc_sym sym_id, mrbc_value *set_val)
Definition keyvalue.c:228
static mrbc_kv * mrbc_kv_i_next(mrbc_kv_iterator *ite)
Definition keyvalue.h:159
struct RKeyValueHandle mrbc_kv_handle
Key-Value handle.
static int mrbc_kv_i_has_next(const mrbc_kv_iterator *ite)
Definition keyvalue.h:143
struct RKeyValue mrbc_kv
Key-Value data.
struct RKeyValueIterator mrbc_kv_iterator
Key-Value iterator.
static mrbc_kv_iterator mrbc_kv_iterator_new(const mrbc_kv_handle *h)
Definition keyvalue.h:122
Include at once the necessary header files.
mrbc_sym sym_id
class name's symbol ID
Definition class.h:97
mrbc_sym sym_id
symbol ID as key.
Definition keyvalue.h:43
mrbc_value value
stored value.
Definition keyvalue.h:44
struct RBasic * obj
Definition boxing_no.h:27
struct RClass * cls
Definition boxing_no.h:28
mrbc_sym mrbc_search_symid(const char *str)
Definition symbol.c:259
mrbc_value mrbc_symbol_new(struct VM *vm, const char *str)
Definition symbol.c:335
void mrbc_make_nested_symbol_s(char *buf, mrbc_sym id1, mrbc_sym id2)
Definition symbol.c:279
void mrbc_separate_nested_symid(mrbc_sym sym_id, mrbc_sym *id1, mrbc_sym *id2)
Definition symbol.c:312
const char * mrbc_symid_to_str(mrbc_sym sym_id)
Definition symbol.c:239
static int mrbc_is_nested_symid(mrbc_sym sym_id)
Definition symbol.h:74
#define MRBC_NESTED_SYMBOL_BUF_LEN
Definition symbol.h:28
#define MRBC_TT_INC_DEC_THRESHOLD
Definition value.h:103
int16_t mrbc_sym
mruby/c symbol ID
Definition value.h:62
@ MRBC_TT_MODULE
Module.
Definition value.h:91
@ MRBC_TT_CLASS
Class.
Definition value.h:90
Global configuration of mruby/c VM's.