Commit 4017a911 authored by twanvl's avatar twanvl

Use toXXX() instead of operator XXX in ScriptValue.

This means that we are more explicit about type conversions.
parent a754df74
...@@ -680,7 +680,7 @@ bool KeywordDatabase::tryExpand(const Keyword& kw, ...@@ -680,7 +680,7 @@ bool KeywordDatabase::tryExpand(const Keyword& kw,
ctx.setVariable(_("used_placeholders"), to_script(used_placeholders)); ctx.setVariable(_("used_placeholders"), to_script(used_placeholders));
// Final check whether the keyword matches // Final check whether the keyword matches
if (match_condition && (bool)*match_condition->eval(ctx) == false) { if (match_condition && match_condition->eval(ctx)->toBool() == false) {
return false; return false;
} }
...@@ -688,7 +688,7 @@ bool KeywordDatabase::tryExpand(const Keyword& kw, ...@@ -688,7 +688,7 @@ bool KeywordDatabase::tryExpand(const Keyword& kw,
bool expand = expand_type == _('1'); bool expand = expand_type == _('1');
if (!expand && expand_type != _('0')) { if (!expand && expand_type != _('0')) {
// default expand, determined by script // default expand, determined by script
expand = expand_default ? (bool)*expand_default->eval(ctx) : true; expand = expand_default ? expand_default->eval(ctx)->toBool() : true;
expand_type = expand ? _('A') : _('a'); expand_type = expand ? _('A') : _('a');
} }
...@@ -726,7 +726,7 @@ bool KeywordDatabase::tryExpand(const Keyword& kw, ...@@ -726,7 +726,7 @@ bool KeywordDatabase::tryExpand(const Keyword& kw,
ScriptType KeywordParamValue::type() const { return SCRIPT_STRING; } ScriptType KeywordParamValue::type() const { return SCRIPT_STRING; }
String KeywordParamValue::typeName() const { return _("keyword parameter"); } String KeywordParamValue::typeName() const { return _("keyword parameter"); }
KeywordParamValue::operator String() const { String KeywordParamValue::toString() const {
String safe_type = replace_all(replace_all(replace_all(type_name, String safe_type = replace_all(replace_all(replace_all(type_name,
_("("),_("-")), _("("),_("-")),
_(")"),_("-")), _(")"),_("-")),
...@@ -734,11 +734,11 @@ KeywordParamValue::operator String() const { ...@@ -734,11 +734,11 @@ KeywordParamValue::operator String() const {
return _("<param-") + safe_type + _(">") + value + _("</param-") + safe_type + _(">"); return _("<param-") + safe_type + _(">") + value + _("</param-") + safe_type + _(">");
} }
KeywordParamValue::operator int() const { return *to_script(value); } // a bit of a hack int KeywordParamValue::toInt() const { return to_script(value)->toInt(); } // a bit of a hack
KeywordParamValue::operator double() const { return *to_script(value); } double KeywordParamValue::toDouble() const { return to_script(value)->toDouble(); }
KeywordParamValue::operator bool() const { return *to_script(value); } bool KeywordParamValue::toBool() const { return to_script(value)->toBool(); }
KeywordParamValue::operator AColor() const { return *to_script(value); } AColor KeywordParamValue::toColor() const { return to_script(value)->toColor(); }
int KeywordParamValue::itemCount() const { return to_script(value)->itemCount(); } int KeywordParamValue::itemCount() const { return to_script(value)->itemCount(); }
ScriptValueP KeywordParamValue::getMember(const String& name) const { ScriptValueP KeywordParamValue::getMember(const String& name) const {
if (name == _("type")) return to_script(type_name); if (name == _("type")) return to_script(type_name);
......
...@@ -193,11 +193,11 @@ class KeywordParamValue : public ScriptValue { ...@@ -193,11 +193,11 @@ class KeywordParamValue : public ScriptValue {
virtual ScriptType type() const; virtual ScriptType type() const;
virtual String typeName() const; virtual String typeName() const;
virtual operator String() const; virtual String toString() const;
virtual operator int() const; virtual int toInt() const;
virtual operator bool() const; virtual bool toBool() const;
virtual operator double() const; virtual double toDouble() const;
virtual operator AColor() const; virtual AColor toColor() const;
virtual int itemCount() const; virtual int itemCount() const;
virtual ScriptValueP getMember(const String& name) const; virtual ScriptValueP getMember(const String& name) const;
}; };
......
...@@ -116,7 +116,7 @@ PackInstance::PackInstance(const PackType& pack_type, PackGenerator& parent) ...@@ -116,7 +116,7 @@ PackInstance::PackInstance(const PackType& pack_type, PackGenerator& parent)
if (pack_type.filter) { if (pack_type.filter) {
FOR_EACH(card, parent.set->cards) { FOR_EACH(card, parent.set->cards) {
Context& ctx = parent.set->getContext(card); Context& ctx = parent.set->getContext(card);
bool keep = *pack_type.filter.invoke(ctx); bool keep = pack_type.filter.invoke(ctx)->toBool();
if (keep) { if (keep) {
cards.push_back(card); cards.push_back(card);
} }
......
...@@ -270,9 +270,9 @@ int Set::positionOfCard(const CardP& card, const ScriptValueP& order_by, const S ...@@ -270,9 +270,9 @@ int Set::positionOfCard(const CardP& card, const ScriptValueP& order_by, const S
vector<int> keep; if(filter) keep.reserve(cards.size()); vector<int> keep; if(filter) keep.reserve(cards.size());
FOR_EACH_CONST(c, cards) { FOR_EACH_CONST(c, cards) {
Context& ctx = getContext(c); Context& ctx = getContext(c);
values.push_back(*order_by->eval(ctx)); values.push_back(order_by->eval(ctx)->toString());
if (filter) { if (filter) {
keep.push_back((bool)*filter->eval(ctx)); keep.push_back(filter->eval(ctx)->toBool());
} }
} }
#if USE_SCRIPT_PROFILING #if USE_SCRIPT_PROFILING
...@@ -292,7 +292,7 @@ int Set::numberOfCards(const ScriptValueP& filter) { ...@@ -292,7 +292,7 @@ int Set::numberOfCards(const ScriptValueP& filter) {
} else { } else {
int n = 0; int n = 0;
FOR_EACH_CONST(c, cards) { FOR_EACH_CONST(c, cards) {
if (*filter->eval(getContext(c))) ++n; if (filter->eval(getContext(c))->toBool()) ++n;
} }
filter_cache.insert(make_pair(filter,n)); filter_cache.insert(make_pair(filter,n));
return n; return n;
......
...@@ -324,7 +324,7 @@ int CardListBase::OnGetItemImage(long pos) const { ...@@ -324,7 +324,7 @@ int CardListBase::OnGetItemImage(long pos) const {
wxListItemAttr* CardListBase::OnGetItemAttr(long pos) const { wxListItemAttr* CardListBase::OnGetItemAttr(long pos) const {
if (!set->game->card_list_color_script) return nullptr; if (!set->game->card_list_color_script) return nullptr;
Context& ctx = set->getContext(getCard(pos)); Context& ctx = set->getContext(getCard(pos));
item_attr.SetTextColour(*set->game->card_list_color_script.invoke(ctx)); item_attr.SetTextColour(set->game->card_list_color_script.invoke(ctx)->toColor());
return &item_attr; return &item_attr;
} }
......
...@@ -83,7 +83,7 @@ ScriptValueP export_set(SetP const& set, vector<CardP> const& cards, ExportTempl ...@@ -83,7 +83,7 @@ ScriptValueP export_set(SetP const& set, vector<CardP> const& cards, ExportTempl
// write as string // write as string
wxFileOutputStream file(outname); wxFileOutputStream file(outname);
wxTextOutputStream stream(file); wxTextOutputStream stream(file);
stream.WriteString(*result); stream.WriteString(result->toString());
} }
return result; return result;
} }
......
...@@ -412,7 +412,7 @@ void ConsolePanel::exec(String const& command) { ...@@ -412,7 +412,7 @@ void ConsolePanel::exec(String const& command) {
message->bitmap = wxBitmap(image); message->bitmap = wxBitmap(image);
} else if (type == SCRIPT_COLOR) { } else if (type == SCRIPT_COLOR) {
message->text = result->toCode(); message->text = result->toCode();
AColor color = (AColor)*result; AColor color = result->toColor();
wxImage image(30,20); wxImage image(30,20);
fill_image(image,color); fill_image(image,color);
set_alpha(image, color.alpha / 255.0); set_alpha(image, color.alpha / 255.0);
......
...@@ -255,7 +255,7 @@ size_t DropDownChoiceList::selection() const { ...@@ -255,7 +255,7 @@ size_t DropDownChoiceList::selection() const {
return 0; return 0;
} else { } else {
// run default script to find out what the default choice would be // run default script to find out what the default choice would be
String default_choice = *field().default_script.invoke( cve.viewer.getContext() ); String default_choice = field().default_script.invoke( cve.viewer.getContext() )->toString();
default_id = group->choiceId(default_choice); default_id = group->choiceId(default_choice);
} }
} }
......
...@@ -109,7 +109,7 @@ size_t DropDownColorList::selection() const { ...@@ -109,7 +109,7 @@ size_t DropDownColorList::selection() const {
return 0; return 0;
} else if (hasDefault()) { } else if (hasDefault()) {
// evaluate script to find default color // evaluate script to find default color
default_color = *field().default_script.invoke(cve.viewer.getContext()); default_color = field().default_script.invoke(cve.viewer.getContext())->toColor();
} }
return selection; return selection;
} }
......
...@@ -67,7 +67,7 @@ ScriptValueP Context::eval(const Script& script, bool useScope) { ...@@ -67,7 +67,7 @@ ScriptValueP Context::eval(const Script& script, bool useScope) {
} }
// Conditional jump // Conditional jump
case I_JUMP_IF_NOT: { case I_JUMP_IF_NOT: {
bool condition = *stack.back(); bool condition = stack.back()->toBool();
stack.pop_back(); stack.pop_back();
if (!condition) { if (!condition) {
instr = &script.instructions[0] + i.data; instr = &script.instructions[0] + i.data;
...@@ -76,7 +76,7 @@ ScriptValueP Context::eval(const Script& script, bool useScope) { ...@@ -76,7 +76,7 @@ ScriptValueP Context::eval(const Script& script, bool useScope) {
} }
// Short-circuiting and/or = conditional jump without pop // Short-circuiting and/or = conditional jump without pop
case I_JUMP_SC_AND: { case I_JUMP_SC_AND: {
bool condition = *stack.back(); bool condition = stack.back()->toBool();
if (!condition) { if (!condition) {
instr = &script.instructions[0] + i.data; instr = &script.instructions[0] + i.data;
} else { } else {
...@@ -85,7 +85,7 @@ ScriptValueP Context::eval(const Script& script, bool useScope) { ...@@ -85,7 +85,7 @@ ScriptValueP Context::eval(const Script& script, bool useScope) {
break; break;
} }
case I_JUMP_SC_OR: { case I_JUMP_SC_OR: {
bool condition = *stack.back(); bool condition = stack.back()->toBool();
if (condition) { if (condition) {
instr = &script.instructions[0] + i.data; instr = &script.instructions[0] + i.data;
} else { } else {
...@@ -109,7 +109,7 @@ ScriptValueP Context::eval(const Script& script, bool useScope) { ...@@ -109,7 +109,7 @@ ScriptValueP Context::eval(const Script& script, bool useScope) {
// Get an object member // Get an object member
case I_MEMBER_C: { case I_MEMBER_C: {
stack.back() = stack.back()->getMember(*script.constants[i.data]); stack.back() = stack.back()->getMember(script.constants[i.data]->toString());
break; break;
} }
// Loop over a container, push next value or jump // Loop over a container, push next value or jump
...@@ -367,13 +367,13 @@ void instrUnary (UnaryInstructionType i, ScriptValueP& a) { ...@@ -367,13 +367,13 @@ void instrUnary (UnaryInstructionType i, ScriptValueP& a) {
case I_NEGATE: { case I_NEGATE: {
ScriptType at = a->type(); ScriptType at = a->type();
if (at == SCRIPT_DOUBLE) { if (at == SCRIPT_DOUBLE) {
a = to_script(-(double)*a); a = to_script(-a->toDouble());
} else { } else {
a = to_script(-(int)*a); a = to_script(-a->toInt());
} }
break; break;
} case I_NOT: } case I_NOT:
a = to_script(!(bool)*a); a = to_script(!a->toBool());
break; break;
} }
} }
...@@ -424,41 +424,41 @@ class ScriptCompose : public ScriptValue { ...@@ -424,41 +424,41 @@ class ScriptCompose : public ScriptValue {
// ----------------------------------------------------------------------------- : Simple instructions : binary // ----------------------------------------------------------------------------- : Simple instructions : binary
// operator on ints // operator on ints
#define OPERATOR_I(OP) \ #define OPERATOR_I(OP) \
a = to_script((int)*a OP (int)*b); \ a = to_script(a->toInt() OP b->toInt()); \
break break
// operator on bools // operator on bools
#define OPERATOR_B(OP) \ #define OPERATOR_B(OP) \
a = to_script((bool)*a OP (bool)*b); \ a = to_script(a->toBool() OP b->toBool()); \
break break
// operator on doubles or ints // operator on doubles or ints
#define OPERATOR_DI(OP) \ #define OPERATOR_DI(OP) \
if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) { \ if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) { \
a = to_script((double)*a OP (double)*b); \ a = to_script(a->toDouble() OP b->toDouble()); \
} else { \ } else { \
a = to_script((int)*a OP (int)*b); \ a = to_script(a->toInt() OP b->toInt()); \
} \ } \
break break
// operator on doubles or ints, defined as a function // operator on doubles or ints, defined as a function
#define OPERATOR_FUN_DI(OP) \ #define OPERATOR_FUN_DI(OP) \
if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) { \ if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) { \
a = to_script(OP((double)*a, (double)*b)); \ a = to_script(OP(a->toDouble(), b->toDouble())); \
} else { \ } else { \
a = to_script(OP((int)*a, (int)*b)); \ a = to_script(OP(a->toInt(), b->toInt())); \
} \ } \
break break
void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP& b) { void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP& b) {
switch (i) { switch (i) {
case I_MEMBER: case I_MEMBER:
a = a->getMember(*b); a = a->getMember(b->toString());
break; break;
case I_ITERATOR_R: case I_ITERATOR_R:
a = rangeIterator(*a, *b); a = rangeIterator(a->toInt(), b->toInt());
break; break;
default: default:
ScriptType at = a->type(), bt = b->type(); ScriptType at = a->type(), bt = b->type();
...@@ -472,46 +472,46 @@ void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP& ...@@ -472,46 +472,46 @@ void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP&
a = intrusive(new ScriptCompose(a, b)); a = intrusive(new ScriptCompose(a, b));
} else if (at == SCRIPT_COLLECTION && bt == SCRIPT_COLLECTION) { } else if (at == SCRIPT_COLLECTION && bt == SCRIPT_COLLECTION) {
a = intrusive(new ScriptConcatCollection(a, b)); a = intrusive(new ScriptConcatCollection(a, b));
} else if (at == SCRIPT_INT && bt == SCRIPT_INT) { } else if (at == SCRIPT_INT && bt == SCRIPT_INT) {
a = to_script((int)*a + (int)*b); a = to_script(a->toInt() + b->toInt());
} else if ((at == SCRIPT_INT || at == SCRIPT_DOUBLE) && } else if ((at == SCRIPT_INT || at == SCRIPT_DOUBLE) &&
(bt == SCRIPT_INT || bt == SCRIPT_DOUBLE)) { (bt == SCRIPT_INT || bt == SCRIPT_DOUBLE)) {
a = to_script((double)*a + (double)*b); a = to_script(a->toDouble() + b->toDouble());
} else { } else {
a = to_script(a->toString() + b->toString()); a = to_script(a->toString() + b->toString());
} }
break; break;
case I_SUB: OPERATOR_DI(-); case I_SUB: OPERATOR_DI(-);
case I_MUL: OPERATOR_DI(*); case I_MUL: OPERATOR_DI(*);
case I_FDIV: case I_FDIV:
a = to_script((double)*a / (double)*b); a = to_script(a->toDouble() / b->toDouble());
break; break;
case I_DIV: case I_DIV:
if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) { if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) {
a = to_script((int)((double)*a / (double)*b)); a = to_script((int)(a->toDouble() / b->toDouble()));
} else { } else {
a = to_script((int)*a / (int)*b); a = to_script(a->toInt() / b->toInt());
} }
break; break;
case I_MOD: case I_MOD:
if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) { if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) {
a = to_script(fmod((double)*a, (double)*b)); a = to_script(fmod(a->toDouble(), b->toDouble()));
} else { } else {
a = to_script((int)*a % (int)*b); a = to_script(a->toInt() % b->toInt());
} }
break; break;
case I_POW: case I_POW:
if (bt == SCRIPT_INT) { if (bt == SCRIPT_INT) {
int bi = *b; int bi = b->toInt();
if (at == SCRIPT_DOUBLE) { if (at == SCRIPT_DOUBLE) {
double aa = *a; double aa = a->toDouble();
if (bi == 0) a = to_script(1); if (bi == 0) a = to_script(1);
else if (bi == 1) a = to_script(aa); else if (bi == 1) a = to_script(aa);
else if (bi == 2) a = to_script(aa * aa); else if (bi == 2) a = to_script(aa * aa);
else if (bi == 3) a = to_script(aa * aa * aa); else if (bi == 3) a = to_script(aa * aa * aa);
else a = to_script(pow(aa,bi)); else a = to_script(pow(aa,bi));
} else { } else {
int aa = *a; int aa = a->toInt();
if (bi == 0) a = to_script(1); if (bi == 0) a = to_script(1);
else if (bi == 1) a = to_script(aa); else if (bi == 1) a = to_script(aa);
else if (bi == 2) a = to_script(aa * aa); else if (bi == 2) a = to_script(aa * aa);
...@@ -519,7 +519,7 @@ void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP& ...@@ -519,7 +519,7 @@ void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP&
else a = to_script(pow((double)aa,bi)); else a = to_script(pow((double)aa,bi));
} }
} else { } else {
a = to_script(pow((double)*a, (double)*b)); a = to_script(pow(a->toDouble(), b->toDouble()));
} }
break; break;
case I_AND: OPERATOR_B(&&); case I_AND: OPERATOR_B(&&);
...@@ -546,7 +546,7 @@ void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP& ...@@ -546,7 +546,7 @@ void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP&
void instrTernary(TernaryInstructionType i, ScriptValueP& a, const ScriptValueP& b, const ScriptValueP& c) { void instrTernary(TernaryInstructionType i, ScriptValueP& a, const ScriptValueP& b, const ScriptValueP& c) {
switch (i) { switch (i) {
case I_RGB: case I_RGB:
a = to_script(Color((int)*a, (int)*b, (int)*c)); a = to_script(Color(a->toInt(), b->toInt(), c->toInt()));
break; break;
} }
} }
...@@ -556,7 +556,7 @@ void instrTernary(TernaryInstructionType i, ScriptValueP& a, const ScriptValueP& ...@@ -556,7 +556,7 @@ void instrTernary(TernaryInstructionType i, ScriptValueP& a, const ScriptValueP&
void instrQuaternary(QuaternaryInstructionType i, ScriptValueP& a, const ScriptValueP& b, const ScriptValueP& c, const ScriptValueP& d) { void instrQuaternary(QuaternaryInstructionType i, ScriptValueP& a, const ScriptValueP& b, const ScriptValueP& c, const ScriptValueP& d) {
switch (i) { switch (i) {
case I_RGBA: case I_RGBA:
a = to_script(AColor((int)*a, (int)*b, (int)*c, (int)*d)); a = to_script(AColor(a->toInt(), b->toInt(), c->toInt(), d->toInt()));
break; break;
} }
} }
......
...@@ -83,10 +83,10 @@ class ScriptMissingVariable : public ScriptValue { ...@@ -83,10 +83,10 @@ class ScriptMissingVariable : public ScriptValue {
ScriptMissingVariable(const String& name) : name(name) {} ScriptMissingVariable(const String& name) : name(name) {}
virtual ScriptType type() const { return SCRIPT_NIL; } virtual ScriptType type() const { return SCRIPT_NIL; }
virtual String typeName() const { return _("missing variable '") + name + _("'"); } virtual String typeName() const { return _("missing variable '") + name + _("'"); }
virtual operator String() const { return wxEmptyString; } virtual String toString() const { return wxEmptyString; }
virtual operator double() const { return 0.0; } virtual double toDouble() const { return 0.0; }
virtual operator int() const { return 0; } virtual int toInt() const { return 0; }
virtual operator bool() const { return false; } virtual bool toBool() const { return false; }
virtual ScriptValueP eval(Context&) const { return script_nil; } // nil() == nil virtual ScriptValueP eval(Context&) const { return script_nil; } // nil() == nil
private: private:
String name; ///< Name of the variable String name; ///< Name of the variable
...@@ -227,7 +227,7 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script) ...@@ -227,7 +227,7 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script)
// Get an object member (almost as normal) // Get an object member (almost as normal)
case I_MEMBER_C: { case I_MEMBER_C: {
String name = *script.constants[i.data]; String name = script.constants[i.data]->toString();
stack.back() = stack.back()->dependencyMember(name, dep); // dependency on member stack.back() = stack.back()->dependencyMember(name, dep); // dependency on member
break; break;
} }
......
...@@ -68,9 +68,9 @@ String format_input(const String& format, const ScriptValue& input) { ...@@ -68,9 +68,9 @@ String format_input(const String& format, const ScriptValue& input) {
// determine type expected by format string // determine type expected by format string
String fmt = _("%") + replace_all(format, _("%"), _("")); String fmt = _("%") + replace_all(format, _("%"), _(""));
if (format.find_first_of(_("DdIiOoXx")) != String::npos) { if (format.find_first_of(_("DdIiOoXx")) != String::npos) {
return String::Format(fmt, (int)input); return String::Format(fmt, input.toInt());
} else if (format.find_first_of(_("EeFfGg")) != String::npos) { } else if (format.find_first_of(_("EeFfGg")) != String::npos) {
return String::Format(fmt, (double)input); return String::Format(fmt, input.toDouble());
} else if (format.find_first_of(_("Ss")) != String::npos) { } else if (format.find_first_of(_("Ss")) != String::npos) {
return format_string(fmt, input.toString()); return format_string(fmt, input.toString());
} else { } else {
...@@ -85,7 +85,7 @@ SCRIPT_FUNCTION(to_string) { ...@@ -85,7 +85,7 @@ SCRIPT_FUNCTION(to_string) {
try { try {
if (format && format->type() == SCRIPT_STRING) { if (format && format->type() == SCRIPT_STRING) {
// format specifier. Be careful, the built in function 'format' has the same name // format specifier. Be careful, the built in function 'format' has the same name
SCRIPT_RETURN(format_input(*format, *input)); SCRIPT_RETURN(format_input(format->toString(), *input));
} else { } else {
// simple conversion // simple conversion
SCRIPT_RETURN(input->toString()); SCRIPT_RETURN(input->toString());
...@@ -101,9 +101,9 @@ SCRIPT_FUNCTION(to_int) { ...@@ -101,9 +101,9 @@ SCRIPT_FUNCTION(to_int) {
try { try {
int result; int result;
if (t == SCRIPT_BOOL) { if (t == SCRIPT_BOOL) {
result = (bool)*input ? 1 : 0; result = input->toBool() ? 1 : 0;
} else if (t == SCRIPT_COLOR) { } else if (t == SCRIPT_COLOR) {
AColor c = (AColor)*input; AColor c = input->toColor();
result = (c.Red() + c.Blue() + c.Green()) / 3; result = (c.Red() + c.Blue() + c.Green()) / 3;
} else if (t == SCRIPT_STRING) { } else if (t == SCRIPT_STRING) {
long l; long l;
...@@ -116,7 +116,7 @@ SCRIPT_FUNCTION(to_int) { ...@@ -116,7 +116,7 @@ SCRIPT_FUNCTION(to_int) {
return delay_error(ScriptErrorConversion(str, input->typeName(), _TYPE_("integer"))); return delay_error(ScriptErrorConversion(str, input->typeName(), _TYPE_("integer")));
} }
} else { } else {
result = (int)*input; result = input->toInt();
} }
SCRIPT_RETURN(result); SCRIPT_RETURN(result);
} catch (const ScriptError& e) { } catch (const ScriptError& e) {
...@@ -130,9 +130,9 @@ SCRIPT_FUNCTION(to_real) { ...@@ -130,9 +130,9 @@ SCRIPT_FUNCTION(to_real) {
try { try {
double result; double result;
if (t == SCRIPT_BOOL) { if (t == SCRIPT_BOOL) {
result = (bool)*input ? 1.0 : 0.0; result = input->toBool() ? 1.0 : 0.0;
} else if (t == SCRIPT_COLOR) { } else if (t == SCRIPT_COLOR) {
AColor c = (AColor)*input; AColor c = input->toColor();
result = (c.Red() + c.Blue() + c.Green()) / 3.0; result = (c.Red() + c.Blue() + c.Green()) / 3.0;
} else if (t == SCRIPT_STRING) { } else if (t == SCRIPT_STRING) {
String str = input->toString(); String str = input->toString();
...@@ -142,7 +142,7 @@ SCRIPT_FUNCTION(to_real) { ...@@ -142,7 +142,7 @@ SCRIPT_FUNCTION(to_real) {
return delay_error(ScriptErrorConversion(str, input->typeName(), _TYPE_("double"))); return delay_error(ScriptErrorConversion(str, input->typeName(), _TYPE_("double")));
} }
} else { } else {
result = (double)*input; result = input->toDouble();
} }
SCRIPT_RETURN(result); SCRIPT_RETURN(result);
} catch (const ScriptError& e) { } catch (const ScriptError& e) {
...@@ -155,12 +155,12 @@ SCRIPT_FUNCTION(to_number) { ...@@ -155,12 +155,12 @@ SCRIPT_FUNCTION(to_number) {
ScriptType t = input->type(); ScriptType t = input->type();
try { try {
if (t == SCRIPT_BOOL) { if (t == SCRIPT_BOOL) {
SCRIPT_RETURN((bool)*input ? 1 : 0); SCRIPT_RETURN(input->toBool() ? 1 : 0);
} else if (t == SCRIPT_COLOR) { } else if (t == SCRIPT_COLOR) {
AColor c = (AColor)*input; AColor c = input->toColor();
SCRIPT_RETURN( (c.Red() + c.Blue() + c.Green()) / 3 ); SCRIPT_RETURN( (c.Red() + c.Blue() + c.Green()) / 3 );
} else if (t == SCRIPT_DOUBLE) { } else if (t == SCRIPT_DOUBLE || t == SCRIPT_INT) {
SCRIPT_RETURN((double)*input); return input;
} else if (t == SCRIPT_NIL) { } else if (t == SCRIPT_NIL) {
SCRIPT_RETURN(0); SCRIPT_RETURN(0);
} else { } else {
...@@ -187,9 +187,9 @@ SCRIPT_FUNCTION(to_boolean) { ...@@ -187,9 +187,9 @@ SCRIPT_FUNCTION(to_boolean) {
ScriptType t = input->type(); ScriptType t = input->type();
bool result; bool result;
if (t == SCRIPT_INT) { if (t == SCRIPT_INT) {
result = (int)*input != 0; result = input->toInt() != 0;
} else { } else {
result = (bool)*input; result = input->toBool();
} }
SCRIPT_RETURN(result); SCRIPT_RETURN(result);
} catch (const ScriptError& e) { } catch (const ScriptError& e) {
...@@ -226,9 +226,9 @@ SCRIPT_FUNCTION(abs) { ...@@ -226,9 +226,9 @@ SCRIPT_FUNCTION(abs) {
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_DOUBLE) { if (t == SCRIPT_DOUBLE) {
SCRIPT_RETURN(fabs((double)*input)); SCRIPT_RETURN(fabs(input->toDouble()));
} else { } else {
SCRIPT_RETURN(abs((int)*input)); SCRIPT_RETURN(abs(input->toInt()));
} }
} }
...@@ -566,7 +566,7 @@ SCRIPT_FUNCTION(filter_list) { ...@@ -566,7 +566,7 @@ SCRIPT_FUNCTION(filter_list) {
ScriptValueP it = input->makeIterator(input); ScriptValueP it = input->makeIterator(input);
while (ScriptValueP v = it->next()) { while (ScriptValueP v = it->next()) {
ctx.setVariable(SCRIPT_VAR_input, v); ctx.setVariable(SCRIPT_VAR_input, v);
if (*filter->eval(ctx)) { if (filter->eval(ctx)->toBool()) {
ret->value.push_back(v); ret->value.push_back(v);
} }
} }
...@@ -609,7 +609,7 @@ SCRIPT_FUNCTION(random_select_many) { ...@@ -609,7 +609,7 @@ SCRIPT_FUNCTION(random_select_many) {
SCRIPT_PARAM_C(ScriptValueP, input); SCRIPT_PARAM_C(ScriptValueP, input);
SCRIPT_PARAM(int, count) ; SCRIPT_PARAM(int, count) ;
SCRIPT_OPTIONAL_PARAM_C_(ScriptValueP, replace); SCRIPT_OPTIONAL_PARAM_C_(ScriptValueP, replace);
bool with_replace = replace && replace->type() != SCRIPT_FUNCTION && (bool)*replace; bool with_replace = replace && replace->type() != SCRIPT_FUNCTION && replace->toBool();
// pick many // pick many
ScriptCustomCollectionP ret(new ScriptCustomCollection); ScriptCustomCollectionP ret(new ScriptCustomCollection);
int itemCount = input->itemCount(); int itemCount = input->itemCount();
......
...@@ -44,7 +44,7 @@ SCRIPT_FUNCTION(new_card) { ...@@ -44,7 +44,7 @@ SCRIPT_FUNCTION(new_card) {
} else if (PackageChoiceValue* pvalue = dynamic_cast<PackageChoiceValue*>(value)) { } else if (PackageChoiceValue* pvalue = dynamic_cast<PackageChoiceValue*>(value)) {
pvalue->package_name = v->toString(); pvalue->package_name = v->toString();
} else if (ColorValue* cvalue = dynamic_cast<ColorValue*>(value)) { } else if (ColorValue* cvalue = dynamic_cast<ColorValue*>(value)) {
cvalue->value = (AColor)*v; cvalue->value = v->toColor();
} else { } else {
throw ScriptError(format_string(_("Can not set value '%s', it is not of the right type"),name)); throw ScriptError(format_string(_("Can not set value '%s', it is not of the right type"),name));
} }
......
...@@ -53,7 +53,7 @@ ScriptRegexP regex_from_script(const ScriptValueP& value) { ...@@ -53,7 +53,7 @@ ScriptRegexP regex_from_script(const ScriptValueP& value) {
ScriptRegexP regex = dynamic_pointer_cast<ScriptRegex>(value); ScriptRegexP regex = dynamic_pointer_cast<ScriptRegex>(value);
if (!regex) { if (!regex) {
// TODO: introduce some kind of caching? // TODO: introduce some kind of caching?
regex = intrusive(new ScriptRegex(*value)); regex = intrusive(new ScriptRegex(value->toString()));
} }
return regex; return regex;
} }
......
...@@ -37,12 +37,12 @@ inline size_t spelled_correctly(const String& input, size_t start, size_t end, S ...@@ -37,12 +37,12 @@ inline size_t spelled_correctly(const String& input, size_t start, size_t end, S
if (extra_test) { if (extra_test) {
// try on untagged // try on untagged
ctx.setVariable(SCRIPT_VAR_input, to_script(word)); ctx.setVariable(SCRIPT_VAR_input, to_script(word));
if (*extra_test->eval(ctx)) { if (extra_test->eval(ctx)->toBool()) {
return true; return true;
} }
// try on tagged // try on tagged
ctx.setVariable(SCRIPT_VAR_input, to_script(input.substr(start,end-start))); ctx.setVariable(SCRIPT_VAR_input, to_script(input.substr(start,end-start)));
if (*extra_test->eval(ctx)) { if (extra_test->eval(ctx)->toBool()) {
return true; return true;
} }
} }
......
...@@ -21,15 +21,16 @@ DECLARE_TYPEOF_COLLECTION(ScriptParseError); ...@@ -21,15 +21,16 @@ DECLARE_TYPEOF_COLLECTION(ScriptParseError);
// ----------------------------------------------------------------------------- : Store // ----------------------------------------------------------------------------- : Store
// TODO: reduce duplication, see from_script
void store(const ScriptValueP& val, String& var) { var = val->toString(); } void store(const ScriptValueP& val, String& var) { var = val->toString(); }
void store(const ScriptValueP& val, int& var) { var = *val; } void store(const ScriptValueP& val, int& var) { var = val->toInt(); }
void store(const ScriptValueP& val, double& var) { var = *val; } void store(const ScriptValueP& val, double& var) { var = val->toDouble(); }
void store(const ScriptValueP& val, bool& var) { var = *val; } void store(const ScriptValueP& val, bool& var) { var = val->toBool(); }
void store(const ScriptValueP& val, Color& var) { var = (AColor)*val; } void store(const ScriptValueP& val, Color& var) { var = val->toColor(); }
void store(const ScriptValueP& val, AColor& var) { var = *val; } void store(const ScriptValueP& val, AColor& var) { var = val->toColor(); }
void store(const ScriptValueP& val, Defaultable<String>& var) { var.assign(*val); } void store(const ScriptValueP& val, Defaultable<String>& var) { var.assign(val->toString()); }
void store(const ScriptValueP& val, Defaultable<Color>& var) { var.assign((AColor)*val); } void store(const ScriptValueP& val, Defaultable<Color>& var) { var.assign(val->toColor()); }
void store(const ScriptValueP& val, Defaultable<AColor>& var) { var.assign(*val); } void store(const ScriptValueP& val, Defaultable<AColor>& var) { var.assign(val->toColor()); }
void store(const ScriptValueP& val, Alignment& var) { var = from_string(val->toString()); } void store(const ScriptValueP& val, Alignment& var) { var = from_string(val->toString()); }
void store(const ScriptValueP& val, Direction& var) { parse_enum(val->toString(),var); } void store(const ScriptValueP& val, Direction& var) { parse_enum(val->toString(),var); }
......
...@@ -74,11 +74,11 @@ class ScriptDelayedError : public ScriptValue { ...@@ -74,11 +74,11 @@ class ScriptDelayedError : public ScriptValue {
// all of these throw // all of these throw
virtual String typeName() const; virtual String typeName() const;
virtual operator String() const; virtual String toString() const;
virtual operator double() const; virtual double toDouble() const;
virtual operator int() const; virtual int toInt() const;
virtual operator bool() const; virtual bool toBool() const;
virtual operator AColor() const; virtual AColor toColor() const;
virtual int itemCount() const; virtual int itemCount() const;
virtual CompareWhat compareAs(String&, void const*&) const; virtual CompareWhat compareAs(String&, void const*&) const;
// these can propagate the error // these can propagate the error
...@@ -273,12 +273,12 @@ class ScriptObject : public ScriptValue { ...@@ -273,12 +273,12 @@ class ScriptObject : public ScriptValue {
inline ScriptObject(const T& v) : value(v) {} inline ScriptObject(const T& v) : value(v) {}
virtual ScriptType type() const { ScriptValueP d = getDefault(); return d ? d->type() : SCRIPT_OBJECT; } virtual ScriptType type() const { ScriptValueP d = getDefault(); return d ? d->type() : SCRIPT_OBJECT; }
virtual String typeName() const { return type_name(*value); } virtual String typeName() const { return type_name(*value); }
virtual operator String() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator String(); } virtual String toString() const { ScriptValueP d = getDefault(); return d ? d->toString() : ScriptValue::toString(); }
virtual operator double() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator double(); } virtual int toInt() const { ScriptValueP d = getDefault(); return d ? d->toInt() : ScriptValue::toInt(); }
virtual operator int() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator int(); } virtual double toDouble() const { ScriptValueP d = getDefault(); return d ? d->toDouble() : ScriptValue::toDouble(); }
virtual operator bool() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator bool(); } virtual bool toBool() const { ScriptValueP d = getDefault(); return d ? d->toBool() : ScriptValue::toBool(); }
virtual operator AColor() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator AColor(); } virtual AColor toColor() const { ScriptValueP d = getDefault(); return d ? d->toColor() : ScriptValue::toColor(); }
virtual String toCode() const { ScriptValueP d = getDefault(); return d ? d->toCode() : to_code(*value); } virtual String toCode() const { ScriptValueP d = getDefault(); return d ? d->toCode() : to_code(*value); }
virtual GeneratedImageP toImage(const ScriptValueP& thisP) const { virtual GeneratedImageP toImage(const ScriptValueP& thisP) const {
ScriptValueP d = getDefault(); return d ? d->toImage(d) : ScriptValue::toImage(thisP); ScriptValueP d = getDefault(); return d ? d->toImage(d) : ScriptValue::toImage(thisP);
} }
...@@ -422,13 +422,13 @@ template <typename T> inline T from_script (const ScriptValueP& va ...@@ -422,13 +422,13 @@ template <typename T> inline T from_script (const ScriptValueP& va
return o->getValue(); return o->getValue();
} }
template <> inline ScriptValueP from_script<ScriptValueP>(const ScriptValueP& value) { return value; } template <> inline ScriptValueP from_script<ScriptValueP>(const ScriptValueP& value) { return value; }
template <> inline String from_script<String> (const ScriptValueP& value) { return *value; } template <> inline String from_script<String> (const ScriptValueP& value) { return value->toString(); }
template <> inline int from_script<int> (const ScriptValueP& value) { return *value; } template <> inline int from_script<int> (const ScriptValueP& value) { return value->toInt(); }
template <> inline double from_script<double> (const ScriptValueP& value) { return *value; } template <> inline double from_script<double> (const ScriptValueP& value) { return value->toDouble(); }
template <> inline bool from_script<bool> (const ScriptValueP& value) { return *value; } template <> inline bool from_script<bool> (const ScriptValueP& value) { return value->toBool(); }
template <> inline Color from_script<Color> (const ScriptValueP& value) { return (AColor)*value; } template <> inline Color from_script<Color> (const ScriptValueP& value) { return value->toColor(); }
template <> inline AColor from_script<AColor> (const ScriptValueP& value) { return *value; } template <> inline AColor from_script<AColor> (const ScriptValueP& value) { return value->toColor(); }
template <> inline wxDateTime from_script<wxDateTime> (const ScriptValueP& value) { return *value; } template <> inline wxDateTime from_script<wxDateTime> (const ScriptValueP& value) { return value->toDateTime(); }
// ----------------------------------------------------------------------------- : EOF // ----------------------------------------------------------------------------- : EOF
#endif #endif
This diff is collapsed.
...@@ -59,28 +59,19 @@ class ScriptValue : public IntrusivePtrBaseWithDelete { ...@@ -59,28 +59,19 @@ class ScriptValue : public IntrusivePtrBaseWithDelete {
virtual CompareWhat compareAs(String& compare_str, void const*& compare_ptr) const; virtual CompareWhat compareAs(String& compare_str, void const*& compare_ptr) const;
/// Convert this value to a string /// Convert this value to a string
virtual operator String() const; virtual String toString() const;
/// Script code to generate this value
virtual String toCode() const;
/// Convert this value to a double /// Convert this value to a double
virtual operator double() const; virtual double toDouble() const;
/// Convert this value to an integer /// Convert this value to an integer
virtual operator int() const; virtual int toInt() const;
/// Convert this value to a boolean /// Convert this value to a boolean
virtual operator bool() const; virtual bool toBool() const;
/// Convert this value to a color /// Convert this value to a color
virtual operator AColor() const; virtual AColor toColor() const;
/// Convert this value to a wxDateTime /// Convert this value to a wxDateTime
virtual operator wxDateTime() const; virtual wxDateTime toDateTime() const;
/// Script code to generate this value
virtual String toCode() const;
/// Explicit overload to convert to a string
/** This is sometimes necessary, because wxString has an int constructor,
* which confuses gcc. */
inline String toString() const { return *this; }
/// Explicit overload to convert to a wxDateTime
/** Overload resolution is sometimes confused by other conversions */
inline wxDateTime toDateTime() const { return *this; }
/// Convert this value to an image /// Convert this value to an image
virtual GeneratedImageP toImage(const ScriptValueP& thisP) const; virtual GeneratedImageP toImage(const ScriptValueP& thisP) const;
......
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