Skip to content

Commit bb2ecaf

Browse files
committed
Work around Ruby 3.0.0 bug and get tests running without a crash (although they will not all pass).
1 parent 7a818ff commit bb2ecaf

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

test/embed_ruby.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <rice/rice.hpp>
2+
#include <ruby/version.h>
23

34
void embed_ruby()
45
{
@@ -17,6 +18,18 @@ void embed_ruby()
1718

1819
initialized__ = true;
1920

20-
rb_eval_string("GC.stress = true");
21+
// Because Ruby 3 no longer initializes the GC module when embedding, calling GC.stress
22+
// results in a crash.
23+
// See https://bugs.ruby-lang.org/issues/17643
24+
if (RUBY_API_VERSION_MAJOR == 3 &&
25+
RUBY_API_VERSION_MINOR == 0 &&
26+
RUBY_API_VERSION_TEENY == 0)
27+
{
28+
// do nothing
29+
}
30+
else
31+
{
32+
rb_eval_string("GC.stress = true");
33+
}
2134
}
2235
}

test/test_Ownership.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ TESTCASE(TransferPointer)
143143
my_class = factory.transfer_pointer
144144
my_class.set_flag(i)
145145
my_class = nil
146-
end
147-
GC.start)";
146+
end)";
148147

149148
m.instance_eval(code);
149+
rb_gc_start();
150150

151151
ASSERT_EQUAL(10, MyClass::constructorCalls);
152152
ASSERT_EQUAL(0, MyClass::copyConstructorCalls);
@@ -167,10 +167,10 @@ TESTCASE(KeepPointer)
167167
10.times do |i|
168168
my_class = factory.keep_pointer
169169
my_class.set_flag(i)
170-
end
171-
GC.start)";
170+
end)";
172171

173172
m.instance_eval(code);
173+
rb_gc_start();
174174

175175
ASSERT_EQUAL(1, MyClass::constructorCalls);
176176
ASSERT_EQUAL(0, MyClass::copyConstructorCalls);
@@ -191,10 +191,10 @@ TESTCASE(KeepReference)
191191
10.times do |i|
192192
my_class = factory.keep_reference
193193
my_class.set_flag(i)
194-
end
195-
GC.start)";
194+
end)";
196195

197196
m.instance_eval(code);
197+
rb_gc_start();
198198

199199
ASSERT_EQUAL(1, MyClass::constructorCalls);
200200
ASSERT_EQUAL(0, MyClass::copyConstructorCalls);
@@ -215,10 +215,10 @@ TESTCASE(CopyReference)
215215
10.times do |i|
216216
my_class = factory.copy_reference
217217
my_class.set_flag(i)
218-
end
219-
GC.start)";
218+
end)";
220219

221220
m.instance_eval(code);
221+
rb_gc_start();
222222

223223
ASSERT_EQUAL(1, MyClass::constructorCalls);
224224
ASSERT_EQUAL(10, MyClass::copyConstructorCalls);
@@ -239,10 +239,10 @@ TESTCASE(TransferValue)
239239
10.times do |i|
240240
my_class = factory.value
241241
my_class.set_flag(i)
242-
end
243-
GC.start)";
242+
end)";
244243

245244
m.instance_eval(code);
245+
rb_gc_start();
246246

247247
ASSERT_EQUAL(10, MyClass::constructorCalls);
248248
ASSERT_EQUAL(10, MyClass::copyConstructorCalls);
@@ -263,10 +263,10 @@ TESTCASE(MoveValue)
263263
10.times do |i|
264264
my_class = factory.move_value
265265
my_class.set_flag(i)
266-
end
267-
GC.start)";
266+
end)";
268267

269268
m.instance_eval(code);
269+
rb_gc_start();
270270

271271
ASSERT_EQUAL(10, MyClass::constructorCalls);
272272
ASSERT_EQUAL(10, MyClass::copyConstructorCalls);

0 commit comments

Comments
 (0)