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