Skip to content

Commit

Permalink
[GR-19220] Implement rb_gc_register_mark_object and rb_enc_str_asciio…
Browse files Browse the repository at this point in the history
…nly_p (#1856).

PullRequest: truffleruby/1195
  • Loading branch information
eregon committed Dec 16, 2019
2 parents 53b07fd + ae302a6 commit d7b6b96
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Compatibility:
* Implemented `Method#<<` and `Method#>>` (#1821).
* The `.bundle` file extension is now used for C extensions on macOS (#1819, #1837).
* Implemented `Comparable#clamp` (#1517).
* Implemented `rb_gc_register_mark_object` and `rb_enc_str_asciionly_p` (#1856, @chrisseaton).

Performance:

Expand Down
6 changes: 6 additions & 0 deletions lib/truffle/truffle/cext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,12 @@ def rb_gc
GC.start
end

GC_ROOTS = []

def rb_gc_register_mark_object(obj)
GC_ROOTS.push obj
end

def rb_nativethread_self
Thread.current
end
Expand Down
10 changes: 10 additions & 0 deletions spec/ruby/optional/capi/encoding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,14 @@
length.should == 4
end
end

describe "rb_enc_str_asciionly_p" do
it "returns true for an ASCII string" do
@s.rb_enc_str_asciionly_p("hello").should be_true
end

it "returns false for a non-ASCII string" do
@s.rb_enc_str_asciionly_p("hüllo").should be_false
end
end
end
9 changes: 9 additions & 0 deletions spec/ruby/optional/capi/ext/encoding_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ static VALUE encoding_spec_rb_enc_codepoint_len(VALUE self, VALUE str) {
return rb_ary_new3(2, LONG2NUM(codepoint), LONG2NUM(len));
}

static VALUE encoding_spec_rb_enc_str_asciionly_p(VALUE self, VALUE str) {
if (rb_enc_str_asciionly_p(str)) {
return Qtrue;
} else {
return Qfalse;
}
}

void Init_encoding_spec(void) {
VALUE cls = rb_define_class("CApiEncodingSpecs", rb_cObject);
rb_define_method(cls, "ENC_CODERANGE_ASCIIONLY",
Expand Down Expand Up @@ -242,6 +250,7 @@ void Init_encoding_spec(void) {
rb_define_method(cls, "rb_to_encoding_index", encoding_spec_rb_to_encoding_index, 1);
rb_define_method(cls, "rb_enc_nth", encoding_spec_rb_enc_nth, 2);
rb_define_method(cls, "rb_enc_codepoint_len", encoding_spec_rb_enc_codepoint_len, 1);
rb_define_method(cls, "rb_enc_str_asciionly_p", encoding_spec_rb_enc_str_asciionly_p, 1);
}

#ifdef __cplusplus
Expand Down
6 changes: 6 additions & 0 deletions spec/ruby/optional/capi/ext/gc_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ static VALUE gc_spec_rb_gc_adjust_memory_usage(VALUE self, VALUE diff) {
return Qnil;
}

static VALUE gc_spec_rb_gc_register_mark_object(VALUE self, VALUE obj) {
rb_gc_register_mark_object(obj);
return Qnil;
}

void Init_gc_spec(void) {
VALUE cls = rb_define_class("CApiGCSpecs", rb_cObject);
registered_tagged_value = INT2NUM(10);
Expand All @@ -48,6 +53,7 @@ void Init_gc_spec(void) {
rb_define_method(cls, "rb_gc_disable", gc_spec_rb_gc_disable, 0);
rb_define_method(cls, "rb_gc", gc_spec_rb_gc, 0);
rb_define_method(cls, "rb_gc_adjust_memory_usage", gc_spec_rb_gc_adjust_memory_usage, 1);
rb_define_method(cls, "rb_gc_register_mark_object", gc_spec_rb_gc_register_mark_object, 1);
}

#ifdef __cplusplus
Expand Down
6 changes: 6 additions & 0 deletions spec/ruby/optional/capi/gc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@
}.should_not raise_error
end
end

describe "rb_gc_register_mark_object" do
it "can be called with an object" do
@f.rb_gc_register_mark_object(Object.new).should be_nil
end
end
end
4 changes: 2 additions & 2 deletions src/main/c/cext/ruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -3247,7 +3247,7 @@ long rb_str_coderange_scan_restartable(const char *s, const char *e, rb_encoding
}

int rb_enc_str_asciionly_p(VALUE str) {
rb_tr_error("rb_enc_str_asciionly_p not implemented");
return polyglot_as_boolean(RUBY_INVOKE_NO_WRAP(str, "ascii_only?"));
}

int rb_enc_unicode_p(rb_encoding *enc) {
Expand Down Expand Up @@ -4715,7 +4715,7 @@ void rb_define_virtual_variable(const char *name, VALUE (*getter)(ANYARGS), void
}

void rb_gc_register_mark_object(VALUE obj) {
rb_tr_error("rb_gc_register_mark_object not implemented");
RUBY_CEXT_INVOKE_NO_WRAP("rb_gc_register_mark_object", obj);
}

ID rb_check_id(volatile VALUE *namep) {
Expand Down

0 comments on commit d7b6b96

Please sign in to comment.