rice 2.1.3 → 4.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (246) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +121 -0
  3. data/CONTRIBUTORS.md +19 -0
  4. data/COPYING +2 -2
  5. data/Gemfile +3 -0
  6. data/README.md +45 -1028
  7. data/Rakefile +95 -12
  8. data/include/rice/rice.hpp +7766 -0
  9. data/include/rice/stl.hpp +1113 -0
  10. data/lib/mkmf-rice.rb +127 -0
  11. data/lib/version.rb +3 -0
  12. data/rice/Address_Registration_Guard.ipp +75 -32
  13. data/rice/Address_Registration_Guard_defn.hpp +60 -56
  14. data/rice/Arg.hpp +80 -4
  15. data/rice/Arg.ipp +51 -0
  16. data/rice/Constructor.hpp +30 -376
  17. data/rice/Data_Object.ipp +234 -107
  18. data/rice/Data_Object_defn.hpp +77 -117
  19. data/rice/Data_Type.hpp +1 -2
  20. data/rice/Data_Type.ipp +251 -295
  21. data/rice/Data_Type_defn.hpp +175 -243
  22. data/rice/Director.hpp +14 -9
  23. data/rice/Enum.hpp +54 -104
  24. data/rice/Enum.ipp +104 -230
  25. data/rice/Exception.hpp +2 -8
  26. data/rice/Exception.ipp +65 -0
  27. data/rice/Exception_defn.hpp +46 -47
  28. data/rice/Identifier.hpp +28 -28
  29. data/rice/Identifier.ipp +23 -27
  30. data/rice/Return.hpp +39 -0
  31. data/rice/Return.ipp +33 -0
  32. data/rice/detail/Exception_Handler.ipp +22 -62
  33. data/rice/detail/Exception_Handler_defn.hpp +76 -91
  34. data/rice/detail/Iterator.hpp +18 -88
  35. data/rice/detail/Iterator.ipp +47 -0
  36. data/rice/detail/Jump_Tag.hpp +21 -0
  37. data/rice/detail/MethodInfo.hpp +44 -0
  38. data/rice/detail/MethodInfo.ipp +78 -0
  39. data/rice/detail/NativeAttribute.hpp +53 -0
  40. data/rice/detail/NativeAttribute.ipp +83 -0
  41. data/rice/detail/NativeFunction.hpp +69 -0
  42. data/rice/detail/NativeFunction.ipp +248 -0
  43. data/rice/detail/RubyFunction.hpp +39 -0
  44. data/rice/detail/RubyFunction.ipp +92 -0
  45. data/rice/detail/Type.hpp +29 -0
  46. data/rice/detail/Type.ipp +138 -0
  47. data/rice/detail/TypeRegistry.hpp +50 -0
  48. data/rice/detail/TypeRegistry.ipp +106 -0
  49. data/rice/detail/Wrapper.hpp +51 -0
  50. data/rice/detail/Wrapper.ipp +151 -0
  51. data/rice/detail/default_allocation_func.hpp +8 -19
  52. data/rice/detail/default_allocation_func.ipp +9 -8
  53. data/rice/detail/from_ruby.hpp +2 -37
  54. data/rice/detail/from_ruby.ipp +1020 -46
  55. data/rice/detail/from_ruby_defn.hpp +38 -0
  56. data/rice/detail/function_traits.hpp +124 -0
  57. data/rice/detail/method_data.hpp +23 -15
  58. data/rice/detail/method_data.ipp +53 -0
  59. data/rice/detail/rice_traits.hpp +116 -0
  60. data/rice/detail/ruby.hpp +9 -50
  61. data/rice/detail/to_ruby.hpp +3 -17
  62. data/rice/detail/to_ruby.ipp +409 -31
  63. data/rice/detail/to_ruby_defn.hpp +48 -0
  64. data/rice/forward_declares.ipp +82 -0
  65. data/rice/global_function.hpp +16 -20
  66. data/rice/global_function.ipp +8 -17
  67. data/rice/rice.hpp +59 -0
  68. data/rice/ruby_mark.hpp +5 -3
  69. data/rice/ruby_try_catch.hpp +4 -4
  70. data/rice/stl.hpp +11 -0
  71. data/sample/callbacks/extconf.rb +6 -0
  72. data/sample/callbacks/sample_callbacks.cpp +35 -0
  73. data/sample/callbacks/test.rb +28 -0
  74. data/sample/enum/extconf.rb +3 -0
  75. data/sample/enum/sample_enum.cpp +3 -17
  76. data/sample/enum/test.rb +2 -2
  77. data/sample/inheritance/animals.cpp +8 -24
  78. data/sample/inheritance/extconf.rb +3 -0
  79. data/sample/inheritance/test.rb +1 -1
  80. data/sample/map/extconf.rb +3 -0
  81. data/sample/map/map.cpp +10 -18
  82. data/sample/map/test.rb +1 -1
  83. data/test/embed_ruby.cpp +34 -0
  84. data/test/embed_ruby.hpp +4 -0
  85. data/test/ext/t1/extconf.rb +3 -0
  86. data/test/ext/t1/t1.cpp +1 -3
  87. data/test/ext/t2/extconf.rb +3 -0
  88. data/test/ext/t2/t2.cpp +1 -1
  89. data/test/extconf.rb +23 -0
  90. data/test/ruby/test_callbacks_sample.rb +28 -0
  91. data/test/ruby/test_multiple_extensions.rb +18 -0
  92. data/test/ruby/test_multiple_extensions_same_class.rb +14 -0
  93. data/test/ruby/test_multiple_extensions_with_inheritance.rb +20 -0
  94. data/test/test_Address_Registration_Guard.cpp +25 -11
  95. data/test/test_Array.cpp +131 -74
  96. data/test/test_Attribute.cpp +147 -0
  97. data/test/test_Builtin_Object.cpp +36 -15
  98. data/test/test_Class.cpp +151 -276
  99. data/test/test_Constructor.cpp +10 -9
  100. data/test/test_Data_Object.cpp +135 -193
  101. data/test/test_Data_Type.cpp +323 -252
  102. data/test/test_Director.cpp +56 -42
  103. data/test/test_Enum.cpp +230 -104
  104. data/test/test_Exception.cpp +7 -7
  105. data/test/test_Hash.cpp +33 -31
  106. data/test/test_Identifier.cpp +6 -6
  107. data/test/test_Inheritance.cpp +221 -0
  108. data/test/test_Iterator.cpp +161 -0
  109. data/test/test_Jump_Tag.cpp +1 -1
  110. data/test/test_Keep_Alive.cpp +161 -0
  111. data/test/test_Memory_Management.cpp +4 -5
  112. data/test/test_Module.cpp +169 -111
  113. data/test/test_Object.cpp +51 -19
  114. data/test/test_Ownership.cpp +275 -0
  115. data/test/test_Self.cpp +205 -0
  116. data/test/test_Stl_Optional.cpp +90 -0
  117. data/test/test_Stl_Pair.cpp +144 -0
  118. data/test/test_Stl_SmartPointer.cpp +200 -0
  119. data/test/test_Stl_String.cpp +74 -0
  120. data/test/test_Stl_Vector.cpp +652 -0
  121. data/test/test_String.cpp +3 -3
  122. data/test/test_Struct.cpp +31 -40
  123. data/test/test_Symbol.cpp +3 -3
  124. data/test/test_To_From_Ruby.cpp +283 -218
  125. data/test/test_global_functions.cpp +41 -20
  126. data/test/unittest.cpp +34 -8
  127. data/test/unittest.hpp +0 -4
  128. metadata +121 -136
  129. data/Doxyfile +0 -2268
  130. data/Makefile.am +0 -26
  131. data/Makefile.in +0 -923
  132. data/README.mingw +0 -8
  133. data/aclocal.m4 +0 -1088
  134. data/bootstrap +0 -8
  135. data/check_stdcxx_11.ac +0 -103
  136. data/config.guess +0 -1421
  137. data/config.sub +0 -1807
  138. data/configure +0 -7367
  139. data/configure.ac +0 -55
  140. data/depcomp +0 -791
  141. data/doxygen.ac +0 -314
  142. data/doxygen.am +0 -186
  143. data/extconf.rb +0 -69
  144. data/install-sh +0 -501
  145. data/missing +0 -215
  146. data/post-autoconf.rb +0 -22
  147. data/post-automake.rb +0 -28
  148. data/rice/Address_Registration_Guard.cpp +0 -22
  149. data/rice/Arg_impl.hpp +0 -129
  150. data/rice/Arg_operators.cpp +0 -21
  151. data/rice/Arg_operators.hpp +0 -19
  152. data/rice/Array.hpp +0 -214
  153. data/rice/Array.ipp +0 -256
  154. data/rice/Builtin_Object.hpp +0 -8
  155. data/rice/Builtin_Object.ipp +0 -50
  156. data/rice/Builtin_Object_defn.hpp +0 -50
  157. data/rice/Class.cpp +0 -57
  158. data/rice/Class.hpp +0 -8
  159. data/rice/Class.ipp +0 -6
  160. data/rice/Class_defn.hpp +0 -83
  161. data/rice/Data_Type.cpp +0 -54
  162. data/rice/Data_Type_fwd.hpp +0 -12
  163. data/rice/Director.cpp +0 -13
  164. data/rice/Exception.cpp +0 -59
  165. data/rice/Exception_Base.hpp +0 -8
  166. data/rice/Exception_Base.ipp +0 -13
  167. data/rice/Exception_Base_defn.hpp +0 -27
  168. data/rice/Hash.hpp +0 -227
  169. data/rice/Hash.ipp +0 -329
  170. data/rice/Identifier.cpp +0 -8
  171. data/rice/Jump_Tag.hpp +0 -24
  172. data/rice/Makefile.am +0 -125
  173. data/rice/Makefile.in +0 -888
  174. data/rice/Module.cpp +0 -84
  175. data/rice/Module.hpp +0 -8
  176. data/rice/Module.ipp +0 -6
  177. data/rice/Module_defn.hpp +0 -88
  178. data/rice/Module_impl.hpp +0 -281
  179. data/rice/Module_impl.ipp +0 -345
  180. data/rice/Object.cpp +0 -169
  181. data/rice/Object.hpp +0 -8
  182. data/rice/Object.ipp +0 -19
  183. data/rice/Object_defn.hpp +0 -191
  184. data/rice/Require_Guard.hpp +0 -21
  185. data/rice/String.cpp +0 -94
  186. data/rice/String.hpp +0 -91
  187. data/rice/Struct.cpp +0 -117
  188. data/rice/Struct.hpp +0 -162
  189. data/rice/Struct.ipp +0 -26
  190. data/rice/Symbol.cpp +0 -25
  191. data/rice/Symbol.hpp +0 -66
  192. data/rice/Symbol.ipp +0 -44
  193. data/rice/config.hpp +0 -47
  194. data/rice/config.hpp.in +0 -46
  195. data/rice/detail/Arguments.hpp +0 -118
  196. data/rice/detail/Auto_Function_Wrapper.hpp +0 -898
  197. data/rice/detail/Auto_Function_Wrapper.ipp +0 -3694
  198. data/rice/detail/Auto_Member_Function_Wrapper.hpp +0 -897
  199. data/rice/detail/Auto_Member_Function_Wrapper.ipp +0 -2774
  200. data/rice/detail/Caster.hpp +0 -103
  201. data/rice/detail/Not_Copyable.hpp +0 -25
  202. data/rice/detail/Wrapped_Function.hpp +0 -33
  203. data/rice/detail/cfp.hpp +0 -24
  204. data/rice/detail/cfp.ipp +0 -51
  205. data/rice/detail/check_ruby_type.cpp +0 -27
  206. data/rice/detail/check_ruby_type.hpp +0 -23
  207. data/rice/detail/creation_funcs.hpp +0 -37
  208. data/rice/detail/creation_funcs.ipp +0 -36
  209. data/rice/detail/define_method_and_auto_wrap.hpp +0 -31
  210. data/rice/detail/define_method_and_auto_wrap.ipp +0 -30
  211. data/rice/detail/demangle.cpp +0 -56
  212. data/rice/detail/demangle.hpp +0 -19
  213. data/rice/detail/env.hpp +0 -11
  214. data/rice/detail/method_data.cpp +0 -86
  215. data/rice/detail/node.hpp +0 -13
  216. data/rice/detail/object_call.hpp +0 -69
  217. data/rice/detail/object_call.ipp +0 -131
  218. data/rice/detail/protect.cpp +0 -29
  219. data/rice/detail/protect.hpp +0 -34
  220. data/rice/detail/ruby_version_code.hpp +0 -6
  221. data/rice/detail/ruby_version_code.hpp.in +0 -6
  222. data/rice/detail/st.hpp +0 -22
  223. data/rice/detail/traits.hpp +0 -43
  224. data/rice/detail/win32.hpp +0 -16
  225. data/rice/detail/wrap_function.hpp +0 -341
  226. data/rice/detail/wrap_function.ipp +0 -514
  227. data/rice/protect.hpp +0 -92
  228. data/rice/protect.ipp +0 -1134
  229. data/rice/rubypp.rb +0 -97
  230. data/rice/to_from_ruby.hpp +0 -8
  231. data/rice/to_from_ruby.ipp +0 -294
  232. data/rice/to_from_ruby_defn.hpp +0 -70
  233. data/ruby.ac +0 -135
  234. data/ruby/Makefile.am +0 -1
  235. data/ruby/Makefile.in +0 -628
  236. data/ruby/lib/Makefile.am +0 -3
  237. data/ruby/lib/Makefile.in +0 -506
  238. data/ruby/lib/mkmf-rice.rb.in +0 -217
  239. data/ruby/lib/version.rb +0 -3
  240. data/sample/Makefile.am +0 -47
  241. data/sample/Makefile.in +0 -489
  242. data/test/Makefile.am +0 -72
  243. data/test/Makefile.in +0 -1213
  244. data/test/ext/Makefile.am +0 -41
  245. data/test/ext/Makefile.in +0 -483
  246. data/test/test_rice.rb +0 -41
