-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstep3.cpp
More file actions
77 lines (62 loc) · 1.56 KB
/
step3.cpp
File metadata and controls
77 lines (62 loc) · 1.56 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
/*
[step3] call ruby method from C.
*/
#include <mruby.h>
#include <mruby/proc.h>
#include <cstdio>
void _error(const char* s)
{
printf("ERROR: %s\n", s);
exit(1);
}
void plus(mrb_state *mrb, mrb_value a, mrb_value b)
{
auto v = mrb_funcall(mrb, mrb_top_self(mrb), "plus", 2, a, b);
printf("mrb_type(v)=%d\n", mrb_type(v));
mrb_funcall(mrb, mrb_top_self(mrb), "p", 1, v);
}
int main()
{
mrb_value str;
mrb_value z;
mrb_state *mrb = mrb_open();
FILE* f = fopen("step3.rb", "r");
if (f == NULL)
_error("file not found.");
auto n = mrb_load_file(mrb, f);
fclose(f);
mrb_funcall(mrb, mrb_top_self(mrb), "hello3", 0);
str = mrb_str_new_cstr(mrb, "string from c-string");
mrb_funcall(mrb, mrb_top_self(mrb), "puts", 1, str);
mrb_funcall(mrb, mrb_top_self(mrb), "p", 1, str);
z = mrb_funcall(mrb, mrb_top_self(mrb), "tak", 3,
mrb_fixnum_value(24),
mrb_fixnum_value(16),
mrb_fixnum_value( 8)
);
str = mrb_str_new_cstr(mrb, "z=");
mrb_funcall(mrb, mrb_top_self(mrb), "print", 2, str, z);
mrb_funcall(mrb, mrb_top_self(mrb), "puts", 0);
if (mrb_type(z) == MRB_TT_FIXNUM)
{
int int_z = mrb_fixnum(z);
printf("z=%d\n", int_z);
}
plus(mrb, mrb_fixnum_value(1), mrb_fixnum_value(1) );
plus(mrb, mrb_str_new_cstr(mrb, "1"), mrb_str_new_cstr(mrb, "1"));
mrb_close(mrb);
}
/*
% gcc -o step3 step3.c -I ../include -I ../src ../mrblib/mrblib.o ../lib/libmruby.a -lm
% ./step3
welcome to step3.
hello3 called.
string from c-string
"string from c-string"
z=9
z=9
mrb_type(v)=3
2
mrb_type(v)=15
"11"
*/