Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-eval/src/eval.c

raw
1 #include <mruby.h>
2 #include <mruby/array.h>
3 #include <mruby/class.h>
4 #include <mruby/compile.h>
5 #include <mruby/irep.h>
6 #include <mruby/proc.h>
7 #include <mruby/opcode.h>
8 #include <mruby/error.h>
9 #include <mruby/presym.h>
10 #include <mruby/variable.h>
11 #include <mruby/internal.h>
12
13 /* provided by mruby-binding */
14 mrb_bool mrb_binding_p(mrb_state *mrb, mrb_value binding);
15 const struct RProc * mrb_binding_extract_proc(mrb_state *mrb, mrb_value binding);
16 struct REnv * mrb_binding_extract_env(mrb_state *mrb, mrb_value binding);
17
18 /* provided by mruby-compiler */
19 typedef mrb_bool mrb_parser_foreach_top_variable_func(mrb_state *mrb, mrb_sym sym, void *user);
20 void mrb_parser_foreach_top_variable(mrb_state *mrb, struct mrb_parser_state *p, mrb_parser_foreach_top_variable_func *func, void *user);
21
22 static struct RProc*
23 create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value binding, const char *file, mrb_int line)
24 {
25 mrbc_context *cxt;
26 struct mrb_parser_state *p;
27 struct RProc *proc;
28 const struct RProc *scope;
29 struct REnv *e;
30 mrb_callinfo *ci; /* callinfo of eval caller */
31 struct RClass *target_class = NULL;
32 struct mrb_context *c = mrb->c;
33
34 if (!mrb_nil_p(binding)) {
35 if (!mrb_binding_p(mrb, binding)) {
36 mrb_raisef(mrb, E_TYPE_ERROR, "wrong argument type %C (expected binding)",
37 mrb_obj_class(mrb, binding));
38 }
39 scope = mrb_binding_extract_proc(mrb, binding);
40 if (MRB_PROC_CFUNC_P(scope)) {
41 e = NULL;
42 }
43 else {
44 e = mrb_binding_extract_env(mrb, binding);
45 mrb_assert(e != NULL);
46 }
47 }
48 else {
49 ci = (c->ci > c->cibase) ? c->ci - 1 : c->cibase;
50 scope = ci->proc;
51 e = NULL;
52 }
53
54 if (file) {
55 if (strlen(file) >= UINT16_MAX) {
56 mrb_raise(mrb, E_ARGUMENT_ERROR, "filename too long");
57 }
58 }
59 else {
60 file = "(eval)";
61 }
62
63 cxt = mrbc_context_new(mrb);
64 cxt->lineno = (uint16_t)line;
65
66 mrbc_filename(mrb, cxt, file);
67 cxt->capture_errors = TRUE;
68 cxt->no_optimize = TRUE;
69 cxt->upper = scope && MRB_PROC_CFUNC_P(scope) ? NULL : scope;
70
71 p = mrb_parse_nstring(mrb, s, len, cxt);
72
73 /* only occur when memory ran out */
74 if (!p) {
75 mrbc_context_free(mrb, cxt);
76 mrb_raise(mrb, E_RUNTIME_ERROR, "Failed to create parser state (out of memory)");
77 }
78
79 if (0 < p->nerr) {
80 /* parse error */
81 mrb_value str;
82
83 mrbc_context_free(mrb, cxt);
84 if (!p->error_buffer[0].message) {
85 mrb_parser_free(p);
86 mrb_raise(mrb, E_SYNTAX_ERROR, "compile error");
87 }
88 if (file) {
89 str = mrb_format(mrb, "file %s line %d: %s",
90 file,
91 p->error_buffer[0].lineno,
92 p->error_buffer[0].message);
93 }
94 else {
95 str = mrb_format(mrb, "line %d: %s",
96 p->error_buffer[0].lineno,
97 p->error_buffer[0].message);
98 }
99 mrb_parser_free(p);
100 mrb_exc_raise(mrb, mrb_exc_new_str(mrb, E_SYNTAX_ERROR, str));
101 }
102
103 proc = mrb_generate_code(mrb, p);
104 if (proc == NULL) {
105 /* codegen error */
106 mrb_parser_free(p);
107 mrbc_context_free(mrb, cxt);
108 mrb_raise(mrb, E_SCRIPT_ERROR, "codegen error");
109 }
110 if (c->ci > c->cibase) {
111 ci = &c->ci[-1];
112 }
113 else {
114 ci = c->cibase;
115 }
116 if (scope) {
117 target_class = MRB_PROC_TARGET_CLASS(scope);
118 if (!MRB_PROC_CFUNC_P(scope)) {
119 if (e == NULL) {
120 /* when `binding` is nil */
121 e = mrb_vm_ci_env(ci);
122 if (e == NULL) {
123 e = mrb_env_new(mrb, c, ci, ci->proc->body.irep->nlocals, ci->stack, target_class);
124 ci->u.env = e;
125 }
126 }
127 proc->e.env = e;
128 proc->flags |= MRB_PROC_ENVSET;
129 mrb_field_write_barrier(mrb, (struct RBasic*)proc, (struct RBasic*)e);
130 }
131 }
132 proc->upper = scope;
133 mrb_vm_ci_target_class_set(mrb->c->ci, target_class);
134 /* mrb_codedump_all(mrb, proc); */
135
136 mrb_parser_free(p);
137 mrbc_context_free(mrb, cxt);
138
139 return proc;
140 }
141
142 static mrb_value
143 exec_irep(mrb_state *mrb, mrb_value self, struct RProc *proc)
144 {
145 /* no argument passed from eval() */
146 mrb->c->ci->n = 0;
147 mrb->c->ci->nk = 0;
148 /* clear block */
149 mrb->c->ci->stack[1] = mrb_nil_value();
150 return mrb_exec_irep(mrb, self, proc);
151 }
152
153 static void
154 binding_eval_error_check(mrb_state *mrb, struct mrb_parser_state *p, const char *file)
155 {
156 if (!p) {
157 mrb_raise(mrb, E_RUNTIME_ERROR, "Failed to create parser state (out of memory)");
158 }
159
160 if (0 < p->nerr) {
161 mrb_value str;
162
163 if (file) {
164 str = mrb_format(mrb, "file %s line %d: %s",
165 file,
166 p->error_buffer[0].lineno,
167 p->error_buffer[0].message);
168 }
169 else {
170 str = mrb_format(mrb, "line %d: %s",
171 p->error_buffer[0].lineno,
172 p->error_buffer[0].message);
173 }
174 mrb_exc_raise(mrb, mrb_exc_new_str(mrb, E_SYNTAX_ERROR, str));
175 }
176 }
177
178 #define LV_BUFFERS 8
179
180 struct expand_lvspace {
181 mrb_irep *irep;
182 struct REnv *env;
183 int numvar;
184 mrb_sym syms[LV_BUFFERS];
185 };
186
187 static mrb_bool
188 expand_lvspace(mrb_state *mrb, mrb_sym sym, void *user)
189 {
190 struct expand_lvspace *p = (struct expand_lvspace*)user;
191 mrb_int symlen;
192 const char *symname = mrb_sym_name_len(mrb, sym, &symlen);
193
194 if (symname && symlen > 0) {
195 if (symname[0] != '&' && symname[0] != '*') {
196 p->syms[p->numvar++] = sym;
197 if (p->numvar >= LV_BUFFERS) {
198 mrb_proc_merge_lvar(mrb, p->irep, p->env, p->numvar, p->syms, NULL);
199 p->numvar = 0;
200 }
201 }
202 }
203
204 return TRUE;
205 }
206
207 struct binding_eval_prepare_body {
208 mrb_value binding;
209 const char *file;
210 mrbc_context *mrbc;
211 struct mrb_parser_state *pstate;
212 };
213
214 static mrb_value
215 binding_eval_prepare_body(mrb_state *mrb, void *opaque)
216 {
217 struct binding_eval_prepare_body *p = (struct binding_eval_prepare_body*)opaque;
218
219 const struct RProc *proc = mrb_binding_extract_proc(mrb, p->binding);
220 mrb_assert(!MRB_PROC_CFUNC_P(proc));
221 p->mrbc->upper = proc;
222 binding_eval_error_check(mrb, p->pstate, p->file);
223
224 struct expand_lvspace args = {
225 (mrb_irep*)proc->body.irep,
226 mrb_binding_extract_env(mrb, p->binding),
227 0,
228 { 0 }
229 };
230 mrb_parser_foreach_top_variable(mrb, p->pstate, expand_lvspace, &args);
231 if (args.numvar > 0) {
232 mrb_proc_merge_lvar(mrb, args.irep, args.env, args.numvar, args.syms, NULL);
233 }
234
235 return mrb_nil_value();
236 }
237
238 static void
239 binding_eval_prepare(mrb_state *mrb, mrb_value binding, const char *expr, mrb_int exprlen, const char *file)
240 {
241 struct binding_eval_prepare_body d = { binding };
242
243 d.mrbc = mrbc_context_new(mrb);
244 d.file = mrbc_filename(mrb, d.mrbc, file ? file : "(eval)");
245 d.mrbc->capture_errors = TRUE;
246 d.pstate = mrb_parse_nstring(mrb, expr, exprlen, d.mrbc);
247
248 mrb_bool error;
249 mrb_value ret = mrb_protect_error(mrb, binding_eval_prepare_body, &d, &error);
250 if (d.pstate) mrb_parser_free(d.pstate);
251 if (d.mrbc) mrbc_context_free(mrb, d.mrbc);
252 if (error) mrb_exc_raise(mrb, ret);
253 }
254
255 static mrb_value
256 f_eval(mrb_state *mrb, mrb_value self)
257 {
258 const char *s;
259 mrb_int len;
260 mrb_value binding = mrb_nil_value();
261 const char *file = NULL;
262 mrb_int line = 1;
263 struct RProc *proc;
264
265 mrb_get_args(mrb, "s|ozi", &s, &len, &binding, &file, &line);
266
267 if (!mrb_nil_p(binding)) {
268 binding_eval_prepare(mrb, binding, s, len, file);
269 }
270 proc = create_proc_from_string(mrb, s, len, binding, file, line);
271 if (!mrb_nil_p(binding)) {
272 self = mrb_iv_get(mrb, binding, MRB_SYM(recv));
273 }
274 mrb_assert(!MRB_PROC_CFUNC_P(proc));
275 return exec_irep(mrb, self, proc);
276 }
277
278 static mrb_value
279 f_instance_eval(mrb_state *mrb, mrb_value self)
280 {
281 if (!mrb_block_given_p(mrb)) {
282 const char *s;
283 mrb_int len;
284 const char *file = NULL;
285 mrb_int line = 1;
286 struct RClass *c;
287 struct RProc *proc;
288
289 mrb_get_args(mrb, "s|zi", &s, &len, &file, &line);
290 c = mrb_singleton_class_ptr(mrb, self);
291 proc = create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line);
292 MRB_PROC_SET_TARGET_CLASS(proc, c);
293 mrb_assert(!MRB_PROC_CFUNC_P(proc));
294 mrb_vm_ci_target_class_set(mrb->c->ci, c);
295 return exec_irep(mrb, self, proc);
296 }
297 else {
298 mrb_get_args(mrb, "");
299 return mrb_obj_instance_eval(mrb, self);
300 }
301 }
302
303 static mrb_value
304 f_class_eval(mrb_state *mrb, mrb_value self)
305 {
306 if (!mrb_block_given_p(mrb)) {
307 const char *s;
308 mrb_int len;
309 const char *file = NULL;
310 mrb_int line = 1;
311 struct RProc *proc;
312
313 mrb_get_args(mrb, "s|zi", &s, &len, &file, &line);
314 proc = create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line);
315 MRB_PROC_SET_TARGET_CLASS(proc, mrb_class_ptr(self));
316 mrb_assert(!MRB_PROC_CFUNC_P(proc));
317 mrb_vm_ci_target_class_set(mrb->c->ci, mrb_class_ptr(self));
318 return exec_irep(mrb, self, proc);
319 }
320 else {
321 mrb_get_args(mrb, "");
322 return mrb_mod_module_eval(mrb, self);
323 }
324 }
325
326 static mrb_value
327 mrb_binding_eval(mrb_state *mrb, mrb_value binding)
328 {
329 mrb_callinfo *ci = mrb->c->ci;
330 int argc = ci->n;
331 mrb_value *argv = ci->stack + 1;
332
333 if (argc < 15) {
334 argv[0] = mrb_ary_new_from_values(mrb, argc, argv);
335 argv[1] = argv[argc]; /* copy block */
336 ci->n = 15;
337 }
338 mrb_ary_splice(mrb, argv[0], 1, 0, binding); /* insert binding as 2nd argument */
339 return f_eval(mrb, binding);
340 }
341
342 void
343 mrb_mruby_eval_gem_init(mrb_state* mrb)
344 {
345 mrb_define_module_function(mrb, mrb->kernel_module, "eval", f_eval, MRB_ARGS_ARG(1, 3));
346 mrb_define_method_id(mrb, mrb_class_get_id(mrb, MRB_SYM(BasicObject)), MRB_SYM(instance_eval), f_instance_eval, MRB_ARGS_OPT(3)|MRB_ARGS_BLOCK());
347 mrb_define_method_id(mrb, mrb_class_get_id(mrb, MRB_SYM(Module)), MRB_SYM(module_eval), f_class_eval, MRB_ARGS_OPT(3)|MRB_ARGS_BLOCK());
348 mrb_define_method_id(mrb, mrb_class_get_id(mrb, MRB_SYM(Module)), MRB_SYM(class_eval), f_class_eval, MRB_ARGS_OPT(3)|MRB_ARGS_BLOCK());
349
350 struct RClass *binding = mrb_class_get_id(mrb, MRB_SYM(Binding));
351 mrb_define_method(mrb, binding, "eval", mrb_binding_eval, MRB_ARGS_ANY());
352 }
353
354 void
355 mrb_mruby_eval_gem_final(mrb_state* mrb)
356 {
357 }