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:
parent
2381ebcc6c
commit
081d7a133f
@ -93,43 +93,67 @@ void value_object_add_property(engine_value *obj, const char *key, engine_value
|
|||||||
}
|
}
|
||||||
|
|
||||||
int value_get_long(engine_value *val) {
|
int value_get_long(engine_value *val) {
|
||||||
// Covert value to long if needed.
|
// Return value directly if already in correct type.
|
||||||
if (Z_TYPE_P(val->value) != IS_LONG) {
|
if (val->kind == IS_LONG) {
|
||||||
convert_to_long(val->value);
|
return Z_LVAL_P(val->value);
|
||||||
val->kind = Z_TYPE_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) {
|
double value_get_double(engine_value *val) {
|
||||||
// Covert value to double if needed.
|
// Return value directly if already in correct type.
|
||||||
if (Z_TYPE_P(val->value) != IS_DOUBLE) {
|
if (val->kind == IS_DOUBLE) {
|
||||||
convert_to_double(val->value);
|
return Z_DVAL_P(val->value);
|
||||||
val->kind = Z_TYPE_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) {
|
bool value_get_bool(engine_value *val) {
|
||||||
// Covert value to long if needed.
|
// Return value directly if already in correct type.
|
||||||
if (Z_TYPE_P(val->value) != IS_BOOL) {
|
if (val->kind == IS_BOOL) {
|
||||||
convert_to_boolean(val->value);
|
return Z_BVAL_P(val->value);
|
||||||
val->kind = Z_TYPE_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) {
|
char *value_get_string(engine_value *val) {
|
||||||
// Covert value to string if needed.
|
// Return value directly if already in correct type.
|
||||||
if (Z_TYPE_P(val->value) != IS_STRING) {
|
if (val->kind == IS_STRING) {
|
||||||
convert_to_cstring(val->value);
|
return Z_STRVAL_P(val->value);
|
||||||
val->kind = Z_TYPE_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) {
|
void value_destroy(engine_value *val) {
|
||||||
|
@ -55,8 +55,6 @@ func (v *Value) Interface() interface{} {
|
|||||||
return v.Float()
|
return v.Float()
|
||||||
case Bool:
|
case Bool:
|
||||||
return v.Bool()
|
return v.Bool()
|
||||||
// case Array:
|
|
||||||
// return v.Map()
|
|
||||||
case String:
|
case String:
|
||||||
return v.String()
|
return v.String()
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,16 @@ typedef struct _engine_value {
|
|||||||
int kind;
|
int kind;
|
||||||
} engine_value;
|
} 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);
|
engine_value *value_new(zval *zv);
|
||||||
int value_kind(engine_value *val);
|
int value_kind(engine_value *val);
|
||||||
void value_destroy(engine_value *val);
|
void value_destroy(engine_value *val);
|
||||||
|
Loading…
Reference in New Issue
Block a user