Skip to content

Commit d94c06a

Browse files
committed
[GR-20296] Backport to 19.3.1: Implement rb_gc_register_mark_object and rb_enc_str_asciionly_p.
PullRequest: truffleruby/1227
2 parents 6b99e98 + 56fbd56 commit d94c06a

File tree

7 files changed

+44
-2
lines changed

7 files changed

+44
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 19.3.1
2+
3+
Compatibility:
4+
5+
* Implemented `rb_gc_register_mark_object` and `rb_enc_str_asciionly_p` (#1856, @chrisseaton).
6+
17
# 19.3.0
28

39
New features:

lib/truffle/truffle/cext.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,12 @@ def rb_gc
14181418
GC.start
14191419
end
14201420

1421+
GC_ROOTS = []
1422+
1423+
def rb_gc_register_mark_object(obj)
1424+
GC_ROOTS.push obj
1425+
end
1426+
14211427
def rb_nativethread_self
14221428
Thread.current
14231429
end

spec/ruby/optional/capi/encoding_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,4 +479,14 @@
479479
length.should == 4
480480
end
481481
end
482+
483+
describe "rb_enc_str_asciionly_p" do
484+
it "returns true for an ASCII string" do
485+
@s.rb_enc_str_asciionly_p("hello").should be_true
486+
end
487+
488+
it "returns false for a non-ASCII string" do
489+
@s.rb_enc_str_asciionly_p("hüllo").should be_false
490+
end
491+
end
482492
end

spec/ruby/optional/capi/ext/encoding_spec.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ static VALUE encoding_spec_rb_enc_codepoint_len(VALUE self, VALUE str) {
196196
return rb_ary_new3(2, LONG2NUM(codepoint), LONG2NUM(len));
197197
}
198198

199+
static VALUE encoding_spec_rb_enc_str_asciionly_p(VALUE self, VALUE str) {
200+
if (rb_enc_str_asciionly_p(str)) {
201+
return Qtrue;
202+
} else {
203+
return Qfalse;
204+
}
205+
}
206+
199207
void Init_encoding_spec(void) {
200208
VALUE cls = rb_define_class("CApiEncodingSpecs", rb_cObject);
201209
rb_define_method(cls, "ENC_CODERANGE_ASCIIONLY",
@@ -242,6 +250,7 @@ void Init_encoding_spec(void) {
242250
rb_define_method(cls, "rb_to_encoding_index", encoding_spec_rb_to_encoding_index, 1);
243251
rb_define_method(cls, "rb_enc_nth", encoding_spec_rb_enc_nth, 2);
244252
rb_define_method(cls, "rb_enc_codepoint_len", encoding_spec_rb_enc_codepoint_len, 1);
253+
rb_define_method(cls, "rb_enc_str_asciionly_p", encoding_spec_rb_enc_str_asciionly_p, 1);
245254
}
246255

247256
#ifdef __cplusplus

spec/ruby/optional/capi/ext/gc_spec.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ static VALUE gc_spec_rb_gc() {
3030
}
3131

3232

33+
static VALUE gc_spec_rb_gc_register_mark_object(VALUE self, VALUE obj) {
34+
rb_gc_register_mark_object(obj);
35+
return Qnil;
36+
}
37+
3338
void Init_gc_spec(void) {
3439
VALUE cls = rb_define_class("CApiGCSpecs", rb_cObject);
3540
registered_tagged_value = INT2NUM(10);
@@ -43,6 +48,7 @@ void Init_gc_spec(void) {
4348
rb_define_method(cls, "rb_gc_enable", gc_spec_rb_gc_enable, 0);
4449
rb_define_method(cls, "rb_gc_disable", gc_spec_rb_gc_disable, 0);
4550
rb_define_method(cls, "rb_gc", gc_spec_rb_gc, 0);
51+
rb_define_method(cls, "rb_gc_register_mark_object", gc_spec_rb_gc_register_mark_object, 1);
4652
}
4753

4854
#ifdef __cplusplus

spec/ruby/optional/capi/gc_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@
5151

5252
end
5353

54+
describe "rb_gc_register_mark_object" do
55+
it "can be called with an object" do
56+
@f.rb_gc_register_mark_object(Object.new).should be_nil
57+
end
58+
end
5459
end

src/main/c/cext/ruby.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,7 +3245,7 @@ long rb_str_coderange_scan_restartable(const char *s, const char *e, rb_encoding
32453245
}
32463246

32473247
int rb_enc_str_asciionly_p(VALUE str) {
3248-
rb_tr_error("rb_enc_str_asciionly_p not implemented");
3248+
return polyglot_as_boolean(RUBY_INVOKE_NO_WRAP(str, "ascii_only?"));
32493249
}
32503250

32513251
int rb_enc_unicode_p(rb_encoding *enc) {
@@ -4711,7 +4711,7 @@ void rb_define_virtual_variable(const char *name, VALUE (*getter)(ANYARGS), void
47114711
}
47124712

47134713
void rb_gc_register_mark_object(VALUE obj) {
4714-
rb_tr_error("rb_gc_register_mark_object not implemented");
4714+
RUBY_CEXT_INVOKE_NO_WRAP("rb_gc_register_mark_object", obj);
47154715
}
47164716

47174717
ID rb_check_id(volatile VALUE *namep) {

0 commit comments

Comments
 (0)