Skip to content

Commit d7b6b96

Browse files
committed
[GR-19220] Implement rb_gc_register_mark_object and rb_enc_str_asciionly_p (#1856).
PullRequest: truffleruby/1195
2 parents 53b07fd + ae302a6 commit d7b6b96

File tree

7 files changed

+40
-2
lines changed

7 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Compatibility:
5858
* Implemented `Method#<<` and `Method#>>` (#1821).
5959
* The `.bundle` file extension is now used for C extensions on macOS (#1819, #1837).
6060
* Implemented `Comparable#clamp` (#1517).
61+
* Implemented `rb_gc_register_mark_object` and `rb_enc_str_asciionly_p` (#1856, @chrisseaton).
6162

6263
Performance:
6364

lib/truffle/truffle/cext.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,12 @@ def rb_gc
14101410
GC.start
14111411
end
14121412

1413+
GC_ROOTS = []
1414+
1415+
def rb_gc_register_mark_object(obj)
1416+
GC_ROOTS.push obj
1417+
end
1418+
14131419
def rb_nativethread_self
14141420
Thread.current
14151421
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
@@ -34,6 +34,11 @@ static VALUE gc_spec_rb_gc_adjust_memory_usage(VALUE self, VALUE diff) {
3434
return Qnil;
3535
}
3636

37+
static VALUE gc_spec_rb_gc_register_mark_object(VALUE self, VALUE obj) {
38+
rb_gc_register_mark_object(obj);
39+
return Qnil;
40+
}
41+
3742
void Init_gc_spec(void) {
3843
VALUE cls = rb_define_class("CApiGCSpecs", rb_cObject);
3944
registered_tagged_value = INT2NUM(10);
@@ -48,6 +53,7 @@ void Init_gc_spec(void) {
4853
rb_define_method(cls, "rb_gc_disable", gc_spec_rb_gc_disable, 0);
4954
rb_define_method(cls, "rb_gc", gc_spec_rb_gc, 0);
5055
rb_define_method(cls, "rb_gc_adjust_memory_usage", gc_spec_rb_gc_adjust_memory_usage, 1);
56+
rb_define_method(cls, "rb_gc_register_mark_object", gc_spec_rb_gc_register_mark_object, 1);
5157
}
5258

5359
#ifdef __cplusplus

spec/ruby/optional/capi/gc_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,10 @@
5858
}.should_not raise_error
5959
end
6060
end
61+
62+
describe "rb_gc_register_mark_object" do
63+
it "can be called with an object" do
64+
@f.rb_gc_register_mark_object(Object.new).should be_nil
65+
end
66+
end
6167
end

src/main/c/cext/ruby.c

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

32493249
int rb_enc_str_asciionly_p(VALUE str) {
3250-
rb_tr_error("rb_enc_str_asciionly_p not implemented");
3250+
return polyglot_as_boolean(RUBY_INVOKE_NO_WRAP(str, "ascii_only?"));
32513251
}
32523252

32533253
int rb_enc_unicode_p(rb_encoding *enc) {
@@ -4715,7 +4715,7 @@ void rb_define_virtual_variable(const char *name, VALUE (*getter)(ANYARGS), void
47154715
}
47164716

47174717
void rb_gc_register_mark_object(VALUE obj) {
4718-
rb_tr_error("rb_gc_register_mark_object not implemented");
4718+
RUBY_CEXT_INVOKE_NO_WRAP("rb_gc_register_mark_object", obj);
47194719
}
47204720

47214721
ID rb_check_id(volatile VALUE *namep) {

0 commit comments

Comments
 (0)