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