@@ -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,7 +1,6 @@
1
1
  #include "unittest.hpp"
2
- #include "rice/String.hpp"
3
- #include "rice/Class.hpp"
4
- #include "rice/global_function.hpp"
2
+ #include "embed_ruby.hpp"
3
+ #include <rice/rice.hpp>
5
4
 
6
5
  using namespace Rice;
7
6
 
@@ -9,7 +8,7 @@ TESTSUITE(Memory_Management);
9
8
 
10
9
  SETUP(Memory_Management)
11
10
  {
12
- ruby_init();
11
+ embed_ruby();
13
12
  }
14
13
 
15
14
  namespace
@@ -46,5 +45,5 @@ TESTCASE(allows_copy_contructors_to_work)
46
45
  Module m = define_module("TestingModule");
47
46
 
48
47
  Object result = m.instance_eval("return_test_class.tmp");
49
- ASSERT_EQUAL(8.0, from_ruby<double>(result.value()));
48
+ ASSERT_EQUAL(8.0, detail::From_Ruby<double>().convert(result.value()));
50
49
  }
data/test/test_Module.cpp CHANGED
@@ -1,10 +1,8 @@
1
1
  #include "unittest.hpp"
2
- #include "rice/Module.hpp"
3
- #include "rice/Exception.hpp"
4
- #include "rice/Array.hpp"
5
- #include "rice/Arg.hpp"
6
- #include "rice/global_function.hpp"
7
- #include "rice/Constructor.hpp"
2
+ #include "embed_ruby.hpp"
3
+
4
+ #include <rice/rice.hpp>
5
+ #include <rice/stl.hpp>
8
6
 
