mruby/c VM Source Code master (2026/08/06)
Loading...
Searching...
No Matches
c_object.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#include <stdlib.h>
20#include <stdint.h>
21#include <string.h>
22#include <assert.h>
23//@endcond
24
25/***** Local headers ********************************************************/
26#include "mrubyc.h"
27
28/***** Macros ***************************************************************/
29#define IS_CLASS_OR_MODULE(v) \
30 (mrbc_type(v) == MRBC_TT_CLASS || mrbc_type(v) == MRBC_TT_MODULE)
31
32
33/***** Local functions ******************************************************/
34#if MRBC_USE_STRING
35//================================================================
38static int set_sym_name_by_id( char *buf, int bufsiz, mrbc_sym sym_id )
39{
40 if( !mrbc_is_nested_symid(sym_id) ) {
41 return mrbc_strcpy( buf, bufsiz, mrbc_symid_to_str(sym_id) );
42 }
43
44 // nested case.
45 mrbc_sym id1, id2;
46 mrbc_separate_nested_symid( sym_id, &id1, &id2 );
47
48 int n = set_sym_name_by_id( buf, bufsiz, id1 );
49 n += mrbc_strcpy( buf+n, bufsiz-n, "::" );
50 n += set_sym_name_by_id( buf+n, bufsiz-n, id2 );
51
52 return n;
53}
54#endif
55
56
57/***** global functions *****************************************************/
58//================================================================
62{
63 // call the initialize method.
64 mrbc_method method;
65 if( !mrbc_find_method(&method, v[0].instance->cls, MRBC_SYM(initialize))) {
66 return;
67 }
68
69 if( method.c_func ) {
70 method.func(vm, v, argc);
71 for( int i = 1; i <= argc; i++ ) {
72 mrbc_decref_empty( v + i );
73 }
74 return;
75 }
76
77 mrbc_callinfo *callinfo = mrbc_push_callinfo(vm, MRBC_SYM(initialize),
78 (v - vm->cur_regs), argc);
79 callinfo->own_class = method.cls;
80
81 vm->cur_irep = method.irep;
82 vm->inst = vm->cur_irep->inst;
83 vm->cur_regs = v;
84}
85
86
87#if MRBC_USE_STRING
88//================================================================
91void mrbc_object_inspect(mrbc_vm *vm, mrbc_value v[], int argc)
92{
93 char buf[64]; // max length of class (or object) name.
94 char *s = buf;
95 mrbc_sym sym_id = find_class_by_object(&v[0])->sym_id;
96 int class_or_module = IS_CLASS_OR_MODULE(v[0]);
97
98 if (!class_or_module) {
99 buf[0] = '#'; buf[1] = '<';
100 s = buf + 2;
101 }
102
103 int bufsiz = sizeof(buf) - (s - buf);
104 int n = set_sym_name_by_id( s, bufsiz, sym_id );
105
106 if (!class_or_module) {
107 mrbc_snprintf(s+n, bufsiz-n, ":%08x>", MRBC_PTR_TO_UINT32(v->instance));
108 }
109
110 SET_RETURN( mrbc_string_new_cstr( vm, buf ));
111}
112#endif
113
114
115/***** Object class *********************************************************/
116//================================================================
119static void c_object_new(mrbc_vm *vm, mrbc_value v[], int argc)
120{
121 v[0] = mrbc_instance_new(vm, v[0].cls, 0);
122 mrbc_instance_call_initialize( vm, v, argc );
123}
124
125
126//================================================================
129static void c_object_not(mrbc_vm *vm, mrbc_value v[], int argc)
130{
132}
133
134
135//================================================================
138static void c_object_neq(mrbc_vm *vm, mrbc_value v[], int argc)
139{
140 int result = mrbc_compare( &v[0], &v[1] );
141 SET_BOOL_RETURN( result != 0 );
142}
143
144
145//================================================================
148static void c_object_compare(mrbc_vm *vm, mrbc_value v[], int argc)
149{
150 int result = mrbc_compare( &v[0], &v[1] );
151 SET_INT_RETURN( result );
152}
153
154
155//================================================================
158static void c_object_equal2(mrbc_vm *vm, mrbc_value v[], int argc)
159{
160 int result = mrbc_compare( &v[0], &v[1] );
161 SET_BOOL_RETURN( result == 0 );
162}
163
164
165//================================================================
168static void c_object_equal3(mrbc_vm *vm, mrbc_value v[], int argc)
169{
170 int result;
171
172 if( mrbc_type(v[0]) == MRBC_TT_CLASS ) {
173 result = mrbc_obj_is_kind_of( &v[1], v[0].cls );
174 } else {
175 result = (mrbc_compare( &v[0], &v[1] ) == 0);
176 }
177
178 SET_BOOL_RETURN( result );
179}
180
181
182//================================================================
185static void c_object_class(mrbc_vm *vm, mrbc_value v[], int argc)
186{
187 mrbc_value value;
188
189 mrbc_set_tt(&value, MRBC_TT_CLASS);
190 value.cls = find_class_by_object( v );
191 SET_RETURN( value );
192}
193
194
195//================================================================
198static void c_object_dup(mrbc_vm *vm, mrbc_value v[], int argc)
199{
200 if( mrbc_type(v[0]) == MRBC_TT_OBJECT ) {
201 mrbc_value new_obj = mrbc_instance_new(vm, v->instance->cls, 0);
202 mrbc_kv_dup( &v->instance->ivar, &new_obj.instance->ivar );
203
204 mrbc_decref( v );
205 *v = new_obj;
206 return;
207 }
208
209
210 // TODO: need support TT_PROC and TT_RANGE. but really need?
211 return;
212}
213
214
215//================================================================
218static void c_object_block_given(mrbc_vm *vm, mrbc_value v[], int argc)
219{
220 mrbc_callinfo *callinfo = vm->callinfo_tail;
221 if( !callinfo ) goto RETURN_FALSE;
222
223 mrbc_value *regs = callinfo->cur_regs + callinfo->reg_offset;
224
225 if( mrbc_type(regs[0]) == MRBC_TT_PROC ) {
226 callinfo = regs[0].proc->callinfo_self;
227 if( !callinfo ) goto RETURN_FALSE;
228
229 regs = callinfo->cur_regs + callinfo->reg_offset;
230 }
231
232 SET_BOOL_RETURN( mrbc_type(regs[callinfo->n_args+1]) == MRBC_TT_PROC );
233 return;
234
235 RETURN_FALSE:
237}
238
239
240//================================================================
243static void c_object_kind_of(mrbc_vm *vm, mrbc_value v[], int argc)
244{
245 if( !IS_CLASS_OR_MODULE(v[1]) ) {
246 mrbc_raise(vm, MRBC_CLASS(TypeError), "class or module required");
247 return;
248 }
249
250 SET_BOOL_RETURN( mrbc_obj_is_kind_of( &v[0], v[1].cls ));
251}
252
253
254//================================================================
257static void c_object_nil(mrbc_vm *vm, mrbc_value v[], int argc)
258{
260}
261
262
263//================================================================
266#if !defined(MRBC_NO_STDIO)
267static void c_object_p(mrbc_vm *vm, mrbc_value v[], int argc)
268{
269 for( int i = 1; i <= argc; i++ ) {
270 mrbc_p( &v[i] );
271 }
272
273 if (argc == 0) {
275
276 } else if (argc == 1) {
277 mrbc_incref( &v[1] );
278 SET_RETURN(v[1]);
279
280 } else {
281 mrbc_value value = mrbc_array_new(vm, argc);
282 for( int i = 1; i <= argc; i++ ) {
283 mrbc_incref( &v[i] );
284 mrbc_array_push( &value, &v[i] );
285 }
286 SET_RETURN(value);
287 }
288}
289#endif
290
291
292//================================================================
295#if !defined(MRBC_NO_STDIO)
296static void c_object_print(mrbc_vm *vm, mrbc_value v[], int argc)
297{
298 for( int i = 1; i <= argc; i++ ) {
299 mrbc_print_sub( &v[i] );
300 }
302}
303#endif
304
305
306//================================================================
309#if !defined(MRBC_NO_STDIO)
310static void c_object_puts(mrbc_vm *vm, mrbc_value v[], int argc)
311{
312 if( argc ) {
313 for( int i = 1; i <= argc; i++ ) {
314 if( mrbc_puts_sub( &v[i] ) == 0 ) mrbc_putchar('\n');
315 }
316 } else {
317 mrbc_putchar('\n');
318 }
320}
321#endif
322
323
324//================================================================
334static void c_object_raise(mrbc_vm *vm, mrbc_value v[], int argc)
335{
336 assert( !mrbc_israised(vm) );
337
338 // case 1. raise (no argument)
339 if( argc == 0 ) {
340 vm->exception = mrbc_exception_new( vm, MRBC_CLASS(RuntimeError), "", 0 );
341 } else
342
343 // case 2. raise "message"
344 if( argc == 1 && mrbc_type(v[1]) == MRBC_TT_STRING ) {
345 vm->exception = mrbc_exception_new( vm, MRBC_CLASS(RuntimeError),
346 mrbc_string_cstr(&v[1]), mrbc_string_size(&v[1]) );
347 } else
348
349 // case 3. raise ExceptionClass
350 if( argc == 1 && mrbc_type(v[1]) == MRBC_TT_CLASS &&
351 mrbc_obj_is_kind_of( &v[1], MRBC_CLASS(Exception))) {
352 vm->exception = mrbc_exception_new( vm, v[1].cls, 0, 0 );
353 } else
354
355 // case 4. raise ExceptionObject
356 if( argc == 1 && mrbc_type(v[1]) == MRBC_TT_EXCEPTION ) {
357 mrbc_incref( &v[1] );
358 vm->exception = v[1];
359 } else
360
361 // case 5. raise ExceptionClass, "param"
362 if( argc == 2 && mrbc_type(v[1]) == MRBC_TT_CLASS
363 && mrbc_type(v[2]) == MRBC_TT_STRING ) {
364 vm->exception = mrbc_exception_new( vm, v[1].cls,
365 mrbc_string_cstr(&v[2]), mrbc_string_size(&v[2]) );
366 } else
367
368 // case 6. raise ExceptionObject, "param"
369 if( argc == 2 && mrbc_type(v[1]) == MRBC_TT_EXCEPTION
370 && mrbc_type(v[2]) == MRBC_TT_STRING ) {
371 vm->exception = mrbc_exception_new( vm, v[1].exception->cls,
372 mrbc_string_cstr(&v[2]), mrbc_string_size(&v[2]) );
373 } else {
374
375 // fail.
376 vm->exception = mrbc_exception_new( vm, MRBC_CLASS(ArgumentError), 0, 0 );
377 }
378
379 // set raised method to exception instance.
380 if( vm->callinfo_tail != 0 &&
382 vm->exception.exception->method_id == 0 ) {
384 }
385
386 vm->flag_preemption = 2;
387}
388
389
390#if defined(MRBC_DEBUG)
391//================================================================
394static void c_object_object_id(mrbc_vm *vm, mrbc_value v[], int argc)
395{
396 // tiny implementation.
398}
399
400
401//================================================================
406static void c_object_instance_methods(mrbc_vm *vm, mrbc_value v[], int argc)
407{
408 if( mrbc_type(v[0]) != MRBC_TT_CLASS ) return;
409
410 int flag_inherit = !(argc >= 1 && mrbc_type(v[1]) == MRBC_TT_FALSE);
411 mrbc_value ret = mrbc_array_new( vm, 0 );
412 mrbc_class *cls = v[0].cls;
414 int nest_idx = 0;
415
416 while( 1 ) {
417 // builtin method.
418 for( int i = 0; i < cls->num_builtin_method; i++ ) {
419 mrbc_array_push( &ret,
420 &mrbc_symbol_value(((struct RBuiltinClass *)cls)->method_symbols[i]) );
421 }
422
423 // no builtin method.
424 const mrbc_method *method = cls->flag_nomethod ? NULL : cls->method_link;
425 while( method ) {
426 mrbc_array_push( &ret, &mrbc_symbol_value(method->sym_id) );
427 method = method->next;
428 }
429
430 if( !flag_inherit ) break;
431
432 cls = mrbc_traverse_class_tree( cls, nest_buf, &nest_idx );
433 if( !cls ) break;
434 if( cls->flag_alias ) cls = cls->aliased;
435 }
436
437 SET_RETURN(ret);
438}
439
440
441//================================================================
444static void c_object_instance_variables(mrbc_vm *vm, mrbc_value v[], int argc)
445{
446 // temporary code for operation check.
447
448 mrbc_value ret = mrbc_array_new( vm, 0 );
449 mrbc_kv_handle *kvh = &v[0].instance->ivar;
450#if 0
451 mrbc_printf("n = %d/%d ", kvh->n_stored, kvh->data_size);
452#endif
453
454 if( mrbc_type(v[0]) == MRBC_TT_OBJECT ) {
455 for( int i = 0; i < kvh->n_stored; i++ ) {
456 mrbc_array_push( &ret, &mrbc_symbol_value(kvh->data[i].sym_id) );
457 }
458 }
459
460 SET_RETURN(ret);
461}
462
463
464#if !defined(MRBC_ALLOC_LIBC)
465//================================================================
468static void c_object_memory_statistics(mrbc_vm *vm, mrbc_value v[], int argc)
469{
470 struct MRBC_ALLOC_STATISTICS mem;
471
472 mrbc_alloc_statistics( &mem );
473 if( argc == 0 || mrbc_type(v[1]) == MRBC_TT_TRUE ) {
474 mrbc_printf("Memory Statistics\n");
475 mrbc_printf(" Total: %d\n", mem.total);
476 mrbc_printf(" Used : %d\n", mem.used);
477 mrbc_printf(" Free : %d\n", mem.free);
478 mrbc_printf(" Frag.: %d\n", mem.fragmentation);
479 }
480
481 // make a return value.
482 mrbc_value ret = mrbc_hash_new(vm, 4);
484 &mrbc_integer_value( mem.total ));
486 &mrbc_integer_value( mem.used ));
488 &mrbc_integer_value( mem.free ));
489 mrbc_hash_set(&ret, &mrbc_symbol_value( mrbc_str_to_symid("fragmentation") ),
490 &mrbc_integer_value( mem.fragmentation ));
491
492 SET_RETURN(ret);
493}
494#endif // MRBC_ALLOC_LIBC
495#endif // MRBC_DEBUG
496
497
498//================================================================
501static void c_object_getiv(mrbc_vm *vm, mrbc_value v[], int argc)
502{
503 mrbc_sym sym_id = mrbc_get_callee_symid(vm);
504 mrbc_value ret = mrbc_instance_getiv(&v[0], sym_id);
505
506 SET_RETURN(ret);
507}
508
509
510//================================================================
513static void c_object_setiv(mrbc_vm *vm, mrbc_value v[], int argc)
514{
515 char namebuf_auto[16];
516 char *namebuf;
517 const char *name = mrbc_get_callee_name(vm);
518 int len = strlen(name);
519
520 if( sizeof(namebuf_auto) < len ) {
521 namebuf = mrbc_alloc(vm, len);
522 } else {
523 namebuf = namebuf_auto;
524 }
525
526 memcpy( namebuf, name, len-1 );
527 namebuf[len-1] = '\0'; // delete '='
528 mrbc_sym sym_id = mrbc_str_to_symid(namebuf);
529
530 mrbc_instance_setiv(&v[0], sym_id, &v[1]);
531
532 if( namebuf != namebuf_auto ) mrbc_free(vm, namebuf);
533}
534
535
536//================================================================
539static void c_object_attr_reader(mrbc_vm *vm, mrbc_value v[], int argc)
540{
541 for( int i = 1; i <= argc; i++ ) {
542 if( mrbc_type(v[i]) != MRBC_TT_SYMBOL ) {
543 // Not support "String" only :symbol
544 mrbc_raise(vm, MRBC_CLASS(TypeError), "not a symbol");
545 return;
546 }
547
548 // define reader method
549 const char *name = mrbc_symbol_cstr(&v[i]);
550 mrbc_define_method(vm, v[0].cls, name, c_object_getiv);
551 }
552}
553
554
555//================================================================
558static void c_object_attr_accessor(mrbc_vm *vm, mrbc_value v[], int argc)
559{
560 for( int i = 1; i <= argc; i++ ) {
561 if( mrbc_type(v[i]) != MRBC_TT_SYMBOL ) {
562 // Not support "String" only :symbol
563 mrbc_raise(vm, MRBC_CLASS(TypeError), "not a symbol");
564 return;
565 }
566
567 // define reader method
568 const char *name = mrbc_symbol_cstr(&v[i]);
569 mrbc_define_method(vm, v[0].cls, name, c_object_getiv);
570
571 // make string "....=" and define writer method.
572 int len = strlen(name);
573 char *namebuf = mrbc_alloc(vm, len+2);
574
575 memcpy(namebuf, name, len);
576 namebuf[len] = '=';
577 namebuf[len+1] = 0;
578 mrbc_symbol_new(vm, namebuf);
579 mrbc_define_method(vm, v[0].cls, namebuf, c_object_setiv);
580 mrbc_free(vm, namebuf);
581 }
582}
583
584
585//================================================================
588static void c_object_include(mrbc_vm *vm, mrbc_value v[], int argc)
589{
590 mrbc_class *self;
591
592 if( IS_CLASS_OR_MODULE(v[0]) ) {
593 self = v[0].cls;
594 } else if( vm->callinfo_tail == 0 ) { // is top level?
595 self = MRBC_CLASS(Object);
596 } else {
597 mrbc_raise( vm, 0, 0 );
598 return; // Error.
599 }
600
601 for( int i = 1; i <= argc; i++ ) {
602 if( mrbc_type(v[i]) != MRBC_TT_MODULE ) {
603 mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong argument type Class");
604 return;
605 }
606 mrbc_class *module = v[i].cls;
607 mrbc_class *alias = mrbc_raw_alloc_no_free( sizeof(mrbc_class) );
608
609 *alias = (mrbc_class){
610 .sym_id = module->sym_id,
611 .flag_module = 1,
612 .flag_alias = 1,
613 .super = self->super,
614 .aliased = module,
615#if defined(MRBC_DEBUG)
616 .obj_mark_ = "MA",
617 .name = module->name,
618#endif
619 };
620 self->super = alias;
621 }
622}
623
624
625//================================================================
628static void c_object_constants(mrbc_vm *vm, mrbc_value v[], int argc)
629{
630 if( mrbc_type(v[0]) != MRBC_TT_CLASS ) {
631 mrbc_raise(vm, MRBC_CLASS(NoMethodError), 0);
632 return;
633 }
634
635 int flag_inherit = !(argc >= 1 && mrbc_type(v[1]) == MRBC_TT_FALSE);
636 mrbc_value ret = mrbc_array_new( vm, 0 );
637 mrbc_class *cls = v[0].cls;
639 int nest_idx = 0;
640
641
642 while( 1 ) {
643 mrbc_get_all_class_const( cls, &ret );
644 if( !flag_inherit ) break;
645
646 cls = mrbc_traverse_class_tree( cls, nest_buf, &nest_idx );
647 if( cls == MRBC_CLASS(Object) ) {
648 cls = mrbc_traverse_class_tree_skip( nest_buf, &nest_idx );
649 }
650 if( !cls ) break;
651 if( cls->flag_alias ) cls = cls->aliased;
652 }
653
654 SET_RETURN(ret);
655}
656
657
658#if MRBC_USE_STRING
659//================================================================
662static void c_object_sprintf(mrbc_vm *vm, mrbc_value v[], int argc)
663{
664 static const int BUF_INC_STEP = 32; // bytes.
665
666 mrbc_value *format = &v[1];
667 if( mrbc_type(*format) != MRBC_TT_STRING ) {
668 mrbc_raise(vm, MRBC_CLASS(TypeError), 0);
669 return;
670 }
671
672 int buflen = BUF_INC_STEP;
673 char *buf = mrbc_alloc(vm, buflen);
674 mrbc_printf_t pf;
675 mrbc_printf_init( &pf, buf, buflen, mrbc_string_cstr(format) );
676
677 int i = 2;
678 int ret;
679 while( 1 ) {
680 mrbc_printf_t pf_bak = pf;
681 ret = mrbc_printf_main( &pf );
682 if( ret == 0 ) break; // normal break loop.
683 if( ret < 0 ) goto INCREASE_BUFFER;
684
685 if( i > argc ) {
686 mrbc_raise(vm, MRBC_CLASS(ArgumentError), "too few arguments");
687 break;
688 }
689
690 // maybe ret == 1
691 switch(pf.fmt.type) {
692 case 'c':
693 if( mrbc_type(v[i]) == MRBC_TT_INTEGER ) {
694 ret = mrbc_printf_char( &pf, v[i].i );
695 } else if( mrbc_type(v[i]) == MRBC_TT_STRING ) {
696 ret = mrbc_printf_char( &pf, mrbc_string_cstr(&v[i])[0] );
697 }
698 break;
699
700 case 's':
701 if( mrbc_type(v[i]) == MRBC_TT_STRING ) {
702 ret = mrbc_printf_bstr( &pf, mrbc_string_cstr(&v[i]),
703 mrbc_string_size(&v[i]), ' ');
704 } else {
705 const char *s;
706 switch( mrbc_type(v[i]) ) {
707 case MRBC_TT_SYMBOL: s = mrbc_symbol_cstr(&v[i]); break;
708 case MRBC_TT_TRUE: s = "true"; break;
709 case MRBC_TT_FALSE: s = "false"; break;
710 default: s = ""; break;
711 }
712 ret = mrbc_printf_str( &pf, s, ' ');
713 }
714 break;
715
716 case 'd':
717 case 'i':
718 case 'u':
719 if( mrbc_type(v[i]) == MRBC_TT_INTEGER ) {
720 ret = mrbc_printf_int( &pf, v[i].i, 10);
721#if MRBC_USE_FLOAT
722 } else if( mrbc_type(v[i]) == MRBC_TT_FLOAT ) {
723 ret = mrbc_printf_int( &pf, (mrbc_int_t)v[i].d, 10);
724#endif
725 } else if( mrbc_type(v[i]) == MRBC_TT_STRING ) {
726 mrbc_int_t ival = atol(mrbc_string_cstr(&v[i]));
727 ret = mrbc_printf_int( &pf, ival, 10 );
728 }
729 break;
730
731 case 'b':
732 case 'B':
733 if( mrbc_type(v[i]) == MRBC_TT_INTEGER ) {
734 ret = mrbc_printf_bit( &pf, v[i].i, 1);
735 }
736 break;
737
738 case 'x':
739 case 'X':
740 if( mrbc_type(v[i]) == MRBC_TT_INTEGER ) {
741 ret = mrbc_printf_bit( &pf, v[i].i, 4);
742 }
743 break;
744
745 case 'o':
746 if( mrbc_type(v[i]) == MRBC_TT_INTEGER ) {
747 ret = mrbc_printf_bit( &pf, v[i].i, 3);
748 }
749 break;
750
751#if MRBC_USE_FLOAT
752 case 'f':
753 case 'e':
754 case 'E':
755 case 'g':
756 case 'G':
757 if( mrbc_type(v[i]) == MRBC_TT_FLOAT ) {
758 ret = mrbc_printf_float( &pf, v[i].d );
759 } else if( mrbc_type(v[i]) == MRBC_TT_INTEGER ) {
760 ret = mrbc_printf_float( &pf, v[i].i );
761 }
762 break;
763#endif
764
765 default:
766 break;
767 }
768 if( ret >= 0 ) {
769 i++;
770 continue; // normal next loop.
771 }
772
773 // maybe buffer full. (ret == -1)
774 if( pf.fmt.width > BUF_INC_STEP ) buflen += pf.fmt.width;
775 pf = pf_bak;
776
777 INCREASE_BUFFER:
778 buflen += BUF_INC_STEP;
779 buf = mrbc_realloc(vm, pf.buf, buflen);
780 mrbc_printf_replace_buffer(&pf, buf, buflen);
781 }
782 mrbc_printf_end( &pf );
783
784 buflen = mrbc_printf_len( &pf );
785 // shrink suitable size. realloc() may move the block.
786 buf = mrbc_realloc(vm, pf.buf, buflen+1);
787 if( buf ) mrbc_printf_replace_buffer(&pf, buf, buflen+1);
788
789 mrbc_value value = mrbc_string_new_alloc( vm, pf.buf, buflen );
790
791 SET_RETURN(value);
792}
793
794
795//================================================================
798#if !defined(MRBC_NO_STDIO)
799static void c_object_printf(mrbc_vm *vm, mrbc_value v[], int argc)
800{
801 c_object_sprintf(vm, v, argc);
804}
805#endif
806
807
808//================================================================
811static void c_object_inspect(mrbc_vm *vm, mrbc_value v[], int argc)
812{
813 mrbc_object_inspect(vm, v, argc);
814}
815#endif // MRBC_USE_STRING
816
817
818/* MRBC_AUTOGEN_METHOD_TABLE
819
820 CLASS("Object")
821 FILE("_autogen_class_object.h")
822 SUPER(0)
823
824 METHOD( "new", c_object_new )
825 METHOD( "!", c_object_not )
826 METHOD( "!=", c_object_neq )
827 METHOD( "<=>", c_object_compare )
828 METHOD( "==", c_object_equal2 )
829 METHOD( "===", c_object_equal3 )
830 METHOD( "class", c_object_class )
831 METHOD( "dup", c_object_dup )
832 METHOD( "block_given?", c_object_block_given )
833 METHOD( "is_a?", c_object_kind_of )
834 METHOD( "kind_of?", c_object_kind_of )
835 METHOD( "nil?", c_object_nil )
836#if !defined(MRBC_NO_STDIO)
837 METHOD( "p", c_object_p )
838 METHOD( "print", c_object_print )
839 METHOD( "puts", c_object_puts )
840#endif
841 METHOD( "raise", c_object_raise )
842 METHOD( "attr_reader",c_object_attr_reader )
843 METHOD( "attr_accessor", c_object_attr_accessor )
844 METHOD( "include", c_object_include )
845 METHOD( "extend", c_object_include )
846 METHOD( "constants", c_object_constants )
847 METHOD( "public", c_ineffect )
848 METHOD( "private", c_ineffect )
849 METHOD( "protected", c_ineffect )
850
851#if MRBC_USE_STRING
852 METHOD( "sprintf", c_object_sprintf )
853#if !defined(MRBC_NO_STDIO)
854 METHOD( "printf", c_object_printf )
855#endif
856 METHOD( "inspect", c_object_inspect )
857 METHOD( "to_s", c_object_inspect )
858#endif
859
860#if defined(MRBC_DEBUG)
861 METHOD( "object_id", c_object_object_id )
862 METHOD( "instance_methods", c_object_instance_methods )
863 METHOD( "instance_variables", c_object_instance_variables )
864#if !defined(MRBC_ALLOC_LIBC)
865 METHOD( "memory_statistics", c_object_memory_statistics )
866#endif
867#endif
868*/
869
870
871
872/***** Nil class ************************************************************/
873//================================================================
876static void c_nil_to_i(mrbc_vm *vm, mrbc_value v[], int argc)
877{
878 v[0] = mrbc_integer_value(0);
879}
880
881
882//================================================================
885static void c_nil_to_a(mrbc_vm *vm, mrbc_value v[], int argc)
886{
887 v[0] = mrbc_array_new(vm, 0);
888}
889
890
891//================================================================
894static void c_nil_to_h(mrbc_vm *vm, mrbc_value v[], int argc)
895{
896 v[0] = mrbc_hash_new(vm, 0);
897}
898
899
900#if MRBC_USE_FLOAT
901//================================================================
904static void c_nil_to_f(mrbc_vm *vm, mrbc_value v[], int argc)
905{
906 v[0] = mrbc_float_value(vm,0);
907}
908#endif
909
910
911#if MRBC_USE_STRING
912//================================================================
915static void c_nil_inspect(mrbc_vm *vm, mrbc_value v[], int argc)
916{
917 if( mrbc_type(v[0]) == MRBC_TT_CLASS ) {
918 mrbc_object_inspect(vm, v, argc);
919 return;
920 }
921
922 v[0] = mrbc_string_new_cstr(vm, "nil");
923}
924
925
926//================================================================
929static void c_nil_to_s(mrbc_vm *vm, mrbc_value v[], int argc)
930{
931 if( mrbc_type(v[0]) == MRBC_TT_CLASS ) {
932 mrbc_object_inspect(vm, v, argc);
933 return;
934 }
935
936 v[0] = mrbc_string_new(vm, NULL, 0);
937}
938#endif // MRBC_USE_STRING
939
940
941/* MRBC_AUTOGEN_METHOD_TABLE
942
943 CLASS("NilClass")
944 APPEND("_autogen_class_object.h")
945
946 METHOD( "to_i", c_nil_to_i )
947 METHOD( "to_a", c_nil_to_a )
948 METHOD( "to_h", c_nil_to_h )
949
950#if MRBC_USE_FLOAT
951 METHOD( "to_f", c_nil_to_f )
952#endif
953
954#if MRBC_USE_STRING
955 METHOD( "inspect", c_nil_inspect )
956 METHOD( "to_s", c_nil_to_s )
957#endif
958*/
959
960
961
962/***** True class ***********************************************************/
963#if MRBC_USE_STRING
964//================================================================
967static void c_true_inspect(mrbc_vm *vm, mrbc_value v[], int argc)
968{
969 if( mrbc_type(v[0]) == MRBC_TT_CLASS ) {
970 mrbc_object_inspect(vm, v, argc);
971 return;
972 }
973
974 v[0] = mrbc_string_new_cstr(vm, "true");
975}
976#endif
977
978
979/* MRBC_AUTOGEN_METHOD_TABLE
980
981 CLASS("TrueClass")
982 APPEND("_autogen_class_object.h")
983
984#if MRBC_USE_STRING
985 METHOD( "inspect", c_true_inspect )
986 METHOD( "to_s", c_true_inspect )
987#endif
988*/
989
990
991
992/***** False class **********************************************************/
993#if MRBC_USE_STRING
994//================================================================
997static void c_false_inspect(mrbc_vm *vm, mrbc_value v[], int argc)
998{
999 if( mrbc_type(v[0]) == MRBC_TT_CLASS ) {
1000 mrbc_object_inspect(vm, v, argc);
1001 return;
1002 }
1003
1004 v[0] = mrbc_string_new_cstr(vm, "false");
1005}
1006#endif // MRBC_USE_STRING
1007
1008
1009/* MRBC_AUTOGEN_METHOD_TABLE
1010
1011 CLASS("FalseClass")
1012 APPEND("_autogen_class_object.h")
1013
1014#if MRBC_USE_STRING
1015 METHOD( "inspect", c_false_inspect )
1016 METHOD( "to_s", c_false_inspect )
1017#endif
1018*/
1019#include "_autogen_class_object.h"
void mrbc_alloc_statistics(struct MRBC_ALLOC_STATISTICS *ret)
Definition alloc.c:984
void * mrbc_raw_alloc_no_free(unsigned int size)
Definition alloc.c:631
#define mrbc_symbol_value(n)
Definition boxing_no.h:74
static void mrbc_set_tt(mrbc_value *p, mrbc_vtype type)
Definition boxing_no.h:125
#define mrbc_type(o)
Definition boxing_no.h:57
#define mrbc_float_value(vm, n)
Definition boxing_no.h:68
struct RObject mrbc_value
Value object. Default version.
#define mrbc_integer(o)
Definition boxing_no.h:58
#define mrbc_integer_value(n)
Definition boxing_no.h:66
int mrbc_array_push(mrbc_value *ary, mrbc_value *set_val)
Definition c_array.c:243
mrbc_value mrbc_array_new(mrbc_vm *vm, int size)
Definition c_array.c:83
mrbc_value mrbc_hash_new(mrbc_vm *vm, int size)
Definition c_hash.c:78
int mrbc_hash_set(mrbc_value *hash, mrbc_value *key, mrbc_value *val)
Definition c_hash.c:160
void mrbc_object_inspect(mrbc_vm *vm, mrbc_value v[], int argc)
Definition c_object.c:91
static int set_sym_name_by_id(char *buf, int bufsiz, mrbc_sym sym_id)
Definition c_object.c:38
void mrbc_instance_call_initialize(mrbc_vm *vm, mrbc_value v[], int argc)
Definition c_object.c:61
#define IS_CLASS_OR_MODULE(v)
Definition c_object.c:29
mrbc_value mrbc_string_new(mrbc_vm *vm, const void *src, int len)
Definition c_string.c:64
mrbc_value mrbc_string_new_alloc(mrbc_vm *vm, void *buf, int len)
Definition c_string.c:88
static char * mrbc_string_cstr(const mrbc_value *v)
Definition c_string.h:122
static mrbc_value mrbc_string_new_cstr(mrbc_vm *vm, const char *src)
Definition c_string.h:91
static int mrbc_string_size(const mrbc_value *str)
Definition c_string.h:114
mrbc_value mrbc_instance_new(struct VM *vm, mrbc_class *cls, int size)
Definition class.c:281
mrbc_class * mrbc_find_method(mrbc_method *r_method, mrbc_class *cls, mrbc_sym sym_id)
Definition class.c:417
int mrbc_obj_is_kind_of(const mrbc_value *obj, const mrbc_class *tcls)
Definition class.c:393
void mrbc_define_method(struct VM *vm, mrbc_class *cls, const char *name, mrbc_func_t cfunc)
Definition class.c:249
mrbc_value mrbc_instance_getiv(mrbc_value *target, mrbc_sym sym_id)
Definition class.c:350
mrbc_class * mrbc_traverse_class_tree(mrbc_class *cls, mrbc_class *nest_buf[], int *nest_idx)
Definition class.c:80
int mrbc_instance_setiv(mrbc_value *target, mrbc_sym sym_id, mrbc_value *v)
Definition class.c:322
mrbc_class * mrbc_traverse_class_tree_skip(mrbc_class *nest_buf[], int *nest_idx)
Definition class.c:111
#define MRBC_TRAVERSE_NEST_LEVEL
Definition class.h:35
struct RMethod mrbc_method
Method management structure.
#define MRBC_CLASS(cls)
Definition class.h:55
static mrbc_class * find_class_by_object(const mrbc_value *obj)
Definition class.h:265
struct RClass mrbc_class
Class object.
void mrbc_p(const mrbc_value *v)
Definition console.c:346
int mrbc_printf_float(mrbc_printf_t *pf, double value)
Definition console.c:939
int mrbc_print_sub(const mrbc_value *v)
Definition console.c:522
void mrbc_printf(const char *fstr,...)
Definition console.c:205
int mrbc_printf_int(mrbc_printf_t *pf, mrbc_int_t value, unsigned int base)
Definition console.c:792
int mrbc_printf_char(mrbc_printf_t *pf, int ch)
Definition console.c:707
void mrbc_nprint(const char *str, int size)
Definition console.c:176
int mrbc_printf_bstr(mrbc_printf_t *pf, const char *str, int len, int pad)
Definition console.c:740
int mrbc_puts_sub(const mrbc_value *v)
Definition console.c:499
void mrbc_snprintf(char *buf, int bufsiz, const char *fstr,...)
Definition console.c:241
void mrbc_printf_replace_buffer(mrbc_printf_t *pf, char *buf, int size)
Definition console.c:629
void mrbc_putchar(char c)
Definition console.c:131
int mrbc_printf_bit(mrbc_printf_t *pf, mrbc_int_t value, int bit)
Definition console.c:879
int mrbc_printf_main(mrbc_printf_t *pf)
Definition console.c:647
static void mrbc_printf_end(mrbc_printf_t *pf)
Definition console.h:150
static int mrbc_printf_len(mrbc_printf_t *pf)
Definition console.h:162
struct RPrintf mrbc_printf_t
printf tiny (mruby/c) version data container.
static int mrbc_printf_str(mrbc_printf_t *pf, const char *str, int pad)
Definition console.h:178
static void mrbc_printf_init(mrbc_printf_t *pf, char *buf, int size, const char *fstr)
Definition console.h:124
void mrbc_raise(struct VM *vm, struct RClass *exc_cls, const char *msg)
Definition error.c:145
mrbc_value mrbc_exception_new(struct VM *vm, struct RClass *exc_cls, const void *message, int len)
Definition error.c:65
#define mrbc_israised(vm)
Definition error.h:38
void mrbc_get_all_class_const(const mrbc_class *cls, mrbc_value *ret)
Definition global.c:124
void mrbc_kv_dup(const mrbc_kv_handle *src, mrbc_kv_handle *dst)
Definition keyvalue.c:397
struct RKeyValueHandle mrbc_kv_handle
Key-Value handle.
Include at once the necessary header files.
mrbc_sym method_id
called method ID.
Definition vm.h:134
uint8_t n_args
num of arguments.
Definition vm.h:136
mrbc_value * cur_regs
copy from mrbc_vm.
Definition vm.h:128
struct RClass * own_class
class that owns method.
Definition vm.h:132
uint8_t reg_offset
register offset after call.
Definition vm.h:135
const uint8_t * inst
pointer to instruction in RITE binary
Definition vm.h:63
Return value structure for mrbc_alloc_statistics function.
Definition alloc.h:39
Built-in class object.
Definition class.h:128
struct RMethod * method_link
pointer to method link.
Definition class.h:107
unsigned int flag_alias
is module alias?
Definition class.h:101
struct RClass * super
pointer to super class.
Definition class.h:103
struct RClass * aliased
aliased class or module.
Definition class.h:108
mrbc_sym sym_id
class name's symbol ID
Definition class.h:97
uint8_t num_builtin_method
num of built-in method.
Definition class.h:102
unsigned int flag_nomethod
is built-in no method class? (= 0)
Definition class.h:99
mrbc_sym method_id
raised method, if it is known.
Definition error.h:52
struct RClass * cls
pointer to class of this object.
Definition class.h:183
struct RKeyValueHandle ivar
instance variable.
Definition class.h:184
uint16_t n_stored
num of stored.
Definition keyvalue.h:55
mrbc_kv * data
pointer to allocated memory.
Definition keyvalue.h:57
uint16_t data_size
data buffer size.
Definition keyvalue.h:54
mrbc_sym sym_id
symbol ID as key.
Definition keyvalue.h:43
struct RClass * cls
return value for mrbc_find_method.
Definition class.h:208
mrbc_sym sym_id
function names symbol ID
Definition class.h:201
struct IREP * irep
to IREP for ruby proc.
Definition class.h:203
struct RMethod * next
link to next method.
Definition class.h:207
uint8_t c_func
0:IREP, 1:C Func, 2:C Func (built-in)
Definition class.h:200
mrbc_func_t func
to C function.
Definition class.h:204
struct RProc * proc
Definition boxing_no.h:30
struct RException * exception
Definition boxing_no.h:35
struct RInstance * instance
Definition boxing_no.h:29
mrbc_vtype tt
Definition boxing_no.h:20
struct RClass * cls
Definition boxing_no.h:28
int16_t width
display width. (e.g. %10d as 10)
Definition console.h:56
char type
format char. (e.g. 'd','f','x'...)
Definition console.h:51
struct RPrintfFormat fmt
Definition console.h:70
char * buf
output buffer.
Definition console.h:66
struct CALLINFO * callinfo_self
callinfo of self object. Valid when 'B'.
Definition c_proc.h:48
const mrbc_irep * cur_irep
IREP currently running.
Definition vm.h:162
mrbc_callinfo * callinfo_tail
Last point of CALLINFO link.
Definition vm.h:166
volatile int8_t flag_preemption
Definition vm.h:155
mrbc_value exception
Raised exception or nil.
Definition vm.h:169
const uint8_t * inst
Instruction pointer.
Definition vm.h:163
mrbc_value * cur_regs
Current register top.
Definition vm.h:164
mrbc_value mrbc_symbol_new(struct VM *vm, const char *str)
Definition symbol.c:335
mrbc_sym mrbc_str_to_symid(const char *str)
Definition symbol.c:218
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 const char * mrbc_symbol_cstr(const mrbc_value *v)
Definition symbol.h:61
static int mrbc_is_nested_symid(mrbc_sym sym_id)
Definition symbol.h:74
#define MRBC_SYM(sym)
Definition symbol.h:34
int mrbc_compare(const mrbc_value *v1, const mrbc_value *v2)
Definition value.c:68
int mrbc_strcpy(char *dest, int destsize, const char *src)
Definition value.c:241
int32_t mrbc_int_t
Definition value.h:47
static void mrbc_decref(mrbc_value *v)
Definition value.h:572
#define SET_BOOL_RETURN(n)
Definition value.h:238
#define SET_INT_RETURN(n)
Definition value.h:243
static void mrbc_decref_empty(mrbc_value *v)
Definition value.h:590
#define MRBC_PTR_TO_UINT32(p)
Definition value.h:513
#define SET_NIL_RETURN()
Definition value.h:226
static void mrbc_incref(mrbc_value *v)
Definition value.h:557
int16_t mrbc_sym
mruby/c symbol ID
Definition value.h:62
@ MRBC_TT_FALSE
FalseClass.
Definition value.h:82
@ MRBC_TT_SYMBOL
Symbol.
Definition value.h:89
@ MRBC_TT_STRING
String.
Definition value.h:98
@ MRBC_TT_FLOAT
Float.
Definition value.h:88
@ MRBC_TT_INTEGER
Integer.
Definition value.h:86
@ MRBC_TT_EXCEPTION
Exception.
Definition value.h:101
@ MRBC_TT_PROC
Proc.
Definition value.h:96
@ MRBC_TT_OBJECT
General instance.
Definition value.h:95
@ MRBC_TT_MODULE
Module.
Definition value.h:91
@ MRBC_TT_TRUE
TrueClass.
Definition value.h:85
@ MRBC_TT_NIL
NilClass.
Definition value.h:81
@ MRBC_TT_CLASS
Class.
Definition value.h:90
#define SET_RETURN(n)
Definition value.h:221
#define SET_FALSE_RETURN()
Definition value.h:230
mrbc_callinfo * mrbc_push_callinfo(mrbc_vm *vm, mrbc_sym method_id, int reg_offset, int n_args)
Definition vm.c:210
struct CALLINFO mrbc_callinfo
Call information.
static const char * mrbc_get_callee_name(const mrbc_vm *vm)
Definition vm.h:227
struct VM mrbc_vm
Virtual Machine.
static mrbc_sym mrbc_get_callee_symid(const mrbc_vm *vm)
Definition vm.h:215
Global configuration of mruby/c VM's.