improve symbol comparison
[awesomized/ext-ion] / ion_private.h
index 56da560b39b19b374c40364c90ce22c4853bb369..809f8532618933d371cdeab49e131cf9f52c280c 100644 (file)
@@ -22,6 +22,7 @@ typedef struct php_ion_serializer {
 
        zend_string *call_custom;
        zend_bool call_magic;
+       zend_bool multi_seq;
 
        uint32_t level;
        HashTable *ids;
@@ -44,6 +45,7 @@ typedef struct php_ion_unserializer {
 
        zend_string *call_custom;
        zend_bool call_magic;
+       zend_bool multi_seq;
 
        uint32_t level;
        HashTable *ids;
@@ -170,6 +172,7 @@ static zend_class_entry
        *ce_Collection,
        *ce_Decimal,
        *ce_Decimal_Context,
+       *ce_Decimal_Context_Rounding,
        *ce_LOB,
        *ce_Reader,
        *ce_Reader_Options,
@@ -257,13 +260,6 @@ static zend_class_entry
        PTR_CHECK(*((void **)obj)); \
 } while (0)
 
-static inline ION_STRING *ion_string_from_cstr(ION_STRING *is, const char *s, size_t l)
-{
-       is->length = l;
-       is->value = (BYTE *) s;
-       return is;
-}
-
 static inline ION_STRING *ion_string_from_zend(ION_STRING *is, const zend_string *zs)
 {
        is->length = zs ? zs->len : 0;
@@ -336,6 +332,27 @@ typedef struct php_ion_symbol {
        zend_object *iloc, std;
 } php_ion_symbol;
 
+static inline int php_ion_symbol_zval_compare(zval *zv1, zval *zv2) {
+       zend_string *zs1 = zval_get_string(zv1);
+       zend_string *zs2 = zval_get_string(zv2);
+
+       if (EG(exception)) {
+               return 0;
+       }
+
+       int result;
+       if (zs1->len > zs2->len) {
+               result = 1;
+       } else if (zs2->len > zs1->len) {
+               result = -1;
+       } else {
+               result = memcmp(zs1->val, zs2->val, zs1->len);
+       }
+       zend_string_release(zs1);
+       zend_string_release(zs2);
+       return result;
+}
+
 static inline void php_ion_symbol_ctor(php_ion_symbol *obj)
 {
        zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("sid"),
@@ -381,6 +398,10 @@ static inline void php_ion_symbol_zval(ION_SYMBOL *sym_ptr, zval *return_value)
        }
 
        php_ion_symbol_ctor(sym);
+
+       if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr)) {
+               GC_DELREF(sym->iloc);
+       }
 }
 
 php_ion_decl(symbol, Symbol, php_ion_symbol_dtor(obj));
@@ -396,25 +417,33 @@ typedef struct php_ion_decimal_ctx {
        zend_object std;
 } php_ion_decimal_ctx;
 
-static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx *obj) {
-       zval tmp, *zbits = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("bits"), 1, &tmp);
+#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)
+{
+       memset(ctx, 0, sizeof(*ctx));
+       ctx->digits = digits;
+       ctx->emax = emax;
+       ctx->emin = emin;
+       ctx->round = round;
+       ctx->clamp = clamp;
+}
 
-       int bits = 128;
-       if (zbits != &EG(uninitialized_zval)) {
-               bits = Z_LVAL_P(zbits);
-       } else {
-               zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("bits"), bits);
+static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx *obj, zend_object *o_round)
+{
+       if (!obj->ctx.digits) {
+               php_ion_decimal_ctx_init_max(&obj->ctx, DEC_ROUND_HALF_EVEN);
        }
-       switch (bits) {
-       case 32:
-       case 64:
-       case 128:
-               decContextDefault(&obj->ctx, bits);
-               break;
-       default:
-               zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
-                               "Decimal context only allows 32, 64 or 128 bits");
+       if (o_round) {
+               update_property_obj(&obj->std, ZEND_STRL("round"), o_round);
+       } else {
+               zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("round"), obj->ctx.round);
        }
