-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmrubyvm.hpp
44 lines (35 loc) · 923 Bytes
/
mrubyvm.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#ifndef __MRUBYVM_HPP__
#define __MRUBYVM_HPP__
class VM : public Module
{
public:
VM() : Module(std::shared_ptr<mrb_state>(mrb_open(), mrb_close))
{
}
VM(mrb_state* mrb) : Module(std::shared_ptr<mrb_state>(mrb, [](mrb_state*) {}))
{
}
~VM()
{
}
void run(const std::string& code)
{
std::stringstream ss;
ss << "begin;";
ss << code << ";";
ss << "rescue => e;";
ss << "p e;";
ss << "end;";
mrb_load_string(mrb.get(), ss.str().c_str());
}
void run_file(const std::string& file)
{
std::shared_ptr<mrbc_context> cxt(mrbc_context_new(mrb.get()), [=](mrbc_context* p) {mrbc_context_free(mrb.get(), p);});
mrbc_filename(mrb.get(), cxt.get(), file.c_str());
std::ifstream t(file);
std::string source((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
mrb_load_nstring_cxt(mrb.get(), source.c_str(), source.length(), cxt.get());
}
};
#endif // __MRUBYVM_HPP__