show versions in MINFO
[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
29 #include "php_ion.h"
30 #include "ion_private.h"
31
32 static ZEND_METHOD(ion_Symbol_ImportLocation, __construct)
33 {
34 php_ion_symbol_iloc *obj = php_ion_obj(symbol_iloc, Z_OBJ_P(ZEND_THIS));
35 PTR_CHECK(obj);
36
37 zend_long location;
38 ZEND_PARSE_PARAMETERS_START(2, 2)
39 Z_PARAM_STR(obj->name)
40 Z_PARAM_LONG(location)
41 ZEND_PARSE_PARAMETERS_END();
42
43 obj->loc.location = (SID) location;
44 php_ion_symbol_iloc_ctor(obj);
45 }
46 static ZEND_METHOD(ion_Symbol, __construct)
47 {
48 php_ion_symbol *obj = php_ion_obj(symbol, Z_OBJ_P(ZEND_THIS));
49 PTR_CHECK(obj);
50
51 zend_long sid = -1;
52 ZEND_PARSE_PARAMETERS_START(0, 3)
53 Z_PARAM_OPTIONAL
54 Z_PARAM_STR_OR_NULL(obj->value)
55 Z_PARAM_LONG(sid)
56 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->iloc, ce_Symbol_ImportLocation)
57 ZEND_PARSE_PARAMETERS_END();
58
59 obj->sym.sid = (SID) sid;
60 php_ion_symbol_ctor(obj);
61 }
62 static ZEND_METHOD(ion_Symbol, equals)
63 {
64 php_ion_symbol *sym = php_ion_obj(symbol, Z_OBJ_P(ZEND_THIS));
65 OBJ_CHECK(sym);
66
67 zend_object *other_obj;
68 ZEND_PARSE_PARAMETERS_START(1, 1)
69 Z_PARAM_OBJ_OF_CLASS(other_obj, ce_Symbol)
70 ZEND_PARSE_PARAMETERS_END();
71
72 BOOL eq = FALSE;
73 iERR err = ion_symbol_is_equal(
74 &sym->sym,
75 &php_ion_obj(symbol, other_obj)->sym, &eq);
76 ION_CHECK(err);
77 RETVAL_BOOL(eq);
78 }
79 static ZEND_METHOD(ion_Symbol, __toString)
80 {
81 php_ion_symbol *sym = php_ion_obj(symbol, Z_OBJ_P(ZEND_THIS));
82 OBJ_CHECK(sym);
83
84 ZEND_PARSE_PARAMETERS_NONE();
85
86 if (!sym->value) {
87 RETURN_EMPTY_STRING();
88 }
89 RETURN_STR_COPY(sym->value);
90 }
91 static ZEND_METHOD(ion_Symbol_Enum, toSymbol)
92 {
93 ZEND_PARSE_PARAMETERS_NONE();
94
95 zval *zc = zend_enum_fetch_case_name(Z_OBJ_P(ZEND_THIS));
96 PTR_CHECK(zc);
97
98 zval *zsym = php_ion_global_symbol_fetch_by_enum(Z_STR_P(zc));
99 PTR_CHECK(zsym);
100 RETVAL_ZVAL(zsym, 1, 0);
101 }
102 static ZEND_METHOD(ion_Symbol_Enum, toSID)
103 {
104 ZEND_PARSE_PARAMETERS_NONE();
105
106 zval *zc = zend_enum_fetch_case_name(Z_OBJ_P(ZEND_THIS));
107 PTR_CHECK(zc);
108
109 zval *zsym = php_ion_global_symbol_fetch_by_enum(Z_STR_P(zc));
110 PTR_CHECK(zsym);
111
112 zval tmp;
113 RETVAL_ZVAL(zend_read_property(Z_OBJCE_P(zsym), Z_OBJ_P(zsym), ZEND_STRL("sid"), 0, &tmp), 1, 0);
114 }
115 static ZEND_METHOD(ion_Symbol_Enum, toString)
116 {
117 ZEND_PARSE_PARAMETERS_NONE();
118
119 zval *zc = zend_enum_fetch_case_name(Z_OBJ_P(ZEND_THIS));
120 PTR_CHECK(zc);
121
122 zval *zsym = php_ion_global_symbol_fetch_by_enum(Z_STR_P(zc));
123 PTR_CHECK(zsym);
124
125 zval tmp;
126 RETVAL_ZVAL(zend_read_property(Z_OBJCE_P(zsym), Z_OBJ_P(zsym), ZEND_STRL("value"), 0, &tmp), 1, 0);
127 }
128 static ZEND_METHOD(ion_Symbol_System, asTable)
129 {
130 ZEND_PARSE_PARAMETERS_NONE();
131
132 object_init_ex(return_value, ce_Symbol_Table_Shared);
133 php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(return_value));
134 ION_CHECK(ion_symbol_table_get_system_table(&obj->tab, 1));
135 php_ion_symbol_table_ctor(obj);
136 ion_symbol_table_lock(obj->tab);
137 }
138 static ZEND_METHOD(ion_Symbol_PHP, asTable)
139 {
140 ZEND_PARSE_PARAMETERS_NONE();
141
142 object_init_ex(return_value, ce_Symbol_Table_Shared);
143 php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(return_value));
144 obj->tab = g_sym_tab_php;
145 php_ion_symbol_table_ctor(obj);
146 ion_symbol_table_lock(obj->tab);
147 }
148 static ZEND_METHOD(ion_Symbol_Table_Local, __construct)
149 {
150 php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(ZEND_THIS));
151 PTR_CHECK(obj);
152
153 ZEND_PARSE_PARAMETERS_NONE();
154
155 ION_CHECK(ion_symbol_table_open_with_type(&obj->tab, NULL, ist_LOCAL));
156 obj->dtor = ion_symbol_table_close;
157 OBJ_CHECK(obj);
158 }
159 static ZEND_METHOD(ion_Symbol_Table_Local, import)
160 {
161 php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(ZEND_THIS));
162 OBJ_CHECK(obj);
163
164 zend_object *zo_import;
165 ZEND_PARSE_PARAMETERS_START(1, 1)
166 Z_PARAM_OBJ_OF_CLASS(zo_import, ce_Symbol_Table);
167 ZEND_PARSE_PARAMETERS_END();
168
169 php_ion_symbol_table_import(obj, php_ion_obj(symbol_table, zo_import));
170 }
171 static ZEND_METHOD(ion_Symbol_Table_Shared, __construct)
172 {
173 php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(ZEND_THIS));
174 PTR_CHECK(obj);
175
176 zend_string *zname;
177 zend_long version = 1;
178 HashTable *ht_sym = NULL;
179 ZEND_PARSE_PARAMETERS_START(1, 3)
180 Z_PARAM_STR(zname)
181 Z_PARAM_OPTIONAL
182 Z_PARAM_LONG(version)
183 Z_PARAM_ARRAY_HT_OR_NULL(ht_sym)
184 ZEND_PARSE_PARAMETERS_END();
185
186 ION_CHECK(ion_symbol_table_open_with_type(&obj->tab, NULL, ist_SHARED));
187 obj->dtor = ion_symbol_table_close;
188
189 ION_STRING is;
190 ION_CHECK(ion_symbol_table_set_name(obj->tab, ion_string_from_zend(&is, zname)));
191 ION_CHECK(ion_symbol_table_set_version(obj->tab, version));
192
193 php_ion_symbol_table_ctor(obj);
194
195 zval *zsym;
196 if (ht_sym) ZEND_HASH_FOREACH_VAL(ht_sym, zsym)
197 {
198 zend_string *str = zval_get_string(zsym);
199 if (EG(exception)) {
200 break;
201 }
202
203 ION_STRING istr;
204 ION_CHECK(ion_symbol_table_add_symbol(obj->tab, ion_string_from_zend(&istr, str), NULL), zend_string_release(str));
205 zend_string_release(str);
206 }
207 ZEND_HASH_FOREACH_END();
208 }
209 static ZEND_METHOD(ion_Symbol_Table, getMaxId)
210 {
211 php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(ZEND_THIS));
212 OBJ_CHECK(obj);
213
214 ZEND_PARSE_PARAMETERS_NONE();
215
216 SID max_sid;
217 ION_CHECK(ion_symbol_table_get_max_sid(obj->tab, &max_sid));
218 RETURN_LONG(max_sid);
219 }
220 static ZEND_METHOD(ion_Symbol_Table, add)
221 {
222 php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(ZEND_THIS));
223 OBJ_CHECK(obj);
224
225 zend_object *zo_sym = NULL;
226 zend_string *zs_sym = NULL;
227 ZEND_PARSE_PARAMETERS_START(1, 1)
228 Z_PARAM_OBJ_OF_CLASS_OR_STR(zo_sym, ce_Symbol, zs_sym)
229 ZEND_PARSE_PARAMETERS_END();
230
231 if (zo_sym) {
232 zval z_sym;
233 ZVAL_OBJ(&z_sym, zo_sym);
234 zs_sym = zval_get_string(&z_sym);
235 ION_CATCH();
236 }
237 SID sid;
238 ION_STRING is;
239 ION_CHECK(ion_symbol_table_add_symbol(obj->tab, ion_string_from_zend(&is, zs_sym), &sid),
240 if (zo_sym) {
241 zend_string_release(zs_sym);
242 });
243 if (zo_sym) {
244 zend_string_release(zs_sym);
245 }
246 RETURN_LONG(sid);
247 }
248 static ZEND_METHOD(ion_Symbol_Table, find)
249 {
250 php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(ZEND_THIS));
251 OBJ_CHECK(obj);
252
253 zend_long zsid;
254 zend_string *zstring = NULL;
255 ZEND_PARSE_PARAMETERS_START(1, 1)
256 Z_PARAM_STR_OR_LONG(zstring, zsid)
257 ZEND_PARSE_PARAMETERS_END();
258
259 if (zstring) {
260 SID sid;
261 ION_STRING is;
262 ION_CHECK(ion_symbol_table_find_by_name(obj->tab, ion_string_from_zend(&is, zstring), &sid));
263 zsid = sid;
264 }
265 ION_SYMBOL *sym;
266 ION_CHECK(ion_symbol_table_get_symbol(obj->tab, zsid, &sym));
267 if (sym) {
268 php_ion_symbol_table_symbol_zval(obj, sym, return_value);
269 }
270 }
271 static ZEND_METHOD(ion_Symbol_Table, findLocal)
272 {
273 php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(ZEND_THIS));
274 OBJ_CHECK(obj);
275
276 zend_long zsid;
277 zend_string *zstring;
278 ZEND_PARSE_PARAMETERS_START(1, 1)
279 Z_PARAM_STR_OR_LONG(zstring, zsid)
280 ZEND_PARSE_PARAMETERS_END();
281
282 if (zstring) {
283 SID sid;
284 ION_STRING is;
285 ION_CHECK(ion_symbol_table_find_by_name(obj->tab, ion_string_from_zend(&is, zstring), &sid));
286 zsid = sid;
287 }
288 ION_SYMBOL *sym;
289 ION_CHECK(ion_symbol_table_get_local_symbol(obj->tab, zsid, &sym));
290 if (sym) {
291 php_ion_symbol_table_symbol_zval(obj, sym, return_value);
292 }
293 }
294 static ZEND_METHOD(ion_Catalog, __construct)
295 {
296 php_ion_catalog *obj = php_ion_obj(catalog, Z_OBJ_P(ZEND_THIS));
297 PTR_CHECK(obj);
298
299 ZEND_PARSE_PARAMETERS_NONE();
300
301 php_ion_catalog_ctor(obj);
302 }
303 static ZEND_METHOD(ion_Catalog, count)
304 {
305 php_ion_catalog *obj = php_ion_obj(catalog, Z_OBJ_P(ZEND_THIS));
306 OBJ_CHECK(obj);
307
308 ZEND_PARSE_PARAMETERS_NONE();
309
310 int32_t c;
311 ION_CHECK(ion_catalog_get_symbol_table_count(obj->cat, &c));
312 RETURN_LONG(c);
313 }
314 static ZEND_METHOD(ion_Catalog, add)
315 {
316 php_ion_catalog *obj = php_ion_obj(catalog, Z_OBJ_P(ZEND_THIS));
317 OBJ_CHECK(obj);
318
319 zend_object *zo_symtab;
320 ZEND_PARSE_PARAMETERS_START(1, 1)
321 Z_PARAM_OBJ_OF_CLASS(zo_symtab, ce_Symbol_Table)
322 ZEND_PARSE_PARAMETERS_END();
323
324 php_ion_symbol_table *o_symtab = php_ion_obj(symbol_table, zo_symtab);
325 php_ion_catalog_add_symbol_table(obj, o_symtab);
326 }
327
328 static ZEND_METHOD(ion_Catalog, remove)
329 {
330 php_ion_catalog *obj = php_ion_obj(catalog, Z_OBJ_P(ZEND_THIS));
331 OBJ_CHECK(obj);
332
333 zend_object *zo_symtab = NULL;
334 zend_string *zs_symtab = NULL;
335 ZEND_PARSE_PARAMETERS_START(1, 1)
336 Z_PARAM_OBJ_OF_CLASS_OR_STR(zo_symtab, ce_Symbol_Table, zs_symtab)
337 ZEND_PARSE_PARAMETERS_END();
338
339 RETVAL_FALSE;
340
341 zval tmp;
342 zval *ztabs = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("symbolTables"), 0, &tmp);
343 if (ztabs) {
344 if (zo_symtab) {
345 // fast path
346 zend_ulong idx = (uintptr_t) &zo_symtab->gc;
347 RETVAL_BOOL(SUCCESS == zend_hash_index_del(Z_ARRVAL_P(ztabs), idx));
348 ION_CHECK(ion_catalog_release_symbol_table(obj->cat, php_ion_obj(symbol_table, zo_symtab)->tab));
349 } else {
350 bool deleted = false;
351 ION_SYMBOL_TABLE *tab;
352 ION_STRING is;
353 ion_string_from_zend(&is, zs_symtab);
354 do {
355 tab = NULL;
356 ION_CHECK(ion_catalog_find_best_match(obj->cat, &is, 0, &tab));
357 if (tab) {
358 ION_CHECK(ion_catalog_release_symbol_table(obj->cat, tab));
359 deleted = true;
360 }
361 } while(tab);
362 RETVAL_BOOL(deleted);
363 }
364 }
365 }
366 static ZEND_METHOD(ion_Catalog, find)
367 {
368 php_ion_catalog *obj = php_ion_obj(catalog, Z_OBJ_P(ZEND_THIS));
369 OBJ_CHECK(obj);
370
371 zend_long zversion = 0;
372 zend_string *zname;
373 ZEND_PARSE_PARAMETERS_START(1, 2)
374 Z_PARAM_STR(zname)
375 Z_PARAM_OPTIONAL
376 Z_PARAM_LONG(zversion)
377 ZEND_PARSE_PARAMETERS_END();
378
379 ION_STRING is;
380 ION_SYMBOL_TABLE *tab_ptr = NULL;
381 ION_CHECK(ion_catalog_find_symbol_table(obj->cat, ion_string_from_zend(&is, zname), zversion, &tab_ptr));
382 if (tab_ptr) {
383 php_ion_catalog_symbol_table_zval(obj, tab_ptr, return_value);
384 }
385 }
386 static ZEND_METHOD(ion_Catalog, findBest)
387 {
388 php_ion_catalog *obj = php_ion_obj(catalog, Z_OBJ_P(ZEND_THIS));
389 OBJ_CHECK(obj);
390
391 zend_long zversion = 0;
392 zend_string *zname;
393 ZEND_PARSE_PARAMETERS_START(1, 2)
394 Z_PARAM_STR(zname)
395 Z_PARAM_OPTIONAL
396 Z_PARAM_LONG(zversion)
397 ZEND_PARSE_PARAMETERS_END();
398
399 ION_STRING is;
400 ION_SYMBOL_TABLE *tab_ptr = NULL;
401 ION_CHECK(ion_catalog_find_best_match(obj->cat, ion_string_from_zend(&is, zname), zversion, &tab_ptr));
402 if (tab_ptr) {
403 php_ion_catalog_symbol_table_zval(obj, tab_ptr, return_value);
404 }
405 }
406 static ZEND_METHOD(ion_Timestamp, __construct)
407 {
408 php_ion_timestamp *obj = php_ion_obj(timestamp, Z_OBJ_P(ZEND_THIS));
409 PTR_CHECK(obj);
410
411 zend_long precision;
412 zend_object *precision_obj = NULL, *format_obj = NULL;
413 zend_string *fmt = NULL, *dt = NULL, *tz = NULL;
414 zend_object *tz_obj = NULL;
415 zval z_tz_tmp, *z_tz_ptr = NULL;
416 ZEND_PARSE_PARAMETERS_START(1, 4)
417 Z_PARAM_OBJ_OF_CLASS_OR_LONG(precision_obj, ce_Timestamp_Precision, precision)
418 Z_PARAM_OPTIONAL
419 Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(format_obj, ce_Timestamp_Format, fmt)
420 Z_PARAM_STR_OR_NULL(dt)
421 Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(tz_obj, php_date_get_timezone_ce(), tz)
422 ZEND_PARSE_PARAMETERS_END();
423
424 if (precision_obj) {
425 precision = Z_LVAL_P(zend_enum_fetch_case_value(precision_obj));
426 }
427 if (format_obj) {
428 fmt = Z_STR_P(zend_enum_fetch_case_value(format_obj));
429 }
430 if (tz_obj) {
431 ZVAL_OBJ(z_tz_ptr = &z_tz_tmp, tz_obj);
432 } else if (tz) {
433 // there's no public API, so call timezone_open
434 zend_function *tz_open = zend_fetch_function_str(ZEND_STRL("timezone_open"));
435 if (tz_open) {
436 zval z_arg;
437 ZVAL_STR(&z_arg, tz);
438 zend_call_known_function(tz_open, NULL, NULL, z_tz_ptr = &z_tz_tmp, 1, &z_arg, NULL);
439 }
440 }
441 php_ion_timestamp_ctor(obj, precision, fmt, dt, z_tz_ptr);
442 if (tz && z_tz_ptr) {
443 zval_ptr_dtor(z_tz_ptr);
444 }
445 }
446 static ZEND_METHOD(ion_Timestamp, __toString)
447 {
448 php_ion_timestamp *obj = php_ion_obj(timestamp, Z_OBJ_P(ZEND_THIS));
449 OBJ_CHECK(obj);
450
451 ZEND_PARSE_PARAMETERS_NONE();
452
453 zval fmt;
454 ZVAL_NULL(&fmt);
455 zend_call_method_with_1_params(&obj->std, obj->std.ce, NULL, "format", return_value,
456 zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("format"), 0, &fmt));
457 }
458 static ZEND_METHOD(ion_Decimal_Context, __construct)
459 {
460 php_ion_decimal_ctx *obj = php_ion_obj(decimal_ctx, Z_OBJ_P(ZEND_THIS));
461 PTR_CHECK(obj);
462
463 zend_bool clamp;
464 zend_object *o_round = NULL;
465 zend_long digits, emax, emin, round;
466 ZEND_PARSE_PARAMETERS_START(5, 5)
467 Z_PARAM_LONG(digits)
468 Z_PARAM_LONG(emax)
469 Z_PARAM_LONG(emin)
470 Z_PARAM_OBJ_OF_CLASS_OR_LONG(o_round, ce_Decimal_Context_Rounding, round)
471 Z_PARAM_BOOL(clamp)
472 ZEND_PARSE_PARAMETERS_END();
473
474 if (o_round) {
475 round = Z_LVAL_P(zend_enum_fetch_case_value(o_round));
476 }
477 php_ion_decimal_ctx_init(&obj->ctx, digits, emax, emin, round, clamp); // NOLINT(cppcoreguidelines-narrowing-conversions)
478 php_ion_decimal_ctx_ctor(obj, o_round);
479 }
480 static inline void make_decimal_ctx(INTERNAL_FUNCTION_PARAMETERS, int kind)
481 {
482 ZEND_PARSE_PARAMETERS_NONE();
483
484 object_init_ex(return_value, ce_Decimal_Context);
485 php_ion_decimal_ctx *obj = php_ion_obj(decimal_ctx, Z_OBJ_P(return_value));
486 decContextDefault(&obj->ctx, kind);
487 php_ion_decimal_ctx_ctor(obj, NULL);
488 }
489 static ZEND_METHOD(ion_Decimal_Context, Dec32)
490 {
491 make_decimal_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, DEC_INIT_DECIMAL32);
492 }
493 static ZEND_METHOD(ion_Decimal_Context, Dec64)
494 {
495 make_decimal_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, DEC_INIT_DECIMAL64);
496 }
497 static ZEND_METHOD(ion_Decimal_Context, Dec128)
498 {
499 make_decimal_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, DEC_INIT_DECIMAL128);
500 }
501 static ZEND_METHOD(ion_Decimal_Context, DecMax)
502 {
503 zend_object *o_round = NULL;
504 zend_long round = DEC_ROUND_HALF_EVEN;
505 ZEND_PARSE_PARAMETERS_START(0, 1)
506 Z_PARAM_OPTIONAL
507 Z_PARAM_OBJ_OF_CLASS_OR_LONG(o_round, ce_Decimal_Context_Rounding, round)
508 ZEND_PARSE_PARAMETERS_END();
509
510 if (o_round) {
511 round = Z_LVAL_P(zend_enum_fetch_case_value(o_round));
512 }
513 object_init_ex(return_value, ce_Decimal_Context);
514 php_ion_decimal_ctx *obj = php_ion_obj(decimal_ctx, Z_OBJ_P(return_value));
515 php_ion_decimal_ctx_init_max(&obj->ctx, round);
516 php_ion_decimal_ctx_ctor(obj, o_round);
517 }
518 static ZEND_METHOD(ion_Decimal, __construct)
519 {
520 php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS));
521 PTR_CHECK(obj);
522
523 zend_long num;
524 zend_string *zstr;
525 ZEND_PARSE_PARAMETERS_START(1, 2)
526 Z_PARAM_STR_OR_LONG(zstr, num)
527 Z_PARAM_OPTIONAL
528 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->ctx, ce_Decimal_Context)
529 ZEND_PARSE_PARAMETERS_END();
530
531 if (obj->ctx) {
532 GC_ADDREF(obj->ctx);
533 } else {
534 zval zdc;
535 object_init_ex(&zdc, ce_Decimal_Context);
536 obj->ctx = Z_OBJ(zdc);
537 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx, obj->ctx), NULL);
538 }
539
540 decContext *ctx = &php_ion_obj(decimal_ctx, obj->ctx)->ctx;
541
542 if (zstr) {
543 ION_CHECK(ion_decimal_from_string(&obj->dec, zstr->val, ctx), OBJ_RELEASE(obj->ctx));
544 } else {
545 php_ion_decimal_from_zend_long(&obj->dec, ctx, num);
546 }
547
548 php_ion_decimal_ctor(obj);
549 OBJ_RELEASE(obj->ctx);
550 }
551 static ZEND_METHOD(ion_Decimal, equals)
552 {
553 php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS));
554 PTR_CHECK(obj);
555
556 zend_object *dec_obj;
557 ZEND_PARSE_PARAMETERS_START(1, 1)
558 Z_PARAM_OBJ_OF_CLASS(dec_obj, ce_Decimal)
559 ZEND_PARSE_PARAMETERS_END();
560
561 BOOL is = FALSE;
562 ION_CHECK(ion_decimal_equals(&obj->dec, &php_ion_obj(decimal, dec_obj)->dec,
563 obj->ctx ? &php_ion_obj(decimal_ctx, obj->ctx)->ctx : NULL, &is));
564 RETURN_BOOL(is);
565 }
566 static ZEND_METHOD(ion_Decimal, __toString)
567 {
568 php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS));
569 PTR_CHECK(obj);
570
571 ZEND_PARSE_PARAMETERS_NONE();
572
573 RETURN_STR(php_ion_decimal_to_string(&obj->dec));
574 }
575 static ZEND_METHOD(ion_Decimal, toInt)
576 {
577 php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS));
578 PTR_CHECK(obj);
579
580 ZEND_PARSE_PARAMETERS_NONE();
581
582 zend_long l;
583 php_ion_decimal_to_zend_long(&obj->dec, &php_ion_obj(decimal_ctx, obj->ctx)->ctx, &l);
584 RETURN_LONG(l);
585 }
586 static ZEND_METHOD(ion_Decimal, isInt)
587 {
588 php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS));
589 PTR_CHECK(obj);
590
591 ZEND_PARSE_PARAMETERS_NONE();
592
593 RETURN_BOOL(ion_decimal_is_integer(&obj->dec));
594 }
595 static ZEND_METHOD(ion_LOB, __construct)
596 {
597 zend_string *value;
598 zend_object *type = NULL;
599 ZEND_PARSE_PARAMETERS_START(1, 2)
600 Z_PARAM_STR(value)
601 Z_PARAM_OPTIONAL
602 Z_PARAM_OBJ_OF_CLASS(type, ce_Type)
603 ZEND_PARSE_PARAMETERS_END();
604
605 if (!type) {
606 type = zend_enum_get_case_cstr(ce_Type, "CLob");
607 }
608 update_property_obj(Z_OBJ_P(ZEND_THIS), ZEND_STRL("type"), type);
609 zend_update_property_str(Z_OBJCE_P(ZEND_THIS), Z_OBJ_P(ZEND_THIS), ZEND_STRL("value"), value);
610 }
611 static ZEND_METHOD(ion_Reader_Options, __construct)
612 {
613 php_ion_reader_options *opt = php_ion_obj(reader_options, Z_OBJ_P(ZEND_THIS));
614 zend_bool ret_sys_val = false, skip_validation = false;
615 zend_long max_depth = 10, max_ann = 10, ann_buf_siz = 0x4000, tmp_buf_siz = 0x4000;
616
617 PTR_CHECK(opt);
618
619 ZEND_PARSE_PARAMETERS_START(0, 9)
620 Z_PARAM_OPTIONAL
621 // public readonly ?\ion\Catalog $catalog = null,
622 Z_PARAM_OBJ_OF_CLASS_OR_NULL(opt->cat, ce_Catalog)
623 // public readonly ?\ion\Decimal\Context $decimalContext = null,
624 Z_PARAM_OBJ_OF_CLASS_OR_NULL(opt->dec_ctx, ce_Decimal_Context)
625 // public readonly ?\Closure $onContextChange = null,
626 Z_PARAM_OBJ_OF_CLASS_OR_NULL(opt->cb, zend_ce_closure);
627 // public readonly bool $returnSystemValues = false,
628 Z_PARAM_BOOL(ret_sys_val)
629 // public readonly int $maxContainerDepth = 10,
630 Z_PARAM_LONG(max_depth)
631 // public readonly int $maxAnnotations = 10,
632 Z_PARAM_LONG(max_ann)
633 // public readonly int $annotationBufferSize = 0x4000,
634 Z_PARAM_LONG(ann_buf_siz)
635 // public readonly int $tempBufferSize = 0x4000,
636 Z_PARAM_LONG(tmp_buf_siz)
637 // public readonly bool $skipCharacterValidation = false,
638 Z_PARAM_BOOL(skip_validation)
639 ZEND_PARSE_PARAMETERS_END();
640
641 if (opt->cb) {
642 zval zcb;
643 ZVAL_OBJ(&zcb, opt->cb);
644 zend_fcall_info_init(&zcb, 0, &opt->ccn.fci, &opt->ccn.fcc, NULL, NULL);
645 opt->opt.context_change_notifier.context = &opt->ccn;
646 update_property_obj(&opt->std, ZEND_STRL("onContextChange"), opt->cb);
647 } else {
648 zend_update_property_null(NULL, &opt->std, ZEND_STRL("onContextChange"));
649 }
650 if (opt->cat) {
651 update_property_obj(&opt->std, ZEND_STRL("catalog"), opt->cat);
652 opt->opt.pcatalog = php_ion_obj(catalog, opt->cat)->cat;
653 } else {
654 zend_update_property_null(NULL, &opt->std, ZEND_STRL("catalog"));
655 }
656 if (opt->dec_ctx) {
657 update_property_obj(&opt->std, ZEND_STRL("decimalContext"), opt->dec_ctx);
658 opt->opt.decimal_context = &php_ion_obj(decimal_ctx, opt->dec_ctx)->ctx;
659 } else {
660 zend_update_property_null(NULL, &opt->std, ZEND_STRL("decimalContext"));
661 }
662 zend_update_property_bool(opt->std.ce, &opt->std, ZEND_STRL("returnSystemValues"),
663 opt->opt.return_system_values = ret_sys_val);
664 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("maxContainerDepth"),
665 opt->opt.max_container_depth = (SIZE) max_depth);
666 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("maxAnnotations"),
667 opt->opt.max_annotation_count = (SIZE) max_ann);
668 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("annotationBufferSize"),
669 opt->opt.max_annotation_buffered = (SIZE) ann_buf_siz);
670 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("tempBufferSize"),
671 opt->opt.chunk_threshold = opt->opt.user_value_threshold = opt->opt.symbol_threshold = (SIZE) tmp_buf_siz);
672 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("skipCharacterValidation"),
673 opt->opt.skip_character_validation = (SIZE) skip_validation);
674 }
675 static ZEND_METHOD(ion_Reader_Reader, hasChildren)
676 {
677 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
678
679 OBJ_CHECK(obj);
680 ZEND_PARSE_PARAMETERS_NONE();
681
682 ION_TYPE t;
683 ION_CHECK(ion_reader_get_type(obj->reader, &t));
684 switch (ION_TYPE_INT(t)) {
685 case tid_LIST_INT:
686 case tid_SEXP_INT:
687 case tid_STRUCT_INT:
688 RETVAL_TRUE;
689 break;
690 default:
691 RETVAL_FALSE;
692 break;
693 }
694 }
695 static ZEND_METHOD(ion_Reader_Reader, getChildren)
696 {
697 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
698 OBJ_CHECK(obj);
699
700 ZEND_PARSE_PARAMETERS_NONE();
701
702 ION_CHECK(ion_reader_step_in(obj->reader));
703
704 RETURN_ZVAL(ZEND_THIS, 1, 0);
705 }
706 static ZEND_METHOD(ion_Reader_Reader, rewind)
707 {
708 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
709 OBJ_CHECK(obj);
710
711 ZEND_PARSE_PARAMETERS_NONE();
712
713 ION_CHECK(ion_reader_next(obj->reader, &obj->state));
714 }
715 static ZEND_METHOD(ion_Reader_Reader, next)
716 {
717 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
718 OBJ_CHECK(obj);
719
720 ZEND_PARSE_PARAMETERS_NONE();
721
722 if (obj->state == tid_EOF) {
723 SIZE depth = 0;
724 ION_CHECK(ion_reader_get_depth(obj->reader, &depth));
725 if (depth) {
726 ION_CHECK(ion_reader_step_out(obj->reader));
727 }
728 }
729 ION_CHECK(ion_reader_next(obj->reader, &obj->state));
730 }
731 static ZEND_METHOD(ion_Reader_Reader, valid)
732 {
733 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
734 OBJ_CHECK(obj);
735
736 ZEND_PARSE_PARAMETERS_NONE();
737 RETURN_BOOL(obj->state != tid_none && obj->state != tid_EOF);
738 }
739 static ZEND_METHOD(ion_Reader_Reader, key)
740 {
741 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
742 OBJ_CHECK(obj);
743
744 ZEND_PARSE_PARAMETERS_NONE();
745 RETURN_IONTYPE(obj->state);
746 }
747 static ZEND_METHOD(ion_Reader_Reader, current)
748 {
749 ZEND_PARSE_PARAMETERS_NONE();
750 RETURN_ZVAL(ZEND_THIS, 1, 0);
751 }
752 static ZEND_METHOD(ion_Reader_Reader, getType)
753 {
754 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
755 OBJ_CHECK(obj);
756
757 ZEND_PARSE_PARAMETERS_NONE();
758
759 ION_TYPE typ;
760 ION_CHECK(ion_reader_get_type(obj->reader, &typ));
761 RETURN_IONTYPE(typ);
762 }
763 static ZEND_METHOD(ion_Reader_Reader, hasAnnotations)
764 {
765 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
766 OBJ_CHECK(obj);
767
768 ZEND_PARSE_PARAMETERS_NONE();
769
770 BOOL has = FALSE;
771 ION_CHECK(ion_reader_has_any_annotations(obj->reader, &has));
772 RETURN_BOOL(has);
773 }
774 static ZEND_METHOD(ion_Reader_Reader, hasAnnotation)
775 {
776 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
777 OBJ_CHECK(obj);
778
779 zend_string *ann_zstr;
780 ZEND_PARSE_PARAMETERS_START(1, 1);
781 Z_PARAM_STR(ann_zstr);
782 ZEND_PARSE_PARAMETERS_END();
783
784 ION_STRING ann_istr;
785 BOOL has = FALSE;
786 ION_CHECK(ion_reader_has_annotation(obj->reader, ion_string_from_zend(&ann_istr, ann_zstr), &has));
787 RETURN_BOOL(has);
788 }
789 static ZEND_METHOD(ion_Reader_Reader, isNull)
790 {
791 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
792 OBJ_CHECK(obj);
793
794 ZEND_PARSE_PARAMETERS_NONE();
795
796 BOOL is = FALSE;
797 ION_CHECK(ion_reader_is_null(obj->reader, &is));
798 RETURN_BOOL(is);
799 }
800 static ZEND_METHOD(ion_Reader_Reader, isInStruct)
801 {
802 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
803 OBJ_CHECK(obj);
804
805 ZEND_PARSE_PARAMETERS_NONE();
806
807 BOOL is = FALSE;
808 ION_CHECK(ion_reader_is_in_struct(obj->reader, &is));
809 RETURN_BOOL(is);
810 }
811 static ZEND_METHOD(ion_Reader_Reader, getFieldName)
812 {
813 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
814 OBJ_CHECK(obj);
815
816 ZEND_PARSE_PARAMETERS_NONE();
817
818 ION_STRING name;
819 ION_CHECK(ion_reader_get_field_name(obj->reader, &name));
820 RETURN_STRINGL((char *) name.value, name.length);
821 }
822 static ZEND_METHOD(ion_Reader_Reader, getFieldNameSymbol)
823 {
824 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
825 OBJ_CHECK(obj);
826
827 ZEND_PARSE_PARAMETERS_NONE();
828
829 ION_SYMBOL *sym_ptr;
830 ION_CHECK(ion_reader_get_field_name_symbol(obj->reader, &sym_ptr));
831
832 php_ion_symbol_zval(sym_ptr, return_value);
833 }
834 static ZEND_METHOD(ion_Reader_Reader, getAnnotations)
835 {
836 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
837 OBJ_CHECK(obj);
838
839 ZEND_PARSE_PARAMETERS_NONE();
840
841 int32_t count, max;
842 if (obj->opt) {
843 max = php_ion_obj(reader_options, obj->opt)->opt.max_annotation_count;
844 } else {
845 max = 10;
846 }
847 ION_STRING *ptr = ecalloc(sizeof(*ptr), max);
848 iERR err = ion_reader_get_annotations(obj->reader, ptr, max, &count);
849 if (!err) {
850 array_init_size(return_value, count);
851 for (int32_t i = 0; i < count; ++i) {
852 add_next_index_str(return_value, zend_string_from_ion(&ptr[i]));
853 }
854 }
855 efree(ptr);
856 ION_CHECK(err);
857 }
858 static ZEND_METHOD(ion_Reader_Reader, getAnnotationSymbols)
859 {
860 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
861 OBJ_CHECK(obj);
862
863 ZEND_PARSE_PARAMETERS_NONE();
864
865 int32_t count, max;
866 if (obj->opt) {
867 max = php_ion_obj(reader_options, obj->opt)->opt.max_annotation_count;
868 } else {
869 max = 10;
870 }
871 ION_SYMBOL *ptr = ecalloc(sizeof(*ptr), max);
872 iERR err = ion_reader_get_annotation_symbols(obj->reader, ptr, max, &count);
873 if (!err) {
874 array_init_size(return_value, count);
875 for (int32_t i = 0; i < count; ++i) {
876 zval zsym;
877 php_ion_symbol_zval(&ptr[i], &zsym);
878 add_next_index_zval(return_value, &zsym);
879 }
880 }
881 efree(ptr);
882 ION_CHECK(err);
883 }
884 static ZEND_METHOD(ion_Reader_Reader, countAnnotations)
885 {
886 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
887 OBJ_CHECK(obj);
888
889 ZEND_PARSE_PARAMETERS_NONE();
890
891 SIZE sz = 0;
892 ION_CHECK(ion_reader_get_annotation_count(obj->reader, &sz));
893 RETURN_LONG(sz);
894 }
895 static ZEND_METHOD(ion_Reader_Reader, getAnnotation)
896 {
897 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
898 OBJ_CHECK(obj);
899
900 zend_long idx;
901 ZEND_PARSE_PARAMETERS_START(1, 1)
902 Z_PARAM_LONG(idx);
903 ZEND_PARSE_PARAMETERS_END();
904
905 ION_STRING ann;
906 ION_CHECK(ion_reader_get_an_annotation(obj->reader, idx, &ann));
907 RETURN_STRINGL((char *) ann.value, ann.length);
908 }
909 static ZEND_METHOD(ion_Reader_Reader, getAnnotationSymbol)
910 {
911 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
912 OBJ_CHECK(obj);
913
914 zend_long idx;
915 ZEND_PARSE_PARAMETERS_START(1, 1)
916 Z_PARAM_LONG(idx);
917 ZEND_PARSE_PARAMETERS_END();
918
919 ION_SYMBOL sym;
920 ION_CHECK(ion_reader_get_an_annotation_symbol(obj->reader, idx, &sym));
921 php_ion_symbol_zval(&sym, return_value);
922 }
923 static ZEND_METHOD(ion_Reader_Reader, readNull)
924 {
925 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
926 OBJ_CHECK(obj);
927
928 ZEND_PARSE_PARAMETERS_NONE();
929
930 ION_TYPE typ;
931 ION_CHECK(ion_reader_read_null(obj->reader, &typ));
932 RETURN_IONTYPE(typ);
933 }
934 static ZEND_METHOD(ion_Reader_Reader, readBool)
935 {
936 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
937 OBJ_CHECK(obj);
938
939 ZEND_PARSE_PARAMETERS_NONE();
940
941 BOOL b;
942 ION_CHECK(ion_reader_read_bool(obj->reader, &b));
943 RETURN_BOOL(b);
944 }
945 static ZEND_METHOD(ion_Reader_Reader, readInt)
946 {
947 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
948 OBJ_CHECK(obj);
949
950 ZEND_PARSE_PARAMETERS_NONE();
951
952 php_ion_reader_read_int(obj->reader, return_value);
953 }
954 static ZEND_METHOD(ion_Reader_Reader, readFloat)
955 {
956 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
957 OBJ_CHECK(obj);
958
959 ZEND_PARSE_PARAMETERS_NONE();
960
961 double dval;
962 ION_CHECK(ion_reader_read_double(obj->reader, &dval));
963 RETURN_DOUBLE(dval);
964 }
965 static ZEND_METHOD(ion_Reader_Reader, readDecimal)
966 {
967 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
968 OBJ_CHECK(obj);
969
970 ZEND_PARSE_PARAMETERS_NONE();
971
972 object_init_ex(return_value, ce_Decimal);
973 php_ion_decimal *dec = php_ion_obj(decimal, Z_OBJ_P(return_value));
974 ION_CHECK(ion_reader_read_ion_decimal(obj->reader, &dec->dec));
975 php_ion_decimal_ctor(dec);
976 }
977 static ZEND_METHOD(ion_Reader_Reader, readTimestamp)
978 {
979 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
980 OBJ_CHECK(obj);
981
982 ZEND_PARSE_PARAMETERS_NONE();
983
984 php_ion_reader_read_timestamp(obj->reader, obj->opt ? &php_ion_obj(reader_options, obj->opt)->opt : NULL, return_value);
985 }
986 static ZEND_METHOD(ion_Reader_Reader, readSymbol)
987 {
988 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
989 OBJ_CHECK(obj);
990
991 ZEND_PARSE_PARAMETERS_NONE();
992
993 ION_SYMBOL sym;
994 ION_CHECK(ion_reader_read_ion_symbol(obj->reader, &sym));
995 php_ion_symbol_zval(&sym, return_value);
996 }
997 static ZEND_METHOD(ion_Reader_Reader, readString)
998 {
999 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1000 OBJ_CHECK(obj);
1001
1002 ZEND_PARSE_PARAMETERS_NONE();
1003
1004 ION_STRING str;
1005 ION_CHECK(ion_reader_read_string(obj->reader, &str));
1006 RETURN_STRINGL((char *) str.value, str.length);
1007 }
1008
1009 typedef iERR (*read_part_fn)(ION_READER *, BYTE *, SIZE, SIZE *);
1010 static void read_part(INTERNAL_FUNCTION_PARAMETERS, read_part_fn fn)
1011 {
1012 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1013 OBJ_CHECK(obj);
1014
1015 zval *ref;
1016 zend_string *zstr;
1017 zend_long len = 0x1000;
1018 ZEND_PARSE_PARAMETERS_START(1, 1)
1019 Z_PARAM_ZVAL(ref)
1020 Z_PARAM_OPTIONAL
1021 Z_PARAM_LONG(len)
1022 ZEND_PARSE_PARAMETERS_END();
1023
1024 ZVAL_DEREF(ref);
1025
1026 if (Z_TYPE_P(ref) == IS_STRING && Z_STRLEN_P(ref) == len) {
1027 /* reuse */
1028 zstr = Z_STR_P(ref);
1029 } else {
1030 zval_dtor(ref);
1031 zstr = zend_string_alloc(len, 0);
1032 }
1033
1034 SIZE read = 0;
1035 ION_CHECK(fn(obj->reader, (BYTE *) zstr->val, zstr->len, &read), goto fail);
1036 if (read > 0) {
1037 if (read < zstr->len) {
1038 zstr = zend_string_truncate(zstr, read, 0);
1039 }
1040 ZVAL_STR(ref, zstr);
1041 RETURN_TRUE;
1042 }
1043 fail:
1044 zend_string_release(zstr);
1045 ZVAL_EMPTY_STRING(ref);
1046 RETURN_FALSE;
1047 }
1048 static ZEND_METHOD(ion_Reader_Reader, readStringPart)
1049 {
1050 read_part(INTERNAL_FUNCTION_PARAM_PASSTHRU, ion_reader_read_partial_string);
1051 }
1052 static ZEND_METHOD(ion_Reader_Reader, readLob)
1053 {
1054 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1055 OBJ_CHECK(obj);
1056
1057 ZEND_PARSE_PARAMETERS_NONE();
1058
1059 php_ion_reader_read_lob(obj->reader, return_value);
1060 }
1061 static ZEND_METHOD(ion_Reader_Reader, readLobPart)
1062 {
1063 read_part(INTERNAL_FUNCTION_PARAM_PASSTHRU, ion_reader_read_lob_partial_bytes);
1064 }
1065 static ZEND_METHOD(ion_Reader_Reader, getPosition)
1066 {
1067 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1068 OBJ_CHECK(obj);
1069
1070 ZEND_PARSE_PARAMETERS_NONE();
1071
1072 int64_t bytes = 0;
1073 int32_t dummy;
1074 ION_CHECK(ion_reader_get_position(obj->reader, &bytes, &dummy, &dummy));
1075 RETURN_LONG(bytes);
1076 }
1077 static ZEND_METHOD(ion_Reader_Reader, getDepth)
1078 {
1079 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1080 OBJ_CHECK(obj);
1081
1082 ZEND_PARSE_PARAMETERS_NONE();
1083
1084 SIZE depth = 0;
1085 ION_CHECK(ion_reader_get_depth(obj->reader, &depth));
1086 RETURN_LONG(depth);
1087 }
1088 static ZEND_METHOD(ion_Reader_Reader, seek)
1089 {
1090 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1091 OBJ_CHECK(obj);
1092
1093 zend_long off, len = -1;
1094 ZEND_PARSE_PARAMETERS_START(1, 2)
1095 Z_PARAM_LONG(off)
1096 Z_PARAM_OPTIONAL
1097 Z_PARAM_LONG(len)
1098 ZEND_PARSE_PARAMETERS_END();
1099
1100 ION_CHECK(ion_reader_seek(obj->reader, off, len));
1101 }
1102 static ZEND_METHOD(ion_Reader_Reader, getValueOffset)
1103 {
1104 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1105 OBJ_CHECK(obj);
1106
1107 ZEND_PARSE_PARAMETERS_NONE();
1108
1109 POSITION off = 0;
1110 ION_CHECK(ion_reader_get_value_offset(obj->reader, &off));
1111 RETURN_LONG(off);
1112 }
1113 static ZEND_METHOD(ion_Reader_Reader, getValueLength)
1114 {
1115 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1116 OBJ_CHECK(obj);
1117
1118 ZEND_PARSE_PARAMETERS_NONE();
1119
1120 SIZE len = 0;
1121 ION_CHECK(ion_reader_get_value_length(obj->reader, &len));
1122 RETURN_LONG(len);
1123 }
1124 static ZEND_METHOD(ion_Reader_Buffer_Reader, __construct)
1125 {
1126 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1127 PTR_CHECK(obj);
1128
1129 zend_string *zstr;
1130 ZEND_PARSE_PARAMETERS_START(1, 2)
1131 Z_PARAM_STR(zstr)
1132 Z_PARAM_OPTIONAL
1133 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Reader_Options)
1134 ZEND_PARSE_PARAMETERS_END();
1135
1136 obj->type = BUFFER_READER;
1137 obj->buffer = zend_string_copy(zstr);
1138
1139 php_ion_reader_ctor(obj);
1140 }
1141 static ZEND_METHOD(ion_Reader_Buffer_Reader, getBuffer)
1142 {
1143 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1144 OBJ_CHECK(obj);
1145
1146 ZEND_PARSE_PARAMETERS_NONE();
1147 RETURN_STR_COPY(obj->buffer);
1148 }
1149
1150 static ZEND_METHOD(ion_Reader_Stream_Reader, __construct)
1151 {
1152 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1153 PTR_CHECK(obj);
1154
1155 zval *zstream;
1156 ZEND_PARSE_PARAMETERS_START(1, 2)
1157 Z_PARAM_RESOURCE(zstream)
1158 Z_PARAM_OPTIONAL
1159 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Reader_Options)
1160 ZEND_PARSE_PARAMETERS_END();
1161
1162 obj->type = STREAM_READER;
1163 php_stream_from_zval_no_verify(obj->stream.ptr, zstream);
1164
1165 php_ion_reader_ctor(obj);
1166 }
1167 static ZEND_METHOD(ion_Reader_Stream_Reader, getStream)
1168 {
1169 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1170 OBJ_CHECK(obj);
1171
1172 ZEND_PARSE_PARAMETERS_NONE();
1173 PTR_CHECK(obj->stream.ptr);
1174
1175 GC_ADDREF(obj->stream.ptr->res);
1176 RETURN_RES(obj->stream.ptr->res);
1177 }
1178 static ZEND_METHOD(ion_Reader_Stream_Reader, resetStream)
1179 {
1180 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1181 OBJ_CHECK(obj);
1182
1183 zval *zstream;
1184 ZEND_PARSE_PARAMETERS_START(1, 1);
1185 Z_PARAM_RESOURCE(zstream);
1186 ZEND_PARSE_PARAMETERS_END();
1187
1188 ION_CHECK(ion_reader_reset_stream(&obj->reader, obj, php_ion_reader_stream_handler));
1189
1190 if (obj->stream.ptr) {
1191 zend_list_delete(obj->stream.ptr->res);
1192 }
1193 php_stream_from_zval_no_verify(obj->stream.ptr, zstream);
1194 PTR_CHECK(obj->stream.ptr);
1195 Z_ADDREF_P(zstream);
1196 }
1197 static ZEND_METHOD(ion_Reader_Stream_Reader, resetStreamWithLength)
1198 {
1199 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1200 OBJ_CHECK(obj);
1201
1202 zval *zstream;
1203 zend_long length;
1204 ZEND_PARSE_PARAMETERS_START(2, 2);
1205 Z_PARAM_RESOURCE(zstream);
1206 Z_PARAM_LONG(length)
1207 ZEND_PARSE_PARAMETERS_END();
1208
1209 ION_CHECK(ion_reader_reset_stream_with_length(&obj->reader, obj, php_ion_reader_stream_handler, length));
1210
1211 if (obj->stream.ptr) {
1212 zend_list_delete(obj->stream.ptr->res);
1213 }
1214 php_stream_from_zval_no_verify(obj->stream.ptr, zstream);
1215 PTR_CHECK(obj->stream.ptr);
1216 Z_ADDREF_P(zstream);
1217 }
1218 static ZEND_METHOD(ion_Writer_Options, __construct)
1219 {
1220 php_ion_writer_options *obj = php_ion_obj(writer_options, Z_OBJ_P(ZEND_THIS));
1221 PTR_CHECK(obj);
1222
1223 zend_bool binary = false, compact_floats = false, escape = false, pretty = false,
1224 tabs = true, flush = false;
1225 zend_long indent = 2, max_depth = 10, max_ann = 10, temp = 0x4000;
1226 ZEND_PARSE_PARAMETERS_START(0, 12)
1227 Z_PARAM_OPTIONAL
1228 //public readonly ?\ion\Catalog $catalog = null,
1229 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->cat, ce_Catalog)
1230 //public readonly ?\ion\Decimal\Context $decimalContext = null,
1231 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->dec_ctx, ce_Decimal_Context)
1232 //public readonly bool $outputBinary = false,
1233 Z_PARAM_BOOL(binary)
1234 //public readonly bool $compactFloats = false,
1235 Z_PARAM_BOOL(compact_floats)
1236 //public readonly bool $escapeNonAscii = false,
1237 Z_PARAM_BOOL(escape)
1238 //public readonly bool $prettyPrint = false,
1239 Z_PARAM_BOOL(pretty)
1240 //public readonly bool $indentTabs = true,
1241 Z_PARAM_BOOL(tabs)
1242 //public readonly int $indentSize = 2,
1243 Z_PARAM_LONG(indent)
1244 //public readonly bool $flushEveryValue = false,
1245 Z_PARAM_BOOL(flush)
1246 //public readonly int $maxContainerDepth = 10,
1247 Z_PARAM_LONG(max_depth)
1248 //public readonly int $maxAnnotations = 10,
1249 Z_PARAM_LONG(max_ann)
1250 //public readonly int $tempBufferSize = 0x4000,
1251 Z_PARAM_LONG(temp)
1252 ZEND_PARSE_PARAMETERS_END();
1253
1254 if (obj->cat) {
1255 update_property_obj(&obj->std, ZEND_STRL("catalog"), obj->cat);
1256 obj->opt.pcatalog = php_ion_obj(catalog, obj->cat)->cat;
1257 } else {
1258 zend_update_property_null(NULL, &obj->std, ZEND_STRL("catalog"));
1259 }
1260 if (obj->dec_ctx) {
1261 update_property_obj(&obj->std, ZEND_STRL("decimalContext"), obj->dec_ctx);
1262 obj->opt.decimal_context = &php_ion_obj(decimal_ctx, obj->dec_ctx)->ctx;
1263 } else {
1264 zend_update_property_null(NULL, &obj->std, ZEND_STRL("decimalContext"));
1265 }
1266 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("outputBinary"),
1267 obj->opt.output_as_binary = binary);
1268 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("compactFloats"),
1269 obj->opt.compact_floats = compact_floats);
1270 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("escapeNonAscii"),
1271 obj->opt.escape_all_non_ascii = escape);
1272 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("prettyPrint"),
1273 obj->opt.pretty_print = pretty);
1274 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("indentTabs"),
1275 obj->opt.indent_with_tabs = tabs);
1276 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("indentSize"),
1277 obj->opt.indent_size = (SIZE) indent);
1278 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("flushEveryValue"),
1279 obj->opt.flush_every_value = flush);
1280 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("maxContainerDepth"),
1281 obj->opt.max_container_depth = (SIZE) max_depth);
1282 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("maxAnnotations"),
1283 obj->opt.max_annotation_count = (SIZE) max_ann);
1284 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("tempBufferSize"),
1285 obj->opt.temp_buffer_size = (SIZE) temp);
1286 }
1287 static ZEND_METHOD(ion_Writer_Writer, writeNull)
1288 {
1289 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1290 OBJ_CHECK(obj);
1291
1292 ZEND_PARSE_PARAMETERS_NONE();
1293
1294 ION_CHECK(ion_writer_write_null(obj->writer));
1295 }
1296 static ZEND_METHOD(ion_Writer_Writer, writeTypedNull)
1297 {
1298 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1299 OBJ_CHECK(obj);
1300
1301 zend_object *typ_obj;
1302 ZEND_PARSE_PARAMETERS_START(1, 1)
1303 Z_PARAM_OBJ_OF_CLASS(typ_obj, ce_Type)
1304 ZEND_PARSE_PARAMETERS_END();
1305
1306 ION_CHECK(ion_writer_write_typed_null(obj->writer, ion_type_from_enum(typ_obj)));
1307 }
1308 static ZEND_METHOD(ion_Writer_Writer, writeBool)
1309 {
1310 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1311 OBJ_CHECK(obj);
1312
1313 zend_bool b;
1314 ZEND_PARSE_PARAMETERS_START(1, 1)
1315 Z_PARAM_BOOL(b)
1316 ZEND_PARSE_PARAMETERS_END();
1317
1318 ION_CHECK(ion_writer_write_bool(obj->writer, b));
1319 }
1320 static ZEND_METHOD(ion_Writer_Writer, writeInt)
1321 {
1322 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1323 OBJ_CHECK(obj);
1324
1325 zend_long l;
1326 zend_string *s;
1327 ZEND_PARSE_PARAMETERS_START(1, 1)
1328 Z_PARAM_STR_OR_LONG(s, l)
1329 ZEND_PARSE_PARAMETERS_END();
1330
1331 if (s) {
1332 ION_INT *i;
1333 ION_CHECK(ion_int_alloc(obj->writer, &i));
1334 ION_CHECK(ion_int_from_chars(i, s->val, s->len));
1335 ION_CHECK(ion_writer_write_ion_int(obj->writer, i));
1336 ion_int_free(i);
1337 } else {
1338 ION_CHECK(ion_writer_write_int64(obj->writer, l));
1339 }
1340 }
1341 static ZEND_METHOD(ion_Writer_Writer, writeFloat)
1342 {
1343 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1344 OBJ_CHECK(obj);
1345
1346 double d;
1347 ZEND_PARSE_PARAMETERS_START(1, 1)
1348 Z_PARAM_DOUBLE(d)
1349 ZEND_PARSE_PARAMETERS_END();
1350
1351 ION_CHECK(ion_writer_write_double(obj->writer, d));
1352 }
1353 static ZEND_METHOD(ion_Writer_Writer, writeDecimal)
1354 {
1355 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1356 OBJ_CHECK(obj);
1357
1358 zend_object *dec_obj;
1359 zend_string *dec_str;
1360 ZEND_PARSE_PARAMETERS_START(1, 1)
1361 Z_PARAM_OBJ_OF_CLASS_OR_STR(dec_obj, ce_Decimal, dec_str)
1362 ZEND_PARSE_PARAMETERS_END();
1363
1364 if (dec_str) {
1365 decContext *ctx = &php_ion_globals.decimal.ctx;
1366 ION_DECIMAL dec = {0};
1367
1368 if (obj->opt) {
1369 php_ion_writer_options *opt_obj = php_ion_obj(writer_options, obj->opt);
1370 if (opt_obj->opt.decimal_context) {
1371 ctx = opt_obj->opt.decimal_context;
1372 }
1373 }
1374 ION_CHECK(ion_decimal_from_string(&dec, dec_str->val, ctx));
1375 ION_CHECK(ion_writer_write_ion_decimal(obj->writer, &dec));
1376 } else {
1377 php_ion_decimal *dec = php_ion_obj(decimal, dec_obj);
1378 ION_CHECK(ion_writer_write_ion_decimal(obj->writer, &dec->dec));
1379 }
1380 }
1381 static ZEND_METHOD(ion_Writer_Writer, writeTimestamp)
1382 {
1383 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1384 OBJ_CHECK(obj);
1385
1386 zend_object *ts_obj;
1387 zend_string *ts_str;
1388 ZEND_PARSE_PARAMETERS_START(1, 1)
1389 Z_PARAM_OBJ_OF_CLASS_OR_STR(ts_obj, ce_Timestamp, ts_str)
1390 ZEND_PARSE_PARAMETERS_END();
1391
1392 decContext *ctx = NULL;
1393 if (obj->opt) {
1394 ctx = php_ion_obj(reader_options, obj->opt)->opt.decimal_context;
1395 }
1396
1397 ION_TIMESTAMP tmp = {0};
1398 if (ts_str) {
1399 SIZE used;
1400 ION_CHECK(ion_timestamp_parse(&tmp, ts_str->val, ts_str->len, &used, ctx));
1401 } else {
1402 php_ion_timestamp *ts = php_ion_obj(timestamp, ts_obj);
1403 OBJ_CHECK(ts);
1404 ion_timestamp_from_php(&tmp, ts, ctx);
1405 }
1406 ION_CHECK(ion_writer_write_timestamp(obj->writer, &tmp));
1407 }
1408 static ZEND_METHOD(ion_Writer_Writer, writeSymbol)
1409 {
1410 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1411 OBJ_CHECK(obj);
1412
1413 zend_object *sym_obj;
1414 zend_string *sym_str;
1415 ZEND_PARSE_PARAMETERS_START(1, 1)
1416 Z_PARAM_OBJ_OF_CLASS_OR_STR(sym_obj, ce_Symbol, sym_str);
1417 ZEND_PARSE_PARAMETERS_END();
1418
1419 if (sym_str) {
1420 ION_STRING is;
1421 ION_CHECK(ion_writer_write_symbol(obj->writer, ion_string_from_zend(&is, sym_str)));
1422 } else {
1423 php_ion_symbol *sym = php_ion_obj(symbol, sym_obj);
1424 PTR_CHECK(sym);
1425 ION_CHECK(ion_writer_write_ion_symbol(obj->writer, &sym->sym));
1426 }
1427 }
1428 static ZEND_METHOD(ion_Writer_Writer, writeString)
1429 {
1430 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1431 OBJ_CHECK(obj);
1432
1433 zend_string *str;
1434 ZEND_PARSE_PARAMETERS_START(1, 1)
1435 Z_PARAM_STR(str)
1436 ZEND_PARSE_PARAMETERS_END();
1437
1438 ION_STRING is;
1439 ION_CHECK(ion_writer_write_string(obj->writer, ion_string_from_zend(&is, str)));
1440 }
1441 static ZEND_METHOD(ion_Writer_Writer, writeCLob)
1442 {
1443 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1444 OBJ_CHECK(obj);
1445
1446 zend_string *str;
1447 ZEND_PARSE_PARAMETERS_START(1, 1)
1448 Z_PARAM_STR(str)
1449 ZEND_PARSE_PARAMETERS_END();
1450
1451 ION_CHECK(ion_writer_write_clob(obj->writer, (BYTE *) str->val, str->len));
1452 }
1453 static ZEND_METHOD(ion_Writer_Writer, writeBLob)
1454 {
1455 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1456 OBJ_CHECK(obj);
1457
1458 zend_string *str;
1459 ZEND_PARSE_PARAMETERS_START(1, 1)
1460 Z_PARAM_STR(str)
1461 ZEND_PARSE_PARAMETERS_END();
1462
1463 ION_CHECK(ion_writer_write_blob(obj->writer, (BYTE *) str->val, str->len));
1464 }
1465 static ZEND_METHOD(ion_Writer_Writer, startLob)
1466 {
1467 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1468 OBJ_CHECK(obj);
1469
1470 zend_object *typ_obj;
1471 ZEND_PARSE_PARAMETERS_START(1, 1)
1472 Z_PARAM_OBJ_OF_CLASS(typ_obj, ce_Type)
1473 ZEND_PARSE_PARAMETERS_END();
1474
1475 ION_CHECK(ion_writer_start_lob(obj->writer, ion_type_from_enum(typ_obj)));
1476 }
1477 static ZEND_METHOD(ion_Writer_Writer, appendLob)
1478 {
1479 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1480 OBJ_CHECK(obj);
1481
1482 zend_string *str;
1483 ZEND_PARSE_PARAMETERS_START(1, 1)
1484 Z_PARAM_STR(str)
1485 ZEND_PARSE_PARAMETERS_END();
1486
1487 ION_CHECK(ion_writer_append_lob(obj->writer, (BYTE *) str->val, str->len));
1488 }
1489 static ZEND_METHOD(ion_Writer_Writer, finishLob)
1490 {
1491 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1492 OBJ_CHECK(obj);
1493
1494 ZEND_PARSE_PARAMETERS_NONE();
1495
1496 ION_CHECK(ion_writer_finish_lob(obj->writer));
1497 }
1498 static ZEND_METHOD(ion_Writer_Writer, startContainer)
1499 {
1500 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1501 OBJ_CHECK(obj);
1502
1503 zend_object *typ_obj;
1504 ZEND_PARSE_PARAMETERS_START(1, 1)
1505 Z_PARAM_OBJ_OF_CLASS(typ_obj, ce_Type)
1506 ZEND_PARSE_PARAMETERS_END();
1507
1508 ION_CHECK(ion_writer_start_container(obj->writer, ion_type_from_enum(typ_obj)));
1509 }
1510 static ZEND_METHOD(ion_Writer_Writer, finishContainer)
1511 {
1512 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1513 OBJ_CHECK(obj);
1514
1515 ZEND_PARSE_PARAMETERS_NONE();
1516
1517 ION_CHECK(ion_writer_finish_container(obj->writer));
1518 }
1519 static ZEND_METHOD(ion_Writer_Writer, writeFieldName)
1520 {
1521 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1522 OBJ_CHECK(obj);
1523
1524 zend_object *sym_obj;
1525 zend_string *sym_str;
1526 ZEND_PARSE_PARAMETERS_START(1, 1)
1527 Z_PARAM_OBJ_OF_CLASS_OR_STR(sym_obj, ce_Symbol, sym_str);
1528 ZEND_PARSE_PARAMETERS_END();
1529
1530 if (sym_str) {
1531 ION_STRING is;
1532 ION_CHECK(ion_writer_write_field_name(obj->writer, ion_string_from_zend(&is, sym_str)));
1533 } else {
1534 php_ion_symbol *sym = php_ion_obj(symbol, sym_obj);
1535 PTR_CHECK(sym);
1536 ION_CHECK(ion_writer_write_field_name_symbol(obj->writer, &sym->sym));
1537 }
1538 }
1539 static ZEND_METHOD(ion_Writer_Writer, writeAnnotation)
1540 {
1541 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1542 OBJ_CHECK(obj);
1543
1544 zval *args;
1545 unsigned argc;
1546 ZEND_PARSE_PARAMETERS_START(1, -1)
1547 Z_PARAM_VARIADIC('+', args, argc);
1548 ZEND_PARSE_PARAMETERS_END();
1549
1550 for (unsigned i = 0; i < argc; ++i) {
1551 switch (Z_TYPE(args[i])) {
1552 case IS_STRING: ;
1553 ION_STRING is;
1554 ION_CHECK(ion_writer_add_annotation(obj->writer, ion_string_from_zend(&is, Z_STR(args[i]))));
1555 break;
1556
1557 case IS_OBJECT:
1558 ION_CHECK(ion_writer_add_annotation_symbol(obj->writer, &php_ion_obj(symbol, Z_OBJ(args[i]))->sym));
1559 break;
1560 }
1561 }
1562 }
1563 static ZEND_METHOD(ion_Writer_Writer, getDepth)
1564 {
1565 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1566 OBJ_CHECK(obj);
1567
1568 ZEND_PARSE_PARAMETERS_NONE();
1569
1570 SIZE depth;
1571 ION_CHECK(ion_writer_get_depth(obj->writer, &depth));
1572 RETURN_LONG(depth);
1573 }
1574 static ZEND_METHOD(ion_Writer_Writer, flush)
1575 {
1576 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1577 OBJ_CHECK(obj);
1578
1579 ZEND_PARSE_PARAMETERS_NONE();
1580
1581 SIZE flushed;
1582 ION_CHECK(ion_writer_flush(obj->writer, &flushed));
1583 RETURN_LONG(flushed);
1584 }
1585 static ZEND_METHOD(ion_Writer_Writer, finish)
1586 {
1587 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1588 OBJ_CHECK(obj);
1589
1590 ZEND_PARSE_PARAMETERS_NONE();
1591
1592 SIZE flushed;
1593 ION_CHECK(ion_writer_finish(obj->writer, &flushed));
1594 RETURN_LONG(flushed);
1595 }
1596 static ZEND_METHOD(ion_Writer_Buffer_Writer, __construct)
1597 {
1598 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1599 PTR_CHECK(obj);
1600
1601 ZEND_PARSE_PARAMETERS_START(0, 1)
1602 Z_PARAM_OPTIONAL
1603 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Writer_Options)
1604 ZEND_PARSE_PARAMETERS_END();
1605
1606 obj->type = BUFFER_WRITER;
1607 php_ion_writer_ctor(obj);
1608 }
1609 static ZEND_METHOD(ion_Writer_Buffer_Writer, getBuffer)
1610 {
1611 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1612 OBJ_CHECK(obj);
1613
1614 ZEND_PARSE_PARAMETERS_NONE();
1615
1616 RETVAL_STR(php_ion_writer_buffer_copy(obj));
1617 }
1618 static ZEND_METHOD(ion_Writer_Buffer_Writer, resetBuffer)
1619 {
1620 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1621 OBJ_CHECK(obj);
1622
1623 ZEND_PARSE_PARAMETERS_NONE();
1624
1625 php_ion_writer_buffer_reset(obj);
1626 }
1627 static ZEND_METHOD(ion_Writer_Stream_Writer, __construct)
1628 {
1629 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1630 PTR_CHECK(obj);
1631
1632 zval *zstream;
1633 ZEND_PARSE_PARAMETERS_START(1, 2)
1634 Z_PARAM_RESOURCE(zstream)
1635 Z_PARAM_OPTIONAL
1636 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Writer_Options)
1637 ZEND_PARSE_PARAMETERS_END();
1638
1639 obj->type = STREAM_WRITER;
1640 php_stream_from_zval_no_verify(obj->stream.ptr, zstream);
1641
1642 php_ion_writer_ctor(obj);
1643 }
1644 static ZEND_METHOD(ion_Writer_Stream_Writer, getStream)
1645 {
1646 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1647 OBJ_CHECK(obj);
1648
1649 ZEND_PARSE_PARAMETERS_NONE();
1650 PTR_CHECK(obj->stream.ptr);
1651
1652 GC_ADDREF(obj->stream.ptr->res);
1653 RETURN_RES(obj->stream.ptr->res);
1654 }
1655
1656 #define Z_PARAM_OBJ_OF_CLASS_OR_NAMED_OR_NULL(obj_dest, iface_ce, impl_ce, arr_dest) \
1657 Z_PARAM_PROLOGUE(0,0) \
1658 if (!zend_parse_arg_obj(_arg, &(obj_dest), iface_ce, true)) { \
1659 if (UNEXPECTED(!zend_parse_arg_array(_arg, &(arr_dest), true, 0))) { \
1660 _error = (iface_ce)->name->val; \
1661 _error_code = ZPP_ERROR_WRONG_CLASS_OR_NULL; \
1662 break; \
1663 } \
1664 zval tmp; \
1665 object_init_ex(&tmp, impl_ce); \
1666 *(&(obj_dest)) = Z_OBJ(tmp); \
1667 zend_call_known_function((impl_ce)->constructor, obj_dest, impl_ce, \
1668 NULL, 0, NULL, Z_ARRVAL_P(arr_dest)); \
1669 if (EG(exception)) { \
1670 OBJ_RELEASE(obj_dest); \
1671 return; \
1672 } \
1673 }
1674
1675 static ZEND_METHOD(ion_Serializer_PHP, __construct)
1676 {
1677 php_ion_serializer_php *obj = php_ion_obj(serializer_php, Z_OBJ_P(ZEND_THIS));
1678 PTR_CHECK(obj);
1679
1680 obj->serializer.call_magic = true;
1681
1682 zval *za_opt = NULL;
1683 ZEND_PARSE_PARAMETERS_START(0, 4)
1684 Z_PARAM_OPTIONAL
1685 Z_PARAM_OBJ_OF_CLASS_OR_NAMED_OR_NULL(obj->opt, ce_Writer_Options, ce_Writer_Options, za_opt)
1686 Z_PARAM_BOOL(obj->serializer.multi_seq)
1687 Z_PARAM_BOOL(obj->serializer.call_magic)
1688 Z_PARAM_STR_OR_NULL(obj->serializer.call_custom)
1689 ZEND_PARSE_PARAMETERS_END();
1690
1691 php_ion_serializer_php_ctor(obj);
1692 if (za_opt) {
1693 OBJ_RELEASE(obj->opt);
1694 }
1695 }
1696 static ZEND_METHOD(ion_Serializer_PHP, serialize)
1697 {
1698 zend_object *obj = Z_OBJ_P(ZEND_THIS);
1699
1700 zval *data;
1701 ZEND_PARSE_PARAMETERS_START(1, 1)
1702 Z_PARAM_ZVAL(data)
1703 ZEND_PARSE_PARAMETERS_END();
1704
1705 php_ion_serialize(&php_ion_obj(serializer_php, obj)->serializer, data, return_value);
1706 }
1707 static ZEND_FUNCTION(ion_serialize)
1708 {
1709 zval *data, *za_ser = NULL;
1710 zend_object *zo_ser = NULL;
1711 ZEND_PARSE_PARAMETERS_START(1, 2)
1712 Z_PARAM_ZVAL(data)
1713 Z_PARAM_OPTIONAL
1714 Z_PARAM_OBJ_OF_CLASS_OR_NAMED_OR_NULL(zo_ser, ce_Serializer, ce_Serializer_PHP, za_ser)
1715 ZEND_PARSE_PARAMETERS_END();
1716
1717 if (!zo_ser || zo_ser->ce == ce_Serializer_PHP) {
1718 // default, fast path
1719 php_ion_serializer *ser = zo_ser ? &php_ion_obj(serializer_php, zo_ser)->serializer : NULL;
1720 php_ion_serialize(ser, data, return_value);
1721 } else {
1722 zend_call_method_with_1_params(zo_ser, NULL, NULL, "serialize", return_value, data);
1723 }
1724
1725 if (za_ser) {
1726 OBJ_RELEASE(zo_ser);
1727 }
1728 }
1729
1730 static ZEND_METHOD(ion_Unserializer_PHP, __construct)
1731 {
1732 php_ion_unserializer_php *obj = php_ion_obj(unserializer_php, Z_OBJ_P(ZEND_THIS));
1733 PTR_CHECK(obj);
1734
1735 obj->unserializer.call_magic = true;
1736
1737 zval *za_opt = NULL;
1738 ZEND_PARSE_PARAMETERS_START(0, 4)
1739 Z_PARAM_OPTIONAL
1740 Z_PARAM_OBJ_OF_CLASS_OR_NAMED_OR_NULL(obj->opt, ce_Reader_Options, ce_Reader_Options, za_opt)
1741 Z_PARAM_BOOL(obj->unserializer.multi_seq)
1742 Z_PARAM_BOOL(obj->unserializer.call_magic)
1743 Z_PARAM_STR_OR_NULL(obj->unserializer.call_custom)
1744 ZEND_PARSE_PARAMETERS_END();
1745
1746 php_ion_unserializer_php_ctor(obj);
1747 if (za_opt) {
1748 OBJ_RELEASE(obj->opt);
1749 }
1750 }
1751 static ZEND_METHOD(ion_Unserializer_PHP, unserialize)
1752 {
1753 zend_object *obj = Z_OBJ_P(ZEND_THIS);
1754
1755 zval *data;
1756 ZEND_PARSE_PARAMETERS_START(1, 1)
1757 Z_PARAM_ZVAL(data)
1758 ZEND_PARSE_PARAMETERS_END();
1759
1760 php_ion_unserialize(&php_ion_obj(unserializer_php, obj)->unserializer, data, return_value);
1761 }
1762 static ZEND_FUNCTION(ion_unserialize)
1763 {
1764 zval *data, *za_ser = NULL;
1765 zend_object *zo_ser = NULL;
1766 ZEND_PARSE_PARAMETERS_START(1, 2)
1767 Z_PARAM_ZVAL(data)
1768 Z_PARAM_OPTIONAL
1769 Z_PARAM_OBJ_OF_CLASS_OR_NAMED_OR_NULL(zo_ser, ce_Unserializer, ce_Unserializer_PHP, za_ser)
1770 ZEND_PARSE_PARAMETERS_END();
1771
1772 if (!zo_ser || zo_ser->ce == ce_Unserializer_PHP) {
1773 // default, fast path
1774 php_ion_unserializer *ser = zo_ser ? &php_ion_obj(unserializer_php, zo_ser)->unserializer : NULL;
1775 php_ion_unserialize(ser, data, return_value);
1776 } else {
1777 zend_call_method_with_1_params(zo_ser, NULL, NULL, "unserialize", return_value, data);
1778 }
1779
1780 if (za_ser) {
1781 OBJ_RELEASE(zo_ser);
1782 }
1783 }
1784
1785 PHP_RINIT_FUNCTION(ion)
1786 {
1787 #if defined(ZTS) && defined(COMPILE_DL_ION)
1788 ZEND_TSRMLS_CACHE_UPDATE();
1789 #endif
1790
1791 php_ion_globals_symbols_init();
1792 php_ion_globals_serializer_init();
1793 php_ion_globals_unserializer_init();
1794 return SUCCESS;
1795 }
1796
1797 PHP_RSHUTDOWN_FUNCTION(ion)
1798 {
1799 php_ion_globals_unserializer_dtor();
1800 php_ion_globals_serializer_dtor();
1801 php_ion_globals_symbols_dtor();
1802 return SUCCESS;
1803 }
1804
1805 #define ZEND_ARG_VARIADIC_OBJ_TYPE_MASK(pass_by_ref, name, classname, type_mask, default_value) \
1806 { #name, ZEND_TYPE_INIT_CLASS_CONST_MASK(#classname, ((type_mask) | _ZEND_ARG_INFO_FLAGS(pass_by_ref, 1, 0))), default_value },
1807 #include "ion_arginfo.h"
1808
1809 // spl_iterators.h includes ext/pcre/php_pcre.h which might not find pcre2.h
1810 extern PHPAPI zend_class_entry *spl_ce_RecursiveIterator;
1811
1812 PHP_MINIT_FUNCTION(ion)
1813 {
1814 // true globals
1815 if (SUCCESS != g_sym_init()) {
1816 return FAILURE;
1817 }
1818 g_intern_str_init();
1819
1820 // Catalog
1821 php_ion_register(catalog, Catalog, zend_ce_countable);
1822
1823 // Decimal
1824 php_ion_register(decimal, Decimal);
1825 php_ion_register(decimal_ctx, Decimal_Context);
1826 ce_Decimal_Context_Rounding = register_class_ion_Decimal_Context_Rounding();
1827
1828 // Exception
1829 ce_Exception = register_class_ion_Exception(zend_ce_exception);
1830
1831 // LOB
1832 ce_LOB = register_class_ion_LOB();
1833
1834 // Reader
1835 ce_Reader = register_class_ion_Reader(spl_ce_RecursiveIterator);
1836 php_ion_register(reader_options, Reader_Options);
1837 php_ion_register(reader, Reader_Reader, ce_Reader);
1838 ce_Reader_Buffer = register_class_ion_Reader_Buffer(ce_Reader);
1839 ce_Reader_Buffer_Reader = register_class_ion_Reader_Buffer_Reader(ce_Reader_Reader, ce_Reader_Buffer);
1840 ce_Reader_Stream = register_class_ion_Reader_Stream(ce_Reader);
1841 ce_Reader_Stream_Reader = register_class_ion_Reader_Stream_Reader(ce_Reader_Reader, ce_Reader_Stream);
1842
1843 // Serializer
1844 ce_Serializer = register_class_ion_Serializer();
1845 php_ion_register(serializer_php, Serializer_PHP, ce_Serializer);
1846
1847 // Symbol
1848 php_ion_register(symbol, Symbol);
1849 oh_Symbol.compare = php_ion_symbol_zval_compare;
1850 php_ion_register(symbol_iloc, Symbol_ImportLocation);
1851 php_ion_register(symbol_table, Symbol_Table);
1852 ce_Symbol_Table->create_object = NULL;
1853 ce_Symbol_Table_Local = register_class_ion_Symbol_Table_Local(ce_Symbol_Table);
1854 ce_Symbol_Table_Local->create_object = create_ion_Symbol_Table;
1855 ce_Symbol_Table_Shared = register_class_ion_Symbol_Table_Shared(ce_Symbol_Table);
1856 ce_Symbol_Table_Shared->create_object = create_ion_Symbol_Table;
1857 ce_Symbol_Enum = register_class_ion_Symbol_Enum();
1858 ce_Symbol_Table_System = register_class_ion_Symbol_System(ce_Symbol_Enum);
1859 ce_Symbol_Table_PHP = register_class_ion_Symbol_PHP(ce_Symbol_Enum);
1860
1861 // Timestamp
1862 ce_Timestamp = register_class_ion_Timestamp(php_date_get_date_ce());
1863 ce_Timestamp_Format = register_class_ion_Timestamp_Format();
1864 ce_Timestamp_Precision = register_class_ion_Timestamp_Precision();
1865
1866 // Type
1867 ce_Type = register_class_ion_Type();
1868
1869 // Writer
1870 ce_Writer = register_class_ion_Writer();
1871 php_ion_register(writer_options, Writer_Options);
1872 php_ion_register(writer, Writer_Writer, ce_Writer);
1873 ce_Writer_Buffer = register_class_ion_Writer_Buffer(ce_Writer);
1874 ce_Writer_Buffer_Writer = register_class_ion_Writer_Buffer_Writer(ce_Writer_Writer, ce_Writer_Buffer);
1875 ce_Writer_Stream = register_class_ion_Writer_Stream(ce_Writer);
1876 ce_Writer_Stream_Writer = register_class_ion_Writer_Stream_Writer(ce_Writer_Writer, ce_Writer_Stream);
1877
1878 // Unserializer
1879 ce_Unserializer = register_class_ion_Unserializer();
1880 php_ion_register(unserializer_php, Unserializer_PHP, ce_Unserializer);
1881
1882 return SUCCESS;
1883 }
1884
1885 PHP_MSHUTDOWN_FUNCTION(ion)
1886 {
1887 if (g_sym_tab_php) {
1888 ion_symbol_table_close(g_sym_tab_php);
1889 }
1890 zend_hash_destroy(&g_sym_hash);
1891 return SUCCESS;
1892 }
1893
1894 PHP_MINFO_FUNCTION(ion)
1895 {
1896 php_info_print_table_start();
1897 php_info_print_table_header(2, "ion support", "enabled");
1898 php_info_print_table_row(2, "ion version", PHP_ION_VERSION);
1899 php_info_print_table_row(2, "ion-c version", "unknown");
1900 php_info_print_table_end();
1901 }
1902
1903 PHP_GINIT_FUNCTION(ion)
1904 {
1905 memset(ion_globals, 0, sizeof(*ion_globals));
1906
1907 php_ion_decimal_ctx_init_max(&ion_globals->decimal.ctx, DEC_ROUND_HALF_EVEN);
1908 php_ion_decimal_from_zend_long(&ion_globals->decimal.zend_max, &ion_globals->decimal.ctx, ZEND_LONG_MAX);
1909 php_ion_decimal_from_zend_long(&ion_globals->decimal.zend_min, &ion_globals->decimal.ctx, ZEND_LONG_MIN);
1910 }
1911
1912 PHP_GSHUTDOWN_FUNCTION(ion)
1913 {
1914 }
1915
1916 static zend_module_dep ion_module_deps[] = {
1917 ZEND_MOD_REQUIRED("date")
1918 ZEND_MOD_REQUIRED("spl")
1919 ZEND_MOD_END
1920 };
1921
1922 zend_module_entry ion_module_entry = {
1923 STANDARD_MODULE_HEADER_EX,
1924 NULL,
1925 ion_module_deps,
1926 "ion", /* Extension name */
1927 ext_functions, /* zend_function_entry */
1928 PHP_MINIT(ion), /* PHP_MINIT - Module initialization */
1929 PHP_MSHUTDOWN(ion), /* PHP_MSHUTDOWN - Module shutdown */
1930 PHP_RINIT(ion), /* PHP_RINIT - Request initialization */
1931 PHP_RSHUTDOWN(ion), /* PHP_RSHUTDOWN - Request shutdown */
1932 PHP_MINFO(ion), /* PHP_MINFO - Module info */
1933 PHP_ION_VERSION, /* Version */
1934 ZEND_MODULE_GLOBALS(ion),
1935 PHP_GINIT(ion), /* PHP_GINIT */
1936 PHP_GSHUTDOWN(ion), /* PHP_GSHUTDOWN */
1937 NULL,
1938 STANDARD_MODULE_PROPERTIES_EX
1939 };
1940
1941 #ifdef COMPILE_DL_ION
1942 # ifdef ZTS
1943 ZEND_TSRMLS_CACHE_DEFINE()
1944 # endif
1945 ZEND_GET_MODULE(ion)
1946 #endif