X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=ion.c;h=e1d77b998b24ef8c9026a88a943ba01420ff156f;hb=477056db110fc9f60d1a8e185df72b8020309317;hp=3dc454a36bd1de5e6ff9755db2661ac75a13dfc5;hpb=d7bbeb1d8fb5cf9f507ac1f19de226ba226054c8;p=awesomized%2Fext-ion diff --git a/ion.c b/ion.c index 3dc454a..e1d77b9 100644 --- a/ion.c +++ b/ion.c @@ -25,11 +25,29 @@ #include "ext/date/php_date.h" #include "ext/spl/spl_exceptions.h" -#include "ext/spl/spl_iterators.h" #include "php_ion.h" #include "ion_private.h" +#define Z_PARAM_OBJ_OF_CLASS_OR_NAMED_OR_NULL(obj_dest, iface_ce, impl_ce, arr_dest) \ + Z_PARAM_PROLOGUE(0,0) \ + if (!zend_parse_arg_obj(_arg, &(obj_dest), iface_ce, true) && !zend_parse_arg_obj(_arg, &(obj_dest), impl_ce, true)) { \ + if (UNEXPECTED(!zend_parse_arg_array(_arg, &(arr_dest), true, 0))) { \ + _error = (iface_ce)->name->val; \ + _error_code = ZPP_ERROR_WRONG_CLASS_OR_NULL; \ + break; \ + } \ + zval tmp; \ + object_init_ex(&tmp, impl_ce); \ + *(&(obj_dest)) = Z_OBJ(tmp); \ + zend_call_known_function((impl_ce)->constructor, obj_dest, impl_ce, \ + NULL, 0, NULL, Z_ARRVAL_P(arr_dest)); \ + if (EG(exception)) { \ + OBJ_RELEASE(obj_dest); \ + return; \ + } \ + } + static ZEND_METHOD(ion_Symbol_ImportLocation, __construct) { php_ion_symbol_iloc *obj = php_ion_obj(symbol_iloc, Z_OBJ_P(ZEND_THIS)); @@ -41,7 +59,7 @@ static ZEND_METHOD(ion_Symbol_ImportLocation, __construct) Z_PARAM_LONG(location) ZEND_PARSE_PARAMETERS_END(); - obj->loc.location = location; + obj->loc.location = (SID) location; php_ion_symbol_iloc_ctor(obj); } static ZEND_METHOD(ion_Symbol, __construct) @@ -57,7 +75,7 @@ static ZEND_METHOD(ion_Symbol, __construct) 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); } static ZEND_METHOD(ion_Symbol, equals) @@ -126,7 +144,7 @@ static ZEND_METHOD(ion_Symbol_Enum, toString) 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) +static ZEND_METHOD(ion_Symbol_System, asTable) { ZEND_PARSE_PARAMETERS_NONE(); @@ -136,7 +154,7 @@ static ZEND_FUNCTION(ion_Symbol_Table_System) php_ion_symbol_table_ctor(obj); ion_symbol_table_lock(obj->tab); } -static ZEND_FUNCTION(ion_Symbol_Table_PHP) +static ZEND_METHOD(ion_Symbol_PHP, asTable) { ZEND_PARSE_PARAMETERS_NONE(); @@ -176,10 +194,12 @@ static ZEND_METHOD(ion_Symbol_Table_Shared, __construct) zend_string *zname; zend_long version = 1; - ZEND_PARSE_PARAMETERS_START(1, 2) + 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)); @@ -190,6 +210,20 @@ static ZEND_METHOD(ion_Symbol_Table_Shared, __construct) 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) { @@ -309,26 +343,7 @@ static ZEND_METHOD(ion_Catalog, add) php_ion_symbol_table *o_symtab = php_ion_obj(symbol_table, zo_symtab); php_ion_catalog_add_symbol_table(obj, o_symtab); } -struct remove_symtab_ctx { - const char *name; - zend_bool deleted; -}; -static int remove_symtab(zval *ztab, void *ctx) -{ - struct remove_symtab_ctx *rsc = ctx; - php_ion_symbol_table *tab = php_ion_obj(symbol_table, Z_OBJ_P(ztab)); - if (tab && tab->tab) { - ION_STRING is; - if (IERR_OK == ion_symbol_table_get_name(tab->tab, &is)) { - if (strcmp((const char *) is.value, rsc->name)) { - return ZEND_HASH_APPLY_KEEP; - } - } - } - rsc->deleted = true; - return ZEND_HASH_APPLY_REMOVE; -} static ZEND_METHOD(ion_Catalog, remove) { php_ion_catalog *obj = php_ion_obj(catalog, Z_OBJ_P(ZEND_THIS)); @@ -348,12 +363,22 @@ static ZEND_METHOD(ion_Catalog, remove) if (zo_symtab) { // fast path zend_ulong idx = (uintptr_t) &zo_symtab->gc; - RETURN_BOOL(SUCCESS == zend_hash_index_del(Z_ARRVAL_P(ztabs), idx)); + 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 { - // iterate over all symbol tables and delete any with matching name - struct remove_symtab_ctx ctx = {zs_symtab->val, false}; - zend_hash_apply_with_argument(Z_ARRVAL_P(ztabs), remove_symtab, &ctx); - RETURN_BOOL(ctx.deleted); + 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); } } } @@ -403,21 +428,39 @@ static ZEND_METHOD(ion_Timestamp, __construct) PTR_CHECK(obj); zend_long precision; - zend_object *precision_obj; - zend_string *fmt = NULL, *dt = NULL; - zval *tz = NULL; + zend_object *precision_obj = NULL, *format_obj = NULL; + zend_string *fmt = NULL, *dt = NULL, *tz = NULL; + zend_object *tz_obj = NULL; + zval z_tz_tmp, *z_tz_ptr = 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) + Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(tz_obj, php_date_get_timezone_ce(), tz) ZEND_PARSE_PARAMETERS_END(); if (precision_obj) { precision = Z_LVAL_P(zend_enum_fetch_case_value(precision_obj)); } - php_ion_timestamp_ctor(obj, precision, fmt, dt, tz); + if (format_obj) { + fmt = Z_STR_P(zend_enum_fetch_case_value(format_obj)); + } + if (tz_obj) { + ZVAL_OBJ(z_tz_ptr = &z_tz_tmp, tz_obj); + } else if (tz) { + // there's no public API, so call timezone_open + zend_function *tz_open = zend_fetch_function_str(ZEND_STRL("timezone_open")); + if (tz_open) { + zval z_arg; + ZVAL_STR(&z_arg, tz); + zend_call_known_function(tz_open, NULL, NULL, z_tz_ptr = &z_tz_tmp, 1, &z_arg, NULL); + } + } + php_ion_timestamp_ctor(obj, precision, fmt, dt, z_tz_ptr); + if (tz && z_tz_ptr) { + zval_ptr_dtor(z_tz_ptr); + } } static ZEND_METHOD(ion_Timestamp, __toString) { @@ -450,7 +493,7 @@ static ZEND_METHOD(ion_Decimal_Context, __construct) 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); + 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) @@ -588,12 +631,11 @@ 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 = 10, max_ann_buf = 512, - sym_thr = 0x4000, uval_thr = 0x4000, chunk_thr = 0x4000, alloc_pgsz = 0x10000; + 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) @@ -603,58 +645,51 @@ static 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 = 0x4000, - Z_PARAM_LONG(sym_thr) - // public readonly int $userValueThreshold = 0x4000, - Z_PARAM_LONG(uval_thr) - // public readonly int $chunkThreshold = 0x4000, - Z_PARAM_LONG(chunk_thr) - // public readonly int $allocationPageSize = 0x10000, - 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); } static ZEND_METHOD(ion_Reader_Reader, hasChildren) { @@ -846,7 +881,12 @@ static ZEND_METHOD(ion_Reader_Reader, getAnnotationSymbols) 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) { @@ -908,7 +948,7 @@ static 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); } static ZEND_METHOD(ion_Reader_Reader, readBool) { @@ -937,7 +977,9 @@ static ZEND_METHOD(ion_Reader_Reader, readFloat) 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); } static ZEND_METHOD(ion_Reader_Reader, readDecimal) { @@ -1018,9 +1060,7 @@ 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; } @@ -1106,16 +1146,20 @@ static ZEND_METHOD(ion_Reader_Buffer_Reader, __construct) PTR_CHECK(obj); zend_string *zstr; + zval *za_opt = NULL; ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_STR(zstr) Z_PARAM_OPTIONAL - Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Reader_Options) + Z_PARAM_OBJ_OF_CLASS_OR_NAMED_OR_NULL(obj->opt, ce_Reader_Options, ce_Reader_Options, za_opt) ZEND_PARSE_PARAMETERS_END(); obj->type = BUFFER_READER; obj->buffer = zend_string_copy(zstr); php_ion_reader_ctor(obj); + if (za_opt) { + OBJ_RELEASE(obj->opt); + } } static ZEND_METHOD(ion_Reader_Buffer_Reader, getBuffer) { @@ -1131,17 +1175,20 @@ static ZEND_METHOD(ion_Reader_Stream_Reader, __construct) php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); PTR_CHECK(obj); - zval *zstream; + zval *zstream, *za_opt = NULL; ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_RESOURCE(zstream) Z_PARAM_OPTIONAL - Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Reader_Options) + Z_PARAM_OBJ_OF_CLASS_OR_NAMED_OR_NULL(obj->opt, ce_Reader_Options, ce_Reader_Options, za_opt) ZEND_PARSE_PARAMETERS_END(); obj->type = STREAM_READER; php_stream_from_zval_no_verify(obj->stream.ptr, zstream); php_ion_reader_ctor(obj); + if (za_opt) { + OBJ_RELEASE(obj->opt); + } } static ZEND_METHOD(ion_Reader_Stream_Reader, getStream) { @@ -1180,7 +1227,7 @@ static ZEND_METHOD(ion_Reader_Stream_Reader, resetStreamWithLength) 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(); @@ -1200,14 +1247,12 @@ static ZEND_METHOD(ion_Writer_Options, __construct) 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 = 0x4000, alloc = 0x10000; - 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, @@ -1222,10 +1267,6 @@ static 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, @@ -1234,47 +1275,40 @@ static ZEND_METHOD(ion_Writer_Options, __construct) Z_PARAM_LONG(max_ann) //public readonly int $tempBufferSize = 0x4000, Z_PARAM_LONG(temp) - //public readonly int $allocationPageSize = 0x10000, - 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); } static ZEND_METHOD(ion_Writer_Writer, writeNull) { @@ -1295,9 +1329,7 @@ static 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))); } static ZEND_METHOD(ion_Writer_Writer, writeBool) { @@ -1356,8 +1388,17 @@ static 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)); @@ -1457,9 +1498,7 @@ static 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))); } static ZEND_METHOD(ion_Writer_Writer, appendLob) { @@ -1492,9 +1531,7 @@ static 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))); } static ZEND_METHOD(ion_Writer_Writer, finishContainer) { @@ -1538,7 +1575,7 @@ static 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; @@ -1569,9 +1606,6 @@ static 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); } static ZEND_METHOD(ion_Writer_Writer, finish) @@ -1583,34 +1617,24 @@ static 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); } -static ZEND_METHOD(ion_Writer_Writer, writeOne) -{ -} -static ZEND_METHOD(ion_Writer_Writer, writeAll) -{ -} 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) + zval *za_opt = NULL; + ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL - Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Writer_Options) + Z_PARAM_OBJ_OF_CLASS_OR_NAMED_OR_NULL(obj->opt, ce_Writer_Options, ce_Writer_Options, za_opt) 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); + if (za_opt) { + OBJ_RELEASE(obj->opt); + } } static ZEND_METHOD(ion_Writer_Buffer_Writer, getBuffer) { @@ -1619,24 +1643,36 @@ static ZEND_METHOD(ion_Writer_Buffer_Writer, getBuffer) ZEND_PARSE_PARAMETERS_NONE(); - RETVAL_STR(zend_string_dup(obj->buffer.str.s, 0)); + RETVAL_STR(php_ion_writer_buffer_copy(obj)); +} +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); - zval *zstream; + zval *zstream, *za_opt = NULL; ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_RESOURCE(zstream) Z_PARAM_OPTIONAL - Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Writer_Options) + Z_PARAM_OBJ_OF_CLASS_OR_NAMED_OR_NULL(obj->opt, ce_Writer_Options, ce_Writer_Options, za_opt) ZEND_PARSE_PARAMETERS_END(); obj->type = STREAM_WRITER; php_stream_from_zval_no_verify(obj->stream.ptr, zstream); php_ion_writer_ctor(obj); + if (za_opt) { + OBJ_RELEASE(obj->opt); + } } static ZEND_METHOD(ion_Writer_Stream_Writer, getStream) { @@ -1650,131 +1686,217 @@ static ZEND_METHOD(ion_Writer_Stream_Writer, getStream) RETURN_RES(obj->stream.ptr->res); } -static ZEND_METHOD(ion_Serializer_PHP, __construct) +static ZEND_METHOD(ion_Serializer_Serializer, serializeNull) +{ + php_ion_serializer *obj = php_ion_obj(serializer, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + ZEND_PARSE_PARAMETERS_NONE(); + + ION_CHECK(ion_writer_write_null(php_ion_obj(writer, obj->wri)->writer)); +} + +static ZEND_METHOD(ion_Serializer_Serializer, serializeBool) +{ + php_ion_serializer *obj = php_ion_obj(serializer, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + zend_bool b; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_BOOL(b) + ZEND_PARSE_PARAMETERS_END(); + + ION_CHECK(ion_writer_write_bool(php_ion_obj(writer, obj->wri)->writer, b)); +} + +static ZEND_METHOD(ion_Serializer_Serializer, serializeInt) +{ + php_ion_serializer *obj = php_ion_obj(serializer, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + zend_long l; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_LONG(l); + ZEND_PARSE_PARAMETERS_END(); + + ION_CHECK(ion_writer_write_int64(php_ion_obj(writer, obj->wri)->writer, l)); +} + +static ZEND_METHOD(ion_Serializer_Serializer, serializeFloat) +{ + php_ion_serializer *obj = php_ion_obj(serializer, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + double d; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(d); + ZEND_PARSE_PARAMETERS_END(); + + ION_CHECK(ion_writer_write_double(php_ion_obj(writer, obj->wri)->writer, d)); +} + +static ZEND_METHOD(ion_Serializer_Serializer, serializeString) { - php_ion_serializer_php *obj = php_ion_obj(serializer_php, Z_OBJ_P(ZEND_THIS)); + php_ion_serializer *obj = php_ion_obj(serializer, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + zend_string *zs; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_STR(zs); + ZEND_PARSE_PARAMETERS_END(); + + ION_STRING is; + ION_CHECK(ion_writer_write_string(php_ion_obj(writer, obj->wri)->writer, ion_string_from_zend(&is, zs))); +} + +static ZEND_METHOD(ion_Serializer_Serializer, serializeArray) +{ + php_ion_serializer *obj = php_ion_obj(serializer, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + zend_array *arr; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_ARRAY_HT(arr) + ZEND_PARSE_PARAMETERS_END(); + + if (zend_array_is_list(arr)) { + php_ion_serialize_list(obj, arr); + } else { + php_ion_serialize_struct(obj, arr, false, false); + } +} + +static ZEND_METHOD(ion_Serializer_Serializer, serializeObject) +{ + php_ion_serializer *obj = php_ion_obj(serializer, Z_OBJ_P(ZEND_THIS)); + OBJ_CHECK(obj); + + zend_object *o; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_OBJ(o) + ZEND_PARSE_PARAMETERS_END(); + + php_ion_serialize_object(obj, o); +} + +static ZEND_METHOD(ion_Serializer_Serializer, __construct) +{ + php_ion_serializer *obj = php_ion_obj(serializer, Z_OBJ_P(ZEND_THIS)); PTR_CHECK(obj); - obj->serializer.call_magic = true; + obj->call_magic = true; ZEND_PARSE_PARAMETERS_START(0, 3) Z_PARAM_OPTIONAL - 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) + Z_PARAM_BOOL(obj->multi_seq) + Z_PARAM_BOOL(obj->call_magic) + Z_PARAM_STR_OR_NULL(obj->call_custom) ZEND_PARSE_PARAMETERS_END(); - php_ion_serializer_php_ctor(obj); + php_ion_serializer_ctor(obj); } -static ZEND_METHOD(ion_Serializer_PHP, __invoke) +static ZEND_METHOD(ion_Serializer_Serializer, serialize) { zend_object *obj = Z_OBJ_P(ZEND_THIS); - zval *data; - ZEND_PARSE_PARAMETERS_START(1, 1) + zval *data, *za_opt = NULL; + zend_object *zo_wri = NULL; + ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_ZVAL(data) + Z_PARAM_OPTIONAL + Z_PARAM_OBJ_OF_CLASS_OR_NAMED_OR_NULL(zo_wri, ce_Writer, ce_Writer_Options, za_opt) ZEND_PARSE_PARAMETERS_END(); - if (obj->ce == ce_Serializer_PHP) { - // default, fast path - php_ion_serialize(&php_ion_obj(serializer_php, obj)->serializer, data, return_value); - } else { - zend_call_method_with_1_params(obj, obj->ce, NULL /* TODO */, "serialize", return_value, data); + php_ion_serializer *ser = php_ion_obj(serializer, obj); + ser->wri = zo_wri; + php_ion_serialize(ser, data, return_value); + ser->wri = NULL; + + if (za_opt) { + OBJ_RELEASE(zo_wri); } } static ZEND_FUNCTION(ion_serialize) { - zval *data; + zval *data, *za_ser = NULL; zend_object *zo_ser = NULL; ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_ZVAL(data) Z_PARAM_OPTIONAL - Z_PARAM_OBJ_OF_CLASS_OR_NULL(zo_ser, ce_Serializer) + Z_PARAM_OBJ_OF_CLASS_OR_NAMED_OR_NULL(zo_ser, ce_Serializer, ce_Serializer_Serializer, za_ser) ZEND_PARSE_PARAMETERS_END(); - if (!zo_ser || zo_ser->ce == ce_Serializer_PHP) { + if (!zo_ser || zo_ser->ce == ce_Serializer_Serializer) { // default, fast path - php_ion_serializer *ser = zo_ser ? &php_ion_obj(serializer_php, zo_ser)->serializer : NULL; + php_ion_serializer *ser = zo_ser ? php_ion_obj(serializer, zo_ser) : NULL; php_ion_serialize(ser, data, return_value); } else { - zend_call_method_with_1_params(zo_ser, NULL, NULL, "__invoke", return_value, data); + zend_call_method_with_1_params(zo_ser, NULL, NULL, "serialize", return_value, data); } -} -static ZEND_METHOD(ion_Serializer_PHP, serialize) -{ - //zend_object *obj = Z_OBJ_P(ZEND_THIS); - zval *data; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_ZVAL(data) - ZEND_PARSE_PARAMETERS_END(); - - // TODO - zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Not implemented"); + if (za_ser) { + OBJ_RELEASE(zo_ser); + } } -static ZEND_METHOD(ion_Unserializer_PHP, __construct) +static ZEND_METHOD(ion_Unserializer_Unserializer, __construct) { - php_ion_unserializer_php *obj = php_ion_obj(unserializer_php, Z_OBJ_P(ZEND_THIS)); + php_ion_unserializer *obj = php_ion_obj(unserializer, Z_OBJ_P(ZEND_THIS)); PTR_CHECK(obj); - obj->unserializer.call_magic = true; + obj->call_magic = true; ZEND_PARSE_PARAMETERS_START(0, 3) Z_PARAM_OPTIONAL - 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) + Z_PARAM_BOOL(obj->multi_seq) + Z_PARAM_BOOL(obj->call_magic) + Z_PARAM_STR_OR_NULL(obj->call_custom) ZEND_PARSE_PARAMETERS_END(); - php_ion_unserializer_php_ctor(obj); + php_ion_unserializer_ctor(obj); } -static ZEND_METHOD(ion_Unserializer_PHP, __invoke) +static ZEND_METHOD(ion_Unserializer_Unserializer, unserialize) { zend_object *obj = Z_OBJ_P(ZEND_THIS); - zval *data; - ZEND_PARSE_PARAMETERS_START(1, 1) + zval *data, *za_opt = NULL; + zend_object *zo_rdr = NULL; + ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_ZVAL(data) + Z_PARAM_OPTIONAL + Z_PARAM_OBJ_OF_CLASS_OR_NAMED_OR_NULL(zo_rdr, ce_Reader, ce_Reader_Options, za_opt) ZEND_PARSE_PARAMETERS_END(); - if (obj->ce == ce_Unserializer_PHP) { - php_ion_unserialize(&php_ion_obj(unserializer_php, obj)->unserializer, data, return_value); - } else { - zend_call_method_with_1_params(obj, obj->ce, NULL /* TODO */, "unserialize", return_value, data); + php_ion_unserializer *ser = php_ion_obj(unserializer, obj); + ser->rdr = zo_rdr; + php_ion_unserialize(ser, data, return_value); + ser->rdr = NULL; + + if (za_opt) { + OBJ_RELEASE(zo_rdr); } } static ZEND_FUNCTION(ion_unserialize) { - zval *data; + zval *data, *za_ser = NULL; zend_object *zo_ser = NULL; ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_ZVAL(data) Z_PARAM_OPTIONAL - Z_PARAM_OBJ_OF_CLASS_OR_NULL(zo_ser, ce_Unserializer) + Z_PARAM_OBJ_OF_CLASS_OR_NAMED_OR_NULL(zo_ser, ce_Unserializer, ce_Unserializer_Unserializer, za_ser) ZEND_PARSE_PARAMETERS_END(); - if (!zo_ser || zo_ser->ce == ce_Unserializer_PHP) { + if (!zo_ser || zo_ser->ce == ce_Unserializer_Unserializer) { // default, fast path - php_ion_unserializer *ser = zo_ser ? &php_ion_obj(unserializer_php, zo_ser)->unserializer : NULL; + php_ion_unserializer *ser = zo_ser ? php_ion_obj(unserializer, zo_ser) : NULL; php_ion_unserialize(ser, data, return_value); } else { - zend_call_method_with_1_params(zo_ser, NULL, NULL, "__invoke", return_value, data); + zend_call_method_with_1_params(zo_ser, NULL, NULL, "unserialize", return_value, data); } -} -static ZEND_METHOD(ion_Unserializer_PHP, unserialize) -{ - //zend_object *obj = Z_OBJ_P(ZEND_THIS); - zval *data; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_ZVAL(data) - ZEND_PARSE_PARAMETERS_END(); - - // TODO - zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Not implemented"); + if (za_ser) { + OBJ_RELEASE(zo_ser); + } } PHP_RINIT_FUNCTION(ion) @@ -1798,15 +1920,19 @@ PHP_RSHUTDOWN_FUNCTION(ion) } #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 }, + { #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) { // true globals if (SUCCESS != g_sym_init()) { return FAILURE; } + g_intern_str_init(); // Catalog php_ion_register(catalog, Catalog, zend_ce_countable); @@ -1816,6 +1942,9 @@ PHP_MINIT_FUNCTION(ion) php_ion_register(decimal_ctx, Decimal_Context); ce_Decimal_Context_Rounding = register_class_ion_Decimal_Context_Rounding(); + // Exception + ce_Exception = register_class_ion_Exception(zend_ce_exception); + // LOB ce_LOB = register_class_ion_LOB(); @@ -1830,7 +1959,7 @@ PHP_MINIT_FUNCTION(ion) // Serializer ce_Serializer = register_class_ion_Serializer(); - php_ion_register(serializer_php, Serializer_PHP, ce_Serializer); + php_ion_register(serializer, Serializer_Serializer, ce_Serializer); // Symbol php_ion_register(symbol, Symbol); @@ -1843,15 +1972,16 @@ PHP_MINIT_FUNCTION(ion) 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); + ce_Symbol_Table_System = register_class_ion_Symbol_System(ce_Symbol_Enum); + ce_Symbol_Table_PHP = register_class_ion_Symbol_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 - php_ion_register(type, Type); + ce_Type = register_class_ion_Type(); // Writer ce_Writer = register_class_ion_Writer(); @@ -1864,7 +1994,7 @@ PHP_MINIT_FUNCTION(ion) // Unserializer ce_Unserializer = register_class_ion_Unserializer(); - php_ion_register(unserializer_php, Unserializer_PHP, ce_Unserializer); + php_ion_register(unserializer, Unserializer_Unserializer, ce_Unserializer); return SUCCESS; } @@ -1882,6 +2012,8 @@ PHP_MINFO_FUNCTION(ion) { php_info_print_table_start(); php_info_print_table_header(2, "ion support", "enabled"); + php_info_print_table_row(2, "ion version", PHP_ION_VERSION); + php_info_print_table_row(2, "ion-c version", "unknown"); php_info_print_table_end(); }