-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwiki-example.cpp
More file actions
143 lines (110 loc) · 3.71 KB
/
wiki-example.cpp
File metadata and controls
143 lines (110 loc) · 3.71 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "mruby.h"
#include "mruby/irep.h"
#include "mruby/value.h"
#include "mruby/array.h"
#include "mruby/string.h"
#include "mruby/range.h"
#include <iostream>
using namespace std;
// 1. retrieve int arg of type int (see mruby.h)
auto we_connected_with_int(mrb_state* mrb, mrb_value self) -> mrb_value
{
// we need an int from the args
mrb_int i = 0;
mrb_get_args(mrb, "i", &i);
return mrb_bool_value(i >= 2);
}
auto we_connected_with_ruby_str(mrb_state* mrb, mrb_value self) -> mrb_value
{
// we need an int from the args
mrb_value result;
mrb_get_args(mrb, "S", &result);
cout << "result_with_ruby_str: \n";
cout << RSTRING_PTR(result);
cout << endl;
return mrb_bool_value(true);
}
auto we_connected_with_c_str(mrb_state* mrb, mrb_value self) -> mrb_value
{
char* result;
mrb_get_args(mrb, "z", &result);
cout << "result_with_c_str: \n";
cout << string(result);
cout << endl;
return mrb_bool_value(true);
}
auto we_connected_with_c_str_len(mrb_state* mrb, mrb_value self) -> mrb_value
{
char* result;
mrb_int len;
mrb_get_args(mrb, "s", &result, &len);
cout << "result_with_c_str_len: \n";
cout << "len: " << len << endl;
cout << string(result, len);
cout << endl;
return mrb_bool_value(true);
}
// NOTE:
// 1. 이 예제에서는 내가 element의 타입이 스트링이라는 것을 알고 있다는 가정이 있다.
auto we_connected_with_ruby_str_arr(mrb_state* mrb, mrb_value self) -> mrb_value
{
mrb_value value;
mrb_int len;
mrb_get_args(mrb, "A!", &value);
cout << "result_with_ruby_str_array: \n";
for (int i=0; i< RARRAY_LEN(value); i++)
{
auto element = RARRAY_PTR(value)[i];
if (!mrb_nil_p(element))
cout << i << " th: " << RSTRING_PTR(element)<< endl;
}
return mrb_bool_value(true);
}
auto load_file(mrb_state* mrb, char const* path) -> bool
{
auto fp = fopen(path,"r");
if (!fp)
return false;
auto obj = mrb_load_file(mrb, fp);
fclose(fp);
return mrb_fixnum(obj) != 0;
}
int main()
{
auto mrb = mrb_open();
if (!mrb) { /* handle error */ }
if (!load_file(mrb, "wiki-example.rb"))
return 1;
// First access the module
auto mod = mrb_module_get(mrb, "WikiExample");
// Get the cls that is defined in the WikiExample module
auto cls = mrb_class_get_under(mrb, mod, "WikiManager");
// Create a new instance of WikiManager, no arguments are needed (0, NULL)
auto c0 = mrb_obj_new(mrb, cls, 0, NULL);
// Call the get_version method on the instance.
auto r0 = mrb_funcall(mrb, c0, "get_version", 0);
// Convert the result (a fixed number wrapped in a mrb_value)
printf("result: %lli\n", mrb_fixnum(r0));
// 2
// add the method to the WikiManager class
mrb_define_method(mrb, cls, "_connected_with_int", we_connected_with_int, MRB_ARGS_REQ(1));
mrb_define_method(mrb, cls, "_connected_with_ruby_str", we_connected_with_ruby_str, MRB_ARGS_REQ(1));
mrb_define_method(mrb, cls, "_connected_with_c_str", we_connected_with_c_str, MRB_ARGS_REQ(1));
mrb_define_method(mrb, cls, "_connected_with_c_str_len", we_connected_with_c_str_len, MRB_ARGS_REQ(1));
mrb_define_method(mrb, cls, "_connected_with_ruby_str_arr", we_connected_with_ruby_str_arr, MRB_ARGS_REQ(1));
// call the connect method on WikiManager
auto r1 = mrb_funcall(mrb, c0, "connect", 0);
cout << "connect result: " << mrb_bool(r1)
<< endl;
// call the connect method on WikiManager
auto c1 = mrb_obj_new(mrb, cls, 0, NULL);
auto r2 = mrb_funcall(mrb, c0, "execute", 1, c1);
cout << "execute result: " << mrb_fixnum(r2)
<< endl;
// If crashed, provide exception info
if (mrb->exc)
mrb_print_error(mrb);
// Close the Ruby environment
mrb_close(mrb);
return 0;
}