1
0
mirror of https://github.com/deuill/go-php.git synced 2024-09-21 00:40:45 +00:00

Return values as copies when converting

Previously, value conversions were made on the original value, and thus,
calling methods like `Value.Int` had the side-effect of changing the
value's type for subsequent calls to `Value.Interface`. Any conversion
required happens on value copies, which are automatically destroyed
after use.
This commit is contained in:
Alex Palaistras 2015-10-18 01:13:28 +01:00
parent 2381ebcc6c
commit 081d7a133f
3 changed files with 54 additions and 22 deletions

View File

@ -93,43 +93,67 @@ void value_object_add_property(engine_value *obj, const char *key, engine_value
}
int value_get_long(engine_value *val) {
// Covert value to long if needed.
if (Z_TYPE_P(val->value) != IS_LONG) {
convert_to_long(val->value);
val->kind = Z_TYPE_P(val->value);
// Return value directly if already in correct type.
if (val->kind == IS_LONG) {
return Z_LVAL_P(val->value);
}
return Z_LVAL_P(val->value);
zval *tmp = value_copy(val->value);
convert_to_long(tmp);
long v = Z_LVAL_P(tmp);
zval_dtor(tmp);
return v;
}
double value_get_double(engine_value *val) {
// Covert value to double if needed.
if (Z_TYPE_P(val->value) != IS_DOUBLE) {
convert_to_double(val->value);
val->kind = Z_TYPE_P(val->value);
// Return value directly if already in correct type.
if (val->kind == IS_DOUBLE) {
return Z_DVAL_P(val->value);
}
return Z_DVAL_P(val->value);
zval *tmp = value_copy(val->value);
convert_to_double(tmp);
double v = Z_DVAL_P(tmp);
zval_dtor(tmp);
return v;
}
bool value_get_bool(engine_value *val) {
// Covert value to long if needed.
if (Z_TYPE_P(val->value) != IS_BOOL) {
convert_to_boolean(val->value);
val->kind = Z_TYPE_P(val->value);
// Return value directly if already in correct type.
if (val->kind == IS_BOOL) {
return Z_BVAL_P(val->value);
}
return Z_BVAL_P(val->value);
zval *tmp = value_copy(val->value);
convert_to_boolean(tmp);
bool v = Z_BVAL_P(tmp);
zval_dtor(tmp);
return v;
}
char *value_get_string(engine_value *val) {
// Covert value to string if needed.
if (Z_TYPE_P(val->value) != IS_STRING) {
convert_to_cstring(val->value);
val->kind = Z_TYPE_P(val->value);
// Return value directly if already in correct type.
if (val->kind == IS_STRING) {
return Z_STRVAL_P(val->value);
}
return Z_STRVAL_P(val->value);
zval *tmp = value_copy(val->value);
convert_to_cstring(tmp);
char *v = Z_STRVAL_P(tmp);
zval_dtor(tmp);
return v;
}
void value_destroy(engine_value *val) {

View File

@ -55,8 +55,6 @@ func (v *Value) Interface() interface{} {
return v.Float()
case Bool:
return v.Bool()
// case Array:
// return v.Map()
case String:
return v.String()
}

View File

@ -10,6 +10,16 @@ typedef struct _engine_value {
int kind;
} engine_value;
static inline zval *value_copy(zval *zv) {
zval *tmp;
ALLOC_ZVAL(tmp);
INIT_PZVAL_COPY(tmp, zv);
zval_copy_ctor(tmp);
return tmp;
}
engine_value *value_new(zval *zv);
int value_kind(engine_value *val);
void value_destroy(engine_value *val);