From 97e57afa75629b70ba2681c8c77e174ae69066e2 Mon Sep 17 00:00:00 2001 From: Peter Ohler Date: Wed, 31 Jul 2024 18:13:31 -0400 Subject: [PATCH 1/5] WIP --- ext/oj/usual.c | 19 ++++++++++++++++++- test/test_parser_usual.rb | 7 +++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ext/oj/usual.c b/ext/oj/usual.c index e6160f9a..afdd67f3 100644 --- a/ext/oj/usual.c +++ b/ext/oj/usual.c @@ -289,6 +289,12 @@ static void close_object(ojParser p) { } rb_hash_bulk_insert(d->vtail - head, head, obj); d->ktail = d->khead + c->ki; + if (0 == head - d->vhead && rb_block_given_p()) { + rb_yield(obj); + // TBD decrement vtail? + d->vtail--; + return; + } d->vtail = head; head--; *head = obj; @@ -572,7 +578,18 @@ static VALUE result(ojParser p) { Usual d = (Usual)p->ctx; if (d->vhead < d->vtail) { - return *d->vhead; + long cnt = d->vtail - d->vhead; + volatile VALUE ary; + volatile VALUE *vp; + + if (1 == cnt) { + return *d->vhead; + } + ary = rb_ary_new(); + for (vp = d->vhead; vp < d->vtail; vp++) { + rb_ary_push(ary, *vp); + } + return ary; } if (d->raise_on_empty) { rb_raise(oj_parse_error_class, "empty string"); diff --git a/test/test_parser_usual.rb b/test/test_parser_usual.rb index f9b620c2..77b49eae 100755 --- a/test/test_parser_usual.rb +++ b/test/test_parser_usual.rb @@ -114,6 +114,13 @@ def test_decimal assert_equal(Float, doc.class) end + def test_multi + p = Oj::Parser.new(:usual) + puts p.just_one + #puts p.parse('{"b":{"x":2}}') + puts p.parse('{"a":1}{"b":{"x":2}} {"c":3}') { |j| puts j } + end + def test_omit_null p = Oj::Parser.new(:usual) p.omit_null = true From 559433b66c7d72ac6233939f9ce5dbfa26e87b73 Mon Sep 17 00:00:00 2001 From: Peter Ohler Date: Thu, 1 Aug 2024 08:56:12 -0400 Subject: [PATCH 2/5] WIP --- ext/oj/usual.c | 4 +++- test/test_parser_usual.rb | 12 ++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/ext/oj/usual.c b/ext/oj/usual.c index afdd67f3..b2f36a84 100644 --- a/ext/oj/usual.c +++ b/ext/oj/usual.c @@ -289,7 +289,9 @@ static void close_object(ojParser p) { } rb_hash_bulk_insert(d->vtail - head, head, obj); d->ktail = d->khead + c->ki; - if (0 == head - d->vhead && rb_block_given_p()) { + + // printf("*** close %ld - %d\n", head - d->vhead, rb_block_given_p()); + if (1 == head - d->vhead && rb_block_given_p()) { rb_yield(obj); // TBD decrement vtail? d->vtail--; diff --git a/test/test_parser_usual.rb b/test/test_parser_usual.rb index 77b49eae..ffc7e6a1 100755 --- a/test/test_parser_usual.rb +++ b/test/test_parser_usual.rb @@ -116,9 +116,17 @@ def test_decimal def test_multi p = Oj::Parser.new(:usual) - puts p.just_one #puts p.parse('{"b":{"x":2}}') - puts p.parse('{"a":1}{"b":{"x":2}} {"c":3}') { |j| puts j } + #puts p.parse('{"a":1}{"b":{"x":2}} {"c":3}') { |j| puts j } + + reader, writer = IO.pipe + writer.write('{"a":1}') + writer.write('{"b":{"x":2}}') + writer.write('{"c":3}') + writer.close + + p.load(reader) { |data| puts data } + reader.close end def test_omit_null From a82556339252fa0a2275f7b6c31aae9faf297a2f Mon Sep 17 00:00:00 2001 From: Peter Ohler Date: Fri, 2 Aug 2024 10:22:57 -0400 Subject: [PATCH 3/5] WIP --- CHANGELOG.md | 4 ++++ ext/oj/usual.c | 11 ++++------- lib/oj/version.rb | 2 +- test/test_parser_usual.rb | 31 ++++++++++++++++++++----------- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cdc4837..61cd0326 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 3.16.5 - unreleased + +- Fixed Oj::Parser so that block procedures work correctly. + ## 3.16.4 - 2024-06-08 - Fixed Oj::Parse EOF issue on larger stream input. diff --git a/ext/oj/usual.c b/ext/oj/usual.c index b2f36a84..f25f3084 100644 --- a/ext/oj/usual.c +++ b/ext/oj/usual.c @@ -290,16 +290,13 @@ static void close_object(ojParser p) { rb_hash_bulk_insert(d->vtail - head, head, obj); d->ktail = d->khead + c->ki; - // printf("*** close %ld - %d\n", head - d->vhead, rb_block_given_p()); - if (1 == head - d->vhead && rb_block_given_p()) { - rb_yield(obj); - // TBD decrement vtail? - d->vtail--; - return; - } d->vtail = head; head--; *head = obj; + if (1 == d->vtail - d->vhead && rb_block_given_p()) { + d->vtail = d->vhead; + rb_yield(obj); + } } static void close_object_class(ojParser p) { diff --git a/lib/oj/version.rb b/lib/oj/version.rb index 15bf90ba..6993dc25 100644 --- a/lib/oj/version.rb +++ b/lib/oj/version.rb @@ -1,4 +1,4 @@ module Oj # Current version of the module. - VERSION = '3.16.4' + VERSION = '3.16.5b' end diff --git a/test/test_parser_usual.rb b/test/test_parser_usual.rb index ffc7e6a1..e327e049 100755 --- a/test/test_parser_usual.rb +++ b/test/test_parser_usual.rb @@ -114,19 +114,28 @@ def test_decimal assert_equal(Float, doc.class) end - def test_multi + def test_multi_parse p = Oj::Parser.new(:usual) - #puts p.parse('{"b":{"x":2}}') - #puts p.parse('{"a":1}{"b":{"x":2}} {"c":3}') { |j| puts j } - - reader, writer = IO.pipe - writer.write('{"a":1}') - writer.write('{"b":{"x":2}}') - writer.write('{"c":3}') - writer.close + out = [] + p.parse('{"a":1}{"b":{"x":2}} {"c":3}') { |j| out.push(j) } + assert_equal([{'a'=>1}, {'b'=>{'x'=>2}},{'c'=>3}], out) + end - p.load(reader) { |data| puts data } - reader.close + def test_multi_load + p = Oj::Parser.new(:usual) + out = [] + r, w = IO.pipe + thread = Thread.new do + ['{"a":1}', '{"b":{"x"', ':2}}{"c":', '3}'].each { |seg| + w.write(seg) + sleep(0.1) + } + w.close + end + p.load(r) { |j| out.push(j) } + r.close + thread.join + assert_equal([{'a'=>1}, {'b'=>{'x'=>2}},{'c'=>3}], out) end def test_omit_null From eee4e4ea71e34ae438d7b38666662eaa52763345 Mon Sep 17 00:00:00 2001 From: Peter Ohler Date: Wed, 7 Aug 2024 19:00:10 -0400 Subject: [PATCH 4/5] Release prep --- CHANGELOG.md | 2 +- lib/oj/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61cd0326..f601c331 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # CHANGELOG -## 3.16.5 - unreleased +## 3.16.5 - 2024-07-07 - Fixed Oj::Parser so that block procedures work correctly. diff --git a/lib/oj/version.rb b/lib/oj/version.rb index 6993dc25..4ffbcdec 100644 --- a/lib/oj/version.rb +++ b/lib/oj/version.rb @@ -1,4 +1,4 @@ module Oj # Current version of the module. - VERSION = '3.16.5b' + VERSION = '3.16.5' end From 1c8ba7e06880f1486aede48c83f65b664a55f059 Mon Sep 17 00:00:00 2001 From: Peter Ohler Date: Wed, 7 Aug 2024 19:01:03 -0400 Subject: [PATCH 5/5] clang format --- ext/oj/usual.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/ext/oj/usual.c b/ext/oj/usual.c index f25f3084..d6296e0b 100644 --- a/ext/oj/usual.c +++ b/ext/oj/usual.c @@ -293,10 +293,10 @@ static void close_object(ojParser p) { d->vtail = head; head--; *head = obj; - if (1 == d->vtail - d->vhead && rb_block_given_p()) { - d->vtail = d->vhead; - rb_yield(obj); - } + if (1 == d->vtail - d->vhead && rb_block_given_p()) { + d->vtail = d->vhead; + rb_yield(obj); + } } static void close_object_class(ojParser p) { @@ -577,17 +577,17 @@ static VALUE result(ojParser p) { Usual d = (Usual)p->ctx; if (d->vhead < d->vtail) { - long cnt = d->vtail - d->vhead; - volatile VALUE ary; - volatile VALUE *vp; - - if (1 == cnt) { - return *d->vhead; - } - ary = rb_ary_new(); - for (vp = d->vhead; vp < d->vtail; vp++) { - rb_ary_push(ary, *vp); - } + long cnt = d->vtail - d->vhead; + volatile VALUE ary; + volatile VALUE *vp; + + if (1 == cnt) { + return *d->vhead; + } + ary = rb_ary_new(); + for (vp = d->vhead; vp < d->vtail; vp++) { + rb_ary_push(ary, *vp); + } return ary; } if (d->raise_on_empty) {