9
7
  using namespace Rice;
10
8
 
@@ -12,7 +10,7 @@ TESTSUITE(Module);
12
10
 
13
11
  SETUP(Object)
14
12
  {
15
- ruby_init();
13
+ embed_ruby();
16
14
  }
17
15
 
18
16
  TESTCASE(construct_from_value)
@@ -49,55 +47,73 @@ TESTCASE(add_handler)
49
47
  Object exc = m.instance_eval("begin; foo; rescue Exception; $!; end");
50
48
  ASSERT_EQUAL(rb_eRuntimeError, CLASS_OF(exc));
51
49
  Exception ex(exc);
52
- ASSERT_EQUAL(String("SILLY"), String(ex.message()));
50
+ ASSERT_EQUAL(String("SILLY"), String(ex.what()));
53
51
  }
54
52
 
55
53
  namespace
56
54
  {
57
55
 
58
- 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
+ }
59
70
 
60
- void define_method_simple_helper(Object o)
71
+ int method_int(Object object, int i)
61
72
  {
62
- define_method_simple_ok = true;
73
+ return i;
63
74
  }
64
75
 
65
76
  } // namespace
66
77
 
67
- TESTCASE(define_method_simple)
78
+ TESTCASE(define_method)
68
79
  {
69
80
  Module m(anonymous_module());
70
- m.define_method("foo", define_method_simple_helper);
71
- define_method_simple_ok = false;
72
- Object o = m.instance_eval("o = Object.new; o.extend(self); o.foo");
73
- 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);
74
87
  }
