oj_windows 3.16.15

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 (102) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +44 -0
  3. data/LICENSE +21 -0
  4. data/README.md +164 -0
  5. data/ext/oj_windows/buf.h +85 -0
  6. data/ext/oj_windows/cache.c +339 -0
  7. data/ext/oj_windows/cache.h +22 -0
  8. data/ext/oj_windows/cache8.c +105 -0
  9. data/ext/oj_windows/cache8.h +21 -0
  10. data/ext/oj_windows/circarray.c +64 -0
  11. data/ext/oj_windows/circarray.h +22 -0
  12. data/ext/oj_windows/code.c +214 -0
  13. data/ext/oj_windows/code.h +40 -0
  14. data/ext/oj_windows/compat.c +239 -0
  15. data/ext/oj_windows/custom.c +1074 -0
  16. data/ext/oj_windows/debug.c +126 -0
  17. data/ext/oj_windows/dump.c +1556 -0
  18. data/ext/oj_windows/dump.h +110 -0
  19. data/ext/oj_windows/dump_compat.c +901 -0
  20. data/ext/oj_windows/dump_leaf.c +162 -0
  21. data/ext/oj_windows/dump_object.c +710 -0
  22. data/ext/oj_windows/dump_strict.c +405 -0
  23. data/ext/oj_windows/encode.h +16 -0
  24. data/ext/oj_windows/err.c +57 -0
  25. data/ext/oj_windows/err.h +67 -0
  26. data/ext/oj_windows/extconf.rb +77 -0
  27. data/ext/oj_windows/fast.c +1710 -0
  28. data/ext/oj_windows/intern.c +325 -0
  29. data/ext/oj_windows/intern.h +22 -0
  30. data/ext/oj_windows/mem.c +320 -0
  31. data/ext/oj_windows/mem.h +53 -0
  32. data/ext/oj_windows/mimic_json.c +919 -0
  33. data/ext/oj_windows/object.c +726 -0
  34. data/ext/oj_windows/odd.c +245 -0
  35. data/ext/oj_windows/odd.h +43 -0
  36. data/ext/oj_windows/oj.c +2097 -0
  37. data/ext/oj_windows/oj.h +420 -0
  38. data/ext/oj_windows/parse.c +1317 -0
  39. data/ext/oj_windows/parse.h +113 -0
  40. data/ext/oj_windows/parser.c +1600 -0
  41. data/ext/oj_windows/parser.h +103 -0
  42. data/ext/oj_windows/rails.c +1484 -0
  43. data/ext/oj_windows/rails.h +18 -0
  44. data/ext/oj_windows/reader.c +222 -0
  45. data/ext/oj_windows/reader.h +137 -0
  46. data/ext/oj_windows/resolve.c +80 -0
  47. data/ext/oj_windows/resolve.h +12 -0
  48. data/ext/oj_windows/rxclass.c +144 -0
  49. data/ext/oj_windows/rxclass.h +26 -0
  50. data/ext/oj_windows/saj.c +675 -0
  51. data/ext/oj_windows/saj2.c +584 -0
  52. data/ext/oj_windows/saj2.h +23 -0
  53. data/ext/oj_windows/scp.c +187 -0
  54. data/ext/oj_windows/simd.h +47 -0
  55. data/ext/oj_windows/sparse.c +946 -0
  56. data/ext/oj_windows/stream_writer.c +329 -0
  57. data/ext/oj_windows/strict.c +189 -0
  58. data/ext/oj_windows/string_writer.c +517 -0
  59. data/ext/oj_windows/trace.c +72 -0
  60. data/ext/oj_windows/trace.h +55 -0
  61. data/ext/oj_windows/usual.c +1218 -0
  62. data/ext/oj_windows/usual.h +69 -0
  63. data/ext/oj_windows/util.c +136 -0
  64. data/ext/oj_windows/util.h +20 -0
  65. data/ext/oj_windows/val_stack.c +101 -0
  66. data/ext/oj_windows/val_stack.h +151 -0
  67. data/ext/oj_windows/validate.c +46 -0
  68. data/ext/oj_windows/wab.c +584 -0
  69. data/lib/oj/active_support_helper.rb +39 -0
  70. data/lib/oj/bag.rb +95 -0
  71. data/lib/oj/easy_hash.rb +52 -0
  72. data/lib/oj/error.rb +21 -0
  73. data/lib/oj/json.rb +188 -0
  74. data/lib/oj/mimic.rb +301 -0
  75. data/lib/oj/saj.rb +80 -0
  76. data/lib/oj/schandler.rb +143 -0
  77. data/lib/oj/state.rb +135 -0
  78. data/lib/oj/version.rb +4 -0
  79. data/lib/oj_windows/active_support_helper.rb +39 -0
  80. data/lib/oj_windows/bag.rb +95 -0
  81. data/lib/oj_windows/easy_hash.rb +52 -0
  82. data/lib/oj_windows/error.rb +21 -0
  83. data/lib/oj_windows/json.rb +188 -0
  84. data/lib/oj_windows/mimic.rb +301 -0
  85. data/lib/oj_windows/saj.rb +80 -0
  86. data/lib/oj_windows/schandler.rb +143 -0
  87. data/lib/oj_windows/state.rb +135 -0
  88. data/lib/oj_windows/version.rb +4 -0
  89. data/lib/oj_windows.rb +15 -0
  90. data/pages/Advanced.md +38 -0
  91. data/pages/Compatibility.md +49 -0
  92. data/pages/Custom.md +37 -0
  93. data/pages/Encoding.md +61 -0
  94. data/pages/InstallOptions.md +20 -0
  95. data/pages/JsonGem.md +60 -0
  96. data/pages/Modes.md +94 -0
  97. data/pages/Options.md +339 -0
  98. data/pages/Parser.md +134 -0
  99. data/pages/Rails.md +85 -0
  100. data/pages/Security.md +43 -0
  101. data/pages/WAB.md +12 -0
  102. metadata +242 -0
