rucy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/src/exception.cpp ADDED
@@ -0,0 +1,124 @@
1
+ // -*- c++ -*-
2
+ #include "rucy/exception.h"
3
+
4
+
5
+ #include <ruby.h>
6
+ #include <rucy/rucy.h>
7
+ #include <rucy/string.h>
8
+
9
+
10
+ #define VA_STRING(format, result) \
11
+ String result; \
12
+ do \
13
+ { \
14
+ if (format) \
15
+ { \
16
+ va_list args; \
17
+ va_start(args, format); \
18
+ result = stringf(format, args); \
19
+ va_end(args); \
20
+ } \
21
+ } \
22
+ while (false)
23
+
24
+
25
+ namespace Rucy
26
+ {
27
+
28
+
29
+ Class
30
+ native_error_class ()
31
+ {
32
+ static Class c =
33
+ rucy_module().define_class("NativeError", rb_eRuntimeError);
34
+ return c;
35
+ }
36
+
37
+
38
+ void
39
+ error (const char* format, ...)
40
+ {
41
+ VA_STRING(format, message);
42
+ throw message;
43
+ }
44
+
45
+ void
46
+ raise (Value type, const char* format, ...)
47
+ {
48
+ VA_STRING(format, message);
49
+ throw RubyException(type, message.c_str());
50
+ }
51
+
52
+
53
+ void
54
+ type_error (const char* format, ...)
55
+ {
56
+ VA_STRING(format, message);
57
+ raise(rb_eTypeError, message.c_str());
58
+ }
59
+
60
+ void
61
+ argument_error (const char* format, ...)
62
+ {
63
+ VA_STRING(format, message);
64
+ raise(rb_eArgError, message.c_str());
65
+ }
66
+
67
+ void
68
+ argument_error (
69
+ const char* method, int nargs, int nargs_expected,
70
+ int n1, int n2, int n3, int n4, int n5)
71
+ {
72
+ String message = stringf(
73
+ "wrong number of arguments for %s: %d for %d",
74
+ method, nargs, nargs_expected);
75
+
76
+ int n[5] = {n1, n2, n3, n4, n5};
77
+ for (int i = 0; i < 5 && n[i] >= 0; ++i)
78
+ message += stringf(" or %d", n[i]);
79
+
80
+ message += ".";
81
+ argument_error(message.c_str());
82
+ }
83
+
84
+ void
85
+ not_implemented_error (const char* format, ...)
86
+ {
87
+ VA_STRING(format, message);
88
+ raise(rb_eNotImpError, message.c_str());
89
+ }
90
+
91
+
92
+ RubyException::RubyException (Value exception)
93
+ : Super(""), val(exception)
94
+ {
95
+ }
96
+
97
+ RubyException::RubyException (Value type, const char* format, ...)
98
+ : Super(""), val(Qnil)
99
+ {
100
+ VA_STRING(format, message);
101
+ val = rb_exc_new2(type, message.c_str());
102
+ }
103
+
104
+ const char*
105
+ RubyException::what () const throw()
106
+ {
107
+ SYM(message);
108
+ return value().call(message).c_str();
109
+ }
110
+
111
+ Value
112
+ RubyException::value () const
113
+ {
114
+ return val;
115
+ }
116
+
117
+
118
+ RubyJumptag::RubyJumptag (int tag)
119
+ : tag(tag)
120
+ {
121
+ }
122
+
123
+
124
+ }// Rucy
data/src/function.cpp ADDED
@@ -0,0 +1,157 @@
1
+ // -*- c++ -*-
2
+ #include "rucy/function.h"
3
+
4
+
5
+ #include <rucy/exception.h>
6
+
7
+
8
+ #ifndef TAG_RAISE
9
+ #define TAG_RAISE 0x6
10
+ #endif
11
+
12
+
13
+ namespace Rucy
14
+ {
15
+
16
+ void
17
+ define_function (const char* name, RubyFunctionN fun)
18
+ {
19
+ rb_define_global_function(name, RUBY_METHOD_FUNC(fun), -1);
20
+ }
21
+ void
22
+ define_function (const char* name, RubyFunction0 fun)
23
+ {
24
+ rb_define_global_function(name, RUBY_METHOD_FUNC(fun), 0);
25
+ }
26
+ void
27
+ define_function (const char* name, RubyFunction1 fun)
28
+ {
29
+ rb_define_global_function(name, RUBY_METHOD_FUNC(fun), 1);
30
+ }
31
+ void
32
+ define_function (const char* name, RubyFunction2 fun)
33
+ {
34
+ rb_define_global_function(name, RUBY_METHOD_FUNC(fun), 2);
35
+ }
36
+ void
37
+ define_function (const char* name, RubyFunction3 fun)
38
+ {
39
+ rb_define_global_function(name, RUBY_METHOD_FUNC(fun), 3);
40
+ }
41
+ void
42
+ define_function (const char* name, RubyFunction4 fun)
43
+ {
44
+ rb_define_global_function(name, RUBY_METHOD_FUNC(fun), 4);
45
+ }
46
+ void
47
+ define_function (const char* name, RubyFunction5 fun)
48
+ {
49
+ rb_define_global_function(name, RUBY_METHOD_FUNC(fun), 5);
50
+ }
51
+ void
52
+ define_function (const char* name, RubyFunction6 fun)
53
+ {
54
+ rb_define_global_function(name, RUBY_METHOD_FUNC(fun), 6);
55
+ }
56
+ void
57
+ define_function (const char* name, RubyFunction7 fun)
58
+ {
59
+ rb_define_global_function(name, RUBY_METHOD_FUNC(fun), 7);
60
+ }
61
+ void
62
+ define_function (const char* name, RubyFunction8 fun)
63
+ {
64
+ rb_define_global_function(name, RUBY_METHOD_FUNC(fun), 8);
65
+ }
66
+
67
+ Value
68
+ call (Symbol name, int argc, const Value* argv)
69
+ {
70
+ return protect(rb_funcall2, Qnil, name.id(), argc, (const VALUE*) argv);
71
+ }
72
+ Value
73
+ call (Symbol name)
74
+ {
75
+ const VALUE args[] = {};
76
+ return protect(rb_funcall2, Qnil, name.id(), 0, args);
77
+ }
78
+ Value
79
+ call (Symbol name, Value v1)
80
+ {
81
+ const VALUE args[] = {v1};
82
+ return protect(rb_funcall2, Qnil, name.id(), 1, args);
83
+ }
84
+ Value
85
+ call (Symbol name, Value v1, Value v2)
86
+ {
87
+ const VALUE args[] = {v1, v2};
88
+ return protect(rb_funcall2, Qnil, name.id(), 2, args);
89
+ }
90
+ Value
91
+ call (Symbol name, Value v1, Value v2, Value v3)
92
+ {
93
+ const VALUE args[] = {v1, v2, v3};
94
+ return protect(rb_funcall2, Qnil, name.id(), 3, args);
95
+ }
96
+ Value
97
+ call (Symbol name, Value v1, Value v2, Value v3, Value v4)
98
+ {
99
+ const VALUE args[] = {v1, v2, v3, v4};
100
+ return protect(rb_funcall2, Qnil, name.id(), 4, args);
101
+ }
102
+ Value
103
+ call (Symbol name, Value v1, Value v2, Value v3, Value v4, Value v5)
104
+ {
105
+ const VALUE args[] = {v1, v2, v3, v4, v5};
106
+ return protect(rb_funcall2, Qnil, name.id(), 5, args);
107
+ }
108
+ Value
109
+ call (Symbol name, Value v1, Value v2, Value v3, Value v4, Value v5, Value v6)
110
+ {
111
+ const VALUE args[] = {v1, v2, v3, v4, v5, v6};
112
+ return protect(rb_funcall2, Qnil, name.id(), 6, args);
113
+ }
114
+ Value
115
+ call (Symbol name, Value v1, Value v2, Value v3, Value v4, Value v5, Value v6, Value v7)
116
+ {
117
+ const VALUE args[] = {v1, v2, v3, v4, v5, v6, v7};
118
+ return protect(rb_funcall2, Qnil, name.id(), 7, args);
119
+ }
120
+ Value
121
+ call (Symbol name, Value v1, Value v2, Value v3, Value v4, Value v5, Value v6, Value v7, Value v8)
122
+ {
123
+ const VALUE args[] = {v1, v2, v3, v4, v5, v6, v7, v8};
124
+ return protect(rb_funcall2, Qnil, name.id(), 8, args);
125
+ }
126
+
127
+ Value
128
+ eval (const char* str)
129
+ {
130
+ SYM(eval);
131
+ return call(eval, str);
132
+ }
133
+
134
+
135
+ Value
136
+ call_protect (VALUE (*fun)(VALUE), VALUE arg)
137
+ {
138
+ int state = 0;
139
+ VALUE ret = rb_protect(fun, arg, &state);
140
+
141
+ if (state != 0)
142
+ {
143
+ VALUE exception = rb_errinfo();
144
+ if (state == TAG_RAISE && RTEST(exception))
145
+ {
146
+ rb_set_errinfo(Qnil);
147
+ throw RubyException(exception);
148
+ }
149
+
150
+ throw RubyJumptag(state);
151
+ }
152
+
153
+ return ret;
154
+ }
155
+
156
+
157
+ }// Rucy
@@ -0,0 +1,74 @@
1
+ // -*- c++ -*-
2
+ % require 'support'
3
+ #include "rucy/function.h"
4
+
5
+
6
+ #include <rucy/exception.h>
7
+
8
+
9
+ #ifndef TAG_RAISE
10
+ #define TAG_RAISE 0x6
11
+ #endif
12
+
13
+
14
+ namespace Rucy
15
+ {
16
+
17
+ void
18
+ define_function (const char* name, RubyFunctionN fun)
19
+ {
20
+ rb_define_global_function(name, RUBY_METHOD_FUNC(fun), -1);
21
+ }
22
+ % NTIMES.each do |n|
23
+ void
24
+ define_function (const char* name, RubyFunction<%= n %> fun)
25
+ {
26
+ rb_define_global_function(name, RUBY_METHOD_FUNC(fun), <%= n %>);
27
+ }
28
+ % end
29
+
30
+ Value
31
+ call (Symbol name, int argc, const Value* argv)
32
+ {
33
+ return protect(rb_funcall2, Qnil, name.id(), argc, (const VALUE*) argv);
34
+ }
35
+ % NTIMES.each do |n|
36
+ Value
37
+ call (Symbol name<%= params(n) {|i| ", Value v#{i}"} %>)
38
+ {
39
+ const VALUE args[] = {<%= params(n, ", ") {|i| "v#{i}"} %>};
40
+ return protect(rb_funcall2, Qnil, name.id(), <%= n %>, args);
41
+ }
42
+ % end
43
+
44
+ Value
45
+ eval (const char* str)
46
+ {
47
+ SYM(eval);
48
+ return call(eval, str);
49
+ }
50
+
51
+
52
+ Value
53
+ call_protect (VALUE (*fun)(VALUE), VALUE arg)
54
+ {
55
+ int state = 0;
56
+ VALUE ret = rb_protect(fun, arg, &state);
57
+
58
+ if (state != 0)
59
+ {
60
+ VALUE exception = rb_errinfo();
61
+ if (state == TAG_RAISE && RTEST(exception))
62
+ {
63
+ rb_set_errinfo(Qnil);
64
+ throw RubyException(exception);
65
+ }
66
+
67
+ throw RubyJumptag(state);
68
+ }
69
+
70
+ return ret;
71
+ }
72
+
73
+
74
+ }// Rucy
data/src/gc.cpp ADDED
@@ -0,0 +1,63 @@
1
+ // -*- c++ -*-
2
+ #include "rucy/gc.h"
3
+
4
+
5
+ namespace Rucy
6
+ {
7
+
8
+
9
+ static void
10
+ register_value (VALUE* v)
11
+ {
12
+ if (!v) return;
13
+ rb_gc_register_address(v);
14
+ }
15
+
16
+ static void
17
+ unregister_value (VALUE* v)
18
+ {
19
+ if (!v) return;
20
+ rb_gc_unregister_address(v);
21
+ }
22
+
23
+ GCGuard::GCGuard (VALUE* v)
24
+ : pval(v)
25
+ {
26
+ register_value(pval);
27
+ }
28
+
29
+ GCGuard::GCGuard (Value* v)
30
+ : pval(v ? &v->v : NULL)
31
+ {
32
+ register_value(pval);
33
+ }
34
+
35
+ GCGuard::~GCGuard ()
36
+ {
37
+ unregister_value(pval);
38
+ }
39
+
40
+
41
+ GlobalValue::GlobalValue (Value v)
42
+ : val(v), guard(&val)
43
+ {
44
+ }
45
+
46
+ Value
47
+ GlobalValue::value () const
48
+ {
49
+ return val;
50
+ }
51
+
52
+ GlobalValue::operator VALUE () const
53
+ {
54
+ return value();
55
+ }
56
+
57
+ GlobalValue::operator Value () const
58
+ {
59
+ return value();
60
+ }
61
+
62
+
63
+ }// Rucy