Skip to content
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
5 changes: 2 additions & 3 deletions ext/json/ext/parser/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# frozen_string_literal: true
require 'mkmf'

have_func("rb_enc_raise", "ruby.h")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All rubies have this now, I haven't check exactly when it was added, but I suspect this was for pre-1.9 rubies.

have_func("rb_enc_interned_str", "ruby.h")

have_func("rb_enc_interned_str", "ruby.h") # RUBY_VERSION >= 3.0
have_func("rb_gc_mark_locations") # Missing on TruffleRuby
append_cflags("-std=c99")

create_makefile 'json/ext/parser'
15 changes: 15 additions & 0 deletions ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2399,6 +2399,19 @@ case 9:
}
}

#ifndef HAVE_RB_GC_MARK_LOCATIONS
// For TruffleRuby
void rb_gc_mark_locations(const VALUE *start, const VALUE *end)
{
VALUE *value = start;

while (value < end) {
rb_gc_mark(*value);
value++;
}
}
#endif

static void JSON_mark(void *ptr)
{
JSON_Parser *json = ptr;
Expand All @@ -2408,6 +2421,8 @@ static void JSON_mark(void *ptr)
rb_gc_mark(json->array_class);
rb_gc_mark(json->decimal_class);
rb_gc_mark(json->match_string);
const VALUE *name_cache_entries = &json->name_cache.entries[0];
rb_gc_mark_locations(name_cache_entries, name_cache_entries + json->name_cache.length);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dyld[4099]: missing symbol called

cc @eregon looks like rb_gc_mark_locations is also missing on Truffle?

}

static void JSON_free(void *ptr)
Expand Down
15 changes: 15 additions & 0 deletions ext/json/ext/parser/parser.rl
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,19 @@ static VALUE cParser_m_parse(VALUE klass, VALUE source, VALUE opts)
}
}

#ifndef HAVE_RB_GC_MARK_LOCATIONS
// For TruffleRuby
void rb_gc_mark_locations(const VALUE *start, const VALUE *end)
{
VALUE *value = start;

while (value < end) {
rb_gc_mark(*value);
value++;
}
}
#endif

static void JSON_mark(void *ptr)
{
JSON_Parser *json = ptr;
Expand All @@ -1028,6 +1041,8 @@ static void JSON_mark(void *ptr)
rb_gc_mark(json->array_class);
rb_gc_mark(json->decimal_class);
rb_gc_mark(json->match_string);
const VALUE *name_cache_entries = &json->name_cache.entries[0];
rb_gc_mark_locations(name_cache_entries, name_cache_entries + json->name_cache.length);
}

static void JSON_free(void *ptr)
Expand Down
20 changes: 20 additions & 0 deletions test/json/json_ext_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ def test_error_messages
assert_equal "unexpected token at 'NaN'", ex.message
end

if GC.respond_to?(:stress=)
def test_gc_stress_parser_new
payload = JSON.dump([{ foo: 1, bar: 2, baz: 3, egg: { spam: 4 } }] * 10)

previous_stress = GC.stress
JSON::Parser.new(payload).parse
ensure
GC.stress = previous_stress
end

def test_gc_stress
payload = JSON.dump([{ foo: 1, bar: 2, baz: 3, egg: { spam: 4 } }] * 10)

previous_stress = GC.stress
JSON.parse(payload)
ensure
GC.stress = previous_stress
end
end

def parse(json)
JSON::Ext::Parser.new(json).parse
end
Expand Down
18 changes: 18 additions & 0 deletions test/json/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@
rescue LoadError
end

if GC.respond_to?(:verify_compaction_references)
# This method was added in Ruby 3.0.0. Calling it this way asks the GC to
# move objects around, helping to find object movement bugs.
begin
GC.verify_compaction_references(double_heap: true, toward: :empty)
rescue NotImplementedError
# Some platforms don't support compaction
end
end

if GC.respond_to?(:auto_compact=)
begin
GC.auto_compact = true
rescue NotImplementedError
# Some platforms don't support compaction
end
end

unless defined?(Test::Unit::CoreAssertions)
require "core_assertions"
Test::Unit::TestCase.include Test::Unit::CoreAssertions
Expand Down