42 lines
925 B
C++
42 lines
925 B
C++
#include "pch.h"
|
|
#include "utils.h"
|
|
|
|
namespace Ruby {
|
|
inline mrb_state *mrb = mrb_open();
|
|
struct Block {
|
|
mrb_value proc;
|
|
|
|
Block(mrb_value proc) : proc(proc) {
|
|
mrb_gc_register(mrb, proc);
|
|
}
|
|
|
|
Block() : proc(mrb_nil_value()) {}
|
|
|
|
void set_proc(mrb_value new_proc) {
|
|
if (!mrb_nil_p(proc))
|
|
mrb_gc_unregister(mrb, proc);
|
|
proc = new_proc;
|
|
if (!mrb_nil_p(proc))
|
|
mrb_gc_register(mrb, proc);
|
|
}
|
|
|
|
Block(const Block &) = delete;
|
|
Block &operator=(const Block &) = delete;
|
|
|
|
mrb_value call(int argc = 0, mrb_value *argv = nullptr) const {
|
|
if (mrb_nil_p(proc))
|
|
return mrb_nil_value();
|
|
return mrb_funcall(mrb, proc, "call", argc, argv);
|
|
}
|
|
|
|
~Block() {
|
|
mrb_gc_unregister(mrb, proc);
|
|
}
|
|
};
|
|
|
|
inline void run_string(const char *code, size_t length) {
|
|
int ai = mrb_gc_arena_save(mrb);
|
|
mrb_load_nstring(mrb, code, length);
|
|
mrb_gc_arena_restore(mrb, ai);
|
|
}
|
|
}; // namespace Ruby
|