rice 3.0.0 → 4.0.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.
Files changed (237) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +121 -0
  3. data/CONTRIBUTORS.md +19 -0
  4. data/Gemfile +3 -0
  5. data/README.md +44 -1025
  6. data/Rakefile +95 -12
  7. data/include/rice/rice.hpp +7766 -0
  8. data/lib/mkmf-rice.rb +127 -0
  9. data/lib/version.rb +3 -0
  10. data/rice/Address_Registration_Guard.ipp +75 -32
  11. data/rice/Address_Registration_Guard_defn.hpp +60 -56
  12. data/rice/Arg.hpp +80 -4
  13. data/rice/Arg.ipp +51 -0
  14. data/rice/Constructor.hpp +12 -14
  15. data/rice/Data_Object.ipp +234 -107
  16. data/rice/Data_Object_defn.hpp +77 -117
  17. data/rice/Data_Type.hpp +1 -2
  18. data/rice/Data_Type.ipp +251 -295
  19. data/rice/Data_Type_defn.hpp +175 -243
  20. data/rice/Director.hpp +11 -6
  21. data/rice/Enum.hpp +54 -104
  22. data/rice/Enum.ipp +104 -230
  23. data/rice/Exception.hpp +2 -8
  24. data/rice/Exception.ipp +65 -0
  25. data/rice/Exception_defn.hpp +46 -47
  26. data/rice/Identifier.hpp +28 -28
  27. data/rice/Identifier.ipp +23 -27
  28. data/rice/Return.hpp +39 -0
  29. data/rice/Return.ipp +33 -0
  30. data/rice/detail/Exception_Handler.ipp +22 -62
  31. data/rice/detail/Exception_Handler_defn.hpp +76 -91
  32. data/rice/detail/Iterator.hpp +18 -88
  33. data/rice/detail/Iterator.ipp +47 -0
  34. data/rice/detail/Jump_Tag.hpp +21 -0
  35. data/rice/detail/MethodInfo.hpp +44 -0
  36. data/rice/detail/MethodInfo.ipp +78 -0
  37. data/rice/detail/NativeAttribute.hpp +53 -0
  38. data/rice/detail/NativeAttribute.ipp +83 -0
  39. data/rice/detail/NativeFunction.hpp +69 -0
  40. data/rice/detail/NativeFunction.ipp +248 -0
  41. data/rice/detail/RubyFunction.hpp +39 -0
  42. data/rice/detail/RubyFunction.ipp +92 -0
  43. data/rice/detail/Type.hpp +29 -0
  44. data/rice/detail/Type.ipp +138 -0
  45. data/rice/detail/TypeRegistry.hpp +50 -0
  46. data/rice/detail/TypeRegistry.ipp +106 -0
  47. data/rice/detail/Wrapper.hpp +51 -0
  48. data/rice/detail/Wrapper.ipp +151 -0
  49. data/rice/detail/default_allocation_func.hpp +8 -19
  50. data/rice/detail/default_allocation_func.ipp +9 -8
  51. data/rice/detail/from_ruby.hpp +2 -37
  52. data/rice/detail/from_ruby.ipp +1020 -46
  53. data/rice/detail/from_ruby_defn.hpp +38 -0
  54. data/rice/detail/function_traits.hpp +124 -0
  55. data/rice/detail/method_data.hpp +23 -15
  56. data/rice/detail/method_data.ipp +53 -0
  57. data/rice/detail/rice_traits.hpp +116 -0
  58. data/rice/detail/ruby.hpp +9 -46
  59. data/rice/detail/to_ruby.hpp +3 -17
  60. data/rice/detail/to_ruby.ipp +409 -31
  61. data/rice/detail/to_ruby_defn.hpp +48 -0
  62. data/rice/forward_declares.ipp +82 -0
  63. data/rice/global_function.hpp +16 -20
  64. data/rice/global_function.ipp +8 -17
  65. data/rice/rice.hpp +59 -0
  66. data/rice/ruby_mark.hpp +5 -3
  67. data/rice/ruby_try_catch.hpp +4 -4
  68. data/rice/stl.hpp +11 -0
  69. data/sample/callbacks/extconf.rb +3 -0
  70. data/sample/callbacks/sample_callbacks.cpp +10 -13
  71. data/sample/enum/extconf.rb +3 -0
  72. data/sample/enum/sample_enum.cpp +3 -17
  73. data/sample/enum/test.rb +2 -2
  74. data/sample/inheritance/animals.cpp +8 -24
  75. data/sample/inheritance/extconf.rb +3 -0
  76. data/sample/inheritance/test.rb +1 -1
  77. data/sample/map/extconf.rb +3 -0
  78. data/sample/map/map.cpp +10 -18
  79. data/sample/map/test.rb +1 -1
  80. data/test/embed_ruby.cpp +18 -5
  81. data/test/ext/t1/extconf.rb +3 -0
  82. data/test/ext/t1/t1.cpp +1 -3
  83. data/test/ext/t2/extconf.rb +3 -0
  84. data/test/ext/t2/t2.cpp +1 -1
  85. data/test/extconf.rb +23 -0
  86. data/test/ruby/test_callbacks_sample.rb +28 -0
  87. data/test/ruby/test_multiple_extensions.rb +18 -0
  88. data/test/ruby/test_multiple_extensions_same_class.rb +14 -0
  89. data/test/ruby/test_multiple_extensions_with_inheritance.rb +20 -0
  90. data/test/test_Address_Registration_Guard.cpp +23 -10
  91. data/test/test_Array.cpp +129 -73
  92. data/test/test_Attribute.cpp +147 -0
  93. data/test/test_Builtin_Object.cpp +34 -14
  94. data/test/test_Class.cpp +149 -275
  95. data/test/test_Constructor.cpp +10 -9
  96. data/test/test_Data_Object.cpp +133 -192
  97. data/test/test_Data_Type.cpp +322 -252
  98. data/test/test_Director.cpp +54 -41
  99. data/test/test_Enum.cpp +228 -103
  100. data/test/test_Exception.cpp +5 -6
  101. data/test/test_Hash.cpp +31 -30
  102. data/test/test_Identifier.cpp +4 -5
  103. data/test/test_Inheritance.cpp +221 -0
  104. data/test/test_Iterator.cpp +161 -0
  105. data/test/test_Jump_Tag.cpp +1 -1
  106. data/test/test_Keep_Alive.cpp +161 -0
  107. data/test/test_Memory_Management.cpp +2 -4
  108. data/test/test_Module.cpp +167 -110
  109. data/test/test_Object.cpp +41 -21
  110. data/test/test_Ownership.cpp +275 -0
  111. data/test/test_Self.cpp +205 -0
  112. data/test/test_Stl_Optional.cpp +90 -0
  113. data/test/test_Stl_Pair.cpp +144 -0
  114. data/test/test_Stl_SmartPointer.cpp +200 -0
  115. data/test/test_Stl_String.cpp +74 -0
  116. data/test/test_Stl_Vector.cpp +652 -0
  117. data/test/test_String.cpp +1 -2
  118. data/test/test_Struct.cpp +29 -39
  119. data/test/test_Symbol.cpp +1 -2
  120. data/test/test_To_From_Ruby.cpp +249 -285
  121. data/test/test_global_functions.cpp +39 -19
  122. data/test/unittest.hpp +0 -4
  123. metadata +63 -139
  124. data/Doxyfile +0 -2268
  125. data/Makefile.am +0 -26
  126. data/Makefile.in +0 -931
  127. data/README.mingw +0 -8
  128. data/aclocal.m4 +0 -1085
  129. data/ax_cxx_compile_stdcxx.m4 +0 -951
  130. data/bootstrap +0 -8
  131. data/config.guess +0 -1421
  132. data/config.sub +0 -1807
  133. data/configure +0 -7792
  134. data/configure.ac +0 -55
  135. data/depcomp +0 -791
  136. data/doxygen.ac +0 -314
  137. data/doxygen.am +0 -186
  138. data/extconf.rb +0 -70
  139. data/install-sh +0 -501
  140. data/missing +0 -215
  141. data/post-autoconf.rb +0 -22
  142. data/post-automake.rb +0 -28
  143. data/rice/Address_Registration_Guard.cpp +0 -22
  144. data/rice/Arg_impl.hpp +0 -129
  145. data/rice/Arg_operators.cpp +0 -21
  146. data/rice/Arg_operators.hpp +0 -19
  147. data/rice/Array.hpp +0 -214
  148. data/rice/Array.ipp +0 -256
  149. data/rice/Builtin_Object.hpp +0 -8
  150. data/rice/Builtin_Object.ipp +0 -50
  151. data/rice/Builtin_Object_defn.hpp +0 -50
  152. data/rice/Class.cpp +0 -57
  153. data/rice/Class.hpp +0 -8
  154. data/rice/Class.ipp +0 -6
  155. data/rice/Class_defn.hpp +0 -84
  156. data/rice/Data_Type.cpp +0 -54
  157. data/rice/Data_Type_fwd.hpp +0 -12
  158. data/rice/Director.cpp +0 -13
  159. data/rice/Exception.cpp +0 -54
  160. data/rice/Exception_Base.hpp +0 -8
  161. data/rice/Exception_Base.ipp +0 -13
  162. data/rice/Exception_Base_defn.hpp +0 -27
  163. data/rice/Hash.hpp +0 -230
  164. data/rice/Hash.ipp +0 -329
  165. data/rice/Identifier.cpp +0 -8
  166. data/rice/Jump_Tag.hpp +0 -24
  167. data/rice/Makefile.am +0 -121
  168. data/rice/Makefile.in +0 -884
  169. data/rice/Module.cpp +0 -84
  170. data/rice/Module.hpp +0 -8
  171. data/rice/Module.ipp +0 -6
  172. data/rice/Module_defn.hpp +0 -88
  173. data/rice/Module_impl.hpp +0 -281
  174. data/rice/Module_impl.ipp +0 -345
  175. data/rice/Object.cpp +0 -169
  176. data/rice/Object.hpp +0 -8
  177. data/rice/Object.ipp +0 -33
  178. data/rice/Object_defn.hpp +0 -214
  179. data/rice/Require_Guard.hpp +0 -21
  180. data/rice/String.cpp +0 -89
  181. data/rice/String.hpp +0 -91
  182. data/rice/Struct.cpp +0 -117
  183. data/rice/Struct.hpp +0 -162
  184. data/rice/Struct.ipp +0 -26
  185. data/rice/Symbol.cpp +0 -25
  186. data/rice/Symbol.hpp +0 -66
  187. data/rice/Symbol.ipp +0 -44
  188. data/rice/config.hpp +0 -47
  189. data/rice/config.hpp.in +0 -46
  190. data/rice/detail/Arguments.hpp +0 -118
  191. data/rice/detail/Auto_Function_Wrapper.hpp +0 -898
  192. data/rice/detail/Auto_Function_Wrapper.ipp +0 -3181
  193. data/rice/detail/Auto_Member_Function_Wrapper.hpp +0 -897
  194. data/rice/detail/Auto_Member_Function_Wrapper.ipp +0 -2501
  195. data/rice/detail/Caster.hpp +0 -103
  196. data/rice/detail/Not_Copyable.hpp +0 -25
  197. data/rice/detail/Wrapped_Function.hpp +0 -33
  198. data/rice/detail/cfp.hpp +0 -24
  199. data/rice/detail/cfp.ipp +0 -51
  200. data/rice/detail/check_ruby_type.cpp +0 -27
  201. data/rice/detail/check_ruby_type.hpp +0 -23
  202. data/rice/detail/creation_funcs.hpp +0 -37
  203. data/rice/detail/creation_funcs.ipp +0 -36
  204. data/rice/detail/define_method_and_auto_wrap.hpp +0 -31
  205. data/rice/detail/define_method_and_auto_wrap.ipp +0 -30
  206. data/rice/detail/demangle.cpp +0 -56
  207. data/rice/detail/demangle.hpp +0 -19
  208. data/rice/detail/env.hpp +0 -11
  209. data/rice/detail/method_data.cpp +0 -92
  210. data/rice/detail/node.hpp +0 -13
  211. data/rice/detail/protect.cpp +0 -29
  212. data/rice/detail/protect.hpp +0 -34
  213. data/rice/detail/ruby_version_code.hpp +0 -6
  214. data/rice/detail/ruby_version_code.hpp.in +0 -6
  215. data/rice/detail/st.hpp +0 -22
  216. data/rice/detail/win32.hpp +0 -16
  217. data/rice/detail/wrap_function.hpp +0 -66
  218. data/rice/protect.hpp +0 -38
  219. data/rice/protect.ipp +0 -1134
  220. data/rice/rubypp.rb +0 -97
  221. data/rice/to_from_ruby.hpp +0 -8
  222. data/rice/to_from_ruby.ipp +0 -418
  223. data/rice/to_from_ruby_defn.hpp +0 -70
  224. data/ruby.ac +0 -135
  225. data/ruby/Makefile.am +0 -1
  226. data/ruby/Makefile.in +0 -628
  227. data/ruby/lib/Makefile.am +0 -3
  228. data/ruby/lib/Makefile.in +0 -506
  229. data/ruby/lib/mkmf-rice.rb.in +0 -217
  230. data/ruby/lib/version.rb +0 -3
  231. data/sample/Makefile.am +0 -53
  232. data/sample/Makefile.in +0 -495
  233. data/test/Makefile.am +0 -73
  234. data/test/Makefile.in +0 -1219
  235. data/test/ext/Makefile.am +0 -41
  236. data/test/ext/Makefile.in +0 -483
  237. data/test/test_rice.rb +0 -45
