mruby/c VM Source Code release 3.4
Loading...
Searching...
No Matches
c_proc.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//@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 if( !proc ) goto RETURN; // ENOMEM
46
47 memset(proc, 0, sizeof(mrbc_proc));
48 MRBC_INIT_OBJECT_HEADER( proc, "PR" );
49 proc->block_or_method = b_or_m;
50 if( b_or_m == 'B' ) {
51 if( vm->cur_regs[0].tt == MRBC_TT_PROC ) {
53 proc->self = vm->cur_regs[0].proc->self;
54 } else {
55 proc->callinfo_self = vm->callinfo_tail;
56 proc->self = vm->cur_regs[0];
57 }
58 mrbc_incref(&proc->self);
59 }
60 proc->callinfo = vm->callinfo_tail;
61 proc->irep = irep;
62
63 RETURN:
64 return (mrbc_value){.tt = MRBC_TT_PROC, .proc = proc};
65}
66
67
68//================================================================
74{
75 mrbc_decref(&val->proc->self);
76 mrbc_raw_free(val->proc);
77}
78
79
80#if defined(MRBC_ALLOC_VMID)
81//================================================================
86void mrbc_proc_clear_vm_id(mrbc_value *v)
87{
88 mrbc_set_vm_id( v->proc, 0 );
89}
90#endif
91
92
93
94/***** Proc class ***********************************************************/
95//================================================================
98static void c_proc_new(struct VM *vm, mrbc_value v[], int argc)
99{
100 if( mrbc_type(v[1]) != MRBC_TT_PROC ) {
101 mrbc_raise(vm, MRBC_CLASS(ArgumentError),
102 "tried to create Proc object without a block");
103 return;
104 }
105
106 v[0] = v[1];
107 v[1].tt = MRBC_TT_EMPTY;
108}
109
110
111//================================================================
114static void c_proc_call(struct VM *vm, mrbc_value v[], int argc)
115{
116 assert( mrbc_type(v[0]) == MRBC_TT_PROC );
117
118 mrbc_callinfo *callinfo_self = v[0].proc->callinfo_self;
119 mrbc_callinfo *callinfo = mrbc_push_callinfo(vm,
120 (callinfo_self ? callinfo_self->method_id : 0),
121 v - vm->cur_regs, argc);
122 if( !callinfo ) return;
123
124 if( callinfo_self ) {
125 callinfo->own_class = callinfo_self->own_class;
126 }
127
128 // target irep
129 vm->cur_irep = v[0].proc->irep;
130 vm->inst = vm->cur_irep->inst;
131 vm->cur_regs = v;
132}
133
134
135/* MRBC_AUTOGEN_METHOD_TABLE
136
137 CLASS("Proc")
138 FILE("_autogen_class_proc.h")
139
140 METHOD( "new", c_proc_new )
141 METHOD( "call", c_proc_call )
142*/
143#include "_autogen_class_proc.h"
void mrbc_raw_free(void *ptr)
Definition alloc.c:695
void mrbc_proc_delete(mrbc_value *val)
Definition c_proc.c:73
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:51
void mrbc_raise(struct VM *vm, struct RClass *exc_cls, const char *msg)
Definition error.c:150
Include at once the necessary header files.
mrbc_sym method_id
called method ID.
Definition vm.h:127
mrbc_class * own_class
class that owns method.
Definition vm.h:125
const uint8_t * inst
pointer to instruction in RITE binary
Definition vm.h:62
struct RProc * proc
Definition value.h:162
mrbc_vtype tt
Definition value.h:152
struct IREP * irep
Definition c_proc.h:46
mrbc_value self
Definition c_proc.h:47
uint8_t block_or_method
Definition c_proc.h:43
struct CALLINFO * callinfo
Definition c_proc.h:44
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
const uint8_t * inst
Instruction pointer.
Definition vm.h:154
mrbc_value * cur_regs
Current register top.
Definition vm.h:155
static void mrbc_decref(mrbc_value *v)
Definition value.h:604
#define MRBC_INIT_OBJECT_HEADER(p, t)
Definition value.h:307
static void mrbc_incref(mrbc_value *v)
Definition value.h:589
#define mrbc_type(o)
Definition value.h:193
@ MRBC_TT_EMPTY
Definition value.h:77
@ MRBC_TT_PROC
Proc.
Definition value.h:93
struct RObject mrbc_value
Definition value.h:174
mrbc_callinfo * mrbc_push_callinfo(struct VM *vm, mrbc_sym method_id, int reg_offset, int n_args)
Definition vm.c:219
struct CALLINFO mrbc_callinfo
Call information.
Global configuration of mruby/c VM's.