json 2.6.3 → 2.13.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.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/BSDL +22 -0
  3. data/CHANGES.md +212 -17
  4. data/LEGAL +8 -0
  5. data/README.md +75 -219
  6. data/ext/json/ext/fbuffer/fbuffer.h +178 -95
  7. data/ext/json/ext/generator/extconf.rb +14 -2
  8. data/ext/json/ext/generator/generator.c +1336 -805
  9. data/ext/json/ext/parser/extconf.rb +8 -25
  10. data/ext/json/ext/parser/parser.c +1365 -3205
  11. data/ext/json/ext/simd/conf.rb +20 -0
  12. data/ext/json/ext/simd/simd.h +187 -0
  13. data/ext/json/ext/vendor/fpconv.c +479 -0
  14. data/ext/json/ext/vendor/jeaiii-ltoa.h +267 -0
  15. data/json.gemspec +48 -53
  16. data/lib/json/add/bigdecimal.rb +39 -10
  17. data/lib/json/add/complex.rb +29 -6
  18. data/lib/json/add/core.rb +1 -1
  19. data/lib/json/add/date.rb +27 -7
  20. data/lib/json/add/date_time.rb +26 -9
  21. data/lib/json/add/exception.rb +25 -7
  22. data/lib/json/add/ostruct.rb +32 -9
  23. data/lib/json/add/range.rb +33 -8
  24. data/lib/json/add/rational.rb +28 -6
  25. data/lib/json/add/regexp.rb +26 -8
  26. data/lib/json/add/set.rb +25 -6
  27. data/lib/json/add/struct.rb +29 -7
  28. data/lib/json/add/symbol.rb +34 -7
  29. data/lib/json/add/time.rb +29 -15
  30. data/lib/json/common.rb +654 -253
  31. data/lib/json/ext/generator/state.rb +106 -0
  32. data/lib/json/ext.rb +35 -5
  33. data/lib/json/generic_object.rb +7 -3
  34. data/lib/json/truffle_ruby/generator.rb +690 -0
  35. data/lib/json/version.rb +3 -7
  36. data/lib/json.rb +58 -21
  37. metadata +19 -26
  38. data/VERSION +0 -1
  39. data/ext/json/ext/generator/depend +0 -1
  40. data/ext/json/ext/generator/generator.h +0 -174
  41. data/ext/json/ext/parser/depend +0 -1
  42. data/ext/json/ext/parser/parser.h +0 -96
  43. data/ext/json/ext/parser/parser.rl +0 -986
  44. data/ext/json/extconf.rb +0 -3
  45. data/lib/json/pure/generator.rb +0 -479
  46. data/lib/json/pure/parser.rb +0 -337
  47. data/lib/json/pure.rb +0 -15
  48. /data/{LICENSE → COPYING} +0 -0
@@ -1,324 +1,675 @@
1
+ #include "ruby.h"
1
2
  #include "../fbuffer/fbuffer.h"
2
- #include "generator.h"
3
+ #include "../vendor/fpconv.c"
3
4
 