@@ -1,5 +1,5 @@
1
1
  #include "unittest.hpp"
2
- #include "rice/Jump_Tag.hpp"
2
+ #include <rice/rice.hpp>
3
3
 
4
4
  using namespace Rice;
5
5
 
@@ -0,0 +1,161 @@
1
+ #include "unittest.hpp"
2
+ #include "embed_ruby.hpp"
3
+ #include <rice/rice.hpp>
4
+ #include <rice/stl.hpp>
5
+
6
+ using namespace Rice;
7
+
8
+ TESTSUITE(Keep_Alive);
9
+
10
+ namespace
11
+ {
12
+ class Listener {
13
+ public:
14
+ virtual ~Listener() = default;
15
+ virtual int getValue() { return 4; }
16
+ };
17
+
18
+ /**
19
+ * This class will recieve a new Listener instance
20
+ * from Ruby
21
+ */
22
+ class ListenerContainer
23
+ {
24
+ public:
25
+ void addListener(Listener* listener)
26
+ {
27
+ mListeners.push_back(listener);
28
+ }
29
+
30
+ void removeListener(Listener* listener)
31
+ {
32
+ auto iter = std::find(mListeners.begin(), mListeners.end(), listener);
33
+ mListeners.erase(iter);
34
+ }
35
+
36
+ int process()
37
+ {
38
+ std::vector<Listener*>::iterator i = mListeners.begin();
39
+ int accum = 0;
40
+ for(; i != mListeners.end(); i++)
41
+ {
42
+ accum += (*i)->getValue();
43
+ }
44
+
45
+ return accum;
46
+ }
47
+
48
+ size_t listenerCount()
49
+ {
50
+ return mListeners.size();
51
+ }
52
+
53
+ private:
54
+ std::vector<Listener*> mListeners;
55
+ };
56
+ }
57
+
58
+ SETUP(Keep_Alive)
59
+ {
60
+ embed_ruby();
61
+ }
62
+
63
+ TESTCASE(test_arg)
64
+ {
65
+ define_class<Listener>("Listener")
66
+ .define_constructor(Constructor<Listener>())
67
+ .define_method("get_value", &Listener::getValue);
68
+
69
+ define_class<ListenerContainer>("ListenerContainer")
70
+ .define_constructor(Constructor<ListenerContainer>())
71
+ .define_method("add_listener", &ListenerContainer::addListener, Arg("listener").keepAlive())
72
+ .define_method("process", &ListenerContainer::process)
73
+ .define_method("listener_count", &ListenerContainer::listenerCount);
74
+
75
+ Module m = define_module("TestingModule");
76
+ Object handler = m.instance_eval("@handler = ListenerContainer.new");
77
+
78
+ ASSERT_EQUAL(INT2NUM(0), handler.call("listener_count").value());
79
+
80
+ m.instance_eval(R"EOS(class MyListener < Listener
81
+ end)EOS");
82
+
83
+ m.instance_eval("@handler.add_listener(MyListener.new)");
84
+
85
+ // Without keep alive, this GC will crash the program because MyListener is no longer in scope
86
+ rb_gc_start();
87
+
88
+ ASSERT_EQUAL(INT2NUM(1), handler.call("listener_count").value());
89
+ ASSERT_EQUAL(INT2NUM(4), handler.call("process").value());
90
+
91
+ // Without keep alive, this GC will crash the program because MyListener is no longer in scope
92
+ rb_gc_start();
93
+ m.instance_eval("@handler.add_listener(Listener.new)");
94
+
95
+ ASSERT_EQUAL(INT2NUM(2), handler.call("listener_count").value());
96
+ ASSERT_EQUAL(INT2NUM(8), handler.call("process").value());
97
+ }
98
+
99
+ namespace
100
+ {
101
+ class Connection;
102
+
103
+ class Column
104
+ {
105
+ public:
106
+ Column(Connection& connection, uint32_t index) : connection_(connection), index_(index)
107
+ {
108
+ }
109
+
110
+ std::string name();
111
+
112
+ private:
113
+ Connection& connection_;
114
+ uint32_t index_;
115
+ };
116
+
117
+ class Connection
118
+ {
119
+ public:
120
+ Column getColumn(uint32_t index)
121
+ {
122
+ return Column(*this, index);
123
+ }
124
+
125
+ std::string getName(uint32_t index)
126
+ {
127
+ return this->prefix_ + std::to_string(index);
128
+ }
129
+
130
+ private:
131
+ std::string prefix_ = "column_";
132
+ };
133
+
134
+ std::string Column::name()
135
+ {
136
+ return this->connection_.getName(this->index_);
137
+ }
138
+ }
139
+
140
+ Object getColumn(Module& m, uint32_t index)
141
+ {
142
+ Object connection = m.instance_eval("Connection.new");
143
+ return connection.call("getColumn", 3);
144
+ }
145
+
146
+ TESTCASE(test_return)
147
+ {
148
+ define_class<Column>("Column")
149
+ .define_method("name", &Column::name);
150
+
151
+ define_class<Connection>("Connection")
152
+ .define_constructor(Constructor<Connection>())
153
+ .define_method("getColumn", &Connection::getColumn, Return().keepAlive());
154
+
155
+ Module m = define_module("TestingModule");
156
+
157
+ Object column = getColumn(m, 3);
158
+ rb_gc_start();
159
+ String name = column.call("name");
160
+ ASSERT_EQUAL("column_3", name.c_str());
161
+ }
@@ -1,8 +1,6 @@
1
1
  #include "unittest.hpp"
