json 2.19.9 → 2.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGES.md +19 -1
- data/LEGAL +3 -3
- data/README.md +5 -7
- data/ext/json/ext/fbuffer/fbuffer.h +2 -2
- data/ext/json/ext/generator/extconf.rb +1 -0
- data/ext/json/ext/generator/generator.c +76 -3
- data/ext/json/ext/json.h +67 -0
- data/ext/json/ext/parser/extconf.rb +33 -0
- data/ext/json/ext/parser/parser.c +1486 -326
- data/ext/json/ext/vendor/fast_float_parser.h +814 -0
- data/lib/json/common.rb +9 -0
- data/lib/json/ext/generator/state.rb +1 -0
- data/lib/json/ext.rb +26 -0
- data/lib/json/truffle_ruby/generator.rb +36 -1
- data/lib/json/version.rb +1 -1
- data/lib/json.rb +21 -3
- metadata +63 -65
- data/ext/json/ext/vendor/ryu.h +0 -819
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 778c5ab5faa1a727c1c453ef34351537fd77dbb9a2f176abc15f6861391baab9
|
|
4
|
+
data.tar.gz: 37f10916f3608596b4665564f61ee872bd647617048c0d593a4d2c917acc87df
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 36af0bdf70e46b374bb7d0ad209e946fffa367f2316a279249295aff233c4b187d69fa65753db29ffb20639e828896fd18e33e6060ecb0908f3a85b170c53443
|
|
7
|
+
data.tar.gz: d148e9ef5ea0b428a562d422acb9357c8a061dada945eacb37b2c97e10c9c050253624881b16a4de547a7705011493db83a442afe230b828f9935a522c86c08d
|
data/CHANGES.md
CHANGED
|
@@ -2,10 +2,28 @@
|
|
|
2
2
|
|
|
3
3
|
### Unreleased
|
|
4
4
|
|
|
5
|
+
### 2026-07-12 (2.21.0)
|
|
6
|
+
|
|
7
|
+
* `JSON.generate` now accept a `sort_keys` option, which takes either a boolean or a block.
|
|
8
|
+
* Added `#empty?` and `#partial_value?` methods on `JSON::ResumableParser`.
|
|
9
|
+
* Numerous correctness and performance fixes for `JSON::ResumableParser`.
|
|
10
|
+
* Avoid triggering Ruby's `float out of range` warning when parsing out of range numbers.
|
|
11
|
+
* Declare C types with Ruby 4.1 `RUBY_TYPED_THREAD_SAFE_FREE`.
|
|
12
|
+
|
|
13
|
+
### 2026-06-23 (2.20.0)
|
|
14
|
+
|
|
15
|
+
* Both C and Java parsers are no longer recursive, so parsing very deep documents with `max_nesting: false` will no longer
|
|
16
|
+
result in `SystemStackError stack level too deep` errors.
|
|
17
|
+
* The `:max_nesting` option still defaults to `100`.
|
|
18
|
+
* Optimized floating point number parsing further by replacing the ryu algorithm by a port of Eisel-Lemire Fast Float.
|
|
19
|
+
* Added `JSON::ResumableParser` to parse streams of JSON documents. Not yet available on JRuby.
|
|
20
|
+
* Deprecate default support of JavaScript comments in the parser and add `allow_comments: true` parsing option.
|
|
21
|
+
* Integrate with Ruby 4.1 `ruby_sized_xfree`.
|
|
22
|
+
|
|
5
23
|
### 2026-06-11 (2.19.9)
|
|
6
24
|
|
|
7
25
|
* Fix buffer overflow that could lead to a crash when writing JSON directly into an IO
|
|
8
|
-
with `JSON.generate(object, io)`. [CVE-
|
|
26
|
+
with `JSON.generate(object, io)`. [CVE-2026-54696].
|
|
9
27
|
|
|
10
28
|
### 2026-06-03 (2.19.8)
|
|
11
29
|
|
data/LEGAL
CHANGED
|
@@ -15,6 +15,6 @@ ext/json/ext/vendor/jeaiii-ltoa.h::
|
|
|
15
15
|
This file is adapted from https://github.com/jeaiii/itoa
|
|
16
16
|
It is licensed under the MIT License
|
|
17
17
|
|
|
18
|
-
ext/json/ext/vendor/
|
|
19
|
-
This file is adapted from the
|
|
20
|
-
It is
|
|
18
|
+
ext/json/ext/vendor/fast_float_parser.h::
|
|
19
|
+
This file is adapted from the Fast Float C++ library by The fast_float authors https://github.com/fastfloat/fast_float
|
|
20
|
+
It is licensed under the MIT License
|
data/README.md
CHANGED
|
@@ -85,7 +85,7 @@ Both of these behavior can be disabled using the `strict: true` option:
|
|
|
85
85
|
|
|
86
86
|
```ruby
|
|
87
87
|
JSON.generate(Object.new, strict: true) # => Object not allowed in JSON (JSON::GeneratorError)
|
|
88
|
-
JSON.generate(Position.new(1, 2)) # => Position not allowed in JSON (JSON::GeneratorError)
|
|
88
|
+
JSON.generate(Position.new(1, 2), strict: true) # => Position not allowed in JSON (JSON::GeneratorError)
|
|
89
89
|
```
|
|
90
90
|
|
|
91
91
|
## JSON::Coder
|
|
@@ -117,13 +117,13 @@ It is also called for objects that do have a JSON equivalent, but are used as Ha
|
|
|
117
117
|
as well as for strings that aren't valid UTF-8:
|
|
118
118
|
|
|
119
119
|
```ruby
|
|
120
|
-
coder = JSON::
|
|
120
|
+
coder = JSON::Coder.new do |object, is_object_key|
|
|
121
121
|
case object
|
|
122
122
|
when String
|
|
123
|
-
if !
|
|
124
|
-
Base64.encode64(
|
|
123
|
+
if !object.valid_encoding? || object.encoding != Encoding::UTF_8
|
|
124
|
+
Base64.encode64(object)
|
|
125
125
|
else
|
|
126
|
-
|
|
126
|
+
object
|
|
127
127
|
end
|
|
128
128
|
else
|
|
129
129
|
object
|
|
@@ -306,5 +306,3 @@ The latest version of this library can be downloaded at
|
|
|
306
306
|
Online Documentation should be located at
|
|
307
307
|
|
|
308
308
|
* https://www.rubydoc.info/gems/json
|
|
309
|
-
|
|
310
|
-
[Ragel]: http://www.colm.net/open-source/ragel/
|
|
@@ -68,7 +68,7 @@ static inline void fbuffer_consumed(FBuffer *fb, size_t consumed)
|
|
|
68
68
|
static void fbuffer_free(FBuffer *fb)
|
|
69
69
|
{
|
|
70
70
|
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
|
|
71
|
-
|
|
71
|
+
JSON_SIZED_FREE_N(fb->ptr, fb->capa);
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
|
|
@@ -92,7 +92,7 @@ static void fbuffer_realloc(FBuffer *fb, size_t new_capa)
|
|
|
92
92
|
fb->type = FBUFFER_HEAP_ALLOCATED;
|
|
93
93
|
MEMCPY(fb->ptr, old_buffer, char, fb->len);
|
|
94
94
|
} else {
|
|
95
|
-
|
|
95
|
+
JSON_SIZED_REALLOC_N(fb->ptr, char, new_capa, fb->capa);
|
|
96
96
|
}
|
|
97
97
|
fb->capa = new_capa;
|
|
98
98
|
}
|
|
@@ -6,6 +6,7 @@ if RUBY_ENGINE == 'truffleruby'
|
|
|
6
6
|
else
|
|
7
7
|
append_cflags("-std=c99")
|
|
8
8
|
have_const("RUBY_TYPED_EMBEDDABLE", "ruby.h") # RUBY_VERSION >= 3.3
|
|
9
|
+
have_func("ruby_xfree_sized", "ruby.h") # RUBY_VERSION >= 4.1
|
|
9
10
|
|
|
10
11
|
$defs << "-DJSON_GENERATOR"
|
|
11
12
|
$defs << "-DJSON_DEBUG" if ENV.fetch("JSON_DEBUG", "0") != "0"
|
|
@@ -34,13 +34,14 @@ typedef struct JSON_Generator_StateStruct {
|
|
|
34
34
|
bool ascii_only;
|
|
35
35
|
bool script_safe;
|
|
36
36
|
bool strict;
|
|
37
|
+
VALUE sort_keys;
|
|
37
38
|
} JSON_Generator_State;
|
|
38
39
|
|
|
39
|
-
static VALUE mJSON, cState, cFragment, eGeneratorError, eNestingError, Encoding_UTF_8;
|
|
40
|
+
static VALUE mJSON, cState, cFragment, eGeneratorError, eNestingError, Encoding_UTF_8, default_sort_keys_proc;
|
|
40
41
|
|
|
41
42
|
static ID i_to_s, i_to_json, i_new, i_encode;
|
|
42
43
|
static VALUE sym_indent, sym_space, sym_space_before, sym_object_nl, sym_array_nl, sym_max_nesting, sym_allow_nan, sym_allow_duplicate_key,
|
|
43
|
-
sym_ascii_only, sym_depth, sym_buffer_initial_length, sym_script_safe, sym_escape_slash, sym_strict, sym_as_json;
|
|
44
|
+
sym_ascii_only, sym_depth, sym_buffer_initial_length, sym_script_safe, sym_escape_slash, sym_strict, sym_as_json, sym_sort_keys;
|
|
44
45
|
|
|
45
46
|
|
|
46
47
|
#define GET_STATE_TO(self, state) \
|
|
@@ -709,6 +710,7 @@ static void State_mark(void *ptr)
|
|
|
709
710
|
rb_gc_mark_movable(state->object_nl);
|
|
710
711
|
rb_gc_mark_movable(state->array_nl);
|
|
711
712
|
rb_gc_mark_movable(state->as_json);
|
|
713
|
+
rb_gc_mark_movable(state->sort_keys);
|
|
712
714
|
}
|
|
713
715
|
|
|
714
716
|
static void State_compact(void *ptr)
|
|
@@ -720,11 +722,16 @@ static void State_compact(void *ptr)
|
|
|
720
722
|
state->object_nl = rb_gc_location(state->object_nl);
|
|
721
723
|
state->array_nl = rb_gc_location(state->array_nl);
|
|
722
724
|
state->as_json = rb_gc_location(state->as_json);
|
|
725
|
+
state->sort_keys = rb_gc_location(state->sort_keys);
|
|
723
726
|
}
|
|
724
727
|
|
|
725
728
|
static size_t State_memsize(const void *ptr)
|
|
726
729
|
{
|
|
730
|
+
#ifdef HAVE_RUBY_TYPED_EMBEDDABLE
|
|
731
|
+
return 0;
|
|
732
|
+
#else
|
|
727
733
|
return sizeof(JSON_Generator_State);
|
|
734
|
+
#endif
|
|
728
735
|
}
|
|
729
736
|
|
|
730
737
|
static const rb_data_type_t JSON_Generator_State_type = {
|
|
@@ -735,7 +742,7 @@ static const rb_data_type_t JSON_Generator_State_type = {
|
|
|
735
742
|
.dsize = State_memsize,
|
|
736
743
|
.dcompact = State_compact,
|
|
737
744
|
},
|
|
738
|
-
.flags = RUBY_TYPED_WB_PROTECTED |
|
|
745
|
+
.flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_EMBEDDABLE,
|
|
739
746
|
};
|
|
740
747
|
|
|
741
748
|
static void state_init(JSON_Generator_State *state)
|
|
@@ -765,6 +772,7 @@ static void vstate_spill(struct generate_json_data *data)
|
|
|
765
772
|
RB_OBJ_WRITTEN(vstate, Qundef, state->object_nl);
|
|
766
773
|
RB_OBJ_WRITTEN(vstate, Qundef, state->array_nl);
|
|
767
774
|
RB_OBJ_WRITTEN(vstate, Qundef, state->as_json);
|
|
775
|
+
RB_OBJ_WRITTEN(vstate, Qundef, state->sort_keys);
|
|
768
776
|
}
|
|
769
777
|
|
|
770
778
|
static inline VALUE json_call_to_json(struct generate_json_data *data, VALUE obj)
|
|
@@ -1046,6 +1054,11 @@ static inline long increase_depth(struct generate_json_data *data)
|
|
|
1046
1054
|
|
|
1047
1055
|
static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
|
|
1048
1056
|
{
|
|
1057
|
+
if (RB_UNLIKELY(data->state->sort_keys)) {
|
|
1058
|
+
obj = rb_proc_call_with_block(data->state->sort_keys, 1, &obj, Qnil);
|
|
1059
|
+
Check_Type(obj, T_HASH);
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1049
1062
|
long depth = increase_depth(data);
|
|
1050
1063
|
|
|
1051
1064
|
if (RHASH_SIZE(obj) == 0) {
|
|
@@ -1372,6 +1385,7 @@ static VALUE cState_init_copy(VALUE obj, VALUE orig)
|
|
|
1372
1385
|
RB_OBJ_WRITTEN(obj, Qundef, objState->object_nl);
|
|
1373
1386
|
RB_OBJ_WRITTEN(obj, Qundef, objState->array_nl);
|
|
1374
1387
|
RB_OBJ_WRITTEN(obj, Qundef, objState->as_json);
|
|
1388
|
+
RB_OBJ_WRITTEN(obj, Qundef, objState->sort_keys);
|
|
1375
1389
|
|
|
1376
1390
|
return obj;
|
|
1377
1391
|
}
|
|
@@ -1718,6 +1732,55 @@ static VALUE cState_ascii_only_set(VALUE self, VALUE enable)
|
|
|
1718
1732
|
return Qnil;
|
|
1719
1733
|
}
|
|
1720
1734
|
|
|
1735
|
+
static VALUE cState_set_default_sort_keys_proc(VALUE self, VALUE proc)
|
|
1736
|
+
{
|
|
1737
|
+
if (!rb_obj_is_proc(proc)) {
|
|
1738
|
+
rb_raise(rb_eTypeError, "sort_key_proc must be a Proc");
|
|
1739
|
+
}
|
|
1740
|
+
return default_sort_keys_proc = proc;
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1743
|
+
static VALUE normalize_sort_keys(VALUE value)
|
|
1744
|
+
{
|
|
1745
|
+
if (rb_obj_is_proc(value)) {
|
|
1746
|
+
return value;
|
|
1747
|
+
} else if (value == Qtrue) {
|
|
1748
|
+
return default_sort_keys_proc;
|
|
1749
|
+
} else if (RTEST(value)) {
|
|
1750
|
+
rb_raise(rb_eTypeError, "The `sort_keys` argument must be a boolean or a Proc");
|
|
1751
|
+
} else {
|
|
1752
|
+
return Qfalse;
|
|
1753
|
+
}
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
/*
|
|
1757
|
+
* call-seq: sort_keys
|
|
1758
|
+
*
|
|
1759
|
+
* Get the value of sort_keys.
|
|
1760
|
+
*/
|
|
1761
|
+
static VALUE cState_sort_keys_p(VALUE self)
|
|
1762
|
+
{
|
|
1763
|
+
GET_STATE(self);
|
|
1764
|
+
return state->sort_keys;
|
|
1765
|
+
}
|
|
1766
|
+
|
|
1767
|
+
/*
|
|
1768
|
+
* call-seq: sort_keys=(value)
|
|
1769
|
+
*
|
|
1770
|
+
* value is a boolean or a proc. If the value is the boolean true, object keys
|
|
1771
|
+
* will be sorted lexicographically in ascending order.
|
|
1772
|
+
*
|
|
1773
|
+
* If the value is a proc, it receives the entire Hash and must return a Hash
|
|
1774
|
+
* with its pairs in the desired order, allowing for arbitrary sorting.
|
|
1775
|
+
*/
|
|
1776
|
+
static VALUE cState_sort_keys_set(VALUE self, VALUE value)
|
|
1777
|
+
{
|
|
1778
|
+
rb_check_frozen(self);
|
|
1779
|
+
GET_STATE(self);
|
|
1780
|
+
RB_OBJ_WRITE(self, &state->sort_keys, normalize_sort_keys(value));
|
|
1781
|
+
return Qnil;
|
|
1782
|
+
}
|
|
1783
|
+
|
|
1721
1784
|
static VALUE cState_allow_duplicate_key_p(VALUE self)
|
|
1722
1785
|
{
|
|
1723
1786
|
GET_STATE(self);
|
|
@@ -1828,6 +1891,9 @@ static int configure_state_i(VALUE key, VALUE val, VALUE _arg)
|
|
|
1828
1891
|
state->as_json_single_arg = proc && rb_proc_arity(proc) == 1;
|
|
1829
1892
|
state_write_value(data, &state->as_json, proc);
|
|
1830
1893
|
}
|
|
1894
|
+
else if (key == sym_sort_keys) {
|
|
1895
|
+
state_write_value(data, &state->sort_keys, normalize_sort_keys(val));
|
|
1896
|
+
}
|
|
1831
1897
|
return ST_CONTINUE;
|
|
1832
1898
|
}
|
|
1833
1899
|
|
|
@@ -1905,6 +1971,8 @@ void Init_generator(void)
|
|
|
1905
1971
|
VALUE mExt = rb_define_module_under(mJSON, "Ext");
|
|
1906
1972
|
VALUE mGenerator = rb_define_module_under(mExt, "Generator");
|
|
1907
1973
|
|
|
1974
|
+
rb_global_variable(&default_sort_keys_proc);
|
|
1975
|
+
|
|
1908
1976
|
rb_global_variable(&eGeneratorError);
|
|
1909
1977
|
eGeneratorError = rb_path2class("JSON::GeneratorError");
|
|
1910
1978
|
|
|
@@ -1914,6 +1982,8 @@ void Init_generator(void)
|
|
|
1914
1982
|
cState = rb_define_class_under(mGenerator, "State", rb_cObject);
|
|
1915
1983
|
rb_define_alloc_func(cState, cState_s_allocate);
|
|
1916
1984
|
rb_define_singleton_method(cState, "from_state", cState_from_state_s, 1);
|
|
1985
|
+
rb_define_singleton_method(cState, "default_sort_keys_proc=", cState_set_default_sort_keys_proc, 1);
|
|
1986
|
+
|
|
1917
1987
|
rb_define_method(cState, "initialize", cState_initialize, -1);
|
|
1918
1988
|
rb_define_alias(cState, "initialize", "initialize"); // avoid method redefinition warnings
|
|
1919
1989
|
rb_define_private_method(cState, "_configure", cState_configure, 1);
|
|
@@ -1953,6 +2023,8 @@ void Init_generator(void)
|
|
|
1953
2023
|
rb_define_method(cState, "buffer_initial_length=", cState_buffer_initial_length_set, 1);
|
|
1954
2024
|
rb_define_method(cState, "generate", cState_generate, -1);
|
|
1955
2025
|
rb_define_method(cState, "_generate_no_fallback", cState_generate_no_fallback, -1);
|
|
2026
|
+
rb_define_method(cState, "sort_keys", cState_sort_keys_p, 0);
|
|
2027
|
+
rb_define_method(cState, "sort_keys=", cState_sort_keys_set, 1);
|
|
1956
2028
|
|
|
1957
2029
|
rb_define_private_method(cState, "allow_duplicate_key?", cState_allow_duplicate_key_p, 0);
|
|
1958
2030
|
|
|
@@ -1982,6 +2054,7 @@ void Init_generator(void)
|
|
|
1982
2054
|
sym_strict = ID2SYM(rb_intern("strict"));
|
|
1983
2055
|
sym_as_json = ID2SYM(rb_intern("as_json"));
|
|
1984
2056
|
sym_allow_duplicate_key = ID2SYM(rb_intern("allow_duplicate_key"));
|
|
2057
|
+
sym_sort_keys = ID2SYM(rb_intern("sort_keys"));
|
|
1985
2058
|
|
|
1986
2059
|
usascii_encindex = rb_usascii_encindex();
|
|
1987
2060
|
utf8_encindex = rb_utf8_encindex();
|
data/ext/json/ext/json.h
CHANGED
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
|
|
12
12
|
#if defined(RUBY_DEBUG) && RUBY_DEBUG
|
|
13
13
|
# define JSON_ASSERT RUBY_ASSERT
|
|
14
|
+
# ifndef JSON_DEBUG
|
|
15
|
+
# define JSON_DEBUG 1
|
|
16
|
+
# endif
|
|
14
17
|
#else
|
|
15
18
|
# ifdef JSON_DEBUG
|
|
16
19
|
# include <assert.h>
|
|
@@ -20,8 +23,22 @@
|
|
|
20
23
|
# endif
|
|
21
24
|
#endif
|
|
22
25
|
|
|
26
|
+
#ifdef JSON_DEBUG
|
|
27
|
+
# define JSON_UNREACHABLE_RETURN(val) rb_bug("Unreachable")
|
|
28
|
+
#else
|
|
29
|
+
# define JSON_UNREACHABLE_RETURN UNREACHABLE_RETURN
|
|
30
|
+
#endif
|
|
31
|
+
|
|
23
32
|
/* shims */
|
|
24
33
|
|
|
34
|
+
#ifndef RUBY_TYPED_THREAD_SAFE_FREE
|
|
35
|
+
#define RUBY_TYPED_THREAD_SAFE_FREE RUBY_TYPED_FREE_IMMEDIATELY
|
|
36
|
+
#endif
|
|
37
|
+
|
|
38
|
+
#ifndef UNDEF_P
|
|
39
|
+
#define UNDEF_P(val) (val == Qundef)
|
|
40
|
+
#endif
|
|
41
|
+
|
|
25
42
|
#if SIZEOF_UINT64_T == SIZEOF_LONG_LONG
|
|
26
43
|
# define INT64T2NUM(x) LL2NUM(x)
|
|
27
44
|
# define UINT64T2NUM(x) ULL2NUM(x)
|
|
@@ -49,6 +66,24 @@ typedef unsigned char _Bool;
|
|
|
49
66
|
#endif
|
|
50
67
|
#endif
|
|
51
68
|
|
|
69
|
+
#ifndef HAVE_RUBY_XFREE_SIZED
|
|
70
|
+
static inline void ruby_xfree_sized(void *ptr, size_t oldsize)
|
|
71
|
+
{
|
|
72
|
+
ruby_xfree(ptr);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static inline void *ruby_xrealloc2_sized(void *ptr, size_t new_elems, size_t elem_size, size_t old_elems)
|
|
76
|
+
{
|
|
77
|
+
return ruby_xrealloc2(ptr, new_elems, elem_size);
|
|
78
|
+
}
|
|
79
|
+
#endif
|
|
80
|
+
|
|
81
|
+
# define JSON_SIZED_REALLOC_N(v, T, m, n) \
|
|
82
|
+
((v) = (T *)ruby_xrealloc2_sized((void *)(v), (m), sizeof(T), (n)))
|
|
83
|
+
|
|
84
|
+
# define JSON_SIZED_FREE(v) ruby_xfree_sized((void *)(v), sizeof(*(v)))
|
|
85
|
+
# define JSON_SIZED_FREE_N(v, n) ruby_xfree_sized((void *)(v), sizeof(*(v)) * (n))
|
|
86
|
+
|
|
52
87
|
#ifndef HAVE_RB_EXT_RACTOR_SAFE
|
|
53
88
|
# undef RUBY_TYPED_FROZEN_SHAREABLE
|
|
54
89
|
# define RUBY_TYPED_FROZEN_SHAREABLE 0
|
|
@@ -113,4 +148,36 @@ typedef unsigned char _Bool;
|
|
|
113
148
|
#define JSON_CPU_LITTLE_ENDIAN_64BITS 0
|
|
114
149
|
#endif
|
|
115
150
|
|
|
151
|
+
#ifdef JSON_TRUFFLERUBY_RB_CATCH_BUG
|
|
152
|
+
|
|
153
|
+
#undef RB_BLOCK_CALL_FUNC_ARGLIST
|
|
154
|
+
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, func_args) VALUE func_args
|
|
155
|
+
|
|
156
|
+
NORETURN(static inline) void json_rb_throw_obj(VALUE tag, VALUE obj)
|
|
157
|
+
{
|
|
158
|
+
VALUE exc = rb_exc_new_str(rb_eException, rb_utf8_str_new_cstr("throw_workaround"));
|
|
159
|
+
rb_ivar_set(exc, rb_intern("@throw_tag"), tag);
|
|
160
|
+
rb_ivar_set(exc, rb_intern("@throw_obj"), obj);
|
|
161
|
+
rb_exc_raise(exc);
|
|
162
|
+
}
|
|
163
|
+
#define rb_throw_obj json_rb_throw_obj
|
|
164
|
+
|
|
165
|
+
static inline VALUE json_rb_catch_obj(VALUE tag, VALUE (*func)(VALUE args), VALUE func_args)
|
|
166
|
+
{
|
|
167
|
+
int status;
|
|
168
|
+
VALUE result = rb_protect(func, func_args, &status);
|
|
169
|
+
if (status) {
|
|
170
|
+
VALUE exc = rb_errinfo();
|
|
171
|
+
if (tag == rb_ivar_get(exc, rb_intern("@throw_tag"))) {
|
|
172
|
+
rb_set_errinfo(Qnil);
|
|
173
|
+
return rb_ivar_get(exc, rb_intern("@throw_obj"));
|
|
174
|
+
}
|
|
175
|
+
rb_jump_tag(status);
|
|
176
|
+
}
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
#define rb_catch_obj json_rb_catch_obj
|
|
180
|
+
|
|
181
|
+
#endif // JSON_TRUFFLERUBY_RB_CATCH_BUG
|
|
182
|
+
|
|
116
183
|
#endif // _JSON_H_
|
|
@@ -2,10 +2,43 @@
|
|
|
2
2
|
require 'mkmf'
|
|
3
3
|
|
|
4
4
|
$defs << "-DJSON_DEBUG" if ENV.fetch("JSON_DEBUG", "0") != "0"
|
|
5
|
+
|
|
6
|
+
if RUBY_ENGINE == 'truffleruby' && RUBY_ENGINE_VERSION < '40.0'
|
|
7
|
+
# Ref: https://github.com/truffleruby/truffleruby/issues/4329
|
|
8
|
+
# Ref: https://github.com/truffleruby/truffleruby/pull/4333
|
|
9
|
+
$defs << "-DJSON_TRUFFLERUBY_RB_CATCH_BUG"
|
|
10
|
+
end
|
|
11
|
+
|
|
5
12
|
have_func("rb_enc_interned_str", "ruby/encoding.h") # RUBY_VERSION >= 3.0
|
|
6
13
|
have_func("rb_str_to_interned_str", "ruby.h") # RUBY_VERSION >= 3.0
|
|
7
14
|
have_func("rb_hash_new_capa", "ruby.h") # RUBY_VERSION >= 3.2
|
|
8
15
|
have_func("rb_hash_bulk_insert", "ruby.h") # Missing on TruffleRuby
|
|
16
|
+
have_func("ruby_xfree_sized", "ruby.h") # RUBY_VERSION >= 4.1
|
|
17
|
+
|
|
18
|
+
def have_builtin_func(name, check_expr, opt = "", &b)
|
|
19
|
+
checking_for checking_message(name.funcall_style, nil, opt) do
|
|
20
|
+
if try_compile(<<SRC, opt, &b)
|
|
21
|
+
int foo;
|
|
22
|
+
int main() { #{check_expr}; return 0; }
|
|
23
|
+
SRC
|
|
24
|
+
$defs.push(format("-DHAVE_BUILTIN_%s", name.tr_cpp))
|
|
25
|
+
true
|
|
26
|
+
else
|
|
27
|
+
false
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
have_builtin_func("__builtin_clzll", "__builtin_clzll(0)")
|
|
33
|
+
|
|
34
|
+
if have_header("x86intrin.h")
|
|
35
|
+
have_func("_lzcnt_u64", "x86intrin.h")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
if have_header("intrin.h")
|
|
39
|
+
have_func("__lzcnt64", "intrin.h")
|
|
40
|
+
have_func("_BitScanReverse64", "intrin.h")
|
|
41
|
+
end
|
|
9
42
|
|
|
10
43
|
if RUBY_ENGINE == "ruby"
|
|
11
44
|
have_const("RUBY_TYPED_EMBEDDABLE", "ruby.h") # RUBY_VERSION >= 3.3
|