mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[json] Make json Ractor safe
This commit is contained in:
parent
4c2e7f26bd
commit
14d7d1df25
4 changed files with 52 additions and 1 deletions
|
@ -619,13 +619,18 @@ static size_t State_memsize(const void *ptr)
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef HAVE_RB_EXT_RACTOR_SAFE
|
||||||
|
# undef RUBY_TYPED_FROZEN_SHAREABLE
|
||||||
|
# define RUBY_TYPED_FROZEN_SHAREABLE 0
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef NEW_TYPEDDATA_WRAPPER
|
#ifdef NEW_TYPEDDATA_WRAPPER
|
||||||
static const rb_data_type_t JSON_Generator_State_type = {
|
static const rb_data_type_t JSON_Generator_State_type = {
|
||||||
"JSON/Generator/State",
|
"JSON/Generator/State",
|
||||||
{NULL, State_free, State_memsize,},
|
{NULL, State_free, State_memsize,},
|
||||||
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
|
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
|
||||||
0, 0,
|
0, 0,
|
||||||
RUBY_TYPED_FREE_IMMEDIATELY,
|
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE,
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
@ -1497,6 +1502,10 @@ static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_l
|
||||||
*/
|
*/
|
||||||
void Init_generator(void)
|
void Init_generator(void)
|
||||||
{
|
{
|
||||||
|
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
||||||
|
rb_ext_ractor_safe(true);
|
||||||
|
#endif
|
||||||
|
|
||||||
#undef rb_intern
|
#undef rb_intern
|
||||||
rb_require("json/common");
|
rb_require("json/common");
|
||||||
|
|
||||||
|
|
|
@ -2119,6 +2119,10 @@ static VALUE cParser_source(VALUE self)
|
||||||
|
|
||||||
void Init_parser(void)
|
void Init_parser(void)
|
||||||
{
|
{
|
||||||
|
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
||||||
|
rb_ext_ractor_safe(true);
|
||||||
|
#endif
|
||||||
|
|
||||||
#undef rb_intern
|
#undef rb_intern
|
||||||
rb_require("json/common");
|
rb_require("json/common");
|
||||||
mJSON = rb_define_module("JSON");
|
mJSON = rb_define_module("JSON");
|
||||||
|
|
|
@ -879,6 +879,10 @@ static VALUE cParser_source(VALUE self)
|
||||||
|
|
||||||
void Init_parser(void)
|
void Init_parser(void)
|
||||||
{
|
{
|
||||||
|
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
||||||
|
rb_ext_ractor_safe(true);
|
||||||
|
#endif
|
||||||
|
|
||||||
#undef rb_intern
|
#undef rb_intern
|
||||||
rb_require("json/common");
|
rb_require("json/common");
|
||||||
mJSON = rb_define_module("JSON");
|
mJSON = rb_define_module("JSON");
|
||||||
|
|
34
test/json/ractor_test.rb
Normal file
34
test/json/ractor_test.rb
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
# encoding: utf-8
|
||||||
|
# frozen_string_literal: false
|
||||||
|
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class JSONInRactorTest < Test::Unit::TestCase
|
||||||
|
def setup
|
||||||
|
skip unless defined? Ractor
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_generate
|
||||||
|
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
|
||||||
|
begin;
|
||||||
|
$VERBOSE = nil
|
||||||
|
require "json"
|
||||||
|
r = Ractor.new do
|
||||||
|
json = JSON.generate({
|
||||||
|
'a' => 2,
|
||||||
|
'b' => 3.141,
|
||||||
|
'c' => 'c',
|
||||||
|
'd' => [ 1, "b", 3.14 ],
|
||||||
|
'e' => { 'foo' => 'bar' },
|
||||||
|
'g' => "\"\0\037",
|
||||||
|
'h' => 1000.0,
|
||||||
|
'i' => 0.001
|
||||||
|
})
|
||||||
|
JSON.parse(json)
|
||||||
|
end
|
||||||
|
expected_json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
|
||||||
|
'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
|
||||||
|
assert_equal(JSON.parse(expected_json), r.take)
|
||||||
|
end;
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Reference in a new issue