2
2
  #include "embed_ruby.hpp"
3
- #include "rice/String.hpp"
4
- #include "rice/Class.hpp"
5
- #include "rice/global_function.hpp"
3
+ #include <rice/rice.hpp>
6
4
 
7
5
  using namespace Rice;
8
6
 
@@ -47,5 +45,5 @@ TESTCASE(allows_copy_contructors_to_work)
47
45
  Module m = define_module("TestingModule");
48
46
 
49
47
  Object result = m.instance_eval("return_test_class.tmp");
50
- ASSERT_EQUAL(8.0, from_ruby<double>(result.value()));
48
+ ASSERT_EQUAL(8.0, detail::From_Ruby<double>().convert(result.value()));
51
49
  }
data/test/test_Module.cpp CHANGED
@@ -1,11 +1,8 @@
1
1
  #include "unittest.hpp"
2
2
  #include "embed_ruby.hpp"
3
- #include "rice/Module.hpp"
4
- #include "rice/Exception.hpp"
5
- #include "rice/Array.hpp"
6
- #include "rice/Arg.hpp"
7
- #include "rice/global_function.hpp"
8
- #include "rice/Constructor.hpp"
3
+
4
+ #include <rice/rice.hpp>
5
+ #include <rice/stl.hpp>
9
6
 