75
88
 
76
- TESTCASE(define_singleton_method_simple)
89
+ TESTCASE(define_singleton_method)
77
90
  {
78
91
  Module m(anonymous_module());
79
- m.define_singleton_method("foo", define_method_simple_helper);
80
- define_method_simple_ok = false;
81
- Object o = m.call("foo");
82
- 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);
83
95
  }
84
96
 
85
- TESTCASE(define_module_function_simple)
97
+ TESTCASE(define_module_function)
86
98
  {
87
99
  Module m(anonymous_module());
88
- m.define_module_function("foo", define_method_simple_helper);
89
- define_method_simple_ok = false;
90
- m.instance_eval("o = Object.new; o.extend(self); o.instance_eval { foo }");
91
- ASSERT(define_method_simple_ok);
92
- define_method_simple_ok = false;
93
- m.call("foo");
94
- 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());
95
111
  }
96
112
 
97
113
  TESTCASE(define_module_does_not_leak_method_to_Object)
98
114
  {
99
115
  Module m = define_module("TestModule");
100
- m.define_module_function("test_module_function", &define_method_simple_helper);
116
+ m.define_module_function("test_module_function", &some_method);
101
117
 
102
118
  Module runner(anonymous_module());
103
119
  ASSERT_EXCEPTION_CHECK(
@@ -110,52 +126,53 @@ TESTCASE(define_module_does_not_leak_method_to_Object)
110
126
  );
111
127
  }
