tests++
[awesomized/ext-ion] / ion_private.h
index 3f13897008e2b3732cd6fe5eccc32149c2b61375..57aaa6f9f886787e20b80036149482e14b89a2c2 100644 (file)
@@ -358,7 +358,7 @@ static inline void php_ion_globals_unserializer_dtor(void)
 } while (0)
 
 #define php_ion_obj(type, obj) \
-       ((php_ion_ ## type *) (obj ? ((char *)(obj) - XtOffsetOf(php_ion_ ## type, std)) : NULL))
+       ((php_ion_ ## type *) ((obj) ? ((char *)(obj) - XtOffsetOf(php_ion_ ## type, std)) : NULL))
 
 #define ION_CHECK_RETURN(r, err, ...) do { \
        iERR __err = err; \
@@ -379,22 +379,24 @@ static inline void php_ion_globals_unserializer_dtor(void)
        } \
 } while (0)
 
-#define PTR_CHECK(ptr, ...) do { \
+#define PTR_CHECK_RETURN(ret, ptr, ...) do { \
        if (!(ptr)) { \
                zend_throw_error(NULL, "Uninitialized object"); \
                __VA_ARGS__; \
-               return; \
+               return ret; \
        } \
 } while (0)
+#define PTR_CHECK(ptr, ...) PTR_CHECK_RETURN(, ptr, __VA_ARGS__)
 
-#define OBJ_CHECK(obj, ...) do { \
-       PTR_CHECK(obj, __VA_ARGS__); \
-       PTR_CHECK(*((void **)obj), __VA_ARGS__); \
+#define OBJ_CHECK_RETURN(ret, obj, ...) do { \
+       PTR_CHECK_RETURN(ret, obj, __VA_ARGS__); \
+       PTR_CHECK_RETURN(ret, *((void **)obj), __VA_ARGS__); \
 } while (0)
+#define OBJ_CHECK(obj, ...) OBJ_CHECK_RETURN(, obj, __VA_ARGS__)
 
 static inline ION_STRING *ion_string_from_zend(ION_STRING *is, const zend_string *zs)
 {
-       is->length = zs ? zs->len : 0;
+       is->length = zs ? (SIZE) zs->len : 0;
        is->value = (BYTE *) (zs ? zs->val : NULL);
        return is;
 }
@@ -411,13 +413,6 @@ static inline void update_property_obj(zend_object *obj, const char *n, size_t l
        zend_update_property(obj->ce, obj, n, l, &zobj);
 }
 
-typedef struct php_ion_type {
-       ION_TYPE typ;
-       zend_object std;
-} php_ion_type;
-
-php_ion_decl(type, Type);
-
 #define RETURN_IONTYPE(typ) do { \
        zend_object *__zo = php_ion_type_fetch(typ); \
        if (UNEXPECTED(!__zo)) { \
@@ -438,6 +433,11 @@ static inline zend_object *php_ion_type_fetch(ION_TYPE typ)
        return zend_enum_get_case(ce_Type, Z_STR_P(ztype));
 }
 
