X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=ion.c;h=d2675f02ba54d3b27ba71f307289b9356efe5d7c;hb=2a72b3a8b7af57bcbb130261b0ed54fa901920fa;hp=1b8340d192c46dc59361844b9b901571dcd11df1;hpb=04f2847752eeaa63ca7d551e4a3875b181ca533b;p=awesomized%2Fext-ion diff --git a/ion.c b/ion.c index 1b8340d..d2675f0 100644 --- a/ion.c +++ b/ion.c @@ -25,56 +25,46 @@ #include "ext/date/php_date.h" #include "ext/spl/spl_exceptions.h" -#include "ext/spl/spl_iterators.h" - -#define DECNUMDIGITS 34 /* DECQUAD_Pmax */ -#include "ionc/ion.h" #include "php_ion.h" -#define ZEND_ARG_VARIADIC_OBJ_TYPE_MASK(pass_by_ref, name, classname, type_mask, default_value) \ - { #name, ZEND_TYPE_INIT_CLASS_CONST_MASK(#classname, type_mask | _ZEND_ARG_INFO_FLAGS(pass_by_ref, 1, 0)), default_value }, -#include "ion_arginfo.h" #include "ion_private.h" -ZEND_METHOD(ion_Symbol_ImportLocation, __construct) +static ZEND_METHOD(ion_Symbol_ImportLocation, __construct) { - zend_long location; php_ion_symbol_iloc *obj = php_ion_obj(symbol_iloc, Z_OBJ_P(ZEND_THIS)); - PTR_CHECK(obj); + zend_long location; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_STR(obj->name) Z_PARAM_LONG(location) ZEND_PARSE_PARAMETERS_END(); - obj->loc.location = location; + obj->loc.location = (SID) location; php_ion_symbol_iloc_ctor(obj); } -ZEND_METHOD(ion_Symbol, __construct) +static ZEND_METHOD(ion_Symbol, __construct) { - zend_long sid = -1; php_ion_symbol *obj = php_ion_obj(symbol, Z_OBJ_P(ZEND_THIS)); - PTR_CHECK(obj); - ZEND_PARSE_PARAMETERS_START(1, 3) - Z_PARAM_STR(obj->value) + zend_long sid = -1; + ZEND_PARSE_PARAMETERS_START(0, 3) Z_PARAM_OPTIONAL + Z_PARAM_STR_OR_NULL(obj->value) Z_PARAM_LONG(sid) Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->iloc, ce_Symbol_ImportLocation) ZEND_PARSE_PARAMETERS_END(); - obj->sym.sid = sid; + obj->sym.sid = (SID) sid; php_ion_symbol_ctor(obj); } -ZEND_METHOD(ion_Symbol, equals) +static ZEND_METHOD(ion_Symbol, equals) { - zend_object *other_obj; php_ion_symbol *sym = php_ion_obj(symbol, Z_OBJ_P(ZEND_THIS)); - OBJ_CHECK(sym); + zend_object *other_obj; ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_OBJ_OF_CLASS(other_obj, ce_Symbol) ZEND_PARSE_PARAMETERS_END(); @@ -86,19 +76,346 @@ ZEND_METHOD(ion_Symbol, equals) ION_CHECK(err); RETVAL_BOOL(eq); } -ZEND_METHOD(ion_Timestamp, __construct) +static ZEND_METHOD(ion_Symbol, __toString) +{ + php_ion_symbol *sym = php_ion_obj(symbol, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(sym); + + ZEND_PARSE_PARAMETERS_NONE(); + + if (!sym->value) { + RETURN_EMPTY_STRING(); + } + RETURN_STR_COPY(sym->value); +} +static ZEND_METHOD(ion_Symbol_Enum, toSymbol) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + zval *zc = zend_enum_fetch_case_name(Z_OBJ_P(ZEND_THIS)); + PTR_CHECK(zc); + + zval *zsym = php_ion_global_symbol_fetch_by_enum(Z_STR_P(zc)); + PTR_CHECK(zsym); + RETVAL_ZVAL(zsym, 1, 0); +} +static ZEND_METHOD(ion_Symbol_Enum, toSID) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + zval *zc = zend_enum_fetch_case_name(Z_OBJ_P(ZEND_THIS)); + PTR_CHECK(zc); + + zval *zsym = php_ion_global_symbol_fetch_by_enum(Z_STR_P(zc)); + PTR_CHECK(zsym); + + zval tmp; + RETVAL_ZVAL(zend_read_property(Z_OBJCE_P(zsym), Z_OBJ_P(zsym), ZEND_STRL("sid"), 0, &tmp), 1, 0); +} +static ZEND_METHOD(ion_Symbol_Enum, toString) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + zval *zc = zend_enum_fetch_case_name(Z_OBJ_P(ZEND_THIS)); + PTR_CHECK(zc); + + zval *zsym = php_ion_global_symbol_fetch_by_enum(Z_STR_P(zc)); + PTR_CHECK(zsym); + + zval tmp; + RETVAL_ZVAL(zend_read_property(Z_OBJCE_P(zsym), Z_OBJ_P(zsym), ZEND_STRL("value"), 0, &tmp), 1, 0); +} +static ZEND_FUNCTION(ion_Symbol_Table_System) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + object_init_ex(return_value, ce_Symbol_Table_Shared); + php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(return_value)); + ION_CHECK(ion_symbol_table_get_system_table(&obj->tab, 1)); + php_ion_symbol_table_ctor(obj); + ion_symbol_table_lock(obj->tab); +} +static ZEND_FUNCTION(ion_Symbol_Table_PHP) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + object_init_ex(return_value, ce_Symbol_Table_Shared); + php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(return_value)); + obj->tab = g_sym_tab_php; + php_ion_symbol_table_ctor(obj); + ion_symbol_table_lock(obj->tab); +} +static ZEND_METHOD(ion_Symbol_Table_Local, __construct) +{ + php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(ZEND_THIS)); + PTR_CHECK(obj); + + ZEND_PARSE_PARAMETERS_NONE(); + + ION_CHECK(ion_symbol_table_open_with_type(&obj->tab, NULL, ist_LOCAL)); + obj->dtor = ion_symbol_table_close; + OBJ_CHECK(obj); +} +static ZEND_METHOD(ion_Symbol_Table_Local, import) +{ + php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + zend_object *zo_import; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_OBJ_OF_CLASS(zo_import, ce_Symbol_Table); + ZEND_PARSE_PARAMETERS_END(); + + php_ion_symbol_table_import(obj, php_ion_obj(symbol_table, zo_import)); +} +static ZEND_METHOD(ion_Symbol_Table_Shared, __construct) +{ + php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(ZEND_THIS)); + PTR_CHECK(obj); + + zend_string *zname; + zend_long version = 1; + HashTable *ht_sym = NULL; + ZEND_PARSE_PARAMETERS_START(1, 3) + Z_PARAM_STR(zname) + Z_PARAM_OPTIONAL + Z_PARAM_LONG(version) + Z_PARAM_ARRAY_HT_OR_NULL(ht_sym) + ZEND_PARSE_PARAMETERS_END(); + + ION_CHECK(ion_symbol_table_open_with_type(&obj->tab, NULL, ist_SHARED)); + obj->dtor = ion_symbol_table_close; + + ION_STRING is; + ION_CHECK(ion_symbol_table_set_name(obj->tab, ion_string_from_zend(&is, zname))); + ION_CHECK(ion_symbol_table_set_version(obj->tab, version)); + + php_ion_symbol_table_ctor(obj); + + zval *zsym; + if (ht_sym) ZEND_HASH_FOREACH_VAL(ht_sym, zsym) + { + zend_string *str = zval_get_string(zsym); + if (EG(exception)) { + break; + } + + ION_STRING istr; + ION_CHECK(ion_symbol_table_add_symbol(obj->tab, ion_string_from_zend(&istr, str), NULL), zend_string_release(str)); + zend_string_release(str); + } + ZEND_HASH_FOREACH_END(); +} +static ZEND_METHOD(ion_Symbol_Table, getMaxId) +{ + php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + ZEND_PARSE_PARAMETERS_NONE(); + + SID max_sid; + ION_CHECK(ion_symbol_table_get_max_sid(obj->tab, &max_sid)); + RETURN_LONG(max_sid); +} +static ZEND_METHOD(ion_Symbol_Table, add) +{ + php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + zend_object *zo_sym = NULL; + zend_string *zs_sym = NULL; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_OBJ_OF_CLASS_OR_STR(zo_sym, ce_Symbol, zs_sym) + ZEND_PARSE_PARAMETERS_END(); + + if (zo_sym) { + zval z_sym; + ZVAL_OBJ(&z_sym, zo_sym); + zs_sym = zval_get_string(&z_sym); + ION_CATCH(); + } + SID sid; + ION_STRING is; + ION_CHECK(ion_symbol_table_add_symbol(obj->tab, ion_string_from_zend(&is, zs_sym), &sid), + if (zo_sym) { + zend_string_release(zs_sym); + }); + if (zo_sym) { + zend_string_release(zs_sym); + } + RETURN_LONG(sid); +} +static ZEND_METHOD(ion_Symbol_Table, find) +{ + php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + zend_long zsid; + zend_string *zstring = NULL; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_STR_OR_LONG(zstring, zsid) + ZEND_PARSE_PARAMETERS_END(); + + if (zstring) { + SID sid; + ION_STRING is; + ION_CHECK(ion_symbol_table_find_by_name(obj->tab, ion_string_from_zend(&is, zstring), &sid)); + zsid = sid; + } + ION_SYMBOL *sym; + ION_CHECK(ion_symbol_table_get_symbol(obj->tab, zsid, &sym)); + if (sym) { + php_ion_symbol_table_symbol_zval(obj, sym, return_value); + } +} +static ZEND_METHOD(ion_Symbol_Table, findLocal) +{ + php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + zend_long zsid; + zend_string *zstring; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_STR_OR_LONG(zstring, zsid) + ZEND_PARSE_PARAMETERS_END(); + + if (zstring) { + SID sid; + ION_STRING is; + ION_CHECK(ion_symbol_table_find_by_name(obj->tab, ion_string_from_zend(&is, zstring), &sid)); + zsid = sid; + } + ION_SYMBOL *sym; + ION_CHECK(ion_symbol_table_get_local_symbol(obj->tab, zsid, &sym)); + if (sym) { + php_ion_symbol_table_symbol_zval(obj, sym, return_value); + } +} +static ZEND_METHOD(ion_Catalog, __construct) +{ + php_ion_catalog *obj = php_ion_obj(catalog, Z_OBJ_P(ZEND_THIS)); + PTR_CHECK(obj); + + ZEND_PARSE_PARAMETERS_NONE(); + + php_ion_catalog_ctor(obj); +} +static ZEND_METHOD(ion_Catalog, count) +{ + php_ion_catalog *obj = php_ion_obj(catalog, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + ZEND_PARSE_PARAMETERS_NONE(); + + int32_t c; + ION_CHECK(ion_catalog_get_symbol_table_count(obj->cat, &c)); + RETURN_LONG(c); +} +static ZEND_METHOD(ion_Catalog, add) +{ + php_ion_catalog *obj = php_ion_obj(catalog, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + zend_object *zo_symtab; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_OBJ_OF_CLASS(zo_symtab, ce_Symbol_Table) + ZEND_PARSE_PARAMETERS_END(); + + php_ion_symbol_table *o_symtab = php_ion_obj(symbol_table, zo_symtab); + php_ion_catalog_add_symbol_table(obj, o_symtab); +} + +static ZEND_METHOD(ion_Catalog, remove) +{ + php_ion_catalog *obj = php_ion_obj(catalog, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + zend_object *zo_symtab = NULL; + zend_string *zs_symtab = NULL; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_OBJ_OF_CLASS_OR_STR(zo_symtab, ce_Symbol_Table, zs_symtab) + ZEND_PARSE_PARAMETERS_END(); + + RETVAL_FALSE; + + zval tmp; + zval *ztabs = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("symbolTables"), 0, &tmp); + if (ztabs) { + if (zo_symtab) { + // fast path + zend_ulong idx = (uintptr_t) &zo_symtab->gc; + RETVAL_BOOL(SUCCESS == zend_hash_index_del(Z_ARRVAL_P(ztabs), idx)); + ION_CHECK(ion_catalog_release_symbol_table(obj->cat, php_ion_obj(symbol_table, zo_symtab)->tab)); + } else { + bool deleted = false; + ION_SYMBOL_TABLE *tab; + ION_STRING is; + ion_string_from_zend(&is, zs_symtab); + do { + tab = NULL; + ION_CHECK(ion_catalog_find_best_match(obj->cat, &is, 0, &tab)); + if (tab) { + ION_CHECK(ion_catalog_release_symbol_table(obj->cat, tab)); + deleted = true; + } + } while(tab); + RETVAL_BOOL(deleted); + } + } +} +static ZEND_METHOD(ion_Catalog, find) +{ + php_ion_catalog *obj = php_ion_obj(catalog, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + zend_long zversion = 0; + zend_string *zname; + ZEND_PARSE_PARAMETERS_START(1, 2) + Z_PARAM_STR(zname) + Z_PARAM_OPTIONAL + Z_PARAM_LONG(zversion) + ZEND_PARSE_PARAMETERS_END(); + + ION_STRING is; + ION_SYMBOL_TABLE *tab_ptr = NULL; + ION_CHECK(ion_catalog_find_symbol_table(obj->cat, ion_string_from_zend(&is, zname), zversion, &tab_ptr)); + if (tab_ptr) { + php_ion_catalog_symbol_table_zval(obj, tab_ptr, return_value); + } +} +static ZEND_METHOD(ion_Catalog, findBest) +{ + php_ion_catalog *obj = php_ion_obj(catalog, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + zend_long zversion = 0; + zend_string *zname; + ZEND_PARSE_PARAMETERS_START(1, 2) + Z_PARAM_STR(zname) + Z_PARAM_OPTIONAL + Z_PARAM_LONG(zversion) + ZEND_PARSE_PARAMETERS_END(); + + ION_STRING is; + ION_SYMBOL_TABLE *tab_ptr = NULL; + ION_CHECK(ion_catalog_find_best_match(obj->cat, ion_string_from_zend(&is, zname), zversion, &tab_ptr)); + if (tab_ptr) { + php_ion_catalog_symbol_table_zval(obj, tab_ptr, return_value); + } +} +static ZEND_METHOD(ion_Timestamp, __construct) { php_ion_timestamp *obj = php_ion_obj(timestamp, Z_OBJ_P(ZEND_THIS)); PTR_CHECK(obj); zend_long precision; - zend_object *precision_obj; + zend_object *precision_obj = NULL, *format_obj = NULL; zend_string *fmt = NULL, *dt = NULL; zval *tz = NULL; ZEND_PARSE_PARAMETERS_START(1, 4) Z_PARAM_OBJ_OF_CLASS_OR_LONG(precision_obj, ce_Timestamp_Precision, precision) Z_PARAM_OPTIONAL - Z_PARAM_STR_OR_NULL(fmt) + Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(format_obj, ce_Timestamp_Format, fmt) Z_PARAM_STR_OR_NULL(dt) Z_PARAM_ZVAL(tz) ZEND_PARSE_PARAMETERS_END(); @@ -106,9 +423,12 @@ ZEND_METHOD(ion_Timestamp, __construct) if (precision_obj) { precision = Z_LVAL_P(zend_enum_fetch_case_value(precision_obj)); } + if (format_obj) { + fmt = Z_STR_P(zend_enum_fetch_case_value(format_obj)); + } php_ion_timestamp_ctor(obj, precision, fmt, dt, tz); } -ZEND_METHOD(ion_Timestamp, __toString) +static ZEND_METHOD(ion_Timestamp, __toString) { php_ion_timestamp *obj = php_ion_obj(timestamp, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -120,21 +440,67 @@ ZEND_METHOD(ion_Timestamp, __toString) zend_call_method_with_1_params(&obj->std, obj->std.ce, NULL, "format", return_value, zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("format"), 0, &fmt)); } -ZEND_METHOD(ion_Decimal_Context, __construct) +static ZEND_METHOD(ion_Decimal_Context, __construct) { php_ion_decimal_ctx *obj = php_ion_obj(decimal_ctx, Z_OBJ_P(ZEND_THIS)); PTR_CHECK(obj); - zend_long bits = 128; + zend_bool clamp; + zend_object *o_round = NULL; + zend_long digits, emax, emin, round; + ZEND_PARSE_PARAMETERS_START(5, 5) + Z_PARAM_LONG(digits) + Z_PARAM_LONG(emax) + Z_PARAM_LONG(emin) + Z_PARAM_OBJ_OF_CLASS_OR_LONG(o_round, ce_Decimal_Context_Rounding, round) + Z_PARAM_BOOL(clamp) + ZEND_PARSE_PARAMETERS_END(); + + if (o_round) { + round = Z_LVAL_P(zend_enum_fetch_case_value(o_round)); + } + php_ion_decimal_ctx_init(&obj->ctx, digits, emax, emin, round, clamp); // NOLINT(cppcoreguidelines-narrowing-conversions) + php_ion_decimal_ctx_ctor(obj, o_round); +} +static inline void make_decimal_ctx(INTERNAL_FUNCTION_PARAMETERS, int kind) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + object_init_ex(return_value, ce_Decimal_Context); + php_ion_decimal_ctx *obj = php_ion_obj(decimal_ctx, Z_OBJ_P(return_value)); + decContextDefault(&obj->ctx, kind); + php_ion_decimal_ctx_ctor(obj, NULL); +} +static ZEND_METHOD(ion_Decimal_Context, Dec32) +{ + make_decimal_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, DEC_INIT_DECIMAL32); +} +static ZEND_METHOD(ion_Decimal_Context, Dec64) +{ + make_decimal_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, DEC_INIT_DECIMAL64); +} +static ZEND_METHOD(ion_Decimal_Context, Dec128) +{ + make_decimal_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, DEC_INIT_DECIMAL128); +} +static ZEND_METHOD(ion_Decimal_Context, DecMax) +{ + zend_object *o_round = NULL; + zend_long round = DEC_ROUND_HALF_EVEN; ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL - Z_PARAM_LONG(bits) + Z_PARAM_OBJ_OF_CLASS_OR_LONG(o_round, ce_Decimal_Context_Rounding, round) ZEND_PARSE_PARAMETERS_END(); - zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("bits"), bits); - php_ion_decimal_ctx_ctor(obj); + if (o_round) { + round = Z_LVAL_P(zend_enum_fetch_case_value(o_round)); + } + object_init_ex(return_value, ce_Decimal_Context); + php_ion_decimal_ctx *obj = php_ion_obj(decimal_ctx, Z_OBJ_P(return_value)); + php_ion_decimal_ctx_init_max(&obj->ctx, round); + php_ion_decimal_ctx_ctor(obj, o_round); } -ZEND_METHOD(ion_Decimal, __construct) +static ZEND_METHOD(ion_Decimal, __construct) { php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS)); PTR_CHECK(obj); @@ -153,26 +519,21 @@ ZEND_METHOD(ion_Decimal, __construct) zval zdc; object_init_ex(&zdc, ce_Decimal_Context); obj->ctx = Z_OBJ(zdc); - php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx, obj->ctx)); + php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx, obj->ctx), NULL); } + decContext *ctx = &php_ion_obj(decimal_ctx, obj->ctx)->ctx; + if (zstr) { - ION_CHECK(ion_decimal_from_string(&obj->dec, zstr->val, - obj->ctx ? &php_ion_obj(decimal_ctx, obj->ctx)->ctx : NULL)); - } else if (num <= INT32_MAX && num >= INT32_MIN) { - ION_CHECK(ion_decimal_from_int32(&obj->dec, num)); - } else if (num > 0 && num <= UINT32_MAX) { - ION_CHECK(ion_decimal_from_uint32(&obj->dec, num)); + ION_CHECK(ion_decimal_from_string(&obj->dec, zstr->val, ctx), OBJ_RELEASE(obj->ctx)); } else { - zend_throw_exception_ex(spl_ce_OverflowException, 0, - "Integer value out of bounds: " ZEND_LONG_FMT " (INT32_MIN < n < UINT32_MAX)", num); - return; + php_ion_decimal_from_zend_long(&obj->dec, ctx, num); } php_ion_decimal_ctor(obj); OBJ_RELEASE(obj->ctx); } -ZEND_METHOD(ion_Decimal, equals) +static ZEND_METHOD(ion_Decimal, equals) { php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS)); PTR_CHECK(obj); @@ -187,7 +548,7 @@ ZEND_METHOD(ion_Decimal, equals) obj->ctx ? &php_ion_obj(decimal_ctx, obj->ctx)->ctx : NULL, &is)); RETURN_BOOL(is); } -ZEND_METHOD(ion_Decimal, __toString) +static ZEND_METHOD(ion_Decimal, __toString) { php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS)); PTR_CHECK(obj); @@ -196,7 +557,7 @@ ZEND_METHOD(ion_Decimal, __toString) RETURN_STR(php_ion_decimal_to_string(&obj->dec)); } -ZEND_METHOD(ion_Decimal, toInt) +static ZEND_METHOD(ion_Decimal, toInt) { php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS)); PTR_CHECK(obj); @@ -204,10 +565,10 @@ ZEND_METHOD(ion_Decimal, toInt) ZEND_PARSE_PARAMETERS_NONE(); zend_long l; - php_ion_decimal_to_int(&obj->dec, &php_ion_obj(decimal_ctx, obj->ctx)->ctx, &l); + php_ion_decimal_to_zend_long(&obj->dec, &php_ion_obj(decimal_ctx, obj->ctx)->ctx, &l); RETURN_LONG(l); } -ZEND_METHOD(ion_Decimal, isInt) +static ZEND_METHOD(ion_Decimal, isInt) { php_ion_decimal *obj = php_ion_obj(decimal, Z_OBJ_P(ZEND_THIS)); PTR_CHECK(obj); @@ -216,16 +577,31 @@ ZEND_METHOD(ion_Decimal, isInt) RETURN_BOOL(ion_decimal_is_integer(&obj->dec)); } -ZEND_METHOD(ion_Reader_Options, __construct) +static ZEND_METHOD(ion_LOB, __construct) +{ + zend_string *value; + zend_object *type = NULL; + ZEND_PARSE_PARAMETERS_START(1, 2) + Z_PARAM_STR(value) + Z_PARAM_OPTIONAL + Z_PARAM_OBJ_OF_CLASS(type, ce_Type) + ZEND_PARSE_PARAMETERS_END(); + + if (!type) { + type = zend_enum_get_case_cstr(ce_Type, "CLob"); + } + update_property_obj(Z_OBJ_P(ZEND_THIS), ZEND_STRL("type"), type); + zend_update_property_str(Z_OBJCE_P(ZEND_THIS), Z_OBJ_P(ZEND_THIS), ZEND_STRL("value"), value); +} +static ZEND_METHOD(ion_Reader_Options, __construct) { php_ion_reader_options *opt = php_ion_obj(reader_options, Z_OBJ_P(ZEND_THIS)); zend_bool ret_sys_val = false, skip_validation = false; - zend_long ch_nl = 0xa, max_depth = 10, max_ann, max_ann_buf = 512, - sym_thr = 0x1000, uval_thr = 0x1000, chunk_thr = 0x1000, alloc_pgsz = 0x1000; + zend_long max_depth = 10, max_ann = 10, ann_buf_siz = 0x4000, tmp_buf_siz = 0x4000; PTR_CHECK(opt); - ZEND_PARSE_PARAMETERS_START(0, 13) + ZEND_PARSE_PARAMETERS_START(0, 9) Z_PARAM_OPTIONAL // public readonly ?\ion\Catalog $catalog = null, Z_PARAM_OBJ_OF_CLASS_OR_NULL(opt->cat, ce_Catalog) @@ -235,60 +611,53 @@ ZEND_METHOD(ion_Reader_Options, __construct) Z_PARAM_OBJ_OF_CLASS_OR_NULL(opt->cb, zend_ce_closure); // public readonly bool $returnSystemValues = false, Z_PARAM_BOOL(ret_sys_val) - // public readonly int $newLine = 0xa, - Z_PARAM_LONG(ch_nl) // public readonly int $maxContainerDepth = 10, Z_PARAM_LONG(max_depth) // public readonly int $maxAnnotations = 10, Z_PARAM_LONG(max_ann) - // public readonly int $maxAnnotationBuffered = 512, - Z_PARAM_LONG(max_ann_buf) - // public readonly int $symbolThreshold = 4096, - Z_PARAM_LONG(sym_thr) - // public readonly int $userValueThreshold = 4096, - Z_PARAM_LONG(uval_thr) - // public readonly int $chunkThreshold = 4096, - Z_PARAM_LONG(chunk_thr) - // public readonly int $allocationPageSize = 4096, - Z_PARAM_LONG(alloc_pgsz) + // public readonly int $annotationBufferSize = 0x4000, + Z_PARAM_LONG(ann_buf_siz) + // public readonly int $tempBufferSize = 0x4000, + Z_PARAM_LONG(tmp_buf_siz) // public readonly bool $skipCharacterValidation = false, Z_PARAM_BOOL(skip_validation) ZEND_PARSE_PARAMETERS_END(); - opt->opt.context_change_notifier = EMPTY_READER_CHANGE_NOTIFIER; if (opt->cb) { + zval zcb; + ZVAL_OBJ(&zcb, opt->cb); + zend_fcall_info_init(&zcb, 0, &opt->ccn.fci, &opt->ccn.fcc, NULL, NULL); + opt->opt.context_change_notifier.context = &opt->ccn; update_property_obj(&opt->std, ZEND_STRL("onContextChange"), opt->cb); + } else { + zend_update_property_null(NULL, &opt->std, ZEND_STRL("onContextChange")); } if (opt->cat) { update_property_obj(&opt->std, ZEND_STRL("catalog"), opt->cat); opt->opt.pcatalog = php_ion_obj(catalog, opt->cat)->cat; + } else { + zend_update_property_null(NULL, &opt->std, ZEND_STRL("catalog")); } if (opt->dec_ctx) { update_property_obj(&opt->std, ZEND_STRL("decimalContext"), opt->dec_ctx); opt->opt.decimal_context = &php_ion_obj(decimal_ctx, opt->dec_ctx)->ctx; + } else { + zend_update_property_null(NULL, &opt->std, ZEND_STRL("decimalContext")); } zend_update_property_bool(opt->std.ce, &opt->std, ZEND_STRL("returnSystemValues"), opt->opt.return_system_values = ret_sys_val); - zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("newLine"), - opt->opt.new_line_char = ch_nl); zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("maxContainerDepth"), - opt->opt.max_container_depth = max_depth); - zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("maxAnnotationCount"), - opt->opt.max_annotation_count = max_ann); - zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("maxAnnotationBuffered"), - opt->opt.max_annotation_buffered = max_ann_buf); - zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("symbolThreshold"), - opt->opt.symbol_threshold = sym_thr); - zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("userValueThreshold"), - opt->opt.user_value_threshold = uval_thr); - zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("chunkThreshold"), - opt->opt.chunk_threshold = chunk_thr); - zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("allocationPageSize"), - opt->opt.allocation_page_size = alloc_pgsz); + opt->opt.max_container_depth = (SIZE) max_depth); + zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("maxAnnotations"), + opt->opt.max_annotation_count = (SIZE) max_ann); + zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("annotationBufferSize"), + opt->opt.max_annotation_buffered = (SIZE) ann_buf_siz); + zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("tempBufferSize"), + opt->opt.chunk_threshold = opt->opt.user_value_threshold = opt->opt.symbol_threshold = (SIZE) tmp_buf_siz); zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("skipCharacterValidation"), - opt->opt.skip_character_validation = skip_validation); + opt->opt.skip_character_validation = (SIZE) skip_validation); } -ZEND_METHOD(ion_Reader_Reader, hasChildren) +static ZEND_METHOD(ion_Reader_Reader, hasChildren) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); @@ -308,7 +677,7 @@ ZEND_METHOD(ion_Reader_Reader, hasChildren) break; } } -ZEND_METHOD(ion_Reader_Reader, getChildren) +static ZEND_METHOD(ion_Reader_Reader, getChildren) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -319,7 +688,7 @@ ZEND_METHOD(ion_Reader_Reader, getChildren) RETURN_ZVAL(ZEND_THIS, 1, 0); } -ZEND_METHOD(ion_Reader_Reader, rewind) +static ZEND_METHOD(ion_Reader_Reader, rewind) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -328,7 +697,7 @@ ZEND_METHOD(ion_Reader_Reader, rewind) ION_CHECK(ion_reader_next(obj->reader, &obj->state)); } -ZEND_METHOD(ion_Reader_Reader, next) +static ZEND_METHOD(ion_Reader_Reader, next) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -344,7 +713,7 @@ ZEND_METHOD(ion_Reader_Reader, next) } ION_CHECK(ion_reader_next(obj->reader, &obj->state)); } -ZEND_METHOD(ion_Reader_Reader, valid) +static ZEND_METHOD(ion_Reader_Reader, valid) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -352,7 +721,7 @@ ZEND_METHOD(ion_Reader_Reader, valid) ZEND_PARSE_PARAMETERS_NONE(); RETURN_BOOL(obj->state != tid_none && obj->state != tid_EOF); } -ZEND_METHOD(ion_Reader_Reader, key) +static ZEND_METHOD(ion_Reader_Reader, key) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -360,12 +729,12 @@ ZEND_METHOD(ion_Reader_Reader, key) ZEND_PARSE_PARAMETERS_NONE(); RETURN_IONTYPE(obj->state); } -ZEND_METHOD(ion_Reader_Reader, current) +static ZEND_METHOD(ion_Reader_Reader, current) { ZEND_PARSE_PARAMETERS_NONE(); RETURN_ZVAL(ZEND_THIS, 1, 0); } -ZEND_METHOD(ion_Reader_Reader, getType) +static ZEND_METHOD(ion_Reader_Reader, getType) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -376,7 +745,7 @@ ZEND_METHOD(ion_Reader_Reader, getType) ION_CHECK(ion_reader_get_type(obj->reader, &typ)); RETURN_IONTYPE(typ); } -ZEND_METHOD(ion_Reader_Reader, hasAnnotations) +static ZEND_METHOD(ion_Reader_Reader, hasAnnotations) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -387,7 +756,7 @@ ZEND_METHOD(ion_Reader_Reader, hasAnnotations) ION_CHECK(ion_reader_has_any_annotations(obj->reader, &has)); RETURN_BOOL(has); } -ZEND_METHOD(ion_Reader_Reader, hasAnnotation) +static ZEND_METHOD(ion_Reader_Reader, hasAnnotation) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -402,7 +771,7 @@ ZEND_METHOD(ion_Reader_Reader, hasAnnotation) ION_CHECK(ion_reader_has_annotation(obj->reader, ion_string_from_zend(&ann_istr, ann_zstr), &has)); RETURN_BOOL(has); } -ZEND_METHOD(ion_Reader_Reader, isNull) +static ZEND_METHOD(ion_Reader_Reader, isNull) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -413,7 +782,7 @@ ZEND_METHOD(ion_Reader_Reader, isNull) ION_CHECK(ion_reader_is_null(obj->reader, &is)); RETURN_BOOL(is); } -ZEND_METHOD(ion_Reader_Reader, isInStruct) +static ZEND_METHOD(ion_Reader_Reader, isInStruct) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -424,7 +793,7 @@ ZEND_METHOD(ion_Reader_Reader, isInStruct) ION_CHECK(ion_reader_is_in_struct(obj->reader, &is)); RETURN_BOOL(is); } -ZEND_METHOD(ion_Reader_Reader, getFieldName) +static ZEND_METHOD(ion_Reader_Reader, getFieldName) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -435,7 +804,7 @@ ZEND_METHOD(ion_Reader_Reader, getFieldName) ION_CHECK(ion_reader_get_field_name(obj->reader, &name)); RETURN_STRINGL((char *) name.value, name.length); } -ZEND_METHOD(ion_Reader_Reader, getFieldNameSymbol) +static ZEND_METHOD(ion_Reader_Reader, getFieldNameSymbol) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -447,7 +816,7 @@ ZEND_METHOD(ion_Reader_Reader, getFieldNameSymbol) php_ion_symbol_zval(sym_ptr, return_value); } -ZEND_METHOD(ion_Reader_Reader, getAnnotations) +static ZEND_METHOD(ion_Reader_Reader, getAnnotations) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -471,14 +840,19 @@ ZEND_METHOD(ion_Reader_Reader, getAnnotations) efree(ptr); ION_CHECK(err); } -ZEND_METHOD(ion_Reader_Reader, getAnnotationSymbols) +static ZEND_METHOD(ion_Reader_Reader, getAnnotationSymbols) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); ZEND_PARSE_PARAMETERS_NONE(); - int32_t count, max = php_ion_obj(reader_options, obj->opt)->opt.max_annotation_count; + int32_t count, max; + if (obj->opt) { + max = php_ion_obj(reader_options, obj->opt)->opt.max_annotation_count; + } else { + max = 10; + } ION_SYMBOL *ptr = ecalloc(sizeof(*ptr), max); iERR err = ion_reader_get_annotation_symbols(obj->reader, ptr, max, &count); if (!err) { @@ -492,7 +866,7 @@ ZEND_METHOD(ion_Reader_Reader, getAnnotationSymbols) efree(ptr); ION_CHECK(err); } -ZEND_METHOD(ion_Reader_Reader, countAnnotations) +static ZEND_METHOD(ion_Reader_Reader, countAnnotations) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -503,7 +877,7 @@ ZEND_METHOD(ion_Reader_Reader, countAnnotations) ION_CHECK(ion_reader_get_annotation_count(obj->reader, &sz)); RETURN_LONG(sz); } -ZEND_METHOD(ion_Reader_Reader, getAnnotation) +static ZEND_METHOD(ion_Reader_Reader, getAnnotation) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -517,7 +891,7 @@ ZEND_METHOD(ion_Reader_Reader, getAnnotation) ION_CHECK(ion_reader_get_an_annotation(obj->reader, idx, &ann)); RETURN_STRINGL((char *) ann.value, ann.length); } -ZEND_METHOD(ion_Reader_Reader, getAnnotationSymbol) +static ZEND_METHOD(ion_Reader_Reader, getAnnotationSymbol) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -531,7 +905,7 @@ ZEND_METHOD(ion_Reader_Reader, getAnnotationSymbol) ION_CHECK(ion_reader_get_an_annotation_symbol(obj->reader, idx, &sym)); php_ion_symbol_zval(&sym, return_value); } -ZEND_METHOD(ion_Reader_Reader, readNull) +static ZEND_METHOD(ion_Reader_Reader, readNull) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -540,9 +914,9 @@ ZEND_METHOD(ion_Reader_Reader, readNull) ION_TYPE typ; ION_CHECK(ion_reader_read_null(obj->reader, &typ)); - RETURN_OBJ_COPY(php_ion_type_fetch(typ)); + RETURN_IONTYPE(typ); } -ZEND_METHOD(ion_Reader_Reader, readBool) +static ZEND_METHOD(ion_Reader_Reader, readBool) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -553,25 +927,27 @@ ZEND_METHOD(ion_Reader_Reader, readBool) ION_CHECK(ion_reader_read_bool(obj->reader, &b)); RETURN_BOOL(b); } -ZEND_METHOD(ion_Reader_Reader, readInt) +static ZEND_METHOD(ion_Reader_Reader, readInt) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); ZEND_PARSE_PARAMETERS_NONE(); - php_ion_unserialize_int(obj, return_value); + php_ion_reader_read_int(obj->reader, return_value); } -ZEND_METHOD(ion_Reader_Reader, readFloat) +static ZEND_METHOD(ion_Reader_Reader, readFloat) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); ZEND_PARSE_PARAMETERS_NONE(); - ION_CHECK(ion_reader_read_double(obj->reader, &Z_DVAL_P(return_value))); + double dval; + ION_CHECK(ion_reader_read_double(obj->reader, &dval)); + RETURN_DOUBLE(dval); } -ZEND_METHOD(ion_Reader_Reader, readDecimal) +static ZEND_METHOD(ion_Reader_Reader, readDecimal) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -583,16 +959,16 @@ ZEND_METHOD(ion_Reader_Reader, readDecimal) ION_CHECK(ion_reader_read_ion_decimal(obj->reader, &dec->dec)); php_ion_decimal_ctor(dec); } -ZEND_METHOD(ion_Reader_Reader, readTimestamp) +static ZEND_METHOD(ion_Reader_Reader, readTimestamp) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); ZEND_PARSE_PARAMETERS_NONE(); - php_ion_unserialize_timestamp(obj, return_value); + php_ion_reader_read_timestamp(obj->reader, obj->opt ? &php_ion_obj(reader_options, obj->opt)->opt : NULL, return_value); } -ZEND_METHOD(ion_Reader_Reader, readSymbol) +static ZEND_METHOD(ion_Reader_Reader, readSymbol) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -603,7 +979,7 @@ ZEND_METHOD(ion_Reader_Reader, readSymbol) ION_CHECK(ion_reader_read_ion_symbol(obj->reader, &sym)); php_ion_symbol_zval(&sym, return_value); } -ZEND_METHOD(ion_Reader_Reader, readString) +static ZEND_METHOD(ion_Reader_Reader, readString) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -650,30 +1026,28 @@ static void read_part(INTERNAL_FUNCTION_PARAMETERS, read_part_fn fn) RETURN_TRUE; } fail: - if (zstr != Z_STR_P(ref)) { - zend_string_release(zstr); - } + zend_string_release(zstr); ZVAL_EMPTY_STRING(ref); RETURN_FALSE; } -ZEND_METHOD(ion_Reader_Reader, readStringPart) +static ZEND_METHOD(ion_Reader_Reader, readStringPart) { read_part(INTERNAL_FUNCTION_PARAM_PASSTHRU, ion_reader_read_partial_string); } -ZEND_METHOD(ion_Reader_Reader, readLob) +static ZEND_METHOD(ion_Reader_Reader, readLob) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); ZEND_PARSE_PARAMETERS_NONE(); - php_ion_unserialize_lob(obj, return_value); + php_ion_reader_read_lob(obj->reader, return_value); } -ZEND_METHOD(ion_Reader_Reader, readLobPart) +static ZEND_METHOD(ion_Reader_Reader, readLobPart) { read_part(INTERNAL_FUNCTION_PARAM_PASSTHRU, ion_reader_read_lob_partial_bytes); } -ZEND_METHOD(ion_Reader_Reader, getPosition) +static ZEND_METHOD(ion_Reader_Reader, getPosition) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -685,7 +1059,7 @@ ZEND_METHOD(ion_Reader_Reader, getPosition) ION_CHECK(ion_reader_get_position(obj->reader, &bytes, &dummy, &dummy)); RETURN_LONG(bytes); } -ZEND_METHOD(ion_Reader_Reader, getDepth) +static ZEND_METHOD(ion_Reader_Reader, getDepth) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -696,7 +1070,7 @@ ZEND_METHOD(ion_Reader_Reader, getDepth) ION_CHECK(ion_reader_get_depth(obj->reader, &depth)); RETURN_LONG(depth); } -ZEND_METHOD(ion_Reader_Reader, seek) +static ZEND_METHOD(ion_Reader_Reader, seek) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -710,7 +1084,7 @@ ZEND_METHOD(ion_Reader_Reader, seek) ION_CHECK(ion_reader_seek(obj->reader, off, len)); } -ZEND_METHOD(ion_Reader_Reader, getValueOffset) +static ZEND_METHOD(ion_Reader_Reader, getValueOffset) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -721,7 +1095,7 @@ ZEND_METHOD(ion_Reader_Reader, getValueOffset) ION_CHECK(ion_reader_get_value_offset(obj->reader, &off)); RETURN_LONG(off); } -ZEND_METHOD(ion_Reader_Reader, getValueLength) +static ZEND_METHOD(ion_Reader_Reader, getValueLength) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -732,7 +1106,7 @@ ZEND_METHOD(ion_Reader_Reader, getValueLength) ION_CHECK(ion_reader_get_value_length(obj->reader, &len)); RETURN_LONG(len); } -ZEND_METHOD(ion_Reader_Buffer_Reader, __construct) +static ZEND_METHOD(ion_Reader_Buffer_Reader, __construct) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); PTR_CHECK(obj); @@ -749,7 +1123,7 @@ ZEND_METHOD(ion_Reader_Buffer_Reader, __construct) php_ion_reader_ctor(obj); } -ZEND_METHOD(ion_Reader_Buffer_Reader, getBuffer) +static ZEND_METHOD(ion_Reader_Buffer_Reader, getBuffer) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -758,7 +1132,7 @@ ZEND_METHOD(ion_Reader_Buffer_Reader, getBuffer) RETURN_STR_COPY(obj->buffer); } -ZEND_METHOD(ion_Reader_Stream_Reader, __construct) +static ZEND_METHOD(ion_Reader_Stream_Reader, __construct) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); PTR_CHECK(obj); @@ -775,7 +1149,7 @@ ZEND_METHOD(ion_Reader_Stream_Reader, __construct) php_ion_reader_ctor(obj); } -ZEND_METHOD(ion_Reader_Stream_Reader, getStream) +static ZEND_METHOD(ion_Reader_Stream_Reader, getStream) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -786,7 +1160,7 @@ ZEND_METHOD(ion_Reader_Stream_Reader, getStream) GC_ADDREF(obj->stream.ptr->res); RETURN_RES(obj->stream.ptr->res); } -ZEND_METHOD(ion_Reader_Stream_Reader, resetStream) +static ZEND_METHOD(ion_Reader_Stream_Reader, resetStream) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -805,14 +1179,14 @@ ZEND_METHOD(ion_Reader_Stream_Reader, resetStream) PTR_CHECK(obj->stream.ptr); Z_ADDREF_P(zstream); } -ZEND_METHOD(ion_Reader_Stream_Reader, resetStreamWithLength) +static ZEND_METHOD(ion_Reader_Stream_Reader, resetStreamWithLength) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); zval *zstream; zend_long length; - ZEND_PARSE_PARAMETERS_START(1, 2); + ZEND_PARSE_PARAMETERS_START(2, 2); Z_PARAM_RESOURCE(zstream); Z_PARAM_LONG(length) ZEND_PARSE_PARAMETERS_END(); @@ -826,20 +1200,18 @@ ZEND_METHOD(ion_Reader_Stream_Reader, resetStreamWithLength) PTR_CHECK(obj->stream.ptr); Z_ADDREF_P(zstream); } -ZEND_METHOD(ion_Writer_Options, __construct) +static ZEND_METHOD(ion_Writer_Options, __construct) { php_ion_writer_options *obj = php_ion_obj(writer_options, Z_OBJ_P(ZEND_THIS)); PTR_CHECK(obj); zend_bool binary = false, compact_floats = false, escape = false, pretty = false, - tabs = true, small_cntr_inl = true, suppress_sys = false, flush = false; - zend_long indent = 2, max_depth = 10, max_ann = 10, temp = 0x400, alloc = 0x1000; - ZEND_PARSE_PARAMETERS_START(0, 16) + tabs = true, flush = false; + zend_long indent = 2, max_depth = 10, max_ann = 10, temp = 0x4000; + ZEND_PARSE_PARAMETERS_START(0, 12) Z_PARAM_OPTIONAL //public readonly ?\ion\Catalog $catalog = null, Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->cat, ce_Catalog) - //public readonly ?\ion\Collection $encodingSymbolTable = null, - Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->col, ce_Collection) //public readonly ?\ion\Decimal\Context $decimalContext = null, Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->dec_ctx, ce_Decimal_Context) //public readonly bool $outputBinary = false, @@ -854,61 +1226,50 @@ ZEND_METHOD(ion_Writer_Options, __construct) Z_PARAM_BOOL(tabs) //public readonly int $indentSize = 2, Z_PARAM_LONG(indent) - //public readonly bool $smallContainersInline = true, - Z_PARAM_BOOL(small_cntr_inl) - //public readonly bool $suppressSystemValues = false, - Z_PARAM_BOOL(suppress_sys) //public readonly bool $flushEveryValue = false, Z_PARAM_BOOL(flush) //public readonly int $maxContainerDepth = 10, Z_PARAM_LONG(max_depth) //public readonly int $maxAnnotations = 10, Z_PARAM_LONG(max_ann) - //public readonly int $tempBufferSize = 0x400, + //public readonly int $tempBufferSize = 0x4000, Z_PARAM_LONG(temp) - //public readonly int $allocationPageSize = 0x1000, - Z_PARAM_LONG(alloc) ZEND_PARSE_PARAMETERS_END(); if (obj->cat) { update_property_obj(&obj->std, ZEND_STRL("catalog"), obj->cat); obj->opt.pcatalog = php_ion_obj(catalog, obj->cat)->cat; - } - if (obj->col) { - // TODO + } else { + zend_update_property_null(NULL, &obj->std, ZEND_STRL("catalog")); } if (obj->dec_ctx) { update_property_obj(&obj->std, ZEND_STRL("decimalContext"), obj->dec_ctx); obj->opt.decimal_context = &php_ion_obj(decimal_ctx, obj->dec_ctx)->ctx; + } else { + zend_update_property_null(NULL, &obj->std, ZEND_STRL("decimalContext")); } zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("outputBinary"), obj->opt.output_as_binary = binary); - zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("comactFloats"), + zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("compactFloats"), obj->opt.compact_floats = compact_floats); - zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("excapeNonAscii"), + zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("escapeNonAscii"), obj->opt.escape_all_non_ascii = escape); zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("prettyPrint"), obj->opt.pretty_print = pretty); zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("indentTabs"), obj->opt.indent_with_tabs = tabs); zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("indentSize"), - obj->opt.indent_size = indent); - zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("smallContainersInline"), - obj->opt.small_containers_in_line = small_cntr_inl); - zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("suppressSystemValues"), - obj->opt.supress_system_values = suppress_sys); + obj->opt.indent_size = (SIZE) indent); zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("flushEveryValue"), obj->opt.flush_every_value = flush); zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("maxContainerDepth"), - obj->opt.max_container_depth = max_depth); + obj->opt.max_container_depth = (SIZE) max_depth); zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("maxAnnotations"), - obj->opt.max_annotation_count = max_ann); + obj->opt.max_annotation_count = (SIZE) max_ann); zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("tempBufferSize"), - obj->opt.temp_buffer_size = temp); - zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("allocationPageSize"), - obj->opt.allocation_page_size = alloc); + obj->opt.temp_buffer_size = (SIZE) temp); } -ZEND_METHOD(ion_Writer_Writer, writeNull) +static ZEND_METHOD(ion_Writer_Writer, writeNull) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -917,7 +1278,7 @@ ZEND_METHOD(ion_Writer_Writer, writeNull) ION_CHECK(ion_writer_write_null(obj->writer)); } -ZEND_METHOD(ion_Writer_Writer, writeTypedNull) +static ZEND_METHOD(ion_Writer_Writer, writeTypedNull) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -927,11 +1288,9 @@ ZEND_METHOD(ion_Writer_Writer, writeTypedNull) Z_PARAM_OBJ_OF_CLASS(typ_obj, ce_Type) ZEND_PARSE_PARAMETERS_END(); - php_ion_type *typ = php_ion_obj(type, typ_obj); - OBJ_CHECK(typ); - ION_CHECK(ion_writer_write_typed_null(obj->writer, php_ion_obj(type, typ)->typ)); + ION_CHECK(ion_writer_write_typed_null(obj->writer, ion_type_from_enum(typ_obj))); } -ZEND_METHOD(ion_Writer_Writer, writeBool) +static ZEND_METHOD(ion_Writer_Writer, writeBool) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -943,7 +1302,7 @@ ZEND_METHOD(ion_Writer_Writer, writeBool) ION_CHECK(ion_writer_write_bool(obj->writer, b)); } -ZEND_METHOD(ion_Writer_Writer, writeInt) +static ZEND_METHOD(ion_Writer_Writer, writeInt) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -964,7 +1323,7 @@ ZEND_METHOD(ion_Writer_Writer, writeInt) ION_CHECK(ion_writer_write_int64(obj->writer, l)); } } -ZEND_METHOD(ion_Writer_Writer, writeFloat) +static ZEND_METHOD(ion_Writer_Writer, writeFloat) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -976,7 +1335,7 @@ ZEND_METHOD(ion_Writer_Writer, writeFloat) ION_CHECK(ion_writer_write_double(obj->writer, d)); } -ZEND_METHOD(ion_Writer_Writer, writeDecimal) +static ZEND_METHOD(ion_Writer_Writer, writeDecimal) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -988,14 +1347,23 @@ ZEND_METHOD(ion_Writer_Writer, writeDecimal) ZEND_PARSE_PARAMETERS_END(); if (dec_str) { - ION_STRING s; - ION_CHECK(ion_writer_write_string(obj->writer, ion_string_from_zend(&s, dec_str))); + decContext *ctx = &php_ion_globals.decimal.ctx; + ION_DECIMAL dec = {0}; + + if (obj->opt) { + php_ion_writer_options *opt_obj = php_ion_obj(writer_options, obj->opt); + if (opt_obj->opt.decimal_context) { + ctx = opt_obj->opt.decimal_context; + } + } + ION_CHECK(ion_decimal_from_string(&dec, dec_str->val, ctx)); + ION_CHECK(ion_writer_write_ion_decimal(obj->writer, &dec)); } else { php_ion_decimal *dec = php_ion_obj(decimal, dec_obj); ION_CHECK(ion_writer_write_ion_decimal(obj->writer, &dec->dec)); } } -ZEND_METHOD(ion_Writer_Writer, writeTimestamp) +static ZEND_METHOD(ion_Writer_Writer, writeTimestamp) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1022,7 +1390,7 @@ ZEND_METHOD(ion_Writer_Writer, writeTimestamp) } ION_CHECK(ion_writer_write_timestamp(obj->writer, &tmp)); } -ZEND_METHOD(ion_Writer_Writer, writeSymbol) +static ZEND_METHOD(ion_Writer_Writer, writeSymbol) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1042,7 +1410,7 @@ ZEND_METHOD(ion_Writer_Writer, writeSymbol) ION_CHECK(ion_writer_write_ion_symbol(obj->writer, &sym->sym)); } } -ZEND_METHOD(ion_Writer_Writer, writeString) +static ZEND_METHOD(ion_Writer_Writer, writeString) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1055,7 +1423,7 @@ ZEND_METHOD(ion_Writer_Writer, writeString) ION_STRING is; ION_CHECK(ion_writer_write_string(obj->writer, ion_string_from_zend(&is, str))); } -ZEND_METHOD(ion_Writer_Writer, writeCLob) +static ZEND_METHOD(ion_Writer_Writer, writeCLob) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1067,7 +1435,7 @@ ZEND_METHOD(ion_Writer_Writer, writeCLob) ION_CHECK(ion_writer_write_clob(obj->writer, (BYTE *) str->val, str->len)); } -ZEND_METHOD(ion_Writer_Writer, writeBLob) +static ZEND_METHOD(ion_Writer_Writer, writeBLob) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1079,7 +1447,7 @@ ZEND_METHOD(ion_Writer_Writer, writeBLob) ION_CHECK(ion_writer_write_blob(obj->writer, (BYTE *) str->val, str->len)); } -ZEND_METHOD(ion_Writer_Writer, startLob) +static ZEND_METHOD(ion_Writer_Writer, startLob) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1089,11 +1457,9 @@ ZEND_METHOD(ion_Writer_Writer, startLob) Z_PARAM_OBJ_OF_CLASS(typ_obj, ce_Type) ZEND_PARSE_PARAMETERS_END(); - php_ion_type *typ = php_ion_obj(type, typ_obj); - OBJ_CHECK(typ); - ION_CHECK(ion_writer_start_lob(obj->writer, php_ion_obj(type, typ)->typ)); + ION_CHECK(ion_writer_start_lob(obj->writer, ion_type_from_enum(typ_obj))); } -ZEND_METHOD(ion_Writer_Writer, appendLob) +static ZEND_METHOD(ion_Writer_Writer, appendLob) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1105,7 +1471,7 @@ ZEND_METHOD(ion_Writer_Writer, appendLob) ION_CHECK(ion_writer_append_lob(obj->writer, (BYTE *) str->val, str->len)); } -ZEND_METHOD(ion_Writer_Writer, finishLob) +static ZEND_METHOD(ion_Writer_Writer, finishLob) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1114,7 +1480,7 @@ ZEND_METHOD(ion_Writer_Writer, finishLob) ION_CHECK(ion_writer_finish_lob(obj->writer)); } -ZEND_METHOD(ion_Writer_Writer, startContainer) +static ZEND_METHOD(ion_Writer_Writer, startContainer) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1124,11 +1490,9 @@ ZEND_METHOD(ion_Writer_Writer, startContainer) Z_PARAM_OBJ_OF_CLASS(typ_obj, ce_Type) ZEND_PARSE_PARAMETERS_END(); - php_ion_type *typ = php_ion_obj(type, typ_obj); - OBJ_CHECK(typ); - ION_CHECK(ion_writer_start_container(obj->writer, php_ion_obj(type, typ)->typ)); + ION_CHECK(ion_writer_start_container(obj->writer, ion_type_from_enum(typ_obj))); } -ZEND_METHOD(ion_Writer_Writer, finishContainer) +static ZEND_METHOD(ion_Writer_Writer, finishContainer) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1137,7 +1501,7 @@ ZEND_METHOD(ion_Writer_Writer, finishContainer) ION_CHECK(ion_writer_finish_container(obj->writer)); } -ZEND_METHOD(ion_Writer_Writer, writeFieldName) +static ZEND_METHOD(ion_Writer_Writer, writeFieldName) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1157,7 +1521,7 @@ ZEND_METHOD(ion_Writer_Writer, writeFieldName) ION_CHECK(ion_writer_write_field_name_symbol(obj->writer, &sym->sym)); } } -ZEND_METHOD(ion_Writer_Writer, writeAnnotation) +static ZEND_METHOD(ion_Writer_Writer, writeAnnotation) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1170,7 +1534,7 @@ ZEND_METHOD(ion_Writer_Writer, writeAnnotation) for (unsigned i = 0; i < argc; ++i) { switch (Z_TYPE(args[i])) { - case IS_STRING: + case IS_STRING: ; ION_STRING is; ION_CHECK(ion_writer_add_annotation(obj->writer, ion_string_from_zend(&is, Z_STR(args[i])))); break; @@ -1181,7 +1545,7 @@ ZEND_METHOD(ion_Writer_Writer, writeAnnotation) } } } -ZEND_METHOD(ion_Writer_Writer, getDepth) +static ZEND_METHOD(ion_Writer_Writer, getDepth) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1192,7 +1556,7 @@ ZEND_METHOD(ion_Writer_Writer, getDepth) ION_CHECK(ion_writer_get_depth(obj->writer, &depth)); RETURN_LONG(depth); } -ZEND_METHOD(ion_Writer_Writer, flush) +static ZEND_METHOD(ion_Writer_Writer, flush) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1201,12 +1565,9 @@ ZEND_METHOD(ion_Writer_Writer, flush) SIZE flushed; ION_CHECK(ion_writer_flush(obj->writer, &flushed)); - if (obj->type == BUFFER_WRITER) { - smart_str_0(&obj->buffer.str); - } RETURN_LONG(flushed); } -ZEND_METHOD(ion_Writer_Writer, finish) +static ZEND_METHOD(ion_Writer_Writer, finish) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1215,45 +1576,40 @@ ZEND_METHOD(ion_Writer_Writer, finish) SIZE flushed; ION_CHECK(ion_writer_finish(obj->writer, &flushed)); - if (obj->type == BUFFER_WRITER) { - smart_str_0(&obj->buffer.str); - } RETURN_LONG(flushed); } -ZEND_METHOD(ion_Writer_Writer, writeOne) -{ -} -ZEND_METHOD(ion_Writer_Writer, writeAll) -{ -} -ZEND_METHOD(ion_Writer_Buffer_Writer, __construct) +static ZEND_METHOD(ion_Writer_Buffer_Writer, __construct) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); PTR_CHECK(obj); - zval *ref; - ZEND_PARSE_PARAMETERS_START(1, 2) - Z_PARAM_ZVAL(ref) + ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Writer_Options) ZEND_PARSE_PARAMETERS_END(); obj->type = BUFFER_WRITER; - ZVAL_COPY(&obj->buffer.val, ref); - zval_dtor(Z_REFVAL_P(ref)); - php_ion_writer_ctor(obj); } -ZEND_METHOD(ion_Writer_Buffer_Writer, getBuffer) +static ZEND_METHOD(ion_Writer_Buffer_Writer, getBuffer) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); ZEND_PARSE_PARAMETERS_NONE(); - RETVAL_STR(zend_string_dup(obj->buffer.str.s, 0)); + RETVAL_STR(php_ion_writer_buffer_copy(obj)); } -ZEND_METHOD(ion_Writer_Stream_Writer, __construct) +static ZEND_METHOD(ion_Writer_Buffer_Writer, resetBuffer) +{ + php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + ZEND_PARSE_PARAMETERS_NONE(); + + php_ion_writer_buffer_reset(obj); +} +static ZEND_METHOD(ion_Writer_Stream_Writer, __construct) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); PTR_CHECK(obj); @@ -1270,7 +1626,7 @@ ZEND_METHOD(ion_Writer_Stream_Writer, __construct) php_ion_writer_ctor(obj); } -ZEND_METHOD(ion_Writer_Stream_Writer, getStream) +static ZEND_METHOD(ion_Writer_Stream_Writer, getStream) { php_ion_writer *obj = php_ion_obj(writer, Z_OBJ_P(ZEND_THIS)); OBJ_CHECK(obj); @@ -1282,31 +1638,24 @@ ZEND_METHOD(ion_Writer_Stream_Writer, getStream) RETURN_RES(obj->stream.ptr->res); } -ZEND_METHOD(ion_PHP_Serializer, __construct) +static ZEND_METHOD(ion_Serializer_PHP, __construct) { - zend_object *obj = Z_OBJ_P(ZEND_THIS); + php_ion_serializer_php *obj = php_ion_obj(serializer_php, Z_OBJ_P(ZEND_THIS)); + PTR_CHECK(obj); - zend_object *writer_obj; - zend_bool call_magic = true; - zend_string *call_custom = NULL; - ZEND_PARSE_PARAMETERS_START(1, 3) - Z_PARAM_OBJ_OF_CLASS(writer_obj, ce_Writer) + obj->serializer.call_magic = true; + + ZEND_PARSE_PARAMETERS_START(0, 4) Z_PARAM_OPTIONAL - Z_PARAM_BOOL(call_magic) - Z_PARAM_STR_OR_NULL(call_custom) + Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Writer_Options) + Z_PARAM_BOOL(obj->serializer.multi_seq) + Z_PARAM_BOOL(obj->serializer.call_magic) + Z_PARAM_STR_OR_NULL(obj->serializer.call_custom) ZEND_PARSE_PARAMETERS_END(); - update_property_obj(obj, ZEND_STRL("writer"), writer_obj); - zend_update_property_bool(obj->ce, obj, ZEND_STRL("callMagicSerialize"), - php_ion_obj(writer, writer_obj)->php.call_magic_serialize = call_magic); - if (call_custom) { - zend_update_property_str(obj->ce, obj, ZEND_STRL("callCustomSerialize"), call_custom); - php_ion_obj(writer, writer_obj)->php.custom_serialize = zend_string_tolower(call_custom); - } else { - zend_update_property_null(obj->ce, obj, ZEND_STRL("callCustomSerialize")); - } + php_ion_serializer_php_ctor(obj); } -ZEND_METHOD(ion_PHP_Serializer, __invoke) +static ZEND_METHOD(ion_Serializer_PHP, serialize) { zend_object *obj = Z_OBJ_P(ZEND_THIS); @@ -1315,117 +1664,72 @@ ZEND_METHOD(ion_PHP_Serializer, __invoke) Z_PARAM_ZVAL(data) ZEND_PARSE_PARAMETERS_END(); - zval tmp, *zwriter; - if ((zwriter = zend_read_property(obj->ce, obj, ZEND_STRL("writer"), 0, &tmp))) { - if (Z_OBJCE_P(zwriter) == ce_Writer_Buffer_Writer || Z_OBJCE_P(zwriter) == ce_Writer_Stream_Writer) { - php_ion_writer *writer = php_ion_obj(writer, Z_OBJ_P(zwriter)); - php_ion_serialize(writer, data, &writer->buffer.val); - RETURN_STR_COPY(writer->buffer.str.s); - } else { - zend_call_method_with_1_params(obj, obj->ce, NULL /* TODO */, "serialize", return_value, data); - } - } + php_ion_serialize(&php_ion_obj(serializer_php, obj)->serializer, data, return_value); } -ZEND_METHOD(ion_PHP_Serializer, serialize) +static ZEND_FUNCTION(ion_serialize) { - //zend_object *obj = Z_OBJ_P(ZEND_THIS); - zval *data; - ZEND_PARSE_PARAMETERS_START(1, 1) + zend_object *zo_ser = NULL; + ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_ZVAL(data) - ZEND_PARSE_PARAMETERS_END(); - - // TODO -} -ZEND_METHOD(ion_PHP_Unserializer, __construct) -{ - zend_object *obj = Z_OBJ_P(ZEND_THIS); - - zend_object *reader_obj; - zend_bool call_magic = true; - zend_string *call_custom = NULL; - ZEND_PARSE_PARAMETERS_START(1, 3) - Z_PARAM_OBJ_OF_CLASS(reader_obj, ce_Reader) Z_PARAM_OPTIONAL - Z_PARAM_BOOL(call_magic) - Z_PARAM_STR_OR_NULL(call_custom) + Z_PARAM_OBJ_OF_CLASS_OR_NULL(zo_ser, ce_Serializer) ZEND_PARSE_PARAMETERS_END(); - update_property_obj(obj, ZEND_STRL("reader"), reader_obj); - zend_update_property_bool(obj->ce, obj, ZEND_STRL("callMagicUnserialize"), - php_ion_obj(reader, reader_obj)->php.call_magic_unserialize = call_magic); - if (call_custom) { - zend_update_property_str(obj->ce, obj, ZEND_STRL("callCustomUnserialize"), call_custom); - php_ion_obj(reader, reader_obj)->php.custom_unserialize = zend_string_tolower(call_custom); + if (!zo_ser || zo_ser->ce == ce_Serializer_PHP) { + // default, fast path + php_ion_serializer *ser = zo_ser ? &php_ion_obj(serializer_php, zo_ser)->serializer : NULL; + php_ion_serialize(ser, data, return_value); } else { - zend_update_property_null(obj->ce, obj, ZEND_STRL("callCustomUnserialize")); + zend_call_method_with_1_params(zo_ser, NULL, NULL, "serialize", return_value, data); } } -ZEND_METHOD(ion_PHP_Unserializer, __invoke) -{ - zend_object *obj = Z_OBJ_P(ZEND_THIS); - zval zreader; - if (zend_read_property(obj->ce, obj, ZEND_STRL("reader"), 0, &zreader)) { - if (Z_OBJCE(zreader) == ce_Reader_Buffer_Reader || Z_OBJCE(zreader) == ce_Reader_Stream_Reader) { - php_ion_unserialize(php_ion_obj(reader, Z_OBJ(zreader)), NULL, return_value); - } else { - zend_call_method_with_0_params(obj, obj->ce, NULL /* TODO */, "unserialize", return_value); - } - } -} -ZEND_METHOD(ion_PHP_Unserializer, unserialize) +static ZEND_METHOD(ion_Unserializer_PHP, __construct) { - //zend_object *obj = Z_OBJ_P(ZEND_THIS); + php_ion_unserializer_php *obj = php_ion_obj(unserializer_php, Z_OBJ_P(ZEND_THIS)); + PTR_CHECK(obj); - ZEND_PARSE_PARAMETERS_NONE(); + obj->unserializer.call_magic = true; - // TODO -} - -ZEND_FUNCTION(ion_serialize) -{ - zval *zv; - zend_object *zo_opt = NULL; - ZEND_PARSE_PARAMETERS_START(1, 2) - Z_PARAM_ZVAL(zv) + ZEND_PARSE_PARAMETERS_START(0, 4) Z_PARAM_OPTIONAL - Z_PARAM_OBJ_OF_CLASS_OR_NULL(zo_opt, ce_Writer_Options) + Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Reader_Options) + Z_PARAM_BOOL(obj->unserializer.multi_seq) + Z_PARAM_BOOL(obj->unserializer.call_magic) + Z_PARAM_STR_OR_NULL(obj->unserializer.call_custom) ZEND_PARSE_PARAMETERS_END(); - zval zwriter; - object_init_ex(&zwriter, ce_Writer_Buffer_Writer); + php_ion_unserializer_php_ctor(obj); +} +static ZEND_METHOD(ion_Unserializer_PHP, unserialize) +{ + zend_object *obj = Z_OBJ_P(ZEND_THIS); - php_ion_writer *obj = php_ion_obj(writer, Z_OBJ(zwriter)); - obj->opt = zo_opt; - obj->type = BUFFER_WRITER; - obj->php.call_magic_serialize = true; - php_ion_writer_ctor(obj); + zval *data; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_ZVAL(data) + ZEND_PARSE_PARAMETERS_END(); - php_ion_serialize(obj, zv, return_value); - zval_ptr_dtor(&zwriter); + php_ion_unserialize(&php_ion_obj(unserializer_php, obj)->unserializer, data, return_value); } -ZEND_FUNCTION(ion_unserialize) +static ZEND_FUNCTION(ion_unserialize) { - zend_string *zstr; - zend_object *zo_opt = NULL; + zval *data; + zend_object *zo_ser = NULL; ZEND_PARSE_PARAMETERS_START(1, 2) - Z_PARAM_STR(zstr) + Z_PARAM_ZVAL(data) Z_PARAM_OPTIONAL - Z_PARAM_OBJ_OF_CLASS_OR_NULL(zo_opt, ce_Reader_Options) + Z_PARAM_OBJ_OF_CLASS_OR_NULL(zo_ser, ce_Unserializer) ZEND_PARSE_PARAMETERS_END(); - zval zreader; - object_init_ex(&zreader, ce_Reader_Buffer_Reader); - - php_ion_reader *obj = php_ion_obj(reader, Z_OBJ(zreader)); - obj->opt = zo_opt; - obj->type = BUFFER_READER; - obj->buffer = zend_string_copy(zstr); - obj->php.call_magic_unserialize = true; - php_ion_reader_ctor(obj); - php_ion_unserialize(obj, NULL, return_value); - zval_ptr_dtor(&zreader); + if (!zo_ser || zo_ser->ce == ce_Unserializer_PHP) { + // default, fast path + php_ion_unserializer *ser = zo_ser ? &php_ion_obj(unserializer_php, zo_ser)->unserializer : NULL; + php_ion_unserialize(ser, data, return_value); + } else { + zend_call_method_with_1_params(zo_ser, NULL, NULL, "__invoke", return_value, data); + } } PHP_RINIT_FUNCTION(ion) @@ -1434,6 +1738,7 @@ PHP_RINIT_FUNCTION(ion) ZEND_TSRMLS_CACHE_UPDATE(); #endif + php_ion_globals_symbols_init(); php_ion_globals_serializer_init(); php_ion_globals_unserializer_init(); return SUCCESS; @@ -1441,70 +1746,98 @@ PHP_RINIT_FUNCTION(ion) PHP_RSHUTDOWN_FUNCTION(ion) { - php_ion_globals_serializer_dtor(); php_ion_globals_unserializer_dtor(); + php_ion_globals_serializer_dtor(); + php_ion_globals_symbols_dtor(); return SUCCESS; } -php_ion_decl(type, Type); -php_ion_decl(symbol, Symbol, php_ion_symbol_dtor(obj)); -php_ion_decl(symbol_iloc, Symbol_ImportLocation, php_ion_symbol_iloc_dtor(obj)); -php_ion_decl(symbol_table, Symbol_Table); -php_ion_decl(decimal, Decimal, php_ion_decimal_dtor(obj)); -php_ion_decl(decimal_ctx, Decimal_Context); -php_ion_decl(timestamp, Timestamp, php_ion_timestamp_dtor(obj)); -php_ion_decl(catalog, Catalog); -php_ion_decl(reader_options, Reader_Options); -php_ion_decl(reader, Reader_Reader, php_ion_reader_dtor(obj)); -php_ion_decl(writer_options, Writer_Options); -php_ion_decl(writer, Writer_Writer, php_ion_writer_dtor(obj)); +#define ZEND_ARG_VARIADIC_OBJ_TYPE_MASK(pass_by_ref, name, classname, type_mask, default_value) \ + { #name, ZEND_TYPE_INIT_CLASS_CONST_MASK(#classname, ((type_mask) | _ZEND_ARG_INFO_FLAGS(pass_by_ref, 1, 0))), default_value }, +#include "ion_arginfo.h" + +// spl_iterators.h includes ext/pcre/php_pcre.h which might not find pcre2.h +extern PHPAPI zend_class_entry *spl_ce_RecursiveIterator; PHP_MINIT_FUNCTION(ion) { - ce_Annotation = register_class_ion_Annotation(); - - php_ion_register(type, Type); - php_ion_register(symbol, Symbol); - php_ion_register(symbol_iloc, Symbol_ImportLocation); - php_ion_register(symbol_table, Symbol_Table); - - ce_Symbol_System = register_class_ion_Symbol_System(); - ce_Symbol_System_SID = register_class_ion_Symbol_System_SID(); + // true globals + if (SUCCESS != g_sym_init()) { + return FAILURE; + } + g_intern_str_init(); - ce_Collection = register_class_ion_Collection(); + // Catalog + php_ion_register(catalog, Catalog, zend_ce_countable); + // Decimal php_ion_register(decimal, Decimal); php_ion_register(decimal_ctx, Decimal_Context); - php_ion_register(timestamp, Timestamp, php_date_get_date_ce()); - ce_Timestamp_Precision = register_class_ion_Timestamp_Precision(); - php_ion_register(catalog, Catalog); + ce_Decimal_Context_Rounding = register_class_ion_Decimal_Context_Rounding(); - ce_Reader = register_class_ion_Reader(spl_ce_RecursiveIterator); + // LOB + ce_LOB = register_class_ion_LOB(); + // Reader + ce_Reader = register_class_ion_Reader(spl_ce_RecursiveIterator); php_ion_register(reader_options, Reader_Options); php_ion_register(reader, Reader_Reader, ce_Reader); - ce_Reader_Buffer = register_class_ion_Reader_Buffer(ce_Reader); ce_Reader_Buffer_Reader = register_class_ion_Reader_Buffer_Reader(ce_Reader_Reader, ce_Reader_Buffer); ce_Reader_Stream = register_class_ion_Reader_Stream(ce_Reader); ce_Reader_Stream_Reader = register_class_ion_Reader_Stream_Reader(ce_Reader_Reader, ce_Reader_Stream); - ce_Writer = register_class_ion_Writer(); + // Serializer + ce_Serializer = register_class_ion_Serializer(); + php_ion_register(serializer_php, Serializer_PHP, ce_Serializer); + + // Symbol + php_ion_register(symbol, Symbol); + oh_Symbol.compare = php_ion_symbol_zval_compare; + php_ion_register(symbol_iloc, Symbol_ImportLocation); + php_ion_register(symbol_table, Symbol_Table); + ce_Symbol_Table->create_object = NULL; + ce_Symbol_Table_Local = register_class_ion_Symbol_Table_Local(ce_Symbol_Table); + ce_Symbol_Table_Local->create_object = create_ion_Symbol_Table; + ce_Symbol_Table_Shared = register_class_ion_Symbol_Table_Shared(ce_Symbol_Table); + ce_Symbol_Table_Shared->create_object = create_ion_Symbol_Table; + ce_Symbol_Enum = register_class_ion_Symbol_Enum(); + ce_Symbol_Table_System = register_class_ion_Symbol_Table_System(ce_Symbol_Enum); + ce_Symbol_Table_PHP = register_class_ion_Symbol_Table_PHP(ce_Symbol_Enum); + + // Timestamp + ce_Timestamp = register_class_ion_Timestamp(php_date_get_date_ce()); + ce_Timestamp_Format = register_class_ion_Timestamp_Format(); + ce_Timestamp_Precision = register_class_ion_Timestamp_Precision(); + + // Type + ce_Type = register_class_ion_Type(); + // Writer + ce_Writer = register_class_ion_Writer(); php_ion_register(writer_options, Writer_Options); php_ion_register(writer, Writer_Writer, ce_Writer); - ce_Writer_Buffer = register_class_ion_Writer_Buffer(ce_Writer); ce_Writer_Buffer_Writer = register_class_ion_Writer_Buffer_Writer(ce_Writer_Writer, ce_Writer_Buffer); ce_Writer_Stream = register_class_ion_Writer_Stream(ce_Writer); ce_Writer_Stream_Writer = register_class_ion_Writer_Stream_Writer(ce_Writer_Writer, ce_Writer_Stream); - ce_PHP_Serializer = register_class_ion_PHP_Serializer(); - ce_PHP_Unserializer = register_class_ion_PHP_Unserializer(); + // Unserializer + ce_Unserializer = register_class_ion_Unserializer(); + php_ion_register(unserializer_php, Unserializer_PHP, ce_Unserializer); return SUCCESS; } +PHP_MSHUTDOWN_FUNCTION(ion) +{ + if (g_sym_tab_php) { + ion_symbol_table_close(g_sym_tab_php); + } + zend_hash_destroy(&g_sym_hash); + return SUCCESS; +} + PHP_MINFO_FUNCTION(ion) { php_info_print_table_start(); @@ -1515,12 +1848,18 @@ PHP_MINFO_FUNCTION(ion) PHP_GINIT_FUNCTION(ion) { memset(ion_globals, 0, sizeof(*ion_globals)); + + php_ion_decimal_ctx_init_max(&ion_globals->decimal.ctx, DEC_ROUND_HALF_EVEN); + php_ion_decimal_from_zend_long(&ion_globals->decimal.zend_max, &ion_globals->decimal.ctx, ZEND_LONG_MAX); + php_ion_decimal_from_zend_long(&ion_globals->decimal.zend_min, &ion_globals->decimal.ctx, ZEND_LONG_MIN); } + PHP_GSHUTDOWN_FUNCTION(ion) { } static zend_module_dep ion_module_deps[] = { + ZEND_MOD_REQUIRED("date") ZEND_MOD_REQUIRED("spl") ZEND_MOD_END }; @@ -1532,7 +1871,7 @@ zend_module_entry ion_module_entry = { "ion", /* Extension name */ ext_functions, /* zend_function_entry */ PHP_MINIT(ion), /* PHP_MINIT - Module initialization */ - NULL, /* PHP_MSHUTDOWN - Module shutdown */ + PHP_MSHUTDOWN(ion), /* PHP_MSHUTDOWN - Module shutdown */ PHP_RINIT(ion), /* PHP_RINIT - Request initialization */ PHP_RSHUTDOWN(ion), /* PHP_RSHUTDOWN - Request shutdown */ PHP_MINFO(ion), /* PHP_MINFO - Module info */