rucy 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/.doc/ext/rucy/class.cpp +244 -0
  2. data/.doc/ext/rucy/exception.cpp +99 -0
  3. data/.doc/ext/rucy/function.cpp +63 -0
  4. data/.doc/ext/rucy/struct.cpp +80 -0
  5. data/.doc/ext/rucy/tester.cpp +41 -184
  6. data/.doc/ext/rucy/value.cpp +42 -0
  7. data/.gitignore +22 -0
  8. data/Rakefile +4 -29
  9. data/VERSION +1 -1
  10. data/ext/rucy/class.cpp +262 -0
  11. data/ext/rucy/class.h +96 -0
  12. data/ext/rucy/exception.cpp +107 -0
  13. data/ext/rucy/extconf.rb +9 -8
  14. data/ext/rucy/function.cpp +68 -0
  15. data/ext/rucy/struct.cpp +83 -0
  16. data/ext/rucy/tester.cpp +41 -197
  17. data/ext/rucy/tester.h +10 -0
  18. data/ext/rucy/value.cpp +46 -0
  19. data/include/rucy/defs.h.erb +17 -0
  20. data/include/rucy/exception.h +2 -0
  21. data/include/rucy/extension.h +206 -0
  22. data/include/rucy/rucy.h +25 -2
  23. data/include/rucy/symbol.h +11 -0
  24. data/include/rucy/value.h.erb +66 -14
  25. data/include/rucy.h +1 -1
  26. data/lib/rucy/module.rb +9 -2
  27. data/rucy.gemspec +16 -40
  28. data/src/exception.cpp +17 -25
  29. data/src/rucy.cpp +2 -2
  30. data/src/symbol.cpp +24 -0
  31. data/src/value.cpp.erb +161 -25
  32. data/task/doc.rake +50 -0
  33. data/test/helpers.rb +7 -0
  34. data/test/test_class.rb +161 -0
  35. data/test/test_exception.rb +39 -0
  36. data/test/test_function.rb +31 -0
  37. data/test/test_struct.rb +30 -0
  38. data/test/test_value.rb +18 -0
  39. metadata +123 -74
  40. data/include/rucy/defs.h +0 -101
  41. data/include/rucy/function.h +0 -245
  42. data/include/rucy/gc.h +0 -59
  43. data/include/rucy/module.h +0 -98
  44. data/include/rucy/value.h +0 -291
  45. data/src/function.cpp +0 -158
  46. data/src/gc.cpp +0 -63
  47. data/src/module.cpp +0 -325
  48. data/src/value.cpp +0 -511
  49. data/task/ext.rake +0 -96
  50. data/test/test_rucy.rb +0 -73