10
7
  using namespace Rice;
11
8
 
@@ -50,55 +47,73 @@ TESTCASE(add_handler)
50
47
  Object exc = m.instance_eval("begin; foo; rescue Exception; $!; end");
51
48
  ASSERT_EQUAL(rb_eRuntimeError, CLASS_OF(exc));
52
49
  Exception ex(exc);
53
- ASSERT_EQUAL(String("SILLY"), String(ex.message()));
50
+ ASSERT_EQUAL(String("SILLY"), String(ex.what()));
54
51
  }
55
52
 
56
53
  namespace
57
54
  {
58
55
 
59
- bool define_method_simple_ok;
56
+ bool some_function()
57
+ {
58
+ return true;
59
+ }
60
+
61
+ Object some_method(Object o)
62
+ {
63
+ return o;
64
+ }
65
+
66
+ int function_int(int i)
67
+ {
68
+ return i;
69
+ }
60
70
 
61
- void define_method_simple_helper(Object o)
71
+ int method_int(Object object, int i)
62
72
  {
63
- define_method_simple_ok = true;
73
+ return i;
64
74
  }
65
75
 
66
76
  } // namespace
67
77
 
68
- TESTCASE(define_method_simple)
78
+ TESTCASE(define_method)
69
79
  {
70
80
  Module m(anonymous_module());
71
- m.define_method("foo", define_method_simple_helper);
72
- define_method_simple_ok = false;
73
- Object o = m.instance_eval("o = Object.new; o.extend(self); o.foo");
74
- ASSERT(define_method_simple_ok);
81
+ m.define_method("some_method", some_method);
82
+
83
+ Object o = m.instance_eval("$o = Object.new");
84
+ Object result = m.instance_eval(R"EOS($o.extend(self)
85
+ $o.some_method)EOS");
86
+ ASSERT_EQUAL(o, result);
75
87
  }
