Skip to content

Commit

Permalink
Merge pull request #78 from danielwaterworth/eval-in-new-thread
Browse files Browse the repository at this point in the history
Evaluate code in a separate thread
  • Loading branch information
SamSaffron committed May 19, 2020
2 parents 4a1414c + caec2bd commit 68fa8a4
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions ext/rbtrace.c
Expand Up @@ -862,13 +862,11 @@ msgq_setup()
#endif
}

static VALUE rbtrace_module;

static VALUE
eval_inspect(VALUE rb_code) {
VALUE binding, result;

binding = rb_eval_string("$rbtrace_binding ||= binding");
result = rb_funcall(binding, rb_intern("eval"), 1, rb_code);
return rb_funcall(result, rb_intern("inspect"), 0);
return rb_funcall(rbtrace_module, rb_intern("eval_and_inspect"), 1, rb_code);
}

static VALUE
Expand Down Expand Up @@ -1028,7 +1026,7 @@ rbtrace__process_event(msgpack_object cmd)
strncpy(query, str.ptr, str.size);
query[str.size] = 0;

VALUE rb_code, binding;
VALUE rb_code;
rb_code = rb_str_new2(query);

val = rb_rescue(eval_inspect, rb_code, rescue_inspect, Qnil);
Expand Down Expand Up @@ -1134,11 +1132,25 @@ send_write(VALUE klass, VALUE val) {
void
Init_rbtrace()
{
VALUE mod = rb_define_module("RBTrace");
VALUE output = rb_define_module_under(mod, "OUT");
rbtrace_module = rb_define_module("RBTrace");
VALUE output = rb_define_module_under(rbtrace_module, "OUT");

rb_define_singleton_method(output, "write", send_write, 1);

rb_eval_string(
"module RBTrace\n"
" def self.eval_context\n"
" @eval_context ||= binding\n"
" end\n"

" def self.eval_and_inspect(code)\n"
" t = Thread.new { Thread.current[:output] = eval_context.eval(code).inspect }\n"
" t.join\n"
" t[:output]\n"
" end\n"
"end\n"
);

// hook into the gc
gc_hook = Data_Wrap_Struct(rb_cObject, rbtrace_gc_mark, NULL, NULL);
rb_global_variable(&gc_hook);
Expand Down

0 comments on commit 68fa8a4

Please sign in to comment.