@@ -0,0 +1,405 @@
1
+ // Copyright (c) 2012, 2017 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #include <errno.h>
5
+ #include <math.h>
6
+ #include <stdio.h>
7
+ #include <stdlib.h>
8
+ #include <string.h>
9
+ #include <time.h>
10
+ #if !IS_WINDOWS
11
+ #include <unistd.h>
12
+ #endif
13
+
14
+ #include "dump.h"
15
+ #include "trace.h"
16
+
17
+ // Workaround in case INFINITY is not defined in math.h or if the OS is CentOS
18
+ #ifdef _MSC_VER
19
+ #define OJ_INFINITY HUGE_VAL
20
+ #else
21
+ #define OJ_INFINITY (1.0 / 0.0)
22
+ #endif
23
+
24
+ typedef unsigned long ulong;
25
+
26
+ static const char inf_val[] = INF_VAL;
27
+ static const char ninf_val[] = NINF_VAL;
28
+ static const char nan_val[] = NAN_VAL;
29
+
30
+ static void raise_strict(VALUE obj) {
31
+ rb_raise(rb_eTypeError, "Failed to dump %s Object to JSON in strict mode.\n", rb_class2name(rb_obj_class(obj)));
32
+ }
33
+
34
+ // Removed dependencies on math due to problems with CentOS 5.4.
35
+ static void dump_float(VALUE obj, int depth, Out out, bool as_ok) {
36
+ char buf[64];
37
+ char* b;
38
+ double d = rb_num2dbl(obj);
39
+ size_t cnt = 0;
40
+
41
+ if (0.0 == d) {
42
+ b = buf;
43
+ *b++ = '0';
44
+ *b++ = '.';
45
+ *b++ = '0';
46
+ *b++ = '\0';
47
+ cnt = 3;
48
+ } else {
49
+ NanDump nd = out->opts->dump_opts.nan_dump;
50
+
51
+ if (AutoNan == nd) {
52
+ nd = RaiseNan;
53
+ }
54
+ if (OJ_INFINITY == d) {
55
+ switch (nd) {
56
+ case RaiseNan:
57
+ case WordNan: raise_strict(obj); break;
58
+ case NullNan:
59
+ strcpy(buf, "null");
60
+ cnt = 4;
61
+ break;
62
+ case HugeNan:
63
+ default:
64
+ strcpy(buf, inf_val);
65
+ cnt = sizeof(inf_val) - 1;
66
+ break;
67
+ }
68
+ } else if (-OJ_INFINITY == d) {
69
+ switch (nd) {
70
+ case RaiseNan:
71
+ case WordNan: raise_strict(obj); break;
72
+ case NullNan:
73
+ strcpy(buf, "null");
74
+ cnt = 4;
75
+ break;
76
+ case HugeNan:
77
+ default:
78
+ strcpy(buf, ninf_val);
79
+ cnt = sizeof(ninf_val) - 1;
80
+ break;
81
+ }
82
+ } else if (isnan(d)) {
83
+ switch (nd) {
84
+ case RaiseNan:
85
+ case WordNan: raise_strict(obj); break;
86
+ case NullNan:
87
+ strcpy(buf, "null");
88
+ cnt = 4;
89
+ break;
90
+ case HugeNan:
91
+ default:
92
+ strcpy(buf, nan_val);
93
+ cnt = sizeof(nan_val) - 1;
94
+ break;
95
+ }
96
+ } else if (d == (double)(long long int)d) {
97
+ cnt = snprintf(buf, sizeof(buf), "%.1f", d);
98
+ } else if (0 == out->opts->float_prec) {
99
+ volatile VALUE rstr = oj_safe_string_convert(obj);
100
+
101
+ cnt = RSTRING_LEN(rstr);
102
+ if ((int)sizeof(buf) <= cnt) {
103
+ cnt = sizeof(buf) - 1;
104
+ }
105
+ memcpy(buf, RSTRING_PTR(rstr), cnt);
106
+ buf[cnt] = '\0';
107
+ } else {
108
+ cnt = oj_dump_float_printf(buf, sizeof(buf), obj, d, out->opts->float_fmt);
109
+ }
110
+ }
111
+ assure_size(out, cnt);
112
+ APPEND_CHARS(out->cur, buf, cnt);
113
+ *out->cur = '\0';
114
+ }
115
+
116
+ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
117
+ size_t size;
118
+ size_t i;
119
+ size_t cnt;
120
+ int d2 = depth + 1;
121
+
122
+ if (Yes == out->opts->circular) {
123
+ if (0 > oj_check_circular(a, out)) {
124
+ oj_dump_nil(Qnil, 0, out, false);
125
+ return;
126
+ }
127
+ }
128
+ cnt = RARRAY_LEN(a);
129
+ *out->cur++ = '[';
130
+ size = 2;
131
+ assure_size(out, size);
132
+ if (0 == cnt) {
133
+ *out->cur++ = ']';
134
+ } else {
135
+ if (out->opts->dump_opts.use) {
136
+ size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
137
+ } else {
138
+ size = d2 * out->indent + 2;
139
+ }
140
+ assure_size(out, size * cnt);
141
+ cnt--;
142
+ for (i = 0; i <= cnt; i++) {
143
+ if (out->opts->dump_opts.use) {
144
+ if (0 < out->opts->dump_opts.array_size) {
145
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
146
+ }
147
+ if (0 < out->opts->dump_opts.indent_size) {
148
+ int i;
149
+ for (i = d2; 0 < i; i--) {
150
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
151
+ }
152
+ }
153
+ } else {
154
+ fill_indent(out, d2);
155
+ }
156
+ if (NullMode == out->opts->mode) {
157
+ oj_dump_null_val(RARRAY_AREF(a, i), d2, out);
158
+ } else {
159
+ oj_dump_strict_val(RARRAY_AREF(a, i), d2, out);
160
+ }
161
+ if (i < cnt) {
162
+ *out->cur++ = ',';
163
+ }
164
+ }
165
+ size = depth * out->indent + 1;
166
+ assure_size(out, size);
167
+ if (out->opts->dump_opts.use) {
168
+ // printf("*** d2: %u indent: %u '%s'\n", d2, out->opts->dump_opts->indent_size,
169
+ // out->opts->dump_opts->indent);
170
+ if (0 < out->opts->dump_opts.array_size) {
171
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
172
+ }
173
+ if (0 < out->opts->dump_opts.indent_size) {
174
+ int i;
175
+
176
+ for (i = depth; 0 < i; i--) {
177
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
178
+ }
179
+ }
180
+ } else {
181
+ fill_indent(out, depth);
182
+ }
183
+ *out->cur++ = ']';
184
+ }
185
+ *out->cur = '\0';
186
+ }
187
+
188
+ static int hash_cb(VALUE key, VALUE value, VALUE ov) {
189
+ Out out = (Out)ov;
190
+ int depth = out->depth;
191
+ long size;
192
+ int rtype = rb_type(key);
193
+
194
+ if (rtype != T_STRING && rtype != T_SYMBOL) {
195
+ rb_raise(rb_eTypeError,
196
+ "In :strict and :null mode all Hash keys must be Strings or Symbols, not %s.\n",
197
+ rb_class2name(rb_obj_class(key)));
198
+ }
199
+ if (out->omit_nil && Qnil == value) {
200
+ return ST_CONTINUE;
201
+ }
202
+ if (!out->opts->dump_opts.use) {
203
+ size = depth * out->indent + 1;
204
+ assure_size(out, size);
205
+ fill_indent(out, depth);
206
+ if (rtype == T_STRING) {
207
+ oj_dump_str(key, 0, out, false);
208
+ } else {
209
+ oj_dump_sym(key, 0, out, false);
210
+ }
211
+ *out->cur++ = ':';
212
+ } else {
213
+ size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
214
+ assure_size(out, size);
215
+ if (0 < out->opts->dump_opts.hash_size) {
216
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
217
+ }
218
+ if (0 < out->opts->dump_opts.indent_size) {
219
+ int i;
220
+ for (i = depth; 0 < i; i--) {
221
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
222
+ }
223
+ }
224
+ if (rtype == T_STRING) {
225
+ oj_dump_str(key, 0, out, false);
226
+ } else {
227
+ oj_dump_sym(key, 0, out, false);
228
+ }
229
+ size = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
230
+ assure_size(out, size);
231
+ if (0 < out->opts->dump_opts.before_size) {
232
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
233
+ }
234
+ *out->cur++ = ':';
235
+ if (0 < out->opts->dump_opts.after_size) {
236
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
237
+ }
238
+ }
239
+ if (NullMode == out->opts->mode) {
240
+ oj_dump_null_val(value, depth, out);
241
+ } else {
242
+ oj_dump_strict_val(value, depth, out);
243
+ }
244
+ out->depth = depth;
245
+ *out->cur++ = ',';
246
+
247
+ return ST_CONTINUE;
248
+ }
249
+
250
+ static void dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
251
+ int cnt;
252
+ size_t size;
253
+
254
+ if (Yes == out->opts->circular) {
255
+ if (0 > oj_check_circular(obj, out)) {
256
+ oj_dump_nil(Qnil, 0, out, false);
257
+ return;
258
+ }
259
+ }
260
+ cnt = (int)RHASH_SIZE(obj);
261
+ size = depth * out->indent + 2;
262
+ assure_size(out, 2);
263
+ *out->cur++ = '{';
264
+ if (0 == cnt) {
265
+ *out->cur++ = '}';
266
+ } else {
267
+ out->depth = depth + 1;
268
+ rb_hash_foreach(obj, hash_cb, (VALUE)out);
269
+ if (',' == *(out->cur - 1)) {
270
+ out->cur--; // backup to overwrite last comma
271
+ }
272
+ if (!out->opts->dump_opts.use) {
273
+ assure_size(out, size);
274
+ fill_indent(out, depth);
275
+ } else {
276
+ size = depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1;
277
+ assure_size(out, size);
278
+ if (0 < out->opts->dump_opts.hash_size) {
279
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
280
+ }
281
+ if (0 < out->opts->dump_opts.indent_size) {
282
+ int i;
283
+
284
+ for (i = depth; 0 < i; i--) {
285
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
286
+ }
287
+ }
288
+ }
289
+ *out->cur++ = '}';
290
+ }
291
+ *out->cur = '\0';
292
+ }
293
+
294
+ static void dump_data_strict(VALUE obj, int depth, Out out, bool as_ok) {
295
+ VALUE clas = rb_obj_class(obj);
296
+
297
+ if (oj_bigdecimal_class == clas) {
298
+ volatile VALUE rstr = oj_safe_string_convert(obj);
299
+
300
+ oj_dump_raw(RSTRING_PTR(rstr), RSTRING_LEN(rstr), out);
301
+ } else {
302
+ raise_strict(obj);
303
+ }
304
+ }
305
+
306
+ static void dump_data_null(VALUE obj, int depth, Out out, bool as_ok) {
307
+ VALUE clas = rb_obj_class(obj);
308
+
309
+ if (oj_bigdecimal_class == clas) {
310
+ volatile VALUE rstr = oj_safe_string_convert(obj);
311
+
312
+ oj_dump_raw(RSTRING_PTR(rstr), RSTRING_LEN(rstr), out);
313
+ } else {
314
+ oj_dump_nil(Qnil, depth, out, false);
315
+ }
316
+ }
317
+
318
+ static DumpFunc strict_funcs[] = {
319
+ NULL, // RUBY_T_NONE = 0x00,
320
+ dump_data_strict, // RUBY_T_OBJECT = 0x01,
321
+ NULL, // RUBY_T_CLASS = 0x02,
322
+ NULL, // RUBY_T_MODULE = 0x03,
323
+ dump_float, // RUBY_T_FLOAT = 0x04,
324
+ oj_dump_str, // RUBY_T_STRING = 0x05,
325
+ NULL, // RUBY_T_REGEXP = 0x06,
326
+ dump_array, // RUBY_T_ARRAY = 0x07,
327
+ dump_hash, // RUBY_T_HASH = 0x08,
328
+ NULL, // RUBY_T_STRUCT = 0x09,
329
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
330
+ NULL, // RUBY_T_FILE = 0x0b,
331
+ dump_data_strict, // RUBY_T_DATA = 0x0c,
332
+ NULL, // RUBY_T_MATCH = 0x0d,
333
+ NULL, // RUBY_T_COMPLEX = 0x0e,
334
+ NULL, // RUBY_T_RATIONAL = 0x0f,
335
+ NULL, // 0x10
336
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
337
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
338
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
339
+ oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
340
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
341
+ };
342
+
343
+ void oj_dump_strict_val(VALUE obj, int depth, Out out) {
344
+ int type = rb_type(obj);
345
+
346
+ TRACE(out->opts->trace, "dump", obj, depth, TraceIn);
347
+ if (MAX_DEPTH < depth) {
348
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
349
+ }
350
+ if (0 < type && type <= RUBY_T_FIXNUM) {
351
+ DumpFunc f = strict_funcs[type];
352
+
353
+ if (NULL != f) {
354
+ f(obj, depth, out, false);
355
+ TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
356
+ return;
357
+ }
358
+ }
359
+ raise_strict(obj);
360
+ }
361
+
362
+ static DumpFunc null_funcs[] = {
363
+ NULL, // RUBY_T_NONE = 0x00,
364
+ dump_data_null, // RUBY_T_OBJECT = 0x01,
365
+ NULL, // RUBY_T_CLASS = 0x02,
366
+ NULL, // RUBY_T_MODULE = 0x03,
367
+ dump_float, // RUBY_T_FLOAT = 0x04,
368
+ oj_dump_str, // RUBY_T_STRING = 0x05,
369
+ NULL, // RUBY_T_REGEXP = 0x06,
370
+ dump_array, // RUBY_T_ARRAY = 0x07,
371
+ dump_hash, // RUBY_T_HASH = 0x08,
372
+ NULL, // RUBY_T_STRUCT = 0x09,
373
+ oj_dump_bignum, // RUBY_T_BIGNUM = 0x0a,
374
+ NULL, // RUBY_T_FILE = 0x0b,
375
+ dump_data_null, // RUBY_T_DATA = 0x0c,
376
+ NULL, // RUBY_T_MATCH = 0x0d,
377
+ NULL, // RUBY_T_COMPLEX = 0x0e,
378
+ NULL, // RUBY_T_RATIONAL = 0x0f,
379
+ NULL, // 0x10
380
+ oj_dump_nil, // RUBY_T_NIL = 0x11,
381
+ oj_dump_true, // RUBY_T_TRUE = 0x12,
382
+ oj_dump_false, // RUBY_T_FALSE = 0x13,
383
+ oj_dump_sym, // RUBY_T_SYMBOL = 0x14,
384
+ oj_dump_fixnum, // RUBY_T_FIXNUM = 0x15,
385
+ };
386
+
387
+ void oj_dump_null_val(VALUE obj, int depth, Out out) {
388
+ int type = rb_type(obj);
389
+
390
+ TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
391
+ if (MAX_DEPTH < depth) {
392
+ rb_raise(rb_eNoMemError, "Too deeply nested.\n");
393
+ }
394
+ if (0 < type && type <= RUBY_T_FIXNUM) {
395
+ DumpFunc f = null_funcs[type];
396
+
397
+ if (NULL != f) {
398
+ f(obj, depth, out, false);
399
+ TRACE(out->opts->trace, "dump", obj, depth, TraceOut);
400
+ return;
401
+ }
402
+ }
403
+ oj_dump_nil(Qnil, depth, out, false);
404
+ TRACE(out->opts->trace, "dump", Qnil, depth, TraceOut);
405
+ }
@@ -0,0 +1,16 @@
1
+ // Copyright (c) 2011 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #ifndef OJ_ENCODE_H
5
+ #define OJ_ENCODE_H
6
+
7
+ #include "oj.h"
8
+ #include "ruby.h"
9
+ #include "ruby/encoding.h"
10
+
11
+ static inline VALUE oj_encode(VALUE rstr) {
12
+ rb_enc_associate(rstr, oj_utf8_encoding);
13
+ return rstr;
14
+ }
15
+
16
+ #endif /* OJ_ENCODE_H */
@@ -0,0 +1,57 @@
1
+ // Copyright (c) 2011 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #include "err.h"
5
+
6
+ #include <stdarg.h>
7
+
8
+ void oj_err_set(Err e, VALUE clas, const char *format, ...) {
9
+ va_list ap;
10
+
11
+ va_start(ap, format);
12
+ e->clas = clas;
13
+ vsnprintf(e->msg, sizeof(e->msg) - 1, format, ap);
14
+ va_end(ap);
15
+ }
16
+
17
+ void oj_err_raise(Err e) {
18
+ rb_raise(e->clas, "%s", e->msg);
19
+ }
20
+
21
+ void _oj_err_set_with_location(Err err,
22
+ VALUE eclas,
23
+ const char *msg,
24
+ const char *json,
25
+ const char *current,
26
+ const char *file,
27
+ int line) {
28
+ int n = 1;
29
+ int col = 1;
30
+
31
+ for (; json < current && '\n' != *current; current--) {
32
+ col++;
33
+ }
34
+ for (; json < current; current--) {
35
+ if ('\n' == *current) {
36
+ n++;
37
+ }
38
+ }
39
+ oj_err_set(err, eclas, "%s at line %d, column %d [%s:%d]", msg, n, col, file, line);
40
+ }
41
+
42
+ void _oj_raise_error(const char *msg, const char *json, const char *current, const char *file, int line) {
43
+ struct _err err;
44
+ int n = 1;
45
+ int col = 1;
46
+
47
+ for (; json < current && '\n' != *current; current--) {
48
+ col++;
49
+ }
50
+ for (; json < current; current--) {
51
+ if ('\n' == *current) {
52
+ n++;
53
+ }
54
+ }
55
+ oj_err_set(&err, oj_parse_error_class, "%s at line %d, column %d [%s:%d]", msg, n, col, file, line);
56
+ rb_raise(err.clas, "%s", err.msg);
57
+ }
@@ -0,0 +1,67 @@
1
+ // Copyright (c) 2011 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #ifndef OJ_ERR_H
5
+ #define OJ_ERR_H
6
+
7
+ #include <errno.h>
8
+
9
+ #include "ruby.h"
10
+
11
+ // Needed to silence 2.4.0 warnings.
12
+ #ifndef NORETURN
13
+ #define NORETURN(x) x
14
+ #endif
15
+
16
+ #define OJ_ERR_START 300
17
+
18
+ typedef enum {
19
+ OJ_OK = 0,
20
+ OJ_ERR_MEMORY = ENOMEM,
21
+ OJ_ERR_PARSE = OJ_ERR_START,
22
+ OJ_ERR_READ,
23
+ OJ_ERR_WRITE,
24
+ OJ_ERR_OVERFLOW,
25
+ OJ_ERR_ARG,
26
+ OJ_ERR_TOO_MANY,
27
+ OJ_ERR_TYPE,
28
+ OJ_ERR_KEY,
29
+ OJ_ABORT,
30
+ OJ_ERR_LAST,
31
+ } ojStatus;
32
+
33
+ #define set_error(err, eclas, msg, json, current) _oj_err_set_with_location(err, eclas, msg, json, current, FILE, LINE)
34
+
35
+ typedef struct _err {
36
+ VALUE clas;
37
+ char msg[128];
38
+ } *Err;
39
+
40
+ extern VALUE oj_parse_error_class;
41
+
42
+ extern void oj_err_set(Err e, VALUE clas, const char *format, ...);
43
+ extern void _oj_err_set_with_location(Err err,
44
+ VALUE eclas,
45
+ const char *msg,
46
+ const char *json,
47
+ const char *current,
48
+ const char *file,
49
+ int line);
50
+
51
+ NORETURN(extern void oj_err_raise(Err e));
52
+
53
+ #define raise_error(msg, json, current) _oj_raise_error(msg, json, current, __FILE__, __LINE__)
54
+
55
+ NORETURN(
56
+ extern void _oj_raise_error(const char *msg, const char *json, const char *current, const char *file, int line));
57
+
58
+ inline static void err_init(Err e) {
59
+ e->clas = Qnil;
60
+ *e->msg = '\0';
61
+ }
62
+
63
+ inline static int err_has(Err e) {
64
+ return (Qnil != e->clas);
65
+ }
66
+
67
+ #endif /* OJ_ERR_H */
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkmf'
4
+ require 'rbconfig'
5
+
6
+ extension_name = 'oj_windows'
7
+ dir_config(extension_name)
8
+
9
+ parts = RUBY_DESCRIPTION.split(' ')
10
+ type = parts[0]
11
+ type = type[4..] if type.start_with?('tcs-')
12
+ is_windows = RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/
13
+ platform = RUBY_PLATFORM
14
+ version = RUBY_VERSION.split('.')
15
+ puts ">>>>> Creating Makefile for #{type} version #{RUBY_VERSION} on #{platform} <<<<<"
16
+
17
+ dflags = {
18
+ 'RUBY_TYPE' => type,
19
+ (type.upcase + '_RUBY') => nil,
20
+ 'RUBY_VERSION' => RUBY_VERSION,
21
+ 'RUBY_VERSION_MAJOR' => version[0],
22
+ 'RUBY_VERSION_MINOR' => version[1],
23
+ 'RUBY_VERSION_MICRO' => version[2],
24
+ 'IS_WINDOWS' => is_windows ? 1 : 0,
25
+ 'RSTRUCT_LEN_RETURNS_INTEGER_OBJECT' => ('ruby' == type && '2' == version[0] && '4' == version[1] && '1' >= version[2]) ? 1 : 0,
26
+ }
27
+
28
+ # Support for compaction.
29
+ have_func('rb_gc_mark_movable')
30
+ have_func('stpcpy')
31
+ unless is_windows
32
+ have_func('pthread_mutex_init')
33
+ have_func('getrlimit', 'sys/resource.h')
34
+ end
35
+ have_func('rb_enc_interned_str')
36
+ have_func('rb_ext_ractor_safe', 'ruby.h')
37
+
38
+ dflags['OJ_DEBUG'] = true unless ENV['OJ_DEBUG'].nil?
39
+
40
+ unless is_windows
41
+ # Enable SIMD optimizations - try SSE4.2 on x86_64 for best performance
42
+ # Falls back to SSE2 or compiler defaults if not available
43
+ if try_cflags('-msse4.2')
44
+ $CPPFLAGS += ' -msse4.2'
45
+ elsif try_cflags('-msse2')
46
+ $CPPFLAGS += ' -msse2'
47
+ end
48
+ $CPPFLAGS += ' -Wall'
49
+ end
50
+
51
+ if enable_config('trace-log', false)
52
+ dflags['OJ_ENABLE_TRACE_LOG'] = 1
53
+ end
54
+
55
+ dflags.each do |k, v|
56
+ if v.nil?
57
+ $CPPFLAGS += " -D#{k}"
58
+ else
59
+ $CPPFLAGS += " -D#{k}=#{v}"
60
+ end
61
+ end
62
+
63
+ # Adding the __attribute__ flag only works with gcc compilers and even then it
64
+ # does not work to check args with varargs so just remove the check.
65
+ if CONFIG['warnflags'].is_a?(String)
66
+ CONFIG['warnflags'].slice!(/ -Wsuggest-attribute=format/)
67
+ CONFIG['warnflags'].slice!(/ -Wdeclaration-after-statement/)
68
+ CONFIG['warnflags'].slice!(/ -Wmissing-noreturn/)
69
+ end
70
+
71
+ create_makefile(File.join(extension_name, extension_name))
72
+
73
+ if is_windows
74
+ %x{nmake clean}
75
+ else
76
+ %x{make clean}
77
+ end