mruby/c VM Source Code release 4.0.0
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.\n");
61 }
62
63 return mrbc_kv_set( &handle_const, sym_id, v );
64}
65
66
67//================================================================
76{
77 char buf[sizeof(mrbc_sym)*4+1];
78
79 make_nested_symbol_s( buf, cls->sym_id, sym_id );
80 mrbc_sym id = mrbc_symbol( mrbc_symbol_new( 0, buf ));
81
82 return mrbc_set_const( id, v );
83}
84
85
86//================================================================
93{
94 return mrbc_kv_get( &handle_const, sym_id );
95}
96
97
98//================================================================
106{
107 char buf[sizeof(mrbc_sym)*4+1];
108
109 make_nested_symbol_s( buf, cls->sym_id, sym_id );
110 mrbc_sym id = mrbc_search_symid(buf);
111 if( id <= 0 ) return 0;
112
113 return mrbc_kv_get( &handle_const, id );
114}
115
116
117//================================================================
124{
126 int flag_object_class = (cls == MRBC_CLASS(Object));
127
128 while( mrbc_kv_i_has_next( &ite ) ) {
129 const mrbc_kv *kv = mrbc_kv_i_next( &ite );
130
131 if( mrbc_is_nested_symid(kv->sym_id) ) {
132 mrbc_sym id1, id2;
133
134 mrbc_separate_nested_symid( kv->sym_id, &id1, &id2 );
135 if( id1 == cls->sym_id ) {
137 }
138
139 } else if( flag_object_class ) {
141 }
142 }
143}
144
145
146//================================================================
154{
155 return mrbc_kv_set( &handle_global, sym_id, v );
156}
157
158
159//================================================================
166{
167 return mrbc_kv_get( &handle_global, sym_id );
168}
169
170
171#if defined(MRBC_ALLOC_VMID)
172//================================================================
175void mrbc_global_clear_vm_id(void)
176{
177 mrbc_kv_clear_vm_id( &handle_const );
178 mrbc_kv_clear_vm_id( &handle_global );
179}
180#endif
181
182
183#ifdef MRBC_DEBUG
184//================================================================
190void mrbc_debug_dump_const( void )
191{
192 mrbc_print("<< Const table dump. >>\n(s_id:identifier = value)\n");
194
195 while( mrbc_kv_i_has_next( &ite ) ) {
196 const mrbc_kv *kv = mrbc_kv_i_next( &ite );
197 const char *s = mrbc_symid_to_str(kv->sym_id);
198
199 if( kv->sym_id < 0x100 ) continue; // OFFSET_BUILTIN_SYMBOL in symbol.c
200
201 mrbc_printf(" %04x:\"%s\"", kv->sym_id, s );
202 if( mrbc_is_nested_symid(kv->sym_id) ) {
203 mrbc_printf("(");
205 mrbc_printf(")");
206 }
207
208 if( mrbc_type(kv->value) == MRBC_TT_CLASS ) {
209 const mrbc_class *cls = kv->value.cls;
210 mrbc_printf(" = Class(symid=$%x name=", cls->sym_id);
212 mrbc_printf(")\n");
213 continue;
214 }
215
216 if( mrbc_type(kv->value) == MRBC_TT_MODULE ) {
217 const mrbc_class *cls = kv->value.cls;
218 mrbc_printf(" = Module(symid=$%x name=", cls->sym_id);
220 mrbc_printf(")\n");
221 continue;
222 }
223
224 mrbc_printf(" = ");
225 mrbc_p_sub( &kv->value );
227 mrbc_printf(".tt=%d\n", mrbc_type(kv->value));
228 } else {
229 mrbc_printf(".tt=%d.ref=%d\n", mrbc_type(kv->value), kv->value.obj->ref_count);
230 }
231 }
232}
233
234
235//================================================================
241void mrbc_debug_dump_global( void )
242{
243 mrbc_print("<< Global table dump. >>\n(s_id:identifier = value)\n");
244
246 while( mrbc_kv_i_has_next( &ite ) ) {
247 mrbc_kv *kv = mrbc_kv_i_next( &ite );
248
249 mrbc_printf(" %04x:%s = ", kv->sym_id, mrbc_symid_to_str(kv->sym_id));
250 mrbc_p_sub( &kv->value );
252 mrbc_printf(" .tt=%d\n", mrbc_type(kv->value));
253 } else {
254 mrbc_printf(" .tt=%d refcnt=%d\n", mrbc_type(kv->value), kv->value.obj->ref_count);
255 }
256 }
257}
258#endif
#define mrbc_symbol(o)
Definition boxing_no.h:60
#define mrbc_symbol_value(n)
Definition boxing_no.h:70
#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:201
int mrbc_p_sub(const mrbc_value *v)
Definition console.c:394
void mrbc_print_symbol(mrbc_sym sym_id)
Definition console.c:148
static void mrbc_print(const char *str)
Definition console.h:108
void mrbc_init_global(void)
Definition global.c:40
int mrbc_set_global(mrbc_sym sym_id, mrbc_value *v)
Definition global.c:153
int mrbc_set_class_const(const mrbc_class *cls, mrbc_sym sym_id, mrbc_value *v)
Definition global.c:75
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:123
mrbc_value * mrbc_get_const(mrbc_sym sym_id)
Definition global.c:92
mrbc_value * mrbc_get_global(mrbc_sym sym_id)
Definition global.c:165
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:105
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:262
mrbc_value mrbc_symbol_new(struct VM *vm, const char *str)
Definition symbol.c:340
void mrbc_separate_nested_symid(mrbc_sym sym_id, mrbc_sym *id1, mrbc_sym *id2)
Definition symbol.c:309
const char * mrbc_symid_to_str(mrbc_sym sym_id)
Definition symbol.c:242
void make_nested_symbol_s(char *buf, mrbc_sym id1, mrbc_sym id2)
Definition symbol.c:282
static int mrbc_is_nested_symid(mrbc_sym sym_id)
Definition symbol.h:71
#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.