oj 3.16.6 → 3.17.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +81 -0
- data/README.md +0 -16
- data/ext/oj/compat.c +3 -3
- data/ext/oj/custom.c +17 -11
- data/ext/oj/dump.c +592 -81
- data/ext/oj/dump.h +9 -2
- data/ext/oj/dump_compat.c +15 -9
- data/ext/oj/dump_leaf.c +1 -1
- data/ext/oj/dump_object.c +32 -17
- data/ext/oj/dump_strict.c +20 -14
- data/ext/oj/extconf.rb +10 -7
- data/ext/oj/fast.c +40 -23
- data/ext/oj/intern.c +1 -1
- data/ext/oj/mimic_json.c +11 -7
- data/ext/oj/object.c +8 -8
- data/ext/oj/oj.c +256 -10
- data/ext/oj/oj.h +55 -52
- data/ext/oj/parse.c +243 -46
- data/ext/oj/parser.c +113 -37
- data/ext/oj/parser.h +2 -0
- data/ext/oj/rails.c +41 -36
- data/ext/oj/rxclass.c +1 -1
- data/ext/oj/rxclass.h +1 -1
- data/ext/oj/safe.c +230 -0
- data/ext/oj/safe.h +79 -0
- data/ext/oj/saj.c +13 -8
- data/ext/oj/scp.c +3 -6
- data/ext/oj/simd.h +219 -0
- data/ext/oj/sparse.c +6 -3
- data/ext/oj/stream_writer.c +3 -13
- data/ext/oj/strict.c +2 -4
- data/ext/oj/string_writer.c +5 -7
- data/ext/oj/usual.c +20 -7
- data/ext/oj/wab.c +5 -4
- data/lib/oj/version.rb +1 -1
- data/pages/Encoding.md +1 -1
- metadata +22 -114
- data/test/_test_active.rb +0 -75
- data/test/_test_active_mimic.rb +0 -95
- data/test/_test_mimic_rails.rb +0 -123
- data/test/activerecord/result_test.rb +0 -31
- data/test/activesupport6/abstract_unit.rb +0 -44
- data/test/activesupport6/decoding_test.rb +0 -133
- data/test/activesupport6/encoding_test.rb +0 -542
- data/test/activesupport6/encoding_test_cases.rb +0 -98
- data/test/activesupport6/test_common.rb +0 -17
- data/test/activesupport6/test_helper.rb +0 -163
- data/test/activesupport6/time_zone_test_helpers.rb +0 -39
- data/test/activesupport7/abstract_unit.rb +0 -52
- data/test/activesupport7/decoding_test.rb +0 -125
- data/test/activesupport7/encoding_test.rb +0 -536
- data/test/activesupport7/encoding_test_cases.rb +0 -104
- data/test/activesupport7/time_zone_test_helpers.rb +0 -47
- data/test/files.rb +0 -29
- data/test/foo.rb +0 -26
- data/test/helper.rb +0 -39
- data/test/isolated/shared.rb +0 -309
- data/test/isolated/test_mimic_after.rb +0 -13
- data/test/isolated/test_mimic_alone.rb +0 -12
- data/test/isolated/test_mimic_as_json.rb +0 -45
- data/test/isolated/test_mimic_before.rb +0 -13
- data/test/isolated/test_mimic_define.rb +0 -28
- data/test/isolated/test_mimic_rails_after.rb +0 -22
- data/test/isolated/test_mimic_rails_before.rb +0 -21
- data/test/isolated/test_mimic_redefine.rb +0 -15
- data/test/json_gem/json_addition_test.rb +0 -216
- data/test/json_gem/json_common_interface_test.rb +0 -155
- data/test/json_gem/json_encoding_test.rb +0 -107
- data/test/json_gem/json_ext_parser_test.rb +0 -21
- data/test/json_gem/json_fixtures_test.rb +0 -36
- data/test/json_gem/json_generator_test.rb +0 -413
- data/test/json_gem/json_generic_object_test.rb +0 -90
- data/test/json_gem/json_parser_test.rb +0 -477
- data/test/json_gem/json_string_matching_test.rb +0 -42
- data/test/json_gem/test_helper.rb +0 -30
- data/test/mem.rb +0 -34
- data/test/perf.rb +0 -102
- data/test/perf_compat.rb +0 -128
- data/test/perf_dump.rb +0 -50
- data/test/perf_fast.rb +0 -162
- data/test/perf_file.rb +0 -62
- data/test/perf_object.rb +0 -134
- data/test/perf_once.rb +0 -59
- data/test/perf_parser.rb +0 -183
- data/test/perf_saj.rb +0 -101
- data/test/perf_scp.rb +0 -140
- data/test/perf_simple.rb +0 -289
- data/test/perf_strict.rb +0 -137
- data/test/perf_wab.rb +0 -129
- data/test/prec.rb +0 -23
- data/test/sample/change.rb +0 -13
- data/test/sample/dir.rb +0 -18
- data/test/sample/doc.rb +0 -35
- data/test/sample/file.rb +0 -47
- data/test/sample/group.rb +0 -15
- data/test/sample/hasprops.rb +0 -15
- data/test/sample/layer.rb +0 -11
- data/test/sample/line.rb +0 -20
- data/test/sample/oval.rb +0 -10
- data/test/sample/rect.rb +0 -9
- data/test/sample/shape.rb +0 -34
- data/test/sample/text.rb +0 -19
- data/test/sample.rb +0 -54
- data/test/sample_json.rb +0 -37
- data/test/test_compat.rb +0 -567
- data/test/test_custom.rb +0 -555
- data/test/test_debian.rb +0 -50
- data/test/test_fast.rb +0 -526
- data/test/test_file.rb +0 -250
- data/test/test_gc.rb +0 -60
- data/test/test_generate.rb +0 -21
- data/test/test_hash.rb +0 -39
- data/test/test_integer_range.rb +0 -72
- data/test/test_null.rb +0 -376
- data/test/test_object.rb +0 -1030
- data/test/test_parser.rb +0 -11
- data/test/test_parser_debug.rb +0 -27
- data/test/test_parser_saj.rb +0 -337
- data/test/test_parser_usual.rb +0 -251
- data/test/test_rails.rb +0 -35
- data/test/test_saj.rb +0 -188
- data/test/test_scp.rb +0 -431
- data/test/test_strict.rb +0 -441
- data/test/test_various.rb +0 -801
- data/test/test_wab.rb +0 -311
- data/test/test_writer.rb +0 -380
- data/test/tests.rb +0 -33
- data/test/tests_mimic.rb +0 -23
- data/test/tests_mimic_addition.rb +0 -16
data/ext/oj/rxclass.h
CHANGED
|
@@ -19,7 +19,7 @@ typedef struct _rxClass {
|
|
|
19
19
|
extern void oj_rxclass_init(RxClass rc);
|
|
20
20
|
extern void oj_rxclass_cleanup(RxClass rc);
|
|
21
21
|
extern int oj_rxclass_append(RxClass rc, const char *expr, VALUE clas);
|
|
22
|
-
extern VALUE oj_rxclass_match(RxClass rc, const char *str,
|
|
22
|
+
extern VALUE oj_rxclass_match(RxClass rc, const char *str, size_t len);
|
|
23
23
|
extern void oj_rxclass_copy(RxClass src, RxClass dest);
|
|
24
24
|
extern void oj_rxclass_rappend(RxClass rc, VALUE rx, VALUE clas);
|
|
25
25
|
|
data/ext/oj/safe.c
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
|
|
2
|
+
#include "safe.h"
|
|
3
|
+
|
|
4
|
+
static VALUE max_hash_size_sym, max_array_size_sym, max_depth_sym, max_total_elements_sym, max_hash_size_error_class,
|
|
5
|
+
max_array_size_error_class, max_depth_error_class, max_total_elements_error_class;
|
|
6
|
+
|
|
7
|
+
static void check_object_size(safe_T safe) {
|
|
8
|
+
if (NIL_P(safe->max_hash_size)) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
struct _usual usual = safe->usual;
|
|
13
|
+
Col current_object_location = usual.ctail - 1;
|
|
14
|
+
|
|
15
|
+
long int number_of_items_in_stack = usual.vtail - usual.vhead;
|
|
16
|
+
long int number_of_items_in_hash = (number_of_items_in_stack - current_object_location->vi - 1) / 2;
|
|
17
|
+
|
|
18
|
+
if (safe->max_hash_size > number_of_items_in_hash) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
rb_raise(max_hash_size_error_class, "Too many object items!");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static void check_array_size(safe_T safe) {
|
|
26
|
+
if (NIL_P(safe->max_array_size)) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
struct _usual usual = safe->usual;
|
|
31
|
+
Col current_object_location = usual.ctail - 1;
|
|
32
|
+
|
|
33
|
+
long int number_of_items_in_stack = usual.vtail - usual.vhead;
|
|
34
|
+
long int number_of_items_in_array = number_of_items_in_stack - current_object_location->vi - 1;
|
|
35
|
+
|
|
36
|
+
if (safe->max_array_size > number_of_items_in_array) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
rb_raise(max_array_size_error_class, "Too many array items!");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static void check_max_depth(safe_T safe, ojParser p) {
|
|
44
|
+
if (NIL_P(safe->max_depth) || safe->max_depth >= (p->depth + 1)) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
rb_raise(max_depth_error_class, "JSON is too deep!");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static void check_max_total_elements(safe_T safe) {
|
|
52
|
+
/*
|
|
53
|
+
* We check if `max_total_elements` is greater than `current_elements_count`
|
|
54
|
+
* (instead of greater than or equal) because top-level elements (e.g., [],
|
|
55
|
+
* null, true) are not counted. As a result, `current_elements_count`
|
|
56
|
+
* always holds one less than the actual total.
|
|
57
|
+
*/
|
|
58
|
+
if (NIL_P(safe->max_total_elements) || safe->max_total_elements > safe->current_elements_count) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
rb_raise(max_total_elements_error_class, "Too many elements!");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static void safe_start(ojParser p) {
|
|
66
|
+
safe_T safe = (safe_T)p->ctx;
|
|
67
|
+
|
|
68
|
+
safe->current_hash_size = 0;
|
|
69
|
+
safe->current_array_size = 0;
|
|
70
|
+
safe->current_elements_count = 0;
|
|
71
|
+
|
|
72
|
+
safe->delegated_start_func(p);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static void safe_open_object(ojParser p) {
|
|
76
|
+
safe_T safe = (safe_T)p->ctx;
|
|
77
|
+
|
|
78
|
+
safe->current_hash_size++;
|
|
79
|
+
safe->current_elements_count++;
|
|
80
|
+
|
|
81
|
+
check_array_size(safe);
|
|
82
|
+
check_max_depth(safe, p);
|
|
83
|
+
check_max_total_elements(safe);
|
|
84
|
+
|
|
85
|
+
safe->delegated_open_object_func(p);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static void safe_open_array(ojParser p) {
|
|
89
|
+
safe_T safe = (safe_T)p->ctx;
|
|
90
|
+
|
|
91
|
+
safe->current_array_size++;
|
|
92
|
+
safe->current_elements_count++;
|
|
93
|
+
|
|
94
|
+
check_array_size(safe);
|
|
95
|
+
check_max_depth(safe, p);
|
|
96
|
+
check_max_total_elements(safe);
|
|
97
|
+
|
|
98
|
+
safe->delegated_open_array_func(p);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
DEFINE_DELEGATED_FUNCTION(add_null);
|
|
102
|
+
DEFINE_DELEGATED_FUNCTION(add_true);
|
|
103
|
+
DEFINE_DELEGATED_FUNCTION(add_false);
|
|
104
|
+
DEFINE_DELEGATED_FUNCTION(add_int);
|
|
105
|
+
DEFINE_DELEGATED_FUNCTION(add_float);
|
|
106
|
+
DEFINE_DELEGATED_FUNCTION(add_big);
|
|
107
|
+
DEFINE_DELEGATED_FUNCTION(add_str);
|
|
108
|
+
|
|
109
|
+
static void safe_open_object_key(ojParser p) {
|
|
110
|
+
safe_T safe = (safe_T)p->ctx;
|
|
111
|
+
|
|
112
|
+
safe->current_hash_size++;
|
|
113
|
+
safe->current_elements_count += 2;
|
|
114
|
+
|
|
115
|
+
check_object_size(safe);
|
|
116
|
+
check_max_depth(safe, p);
|
|
117
|
+
check_max_total_elements(safe);
|
|
118
|
+
|
|
119
|
+
safe->delegated_open_object_key_func(p);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
static void safe_open_array_key(ojParser p) {
|
|
123
|
+
safe_T safe = (safe_T)p->ctx;
|
|
124
|
+
|
|
125
|
+
safe->current_array_size++;
|
|
126
|
+
safe->current_elements_count += 2;
|
|
127
|
+
|
|
128
|
+
check_object_size(safe);
|
|
129
|
+
check_max_depth(safe, p);
|
|
130
|
+
check_max_total_elements(safe);
|
|
131
|
+
|
|
132
|
+
safe->delegated_open_array_key_func(p);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
DEFINE_DELEGATED_OBJECT_FUNCTION(add_null);
|
|
136
|
+
DEFINE_DELEGATED_OBJECT_FUNCTION(add_true);
|
|
137
|
+
DEFINE_DELEGATED_OBJECT_FUNCTION(add_false);
|
|
138
|
+
DEFINE_DELEGATED_OBJECT_FUNCTION(add_int);
|
|
139
|
+
DEFINE_DELEGATED_OBJECT_FUNCTION(add_float);
|
|
140
|
+
DEFINE_DELEGATED_OBJECT_FUNCTION(add_big);
|
|
141
|
+
DEFINE_DELEGATED_OBJECT_FUNCTION(add_str);
|
|
142
|
+
|
|
143
|
+
void oj_init_safe_parser(ojParser p, safe_T safe, VALUE options) {
|
|
144
|
+
// Safe parser inherits all members of usual parser
|
|
145
|
+
oj_init_usual(p, &safe->usual);
|
|
146
|
+
|
|
147
|
+
safe->delegated_start_func = p->start;
|
|
148
|
+
p->start = safe_start;
|
|
149
|
+
|
|
150
|
+
Funcs f;
|
|
151
|
+
|
|
152
|
+
// Array parser functions
|
|
153
|
+
f = &p->funcs[ARRAY_FUN];
|
|
154
|
+
safe->delegated_open_object_func = f->open_object;
|
|
155
|
+
f->open_object = safe_open_object;
|
|
156
|
+
safe->delegated_open_array_func = f->open_array;
|
|
157
|
+
f->open_array = safe_open_array;
|
|
158
|
+
// The following overrides are done for counting objects
|
|
159
|
+
safe->delegated_add_null_func = f->add_null;
|
|
160
|
+
f->add_null = safe_add_null;
|
|
161
|
+
safe->delegated_add_true_func = f->add_true;
|
|
162
|
+
f->add_true = safe_add_true;
|
|
163
|
+
safe->delegated_add_false_func = f->add_false;
|
|
164
|
+
f->add_false = safe_add_false;
|
|
165
|
+
safe->delegated_add_int_func = f->add_int;
|
|
166
|
+
f->add_int = safe_add_int;
|
|
167
|
+
safe->delegated_add_float_func = f->add_float;
|
|
168
|
+
f->add_float = safe_add_float;
|
|
169
|
+
safe->delegated_add_big_func = f->add_big;
|
|
170
|
+
f->add_big = safe_add_big;
|
|
171
|
+
safe->delegated_add_str_func = f->add_str;
|
|
172
|
+
f->add_str = safe_add_str;
|
|
173
|
+
|
|
174
|
+
// Object parser functions
|
|
175
|
+
f = &p->funcs[OBJECT_FUN];
|
|
176
|
+
safe->delegated_open_object_key_func = f->open_object;
|
|
177
|
+
f->open_object = safe_open_object_key;
|
|
178
|
+
safe->delegated_open_array_key_func = f->open_array;
|
|
179
|
+
f->open_array = safe_open_array_key;
|
|
180
|
+
// The following overrides are done for counting objects
|
|
181
|
+
safe->delegated_add_null_key_func = f->add_null;
|
|
182
|
+
f->add_null = safe_add_null_key;
|
|
183
|
+
safe->delegated_add_true_key_func = f->add_true;
|
|
184
|
+
f->add_true = safe_add_true_key;
|
|
185
|
+
safe->delegated_add_false_key_func = f->add_false;
|
|
186
|
+
f->add_false = safe_add_false_key;
|
|
187
|
+
safe->delegated_add_int_key_func = f->add_int;
|
|
188
|
+
f->add_int = safe_add_int_key;
|
|
189
|
+
safe->delegated_add_float_key_func = f->add_float;
|
|
190
|
+
f->add_float = safe_add_float_key;
|
|
191
|
+
safe->delegated_add_big_key_func = f->add_big;
|
|
192
|
+
f->add_big = safe_add_big_key;
|
|
193
|
+
safe->delegated_add_str_key_func = f->add_str;
|
|
194
|
+
f->add_str = safe_add_str_key;
|
|
195
|
+
|
|
196
|
+
SET_CONFIG(max_hash_size);
|
|
197
|
+
SET_CONFIG(max_array_size);
|
|
198
|
+
SET_CONFIG(max_depth);
|
|
199
|
+
SET_CONFIG(max_total_elements);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
void oj_set_parser_safe(ojParser p, VALUE options) {
|
|
203
|
+
safe_T s = OJ_R_ALLOC(struct _safe_S);
|
|
204
|
+
|
|
205
|
+
oj_init_safe_parser(p, s, options);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
void oj_safe_init(VALUE parser_class) {
|
|
209
|
+
VALUE validation_error_class = rb_define_class_under(parser_class, "ValidationError", rb_eRuntimeError);
|
|
210
|
+
|
|
211
|
+
max_hash_size_error_class = rb_define_class_under(parser_class, "HashSizeError", validation_error_class);
|
|
212
|
+
max_array_size_error_class = rb_define_class_under(parser_class, "ArraySizeError", validation_error_class);
|
|
213
|
+
max_depth_error_class = rb_define_class_under(parser_class, "DepthError", validation_error_class);
|
|
214
|
+
max_total_elements_error_class = rb_define_class_under(parser_class, "TotalElementsError", validation_error_class);
|
|
215
|
+
|
|
216
|
+
rb_gc_register_address(&max_hash_size_error_class);
|
|
217
|
+
rb_gc_register_address(&max_array_size_error_class);
|
|
218
|
+
rb_gc_register_address(&max_depth_error_class);
|
|
219
|
+
rb_gc_register_address(&max_total_elements_error_class);
|
|
220
|
+
|
|
221
|
+
max_hash_size_sym = ID2SYM(rb_intern("max_hash_size"));
|
|
222
|
+
max_array_size_sym = ID2SYM(rb_intern("max_array_size"));
|
|
223
|
+
max_depth_sym = ID2SYM(rb_intern("max_depth"));
|
|
224
|
+
max_total_elements_sym = ID2SYM(rb_intern("max_total_elements"));
|
|
225
|
+
|
|
226
|
+
rb_gc_register_address(&max_hash_size_sym);
|
|
227
|
+
rb_gc_register_address(&max_array_size_sym);
|
|
228
|
+
rb_gc_register_address(&max_depth_sym);
|
|
229
|
+
rb_gc_register_address(&max_total_elements_sym);
|
|
230
|
+
}
|
data/ext/oj/safe.h
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
#include <ruby.h>
|
|
2
|
+
|
|
3
|
+
#include "parser.h"
|
|
4
|
+
#include "usual.h"
|
|
5
|
+
|
|
6
|
+
#define SET_CONFIG(config_name) \
|
|
7
|
+
do { \
|
|
8
|
+
VALUE rb_##config_name = rb_hash_aref(options, config_name##_sym); \
|
|
9
|
+
\
|
|
10
|
+
if (RB_INTEGER_TYPE_P(rb_##config_name)) { \
|
|
11
|
+
safe->config_name = NUM2LONG(rb_##config_name); \
|
|
12
|
+
} else if (!NIL_P(rb_##config_name)) { \
|
|
13
|
+
rb_raise(rb_eArgError, "Incorrect value provided for `" #config_name "`"); \
|
|
14
|
+
} else { \
|
|
15
|
+
safe->config_name = Qnil; \
|
|
16
|
+
} \
|
|
17
|
+
} while (0);
|
|
18
|
+
|
|
19
|
+
#define DEFINE_DELEGATED_FUNCTION(function_name) \
|
|
20
|
+
static void safe_##function_name(ojParser p) { \
|
|
21
|
+
safe_T safe = (safe_T)p->ctx; \
|
|
22
|
+
\
|
|
23
|
+
safe->current_elements_count++; \
|
|
24
|
+
\
|
|
25
|
+
check_array_size(safe); \
|
|
26
|
+
check_max_total_elements(safe); \
|
|
27
|
+
\
|
|
28
|
+
safe->delegated_##function_name##_func(p); \
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
#define DEFINE_DELEGATED_OBJECT_FUNCTION(function_name) \
|
|
32
|
+
static void safe_##function_name##_key(ojParser p) { \
|
|
33
|
+
safe_T safe = (safe_T)p->ctx; \
|
|
34
|
+
\
|
|
35
|
+
safe->current_elements_count += 2; \
|
|
36
|
+
\
|
|
37
|
+
check_object_size(safe); \
|
|
38
|
+
check_max_total_elements(safe); \
|
|
39
|
+
\
|
|
40
|
+
safe->delegated_##function_name##_key_func(p); \
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
typedef struct _safe_S {
|
|
44
|
+
struct _usual usual;
|
|
45
|
+
|
|
46
|
+
long int max_hash_size;
|
|
47
|
+
long int max_array_size;
|
|
48
|
+
long int max_depth;
|
|
49
|
+
long int max_total_elements;
|
|
50
|
+
long int max_json_size_bytes;
|
|
51
|
+
|
|
52
|
+
long int current_hash_size;
|
|
53
|
+
long int current_array_size;
|
|
54
|
+
long int current_elements_count;
|
|
55
|
+
|
|
56
|
+
void (*delegated_start_func)(struct _ojParser *p);
|
|
57
|
+
|
|
58
|
+
// Array functions
|
|
59
|
+
void (*delegated_open_object_func)(struct _ojParser *p);
|
|
60
|
+
void (*delegated_open_array_func)(struct _ojParser *p);
|
|
61
|
+
void (*delegated_add_null_func)(struct _ojParser *p);
|
|
62
|
+
void (*delegated_add_true_func)(struct _ojParser *p);
|
|
63
|
+
void (*delegated_add_false_func)(struct _ojParser *p);
|
|
64
|
+
void (*delegated_add_int_func)(struct _ojParser *p);
|
|
65
|
+
void (*delegated_add_float_func)(struct _ojParser *p);
|
|
66
|
+
void (*delegated_add_big_func)(struct _ojParser *p);
|
|
67
|
+
void (*delegated_add_str_func)(struct _ojParser *p);
|
|
68
|
+
|
|
69
|
+
// Object functions
|
|
70
|
+
void (*delegated_open_object_key_func)(struct _ojParser *p);
|
|
71
|
+
void (*delegated_open_array_key_func)(struct _ojParser *p);
|
|
72
|
+
void (*delegated_add_null_key_func)(struct _ojParser *p);
|
|
73
|
+
void (*delegated_add_true_key_func)(struct _ojParser *p);
|
|
74
|
+
void (*delegated_add_false_key_func)(struct _ojParser *p);
|
|
75
|
+
void (*delegated_add_int_key_func)(struct _ojParser *p);
|
|
76
|
+
void (*delegated_add_float_key_func)(struct _ojParser *p);
|
|
77
|
+
void (*delegated_add_big_key_func)(struct _ojParser *p);
|
|
78
|
+
void (*delegated_add_str_key_func)(struct _ojParser *p);
|
|
79
|
+
} *safe_T;
|
data/ext/oj/saj.c
CHANGED
|
@@ -94,8 +94,7 @@ inline static void call_add_value(VALUE handler, VALUE value, const char *key) {
|
|
|
94
94
|
if (0 == key) {
|
|
95
95
|
k = Qnil;
|
|
96
96
|
} else {
|
|
97
|
-
k =
|
|
98
|
-
k = oj_encode(k);
|
|
97
|
+
k = rb_utf8_str_new_cstr(key);
|
|
99
98
|
}
|
|
100
99
|
rb_funcall(handler, oj_add_value_id, 2, value, k);
|
|
101
100
|
}
|
|
@@ -106,8 +105,7 @@ inline static void call_no_value(VALUE handler, ID method, const char *key) {
|
|
|
106
105
|
if (0 == key) {
|
|
107
106
|
k = Qnil;
|
|
108
107
|
} else {
|
|
109
|
-
k =
|
|
110
|
-
k = oj_encode(k);
|
|
108
|
+
k = rb_utf8_str_new_cstr(key);
|
|
111
109
|
}
|
|
112
110
|
rb_funcall(handler, method, 1, k);
|
|
113
111
|
}
|
|
@@ -257,9 +255,8 @@ static void read_str(ParseInfo pi, const char *key) {
|
|
|
257
255
|
|
|
258
256
|
text = read_quoted_value(pi);
|
|
259
257
|
if (pi->has_add_value) {
|
|
260
|
-
VALUE s =
|
|
258
|
+
VALUE s = rb_utf8_str_new_cstr(text);
|
|
261
259
|
|
|
262
|
-
s = oj_encode(s);
|
|
263
260
|
call_add_value(pi->handler, s, key);
|
|
264
261
|
}
|
|
265
262
|
}
|
|
@@ -651,8 +648,16 @@ oj_saj_parse(int argc, VALUE *argv, VALUE self) {
|
|
|
651
648
|
len = lseek(fd, 0, SEEK_END);
|
|
652
649
|
lseek(fd, 0, SEEK_SET);
|
|
653
650
|
json = OJ_R_ALLOC_N(char, len + 1);
|
|
654
|
-
|
|
655
|
-
|
|
651
|
+
{
|
|
652
|
+
size_t total = 0;
|
|
653
|
+
|
|
654
|
+
while (total < len) {
|
|
655
|
+
cnt = read(fd, json + total, len - total);
|
|
656
|
+
if (cnt <= 0) {
|
|
657
|
+
rb_raise(rb_eIOError, "failed to read from IO Object.");
|
|
658
|
+
}
|
|
659
|
+
total += cnt;
|
|
660
|
+
}
|
|
656
661
|
}
|
|
657
662
|
json[len] = '\0';
|
|
658
663
|
#endif
|
data/ext/oj/scp.c
CHANGED
|
@@ -56,9 +56,8 @@ static void add_value(ParseInfo pi, VALUE val) {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
static void add_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
|
|
59
|
-
volatile VALUE rstr =
|
|
59
|
+
volatile VALUE rstr = rb_utf8_str_new(str, len);
|
|
60
60
|
|
|
61
|
-
rstr = oj_encode(rstr);
|
|
62
61
|
rb_funcall(pi->handler, oj_add_value_id, 1, rstr);
|
|
63
62
|
}
|
|
64
63
|
|
|
@@ -87,9 +86,8 @@ static VALUE hash_key(ParseInfo pi, const char *key, size_t klen) {
|
|
|
87
86
|
}
|
|
88
87
|
|
|
89
88
|
static void hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, const char *orig) {
|
|
90
|
-
volatile VALUE rstr =
|
|
89
|
+
volatile VALUE rstr = rb_utf8_str_new(str, len);
|
|
91
90
|
|
|
92
|
-
rstr = oj_encode(rstr);
|
|
93
91
|
rb_funcall(pi->handler, oj_hash_set_id, 3, stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, kval), rstr);
|
|
94
92
|
}
|
|
95
93
|
|
|
@@ -107,9 +105,8 @@ static void hash_set_value(ParseInfo pi, Val kval, VALUE value) {
|
|
|
107
105
|
}
|
|
108
106
|
|
|
109
107
|
static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
|
|
110
|
-
volatile VALUE rstr =
|
|
108
|
+
volatile VALUE rstr = rb_utf8_str_new(str, len);
|
|
111
109
|
|
|
112
|
-
rstr = oj_encode(rstr);
|
|
113
110
|
rb_funcall(pi->handler, oj_array_append_id, 2, stack_peek(&pi->stack)->val, rstr);
|
|
114
111
|
}
|
|
115
112
|
|
data/ext/oj/simd.h
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
#ifndef OJ_SIMD_H
|
|
2
|
+
#define OJ_SIMD_H
|
|
3
|
+
|
|
4
|
+
// SIMD architecture detection and configuration
|
|
5
|
+
// This header provides unified SIMD support across different CPU architectures
|
|
6
|
+
// with cross-platform runtime detection (Windows/Linux/Mac)
|
|
7
|
+
|
|
8
|
+
// SIMD implementation enum - used for runtime selection
|
|
9
|
+
typedef enum _simd_implementation { SIMD_NONE, SIMD_NEON, SIMD_SSE2, SIMD_SSE42 } SIMD_Implementation;
|
|
10
|
+
|
|
11
|
+
// Define in oj.c.
|
|
12
|
+
extern SIMD_Implementation SIMD_Impl;
|
|
13
|
+
|
|
14
|
+
// Runtime CPU detection function (implemented in oj.c)
|
|
15
|
+
SIMD_Implementation oj_get_simd_implementation(void);
|
|
16
|
+
|
|
17
|
+
// =============================================================================
|
|
18
|
+
// Compiler compatibility macros
|
|
19
|
+
// =============================================================================
|
|
20
|
+
|
|
21
|
+
// Branch prediction hints
|
|
22
|
+
#if defined(__GNUC__) || defined(__clang__)
|
|
23
|
+
#define OJ_LIKELY(x) __builtin_expect(!!(x), 1)
|
|
24
|
+
#define OJ_UNLIKELY(x) __builtin_expect(!!(x), 0)
|
|
25
|
+
#else
|
|
26
|
+
#define OJ_LIKELY(x) (x)
|
|
27
|
+
#define OJ_UNLIKELY(x) (x)
|
|
28
|
+
#endif
|
|
29
|
+
|
|
30
|
+
// Prefetch hints
|
|
31
|
+
#if defined(__GNUC__) || defined(__clang__)
|
|
32
|
+
#define OJ_PREFETCH(addr) __builtin_prefetch(addr, 0, 0)
|
|
33
|
+
#elif defined(_MSC_VER)
|
|
34
|
+
#include <intrin.h>
|
|
35
|
+
#define OJ_PREFETCH(addr) _mm_prefetch((const char *)(addr), _MM_HINT_T0)
|
|
36
|
+
#else
|
|
37
|
+
#define OJ_PREFETCH(addr) ((void)0)
|
|
38
|
+
#endif
|
|
39
|
+
|
|
40
|
+
// Count trailing zeros (for SSE2 mask scanning)
|
|
41
|
+
#if defined(__GNUC__) || defined(__clang__)
|
|
42
|
+
#define OJ_CTZ(x) __builtin_ctz(x)
|
|
43
|
+
#define OJ_CTZ64(x) __builtin_ctzll(x)
|
|
44
|
+
#elif defined(_MSC_VER)
|
|
45
|
+
#include <intrin.h>
|
|
46
|
+
static __inline int oj_ctz_msvc(unsigned int x) {
|
|
47
|
+
unsigned long index;
|
|
48
|
+
if (0 == x) {
|
|
49
|
+
return 32;
|
|
50
|
+
}
|
|
51
|
+
_BitScanForward(&index, x);
|
|
52
|
+
return (int)index;
|
|
53
|
+
}
|
|
54
|
+
static __inline int oj_ctz64_msvc(uint64_t x) {
|
|
55
|
+
unsigned long index;
|
|
56
|
+
if (_BitScanForward64(&index, x)) {
|
|
57
|
+
return (int)index;
|
|
58
|
+
}
|
|
59
|
+
return 64;
|
|
60
|
+
}
|
|
61
|
+
#define OJ_CTZ(x) oj_ctz_msvc(x)
|
|
62
|
+
#define OJ_CTZ64(x) oj_ctz64_msvc(x)
|
|
63
|
+
#else
|
|
64
|
+
// Fallback: naive implementation
|
|
65
|
+
static inline int oj_ctz_fallback(unsigned int x) {
|
|
66
|
+
int count = 0;
|
|
67
|
+
while ((x & 1) == 0 && count < 32) {
|
|
68
|
+
x >>= 1;
|
|
69
|
+
count++;
|
|
70
|
+
}
|
|
71
|
+
return count;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static inline int oj_ctz64_fallback(uint64_t x) {
|
|
75
|
+
int count = 0;
|
|
76
|
+
while ((x & 1) == 0 && count < 64) {
|
|
77
|
+
x >>= 1;
|
|
78
|
+
count++;
|
|
79
|
+
}
|
|
80
|
+
return count;
|
|
81
|
+
}
|
|
82
|
+
#define OJ_CTZ(x) oj_ctz_fallback(x)
|
|
83
|
+
#define OJ_CTZ64(x) oj_ctz64_fallback(x)
|
|
84
|
+
#endif
|
|
85
|
+
|
|
86
|
+
// =============================================================================
|
|
87
|
+
// x86/x86_64 SIMD detection
|
|
88
|
+
// =============================================================================
|
|
89
|
+
#if defined(__x86_64__) || defined(__i386__) || defined(_M_IX86) || defined(_M_X64)
|
|
90
|
+
#define HAVE_SIMD_X86 1
|
|
91
|
+
|
|
92
|
+
// Include appropriate SIMD headers
|
|
93
|
+
#if defined(_MSC_VER)
|
|
94
|
+
// MSVC: use intrin.h for all intrinsics
|
|
95
|
+
#include <intrin.h>
|
|
96
|
+
#define HAVE_SIMD_SSE4_2 1
|
|
97
|
+
#define HAVE_SIMD_SSE2 1
|
|
98
|
+
#elif defined(__GNUC__) || defined(__clang__)
|
|
99
|
+
// GCC/Clang: check for header availability and include them
|
|
100
|
+
// We include headers but use target attributes to enable instructions per-function
|
|
101
|
+
// Include cpuid.h for __get_cpuid fallback when __builtin_cpu_supports is unavailable
|
|
102
|
+
#if __has_include(<cpuid.h>)
|
|
103
|
+
#include <cpuid.h>
|
|
104
|
+
#endif
|
|
105
|
+
#if defined(__SSE4_2__) || defined(__SSE2__)
|
|
106
|
+
// If any SSE is enabled globally, x86intrin.h should be available
|
|
107
|
+
#include <x86intrin.h>
|
|
108
|
+
#define HAVE_SIMD_SSE4_2 1
|
|
109
|
+
#define HAVE_SIMD_SSE2 1
|
|
110
|
+
#else
|
|
111
|
+
// Try to include headers anyway for target attribute functions
|
|
112
|
+
#if __has_include(<x86intrin.h>)
|
|
113
|
+
#include <x86intrin.h>
|
|
114
|
+
#define HAVE_SIMD_SSE4_2 1
|
|
115
|
+
#define HAVE_SIMD_SSE2 1
|
|
116
|
+
#elif __has_include(<nmmintrin.h>)
|
|
117
|
+
#include <nmmintrin.h>
|
|
118
|
+
#define HAVE_SIMD_SSE4_2 1
|
|
119
|
+
#define HAVE_SIMD_SSE2 1
|
|
120
|
+
#elif __has_include(<emmintrin.h>)
|
|
121
|
+
#include <emmintrin.h>
|
|
122
|
+
#define HAVE_SIMD_SSE2 1
|
|
123
|
+
#endif
|
|
124
|
+
#endif
|
|
125
|
+
#endif
|
|
126
|
+
|
|
127
|
+
// Target attribute macros for function-level SIMD enabling
|
|
128
|
+
#if defined(__clang__) || defined(__GNUC__)
|
|
129
|
+
#define OJ_TARGET_SSE42 __attribute__((target("sse4.2")))
|
|
130
|
+
#define OJ_TARGET_SSE2 __attribute__((target("sse2")))
|
|
131
|
+
#else
|
|
132
|
+
// MSVC doesn't need target attributes - intrinsics are always available
|
|
133
|
+
#define OJ_TARGET_SSE42
|
|
134
|
+
#define OJ_TARGET_SSE2
|
|
135
|
+
#endif
|
|
136
|
+
|
|
137
|
+
#endif // x86/x86_64
|
|
138
|
+
|
|
139
|
+
// =============================================================================
|
|
140
|
+
// ARM NEON detection
|
|
141
|
+
// =============================================================================
|
|
142
|
+
#if defined(__ARM_NEON) || defined(__ARM_NEON__) || defined(__aarch64__) || defined(_M_ARM64)
|
|
143
|
+
#define HAVE_SIMD_NEON 1
|
|
144
|
+
#define SIMD_MINIMUM_THRESHOLD 6
|
|
145
|
+
#include <arm_neon.h>
|
|
146
|
+
#endif
|
|
147
|
+
|
|
148
|
+
// =============================================================================
|
|
149
|
+
// SIMD type string for debugging/logging
|
|
150
|
+
// =============================================================================
|
|
151
|
+
#if defined(HAVE_SIMD_SSE4_2) || defined(HAVE_SIMD_SSE2)
|
|
152
|
+
#define HAVE_SIMD_STRING_SCAN 1
|
|
153
|
+
#define SIMD_TYPE "x86 (runtime detected)"
|
|
154
|
+
#elif defined(HAVE_SIMD_NEON)
|
|
155
|
+
#define HAVE_SIMD_STRING_SCAN 1
|
|
156
|
+
#define SIMD_TYPE "NEON"
|
|
157
|
+
#else
|
|
158
|
+
#define SIMD_TYPE "none"
|
|
159
|
+
#endif
|
|
160
|
+
|
|
161
|
+
#if defined(HAVE_SIMD_SSE4_2)
|
|
162
|
+
|
|
163
|
+
#define SIMD_MINIMUM_THRESHOLD 6
|
|
164
|
+
|
|
165
|
+
extern void initialize_sse42(void);
|
|
166
|
+
|
|
167
|
+
static inline OJ_TARGET_SSE42 __m128i vector_lookup_sse42(__m128i input, __m128i *lookup_table, int tab_size) {
|
|
168
|
+
// Extract high 4 bits to determine which 16-byte chunk (0-15)
|
|
169
|
+
__m128i hi_index = _mm_and_si128(_mm_srli_epi32(input, 4), _mm_set1_epi8(0x0F));
|
|
170
|
+
|
|
171
|
+
// Extract low 4 bits for index within the chunk (0-15)
|
|
172
|
+
__m128i low_index = _mm_and_si128(input, _mm_set1_epi8(0x0F));
|
|
173
|
+
|
|
174
|
+
// Perform lookups in all 16 tables
|
|
175
|
+
__m128i results[16];
|
|
176
|
+
for (int i = 0; i < tab_size; i++) {
|
|
177
|
+
results[i] = _mm_shuffle_epi8(lookup_table[i], low_index);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Create masks for each chunk and blend results
|
|
181
|
+
__m128i final_result = _mm_setzero_si128();
|
|
182
|
+
|
|
183
|
+
for (int i = 0; i < tab_size; i++) {
|
|
184
|
+
__m128i mask = _mm_cmpeq_epi8(hi_index, _mm_set1_epi8(i));
|
|
185
|
+
__m128i masked_result = _mm_and_si128(mask, results[i]);
|
|
186
|
+
final_result = _mm_or_si128(final_result, masked_result);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return final_result;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
#endif
|
|
193
|
+
|
|
194
|
+
#ifndef __has_builtin
|
|
195
|
+
#define __has_builtin(x) 0
|
|
196
|
+
#endif
|
|
197
|
+
|
|
198
|
+
#if __has_builtin(__builtin_memcpy)
|
|
199
|
+
#define HAVE_FAST_MEMCPY 1
|
|
200
|
+
|
|
201
|
+
inline static void fast_memcpy16(void *dest, const void *src, size_t n) {
|
|
202
|
+
char *d = (char *)dest;
|
|
203
|
+
char *s = (char *)src;
|
|
204
|
+
if (n >= 8) {
|
|
205
|
+
__builtin_memcpy(d, s, 8);
|
|
206
|
+
__builtin_memcpy(d + n - 8, s + n - 8, 8);
|
|
207
|
+
} else if (n >= 4) {
|
|
208
|
+
__builtin_memcpy(d, s, 4);
|
|
209
|
+
__builtin_memcpy(d + n - 4, s + n - 4, 4);
|
|
210
|
+
} else if (n >= 2) {
|
|
211
|
+
__builtin_memcpy(d, s, 2);
|
|
212
|
+
__builtin_memcpy(d + n - 2, s + n - 2, 2);
|
|
213
|
+
} else if (n >= 1) {
|
|
214
|
+
*d = *s;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
#endif
|
|
218
|
+
|
|
219
|
+
#endif /* OJ_SIMD_H */
|
data/ext/oj/sparse.c
CHANGED
|
@@ -399,6 +399,7 @@ static void read_num(ParseInfo pi) {
|
|
|
399
399
|
char c;
|
|
400
400
|
|
|
401
401
|
reader_protect(&pi->rd);
|
|
402
|
+
ni.pi = pi;
|
|
402
403
|
ni.i = 0;
|
|
403
404
|
ni.num = 0;
|
|
404
405
|
ni.div = 1;
|
|
@@ -415,7 +416,7 @@ static void read_num(ParseInfo pi) {
|
|
|
415
416
|
ni.bigdec_load = pi->options.compat_bigdec;
|
|
416
417
|
} else {
|
|
417
418
|
ni.no_big = (FloatDec == pi->options.bigdec_load || FastDec == pi->options.bigdec_load ||
|
|
418
|
-
|
|
419
|
+
RubyDec == pi->options.bigdec_load);
|
|
419
420
|
ni.bigdec_load = pi->options.bigdec_load;
|
|
420
421
|
}
|
|
421
422
|
|
|
@@ -549,6 +550,7 @@ static void read_nan(ParseInfo pi) {
|
|
|
549
550
|
struct _numInfo ni;
|
|
550
551
|
char c;
|
|
551
552
|
|
|
553
|
+
ni.pi = pi;
|
|
552
554
|
ni.str = pi->rd.str;
|
|
553
555
|
ni.i = 0;
|
|
554
556
|
ni.num = 0;
|
|
@@ -565,7 +567,7 @@ static void read_nan(ParseInfo pi) {
|
|
|
565
567
|
ni.bigdec_load = pi->options.compat_bigdec;
|
|
566
568
|
} else {
|
|
567
569
|
ni.no_big = (FloatDec == pi->options.bigdec_load || FastDec == pi->options.bigdec_load ||
|
|
568
|
-
|
|
570
|
+
RubyDec == pi->options.bigdec_load);
|
|
569
571
|
ni.bigdec_load = pi->options.bigdec_load;
|
|
570
572
|
}
|
|
571
573
|
|
|
@@ -745,6 +747,7 @@ void oj_sparse2(ParseInfo pi) {
|
|
|
745
747
|
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "expected NaN");
|
|
746
748
|
return;
|
|
747
749
|
}
|
|
750
|
+
ni.pi = pi;
|
|
748
751
|
ni.str = pi->rd.str;
|
|
749
752
|
ni.i = 0;
|
|
750
753
|
ni.num = 0;
|
|
@@ -761,7 +764,7 @@ void oj_sparse2(ParseInfo pi) {
|
|
|
761
764
|
ni.bigdec_load = pi->options.compat_bigdec;
|
|
762
765
|
} else {
|
|
763
766
|
ni.no_big = (FloatDec == pi->options.bigdec_load || FastDec == pi->options.bigdec_load ||
|
|
764
|
-
|
|
767
|
+
RubyDec == pi->options.bigdec_load);
|
|
765
768
|
ni.bigdec_load = pi->options.bigdec_load;
|
|
766
769
|
}
|
|
767
770
|
add_num_value(pi, &ni);
|
data/ext/oj/stream_writer.c
CHANGED
|
@@ -42,22 +42,12 @@ static void stream_writer_write(StreamWriter sw) {
|
|
|
42
42
|
|
|
43
43
|
switch (sw->type) {
|
|
44
44
|
case STRING_IO:
|
|
45
|
-
case STREAM_IO:
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
// Oddly enough, when pushing ASCII characters with UTF-8 encoding or
|
|
49
|
-
// even ASCII-8BIT does not change the output encoding. Pushing any
|
|
50
|
-
// non-ASCII no matter what the encoding changes the output encoding
|
|
51
|
-
// to ASCII-8BIT if it the string is not forced to UTF-8 here.
|
|
52
|
-
rs = oj_encode(rs);
|
|
45
|
+
case STREAM_IO:
|
|
46
|
+
case FILE_IO: {
|
|
47
|
+
volatile VALUE rs = rb_utf8_str_new(sw->sw.out.buf, size);
|
|
53
48
|
rb_funcall(sw->stream, oj_write_id, 1, rs);
|
|
54
49
|
break;
|
|
55
50
|
}
|
|
56
|
-
case FILE_IO:
|
|
57
|
-
if (size != write(sw->fd, sw->sw.out.buf, size)) {
|
|
58
|
-
rb_raise(rb_eIOError, "Write failed. [_%d_:%s]\n", errno, strerror(errno));
|
|
59
|
-
}
|
|
60
|
-
break;
|
|
61
51
|
default: rb_raise(rb_eArgError, "expected an IO Object.");
|
|
62
52
|
}
|
|
63
53
|
stream_writer_reset_buf(sw);
|