oj 3.13.17 → 3.16.3

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 (156) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +77 -0
  3. data/README.md +4 -2
  4. data/ext/oj/buf.h +7 -6
  5. data/ext/oj/cache.c +29 -26
  6. data/ext/oj/cache.h +3 -2
  7. data/ext/oj/cache8.c +10 -9
  8. data/ext/oj/circarray.c +7 -5
  9. data/ext/oj/circarray.h +2 -2
  10. data/ext/oj/code.c +5 -12
  11. data/ext/oj/code.h +2 -2
  12. data/ext/oj/compat.c +20 -60
  13. data/ext/oj/custom.c +44 -96
  14. data/ext/oj/debug.c +3 -9
  15. data/ext/oj/dump.c +69 -39
  16. data/ext/oj/dump.h +1 -4
  17. data/ext/oj/dump_compat.c +557 -592
  18. data/ext/oj/dump_leaf.c +3 -5
  19. data/ext/oj/dump_object.c +42 -48
  20. data/ext/oj/dump_strict.c +10 -22
  21. data/ext/oj/encoder.c +1 -1
  22. data/ext/oj/err.c +2 -13
  23. data/ext/oj/err.h +9 -12
  24. data/ext/oj/extconf.rb +16 -6
  25. data/ext/oj/fast.c +76 -106
  26. data/ext/oj/intern.c +63 -51
  27. data/ext/oj/intern.h +3 -7
  28. data/ext/oj/mem.c +318 -0
  29. data/ext/oj/mem.h +53 -0
  30. data/ext/oj/mimic_json.c +43 -30
  31. data/ext/oj/object.c +61 -70
  32. data/ext/oj/odd.c +8 -6
  33. data/ext/oj/odd.h +4 -4
  34. data/ext/oj/oj.c +243 -205
  35. data/ext/oj/oj.h +82 -78
  36. data/ext/oj/parse.c +123 -188
  37. data/ext/oj/parse.h +23 -24
  38. data/ext/oj/parser.c +103 -63
  39. data/ext/oj/parser.h +19 -9
  40. data/ext/oj/rails.c +68 -92
  41. data/ext/oj/reader.c +10 -15
  42. data/ext/oj/reader.h +4 -2
  43. data/ext/oj/resolve.c +3 -4
  44. data/ext/oj/rxclass.c +6 -5
  45. data/ext/oj/rxclass.h +1 -1
  46. data/ext/oj/saj.c +10 -9
  47. data/ext/oj/saj2.c +74 -92
  48. data/ext/oj/saj2.h +23 -0
  49. data/ext/oj/scp.c +3 -14
  50. data/ext/oj/sparse.c +22 -70
  51. data/ext/oj/stream_writer.c +43 -35
  52. data/ext/oj/strict.c +20 -52
  53. data/ext/oj/string_writer.c +60 -34
  54. data/ext/oj/trace.h +31 -4
  55. data/ext/oj/usual.c +125 -150
  56. data/ext/oj/usual.h +69 -0
  57. data/ext/oj/util.h +1 -1
  58. data/ext/oj/val_stack.c +14 -3
  59. data/ext/oj/val_stack.h +8 -7
  60. data/ext/oj/wab.c +25 -57
  61. data/lib/oj/active_support_helper.rb +1 -3
  62. data/lib/oj/bag.rb +7 -1
  63. data/lib/oj/easy_hash.rb +4 -5
  64. data/lib/oj/error.rb +0 -1
  65. data/lib/oj/json.rb +162 -150
  66. data/lib/oj/mimic.rb +6 -2
  67. data/lib/oj/state.rb +9 -6
  68. data/lib/oj/version.rb +1 -2
  69. data/lib/oj.rb +2 -0
  70. data/pages/Compatibility.md +1 -1
  71. data/pages/InstallOptions.md +20 -0
  72. data/pages/Options.md +10 -0
  73. data/test/_test_active.rb +8 -9
  74. data/test/_test_active_mimic.rb +7 -8
  75. data/test/_test_mimic_rails.rb +17 -20
  76. data/test/activerecord/result_test.rb +5 -6
  77. data/test/files.rb +15 -15
  78. data/test/foo.rb +9 -72
  79. data/test/helper.rb +11 -8
  80. data/test/isolated/shared.rb +3 -2
  81. data/test/json_gem/json_addition_test.rb +2 -2
  82. data/test/json_gem/json_common_interface_test.rb +8 -6
  83. data/test/json_gem/json_encoding_test.rb +0 -0
  84. data/test/json_gem/json_ext_parser_test.rb +1 -0
  85. data/test/json_gem/json_fixtures_test.rb +3 -2
  86. data/test/json_gem/json_generator_test.rb +53 -37
  87. data/test/json_gem/json_generic_object_test.rb +11 -11
  88. data/test/json_gem/json_parser_test.rb +47 -47
  89. data/test/json_gem/json_string_matching_test.rb +9 -9
  90. data/test/json_gem/test_helper.rb +7 -3
  91. data/test/mem.rb +13 -12
  92. data/test/perf.rb +21 -26
  93. data/test/perf_compat.rb +31 -33
  94. data/test/perf_dump.rb +28 -28
  95. data/test/perf_fast.rb +80 -82
  96. data/test/perf_file.rb +27 -29
  97. data/test/perf_object.rb +65 -69
  98. data/test/perf_once.rb +12 -11
  99. data/test/perf_parser.rb +42 -48
  100. data/test/perf_saj.rb +46 -54
  101. data/test/perf_scp.rb +57 -69
  102. data/test/perf_simple.rb +41 -39
  103. data/test/perf_strict.rb +68 -70
  104. data/test/perf_wab.rb +67 -69
  105. data/test/prec.rb +5 -5
  106. data/test/sample/change.rb +0 -1
  107. data/test/sample/dir.rb +0 -1
  108. data/test/sample/doc.rb +0 -1
  109. data/test/sample/file.rb +0 -1
  110. data/test/sample/group.rb +0 -1
  111. data/test/sample/hasprops.rb +0 -1
  112. data/test/sample/layer.rb +0 -1
  113. data/test/sample/rect.rb +0 -1
  114. data/test/sample/shape.rb +0 -1
  115. data/test/sample/text.rb +0 -1
  116. data/test/sample.rb +16 -16
  117. data/test/sample_json.rb +8 -8
  118. data/test/test_compat.rb +80 -53
  119. data/test/test_custom.rb +73 -51
  120. data/test/test_debian.rb +7 -10
  121. data/test/test_fast.rb +86 -90
  122. data/test/test_file.rb +28 -35
  123. data/test/test_gc.rb +16 -5
  124. data/test/test_generate.rb +5 -5
  125. data/test/test_hash.rb +4 -4
  126. data/test/test_integer_range.rb +9 -9
  127. data/test/test_null.rb +20 -20
  128. data/test/test_object.rb +94 -96
  129. data/test/test_parser.rb +6 -22
  130. data/test/test_parser_debug.rb +27 -0
  131. data/test/test_parser_saj.rb +61 -22
  132. data/test/test_parser_usual.rb +16 -6
  133. data/test/test_rails.rb +2 -2
  134. data/test/test_saj.rb +10 -8
  135. data/test/test_scp.rb +37 -39
  136. data/test/test_strict.rb +40 -32
  137. data/test/test_various.rb +148 -100
  138. data/test/test_wab.rb +48 -44
  139. data/test/test_writer.rb +47 -47
  140. data/test/tests.rb +13 -4
  141. data/test/tests_mimic.rb +12 -3
  142. data/test/tests_mimic_addition.rb +12 -3
  143. metadata +36 -27
  144. data/test/activesupport4/decoding_test.rb +0 -108
  145. data/test/activesupport4/encoding_test.rb +0 -531
  146. data/test/activesupport4/test_helper.rb +0 -41
  147. data/test/activesupport5/abstract_unit.rb +0 -45
  148. data/test/activesupport5/decoding_test.rb +0 -133
  149. data/test/activesupport5/encoding_test.rb +0 -500
  150. data/test/activesupport5/encoding_test_cases.rb +0 -98
  151. data/test/activesupport5/test_helper.rb +0 -72
  152. data/test/activesupport5/time_zone_test_helpers.rb +0 -39
  153. data/test/bar.rb +0 -11
  154. data/test/baz.rb +0 -16
  155. data/test/bug.rb +0 -16
  156. data/test/zoo.rb +0 -13
