json 2.18.1 → 2.20.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.
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,18 @@
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 UNDEF_P
35
+ #define UNDEF_P(val) (val == Qundef)
36
+ #endif
37
+
25
38
  #if SIZEOF_UINT64_T == SIZEOF_LONG_LONG
26
39
  # define INT64T2NUM(x) LL2NUM(x)
27
40
  # define UINT64T2NUM(x) ULL2NUM(x)
@@ -49,14 +62,47 @@ typedef unsigned char _Bool;
49
62
  #endif
50
63
  #endif
51
64
 
65
+ #ifndef HAVE_RUBY_XFREE_SIZED
66
+ static inline void ruby_xfree_sized(void *ptr, size_t oldsize)
67
+ {
68
+ ruby_xfree(ptr);
69
+ }
70
+
71
+ static inline void *ruby_xrealloc2_sized(void *ptr, size_t new_elems, size_t elem_size, size_t old_elems)
72
+ {
73
+ return ruby_xrealloc2(ptr, new_elems, elem_size);
74
+ }
75
+ #endif
76
+
77
+ # define JSON_SIZED_REALLOC_N(v, T, m, n) \
78
+ ((v) = (T *)ruby_xrealloc2_sized((void *)(v), (m), sizeof(T), (n)))
79
+
80
+ # define JSON_SIZED_FREE(v) ruby_xfree_sized((void *)(v), sizeof(*(v)))
81
+ # define JSON_SIZED_FREE_N(v, n) ruby_xfree_sized((void *)(v), sizeof(*(v)) * (n))
82
+
52
83
  #ifndef HAVE_RB_EXT_RACTOR_SAFE
53
84
  # undef RUBY_TYPED_FROZEN_SHAREABLE
54
85
  # define RUBY_TYPED_FROZEN_SHAREABLE 0
55
86
  #endif
56
87
 
88
+ #ifdef RUBY_TYPED_EMBEDDABLE
89
+ # define HAVE_RUBY_TYPED_EMBEDDABLE 1
90
+ #else
91
+ # ifdef HAVE_CONST_RUBY_TYPED_EMBEDDABLE
92
+ # define RUBY_TYPED_EMBEDDABLE RUBY_TYPED_EMBEDDABLE
93
+ # define HAVE_RUBY_TYPED_EMBEDDABLE 1
94
+ # else
95
+ # define RUBY_TYPED_EMBEDDABLE 0
96
+ # endif
97
+ #endif
98
+
57
99
  #ifndef NORETURN
100
+ #if defined(__has_attribute) && __has_attribute(noreturn)
101
+ #define NORETURN(x) __attribute__((noreturn)) x
102
+ #else
58
103
  #define NORETURN(x) x
59
104
  #endif
105
+ #endif
60
106
 
61
107
  #ifndef NOINLINE
62
108
  #if defined(__has_attribute) && __has_attribute(noinline)
@@ -98,4 +144,36 @@ typedef unsigned char _Bool;
98
144
  #define JSON_CPU_LITTLE_ENDIAN_64BITS 0
99
145
  #endif
100
146
 
147
+ #ifdef JSON_TRUFFLERUBY_RB_CATCH_BUG
148
+
149
+ #undef RB_BLOCK_CALL_FUNC_ARGLIST
150
+ #define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, func_args) VALUE func_args
151
+
152
+ NORETURN(static inline) void json_rb_throw_obj(VALUE tag, VALUE obj)
153
+ {
154
+ VALUE exc = rb_exc_new_str(rb_eException, rb_utf8_str_new_cstr("throw_workaround"));
155
+ rb_ivar_set(exc, rb_intern("@throw_tag"), tag);
156
+ rb_ivar_set(exc, rb_intern("@throw_obj"), obj);
157
+ rb_exc_raise(exc);
158
+ }
159
+ #define rb_throw_obj json_rb_throw_obj
160
+
161
+ static inline VALUE json_rb_catch_obj(VALUE tag, VALUE (*func)(VALUE args), VALUE func_args)
162
+ {
163
+ int status;
164
+ VALUE result = rb_protect(func, func_args, &status);
165
+ if (status) {
166
+ VALUE exc = rb_errinfo();
167
+ if (tag == rb_ivar_get(exc, rb_intern("@throw_tag"))) {
168
+ rb_set_errinfo(Qnil);
169
+ return rb_ivar_get(exc, rb_intern("@throw_obj"));
170
+ }
171
+ rb_jump_tag(status);
172
+ }
173
+ return result;
174
+ }
175
+ #define rb_catch_obj json_rb_catch_obj
176
+
177
+ #endif // JSON_TRUFFLERUBY_RB_CATCH_BUG
178
+
101
179
  #endif // _JSON_H_
@@ -2,10 +2,47 @@
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
42
+
43
+ if RUBY_ENGINE == "ruby"
44
+ have_const("RUBY_TYPED_EMBEDDABLE", "ruby.h") # RUBY_VERSION >= 3.3
45
+ end
9
46
 
10
47
  append_cflags("-std=c99")
11
48