4
- static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
5
- mHash, mArray,
5
+ #include <math.h>
6
+ #include <ctype.h>
7
+
8
+ #include "../simd/simd.h"
9
+
10
+ /* ruby api and some helpers */
11
+
12
+ typedef struct JSON_Generator_StateStruct {
13
+ VALUE indent;
14
+ VALUE space;
15
+ VALUE space_before;
16
+ VALUE object_nl;
17
+ VALUE array_nl;
18
+ VALUE as_json;
19
+
20
+ long max_nesting;
21
+ long depth;
22
+ long buffer_initial_length;
23
+
24
+ bool allow_nan;
25
+ bool ascii_only;
26
+ bool script_safe;
27
+ bool strict;
28
+ } JSON_Generator_State;
29
+
30
+ #ifndef RB_UNLIKELY
31
+ #define RB_UNLIKELY(cond) (cond)
32
+ #endif
33
+
34
+ static VALUE mJSON, cState, cFragment, mString_Extend, eGeneratorError, eNestingError, Encoding_UTF_8;
35
+
36
+ static ID i_to_s, i_to_json, i_new, i_pack, i_unpack, i_create_id, i_extend, i_encode;
37
+ static VALUE sym_indent, sym_space, sym_space_before, sym_object_nl, sym_array_nl, sym_max_nesting, sym_allow_nan,
38
+ sym_ascii_only, sym_depth, sym_buffer_initial_length, sym_script_safe, sym_escape_slash, sym_strict, sym_as_json;
39
+
40
+
41
+ #define GET_STATE_TO(self, state) \
42
+ TypedData_Get_Struct(self, JSON_Generator_State, &JSON_Generator_State_type, state)
43
+
44
+ #define GET_STATE(self) \
45
+ JSON_Generator_State *state; \
46
+ GET_STATE_TO(self, state)
47
+
48
+ struct generate_json_data;
49
+
50
+ typedef void (*generator_func)(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
51
+
52
+ struct generate_json_data {
53
+ FBuffer *buffer;
54
+ VALUE vstate;
55
+ JSON_Generator_State *state;
56
+ VALUE obj;
57
+ generator_func func;
58
+ };
59
+
60
+ static VALUE cState_from_state_s(VALUE self, VALUE opts);
61
+ static VALUE cState_partial_generate(VALUE self, VALUE obj, generator_func, VALUE io);
62
+ static void generate_json(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
63
+ static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
64
+ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
65
+ static void generate_json_string(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
66
+ static void generate_json_null(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
67
+ static void generate_json_false(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
68
+ static void generate_json_true(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
6
69
  #ifdef RUBY_INTEGER_UNIFICATION
7
- mInteger,
70
+ static void generate_json_integer(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
71
+ #endif
72
+ static void generate_json_fixnum(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
73
+ static void generate_json_bignum(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
74
+ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
75
+ static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *data, VALUE obj);
76
+
77
+ static int usascii_encindex, utf8_encindex, binary_encindex;
78
+
79
+ #ifdef RBIMPL_ATTR_NORETURN
80
+ RBIMPL_ATTR_NORETURN()
81
+ #endif
82
+ static void raise_generator_error_str(VALUE invalid_object, VALUE str)
83
+ {
84
+ VALUE exc = rb_exc_new_str(eGeneratorError, str);
85
+ rb_ivar_set(exc, rb_intern("@invalid_object"), invalid_object);
86
+ rb_exc_raise(exc);
87
+ }
88
+
89
+ #ifdef RBIMPL_ATTR_NORETURN
90
+ RBIMPL_ATTR_NORETURN()
91
+ #endif
92
+ #ifdef RBIMPL_ATTR_FORMAT
93
+ RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
94
+ #endif
95
+ static void raise_generator_error(VALUE invalid_object, const char *fmt, ...)
96
+ {
97
+ va_list args;
98
+ va_start(args, fmt);
99
+ VALUE str = rb_vsprintf(fmt, args);
100
+ va_end(args);
101
+ raise_generator_error_str(invalid_object, str);
102
+ }
103
+
104
+ // 0 - single byte char that don't need to be escaped.
105
+ // (x | 8) - char that needs to be escaped.
106
+ static const unsigned char CHAR_LENGTH_MASK = 7;
107
+ static const unsigned char ESCAPE_MASK = 8;
108
+
109
+ typedef struct _search_state {
110
+ const char *ptr;
111
+ const char *end;
112
+ const char *cursor;
113
+ FBuffer *buffer;
114
+
115
+ #ifdef HAVE_SIMD
116
+ const char *chunk_base;
117
+ const char *chunk_end;
118
+ bool has_matches;
119
+
120
+ #if defined(HAVE_SIMD_NEON)
121
+ uint64_t matches_mask;
122
+ #elif defined(HAVE_SIMD_SSE2)
123
+ int matches_mask;
124
+ #else
125
+ #error "Unknown SIMD Implementation."
126
+ #endif /* HAVE_SIMD_NEON */
127
+ #endif /* HAVE_SIMD */
128
+ } search_state;
129
+
130
+ #if (defined(__GNUC__ ) || defined(__clang__))
131
+ #define FORCE_INLINE __attribute__((always_inline))
8
132
  #else
9
- mFixnum, mBignum,
133
+ #define FORCE_INLINE
10
134
  #endif
11
- mFloat, mString, mString_Extend,
12
- mTrueClass, mFalseClass, mNilClass, eGeneratorError,
13
- eNestingError;
14
135
 
15
- static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
16
- i_object_nl, i_array_nl, i_max_nesting, i_allow_nan, i_ascii_only,
17
- i_pack, i_unpack, i_create_id, i_extend, i_key_p,
18
- i_aref, i_send, i_respond_to_p, i_match, i_keys, i_depth,
19
- i_buffer_initial_length, i_dup, i_escape_slash;
136
+ static inline FORCE_INLINE void search_flush(search_state *search)
137
+ {
138
+ // Do not remove this conditional without profiling, specifically escape-heavy text.
139
+ // escape_UTF8_char_basic will advance search->ptr and search->cursor (effectively a search_flush).
140
+ // For back-to-back characters that need to be escaped, specifcally for the SIMD code paths, this method
141
+ // will be called just before calling escape_UTF8_char_basic. There will be no characers to append for the
142
+ // consecutive characters that need to be escaped. While the fbuffer_append is a no-op if
143
+ // nothing needs to be flushed, we can save a few memory references with this conditional.
144
+ if (search->ptr > search->cursor) {
145
+ fbuffer_append(search->buffer, search->cursor, search->ptr - search->cursor);
146
+ search->cursor = search->ptr;
147
+ }
148
+ }
20
149
 
21
- /*
22
- * Copyright 2001-2004 Unicode, Inc.
150
+ static const unsigned char escape_table_basic[256] = {
151
+ // ASCII Control Characters
152
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
153
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
154
+ // ASCII Characters
155
+ 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // '"'
156
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
157
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
158
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
159
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
161
+ };
162
+
163
+ static unsigned char (*search_escape_basic_impl)(search_state *);
164
+
165
+ static inline unsigned char search_escape_basic(search_state *search)
166
+ {
167
+ while (search->ptr < search->end) {
168
+ if (RB_UNLIKELY(escape_table_basic[(const unsigned char)*search->ptr])) {
169
+ search_flush(search);
170
+ return 1;
171
+ } else {
172
+ search->ptr++;
173
+ }
174
+ }
175
+ search_flush(search);
176
+ return 0;
177
+ }
178
+
179
+ static inline FORCE_INLINE void escape_UTF8_char_basic(search_state *search)
180
+ {
181
+ const unsigned char ch = (unsigned char)*search->ptr;
182
+ switch (ch) {
183
+ case '"': fbuffer_append(search->buffer, "\\\"", 2); break;
184
+ case '\\': fbuffer_append(search->buffer, "\\\\", 2); break;
185
+ case '/': fbuffer_append(search->buffer, "\\/", 2); break;
186
+ case '\b': fbuffer_append(search->buffer, "\\b", 2); break;
187
+ case '\f': fbuffer_append(search->buffer, "\\f", 2); break;
188
+ case '\n': fbuffer_append(search->buffer, "\\n", 2); break;
189
+ case '\r': fbuffer_append(search->buffer, "\\r", 2); break;
190
+ case '\t': fbuffer_append(search->buffer, "\\t", 2); break;
191
+ default: {
192
+ const char *hexdig = "0123456789abcdef";
193
+ char scratch[6] = { '\\', 'u', '0', '0', 0, 0 };
194
+ scratch[4] = hexdig[(ch >> 4) & 0xf];
195
+ scratch[5] = hexdig[ch & 0xf];
196
+ fbuffer_append(search->buffer, scratch, 6);
197
+ break;
198
+ }
199
+ }
200
+ search->ptr++;
201
+ search->cursor = search->ptr;
202
+ }
203
+
204
+ /* Converts in_string to a JSON string (without the wrapping '"'
205
+ * characters) in FBuffer out_buffer.
23
206
  *
24
- * Disclaimer
207
+ * Character are JSON-escaped according to:
25
208
  *
26
- * This source code is provided as is by Unicode, Inc. No claims are
27
- * made as to fitness for any particular purpose. No warranties of any
28
- * kind are expressed or implied. The recipient agrees to determine
29
- * applicability of information provided. If this file has been
30
- * purchased on magnetic or optical media from Unicode, Inc., the
31
- * sole remedy for any claim will be exchange of defective media
32
- * within 90 days of receipt.
209
+ * - Always: ASCII control characters (0x00-0x1F), dquote, and
210
+ * backslash.
33
211
  *
34
- * Limitations on Rights to Redistribute This Code
212
+ * - If out_ascii_only: non-ASCII characters (>0x7F)
35
213
  *
36
- * Unicode, Inc. hereby grants the right to freely use the information
37
- * supplied in this file in the creation of products supporting the
38
- * Unicode Standard, and to make copies of this file in any form
39
- * for internal or external distribution as long as this notice
40
- * remains attached.
214
+ * - If script_safe: forwardslash (/), line separator (U+2028), and
215
+ * paragraph separator (U+2029)
216
+ *
217
+ * Everything else (should be UTF-8) is just passed through and
218
+ * appended to the result.
41
219
  */
220
+ static inline void convert_UTF8_to_JSON(search_state *search)
221
+ {
222
+ while (search_escape_basic_impl(search)) {
223
+ escape_UTF8_char_basic(search);
224
+ }
225
+ }
42
226
 
43
- /*
44
- * Index into the table below with the first byte of a UTF-8 sequence to
45
- * get the number of trailing bytes that are supposed to follow it.
46
- * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
47
- * left as-is for anyone who may want to do such conversion, which was
48
- * allowed in earlier algorithms.
49
- */
50
- static const char trailingBytesForUTF8[256] = {
51
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
52
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
53
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
54
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
55
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
56
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
57
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
58
- 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
59
- };
227
+ static inline void escape_UTF8_char(search_state *search, unsigned char ch_len)
228
+ {
229
+ const unsigned char ch = (unsigned char)*search->ptr;
230
+ switch (ch_len) {
231
+ case 1: {
232
+ switch (ch) {
233
+ case '"': fbuffer_append(search->buffer, "\\\"", 2); break;
234
+ case '\\': fbuffer_append(search->buffer, "\\\\", 2); break;
235
+ case '/': fbuffer_append(search->buffer, "\\/", 2); break;
236
+ case '\b': fbuffer_append(search->buffer, "\\b", 2); break;
237
+ case '\f': fbuffer_append(search->buffer, "\\f", 2); break;
238
+ case '\n': fbuffer_append(search->buffer, "\\n", 2); break;
239
+ case '\r': fbuffer_append(search->buffer, "\\r", 2); break;
240
+ case '\t': fbuffer_append(search->buffer, "\\t", 2); break;
241
+ default: {
242
+ const char *hexdig = "0123456789abcdef";
243
+ char scratch[6] = { '\\', 'u', '0', '0', 0, 0 };
244
+ scratch[4] = hexdig[(ch >> 4) & 0xf];
245
+ scratch[5] = hexdig[ch & 0xf];
246
+ fbuffer_append(search->buffer, scratch, 6);
247
+ break;
248
+ }
249
+ }
250
+ break;
251
+ }
252
+ case 3: {
253
+ if (search->ptr[2] & 1) {
254
+ fbuffer_append(search->buffer, "\\u2029", 6);
255
+ } else {
256
+ fbuffer_append(search->buffer, "\\u2028", 6);
257
+ }
258
+ break;
259
+ }
260
+ }
261
+ search->cursor = (search->ptr += ch_len);
262
+ }
60
263
 
61
- /*
62
- * Magic values subtracted from a buffer value during UTF8 conversion.
63
- * This table contains as many values as there might be trailing bytes
64
- * in a UTF-8 sequence.
65
- */
66
- static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
67
- 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
264
+ #ifdef HAVE_SIMD
68
265
 
69
- /*
70
- * Utility routine to tell whether a sequence of bytes is legal UTF-8.
71
- * This must be called with the length pre-determined by the first byte.
72
- * If not calling this from ConvertUTF8to*, then the length can be set by:
73
- * length = trailingBytesForUTF8[*source]+1;
74
- * and the sequence is illegal right away if there aren't that many bytes
75
- * available.
76
- * If presented with a length > 4, this returns 0. The Unicode
77
- * definition of UTF-8 goes up to 4-byte sequences.
78
- */
79
- static unsigned char isLegalUTF8(const UTF8 *source, unsigned long length)
80
- {
81
- UTF8 a;
82
- const UTF8 *srcptr = source+length;
83
- switch (length) {
84
- default: return 0;
85
- /* Everything else falls through when "1"... */
86
- case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
87
- case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
88
- case 2: if ((a = (*--srcptr)) > 0xBF) return 0;
89
-
90
- switch (*source) {
91
- /* no fall-through in this inner switch */
92
- case 0xE0: if (a < 0xA0) return 0; break;
93
- case 0xED: if (a > 0x9F) return 0; break;
94
- case 0xF0: if (a < 0x90) return 0; break;
95
- case 0xF4: if (a > 0x8F) return 0; break;
96
- default: if (a < 0x80) return 0;
97
- }
266
+ static inline FORCE_INLINE char *copy_remaining_bytes(search_state *search, unsigned long vec_len, unsigned long len)
267
+ {
268
+ // Flush the buffer so everything up until the last 'len' characters are unflushed.
269
+ search_flush(search);
98
270
 
99
- case 1: if (*source >= 0x80 && *source < 0xC2) return 0;
100
- }
101
- if (*source > 0xF4) return 0;
102
- return 1;
271
+ FBuffer *buf = search->buffer;
272
+ fbuffer_inc_capa(buf, vec_len);
273
+
274
+ char *s = (buf->ptr + buf->len);
275
+
276
+ // Pad the buffer with dummy characters that won't need escaping.
277
+ // This seem wateful at first sight, but memset of vector length is very fast.
278
+ memset(s, 'X', vec_len);
279
+
280
+ // Optimistically copy the remaining 'len' characters to the output FBuffer. If there are no characters
281
+ // to escape, then everything ends up in the correct spot. Otherwise it was convenient temporary storage.
282
+ MEMCPY(s, search->ptr, char, len);
283
+
284
+ return s;
103
285
  }
104
286
 
105
- /* Escapes the UTF16 character and stores the result in the buffer buf. */
106
- static void unicode_escape(char *buf, UTF16 character)
287
+ #ifdef HAVE_SIMD_NEON
288
+
289
+ static inline FORCE_INLINE unsigned char neon_next_match(search_state *search)
107
290
  {
108
- const char *digits = "0123456789abcdef";
291
+ uint64_t mask = search->matches_mask;
292
+ uint32_t index = trailing_zeros64(mask) >> 2;
109
293
 
110
- buf[2] = digits[character >> 12];
111
- buf[3] = digits[(character >> 8) & 0xf];
112
- buf[4] = digits[(character >> 4) & 0xf];
113
- buf[5] = digits[character & 0xf];
294
+ // It is assumed escape_UTF8_char_basic will only ever increase search->ptr by at most one character.
295
+ // If we want to use a similar approach for full escaping we'll need to ensure:
296
+ // search->chunk_base + index >= search->ptr
297
+ // However, since we know escape_UTF8_char_basic only increases search->ptr by one, if the next match
298
+ // is one byte after the previous match then:
299
+ // search->chunk_base + index == search->ptr
300
+ search->ptr = search->chunk_base + index;
301
+ mask &= mask - 1;
302
+ search->matches_mask = mask;
303
+ search_flush(search);
304
+ return 1;
114
305
  }
115
306
 
116
- /* Escapes the UTF16 character and stores the result in the buffer buf, then
117
- * the buffer buf is appended to the FBuffer buffer. */
118
- static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16
119
- character)
307
+ static inline unsigned char search_escape_basic_neon(search_state *search)
120
308
  {
121
- unicode_escape(buf, character);
122
- fbuffer_append(buffer, buf, 6);
309
+ if (RB_UNLIKELY(search->has_matches)) {
310
+ // There are more matches if search->matches_mask > 0.
311
+ if (search->matches_mask > 0) {
312
+ return neon_next_match(search);
313
+ } else {
314
+ // neon_next_match will only advance search->ptr up to the last matching character.
315
+ // Skip over any characters in the last chunk that occur after the last match.
316
+ search->has_matches = false;
317
+ search->ptr = search->chunk_end;
318
+ }
319
+ }
320
+
321
+ /*
322
+ * The code below implements an SIMD-based algorithm to determine if N bytes at a time
323
+ * need to be escaped.
324
+ *
325
+ * Assume the ptr = "Te\sting!" (the double quotes are included in the string)
326
+ *
327
+ * The explanation will be limited to the first 8 bytes of the string for simplicity. However
328
+ * the vector insructions may work on larger vectors.
329
+ *
330
+ * First, we load three constants 'lower_bound', 'backslash' and 'dblquote" in vector registers.
331
+ *
332
+ * lower_bound: [20 20 20 20 20 20 20 20]
333
+ * backslash: [5C 5C 5C 5C 5C 5C 5C 5C]
334
+ * dblquote: [22 22 22 22 22 22 22 22]
335
+ *
336
+ * Next we load the first chunk of the ptr:
337
+ * [22 54 65 5C 73 74 69 6E] (" T e \ s t i n)
338
+ *
339
+ * First we check if any byte in chunk is less than 32 (0x20). This returns the following vector
340
+ * as no bytes are less than 32 (0x20):
341
+ * [0 0 0 0 0 0 0 0]
342
+ *
343
+ * Next, we check if any byte in chunk is equal to a backslash:
344
+ * [0 0 0 FF 0 0 0 0]
345
+ *
346
+ * Finally we check if any byte in chunk is equal to a double quote:
347
+ * [FF 0 0 0 0 0 0 0]
348
+ *
349
+ * Now we have three vectors where each byte indicates if the corresponding byte in chunk
350
+ * needs to be escaped. We combine these vectors with a series of logical OR instructions.
351
+ * This is the needs_escape vector and it is equal to:
352
+ * [FF 0 0 FF 0 0 0 0]
353
+ *
354
+ * Next we compute the bitwise AND between each byte and 0x1 and compute the horizontal sum of
355
+ * the values in the vector. This computes how many bytes need to be escaped within this chunk.
356
+ *
357
+ * Finally we compute a mask that indicates which bytes need to be escaped. If the mask is 0 then,
358
+ * no bytes need to be escaped and we can continue to the next chunk. If the mask is not 0 then we
359
+ * have at least one byte that needs to be escaped.
360
+ */
361
+
362
+ if (string_scan_simd_neon(&search->ptr, search->end, &search->matches_mask)) {
363
+ search->has_matches = true;
364
+ search->chunk_base = search->ptr;
365
+ search->chunk_end = search->ptr + sizeof(uint8x16_t);
366
+ return neon_next_match(search);
367
+ }
368
+
369
+ // There are fewer than 16 bytes left.
370
+ unsigned long remaining = (search->end - search->ptr);
371
+ if (remaining >= SIMD_MINIMUM_THRESHOLD) {
372
+ char *s = copy_remaining_bytes(search, sizeof(uint8x16_t), remaining);
373
+
374
+ uint64_t mask = compute_chunk_mask_neon(s);
375
+
376
+ if (!mask) {
377
+ // Nothing to escape, ensure search_flush doesn't do anything by setting
378
+ // search->cursor to search->ptr.
379
+ fbuffer_consumed(search->buffer, remaining);
380
+ search->ptr = search->end;
381
+ search->cursor = search->end;
382
+ return 0;
383
+ }
384
+
385
+ search->matches_mask = mask;
386
+ search->has_matches = true;
387
+ search->chunk_end = search->end;
388
+ search->chunk_base = search->ptr;
389
+ return neon_next_match(search);
390
+ }
391
+
392
+ if (search->ptr < search->end) {
393
+ return search_escape_basic(search);
394
+ }
395
+
396
+ search_flush(search);
397
+ return 0;
123
398
  }
399
+ #endif /* HAVE_SIMD_NEON */
400
+
401
+ #ifdef HAVE_SIMD_SSE2
124
402
 
125
- /* Converts string to a JSON string in FBuffer buffer, where all but the ASCII
126
- * and control characters are JSON escaped. */
127
- static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string, char escape_slash)
403
+ static inline FORCE_INLINE unsigned char sse2_next_match(search_state *search)
128
404
  {
129
- const UTF8 *source = (UTF8 *) RSTRING_PTR(string);
130
- const UTF8 *sourceEnd = source + RSTRING_LEN(string);
131
- char buf[6] = { '\\', 'u' };
405
+ int mask = search->matches_mask;
406
+ int index = trailing_zeros(mask);
132
407
 
133
- while (source < sourceEnd) {
134
- UTF32 ch = 0;
135
- unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
136
- if (source + extraBytesToRead >= sourceEnd) {
137
- rb_raise(rb_path2class("JSON::GeneratorError"),
138
- "partial character in source, but hit end");
139
- }
140
- if (!isLegalUTF8(source, extraBytesToRead+1)) {
141
- rb_raise(rb_path2class("JSON::GeneratorError"),
142
- "source sequence is illegal/malformed utf-8");
143
- }
144
- /*
145
- * The cases all fall through. See "Note A" below.
146
- */
147
- switch (extraBytesToRead) {
148
- case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
149
- case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
150
- case 3: ch += *source++; ch <<= 6;
151
- case 2: ch += *source++; ch <<= 6;
152
- case 1: ch += *source++; ch <<= 6;
153
- case 0: ch += *source++;
154
- }
155
- ch -= offsetsFromUTF8[extraBytesToRead];
156
-
157
- if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
158
- /* UTF-16 surrogate values are illegal in UTF-32 */
159
- if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
160
- #if UNI_STRICT_CONVERSION
161
- source -= (extraBytesToRead+1); /* return to the illegal value itself */
162
- rb_raise(rb_path2class("JSON::GeneratorError"),
163
- "source sequence is illegal/malformed utf-8");
408
+ // It is assumed escape_UTF8_char_basic will only ever increase search->ptr by at most one character.
409
+ // If we want to use a similar approach for full escaping we'll need to ensure:
410
+ // search->chunk_base + index >= search->ptr
411
+ // However, since we know escape_UTF8_char_basic only increases search->ptr by one, if the next match
412
+ // is one byte after the previous match then:
413
+ // search->chunk_base + index == search->ptr
414
+ search->ptr = search->chunk_base + index;
415
+ mask &= mask - 1;
416
+ search->matches_mask = mask;
417
+ search_flush(search);
418
+ return 1;
419
+ }
420
+
421
+ #if defined(__clang__) || defined(__GNUC__)
422
+ #define TARGET_SSE2 __attribute__((target("sse2")))
164
423
  #else
165
- unicode_escape_to_buffer(buffer, buf, UNI_REPLACEMENT_CHAR);
424
+ #define TARGET_SSE2
166
425
  #endif
426
+
427
+ static inline TARGET_SSE2 FORCE_INLINE unsigned char search_escape_basic_sse2(search_state *search)
428
+ {
429
+ if (RB_UNLIKELY(search->has_matches)) {
430
+ // There are more matches if search->matches_mask > 0.
431
+ if (search->matches_mask > 0) {
432
+ return sse2_next_match(search);
433
+ } else {
434
+ // sse2_next_match will only advance search->ptr up to the last matching character.
435
+ // Skip over any characters in the last chunk that occur after the last match.
436
+ search->has_matches = false;
437
+ if (RB_UNLIKELY(search->chunk_base + sizeof(__m128i) >= search->end)) {
438
+ search->ptr = search->end;
167
439
  } else {
168
- /* normal case */
169
- if (ch >= 0x20 && ch <= 0x7f) {
170
- switch (ch) {
171
- case '\\':
172
- fbuffer_append(buffer, "\\\\", 2);
173
- break;
174
- case '"':
175
- fbuffer_append(buffer, "\\\"", 2);
176
- break;
177
- case '/':
178
- if(escape_slash) {
179
- fbuffer_append(buffer, "\\/", 2);
180
- break;
181
- }
182
- default:
183
- fbuffer_append_char(buffer, (char)ch);
184
- break;
185
- }
186
- } else {
187
- switch (ch) {
188
- case '\n':
189
- fbuffer_append(buffer, "\\n", 2);
190
- break;
191
- case '\r':
192
- fbuffer_append(buffer, "\\r", 2);
193
- break;
194
- case '\t':
195
- fbuffer_append(buffer, "\\t", 2);
196
- break;
197
- case '\f':
198
- fbuffer_append(buffer, "\\f", 2);
199
- break;
200
- case '\b':
201
- fbuffer_append(buffer, "\\b", 2);
202
- break;
203
- default:
204
- unicode_escape_to_buffer(buffer, buf, (UTF16) ch);
205
- break;
440
+ search->ptr = search->chunk_base + sizeof(__m128i);
441
+ }
442
+ }
443
+ }
444
+
445
+ if (string_scan_simd_sse2(&search->ptr, search->end, &search->matches_mask)) {
446
+ search->has_matches = true;
447
+ search->chunk_base = search->ptr;
448
+ search->chunk_end = search->ptr + sizeof(__m128i);
449
+ return sse2_next_match(search);
450
+ }
451
+
452
+ // There are fewer than 16 bytes left.
453
+ unsigned long remaining = (search->end - search->ptr);
454
+ if (remaining >= SIMD_MINIMUM_THRESHOLD) {
455
+ char *s = copy_remaining_bytes(search, sizeof(__m128i), remaining);
456
+
457
+ int needs_escape_mask = compute_chunk_mask_sse2(s);
458
+
459
+ if (needs_escape_mask == 0) {
460
+ // Nothing to escape, ensure search_flush doesn't do anything by setting
461
+ // search->cursor to search->ptr.
462
+ fbuffer_consumed(search->buffer, remaining);
463
+ search->ptr = search->end;
464
+ search->cursor = search->end;
465
+ return 0;
466
+ }
467
+
468
+ search->has_matches = true;
469
+ search->matches_mask = needs_escape_mask;
470
+ search->chunk_base = search->ptr;
471
+ return sse2_next_match(search);
472
+ }
473
+
474
+ if (search->ptr < search->end) {
475
+ return search_escape_basic(search);
476
+ }
477
+
478
+ search_flush(search);
479
+ return 0;
480
+ }
481
+
482
+ #endif /* HAVE_SIMD_SSE2 */
483
+
484
+ #endif /* HAVE_SIMD */
485
+
486
+ static const unsigned char script_safe_escape_table[256] = {
487
+ // ASCII Control Characters
488
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
489
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
490
+ // ASCII Characters
491
+ 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, // '"' and '/'
492
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
493
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
494
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
495
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
496
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
497
+ // Continuation byte
498
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
499
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
500
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
501
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
502
+ // First byte of a 2-byte code point
503
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
504
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
505
+ // First byte of a 3-byte code point
506
+ 3, 3,11, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xE2 is the start of \u2028 and \u2029
507
+ //First byte of a 4+ byte code point
508
+ 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 9, 9,
509
+ };
510
+
511
+ static inline unsigned char search_script_safe_escape(search_state *search)
512
+ {
513
+ while (search->ptr < search->end) {
514
+ unsigned char ch = (unsigned char)*search->ptr;
515
+ unsigned char ch_len = script_safe_escape_table[ch];
516
+
517
+ if (RB_UNLIKELY(ch_len)) {
518
+ if (ch_len & ESCAPE_MASK) {
519
+ if (RB_UNLIKELY(ch_len == 11)) {
520
+ const unsigned char *uptr = (const unsigned char *)search->ptr;
521
+ if (!(uptr[1] == 0x80 && (uptr[2] >> 1) == 0x54)) {
522
+ search->ptr += 3;
523
+ continue;
206
524
  }
207
525
  }
526
+ search_flush(search);
527
+ return ch_len & CHAR_LENGTH_MASK;
528
+ } else {
529
+ search->ptr += ch_len;
208
530
  }
209
- } else if (ch > UNI_MAX_UTF16) {
210
- #if UNI_STRICT_CONVERSION
211
- source -= (extraBytesToRead+1); /* return to the start */
212
- rb_raise(rb_path2class("JSON::GeneratorError"),
213
- "source sequence is illegal/malformed utf8");
214
- #else
215
- unicode_escape_to_buffer(buffer, buf, UNI_REPLACEMENT_CHAR);
216
- #endif
217
531
  } else {
218
- /* target is a character in range 0xFFFF - 0x10FFFF. */
219
- ch -= halfBase;
220
- unicode_escape_to_buffer(buffer, buf, (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START));
221
- unicode_escape_to_buffer(buffer, buf, (UTF16)((ch & halfMask) + UNI_SUR_LOW_START));
532
+ search->ptr++;
222
533
  }
223
534
  }
224
- RB_GC_GUARD(string);
225
- }
226
-
227
- /* Converts string to a JSON string in FBuffer buffer, where only the
228
- * characters required by the JSON standard are JSON escaped. The remaining
229
- * characters (should be UTF8) are just passed through and appended to the
230
- * result. */
231
- static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string, char escape_slash)
232
- {
233
- const char *ptr = RSTRING_PTR(string), *p;
234
- unsigned long len = RSTRING_LEN(string), start = 0, end = 0;
235
- const char *escape = NULL;
236
- int escape_len;
237
- unsigned char c;
238
- char buf[6] = { '\\', 'u' };
239
- int ascii_only = rb_enc_str_asciionly_p(string);
240
-
241
- for (start = 0, end = 0; end < len;) {
242
- p = ptr + end;
243
- c = (unsigned char) *p;
244
- if (c < 0x20) {
245
- switch (c) {
246
- case '\n':
247
- escape = "\\n";
248
- escape_len = 2;
249
- break;
250
- case '\r':
251
- escape = "\\r";
252
- escape_len = 2;
253
- break;
254
- case '\t':
255
- escape = "\\t";
256
- escape_len = 2;
257
- break;
258
- case '\f':
259
- escape = "\\f";
260
- escape_len = 2;
261
- break;
262
- case '\b':
263
- escape = "\\b";
264
- escape_len = 2;
265
- break;
266
- default:
267
- unicode_escape(buf, (UTF16) *p);
268
- escape = buf;
269
- escape_len = 6;
535
+ search_flush(search);
536
+ return 0;
537
+ }
538
+
539
+ static void convert_UTF8_to_script_safe_JSON(search_state *search)
540
+ {
541
+ unsigned char ch_len;
542
+ while ((ch_len = search_script_safe_escape(search))) {
543
+ escape_UTF8_char(search, ch_len);
544
+ }
545
+ }
546
+
547
+ static const unsigned char ascii_only_escape_table[256] = {
548
+ // ASCII Control Characters
549
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
550
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
551
+ // ASCII Characters
552
+ 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // '"'
553
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
554
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
555
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
556
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
557
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
558
+ // Continuation byte
559
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
560
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
561
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
562
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
563
+ // First byte of a 2-byte code point
564
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
565
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
566
+ // First byte of a 3-byte code point
567
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
568
+ //First byte of a 4+ byte code point
569
+ 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 9, 9,
570
+ };
571
+
572
+ static inline unsigned char search_ascii_only_escape(search_state *search, const unsigned char escape_table[256])
573
+ {
574
+ while (search->ptr < search->end) {
575
+ unsigned char ch = (unsigned char)*search->ptr;
576
+ unsigned char ch_len = escape_table[ch];
577
+
578
+ if (RB_UNLIKELY(ch_len)) {
579
+ search_flush(search);
580
+ return ch_len & CHAR_LENGTH_MASK;
581
+ } else {
582
+ search->ptr++;
583
+ }
584
+ }
585
+ search_flush(search);
586
+ return 0;
587
+ }
588
+
589
+ static inline void full_escape_UTF8_char(search_state *search, unsigned char ch_len)
590
+ {
591
+ const unsigned char ch = (unsigned char)*search->ptr;
592
+ switch (ch_len) {
593
+ case 1: {
594
+ switch (ch) {
595
+ case '"': fbuffer_append(search->buffer, "\\\"", 2); break;
596
+ case '\\': fbuffer_append(search->buffer, "\\\\", 2); break;
597
+ case '/': fbuffer_append(search->buffer, "\\/", 2); break;
598
+ case '\b': fbuffer_append(search->buffer, "\\b", 2); break;
599
+ case '\f': fbuffer_append(search->buffer, "\\f", 2); break;
600
+ case '\n': fbuffer_append(search->buffer, "\\n", 2); break;
601
+ case '\r': fbuffer_append(search->buffer, "\\r", 2); break;
602
+ case '\t': fbuffer_append(search->buffer, "\\t", 2); break;
603
+ default: {
604
+ const char *hexdig = "0123456789abcdef";
605
+ char scratch[6] = { '\\', 'u', '0', '0', 0, 0 };
606
+ scratch[4] = hexdig[(ch >> 4) & 0xf];
607
+ scratch[5] = hexdig[ch & 0xf];
608
+ fbuffer_append(search->buffer, scratch, 6);
270
609
  break;
610
+ }
271
611
  }
272
- } else {
273
- switch (c) {
274
- case '\\':
275
- escape = "\\\\";
276
- escape_len = 2;
612
+ break;
613
+ }
614
+ default: {
615
+ const char *hexdig = "0123456789abcdef";
616
+ char scratch[12] = { '\\', 'u', 0, 0, 0, 0, '\\', 'u' };
617
+
618
+ uint32_t wchar = 0;
619
+
620
+ switch (ch_len) {
621
+ case 2:
622
+ wchar = ch & 0x1F;
277
623
  break;
278
- case '"':
279
- escape = "\\\"";
280
- escape_len = 2;
624
+ case 3:
625
+ wchar = ch & 0x0F;
281
626
  break;
282
- case '/':
283
- if(escape_slash) {
284
- escape = "\\/";
285
- escape_len = 2;
286
- break;
287
- }
288
- default:
289
- {
290
- unsigned short clen = 1;
291
- if (!ascii_only) {
292
- clen += trailingBytesForUTF8[c];
293
- if (end + clen > len) {
294
- rb_raise(rb_path2class("JSON::GeneratorError"),
295
- "partial character in source, but hit end");
296
- }
297
- if (!isLegalUTF8((UTF8 *) p, clen)) {
298
- rb_raise(rb_path2class("JSON::GeneratorError"),
299
- "source sequence is illegal/malformed utf-8");
300
- }
301
- }
302
- end += clen;
303
- }
304
- continue;
627
+ case 4:
628
+ wchar = ch & 0x07;
305
629
  break;
306
630
  }
631
+
632
+ for (short i = 1; i < ch_len; i++) {
633
+ wchar = (wchar << 6) | (search->ptr[i] & 0x3F);
634
+ }
635
+
636
+ if (wchar <= 0xFFFF) {
637
+ scratch[2] = hexdig[wchar >> 12];
638
+ scratch[3] = hexdig[(wchar >> 8) & 0xf];
639
+ scratch[4] = hexdig[(wchar >> 4) & 0xf];
640
+ scratch[5] = hexdig[wchar & 0xf];
641
+ fbuffer_append(search->buffer, scratch, 6);
642
+ } else {
643
+ uint16_t hi, lo;
644
+ wchar -= 0x10000;
645
+ hi = 0xD800 + (uint16_t)(wchar >> 10);
646
+ lo = 0xDC00 + (uint16_t)(wchar & 0x3FF);
647
+
648
+ scratch[2] = hexdig[hi >> 12];
649
+ scratch[3] = hexdig[(hi >> 8) & 0xf];
650
+ scratch[4] = hexdig[(hi >> 4) & 0xf];
651
+ scratch[5] = hexdig[hi & 0xf];
652
+
653
+ scratch[8] = hexdig[lo >> 12];
654
+ scratch[9] = hexdig[(lo >> 8) & 0xf];
655
+ scratch[10] = hexdig[(lo >> 4) & 0xf];
656
+ scratch[11] = hexdig[lo & 0xf];
657
+
658
+ fbuffer_append(search->buffer, scratch, 12);
659
+ }
660
+
661
+ break;
307
662
  }
308
- fbuffer_append(buffer, ptr + start, end - start);
309
- fbuffer_append(buffer, escape, escape_len);
310
- start = ++end;
311
- escape = NULL;
312
663
  }
313
- fbuffer_append(buffer, ptr + start, end - start);
664
+ search->cursor = (search->ptr += ch_len);
314
665
  }
315
666
 
316
- static char *fstrndup(const char *ptr, unsigned long len) {
317
- char *result;
318
- if (len <= 0) return NULL;
319
- result = ALLOC_N(char, len);
320
- memcpy(result, ptr, len);
321
- return result;
667
+ static void convert_UTF8_to_ASCII_only_JSON(search_state *search, const unsigned char escape_table[256])
668
+ {
669
+ unsigned char ch_len;
670
+ while ((ch_len = search_ascii_only_escape(search, escape_table))) {
671
+ full_escape_UTF8_char(search, ch_len);
672
+ }
322
673
  }
323
674
 
324
675
  /*
@@ -413,7 +764,9 @@ static char *fstrndup(const char *ptr, unsigned long len) {
413
764
  */
414
765
  static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
415
766
  {
416
- GENERATE_JSON(object);
767
+ rb_check_arity(argc, 0, 1);
768
+ VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
769
+ return cState_partial_generate(Vstate, self, generate_json_object, Qfalse);
417
770
  }
418
771
 
419
772
  /*
@@ -424,8 +777,11 @@ static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
424
777
  * _state_ is a JSON::State object, that can also be used to configure the
425
778
  * produced JSON string output further.
426
779
  */
427
- static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
428
- GENERATE_JSON(array);
780
+ static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self)
781
+ {
782
+ rb_check_arity(argc, 0, 1);
783
+ VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
784
+ return cState_partial_generate(Vstate, self, generate_json_array, Qfalse);
429
785
  }
430
786
 
431
787
  #ifdef RUBY_INTEGER_UNIFICATION
@@ -436,7 +792,9 @@ static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
436
792
  */
437
793
  static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
438
794
  {
439
- GENERATE_JSON(integer);
795
+ rb_check_arity(argc, 0, 1);
796
+ VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
797
+ return cState_partial_generate(Vstate, self, generate_json_integer, Qfalse);
440
798
  }
441
799
 
442
800
  #else
@@ -447,7 +805,9 @@ static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
447
805
  */
448
806
  static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self)
449
807
  {
450
- GENERATE_JSON(fixnum);
808
+ rb_check_arity(argc, 0, 1);
809
+ VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
810
+ return cState_partial_generate(Vstate, self, generate_json_fixnum, Qfalse);
451
811
  }
452
812
 
453
813
  /*
@@ -457,7 +817,9 @@ static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self)
457
817
  */
458
818
  static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self)
459
819
  {
460
- GENERATE_JSON(bignum);
820
+ rb_check_arity(argc, 0, 1);
821
+ VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
822
+ return cState_partial_generate(Vstate, self, generate_json_bignum, Qfalse);
461
823
  }
462
824
  #endif
463
825
 
@@ -468,7 +830,9 @@ static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self)
468
830
  */
469
831
  static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
470
832
  {
471
- GENERATE_JSON(float);
833
+ rb_check_arity(argc, 0, 1);
834
+ VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
835
+ return cState_partial_generate(Vstate, self, generate_json_float, Qfalse);
472
836
  }
473
837
 
474
838
  /*
@@ -476,8 +840,10 @@ static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
476
840
  *
477
841
  * Extends _modul_ with the String::Extend module.
478
842
  */
479
- static VALUE mString_included_s(VALUE self, VALUE modul) {
843
+ static VALUE mString_included_s(VALUE self, VALUE modul)
844
+ {
480
845
  VALUE result = rb_funcall(modul, i_extend, 1, mString_Extend);
846
+ rb_call_super(1, &modul);
481
847
  return result;
482
848
  }
483
849
 
@@ -490,7 +856,9 @@ static VALUE mString_included_s(VALUE self, VALUE modul) {
490
856
  */
491
857
  static VALUE mString_to_json(int argc, VALUE *argv, VALUE self)
492
858
  {
493
- GENERATE_JSON(string);
859
+ rb_check_arity(argc, 0, 1);
860
+ VALUE Vstate = cState_from_state_s(cState, argc == 1 ? argv[0] : Qnil);
861
+ return cState_partial_generate(Vstate, self, generate_json_string, Qfalse);
494
862
  }
495
863
 
496
864
  /*
@@ -507,7 +875,7 @@ static VALUE mString_to_json_raw_object(VALUE self)
507
875
  VALUE result = rb_hash_new();
508
876
  rb_hash_aset(result, rb_funcall(mJSON, i_create_id, 0), rb_class_name(rb_obj_class(self)));
509
877
  ary = rb_funcall(self, i_unpack, 1, rb_str_new2("C*"));
510
- rb_hash_aset(result, rb_str_new2("raw"), ary);
878
+ rb_hash_aset(result, rb_utf8_str_new_lit("raw"), ary);
511
879
  return result;
512
880
  }
513
881
 
@@ -545,7 +913,8 @@ static VALUE mString_Extend_json_create(VALUE self, VALUE o)
545
913
  */
546
914
  static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self)
547
915
  {
548
- GENERATE_JSON(true);
916
+ rb_check_arity(argc, 0, 1);
917
+ return rb_utf8_str_new("true", 4);
549
918
  }
550
919
 
551
920
  /*
@@ -555,7 +924,8 @@ static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self)
555
924
  */
556
925
  static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self)
557
926
  {
558
- GENERATE_JSON(false);
927
+ rb_check_arity(argc, 0, 1);
928
+ return rb_utf8_str_new("false", 5);
559
929
  }
560
930
 
561
931
  /*
@@ -565,7 +935,8 @@ static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self)
565
935
  */
566
936
  static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self)
567
937
  {
568
- GENERATE_JSON(null);
938
+ rb_check_arity(argc, 0, 1);
939
+ return rb_utf8_str_new("null", 4);
569
940
  }
570
941
 
571
942
  /*
@@ -582,36 +953,40 @@ static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
582
953
  rb_scan_args(argc, argv, "01", &state);
583
954
  Check_Type(string, T_STRING);
584
955
  state = cState_from_state_s(cState, state);
585
- return cState_partial_generate(state, string);
956
+ return cState_partial_generate(state, string, generate_json_string, Qfalse);
957
+ }
958
+
959
+ static void State_mark(void *ptr)
960
+ {
961
+ JSON_Generator_State *state = ptr;
962
+ rb_gc_mark_movable(state->indent);
963
+ rb_gc_mark_movable(state->space);
964
+ rb_gc_mark_movable(state->space_before);
965
+ rb_gc_mark_movable(state->object_nl);
966
+ rb_gc_mark_movable(state->array_nl);
967
+ rb_gc_mark_movable(state->as_json);
968
+ }
969
+
970
+ static void State_compact(void *ptr)
971
+ {
972
+ JSON_Generator_State *state = ptr;
973
+ state->indent = rb_gc_location(state->indent);
974
+ state->space = rb_gc_location(state->space);
975
+ state->space_before = rb_gc_location(state->space_before);
976
+ state->object_nl = rb_gc_location(state->object_nl);
977
+ state->array_nl = rb_gc_location(state->array_nl);
978
+ state->as_json = rb_gc_location(state->as_json);
586
979
  }
587
980
 
588
981
  static void State_free(void *ptr)
589
982
  {
590
983
  JSON_Generator_State *state = ptr;
591
- if (state->indent) ruby_xfree(state->indent);
592
- if (state->space) ruby_xfree(state->space);
593
- if (state->space_before) ruby_xfree(state->space_before);
594
- if (state->object_nl) ruby_xfree(state->object_nl);
595
- if (state->array_nl) ruby_xfree(state->array_nl);
596
- if (state->array_delim) fbuffer_free(state->array_delim);
597
- if (state->object_delim) fbuffer_free(state->object_delim);
598
- if (state->object_delim2) fbuffer_free(state->object_delim2);
599
984
  ruby_xfree(state);
600
985
  }
601
986
 
602
987
  static size_t State_memsize(const void *ptr)
603
988
  {
604
- const JSON_Generator_State *state = ptr;
605
- size_t size = sizeof(*state);
606
- if (state->indent) size += state->indent_len + 1;
607
- if (state->space) size += state->space_len + 1;
608
- if (state->space_before) size += state->space_before_len + 1;
609
- if (state->object_nl) size += state->object_nl_len + 1;
610
- if (state->array_nl) size += state->array_nl_len + 1;
611
- if (state->array_delim) size += FBUFFER_CAPA(state->array_delim);
612
- if (state->object_delim) size += FBUFFER_CAPA(state->object_delim);
613
- if (state->object_delim2) size += FBUFFER_CAPA(state->object_delim2);
614
- return size;
989
+ return sizeof(JSON_Generator_State);
615
990
  }
616
991
 
617
992
  #ifndef HAVE_RB_EXT_RACTOR_SAFE
@@ -619,508 +994,517 @@ static size_t State_memsize(const void *ptr)
619
994
  # define RUBY_TYPED_FROZEN_SHAREABLE 0
620
995
  #endif
621
996
 
622
- #ifdef NEW_TYPEDDATA_WRAPPER
623
997
  static const rb_data_type_t JSON_Generator_State_type = {
624
998
  "JSON/Generator/State",
625
- {NULL, State_free, State_memsize,},
626
- #ifdef RUBY_TYPED_FREE_IMMEDIATELY
999
+ {
1000
+ .dmark = State_mark,
1001
+ .dfree = State_free,
1002
+ .dsize = State_memsize,
1003
+ .dcompact = State_compact,
1004
+ },
627
1005
  0, 0,
628
- RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE,
629
- #endif
1006
+ RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE,
630
1007
  };
631
- #endif
632
1008
 
633
- static VALUE cState_s_allocate(VALUE klass)
1009
+ static void state_init(JSON_Generator_State *state)
634
1010
  {
635
- JSON_Generator_State *state;
636
- return TypedData_Make_Struct(klass, JSON_Generator_State,
637
- &JSON_Generator_State_type, state);
638
- }
639
-
640
- /*
641
- * call-seq: configure(opts)
642
- *
643
- * Configure this State instance with the Hash _opts_, and return
644
- * itself.
645
- */
646
- static VALUE cState_configure(VALUE self, VALUE opts)
647
- {
648
- VALUE tmp;
649
- GET_STATE(self);
650
- tmp = rb_check_convert_type(opts, T_HASH, "Hash", "to_hash");
651
- if (NIL_P(tmp)) tmp = rb_convert_type(opts, T_HASH, "Hash", "to_h");
652
- opts = tmp;
653
- tmp = rb_hash_aref(opts, ID2SYM(i_indent));
654
- if (RTEST(tmp)) {
655
- unsigned long len;
656
- Check_Type(tmp, T_STRING);
657
- len = RSTRING_LEN(tmp);
658
- state->indent = fstrndup(RSTRING_PTR(tmp), len + 1);
659
- state->indent_len = len;
660
- }
661
- tmp = rb_hash_aref(opts, ID2SYM(i_space));
662
- if (RTEST(tmp)) {
663
- unsigned long len;
664
- Check_Type(tmp, T_STRING);
665
- len = RSTRING_LEN(tmp);
666
- state->space = fstrndup(RSTRING_PTR(tmp), len + 1);
667
- state->space_len = len;
668
- }
669
- tmp = rb_hash_aref(opts, ID2SYM(i_space_before));
670
- if (RTEST(tmp)) {
671
- unsigned long len;
672
- Check_Type(tmp, T_STRING);
673
- len = RSTRING_LEN(tmp);
674
- state->space_before = fstrndup(RSTRING_PTR(tmp), len + 1);
675
- state->space_before_len = len;
676
- }
677
- tmp = rb_hash_aref(opts, ID2SYM(i_array_nl));
678
- if (RTEST(tmp)) {
679
- unsigned long len;
680
- Check_Type(tmp, T_STRING);
681
- len = RSTRING_LEN(tmp);
682
- state->array_nl = fstrndup(RSTRING_PTR(tmp), len + 1);
683
- state->array_nl_len = len;
684
- }
685
- tmp = rb_hash_aref(opts, ID2SYM(i_object_nl));
686
- if (RTEST(tmp)) {
687
- unsigned long len;
688
- Check_Type(tmp, T_STRING);
689
- len = RSTRING_LEN(tmp);
690
- state->object_nl = fstrndup(RSTRING_PTR(tmp), len + 1);
691
- state->object_nl_len = len;
692
- }
693
- tmp = ID2SYM(i_max_nesting);
694
1011
  state->max_nesting = 100;
695
- if (option_given_p(opts, tmp)) {
696
- VALUE max_nesting = rb_hash_aref(opts, tmp);
697
- if (RTEST(max_nesting)) {
698
- Check_Type(max_nesting, T_FIXNUM);
699
- state->max_nesting = FIX2LONG(max_nesting);
700
- } else {
701
- state->max_nesting = 0;
702
- }
703
- }
704
- tmp = ID2SYM(i_depth);
705
- state->depth = 0;
706
- if (option_given_p(opts, tmp)) {
707
- VALUE depth = rb_hash_aref(opts, tmp);
708
- if (RTEST(depth)) {
709
- Check_Type(depth, T_FIXNUM);
710
- state->depth = FIX2LONG(depth);
711
- } else {
712
- state->depth = 0;
713
- }
714
- }
715
- tmp = ID2SYM(i_buffer_initial_length);
716
- if (option_given_p(opts, tmp)) {
717
- VALUE buffer_initial_length = rb_hash_aref(opts, tmp);
718
- if (RTEST(buffer_initial_length)) {
719
- long initial_length;
720
- Check_Type(buffer_initial_length, T_FIXNUM);
721
- initial_length = FIX2LONG(buffer_initial_length);
722
- if (initial_length > 0) state->buffer_initial_length = initial_length;
723
- }
724
- }
725
- tmp = rb_hash_aref(opts, ID2SYM(i_allow_nan));
726
- state->allow_nan = RTEST(tmp);
727
- tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only));
728
- state->ascii_only = RTEST(tmp);
729
- tmp = rb_hash_aref(opts, ID2SYM(i_escape_slash));
730
- state->escape_slash = RTEST(tmp);
731
- return self;
1012
+ state->buffer_initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT;
732
1013
  }
733
1014
 
734
- static void set_state_ivars(VALUE hash, VALUE state)
1015
+ static VALUE cState_s_allocate(VALUE klass)
735
1016
  {
736
- VALUE ivars = rb_obj_instance_variables(state);
737
- int i = 0;
738
- for (i = 0; i < RARRAY_LEN(ivars); i++) {
739
- VALUE key = rb_funcall(rb_ary_entry(ivars, i), i_to_s, 0);
740
- long key_len = RSTRING_LEN(key);
741
- VALUE value = rb_iv_get(state, StringValueCStr(key));
742
- rb_hash_aset(hash, rb_str_intern(rb_str_substr(key, 1, key_len - 1)), value);
743
- }
1017
+ JSON_Generator_State *state;
1018
+ VALUE obj = TypedData_Make_Struct(klass, JSON_Generator_State, &JSON_Generator_State_type, state);
1019
+ state_init(state);
1020
+ return obj;
744
1021
  }
745
1022
 
746
- /*
747
- * call-seq: to_h
748
- *
749
- * Returns the configuration instance variables as a hash, that can be
750
- * passed to the configure method.
751
- */
752
- static VALUE cState_to_h(VALUE self)
1023
+ static void vstate_spill(struct generate_json_data *data)
753
1024
  {
754
- VALUE result = rb_hash_new();
755
- GET_STATE(self);
756
- set_state_ivars(result, self);
757
- rb_hash_aset(result, ID2SYM(i_indent), rb_str_new(state->indent, state->indent_len));
758
- rb_hash_aset(result, ID2SYM(i_space), rb_str_new(state->space, state->space_len));
759
- rb_hash_aset(result, ID2SYM(i_space_before), rb_str_new(state->space_before, state->space_before_len));
760
- rb_hash_aset(result, ID2SYM(i_object_nl), rb_str_new(state->object_nl, state->object_nl_len));
761
- rb_hash_aset(result, ID2SYM(i_array_nl), rb_str_new(state->array_nl, state->array_nl_len));
762
- rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
763
- rb_hash_aset(result, ID2SYM(i_ascii_only), state->ascii_only ? Qtrue : Qfalse);
764
- rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
765
- rb_hash_aset(result, ID2SYM(i_escape_slash), state->escape_slash ? Qtrue : Qfalse);
766
- rb_hash_aset(result, ID2SYM(i_depth), LONG2FIX(state->depth));
767
- rb_hash_aset(result, ID2SYM(i_buffer_initial_length), LONG2FIX(state->buffer_initial_length));
768
- return result;
1025
+ VALUE vstate = cState_s_allocate(cState);
1026
+ GET_STATE(vstate);
1027
+ MEMCPY(state, data->state, JSON_Generator_State, 1);
1028
+ data->state = state;
1029
+ data->vstate = vstate;
1030
+ RB_OBJ_WRITTEN(vstate, Qundef, state->indent);
1031
+ RB_OBJ_WRITTEN(vstate, Qundef, state->space);
1032
+ RB_OBJ_WRITTEN(vstate, Qundef, state->space_before);
1033
+ RB_OBJ_WRITTEN(vstate, Qundef, state->object_nl);
1034
+ RB_OBJ_WRITTEN(vstate, Qundef, state->array_nl);
1035
+ RB_OBJ_WRITTEN(vstate, Qundef, state->as_json);
769
1036
  }
770
1037
 
771
- /*
772
- * call-seq: [](name)
773
- *
774
- * Returns the value returned by method +name+.
775
- */
776
- static VALUE cState_aref(VALUE self, VALUE name)
1038
+ static inline VALUE vstate_get(struct generate_json_data *data)
777
1039
  {
778
- name = rb_funcall(name, i_to_s, 0);
779
- if (RTEST(rb_funcall(self, i_respond_to_p, 1, name))) {
780
- return rb_funcall(self, i_send, 1, name);
781
- } else {
782
- return rb_attr_get(self, rb_intern_str(rb_str_concat(rb_str_new2("@"), name)));
1040
+ if (RB_UNLIKELY(!data->vstate)) {
1041
+ vstate_spill(data);
783
1042
  }
1043
+ return data->vstate;
784
1044
  }
785
1045
 
786
- /*
787
- * call-seq: []=(name, value)
788
- *
789
- * Sets the attribute name to value.
790
- */
791
- static VALUE cState_aset(VALUE self, VALUE name, VALUE value)
1046
+ struct hash_foreach_arg {
1047
+ struct generate_json_data *data;
1048
+ int iter;
1049
+ };
1050
+
1051
+ static VALUE
1052
+ convert_string_subclass(VALUE key)
792
1053
  {
793
- VALUE name_writer;
1054
+ VALUE key_to_s = rb_funcall(key, i_to_s, 0);
794
1055
 
795
- name = rb_funcall(name, i_to_s, 0);
796
- name_writer = rb_str_cat2(rb_str_dup(name), "=");
797
- if (RTEST(rb_funcall(self, i_respond_to_p, 1, name_writer))) {
798
- return rb_funcall(self, i_send, 2, name_writer, value);
799
- } else {
800
- rb_ivar_set(self, rb_intern_str(rb_str_concat(rb_str_new2("@"), name)), value);
1056
+ if (RB_UNLIKELY(!RB_TYPE_P(key_to_s, T_STRING))) {
1057
+ VALUE cname = rb_obj_class(key);
1058
+ rb_raise(rb_eTypeError,
1059
+ "can't convert %"PRIsVALUE" to %s (%"PRIsVALUE"#%s gives %"PRIsVALUE")",
1060
+ cname, "String", cname, "to_s", rb_obj_class(key_to_s));
801
1061
  }
802
- return Qnil;
803
- }
804
1062
 
805
- struct hash_foreach_arg {
806
- FBuffer *buffer;
807
- JSON_Generator_State *state;
808
- VALUE Vstate;
809
- int iter;
810
- };
1063
+ return key_to_s;
1064
+ }
811
1065
 
812
1066
  static int
813
1067
  json_object_i(VALUE key, VALUE val, VALUE _arg)
814
1068
  {
815
1069
  struct hash_foreach_arg *arg = (struct hash_foreach_arg *)_arg;
816
- FBuffer *buffer = arg->buffer;
817
- JSON_Generator_State *state = arg->state;
818
- VALUE Vstate = arg->Vstate;
819
-
820
- char *object_nl = state->object_nl;
821
- long object_nl_len = state->object_nl_len;
822
- char *indent = state->indent;
823
- long indent_len = state->indent_len;
824
- char *delim = FBUFFER_PTR(state->object_delim);
825
- long delim_len = FBUFFER_LEN(state->object_delim);
826
- char *delim2 = FBUFFER_PTR(state->object_delim2);
827
- long delim2_len = FBUFFER_LEN(state->object_delim2);
1070
+ struct generate_json_data *data = arg->data;
1071
+
1072
+ FBuffer *buffer = data->buffer;
1073
+ JSON_Generator_State *state = data->state;
1074
+
828
1075
  long depth = state->depth;
829
1076
  int j;
830
- VALUE klass, key_to_s;
831
1077
 
832
- if (arg->iter > 0) fbuffer_append(buffer, delim, delim_len);
833
- if (object_nl) {
834
- fbuffer_append(buffer, object_nl, object_nl_len);
1078
+ if (arg->iter > 0) fbuffer_append_char(buffer, ',');
1079
+ if (RB_UNLIKELY(data->state->object_nl)) {
1080
+ fbuffer_append_str(buffer, data->state->object_nl);
835
1081
  }
836
- if (indent) {
1082
+ if (RB_UNLIKELY(data->state->indent)) {
837
1083
  for (j = 0; j < depth; j++) {
838
- fbuffer_append(buffer, indent, indent_len);
1084
+ fbuffer_append_str(buffer, data->state->indent);
839
1085
  }
840
1086
  }
841
1087
 
842
- klass = CLASS_OF(key);
843
- if (klass == rb_cString) {
844
- key_to_s = key;
845
- } else if (klass == rb_cSymbol) {
846
- key_to_s = rb_id2str(SYM2ID(key));
1088
+ VALUE key_to_s;
1089
+ switch (rb_type(key)) {
1090
+ case T_STRING:
1091
+ if (RB_LIKELY(RBASIC_CLASS(key) == rb_cString)) {
1092
+ key_to_s = key;
1093
+ } else {
1094
+ key_to_s = convert_string_subclass(key);
1095
+ }
1096
+ break;
1097
+ case T_SYMBOL:
1098
+ key_to_s = rb_sym2str(key);
1099
+ break;
1100
+ default:
1101
+ key_to_s = rb_convert_type(key, T_STRING, "String", "to_s");
1102
+ break;
1103
+ }
1104
+
1105
+ if (RB_LIKELY(RBASIC_CLASS(key_to_s) == rb_cString)) {
1106
+ generate_json_string(buffer, data, key_to_s);
847
1107
  } else {
848
- key_to_s = rb_funcall(key, i_to_s, 0);
1108
+ generate_json(buffer, data, key_to_s);
849
1109
  }
850
- Check_Type(key_to_s, T_STRING);
851
- generate_json(buffer, Vstate, state, key_to_s);
852
- fbuffer_append(buffer, delim2, delim2_len);
853
- generate_json(buffer, Vstate, state, val);
1110
+ if (RB_UNLIKELY(state->space_before)) fbuffer_append_str(buffer, data->state->space_before);
1111
+ fbuffer_append_char(buffer, ':');
1112
+ if (RB_UNLIKELY(state->space)) fbuffer_append_str(buffer, data->state->space);
1113
+ generate_json(buffer, data, val);
854
1114
 
855
1115
  arg->iter++;
856
1116
  return ST_CONTINUE;
857
1117
  }
858
1118
 
859
- static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1119
+ static inline long increase_depth(struct generate_json_data *data)
860
1120
  {
861
- char *object_nl = state->object_nl;
862
- long object_nl_len = state->object_nl_len;
863
- char *indent = state->indent;
864
- long indent_len = state->indent_len;
865
- long max_nesting = state->max_nesting;
1121
+ JSON_Generator_State *state = data->state;
866
1122
  long depth = ++state->depth;
1123
+ if (RB_UNLIKELY(depth > state->max_nesting && state->max_nesting)) {
1124
+ rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
1125
+ }
1126
+ return depth;
1127
+ }
1128
+
1129
+ static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1130
+ {
867
1131
  int j;
868
- struct hash_foreach_arg arg;
1132
+ long depth = increase_depth(data);
869
1133
 
870
- if (max_nesting != 0 && depth > max_nesting) {
871
- fbuffer_free(buffer);
872
- rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
1134
+ if (RHASH_SIZE(obj) == 0) {
1135
+ fbuffer_append(buffer, "{}", 2);
1136
+ --data->state->depth;
1137
+ return;
873
1138
  }
1139
+
874
1140
  fbuffer_append_char(buffer, '{');
875
1141
 
876
- arg.buffer = buffer;
877
- arg.state = state;
878
- arg.Vstate = Vstate;
879
- arg.iter = 0;
1142
+ struct hash_foreach_arg arg = {
1143
+ .data = data,
1144
+ .iter = 0,
1145
+ };
880
1146
  rb_hash_foreach(obj, json_object_i, (VALUE)&arg);
881
1147
 
882
- depth = --state->depth;
883
- if (object_nl) {
884
- fbuffer_append(buffer, object_nl, object_nl_len);
885
- if (indent) {
1148
+ depth = --data->state->depth;
1149
+ if (RB_UNLIKELY(data->state->object_nl)) {
1150
+ fbuffer_append_str(buffer, data->state->object_nl);
1151
+ if (RB_UNLIKELY(data->state->indent)) {
886
1152
  for (j = 0; j < depth; j++) {
887
- fbuffer_append(buffer, indent, indent_len);
1153
+ fbuffer_append_str(buffer, data->state->indent);
888
1154
  }
889
1155
  }
890
1156
  }
891
1157
  fbuffer_append_char(buffer, '}');
892
1158
  }
893
1159
 
894
- static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1160
+ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
895
1161
  {
896
- char *array_nl = state->array_nl;
897
- long array_nl_len = state->array_nl_len;
898
- char *indent = state->indent;
899
- long indent_len = state->indent_len;
900
- long max_nesting = state->max_nesting;
901
- char *delim = FBUFFER_PTR(state->array_delim);
902
- long delim_len = FBUFFER_LEN(state->array_delim);
903
- long depth = ++state->depth;
904
1162
  int i, j;
905
- if (max_nesting != 0 && depth > max_nesting) {
906
- fbuffer_free(buffer);
907
- rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
1163
+ long depth = increase_depth(data);
1164
+
1165
+ if (RARRAY_LEN(obj) == 0) {
1166
+ fbuffer_append(buffer, "[]", 2);
1167
+ --data->state->depth;
1168
+ return;
908
1169
  }
1170
+
909
1171
  fbuffer_append_char(buffer, '[');
910
- if (array_nl) fbuffer_append(buffer, array_nl, array_nl_len);
911
- for(i = 0; i < RARRAY_LEN(obj); i++) {
912
- if (i > 0) fbuffer_append(buffer, delim, delim_len);
913
- if (indent) {
1172
+ if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
1173
+ for (i = 0; i < RARRAY_LEN(obj); i++) {
1174
+ if (i > 0) {
1175
+ fbuffer_append_char(buffer, ',');
1176
+ if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
1177
+ }
1178
+ if (RB_UNLIKELY(data->state->indent)) {
914
1179
  for (j = 0; j < depth; j++) {
915
- fbuffer_append(buffer, indent, indent_len);
1180
+ fbuffer_append_str(buffer, data->state->indent);
916
1181
  }
917
1182
  }
918
- generate_json(buffer, Vstate, state, rb_ary_entry(obj, i));
1183
+ generate_json(buffer, data, RARRAY_AREF(obj, i));
919
1184
  }
920
- state->depth = --depth;
921
- if (array_nl) {
922
- fbuffer_append(buffer, array_nl, array_nl_len);
923
- if (indent) {
1185
+ data->state->depth = --depth;
1186
+ if (RB_UNLIKELY(data->state->array_nl)) {
1187
+ fbuffer_append_str(buffer, data->state->array_nl);
1188
+ if (RB_UNLIKELY(data->state->indent)) {
924
1189
  for (j = 0; j < depth; j++) {
925
- fbuffer_append(buffer, indent, indent_len);
1190
+ fbuffer_append_str(buffer, data->state->indent);
926
1191
  }
927
1192
  }
928
1193
  }
929
1194
  fbuffer_append_char(buffer, ']');
930
1195
  }
931
1196
 
932
- #ifdef HAVE_RUBY_ENCODING_H
933
- static int enc_utf8_compatible_p(rb_encoding *enc)
1197
+ static inline int enc_utf8_compatible_p(int enc_idx)
934
1198
  {
935
- if (enc == rb_usascii_encoding()) return 1;
936
- if (enc == rb_utf8_encoding()) return 1;
1199
+ if (enc_idx == usascii_encindex) return 1;
1200
+ if (enc_idx == utf8_encindex) return 1;
937
1201
  return 0;
938
1202
  }
939
- #endif
940
1203
 
941
- static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1204
+ static VALUE encode_json_string_try(VALUE str)
942
1205
  {
1206
+ return rb_funcall(str, i_encode, 1, Encoding_UTF_8);
1207
+ }
1208
+
1209
+ static VALUE encode_json_string_rescue(VALUE str, VALUE exception)
1210
+ {
1211
+ raise_generator_error_str(str, rb_funcall(exception, rb_intern("message"), 0));
1212
+ return Qundef;
1213
+ }
1214
+
1215
+ static inline VALUE ensure_valid_encoding(VALUE str)
1216
+ {
1217
+ int encindex = RB_ENCODING_GET(str);
1218
+ VALUE utf8_string;
1219
+ if (RB_UNLIKELY(!enc_utf8_compatible_p(encindex))) {
1220
+ if (encindex == binary_encindex) {
1221
+ utf8_string = rb_enc_associate_index(rb_str_dup(str), utf8_encindex);
1222
+ switch (rb_enc_str_coderange(utf8_string)) {
1223
+ case ENC_CODERANGE_7BIT:
1224
+ return utf8_string;
1225
+ case ENC_CODERANGE_VALID:
1226
+ // For historical reason, we silently reinterpret binary strings as UTF-8 if it would work.
1227
+ // TODO: Raise in 3.0.0
1228
+ rb_warn("JSON.generate: UTF-8 string passed as BINARY, this will raise an encoding error in json 3.0");
1229
+ return utf8_string;
1230
+ break;
1231
+ }
1232
+ }
1233
+
1234
+ str = rb_rescue(encode_json_string_try, str, encode_json_string_rescue, str);
1235
+ }
1236
+ return str;
1237
+ }
1238
+
1239
+ static void generate_json_string(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1240
+ {
1241
+ obj = ensure_valid_encoding(obj);
1242
+
943
1243
  fbuffer_append_char(buffer, '"');
944
- #ifdef HAVE_RUBY_ENCODING_H
945
- if (!enc_utf8_compatible_p(rb_enc_get(obj))) {
946
- obj = rb_str_export_to_enc(obj, rb_utf8_encoding());
1244
+
1245
+ long len;
1246
+ search_state search;
1247
+ search.buffer = buffer;
1248
+ RSTRING_GETMEM(obj, search.ptr, len);
1249
+ search.cursor = search.ptr;
1250
+ search.end = search.ptr + len;
1251
+
1252
+ #ifdef HAVE_SIMD
1253
+ search.matches_mask = 0;
1254
+ search.has_matches = false;
1255
+ search.chunk_base = NULL;
1256
+ #endif /* HAVE_SIMD */
1257
+
1258
+ switch (rb_enc_str_coderange(obj)) {
1259
+ case ENC_CODERANGE_7BIT:
1260
+ case ENC_CODERANGE_VALID:
1261
+ if (RB_UNLIKELY(data->state->ascii_only)) {
1262
+ convert_UTF8_to_ASCII_only_JSON(&search, data->state->script_safe ? script_safe_escape_table : ascii_only_escape_table);
1263
+ } else if (RB_UNLIKELY(data->state->script_safe)) {
1264
+ convert_UTF8_to_script_safe_JSON(&search);
1265
+ } else {
1266
+ convert_UTF8_to_JSON(&search);
1267
+ }
1268
+ break;
1269
+ default:
1270
+ raise_generator_error(obj, "source sequence is illegal/malformed utf-8");
1271
+ break;
947
1272
  }
948
- #endif
949
- if (state->ascii_only) {
950
- convert_UTF8_to_JSON_ASCII(buffer, obj, state->escape_slash);
1273
+ fbuffer_append_char(buffer, '"');
1274
+ }
1275
+
1276
+ static void generate_json_fallback(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1277
+ {
1278
+ VALUE tmp;
1279
+ if (rb_respond_to(obj, i_to_json)) {
1280
+ tmp = rb_funcall(obj, i_to_json, 1, vstate_get(data));
1281
+ Check_Type(tmp, T_STRING);
1282
+ fbuffer_append_str(buffer, tmp);
951
1283
  } else {
952
- convert_UTF8_to_JSON(buffer, obj, state->escape_slash);
1284
+ tmp = rb_funcall(obj, i_to_s, 0);
1285
+ Check_Type(tmp, T_STRING);
1286
+ generate_json_string(buffer, data, tmp);
953
1287
  }
954
- fbuffer_append_char(buffer, '"');
955
1288
  }
956
1289
 
957
- static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1290
+ static inline void generate_json_symbol(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1291
+ {
1292
+ if (data->state->strict) {
1293
+ generate_json_string(buffer, data, rb_sym2str(obj));
1294
+ } else {
1295
+ generate_json_fallback(buffer, data, obj);
1296
+ }
1297
+ }
1298
+
1299
+ static void generate_json_null(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
958
1300
  {
959
1301
  fbuffer_append(buffer, "null", 4);
960
1302
  }
961
1303
 
962
- static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1304
+ static void generate_json_false(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
963
1305
  {
964
1306
  fbuffer_append(buffer, "false", 5);
965
1307
  }
966
1308
 
967
- static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1309
+ static void generate_json_true(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
968
1310
  {
969
1311
  fbuffer_append(buffer, "true", 4);
970
1312
  }
971
1313
 
972
- static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1314
+ static void generate_json_fixnum(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
973
1315
  {
974
1316
  fbuffer_append_long(buffer, FIX2LONG(obj));
975
1317
  }
976
1318
 
977
- static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1319
+ static void generate_json_bignum(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
978
1320
  {
979
1321
  VALUE tmp = rb_funcall(obj, i_to_s, 0);
980
1322
  fbuffer_append_str(buffer, tmp);
981
1323
  }
982
1324
 
983
1325
  #ifdef RUBY_INTEGER_UNIFICATION
984
- static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1326
+ static void generate_json_integer(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
985
1327
  {
986
1328
  if (FIXNUM_P(obj))
987
- generate_json_fixnum(buffer, Vstate, state, obj);
1329
+ generate_json_fixnum(buffer, data, obj);
988
1330
  else
989
- generate_json_bignum(buffer, Vstate, state, obj);
1331
+ generate_json_bignum(buffer, data, obj);
990
1332
  }
991
1333
  #endif
992
- static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1334
+
1335
+ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
993
1336
  {
994
1337
  double value = RFLOAT_VALUE(obj);
995
- char allow_nan = state->allow_nan;
996
- VALUE tmp = rb_funcall(obj, i_to_s, 0);
997
- if (!allow_nan) {
998
- if (isinf(value)) {
999
- fbuffer_free(buffer);
1000
- rb_raise(eGeneratorError, "%"PRIsVALUE" not allowed in JSON", RB_OBJ_STRING(tmp));
1001
- } else if (isnan(value)) {
1002
- fbuffer_free(buffer);
1003
- rb_raise(eGeneratorError, "%"PRIsVALUE" not allowed in JSON", RB_OBJ_STRING(tmp));
1338
+ char allow_nan = data->state->allow_nan;
1339
+ if (isinf(value) || isnan(value)) {
1340
+ /* for NaN and Infinity values we either raise an error or rely on Float#to_s. */
1341
+ if (!allow_nan) {
1342
+ if (data->state->strict && data->state->as_json) {
1343
+ VALUE casted_obj = rb_proc_call_with_block(data->state->as_json, 1, &obj, Qnil);
1344
+ if (casted_obj != obj) {
1345
+ increase_depth(data);
1346
+ generate_json(buffer, data, casted_obj);
1347
+ data->state->depth--;
1348
+ return;
1349
+ }
1350
+ }
1351
+ raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", rb_funcall(obj, i_to_s, 0));
1004
1352
  }
1353
+
1354
+ VALUE tmp = rb_funcall(obj, i_to_s, 0);
1355
+ fbuffer_append_str(buffer, tmp);
1356
+ return;
1005
1357
  }
1006
- fbuffer_append_str(buffer, tmp);
1358
+
1359
+ /* This implementation writes directly into the buffer. We reserve
1360
+ * the 28 characters that fpconv_dtoa states as its maximum.
1361
+ */
1362
+ fbuffer_inc_capa(buffer, 28);
1363
+ char* d = buffer->ptr + buffer->len;
1364
+ int len = fpconv_dtoa(value, d);
1365
+
1366
+ /* fpconv_dtoa converts a float to its shortest string representation,
1367
+ * but it adds a ".0" if this is a plain integer.
1368
+ */
1369
+ fbuffer_consumed(buffer, len);
1007
1370
  }
1008
1371
 
1009
- static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
1372
+ static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1010
1373
  {
1011
- VALUE tmp;
1012
- VALUE klass = CLASS_OF(obj);
1013
- if (klass == rb_cHash) {
1014
- generate_json_object(buffer, Vstate, state, obj);
1015
- } else if (klass == rb_cArray) {
1016
- generate_json_array(buffer, Vstate, state, obj);
1017
- } else if (klass == rb_cString) {
1018
- generate_json_string(buffer, Vstate, state, obj);
1019
- } else if (obj == Qnil) {
1020
- generate_json_null(buffer, Vstate, state, obj);
1374
+ VALUE fragment = RSTRUCT_GET(obj, 0);
1375
+ Check_Type(fragment, T_STRING);
1376
+ fbuffer_append_str(buffer, fragment);
1377
+ }
1378
+
1379
+ static void generate_json(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
1380
+ {
1381
+ bool as_json_called = false;
1382
+ start:
1383
+ if (obj == Qnil) {
1384
+ generate_json_null(buffer, data, obj);
1021
1385
  } else if (obj == Qfalse) {
1022
- generate_json_false(buffer, Vstate, state, obj);
1386
+ generate_json_false(buffer, data, obj);
1023
1387
  } else if (obj == Qtrue) {
1024
- generate_json_true(buffer, Vstate, state, obj);
1025
- } else if (FIXNUM_P(obj)) {
1026
- generate_json_fixnum(buffer, Vstate, state, obj);
1027
- } else if (RB_TYPE_P(obj, T_BIGNUM)) {
1028
- generate_json_bignum(buffer, Vstate, state, obj);
1029
- } else if (klass == rb_cFloat) {
1030
- generate_json_float(buffer, Vstate, state, obj);
1031
- } else if (rb_respond_to(obj, i_to_json)) {
1032
- tmp = rb_funcall(obj, i_to_json, 1, Vstate);
1033
- Check_Type(tmp, T_STRING);
1034
- fbuffer_append_str(buffer, tmp);
1388
+ generate_json_true(buffer, data, obj);
1389
+ } else if (RB_SPECIAL_CONST_P(obj)) {
1390
+ if (RB_FIXNUM_P(obj)) {
1391
+ generate_json_fixnum(buffer, data, obj);
1392
+ } else if (RB_FLONUM_P(obj)) {
1393
+ generate_json_float(buffer, data, obj);
1394
+ } else if (RB_STATIC_SYM_P(obj)) {
1395
+ generate_json_symbol(buffer, data, obj);
1396
+ } else {
1397
+ goto general;
1398
+ }
1035
1399
  } else {
1036
- tmp = rb_funcall(obj, i_to_s, 0);
1037
- Check_Type(tmp, T_STRING);
1038
- generate_json_string(buffer, Vstate, state, tmp);
1400
+ VALUE klass = RBASIC_CLASS(obj);
1401
+ switch (RB_BUILTIN_TYPE(obj)) {
1402
+ case T_BIGNUM:
1403
+ generate_json_bignum(buffer, data, obj);
1404
+ break;
1405
+ case T_HASH:
1406
+ if (klass != rb_cHash) goto general;
1407
+ generate_json_object(buffer, data, obj);
1408
+ break;
1409
+ case T_ARRAY:
1410
+ if (klass != rb_cArray) goto general;
1411
+ generate_json_array(buffer, data, obj);
1412
+ break;
1413
+ case T_STRING:
1414
+ if (klass != rb_cString) goto general;
1415
+ generate_json_string(buffer, data, obj);
1416
+ break;
1417
+ case T_SYMBOL:
1418
+ generate_json_symbol(buffer, data, obj);
1419
+ break;
1420
+ case T_FLOAT:
1421
+ if (klass != rb_cFloat) goto general;
1422
+ generate_json_float(buffer, data, obj);
1423
+ break;
1424
+ case T_STRUCT:
1425
+ if (klass != cFragment) goto general;
1426
+ generate_json_fragment(buffer, data, obj);
1427
+ break;
1428
+ default:
1429
+ general:
1430
+ if (data->state->strict) {
1431
+ if (RTEST(data->state->as_json) && !as_json_called) {
1432
+ obj = rb_proc_call_with_block(data->state->as_json, 1, &obj, Qnil);
1433
+ as_json_called = true;
1434
+ goto start;
1435
+ } else {
1436
+ raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", CLASS_OF(obj));
1437
+ }
1438
+ } else {
1439
+ generate_json_fallback(buffer, data, obj);
1440
+ }
1441
+ }
1039
1442
  }
1040
1443
  }
1041
1444
 
1042
- static FBuffer *cState_prepare_buffer(VALUE self)
1445
+ static VALUE generate_json_try(VALUE d)
1043
1446
  {
1044
- FBuffer *buffer;
1045
- GET_STATE(self);
1046
- buffer = fbuffer_alloc(state->buffer_initial_length);
1447
+ struct generate_json_data *data = (struct generate_json_data *)d;
1047
1448
 
1048
- if (state->object_delim) {
1049
- fbuffer_clear(state->object_delim);
1050
- } else {
1051
- state->object_delim = fbuffer_alloc(16);
1052
- }
1053
- fbuffer_append_char(state->object_delim, ',');
1054
- if (state->object_delim2) {
1055
- fbuffer_clear(state->object_delim2);
1056
- } else {
1057
- state->object_delim2 = fbuffer_alloc(16);
1058
- }
1059
- if (state->space_before) fbuffer_append(state->object_delim2, state->space_before, state->space_before_len);
1060
- fbuffer_append_char(state->object_delim2, ':');
1061
- if (state->space) fbuffer_append(state->object_delim2, state->space, state->space_len);
1449
+ data->func(data->buffer, data, data->obj);
1062
1450
 
1063
- if (state->array_delim) {
1064
- fbuffer_clear(state->array_delim);
1065
- } else {
1066
- state->array_delim = fbuffer_alloc(16);
1067
- }
1068
- fbuffer_append_char(state->array_delim, ',');
1069
- if (state->array_nl) fbuffer_append(state->array_delim, state->array_nl, state->array_nl_len);
1070
- return buffer;
1451
+ return Qnil;
1071
1452
  }
1072
1453
 
1073
- static VALUE cState_partial_generate(VALUE self, VALUE obj)
1454
+ static VALUE generate_json_rescue(VALUE d, VALUE exc)
1455
+ {
1456
+ struct generate_json_data *data = (struct generate_json_data *)d;
1457
+ fbuffer_free(data->buffer);
1458
+
1459
+ rb_exc_raise(exc);
1460
+
1461
+ return Qundef;
1462
+ }
1463
+
1464
+ static VALUE cState_partial_generate(VALUE self, VALUE obj, generator_func func, VALUE io)
1074
1465
  {
1075
- FBuffer *buffer = cState_prepare_buffer(self);
1076
1466
  GET_STATE(self);
1077
- generate_json(buffer, self, state, obj);
1078
- return fbuffer_to_s(buffer);
1467
+
1468
+ char stack_buffer[FBUFFER_STACK_SIZE];
1469
+ FBuffer buffer = {
1470
+ .io = RTEST(io) ? io : Qfalse,
1471
+ };
1472
+ fbuffer_stack_init(&buffer, state->buffer_initial_length, stack_buffer, FBUFFER_STACK_SIZE);
1473
+
1474
+ struct generate_json_data data = {
1475
+ .buffer = &buffer,
1476
+ .vstate = self,
1477
+ .state = state,
1478
+ .obj = obj,
1479
+ .func = func
1480
+ };
1481
+ rb_rescue(generate_json_try, (VALUE)&data, generate_json_rescue, (VALUE)&data);
1482
+
1483
+ return fbuffer_finalize(&buffer);
1079
1484
  }
1080
1485
 
1081
- /*
1082
- * call-seq: generate(obj)
1486
+ /* call-seq:
1487
+ * generate(obj) -> String
1488
+ * generate(obj, anIO) -> anIO
1083
1489
  *
1084
1490
  * Generates a valid JSON document from object +obj+ and returns the
1085
1491
  * result. If no valid JSON document can be created this method raises a
1086
1492
  * GeneratorError exception.
1087
1493
  */
1088
- static VALUE cState_generate(VALUE self, VALUE obj)
1494
+ static VALUE cState_generate(int argc, VALUE *argv, VALUE self)
1089
1495
  {
1090
- VALUE result = cState_partial_generate(self, obj);
1496
+ rb_check_arity(argc, 1, 2);
1497
+ VALUE obj = argv[0];
1498
+ VALUE io = argc > 1 ? argv[1] : Qnil;
1499
+ VALUE result = cState_partial_generate(self, obj, generate_json, io);
1091
1500
  GET_STATE(self);
1092
1501
  (void)state;
1093
1502
  return result;
1094
1503
  }
1095
1504
 
1096
- /*
1097
- * call-seq: new(opts = {})
1098
- *
1099
- * Instantiates a new State object, configured by _opts_.
1100
- *
1101
- * _opts_ can have the following keys:
1102
- *
1103
- * * *indent*: a string used to indent levels (default: ''),
1104
- * * *space*: a string that is put after, a : or , delimiter (default: ''),
1105
- * * *space_before*: a string that is put before a : pair delimiter (default: ''),
1106
- * * *object_nl*: a string that is put at the end of a JSON object (default: ''),
1107
- * * *array_nl*: a string that is put at the end of a JSON array (default: ''),
1108
- * * *allow_nan*: true if NaN, Infinity, and -Infinity should be
1109
- * generated, otherwise an exception is thrown, if these values are
1110
- * encountered. This options defaults to false.
1111
- * * *ascii_only*: true if only ASCII characters should be generated. This
1112
- * option defaults to false.
1113
- * * *buffer_initial_length*: sets the initial length of the generator's
1114
- * internal buffer.
1115
- */
1116
1505
  static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
1117
1506
  {
1118
- VALUE opts;
1119
- GET_STATE(self);
1120
- state->max_nesting = 100;
1121
- state->buffer_initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT;
1122
- rb_scan_args(argc, argv, "01", &opts);
1123
- if (!NIL_P(opts)) cState_configure(self, opts);
1507
+ rb_warn("The json gem extension was loaded with the stdlib ruby code. You should upgrade rubygems with `gem update --system`");
1124
1508
  return self;
1125
1509
  }
1126
1510
 
@@ -1140,14 +1524,12 @@ static VALUE cState_init_copy(VALUE obj, VALUE orig)
1140
1524
  if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State");
1141
1525
 
1142
1526
  MEMCPY(objState, origState, JSON_Generator_State, 1);
1143
- objState->indent = fstrndup(origState->indent, origState->indent_len);
1144
- objState->space = fstrndup(origState->space, origState->space_len);
1145
- objState->space_before = fstrndup(origState->space_before, origState->space_before_len);
1146
- objState->object_nl = fstrndup(origState->object_nl, origState->object_nl_len);
1147
- objState->array_nl = fstrndup(origState->array_nl, origState->array_nl_len);
1148
- if (origState->array_delim) objState->array_delim = fbuffer_dup(origState->array_delim);
1149
- if (origState->object_delim) objState->object_delim = fbuffer_dup(origState->object_delim);
1150
- if (origState->object_delim2) objState->object_delim2 = fbuffer_dup(origState->object_delim2);
1527
+ objState->indent = origState->indent;
1528
+ objState->space = origState->space;
1529
+ objState->space_before = origState->space_before;
1530
+ objState->object_nl = origState->object_nl;
1531
+ objState->array_nl = origState->array_nl;
1532
+ objState->as_json = origState->as_json;
1151
1533
  return obj;
1152
1534
  }
1153
1535
 
@@ -1177,7 +1559,18 @@ static VALUE cState_from_state_s(VALUE self, VALUE opts)
1177
1559
  static VALUE cState_indent(VALUE self)
1178
1560
  {
1179
1561
  GET_STATE(self);
1180
- return state->indent ? rb_str_new(state->indent, state->indent_len) : rb_str_new2("");
1562
+ return state->indent ? state->indent : rb_str_freeze(rb_utf8_str_new("", 0));
1563
+ }
1564
+
1565
+ static VALUE string_config(VALUE config)
1566
+ {
1567
+ if (RTEST(config)) {
1568
+ Check_Type(config, T_STRING);
1569
+ if (RSTRING_LEN(config)) {
1570
+ return rb_str_new_frozen(config);
1571
+ }
1572
+ }
1573
+ return Qfalse;
1181
1574
  }
1182
1575
 
1183
1576
  /*
@@ -1187,21 +1580,8 @@ static VALUE cState_indent(VALUE self)
1187
1580
  */
1188
1581
  static VALUE cState_indent_set(VALUE self, VALUE indent)
1189
1582
  {
1190
- unsigned long len;
1191
1583
  GET_STATE(self);
1192
- Check_Type(indent, T_STRING);
1193
- len = RSTRING_LEN(indent);
1194
- if (len == 0) {
1195
- if (state->indent) {
1196
- ruby_xfree(state->indent);
1197
- state->indent = NULL;
1198
- state->indent_len = 0;
1199
- }
1200
- } else {
1201
- if (state->indent) ruby_xfree(state->indent);
1202
- state->indent = fstrndup(RSTRING_PTR(indent), len);
1203
- state->indent_len = len;
1204
- }
1584
+ RB_OBJ_WRITE(self, &state->indent, string_config(indent));
1205
1585
  return Qnil;
1206
1586
  }
1207
1587
 
@@ -1214,7 +1594,7 @@ static VALUE cState_indent_set(VALUE self, VALUE indent)
1214
1594
  static VALUE cState_space(VALUE self)
1215
1595
  {
1216
1596
  GET_STATE(self);
1217
- return state->space ? rb_str_new(state->space, state->space_len) : rb_str_new2("");
1597
+ return state->space ? state->space : rb_str_freeze(rb_utf8_str_new("", 0));
1218
1598
  }
1219
1599
 
1220
1600
  /*
@@ -1225,21 +1605,8 @@ static VALUE cState_space(VALUE self)
1225
1605
  */
1226
1606
  static VALUE cState_space_set(VALUE self, VALUE space)
1227
1607
  {
1228
- unsigned long len;
1229
1608
  GET_STATE(self);
1230
- Check_Type(space, T_STRING);
1231
- len = RSTRING_LEN(space);
1232
- if (len == 0) {
1233
- if (state->space) {
1234
- ruby_xfree(state->space);
1235
- state->space = NULL;
1236
- state->space_len = 0;
1237
- }
1238
- } else {
1239
- if (state->space) ruby_xfree(state->space);
1240
- state->space = fstrndup(RSTRING_PTR(space), len);
1241
- state->space_len = len;
1242
- }
1609
+ RB_OBJ_WRITE(self, &state->space, string_config(space));
1243
1610
  return Qnil;
1244
1611
  }
1245
1612
 
@@ -1251,7 +1618,7 @@ static VALUE cState_space_set(VALUE self, VALUE space)
1251
1618
  static VALUE cState_space_before(VALUE self)
1252
1619
  {
1253
1620
  GET_STATE(self);
1254
- return state->space_before ? rb_str_new(state->space_before, state->space_before_len) : rb_str_new2("");
1621
+ return state->space_before ? state->space_before : rb_str_freeze(rb_utf8_str_new("", 0));
1255
1622
  }
1256
1623
 
1257
1624
  /*
@@ -1261,21 +1628,8 @@ static VALUE cState_space_before(VALUE self)
1261
1628
  */
1262
1629
  static VALUE cState_space_before_set(VALUE self, VALUE space_before)
1263
1630
  {
1264
- unsigned long len;
1265
1631
  GET_STATE(self);
1266
- Check_Type(space_before, T_STRING);
1267
- len = RSTRING_LEN(space_before);
1268
- if (len == 0) {
1269
- if (state->space_before) {
1270
- ruby_xfree(state->space_before);
1271
- state->space_before = NULL;
1272
- state->space_before_len = 0;
1273
- }
1274
- } else {
1275
- if (state->space_before) ruby_xfree(state->space_before);
1276
- state->space_before = fstrndup(RSTRING_PTR(space_before), len);
1277
- state->space_before_len = len;
1278
- }
1632
+ RB_OBJ_WRITE(self, &state->space_before, string_config(space_before));
1279
1633
  return Qnil;
1280
1634
  }
1281
1635
 
@@ -1288,7 +1642,7 @@ static VALUE cState_space_before_set(VALUE self, VALUE space_before)
1288
1642
  static VALUE cState_object_nl(VALUE self)
1289
1643
  {
1290
1644
  GET_STATE(self);
1291
- return state->object_nl ? rb_str_new(state->object_nl, state->object_nl_len) : rb_str_new2("");
1645
+ return state->object_nl ? state->object_nl : rb_str_freeze(rb_utf8_str_new("", 0));
1292
1646
  }
1293
1647
 
1294
1648
  /*
@@ -1299,20 +1653,8 @@ static VALUE cState_object_nl(VALUE self)
1299
1653
  */
1300
1654
  static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
1301
1655
  {
1302
- unsigned long len;
1303
1656
  GET_STATE(self);
1304
- Check_Type(object_nl, T_STRING);
1305
- len = RSTRING_LEN(object_nl);
1306
- if (len == 0) {
1307
- if (state->object_nl) {
1308
- ruby_xfree(state->object_nl);
1309
- state->object_nl = NULL;
1310
- }
1311
- } else {
1312
- if (state->object_nl) ruby_xfree(state->object_nl);
1313
- state->object_nl = fstrndup(RSTRING_PTR(object_nl), len);
1314
- state->object_nl_len = len;
1315
- }
1657
+ RB_OBJ_WRITE(self, &state->object_nl, string_config(object_nl));
1316
1658
  return Qnil;
1317
1659
  }
1318
1660
 
@@ -1324,7 +1666,7 @@ static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
1324
1666
  static VALUE cState_array_nl(VALUE self)
1325
1667
  {
1326
1668
  GET_STATE(self);
1327
- return state->array_nl ? rb_str_new(state->array_nl, state->array_nl_len) : rb_str_new2("");
1669
+ return state->array_nl ? state->array_nl : rb_str_freeze(rb_utf8_str_new("", 0));
1328
1670
  }
1329
1671
 
1330
1672
  /*
@@ -1334,23 +1676,33 @@ static VALUE cState_array_nl(VALUE self)
1334
1676
  */
1335
1677
  static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
1336
1678
  {
1337
- unsigned long len;
1338
1679
  GET_STATE(self);
1339
- Check_Type(array_nl, T_STRING);
1340
- len = RSTRING_LEN(array_nl);
1341
- if (len == 0) {
1342
- if (state->array_nl) {
1343
- ruby_xfree(state->array_nl);
1344
- state->array_nl = NULL;
1345
- }
1346
- } else {
1347
- if (state->array_nl) ruby_xfree(state->array_nl);
1348
- state->array_nl = fstrndup(RSTRING_PTR(array_nl), len);
1349
- state->array_nl_len = len;
1350
- }
1680
+ RB_OBJ_WRITE(self, &state->array_nl, string_config(array_nl));
1351
1681
  return Qnil;
1352
1682
  }
1353
1683
 
1684
+ /*
1685
+ * call-seq: as_json()
1686
+ *
1687
+ * This string is put at the end of a line that holds a JSON array.
1688
+ */
1689
+ static VALUE cState_as_json(VALUE self)
1690
+ {
1691
+ GET_STATE(self);
1692
+ return state->as_json;
1693
+ }
1694
+
1695
+ /*
1696
+ * call-seq: as_json=(as_json)
1697
+ *
1698
+ * This string is put at the end of a line that holds a JSON array.
1699
+ */
1700
+ static VALUE cState_as_json_set(VALUE self, VALUE as_json)
1701
+ {
1702
+ GET_STATE(self);
1703
+ RB_OBJ_WRITE(self, &state->as_json, rb_convert_type(as_json, T_DATA, "Proc", "to_proc"));
1704
+ return Qnil;
1705
+ }
1354
1706
 
1355
1707
  /*
1356
1708
  * call-seq: check_circular?
@@ -1376,6 +1728,11 @@ static VALUE cState_max_nesting(VALUE self)
1376
1728
  return LONG2FIX(state->max_nesting);
1377
1729
  }
1378
1730
 
1731
+ static long long_config(VALUE num)
1732
+ {
1733
+ return RTEST(num) ? FIX2LONG(num) : 0;
1734
+ }
1735
+
1379
1736
  /*
1380
1737
  * call-seq: max_nesting=(depth)
1381
1738
  *
@@ -1385,32 +1742,63 @@ static VALUE cState_max_nesting(VALUE self)
1385
1742
  static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
1386
1743
  {
1387
1744
  GET_STATE(self);
1388
- Check_Type(depth, T_FIXNUM);
1389
- return state->max_nesting = FIX2LONG(depth);
1745
+ state->max_nesting = long_config(depth);
1746
+ return Qnil;
1390
1747
  }
1391
1748
 
1392
1749
  /*
1393
- * call-seq: escape_slash
1750
+ * call-seq: script_safe
1394
1751
  *
1395
1752
  * If this boolean is true, the forward slashes will be escaped in
1396
1753
  * the json output.
1397
1754
  */
1398
- static VALUE cState_escape_slash(VALUE self)
1755
+ static VALUE cState_script_safe(VALUE self)
1399
1756
  {
1400
1757
  GET_STATE(self);
1401
- return state->escape_slash ? Qtrue : Qfalse;
1758
+ return state->script_safe ? Qtrue : Qfalse;
1402
1759
  }
1403
1760
 
1404
1761
  /*
1405
- * call-seq: escape_slash=(depth)
1762
+ * call-seq: script_safe=(enable)
1406
1763
  *
1407
1764
  * This sets whether or not the forward slashes will be escaped in
1408
1765
  * the json output.
1409
1766
  */
1410
- static VALUE cState_escape_slash_set(VALUE self, VALUE enable)
1767
+ static VALUE cState_script_safe_set(VALUE self, VALUE enable)
1411
1768
  {
1412
1769
  GET_STATE(self);
1413
- state->escape_slash = RTEST(enable);
1770
+ state->script_safe = RTEST(enable);
1771
+ return Qnil;
1772
+ }
1773
+
1774
+ /*
1775
+ * call-seq: strict
1776
+ *
1777
+ * If this boolean is false, types unsupported by the JSON format will
1778
+ * be serialized as strings.
1779
+ * If this boolean is true, types unsupported by the JSON format will
1780
+ * raise a JSON::GeneratorError.
1781
+ */
1782
+ static VALUE cState_strict(VALUE self)
1783
+ {
1784
+ GET_STATE(self);
1785
+ return state->strict ? Qtrue : Qfalse;
1786
+ }
1787
+
1788
+ /*
1789
+ * call-seq: strict=(enable)
1790
+ *
1791
+ * This sets whether or not to serialize types unsupported by the
1792
+ * JSON format as strings.
1793
+ * If this boolean is false, types unsupported by the JSON format will
1794
+ * be serialized as strings.
1795
+ * If this boolean is true, types unsupported by the JSON format will
1796
+ * raise a JSON::GeneratorError.
1797
+ */
1798
+ static VALUE cState_strict_set(VALUE self, VALUE enable)
1799
+ {
1800
+ GET_STATE(self);
1801
+ state->strict = RTEST(enable);
1414
1802
  return Qnil;
1415
1803
  }
1416
1804
 
@@ -1426,6 +1814,18 @@ static VALUE cState_allow_nan_p(VALUE self)
1426
1814
  return state->allow_nan ? Qtrue : Qfalse;
1427
1815
  }
1428
1816
 
1817
+ /*
1818
+ * call-seq: allow_nan=(enable)
1819
+ *
1820
+ * This sets whether or not to serialize NaN, Infinity, and -Infinity
1821
+ */
1822
+ static VALUE cState_allow_nan_set(VALUE self, VALUE enable)
1823
+ {
1824
+ GET_STATE(self);
1825
+ state->allow_nan = RTEST(enable);
1826
+ return Qnil;
1827
+ }
1828
+
1429
1829
  /*
1430
1830
  * call-seq: ascii_only?
1431
1831
  *
@@ -1438,6 +1838,18 @@ static VALUE cState_ascii_only_p(VALUE self)
1438
1838
  return state->ascii_only ? Qtrue : Qfalse;
1439
1839
  }
1440
1840
 
1841
+ /*
1842
+ * call-seq: ascii_only=(enable)
1843
+ *
1844
+ * This sets whether only ASCII characters should be generated.
1845
+ */
1846
+ static VALUE cState_ascii_only_set(VALUE self, VALUE enable)
1847
+ {
1848
+ GET_STATE(self);
1849
+ state->ascii_only = RTEST(enable);
1850
+ return Qnil;
1851
+ }
1852
+
1441
1853
  /*
1442
1854
  * call-seq: depth
1443
1855
  *
@@ -1458,8 +1870,7 @@ static VALUE cState_depth(VALUE self)
1458
1870
  static VALUE cState_depth_set(VALUE self, VALUE depth)
1459
1871
  {
1460
1872
  GET_STATE(self);
1461
- Check_Type(depth, T_FIXNUM);
1462
- state->depth = FIX2LONG(depth);
1873
+ state->depth = long_config(depth);
1463
1874
  return Qnil;
1464
1875
  }
1465
1876
 
@@ -1474,6 +1885,15 @@ static VALUE cState_buffer_initial_length(VALUE self)
1474
1885
  return LONG2FIX(state->buffer_initial_length);
1475
1886
  }
1476
1887
 
1888
+ static void buffer_initial_length_set(JSON_Generator_State *state, VALUE buffer_initial_length)
1889
+ {
1890
+ Check_Type(buffer_initial_length, T_FIXNUM);
1891
+ long initial_length = FIX2LONG(buffer_initial_length);
1892
+ if (initial_length > 0) {
1893
+ state->buffer_initial_length = initial_length;
1894
+ }
1895
+ }
1896
+
1477
1897
  /*
1478
1898
  * call-seq: buffer_initial_length=(length)
1479
1899
  *
@@ -1482,16 +1902,76 @@ static VALUE cState_buffer_initial_length(VALUE self)
1482
1902
  */
1483
1903
  static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_length)
1484
1904
  {
1485
- long initial_length;
1486
1905
  GET_STATE(self);
1487
- Check_Type(buffer_initial_length, T_FIXNUM);
1488
- initial_length = FIX2LONG(buffer_initial_length);
1489
- if (initial_length > 0) {
1490
- state->buffer_initial_length = initial_length;
1491
- }
1906
+ buffer_initial_length_set(state, buffer_initial_length);
1492
1907
  return Qnil;
1493
1908
  }
1494
1909
 
1910
+ static int configure_state_i(VALUE key, VALUE val, VALUE _arg)
1911
+ {
1912
+ JSON_Generator_State *state = (JSON_Generator_State *)_arg;
1913
+
1914
+ if (key == sym_indent) { state->indent = string_config(val); }
1915
+ else if (key == sym_space) { state->space = string_config(val); }
1916
+ else if (key == sym_space_before) { state->space_before = string_config(val); }
1917
+ else if (key == sym_object_nl) { state->object_nl = string_config(val); }
1918
+ else if (key == sym_array_nl) { state->array_nl = string_config(val); }
1919
+ else if (key == sym_max_nesting) { state->max_nesting = long_config(val); }
1920
+ else if (key == sym_allow_nan) { state->allow_nan = RTEST(val); }
1921
+ else if (key == sym_ascii_only) { state->ascii_only = RTEST(val); }
1922
+ else if (key == sym_depth) { state->depth = long_config(val); }
1923
+ else if (key == sym_buffer_initial_length) { buffer_initial_length_set(state, val); }
1924
+ else if (key == sym_script_safe) { state->script_safe = RTEST(val); }
1925
+ else if (key == sym_escape_slash) { state->script_safe = RTEST(val); }
1926
+ else if (key == sym_strict) { state->strict = RTEST(val); }
1927
+ else if (key == sym_as_json) { state->as_json = RTEST(val) ? rb_convert_type(val, T_DATA, "Proc", "to_proc") : Qfalse; }
1928
+ return ST_CONTINUE;
1929
+ }
1930
+
1931
+ static void configure_state(JSON_Generator_State *state, VALUE config)
1932
+ {
1933
+ if (!RTEST(config)) return;
1934
+
1935
+ Check_Type(config, T_HASH);
1936
+
1937
+ if (!RHASH_SIZE(config)) return;
1938
+
1939
+ // We assume in most cases few keys are set so it's faster to go over
1940
+ // the provided keys than to check all possible keys.
1941
+ rb_hash_foreach(config, configure_state_i, (VALUE)state);
1942
+ }
1943
+
1944
+ static VALUE cState_configure(VALUE self, VALUE opts)
1945
+ {
1946
+ GET_STATE(self);
1947
+ configure_state(state, opts);
1948
+ return self;
1949
+ }
1950
+
1951
+ static VALUE cState_m_generate(VALUE klass, VALUE obj, VALUE opts, VALUE io)
1952
+ {
1953
+ JSON_Generator_State state = {0};
1954
+ state_init(&state);
1955
+ configure_state(&state, opts);
1956
+
1957
+ char stack_buffer[FBUFFER_STACK_SIZE];
1958
+ FBuffer buffer = {
1959
+ .io = RTEST(io) ? io : Qfalse,
1960
+ };
1961
+ fbuffer_stack_init(&buffer, state.buffer_initial_length, stack_buffer, FBUFFER_STACK_SIZE);
1962
+
1963
+ struct generate_json_data data = {
1964
+ .buffer = &buffer,
1965
+ .vstate = Qfalse,
1966
+ .state = &state,
1967
+ .obj = obj,
1968
+ .func = generate_json,
1969
+ };
1970
+ rb_rescue(generate_json_try, (VALUE)&data, generate_json_rescue, (VALUE)&data);
1971
+
1972
+ return fbuffer_finalize(&buffer);
1973
+ }
1974
+
1495
1975
  /*
1496
1976
  *
1497
1977
  */
@@ -1505,18 +1985,26 @@ void Init_generator(void)
1505
1985
  rb_require("json/common");
1506
1986
 
1507
1987
  mJSON = rb_define_module("JSON");
1508
- mExt = rb_define_module_under(mJSON, "Ext");
1509
- mGenerator = rb_define_module_under(mExt, "Generator");
1510
1988
 
1989
+ rb_global_variable(&cFragment);
1990
+ cFragment = rb_const_get(mJSON, rb_intern("Fragment"));
1991
+
1992
+ VALUE mExt = rb_define_module_under(mJSON, "Ext");
1993
+ VALUE mGenerator = rb_define_module_under(mExt, "Generator");
1994
+
1995
+ rb_global_variable(&eGeneratorError);
1511
1996
  eGeneratorError = rb_path2class("JSON::GeneratorError");
1997
+
1998
+ rb_global_variable(&eNestingError);
1512
1999
  eNestingError = rb_path2class("JSON::NestingError");
1513
- rb_gc_register_mark_object(eGeneratorError);
1514
- rb_gc_register_mark_object(eNestingError);
1515
2000
 
1516
2001
  cState = rb_define_class_under(mGenerator, "State", rb_cObject);
1517
2002
  rb_define_alloc_func(cState, cState_s_allocate);
1518
2003
  rb_define_singleton_method(cState, "from_state", cState_from_state_s, 1);
1519
2004
  rb_define_method(cState, "initialize", cState_initialize, -1);
2005
+ rb_define_alias(cState, "initialize", "initialize"); // avoid method redefinition warnings
2006
+ rb_define_private_method(cState, "_configure", cState_configure, 1);
2007
+
1520
2008
  rb_define_method(cState, "initialize_copy", cState_init_copy, 1);
1521
2009
  rb_define_method(cState, "indent", cState_indent, 0);
1522
2010
  rb_define_method(cState, "indent=", cState_indent_set, 1);
@@ -1528,81 +2016,124 @@ void Init_generator(void)
1528
2016
  rb_define_method(cState, "object_nl=", cState_object_nl_set, 1);
1529
2017
  rb_define_method(cState, "array_nl", cState_array_nl, 0);
1530
2018
  rb_define_method(cState, "array_nl=", cState_array_nl_set, 1);
2019
+ rb_define_method(cState, "as_json", cState_as_json, 0);
2020
+ rb_define_method(cState, "as_json=", cState_as_json_set, 1);
1531
2021
  rb_define_method(cState, "max_nesting", cState_max_nesting, 0);
1532
2022
  rb_define_method(cState, "max_nesting=", cState_max_nesting_set, 1);
1533
- rb_define_method(cState, "escape_slash", cState_escape_slash, 0);
1534
- rb_define_method(cState, "escape_slash?", cState_escape_slash, 0);
1535
- rb_define_method(cState, "escape_slash=", cState_escape_slash_set, 1);
2023
+ rb_define_method(cState, "script_safe", cState_script_safe, 0);
2024
+ rb_define_method(cState, "script_safe?", cState_script_safe, 0);
2025
+ rb_define_method(cState, "script_safe=", cState_script_safe_set, 1);
2026
+ rb_define_alias(cState, "escape_slash", "script_safe");
2027
+ rb_define_alias(cState, "escape_slash?", "script_safe?");
2028
+ rb_define_alias(cState, "escape_slash=", "script_safe=");
2029
+ rb_define_method(cState, "strict", cState_strict, 0);
2030
+ rb_define_method(cState, "strict?", cState_strict, 0);
2031
+ rb_define_method(cState, "strict=", cState_strict_set, 1);
1536
2032
  rb_define_method(cState, "check_circular?", cState_check_circular_p, 0);
1537
2033
  rb_define_method(cState, "allow_nan?", cState_allow_nan_p, 0);
2034
+ rb_define_method(cState, "allow_nan=", cState_allow_nan_set, 1);
1538
2035
  rb_define_method(cState, "ascii_only?", cState_ascii_only_p, 0);
2036
+ rb_define_method(cState, "ascii_only=", cState_ascii_only_set, 1);
1539
2037
  rb_define_method(cState, "depth", cState_depth, 0);
1540
2038
  rb_define_method(cState, "depth=", cState_depth_set, 1);
1541
2039
  rb_define_method(cState, "buffer_initial_length", cState_buffer_initial_length, 0);
1542
2040
  rb_define_method(cState, "buffer_initial_length=", cState_buffer_initial_length_set, 1);
1543
- rb_define_method(cState, "configure", cState_configure, 1);
1544
- rb_define_alias(cState, "merge", "configure");
1545
- rb_define_method(cState, "to_h", cState_to_h, 0);
1546
- rb_define_alias(cState, "to_hash", "to_h");
1547
- rb_define_method(cState, "[]", cState_aref, 1);
1548
- rb_define_method(cState, "[]=", cState_aset, 2);
1549
- rb_define_method(cState, "generate", cState_generate, 1);
1550
-
1551
- mGeneratorMethods = rb_define_module_under(mGenerator, "GeneratorMethods");
1552
- mObject = rb_define_module_under(mGeneratorMethods, "Object");
2041
+ rb_define_method(cState, "generate", cState_generate, -1);
2042
+ rb_define_alias(cState, "generate_new", "generate"); // :nodoc:
2043
+
2044
+ rb_define_singleton_method(cState, "generate", cState_m_generate, 3);
2045
+
2046
+ VALUE mGeneratorMethods = rb_define_module_under(mGenerator, "GeneratorMethods");
2047
+
2048
+ VALUE mObject = rb_define_module_under(mGeneratorMethods, "Object");
1553
2049
  rb_define_method(mObject, "to_json", mObject_to_json, -1);
1554
- mHash = rb_define_module_under(mGeneratorMethods, "Hash");
2050
+
2051
+ VALUE mHash = rb_define_module_under(mGeneratorMethods, "Hash");
1555
2052
  rb_define_method(mHash, "to_json", mHash_to_json, -1);
1556
- mArray = rb_define_module_under(mGeneratorMethods, "Array");
2053
+
2054
+ VALUE mArray = rb_define_module_under(mGeneratorMethods, "Array");
1557
2055
  rb_define_method(mArray, "to_json", mArray_to_json, -1);
2056
+
1558
2057
  #ifdef RUBY_INTEGER_UNIFICATION
1559
- mInteger = rb_define_module_under(mGeneratorMethods, "Integer");
2058
+ VALUE mInteger = rb_define_module_under(mGeneratorMethods, "Integer");
1560
2059
  rb_define_method(mInteger, "to_json", mInteger_to_json, -1);
1561
2060
  #else
1562
- mFixnum = rb_define_module_under(mGeneratorMethods, "Fixnum");
2061
+ VALUE mFixnum = rb_define_module_under(mGeneratorMethods, "Fixnum");
1563
2062
  rb_define_method(mFixnum, "to_json", mFixnum_to_json, -1);
1564
- mBignum = rb_define_module_under(mGeneratorMethods, "Bignum");
2063
+
2064
+ VALUE mBignum = rb_define_module_under(mGeneratorMethods, "Bignum");
1565
2065
  rb_define_method(mBignum, "to_json", mBignum_to_json, -1);
1566
2066
  #endif
1567
- mFloat = rb_define_module_under(mGeneratorMethods, "Float");
2067
+ VALUE mFloat = rb_define_module_under(mGeneratorMethods, "Float");
1568
2068
  rb_define_method(mFloat, "to_json", mFloat_to_json, -1);
1569
- mString = rb_define_module_under(mGeneratorMethods, "String");
2069
+
2070
+ VALUE mString = rb_define_module_under(mGeneratorMethods, "String");
1570
2071
  rb_define_singleton_method(mString, "included", mString_included_s, 1);
1571
2072
  rb_define_method(mString, "to_json", mString_to_json, -1);
1572
2073
  rb_define_method(mString, "to_json_raw", mString_to_json_raw, -1);
1573
2074
  rb_define_method(mString, "to_json_raw_object", mString_to_json_raw_object, 0);
2075
+
1574
2076
  mString_Extend = rb_define_module_under(mString, "Extend");
1575
2077
  rb_define_method(mString_Extend, "json_create", mString_Extend_json_create, 1);
1576
- mTrueClass = rb_define_module_under(mGeneratorMethods, "TrueClass");
2078
+
2079
+ VALUE mTrueClass = rb_define_module_under(mGeneratorMethods, "TrueClass");
1577
2080
  rb_define_method(mTrueClass, "to_json", mTrueClass_to_json, -1);
1578
- mFalseClass = rb_define_module_under(mGeneratorMethods, "FalseClass");
2081
+
2082
+ VALUE mFalseClass = rb_define_module_under(mGeneratorMethods, "FalseClass");
1579
2083
  rb_define_method(mFalseClass, "to_json", mFalseClass_to_json, -1);
1580
- mNilClass = rb_define_module_under(mGeneratorMethods, "NilClass");
2084
+
2085
+ VALUE mNilClass = rb_define_module_under(mGeneratorMethods, "NilClass");
1581
2086
  rb_define_method(mNilClass, "to_json", mNilClass_to_json, -1);
1582
2087
 
2088
+ rb_global_variable(&Encoding_UTF_8);
2089
+ Encoding_UTF_8 = rb_const_get(rb_path2class("Encoding"), rb_intern("UTF_8"));
2090
+
1583
2091
  i_to_s = rb_intern("to_s");
1584
2092
  i_to_json = rb_intern("to_json");
1585
2093
  i_new = rb_intern("new");
1586
- i_indent = rb_intern("indent");
1587
- i_space = rb_intern("space");
1588
- i_space_before = rb_intern("space_before");
1589
- i_object_nl = rb_intern("object_nl");
1590
- i_array_nl = rb_intern("array_nl");
1591
- i_max_nesting = rb_intern("max_nesting");
1592
- i_escape_slash = rb_intern("escape_slash");
1593
- i_allow_nan = rb_intern("allow_nan");
1594
- i_ascii_only = rb_intern("ascii_only");
1595
- i_depth = rb_intern("depth");
1596
- i_buffer_initial_length = rb_intern("buffer_initial_length");
1597
2094
  i_pack = rb_intern("pack");
1598
2095
  i_unpack = rb_intern("unpack");
1599
2096
  i_create_id = rb_intern("create_id");
1600
2097
  i_extend = rb_intern("extend");
1601
- i_key_p = rb_intern("key?");
1602
- i_aref = rb_intern("[]");
1603
- i_send = rb_intern("__send__");
1604
- i_respond_to_p = rb_intern("respond_to?");
1605
- i_match = rb_intern("match");
1606
- i_keys = rb_intern("keys");
1607
- i_dup = rb_intern("dup");
2098
+ i_encode = rb_intern("encode");
2099
+
2100
+ sym_indent = ID2SYM(rb_intern("indent"));
2101
+ sym_space = ID2SYM(rb_intern("space"));
2102
+ sym_space_before = ID2SYM(rb_intern("space_before"));
2103
+ sym_object_nl = ID2SYM(rb_intern("object_nl"));
2104
+ sym_array_nl = ID2SYM(rb_intern("array_nl"));
2105
+ sym_max_nesting = ID2SYM(rb_intern("max_nesting"));
2106
+ sym_allow_nan = ID2SYM(rb_intern("allow_nan"));
2107
+ sym_ascii_only = ID2SYM(rb_intern("ascii_only"));
2108
+ sym_depth = ID2SYM(rb_intern("depth"));
2109
+ sym_buffer_initial_length = ID2SYM(rb_intern("buffer_initial_length"));
2110
+ sym_script_safe = ID2SYM(rb_intern("script_safe"));
2111
+ sym_escape_slash = ID2SYM(rb_intern("escape_slash"));
2112
+ sym_strict = ID2SYM(rb_intern("strict"));
2113
+ sym_as_json = ID2SYM(rb_intern("as_json"));
2114
+
2115
+ usascii_encindex = rb_usascii_encindex();
2116
+ utf8_encindex = rb_utf8_encindex();
2117
+ binary_encindex = rb_ascii8bit_encindex();
2118
+
2119
+ rb_require("json/ext/generator/state");
2120
+
2121
+
2122
+ switch (find_simd_implementation()) {
2123
+ #ifdef HAVE_SIMD
2124
+ #ifdef HAVE_SIMD_NEON
2125
+ case SIMD_NEON:
2126
+ search_escape_basic_impl = search_escape_basic_neon;
2127
+ break;
2128
+ #endif /* HAVE_SIMD_NEON */
2129
+ #ifdef HAVE_SIMD_SSE2
2130
+ case SIMD_SSE2:
2131
+ search_escape_basic_impl = search_escape_basic_sse2;
2132
+ break;
2133
+ #endif /* HAVE_SIMD_SSE2 */
2134
+ #endif /* HAVE_SIMD */
2135
+ default:
2136
+ search_escape_basic_impl = search_escape_basic;
2137
+ break;
2138
+ }
1608
2139
  }