+static inline ION_TYPE ion_type_from_enum(zend_object *zo)
+{
+       return (ION_TYPE) Z_LVAL_P(zend_enum_fetch_case_value(zo));
+}
+
 typedef struct php_ion_symbol_iloc {
        ION_SYMBOL_IMPORT_LOCATION loc;
        zend_string *name;
@@ -639,7 +639,7 @@ typedef struct php_ion_decimal_ctx {
 #define php_ion_decimal_ctx_init_max(c, rounding) \
        php_ion_decimal_ctx_init((c), DEC_MAX_DIGITS, DEC_MAX_EMAX, DEC_MIN_EMIN, (rounding), false)
 static inline void php_ion_decimal_ctx_init(decContext *ctx,
-               zend_long digits, zend_long emax, zend_long emin, zend_long round, zend_bool clamp)
+               int digits, int emax, int emin, enum rounding round, zend_bool clamp)
 {
        memset(ctx, 0, sizeof(*ctx));
        ctx->digits = digits;
@@ -771,7 +771,7 @@ static inline zend_long php_usec_from_ion(const decQuad *frac, decContext *ctx)
        return (zend_long) decQuadToUInt32(&result, ctx, DEC_ROUND_HALF_EVEN);
 }
 
-static inline decQuad *ion_ts_frac_from_usec(decQuad *frac, zend_long usec, decContext *ctx)
+static inline decQuad *ion_ts_frac_from_usec(decQuad *frac, int usec, decContext *ctx)
 {
        if (!ctx) {
                ctx = &php_ion_globals.decimal.ctx;
@@ -780,7 +780,7 @@ static inline decQuad *ion_ts_frac_from_usec(decQuad *frac, zend_long usec, decC
        return decQuadDivide(frac, decQuadFromInt32(&us, usec), decQuadFromInt32(&microsecs, 1000000), ctx);
 }
 
-static inline zend_string *php_dt_format_from_precision(int precision)
+static inline zend_string *php_dt_format_from_precision(uint8_t precision)
 {
        switch (precision & 0x7f) {
        case ION_TS_FRAC:
@@ -802,7 +802,7 @@ static inline zend_string *php_dt_format_from_precision(int precision)
 
 static inline timelib_time* php_time_from_ion(const ION_TIMESTAMP *ts, decContext *ctx, zend_string **fmt)
 {
-       timelib_time *time = timelib_time_ctor();
+       timelib_time *time = ecalloc(1, sizeof(*time));
 
        int precision = ION_TS_FRAC;
        ion_timestamp_get_precision(ts,  &precision);
@@ -854,7 +854,7 @@ static inline ION_TIMESTAMP *ion_timestamp_from_php(ION_TIMESTAMP *buf, php_ion_
                                "Invalid precision (%d) of ion\\Timestamp", precision);
        } else switch ((buf->precision = precision)) {
        case ION_TS_FRAC:
-               ion_ts_frac_from_usec(&buf->fraction, ts->time->us, ctx);
+               ion_ts_frac_from_usec(&buf->fraction, (int) ts->time->us, ctx);
                /* fallthrough */
        case ION_TS_SEC:
                buf->seconds = ts->time->s;
@@ -873,7 +873,7 @@ static inline ION_TIMESTAMP *ion_timestamp_from_php(ION_TIMESTAMP *buf, php_ion_
                buf->year = ts->time->y;
                /* fallthrough */
        default:
-               buf->tz_offset = ts->time->z / 60;
+               buf->tz_offset = (short) (ts->time->z / 60);
                if (buf->tz_offset) {
                        buf->precision |= 0x80;
                }
@@ -987,12 +987,33 @@ static inline void php_ion_catalog_symbol_table_zval(php_ion_catalog *obj, ION_S
 
 php_ion_decl(catalog, Catalog, php_ion_catalog_dtor(obj));
 
+typedef struct php_ion_reader_options_ccn_ctx {
+       zend_object *obj;
+       zend_fcall_info fci;
+       zend_fcall_info_cache fcc;
+} php_ion_reader_options_ccn_ctx;
+
 typedef struct php_ion_reader_options {
        ION_READER_OPTIONS opt;
+       php_ion_reader_options_ccn_ctx ccn;
        zend_object *cat, *dec_ctx, *cb, std;
 } php_ion_reader_options;
 
-php_ion_decl(reader_options, Reader_Options);
+static inline void php_ion_reader_options_dtor(php_ion_reader_options *obj)
+{
+       if (obj->cb) {
+               zend_fcall_info_args_clear(&obj->ccn.fci, true);
+       }
+}
+
+php_ion_decl(reader_options, Reader_Options, php_ion_reader_options_dtor(obj));
+
+static inline zend_object *php_ion_reader_options_new(void)
+{
+       zend_object *obj = create_ion_Reader_Options(NULL);
+       zend_call_known_instance_method_with_0_params(obj->ce->constructor, obj, NULL);
+       return obj;
+}
 
 typedef struct php_ion_reader {
        ION_READER *reader;
@@ -1039,26 +1060,31 @@ static inline iERR php_ion_reader_stream_handler(struct _ion_user_stream *user)
 
 static inline iERR on_context_change(void *context, ION_COLLECTION *imports)
 {
+       iERR e = IERR_OK;
+
        if (context) {
-               php_ion_reader *obj = php_ion_obj(reader, context);
-               (void) obj;
+               php_ion_reader_options_ccn_ctx *ctx = context;
+
+               zval zobj;
+               ZVAL_OBJ(&zobj, ctx->obj);
+               zend_fcall_info_argn(&ctx->fci, 1, &zobj);
+               if (SUCCESS != zend_fcall_info_call(&ctx->fci, &ctx->fcc, NULL, NULL)) {
+                       e = IERR_INTERNAL_ERROR;
+               }
+               zend_fcall_info_args_clear(&ctx->fci, false);
        }
-       fprintf(stderr, "%s\n", __FUNCTION__);
-       return IERR_OK;
+       return e;
 }
 
-static ION_READER_CONTEXT_CHANGE_NOTIFIER EMPTY_READER_CHANGE_NOTIFIER = {
-       on_context_change,
-       NULL
-};
-
 static inline void php_ion_reader_ctor(php_ion_reader *obj)
 {
        iERR err;
        php_ion_reader_options *opt = php_ion_obj(reader_options, obj->opt);
 
-       if (opt) {
-               opt->opt.context_change_notifier.context = obj;
+       if (opt && opt->opt.context_change_notifier.context) {
+               php_ion_reader_options_ccn_ctx *ctx = opt->opt.context_change_notifier.context;
+               ctx->obj = &obj->std;
+               opt->opt.context_change_notifier.notify = on_context_change;
        }
        if (obj->type == STREAM_READER) {
                PTR_CHECK(obj->stream.ptr);
@@ -1069,12 +1095,7 @@ static inline void php_ion_reader_ctor(php_ion_reader *obj)
                err = ion_reader_open_stream(&obj->reader, obj, php_ion_reader_stream_handler, opt ? &opt->opt : NULL);
 
        } else {
-               err = ion_reader_open_buffer(&obj->reader,
-                               (BYTE *) obj->buffer->val, obj->buffer->len,
-                               opt ? &opt->opt : NULL);
-       }
-       if (opt) {
-               opt->opt.context_change_notifier.context = NULL;
+               err = ion_reader_open_buffer(&obj->reader, (BYTE *) obj->buffer->val, (SIZE) obj->buffer->len, opt ? &opt->opt : NULL);
        }
 
        ION_CHECK(err);
@@ -1108,6 +1129,13 @@ typedef struct php_ion_writer_options {
 
 php_ion_decl(writer_options, Writer_Options);
 
+static inline zend_object *php_ion_writer_options_new(void)
+{
+       zend_object *obj = create_ion_Writer_Options(NULL);
+       zend_call_known_instance_method_with_0_params(obj->ce->constructor, obj, NULL);
+       return obj;
+}
+
 typedef struct php_ion_writer {
        ION_WRITER *writer;
        enum {
@@ -1222,13 +1250,56 @@ static inline iERR php_ion_writer_buffer_handler(struct _ion_user_stream *user)
        return IERR_OK;
 }
 
+static inline ION_COLLECTION *php_ion_catalog_collection(php_ion_catalog *cat)
+{
+       /* do not look too close */
+       struct {
+               void *owner;
+               ION_SYMBOL_TABLE *sys;
+               ION_COLLECTION collection;
+       } *cat_ptr = (void *) cat->cat;
+       return &cat_ptr->collection;
+}
+
+static inline void php_ion_writer_options_init_shared_imports(php_ion_writer_options *opt)
+{
+       php_ion_catalog *cat = php_ion_obj(catalog, opt->cat);
+       OBJ_CHECK(cat);
+
+       ION_CHECK(ion_writer_options_initialize_shared_imports(&opt->opt));
+
+       ION_COLLECTION *col = php_ion_catalog_collection(cat);
+       if (!ION_COLLECTION_IS_EMPTY(col)) {
+               // holy, nah, forget it batman...
+#ifndef IPCN_pNODE_TO_pDATA
+# define IPCN_pNODE_TO_pDATA(x)    (&((x)->_data[0]))
+#endif
+               ION_COLLECTION_CURSOR cur;
+               ION_COLLECTION_OPEN(col, cur);
+               while (cur) {
+                       ION_SYMBOL_TABLE **ptr;
+                       ION_COLLECTION_NEXT(cur, ptr);
+                       if (*ptr) {
+                               ION_CHECK(ion_writer_options_add_shared_imports_symbol_tables(&opt->opt, ptr, 1));
+                       } else {
+                               break;
+                       }
+               }
+       }
+}
+
 static inline void php_ion_writer_ctor(php_ion_writer *obj)
 {
+       php_ion_writer_options *opt = NULL;
+
        if (obj->opt) {
                update_property_obj(&obj->std, ZEND_STRL("options"), obj->opt);
+               opt = php_ion_obj(writer_options, obj->opt);
+               if (opt->cat) {
+                       php_ion_writer_options_init_shared_imports(opt);
+               }
        }
 
-       php_ion_writer_options *opt = php_ion_obj(writer_options, obj->opt);
        ION_STREAM_HANDLER h;
        if (obj->type == STREAM_WRITER) {
                h = php_ion_writer_stream_handler;
@@ -1238,6 +1309,7 @@ static inline void php_ion_writer_ctor(php_ion_writer *obj)
                php_ion_writer_buffer_init(obj);
        }
 
+
        ION_CHECK(ion_writer_open_stream(&obj->writer, h, obj, opt ? &opt->opt : NULL));
        OBJ_CHECK(obj);
 }
@@ -1247,6 +1319,12 @@ static inline void php_ion_writer_dtor(php_ion_writer *obj)
        if (obj->writer) {
                ion_writer_close(obj->writer);
        }
+       if (obj->opt) {
+               php_ion_writer_options *opt = php_ion_obj(writer_options, obj->opt);
+               if (opt->cat) {
+                       ion_writer_options_close_shared_imports(&opt->opt);
+               }
+       }
        if (obj->type == STREAM_WRITER) {
                if (obj->stream.buf.value) {
                        efree(obj->stream.buf.value);
@@ -1276,6 +1354,8 @@ static inline void php_ion_serializer_php_ctor(php_ion_serializer_php *ser_obj)
        ser_obj->serializer.ids = global_ser->ids;
        ser_obj->serializer.tmp = global_ser->tmp;
 
+       zend_update_property_bool(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("multiSequence"),
+                       ser_obj->serializer.multi_seq);
        zend_update_property_bool(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callMagicSerialize"),
                        ser_obj->serializer.call_magic);
        if (ser_obj->serializer.call_custom) {
@@ -1286,7 +1366,7 @@ static inline void php_ion_serializer_php_ctor(php_ion_serializer_php *ser_obj)
                zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize"));
        }
        if (!ser_obj->opt) {
-               ser_obj->opt = create_ion_Writer_Options(NULL);
+               ser_obj->opt = php_ion_writer_options_new();
        } else {
                GC_ADDREF(ser_obj->opt);
        }
@@ -1326,11 +1406,11 @@ static inline void php_ion_serialize_struct(php_ion_serializer *ser, zend_array
                                prop_name = k->val;
                                prop_len = k->len;
                        }
-                       ion_string_assign_cstr(&is, (char *) prop_name, prop_len);
+                       ion_string_assign_cstr(&is, (char *) prop_name, (SIZE) prop_len);
                } else {
                        char *end = buf + sizeof(buf) - 1;
                        char *ptr = zend_print_long_to_buf(end, (zend_long) h);
-                       ion_string_assign_cstr(&is, ptr, end - ptr);
+                       ion_string_assign_cstr(&is, ptr, (SIZE) (end - ptr));
                }
 
                // WATCH OUT: field names need to be copied
@@ -1465,15 +1545,16 @@ static inline bool can_call_magic_serialize(php_ion_serializer *ser, zend_class_
        return ce->__serialize && ser->call_magic;
 }
 
-static inline bool can_call_iface_serialize(php_ion_serializer *, zend_class_entry *ce)
+static inline bool can_call_iface_serialize(php_ion_serializer *ser, zend_class_entry *ce)
 {
-       return !!ce->serialize;
+  (void) ser;
+       return !!ce->serialize; // NOLINT
 }
 
 static inline bool can_call_custom_serialize(php_ion_serializer *ser, zend_object *zobject, zend_function **fn)
 {
        if (ser->call_custom) {
-               return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom));
+               return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom)); // NOLINT
        }
        return false;
 }
@@ -1656,7 +1737,7 @@ void php_ion_serialize(php_ion_serializer *ser, zval *zv, zval *return_value)
        writer->type = BUFFER_WRITER;
 
        if (ser->options) {
-               zo_opt = writer->opt = create_ion_Writer_Options(NULL);
+               zo_opt = writer->opt = php_ion_writer_options_new();
                php_ion_obj(writer_options, writer->opt)->opt = *ser->options;
        }
 
@@ -1693,17 +1774,19 @@ static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php *ser_o
        ser_obj->unserializer.tmp = global_ser->tmp;
        ser_obj->unserializer.addref = global_ser->addref;
 
-       zend_update_property_bool(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callMagicSerialize"),
+       zend_update_property_bool(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("multiSequence"),
+                       ser_obj->unserializer.multi_seq);
+       zend_update_property_bool(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callMagicUnserialize"),
                        ser_obj->unserializer.call_magic);
        if (ser_obj->unserializer.call_custom) {
-               zend_update_property_str(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize"),
+               zend_update_property_str(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomUnserialize"),
                                ser_obj->unserializer.call_custom);
                ser_obj->unserializer.call_custom = zend_string_tolower(ser_obj->unserializer.call_custom);
        } else {
-               zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize"));
+               zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomUnserialize"));
        }
        if (!ser_obj->opt) {
-               ser_obj->opt = create_ion_Reader_Options(NULL);
+               ser_obj->opt = php_ion_reader_options_new();
        } else {
                GC_ADDREF(ser_obj->opt);
        }
@@ -1734,7 +1817,7 @@ static inline bool can_call_iface_unserialize(php_ion_unserializer *ser, zend_cl
 static inline bool can_call_custom_unserialize(php_ion_unserializer *ser, zend_object *zobject, zend_function **fn)
 {
        if (ser->call_custom) {
-               return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom));
+               return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom)); // NOLINT
        }
        return false;
 }
@@ -2018,9 +2101,9 @@ static inline void php_ion_unserialize_list(php_ion_unserializer *ser, zval *ret
 static inline void php_ion_reader_read_lob(ION_READER *reader, zval *return_value)
 {
        zend_string *zstr = zend_string_alloc(0x1000, 0);
-again:
+again: ;
        SIZE read = 0;
-       iERR err = ion_reader_read_lob_bytes(reader, (BYTE *) zstr->val, zstr->len, &read);
+       iERR err = ion_reader_read_lob_bytes(reader, (BYTE *) zstr->val, (SIZE) zstr->len, &read);
        if (err == IERR_BUFFER_TOO_SMALL) {
                zstr = zend_string_extend(zstr, zstr->len << 2, 0);
                goto again;
@@ -2064,7 +2147,7 @@ static inline void php_ion_reader_read_int(ION_READER *reader, zval *return_valu
                RETVAL_LONG(i64);
                goto done;
 
-       case IERR_NUMERIC_OVERFLOW:
+       case IERR_NUMERIC_OVERFLOW: ;
                SIZE max, len;
                ION_CHECK(ion_int_char_length(num, &max));
                zend_string *zs = zend_string_alloc(max, 0);
@@ -2090,7 +2173,7 @@ static inline void php_ion_unserialize_backref(php_ion_unserializer *ser, zval *
                zend_hash_next_index_insert(ser->addref, return_value);
        } else {
                zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INTERNAL_ERROR,
-                               "Could not find back reference %ld", Z_LVAL_P(return_value));
+                               "Could not find back reference " ZEND_LONG_FMT, Z_LVAL_P(return_value));
        }
 }
 
@@ -2188,7 +2271,7 @@ static inline void php_ion_unserialize_annotations(php_ion_unserializer *ser)
 static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *return_value, ION_TYPE *typ)
 {
        if (typ) {
-               memcpy(&ser->type, typ, sizeof(ser->type));
+               memcpy(&ser->type, typ, sizeof(ser->type)); // NOLINT
        } else {
                typ = &ser->type;
                ION_CHECK(ion_reader_next(ser->reader, typ));
@@ -2216,7 +2299,7 @@ static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *ret
                ION_CHECK(ion_reader_read_null(ser->reader, typ));
                RETURN_NULL();
 
-       case tid_BOOL_INT:
+       case tid_BOOL_INT: ;
                BOOL bval;
                ION_CHECK(ion_reader_read_bool(ser->reader, &bval));
                RETURN_BOOL(bval);
@@ -2238,7 +2321,7 @@ static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *ret
                }
                return;
 
-       case tid_FLOAT_INT:
+       case tid_FLOAT_INT: ;
                double d;
                ION_CHECK(ion_reader_read_double(ser->reader, &d));
                RETURN_DOUBLE(d);
@@ -2256,7 +2339,7 @@ static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *ret
                zend_hash_next_index_insert(ser->ids, return_value);
                return;
 
-       case tid_SYMBOL_INT:
+       case tid_SYMBOL_INT: ;
                ION_SYMBOL sym;
                ION_CHECK(ion_reader_read_ion_symbol(ser->reader, &sym));
                php_ion_symbol_zval(&sym, return_value);
@@ -2267,7 +2350,7 @@ static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *ret
                zend_hash_next_index_insert(ser->ids, return_value);
                return;
 
-       case tid_STRING_INT:
+       case tid_STRING_INT: ;
                ION_STRING str;
                ION_CHECK(ion_reader_read_string(ser->reader, &str));
                RETVAL_STRINGL((char *) str.value, str.length);
@@ -2374,7 +2457,7 @@ void php_ion_unserialize(php_ion_unserializer *ser, zval *zdata, zval *return_va
        }
 
        if (ser->options) {
-               zo_opt = reader->opt = create_ion_Reader_Options(NULL);
+               zo_opt = reader->opt = php_ion_reader_options_new();
                php_ion_obj(reader_options, reader->opt)->opt = *ser->options;
        }