mruby/c VM Source Code release 4.0.0
Loading...
Searching...
No Matches
c_proc.c
Go to the documentation of this file.
1
14
15/***** Feature test switches ************************************************/
16/***** System headers *******************************************************/
17//@cond
18#include "vm_config.h"
19//@endcond
20
21/***** Local headers ********************************************************/
22#include "mrubyc.h"
23
24/***** Constat values *******************************************************/
25/***** Macros ***************************************************************/
26/***** Typedefs *************************************************************/
27/***** Function prototypes **************************************************/
28/***** Local variables ******************************************************/
29/***** Global variables *****************************************************/
30/***** Signal catching functions ********************************************/
31/***** Local functions ******************************************************/
32/***** Global functions *****************************************************/
33
34//================================================================
42mrbc_value mrbc_proc_new(struct VM *vm, void *irep, uint8_t b_or_m)
43{
44 mrbc_proc *proc = mrbc_alloc(vm, sizeof(mrbc_proc));
45
46 memset(proc, 0, sizeof(mrbc_proc));
47 MRBC_INIT_OBJECT_HEADER( proc, "PR" );
48 proc->block_or_method = b_or_m;
49 if( b_or_m == 'B' ) {
50 if( mrbc_type(vm->cur_regs[0]) == MRBC_TT_PROC ) {
52 proc->self = vm->cur_regs[0].proc->self;
53 } else {
54 proc->callinfo_self = vm->callinfo_tail;
55 proc->self = vm->cur_regs[0];
56 }
57 mrbc_incref(&proc->self);
58 }
59 proc->callinfo = vm->callinfo_tail;
60 proc->irep = irep;
61
62 return mrbc_immediate_value(MRBC_TT_PROC, .proc = proc);
63}
64
65
66//================================================================
72{
73 mrbc_decref(&val->proc->self);
74 mrbc_raw_free(val->proc);
75}
76
77
78#if defined(MRBC_ALLOC_VMID)
79//================================================================
84void mrbc_proc_clear_vm_id(mrbc_value *v)
85{
86 mrbc_set_vm_id( v->proc, 0 );
87}
88#endif
89
90
91
92/***** Proc class ***********************************************************/
93//================================================================
96static void c_proc_new(mrbc_vm *vm, mrbc_value v[], int argc)
97{
98 if( mrbc_type(v[1]) != MRBC_TT_PROC ) {
99 mrbc_raise(vm, MRBC_CLASS(ArgumentError),
100 "tried to create Proc object without a block");
101 return;
102 }
103
104 v[0] = v[1];
106}
107
108
109//================================================================
112static void c_proc_call(mrbc_vm *vm, mrbc_value v[], int argc)
113{
114 assert( mrbc_type(v[0]) == MRBC_TT_PROC );
115
116 mrbc_callinfo *callinfo_self = v[0].proc->callinfo_self;
117 mrbc_callinfo *callinfo = mrbc_push_callinfo(vm,
118 (callinfo_self ? callinfo_self->method_id : 0),
119 v - vm->cur_regs, argc);
120 if( !callinfo ) return;
121
122 if( callinfo_self ) {
123 callinfo->own_class = callinfo_self->own_class;
124 }
125 callinfo->is_called_block = 1;
126
127 // target irep
128 vm->cur_irep = v[0].proc->irep;
129 vm->inst = vm->cur_irep->inst;
130 vm->cur_regs = v;
131}
132
133
134/* MRBC_AUTOGEN_METHOD_TABLE
135
136 CLASS("Proc")
137 FILE("_autogen_class_proc.h")
138
139 METHOD( "new", c_proc_new )
140 METHOD( "call", c_proc_call )
141*/
142#include "_autogen_class_proc.h"
void mrbc_raw_free(void *ptr)
Definition alloc.c:707
#define mrbc_immediate_value(...)
Definition boxing_no.h:71
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
struct RObject mrbc_value
Value object. Default version.
void mrbc_proc_delete(mrbc_value *val)
Definition c_proc.c:71
mrbc_value mrbc_proc_new(struct VM *vm, void *irep, uint8_t b_or_m)
Definition c_proc.c:42
struct RProc mrbc_proc
Proc object.
#define MRBC_CLASS(cls)
Definition class.h:55
void mrbc_raise(struct VM *vm, struct RClass *exc_cls, const char *msg)
Definition error.c:145
Include at once the necessary header files.
mrbc_sym method_id
called method ID.
Definition vm.h:134
struct RClass * own_class
class that owns method.
Definition vm.h:132
uint8_t is_called_block
flags when block calls.
Definition vm.h:138
const uint8_t * inst
pointer to instruction in RITE binary
Definition vm.h:63
struct RProc * proc
Definition boxing_no.h:30
struct IREP * irep
Target IREP.
Definition c_proc.h:49
mrbc_value self
Copy of self object. Valid when 'B'.
Definition c_proc.h:50
uint8_t block_or_method
'B' or 'M' char code
Definition c_proc.h:46
struct CALLINFO * callinfo
callinfo when proc was created.
Definition c_proc.h:47
struct CALLINFO * callinfo_self
callinfo of self object. Valid when 'B'.
Definition c_proc.h:48
Virtual Machine.
Definition vm.h:150
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
const uint8_t * inst
Instruction pointer.
Definition vm.h:163
mrbc_value * cur_regs
Current register top.
Definition vm.h:164
static void mrbc_decref(mrbc_value *v)
Definition value.h:561
#define MRBC_INIT_OBJECT_HEADER(p, t)
Definition value.h:146
static void mrbc_incref(mrbc_value *v)
Definition value.h:546
@ MRBC_TT_EMPTY
Definition value.h:80
@ MRBC_TT_PROC
Proc.
Definition value.h:96
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.
struct VM mrbc_vm
Virtual Machine.
Global configuration of mruby/c VM's.