112
128
 
113
- namespace
114
- {
115
-
116
- int define_method_int_result;
117
-
118
- void define_method_int_helper(Object o, int i)
129
+ TESTCASE(define_function_int)
119
130
  {
120
- 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));
121
135
  }
122
136
 
123
- } // namespace
124
-
125
137
  TESTCASE(define_method_int)
126
138
  {
127
139
  Module m(anonymous_module());
128
- m.define_method("foo", define_method_int_helper);
129
- define_method_int_result = 0;
130
- Object o = m.instance_eval("o = Object.new; o.extend(self); o.foo(42)");
131
- 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));
132
143
  }
133
144
 
134
145
  TESTCASE(define_singleton_method_int)
135
146
  {
136
147
  Module m(anonymous_module());
137
- m.define_singleton_method("foo", define_method_int_helper);
138
- define_method_int_result = 0;
139
- Object o = m.call("foo", 42);
140
- 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));
141
159
  }
142
160
 
143
161
  TESTCASE(define_module_function_int)
144
162
  {
145
163
  Module m(anonymous_module());
146
- m.define_module_function("foo", define_method_int_helper);
147
- define_method_int_result = 0;
148
- m.instance_eval("o = Object.new; o.extend(self); o.instance_eval { foo(42) }");
149
- ASSERT_EQUAL(42, define_method_int_result);
150
- define_method_int_result = 0;
151
- m.call("foo", 42);
152
- 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));
153
170
  }
