coverage++
[awesomized/ext-ion] / ion.c
diff --git a/ion.c b/ion.c
index 81231d653c5baba9c8f59c530e2bd4ea03f3c457..28d5a5cdc3356a15198b7248e02361e5c83bec64 100644 (file)
--- a/ion.c
+++ b/ion.c
@@ -409,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();
@@ -423,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)
@@ -861,7 +864,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) {
@@ -952,7 +960,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)
 {
@@ -1033,9 +1043,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;
 }
@@ -1195,7 +1203,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();
@@ -1577,9 +1585,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)
@@ -1591,9 +1596,6 @@ 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)
@@ -1607,17 +1609,12 @@ 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)
@@ -1627,7 +1624,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)
 {
@@ -1785,6 +1791,7 @@ PHP_MINIT_FUNCTION(ion)
        if (SUCCESS != g_sym_init())  {
                return FAILURE;
        }
+       g_intern_str_init();
 
        // Catalog
        php_ion_register(catalog, Catalog, zend_ce_countable);
@@ -1826,6 +1833,7 @@ 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