76
88
 
77
- TESTCASE(define_singleton_method_simple)
89
+ TESTCASE(define_singleton_method)
78
90
  {
79
91
  Module m(anonymous_module());
80
- m.define_singleton_method("foo", define_method_simple_helper);
81
- define_method_simple_ok = false;
82
- Object o = m.call("foo");
83
- ASSERT(define_method_simple_ok);
92
+ m.define_singleton_method("some_method", some_method);
93
+ Object result = m.call("some_method");
94
+ ASSERT_EQUAL(m, result);
84
95
  }
85
96
 
86
- TESTCASE(define_module_function_simple)
97
+ TESTCASE(define_module_function)
87
98
  {
88
99
  Module m(anonymous_module());
89
- m.define_module_function("foo", define_method_simple_helper);
90
- define_method_simple_ok = false;
91
- m.instance_eval("o = Object.new; o.extend(self); o.instance_eval { foo }");
92
- ASSERT(define_method_simple_ok);
93
- define_method_simple_ok = false;
94
- m.call("foo");
95
- ASSERT(define_method_simple_ok);
100
+ m.define_module_function("some_function", some_function);
101
+
102
+ Object result = m.instance_eval(R"EOS(o = Object.new
103
+ o.extend(self)
104
+ o.instance_eval do
105
+ some_function
106
+ end)EOS");
107
+
108
+ ASSERT_EQUAL(Qtrue, result.value());
109
+ result = m.call("some_function");
110
+ ASSERT_EQUAL(Qtrue, result.value());
96
111
  }
97
112
 
98
113
  TESTCASE(define_module_does_not_leak_method_to_Object)
99
114
  {
100
115
  Module m = define_module("TestModule");
101
- m.define_module_function("test_module_function", &define_method_simple_helper);
116
+ m.define_module_function("test_module_function", &some_method);
102
117
 
103
118
  Module runner(anonymous_module());
104
119
  ASSERT_EXCEPTION_CHECK(
@@ -111,52 +126,53 @@ TESTCASE(define_module_does_not_leak_method_to_Object)
111
126
  );
112
127
  }
