Skip to content

Commit e285489

Browse files
committed
json_pure: fix ractor compatibility
This actually never worked, because the test was always testing the ext version from the stdlib, never the pure version nor the current ext version.
1 parent 7fd3531 commit e285489

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

lib/json/pure/parser.rb

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,25 +148,26 @@ def convert_encoding(source)
148148
end
149149

150150
# Unescape characters in strings.
151-
UNESCAPE_MAP = Hash.new { |h, k| h[k] = k.chr }
152-
UNESCAPE_MAP.update({
153-
?" => '"',
154-
?\\ => '\\',
155-
?/ => '/',
156-
?b => "\b",
157-
?f => "\f",
158-
?n => "\n",
159-
?r => "\r",
160-
?t => "\t",
161-
?u => nil,
162-
})
151+
# UNESCAPE_MAP = Hash.new { |h, k| puts; p [:k, k]; h[k] = k.chr }
152+
UNESCAPE_MAP = {
153+
'"' => '"',
154+
'\\' => '\\',
155+
'/' => '/',
156+
'b' => "\b",
157+
'f' => "\f",
158+
'n' => "\n",
159+
'r' => "\r",
160+
't' => "\t",
161+
'u' => nil,
162+
}.freeze
163163

164164
STR_UMINUS = ''.respond_to?(:-@)
165165
def parse_string
166166
if scan(STRING)
167167
return '' if self[1].empty?
168-
string = self[1].gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do |c|
169-
if u = UNESCAPE_MAP[$&[1]]
168+
string = self[1].gsub(%r{(?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff])}n) do |c|
169+
k = $&[1]
170+
if u = UNESCAPE_MAP.fetch(k) { k.chr }
170171
u
171172
else # \uXXXX
172173
bytes = ''.b

test/json/ractor_test.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99

1010
class JSONInRactorTest < Test::Unit::TestCase
1111
def test_generate
12-
assert_separately(%w[-rjson -Ilib -Iext], "#{<<~"begin;"}\n#{<<~'end;'}", ignore_stderr: true)
13-
begin;
14-
$VERBOSE = nil
15-
require "json"
12+
pid = fork do
1613
r = Ractor.new do
1714
json = JSON.generate({
1815
'a' => 2,
@@ -26,9 +23,22 @@ def test_generate
2623
})
2724
JSON.parse(json)
2825
end
29-
expected_json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
30-
'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
31-
assert_equal(JSON.parse(expected_json), r.take)
32-
end;
26+
expected_json = JSON.parse('{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
27+
'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}')
28+
actual_json = r.take
29+
30+
if expected_json == actual_json
31+
exit 0
32+
else
33+
puts "Expected:"
34+
puts expected_json
35+
puts "Acutual:"
36+
puts actual_json
37+
puts
38+
exit 1
39+
end
40+
end
41+
_, status = Process.waitpid2(pid)
42+
assert_predicate status, :success?
3343
end
34-
end if defined?(Ractor)
44+
end if defined?(Ractor) && Process.respond_to?(:fork)

0 commit comments

Comments
 (0)