@@ -0,0 +1,244 @@
1
+ #include "class.h"
2
+
3
+
4
+ #include <rucy.h>
5
+
6
+
7
+ using namespace Rucy;
8
+
9
+
10
+ static Class cBase, cSub, cRubyObj;
11
+
12
+
13
+ RUCY_WRAPPER_VALUE_FROM_TO(Base, cBase)
14
+ RUCY_WRAPPER_VALUE_FROM_TO(Sub, cSub)
15
+ RUCY_WRAPPER_VALUE_FROM_TO(RubyObj, cRubyObj)
16
+
17
+
18
+ template <typename T> Class get_class ();
19
+
20
+ template <> Class get_class<Base> () {return cBase;}
21
+
22
+ template <> Class get_class<Sub> () {return cSub;}
23
+
24
+
25
+ template <typename T>
26
+ class RubyBase : public ClassWrapper<T>
27
+ {
28
+
29
+ typedef ClassWrapper<T> Super;
30
+
31
+ RUCY_OVERRIDE_ID_START(name_overridable_faster)
32
+ RUCY_OVERRIDE_ID_LAST
33
+
34
+ public:
35
+
36
+ virtual const char* name_overridable () const
37
+ {
38
+ RUCY_OVERRIDABLE_METHOD(name_overridable, get_class<T>(), .c_str());
39
+ }
40
+
41
+ virtual const char* name_overridable_faster () const
42
+ {
43
+ RUCY_OVERRIDABLE_METHOD_FAST(name_overridable_faster, get_class<T>(), .c_str());
44
+ }
45
+
46
+ bool is_name_overridable_faster_overridden () const
47
+ {
48
+ SYM(name_overridable_faster);
49
+ return RUCY_IS_OVERRIDDEN(name_overridable_faster, get_class<T>());
50
+ }
51
+
52
+ };// RubyBase
53
+
54
+
55
+ #define THIS(type) to<type*>(self)
56
+
57
+ #define CHECK(type) RUCY_CHECK_OBJ(self, type, c##type)
58
+
59
+ #define CALL(type, obj, fun) RUCY_WRAPPER_CALL(ClassWrapper<type>, obj, fun)
60
+
61
+
62
+ /*
63
+ alloc function.
64
+ */
65
+ static
66
+ VALUE base_alloc(VALUE klass)
67
+ {
68
+ return value(new RubyBase<Base>, klass);
69
+ }
70
+
71
+ static
72
+ VALUE name(VALUE self)
73
+ {
74
+ CHECK(Base);
75
+ return value(THIS(Base)->name());
76
+ }
77
+
78
+ static
79
+ VALUE call_name(VALUE self)
80
+ {
81
+ CHECK(Base);
82
+ return value(THIS(Base)->name());
83
+ }
84
+
85
+ static
86
+ VALUE base_name_overridable(VALUE self)
87
+ {
88
+ CHECK(Base);
89
+ return value(CALL(Base, THIS(Base), name_overridable()));
90
+ }
91
+
92
+ static
93
+ VALUE call_name_overridable(VALUE self)
94
+ {
95
+ CHECK(Base);
96
+ return value(THIS(Base)->name_overridable());
97
+ }
98
+
99
+ static
100
+ VALUE base_name_overridable_faster(VALUE self)
101
+ {
102
+ CHECK(Base);
103
+ return value(CALL(Base, THIS(Base), name_overridable_faster()));
104
+ }
105
+
106
+ static
107
+ VALUE call_name_overridable_faster(VALUE self)
108
+ {
109
+ CHECK(Base);
110
+ return value(THIS(Base)->name_overridable_faster());
111
+ }
112
+
113
+ template <typename T>
114
+ static
115
+ VALUE is_name_overridable_faster_overridden(VALUE self)
116
+ {
117
+ RUCY_CHECK_OBJ(self, T, get_class<T>());
118
+ RubyBase<T>* obj = dynamic_cast<RubyBase<T>*>(THIS(T));
119
+ if (!obj) invalid_object_error("dynamic_cast() failed.");
120
+ return value(obj->is_name_overridable_faster_overridden());
121
+ }
122
+
123
+ template <typename T>
124
+ static
125
+ VALUE clear_override_flags(VALUE self)
126
+ {
127
+ RUCY_CHECK_OBJ(self, T, get_class<T>());
128
+ ClassWrapper<T>* obj = dynamic_cast<ClassWrapper<T>*>(THIS(T));
129
+ if (obj) obj->clear_override_flags();
130
+ }
131
+
132
+ template <bool singleton>
133
+ static
134
+ VALUE method_added_or_removed(VALUE self, VALUE method_name)
135
+ {
136
+ SYMBOL(klass, "class");
137
+ SYM(name);
138
+ eval(
139
+ Xot::stringf(
140
+ "ObjectSpace.each_object(%s) {|o| o.clear_override_flags}",
141
+ (singleton ? self(klass) : self)(name).c_str()).c_str());
142
+ }
143
+
144
+ static
145
+ VALUE base_new_raw(VALUE self)
146
+ {
147
+ return value(new Base);
148
+ }
149
+
150
+
151
+ /*
152
+ alloc function.
153
+ */
154
+ static
155
+ VALUE sub_alloc(VALUE klass)
156
+ {
157
+ return value(new RubyBase<Sub>, klass);
158
+ }
159
+
160
+ static
161
+ VALUE sub_name_overridable(VALUE self)
162
+ {
163
+ CHECK(Sub);
164
+ return value(CALL(Sub, THIS(Sub), name_overridable()));
165
+ }
166
+
167
+ static
168
+ VALUE sub_name_overridable_faster(VALUE self)
169
+ {
170
+ CHECK(Sub);
171
+ return value(CALL(Sub, THIS(Sub), name_overridable_faster()));
172
+ }
173
+
174
+ static
175
+ VALUE sub_new_raw(VALUE self)
176
+ {
177
+ return value(new Sub);
178
+ }
179
+
180
+
181
+ /*
182
+ alloc function.
183
+ */
184
+ static
185
+ VALUE rubyobj_alloc(VALUE klass)
186
+ {
187
+ return value(new ClassWrapper<RubyObj>, klass);
188
+ }
189
+
190
+ static Xot::Ref<RubyObj> rubyobj_ref;
191
+
192
+ static
193
+ VALUE rubyobj_set_ref(VALUE self, VALUE obj)
194
+ {
195
+ rubyobj_ref = to<RubyObj*>(obj);
196
+ return obj;
197
+ }
198
+
199
+ static
200
+ VALUE rubyobj_clear_ref(VALUE self)
201
+ {
202
+ rubyobj_ref.reset();
203
+ }
204
+
205
+
206
+ void
207
+ Init_class ()
208
+ {
209
+ Module mRucy = rb_define_module("Rucy");
210
+ Module mTester = rb_define_module_under(mRucy, "Tester");
211
+
212
+ cBase = rb_define_class_under(mTester, "Base", rb_cObject);
213
+ rb_define_alloc_func(cBase, base_alloc);
214
+ rb_define_method(cBase, "name", RUBY_METHOD_FUNC(name), 0);
215
+ rb_define_method(cBase, "name_overridable", RUBY_METHOD_FUNC(base_name_overridable), 0);
216
+ rb_define_method(cBase, "name_overridable_faster", RUBY_METHOD_FUNC(base_name_overridable_faster), 0);
217
+ rb_define_method(cBase, "call_name", RUBY_METHOD_FUNC(call_name), 0);
218
+ rb_define_method(cBase, "call_name_overridable", RUBY_METHOD_FUNC(call_name_overridable), 0);
219
+ rb_define_method(cBase, "call_name_overridable_faster", RUBY_METHOD_FUNC(call_name_overridable_faster), 0);
220
+ cBase.define_method(
221
+ "is_name_overridable_faster_overridden",
222
+ is_name_overridable_faster_overridden<Base>);
223
+ cBase.define_method("clear_override_flags", clear_override_flags<Base>);
224
+ cBase.define_method("singleton_method_added", method_added_or_removed<true>);
225
+ cBase.define_method("singleton_method_removed", method_added_or_removed<true>);
226
+ cBase.define_singleton_method("method_added", method_added_or_removed<false>);
227
+ cBase.define_singleton_method("method_removed", method_added_or_removed<false>);
228
+ rb_define_singleton_method(cBase, "new_raw", RUBY_METHOD_FUNC(base_new_raw), 0);
229
+
230
+ cSub = rb_define_class_under(mTester, "Sub", cBase);
231
+ rb_define_alloc_func(cSub, sub_alloc);
232
+ rb_define_method(cSub, "name_overridable", RUBY_METHOD_FUNC(sub_name_overridable), 0);
233
+ rb_define_method(cSub, "name_overridable_faster", RUBY_METHOD_FUNC(sub_name_overridable_faster), 0);
234
+ cSub.define_method(
235
+ "is_name_overridable_faster_overridden",
236
+ is_name_overridable_faster_overridden<Sub>);
237
+ cSub.define_method("clear_override_flags", clear_override_flags<Sub>);
238
+ rb_define_singleton_method(cSub, "new_raw", RUBY_METHOD_FUNC(sub_new_raw), 0);
239
+
240
+ cRubyObj = rb_define_class_under(mTester, "RubyObj", rb_cObject);
241
+ rb_define_alloc_func(cRubyObj, rubyobj_alloc);
242
+ rb_define_function(cRubyObj, "set_ref", RUBY_METHOD_FUNC(rubyobj_set_ref), 1);
243
+ rb_define_function(cRubyObj, "clear_ref", RUBY_METHOD_FUNC(rubyobj_clear_ref), 0);
244
+ }
@@ -0,0 +1,99 @@
1
+ #include <rucy.h>
2
+
3
+
4
+ using namespace Rucy;
5
+
6
+
7
+ /*
8
+ raise ruby's exception.
9
+ */
10
+ static
11
+ VALUE raise_ruby_exception(VALUE self)
12
+ {
13
+ throw RubyException(rb_eStandardError, "raise_ruby_exception");
14
+ }
15
+
16
+ /*
17
+ raise in eval.
18
+ */
19
+ static
20
+ VALUE raise_in_eval(VALUE self)
21
+ {
22
+ eval("raise 'raise_in_eval'");
23
+ }
24
+
25
+ /*
26
+ throw nothing.
27
+ */
28
+ static
29
+ VALUE throw_nothing(VALUE self)
30
+ {
31
+ throw;
32
+ }
33
+
34
+ /*
35
+ throw std::exception.
36
+ */
37
+ static
38
+ VALUE throw_std_exception(VALUE self)
39
+ {
40
+ throw std::exception();
41
+ }
42
+
43
+ /*
44
+ throw std::runtime_error.
45
+ */
46
+ static
47
+ VALUE throw_std_runtime_error(VALUE self)
48
+ {
49
+ throw std::runtime_error("std::runtime_error");
50
+ }
51
+
52
+ struct MyException : public std::runtime_error
53
+ {
54
+ MyException() : runtime_error("") {}
55
+ };
56
+
57
+ /*
58
+ throw custom exception class.
59
+ */
60
+ static
61
+ VALUE throw_custom_exception(VALUE self)
62
+ {
63
+ throw MyException();
64
+ }
65
+
66
+ /*
67
+ throw std::string.
68
+ */
69
+ static
70
+ VALUE throw_std_string(VALUE self)
71
+ {
72
+ throw std::string("std::string");
73
+ }
74
+
75
+ /*
76
+ throw char*.
77
+ */
78
+ static
79
+ VALUE throw_cstring(VALUE self)
80
+ {
81
+ throw "cstring";
82
+ }
83
+
84
+
85
+ void
86
+ Init_exception ()
87
+ {
88
+ Module mRucy = rb_define_module("Rucy");
89
+ Module mTester = rb_define_module_under(mRucy, "Tester");
90
+
91
+ rb_define_method(mTester, "raise_ruby_exception", RUBY_METHOD_FUNC(raise_ruby_exception), 0);
92
+ rb_define_method(mTester, "raise_in_eval", RUBY_METHOD_FUNC(raise_in_eval), 0);
93
+ rb_define_method(mTester, "throw_nothing", RUBY_METHOD_FUNC(throw_nothing), 0);
94
+ rb_define_method(mTester, "throw_std_exception", RUBY_METHOD_FUNC(throw_std_exception), 0);
95
+ rb_define_method(mTester, "throw_std_runtime_error", RUBY_METHOD_FUNC(throw_std_runtime_error), 0);
96
+ rb_define_method(mTester, "throw_custom_exception", RUBY_METHOD_FUNC(throw_custom_exception), 0);
97
+ rb_define_method(mTester, "throw_std_string", RUBY_METHOD_FUNC(throw_std_string), 0);
98
+ rb_define_method(mTester, "throw_cstring", RUBY_METHOD_FUNC(throw_cstring), 0);
99
+ }
@@ -0,0 +1,63 @@
1
+ #include <rucy.h>
2
+
3
+
4
+ using namespace Rucy;
5
+
6
+
7
+ /*
8
+ do nothing.
9
+ */
10
+ static
11
+ VALUE do_nothing(VALUE self)
12
+ {
13
+ }
14
+
15
+ /*
16
+ return nil.
17
+ */
18
+ static
19
+ VALUE return_nil(VALUE self)
20
+ {
21
+ return Qnil;
22
+ }
23
+
24
+ /*
25
+ return int.
26
+ */
27
+ static
28
+ VALUE return_int(VALUE self)
29
+ {
30
+ return value(1);
31
+ }
32
+
33
+ /*
34
+ return flaot.
35
+ */
36
+ static
37
+ VALUE return_float(VALUE self)
38
+ {
39
+ return value(1.0f);
40
+ }
41
+
42
+ /*
43
+ return string.
44
+ */
45
+ static
46
+ VALUE return_string(VALUE self)
47
+ {
48
+ return value("");
49
+ }
50
+
51
+
52
+ void
53
+ Init_function ()
54
+ {
55
+ Module mRucy = rb_define_module("Rucy");
56
+ Module mTester = rb_define_module_under(mRucy, "Tester");
57
+
58
+ rb_define_method(mTester, "do_nothing", RUBY_METHOD_FUNC(do_nothing), -1);
59
+ rb_define_method(mTester, "return_nil", RUBY_METHOD_FUNC(return_nil), 0);
60
+ rb_define_method(mTester, "return_int", RUBY_METHOD_FUNC(return_int), 0);
61
+ rb_define_method(mTester, "return_float", RUBY_METHOD_FUNC(return_float), 0);
62
+ rb_define_method(mTester, "return_string", RUBY_METHOD_FUNC(return_string), 0);
63
+ }
@@ -0,0 +1,80 @@
1
+ #include <rucy.h>
2
+
3
+
4
+ using namespace Rucy;
5
+
6
+
7
+ struct Struct
8
+ {
9
+
10
+ int num;
11
+
12
+ Struct () : num(0) {}
13
+
14
+ };// Struct
15
+
16
+
17
+ static Class cStruct;
18
+
19
+
20
+ namespace Rucy
21
+ {
22
+
23
+
24
+ static Value
25
+ value (const Struct& obj)
26
+ {
27
+ return new_type(cStruct, new Struct(obj));
28
+ }
29
+
30
+ template <> inline Struct*
31
+ value_to<Struct*> (Value val, bool)
32
+ {
33
+ return get_type_ptr<Struct>(val, cStruct);
34
+ }
35
+
36
+
37
+ }// Rucy
38
+
39
+
40
+ /*
41
+ alloc function.
42
+ */
43
+ static
44
+ VALUE alloc(VALUE klass)
45
+ {
46
+ return new_type<Struct>(klass);
47
+ }
48
+
49
+ /*
50
+ get num.
51
+ */
52
+ static
53
+ VALUE get_num(VALUE self)
54
+ {
55
+ Struct* obj = to<Struct*>(self);
56
+ if (obj) return value(obj->num);
57
+ }
58
+
59
+ /*
60
+ set num.
61
+ */
62
+ static
63
+ VALUE set_num(VALUE self, VALUE num)
64
+ {
65
+ Struct* obj = to<Struct*>(self);
66
+ if (obj) obj->num = to<int>(num);
67
+ }
68
+
69
+
70
+ void
71
+ Init_struct ()
72
+ {
73
+ Module mRucy = rb_define_module("Rucy");
74
+ Module mTester = rb_define_module_under(mRucy, "Tester");
75
+
76
+ cStruct = rb_define_class_under(mTester, "Struct", rb_cObject);
77
+ rb_define_alloc_func(cStruct, alloc);
78
+ rb_define_method(cStruct, "num", RUBY_METHOD_FUNC(get_num), 0);
79
+ rb_define_method(cStruct, "num=", RUBY_METHOD_FUNC(set_num), 1);
80
+ }