-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhello.cpp
More file actions
105 lines (86 loc) · 2.25 KB
/
hello.cpp
File metadata and controls
105 lines (86 loc) · 2.25 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdlib.h>
#include <stdio.h>
/* Include the mruby header */
#include <mruby.h>
#include <mruby/proc.h>
#include <mruby/class.h>
#include <mruby/data.h>
#include <mruby/compile.h>
#include <mruby/string.h>
static void test_free(mrb_state* mrb, void* p);
struct RClass* TestClass;
static const struct mrb_data_type test_type = {
"TestClass", test_free
};
static void test_free(mrb_state* mrb, void* p)
{
auto value = (int*)p;
free(value);
printf("test_free is called\n");
}
mrb_value test_init(mrb_state* mrb, mrb_value exec)
{
printf("Test.initialize is called\n");
return mrb_nil_value();
}
mrb_value test_run(mrb_state* mrb, mrb_value exec)
{
for (int i=0; i<10; i++)
printf("Test is running: %d\n", i);
return mrb_fixnum_value(10);
}
void init_TestClass(mrb_state* mrb)
{
TestClass = mrb_define_class(mrb, "Test", mrb->object_class);
MRB_SET_INSTANCE_TT(TestClass, MRB_TT_CLASS);
mrb_define_method(mrb, TestClass, "initialize", test_init, MRB_ARGS_NONE());
mrb_define_method(mrb, TestClass, "run", test_run, MRB_ARGS_NONE()); }
static void
p(mrb_state *mrb, mrb_value obj, int prompt)
{
mrb_value val;
val = mrb_funcall(mrb, obj, "inspect", 0);
if (prompt) {
if (!mrb->exc) {
fputs(" => ", stdout);
}
else {
val = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0);
}
}
if (!mrb_string_p(val)) {
val = mrb_obj_as_string(mrb, obj);
}
fwrite(RSTRING_PTR(val), RSTRING_LEN(val), 1, stdout);
putc('\n', stdout);
}
int main()
{
mrb_state *mrb = mrb_open();
init_TestClass(mrb);
char code[] = "$t = Test.new; res = $t.run; p res";
printf("Executing Ruby code from C!\n");
auto c = mrbc_context_new(mrb);
auto p = mrb_parse_string(mrb, code, c);
auto n = mrb_generate_code(mrb, p);
// mrb_run(mrb, n, mrb_top_self(mrb));
unsigned stack_keep = 0;
auto result = mrb_vm_run(mrb,
n,
mrb_top_self(mrb),
stack_keep);
if (mrb->exc) // have exception
{
mrb_p(mrb, mrb_obj_value(mrb->exc));
mrb->exc = 0;
}
else
{
if (!mrb_respond_to(mrb, result, mrb_intern_lit(mrb, "inspect")))
result = mrb_any_to_s(mrb, result);
mrb_p(mrb, result);
}
mrbc_context_free(mrb, c);
mrb_close(mrb);
return 0;
}