+       zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("digits"), obj->ctx.digits);
+       zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("eMax"), obj->ctx.emax);
+       zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("eMin"), obj->ctx.emin);
+       zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("clamp"), obj->ctx.clamp);
 }
 
 php_ion_decl(decimal_ctx, Decimal_Context);
@@ -424,6 +453,25 @@ typedef struct php_ion_decimal {
        zend_object *ctx, std;
 } php_ion_decimal;
 
+static inline void php_ion_decimal_from_zend_long(ION_DECIMAL *dec, decContext *ctx, zend_long num)
+{
+       if (num <= INT32_MAX && num >= INT32_MIN) {
+               ION_CHECK(ion_decimal_from_int32(dec, num));
+       } else if (num > 0 && num <= UINT32_MAX) {
+               ION_CHECK(ion_decimal_from_uint32(dec, num));
+       } else {
+               ION_INT *iint;
+               ION_CHECK(ion_int_alloc(NULL, &iint));
+               ION_CHECK(ion_int_from_long(iint, num),
+                               ion_int_free(iint));
+               /* WATCH OUT: BS API */
+               dec->type = ION_DECIMAL_TYPE_QUAD;
+               ION_CHECK(ion_decimal_from_ion_int(dec, ctx, iint),
+                               ion_int_free(iint));
+               ion_int_free(iint);
+       }
+}
+
 static inline zend_string *php_ion_decimal_to_string(ION_DECIMAL *dec)
 {
        zend_string *zstr = zend_string_alloc(ION_DECIMAL_STRLEN(dec), 0);
@@ -431,7 +479,7 @@ static inline zend_string *php_ion_decimal_to_string(ION_DECIMAL *dec)
        return zend_string_truncate(zstr, strlen(zstr->val), 0);
 }
 
-static inline void php_ion_decimal_to_int(ION_DECIMAL *dec, decContext *ctx, zend_long *l)
+static inline void php_ion_decimal_to_zend_long(ION_DECIMAL *dec, decContext *ctx, zend_long *l)
 {
        ION_INT *ii = NULL;
        ION_CHECK(ion_int_alloc(NULL, &ii));
@@ -442,20 +490,41 @@ static inline void php_ion_decimal_to_int(ION_DECIMAL *dec, decContext *ctx, zen
        ion_int_free(ii);
 }
 
+static inline bool php_ion_decimal_fits_zend_long(php_ion_decimal *obj)
+{
+       int32_t result;
+
+       if (!ion_decimal_is_integer(&obj->dec)) {
+               return false;
+       }
+
+       result  = 1;
+       ion_decimal_compare(&obj->dec, &g_ion_dec_zend_max, &g_dec_ctx, &result);
+       if (result == 1) {
+               return false;
+       }
+       result = -1;
+       ion_decimal_compare(&obj->dec, &g_ion_dec_zend_min, &g_dec_ctx, &result);
+       if (result == -1) {
+               return false;
+       }
+       return true;
+}
+
 static inline void php_ion_decimal_ctor(php_ion_decimal *obj)
 {
        if (!obj->ctx) {
                zval zdc;
                object_init_ex(&zdc, ce_Decimal_Context);
                obj->ctx = Z_OBJ(zdc);
-               php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx, obj->ctx));
+               php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx, obj->ctx), NULL);
                GC_DELREF(obj->ctx);
        }
        update_property_obj(&obj->std, ZEND_STRL("context"), obj->ctx);
 
-       if (ion_decimal_is_integer(&obj->dec)) {
+       if (php_ion_decimal_fits_zend_long(obj)) {
                zend_long l;
-               php_ion_decimal_to_int(&obj->dec, &php_ion_obj(decimal_ctx, obj->ctx)->ctx, &l);
+               php_ion_decimal_to_zend_long(&obj->dec, &php_ion_obj(decimal_ctx, obj->ctx)->ctx, &l);
                zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("number"), l);
        } else {
                zend_string *zstr = php_ion_decimal_to_string(&obj->dec);
@@ -934,17 +1003,17 @@ static inline void php_ion_serialize_struct(php_ion_serializer *ser, zend_array
                        size_t prop_len;
                        const char *class_name, *prop_name;
                        if (props && (SUCCESS == zend_unmangle_property_name_ex(k, &class_name, &prop_name, &prop_len)) && class_name) {
-                               ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("p"))));
-                               ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, class_name, prop_name - class_name - 1)));
+                               ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("p"))));
+                               ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_assign_cstr(&is, (char *) class_name, prop_name - class_name - 1)));
                        } else {
                                prop_name = k->val;
                                prop_len = k->len;
                        }