154
171
 
155
- TESTCASE(define_method_int_passed_no_args)
172
+ TESTCASE(method_int_passed_no_args)
156
173
  {
157
174
  Module m(anonymous_module());
158
- m.define_method("foo", define_method_int_helper);
175
+ m.define_method("foo", method_int);
159
176
  ASSERT_EXCEPTION_CHECK(
160
177
  Exception,
161
178
  m.instance_eval("o = Object.new; o.extend(self); o.foo"),
@@ -166,47 +183,14 @@ TESTCASE(define_method_int_passed_no_args)
166
183
  );
167
184
  }
168
185
 
169
- namespace
170
- {
171
-
172
- struct Foo
173
- {
174
- int x;
175
- };
176
-
177
- int define_method_int_foo_result_i;
178
- Foo * define_method_int_foo_result_x;
179
-
180
- void define_method_int_foo_helper(Object o, int i, Foo * x)
181
- {
182
- define_method_int_foo_result_i = i;
183
- define_method_int_foo_result_x = x;
184
- }
185
-
186
- } // namespace
187
-
188
- template<>
189
- Foo * from_ruby<Foo *>(Object x)
190
- {
191
- Foo * retval;
192
- Data_Get_Struct(x.value(), Foo, retval);
193
- return retval;
194
- }
195
-
196
186
  TESTCASE(define_singleton_method_int_foo)
197
187
  {
198
188
  Module m(anonymous_module());
199
- m.define_singleton_method("int_and_foo", define_method_int_foo_helper);
200
- define_method_int_result = 0;
201
- Foo * foo = new Foo;
202
- foo->x = 1024;
203
- VALUE f = Data_Wrap_Struct(rb_cObject, 0, Default_Free_Function<Foo>::free, foo);
204
- m.call("int_and_foo", 42, Object(f));
205
- ASSERT_EQUAL(42, define_method_int_foo_result_i);
206
- ASSERT_EQUAL(foo, define_method_int_foo_result_x);
207
- }
189
+ m.define_singleton_method("method_int", method_int);
208
190
 
209
- // 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
+ }
210
194
 
211
195
  TESTCASE(include_module)
