docker: fix build without submodule
[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_FUNCTION(ion_Symbol_Table_System)
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_FUNCTION(ion_Symbol_Table_PHP)
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;
414 zval *tz = NULL;
415 ZEND_PARSE_PARAMETERS_START(1, 4)
416 Z_PARAM_OBJ_OF_CLASS_OR_LONG(precision_obj, ce_Timestamp_Precision, precision)
417 Z_PARAM_OPTIONAL
418 Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(format_obj, ce_Timestamp_Format, fmt)
419 Z_PARAM_STR_OR_NULL(dt)
420 Z_PARAM_ZVAL(tz)
421 ZEND_PARSE_PARAMETERS_END();
422
423 if (precision_obj) {
424 precision = Z_LVAL_P(zend_enum_fetch_case_value(precision_obj));
425 }
426 if (format_obj) {
427 fmt = Z_STR_P(zend_enum_fetch_case_value(format_obj));
428 }
429 php_ion_timestamp_ctor(obj, precision, fmt, dt, tz);
430 }
431 static ZEND_METHOD(ion_Timestamp, __toString)
432 {
433 php_ion_timestamp *obj = php_ion_obj(timestamp, Z_OBJ_P(ZEND_THIS));
434 OBJ_CHECK(obj);
435
436 ZEND_PARSE_PARAMETERS_NONE();
437
438 zval fmt;
439 ZVAL_NULL(&fmt);
440 zend_call_method_with_1_params(&obj->std, obj->std.ce, NULL, "format", return_value,
441 zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("format"), 0, &fmt));
442 }
443 static ZEND_METHOD(ion_Decimal_Context, __construct)
444 {
445 php_ion_decimal_ctx *obj = php_ion_obj(decimal_ctx, Z_OBJ_P(ZEND_THIS));
446 PTR_CHECK(obj);
447
448 zend_bool clamp;
449 zend_object *o_round = NULL;
450 zend_long digits, emax, emin, round;
451 ZEND_PARSE_PARAMETERS_START(5, 5)
452 Z_PARAM_LONG(digits)
453 Z_PARAM_LONG(emax)
454 Z_PARAM_LONG(emin)
455 Z_PARAM_OBJ_OF_CLASS_OR_LONG(o_round, ce_Decimal_Context_Rounding, round)
456 Z_PARAM_BOOL(clamp)
457 ZEND_PARSE_PARAMETERS_END();
458
459 if (o_round) {
460 round = Z_LVAL_P(zend_enum_fetch_case_value(o_round));
461 }
462 php_ion_decimal_ctx_init(&obj->ctx, digits, emax, emin, round, clamp); // NOLINT(cppcoreguidelines-narrowing-conversions)
463 php_ion_decimal_ctx_ctor(obj, o_round);
464 }
465 static inline void make_decimal_ctx(INTERNAL_FUNCTION_PARAMETERS, int kind)
466 {
467 ZEND_PARSE_PARAMETERS_NONE();
468
469 object_init_ex(return_value, ce_Decimal_Context);
470 php_ion_decimal_ctx *obj = php_ion_obj(decimal_ctx, Z_OBJ_P(return_value));
471 decContextDefault(&obj->ctx, kind);
472 php_ion_decimal_ctx_ctor(obj, NULL);
473 }
474 static ZEND_METHOD(ion_Decimal_Context, Dec32)
475 {
476 make_decimal_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, DEC_INIT_DECIMAL32);
477 }
478 static ZEND_METHOD(ion_Decimal_Context, Dec64)
479 {
480 make_decimal_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, DEC_INIT_DECIMAL64);
481 }
482 static ZEND_METHOD(ion_Decimal_Context, Dec128)
483 {
484 make_decimal_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, DEC_INIT_DECIMAL128);
485 }
486 static ZEND_METHOD(ion_Decimal_Context, DecMax)
487 {
488 zend_object *o_round = NULL;
489 zend_long round = DEC_ROUND_HALF_EVEN;
490 ZEND_PARSE_PARAMETERS_START(0, 1)
491 Z_PARAM_OPTIONAL
492 Z_PARAM_OBJ_OF_CLASS_OR_LONG(o_round, ce_Decimal_Context_Rounding, round)
493 ZEND_PARSE_PARAMETERS_END();
494
495 if (o_round) {
496 round = Z_LVAL_P(zend_enum_fetch_case_value(o_round));
497 }
498 object_init_ex(return_value, ce_Decimal_Context);
499 php_ion_decimal_ctx *obj = php_ion_obj(decimal_ctx, Z_OBJ_P(return_value));
500 php_ion_decimal_ctx_init_max(&obj->ctx, round);
501 php_ion_decimal_ctx_ctor(obj, o_round);
502 }
503 static ZEND_METHOD(ion_Decimal, __construct)
504 {
505 php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS));
506 PTR_CHECK(obj);
507
508 zend_long num;
509 zend_string *zstr;
510 ZEND_PARSE_PARAMETERS_START(1, 2)
511 Z_PARAM_STR_OR_LONG(zstr, num)
512 Z_PARAM_OPTIONAL
513 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->ctx, ce_Decimal_Context)
514 ZEND_PARSE_PARAMETERS_END();
515
516 if (obj->ctx) {
517 GC_ADDREF(obj->ctx);
518 } else {
519 zval zdc;
520 object_init_ex(&zdc, ce_Decimal_Context);
521 obj->ctx = Z_OBJ(zdc);
522 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx, obj->ctx), NULL);
523 }
524
525 decContext *ctx = &php_ion_obj(decimal_ctx, obj->ctx)->ctx;
526
527 if (zstr) {
528 ION_CHECK(ion_decimal_from_string(&obj->dec, zstr->val, ctx), OBJ_RELEASE(obj->ctx));
529 } else {
530 php_ion_decimal_from_zend_long(&obj->dec, ctx, num);
531 }
532
533 php_ion_decimal_ctor(obj);
534 OBJ_RELEASE(obj->ctx);
535 }
536 static ZEND_METHOD(ion_Decimal, equals)
537 {
538 php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS));
539 PTR_CHECK(obj);
540
541 zend_object *dec_obj;
542 ZEND_PARSE_PARAMETERS_START(1, 1)
543 Z_PARAM_OBJ_OF_CLASS(dec_obj, ce_Decimal)
544 ZEND_PARSE_PARAMETERS_END();
545
546 BOOL is = FALSE;
547 ION_CHECK(ion_decimal_equals(&obj->dec, &php_ion_obj(decimal, dec_obj)->dec,
548 obj->ctx ? &php_ion_obj(decimal_ctx, obj->ctx)->ctx : NULL, &is));
549 RETURN_BOOL(is);
550 }
551 static ZEND_METHOD(ion_Decimal, __toString)
552 {
553 php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS));
554 PTR_CHECK(obj);
555
556 ZEND_PARSE_PARAMETERS_NONE();
557
558 RETURN_STR(php_ion_decimal_to_string(&obj->dec));
559 }
560 static ZEND_METHOD(ion_Decimal, toInt)
561 {
562 php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS));
563 PTR_CHECK(obj);
564
565 ZEND_PARSE_PARAMETERS_NONE();
566
567 zend_long l;
568 php_ion_decimal_to_zend_long(&obj->dec, &php_ion_obj(decimal_ctx, obj->ctx)->ctx, &l);
569 RETURN_LONG(l);
570 }
571 static ZEND_METHOD(ion_Decimal, isInt)
572 {
573 php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS));
574 PTR_CHECK(obj);
575
576 ZEND_PARSE_PARAMETERS_NONE();
577
578 RETURN_BOOL(ion_decimal_is_integer(&obj->dec));
579 }
580 static ZEND_METHOD(ion_LOB, __construct)
581 {
582 zend_string *value;
583 zend_object *type = NULL;
584 ZEND_PARSE_PARAMETERS_START(1, 2)
585 Z_PARAM_STR(value)
586 Z_PARAM_OPTIONAL
587 Z_PARAM_OBJ_OF_CLASS(type, ce_Type)
588 ZEND_PARSE_PARAMETERS_END();
589
590 if (!type) {
591 type = zend_enum_get_case_cstr(ce_Type, "CLob");
592 }
593 update_property_obj(Z_OBJ_P(ZEND_THIS), ZEND_STRL("type"), type);
594 zend_update_property_str(Z_OBJCE_P(ZEND_THIS), Z_OBJ_P(ZEND_THIS), ZEND_STRL("value"), value);
595 }
596 static ZEND_METHOD(ion_Reader_Options, __construct)
597 {
598 php_ion_reader_options *opt = php_ion_obj(reader_options, Z_OBJ_P(ZEND_THIS));
599 zend_bool ret_sys_val = false, skip_validation = false;
600 zend_long max_depth = 10, max_ann = 10, ann_buf_siz = 0x4000, tmp_buf_siz = 0x4000;
601
602 PTR_CHECK(opt);
603
604 ZEND_PARSE_PARAMETERS_START(0, 9)
605 Z_PARAM_OPTIONAL
606 // public readonly ?\ion\Catalog $catalog = null,
607 Z_PARAM_OBJ_OF_CLASS_OR_NULL(opt->cat, ce_Catalog)
608 // public readonly ?\ion\Decimal\Context $decimalContext = null,
609 Z_PARAM_OBJ_OF_CLASS_OR_NULL(opt->dec_ctx, ce_Decimal_Context)
610 // public readonly ?\Closure $onContextChange = null,
611 Z_PARAM_OBJ_OF_CLASS_OR_NULL(opt->cb, zend_ce_closure);
612 // public readonly bool $returnSystemValues = false,
613 Z_PARAM_BOOL(ret_sys_val)
614 // public readonly int $maxContainerDepth = 10,
615 Z_PARAM_LONG(max_depth)
616 // public readonly int $maxAnnotations = 10,
617 Z_PARAM_LONG(max_ann)
618 // public readonly int $annotationBufferSize = 0x4000,
619 Z_PARAM_LONG(ann_buf_siz)
620 // public readonly int $tempBufferSize = 0x4000,
621 Z_PARAM_LONG(tmp_buf_siz)
622 // public readonly bool $skipCharacterValidation = false,
623 Z_PARAM_BOOL(skip_validation)
624 ZEND_PARSE_PARAMETERS_END();
625
626 if (opt->cb) {
627 zval zcb;
628 ZVAL_OBJ(&zcb, opt->cb);
629 zend_fcall_info_init(&zcb, 0, &opt->ccn.fci, &opt->ccn.fcc, NULL, NULL);
630 opt->opt.context_change_notifier.context = &opt->ccn;
631 update_property_obj(&opt->std, ZEND_STRL("onContextChange"), opt->cb);
632 } else {
633 zend_update_property_null(NULL, &opt->std, ZEND_STRL("onContextChange"));
634 }
635 if (opt->cat) {
636 update_property_obj(&opt->std, ZEND_STRL("catalog"), opt->cat);
637 opt->opt.pcatalog = php_ion_obj(catalog, opt->cat)->cat;
638 } else {
639 zend_update_property_null(NULL, &opt->std, ZEND_STRL("catalog"));
640 }
641 if (opt->dec_ctx) {
642 update_property_obj(&opt->std, ZEND_STRL("decimalContext"), opt->dec_ctx);
643 opt->opt.decimal_context = &php_ion_obj(decimal_ctx, opt->dec_ctx)->ctx;
644 } else {
645 zend_update_property_null(NULL, &opt->std, ZEND_STRL("decimalContext"));
646 }
647 zend_update_property_bool(opt->std.ce, &opt->std, ZEND_STRL("returnSystemValues"),
648 opt->opt.return_system_values = ret_sys_val);
649 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("maxContainerDepth"),
650 opt->opt.max_container_depth = (SIZE) max_depth);
651 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("maxAnnotations"),
652 opt->opt.max_annotation_count = (SIZE) max_ann);
653 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("annotationBufferSize"),
654 opt->opt.max_annotation_buffered = (SIZE) ann_buf_siz);
655 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("tempBufferSize"),
656 opt->opt.chunk_threshold = opt->opt.user_value_threshold = opt->opt.symbol_threshold = (SIZE) tmp_buf_siz);
657 zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("skipCharacterValidation"),
658 opt->opt.skip_character_validation = (SIZE) skip_validation);
659 }
660 static ZEND_METHOD(ion_Reader_Reader, hasChildren)
661 {
662 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
663
664 OBJ_CHECK(obj);
665 ZEND_PARSE_PARAMETERS_NONE();
666
667 ION_TYPE t;
668 ION_CHECK(ion_reader_get_type(obj->reader, &t));
669 switch (ION_TYPE_INT(t)) {
670 case tid_LIST_INT:
671 case tid_SEXP_INT:
672 case tid_STRUCT_INT:
673 RETVAL_TRUE;
674 break;
675 default:
676 RETVAL_FALSE;
677 break;
678 }
679 }
680 static ZEND_METHOD(ion_Reader_Reader, getChildren)
681 {
682 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
683 OBJ_CHECK(obj);
684
685 ZEND_PARSE_PARAMETERS_NONE();
686
687 ION_CHECK(ion_reader_step_in(obj->reader));
688
689 RETURN_ZVAL(ZEND_THIS, 1, 0);
690 }
691 static ZEND_METHOD(ion_Reader_Reader, rewind)
692 {
693 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
694 OBJ_CHECK(obj);
695
696 ZEND_PARSE_PARAMETERS_NONE();
697
698 ION_CHECK(ion_reader_next(obj->reader, &obj->state));
699 }
700 static ZEND_METHOD(ion_Reader_Reader, next)
701 {
702 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
703 OBJ_CHECK(obj);
704
705 ZEND_PARSE_PARAMETERS_NONE();
706
707 if (obj->state == tid_EOF) {
708 SIZE depth = 0;
709 ION_CHECK(ion_reader_get_depth(obj->reader, &depth));
710 if (depth) {
711 ION_CHECK(ion_reader_step_out(obj->reader));
712 }
713 }
714 ION_CHECK(ion_reader_next(obj->reader, &obj->state));
715 }
716 static ZEND_METHOD(ion_Reader_Reader, valid)
717 {
718 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
719 OBJ_CHECK(obj);
720
721 ZEND_PARSE_PARAMETERS_NONE();
722 RETURN_BOOL(obj->state != tid_none && obj->state != tid_EOF);
723 }
724 static ZEND_METHOD(ion_Reader_Reader, key)
725 {
726 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
727 OBJ_CHECK(obj);
728
729 ZEND_PARSE_PARAMETERS_NONE();
730 RETURN_IONTYPE(obj->state);
731 }
732 static ZEND_METHOD(ion_Reader_Reader, current)
733 {
734 ZEND_PARSE_PARAMETERS_NONE();
735 RETURN_ZVAL(ZEND_THIS, 1, 0);
736 }
737 static ZEND_METHOD(ion_Reader_Reader, getType)
738 {
739 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
740 OBJ_CHECK(obj);
741
742 ZEND_PARSE_PARAMETERS_NONE();
743
744 ION_TYPE typ;
745 ION_CHECK(ion_reader_get_type(obj->reader, &typ));
746 RETURN_IONTYPE(typ);
747 }
748 static ZEND_METHOD(ion_Reader_Reader, hasAnnotations)
749 {
750 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
751 OBJ_CHECK(obj);
752
753 ZEND_PARSE_PARAMETERS_NONE();
754
755 BOOL has = FALSE;
756 ION_CHECK(ion_reader_has_any_annotations(obj->reader, &has));
757 RETURN_BOOL(has);
758 }
759 static ZEND_METHOD(ion_Reader_Reader, hasAnnotation)
760 {
761 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
762 OBJ_CHECK(obj);
763
764 zend_string *ann_zstr;
765 ZEND_PARSE_PARAMETERS_START(1, 1);
766 Z_PARAM_STR(ann_zstr);
767 ZEND_PARSE_PARAMETERS_END();
768
769 ION_STRING ann_istr;
770 BOOL has = FALSE;
771 ION_CHECK(ion_reader_has_annotation(obj->reader, ion_string_from_zend(&ann_istr, ann_zstr), &has));
772 RETURN_BOOL(has);
773 }
774 static ZEND_METHOD(ion_Reader_Reader, isNull)
775 {
776 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
777 OBJ_CHECK(obj);
778
779 ZEND_PARSE_PARAMETERS_NONE();
780
781 BOOL is = FALSE;
782 ION_CHECK(ion_reader_is_null(obj->reader, &is));
783 RETURN_BOOL(is);
784 }
785 static ZEND_METHOD(ion_Reader_Reader, isInStruct)
786 {
787 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
788 OBJ_CHECK(obj);
789
790 ZEND_PARSE_PARAMETERS_NONE();
791
792 BOOL is = FALSE;
793 ION_CHECK(ion_reader_is_in_struct(obj->reader, &is));
794 RETURN_BOOL(is);
795 }
796 static ZEND_METHOD(ion_Reader_Reader, getFieldName)
797 {
798 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
799 OBJ_CHECK(obj);
800
801 ZEND_PARSE_PARAMETERS_NONE();
802
803 ION_STRING name;
804 ION_CHECK(ion_reader_get_field_name(obj->reader, &name));
805 RETURN_STRINGL((char *) name.value, name.length);
806 }
807 static ZEND_METHOD(ion_Reader_Reader, getFieldNameSymbol)
808 {
809 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
810 OBJ_CHECK(obj);
811
812 ZEND_PARSE_PARAMETERS_NONE();
813
814 ION_SYMBOL *sym_ptr;
815 ION_CHECK(ion_reader_get_field_name_symbol(obj->reader, &sym_ptr));
816
817 php_ion_symbol_zval(sym_ptr, return_value);
818 }
819 static ZEND_METHOD(ion_Reader_Reader, getAnnotations)
820 {
821 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
822 OBJ_CHECK(obj);
823
824 ZEND_PARSE_PARAMETERS_NONE();
825
826 int32_t count, max;
827 if (obj->opt) {
828 max = php_ion_obj(reader_options, obj->opt)->opt.max_annotation_count;
829 } else {
830 max = 10;
831 }
832 ION_STRING *ptr = ecalloc(sizeof(*ptr), max);
833 iERR err = ion_reader_get_annotations(obj->reader, ptr, max, &count);
834 if (!err) {
835 array_init_size(return_value, count);
836 for (int32_t i = 0; i < count; ++i) {
837 add_next_index_str(return_value, zend_string_from_ion(&ptr[i]));
838 }
839 }
840 efree(ptr);
841 ION_CHECK(err);
842 }
843 static ZEND_METHOD(ion_Reader_Reader, getAnnotationSymbols)
844 {
845 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
846 OBJ_CHECK(obj);
847
848 ZEND_PARSE_PARAMETERS_NONE();
849
850 int32_t count, max;
851 if (obj->opt) {
852 max = php_ion_obj(reader_options, obj->opt)->opt.max_annotation_count;
853 } else {
854 max = 10;
855 }
856 ION_SYMBOL *ptr = ecalloc(sizeof(*ptr), max);
857 iERR err = ion_reader_get_annotation_symbols(obj->reader, ptr, max, &count);
858 if (!err) {
859 array_init_size(return_value, count);
860 for (int32_t i = 0; i < count; ++i) {
861 zval zsym;
862 php_ion_symbol_zval(&ptr[i], &zsym);
863 add_next_index_zval(return_value, &zsym);
864 }
865 }
866 efree(ptr);
867 ION_CHECK(err);
868 }
869 static ZEND_METHOD(ion_Reader_Reader, countAnnotations)
870 {
871 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
872 OBJ_CHECK(obj);
873
874 ZEND_PARSE_PARAMETERS_NONE();
875
876 SIZE sz = 0;
877 ION_CHECK(ion_reader_get_annotation_count(obj->reader, &sz));
878 RETURN_LONG(sz);
879 }
880 static ZEND_METHOD(ion_Reader_Reader, getAnnotation)
881 {
882 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
883 OBJ_CHECK(obj);
884
885 zend_long idx;
886 ZEND_PARSE_PARAMETERS_START(1, 1)
887 Z_PARAM_LONG(idx);
888 ZEND_PARSE_PARAMETERS_END();
889
890 ION_STRING ann;
891 ION_CHECK(ion_reader_get_an_annotation(obj->reader, idx, &ann));
892 RETURN_STRINGL((char *) ann.value, ann.length);
893 }
894 static ZEND_METHOD(ion_Reader_Reader, getAnnotationSymbol)
895 {
896 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
897 OBJ_CHECK(obj);
898
899 zend_long idx;
900 ZEND_PARSE_PARAMETERS_START(1, 1)
901 Z_PARAM_LONG(idx);
902 ZEND_PARSE_PARAMETERS_END();
903
904 ION_SYMBOL sym;
905 ION_CHECK(ion_reader_get_an_annotation_symbol(obj->reader, idx, &sym));
906 php_ion_symbol_zval(&sym, return_value);
907 }
908 static ZEND_METHOD(ion_Reader_Reader, readNull)
909 {
910 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
911 OBJ_CHECK(obj);
912
913 ZEND_PARSE_PARAMETERS_NONE();
914
915 ION_TYPE typ;
916 ION_CHECK(ion_reader_read_null(obj->reader, &typ));
917 RETURN_IONTYPE(typ);
918 }
919 static ZEND_METHOD(ion_Reader_Reader, readBool)
920 {
921 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
922 OBJ_CHECK(obj);
923
924 ZEND_PARSE_PARAMETERS_NONE();
925
926 BOOL b;
927 ION_CHECK(ion_reader_read_bool(obj->reader, &b));
928 RETURN_BOOL(b);
929 }
930 static ZEND_METHOD(ion_Reader_Reader, readInt)
931 {
932 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
933 OBJ_CHECK(obj);
934
935 ZEND_PARSE_PARAMETERS_NONE();
936
937 php_ion_reader_read_int(obj->reader, return_value);
938 }
939 static ZEND_METHOD(ion_Reader_Reader, readFloat)
940 {
941 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
942 OBJ_CHECK(obj);
943
944 ZEND_PARSE_PARAMETERS_NONE();
945
946 double dval;
947 ION_CHECK(ion_reader_read_double(obj->reader, &dval));
948 RETURN_DOUBLE(dval);
949 }
950 static ZEND_METHOD(ion_Reader_Reader, readDecimal)
951 {
952 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
953 OBJ_CHECK(obj);
954
955 ZEND_PARSE_PARAMETERS_NONE();
956
957 object_init_ex(return_value, ce_Decimal);
958 php_ion_decimal *dec = php_ion_obj(decimal, Z_OBJ_P(return_value));
959 ION_CHECK(ion_reader_read_ion_decimal(obj->reader, &dec->dec));
960 php_ion_decimal_ctor(dec);
961 }
962 static ZEND_METHOD(ion_Reader_Reader, readTimestamp)
963 {
964 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
965 OBJ_CHECK(obj);
966
967 ZEND_PARSE_PARAMETERS_NONE();
968
969 php_ion_reader_read_timestamp(obj->reader, obj->opt ? &php_ion_obj(reader_options, obj->opt)->opt : NULL, return_value);
970 }
971 static ZEND_METHOD(ion_Reader_Reader, readSymbol)
972 {
973 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
974 OBJ_CHECK(obj);
975
976 ZEND_PARSE_PARAMETERS_NONE();
977
978 ION_SYMBOL sym;
979 ION_CHECK(ion_reader_read_ion_symbol(obj->reader, &sym));
980 php_ion_symbol_zval(&sym, return_value);
981 }
982 static ZEND_METHOD(ion_Reader_Reader, readString)
983 {
984 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
985 OBJ_CHECK(obj);
986
987 ZEND_PARSE_PARAMETERS_NONE();
988
989 ION_STRING str;
990 ION_CHECK(ion_reader_read_string(obj->reader, &str));
991 RETURN_STRINGL((char *) str.value, str.length);
992 }
993
994 typedef iERR (*read_part_fn)(ION_READER *, BYTE *, SIZE, SIZE *);
995 static void read_part(INTERNAL_FUNCTION_PARAMETERS, read_part_fn fn)
996 {
997 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
998 OBJ_CHECK(obj);
999
1000 zval *ref;
1001 zend_string *zstr;
1002 zend_long len = 0x1000;
1003 ZEND_PARSE_PARAMETERS_START(1, 1)
1004 Z_PARAM_ZVAL(ref)
1005 Z_PARAM_OPTIONAL
1006 Z_PARAM_LONG(len)
1007 ZEND_PARSE_PARAMETERS_END();
1008
1009 ZVAL_DEREF(ref);
1010
1011 if (Z_TYPE_P(ref) == IS_STRING && Z_STRLEN_P(ref) == len) {
1012 /* reuse */
1013 zstr = Z_STR_P(ref);
1014 } else {
1015 zval_dtor(ref);
1016 zstr = zend_string_alloc(len, 0);
1017 }
1018
1019 SIZE read = 0;
1020 ION_CHECK(fn(obj->reader, (BYTE *) zstr->val, zstr->len, &read), goto fail);
1021 if (read > 0) {
1022 if (read < zstr->len) {
1023 zstr = zend_string_truncate(zstr, read, 0);
1024 }
1025 ZVAL_STR(ref, zstr);
1026 RETURN_TRUE;
1027 }
1028 fail:
1029 zend_string_release(zstr);
1030 ZVAL_EMPTY_STRING(ref);
1031 RETURN_FALSE;
1032 }
1033 static ZEND_METHOD(ion_Reader_Reader, readStringPart)
1034 {
1035 read_part(INTERNAL_FUNCTION_PARAM_PASSTHRU, ion_reader_read_partial_string);
1036 }
1037 static ZEND_METHOD(ion_Reader_Reader, readLob)
1038 {
1039 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1040 OBJ_CHECK(obj);
1041
1042 ZEND_PARSE_PARAMETERS_NONE();
1043
1044 php_ion_reader_read_lob(obj->reader, return_value);
1045 }
1046 static ZEND_METHOD(ion_Reader_Reader, readLobPart)
1047 {
1048 read_part(INTERNAL_FUNCTION_PARAM_PASSTHRU, ion_reader_read_lob_partial_bytes);
1049 }
1050 static ZEND_METHOD(ion_Reader_Reader, getPosition)
1051 {
1052 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1053 OBJ_CHECK(obj);
1054
1055 ZEND_PARSE_PARAMETERS_NONE();
1056
1057 int64_t bytes = 0;
1058 int32_t dummy;
1059 ION_CHECK(ion_reader_get_position(obj->reader, &bytes, &dummy, &dummy));
1060 RETURN_LONG(bytes);
1061 }
1062 static ZEND_METHOD(ion_Reader_Reader, getDepth)
1063 {
1064 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1065 OBJ_CHECK(obj);
1066
1067 ZEND_PARSE_PARAMETERS_NONE();
1068
1069 SIZE depth = 0;
1070 ION_CHECK(ion_reader_get_depth(obj->reader, &depth));
1071 RETURN_LONG(depth);
1072 }
1073 static ZEND_METHOD(ion_Reader_Reader, seek)
1074 {
1075 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1076 OBJ_CHECK(obj);
1077
1078 zend_long off, len = -1;
1079 ZEND_PARSE_PARAMETERS_START(1, 2)
1080 Z_PARAM_LONG(off)
1081 Z_PARAM_OPTIONAL
1082 Z_PARAM_LONG(len)
1083 ZEND_PARSE_PARAMETERS_END();
1084
1085 ION_CHECK(ion_reader_seek(obj->reader, off, len));
1086 }
1087 static ZEND_METHOD(ion_Reader_Reader, getValueOffset)
1088 {
1089 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1090 OBJ_CHECK(obj);
1091
1092 ZEND_PARSE_PARAMETERS_NONE();
1093
1094 POSITION off = 0;
1095 ION_CHECK(ion_reader_get_value_offset(obj->reader, &off));
1096 RETURN_LONG(off);
1097 }
1098 static ZEND_METHOD(ion_Reader_Reader, getValueLength)
1099 {
1100 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1101 OBJ_CHECK(obj);
1102
1103 ZEND_PARSE_PARAMETERS_NONE();
1104
1105 SIZE len = 0;
1106 ION_CHECK(ion_reader_get_value_length(obj->reader, &len));
1107 RETURN_LONG(len);
1108 }
1109 static ZEND_METHOD(ion_Reader_Buffer_Reader, __construct)
1110 {
1111 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1112 PTR_CHECK(obj);
1113
1114 zend_string *zstr;
1115 ZEND_PARSE_PARAMETERS_START(1, 2)
1116 Z_PARAM_STR(zstr)
1117 Z_PARAM_OPTIONAL
1118 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Reader_Options)
1119 ZEND_PARSE_PARAMETERS_END();
1120
1121 obj->type = BUFFER_READER;
1122 obj->buffer = zend_string_copy(zstr);
1123
1124 php_ion_reader_ctor(obj);
1125 }
1126 static ZEND_METHOD(ion_Reader_Buffer_Reader, getBuffer)
1127 {
1128 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1129 OBJ_CHECK(obj);
1130
1131 ZEND_PARSE_PARAMETERS_NONE();
1132 RETURN_STR_COPY(obj->buffer);
1133 }
1134
1135 static ZEND_METHOD(ion_Reader_Stream_Reader, __construct)
1136 {
1137 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1138 PTR_CHECK(obj);
1139
1140 zval *zstream;
1141 ZEND_PARSE_PARAMETERS_START(1, 2)
1142 Z_PARAM_RESOURCE(zstream)
1143 Z_PARAM_OPTIONAL
1144 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Reader_Options)
1145 ZEND_PARSE_PARAMETERS_END();
1146
1147 obj->type = STREAM_READER;
1148 php_stream_from_zval_no_verify(obj->stream.ptr, zstream);
1149
1150 php_ion_reader_ctor(obj);
1151 }
1152 static ZEND_METHOD(ion_Reader_Stream_Reader, getStream)
1153 {
1154 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1155 OBJ_CHECK(obj);
1156
1157 ZEND_PARSE_PARAMETERS_NONE();
1158 PTR_CHECK(obj->stream.ptr);
1159
1160 GC_ADDREF(obj->stream.ptr->res);
1161 RETURN_RES(obj->stream.ptr->res);
1162 }
1163 static ZEND_METHOD(ion_Reader_Stream_Reader, resetStream)
1164 {
1165 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1166 OBJ_CHECK(obj);
1167
1168 zval *zstream;
1169 ZEND_PARSE_PARAMETERS_START(1, 1);
1170 Z_PARAM_RESOURCE(zstream);
1171 ZEND_PARSE_PARAMETERS_END();
1172
1173 ION_CHECK(ion_reader_reset_stream(&obj->reader, obj, php_ion_reader_stream_handler));
1174
1175 if (obj->stream.ptr) {
1176 zend_list_delete(obj->stream.ptr->res);
1177 }
1178 php_stream_from_zval_no_verify(obj->stream.ptr, zstream);
1179 PTR_CHECK(obj->stream.ptr);
1180 Z_ADDREF_P(zstream);
1181 }
1182 static ZEND_METHOD(ion_Reader_Stream_Reader, resetStreamWithLength)
1183 {
1184 php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS));
1185 OBJ_CHECK(obj);
1186
1187 zval *zstream;
1188 zend_long length;
1189 ZEND_PARSE_PARAMETERS_START(2, 2);
1190 Z_PARAM_RESOURCE(zstream);
1191 Z_PARAM_LONG(length)
1192 ZEND_PARSE_PARAMETERS_END();
1193
1194 ION_CHECK(ion_reader_reset_stream_with_length(&obj->reader, obj, php_ion_reader_stream_handler, length));
1195
1196 if (obj->stream.ptr) {
1197 zend_list_delete(obj->stream.ptr->res);
1198 }
1199 php_stream_from_zval_no_verify(obj->stream.ptr, zstream);
1200 PTR_CHECK(obj->stream.ptr);
1201 Z_ADDREF_P(zstream);
1202 }
1203 static ZEND_METHOD(ion_Writer_Options, __construct)
1204 {
1205 php_ion_writer_options *obj = php_ion_obj(writer_options, Z_OBJ_P(ZEND_THIS));
1206 PTR_CHECK(obj);
1207
1208 zend_bool binary = false, compact_floats = false, escape = false, pretty = false,
1209 tabs = true, flush = false;
1210 zend_long indent = 2, max_depth = 10, max_ann = 10, temp = 0x4000;
1211 ZEND_PARSE_PARAMETERS_START(0, 12)
1212 Z_PARAM_OPTIONAL
1213 //public readonly ?\ion\Catalog $catalog = null,
1214 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->cat, ce_Catalog)
1215 //public readonly ?\ion\Decimal\Context $decimalContext = null,
1216 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->dec_ctx, ce_Decimal_Context)
1217 //public readonly bool $outputBinary = false,
1218 Z_PARAM_BOOL(binary)
1219 //public readonly bool $compactFloats = false,
1220 Z_PARAM_BOOL(compact_floats)
1221 //public readonly bool $escapeNonAscii = false,
1222 Z_PARAM_BOOL(escape)
1223 //public readonly bool $prettyPrint = false,
1224 Z_PARAM_BOOL(pretty)
1225 //public readonly bool $indentTabs = true,
1226 Z_PARAM_BOOL(tabs)
1227 //public readonly int $indentSize = 2,
1228 Z_PARAM_LONG(indent)
1229 //public readonly bool $flushEveryValue = false,
1230 Z_PARAM_BOOL(flush)
1231 //public readonly int $maxContainerDepth = 10,
1232 Z_PARAM_LONG(max_depth)
1233 //public readonly int $maxAnnotations = 10,
1234 Z_PARAM_LONG(max_ann)
1235 //public readonly int $tempBufferSize = 0x4000,
1236 Z_PARAM_LONG(temp)
1237 ZEND_PARSE_PARAMETERS_END();
1238
1239 if (obj->cat) {
1240 update_property_obj(&obj->std, ZEND_STRL("catalog"), obj->cat);
1241 obj->opt.pcatalog = php_ion_obj(catalog, obj->cat)->cat;
1242 } else {
1243 zend_update_property_null(NULL, &obj->std, ZEND_STRL("catalog"));
1244 }
1245 if (obj->dec_ctx) {
1246 update_property_obj(&obj->std, ZEND_STRL("decimalContext"), obj->dec_ctx);
1247 obj->opt.decimal_context = &php_ion_obj(decimal_ctx, obj->dec_ctx)->ctx;
1248 } else {
1249 zend_update_property_null(NULL, &obj->std, ZEND_STRL("decimalContext"));
1250 }
1251 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("outputBinary"),
1252 obj->opt.output_as_binary = binary);
1253 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("compactFloats"),
1254 obj->opt.compact_floats = compact_floats);
1255 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("escapeNonAscii"),
1256 obj->opt.escape_all_non_ascii = escape);
1257 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("prettyPrint"),
1258 obj->opt.pretty_print = pretty);
1259 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("indentTabs"),
1260 obj->opt.indent_with_tabs = tabs);
1261 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("indentSize"),
1262 obj->opt.indent_size = (SIZE) indent);
1263 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("flushEveryValue"),
1264 obj->opt.flush_every_value = flush);
1265 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("maxContainerDepth"),
1266 obj->opt.max_container_depth = (SIZE) max_depth);
1267 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("maxAnnotations"),
1268 obj->opt.max_annotation_count = (SIZE) max_ann);
1269 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("tempBufferSize"),
1270 obj->opt.temp_buffer_size = (SIZE) temp);
1271 }
1272 static ZEND_METHOD(ion_Writer_Writer, writeNull)
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 ION_CHECK(ion_writer_write_null(obj->writer));
1280 }
1281 static ZEND_METHOD(ion_Writer_Writer, writeTypedNull)
1282 {
1283 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1284 OBJ_CHECK(obj);
1285
1286 zend_object *typ_obj;
1287 ZEND_PARSE_PARAMETERS_START(1, 1)
1288 Z_PARAM_OBJ_OF_CLASS(typ_obj, ce_Type)
1289 ZEND_PARSE_PARAMETERS_END();
1290
1291 ION_CHECK(ion_writer_write_typed_null(obj->writer, ion_type_from_enum(typ_obj)));
1292 }
1293 static ZEND_METHOD(ion_Writer_Writer, writeBool)
1294 {
1295 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1296 OBJ_CHECK(obj);
1297
1298 zend_bool b;
1299 ZEND_PARSE_PARAMETERS_START(1, 1)
1300 Z_PARAM_BOOL(b)
1301 ZEND_PARSE_PARAMETERS_END();
1302
1303 ION_CHECK(ion_writer_write_bool(obj->writer, b));
1304 }
1305 static ZEND_METHOD(ion_Writer_Writer, writeInt)
1306 {
1307 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1308 OBJ_CHECK(obj);
1309
1310 zend_long l;
1311 zend_string *s;
1312 ZEND_PARSE_PARAMETERS_START(1, 1)
1313 Z_PARAM_STR_OR_LONG(s, l)
1314 ZEND_PARSE_PARAMETERS_END();
1315
1316 if (s) {
1317 ION_INT *i;
1318 ION_CHECK(ion_int_alloc(obj->writer, &i));
1319 ION_CHECK(ion_int_from_chars(i, s->val, s->len));
1320 ION_CHECK(ion_writer_write_ion_int(obj->writer, i));
1321 ion_int_free(i);
1322 } else {
1323 ION_CHECK(ion_writer_write_int64(obj->writer, l));
1324 }
1325 }
1326 static ZEND_METHOD(ion_Writer_Writer, writeFloat)
1327 {
1328 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1329 OBJ_CHECK(obj);
1330
1331 double d;
1332 ZEND_PARSE_PARAMETERS_START(1, 1)
1333 Z_PARAM_DOUBLE(d)
1334 ZEND_PARSE_PARAMETERS_END();
1335
1336 ION_CHECK(ion_writer_write_double(obj->writer, d));
1337 }
1338 static ZEND_METHOD(ion_Writer_Writer, writeDecimal)
1339 {
1340 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1341 OBJ_CHECK(obj);
1342
1343 zend_object *dec_obj;
1344 zend_string *dec_str;
1345 ZEND_PARSE_PARAMETERS_START(1, 1)
1346 Z_PARAM_OBJ_OF_CLASS_OR_STR(dec_obj, ce_Decimal, dec_str)
1347 ZEND_PARSE_PARAMETERS_END();
1348
1349 if (dec_str) {
1350 decContext *ctx = &php_ion_globals.decimal.ctx;
1351 ION_DECIMAL dec = {0};
1352
1353 if (obj->opt) {
1354 php_ion_writer_options *opt_obj = php_ion_obj(writer_options, obj->opt);
1355 if (opt_obj->opt.decimal_context) {
1356 ctx = opt_obj->opt.decimal_context;
1357 }
1358 }
1359 ION_CHECK(ion_decimal_from_string(&dec, dec_str->val, ctx));
1360 ION_CHECK(ion_writer_write_ion_decimal(obj->writer, &dec));
1361 } else {
1362 php_ion_decimal *dec = php_ion_obj(decimal, dec_obj);
1363 ION_CHECK(ion_writer_write_ion_decimal(obj->writer, &dec->dec));
1364 }
1365 }
1366 static ZEND_METHOD(ion_Writer_Writer, writeTimestamp)
1367 {
1368 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1369 OBJ_CHECK(obj);
1370
1371 zend_object *ts_obj;
1372 zend_string *ts_str;
1373 ZEND_PARSE_PARAMETERS_START(1, 1)
1374 Z_PARAM_OBJ_OF_CLASS_OR_STR(ts_obj, ce_Timestamp, ts_str)
1375 ZEND_PARSE_PARAMETERS_END();
1376
1377 decContext *ctx = NULL;
1378 if (obj->opt) {
1379 ctx = php_ion_obj(reader_options, obj->opt)->opt.decimal_context;
1380 }
1381
1382 ION_TIMESTAMP tmp = {0};
1383 if (ts_str) {
1384 SIZE used;
1385 ION_CHECK(ion_timestamp_parse(&tmp, ts_str->val, ts_str->len, &used, ctx));
1386 } else {
1387 php_ion_timestamp *ts = php_ion_obj(timestamp, ts_obj);
1388 OBJ_CHECK(ts);
1389 ion_timestamp_from_php(&tmp, ts, ctx);
1390 }
1391 ION_CHECK(ion_writer_write_timestamp(obj->writer, &tmp));
1392 }
1393 static ZEND_METHOD(ion_Writer_Writer, writeSymbol)
1394 {
1395 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1396 OBJ_CHECK(obj);
1397
1398 zend_object *sym_obj;
1399 zend_string *sym_str;
1400 ZEND_PARSE_PARAMETERS_START(1, 1)
1401 Z_PARAM_OBJ_OF_CLASS_OR_STR(sym_obj, ce_Symbol, sym_str);
1402 ZEND_PARSE_PARAMETERS_END();
1403
1404 if (sym_str) {
1405 ION_STRING is;
1406 ION_CHECK(ion_writer_write_symbol(obj->writer, ion_string_from_zend(&is, sym_str)));
1407 } else {
1408 php_ion_symbol *sym = php_ion_obj(symbol, sym_obj);
1409 PTR_CHECK(sym);
1410 ION_CHECK(ion_writer_write_ion_symbol(obj->writer, &sym->sym));
1411 }
1412 }
1413 static ZEND_METHOD(ion_Writer_Writer, writeString)
1414 {
1415 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1416 OBJ_CHECK(obj);
1417
1418 zend_string *str;
1419 ZEND_PARSE_PARAMETERS_START(1, 1)
1420 Z_PARAM_STR(str)
1421 ZEND_PARSE_PARAMETERS_END();
1422
1423 ION_STRING is;
1424 ION_CHECK(ion_writer_write_string(obj->writer, ion_string_from_zend(&is, str)));
1425 }
1426 static ZEND_METHOD(ion_Writer_Writer, writeCLob)
1427 {
1428 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1429 OBJ_CHECK(obj);
1430
1431 zend_string *str;
1432 ZEND_PARSE_PARAMETERS_START(1, 1)
1433 Z_PARAM_STR(str)
1434 ZEND_PARSE_PARAMETERS_END();
1435
1436 ION_CHECK(ion_writer_write_clob(obj->writer, (BYTE *) str->val, str->len));
1437 }
1438 static ZEND_METHOD(ion_Writer_Writer, writeBLob)
1439 {
1440 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1441 OBJ_CHECK(obj);
1442
1443 zend_string *str;
1444 ZEND_PARSE_PARAMETERS_START(1, 1)
1445 Z_PARAM_STR(str)
1446 ZEND_PARSE_PARAMETERS_END();
1447
1448 ION_CHECK(ion_writer_write_blob(obj->writer, (BYTE *) str->val, str->len));
1449 }
1450 static ZEND_METHOD(ion_Writer_Writer, startLob)
1451 {
1452 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1453 OBJ_CHECK(obj);
1454
1455 zend_object *typ_obj;
1456 ZEND_PARSE_PARAMETERS_START(1, 1)
1457 Z_PARAM_OBJ_OF_CLASS(typ_obj, ce_Type)
1458 ZEND_PARSE_PARAMETERS_END();
1459
1460 ION_CHECK(ion_writer_start_lob(obj->writer, ion_type_from_enum(typ_obj)));
1461 }
1462 static ZEND_METHOD(ion_Writer_Writer, appendLob)
1463 {
1464 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1465 OBJ_CHECK(obj);
1466
1467 zend_string *str;
1468 ZEND_PARSE_PARAMETERS_START(1, 1)
1469 Z_PARAM_STR(str)
1470 ZEND_PARSE_PARAMETERS_END();
1471
1472 ION_CHECK(ion_writer_append_lob(obj->writer, (BYTE *) str->val, str->len));
1473 }
1474 static ZEND_METHOD(ion_Writer_Writer, finishLob)
1475 {
1476 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1477 OBJ_CHECK(obj);
1478
1479 ZEND_PARSE_PARAMETERS_NONE();
1480
1481 ION_CHECK(ion_writer_finish_lob(obj->writer));
1482 }
1483 static ZEND_METHOD(ion_Writer_Writer, startContainer)
1484 {
1485 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1486 OBJ_CHECK(obj);
1487
1488 zend_object *typ_obj;
1489 ZEND_PARSE_PARAMETERS_START(1, 1)
1490 Z_PARAM_OBJ_OF_CLASS(typ_obj, ce_Type)
1491 ZEND_PARSE_PARAMETERS_END();
1492
1493 ION_CHECK(ion_writer_start_container(obj->writer, ion_type_from_enum(typ_obj)));
1494 }
1495 static ZEND_METHOD(ion_Writer_Writer, finishContainer)
1496 {
1497 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1498 OBJ_CHECK(obj);
1499
1500 ZEND_PARSE_PARAMETERS_NONE();
1501
1502 ION_CHECK(ion_writer_finish_container(obj->writer));
1503 }
1504 static ZEND_METHOD(ion_Writer_Writer, writeFieldName)
1505 {
1506 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1507 OBJ_CHECK(obj);
1508
1509 zend_object *sym_obj;
1510 zend_string *sym_str;
1511 ZEND_PARSE_PARAMETERS_START(1, 1)
1512 Z_PARAM_OBJ_OF_CLASS_OR_STR(sym_obj, ce_Symbol, sym_str);
1513 ZEND_PARSE_PARAMETERS_END();
1514
1515 if (sym_str) {
1516 ION_STRING is;
1517 ION_CHECK(ion_writer_write_field_name(obj->writer, ion_string_from_zend(&is, sym_str)));
1518 } else {
1519 php_ion_symbol *sym = php_ion_obj(symbol, sym_obj);
1520 PTR_CHECK(sym);
1521 ION_CHECK(ion_writer_write_field_name_symbol(obj->writer, &sym->sym));
1522 }
1523 }
1524 static ZEND_METHOD(ion_Writer_Writer, writeAnnotation)
1525 {
1526 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1527 OBJ_CHECK(obj);
1528
1529 zval *args;
1530 unsigned argc;
1531 ZEND_PARSE_PARAMETERS_START(1, -1)
1532 Z_PARAM_VARIADIC('+', args, argc);
1533 ZEND_PARSE_PARAMETERS_END();
1534
1535 for (unsigned i = 0; i < argc; ++i) {
1536 switch (Z_TYPE(args[i])) {
1537 case IS_STRING: ;
1538 ION_STRING is;
1539 ION_CHECK(ion_writer_add_annotation(obj->writer, ion_string_from_zend(&is, Z_STR(args[i]))));
1540 break;
1541
1542 case IS_OBJECT:
1543 ION_CHECK(ion_writer_add_annotation_symbol(obj->writer, &php_ion_obj(symbol, Z_OBJ(args[i]))->sym));
1544 break;
1545 }
1546 }
1547 }
1548 static ZEND_METHOD(ion_Writer_Writer, getDepth)
1549 {
1550 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1551 OBJ_CHECK(obj);
1552
1553 ZEND_PARSE_PARAMETERS_NONE();
1554
1555 SIZE depth;
1556 ION_CHECK(ion_writer_get_depth(obj->writer, &depth));
1557 RETURN_LONG(depth);
1558 }
1559 static ZEND_METHOD(ion_Writer_Writer, flush)
1560 {
1561 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1562 OBJ_CHECK(obj);
1563
1564 ZEND_PARSE_PARAMETERS_NONE();
1565
1566 SIZE flushed;
1567 ION_CHECK(ion_writer_flush(obj->writer, &flushed));
1568 RETURN_LONG(flushed);
1569 }
1570 static ZEND_METHOD(ion_Writer_Writer, finish)
1571 {
1572 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1573 OBJ_CHECK(obj);
1574
1575 ZEND_PARSE_PARAMETERS_NONE();
1576
1577 SIZE flushed;
1578 ION_CHECK(ion_writer_finish(obj->writer, &flushed));
1579 RETURN_LONG(flushed);
1580 }
1581 static ZEND_METHOD(ion_Writer_Buffer_Writer, __construct)
1582 {
1583 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1584 PTR_CHECK(obj);
1585
1586 ZEND_PARSE_PARAMETERS_START(0, 1)
1587 Z_PARAM_OPTIONAL
1588 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Writer_Options)
1589 ZEND_PARSE_PARAMETERS_END();
1590
1591 obj->type = BUFFER_WRITER;
1592 php_ion_writer_ctor(obj);
1593 }
1594 static ZEND_METHOD(ion_Writer_Buffer_Writer, getBuffer)
1595 {
1596 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1597 OBJ_CHECK(obj);
1598
1599 ZEND_PARSE_PARAMETERS_NONE();
1600
1601 RETVAL_STR(php_ion_writer_buffer_copy(obj));
1602 }
1603 static ZEND_METHOD(ion_Writer_Buffer_Writer, resetBuffer)
1604 {
1605 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1606 OBJ_CHECK(obj);
1607
1608 ZEND_PARSE_PARAMETERS_NONE();
1609
1610 php_ion_writer_buffer_reset(obj);
1611 }
1612 static ZEND_METHOD(ion_Writer_Stream_Writer, __construct)
1613 {
1614 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1615 PTR_CHECK(obj);
1616
1617 zval *zstream;
1618 ZEND_PARSE_PARAMETERS_START(1, 2)
1619 Z_PARAM_RESOURCE(zstream)
1620 Z_PARAM_OPTIONAL
1621 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Writer_Options)
1622 ZEND_PARSE_PARAMETERS_END();
1623
1624 obj->type = STREAM_WRITER;
1625 php_stream_from_zval_no_verify(obj->stream.ptr, zstream);
1626
1627 php_ion_writer_ctor(obj);
1628 }
1629 static ZEND_METHOD(ion_Writer_Stream_Writer, getStream)
1630 {
1631 php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS));
1632 OBJ_CHECK(obj);
1633
1634 ZEND_PARSE_PARAMETERS_NONE();
1635 PTR_CHECK(obj->stream.ptr);
1636
1637 GC_ADDREF(obj->stream.ptr->res);
1638 RETURN_RES(obj->stream.ptr->res);
1639 }
1640
1641 static ZEND_METHOD(ion_Serializer_PHP, __construct)
1642 {
1643 php_ion_serializer_php *obj = php_ion_obj(serializer_php, Z_OBJ_P(ZEND_THIS));
1644 PTR_CHECK(obj);
1645
1646 obj->serializer.call_magic = true;
1647
1648 ZEND_PARSE_PARAMETERS_START(0, 4)
1649 Z_PARAM_OPTIONAL
1650 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Writer_Options)
1651 Z_PARAM_BOOL(obj->serializer.multi_seq)
1652 Z_PARAM_BOOL(obj->serializer.call_magic)
1653 Z_PARAM_STR_OR_NULL(obj->serializer.call_custom)
1654 ZEND_PARSE_PARAMETERS_END();
1655
1656 php_ion_serializer_php_ctor(obj);
1657 }
1658 static ZEND_METHOD(ion_Serializer_PHP, serialize)
1659 {
1660 zend_object *obj = Z_OBJ_P(ZEND_THIS);
1661
1662 zval *data;
1663 ZEND_PARSE_PARAMETERS_START(1, 1)
1664 Z_PARAM_ZVAL(data)
1665 ZEND_PARSE_PARAMETERS_END();
1666
1667 php_ion_serialize(&php_ion_obj(serializer_php, obj)->serializer, data, return_value);
1668 }
1669 static ZEND_FUNCTION(ion_serialize)
1670 {
1671 zval *data;
1672 zend_object *zo_ser = NULL;
1673 ZEND_PARSE_PARAMETERS_START(1, 2)
1674 Z_PARAM_ZVAL(data)
1675 Z_PARAM_OPTIONAL
1676 Z_PARAM_OBJ_OF_CLASS_OR_NULL(zo_ser, ce_Serializer)
1677 ZEND_PARSE_PARAMETERS_END();
1678
1679 if (!zo_ser || zo_ser->ce == ce_Serializer_PHP) {
1680 // default, fast path
1681 php_ion_serializer *ser = zo_ser ? &php_ion_obj(serializer_php, zo_ser)->serializer : NULL;
1682 php_ion_serialize(ser, data, return_value);
1683 } else {
1684 zend_call_method_with_1_params(zo_ser, NULL, NULL, "serialize", return_value, data);
1685 }
1686 }
1687
1688 static ZEND_METHOD(ion_Unserializer_PHP, __construct)
1689 {
1690 php_ion_unserializer_php *obj = php_ion_obj(unserializer_php, Z_OBJ_P(ZEND_THIS));
1691 PTR_CHECK(obj);
1692
1693 obj->unserializer.call_magic = true;
1694
1695 ZEND_PARSE_PARAMETERS_START(0, 4)
1696 Z_PARAM_OPTIONAL
1697 Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Reader_Options)
1698 Z_PARAM_BOOL(obj->unserializer.multi_seq)
1699 Z_PARAM_BOOL(obj->unserializer.call_magic)
1700 Z_PARAM_STR_OR_NULL(obj->unserializer.call_custom)
1701 ZEND_PARSE_PARAMETERS_END();
1702
1703 php_ion_unserializer_php_ctor(obj);
1704 }
1705 static ZEND_METHOD(ion_Unserializer_PHP, unserialize)
1706 {
1707 zend_object *obj = Z_OBJ_P(ZEND_THIS);
1708
1709 zval *data;
1710 ZEND_PARSE_PARAMETERS_START(1, 1)
1711 Z_PARAM_ZVAL(data)
1712 ZEND_PARSE_PARAMETERS_END();
1713
1714 php_ion_unserialize(&php_ion_obj(unserializer_php, obj)->unserializer, data, return_value);
1715 }
1716 static ZEND_FUNCTION(ion_unserialize)
1717 {
1718 zval *data;
1719 zend_object *zo_ser = NULL;
1720 ZEND_PARSE_PARAMETERS_START(1, 2)
1721 Z_PARAM_ZVAL(data)
1722 Z_PARAM_OPTIONAL
1723 Z_PARAM_OBJ_OF_CLASS_OR_NULL(zo_ser, ce_Unserializer)
1724 ZEND_PARSE_PARAMETERS_END();
1725
1726 if (!zo_ser || zo_ser->ce == ce_Unserializer_PHP) {
1727 // default, fast path
1728 php_ion_unserializer *ser = zo_ser ? &php_ion_obj(unserializer_php, zo_ser)->unserializer : NULL;
1729 php_ion_unserialize(ser, data, return_value);
1730 } else {
1731 zend_call_method_with_1_params(zo_ser, NULL, NULL, "__invoke", return_value, data);
1732 }
1733 }
1734
1735 PHP_RINIT_FUNCTION(ion)
1736 {
1737 #if defined(ZTS) && defined(COMPILE_DL_ION)
1738 ZEND_TSRMLS_CACHE_UPDATE();
1739 #endif
1740
1741 php_ion_globals_symbols_init();
1742 php_ion_globals_serializer_init();
1743 php_ion_globals_unserializer_init();
1744 return SUCCESS;
1745 }
1746
1747 PHP_RSHUTDOWN_FUNCTION(ion)
1748 {
1749 php_ion_globals_unserializer_dtor();
1750 php_ion_globals_serializer_dtor();
1751 php_ion_globals_symbols_dtor();
1752 return SUCCESS;
1753 }
1754
1755 #define ZEND_ARG_VARIADIC_OBJ_TYPE_MASK(pass_by_ref, name, classname, type_mask, default_value) \
1756 { #name, ZEND_TYPE_INIT_CLASS_CONST_MASK(#classname, ((type_mask) | _ZEND_ARG_INFO_FLAGS(pass_by_ref, 1, 0))), default_value },
1757 #include "ion_arginfo.h"
1758
1759 // spl_iterators.h includes ext/pcre/php_pcre.h which might not find pcre2.h
1760 extern PHPAPI zend_class_entry *spl_ce_RecursiveIterator;
1761
1762 PHP_MINIT_FUNCTION(ion)
1763 {
1764 // true globals
1765 if (SUCCESS != g_sym_init()) {
1766 return FAILURE;
1767 }
1768 g_intern_str_init();
1769
1770 // Catalog
1771 php_ion_register(catalog, Catalog, zend_ce_countable);
1772
1773 // Decimal
1774 php_ion_register(decimal, Decimal);
1775 php_ion_register(decimal_ctx, Decimal_Context);
1776 ce_Decimal_Context_Rounding = register_class_ion_Decimal_Context_Rounding();
1777
1778 // LOB
1779 ce_LOB = register_class_ion_LOB();
1780
1781 // Reader
1782 ce_Reader = register_class_ion_Reader(spl_ce_RecursiveIterator);
1783 php_ion_register(reader_options, Reader_Options);
1784 php_ion_register(reader, Reader_Reader, ce_Reader);
1785 ce_Reader_Buffer = register_class_ion_Reader_Buffer(ce_Reader);
1786 ce_Reader_Buffer_Reader = register_class_ion_Reader_Buffer_Reader(ce_Reader_Reader, ce_Reader_Buffer);
1787 ce_Reader_Stream = register_class_ion_Reader_Stream(ce_Reader);
1788 ce_Reader_Stream_Reader = register_class_ion_Reader_Stream_Reader(ce_Reader_Reader, ce_Reader_Stream);
1789
1790 // Serializer
1791 ce_Serializer = register_class_ion_Serializer();
1792 php_ion_register(serializer_php, Serializer_PHP, ce_Serializer);
1793
1794 // Symbol
1795 php_ion_register(symbol, Symbol);
1796 oh_Symbol.compare = php_ion_symbol_zval_compare;
1797 php_ion_register(symbol_iloc, Symbol_ImportLocation);
1798 php_ion_register(symbol_table, Symbol_Table);
1799 ce_Symbol_Table->create_object = NULL;
1800 ce_Symbol_Table_Local = register_class_ion_Symbol_Table_Local(ce_Symbol_Table);
1801 ce_Symbol_Table_Local->create_object = create_ion_Symbol_Table;
1802 ce_Symbol_Table_Shared = register_class_ion_Symbol_Table_Shared(ce_Symbol_Table);
1803 ce_Symbol_Table_Shared->create_object = create_ion_Symbol_Table;
1804 ce_Symbol_Enum = register_class_ion_Symbol_Enum();
1805 ce_Symbol_Table_System = register_class_ion_Symbol_Table_System(ce_Symbol_Enum);
1806 ce_Symbol_Table_PHP = register_class_ion_Symbol_Table_PHP(ce_Symbol_Enum);
1807
1808 // Timestamp
1809 ce_Timestamp = register_class_ion_Timestamp(php_date_get_date_ce());
1810 ce_Timestamp_Format = register_class_ion_Timestamp_Format();
1811 ce_Timestamp_Precision = register_class_ion_Timestamp_Precision();
1812
1813 // Type
1814 ce_Type = register_class_ion_Type();
1815
1816 // Writer
1817 ce_Writer = register_class_ion_Writer();
1818 php_ion_register(writer_options, Writer_Options);
1819 php_ion_register(writer, Writer_Writer, ce_Writer);
1820 ce_Writer_Buffer = register_class_ion_Writer_Buffer(ce_Writer);
1821 ce_Writer_Buffer_Writer = register_class_ion_Writer_Buffer_Writer(ce_Writer_Writer, ce_Writer_Buffer);
1822 ce_Writer_Stream = register_class_ion_Writer_Stream(ce_Writer);
1823 ce_Writer_Stream_Writer = register_class_ion_Writer_Stream_Writer(ce_Writer_Writer, ce_Writer_Stream);
1824
1825 // Unserializer
1826 ce_Unserializer = register_class_ion_Unserializer();
1827 php_ion_register(unserializer_php, Unserializer_PHP, ce_Unserializer);
1828
1829 return SUCCESS;
1830 }
1831
1832 PHP_MSHUTDOWN_FUNCTION(ion)
1833 {
1834 if (g_sym_tab_php) {
1835 ion_symbol_table_close(g_sym_tab_php);
1836 }
1837 zend_hash_destroy(&g_sym_hash);
1838 return SUCCESS;
1839 }
1840
1841 PHP_MINFO_FUNCTION(ion)
1842 {
1843 php_info_print_table_start();
1844 php_info_print_table_header(2, "ion support", "enabled");
1845 php_info_print_table_end();
1846 }
1847
1848 PHP_GINIT_FUNCTION(ion)
1849 {
1850 memset(ion_globals, 0, sizeof(*ion_globals));
1851
1852 php_ion_decimal_ctx_init_max(&ion_globals->decimal.ctx, DEC_ROUND_HALF_EVEN);
1853 php_ion_decimal_from_zend_long(&ion_globals->decimal.zend_max, &ion_globals->decimal.ctx, ZEND_LONG_MAX);
1854 php_ion_decimal_from_zend_long(&ion_globals->decimal.zend_min, &ion_globals->decimal.ctx, ZEND_LONG_MIN);
1855 }
1856
1857 PHP_GSHUTDOWN_FUNCTION(ion)
1858 {
1859 }
1860
1861 static zend_module_dep ion_module_deps[] = {
1862 ZEND_MOD_REQUIRED("date")
1863 ZEND_MOD_REQUIRED("spl")
1864 ZEND_MOD_END
1865 };
1866
1867 zend_module_entry ion_module_entry = {
1868 STANDARD_MODULE_HEADER_EX,
1869 NULL,
1870 ion_module_deps,
1871 "ion", /* Extension name */
1872 ext_functions, /* zend_function_entry */
1873 PHP_MINIT(ion), /* PHP_MINIT - Module initialization */
1874 PHP_MSHUTDOWN(ion), /* PHP_MSHUTDOWN - Module shutdown */
1875 PHP_RINIT(ion), /* PHP_RINIT - Request initialization */
1876 PHP_RSHUTDOWN(ion), /* PHP_RSHUTDOWN - Request shutdown */
1877 PHP_MINFO(ion), /* PHP_MINFO - Module info */
1878 PHP_ION_VERSION, /* Version */
1879 ZEND_MODULE_GLOBALS(ion),
1880 PHP_GINIT(ion), /* PHP_GINIT */
1881 PHP_GSHUTDOWN(ion), /* PHP_GSHUTDOWN */
1882 NULL,
1883 STANDARD_MODULE_PROPERTIES_EX
1884 };
1885
1886 #ifdef COMPILE_DL_ION
1887 # ifdef ZTS
1888 ZEND_TSRMLS_CACHE_DEFINE()
1889 # endif
1890 ZEND_GET_MODULE(ion)
1891 #endif