-                       ION_CHECK(ion_writer_write_field_name(ser->writer, ion_string_from_cstr(&is, prop_name, prop_len)));
+                       ION_CHECK(ion_writer_write_field_name(ser->writer, ion_string_assign_cstr(&is, (char *) prop_name, prop_len)));
                } else {
                        char buf[MAX_LENGTH_OF_LONG + 1], *end = buf + sizeof(buf) - 1;
                        char *ptr = zend_print_long_to_buf(end, (zend_long) h);
-                       ION_CHECK(ion_writer_write_field_name(ser->writer, ion_string_from_cstr(&is, ptr, end - ptr)));
+                       ION_CHECK(ion_writer_write_field_name(ser->writer, ion_string_assign_cstr(&is, ptr, end - ptr)));
                }
 
                php_ion_serialize_zval(ser, v);
@@ -985,9 +1054,9 @@ static inline void php_ion_serialize_object_iface(php_ion_serializer *ser, zend_
        ZVAL_OBJ(&tmp, zobject);
        if (SUCCESS == zobject->ce->serialize(&tmp, &buf, &len, NULL)) {
                ION_STRING is;
-               ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("S"))));
+               ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("S"))));
                ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name)));
-               ION_CHECK(ion_writer_write_string(ser->writer, ion_string_from_cstr(&is, (char *) buf, len)));
+               ION_CHECK(ion_writer_write_string(ser->writer, ion_string_assign_cstr(&is, (char *) buf, len)));
                efree(buf);
        } else if (!EG(exception)){
                zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR,
@@ -1005,7 +1074,7 @@ static inline void php_ion_serialize_object_magic(php_ion_serializer *ser, zend_
 
        if (IS_ARRAY == Z_TYPE(rv)) {
                ION_STRING is;
-               ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, fn ? "C" : "O", 1)));
+               ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_assign_cstr(&is, fn ? "C" : "O", 1)));
                ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name)));
                php_ion_serialize_zval(ser, &rv);
                zval_ptr_dtor(&rv);
@@ -1020,7 +1089,7 @@ static inline void php_ion_serialize_object_magic(php_ion_serializer *ser, zend_
 static inline void php_ion_serialize_object_enum(php_ion_serializer *ser, zend_object *zobject)
 {
        ION_STRING is;
-       ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("E"))));
+       ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("E"))));
 
        ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name)));
        zval *z_cname = zend_enum_fetch_case_name(zobject);
@@ -1032,10 +1101,10 @@ static inline void php_ion_serialize_object_std(php_ion_serializer *ser, zend_ob
        ION_STRING is;
 
        if (zobject->ce != zend_standard_class_def) {
-               ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("c"))));
+               ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("c"))));
                ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name)));
        } else {
-               ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("o"))));
+               ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("o"))));
        }
 
        zval zobj;
@@ -1131,7 +1200,7 @@ static inline void php_ion_serialize_refcounted(php_ion_serializer *ser, zval *z
        if (zend_hash_index_exists(ser->ids, idx)) {
                zval *num = zend_hash_index_find(ser->ids, idx);
 
-               ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("r"))));
+               ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("r"))));
                ION_CHECK(ion_writer_write_int64(ser->writer, Z_LVAL_P(num)));
        } else {
                zval num;
@@ -1156,7 +1225,7 @@ static inline void php_ion_serialize_refcounted(php_ion_serializer *ser, zval *z
                        break;
 
                case IS_REFERENCE:
-                       ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("R"))));
+                       ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("R"))));
                        php_ion_serialize_zval(ser, Z_REFVAL_P(zv));
                        break;
                }
@@ -1197,6 +1266,41 @@ static inline void php_ion_serialize_zval(php_ion_serializer *ser, zval *zv)
 
 php_ion_decl(serializer_php, Serializer_PHP, php_ion_serializer_php_dtor(obj));
 
+static inline void php_ion_serialize_ex(php_ion_serializer *ser, zval *zv)
+{
+       HashPosition pos;
+       HashTable *arr = NULL;
+
+       if (ser->multi_seq) {
+               if (Z_TYPE_P(zv) != IS_ARRAY || !zend_array_is_list(Z_ARRVAL_P(zv))) {
+                       zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
+                                       "Expected a packed, consecutively numerically indexed array as argument to the multi sequence serializer");
+                       return;
+               }
+
+               arr = Z_ARRVAL_P(zv);
+
+               zend_hash_internal_pointer_reset_ex(arr, &pos);
+               zv = zend_hash_get_current_data_ex(arr, &pos);
+       }
+
+       while (zv) {
+               /* start off with a global PHP annotation instead of repeating it all over the place */
+               if (0 == php_ion_globals_serializer_step()) {
+                       ION_STRING is;
+                       ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("PHP"))));
+               }
+               php_ion_serialize_zval(ser, zv);
+               php_ion_globals_serializer_exit();
+
+               if (!ser->multi_seq) {
+                       break;
+               }
+               zend_hash_move_forward_ex(arr, &pos);
+               zv = zend_hash_get_current_data_ex(arr, &pos);
+       }
+}
+
 void php_ion_serialize(php_ion_serializer *ser, zval *zv, zval *return_value)
 {
        zend_object *zo_opt = NULL, *zo_ser = NULL;
@@ -1224,14 +1328,9 @@ void php_ion_serialize(php_ion_serializer *ser, zval *zv, zval *return_value)
        ser->writer = writer->writer;
        ser->buffer = &writer->buffer.str;
 
-       /* start off with a global PHP annotation instead of repeating it all over the place */
-       if (0 == php_ion_globals_serializer_step()) {
-               ION_STRING is;
-               ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("PHP"))),
-                               if (zo_ser) OBJ_RELEASE(zo_ser));
+       if (!EG(exception)) {
+               php_ion_serialize_ex(ser, zv);
        }
-       php_ion_serialize_zval(ser, zv);
-       php_ion_globals_serializer_exit();
 
        /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
        ion_writer_flush(ser->writer, NULL);
@@ -1358,6 +1457,27 @@ static inline void php_ion_unserialize_object_iface(php_ion_unserializer *ser, z
        }
 }
 
