Commit 2442c585 authored by twanvl's avatar twanvl

Fixed conversion bug in position_of function;

to_real no longer needs to throw exceptions when conversion fails
parent 40291a4d
...@@ -225,7 +225,7 @@ int Set::positionOfCard(const CardP& card, const ScriptValueP& order_by, const S ...@@ -225,7 +225,7 @@ int Set::positionOfCard(const CardP& card, const ScriptValueP& order_by, const S
Context& ctx = getContext(c); Context& ctx = getContext(c);
values.push_back(*order_by->eval(ctx)); values.push_back(*order_by->eval(ctx));
if (filter) { if (filter) {
keep.push_back(*filter->eval(ctx)); keep.push_back((bool)*filter->eval(ctx));
} }
} }
// 3. initialize order cache // 3. initialize order cache
......
...@@ -110,7 +110,7 @@ SCRIPT_FUNCTION(to_int) { ...@@ -110,7 +110,7 @@ SCRIPT_FUNCTION(to_int) {
} else if (str.empty()) { } else if (str.empty()) {
result = 0; result = 0;
} else { } else {
return new_intrusive1<ScriptDelayedError>(_ERROR_3_("can't convert value", input->toString(), input->typeName(), _TYPE_("integer"))); return delayError(_ERROR_3_("can't convert value", str, input->typeName(), _TYPE_("integer")));
} }
} else { } else {
result = (int)*input; result = (int)*input;
...@@ -122,9 +122,26 @@ SCRIPT_FUNCTION(to_int) { ...@@ -122,9 +122,26 @@ SCRIPT_FUNCTION(to_int) {
} }
SCRIPT_FUNCTION(to_real) { SCRIPT_FUNCTION(to_real) {
ScriptValueP input = ctx.getVariable(SCRIPT_VAR_input);
ScriptType t = input->type();
try { try {
SCRIPT_PARAM_C(double, input); double result;
SCRIPT_RETURN(input); if (t == SCRIPT_BOOL) {
result = (bool)*input ? 1.0 : 0.0;
} else if (t == SCRIPT_COLOR) {
AColor c = (AColor)*input;
result = (c.Red() + c.Blue() + c.Green()) / 3.0;
} else if (t == SCRIPT_STRING) {
String str = input->toString();
if (str.empty()) {
result = 0.0;
} else if (!str.ToDouble(&result)) {
return delayError(_ERROR_3_("can't convert value", str, input->typeName(), _TYPE_("double")));
}
} else {
result = (double)*input;
}
SCRIPT_RETURN(result);
} catch (const ScriptError& e) { } catch (const ScriptError& e) {
return new_intrusive1<ScriptDelayedError>(e); return new_intrusive1<ScriptDelayedError>(e);
} }
...@@ -133,27 +150,31 @@ SCRIPT_FUNCTION(to_real) { ...@@ -133,27 +150,31 @@ SCRIPT_FUNCTION(to_real) {
SCRIPT_FUNCTION(to_number) { SCRIPT_FUNCTION(to_number) {
ScriptValueP input = ctx.getVariable(SCRIPT_VAR_input); ScriptValueP input = ctx.getVariable(SCRIPT_VAR_input);
ScriptType t = input->type(); ScriptType t = input->type();
if (t == SCRIPT_BOOL) { try {
SCRIPT_RETURN((bool)*input ? 1 : 0); if (t == SCRIPT_BOOL) {
} else if (t == SCRIPT_COLOR) { SCRIPT_RETURN((bool)*input ? 1 : 0);
AColor c = (AColor)*input; } else if (t == SCRIPT_COLOR) {
SCRIPT_RETURN( (c.Red() + c.Blue() + c.Green()) / 3 ); AColor c = (AColor)*input;
} else if (t == SCRIPT_DOUBLE) { SCRIPT_RETURN( (c.Red() + c.Blue() + c.Green()) / 3 );
SCRIPT_RETURN((double)*input); } else if (t == SCRIPT_DOUBLE) {
} else if (t == SCRIPT_NIL) { SCRIPT_RETURN((double)*input);
SCRIPT_RETURN(0); } else if (t == SCRIPT_NIL) {
} else {
String s = input->toString();
long l; double d;
if (s.ToLong(&l)) {
SCRIPT_RETURN((int)l);
} else if (s.ToDouble(&d)) {
SCRIPT_RETURN((double)d);
} else if (s.empty()) {
SCRIPT_RETURN(0); SCRIPT_RETURN(0);
} else { } else {
return delayError(_ERROR_2_("can't convert", input->typeName(), _TYPE_("double"))); String str = input->toString();
long l; double d;
if (str.ToLong(&l)) {
SCRIPT_RETURN((int)l);
} else if (str.ToDouble(&d)) {
SCRIPT_RETURN((double)d);
} else if (str.empty()) {
SCRIPT_RETURN(0);
} else {
return delayError(_ERROR_3_("can't convert value", str, input->typeName(), _TYPE_("double")));
}
} }
} catch (const ScriptError& e) {
return new_intrusive1<ScriptDelayedError>(e);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment