Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-bin-debugger/tools/mrdb/apiprint.c

raw
1 /*
2 ** apiprint.c
3 **
4 */
5
6 #include <string.h>
7 #include "mrdb.h"
8 #include <mruby/value.h>
9 #include <mruby/class.h>
10 #include <mruby/compile.h>
11 #include <mruby/error.h>
12 #include <mruby/numeric.h>
13 #include <mruby/string.h>
14 #include <mruby/presym.h>
15 #include "apiprint.h"
16
17 static void
18 mrdb_check_syntax(mrb_state *mrb, mrb_debug_context *dbg, const char *expr, size_t len)
19 {
20 mrbc_context *c;
21
22 c = mrbc_context_new(mrb);
23 c->no_exec = TRUE;
24 c->capture_errors = TRUE;
25 mrbc_filename(mrb, c, (const char*)dbg->prvfile);
26 c->lineno = dbg->prvline;
27
28 /* Load program */
29 mrb_load_nstring_cxt(mrb, expr, len, c);
30
31 mrbc_context_free(mrb, c);
32 }
33
34 mrb_value
35 mrb_debug_eval(mrb_state *mrb, mrb_debug_context *dbg, const char *expr, size_t len, mrb_bool *exc, int direct_eval)
36 {
37 void (*tmp)(struct mrb_state*, const struct mrb_irep*, const mrb_code*, mrb_value*);
38 mrb_value ruby_code;
39 mrb_value s;
40 mrb_value v;
41 mrb_value recv;
42
43 /* disable code_fetch_hook */
44 tmp = mrb->code_fetch_hook;
45 mrb->code_fetch_hook = NULL;
46
47 mrdb_check_syntax(mrb, dbg, expr, len);
48 if (mrb->exc) {
49 v = mrb_obj_value(mrb->exc);
50 mrb->exc = 0;
51 }
52 else if (direct_eval) {
53 recv = dbg->regs[0];
54
55 v = mrb_funcall(mrb, recv, expr, 0);
56 }
57 else {
58 /*
59 * begin
60 * expr
61 * rescue => e
62 * e
63 * end
64 */
65 ruby_code = mrb_str_new_lit(mrb, "begin\n");
66 ruby_code = mrb_str_cat(mrb, ruby_code, expr, len);
67 ruby_code = mrb_str_cat_lit(mrb, ruby_code, "\nrescue => e\ne\nend");
68
69 recv = dbg->regs[0];
70
71 v = mrb_funcall_argv(mrb, recv, MRB_SYM(instance_eval), 1, &ruby_code);
72 }
73
74 if (exc) {
75 *exc = mrb_obj_is_kind_of(mrb, v, E_EXCEPTION);
76 }
77
78 s = mrb_inspect(mrb, v);
79
80 /* enable code_fetch_hook */
81 mrb->code_fetch_hook = tmp;
82
83 return s;
84 }