+static inline void php_ion_unserialize_field_name(php_ion_unserializer *ser, zend_string **key)
+{
+       // FIXME: symbol table
+       ION_STRING name;
+       ION_CHECK(ion_reader_get_field_name(ser->reader, &name));
+       if (!name.length) {
+               ION_SYMBOL *is_ptr;
+               ION_CHECK(ion_reader_get_field_name_symbol(ser->reader, &is_ptr));
+               if (!ION_SYMBOL_IS_NULL(is_ptr) && is_ptr->value.length) {
+                       name = is_ptr->value;
+               } else if (is_ptr) {
+                       char buf[MAX_LENGTH_OF_LONG + 1 + 1] = {0}, *end = buf + sizeof(buf) - 1, *ptr;
+                       ptr = zend_print_long_to_buf(end, is_ptr->sid);
+                       *--ptr = '$';
+                       *key = zend_string_init(ptr, end - ptr, 0);
+                       return;
+               }
+       }
+       *key = zend_string_from_ion(&name);
+}
+
 static void php_ion_unserialize_props(php_ion_unserializer *ser, zval *return_value)
 {
        zend_hash_next_index_insert(ser->ids, return_value);
@@ -1372,9 +1492,9 @@ static void php_ion_unserialize_props(php_ion_unserializer *ser, zval *return_va
                        break;
                }
 
-               ION_STRING is;
-               ION_CHECK(ion_reader_get_field_name(ser->reader, &is));
-               zend_string *key = zend_string_from_ion(&is);
+               zend_string *key;
+               php_ion_unserialize_field_name(ser, &key);
+               ION_CATCH();
 
                zval zvalue;
                php_ion_unserialize_zval(ser, &zvalue, &typ);
@@ -1392,6 +1512,13 @@ static void php_ion_unserialize_props(php_ion_unserializer *ser, zval *return_va
        ION_CHECK(ion_reader_step_out(ser->reader));
 }
 
+/**
+ * @link https://amzn.github.io/ion-docs/docs/spec.html#struct
+ * When two fields in the same struct have the same name [...] Implementations must preserve all such fields,
+ * i.e., they may not discard fields that have repeated names. However, implementations may reorder fields
+ * (the binary format identifies structs that are sorted by symbolID), so certain operations may lead to
+ * nondeterministic behavior.
+ */
 static inline void php_ion_unserialize_hash(php_ion_unserializer *ser, zval *return_value)
 {
        zend_hash_next_index_insert(ser->ids, return_value);
@@ -1402,21 +1529,31 @@ static inline void php_ion_unserialize_hash(php_ion_unserializer *ser, zval *ret
                ION_TYPE typ;
                ION_CHECK(ion_reader_next(ser->reader, &typ));
 
-               ION_STRING name;
-               ION_CHECK(ion_reader_get_field_name(ser->reader, &name));
-               zend_string *key = zend_string_from_ion(&name);
+               if (typ == tid_EOF) {
+                       break;
+               }
+
+               zend_string *key;
+               php_ion_unserialize_field_name(ser, &key);
+               ION_CATCH();
 
                zval zvalue;
                php_ion_unserialize_zval(ser, &zvalue, &typ);
                ION_CATCH(zend_string_release(key));
 
-               if (typ == tid_EOF) {
-                       zend_string_release(key);
-                       break;
+               // FIXME:: too naive; b0rked if the previous value is an array
+               if (zend_symtable_exists(HASH_OF(return_value), key)) {
+                       zval tmp, *prev = zend_hash_find(HASH_OF(return_value), key);
+                       if (Z_TYPE_P(prev) != IS_ARRAY) {
+                               array_init(&tmp);
+                               Z_TRY_ADDREF_P(prev);
+                               zend_hash_next_index_insert(Z_ARRVAL(tmp), prev);
+                               prev = zend_hash_update(HASH_OF(return_value), key, &tmp);
+                       }
+                       zend_hash_next_index_insert(Z_ARRVAL_P(prev), &zvalue);
+               } else {
+                       zend_symtable_update(HASH_OF(return_value), key, &zvalue);
                }
-
-               zend_symtable_update(HASH_OF(return_value), key, &zvalue);
-
                zend_string_release(key);
        }
 
@@ -1519,14 +1656,14 @@ static inline void php_ion_unserialize_list(php_ion_unserializer *ser, zval *ret
                ION_TYPE typ;
                ION_CHECK(ion_reader_next(ser->reader, &typ));
 
-               zval next;
-               php_ion_unserialize_zval(ser, &next, &typ);
-               ION_CATCH();
-
                if (typ == tid_EOF) {
                        break;
                }
 
+               zval next;
+               php_ion_unserialize_zval(ser, &next, &typ);
+               ION_CATCH();
+
                zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &next);
        }
 
@@ -1585,10 +1722,10 @@ static inline void php_ion_reader_read_int(ION_READER *reader, zval *return_valu
        case IERR_NUMERIC_OVERFLOW:
                SIZE max, len;
                ION_CHECK(ion_int_char_length(num, &max));
-               zend_string *zs = zend_string_alloc(max-1, 0);
+               zend_string *zs = zend_string_alloc(max, 0);
 
                err = ion_int_to_char(num, (BYTE *) zs->val, max, &len);
-               ZEND_ASSERT(len == zs->len);
+               zs->val[zs->len = len] = 0;
                RETVAL_STR(zs);
                /* fall through */
 
@@ -1622,6 +1759,10 @@ static inline void php_ion_unserialize_annotations(php_ion_unserializer *ser)
                ION_STRING ann_str;
                ION_CHECK(ion_reader_get_an_annotation(ser->reader, i, &ann_str));
 
+               if (ann_str.length != 1) {
+                       continue;
+               }
+
                switch (*ann_str.value) {
                case 'R':
                        if (ser->annotations.makeref) {
@@ -1714,19 +1855,21 @@ static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *ret
                ZVAL_DEREF(return_value);
        }
 
-       BOOL bval;
-       ION_CHECK(ion_reader_is_null(ser->reader, &bval));
-       if (bval) {
-               goto read_null;
+       if (ION_TYPE_INT(*typ) > 0) {
+               BOOL is_null;
+               ION_CHECK(ion_reader_is_null(ser->reader, &is_null));
+               if (is_null) {
+                       RETURN_NULL();
+               }
        }
 
        switch (ION_TYPE_INT(*typ)) {
        case tid_NULL_INT:
-read_null: ;
                ION_CHECK(ion_reader_read_null(ser->reader, typ));
                RETURN_NULL();
 
        case tid_BOOL_INT:
+               BOOL bval;
                ION_CHECK(ion_reader_read_bool(ser->reader, &bval));
                RETURN_BOOL(bval);
 
@@ -1823,6 +1966,28 @@ unserialize_struct: ;
 
 php_ion_decl(unserializer_php, Unserializer_PHP, php_ion_unserializer_php_dtor(obj));
 
+static inline void php_ion_unserialize_ex(php_ion_unserializer *ser, zval *return_value)
+{
+       if (ser->multi_seq) {
+               array_init(return_value);
+       }
+
+       do {
+               zval tmp;
+               ZVAL_NULL(&tmp);
+               php_ion_globals_unserializer_step();
+               php_ion_unserialize_zval(ser, &tmp, NULL);
+               php_ion_globals_unserializer_exit();
+               ION_CATCH(zval_ptr_dtor(&tmp));
+
+               if (!ser->multi_seq) {
+                       RETURN_COPY_VALUE(&tmp);
+               } else if (ser->type != tid_EOF) {
+                       zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
+               }
+       } while (ser->type != tid_EOF);
+}
+
 void php_ion_unserialize(php_ion_unserializer *ser, zval *zdata, zval *return_value)
 {
        zend_object *zo_opt = NULL, *zo_ser = NULL;
@@ -1840,23 +2005,24 @@ void php_ion_unserialize(php_ion_unserializer *ser, zval *zdata, zval *return_va
        zend_object *zo_reader;
        php_ion_reader *reader;
        ZVAL_DEREF(zdata);
-       switch (Z_TYPE_P(zdata)) {
-       case IS_STRING:
-               zo_reader = create_ion_Reader_Reader(ce_Reader_Buffer_Reader);
-               reader = php_ion_obj(reader, zo_reader);
-               reader->type = BUFFER_READER;
-               reader->buffer = zend_string_copy(Z_STR_P(zdata));
-               break;
 
-       case IS_RESOURCE:
+       if (Z_TYPE_P(zdata) == IS_RESOURCE) {
                zo_reader = create_ion_Reader_Reader(ce_Reader_Stream_Reader);
                reader = php_ion_obj(reader, zo_reader);
                reader->type = STREAM_READER;
                php_stream_from_zval_no_verify(reader->stream.ptr, zdata);
-               break;
-
-       default:
-               ZEND_ASSERT(!IS_STRING && !IS_RESOURCE);
+       } else if (Z_TYPE_P(zdata) <= IS_STRING) {
+               zo_reader = create_ion_Reader_Reader(ce_Reader_Buffer_Reader);
+               reader = php_ion_obj(reader, zo_reader);
+               reader->type = BUFFER_READER;
+               reader->buffer = zval_get_string(zdata);
+       } else {
+               zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
+                               "Invalid source to unserialize; expected string or resource");
+               if (zo_ser) {
+                       OBJ_RELEASE(zo_ser);
+               }
+               return;
        }
 
        if (ser->options) {
@@ -1867,9 +2033,9 @@ void php_ion_unserialize(php_ion_unserializer *ser, zval *zdata, zval *return_va
        php_ion_reader_ctor(reader);
        ser->reader = reader->reader;
 
-       php_ion_globals_unserializer_step();
-       php_ion_unserialize_zval(ser, return_value, NULL);
-       php_ion_globals_unserializer_exit();
+       if (!EG(exception)) {
+               php_ion_unserialize_ex(ser, return_value);
+       }
 
        OBJ_RELEASE(zo_reader);
        if (zo_opt)  {