113
128
 
114
- namespace
115
- {
116
-
117
- int define_method_int_result;
118
-
119
- void define_method_int_helper(Object o, int i)
129
+ TESTCASE(define_function_int)
120
130
  {
121
- define_method_int_result = i;
131
+ Module m(anonymous_module());
132
+ m.define_function("foo", function_int);
133
+ Object result = m.instance_eval("o = Object.new; o.extend(self); o.foo(42)");
134
+ ASSERT_EQUAL(42, detail::From_Ruby<int>().convert(result));
122
135
  }
123
136
 
124
- } // namespace
125
-
126
137
  TESTCASE(define_method_int)
127
138
  {
128
139
  Module m(anonymous_module());
129
- m.define_method("foo", define_method_int_helper);
130
- define_method_int_result = 0;
131
- Object o = m.instance_eval("o = Object.new; o.extend(self); o.foo(42)");
132
- ASSERT_EQUAL(42, define_method_int_result);
140
+ m.define_method("foo", method_int);
141
+ Object result = m.instance_eval("o = Object.new; o.extend(self); o.foo(42)");
142
+ ASSERT_EQUAL(42, detail::From_Ruby<int>().convert(result));
133
143
  }
134
144
 
135
145
  TESTCASE(define_singleton_method_int)
136
146
  {
137
147
  Module m(anonymous_module());
138
- m.define_singleton_method("foo", define_method_int_helper);
139
- define_method_int_result = 0;
140
- Object o = m.call("foo", 42);
141
- ASSERT_EQUAL(42, define_method_int_result);
148
+ m.define_singleton_method("foo", method_int);
149
+ Object result = m.call("foo", 42);
150
+ ASSERT_EQUAL(42, detail::From_Ruby<int>().convert(result));
151
+ }
152
+
153
+ TESTCASE(define_singleton_function_int)
154
+ {
155
+ Module m(anonymous_module());
156
+ m.define_singleton_function("foo", function_int);
157
+ Object result = m.call("foo", 42);
158
+ ASSERT_EQUAL(42, detail::From_Ruby<int>().convert(result));
142
159
  }
143
160
 
144
161
  TESTCASE(define_module_function_int)
145
162
  {
146
163
  Module m(anonymous_module());
147
- m.define_module_function("foo", define_method_int_helper);
148
- define_method_int_result = 0;
149
- m.instance_eval("o = Object.new; o.extend(self); o.instance_eval { foo(42) }");
150
- ASSERT_EQUAL(42, define_method_int_result);
151
- define_method_int_result = 0;
152
- m.call("foo", 42);
153
- ASSERT_EQUAL(42, define_method_int_result);
164
+ m.define_module_function("foo", function_int);
165
+ Object result = m.instance_eval("o = Object.new; o.extend(self); o.instance_eval { foo(42) }");
166
+ ASSERT_EQUAL(42, detail::From_Ruby<int>().convert(result));
167
+
168
+ result = m.call("foo", 42);
169
+ ASSERT_EQUAL(42, detail::From_Ruby<int>().convert(result));
154
170
  }
155
171
 
156
- TESTCASE(define_method_int_passed_no_args)
172
+ TESTCASE(method_int_passed_no_args)
157
173
  {
158
174
  Module m(anonymous_module());
159
- m.define_method("foo", define_method_int_helper);
175
+ m.define_method("foo", method_int);
160
176
  ASSERT_EXCEPTION_CHECK(
161
177
  Exception,
162
178
  m.instance_eval("o = Object.new; o.extend(self); o.foo"),
@@ -167,47 +183,14 @@ TESTCASE(define_method_int_passed_no_args)
167
183
  );
168
184
  }
169
185
 
170
- namespace
171
- {
172
-
173
- struct Foo
174
- {
175
- int x;
176
- };
177
-
178
- int define_method_int_foo_result_i;
179
- Foo * define_method_int_foo_result_x;
180
-
181
- void define_method_int_foo_helper(Object o, int i, Foo * x)
182
- {
183
- define_method_int_foo_result_i = i;
184
- define_method_int_foo_result_x = x;
185
- }
186
-
187
- } // namespace
188
-
189
- template<>
190
- Foo * from_ruby<Foo *>(Object x)
191
- {
192
- Foo * retval;
193
- Data_Get_Struct(x.value(), Foo, retval);
194
- return retval;
195
- }
196
-
197
186
  TESTCASE(define_singleton_method_int_foo)
