improve decimal support
[awesomized/ext-ion] / ion.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: ion |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2021, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include "php.h"
18 #include "ext/standard/info.h"
19
20 #include "Zend/zend_enum.h"
21 #include "Zend/zend_exceptions.h"
22 #include "Zend/zend_closures.h"
23 #include "Zend/zend_interfaces.h"
24 #include "Zend/zend_smart_str.h"
25
26 #include "ext/date/php_date.h"
27 #include "ext/spl/spl_exceptions.h"
28 #include "ext/spl/spl_iterators.h"
29
30 #define DECNUMDIGITS 34 /* DECQUAD_Pmax */
31 #include "ionc/ion.h"
32
33 #include "php_ion.h"
34 #define ZEND_ARG_VARIADIC_OBJ_TYPE_MASK(pass_by_ref, name, classname, type_mask, default_value) \
35 { #name, ZEND_TYPE_INIT_CLASS_CONST_MASK(#classname, type_mask | _ZEND_ARG_INFO_FLAGS(pass_by_ref, 1, 0)), default_value },
36 #include "ion_arginfo.h"
37 #include "ion_private.h"
38
39 ZEND_METHOD(ion_Symbol_ImportLocation, __construct)
40 {
41 zend_long location;
42 php_ion_symbol_iloc *obj = php_ion_obj(symbol_iloc, Z_OBJ_P(ZEND_THIS));
43
44 PTR_CHECK(obj);
45
46 ZEND_PARSE_PARAMETERS_START(2, 2)
47 Z_PARAM_STR(obj->name)
48 Z_PARAM_LONG(location)
49 ZEND_PARSE_PARAMETERS_END();
50
51 obj->loc.location = location;
52 php_ion_symbol_iloc_ctor(obj);
53 }
54 ZEND_METHOD(ion_Symbol, __construct)
55 {
56 zend_long sid = -1;
57 php_ion_symbol *obj = php_ion_obj(symbol, Z_OBJ_P(ZEND_THIS));
58
59 PTR_CHECK(obj);
60
61 ZEND_PARSE_PARAMETERS_START(1, 3)
62 Z_PARAM_STR(obj->value)
63 Z_PARAM_OPTIONAL
64 Z_PARAM_LONG(sid)
65 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->iloc, ce_Symbol_ImportLocation)
66 ZEND_PARSE_PARAMETERS_END();
67
68 obj->sym.sid = sid;
69 php_ion_symbol_ctor(obj);
70 }
71 ZEND_METHOD(ion_Symbol, equals)
72 {
73 zend_object *other_obj;
74 php_ion_symbol *sym = php_ion_obj(symbol, Z_OBJ_P(ZEND_THIS));
75
76 OBJ_CHECK(sym);
77
78 ZEND_PARSE_PARAMETERS_START(1, 1)
79 Z_PARAM_OBJ_OF_CLASS(other_obj, ce_Symbol)
80 ZEND_PARSE_PARAMETERS_END();
81
82 BOOL eq = FALSE;
83 iERR err = ion_symbol_is_equal(
84 &sym->sym,
85 &php_ion_obj(symbol, other_obj)->sym, &eq);
86 ION_CHECK(err);
87 RETVAL_BOOL(eq);
88 }
89 ZEND_METHOD(ion_Timestamp, __construct)
90 {
91 zend_long precision;
92 zend_string *fmt = NULL, *dt = NULL;
93 zval *tz = NULL;
94 php_ion_timestamp *obj = php_ion_obj(timestamp, Z_OBJ_P(ZEND_THIS));
95 PTR_CHECK(obj);
96
97 ZEND_PARSE_PARAMETERS_START(1, 4)
98 Z_PARAM_LONG(precision)
99 Z_PARAM_OPTIONAL
100 Z_PARAM_STR_OR_NULL(fmt)
101 Z_PARAM_STR(dt)
102 Z_PARAM_ZVAL(tz)
103 ZEND_PARSE_PARAMETERS_END();
104
105 php_ion_timestamp_ctor(obj, precision, fmt, dt, tz);
106 }
107 ZEND_METHOD(ion_Timestamp, __toString)
108 {
109 php_ion_timestamp *obj = php_ion_obj(timestamp, Z_OBJ_P(ZEND_THIS));
110 OBJ_CHECK(obj);
111
112 ZEND_PARSE_PARAMETERS_NONE();
113
114 zval fmt;
115 ZVAL_NULL(&fmt);
116 zend_call_method_with_1_params(&obj->std, obj->std.ce, NULL, "format", return_value,
117 zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("format"), 0, &fmt));
118 }
119 ZEND_METHOD(ion_Decimal_Context, __construct)
120 {
121 php_ion_decimal_ctx *obj = php_ion_obj(decimal_ctx, Z_OBJ_P(ZEND_THIS));
122 PTR_CHECK(obj);
123
124 zend_long bits = 128;
125 ZEND_PARSE_PARAMETERS_START(0, 1)
126 Z_PARAM_OPTIONAL
127 Z_PARAM_LONG(bits)
128 ZEND_PARSE_PARAMETERS_END();
129
130 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("bits"), bits);
131 php_ion_decimal_ctx_ctor(obj);
132 }
133 ZEND_METHOD(ion_Decimal, __construct)
134 {
135 php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS));
136 PTR_CHECK(obj);
137
138 zend_long num;
139 zend_string *zstr;
140 ZEND_PARSE_PARAMETERS_START(1, 2)
141 Z_PARAM_STR_OR_LONG(zstr, num)
142 Z_PARAM_OPTIONAL
143 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->ctx, ce_Decimal_Context)
144 ZEND_PARSE_PARAMETERS_END();
145
146 if (obj->ctx) {
147 GC_ADDREF(obj->ctx);
148 } else {
149 zval zdc;
150 object_init_ex(&zdc, ce_Decimal_Context);
151 obj->ctx = Z_OBJ(zdc);
152 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx, obj->ctx));
153 }
154
155 if (zstr) {
156 ION_CHECK(ion_decimal_from_string(&obj->dec, zstr->val,
157 obj->ctx ? &php_ion_obj(decimal_ctx, obj->ctx)->ctx : NULL));
158 } else if (num <= INT32_MAX && num >= INT32_MIN) {
159 ION_CHECK(ion_decimal_from_int32(&obj->dec, num));
160 } else if (num > 0 && num <= UINT32_MAX) {
161 ION_CHECK(ion_decimal_from_uint32(&obj->dec, num));
162 } else {
163 zend_throw_exception_ex(spl_ce_OverflowException, 0,
164 "Integer value out of bounds: " ZEND_LONG_FMT " (INT32_MIN < n < UINT32_MAX)", num);
165 return;
166 }
167
168 php_ion_decimal_ctor(obj);
169 OBJ_RELEASE(obj->ctx);
170 }
171 ZEND_METHOD(ion_Decimal, equals)
172 {
173 php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS));
174 PTR_CHECK(obj);
175
176 zend_object *dec_obj;
177 ZEND_PARSE_PARAMETERS_START(1, 1)
178 Z_PARAM_OBJ_OF_CLASS(dec_obj, ce_Decimal)
179 ZEND_PARSE_PARAMETERS_END();
180
181 BOOL is = FALSE;
182 ION_CHECK(ion_decimal_equals(&obj->dec, &php_ion_obj(decimal, dec_obj)->dec,
183 obj->ctx ? &php_ion_obj(decimal_ctx, obj->ctx)->ctx : NULL, &is));
184 RETURN_BOOL(is);
185 }
186 ZEND_METHOD(ion_Decimal, __toString)
187 {
188 php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS));
189 PTR_CHECK(obj);
190
191 ZEND_PARSE_PARAMETERS_NONE();
192
193 RETURN_STR(php_ion_decimal_to_string(&obj->dec));
194 }
195 ZEND_METHOD(ion_Decimal, toInt)
196 {
197 php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS));
198 PTR_CHECK(obj);
199
200 ZEND_PARSE_PARAMETERS_NONE();
201
202 zend_long l;
203 php_ion_decimal_to_int(&obj->dec, &php_ion_obj(decimal_ctx, obj->ctx)->ctx, &l);
204 RETURN_LONG(l);
205 }
206 ZEND_METHOD(ion_Decimal, isInt)
207 {
208 php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS));
209 PTR_CHECK(obj);
210
211 ZEND_PARSE_PARAMETERS_NONE();
212
213 RETURN_BOOL(ion_decimal_is_integer(&obj->dec));
214 }
215 ZEND_METHOD(ion_Reader_Options, __construct)
216 {
217 php_ion_reader_options *opt = php_ion_obj(reader_options, Z_OBJ_P(ZEND_THIS));
218 zend_bool ret_sys_val = false, skip_validation = false;
219 zend_long ch_nl = 0xa, max_depth = 10, max_ann, max_ann_buf = 512,
220 sym_thr = 0x1000, uval_thr = 0x1000, chunk_thr = 0x1000, alloc_pgsz = 0x1000;
221
222 PTR_CHECK(opt);
223
224 ZEND_PARSE_PARAMETERS_START(0, 13)
225 Z_PARAM_OPTIONAL
226 // public readonly ?\ion\Catalog $catalog = null,
227 Z_PARAM_OBJ_OF_CLASS_OR_NULL(opt->cat, ce_Catalog)
228 // public readonly ?\ion\Decimal\Context $decimalContext = null,
229 Z_PARAM_OBJ_OF_CLASS_OR_NULL(opt->dec_ctx, ce_Decimal_Context)
230 // public readonly ?\Closure $onContextChange = null,
231 Z_PARAM_OBJ_OF_CLASS_OR_NULL(opt->cb, zend_ce_closure);
232 // public readonly bool $returnSystemValues = false,
233 Z_PARAM_BOOL(ret_sys_val)
234 // public readonly int $newLine = 0xa,
235 Z_PARAM_LONG(ch_nl)
236 // public readonly int $maxContainerDepth = 10,
237 Z_PARAM_LONG(max_depth)
238 // public readonly int $maxAnnotations = 10,
239 Z_PARAM_LONG(max_ann)
240 // public readonly int $maxAnnotationBuffered = 512,
241 Z_PARAM_LONG(max_ann_buf)
242 // public readonly int $symbolThreshold = 4096,
243 Z_PARAM_LONG(sym_thr)
244 // public readonly int $userValueThreshold = 4096,
245 Z_PARAM_LONG(uval_thr)
246 // public readonly int $chunkThreshold = 4096,
247 Z_PARAM_LONG(chunk_thr)
248 // public readonly int $allocationPageSize = 4096,
249 Z_PARAM_LONG(alloc_pgsz)
250 // public readonly bool $skipCharacterValidation = false,
251 Z_PARAM_BOOL(skip_validation)
252 ZEND_PARSE_PARAMETERS_END();
253
254 opt->opt.context_change_notifier = EMPTY_READER_CHANGE_NOTIFIER;
255 if (opt->cb) {
256 update_property_obj(&opt->std, ZEND_STRL("onContextChange"), opt->cb);
257 }
258 if (opt->cat) {
259 update_property_obj(&opt->std, ZEND_STRL("catalog"), opt->cat);
260 opt->opt.pcatalog = php_ion_obj(catalog, opt->cat)->cat;
261 }
262 if (opt->dec_ctx) {
263 update_property_obj(&opt->std, ZEND_STRL("decimalContext"), opt->dec_ctx);
264 opt->opt.decimal_context = &php_ion_obj(decimal_ctx, opt->dec_ctx)->ctx;
265 }
266 zend_update_property_bool(opt->std.ce, &opt->std, ZEND_STRL("returnSystemValues"),
267 opt->opt.return_system_values = ret_sys_val);
268 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("newLine"),
269 opt->opt.new_line_char = ch_nl);
270 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("maxContainerDepth"),
271 opt->opt.max_container_depth = max_depth);
272 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("maxAnnotationCount"),
273 opt->opt.max_annotation_count = max_ann);
274 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("maxAnnotationBuffered"),
275 opt->opt.max_annotation_buffered = max_ann_buf);
276 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("symbolThreshold"),
277 opt->opt.symbol_threshold = sym_thr);
278 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("userValueThreshold"),
279 opt->opt.user_value_threshold = uval_thr);
280 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("chunkThreshold"),
281 opt->opt.chunk_threshold = chunk_thr);
282 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("allocationPageSize"),
283 opt->opt.allocation_page_size = alloc_pgsz);
284 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("skipCharacterValidation"),
285 opt->opt.skip_character_validation = skip_validation);
286 }
287 ZEND_METHOD(ion_Reader_Reader, hasChildren)
288 {
289 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
290
291 OBJ_CHECK(obj);
292 ZEND_PARSE_PARAMETERS_NONE();
293
294 ION_TYPE t;
295 ION_CHECK(ion_reader_get_type(obj->reader, &t));
296 switch (ION_TYPE_INT(t)) {
297 case tid_LIST_INT:
298 case tid_SEXP_INT:
299 case tid_STRUCT_INT:
300 RETVAL_TRUE;
301 break;
302 default:
303 RETVAL_FALSE;
304 break;
305 }
306 }
307 ZEND_METHOD(ion_Reader_Reader, getChildren)
308 {
309 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
310 OBJ_CHECK(obj);
311
312 ZEND_PARSE_PARAMETERS_NONE();
313
314 ION_CHECK(ion_reader_step_in(obj->reader));
315
316 RETURN_ZVAL(ZEND_THIS, 1, 0);
317 }
318 ZEND_METHOD(ion_Reader_Reader, rewind)
319 {
320 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
321 OBJ_CHECK(obj);
322
323 ZEND_PARSE_PARAMETERS_NONE();
324
325 ION_CHECK(ion_reader_next(obj->reader, &obj->state));
326 }
327 ZEND_METHOD(ion_Reader_Reader, next)
328 {
329 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
330 OBJ_CHECK(obj);
331
332 ZEND_PARSE_PARAMETERS_NONE();
333
334 if (obj->state == tid_EOF) {
335 SIZE depth = 0;
336 ION_CHECK(ion_reader_get_depth(obj->reader, &depth));
337 if (depth) {
338 ION_CHECK(ion_reader_step_out(obj->reader));
339 }
340 }
341 ION_CHECK(ion_reader_next(obj->reader, &obj->state));
342 }
343 ZEND_METHOD(ion_Reader_Reader, valid)
344 {
345 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
346 OBJ_CHECK(obj);
347
348 ZEND_PARSE_PARAMETERS_NONE();
349 RETURN_BOOL(obj->state != tid_none && obj->state != tid_EOF);
350 }
351 ZEND_METHOD(ion_Reader_Reader, key)
352 {
353 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
354 OBJ_CHECK(obj);
355
356 ZEND_PARSE_PARAMETERS_NONE();
357 RETURN_IONTYPE(obj->state);
358 }
359 ZEND_METHOD(ion_Reader_Reader, current)
360 {
361 ZEND_PARSE_PARAMETERS_NONE();
362 RETURN_ZVAL(ZEND_THIS, 1, 0);
363 }
364 ZEND_METHOD(ion_Reader_Reader, getType)
365 {
366 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
367 OBJ_CHECK(obj);
368
369 ZEND_PARSE_PARAMETERS_NONE();
370
371 ION_TYPE typ;
372 ION_CHECK(ion_reader_get_type(obj->reader, &typ));
373 RETURN_IONTYPE(typ);
374 }
375 ZEND_METHOD(ion_Reader_Reader, hasAnnotations)
376 {
377 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
378 OBJ_CHECK(obj);
379
380 ZEND_PARSE_PARAMETERS_NONE();
381
382 BOOL has = FALSE;
383 ION_CHECK(ion_reader_has_any_annotations(obj->reader, &has));
384 RETURN_BOOL(has);
385 }
386 ZEND_METHOD(ion_Reader_Reader, hasAnnotation)
387 {
388 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
389 OBJ_CHECK(obj);
390
391 zend_string *ann_zstr;
392 ZEND_PARSE_PARAMETERS_START(1, 1);
393 Z_PARAM_STR(ann_zstr);
394 ZEND_PARSE_PARAMETERS_END();
395
396 ION_STRING ann_istr;
397 BOOL has = FALSE;
398 ION_CHECK(ion_reader_has_annotation(obj->reader, ion_string_from_zend(&ann_istr, ann_zstr), &has));
399 RETURN_BOOL(has);
400 }
401 ZEND_METHOD(ion_Reader_Reader, isNull)
402 {
403 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
404 OBJ_CHECK(obj);
405
406 ZEND_PARSE_PARAMETERS_NONE();
407
408 BOOL is = FALSE;
409 ION_CHECK(ion_reader_is_null(obj->reader, &is));
410 RETURN_BOOL(is);
411 }
412 ZEND_METHOD(ion_Reader_Reader, isInStruct)
413 {
414 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
415 OBJ_CHECK(obj);
416
417 ZEND_PARSE_PARAMETERS_NONE();
418
419 BOOL is = FALSE;
420 ION_CHECK(ion_reader_is_in_struct(obj->reader, &is));
421 RETURN_BOOL(is);
422 }
423 ZEND_METHOD(ion_Reader_Reader, getFieldName)
424 {
425 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
426 OBJ_CHECK(obj);
427
428 ZEND_PARSE_PARAMETERS_NONE();
429
430 ION_STRING name;
431 ION_CHECK(ion_reader_get_field_name(obj->reader, &name));
432 RETURN_STRINGL((char *) name.value, name.length);
433 }
434 ZEND_METHOD(ion_Reader_Reader, getFieldNameSymbol)
435 {
436 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
437 OBJ_CHECK(obj);
438
439 ZEND_PARSE_PARAMETERS_NONE();
440
441 ION_SYMBOL *sym_ptr;
442 ION_CHECK(ion_reader_get_field_name_symbol(obj->reader, &sym_ptr));
443
444 php_ion_symbol_zval(sym_ptr, return_value);
445 }
446 ZEND_METHOD(ion_Reader_Reader, getAnnotations)
447 {
448 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
449 OBJ_CHECK(obj);
450
451 ZEND_PARSE_PARAMETERS_NONE();
452
453 int32_t count, max;
454 if (obj->opt) {
455 max = php_ion_obj(reader_options, obj->opt)->opt.max_annotation_count;
456 } else {
457 max = 10;
458 }
459 ION_STRING *ptr = ecalloc(sizeof(*ptr), max);
460 iERR err = ion_reader_get_annotations(obj->reader, ptr, max, &count);
461 if (!err) {
462 array_init_size(return_value, count);
463 for (int32_t i = 0; i < count; ++i) {
464 add_next_index_str(return_value, zend_string_from_ion(&ptr[i]));
465 }
466 }
467 efree(ptr);
468 ION_CHECK(err);
469 }
470 ZEND_METHOD(ion_Reader_Reader, getAnnotationSymbols)
471 {
472 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
473 OBJ_CHECK(obj);
474
475 ZEND_PARSE_PARAMETERS_NONE();
476
477 int32_t count, max = php_ion_obj(reader_options, obj->opt)->opt.max_annotation_count;
478 ION_SYMBOL *ptr = ecalloc(sizeof(*ptr), max);
479 iERR err = ion_reader_get_annotation_symbols(obj->reader, ptr, max, &count);
480 if (!err) {
481 array_init_size(return_value, count);
482 for (int32_t i = 0; i < count; ++i) {
483 zval zsym;
484 php_ion_symbol_zval(&ptr[i], &zsym);
485 add_next_index_zval(return_value, &zsym);
486 }
487 }
488 efree(ptr);
489 ION_CHECK(err);
490 }
491 ZEND_METHOD(ion_Reader_Reader, countAnnotations)
492 {
493 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
494 OBJ_CHECK(obj);
495
496 ZEND_PARSE_PARAMETERS_NONE();
497
498 SIZE sz = 0;
499 ION_CHECK(ion_reader_get_annotation_count(obj->reader, &sz));
500 RETURN_LONG(sz);
501 }
502 ZEND_METHOD(ion_Reader_Reader, getAnnotation)
503 {
504 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
505 OBJ_CHECK(obj);
506
507 zend_long idx;
508 ZEND_PARSE_PARAMETERS_START(1, 1)
509 Z_PARAM_LONG(idx);
510 ZEND_PARSE_PARAMETERS_END();
511
512 ION_STRING ann;
513 ION_CHECK(ion_reader_get_an_annotation(obj->reader, idx, &ann));
514 RETURN_STRINGL((char *) ann.value, ann.length);
515 }
516 ZEND_METHOD(ion_Reader_Reader, getAnnotationSymbol)
517 {
518 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
519 OBJ_CHECK(obj);
520
521 zend_long idx;
522 ZEND_PARSE_PARAMETERS_START(1, 1)
523 Z_PARAM_LONG(idx);
524 ZEND_PARSE_PARAMETERS_END();
525
526 ION_SYMBOL sym;
527 ION_CHECK(ion_reader_get_an_annotation_symbol(obj->reader, idx, &sym));
528 php_ion_symbol_zval(&sym, return_value);
529 }
530 ZEND_METHOD(ion_Reader_Reader, readNull)
531 {
532 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
533 OBJ_CHECK(obj);
534
535 ZEND_PARSE_PARAMETERS_NONE();
536
537 ION_TYPE typ;
538 ION_CHECK(ion_reader_read_null(obj->reader, &typ));
539 RETURN_OBJ_COPY(php_ion_type_fetch(typ));
540 }
541 ZEND_METHOD(ion_Reader_Reader, readBool)
542 {
543 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
544 OBJ_CHECK(obj);
545
546 ZEND_PARSE_PARAMETERS_NONE();
547
548 BOOL b;
549 ION_CHECK(ion_reader_read_bool(obj->reader, &b));
550 RETURN_BOOL(b);
551 }
552 ZEND_METHOD(ion_Reader_Reader, readInt)
553 {
554 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
555 OBJ_CHECK(obj);
556
557 ZEND_PARSE_PARAMETERS_NONE();
558
559 php_ion_unserialize_int(obj, return_value);
560 }
561 ZEND_METHOD(ion_Reader_Reader, readFloat)
562 {
563 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
564 OBJ_CHECK(obj);
565
566 ZEND_PARSE_PARAMETERS_NONE();
567
568 ION_CHECK(ion_reader_read_double(obj->reader, &Z_DVAL_P(return_value)));
569 }
570 ZEND_METHOD(ion_Reader_Reader, readDecimal)
571 {
572 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
573 OBJ_CHECK(obj);
574
575 ZEND_PARSE_PARAMETERS_NONE();
576
577 object_init_ex(return_value, ce_Decimal);
578 php_ion_decimal *dec = php_ion_obj(decimal, Z_OBJ_P(return_value));
579 ION_CHECK(ion_reader_read_ion_decimal(obj->reader, &dec->dec));
580 php_ion_decimal_ctor(dec);
581 }
582 ZEND_METHOD(ion_Reader_Reader, readTimestamp)
583 {
584 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
585 OBJ_CHECK(obj);
586
587 ZEND_PARSE_PARAMETERS_NONE();
588
589 php_ion_unserialize_timestamp(obj, return_value);
590 }
591 ZEND_METHOD(ion_Reader_Reader, readSymbol)
592 {
593 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
594 OBJ_CHECK(obj);
595
596 ZEND_PARSE_PARAMETERS_NONE();
597
598 ION_SYMBOL sym;
599 ION_CHECK(ion_reader_read_ion_symbol(obj->reader, &sym));
600 php_ion_symbol_zval(&sym, return_value);
601 }
602 ZEND_METHOD(ion_Reader_Reader, readString)
603 {
604 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
605 OBJ_CHECK(obj);
606
607 ZEND_PARSE_PARAMETERS_NONE();
608
609 ION_STRING str;
610 ION_CHECK(ion_reader_read_string(obj->reader, &str));
611 RETURN_STRINGL((char *) str.value, str.length);
612 }
613
614 typedef iERR (*read_part_fn)(ION_READER *, BYTE *, SIZE, SIZE *);
615 static void read_part(INTERNAL_FUNCTION_PARAMETERS, read_part_fn fn)
616 {
617 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
618 OBJ_CHECK(obj);
619
620 zval *ref;
621 zend_string *zstr;
622 zend_long len = 0x1000;
623 ZEND_PARSE_PARAMETERS_START(1, 1)
624 Z_PARAM_ZVAL(ref)
625 Z_PARAM_OPTIONAL
626 Z_PARAM_LONG(len)
627 ZEND_PARSE_PARAMETERS_END();
628
629 ZVAL_DEREF(ref);
630
631 if (Z_TYPE_P(ref) == IS_STRING && Z_STRLEN_P(ref) == len) {
632 /* reuse */
633 zstr = Z_STR_P(ref);
634 } else {
635 zval_dtor(ref);
636 zstr = zend_string_alloc(len, 0);
637 }
638
639 SIZE read = 0;
640 ION_CHECK(fn(obj->reader, (BYTE *) zstr->val, zstr->len, &read), goto fail);
641 if (read > 0) {
642 if (read < zstr->len) {
643 zstr = zend_string_truncate(zstr, read, 0);
644 }
645 ZVAL_STR(ref, zstr);
646 RETURN_TRUE;
647 }
648 fail:
649 if (zstr != Z_STR_P(ref)) {
650 zend_string_release(zstr);
651 }
652 ZVAL_EMPTY_STRING(ref);
653 RETURN_FALSE;
654 }
655 ZEND_METHOD(ion_Reader_Reader, readStringPart)
656 {
657 read_part(INTERNAL_FUNCTION_PARAM_PASSTHRU, ion_reader_read_partial_string);
658 }
659 ZEND_METHOD(ion_Reader_Reader, readLob)
660 {
661 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
662 OBJ_CHECK(obj);
663
664 ZEND_PARSE_PARAMETERS_NONE();
665
666 php_ion_unserialize_lob(obj, return_value);
667 }
668 ZEND_METHOD(ion_Reader_Reader, readLobPart)
669 {
670 read_part(INTERNAL_FUNCTION_PARAM_PASSTHRU, ion_reader_read_lob_partial_bytes);
671 }
672 ZEND_METHOD(ion_Reader_Reader, getPosition)
673 {
674 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
675 OBJ_CHECK(obj);
676
677 ZEND_PARSE_PARAMETERS_NONE();
678
679 int64_t bytes = 0;
680 int32_t dummy;
681 ION_CHECK(ion_reader_get_position(obj->reader, &bytes, &dummy, &dummy));
682 RETURN_LONG(bytes);
683 }
684 ZEND_METHOD(ion_Reader_Reader, getDepth)
685 {
686 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
687 OBJ_CHECK(obj);
688
689 ZEND_PARSE_PARAMETERS_NONE();
690
691 SIZE depth = 0;
692 ION_CHECK(ion_reader_get_depth(obj->reader, &depth));
693 RETURN_LONG(depth);
694 }
695 ZEND_METHOD(ion_Reader_Reader, seek)
696 {
697 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
698 OBJ_CHECK(obj);
699
700 zend_long off, len = -1;
701 ZEND_PARSE_PARAMETERS_START(1, 2)
702 Z_PARAM_LONG(off)
703 Z_PARAM_OPTIONAL
704 Z_PARAM_LONG(len)
705 ZEND_PARSE_PARAMETERS_END();
706
707 ION_CHECK(ion_reader_seek(obj->reader, off, len));
708 }
709 ZEND_METHOD(ion_Reader_Reader, getValueOffset)
710 {
711 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
712 OBJ_CHECK(obj);
713
714 ZEND_PARSE_PARAMETERS_NONE();
715
716 POSITION off = 0;
717 ION_CHECK(ion_reader_get_value_offset(obj->reader, &off));
718 RETURN_LONG(off);
719 }
720 ZEND_METHOD(ion_Reader_Reader, getValueLength)
721 {
722 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
723 OBJ_CHECK(obj);
724
725 ZEND_PARSE_PARAMETERS_NONE();
726
727 SIZE len = 0;
728 ION_CHECK(ion_reader_get_value_length(obj->reader, &len));
729 RETURN_LONG(len);
730 }
731 ZEND_METHOD(ion_Reader_Buffer_Reader, __construct)
732 {
733 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
734 PTR_CHECK(obj);
735
736 zend_string *zstr;
737 ZEND_PARSE_PARAMETERS_START(1, 2)
738 Z_PARAM_STR(zstr)
739 Z_PARAM_OPTIONAL
740 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Reader_Options)
741 ZEND_PARSE_PARAMETERS_END();
742
743 obj->type = BUFFER_READER;
744 obj->buffer = zend_string_copy(zstr);
745
746 php_ion_reader_ctor(obj);
747 }
748 ZEND_METHOD(ion_Reader_Buffer_Reader, getBuffer)
749 {
750 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
751 OBJ_CHECK(obj);
752
753 ZEND_PARSE_PARAMETERS_NONE();
754 RETURN_STR_COPY(obj->buffer);
755 }
756
757 ZEND_METHOD(ion_Reader_Stream_Reader, __construct)
758 {
759 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
760 PTR_CHECK(obj);
761
762 zval *zstream;
763 ZEND_PARSE_PARAMETERS_START(1, 2)
764 Z_PARAM_RESOURCE(zstream)
765 Z_PARAM_OPTIONAL
766 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Reader_Options)
767 ZEND_PARSE_PARAMETERS_END();
768
769 obj->type = STREAM_READER;
770 php_stream_from_zval_no_verify(obj->stream.ptr, zstream);
771
772 php_ion_reader_ctor(obj);
773 }
774 ZEND_METHOD(ion_Reader_Stream_Reader, getStream)
775 {
776 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
777 OBJ_CHECK(obj);
778
779 ZEND_PARSE_PARAMETERS_NONE();
780 PTR_CHECK(obj->stream.ptr);
781
782 GC_ADDREF(obj->stream.ptr->res);
783 RETURN_RES(obj->stream.ptr->res);
784 }
785 ZEND_METHOD(ion_Reader_Stream_Reader, resetStream)
786 {
787 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
788 OBJ_CHECK(obj);
789
790 zval *zstream;
791 ZEND_PARSE_PARAMETERS_START(1, 1);
792 Z_PARAM_RESOURCE(zstream);
793 ZEND_PARSE_PARAMETERS_END();
794
795 ION_CHECK(ion_reader_reset_stream(&obj->reader, obj, php_ion_reader_stream_handler));
796
797 if (obj->stream.ptr) {
798 zend_list_delete(obj->stream.ptr->res);
799 }
800 php_stream_from_zval_no_verify(obj->stream.ptr, zstream);
801 PTR_CHECK(obj->stream.ptr);
802 Z_ADDREF_P(zstream);
803 }
804 ZEND_METHOD(ion_Reader_Stream_Reader, resetStreamWithLength)
805 {
806 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
807 OBJ_CHECK(obj);
808
809 zval *zstream;
810 zend_long length;
811 ZEND_PARSE_PARAMETERS_START(1, 2);
812 Z_PARAM_RESOURCE(zstream);
813 Z_PARAM_LONG(length)
814 ZEND_PARSE_PARAMETERS_END();
815
816 ION_CHECK(ion_reader_reset_stream_with_length(&obj->reader, obj, php_ion_reader_stream_handler, length));
817
818 if (obj->stream.ptr) {
819 zend_list_delete(obj->stream.ptr->res);
820 }
821 php_stream_from_zval_no_verify(obj->stream.ptr, zstream);
822 PTR_CHECK(obj->stream.ptr);
823 Z_ADDREF_P(zstream);
824 }
825 ZEND_METHOD(ion_Writer_Options, __construct)
826 {
827 php_ion_writer_options *obj = php_ion_obj(writer_options, Z_OBJ_P(ZEND_THIS));
828 PTR_CHECK(obj);
829
830 zend_bool binary = false, compact_floats = false, escape = false, pretty = false,
831 tabs = true, small_cntr_inl = true, suppress_sys = false, flush = false;
832 zend_long indent = 2, max_depth = 10, max_ann = 10, temp = 0x400, alloc = 0x1000;
833 ZEND_PARSE_PARAMETERS_START(0, 16)
834 Z_PARAM_OPTIONAL
835 //public readonly ?\ion\Catalog $catalog = null,
836 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->cat, ce_Catalog)
837 //public readonly ?\ion\Collection $encodingSymbolTable = null,
838 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->col, ce_Collection)
839 //public readonly ?\ion\Decimal\Context $decimalContext = null,
840 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->dec_ctx, ce_Decimal_Context)
841 //public readonly bool $outputBinary = false,
842 Z_PARAM_BOOL(binary)
843 //public readonly bool $compactFloats = false,
844 Z_PARAM_BOOL(compact_floats)
845 //public readonly bool $escapeNonAscii = false,
846 Z_PARAM_BOOL(escape)
847 //public readonly bool $prettyPrint = false,
848 Z_PARAM_BOOL(pretty)
849 //public readonly bool $indentTabs = true,
850 Z_PARAM_BOOL(tabs)
851 //public readonly int $indentSize = 2,
852 Z_PARAM_LONG(indent)
853 //public readonly bool $smallContainersInline = true,
854 Z_PARAM_BOOL(small_cntr_inl)
855 //public readonly bool $suppressSystemValues = false,
856 Z_PARAM_BOOL(suppress_sys)
857 //public readonly bool $flushEveryValue = false,
858 Z_PARAM_BOOL(flush)
859 //public readonly int $maxContainerDepth = 10,
860 Z_PARAM_LONG(max_depth)
861 //public readonly int $maxAnnotations = 10,
862 Z_PARAM_LONG(max_ann)
863 //public readonly int $tempBufferSize = 0x400,
864 Z_PARAM_LONG(temp)
865 //public readonly int $allocationPageSize = 0x1000,
866 Z_PARAM_LONG(alloc)
867 ZEND_PARSE_PARAMETERS_END();
868
869 if (obj->cat) {
870 update_property_obj(&obj->std, ZEND_STRL("catalog"), obj->cat);
871 obj->opt.pcatalog = php_ion_obj(catalog, obj->cat)->cat;
872 }
873 if (obj->col) {
874 // TODO
875 }
876 if (obj->dec_ctx) {
877 update_property_obj(&obj->std, ZEND_STRL("decimalContext"), obj->dec_ctx);
878 obj->opt.decimal_context = &php_ion_obj(decimal_ctx, obj->dec_ctx)->ctx;
879 }
880 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("outputBinary"),
881 obj->opt.output_as_binary = binary);
882 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("comactFloats"),
883 obj->opt.compact_floats = compact_floats);
884 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("excapeNonAscii"),
885 obj->opt.escape_all_non_ascii = escape);
886 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("prettyPrint"),
887 obj->opt.pretty_print = pretty);
888 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("indentTabs"),
889 obj->opt.indent_with_tabs = tabs);
890 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("indentSize"),
891 obj->opt.indent_size = indent);
892 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("smallContainersInline"),
893 obj->opt.small_containers_in_line = small_cntr_inl);
894 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("suppressSystemValues"),
895 obj->opt.supress_system_values = suppress_sys);
896 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("flushEveryValue"),
897 obj->opt.flush_every_value = flush);
898 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("maxContainerDepth"),
899 obj->opt.max_container_depth = max_depth);
900 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("maxAnnotations"),
901 obj->opt.max_annotation_count = max_ann);
902 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("tempBufferSize"),
903 obj->opt.temp_buffer_size = temp);
904 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("allocationPageSize"),
905 obj->opt.allocation_page_size = alloc);
906 }
907 ZEND_METHOD(ion_Writer_Writer, writeNull)
908 {
909 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
910 OBJ_CHECK(obj);
911
912 ZEND_PARSE_PARAMETERS_NONE();
913
914 ION_CHECK(ion_writer_write_null(obj->writer));
915 }
916 ZEND_METHOD(ion_Writer_Writer, writeTypedNull)
917 {
918 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
919 OBJ_CHECK(obj);
920
921 zend_object *typ_obj;
922 ZEND_PARSE_PARAMETERS_START(1, 1)
923 Z_PARAM_OBJ_OF_CLASS(typ_obj, ce_Type)
924 ZEND_PARSE_PARAMETERS_END();
925
926 php_ion_type *typ = php_ion_obj(type, typ_obj);
927 OBJ_CHECK(typ);
928 ION_CHECK(ion_writer_write_typed_null(obj->writer, php_ion_obj(type, typ)->typ));
929 }
930 ZEND_METHOD(ion_Writer_Writer, writeBool)
931 {
932 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
933 OBJ_CHECK(obj);
934
935 zend_bool b;
936 ZEND_PARSE_PARAMETERS_START(1, 1)
937 Z_PARAM_BOOL(b)
938 ZEND_PARSE_PARAMETERS_END();
939
940 ION_CHECK(ion_writer_write_bool(obj->writer, b));
941 }
942 ZEND_METHOD(ion_Writer_Writer, writeInt)
943 {
944 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
945 OBJ_CHECK(obj);
946
947 zend_long l;
948 zend_string *s;
949 ZEND_PARSE_PARAMETERS_START(1, 1)
950 Z_PARAM_STR_OR_LONG(s, l)
951 ZEND_PARSE_PARAMETERS_END();
952
953 if (s) {
954 ION_INT *i;
955 ION_CHECK(ion_int_alloc(obj->writer, &i));
956 ION_CHECK(ion_int_from_chars(i, s->val, s->len));
957 ION_CHECK(ion_writer_write_ion_int(obj->writer, i));
958 ion_int_free(i);
959 } else {
960 ION_CHECK(ion_writer_write_int64(obj->writer, l));
961 }
962 }
963 ZEND_METHOD(ion_Writer_Writer, writeFloat)
964 {
965 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
966 OBJ_CHECK(obj);
967
968 double d;
969 ZEND_PARSE_PARAMETERS_START(1, 1)
970 Z_PARAM_DOUBLE(d)
971 ZEND_PARSE_PARAMETERS_END();
972
973 ION_CHECK(ion_writer_write_double(obj->writer, d));
974 }
975 ZEND_METHOD(ion_Writer_Writer, writeDecimal)
976 {
977 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
978 OBJ_CHECK(obj);
979
980 zend_object *dec_obj;
981 zend_string *dec_str;
982 ZEND_PARSE_PARAMETERS_START(1, 1)
983 Z_PARAM_OBJ_OF_CLASS_OR_STR(dec_obj, ce_Decimal, dec_str)
984 ZEND_PARSE_PARAMETERS_END();
985
986 if (dec_str) {
987 ION_STRING s;
988 ION_CHECK(ion_writer_write_string(obj->writer, ion_string_from_zend(&s, dec_str)));
989 } else {
990 php_ion_decimal *dec = php_ion_obj(decimal, dec_obj);
991 ION_CHECK(ion_writer_write_ion_decimal(obj->writer, &dec->dec));
992 }
993 }
994 ZEND_METHOD(ion_Writer_Writer, writeTimestamp)
995 {
996 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
997 OBJ_CHECK(obj);
998
999 zend_object *ts_obj;
1000 zend_string *ts_str;
1001 ZEND_PARSE_PARAMETERS_START(1, 1)
1002 Z_PARAM_OBJ_OF_CLASS_OR_STR(ts_obj, ce_Timestamp, ts_str)
1003 ZEND_PARSE_PARAMETERS_END();
1004
1005 decContext *ctx = NULL;
1006 if (obj->opt) {
1007 ctx = php_ion_obj(reader_options, obj->opt)->opt.decimal_context;
1008 }
1009
1010 ION_TIMESTAMP tmp = {0};
1011 if (ts_str) {
1012 SIZE used;
1013 ION_CHECK(ion_timestamp_parse(&tmp, ts_str->val, ts_str->len, &used, ctx));
1014 } else {
1015 php_ion_timestamp *ts = php_ion_obj(timestamp, ts_obj);
1016 OBJ_CHECK(ts);
1017 ion_timestamp_from_php(&tmp, ts, ctx);
1018 }
1019 ION_CHECK(ion_writer_write_timestamp(obj->writer, &tmp));
1020 }
1021 ZEND_METHOD(ion_Writer_Writer, writeSymbol)
1022 {
1023 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1024 OBJ_CHECK(obj);
1025
1026 zend_object *sym_obj;
1027 zend_string *sym_str;
1028 ZEND_PARSE_PARAMETERS_START(1, 1)
1029 Z_PARAM_OBJ_OF_CLASS_OR_STR(sym_obj, ce_Symbol, sym_str);
1030 ZEND_PARSE_PARAMETERS_END();
1031
1032 if (sym_str) {
1033 ION_STRING is;
1034 ION_CHECK(ion_writer_write_symbol(obj->writer, ion_string_from_zend(&is, sym_str)));
1035 } else {
1036 php_ion_symbol *sym = php_ion_obj(symbol, sym_obj);
1037 PTR_CHECK(sym);
1038 ION_CHECK(ion_writer_write_ion_symbol(obj->writer, &sym->sym));
1039 }
1040 }
1041 ZEND_METHOD(ion_Writer_Writer, writeString)
1042 {
1043 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1044 OBJ_CHECK(obj);
1045
1046 zend_string *str;
1047 ZEND_PARSE_PARAMETERS_START(1, 1)
1048 Z_PARAM_STR(str)
1049 ZEND_PARSE_PARAMETERS_END();
1050
1051 ION_STRING is;
1052 ION_CHECK(ion_writer_write_string(obj->writer, ion_string_from_zend(&is, str)));
1053 }
1054 ZEND_METHOD(ion_Writer_Writer, writeCLob)
1055 {
1056 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1057 OBJ_CHECK(obj);
1058
1059 zend_string *str;
1060 ZEND_PARSE_PARAMETERS_START(1, 1)
1061 Z_PARAM_STR(str)
1062 ZEND_PARSE_PARAMETERS_END();
1063
1064 ION_CHECK(ion_writer_write_clob(obj->writer, (BYTE *) str->val, str->len));
1065 }
1066 ZEND_METHOD(ion_Writer_Writer, writeBLob)
1067 {
1068 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1069 OBJ_CHECK(obj);
1070
1071 zend_string *str;
1072 ZEND_PARSE_PARAMETERS_START(1, 1)
1073 Z_PARAM_STR(str)
1074 ZEND_PARSE_PARAMETERS_END();
1075
1076 ION_CHECK(ion_writer_write_blob(obj->writer, (BYTE *) str->val, str->len));
1077 }
1078 ZEND_METHOD(ion_Writer_Writer, startLob)
1079 {
1080 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1081 OBJ_CHECK(obj);
1082
1083 zend_object *typ_obj;
1084 ZEND_PARSE_PARAMETERS_START(1, 1)
1085 Z_PARAM_OBJ_OF_CLASS(typ_obj, ce_Type)
1086 ZEND_PARSE_PARAMETERS_END();
1087
1088 php_ion_type *typ = php_ion_obj(type, typ_obj);
1089 OBJ_CHECK(typ);
1090 ION_CHECK(ion_writer_start_lob(obj->writer, php_ion_obj(type, typ)->typ));
1091 }
1092 ZEND_METHOD(ion_Writer_Writer, appendLob)
1093 {
1094 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1095 OBJ_CHECK(obj);
1096
1097 zend_string *str;
1098 ZEND_PARSE_PARAMETERS_START(1, 1)
1099 Z_PARAM_STR(str)
1100 ZEND_PARSE_PARAMETERS_END();
1101
1102 ION_CHECK(ion_writer_append_lob(obj->writer, (BYTE *) str->val, str->len));
1103 }
1104 ZEND_METHOD(ion_Writer_Writer, finishLob)
1105 {
1106 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1107 OBJ_CHECK(obj);
1108
1109 ZEND_PARSE_PARAMETERS_NONE();
1110
1111 ION_CHECK(ion_writer_finish_lob(obj->writer));
1112 }
1113 ZEND_METHOD(ion_Writer_Writer, startContainer)
1114 {
1115 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1116 OBJ_CHECK(obj);
1117
1118 zend_object *typ_obj;
1119 ZEND_PARSE_PARAMETERS_START(1, 1)
1120 Z_PARAM_OBJ_OF_CLASS(typ_obj, ce_Type)
1121 ZEND_PARSE_PARAMETERS_END();
1122
1123 php_ion_type *typ = php_ion_obj(type, typ_obj);
1124 OBJ_CHECK(typ);
1125 ION_CHECK(ion_writer_start_container(obj->writer, php_ion_obj(type, typ)->typ));
1126 }
1127 ZEND_METHOD(ion_Writer_Writer, finishContainer)
1128 {
1129 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1130 OBJ_CHECK(obj);
1131
1132 ZEND_PARSE_PARAMETERS_NONE();
1133
1134 ION_CHECK(ion_writer_finish_container(obj->writer));
1135 }
1136 ZEND_METHOD(ion_Writer_Writer, writeFieldName)
1137 {
1138 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1139 OBJ_CHECK(obj);
1140
1141 zend_object *sym_obj;
1142 zend_string *sym_str;
1143 ZEND_PARSE_PARAMETERS_START(1, 1)
1144 Z_PARAM_OBJ_OF_CLASS_OR_STR(sym_obj, ce_Symbol, sym_str);
1145 ZEND_PARSE_PARAMETERS_END();
1146
1147 if (sym_str) {
1148 ION_STRING is;
1149 ION_CHECK(ion_writer_write_field_name(obj->writer, ion_string_from_zend(&is, sym_str)));
1150 } else {
1151 php_ion_symbol *sym = php_ion_obj(symbol, sym_obj);
1152 PTR_CHECK(sym);
1153 ION_CHECK(ion_writer_write_field_name_symbol(obj->writer, &sym->sym));
1154 }
1155 }
1156 ZEND_METHOD(ion_Writer_Writer, writeAnnotation)
1157 {
1158 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1159 OBJ_CHECK(obj);
1160
1161 zval *args;
1162 unsigned argc;
1163 ZEND_PARSE_PARAMETERS_START(1, -1)
1164 Z_PARAM_VARIADIC('+', args, argc);
1165 ZEND_PARSE_PARAMETERS_END();
1166
1167 for (unsigned i = 0; i < argc; ++i) {
1168 switch (Z_TYPE(args[i])) {
1169 case IS_STRING:
1170 ION_STRING is;
1171 ION_CHECK(ion_writer_add_annotation(obj->writer, ion_string_from_zend(&is, Z_STR(args[i]))));
1172 break;
1173
1174 case IS_OBJECT:
1175 ION_CHECK(ion_writer_add_annotation_symbol(obj->writer, &php_ion_obj(symbol, Z_OBJ(args[i]))->sym));
1176 break;
1177 }
1178 }
1179 }
1180 ZEND_METHOD(ion_Writer_Writer, getDepth)
1181 {
1182 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1183 OBJ_CHECK(obj);
1184
1185 ZEND_PARSE_PARAMETERS_NONE();
1186
1187 SIZE depth;
1188 ION_CHECK(ion_writer_get_depth(obj->writer, &depth));
1189 RETURN_LONG(depth);
1190 }
1191 ZEND_METHOD(ion_Writer_Writer, flush)
1192 {
1193 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1194 OBJ_CHECK(obj);
1195
1196 ZEND_PARSE_PARAMETERS_NONE();
1197
1198 SIZE flushed;
1199 ION_CHECK(ion_writer_flush(obj->writer, &flushed));
1200 if (obj->type == BUFFER_WRITER) {
1201 smart_str_0(&obj->buffer.str);
1202 }
1203 RETURN_LONG(flushed);
1204 }
1205 ZEND_METHOD(ion_Writer_Writer, finish)
1206 {
1207 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1208 OBJ_CHECK(obj);
1209
1210 ZEND_PARSE_PARAMETERS_NONE();
1211
1212 SIZE flushed;
1213 ION_CHECK(ion_writer_finish(obj->writer, &flushed));
1214 if (obj->type == BUFFER_WRITER) {
1215 smart_str_0(&obj->buffer.str);
1216 }
1217 RETURN_LONG(flushed);
1218 }
1219 ZEND_METHOD(ion_Writer_Writer, writeOne)
1220 {
1221 }
1222 ZEND_METHOD(ion_Writer_Writer, writeAll)
1223 {
1224 }
1225 ZEND_METHOD(ion_Writer_Buffer_Writer, __construct)
1226 {
1227 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1228 PTR_CHECK(obj);
1229
1230 zval *ref;
1231 ZEND_PARSE_PARAMETERS_START(1, 2)
1232 Z_PARAM_ZVAL(ref)
1233 Z_PARAM_OPTIONAL
1234 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Writer_Options)
1235 ZEND_PARSE_PARAMETERS_END();
1236
1237 obj->type = BUFFER_WRITER;
1238 ZVAL_COPY(&obj->buffer.val, ref);
1239 zval_dtor(Z_REFVAL_P(ref));
1240
1241 php_ion_writer_ctor(obj);
1242 }
1243 ZEND_METHOD(ion_Writer_Buffer_Writer, getBuffer)
1244 {
1245 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1246 OBJ_CHECK(obj);
1247
1248 ZEND_PARSE_PARAMETERS_NONE();
1249
1250 RETVAL_STR(zend_string_dup(obj->buffer.str.s, 0));
1251 }
1252 ZEND_METHOD(ion_Writer_Stream_Writer, __construct)
1253 {
1254 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1255 PTR_CHECK(obj);
1256
1257 zval *zstream;
1258 ZEND_PARSE_PARAMETERS_START(1, 2)
1259 Z_PARAM_RESOURCE(zstream)
1260 Z_PARAM_OPTIONAL
1261 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Writer_Options)
1262 ZEND_PARSE_PARAMETERS_END();
1263
1264 obj->type = STREAM_WRITER;
1265 php_stream_from_zval_no_verify(obj->stream.ptr, zstream);
1266
1267 php_ion_writer_ctor(obj);
1268 }
1269 ZEND_METHOD(ion_Writer_Stream_Writer, getStream)
1270 {
1271 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1272 OBJ_CHECK(obj);
1273
1274 ZEND_PARSE_PARAMETERS_NONE();
1275 PTR_CHECK(obj->stream.ptr);
1276
1277 GC_ADDREF(obj->stream.ptr->res);
1278 RETURN_RES(obj->stream.ptr->res);
1279 }
1280
1281 ZEND_METHOD(ion_PHP_Serializer, __construct)
1282 {
1283 zend_object *obj = Z_OBJ_P(ZEND_THIS);
1284
1285 zend_object *writer_obj;
1286 zend_bool call_magic = true;
1287 zend_string *call_custom = NULL;
1288 ZEND_PARSE_PARAMETERS_START(1, 3)
1289 Z_PARAM_OBJ_OF_CLASS(writer_obj, ce_Writer)
1290 Z_PARAM_OPTIONAL
1291 Z_PARAM_BOOL(call_magic)
1292 Z_PARAM_STR_OR_NULL(call_custom)
1293 ZEND_PARSE_PARAMETERS_END();
1294
1295 update_property_obj(obj, ZEND_STRL("writer"), writer_obj);
1296 zend_update_property_bool(obj->ce, obj, ZEND_STRL("callMagicSerialize"),
1297 php_ion_obj(writer, writer_obj)->php.call_magic_serialize = call_magic);
1298 if (call_custom) {
1299 zend_update_property_str(obj->ce, obj, ZEND_STRL("callCustomSerialize"), call_custom);
1300 php_ion_obj(writer, writer_obj)->php.custom_serialize = zend_string_tolower(call_custom);
1301 } else {
1302 zend_update_property_null(obj->ce, obj, ZEND_STRL("callCustomSerialize"));
1303 }
1304 }
1305 ZEND_METHOD(ion_PHP_Serializer, __invoke)
1306 {
1307 zend_object *obj = Z_OBJ_P(ZEND_THIS);
1308
1309 zval *data;
1310 ZEND_PARSE_PARAMETERS_START(1, 1)
1311 Z_PARAM_ZVAL(data)
1312 ZEND_PARSE_PARAMETERS_END();
1313
1314 zval tmp, *zwriter;
1315 if ((zwriter = zend_read_property(obj->ce, obj, ZEND_STRL("writer"), 0, &tmp))) {
1316 if (Z_OBJCE_P(zwriter) == ce_Writer_Buffer_Writer || Z_OBJCE_P(zwriter) == ce_Writer_Stream_Writer) {
1317 php_ion_writer *writer = php_ion_obj(writer, Z_OBJ_P(zwriter));
1318 php_ion_serialize(writer, data, &writer->buffer.val);
1319 RETURN_STR_COPY(writer->buffer.str.s);
1320 } else {
1321 zend_call_method_with_1_params(obj, obj->ce, NULL /* TODO */, "serialize", return_value, data);
1322 }
1323 }
1324 }
1325 ZEND_METHOD(ion_PHP_Serializer, serialize)
1326 {
1327 //zend_object *obj = Z_OBJ_P(ZEND_THIS);
1328
1329 zval *data;
1330 ZEND_PARSE_PARAMETERS_START(1, 1)
1331 Z_PARAM_ZVAL(data)
1332 ZEND_PARSE_PARAMETERS_END();
1333
1334 // TODO
1335 }
1336 ZEND_METHOD(ion_PHP_Unserializer, __construct)
1337 {
1338 zend_object *obj = Z_OBJ_P(ZEND_THIS);
1339
1340 zend_object *reader_obj;
1341 zend_bool call_magic = true;
1342 zend_string *call_custom = NULL;
1343 ZEND_PARSE_PARAMETERS_START(1, 3)
1344 Z_PARAM_OBJ_OF_CLASS(reader_obj, ce_Reader)
1345 Z_PARAM_OPTIONAL
1346 Z_PARAM_BOOL(call_magic)
1347 Z_PARAM_STR_OR_NULL(call_custom)
1348 ZEND_PARSE_PARAMETERS_END();
1349
1350 update_property_obj(obj, ZEND_STRL("reader"), reader_obj);
1351 zend_update_property_bool(obj->ce, obj, ZEND_STRL("callMagicUnserialize"),
1352 php_ion_obj(reader, reader_obj)->php.call_magic_unserialize = call_magic);
1353 if (call_custom) {
1354 zend_update_property_str(obj->ce, obj, ZEND_STRL("callCustomUnserialize"), call_custom);
1355 php_ion_obj(reader, reader_obj)->php.custom_unserialize = zend_string_tolower(call_custom);
1356 } else {
1357 zend_update_property_null(obj->ce, obj, ZEND_STRL("callCustomUnserialize"));
1358 }
1359 }
1360 ZEND_METHOD(ion_PHP_Unserializer, __invoke)
1361 {
1362 zend_object *obj = Z_OBJ_P(ZEND_THIS);
1363
1364 zval zreader;
1365 if (zend_read_property(obj->ce, obj, ZEND_STRL("reader"), 0, &zreader)) {
1366 if (Z_OBJCE(zreader) == ce_Reader_Buffer_Reader || Z_OBJCE(zreader) == ce_Reader_Stream_Reader) {
1367 php_ion_unserialize(php_ion_obj(reader, Z_OBJ(zreader)), NULL, return_value);
1368 } else {
1369 zend_call_method_with_0_params(obj, obj->ce, NULL /* TODO */, "unserialize", return_value);
1370 }
1371 }
1372 }
1373 ZEND_METHOD(ion_PHP_Unserializer, unserialize)
1374 {
1375 //zend_object *obj = Z_OBJ_P(ZEND_THIS);
1376
1377 ZEND_PARSE_PARAMETERS_NONE();
1378
1379 // TODO
1380 }
1381
1382 ZEND_FUNCTION(ion_serialize)
1383 {
1384 zval *zv;
1385 zend_object *zo_opt = NULL;
1386 ZEND_PARSE_PARAMETERS_START(1, 2)
1387 Z_PARAM_ZVAL(zv)
1388 Z_PARAM_OPTIONAL
1389 Z_PARAM_OBJ_OF_CLASS_OR_NULL(zo_opt, ce_Writer_Options)
1390 ZEND_PARSE_PARAMETERS_END();
1391
1392 zval zwriter;
1393 object_init_ex(&zwriter, ce_Writer_Buffer_Writer);
1394
1395 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ(zwriter));
1396 obj->opt = zo_opt;
1397 obj->type = BUFFER_WRITER;
1398 obj->php.call_magic_serialize = true;
1399 php_ion_writer_ctor(obj);
1400
1401 php_ion_serialize(obj, zv, return_value);
1402 zval_ptr_dtor(&zwriter);
1403 }
1404 ZEND_FUNCTION(ion_unserialize)
1405 {
1406 zend_string *zstr;
1407 zend_object *zo_opt = NULL;
1408 ZEND_PARSE_PARAMETERS_START(1, 2)
1409 Z_PARAM_STR(zstr)
1410 Z_PARAM_OPTIONAL
1411 Z_PARAM_OBJ_OF_CLASS_OR_NULL(zo_opt, ce_Reader_Options)
1412 ZEND_PARSE_PARAMETERS_END();
1413
1414 zval zreader;
1415 object_init_ex(&zreader, ce_Reader_Buffer_Reader);
1416
1417 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ(zreader));
1418 obj->opt = zo_opt;
1419 obj->type = BUFFER_READER;
1420 obj->buffer = zend_string_copy(zstr);
1421 obj->php.call_magic_unserialize = true;
1422 php_ion_reader_ctor(obj);
1423 php_ion_unserialize(obj, NULL, return_value);
1424 zval_ptr_dtor(&zreader);
1425 }
1426
1427 PHP_RINIT_FUNCTION(ion)
1428 {
1429 #if defined(ZTS) && defined(COMPILE_DL_ION)
1430 ZEND_TSRMLS_CACHE_UPDATE();
1431 #endif
1432
1433 php_ion_globals_serializer_init();
1434 php_ion_globals_unserializer_init();
1435 return SUCCESS;
1436 }
1437
1438 PHP_RSHUTDOWN_FUNCTION(ion)
1439 {
1440 php_ion_globals_serializer_dtor();
1441 php_ion_globals_unserializer_dtor();
1442 return SUCCESS;
1443 }
1444
1445 php_ion_decl(type, Type);
1446 php_ion_decl(symbol, Symbol, php_ion_symbol_dtor(obj));
1447 php_ion_decl(symbol_iloc, Symbol_ImportLocation, php_ion_symbol_iloc_dtor(obj));
1448 php_ion_decl(symbol_table, Symbol_Table);
1449 php_ion_decl(decimal, Decimal, php_ion_decimal_dtor(obj));
1450 php_ion_decl(decimal_ctx, Decimal_Context);
1451 php_ion_decl(timestamp, Timestamp, php_ion_timestamp_dtor(obj));
1452 php_ion_decl(catalog, Catalog);
1453 php_ion_decl(reader_options, Reader_Options);
1454 php_ion_decl(reader, Reader_Reader, php_ion_reader_dtor(obj));
1455 php_ion_decl(writer_options, Writer_Options);
1456 php_ion_decl(writer, Writer_Writer, php_ion_writer_dtor(obj));
1457
1458 PHP_MINIT_FUNCTION(ion)
1459 {
1460 ce_Annotation = register_class_ion_Annotation();
1461
1462 php_ion_register(type, Type);
1463 php_ion_register(symbol, Symbol);
1464 php_ion_register(symbol_iloc, Symbol_ImportLocation);
1465 php_ion_register(symbol_table, Symbol_Table);
1466
1467 ce_Symbol_System = register_class_ion_Symbol_System();
1468 ce_Symbol_System_SID = register_class_ion_Symbol_System_SID();
1469
1470 ce_Collection = register_class_ion_Collection();
1471
1472 php_ion_register(decimal, Decimal);
1473 php_ion_register(decimal_ctx, Decimal_Context);
1474 php_ion_register(timestamp, Timestamp, php_date_get_date_ce());
1475 php_ion_register(catalog, Catalog);
1476
1477 ce_Reader = register_class_ion_Reader(spl_ce_RecursiveIterator);
1478
1479 php_ion_register(reader_options, Reader_Options);
1480 php_ion_register(reader, Reader_Reader, ce_Reader);
1481
1482 ce_Reader_Buffer = register_class_ion_Reader_Buffer(ce_Reader);
1483 ce_Reader_Buffer_Reader = register_class_ion_Reader_Buffer_Reader(ce_Reader_Reader, ce_Reader_Buffer);
1484 ce_Reader_Stream = register_class_ion_Reader_Stream(ce_Reader);
1485 ce_Reader_Stream_Reader = register_class_ion_Reader_Stream_Reader(ce_Reader_Reader, ce_Reader_Stream);
1486
1487 ce_Writer = register_class_ion_Writer();
1488
1489 php_ion_register(writer_options, Writer_Options);
1490 php_ion_register(writer, Writer_Writer, ce_Writer);
1491
1492 ce_Writer_Buffer = register_class_ion_Writer_Buffer(ce_Writer);
1493 ce_Writer_Buffer_Writer = register_class_ion_Writer_Buffer_Writer(ce_Writer_Writer, ce_Writer_Buffer);
1494 ce_Writer_Stream = register_class_ion_Writer_Stream(ce_Writer);
1495 ce_Writer_Stream_Writer = register_class_ion_Writer_Stream_Writer(ce_Writer_Writer, ce_Writer_Stream);
1496
1497 ce_PHP_Serializer = register_class_ion_PHP_Serializer();
1498 ce_PHP_Unserializer = register_class_ion_PHP_Unserializer();
1499
1500 return SUCCESS;
1501 }
1502
1503 PHP_MINFO_FUNCTION(ion)
1504 {
1505 php_info_print_table_start();
1506 php_info_print_table_header(2, "ion support", "enabled");
1507 php_info_print_table_end();
1508 }
1509
1510 PHP_GINIT_FUNCTION(ion)
1511 {
1512 memset(ion_globals, 0, sizeof(*ion_globals));
1513 }
1514 PHP_GSHUTDOWN_FUNCTION(ion)
1515 {
1516 }
1517
1518 static zend_module_dep ion_module_deps[] = {
1519 ZEND_MOD_REQUIRED("spl")
1520 ZEND_MOD_END
1521 };
1522
1523 zend_module_entry ion_module_entry = {
1524 STANDARD_MODULE_HEADER_EX,
1525 NULL,
1526 ion_module_deps,
1527 "ion", /* Extension name */
1528 ext_functions, /* zend_function_entry */
1529 PHP_MINIT(ion), /* PHP_MINIT - Module initialization */
1530 NULL, /* PHP_MSHUTDOWN - Module shutdown */
1531 PHP_RINIT(ion), /* PHP_RINIT - Request initialization */
1532 PHP_RSHUTDOWN(ion), /* PHP_RSHUTDOWN - Request shutdown */
1533 PHP_MINFO(ion), /* PHP_MINFO - Module info */
1534 PHP_ION_VERSION, /* Version */
1535 ZEND_MODULE_GLOBALS(ion),
1536 PHP_GINIT(ion), /* PHP_GINIT */
1537 PHP_GSHUTDOWN(ion), /* PHP_GSHUTDOWN */
1538 NULL,
1539 STANDARD_MODULE_PROPERTIES_EX
1540 };
1541
1542 #ifdef COMPILE_DL_ION
1543 # ifdef ZTS
1544 ZEND_TSRMLS_CACHE_DEFINE()
1545 # endif
1546 ZEND_GET_MODULE(ion)
1547 #endif