X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=ion_private.h;h=50d99213dbc236740c9ece3ebad95491d43f9c60;hb=5a6253e058e01c9467b15eb6d7714666c6e62a9e;hp=6dc21d409e4adb34f227478584917ee8add810d8;hpb=a81110e746d52da11103a5c4c46c0bab809d8598;p=awesomized%2Fext-ion diff --git a/ion_private.h b/ion_private.h index 6dc21d4..50d9921 100644 --- a/ion_private.h +++ b/ion_private.h @@ -11,8 +11,15 @@ */ #include "php.h" +#include "ext/standard/php_var.h" #include "ext/date/php_date.h" +#if PHP_DEBUG +# define LOCAL static +#else +# define LOCAL static inline +#endif + #define DECNUMDIGITS 34 /* DECQUAD_Pmax */ #include "ionc/ion.h" @@ -71,7 +78,7 @@ static HashTable g_sym_map; g_sym_map_add(ION_SYS_SID_ ## c_name, enum_name, sizeof(enum_name)-1); \ } while (0) -static inline void g_sym_hash_add(int sid, const char *str, size_t len) +LOCAL void g_sym_hash_add(int sid, const char *str, size_t len) { zval zl, zs; ZVAL_LONG(&zl, sid); @@ -80,14 +87,14 @@ static inline void g_sym_hash_add(int sid, const char *str, size_t len) zend_hash_index_add(&g_sym_hash, sid, &zs); } -static inline void g_sym_map_add(int sid, const char *str, size_t len) +LOCAL void g_sym_map_add(int sid, const char *str, size_t len) { zval zv; ZVAL_LONG(&zv, sid); zend_hash_str_add(&g_sym_map, str, len, &zv); } -static inline int g_sym_init(void) +LOCAL int g_sym_init(void) { zend_hash_init(&g_sym_hash, 0, NULL, NULL, 1); zend_hash_init(&g_sym_map, 0, NULL, NULL, 1); @@ -122,6 +129,26 @@ static inline int g_sym_init(void) return SUCCESS; } +static struct { + zend_string *Year, *Month, *Day, *Min, *Sec, *Frac, *MinTZ, *SecTZ, *FracTZ; +} g_intern_str; + +static void g_intern_str_init() +{ +#define NEW_INTERN_STR(s) \ + g_intern_str.s = zend_string_init_interned(#s, sizeof(#s)-1, 1) + NEW_INTERN_STR(Year); + NEW_INTERN_STR(Month); + NEW_INTERN_STR(Day); + NEW_INTERN_STR(Min); + NEW_INTERN_STR(Sec); + NEW_INTERN_STR(Frac); + NEW_INTERN_STR(MinTZ); + NEW_INTERN_STR(SecTZ); + NEW_INTERN_STR(FracTZ); +#undef NEW_INTERN_STR +} + typedef struct php_ion_serializer { ION_WRITER *writer; ION_WRITER_OPTIONS *options; @@ -136,7 +163,7 @@ typedef struct php_ion_serializer { HashTable *tmp; } php_ion_serializer; -typedef struct php_ion_annotaions { +typedef struct php_ion_annotations { uint8_t shared_symtab:1; uint8_t backref:1; uint8_t makeref:1; @@ -195,7 +222,6 @@ ZEND_DECLARE_MODULE_GLOBALS(ion); static zend_class_entry *ce_Catalog, - *ce_Collection, *ce_Decimal, *ce_Decimal_Context, *ce_Decimal_Context_Rounding, @@ -218,6 +244,7 @@ static zend_class_entry *ce_Symbol_Table_Shared, *ce_Symbol_Table_System, *ce_Timestamp, + *ce_Timestamp_Format, *ce_Timestamp_Precision, *ce_Type, *ce_Unserializer, @@ -231,17 +258,17 @@ static zend_class_entry *ce_Writer_Writer ; -static inline void php_ion_globals_symbols_init(void) +LOCAL void php_ion_globals_symbols_init(void) { zend_hash_init(&php_ion_globals.symbol.cache, 0, NULL, ZVAL_PTR_DTOR, 0); } -static inline void php_ion_globals_symbols_dtor(void) +LOCAL void php_ion_globals_symbols_dtor(void) { zend_hash_destroy(&php_ion_globals.symbol.cache); } -static inline void php_ion_globals_serializer_init(void) +LOCAL void php_ion_globals_serializer_init(void) { php_ion_serializer *s = &php_ion_globals.serializer; HashTable *h = php_ion_globals._ht.serializer; @@ -250,7 +277,7 @@ static inline void php_ion_globals_serializer_init(void) zend_hash_init(s->ids = &h[1], 0, NULL, NULL, 0); } -static inline uint32_t php_ion_globals_serializer_step(void) +LOCAL uint32_t php_ion_globals_serializer_step(void) { php_ion_serializer *s = &php_ion_globals.serializer; uint32_t level; @@ -262,7 +289,7 @@ static inline uint32_t php_ion_globals_serializer_step(void) return level; } -static inline uint32_t php_ion_globals_serializer_exit(void) +LOCAL uint32_t php_ion_globals_serializer_exit(void) { php_ion_serializer *s = &php_ion_globals.serializer; @@ -274,7 +301,7 @@ static inline uint32_t php_ion_globals_serializer_exit(void) return s->level; } -static inline void php_ion_globals_serializer_dtor(void) +LOCAL void php_ion_globals_serializer_dtor(void) { php_ion_serializer *s = &php_ion_globals.serializer; @@ -290,7 +317,7 @@ void ZVAL_ADDREF(zval *zv) Z_TRY_ADDREF_P(zv); } } -static inline void php_ion_globals_unserializer_init(void) +LOCAL void php_ion_globals_unserializer_init(void) { php_ion_unserializer *s = &php_ion_globals.unserializer; HashTable *h = php_ion_globals._ht.unserializer; @@ -300,7 +327,7 @@ static inline void php_ion_globals_unserializer_init(void) zend_hash_init(s->addref = &h[2], 0, NULL, ZVAL_ADDREF, 0); } -static inline void php_ion_globals_unserializer_step(void) +LOCAL void php_ion_globals_unserializer_step(void) { php_ion_unserializer *s = &php_ion_globals.unserializer; @@ -311,7 +338,7 @@ static inline void php_ion_globals_unserializer_step(void) } } -static inline void php_ion_globals_unserializer_exit(void) +LOCAL void php_ion_globals_unserializer_exit(void) { php_ion_unserializer *s = &php_ion_globals.unserializer; @@ -323,7 +350,7 @@ static inline void php_ion_globals_unserializer_exit(void) } } -static inline void php_ion_globals_unserializer_dtor(void) +LOCAL void php_ion_globals_unserializer_dtor(void) { php_ion_unserializer *s = &php_ion_globals.unserializer; @@ -332,9 +359,18 @@ static inline void php_ion_globals_unserializer_dtor(void) zend_hash_destroy(s->tmp); } -#define php_ion_decl(type, cname, ...) \ +#define php_ion_obj(type, zo) \ + ((php_ion_ ##type *) php_ion_obj_ex(zo, XtOffsetOf(php_ion_ ## type, std))) +LOCAL void *php_ion_obj_ex(void *obj, ptrdiff_t offset) { + if (obj) { + return ((char *) obj) - offset; + } + return NULL; +} + +#define php_ion_decl(type, cname) \ static zend_object_handlers oh_ ## cname; \ - static inline zend_object *create_ion_ ## cname(zend_class_entry *ce) \ + LOCAL zend_object *create_ion_ ## cname(zend_class_entry *ce) \ { \ if (!ce) ce = ce_ ## cname; \ php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \ @@ -343,12 +379,19 @@ static inline void php_ion_globals_unserializer_dtor(void) o->std.handlers = &oh_ ## cname; \ return &o->std; \ } \ - static inline void free_ion_ ## cname(zend_object *std) \ + LOCAL void free_ion_ ## cname(zend_object *std) \ { \ php_ion_ ## type *obj = php_ion_obj(type, std); \ - __VA_ARGS__; \ + php_ion_ ## type ## _dtor(obj); \ zend_object_std_dtor(std); \ - (void) obj; \ + } \ + LOCAL zend_object *clone_ion_ ## cname(zend_object *std) \ + { \ + php_ion_ ## type *old_obj = php_ion_obj(type, std), \ + *new_obj = php_ion_obj(type, create_ion_ ## cname(std->ce)); \ + php_ion_ ## type ## _copy(new_obj, old_obj); \ + (void) old_obj; \ + return &new_obj->std; \ } #define php_ion_register(type, cname, ...) do { \ ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \ @@ -356,14 +399,12 @@ static inline void php_ion_globals_unserializer_dtor(void) memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \ oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \ oh_ ## cname .free_obj = free_ion_ ## cname; \ + oh_ ## cname .clone_obj = clone_ion_ ## cname; \ } while (0) -#define php_ion_obj(type, obj) \ - ((php_ion_ ## type *) (obj ? ((char *)(obj) - XtOffsetOf(php_ion_ ## type, std)) : NULL)) - #define ION_CHECK_RETURN(r, err, ...) do { \ iERR __err = err; \ - if (__err) { \ + if (UNEXPECTED(__err)) { \ zend_throw_exception_ex(spl_ce_RuntimeException, __err, "%s: %s", ion_error_to_str(__err), #err); \ __VA_ARGS__; \ return r; \ @@ -374,51 +415,46 @@ static inline void php_ion_globals_unserializer_dtor(void) ION_CHECK_RETURN(, err, __VA_ARGS__) #define ION_CATCH(...) do { \ - if (EG(exception)) { \ + if (UNEXPECTED(EG(exception))) { \ __VA_ARGS__; \ return; \ } \ } while (0) -#define PTR_CHECK(ptr, ...) do { \ - if (!(ptr)) { \ +#define PTR_CHECK_RETURN(ret, ptr, ...) do { \ + if (UNEXPECTED(!(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) +LOCAL 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; } -static inline zend_string *zend_string_from_ion(const ION_STRING *s) +LOCAL zend_string *zend_string_from_ion(const ION_STRING *s) { return zend_string_init((const char *) s->value, s->length, 0); } -static inline void update_property_obj(zend_object *obj, const char *n, size_t l, zend_object *p) +LOCAL void update_property_obj(zend_object *obj, const char *n, size_t l, zend_object *p) { zval zobj; ZVAL_OBJ(&zobj, p); 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)) { \ @@ -427,7 +463,7 @@ php_ion_decl(type, Type); RETURN_OBJ_COPY(__zo); \ } while(0) -static inline zend_object *php_ion_type_fetch(ION_TYPE typ) +LOCAL zend_object *php_ion_type_fetch(ION_TYPE typ) { zend_long index = ION_TYPE_INT(typ); zval *ztype = zend_hash_index_find(ce_Type->backed_enum_table, index); @@ -439,25 +475,38 @@ static inline zend_object *php_ion_type_fetch(ION_TYPE typ) return zend_enum_get_case(ce_Type, Z_STR_P(ztype)); } +LOCAL 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; zend_object std; } php_ion_symbol_iloc; -static inline void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc *obj) +LOCAL void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc *obj) { zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("location"), obj->loc.location); zend_update_property_str(obj->std.ce, &obj->std, ZEND_STRL("name"), obj->name); ion_string_from_zend(&obj->loc.name, obj->name); } -static inline void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc *obj) +LOCAL void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc *obj) { zend_string_release(obj->name); } -php_ion_decl(symbol_iloc, Symbol_ImportLocation, php_ion_symbol_iloc_dtor(obj)); +LOCAL void php_ion_symbol_iloc_copy(php_ion_symbol_iloc *new_obj, php_ion_symbol_iloc *old_obj) +{ + zend_objects_clone_members(&new_obj->std, &old_obj->std); + new_obj->name = zend_string_copy(old_obj->name); + ion_string_from_zend(&new_obj->loc.name, new_obj->name); + new_obj->loc.location = old_obj->loc.location; +} + +php_ion_decl(symbol_iloc, Symbol_ImportLocation); typedef struct php_ion_symbol { ION_SYMBOL sym; @@ -465,7 +514,7 @@ typedef struct php_ion_symbol { zend_object *iloc, std; } php_ion_symbol; -static inline int php_ion_symbol_zval_compare(zval *zv1, zval *zv2) { +LOCAL int php_ion_symbol_zval_compare(zval *zv1, zval *zv2) { zend_string *zs1 = zval_get_string(zv1); zend_string *zs2 = zval_get_string(zv2); @@ -486,7 +535,7 @@ static inline int php_ion_symbol_zval_compare(zval *zv1, zval *zv2) { return result; } -static inline void php_ion_symbol_ctor(php_ion_symbol *obj) +LOCAL void php_ion_symbol_ctor(php_ion_symbol *obj) { zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("sid"), obj->sym.sid); @@ -504,14 +553,28 @@ static inline void php_ion_symbol_ctor(php_ion_symbol *obj) } } -static inline void php_ion_symbol_dtor(php_ion_symbol *obj) +LOCAL void php_ion_symbol_dtor(php_ion_symbol *obj) { if (obj->value) { zend_string_release(obj->value); } } -static inline void php_ion_symbol_zval(ION_SYMBOL *sym_ptr, zval *return_value) +LOCAL void php_ion_symbol_copy(php_ion_symbol *new_obj, php_ion_symbol *old_obj) +{ + zend_objects_clone_members(&new_obj->std, &old_obj->std); + new_obj->sym = old_obj->sym; + if (old_obj->value) { + new_obj->value = zend_string_copy(old_obj->value); + ion_string_from_zend(&new_obj->sym.value, new_obj->value); + } + + if ((new_obj->iloc = old_obj->iloc)) { + new_obj->sym.import_location = php_ion_obj(symbol_iloc, new_obj->iloc)->loc; + } +} + +LOCAL void php_ion_symbol_zval(ION_SYMBOL *sym_ptr, zval *return_value) { object_init_ex(return_value, ce_Symbol); php_ion_symbol *sym = php_ion_obj(symbol, Z_OBJ_P(return_value)); @@ -537,7 +600,7 @@ static inline void php_ion_symbol_zval(ION_SYMBOL *sym_ptr, zval *return_value) } } -static inline zval *php_ion_global_symbol_fetch_by_enum(zend_string *name) +LOCAL zval *php_ion_global_symbol_fetch_by_enum(zend_string *name) { zval *zgs = zend_hash_find(&php_ion_globals.symbol.cache, name); if (!zgs) { @@ -558,7 +621,7 @@ static inline zval *php_ion_global_symbol_fetch_by_enum(zend_string *name) return zgs; } -php_ion_decl(symbol, Symbol, php_ion_symbol_dtor(obj)); +php_ion_decl(symbol, Symbol); typedef struct php_ion_symbol_table { ION_SYMBOL_TABLE *tab; @@ -566,21 +629,25 @@ typedef struct php_ion_symbol_table { zend_object std; } php_ion_symbol_table; -static inline void php_ion_symbol_table_ctor(php_ion_symbol_table *obj) +LOCAL void php_ion_symbol_table_ctor(php_ion_symbol_table *obj) { OBJ_CHECK(obj); - ION_STRING is; - if (IERR_OK == ion_symbol_table_get_name(obj->tab, &is)) { - zend_update_property_stringl(obj->std.ce, &obj->std, ZEND_STRL("name"), (char *) is.value, is.length); - } - int32_t iv; - if (IERR_OK == ion_symbol_table_get_version(obj->tab, &iv)) { - zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("version"), iv); + ION_SYMBOL_TABLE_TYPE typ = ist_EMPTY; + ion_symbol_table_get_type(obj->tab, &typ); + if (typ != ist_LOCAL) { + ION_STRING is; + if (IERR_OK == ion_symbol_table_get_name(obj->tab, &is)) { + zend_update_property_stringl(obj->std.ce, &obj->std, ZEND_STRL("name"), (char *) is.value, is.length); + } + int32_t iv; + if (IERR_OK == ion_symbol_table_get_version(obj->tab, &iv)) { + zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("version"), iv); + } } } -static inline void php_ion_symbol_table_dtor(php_ion_symbol_table *obj) +LOCAL void php_ion_symbol_table_dtor(php_ion_symbol_table *obj) { if (obj->tab) { if (obj->dtor) { @@ -590,7 +657,20 @@ static inline void php_ion_symbol_table_dtor(php_ion_symbol_table *obj) } } -static inline void php_ion_symbol_table_import(php_ion_symbol_table *obj, php_ion_symbol_table *import) +LOCAL void php_ion_symbol_table_copy(php_ion_symbol_table *new_obj, php_ion_symbol_table *old_obj) +{ + // do not clone cache members + // zend_objects_clone_members(...) + if ((new_obj->dtor = old_obj->dtor)) { + ION_CHECK(ion_symbol_table_clone_with_owner(old_obj->tab, &new_obj->tab, NULL)); + } else { + new_obj->tab = old_obj->tab; + } + // update non-cache members + php_ion_symbol_table_ctor(new_obj); +} + +LOCAL void php_ion_symbol_table_import(php_ion_symbol_table *obj, php_ion_symbol_table *import) { OBJ_CHECK(obj); OBJ_CHECK(import); @@ -610,7 +690,7 @@ static inline void php_ion_symbol_table_import(php_ion_symbol_table *obj, php_io } } -static inline void php_ion_symbol_table_symbol_zval(php_ion_symbol_table *obj, ION_SYMBOL *sym, zval *return_value) +LOCAL void php_ion_symbol_table_symbol_zval(php_ion_symbol_table *obj, ION_SYMBOL *sym, zval *return_value) { zval tmp; zval *zsyms = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("symbols"), 0, &tmp); @@ -630,7 +710,7 @@ static inline void php_ion_symbol_table_symbol_zval(php_ion_symbol_table *obj, I } } -php_ion_decl(symbol_table, Symbol_Table, php_ion_symbol_table_dtor(obj)); +php_ion_decl(symbol_table, Symbol_Table); typedef struct php_ion_decimal_ctx { decContext ctx; @@ -639,8 +719,8 @@ 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) +LOCAL void php_ion_decimal_ctx_init(decContext *ctx, + int digits, int emax, int emin, enum rounding round, zend_bool clamp) { memset(ctx, 0, sizeof(*ctx)); ctx->digits = digits; @@ -650,7 +730,7 @@ static inline void php_ion_decimal_ctx_init(decContext *ctx, ctx->clamp = clamp; } -static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx *obj, zend_object *o_round) +LOCAL 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); @@ -666,6 +746,16 @@ static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx *obj, zend_objec zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("clamp"), obj->ctx.clamp); } +LOCAL void php_ion_decimal_ctx_dtor(php_ion_decimal_ctx *obj) +{ +} + +LOCAL void php_ion_decimal_ctx_copy(php_ion_decimal_ctx *new_obj, php_ion_decimal_ctx *old_obj) +{ + zend_objects_clone_members(&new_obj->std, &old_obj->std); + new_obj->ctx = old_obj->ctx; +} + php_ion_decl(decimal_ctx, Decimal_Context); typedef struct php_ion_decimal { @@ -673,7 +763,7 @@ 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) +LOCAL 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)); @@ -692,14 +782,14 @@ static inline void php_ion_decimal_from_zend_long(ION_DECIMAL *dec, decContext * } } -static inline zend_string *php_ion_decimal_to_string(ION_DECIMAL *dec) +LOCAL zend_string *php_ion_decimal_to_string(ION_DECIMAL *dec) { zend_string *zstr = zend_string_alloc(ION_DECIMAL_STRLEN(dec), 0); (void) ion_decimal_to_string(dec, zstr->val); return zend_string_truncate(zstr, strlen(zstr->val), 0); } -static inline void php_ion_decimal_to_zend_long(ION_DECIMAL *dec, decContext *ctx, zend_long *l) +LOCAL 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)); @@ -710,7 +800,7 @@ static inline void php_ion_decimal_to_zend_long(ION_DECIMAL *dec, decContext *ct ion_int_free(ii); } -static inline bool php_ion_decimal_fits_zend_long(php_ion_decimal *obj) +LOCAL bool php_ion_decimal_fits_zend_long(php_ion_decimal *obj) { int32_t result; @@ -731,7 +821,7 @@ static inline bool php_ion_decimal_fits_zend_long(php_ion_decimal *obj) return true; } -static inline void php_ion_decimal_ctor(php_ion_decimal *obj) +LOCAL void php_ion_decimal_ctor(php_ion_decimal *obj) { if (!obj->ctx) { zval zdc; @@ -753,16 +843,23 @@ static inline void php_ion_decimal_ctor(php_ion_decimal *obj) } } -static inline void php_ion_decimal_dtor(php_ion_decimal *obj) +LOCAL void php_ion_decimal_dtor(php_ion_decimal *obj) { ion_decimal_free(&obj->dec); } -php_ion_decl(decimal, Decimal, php_ion_decimal_dtor(obj)); +LOCAL void php_ion_decimal_copy(php_ion_decimal *new_obj, php_ion_decimal *old_obj) +{ + zend_objects_clone_members(&new_obj->std, &old_obj->std); + new_obj->ctx = old_obj->ctx; + ION_CHECK(ion_decimal_copy(&new_obj->dec, &old_obj->dec)); +} + +php_ion_decl(decimal, Decimal); typedef php_date_obj php_ion_timestamp; -static inline zend_long php_usec_from_ion(const decQuad *frac, decContext *ctx) +LOCAL zend_long php_usec_from_ion(const decQuad *frac, decContext *ctx) { if (!ctx) { ctx = &php_ion_globals.decimal.ctx; @@ -772,7 +869,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) +LOCAL decQuad *ion_ts_frac_from_usec(decQuad *frac, int usec, decContext *ctx) { if (!ctx) { ctx = &php_ion_globals.decimal.ctx; @@ -781,33 +878,47 @@ static inline decQuad *ion_ts_frac_from_usec(decQuad *frac, zend_long usec, decC return decQuadDivide(frac, decQuadFromInt32(&us, usec), decQuadFromInt32(µsecs, 1000000), ctx); } -static inline zend_string *php_dt_format_from_precision(int precision) +LOCAL zend_string *php_ion_timestamp_format_fetch(zend_string *fmt_case) { - switch (precision & 0x7f) { + return Z_STR_P(zend_enum_fetch_case_value(zend_enum_get_case(ce_Timestamp_Format, fmt_case))); +} + +LOCAL zend_string *php_dt_format_from_precision(uint8_t precision) +{ + switch (precision) { + case ION_TS_FRAC | 0x80: + return php_ion_timestamp_format_fetch(g_intern_str.FracTZ); case ION_TS_FRAC: - return zend_string_init(ZEND_STRL("c"), 0); + return php_ion_timestamp_format_fetch(g_intern_str.Frac); + case ION_TS_SEC | 0x80: + return php_ion_timestamp_format_fetch(g_intern_str.SecTZ); case ION_TS_SEC: - return zend_string_init(ZEND_STRL("Y-m-d\\TH:i:sP"), 0); + return php_ion_timestamp_format_fetch(g_intern_str.Sec); + case ION_TS_MIN | 0x80: + return php_ion_timestamp_format_fetch(g_intern_str.MinTZ); case ION_TS_MIN: - return zend_string_init(ZEND_STRL("Y-m-d\\TH:iP"), 0); + return php_ion_timestamp_format_fetch(g_intern_str.Min); case ION_TS_DAY: - return zend_string_init(ZEND_STRL("Y-m-d\\T"), 0); + return php_ion_timestamp_format_fetch(g_intern_str.Day); case ION_TS_MONTH: - return zend_string_init(ZEND_STRL("Y-m\\T"), 0); + return php_ion_timestamp_format_fetch(g_intern_str.Month); case ION_TS_YEAR: - return zend_string_init(ZEND_STRL("Y\\T"), 0); + return php_ion_timestamp_format_fetch(g_intern_str.Year); default: - return zend_string_init(ZEND_STRL("c"), 0); + return ZSTR_CHAR('c'); } } -static inline timelib_time* php_time_from_ion(const ION_TIMESTAMP *ts, decContext *ctx, zend_string **fmt) +LOCAL 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); - switch (precision) { + /* defaults */ + time->y = 1970; + time->m = 1; + time->d = 1; + + switch (ts->precision & 0x7f) { case ION_TS_FRAC: time->us = php_usec_from_ion(&ts->fraction, ctx); /* fallthrough */ @@ -829,7 +940,7 @@ static inline timelib_time* php_time_from_ion(const ION_TIMESTAMP *ts, decContex /* fallthrough */ default: time->z = ts->tz_offset * 60; - if (time->z) { + if (time->z || ts->precision & 0x80) { time->zone_type = TIMELIB_ZONETYPE_OFFSET; } else { time->zone_type = TIMELIB_ZONETYPE_ID; @@ -838,24 +949,24 @@ static inline timelib_time* php_time_from_ion(const ION_TIMESTAMP *ts, decContex } if (fmt) { - *fmt = php_dt_format_from_precision(precision); + *fmt = php_dt_format_from_precision(ts->precision); } return time; } -static inline ION_TIMESTAMP *ion_timestamp_from_php(ION_TIMESTAMP *buf, php_ion_timestamp *ts, decContext *ctx) +LOCAL ION_TIMESTAMP *ion_timestamp_from_php(ION_TIMESTAMP *buf, php_ion_timestamp *ts, decContext *ctx) { memset(buf, 0, sizeof(*buf)); zval tmp; int precision = Z_LVAL_P(zend_read_property(ts->std.ce, &ts->std, ZEND_STRL("precision"), 0, &tmp)); - if (!precision || precision > ION_TS_FRAC) { + if (!precision || precision > (ION_TS_FRAC|0x80)) { zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG, "Invalid precision (%d) of ion\\Timestamp", precision); - } else switch ((buf->precision = precision)) { + } else switch ((buf->precision = precision) & 0x7f) { 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; @@ -874,7 +985,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; } @@ -883,7 +994,7 @@ static inline ION_TIMESTAMP *ion_timestamp_from_php(ION_TIMESTAMP *buf, php_ion_ return buf; } -static inline void php_ion_timestamp_ctor(php_ion_timestamp *obj, zend_long precision, zend_string *fmt, zend_string *dt, zval *tz) +LOCAL void php_ion_timestamp_ctor(php_ion_timestamp *obj, zend_long precision, zend_string *fmt, zend_string *dt, zval *tz) { if (!obj->time) { php_date_initialize(obj, dt ? dt->val : "", dt ? dt->len : 0, fmt ? fmt->val : NULL, tz, PHP_DATE_INIT_CTOR); @@ -900,19 +1011,54 @@ typedef struct php_ion_catalog { zend_object std; } php_ion_catalog; -static inline void php_ion_catalog_ctor(php_ion_catalog *obj) +LOCAL void php_ion_catalog_ctor(php_ion_catalog *obj) { ION_CHECK(ion_catalog_open(&obj->cat)); } -static inline void php_ion_catalog_dtor(php_ion_catalog *obj) +LOCAL void php_ion_catalog_dtor(php_ion_catalog *obj) { if (obj->cat) { ion_catalog_close(obj->cat); } } -static inline zend_string *ion_symbol_table_to_key(ION_SYMBOL_TABLE *tab) +LOCAL 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; +} +// see https://github.com/amzn/ion-c/issues/269 +#ifndef IPCN_pNODE_TO_pDATA +# define IPCN_pNODE_TO_pDATA(x) (&((x)->_data[0])) +#endif + +LOCAL void php_ion_catalog_copy(php_ion_catalog *new_obj, php_ion_catalog *old_obj) +{ + // do not clone cache members + php_ion_catalog_ctor(new_obj); + OBJ_CHECK(new_obj); + + ION_COLLECTION *col = php_ion_catalog_collection(old_obj); + if (!ION_COLLECTION_IS_EMPTY(col)) { + 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_catalog_add_symbol_table(new_obj->cat, *ptr)); + } + } + } +} + +LOCAL zend_string *ion_symbol_table_to_key(ION_SYMBOL_TABLE *tab) { int32_t version; ION_STRING is; @@ -928,7 +1074,7 @@ static inline zend_string *ion_symbol_table_to_key(ION_SYMBOL_TABLE *tab) return s.s; } -static inline void php_ion_catalog_add_symbol_table(php_ion_catalog *obj, php_ion_symbol_table *tab) +LOCAL void php_ion_catalog_add_symbol_table(php_ion_catalog *obj, php_ion_symbol_table *tab) { OBJ_CHECK(obj); OBJ_CHECK(tab); @@ -954,7 +1100,7 @@ static inline void php_ion_catalog_add_symbol_table(php_ion_catalog *obj, php_io } } -static inline void php_ion_catalog_symbol_table_zval(php_ion_catalog *obj, ION_SYMBOL_TABLE *tab, zval *return_value) +LOCAL void php_ion_catalog_symbol_table_zval(php_ion_catalog *obj, ION_SYMBOL_TABLE *tab, zval *return_value) { zend_string *key = ion_symbol_table_to_key(tab); PTR_CHECK(key); @@ -969,11 +1115,9 @@ static inline void php_ion_catalog_symbol_table_zval(php_ion_catalog *obj, ION_S } } - php_error_docref(NULL, E_NOTICE, "Previously unknown ion\\Symbol\\Table encountered: %s", key->val); - object_init_ex(return_value, ce_Symbol_Table_Shared); php_ion_symbol_table *o_tab = php_ion_obj(symbol_table, Z_OBJ_P(return_value)); - OBJ_CHECK(o_tab, zend_string_release(key)); + o_tab->tab = tab; php_ion_symbol_table_ctor(o_tab); if (ztabs) { @@ -986,15 +1130,52 @@ static inline void php_ion_catalog_symbol_table_zval(php_ion_catalog *obj, ION_S zend_string_release(key); } -php_ion_decl(catalog, Catalog, php_ion_catalog_dtor(obj)); +php_ion_decl(catalog, Catalog); + +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; +LOCAL void php_ion_reader_options_dtor(php_ion_reader_options *obj) +{ + if (obj->cb) { + zend_fcall_info_args_clear(&obj->ccn.fci, true); + } +} + +LOCAL void php_ion_reader_options_copy(php_ion_reader_options *new_obj, php_ion_reader_options *old_obj) +{ + zend_objects_clone_members(&new_obj->std, &old_obj->std); + + new_obj->opt = old_obj->opt; + new_obj->cat = old_obj->cat; + new_obj->dec_ctx = old_obj->dec_ctx; + new_obj->cb = old_obj->cb; + if (new_obj->cb) { + zval zcb; + ZVAL_OBJ(&zcb, new_obj->cb); + zend_fcall_info_init(&zcb, 0, &new_obj->ccn.fci, &new_obj->ccn.fcc, NULL, NULL); + new_obj->opt.context_change_notifier.context = &new_obj->ccn; + } +} + php_ion_decl(reader_options, Reader_Options); +LOCAL 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; ION_TYPE state; @@ -1012,7 +1193,7 @@ typedef struct php_ion_reader { zend_object *opt, std; } php_ion_reader; -static inline iERR php_ion_reader_stream_handler(struct _ion_user_stream *user) +LOCAL iERR php_ion_reader_stream_handler(struct _ion_user_stream *user) { php_ion_reader *reader = (php_ion_reader *) user->handler_state; size_t remaining = 0, spare = reader->stream.buf.length; @@ -1038,50 +1219,51 @@ static inline iERR php_ion_reader_stream_handler(struct _ion_user_stream *user) return IERR_READ_ERROR; } -static inline iERR on_context_change(void *context, ION_COLLECTION *imports) +LOCAL 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) +LOCAL 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); GC_ADDREF(obj->stream.ptr->res); - obj->stream.buf.length = opt && opt->opt.allocation_page_size ? opt->opt.allocation_page_size : 0x10000; + obj->stream.buf.length = opt && opt->opt.chunk_threshold ? opt->opt.chunk_threshold : 0x4000; obj->stream.buf.value = emalloc(obj->stream.buf.length); 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); OBJ_CHECK(obj); } -static inline void php_ion_reader_dtor(php_ion_reader *obj) + +LOCAL void php_ion_reader_dtor(php_ion_reader *obj) { if (obj->reader) { ion_reader_close(obj->reader); @@ -1100,15 +1282,37 @@ static inline void php_ion_reader_dtor(php_ion_reader *obj) } } -php_ion_decl(reader, Reader_Reader, php_ion_reader_dtor(obj)); +#define php_ion_reader_copy(n,o) +php_ion_decl(reader, Reader_Reader); +#define clone_ion_Reader_Reader NULL typedef struct php_ion_writer_options { ION_WRITER_OPTIONS opt; - zend_object *cat, *dec_ctx, *col, std; + zend_object *cat, *dec_ctx, std; } php_ion_writer_options; +LOCAL void php_ion_writer_options_copy(php_ion_writer_options *new_obj, php_ion_writer_options *old_obj) +{ + zend_objects_clone_members(&new_obj->std, &old_obj->std); + + new_obj->opt = old_obj->opt; + new_obj->cat = old_obj->cat; + new_obj->dec_ctx = old_obj->dec_ctx; +} + +LOCAL void php_ion_writer_options_dtor(php_ion_writer_options *obj) +{ +} + php_ion_decl(writer_options, Writer_Options); +LOCAL 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 { @@ -1117,8 +1321,8 @@ typedef struct php_ion_writer { } type; union { struct { - zval val; smart_str str; + struct _ion_user_stream *usr; } buffer; struct { ION_STRING buf; @@ -1129,7 +1333,7 @@ typedef struct php_ion_writer { } php_ion_writer; -static inline iERR php_ion_writer_stream_handler(struct _ion_user_stream *user) +LOCAL iERR php_ion_writer_stream_handler(struct _ion_user_stream *user) { php_ion_writer *writer = (php_ion_writer *) user->handler_state; @@ -1144,92 +1348,117 @@ static inline iERR php_ion_writer_stream_handler(struct _ion_user_stream *user) return IERR_OK; } -#define REF_STR() do { \ - ZVAL_NEW_STR(ref, obj->buffer.str.s); \ - GC_ADDREF(obj->buffer.str.s); \ -} while (0) - -#define NEW_REF_STR() do {\ - if (Z_STR_P(ref) != obj->buffer.str.s) { \ - zval_ptr_dtor(ref); \ - REF_STR(); \ - } \ -} while(0) - -static inline void php_ion_writer_stream_init(php_ion_writer *obj, php_ion_writer_options *opt) +LOCAL void php_ion_writer_stream_init(php_ion_writer *obj, php_ion_writer_options *opt) { PTR_CHECK(obj->stream.ptr); GC_ADDREF(obj->stream.ptr->res); - obj->stream.buf.length = opt ? opt->opt.allocation_page_size : 0x1000; + obj->stream.buf.length = opt ? opt->opt.temp_buffer_size : 0x1000; obj->stream.buf.value = emalloc(obj->stream.buf.length); } -static inline void php_ion_writer_buffer_init(php_ion_writer *obj) + +LOCAL void php_ion_writer_buffer_offer(php_ion_writer *obj) { - zval *ref = &obj->buffer.val; - ZVAL_DEREF(ref); + if (obj->buffer.usr) { + obj->buffer.usr->curr = (BYTE *) &obj->buffer.str.s->val[obj->buffer.str.s->len]; + obj->buffer.usr->limit = obj->buffer.usr->curr + obj->buffer.str.a - obj->buffer.str.s->len; + } +} - smart_str_alloc(&obj->buffer.str, 0, 0); - smart_str_0(&obj->buffer.str); - REF_STR(); +LOCAL void php_ion_writer_buffer_init(php_ion_writer *obj) +{ + smart_str_alloc(&obj->buffer.str, 0, false); + php_ion_writer_buffer_offer(obj); } -static inline void php_ion_writer_buffer_grow(php_ion_writer *obj) +LOCAL void php_ion_writer_buffer_reset(php_ion_writer *obj) { - zval *ref = &obj->buffer.val; - ZVAL_DEREF(ref); + smart_str_free(&obj->buffer.str); + memset(&obj->buffer.str, 0, sizeof(obj->buffer.str)); + php_ion_writer_buffer_init(obj); +} - switch (GC_REFCOUNT(obj->buffer.str.s)) { - case 2: - // nothing to do - break; - case 1: - // we've been separated - GC_ADDREF(obj->buffer.str.s); - break; - default: - // we have to separate - fprintf(stderr, "SEPARATE\n"); - obj->buffer.str.s = zend_string_dup(obj->buffer.str.s, 0); - break; +LOCAL zend_string *php_ion_writer_buffer_copy(php_ion_writer *obj) +{ + if (obj->buffer.usr) { + // ensure that brain-dead ion_stream interface calls us again + obj->buffer.usr->curr = NULL; + obj->buffer.usr->limit = NULL; } + smart_str_0(&obj->buffer.str); + return zend_string_copy(obj->buffer.str.s); +} - zend_string *old = obj->buffer.str.s; - GC_DELREF(old); - smart_str_erealloc(&obj->buffer.str, obj->buffer.str.a << 1); - if (old == obj->buffer.str.s) { - GC_ADDREF(old); - } else if(old == Z_STR_P(ref)) { - ZVAL_NULL(ref); - } +LOCAL void php_ion_writer_buffer_separate(php_ion_writer *obj, bool grow) +{ + // see zend_string_separate and smart_str_erealloc + zend_string *old_str = obj->buffer.str.s; + zend_string *new_str = zend_string_alloc(obj->buffer.str.a << grow, false); + memcpy(new_str->val, old_str->val, new_str->len = old_str->len); + zend_string_release(old_str); + obj->buffer.str.s = new_str; +} - NEW_REF_STR(); +LOCAL void php_ion_writer_buffer_grow(php_ion_writer *obj) +{ + if (obj->buffer.usr && obj->buffer.usr->curr) { + obj->buffer.str.s->len = obj->buffer.usr->curr - (BYTE *) obj->buffer.str.s->val; + } + if (obj->buffer.usr && obj->buffer.usr->curr && obj->buffer.usr->curr == obj->buffer.usr->limit) { + if (UNEXPECTED(GC_REFCOUNT(obj->buffer.str.s) > 1)) { + php_ion_writer_buffer_separate(obj, true); + } else { + smart_str_erealloc(&obj->buffer.str, obj->buffer.str.a << 1); + } + } else if (UNEXPECTED(GC_REFCOUNT(obj->buffer.str.s) > 1)) { + php_ion_writer_buffer_separate(obj, false); + } + php_ion_writer_buffer_offer(obj); } -static inline iERR php_ion_writer_buffer_handler(struct _ion_user_stream *user) + +LOCAL iERR php_ion_writer_buffer_handler(struct _ion_user_stream *user) { php_ion_writer *writer = (php_ion_writer *) user->handler_state; + writer->buffer.usr = user; + php_ion_writer_buffer_grow(writer); + return IERR_OK; +} - if (user->curr) { - writer->buffer.str.s->len += user->curr - (BYTE *) &writer->buffer.str.s->val[writer->buffer.str.s->len]; - smart_str_0(&writer->buffer.str); - if (user->limit == user->curr) { - php_ion_writer_buffer_grow(writer); +LOCAL 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... + 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)); + } } } - user->curr = (BYTE *) &writer->buffer.str.s->val[writer->buffer.str.s->len]; - user->limit = user->curr + writer->buffer.str.a - writer->buffer.str.s->len; - - return IERR_OK; } -static inline void php_ion_writer_ctor(php_ion_writer *obj) +LOCAL 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; @@ -1239,15 +1468,22 @@ 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); } -static inline void php_ion_writer_dtor(php_ion_writer *obj) +LOCAL 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); @@ -1260,23 +1496,26 @@ static inline void php_ion_writer_dtor(php_ion_writer *obj) smart_str_0(&obj->buffer.str); zend_string_release(obj->buffer.str.s); } - zval_ptr_dtor(&obj->buffer.val); } } -php_ion_decl(writer, Writer_Writer, php_ion_writer_dtor(obj)); +#define php_ion_writer_copy(o,n) +php_ion_decl(writer, Writer_Writer); +#define clone_ion_Writer_Writer NULL typedef struct php_ion_serializer_php { php_ion_serializer serializer; zend_object *opt, std; } php_ion_serializer_php; -static inline void php_ion_serializer_php_ctor(php_ion_serializer_php *ser_obj) +LOCAL void php_ion_serializer_php_ctor(php_ion_serializer_php *ser_obj) { php_ion_serializer *global_ser = &php_ion_globals.serializer; 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) { @@ -1287,7 +1526,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); } @@ -1296,16 +1535,16 @@ static inline void php_ion_serializer_php_ctor(php_ion_serializer_php *ser_obj) GC_DELREF(ser_obj->opt); } -static inline void php_ion_serializer_php_dtor(php_ion_serializer_php *obj) +LOCAL void php_ion_serializer_php_dtor(php_ion_serializer_php *obj) { if (obj->serializer.call_custom) { zend_string_release(obj->serializer.call_custom); } } -static inline void php_ion_serialize_zval(php_ion_serializer *, zval *); +LOCAL void php_ion_serialize_zval(php_ion_serializer *, zval *); -static inline void php_ion_serialize_struct(php_ion_serializer *ser, zend_array *arr, bool unmangle_props, bool annotate_props) +LOCAL void php_ion_serialize_struct(php_ion_serializer *ser, zend_array *arr, bool unmangle_props, bool annotate_props) { ION_CHECK(ion_writer_start_container(ser->writer, tid_STRUCT)); @@ -1327,11 +1566,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 @@ -1346,7 +1585,7 @@ static inline void php_ion_serialize_struct(php_ion_serializer *ser, zend_array ION_CHECK(ion_writer_finish_container(ser->writer)); } -static inline void php_ion_serialize_list(php_ion_serializer *ser, zend_array *arr) +LOCAL void php_ion_serialize_list(php_ion_serializer *ser, zend_array *arr) { ION_CHECK(ion_writer_start_container(ser->writer, tid_LIST)); @@ -1359,7 +1598,7 @@ static inline void php_ion_serialize_list(php_ion_serializer *ser, zend_array *a ION_CHECK(ion_writer_finish_container(ser->writer)); } -static inline void php_ion_serialize_array(php_ion_serializer *ser, zend_array *arr) +LOCAL void php_ion_serialize_array(php_ion_serializer *ser, zend_array *arr) { if (zend_array_is_list(arr)) { php_ion_serialize_list(ser, arr); @@ -1368,7 +1607,7 @@ static inline void php_ion_serialize_array(php_ion_serializer *ser, zend_array * } } -static inline void php_ion_serialize_object_iface(php_ion_serializer *ser, zend_object *zobject) +LOCAL void php_ion_serialize_object_iface(php_ion_serializer *ser, zend_object *zobject) { uint8_t *buf; size_t len; @@ -1379,7 +1618,7 @@ static inline void php_ion_serialize_object_iface(php_ion_serializer *ser, zend_ ION_STRING is; ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_SERIALIZEABLE)); 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_assign_cstr(&is, (char *) buf, len))); + ION_CHECK(ion_writer_write_clob(ser->writer, buf, len)); efree(buf); } else if (!EG(exception)){ zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR, @@ -1387,7 +1626,7 @@ static inline void php_ion_serialize_object_iface(php_ion_serializer *ser, zend_ } } -static inline void php_ion_serialize_object_magic(php_ion_serializer *ser, zend_object *zobject, zend_function *fn) +LOCAL void php_ion_serialize_object_magic(php_ion_serializer *ser, zend_object *zobject, zend_function *fn) { zval rv; @@ -1409,7 +1648,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) +LOCAL void php_ion_serialize_object_enum(php_ion_serializer *ser, zend_object *zobject) { ION_STRING is; ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_ENUM)); @@ -1419,7 +1658,7 @@ static inline void php_ion_serialize_object_enum(php_ion_serializer *ser, zend_o ION_CHECK(ion_writer_write_symbol(ser->writer, ion_string_from_zend(&is, Z_STR_P(z_cname)))); } -static inline void php_ion_serialize_object_std(php_ion_serializer *ser, zend_object *zobject) +LOCAL void php_ion_serialize_object_std(php_ion_serializer *ser, zend_object *zobject) { ION_STRING is; @@ -1443,10 +1682,10 @@ static inline void php_ion_serialize_object_std(php_ion_serializer *ser, zend_ob } } -static inline void php_ion_serialize_object_lob(php_ion_serializer *ser, zend_object *zobject) +LOCAL void php_ion_serialize_object_lob(php_ion_serializer *ser, zend_object *zobject) { - zval tmp_type, *type = zend_read_property(NULL, zobject, ZEND_STRL("type"), 0, &tmp_type); - zval tmp_value, *value = zend_read_property(NULL, zobject, ZEND_STRL("value"), 0, &tmp_value); + zval tmp_type, *type = zend_read_property_ex(NULL, zobject, ZSTR_KNOWN(ZEND_STR_TYPE), 0, &tmp_type); + zval tmp_value, *value = zend_read_property_ex(NULL, zobject, ZSTR_KNOWN(ZEND_STR_VALUE), 0, &tmp_value); switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type)))) { case tid_BLOB_INT: ION_CHECK(ion_writer_write_blob(ser->writer, (BYTE *) Z_STRVAL_P(value), Z_STRLEN_P(value))); @@ -1461,25 +1700,26 @@ static inline void php_ion_serialize_object_lob(php_ion_serializer *ser, zend_ob } } -static inline bool can_call_magic_serialize(php_ion_serializer *ser, zend_class_entry *ce) +LOCAL bool can_call_magic_serialize(php_ion_serializer *ser, zend_class_entry *ce) { return ce->__serialize && ser->call_magic; } -static inline bool can_call_iface_serialize(php_ion_serializer *, zend_class_entry *ce) +LOCAL 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) +LOCAL 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; } -static inline void php_ion_serialize_object(php_ion_serializer *ser, zend_object *zobject) +LOCAL void php_ion_serialize_object(php_ion_serializer *ser, zend_object *zobject) { zend_function *fn; zend_class_entry *ce = zobject->ce; @@ -1515,7 +1755,7 @@ static inline void php_ion_serialize_object(php_ion_serializer *ser, zend_object } } -static inline bool php_ion_serialize_system_value(php_ion_serializer *ser, zval *zv) +LOCAL bool php_ion_serialize_system_value(php_ion_serializer *ser, zval *zv) { if (1 == php_ion_globals.serializer.level) { if (Z_TYPE_P(zv) == IS_OBJECT) { @@ -1529,21 +1769,18 @@ static inline bool php_ion_serialize_system_value(php_ion_serializer *ser, zval return false; } -static inline void php_ion_serialize_refcounted(php_ion_serializer *ser, zval *zv) +LOCAL bool php_ion_serialize_backref(php_ion_serializer *ser, zval *zv) { - if (php_ion_serialize_system_value(ser, zv)) { - return; + if (Z_TYPE_P(zv) == IS_STRING && Z_STR_P(zv) == zend_empty_string) { + return false; + } + if (Z_TYPE_P(zv) == IS_ARRAY && Z_ARR_P(zv) == &zend_empty_array) { + return false; } zend_ulong idx = (zend_ulong) (uintptr_t) Z_COUNTED_P(zv); - - ION_STRING is; - if (zend_hash_index_exists(ser->ids, idx)) { - zval *num = zend_hash_index_find(ser->ids, idx); - - ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_BACKREF)); - ION_CHECK(ion_writer_write_int64(ser->writer, Z_LVAL_P(num))); - } else { + zval *ref = zend_hash_index_find(ser->ids, idx); + if (!ref) { zval num; ZVAL_LONG(&num, zend_hash_num_elements(ser->ids)); @@ -1552,28 +1789,45 @@ static inline void php_ion_serialize_refcounted(php_ion_serializer *ser, zval *z Z_TRY_ADDREF_P(zv); zend_hash_next_index_insert(ser->tmp, zv); - switch (Z_TYPE_P(zv)) { - case IS_STRING: - ION_CHECK(ion_writer_write_string(ser->writer, ion_string_from_zend(&is, Z_STR_P(zv)))); - break; + return false; + } - case IS_ARRAY: - php_ion_serialize_array(ser, Z_ARRVAL_P(zv)); - break; + ION_CHECK_RETURN(true, ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_BACKREF)); + ION_CHECK_RETURN(true, ion_writer_write_int64(ser->writer, Z_LVAL_P(ref))); + return true; +} - case IS_OBJECT: - php_ion_serialize_object(ser, Z_OBJ_P(zv)); - break; +LOCAL void php_ion_serialize_refcounted(php_ion_serializer *ser, zval *zv) +{ + if (php_ion_serialize_system_value(ser, zv)) { + return; + } + if (php_ion_serialize_backref(ser, zv)) { + return; + } - case IS_REFERENCE: - ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_REFERENCE)); - php_ion_serialize_zval(ser, Z_REFVAL_P(zv)); - break; - } + ION_STRING is; + switch (Z_TYPE_P(zv)) { + case IS_STRING: + ION_CHECK(ion_writer_write_string(ser->writer, ion_string_from_zend(&is, Z_STR_P(zv)))); + break; + + case IS_ARRAY: + php_ion_serialize_array(ser, Z_ARRVAL_P(zv)); + break; + + case IS_OBJECT: + php_ion_serialize_object(ser, Z_OBJ_P(zv)); + break; + + case IS_REFERENCE: + ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_REFERENCE)); + php_ion_serialize_zval(ser, Z_REFVAL_P(zv)); + break; } } -static inline void php_ion_serialize_zval(php_ion_serializer *ser, zval *zv) +LOCAL void php_ion_serialize_zval(php_ion_serializer *ser, zval *zv) { OBJ_CHECK(ser); @@ -1605,9 +1859,11 @@ 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)); +#define php_ion_serializer_php_copy(o,n) +php_ion_decl(serializer_php, Serializer_PHP); +#define clone_ion_Serializer_PHP NULL -static inline void php_ion_serialize_ex(php_ion_serializer *ser, zval *zv) +LOCAL void php_ion_serialize_ex(php_ion_serializer *ser, zval *zv) { HashPosition pos; HashTable *arr = NULL; @@ -1657,7 +1913,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; } @@ -1687,24 +1943,26 @@ typedef struct php_ion_unserializer_php { zend_object *opt, std; } php_ion_unserializer_php; -static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php *ser_obj) +LOCAL void php_ion_unserializer_php_ctor(php_ion_unserializer_php *ser_obj) { php_ion_unserializer *global_ser = &php_ion_globals.unserializer; ser_obj->unserializer.ids = global_ser->ids; 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); } @@ -1713,34 +1971,34 @@ static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php *ser_o GC_DELREF(ser_obj->opt); } -static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php *obj) +LOCAL void php_ion_unserializer_php_dtor(php_ion_unserializer_php *obj) { if (obj->unserializer.call_custom) { zend_string_release(obj->unserializer.call_custom); } } -static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *return_value, ION_TYPE *typ); +LOCAL void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *return_value, ION_TYPE *typ); -static inline bool can_call_magic_unserialize(php_ion_unserializer *ser, zend_class_entry *ce) +LOCAL bool can_call_magic_unserialize(php_ion_unserializer *ser, zend_class_entry *ce) { return (ce && ce->__unserialize && ser->call_magic); } -static inline bool can_call_iface_unserialize(php_ion_unserializer *ser, zend_class_entry *ce) +LOCAL bool can_call_iface_unserialize(php_ion_unserializer *ser, zend_class_entry *ce) { return (ce && ce->unserialize); } -static inline bool can_call_custom_unserialize(php_ion_unserializer *ser, zend_object *zobject, zend_function **fn) +LOCAL 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; } -static inline zval *php_ion_unserialize_class(php_ion_unserializer *ser, zval *return_value) +LOCAL zval *php_ion_unserialize_class(php_ion_unserializer *ser, zval *return_value) { zend_class_entry *ce = zend_lookup_class(ser->annotations.object_class); @@ -1754,9 +2012,11 @@ static inline zval *php_ion_unserialize_class(php_ion_unserializer *ser, zval *r return NULL; } -static inline void php_ion_unserialize_object_enum(php_ion_unserializer *ser, zval *return_value) +LOCAL void php_ion_unserialize_object_enum(php_ion_unserializer *ser, zval *return_value) { zend_string *zs_case = zval_get_string(return_value); + zend_hash_next_index_insert(ser->tmp, return_value); + ZVAL_NULL(return_value); ION_CATCH(); zend_class_entry *ce = zend_lookup_class(ser->annotations.object_class); @@ -1775,18 +2035,23 @@ static inline void php_ion_unserialize_object_enum(php_ion_unserializer *ser, zv zend_string_release(zs_case); } -static inline void php_ion_unserialize_object_iface(php_ion_unserializer *ser, zval *return_value) +LOCAL void php_ion_unserialize_object_iface(php_ion_unserializer *ser, zval *return_value) { zend_class_entry *ce = zend_lookup_class(ser->annotations.object_class); if (can_call_iface_unserialize(ser, ce)) { zend_string *s = zval_get_string(return_value); + zend_hash_next_index_insert(ser->tmp, return_value); ZVAL_NULL(return_value); zval *backref = zend_hash_next_index_insert(ser->ids, return_value); if (SUCCESS == ce->unserialize(backref, ce, (BYTE *) s->val, s->len, NULL)) { RETVAL_ZVAL(backref, 0, 0); - } else if (!EG(exception)) { - zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR, - "Failed to unserialize class %s", ce->name->val); + } else { + zval_ptr_dtor(backref); + ZVAL_NULL(backref); + if (!EG(exception)) { + zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR, + "Failed to unserialize class %s", ce->name->val); + } } zend_string_release(s); } else { @@ -1795,25 +2060,47 @@ 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) +LOCAL void php_ion_unserialize_field_name_ex(php_ion_unserializer *ser, ION_STRING *name, SID *sid) { - // FIXME: symbol table - ION_STRING name; - ION_CHECK(ion_reader_get_field_name(ser->reader, &name)); - if (!name.length) { + 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; + ION_STRING_ASSIGN(name, &is_ptr->value); + } else { + *sid = is_ptr->sid; } } - *key = zend_string_from_ion(&name); +} + +LOCAL void php_ion_unserialize_field_name(php_ion_unserializer *ser, zend_string **key, bool is_prop) +{ + // FIXME: symbol table + ION_STRING name; + SID sid = UNKNOWN_SID; + char buf[MAX_LENGTH_OF_LONG + 1 + 1] = {0}, *end = buf + sizeof(buf) - 1, *ptr; + + php_ion_unserialize_field_name_ex(ser, &name, &sid); + ION_CATCH(); + + switch (name.length) { + case 0: + ptr = zend_print_long_to_buf(end, sid); + *--ptr = '$'; + *key = zend_string_init(ptr, end - ptr, 0); + break; + case 1: + *key = ZSTR_CHAR(*name.value); + break; + default: + if (is_prop) { + *key = zend_string_init_interned((char *) name.value, name.length, 0); + } else { + *key = zend_string_from_ion(&name); + } + break; + } } static void php_ion_unserialize_props(php_ion_unserializer *ser, zval *return_value) @@ -1831,7 +2118,7 @@ static void php_ion_unserialize_props(php_ion_unserializer *ser, zval *return_va } zend_string *key; - php_ion_unserialize_field_name(ser, &key); + php_ion_unserialize_field_name(ser, &key, true); ION_CATCH(); zval zvalue; @@ -1857,7 +2144,7 @@ static void php_ion_unserialize_props(php_ion_unserializer *ser, zval *return_va * (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) +LOCAL void php_ion_unserialize_hash(php_ion_unserializer *ser, zval *return_value) { zend_hash_next_index_insert(ser->ids, return_value); @@ -1872,7 +2159,7 @@ static inline void php_ion_unserialize_hash(php_ion_unserializer *ser, zval *ret } zend_string *key; - php_ion_unserialize_field_name(ser, &key); + php_ion_unserialize_field_name(ser, &key, false); ION_CATCH(); zval zvalue; @@ -1898,7 +2185,7 @@ static inline void php_ion_unserialize_hash(php_ion_unserializer *ser, zval *ret ION_CHECK(ion_reader_step_out(ser->reader)); } -static inline void verify_unserializer(php_ion_unserializer *ser, zend_object *zobject, zend_function **fn) +LOCAL void verify_unserializer(php_ion_unserializer *ser, zend_object *zobject, zend_function **fn) { switch (ser->annotations.object_type) { case 'c': @@ -1926,11 +2213,11 @@ static inline void verify_unserializer(php_ion_unserializer *ser, zend_object *z } } -static inline void php_ion_unserialize_object(php_ion_unserializer *ser, zval *return_value) +LOCAL void php_ion_unserialize_object(php_ion_unserializer *ser, zval *return_value) { // backup possible backref to array returned by magic/custom __serialize() zval *input = zend_hash_next_index_insert(ser->tmp, return_value); - + ZVAL_NULL(return_value); php_ion_unserialize_class(ser, return_value); ION_CATCH(); @@ -1959,7 +2246,7 @@ static inline void php_ion_unserialize_object(php_ion_unserializer *ser, zval *r zval_ptr_dtor(&rv); } -static inline void php_ion_unserialize_struct(php_ion_unserializer *ser, zval *return_value) +LOCAL void php_ion_unserialize_struct(php_ion_unserializer *ser, zval *return_value) { if (ser->annotations.object_class) { switch (ser->annotations.object_type) { @@ -1992,7 +2279,7 @@ static inline void php_ion_unserialize_struct(php_ion_unserializer *ser, zval *r } } -static inline void php_ion_unserialize_list(php_ion_unserializer *ser, zval *return_value) +LOCAL void php_ion_unserialize_list(php_ion_unserializer *ser, zval *return_value) { ION_CHECK(ion_reader_step_in(ser->reader)); array_init(return_value); @@ -2016,12 +2303,12 @@ static inline void php_ion_unserialize_list(php_ion_unserializer *ser, zval *ret ION_CHECK(ion_reader_step_out(ser->reader)); } -static inline void php_ion_reader_read_lob(ION_READER *reader, zval *return_value) +LOCAL 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; @@ -2034,7 +2321,7 @@ again: RETURN_STR(zstr); } -static inline void php_ion_reader_read_timestamp(ION_READER *reader, ION_READER_OPTIONS *opt, zval *return_value) +LOCAL void php_ion_reader_read_timestamp(ION_READER *reader, ION_READER_OPTIONS *opt, zval *return_value) { ION_TIMESTAMP ts; ION_CHECK(ion_reader_read_timestamp(reader, &ts)); @@ -2051,7 +2338,7 @@ static inline void php_ion_reader_read_timestamp(ION_READER *reader, ION_READER_ OBJ_CHECK(ts_obj); } -static inline void php_ion_reader_read_int(ION_READER *reader, zval *return_value) +LOCAL void php_ion_reader_read_int(ION_READER *reader, zval *return_value) { ION_INT *num = NULL; ION_CHECK(ion_int_alloc(reader, &num)); @@ -2065,7 +2352,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); @@ -2082,7 +2369,7 @@ static inline void php_ion_reader_read_int(ION_READER *reader, zval *return_valu } } -static inline void php_ion_unserialize_backref(php_ion_unserializer *ser, zval *return_value) +LOCAL void php_ion_unserialize_backref(php_ion_unserializer *ser, zval *return_value) { zval *backref = zend_hash_index_find(ser->ids, Z_LVAL_P(return_value)); @@ -2091,11 +2378,11 @@ 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)); } } -static inline void php_ion_unserialize_annotations(php_ion_unserializer *ser) +LOCAL void php_ion_unserialize_annotations(php_ion_unserializer *ser) { memset(&ser->annotations, 0, sizeof(ser->annotations)); @@ -2186,10 +2473,10 @@ 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) +LOCAL 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)); @@ -2217,7 +2504,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); @@ -2239,7 +2526,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); @@ -2257,23 +2544,21 @@ 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); if (ser->annotations.object_type) { - zend_hash_next_index_insert(ser->tmp, return_value); goto unserialize_struct; } 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); if (ser->annotations.object_type) { - zend_hash_next_index_insert(ser->tmp, return_value); goto unserialize_struct; } zend_hash_next_index_insert(ser->ids, return_value); @@ -2283,7 +2568,6 @@ static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *ret case tid_BLOB_INT: php_ion_reader_read_lob(ser->reader, return_value); if (ser->annotations.object_type) { - zend_hash_next_index_insert(ser->tmp, return_value); goto unserialize_struct; } zend_hash_next_index_insert(ser->ids, return_value); @@ -2313,9 +2597,11 @@ unserialize_struct: ; } } -php_ion_decl(unserializer_php, Unserializer_PHP, php_ion_unserializer_php_dtor(obj)); +#define php_ion_unserializer_php_copy(o,n) +php_ion_decl(unserializer_php, Unserializer_PHP); +#define clone_ion_Unserializer_PHP NULL -static inline void php_ion_unserialize_ex(php_ion_unserializer *ser, zval *return_value) +LOCAL void php_ion_unserialize_ex(php_ion_unserializer *ser, zval *return_value) { if (ser->multi_seq) { array_init(return_value); @@ -2367,7 +2653,8 @@ void php_ion_unserialize(php_ion_unserializer *ser, zval *zdata, zval *return_va 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"); + "Invalid source to unserialize: expected string or resource, got %s", + zend_zval_type_name(zdata)); if (zo_ser) { OBJ_RELEASE(zo_ser); } @@ -2375,7 +2662,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; }