use already available ion_string_assign_cstr API
[awesomized/ext-ion] / ion.c
diff --git a/ion.c b/ion.c
index 192fb830d510ae42906d38f8996b59617c92cf75..3e8ba015131f2feaea946b2820ba7d59cf609849 100644 (file)
--- a/ion.c
+++ b/ion.c
 #define DECNUMDIGITS 34 /* DECQUAD_Pmax */
 #include "ionc/ion.h"
 
+static decContext g_dec_ctx;
+static ION_INT *g_ion_int_zend_max, *g_ion_int_zend_min;
+static ION_DECIMAL g_ion_dec_zend_max, g_ion_dec_zend_min;
+
 #include "php_ion.h"
 #define ZEND_ARG_VARIADIC_OBJ_TYPE_MASK(pass_by_ref, name, classname, type_mask, default_value) \
        { #name, ZEND_TYPE_INIT_CLASS_CONST_MASK(#classname, type_mask | _ZEND_ARG_INFO_FLAGS(pass_by_ref, 1, 0)), default_value },
@@ -93,7 +97,7 @@ ZEND_METHOD(ion_Symbol, __toString)
        if (!sym->value) {
                RETURN_EMPTY_STRING();
        }
-       RETURN_STR(sym->value);
+       RETURN_STR_COPY(sym->value);
 }
 ZEND_METHOD(ion_Timestamp, __construct)
 {
@@ -225,6 +229,22 @@ ZEND_METHOD(ion_Decimal, isInt)
 
        RETURN_BOOL(ion_decimal_is_integer(&obj->dec));
 }
+ZEND_METHOD(ion_LOB, __construct)
+{
+       zend_string *value;
+       zend_object *type = NULL;
+       ZEND_PARSE_PARAMETERS_START(1, 2)
+               Z_PARAM_STR(value)
+               Z_PARAM_OPTIONAL
+               Z_PARAM_OBJ_OF_CLASS(type, ce_Type)
+       ZEND_PARSE_PARAMETERS_END();
+
+       if (!type) {
+               type = zend_enum_get_case_cstr(ce_Type, "CLob");
+       }
+       update_property_obj(Z_OBJ_P(ZEND_THIS), ZEND_STRL("type"), type);
+       zend_update_property_str(Z_OBJCE_P(ZEND_THIS), Z_OBJ_P(ZEND_THIS), ZEND_STRL("value"), value);
+}
 ZEND_METHOD(ion_Reader_Options, __construct)
 {
        php_ion_reader_options *opt = php_ion_obj(reader_options, Z_OBJ_P(ZEND_THIS));
@@ -1431,6 +1451,18 @@ PHP_RSHUTDOWN_FUNCTION(ion)
 
 PHP_MINIT_FUNCTION(ion)
 {
+       if (!g_ion_int_zend_max) {
+               decContextDefault(&g_dec_ctx, DEC_INIT_DECIMAL64);
+
+               ion_int_alloc(NULL, &g_ion_int_zend_max);
+               ion_int_from_long(g_ion_int_zend_max, ZEND_LONG_MAX);
+               ion_decimal_from_ion_int(&g_ion_dec_zend_max, &g_dec_ctx, g_ion_int_zend_max);
+
+               ion_int_alloc(NULL, &g_ion_int_zend_min);
+               ion_int_from_long(g_ion_int_zend_min, ZEND_LONG_MIN);
+               ion_decimal_from_ion_int(&g_ion_dec_zend_min, &g_dec_ctx, g_ion_int_zend_min);
+       }
+
        ce_Annotation = register_class_ion_Annotation();
 
        php_ion_register(type, Type);
@@ -1442,6 +1474,7 @@ PHP_MINIT_FUNCTION(ion)
        ce_Symbol_System_SID = register_class_ion_Symbol_System_SID();
 
        ce_Collection = register_class_ion_Collection();
+       ce_LOB = register_class_ion_LOB();
 
        php_ion_register(decimal, Decimal);
        php_ion_register(decimal_ctx, Decimal_Context);
@@ -1477,6 +1510,14 @@ PHP_MINIT_FUNCTION(ion)
        return SUCCESS;
 }
 
+PHP_MSHUTDOWN_FUNCTION(ion)
+{
+       if (g_ion_int_zend_max) {
+               ion_int_free(g_ion_int_zend_max);
+               ion_int_free(g_ion_int_zend_min);
+       }
+       return SUCCESS;
+}
 PHP_MINFO_FUNCTION(ion)
 {
        php_info_print_table_start();
@@ -1504,7 +1545,7 @@ zend_module_entry ion_module_entry = {
        "ion",                                  /* Extension name */
        ext_functions,                  /* zend_function_entry */
        PHP_MINIT(ion),                 /* PHP_MINIT - Module initialization */
-       NULL,                                   /* PHP_MSHUTDOWN - Module shutdown */
+       PHP_MSHUTDOWN(ion),             /* PHP_MSHUTDOWN - Module shutdown */
        PHP_RINIT(ion),                 /* PHP_RINIT - Request initialization */
        PHP_RSHUTDOWN(ion),             /* PHP_RSHUTDOWN - Request shutdown */
        PHP_MINFO(ion),                 /* PHP_MINFO - Module info */