[ruby/json] Ractor-shareable JSON::Coder

https://github.com/ruby/json/commit/58d60d6b76
This commit is contained in:
Étienne Barrié 2025-11-18 11:44:06 +01:00 committed by git
parent 7ae0809c7c
commit bdca2a9975
6 changed files with 102 additions and 2 deletions

View File

@ -1630,6 +1630,7 @@ static VALUE string_config(VALUE config)
*/
static VALUE cState_indent_set(VALUE self, VALUE indent)
{
rb_check_frozen(self);
GET_STATE(self);
RB_OBJ_WRITE(self, &state->indent, string_config(indent));
return Qnil;
@ -1655,6 +1656,7 @@ static VALUE cState_space(VALUE self)
*/
static VALUE cState_space_set(VALUE self, VALUE space)
{
rb_check_frozen(self);
GET_STATE(self);
RB_OBJ_WRITE(self, &state->space, string_config(space));
return Qnil;
@ -1678,6 +1680,7 @@ static VALUE cState_space_before(VALUE self)
*/
static VALUE cState_space_before_set(VALUE self, VALUE space_before)
{
rb_check_frozen(self);
GET_STATE(self);
RB_OBJ_WRITE(self, &state->space_before, string_config(space_before));
return Qnil;
@ -1703,6 +1706,7 @@ static VALUE cState_object_nl(VALUE self)
*/
static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
{
rb_check_frozen(self);
GET_STATE(self);
RB_OBJ_WRITE(self, &state->object_nl, string_config(object_nl));
return Qnil;
@ -1726,6 +1730,7 @@ static VALUE cState_array_nl(VALUE self)
*/
static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
{
rb_check_frozen(self);
GET_STATE(self);
RB_OBJ_WRITE(self, &state->array_nl, string_config(array_nl));
return Qnil;
@ -1749,6 +1754,7 @@ static VALUE cState_as_json(VALUE self)
*/
static VALUE cState_as_json_set(VALUE self, VALUE as_json)
{
rb_check_frozen(self);
GET_STATE(self);
RB_OBJ_WRITE(self, &state->as_json, rb_convert_type(as_json, T_DATA, "Proc", "to_proc"));
return Qnil;
@ -1791,6 +1797,7 @@ static long long_config(VALUE num)
*/
static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
{
rb_check_frozen(self);
GET_STATE(self);
state->max_nesting = long_config(depth);
return Qnil;
@ -1816,6 +1823,7 @@ static VALUE cState_script_safe(VALUE self)
*/
static VALUE cState_script_safe_set(VALUE self, VALUE enable)
{
rb_check_frozen(self);
GET_STATE(self);
state->script_safe = RTEST(enable);
return Qnil;
@ -1847,6 +1855,7 @@ static VALUE cState_strict(VALUE self)
*/
static VALUE cState_strict_set(VALUE self, VALUE enable)
{
rb_check_frozen(self);
GET_STATE(self);
state->strict = RTEST(enable);
return Qnil;
@ -1871,6 +1880,7 @@ static VALUE cState_allow_nan_p(VALUE self)
*/
static VALUE cState_allow_nan_set(VALUE self, VALUE enable)
{
rb_check_frozen(self);
GET_STATE(self);
state->allow_nan = RTEST(enable);
return Qnil;
@ -1895,6 +1905,7 @@ static VALUE cState_ascii_only_p(VALUE self)
*/
static VALUE cState_ascii_only_set(VALUE self, VALUE enable)
{
rb_check_frozen(self);
GET_STATE(self);
state->ascii_only = RTEST(enable);
return Qnil;
@ -1932,6 +1943,7 @@ static VALUE cState_depth(VALUE self)
*/
static VALUE cState_depth_set(VALUE self, VALUE depth)
{
rb_check_frozen(self);
GET_STATE(self);
state->depth = long_config(depth);
return Qnil;
@ -1965,6 +1977,7 @@ static void buffer_initial_length_set(JSON_Generator_State *state, VALUE buffer_
*/
static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_length)
{
rb_check_frozen(self);
GET_STATE(self);
buffer_initial_length_set(state, buffer_initial_length);
return Qnil;
@ -2031,6 +2044,7 @@ static void configure_state(JSON_Generator_State *state, VALUE vstate, VALUE con
static VALUE cState_configure(VALUE self, VALUE opts)
{
rb_check_frozen(self);
GET_STATE(self);
configure_state(state, self, opts);
return self;

View File

@ -1048,7 +1048,7 @@ module JSON
options[:as_json] = as_json if as_json
@state = State.new(options).freeze
@parser_config = Ext::Parser::Config.new(ParserOptions.prepare(options))
@parser_config = Ext::Parser::Config.new(ParserOptions.prepare(options)).freeze
end
# call-seq:

View File

@ -1466,6 +1466,7 @@ static void parser_config_init(JSON_ParserConfig *config, VALUE opts)
*/
static VALUE cParserConfig_initialize(VALUE self, VALUE opts)
{
rb_check_frozen(self);
GET_PARSER_CONFIG;
parser_config_init(config, opts);
@ -1553,6 +1554,11 @@ static size_t JSON_ParserConfig_memsize(const void *ptr)
return sizeof(JSON_ParserConfig);
}
#ifndef HAVE_RB_EXT_RACTOR_SAFE
# undef RUBY_TYPED_FROZEN_SHAREABLE
# define RUBY_TYPED_FROZEN_SHAREABLE 0
#endif
static const rb_data_type_t JSON_ParserConfig_type = {
"JSON::Ext::Parser/ParserConfig",
{
@ -1561,7 +1567,7 @@ static const rb_data_type_t JSON_ParserConfig_type = {
JSON_ParserConfig_memsize,
},
0, 0,
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE,
};
static VALUE cJSON_parser_s_allocate(VALUE klass)

View File

@ -901,4 +901,18 @@ class JSONGeneratorTest < Test::Unit::TestCase
end
assert_equal %(detected duplicate key "foo" in #{hash.inspect}), error.message
end
def test_frozen
state = JSON::State.new.freeze
assert_raise(FrozenError) do
state.configure(max_nesting: 1)
end
setters = state.methods.grep(/\w=$/)
assert_not_empty setters
setters.each do |setter|
assert_raise(FrozenError) do
state.send(setter, 1)
end
end
end
end

View File

@ -820,6 +820,13 @@ class JSONParserTest < Test::Unit::TestCase
assert_equal [], JSON.parse("[\n#{' ' * (8 + 8 + 4 + 3)}]")
end
def test_frozen
parser_config = JSON::Parser::Config.new({}).freeze
assert_raise FrozenError do
parser_config.send(:initialize, {})
end
end
private
def assert_equal_float(expected, actual, delta = 1e-2)

View File

@ -52,4 +52,63 @@ class JSONInRactorTest < Test::Unit::TestCase
_, status = Process.waitpid2(pid)
assert_predicate status, :success?
end
def test_coder
coder = JSON::Coder.new.freeze
assert Ractor.shareable?(coder)
pid = fork do
Warning[:experimental] = false
r = Ractor.new(coder) do |coder|
json = coder.dump({
'a' => 2,
'b' => 3.141,
'c' => 'c',
'd' => [ 1, "b", 3.14 ],
'e' => { 'foo' => 'bar' },
'g' => "\"\0\037",
'h' => 1000.0,
'i' => 0.001
})
coder.load(json)
end
expected_json = JSON.parse('{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}')
actual_json = r.value
if expected_json == actual_json
exit 0
else
puts "Expected:"
puts expected_json
puts "Actual:"
puts actual_json
puts
exit 1
end
end
_, status = Process.waitpid2(pid)
assert_predicate status, :success?
end
class NonNative
def initialize(value)
@value = value
end
end
def test_coder_proc
block = Ractor.shareable_proc { |value| value.as_json }
coder = JSON::Coder.new(&block).freeze
assert Ractor.shareable?(coder)
pid = fork do
Warning[:experimental] = false
assert_equal [{}], Ractor.new(coder) { |coder|
coder.load('[{}]')
}.value
end
_, status = Process.waitpid2(pid)
assert_predicate status, :success?
end if Ractor.respond_to?(:shareable_proc)
end if defined?(Ractor) && Process.respond_to?(:fork)