212
196
  {
@@ -223,7 +207,7 @@ TESTCASE(include_module)
223
207
  TESTCASE(const_set_get_by_id)
224
208
  {
225
209
  Module m(anonymous_module());
226
- Object v = to_ruby(42);
210
+ Object v = detail::to_ruby(42);
227
211
  Module & m2(m.const_set(rb_intern("FOO"), v));
228
212
  ASSERT_EQUAL(&m, &m2);
229
213
  ASSERT_EQUAL(v, m.const_get(rb_intern("FOO")));
@@ -232,7 +216,7 @@ TESTCASE(const_set_get_by_id)
232
216
  TESTCASE(const_set_get_by_identifier)
233
217
  {
234
218
  Module m(anonymous_module());
235
- Object v = to_ruby(42);
219
+ Object v = detail::to_ruby(42);
236
220
  Module & m2(m.const_set(Identifier("FOO"), v));
237
221
  ASSERT_EQUAL(&m, &m2);
238
222
  ASSERT_EQUAL(v, m.const_get(Identifier("FOO")));
@@ -241,7 +225,7 @@ TESTCASE(const_set_get_by_identifier)
241
225
  TESTCASE(const_set_get_by_string)
242
226
  {
243
227
  Module m(anonymous_module());
244
- Object v = to_ruby(42);
228
+ Object v = detail::to_ruby(42);
245
229
  Module & m2(m.const_set("FOO", v));
246
230
  ASSERT_EQUAL(&m, &m2);
247
231
  ASSERT_EQUAL(v, m.const_get("FOO"));
@@ -250,7 +234,7 @@ TESTCASE(const_set_get_by_string)
250
234
  TESTCASE(remove_const)
251
235
  {
252
236
  Module m(anonymous_module());
253
- Object v = to_ruby(42);
237
+ Object v = detail::to_ruby(42);
254
238
  m.const_set("FOO", v);
255
239
  ASSERT_EQUAL(v, m.const_get("FOO"));
256
240
  m.remove_const("FOO");
@@ -270,9 +254,7 @@ TESTCASE(mod_name_anonymous)
270
254
  ASSERT_EQUAL(String(""), m.name());
271
255
  }
272
256
 
273
- /**
274
- * Tests for default arguments
275
- */
257
+ // Tests for default arguments
276
258
  namespace
277
259
  {
278
260
  int defaults_method_one_arg1;
@@ -290,7 +272,7 @@ namespace
290
272
  TESTCASE(define_method_default_arguments)
291
273
  {
292
274
  Module m(anonymous_module());
293
- 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);
294
276
 
295
277
  Object o = m.instance_eval("o = Object.new; o.extend(self); o");
296
278
  o.call("foo", 2);
@@ -315,7 +297,7 @@ TESTCASE(define_method_default_arguments)
315
297
  TESTCASE(default_arguments_still_throws_argument_error)
316
298
  {
317
299
  Module m(anonymous_module());
318
- 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);
319
301
 
320
302
  ASSERT_EXCEPTION_CHECK(
321
303
  Exception,
@@ -347,7 +329,7 @@ TESTCASE(defining_methods_with_single_default_argument)
347
329
  {
348
330
  // define_method
349
331
  Module m(anonymous_module());
350
- 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));
351
333
  m.instance_eval("o = Object.new; o.extend(self); o.foo()");
352
334
  ASSERT_EQUAL(4, the_one_default_arg);
353
335
 
@@ -355,7 +337,7 @@ TESTCASE(defining_methods_with_single_default_argument)
355
337
 
356
338
  // define_singleton_method
357
339
  Class c(anonymous_class());
358
- 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));
359
341
  c.call("foo");
360
342
  ASSERT_EQUAL(4, the_one_default_arg);
361
343
 
@@ -371,7 +353,7 @@ TESTCASE(defining_methods_with_single_default_argument)
371
353
  TESTCASE(default_arguments_for_define_singleton_method)
372
354
  {
373
355
  Class c(anonymous_class());
374
- 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);
375
357
 
376
358
  c.call("foo", 2);
377
359
 
@@ -395,7 +377,7 @@ TESTCASE(default_arguments_for_define_singleton_method)
395
377
  TESTCASE(default_arguments_for_define_module_function)
396
378
  {
397
379
  Module m(anonymous_module());
398
- 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);
399
381
 
400
382
  m.call("foo", 2);
401
383
 
@@ -431,7 +413,7 @@ TESTCASE(define_method_works_with_reference_arguments)
431
413
  {
432
414
  Module m(anonymous_module());
433
415
  m.define_module_function("foo", &with_defaults_and_references,
434
- (Arg("x"), Arg("doIt") = false));
416
+ Arg("x"), Arg("doIt") = false);
435
417
 
436
418
  m.call("foo", "test");
437
419
 
@@ -471,8 +453,8 @@ TESTCASE(define_method_works_with_const_reference_return)
471
453
  ASSERT_EQUAL("ReturnTest", result.class_of().name().c_str());
472
454
  }
473
455
 
474
- /*
475
- namespace {
456
+ namespace
457
+ {
476
458
  float with_reference_defaults_x;
477
459
  std::string with_reference_defaults_str;
478
460
 
@@ -487,11 +469,87 @@ TESTCASE(define_method_works_with_reference_const_default_values)
487
469
  {
488
470
  Module m(anonymous_module());
489
471
  m.define_module_function("bar", &with_reference_defaults,
490
- (Arg("x"), Arg("str") = std::string("testing")));
472
+ Arg("x"), Arg("str") = std::string("testing"));
491
473
 
492
474
  m.call("bar", 3);
493
475
 
494
476
  ASSERT_EQUAL(3, with_reference_defaults_x);
495
477
  ASSERT_EQUAL("testing", with_reference_defaults_str);
496
478
  }
497
- */
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
+ }