X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;ds=sidebyside;f=ion.c;h=e84d62518e153b35a3c16add6b7a4d1e183a3a61;hb=d901f111e3cef4889b8f45968b8e053b9d79dd8f;hp=8bf7e0a3e11b63a5f2ab0361fb3ee629ee1f2726;hpb=ac7822dd9dd0d5cf0855df8a750468d453bc3053;p=awesomized%2Fext-ion diff --git a/ion.c b/ion.c index 8bf7e0a..e84d625 100644 --- a/ion.c +++ b/ion.c @@ -29,6 +29,43 @@ #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; \ + } \ + } + +#define Z_PARAM_HASH_OR_INSTANCE_OF_NULL(za_dest, zo_inst, ce_inst) \ + Z_PARAM_PROLOGUE(0, 0) { \ + zval *_arr; \ + if (zend_parse_arg_array(_arg, &(_arr), true, 0)) { \ + za_dest = Z_ARRVAL_P(_arr); \ + } else { \ + zend_object *_obj = NULL; \ + if (zend_parse_arg_obj(_arg, &_obj, ce, true)) { \ + za_dest = _obj->handlers->get_properties(_obj); \ + } else if (zend_parse_arg_obj(_arg, &(zo_inst), ce_inst, true)) { \ + } else { \ + _error = ce->name->val; \ + _error_code = ZPP_ERROR_WRONG_CLASS_OR_NULL; \ + break; \ + } \ + } \ + } + static ZEND_METHOD(ion_Symbol_ImportLocation, __construct) { php_ion_symbol_iloc *obj = php_ion_obj(symbol_iloc, Z_OBJ_P(ZEND_THIS)); @@ -125,7 +162,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(); @@ -135,7 +172,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(); @@ -410,14 +447,15 @@ static ZEND_METHOD(ion_Timestamp, __construct) zend_long precision; zend_object *precision_obj = NULL, *format_obj = NULL; - zend_string *fmt = NULL, *dt = NULL; - zval *tz = 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_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) { @@ -426,7 +464,21 @@ static ZEND_METHOD(ion_Timestamp, __construct) if (format_obj) { fmt = Z_STR_P(zend_enum_fetch_case_value(format_obj)); } - php_ion_timestamp_ctor(obj, precision, fmt, dt, tz); + 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) { @@ -593,86 +645,63 @@ static ZEND_METHOD(ion_LOB, __construct) 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) +static ZEND_METHOD(ion_Reader_Reader, __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; + php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); - PTR_CHECK(opt); + PTR_CHECK(obj); - ZEND_PARSE_PARAMETERS_START(0, 13) + zval *zstream = NULL; + zend_bool ret_sys_val = false, skip_validation = false; + zend_long max_depth = 10, max_ann = 10, ann_buf_siz = 0x4000, tmp_buf_siz = 0x4000; + ZEND_PARSE_PARAMETERS_START(1, 10) + Z_PARAM_PROLOGUE(0, 0) + if (!instanceof_function(obj->std.ce, ce_Reader_Buffer) && + zend_parse_arg_resource(_arg, &zstream, 0)) { + obj->type = STREAM_READER; + php_stream_from_zval_no_verify(obj->stream.ptr, zstream); + PTR_CHECK(obj->stream.ptr); + GC_ADDREF(obj->stream.ptr->res); + } else if (!instanceof_function(obj->std.ce, ce_Reader_Stream) && + zend_parse_arg_str(_arg, &obj->buffer, 0, _i)) { + obj->type = BUFFER_READER; + zend_string_addref(obj->buffer); + } else { + _error_code = ZPP_ERROR_WRONG_ARG; + _error = "of type string or resource"; + break; + } Z_PARAM_OPTIONAL // public readonly ?\ion\Catalog $catalog = null, - Z_PARAM_OBJ_OF_CLASS_OR_NULL(opt->cat, ce_Catalog) + Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->cat, ce_Catalog) // public readonly ?\ion\Decimal\Context $decimalContext = null, - Z_PARAM_OBJ_OF_CLASS_OR_NULL(opt->dec_ctx, ce_Decimal_Context) + Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->dec_ctx, ce_Decimal_Context) // public readonly ?\Closure $onContextChange = null, - Z_PARAM_OBJ_OF_CLASS_OR_NULL(opt->cb, zend_ce_closure); + Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->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(); - 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 = (int) ch_nl); - zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("maxContainerDepth"), - 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("maxAnnotationBuffered"), - opt->opt.max_annotation_buffered = (SIZE) max_ann_buf); - zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("symbolThreshold"), - opt->opt.symbol_threshold = (SIZE) sym_thr); - zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("userValueThreshold"), - opt->opt.user_value_threshold = (SIZE) uval_thr); - zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("chunkThreshold"), - opt->opt.chunk_threshold = (SIZE) chunk_thr); - zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("allocationPageSize"), - opt->opt.allocation_page_size = (SIZE) alloc_pgsz); - zend_update_property_long(opt->std.ce, &opt->std, ZEND_STRL("skipCharacterValidation"), - opt->opt.skip_character_validation = (SIZE) skip_validation); + obj->options.return_system_values = ret_sys_val; + obj->options.max_container_depth = (SIZE) max_depth; + obj->options.max_annotation_count = (SIZE) max_ann; + obj->options.max_annotation_buffered = (SIZE) ann_buf_siz; + obj->options.chunk_threshold = (SIZE) tmp_buf_siz; + obj->options.user_value_threshold = (SIZE) tmp_buf_siz; + obj->options.symbol_threshold = (SIZE) tmp_buf_siz; + obj->options.skip_character_validation = skip_validation; + + php_ion_reader_ctor(obj); } static ZEND_METHOD(ion_Reader_Reader, hasChildren) { @@ -841,8 +870,8 @@ static ZEND_METHOD(ion_Reader_Reader, getAnnotations) ZEND_PARSE_PARAMETERS_NONE(); int32_t count, max; - if (obj->opt) { - max = php_ion_obj(reader_options, obj->opt)->opt.max_annotation_count; + if (obj->options.max_annotation_count) { + max = obj->options.max_annotation_count; } else { max = 10; } @@ -864,7 +893,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->options.max_annotation_count) { + max = obj->options.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) { @@ -955,7 +989,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) { @@ -976,7 +1012,7 @@ static ZEND_METHOD(ion_Reader_Reader, readTimestamp) ZEND_PARSE_PARAMETERS_NONE(); - php_ion_reader_read_timestamp(obj->reader, obj->opt ? &php_ion_obj(reader_options, obj->opt)->opt : NULL, return_value); + php_ion_reader_read_timestamp(obj->reader, &obj->options, return_value); } static ZEND_METHOD(ion_Reader_Reader, readSymbol) { @@ -1036,9 +1072,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; } @@ -1118,23 +1152,7 @@ static ZEND_METHOD(ion_Reader_Reader, getValueLength) ION_CHECK(ion_reader_get_value_length(obj->reader, &len)); RETURN_LONG(len); } -static ZEND_METHOD(ion_Reader_Buffer_Reader, __construct) -{ - php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); - PTR_CHECK(obj); - - zend_string *zstr; - 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) - ZEND_PARSE_PARAMETERS_END(); - obj->type = BUFFER_READER; - obj->buffer = zend_string_copy(zstr); - - php_ion_reader_ctor(obj); -} static ZEND_METHOD(ion_Reader_Buffer_Reader, getBuffer) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); @@ -1144,23 +1162,6 @@ static ZEND_METHOD(ion_Reader_Buffer_Reader, getBuffer) RETURN_STR_COPY(obj->buffer); } -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; - 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) - ZEND_PARSE_PARAMETERS_END(); - - obj->type = STREAM_READER; - php_stream_from_zval_no_verify(obj->stream.ptr, zstream); - - php_ion_reader_ctor(obj); -} static ZEND_METHOD(ion_Reader_Stream_Reader, getStream) { php_ion_reader *obj = php_ion_obj(reader, Z_OBJ_P(ZEND_THIS)); @@ -1198,7 +1199,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(); @@ -1212,15 +1213,32 @@ static ZEND_METHOD(ion_Reader_Stream_Reader, resetStreamWithLength) PTR_CHECK(obj->stream.ptr); Z_ADDREF_P(zstream); } -static ZEND_METHOD(ion_Writer_Options, __construct) + +static void _ion_Writer_Writer___construct(bool stream_writer, INTERNAL_FUNCTION_PARAMETERS) { - php_ion_writer_options *obj = php_ion_obj(writer_options, Z_OBJ_P(ZEND_THIS)); + php_ion_writer *obj = php_ion_obj(writer, 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 = 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 + stream_writer, 12 + stream_writer) + if (!stream_writer) { + obj->type = BUFFER_WRITER; + } else { + Z_PARAM_PROLOGUE(0, 0) + obj->type = STREAM_WRITER; + zval *zstream = NULL; + if (zend_parse_arg_resource(_arg, &zstream, 0)) { + php_stream_from_zval_no_verify(obj->stream.ptr, zstream); + PTR_CHECK(obj->stream.ptr); + GC_ADDREF(obj->stream.ptr->res); + } else { + _error_code = Z_EXPECTED_RESOURCE; + _error = "of type resource"; + break; + } + } Z_PARAM_OPTIONAL //public readonly ?\ion\Catalog $catalog = null, Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->cat, ce_Catalog) @@ -1238,10 +1256,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, @@ -1250,48 +1264,25 @@ 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; - } 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("compactFloats"), - obj->opt.compact_floats = compact_floats); - 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 = (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); - 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 = (SIZE) max_depth); - zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("maxAnnotations"), - 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 = (SIZE) temp); - zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("allocationPageSize"), - obj->opt.allocation_page_size = (SIZE) alloc); + obj->options.output_as_binary = binary; + obj->options.compact_floats = compact_floats; + obj->options.escape_all_non_ascii = escape; + obj->options.pretty_print = pretty; + obj->options.indent_with_tabs = tabs; + obj->options.indent_size = (SIZE) indent; + obj->options.flush_every_value = flush; + obj->options.max_container_depth = (SIZE) max_depth; + obj->options.max_annotation_count = (SIZE) max_ann; + obj->options.temp_buffer_size = (SIZE) temp; + + php_ion_writer_ctor(obj); +} + +static ZEND_METHOD(ion_Writer_Writer, __construct) +{ + _ion_Writer_Writer___construct(false, INTERNAL_FUNCTION_PARAM_PASSTHRU); } static ZEND_METHOD(ion_Writer_Writer, writeNull) { @@ -1371,8 +1362,14 @@ 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->options.decimal_context) { + ctx = obj->options.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)); @@ -1389,11 +1386,7 @@ static ZEND_METHOD(ion_Writer_Writer, writeTimestamp) Z_PARAM_OBJ_OF_CLASS_OR_STR(ts_obj, ce_Timestamp, ts_str) ZEND_PARSE_PARAMETERS_END(); - decContext *ctx = NULL; - if (obj->opt) { - ctx = php_ion_obj(reader_options, obj->opt)->opt.decimal_context; - } - + decContext *ctx = obj->options.decimal_context; ION_TIMESTAMP tmp = {0}; if (ts_str) { SIZE used; @@ -1580,9 +1573,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) @@ -1594,60 +1584,33 @@ 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) { + _ion_Writer_Writer___construct(false, INTERNAL_FUNCTION_PARAM_PASSTHRU); } -static ZEND_METHOD(ion_Writer_Buffer_Writer, __construct) +static ZEND_METHOD(ion_Writer_Buffer_Writer, getBuffer) { 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) - Z_PARAM_OPTIONAL - Z_PARAM_OBJ_OF_CLASS_OR_NULL(obj->opt, ce_Writer_Options) - ZEND_PARSE_PARAMETERS_END(); + OBJ_CHECK(obj); - obj->type = BUFFER_WRITER; - ZVAL_COPY(&obj->buffer.val, ref); - zval_dtor(Z_REFVAL_P(ref)); + ZEND_PARSE_PARAMETERS_NONE(); - php_ion_writer_ctor(obj); + RETVAL_STR(php_ion_writer_buffer_copy(obj)); } -static ZEND_METHOD(ion_Writer_Buffer_Writer, getBuffer) +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(); - RETVAL_STR(zend_string_dup(obj->buffer.str.s, 0)); + 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; - 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) - ZEND_PARSE_PARAMETERS_END(); - - obj->type = STREAM_WRITER; - php_stream_from_zval_no_verify(obj->stream.ptr, zstream); - - php_ion_writer_ctor(obj); + _ion_Writer_Writer___construct(true, INTERNAL_FUNCTION_PARAM_PASSTHRU); } static ZEND_METHOD(ion_Writer_Stream_Writer, getStream) { @@ -1661,97 +1624,142 @@ 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, __construct) { - 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)); PTR_CHECK(obj); - obj->serializer.call_magic = true; + obj->call_magic = true; - ZEND_PARSE_PARAMETERS_START(0, 4) + 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, serialize) +static ZEND_METHOD(ion_Serializer_Serializer, serialize) { zend_object *obj = Z_OBJ_P(ZEND_THIS); zval *data; - ZEND_PARSE_PARAMETERS_START(1, 1) + zend_object *zo_wri = NULL; + zend_array *za_named = NULL; + ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_ZVAL(data) + Z_PARAM_OPTIONAL + Z_PARAM_PROLOGUE(0, 0) + if (!zend_parse_arg_obj(_arg, &zo_wri, ce_Writer, true)) { + if (!zend_parse_arg_array_ht(_arg, &za_named, false, false, false)) { + _error_code = ZPP_ERROR_WRONG_ARG; + _error = "of type array or \\ion\\Writer"; + break; + } + zo_wri = create_ion_Writer_Writer(ce_Writer_Buffer_Writer); + call_constructor(zo_wri, 0, NULL, za_named); + ION_CATCH(OBJ_RELEASE(zo_wri)); + } ZEND_PARSE_PARAMETERS_END(); - php_ion_serialize(&php_ion_obj(serializer_php, obj)->serializer, data, return_value); + 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_named) { + OBJ_RELEASE(zo_wri); + } } static ZEND_FUNCTION(ion_serialize) { zval *data; zend_object *zo_ser = NULL; + zend_array *za_named = 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_PROLOGUE(0, 0) + if (!zend_parse_arg_obj(_arg, &zo_ser, ce_Serializer, true)) { + if (!zend_parse_arg_array_ht(_arg, &za_named, false, false, false)) { + _error_code = ZPP_ERROR_WRONG_ARG; + _error = "of type array or \\ion\\Writer"; + break; + } + zo_ser = create_ion_Serializer_Serializer(NULL); + call_constructor(zo_ser, 0, NULL, za_named); + ION_CATCH(OBJ_RELEASE(zo_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_serialize(ser, data, return_value); + php_ion_serialize(php_ion_obj(serializer, zo_ser), data, return_value); } else { zend_call_method_with_1_params(zo_ser, NULL, NULL, "serialize", return_value, data); } + + if (za_named) { + 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, 4) + 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, unserialize) +static ZEND_METHOD(ion_Unserializer_Unserializer, 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(); - php_ion_unserialize(&php_ion_obj(unserializer_php, obj)->unserializer, data, return_value); + php_ion_unserialize(php_ion_obj(unserializer, Z_OBJ_P(ZEND_THIS)), data, return_value); } static ZEND_FUNCTION(ion_unserialize) { zval *data; zend_object *zo_ser = NULL; + zend_array *za_named = 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_PROLOGUE(0, 0) + if (!zend_parse_arg_obj(_arg, &zo_ser, ce_Unserializer, true)) { + if (!zend_parse_arg_array_ht(_arg, &za_named, false, false, false)) { + _error_code = ZPP_ERROR_WRONG_ARG; + _error = "of type array of \\ion\\Unserializer"; + break; + } + zo_ser = create_ion_Unserializer_Unserializer(NULL); + call_constructor(zo_ser, 0, NULL, za_named); + ION_CATCH(OBJ_RELEASE(zo_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_unserialize(ser, data, return_value); + php_ion_unserialize(php_ion_obj(unserializer, zo_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); + } + + if (za_named) { + OBJ_RELEASE(zo_ser); } } @@ -1798,12 +1806,14 @@ 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(); // 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); @@ -1812,7 +1822,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); @@ -1825,8 +1835,8 @@ 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()); @@ -1838,7 +1848,6 @@ PHP_MINIT_FUNCTION(ion) // 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); @@ -1847,17 +1856,14 @@ 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; } PHP_MSHUTDOWN_FUNCTION(ion) { - if (g_sym_tab_php) { - ion_symbol_table_close(g_sym_tab_php); - } - zend_hash_destroy(&g_sym_hash); + g_sym_dtor(); return SUCCESS; } @@ -1865,6 +1871,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(); }