X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=ion.c;h=d2675f02ba54d3b27ba71f307289b9356efe5d7c;hb=2a72b3a8b7af57bcbb130261b0ed54fa901920fa;hp=45f34b86d425ae776a4e47b1ef605171dd338384;hpb=e1d8621fc8f0e88bd98174e11e2384a09d838d7f;p=awesomized%2Fext-ion diff --git a/ion.c b/ion.c index 45f34b8..d2675f0 100644 --- a/ion.c +++ b/ion.c @@ -25,7 +25,6 @@ #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" @@ -41,7 +40,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 +56,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) @@ -201,8 +200,8 @@ static ZEND_METHOD(ion_Symbol_Table_Shared, __construct) break; } - ION_STRING is; - ION_CHECK(ion_symbol_table_add_symbol(obj->tab, ion_string_from_zend(&is, str), NULL), zend_string_release(str)); + 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(); @@ -410,13 +409,13 @@ static ZEND_METHOD(ion_Timestamp, __construct) 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(); @@ -424,6 +423,9 @@ static 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); } static ZEND_METHOD(ion_Timestamp, __toString) @@ -457,7 +459,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) @@ -595,12 +597,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) @@ -610,22 +611,14 @@ 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(); @@ -653,24 +646,16 @@ static ZEND_METHOD(ion_Reader_Options, __construct) } 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); + 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 = 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_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) { @@ -862,7 +847,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) { @@ -924,7 +914,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) { @@ -953,7 +943,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) { @@ -1034,9 +1026,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; } @@ -1196,7 +1186,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(); @@ -1216,9 +1206,9 @@ 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) @@ -1236,10 +1226,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, @@ -1248,8 +1234,6 @@ 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) { @@ -1275,21 +1259,15 @@ static ZEND_METHOD(ion_Writer_Options, __construct) 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) { @@ -1310,9 +1288,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) { @@ -1371,8 +1347,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)); @@ -1472,9 +1457,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) { @@ -1507,9 +1490,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) { @@ -1553,7 +1534,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; @@ -1584,9 +1565,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) @@ -1598,33 +1576,19 @@ 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) + 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); } static ZEND_METHOD(ion_Writer_Buffer_Writer, getBuffer) @@ -1634,7 +1598,16 @@ 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) { @@ -1780,15 +1753,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); @@ -1830,10 +1807,11 @@ PHP_MINIT_FUNCTION(ion) // 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();