Skip to content

Add new C APIs used by the new json release #1856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Compatibility:
* Joni has been updated from 2.1.25 to 2.1.30.
* Implemented `Method#<<` and `Method#>>` (#1821).
* The `.bundle` file extension is now used for C extensions on macOS (#1819, #1837).
* 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