198
187
  {
199
188
  Module m(anonymous_module());
200
- m.define_singleton_method("int_and_foo", define_method_int_foo_helper);
201
- define_method_int_result = 0;
202
- Foo * foo = new Foo;
203
- foo->x = 1024;
204
- VALUE f = Data_Wrap_Struct(rb_cObject, 0, Default_Free_Function<Foo>::free, foo);
205
- m.call("int_and_foo", 42, Object(f));
206
- ASSERT_EQUAL(42, define_method_int_foo_result_i);
207
- ASSERT_EQUAL(foo, define_method_int_foo_result_x);
208
- }
189
+ m.define_singleton_method("method_int", method_int);
209
190
 
210
- // TODO: how to test define_iterator?
191
+ Object result = m.call("method_int", 42);
192
+ ASSERT_EQUAL(42, detail::From_Ruby<int>().convert(result));
193
+ }
211
194
 
212
195
  TESTCASE(include_module)
213
196
  {
@@ -224,7 +207,7 @@ TESTCASE(include_module)
224
207
  TESTCASE(const_set_get_by_id)
225
208
  {
226
209
  Module m(anonymous_module());
227
- Object v = to_ruby(42);
210
+ Object v = detail::to_ruby(42);
228
211
  Module & m2(m.const_set(rb_intern("FOO"), v));
229
212
  ASSERT_EQUAL(&m, &m2);
230
213
  ASSERT_EQUAL(v, m.const_get(rb_intern("FOO")));
@@ -233,7 +216,7 @@ TESTCASE(const_set_get_by_id)
233
216
  TESTCASE(const_set_get_by_identifier)
234
217
  {
235
218
  Module m(anonymous_module());
236
- Object v = to_ruby(42);
219
+ Object v = detail::to_ruby(42);
237
220
  Module & m2(m.const_set(Identifier("FOO"), v));
238
221
  ASSERT_EQUAL(&m, &m2);
239
222
  ASSERT_EQUAL(v, m.const_get(Identifier("FOO")));
@@ -242,7 +225,7 @@ TESTCASE(const_set_get_by_identifier)
242
225
  TESTCASE(const_set_get_by_string)
243
226
  {
244
227
  Module m(anonymous_module());
245
- Object v = to_ruby(42);
228
+ Object v = detail::to_ruby(42);
246
229
  Module & m2(m.const_set("FOO", v));
247
230
  ASSERT_EQUAL(&m, &m2);
248
231
  ASSERT_EQUAL(v, m.const_get("FOO"));
@@ -251,7 +234,7 @@ TESTCASE(const_set_get_by_string)
251
234
  TESTCASE(remove_const)
252
235
  {
253
236
  Module m(anonymous_module());
254
- Object v = to_ruby(42);
237
+ Object v = detail::to_ruby(42);
255
238
  m.const_set("FOO", v);
256
239
  ASSERT_EQUAL(v, m.const_get("FOO"));
257
240
  m.remove_const("FOO");
@@ -271,9 +254,7 @@ TESTCASE(mod_name_anonymous)
271
254
  ASSERT_EQUAL(String(""), m.name());
272
255
  }
273
256
 
274
- /**
275
- * Tests for default arguments
276
- */
257
+ // Tests for default arguments
277
258
  namespace
278
259
  {
279
260
  int defaults_method_one_arg1;
@@ -291,7 +272,7 @@ namespace
291
272
  TESTCASE(define_method_default_arguments)
292
273
  {
293
274
  Module m(anonymous_module());
294
- m.define_method("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true));
275
+ m.define_function("foo", &defaults_method_one, Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true);
295
276
 
296
277
  Object o = m.instance_eval("o = Object.new; o.extend(self); o");
297
278
  o.call("foo", 2);
@@ -316,7 +297,7 @@ TESTCASE(define_method_default_arguments)
316
297
  TESTCASE(default_arguments_still_throws_argument_error)
317
298
  {
318
299
  Module m(anonymous_module());
319
- m.define_method("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true));
300
+ m.define_function("foo", &defaults_method_one, Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true);
320
301
 
321
302
  ASSERT_EXCEPTION_CHECK(
322
303
  Exception,
@@ -348,7 +329,7 @@ TESTCASE(defining_methods_with_single_default_argument)
348
329
  {
349
330
  // define_method
350
331
  Module m(anonymous_module());
351
- m.define_method("foo", &method_with_one_default_arg, (Arg("num") = 4));
332
+ m.define_function("foo", &method_with_one_default_arg, (Arg("num") = 4));
352
333
  m.instance_eval("o = Object.new; o.extend(self); o.foo()");
353
334
  ASSERT_EQUAL(4, the_one_default_arg);
354
335
 
@@ -356,7 +337,7 @@ TESTCASE(defining_methods_with_single_default_argument)
356
337
 
357
338
  // define_singleton_method
358
339
  Class c(anonymous_class());
359
- c.define_singleton_method("foo", &method_with_one_default_arg, (Arg("num") = 4));
340
+ c.define_singleton_function("foo", &method_with_one_default_arg, (Arg("num") = 4));
360
341
  c.call("foo");
361
342
  ASSERT_EQUAL(4, the_one_default_arg);
362
343
 
@@ -372,7 +353,7 @@ TESTCASE(defining_methods_with_single_default_argument)
372
353
  TESTCASE(default_arguments_for_define_singleton_method)
373
354
  {
374
355
  Class c(anonymous_class());
375
- c.define_singleton_method("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true));
356
+ c.define_singleton_function("foo", &defaults_method_one, Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true);
376
357
 
377
358
  c.call("foo", 2);
378
359
 
@@ -396,7 +377,7 @@ TESTCASE(default_arguments_for_define_singleton_method)
396
377
  TESTCASE(default_arguments_for_define_module_function)
397
378
  {
398
379
  Module m(anonymous_module());
399
- m.define_module_function("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true));
380
+ m.define_module_function("foo", &defaults_method_one, Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true);
400
381
 
401
382
  m.call("foo", 2);
402
383
 
@@ -432,7 +413,7 @@ TESTCASE(define_method_works_with_reference_arguments)
432
413
  {
433
414
  Module m(anonymous_module());
434
415
  m.define_module_function("foo", &with_defaults_and_references,
435
- (Arg("x"), Arg("doIt") = false));
416
+ Arg("x"), Arg("doIt") = false);
436
417
 
437
418
  m.call("foo", "test");
438
419
 
@@ -472,8 +453,8 @@ TESTCASE(define_method_works_with_const_reference_return)
472
453
  ASSERT_EQUAL("ReturnTest", result.class_of().name().c_str());
473
454
  }
474
455
 
475
- /*
476
- namespace {
456
+ namespace
457
+ {
477
458
  float with_reference_defaults_x;
478
459
  std::string with_reference_defaults_str;
479
460
 
@@ -488,11 +469,87 @@ TESTCASE(define_method_works_with_reference_const_default_values)
488
469
  {
489
470
  Module m(anonymous_module());
490
471
  m.define_module_function("bar", &with_reference_defaults,
491
- (Arg("x"), Arg("str") = std::string("testing")));
472
+ Arg("x"), Arg("str") = std::string("testing"));
492
473
 
493
474
  m.call("bar", 3);
494
475
 
495
476
  ASSERT_EQUAL(3, with_reference_defaults_x);
496
477
  ASSERT_EQUAL("testing", with_reference_defaults_str);
497
478
  }
498
- */
479
+
480
+ namespace
481
+ {
482
+ int with_pointers_x;
483
+ std::string with_pointers_str;
484
+
485
+ void with_pointers(int* x, std::string const* str)
486
+ {
487
+ with_pointers_x = *x;
488
+ with_pointers_str = *str;
489
+ }
490
+ }
491
+
492
+ TESTCASE(define_method_works_with_pointers)
493
+ {
494
+ Module m(anonymous_module());
495
+ m.define_module_function("bar", &with_pointers);
496
+
497
+ m.call("bar", 3, "testing");
498
+
499
+ ASSERT_EQUAL(3, with_pointers_x);
500
+ ASSERT_EQUAL("testing", with_pointers_str);
501
+ }
502
+
503
+ namespace
504
+ {
505
+ int intValue;
506
+ bool boolValue;
507
+ float floatValue;
508
+ double doubleValue;
509
+
510
+ void withPointers(const int* anInt, const bool* aBool, const float* aFloat, const double* aDouble)
511
+ {
512
+ intValue = *anInt;
513
+ boolValue = *aBool;
514
+ floatValue = *aFloat;
515
+ doubleValue = *aDouble;
516
+ }
517
+
518
+ void withReferences(const int& anInt, const bool& aBool, const float& aFloat, const double& aDouble)
519
+ {
520
+ intValue = anInt;
521
+ boolValue = aBool;
522
+ floatValue = aFloat;
523
+ doubleValue = aDouble;
524
+ }
525
+ }
526
+
527
+ TESTCASE(pointers)
528
+ {
529
+ define_global_function("with_pointers", &withPointers);
530
+
531
+ Module m = define_module("TestingModule");
532
+
533
+ std::string code = "with_pointers(32, true, 33.0, 34.0)";
534
+ m.instance_eval(code);
535
+
536
+ ASSERT_EQUAL(intValue, 32);
537
+ ASSERT_EQUAL(boolValue, true);
538
+ ASSERT_EQUAL(floatValue, 33.0);
539
+ ASSERT_EQUAL(doubleValue, 34.0);
540
+ }
541
+
542
+ TESTCASE(references)
543
+ {
544
+ define_global_function("with_references", &withReferences);
545
+
546
+ Module m = define_module("TestingModule");
547
+
548
+ std::string code = "with_references(42, true, 43.0, 44.0)";
549
+ m.instance_eval(code);
550
+
551
+ ASSERT_EQUAL(intValue, 42);
552
+ ASSERT_EQUAL(boolValue, true);
553
+ ASSERT_EQUAL(floatValue, 43.0);
554
+ ASSERT_EQUAL(doubleValue, 44.0);
555
+ }