Add another unit test for expressions

refs #8074
This commit is contained in:
Gunnar Beutner 2014-12-15 17:23:18 +01:00
parent b9831f0be4
commit d72bd4fec2
3 changed files with 21 additions and 11 deletions

View File

@ -263,7 +263,7 @@ Value FunctionCallExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) c
String index;
if (m_FName->GetReference(frame, false, &self, &index))
vfunc = VMOps::GetField(self, index);
vfunc = VMOps::GetField(self, index, m_DebugInfo);
else
vfunc = m_FName->Evaluate(frame);
@ -343,7 +343,7 @@ Value SetExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
Value right = m_Operand2->Evaluate(frame, dhint);
if (m_Op != OpSetLiteral) {
Value object = VMOps::GetField(parent, index);
Value object = VMOps::GetField(parent, index, m_DebugInfo);
Expression *lhs = MakeLiteral(object);
Expression *rhs = MakeLiteral(right);
@ -409,7 +409,7 @@ Value IndexerExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
Value object = m_Operand1->Evaluate(frame, dhint);
String index = m_Operand2->Evaluate(frame, dhint);
return VMOps::GetField(object, index);
return VMOps::GetField(object, index, m_DebugInfo);
}
bool IndexerExpression::GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const
@ -421,7 +421,7 @@ bool IndexerExpression::GetReference(ScriptFrame& frame, bool init_dict, Value *
if (init_dict && VMOps::GetField(vparent, vindex).IsEmpty())
VMOps::SetField(vparent, vindex, new Dictionary());
*parent = VMOps::GetField(vparent, vindex);
*parent = VMOps::GetField(vparent, vindex, m_DebugInfo);
} else
*parent = m_Operand1->Evaluate(frame);

View File

@ -186,7 +186,7 @@ public:
}
}
static inline Value GetPrototypeField(const Value& context, const String& field, const DebugInfo& debugInfo = DebugInfo())
static inline Value GetPrototypeField(const Value& context, const String& field, bool not_found_error = true, const DebugInfo& debugInfo = DebugInfo())
{
Type::Ptr type = context.GetReflectionType();
@ -194,18 +194,24 @@ public:
Object::Ptr object = type->GetPrototype();
if (HasField(object, field))
return GetField(object, field);
return GetField(object, field, debugInfo);
type = type->GetBaseType();
} while (type);
return Empty;
if (not_found_error)
BOOST_THROW_EXCEPTION(ScriptError("Invalid field name: '" + field + "'", debugInfo));
else
return Empty;
}
static inline Value GetField(const Value& context, const String& field, const DebugInfo& debugInfo = DebugInfo())
{
if (context.IsEmpty())
return Empty;
if (!context.IsObject())
return GetPrototypeField(context, field);
return GetPrototypeField(context, field, true, debugInfo);
Object::Ptr object = context;
@ -215,7 +221,7 @@ public:
if (dict->Contains(field))
return dict->Get(field);
else
return GetPrototypeField(context, field);
return GetPrototypeField(context, field, false, debugInfo);
}
Array::Ptr arr = dynamic_pointer_cast<Array>(object);
@ -226,7 +232,7 @@ public:
try {
index = Convert::ToLong(field);
} catch (...) {
return GetPrototypeField(context, field);
return GetPrototypeField(context, field, true, debugInfo);
}
return arr->Get(index);
@ -240,7 +246,7 @@ public:
int fid = type->GetFieldId(field);
if (fid == -1)
return GetPrototypeField(context, field);
return GetPrototypeField(context, field, true, debugInfo);
return object->GetField(fid);
}

View File

@ -307,6 +307,10 @@ BOOST_AUTO_TEST_CASE(advanced)
expr = ConfigCompiler::CompileText("<test>", "var e = 3; e");
BOOST_CHECK(expr->Evaluate(frame) == 3);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "Array.x");
BOOST_CHECK_THROW(expr->Evaluate(frame), ScriptError);
delete expr;
}
BOOST_AUTO_TEST_SUITE_END()