data/ext/oj/saj2.c CHANGED
@@ -1,22 +1,14 @@
1
1
  // Copyright (c) 2021, Peter Ohler, All rights reserved.
2
2
 
3
+ #include "saj2.h"
4
+
3
5
  #include "cache.h"
6
+ #include "mem.h"
4
7
  #include "oj.h"
5
8
  #include "parser.h"
6
9
 
7
- typedef struct _delegate {
8
- VALUE handler;
9
- VALUE *keys;
10
- VALUE *tail;
11
- size_t klen;
12
- struct _cache *str_cache;
13
- uint8_t cache_str;
14
- bool cache_keys;
15
- bool thread_safe;
16
- } * Delegate;
17
-
18
10
  static VALUE get_key(ojParser p) {
19
- Delegate d = (Delegate)p->ctx;
11
+ Saj d = (Saj)p->ctx;
20
12
  const char *key = buf_str(&p->key);
21
13
  size_t len = buf_len(&p->key);
22
14
  volatile VALUE rkey;
@@ -29,12 +21,12 @@ static VALUE get_key(ojParser p) {
29
21
  return rkey;
30
22
  }
31
23
 
32
- static void push_key(Delegate d, VALUE key) {
24
+ static void push_key(Saj d, VALUE key) {
33
25
  if (d->klen <= (size_t)(d->tail - d->keys)) {
34
26
  size_t off = d->tail - d->keys;
35
27
 
36
28
  d->klen += d->klen / 2;
37
- REALLOC_N(d->keys, VALUE, d->klen);
29
+ OJ_R_REALLOC_N(d->keys, VALUE, d->klen);
38
30
  d->tail = d->keys + off;
39
31
  }
40
32
  *d->tail = key;
@@ -45,15 +37,15 @@ static void noop(ojParser p) {
45
37
  }
46
38
 
47
39
  static void open_object(ojParser p) {
48
- rb_funcall(((Delegate)p->ctx)->handler, oj_hash_start_id, 1, Qnil);
40
+ rb_funcall(((Saj)p->ctx)->handler, oj_hash_start_id, 1, Qnil);
49
41
  }
50
42
 
51
43
  static void open_object_loc(ojParser p) {
52
- rb_funcall(((Delegate)p->ctx)->handler, oj_hash_start_id, 3, Qnil, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
44
+ rb_funcall(((Saj)p->ctx)->handler, oj_hash_start_id, 3, Qnil, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
53
45
  }
54
46
 
55
47
  static void open_object_key(ojParser p) {
56
- Delegate d = (Delegate)p->ctx;
48
+ Saj d = (Saj)p->ctx;
57
49
  volatile VALUE key = get_key(p);
58
50
 
59
51
  push_key(d, key);
@@ -61,7 +53,7 @@ static void open_object_key(ojParser p) {
61
53
  }
62
54
 
63
55
  static void open_object_loc_key(ojParser p) {
64
- Delegate d = (Delegate)p->ctx;
56
+ Saj d = (Saj)p->ctx;
65
57
  volatile VALUE key = get_key(p);
66
58
 
67
59
  push_key(d, key);
@@ -69,15 +61,15 @@ static void open_object_loc_key(ojParser p) {
69
61
  }
70
62
 
71
63
  static void open_array(ojParser p) {
72
- rb_funcall(((Delegate)p->ctx)->handler, oj_array_start_id, 1, Qnil);
64
+ rb_funcall(((Saj)p->ctx)->handler, oj_array_start_id, 1, Qnil);
73
65
  }
74
66
 
75
67
  static void open_array_loc(ojParser p) {
76
- rb_funcall(((Delegate)p->ctx)->handler, oj_array_start_id, 3, Qnil, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
68
+ rb_funcall(((Saj)p->ctx)->handler, oj_array_start_id, 3, Qnil, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
77
69
  }
78
70
 
79
71
  static void open_array_key(ojParser p) {
80
- Delegate d = (Delegate)p->ctx;
72
+ Saj d = (Saj)p->ctx;
81
73
  volatile VALUE key = get_key(p);
82
74
 
83
75
  push_key(d, key);
@@ -85,7 +77,7 @@ static void open_array_key(ojParser p) {
85
77
  }
86
78
 
87
79
  static void open_array_loc_key(ojParser p) {
88
- Delegate d = (Delegate)p->ctx;
80
+ Saj d = (Saj)p->ctx;
89
81
  volatile VALUE key = get_key(p);
90
82
 
91
83
  push_key(d, key);
@@ -93,8 +85,8 @@ static void open_array_loc_key(ojParser p) {
93
85
  }
94
86
 
95
87
  static void close_object(ojParser p) {
96
- Delegate d = (Delegate)p->ctx;
97
- VALUE key = Qnil;
88
+ Saj d = (Saj)p->ctx;
89
+ VALUE key = Qnil;
98
90
 
99
91
  if (OBJECT_FUN == p->stack[p->depth]) {
100
92
  d->tail--;
@@ -107,8 +99,8 @@ static void close_object(ojParser p) {
107
99
  }
108
100
 
109
101
  static void close_object_loc(ojParser p) {
110
- Delegate d = (Delegate)p->ctx;
111
- VALUE key = Qnil;
102
+ Saj d = (Saj)p->ctx;
103
+ VALUE key = Qnil;
112
104
 
113
105
  if (OBJECT_FUN == p->stack[p->depth]) {
114
106
  d->tail--;
@@ -121,8 +113,8 @@ static void close_object_loc(ojParser p) {
121
113
  }
122
114
 
123
115
  static void close_array(ojParser p) {
124
- Delegate d = (Delegate)p->ctx;
125
- VALUE key = Qnil;
116
+ Saj d = (Saj)p->ctx;
117
+ VALUE key = Qnil;
126
118
 
127
119
  if (OBJECT_FUN == p->stack[p->depth]) {
128
120
  d->tail--;
@@ -135,8 +127,8 @@ static void close_array(ojParser p) {
135
127
  }
136
128
 
137
129
  static void close_array_loc(ojParser p) {
138
- Delegate d = (Delegate)p->ctx;
139
- VALUE key = Qnil;
130
+ Saj d = (Saj)p->ctx;
131
+ VALUE key = Qnil;
140
132
 
141
133
  if (OBJECT_FUN == p->stack[p->depth]) {
142
134
  d->tail--;
@@ -149,25 +141,19 @@ static void close_array_loc(ojParser p) {
149
141
  }
150
142
 
151
143
  static void add_null(ojParser p) {
152
- rb_funcall(((Delegate)p->ctx)->handler, oj_add_value_id, 2, Qnil, Qnil);
144
+ rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, Qnil, Qnil);
153
145
  }
154
146
 
155
147
  static void add_null_loc(ojParser p) {
156
- rb_funcall(((Delegate)p->ctx)->handler,
157
- oj_add_value_id,
158
- 4,
159
- Qnil,
160
- Qnil,
161
- LONG2FIX(p->line),
162
- LONG2FIX(p->cur - p->col));
148
+ rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 4, Qnil, Qnil, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
163
149
  }
164
150
 
165
151
  static void add_null_key(ojParser p) {
166
- rb_funcall(((Delegate)p->ctx)->handler, oj_add_value_id, 2, Qnil, get_key(p));
152
+ rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, Qnil, get_key(p));
167
153
  }
168
154
 
169
155
  static void add_null_key_loc(ojParser p) {
170
- rb_funcall(((Delegate)p->ctx)->handler,
156
+ rb_funcall(((Saj)p->ctx)->handler,
171
157
  oj_add_value_id,
172
158
  4,
173
159
  Qnil,
@@ -177,25 +163,19 @@ static void add_null_key_loc(ojParser p) {
177
163
  }
178
164
 
179
165
  static void add_true(ojParser p) {
180
- rb_funcall(((Delegate)p->ctx)->handler, oj_add_value_id, 2, Qtrue, Qnil);
166
+ rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, Qtrue, Qnil);
181
167
  }
182
168
 
183
169
  static void add_true_loc(ojParser p) {
184
- rb_funcall(((Delegate)p->ctx)->handler,
185
- oj_add_value_id,
186
- 4,
187
- Qtrue,
188
- Qnil,
189
- LONG2FIX(p->line),
190
- LONG2FIX(p->cur - p->col));
170
+ rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 4, Qtrue, Qnil, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
191
171
  }
192
172
 
193
173
  static void add_true_key(ojParser p) {
194
- rb_funcall(((Delegate)p->ctx)->handler, oj_add_value_id, 2, Qtrue, get_key(p));
174
+ rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, Qtrue, get_key(p));
195
175
  }
196
176
 
197
177
  static void add_true_key_loc(ojParser p) {
198
- rb_funcall(((Delegate)p->ctx)->handler,
178
+ rb_funcall(((Saj)p->ctx)->handler,
199
179
  oj_add_value_id,
200
180
  4,
201
181
  Qtrue,
@@ -205,25 +185,19 @@ static void add_true_key_loc(ojParser p) {
205
185
  }
206
186
 
207
187
  static void add_false(ojParser p) {
208
- rb_funcall(((Delegate)p->ctx)->handler, oj_add_value_id, 2, Qfalse, Qnil);
188
+ rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, Qfalse, Qnil);
209
189
  }
210
190
 
211
191
  static void add_false_loc(ojParser p) {
212
- rb_funcall(((Delegate)p->ctx)->handler,
213
- oj_add_value_id,
214
- 4,
215
- Qfalse,
216
- Qnil,
217
- LONG2FIX(p->line),
218
- LONG2FIX(p->cur - p->col));
192
+ rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 4, Qfalse, Qnil, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
219
193
  }
220
194
 
221
195
  static void add_false_key(ojParser p) {
222
- rb_funcall(((Delegate)p->ctx)->handler, oj_add_value_id, 2, Qfalse, get_key(p));
196
+ rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, Qfalse, get_key(p));
223
197
  }
224
198
 
225
199
  static void add_false_key_loc(ojParser p) {
226
- rb_funcall(((Delegate)p->ctx)->handler,
200
+ rb_funcall(((Saj)p->ctx)->handler,
227
201
  oj_add_value_id,
228
202
  4,
229
203
  Qfalse,
@@ -233,11 +207,11 @@ static void add_false_key_loc(ojParser p) {
233
207
  }
234
208
 
235
209
  static void add_int(ojParser p) {
236
- rb_funcall(((Delegate)p->ctx)->handler, oj_add_value_id, 2, LONG2NUM(p->num.fixnum), Qnil);
210
+ rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, LONG2NUM(p->num.fixnum), Qnil);
237
211
  }
238
212
 
239
213
  static void add_int_loc(ojParser p) {
240
- rb_funcall(((Delegate)p->ctx)->handler,
214
+ rb_funcall(((Saj)p->ctx)->handler,
241
215
  oj_add_value_id,
242
216
  4,
243
217
  LONG2NUM(p->num.fixnum),
@@ -247,11 +221,11 @@ static void add_int_loc(ojParser p) {
247
221
  }
248
222
 
249
223
  static void add_int_key(ojParser p) {
250
- rb_funcall(((Delegate)p->ctx)->handler, oj_add_value_id, 2, LONG2NUM(p->num.fixnum), get_key(p));
224
+ rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, LONG2NUM(p->num.fixnum), get_key(p));
251
225
  }
252
226
 
253
227
  static void add_int_key_loc(ojParser p) {
254
- rb_funcall(((Delegate)p->ctx)->handler,
228
+ rb_funcall(((Saj)p->ctx)->handler,
255
229
  oj_add_value_id,
256
230
  4,
257
231
  LONG2NUM(p->num.fixnum),
@@ -261,11 +235,11 @@ static void add_int_key_loc(ojParser p) {
261
235
  }
262
236
 
263
237
  static void add_float(ojParser p) {
264
- rb_funcall(((Delegate)p->ctx)->handler, oj_add_value_id, 2, rb_float_new(p->num.dub), Qnil);
238
+ rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, rb_float_new(p->num.dub), Qnil);
265
239
  }
266
240
 
267
241
  static void add_float_loc(ojParser p) {
268
- rb_funcall(((Delegate)p->ctx)->handler,
242
+ rb_funcall(((Saj)p->ctx)->handler,
269
243
  oj_add_value_id,
270
244
  4,
271
245
  rb_float_new(p->num.dub),
@@ -275,11 +249,11 @@ static void add_float_loc(ojParser p) {
275
249
  }
276
250
 
277
251
  static void add_float_key(ojParser p) {
278
- rb_funcall(((Delegate)p->ctx)->handler, oj_add_value_id, 2, rb_float_new(p->num.dub), get_key(p));
252
+ rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, rb_float_new(p->num.dub), get_key(p));
279
253
  }
280
254
 
281
255
  static void add_float_key_loc(ojParser p) {
282
- rb_funcall(((Delegate)p->ctx)->handler,
256
+ rb_funcall(((Saj)p->ctx)->handler,
283
257
  oj_add_value_id,
284
258
  4,
285
259
  rb_float_new(p->num.dub),
@@ -289,7 +263,7 @@ static void add_float_key_loc(ojParser p) {
289
263
  }
290
264
 
291
265
  static void add_big(ojParser p) {
292
- rb_funcall((VALUE)p->ctx,
266
+ rb_funcall(((Saj)p->ctx)->handler,
293
267
  oj_add_value_id,
294
268
  2,
295
269
  rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new(buf_str(&p->buf), buf_len(&p->buf))),
@@ -297,7 +271,7 @@ static void add_big(ojParser p) {
297
271
  }
298
272
 
299
273
  static void add_big_loc(ojParser p) {
300
- rb_funcall((VALUE)p->ctx,
274
+ rb_funcall(((Saj)p->ctx)->handler,
301
275
  oj_add_value_id,
302
276
  4,
303
277
  rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new(buf_str(&p->buf), buf_len(&p->buf))),
@@ -307,7 +281,7 @@ static void add_big_loc(ojParser p) {
307
281
  }
308
282
 
309
283
  static void add_big_key(ojParser p) {
310
- rb_funcall((VALUE)p->ctx,
284
+ rb_funcall(((Saj)p->ctx)->handler,
311
285
  oj_add_value_id,
312
286
  2,
313
287
  rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new(buf_str(&p->buf), buf_len(&p->buf))),
@@ -315,7 +289,7 @@ static void add_big_key(ojParser p) {
315
289
  }
316
290
 
317
291
  static void add_big_key_loc(ojParser p) {
318
- rb_funcall((VALUE)p->ctx,
292
+ rb_funcall(((Saj)p->ctx)->handler,
319
293
  oj_add_value_id,
320
294
  4,
321
295
  rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new(buf_str(&p->buf), buf_len(&p->buf))),
@@ -325,7 +299,7 @@ static void add_big_key_loc(ojParser p) {
325
299
  }
326
300
 
327
301
  static void add_str(ojParser p) {
328
- Delegate d = (Delegate)p->ctx;
302
+ Saj d = (Saj)p->ctx;
329
303
  volatile VALUE rstr;
330
304
  const char *str = buf_str(&p->buf);
331
305
  size_t len = buf_len(&p->buf);
@@ -339,7 +313,7 @@ static void add_str(ojParser p) {
339
313
  }
340
314
 
341
315
  static void add_str_loc(ojParser p) {
342
- Delegate d = (Delegate)p->ctx;
316
+ Saj d = (Saj)p->ctx;
343
317
  volatile VALUE rstr;
344
318
  const char *str = buf_str(&p->buf);
345
319
  size_t len = buf_len(&p->buf);
@@ -353,7 +327,7 @@ static void add_str_loc(ojParser p) {
353
327
  }
354
328
 
355
329
  static void add_str_key(ojParser p) {
356
- Delegate d = (Delegate)p->ctx;
330
+ Saj d = (Saj)p->ctx;
357
331
  volatile VALUE rstr;
358
332
  const char *str = buf_str(&p->buf);
359
333
  size_t len = buf_len(&p->buf);
@@ -367,7 +341,7 @@ static void add_str_key(ojParser p) {
367
341
  }
368
342
 
369
343
  static void add_str_key_loc(ojParser p) {
370
- Delegate d = (Delegate)p->ctx;
344
+ Saj d = (Saj)p->ctx;
371
345
  volatile VALUE rstr;
372
346
  const char *str = buf_str(&p->buf);
373
347
  size_t len = buf_len(&p->buf);
@@ -400,7 +374,7 @@ static void reset(ojParser p) {
400
374
  }
401
375
 
402
376
  static VALUE option(ojParser p, const char *key, VALUE value) {
403
- Delegate d = (Delegate)p->ctx;
377
+ Saj d = (Saj)p->ctx;
404
378
 
405
379
  if (0 == strcmp(key, "handler")) {
406
380
  return d->handler;
@@ -537,7 +511,7 @@ static VALUE option(ojParser p, const char *key, VALUE value) {
537
511
 
538
512
  return INT2NUM((int)d->cache_str);
539
513
  }
540
- rb_raise(rb_eArgError, "%s is not an option for the SAJ (Simple API for JSON) delegate", key);
514
+ rb_raise(rb_eArgError, "%s is not an option for the SAJ (Simple API for JSON) saj", key);
541
515
 
542
516
  return Qnil; // Never reached due to the raise but required by the compiler.
543
517
  }
@@ -547,27 +521,27 @@ static VALUE result(ojParser p) {
547
521
  }
548
522
 
549
523
  static void start(ojParser p) {
550
- Delegate d = (Delegate)p->ctx;
524
+ Saj d = (Saj)p->ctx;
551
525
 
552
526
  d->tail = d->keys;
553
527
  }
554
528
 
555
529
  static void dfree(ojParser p) {
556
- Delegate d = (Delegate)p->ctx;
530
+ Saj d = (Saj)p->ctx;
557
531
 
558
532
  if (NULL != d->keys) {
559
- xfree(d->keys);
533
+ OJ_R_FREE(d->keys);
560
534
  }
561
535
  cache_free(d->str_cache);
562
- xfree(p->ctx);
536
+ OJ_R_FREE(p->ctx);
563
537
  }
564
538
 
565
539
  static void mark(ojParser p) {
566
- if (NULL == p->ctx) {
540
+ if (NULL == p || NULL == p->ctx) {
567
541
  return;
568
542
  }
569
- Delegate d = (Delegate)p->ctx;
570
- VALUE *kp;
543
+ Saj d = (Saj)p->ctx;
544
+ VALUE *kp;
571
545
 
572
546
  cache_mark(d->str_cache);
573
547
  if (Qnil != d->handler) {
@@ -584,13 +558,15 @@ static VALUE form_str(const char *str, size_t len) {
584
558
  return rb_str_freeze(rb_utf8_str_new(str, len));
585
559
  }
586
560
 
587
- void oj_set_parser_saj(ojParser p) {
588
- Delegate d = ALLOC(struct _delegate);
589
-
590
- d->klen = 256;
591
- d->keys = ALLOC_N(VALUE, d->klen);
592
- d->tail = d->keys;
593
- d->str_cache = cache_create(0, form_str, true, false);
561
+ void oj_init_saj(ojParser p, Saj d) {
562
+ d->klen = 256;
563
+ d->keys = OJ_R_ALLOC_N(VALUE, d->klen);
564
+ d->tail = d->keys;
565
+ d->handler = Qnil;
566
+ d->str_cache = cache_create(0, form_str, true, false);
567
+ d->cache_str = 16;
568
+ d->cache_keys = true;
569
+ d->thread_safe = false;
594
570
 
595
571
  p->ctx = (void *)d;
596
572
  reset(p);
@@ -600,3 +576,9 @@ void oj_set_parser_saj(ojParser p) {
600
576
  p->mark = mark;
601
577
  p->start = start;
602
578
  }
579
+
580
+ void oj_set_parser_saj(ojParser p) {
581
+ Saj d = OJ_R_ALLOC(struct _saj);
582
+
583
+ oj_init_saj(p, d);
584
+ }
data/ext/oj/saj2.h ADDED
@@ -0,0 +1,23 @@
1
+ // Copyright (c) 2021, Peter Ohler, All rights reserved.
2
+
3
+ #include <ruby.h>
4
+ #include <stdbool.h>
5
+
6
+ struct _cache;
7
+ struct _ojParser;
8
+
9
+ typedef struct _saj {
10
+ VALUE handler;
11
+ VALUE *keys;
12
+ VALUE *tail;
13
+ size_t klen;
14
+ struct _cache *str_cache;
15
+ uint8_t cache_str;
16
+ bool cache_keys;
17
+ bool thread_safe;
18
+ } *Saj;
19
+
20
+ // Initialize the parser with the SAJ delegate. If the SAJ delegate is wrapped
21
+ // then this function is called first and then the parser functions can be
22
+ // replaced.
23
+ extern void oj_init_saj(struct _ojParser *p, Saj d);
data/ext/oj/scp.c CHANGED
@@ -33,8 +33,7 @@ static VALUE noop_hash_key(ParseInfo pi, const char *key, size_t klen) {
33
33
  return Qundef;
34
34
  }
35
35
 
36
- static void
37
- noop_hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, const char *orig) {
36
+ static void noop_hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, const char *orig) {
38
37
  }
39
38
 
40
39
  static void noop_hash_set_num(ParseInfo pi, Val kval, NumInfo ni) {
@@ -91,12 +90,7 @@ static void hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, c
91
90
  volatile VALUE rstr = rb_str_new(str, len);
92
91
 
93
92
  rstr = oj_encode(rstr);
94
- rb_funcall(pi->handler,
95
- oj_hash_set_id,
96
- 3,
97
- stack_peek(&pi->stack)->val,
98
- oj_calc_hash_key(pi, kval),
99
- rstr);
93
+ rb_funcall(pi->handler, oj_hash_set_id, 3, stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, kval), rstr);
100
94
  }
101
95
 
102
96
  static void hash_set_num(ParseInfo pi, Val kval, NumInfo ni) {
@@ -109,12 +103,7 @@ static void hash_set_num(ParseInfo pi, Val kval, NumInfo ni) {
109
103
  }
110
104
 
111
105
  static void hash_set_value(ParseInfo pi, Val kval, VALUE value) {
112
- rb_funcall(pi->handler,
113
- oj_hash_set_id,
114
- 3,
115
- stack_peek(&pi->stack)->val,
116
- oj_calc_hash_key(pi, kval),
117
- value);
106
+ rb_funcall(pi->handler, oj_hash_set_id, 3, stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, kval), value);
118
107
  }
119
108
 
120
109
  static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {