mruby/c VM Source Code release 3.4
Loading...
Searching...
No Matches
global.c
Go to the documentation of this file.
1
13
14/***** Feature test switches ************************************************/
15/***** System headers *******************************************************/
16//@cond
17#include "vm_config.h"
18//@endcond
19
20/***** Local headers ********************************************************/
21#include "mrubyc.h"
22
23/***** Constat values *******************************************************/
24/***** Macros ***************************************************************/
25/***** Typedefs *************************************************************/
26/***** Function prototypes **************************************************/
27/***** Local variables ******************************************************/
30
31/***** Global variables *****************************************************/
32/***** Signal catching functions ********************************************/
33/***** Local functions ******************************************************/
34/***** Global functions *****************************************************/
35
36//================================================================
44
45
46//================================================================
54{
55 if( mrbc_kv_get( &handle_const, sym_id ) != NULL ) {
56 mrbc_printf("warning: already initialized constant.\n");
57 }
58
59 return mrbc_kv_set( &handle_const, sym_id, v );
60}
61
62
63//================================================================
71int mrbc_set_class_const( const struct RClass *cls, mrbc_sym sym_id, mrbc_value *v )
72{
73 char buf[sizeof(mrbc_sym)*4+1];
74
75 make_nested_symbol_s( buf, cls->sym_id, sym_id );
76 mrbc_sym id = mrbc_symbol( mrbc_symbol_new( 0, buf ));
77
78 return mrbc_set_const( id, v );
79}
80
81
82//================================================================
89{
90 return mrbc_kv_get( &handle_const, sym_id );
91}
92
93
94//================================================================
101mrbc_value * mrbc_get_class_const( const struct RClass *cls, mrbc_sym sym_id )
102{
103 char buf[sizeof(mrbc_sym)*4+1];
104
105 make_nested_symbol_s( buf, cls->sym_id, sym_id );
106 mrbc_sym id = mrbc_search_symid(buf);
107 if( id <= 0 ) return 0;
108
109 return mrbc_kv_get( &handle_const, id );
110}
111
112
113//================================================================
119void mrbc_get_all_class_const( const struct RClass *cls, mrbc_value *ret )
120{
122 int flag_object_class = (cls == MRBC_CLASS(Object));
123
124 while( mrbc_kv_i_has_next( &ite ) ) {
125 const mrbc_kv *kv = mrbc_kv_i_next( &ite );
126
127 if( mrbc_is_nested_symid(kv->sym_id) ) {
128 mrbc_sym id1, id2;
129
130 mrbc_separate_nested_symid( kv->sym_id, &id1, &id2 );
131 if( id1 == cls->sym_id ) {
133 }
134
135 } else if( flag_object_class ) {
137 }
138 }
139}
140
141
142//================================================================
150{
151 return mrbc_kv_set( &handle_global, sym_id, v );
152}
153
154
155//================================================================
162{
163 return mrbc_kv_get( &handle_global, sym_id );
164}
165
166
167#if defined(MRBC_ALLOC_VMID)
168//================================================================
171void mrbc_global_clear_vm_id(void)
172{
173 mrbc_kv_clear_vm_id( &handle_const );
174 mrbc_kv_clear_vm_id( &handle_global );
175}
176#endif
177
178
179#ifdef MRBC_DEBUG
180//================================================================
186void mrbc_debug_dump_const( void )
187{
188 mrbc_print("<< Const table dump. >>\n(s_id:identifier = value)\n");
190
191 while( mrbc_kv_i_has_next( &ite ) ) {
192 const mrbc_kv *kv = mrbc_kv_i_next( &ite );
193 const char *s = mrbc_symid_to_str(kv->sym_id);
194
195 if( kv->sym_id < 0x100 ) continue;
196
197 mrbc_printf(" %04x:\"%s\"", kv->sym_id, s );
198 if( mrbc_is_nested_symid(kv->sym_id) ) {
199 mrbc_printf("(");
201 mrbc_printf(")");
202 }
203
204 if( kv->value.tt == MRBC_TT_CLASS ) {
205 const mrbc_class *cls = kv->value.cls;
206 mrbc_printf(" = Class(symid=$%x name=", cls->sym_id);
208 mrbc_printf(")\n");
209 continue;
210 }
211
212 if( kv->value.tt == MRBC_TT_MODULE ) {
213 const mrbc_class *cls = kv->value.cls;
214 mrbc_printf(" = Module(symid=$%x name=", cls->sym_id);
216 mrbc_printf(")\n");
217 continue;
218 }
219
220 mrbc_printf(" = ");
221 mrbc_p_sub( &kv->value );
223 mrbc_printf(".tt=%d\n", mrbc_type(kv->value));
224 } else {
225 mrbc_printf(".tt=%d.ref=%d\n", mrbc_type(kv->value), kv->value.obj->ref_count);
226 }
227 }
228}
229
230
231//================================================================
237void mrbc_debug_dump_global( void )
238{
239 mrbc_print("<< Global table dump. >>\n(s_id:identifier = value)\n");
240
242 while( mrbc_kv_i_has_next( &ite ) ) {
243 mrbc_kv *kv = mrbc_kv_i_next( &ite );
244
245 mrbc_printf(" %04x:%s = ", kv->sym_id, mrbc_symid_to_str(kv->sym_id));
246 mrbc_p_sub( &kv->value );
248 mrbc_printf(" .tt=%d\n", mrbc_type(kv->value));
249 } else {
250 mrbc_printf(" .tt=%d refcnt=%d\n", mrbc_type(kv->value), kv->value.obj->ref_count);
251 }
252 }
253}
254#endif
int mrbc_array_push(mrbc_value *ary, mrbc_value *set_val)
Definition c_array.c:252
#define MRBC_CLASS(cls)
Definition class.h:51
struct RClass mrbc_class
Class object.
void mrbc_printf(const char *fstr,...)
Definition console.c:180
int mrbc_p_sub(const mrbc_value *v)
Definition console.c:333
void mrbc_print_symbol(mrbc_sym sym_id)
Definition console.c:127
static void mrbc_print(const char *str)
Definition console.h:104
mrbc_value * mrbc_get_class_const(const struct RClass *cls, mrbc_sym sym_id)
Definition global.c:101
void mrbc_init_global(void)
Definition global.c:39
int mrbc_set_global(mrbc_sym sym_id, mrbc_value *v)
Definition global.c:149
void mrbc_get_all_class_const(const struct RClass *cls, mrbc_value *ret)
Definition global.c:119
static mrbc_kv_handle handle_global
for global variables.
Definition global.c:29
mrbc_value * mrbc_get_const(mrbc_sym sym_id)
Definition global.c:88
mrbc_value * mrbc_get_global(mrbc_sym sym_id)
Definition global.c:161
int mrbc_set_class_const(const struct RClass *cls, mrbc_sym sym_id, mrbc_value *v)
Definition global.c:71
static mrbc_kv_handle handle_const
for global(Object) constants.
Definition global.c:28
int mrbc_set_const(mrbc_sym sym_id, mrbc_value *v)
Definition global.c:53
int mrbc_kv_init_handle(struct VM *vm, mrbc_kv_handle *kvh, int size)
Definition keyvalue.c:131
mrbc_value * mrbc_kv_get(mrbc_kv_handle *kvh, mrbc_sym sym_id)
Definition keyvalue.c:292
int mrbc_kv_set(mrbc_kv_handle *kvh, mrbc_sym sym_id, mrbc_value *set_val)
Definition keyvalue.c:233
static mrbc_kv * mrbc_kv_i_next(mrbc_kv_iterator *ite)
Definition keyvalue.h:152
struct RKeyValueHandle mrbc_kv_handle
Key-Value handle.
static int mrbc_kv_i_has_next(const mrbc_kv_iterator *ite)
Definition keyvalue.h:136
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:115
Include at once the necessary header files.
Class object.
Definition class.h:83
mrbc_sym sym_id
class name's symbol ID
Definition class.h:84
mrbc_sym sym_id
symbol ID as key.
Definition keyvalue.h:42
mrbc_value value
stored value.
Definition keyvalue.h:43
mrbc_vtype tt
Definition value.h:152
struct RBasic * obj
Definition value.h:159
struct RClass * cls
Definition value.h:160
mrbc_sym mrbc_search_symid(const char *str)
Definition symbol.c:258
mrbc_value mrbc_symbol_new(struct VM *vm, const char *str)
Definition symbol.c:334
void mrbc_separate_nested_symid(mrbc_sym sym_id, mrbc_sym *id1, mrbc_sym *id2)
Definition symbol.c:305
const char * mrbc_symid_to_str(mrbc_sym sym_id)
Definition symbol.c:238
void make_nested_symbol_s(char *buf, mrbc_sym id1, mrbc_sym id2)
Definition symbol.c:278
static int mrbc_is_nested_symid(mrbc_sym sym_id)
Definition symbol.h:68
#define mrbc_symbol(o)
Definition value.h:196
#define mrbc_symbol_value(n)
Definition value.h:214
#define MRBC_TT_INC_DEC_THRESHOLD
Definition value.h:100
#define mrbc_type(o)
Definition value.h:193
int16_t mrbc_sym
mruby/c symbol ID
Definition value.h:59
@ MRBC_TT_MODULE
Module.
Definition value.h:88
@ MRBC_TT_CLASS
Class.
Definition value.h:87
struct RObject mrbc_value
Definition value.h:174
Global configuration of mruby/c VM's.