mruby/c VM Source Code release 4.0.0
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/***** 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//================================================================
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//================================================================
83void mrbc_object_inspect(mrbc_vm *vm, mrbc_value v[], int argc)
84{
85 char buf[64]; // max length of class (or object) name.
86 char *s = buf;
87 mrbc_sym sym_id = find_class_by_object(&v[0])->sym_id;
88 int class_or_module = (mrbc_type(v[0]) == MRBC_TT_CLASS || mrbc_type(v[0]) == MRBC_TT_MODULE);
89
90 if (!class_or_module) {
91 buf[0] = '#'; buf[1] = '<';
92 s = buf + 2;
93 }
94
95 int bufsiz = sizeof(buf) - (s - buf);
96 int n = set_sym_name_by_id( s, bufsiz, sym_id );
97
98 if (!class_or_module) {
99 mrbc_snprintf(s+n, bufsiz-n, ":%08x>", MRBC_PTR_TO_UINT32(v->instance));
100 }
101
102 SET_RETURN( mrbc_string_new_cstr( vm, buf ));
103}
104
105
106/***** Object class *********************************************************/
107//================================================================
110static void c_object_new(mrbc_vm *vm, mrbc_value v[], int argc)
111{
112 v[0] = mrbc_instance_new(vm, v[0].cls, 0);
113 mrbc_instance_call_initialize( vm, v, argc );
114}
115
116
117//================================================================
120static void c_object_not(mrbc_vm *vm, mrbc_value v[], int argc)
121{
123}
124
125
126//================================================================
129static void c_object_neq(mrbc_vm *vm, mrbc_value v[], int argc)
130{
131 int result = mrbc_compare( &v[0], &v[1] );
132 SET_BOOL_RETURN( result != 0 );
133}
134
135
136//================================================================
139static void c_object_compare(mrbc_vm *vm, mrbc_value v[], int argc)
140{
141 int result = mrbc_compare( &v[0], &v[1] );
142 SET_INT_RETURN( result );
143}
144
145
146//================================================================
149static void c_object_equal2(mrbc_vm *vm, mrbc_value v[], int argc)
150{
151 int result = mrbc_compare( &v[0], &v[1] );
152 SET_BOOL_RETURN( result == 0 );
153}
154
155
156//================================================================
159static void c_object_equal3(mrbc_vm *vm, mrbc_value v[], int argc)
160{
161 int result;
162
163 if( mrbc_type(v[0]) == MRBC_TT_CLASS ) {
164 result = mrbc_obj_is_kind_of( &v[1], v[0].cls );
165 } else {
166 result = (mrbc_compare( &v[0], &v[1] ) == 0);
167 }
168
169 SET_BOOL_RETURN( result );
170}
171
172
173//================================================================
176static void c_object_class(mrbc_vm *vm, mrbc_value v[], int argc)
177{
178 mrbc_value value;
179
180 mrbc_set_tt(&value, MRBC_TT_CLASS);
181 value.cls = find_class_by_object( v );
182 SET_RETURN( value );
183}
184
185
186//================================================================
189static void c_object_dup(mrbc_vm *vm, mrbc_value v[], int argc)
190{
191 if( mrbc_type(v[0]) == MRBC_TT_OBJECT ) {
192 mrbc_value new_obj = mrbc_instance_new(vm, v->instance->cls, 0);
193 mrbc_kv_dup( &v->instance->ivar, &new_obj.instance->ivar );
194
195 mrbc_decref( v );
196 *v = new_obj;
197 return;
198 }
199
200
201 // TODO: need support TT_PROC and TT_RANGE. but really need?
202 return;
203}
204
205
206//================================================================
209static void c_object_block_given(mrbc_vm *vm, mrbc_value v[], int argc)
210{
211 mrbc_callinfo *callinfo = vm->callinfo_tail;
212 if( !callinfo ) goto RETURN_FALSE;
213
214 mrbc_value *regs = callinfo->cur_regs + callinfo->reg_offset;
215
216 if( mrbc_type(regs[0]) == MRBC_TT_PROC ) {
217 callinfo = regs[0].proc->callinfo_self;
218 if( !callinfo ) goto RETURN_FALSE;
219
220 regs = callinfo->cur_regs + callinfo->reg_offset;
221 }
222
223 SET_BOOL_RETURN( mrbc_type(regs[callinfo->n_args+1]) == MRBC_TT_PROC );
224 return;
225
226 RETURN_FALSE:
228}
229
230
231//================================================================
234static void c_object_kind_of(mrbc_vm *vm, mrbc_value v[], int argc)
235{
236 if( mrbc_type(v[1]) != MRBC_TT_CLASS && mrbc_type(v[1]) != MRBC_TT_MODULE ) {
237 mrbc_raise(vm, MRBC_CLASS(TypeError), "class or module required");
238 return;
239 }
240
241 SET_BOOL_RETURN( mrbc_obj_is_kind_of( &v[0], v[1].cls ));
242}
243
244
245//================================================================
248static void c_object_nil(mrbc_vm *vm, mrbc_value v[], int argc)
249{
251}
252
253
254//================================================================
257#if !defined(MRBC_NO_STDIO)
258static void c_object_p(mrbc_vm *vm, mrbc_value v[], int argc)
259{
260 for( int i = 1; i <= argc; i++ ) {
261 mrbc_p( &v[i] );
262 }
263
264 if (argc == 0) {
266
267 } else if (argc == 1) {
268 mrbc_incref( &v[1] );
269 SET_RETURN(v[1]);
270
271 } else {
272 mrbc_value value = mrbc_array_new(vm, argc);
273 for( int i = 1; i <= argc; i++ ) {
274 mrbc_incref( &v[i] );
275 mrbc_array_push( &value, &v[i] );
276 }
277 SET_RETURN(value);
278 }
279}
280#endif
281
282
283//================================================================
286#if !defined(MRBC_NO_STDIO)
287static void c_object_print(mrbc_vm *vm, mrbc_value v[], int argc)
288{
289 for( int i = 1; i <= argc; i++ ) {
290 mrbc_print_sub( &v[i] );
291 }
293}
294#endif
295
296
297//================================================================
300#if !defined(MRBC_NO_STDIO)
301static void c_object_puts(mrbc_vm *vm, mrbc_value v[], int argc)
302{
303 if( argc ) {
304 for( int i = 1; i <= argc; i++ ) {
305 if( mrbc_puts_sub( &v[i] ) == 0 ) mrbc_putchar('\n');
306 }
307 } else {
308 mrbc_putchar('\n');
309 }
311}
312#endif
313
314
315//================================================================
325static void c_object_raise(mrbc_vm *vm, mrbc_value v[], int argc)
326{
327 assert( !mrbc_israised(vm) );
328
329 // case 1. raise (no argument)
330 if( argc == 0 ) {
331 vm->exception = mrbc_exception_new( vm, MRBC_CLASS(RuntimeError), "", 0 );
332 } else
333
334 // case 2. raise "message"
335 if( argc == 1 && mrbc_type(v[1]) == MRBC_TT_STRING ) {
336 vm->exception = mrbc_exception_new( vm, MRBC_CLASS(RuntimeError),
337 mrbc_string_cstr(&v[1]), mrbc_string_size(&v[1]) );
338 } else
339
340 // case 3. raise ExceptionClass
341 if( argc == 1 && mrbc_type(v[1]) == MRBC_TT_CLASS &&
342 mrbc_obj_is_kind_of( &v[1], MRBC_CLASS(Exception))) {
343 vm->exception = mrbc_exception_new( vm, v[1].cls, 0, 0 );
344 } else
345
346 // case 4. raise ExceptionObject
347 if( argc == 1 && mrbc_type(v[1]) == MRBC_TT_EXCEPTION ) {
348 mrbc_incref( &v[1] );
349 vm->exception = v[1];
350 } else
351
352 // case 5. raise ExceptionClass, "param"
353 if( argc == 2 && mrbc_type(v[1]) == MRBC_TT_CLASS
354 && mrbc_type(v[2]) == MRBC_TT_STRING ) {
355 vm->exception = mrbc_exception_new( vm, v[1].cls,
356 mrbc_string_cstr(&v[2]), mrbc_string_size(&v[2]) );
357 } else
358
359 // case 6. raise ExceptionObject, "param"
360 if( argc == 2 && mrbc_type(v[1]) == MRBC_TT_EXCEPTION
361 && mrbc_type(v[2]) == MRBC_TT_STRING ) {
362 vm->exception = mrbc_exception_new( vm, v[1].exception->cls,
363 mrbc_string_cstr(&v[2]), mrbc_string_size(&v[2]) );
364 } else {
365
366 // fail.
367 vm->exception = mrbc_exception_new( vm, MRBC_CLASS(ArgumentError), 0, 0 );
368 }
369
370 // set raised method to exception instance.
371 if( vm->callinfo_tail != 0 &&
373 vm->exception.exception->method_id == 0 ) {
375 }
376
377 vm->flag_preemption = 2;
378}
379
380
381#if defined(MRBC_DEBUG)
382//================================================================
385static void c_object_object_id(mrbc_vm *vm, mrbc_value v[], int argc)
386{
387 // tiny implementation.
389}
390
391
392//================================================================
397static void c_object_instance_methods(mrbc_vm *vm, mrbc_value v[], int argc)
398{
399 if( mrbc_type(v[0]) != MRBC_TT_CLASS ) return;
400
401 int flag_inherit = !(argc >= 1 && mrbc_type(v[1]) == MRBC_TT_FALSE);
402 mrbc_value ret = mrbc_array_new( vm, 0 );
403 mrbc_class *cls = v[0].cls;
405 int nest_idx = 0;
406
407 while( 1 ) {
408 // builtin method.
409 for( int i = 0; i < cls->num_builtin_method; i++ ) {
410 mrbc_array_push( &ret,
411 &mrbc_symbol_value(((struct RBuiltinClass *)cls)->method_symbols[i]) );
412 }
413
414 // no builtin method.
415 const mrbc_method *method = cls->flag_nomethod ? NULL : cls->method_link;
416 while( method ) {
417 mrbc_array_push( &ret, &mrbc_symbol_value(method->sym_id) );
418 method = method->next;
419 }
420
421 if( !flag_inherit ) break;
422
423 cls = mrbc_traverse_class_tree( cls, nest_buf, &nest_idx );
424 if( !cls ) break;
425 if( cls->flag_alias ) cls = cls->aliased;
426 }
427
428 SET_RETURN(ret);
429}
430
431
432//================================================================
435static void c_object_instance_variables(mrbc_vm *vm, mrbc_value v[], int argc)
436{
437 // temporary code for operation check.
438
439 mrbc_value ret = mrbc_array_new( vm, 0 );
440 mrbc_kv_handle *kvh = &v[0].instance->ivar;
441#if 0
442 mrbc_printf("n = %d/%d ", kvh->n_stored, kvh->data_size);
443#endif
444
445 if( mrbc_type(v[0]) == MRBC_TT_OBJECT ) {
446 for( int i = 0; i < kvh->n_stored; i++ ) {
447 mrbc_array_push( &ret, &mrbc_symbol_value(kvh->data[i].sym_id) );
448 }
449 }
450
451 SET_RETURN(ret);
452}
453
454
455#if !defined(MRBC_ALLOC_LIBC)
456//================================================================
459static void c_object_memory_statistics(mrbc_vm *vm, mrbc_value v[], int argc)
460{
461 struct MRBC_ALLOC_STATISTICS mem;
462
463 mrbc_alloc_statistics( &mem );
464 if( argc == 0 || mrbc_type(v[1]) == MRBC_TT_TRUE ) {
465 mrbc_printf("Memory Statistics\n");
466 mrbc_printf(" Total: %d\n", mem.total);
467 mrbc_printf(" Used : %d\n", mem.used);
468 mrbc_printf(" Free : %d\n", mem.free);
469 mrbc_printf(" Frag.: %d\n", mem.fragmentation);
470 }
471
472 // make a return value.
473 mrbc_value ret = mrbc_hash_new(vm, 4);
475 &mrbc_integer_value( mem.total ));
477 &mrbc_integer_value( mem.used ));
479 &mrbc_integer_value( mem.free ));
480 mrbc_hash_set(&ret, &mrbc_symbol_value( mrbc_str_to_symid("fragmentation") ),
481 &mrbc_integer_value( mem.fragmentation ));
482
483 SET_RETURN(ret);
484}
485#endif // MRBC_ALLOC_LIBC
486#endif // MRBC_DEBUG
487
488
489//================================================================
492static void c_object_getiv(mrbc_vm *vm, mrbc_value v[], int argc)
493{
494 mrbc_sym sym_id = mrbc_get_callee_symid(vm);
495 mrbc_value ret = mrbc_instance_getiv(&v[0], sym_id);
496
497 SET_RETURN(ret);
498}
499
500
501//================================================================
504static void c_object_setiv(mrbc_vm *vm, mrbc_value v[], int argc)
505{
506 char namebuf_auto[16];
507 char *namebuf;
508 const char *name = mrbc_get_callee_name(vm);
509 int len = strlen(name);
510
511 if( sizeof(namebuf_auto) < len ) {
512 namebuf = mrbc_alloc(vm, len);
513 } else {
514 namebuf = namebuf_auto;
515 }
516
517 memcpy( namebuf, name, len-1 );
518 namebuf[len-1] = '\0'; // delete '='
519 mrbc_sym sym_id = mrbc_str_to_symid(namebuf);
520
521 mrbc_instance_setiv(&v[0], sym_id, &v[1]);
522
523 if( namebuf != namebuf_auto ) mrbc_free(vm, namebuf);
524}
525
526
527//================================================================
530static void c_object_attr_reader(mrbc_vm *vm, mrbc_value v[], int argc)
531{
532 for( int i = 1; i <= argc; i++ ) {
533 if( mrbc_type(v[i]) != MRBC_TT_SYMBOL ) {
534 // Not support "String" only :symbol
535 mrbc_raise(vm, MRBC_CLASS(TypeError), "not a symbol");
536 return;
537 }
538
539 // define reader method
540 const char *name = mrbc_symbol_cstr(&v[i]);
541 mrbc_define_method(vm, v[0].cls, name, c_object_getiv);
542 }
543}
544
545
546//================================================================
549static void c_object_attr_accessor(mrbc_vm *vm, mrbc_value v[], int argc)
550{
551 for( int i = 1; i <= argc; i++ ) {
552 if( mrbc_type(v[i]) != MRBC_TT_SYMBOL ) {
553 // Not support "String" only :symbol
554 mrbc_raise(vm, MRBC_CLASS(TypeError), "not a symbol");
555 return;
556 }
557
558 // define reader method
559 const char *name = mrbc_symbol_cstr(&v[i]);
560 mrbc_define_method(vm, v[0].cls, name, c_object_getiv);
561
562 // make string "....=" and define writer method.
563 int len = strlen(name);
564 char *namebuf = mrbc_alloc(vm, len+2);
565
566 memcpy(namebuf, name, len);
567 namebuf[len] = '=';
568 namebuf[len+1] = 0;
569 mrbc_symbol_new(vm, namebuf);
570 mrbc_define_method(vm, v[0].cls, namebuf, c_object_setiv);
571 mrbc_free(vm, namebuf);
572 }
573}
574
575
576//================================================================
579static void c_object_include(mrbc_vm *vm, mrbc_value v[], int argc)
580{
581 mrbc_class *self;
582
583 if( mrbc_type(v[0]) == MRBC_TT_CLASS || mrbc_type(v[0]) == MRBC_TT_MODULE ) {
584 self = v[0].cls;
585 } else if( vm->callinfo_tail == 0 ) { // is top level?
586 self = MRBC_CLASS(Object);
587 } else {
588 mrbc_raise( vm, 0, 0 );
589 return; // Error.
590 }
591
592 for( int i = 1; i <= argc; i++ ) {
593 if( mrbc_type(v[i]) != MRBC_TT_MODULE ) {
594 mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong argument type Class");
595 return;
596 }
597 mrbc_class *module = v[i].cls;
598 mrbc_class *alias = mrbc_raw_alloc_no_free( sizeof(mrbc_class) );
599
600 *alias = (mrbc_class){
601 .sym_id = module->sym_id,
602 .flag_module = 1,
603 .flag_alias = 1,
604 .super = self->super,
605 .aliased = module,
606#if defined(MRBC_DEBUG)
607 .obj_mark_ = "MA",
608 .name = module->name,
609#endif
610 };
611 self->super = alias;
612 }
613}
614
615
616//================================================================
619static void c_object_constants(mrbc_vm *vm, mrbc_value v[], int argc)
620{
621 if( mrbc_type(v[0]) != MRBC_TT_CLASS ) {
622 mrbc_raise(vm, MRBC_CLASS(NoMethodError), 0);
623 return;
624 }
625
626 int flag_inherit = !(argc >= 1 && mrbc_type(v[1]) == MRBC_TT_FALSE);
627 mrbc_value ret = mrbc_array_new( vm, 0 );
628 mrbc_class *cls = v[0].cls;
630 int nest_idx = 0;
631
632
633 while( 1 ) {
634 mrbc_get_all_class_const( cls, &ret );
635 if( !flag_inherit ) break;
636
637 cls = mrbc_traverse_class_tree( cls, nest_buf, &nest_idx );
638 if( cls == MRBC_CLASS(Object) ) {
639 cls = mrbc_traverse_class_tree_skip( nest_buf, &nest_idx );
640 }
641 if( !cls ) break;
642 if( cls->flag_alias ) cls = cls->aliased;
643 }
644
645 SET_RETURN(ret);
646}
647
648
649#if MRBC_USE_STRING
650//================================================================
653static void c_object_sprintf(mrbc_vm *vm, mrbc_value v[], int argc)
654{
655 static const int BUF_INC_STEP = 32; // bytes.
656
657 mrbc_value *format = &v[1];
658 if( mrbc_type(*format) != MRBC_TT_STRING ) {
659 mrbc_raise(vm, MRBC_CLASS(TypeError), 0);
660 return;
661 }
662
663 int buflen = BUF_INC_STEP;
664 char *buf = mrbc_alloc(vm, buflen);
665 mrbc_printf_t pf;
666 mrbc_printf_init( &pf, buf, buflen, mrbc_string_cstr(format) );
667
668 int i = 2;
669 int ret;
670 while( 1 ) {
671 mrbc_printf_t pf_bak = pf;
672 ret = mrbc_printf_main( &pf );
673 if( ret == 0 ) break; // normal break loop.
674 if( ret < 0 ) goto INCREASE_BUFFER;
675
676 if( i > argc ) {
677 mrbc_raise(vm, MRBC_CLASS(ArgumentError), "too few arguments");
678 break;
679 }
680
681 // maybe ret == 1
682 switch(pf.fmt.type) {
683 case 'c':
684 if( mrbc_type(v[i]) == MRBC_TT_INTEGER ) {
685 ret = mrbc_printf_char( &pf, v[i].i );
686 } else if( mrbc_type(v[i]) == MRBC_TT_STRING ) {
687 ret = mrbc_printf_char( &pf, mrbc_string_cstr(&v[i])[0] );
688 }
689 break;
690
691 case 's':
692 if( mrbc_type(v[i]) == MRBC_TT_STRING ) {
693 ret = mrbc_printf_bstr( &pf, mrbc_string_cstr(&v[i]),
694 mrbc_string_size(&v[i]), ' ');
695 } else {
696 const char *s;
697 switch( mrbc_type(v[i]) ) {
698 case MRBC_TT_SYMBOL: s = mrbc_symbol_cstr(&v[i]); break;
699 case MRBC_TT_TRUE: s = "true"; break;
700 case MRBC_TT_FALSE: s = "false"; break;
701 default: s = ""; break;
702 }
703 ret = mrbc_printf_str( &pf, s, ' ');
704 }
705 break;
706
707 case 'd':
708 case 'i':
709 case 'u':
710 if( mrbc_type(v[i]) == MRBC_TT_INTEGER ) {
711 ret = mrbc_printf_int( &pf, v[i].i, 10);
712#if MRBC_USE_FLOAT
713 } else if( mrbc_type(v[i]) == MRBC_TT_FLOAT ) {
714 ret = mrbc_printf_int( &pf, (mrbc_int_t)v[i].d, 10);
715#endif
716 } else if( mrbc_type(v[i]) == MRBC_TT_STRING ) {
717 mrbc_int_t ival = atol(mrbc_string_cstr(&v[i]));
718 ret = mrbc_printf_int( &pf, ival, 10 );
719 }
720 break;
721
722 case 'b':
723 case 'B':
724 if( mrbc_type(v[i]) == MRBC_TT_INTEGER ) {
725 ret = mrbc_printf_bit( &pf, v[i].i, 1);
726 }
727 break;
728
729 case 'x':
730 case 'X':
731 if( mrbc_type(v[i]) == MRBC_TT_INTEGER ) {
732 ret = mrbc_printf_bit( &pf, v[i].i, 4);
733 }
734 break;
735
736 case 'o':
737 if( mrbc_type(v[i]) == MRBC_TT_INTEGER ) {
738 ret = mrbc_printf_bit( &pf, v[i].i, 3);
739 }
740 break;
741
742#if MRBC_USE_FLOAT
743 case 'f':
744 case 'e':
745 case 'E':
746 case 'g':
747 case 'G':
748 if( mrbc_type(v[i]) == MRBC_TT_FLOAT ) {
749 ret = mrbc_printf_float( &pf, v[i].d );
750 } else if( mrbc_type(v[i]) == MRBC_TT_INTEGER ) {
751 ret = mrbc_printf_float( &pf, v[i].i );
752 }
753 break;
754#endif
755
756 default:
757 break;
758 }
759 if( ret >= 0 ) {
760 i++;
761 continue; // normal next loop.
762 }
763
764 // maybe buffer full. (ret == -1)
765 if( pf.fmt.width > BUF_INC_STEP ) buflen += pf.fmt.width;
766 pf = pf_bak;
767
768 INCREASE_BUFFER:
769 buflen += BUF_INC_STEP;
770 buf = mrbc_realloc(vm, pf.buf, buflen);
771 mrbc_printf_replace_buffer(&pf, buf, buflen);
772 }
773 mrbc_printf_end( &pf );
774
775 buflen = mrbc_printf_len( &pf );
776 mrbc_realloc(vm, pf.buf, buflen+1); // shrink suitable size.
777
778 mrbc_value value = mrbc_string_new_alloc( vm, pf.buf, buflen );
779
780 SET_RETURN(value);
781}
782
783
784//================================================================
787#if !defined(MRBC_NO_STDIO)
788static void c_object_printf(mrbc_vm *vm, mrbc_value v[], int argc)
789{
790 c_object_sprintf(vm, v, argc);
793}
794#endif
795
796
797//================================================================
800static void c_object_inspect(mrbc_vm *vm, mrbc_value v[], int argc)
801{
802 mrbc_object_inspect(vm, v, argc);
803}
804#endif // MRBC_USE_STRING
805
806
807/* MRBC_AUTOGEN_METHOD_TABLE
808
809 CLASS("Object")
810 FILE("_autogen_class_object.h")
811 SUPER(0)
812
813 METHOD( "new", c_object_new )
814 METHOD( "!", c_object_not )
815 METHOD( "!=", c_object_neq )
816 METHOD( "<=>", c_object_compare )
817 METHOD( "==", c_object_equal2 )
818 METHOD( "===", c_object_equal3 )
819 METHOD( "class", c_object_class )
820 METHOD( "dup", c_object_dup )
821 METHOD( "block_given?", c_object_block_given )
822 METHOD( "is_a?", c_object_kind_of )
823 METHOD( "kind_of?", c_object_kind_of )
824 METHOD( "nil?", c_object_nil )
825#if !defined(MRBC_NO_STDIO)
826 METHOD( "p", c_object_p )
827 METHOD( "print", c_object_print )
828 METHOD( "puts", c_object_puts )
829#endif
830 METHOD( "raise", c_object_raise )
831 METHOD( "attr_reader",c_object_attr_reader )
832 METHOD( "attr_accessor", c_object_attr_accessor )
833 METHOD( "include", c_object_include )
834 METHOD( "extend", c_object_include )
835 METHOD( "constants", c_object_constants )
836 METHOD( "public", c_ineffect )
837 METHOD( "private", c_ineffect )
838 METHOD( "protected", c_ineffect )
839
840#if MRBC_USE_STRING
841 METHOD( "sprintf", c_object_sprintf )
842#if !defined(MRBC_NO_STDIO)
843 METHOD( "printf", c_object_printf )
844#endif
845 METHOD( "inspect", c_object_inspect )
846 METHOD( "to_s", c_object_inspect )
847#endif
848
849#if defined(MRBC_DEBUG)
850 METHOD( "object_id", c_object_object_id )
851 METHOD( "instance_methods", c_object_instance_methods )
852 METHOD( "instance_variables", c_object_instance_variables )
853#if !defined(MRBC_ALLOC_LIBC)
854 METHOD( "memory_statistics", c_object_memory_statistics )
855#endif
856#endif
857*/
858
859
860
861/***** Nil class ************************************************************/
862//================================================================
865static void c_nil_to_i(mrbc_vm *vm, mrbc_value v[], int argc)
866{
867 v[0] = mrbc_integer_value(0);
868}
869
870
871//================================================================
874static void c_nil_to_a(mrbc_vm *vm, mrbc_value v[], int argc)
875{
876 v[0] = mrbc_array_new(vm, 0);
877}
878
879
880//================================================================
883static void c_nil_to_h(mrbc_vm *vm, mrbc_value v[], int argc)
884{
885 v[0] = mrbc_hash_new(vm, 0);
886}
887
888
889#if MRBC_USE_FLOAT
890//================================================================
893static void c_nil_to_f(mrbc_vm *vm, mrbc_value v[], int argc)
894{
895 v[0] = mrbc_float_value(vm,0);
896}
897#endif
898
899
900#if MRBC_USE_STRING
901//================================================================
904static void c_nil_inspect(mrbc_vm *vm, mrbc_value v[], int argc)
905{
906 if( mrbc_type(v[0]) == MRBC_TT_CLASS ) {
907 mrbc_object_inspect(vm, v, argc);
908 return;
909 }
910
911 v[0] = mrbc_string_new_cstr(vm, "nil");
912}
913
914
915//================================================================
918static void c_nil_to_s(mrbc_vm *vm, mrbc_value v[], int argc)
919{
920 if( mrbc_type(v[0]) == MRBC_TT_CLASS ) {
921 mrbc_object_inspect(vm, v, argc);
922 return;
923 }
924
925 v[0] = mrbc_string_new(vm, NULL, 0);
926}
927#endif // MRBC_USE_STRING
928
929
930/* MRBC_AUTOGEN_METHOD_TABLE
931
932 CLASS("NilClass")
933 APPEND("_autogen_class_object.h")
934
935 METHOD( "to_i", c_nil_to_i )
936 METHOD( "to_a", c_nil_to_a )
937 METHOD( "to_h", c_nil_to_h )
938
939#if MRBC_USE_FLOAT
940 METHOD( "to_f", c_nil_to_f )
941#endif
942
943#if MRBC_USE_STRING
944 METHOD( "inspect", c_nil_inspect )
945 METHOD( "to_s", c_nil_to_s )
946#endif
947*/
948
949
950
951/***** True class ***********************************************************/
952#if MRBC_USE_STRING
953//================================================================
956static void c_true_inspect(mrbc_vm *vm, mrbc_value v[], int argc)
957{
958 if( mrbc_type(v[0]) == MRBC_TT_CLASS ) {
959 mrbc_object_inspect(vm, v, argc);
960 return;
961 }
962
963 v[0] = mrbc_string_new_cstr(vm, "true");
964}
965#endif
966
967
968/* MRBC_AUTOGEN_METHOD_TABLE
969
970 CLASS("TrueClass")
971 APPEND("_autogen_class_object.h")
972
973#if MRBC_USE_STRING
974 METHOD( "inspect", c_true_inspect )
975 METHOD( "to_s", c_true_inspect )
976#endif
977*/
978
979
980
981/***** False class **********************************************************/
982#if MRBC_USE_STRING
983//================================================================
986static void c_false_inspect(mrbc_vm *vm, mrbc_value v[], int argc)
987{
988 if( mrbc_type(v[0]) == MRBC_TT_CLASS ) {
989 mrbc_object_inspect(vm, v, argc);
990 return;
991 }
992
993 v[0] = mrbc_string_new_cstr(vm, "false");
994}
995#endif // MRBC_USE_STRING
996
997
998/* MRBC_AUTOGEN_METHOD_TABLE
999
1000 CLASS("FalseClass")
1001 APPEND("_autogen_class_object.h")
1002
1003#if MRBC_USE_STRING
1004 METHOD( "inspect", c_false_inspect )
1005 METHOD( "to_s", c_false_inspect )
1006#endif
1007*/
1008#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:70
static void mrbc_set_tt(mrbc_value *p, mrbc_vtype type)
Definition boxing_no.h:119
#define mrbc_type(o)
Definition boxing_no.h:57
#define mrbc_float_value(vm, n)
Definition boxing_no.h:65
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:64
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:83
static int set_sym_name_by_id(char *buf, int bufsiz, mrbc_sym sym_id)
Definition c_object.c:32
void mrbc_instance_call_initialize(mrbc_vm *vm, mrbc_value v[], int argc)
Definition c_object.c:54
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:306
mrbc_class * mrbc_find_method(mrbc_method *r_method, mrbc_class *cls, mrbc_sym sym_id)
Definition class.c:415
int mrbc_obj_is_kind_of(const mrbc_value *obj, const mrbc_class *tcls)
Definition class.c:391
void mrbc_define_method(struct VM *vm, mrbc_class *cls, const char *name, mrbc_func_t cfunc)
Definition class.c:274
mrbc_value mrbc_instance_getiv(mrbc_value *obj, mrbc_sym sym_id)
Definition class.c:360
mrbc_class * mrbc_traverse_class_tree(mrbc_class *cls, mrbc_class *nest_buf[], int *nest_idx)
Definition class.c:68
void mrbc_instance_setiv(mrbc_value *obj, mrbc_sym sym_id, mrbc_value *v)
Definition class.c:346
mrbc_class * mrbc_traverse_class_tree_skip(mrbc_class *nest_buf[], int *nest_idx)
Definition class.c:99
#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:262
struct RClass mrbc_class
Class object.
void mrbc_p(const mrbc_value *v)
Definition console.c:342
int mrbc_printf_float(mrbc_printf_t *pf, double value)
Definition console.c:935
int mrbc_print_sub(const mrbc_value *v)
Definition console.c:518
void mrbc_printf(const char *fstr,...)
Definition console.c:201
int mrbc_printf_int(mrbc_printf_t *pf, mrbc_int_t value, unsigned int base)
Definition console.c:788
int mrbc_printf_char(mrbc_printf_t *pf, int ch)
Definition console.c:703
void mrbc_nprint(const char *str, int size)
Definition console.c:172
int mrbc_printf_bstr(mrbc_printf_t *pf, const char *str, int len, int pad)
Definition console.c:736
int mrbc_puts_sub(const mrbc_value *v)
Definition console.c:495
void mrbc_snprintf(char *buf, int bufsiz, const char *fstr,...)
Definition console.c:237
void mrbc_printf_replace_buffer(mrbc_printf_t *pf, char *buf, int size)
Definition console.c:625
void mrbc_putchar(char c)
Definition console.c:127
int mrbc_printf_bit(mrbc_printf_t *pf, mrbc_int_t value, int bit)
Definition console.c:875
int mrbc_printf_main(mrbc_printf_t *pf)
Definition console.c:643
static void mrbc_printf_end(mrbc_printf_t *pf)
Definition console.h:148
static int mrbc_printf_len(mrbc_printf_t *pf)
Definition console.h:160
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:176
static void mrbc_printf_init(mrbc_printf_t *pf, char *buf, int size, const char *fstr)
Definition console.h:122
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:123
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:127
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:182
struct RKeyValueHandle ivar
instance variable.
Definition class.h:183
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:207
mrbc_sym sym_id
function names symbol ID
Definition class.h:200
struct IREP * irep
to IREP for ruby proc.
Definition class.h:202
struct RMethod * next
link to next method.
Definition class.h:206
uint8_t c_func
0:IREP, 1:C Func, 2:C Func (built-in)
Definition class.h:199
mrbc_func_t func
to C function.
Definition class.h:203
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:340
mrbc_sym mrbc_str_to_symid(const char *str)
Definition symbol.c:221
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
static const char * mrbc_symbol_cstr(const mrbc_value *v)
Definition symbol.h:58
static int mrbc_is_nested_symid(mrbc_sym sym_id)
Definition symbol.h:71
#define MRBC_SYM(sym)
Definition symbol.h:31
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:561
#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:579
#define MRBC_PTR_TO_UINT32(p)
Definition value.h:504
#define SET_NIL_RETURN()
Definition value.h:226
static void mrbc_incref(mrbc_value *v)
Definition value.h:546
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:206
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.