2013-10-20 15:06:05 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2013-10-20 15:06:05 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-10-16 17:44:06 +02:00
|
|
|
#include "config/expression.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "config/configitem.hpp"
|
2014-11-15 08:20:22 +01:00
|
|
|
#include "config/vmops.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/array.hpp"
|
2014-10-26 19:59:49 +01:00
|
|
|
#include "base/json.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/object.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-12-10 15:06:09 +01:00
|
|
|
#include "base/scripterror.hpp"
|
2014-03-18 15:29:04 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2014-03-22 08:40:09 +01:00
|
|
|
#include <boost/exception_ptr.hpp>
|
|
|
|
#include <boost/exception/errinfo_nested_exception.hpp>
|
2013-10-20 15:06:05 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Expression::~Expression(void)
|
2014-03-22 10:29:45 +01:00
|
|
|
{ }
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value Expression::Evaluate(VMFrame& frame, DebugHint *dhint) const
|
2013-10-20 15:06:05 +02:00
|
|
|
{
|
2014-03-22 10:29:45 +01:00
|
|
|
try {
|
2014-03-29 13:48:04 +01:00
|
|
|
#ifdef _DEBUG
|
2014-11-16 16:20:39 +01:00
|
|
|
/* std::ostringstream msgbuf;
|
2014-11-09 19:48:28 +01:00
|
|
|
ShowCodeFragment(msgbuf, GetDebugInfo(), false);
|
|
|
|
Log(LogDebug, "Expression")
|
2014-11-16 16:20:39 +01:00
|
|
|
<< "Executing:\n" << msgbuf.str();*/
|
2014-03-29 13:48:04 +01:00
|
|
|
#endif /* _DEBUG */
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
return DoEvaluate(frame, dhint);
|
2014-11-24 00:04:26 +01:00
|
|
|
} catch (const InterruptExecutionError&) {
|
|
|
|
throw;
|
2014-12-10 15:06:09 +01:00
|
|
|
} catch (const ScriptError& ex) {
|
|
|
|
throw;
|
2014-03-22 10:29:45 +01:00
|
|
|
} catch (const std::exception& ex) {
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Error while evaluating expression: " + String(ex.what()), GetDebugInfo())
|
|
|
|
<< boost::errinfo_nested_exception(boost::current_exception()));
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
2013-10-20 15:06:05 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
const DebugInfo& Expression::GetDebugInfo(void) const
|
2014-03-23 11:27:40 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
static DebugInfo debugInfo;
|
|
|
|
return debugInfo;
|
2014-03-23 19:58:24 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
std::vector<Expression *> icinga::MakeIndexer(const String& index1)
|
2014-03-23 19:58:24 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
std::vector<Expression *> result;
|
2014-11-23 15:20:51 +01:00
|
|
|
result.push_back(new VariableExpression(index1));
|
2014-11-09 19:48:28 +01:00
|
|
|
return result;
|
2014-03-23 19:58:24 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
void DictExpression::MakeInline(void)
|
2013-10-20 15:06:05 +02:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
m_Inline = true;
|
2013-10-20 15:06:05 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
LiteralExpression::LiteralExpression(const Value& value)
|
|
|
|
: m_Value(value)
|
|
|
|
{ }
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value LiteralExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Value;
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
const DebugInfo& DebuggableExpression::GetDebugInfo(void) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_DebugInfo;
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value VariableExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return VMOps::Variable(frame, m_Variable);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value NegateExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return ~(long)m_Operand->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value LogicalNegateExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-28 13:25:40 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return !m_Operand->Evaluate(frame).ToBool();
|
2014-03-28 13:25:40 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value AddExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame) + m_Operand2->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value SubtractExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame) - m_Operand2->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value MultiplyExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame) * m_Operand2->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value DivideExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame) / m_Operand2->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-25 17:04:20 +01:00
|
|
|
Value ModuloExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
|
|
|
{
|
|
|
|
return (long)m_Operand1->Evaluate(frame) % (long)m_Operand2->Evaluate(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value XorExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
|
|
|
{
|
|
|
|
return (long)m_Operand1->Evaluate(frame) ^ (long)m_Operand2->Evaluate(frame);
|
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value BinaryAndExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame) & m_Operand2->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value BinaryOrExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame) | m_Operand2->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value ShiftLeftExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame) << m_Operand2->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value ShiftRightExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame) >> m_Operand2->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value EqualExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame) == m_Operand2->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value NotEqualExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame) != m_Operand2->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value LessThanExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame) < m_Operand2->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value GreaterThanExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame) > m_Operand2->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value LessThanOrEqualExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame) <= m_Operand2->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value GreaterThanOrEqualExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame) >= m_Operand2->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value InExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2013-10-20 15:06:05 +02:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
Value right = m_Operand2->Evaluate(frame);
|
2014-03-22 10:29:45 +01:00
|
|
|
|
2014-03-28 12:17:22 +01:00
|
|
|
if (right.IsEmpty())
|
|
|
|
return false;
|
|
|
|
else if (!right.IsObjectType<Array>())
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Invalid right side argument for 'in' operator: " + JsonEncode(right), GetDebugInfo()));
|
2014-03-22 10:29:45 +01:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value left = m_Operand1->Evaluate(frame);
|
2014-11-15 08:20:22 +01:00
|
|
|
|
2014-03-22 10:29:45 +01:00
|
|
|
Array::Ptr arr = right;
|
2014-11-15 08:22:09 +01:00
|
|
|
return arr->Contains(left);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value NotInExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
Value right = m_Operand2->Evaluate(frame);
|
2014-11-09 19:48:28 +01:00
|
|
|
|
|
|
|
if (right.IsEmpty())
|
2014-11-23 09:52:17 +01:00
|
|
|
return true;
|
2014-11-09 19:48:28 +01:00
|
|
|
else if (!right.IsObjectType<Array>())
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Invalid right side argument for 'in' operator: " + JsonEncode(right), GetDebugInfo()));
|
2014-11-09 19:48:28 +01:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value left = m_Operand1->Evaluate(frame);
|
2014-11-09 19:48:28 +01:00
|
|
|
|
|
|
|
Array::Ptr arr = right;
|
2014-11-15 08:22:09 +01:00
|
|
|
return !arr->Contains(left);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value LogicalAndExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame).ToBool() && m_Operand2->Evaluate(frame).ToBool();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value LogicalOrExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return m_Operand1->Evaluate(frame).ToBool() || m_Operand2->Evaluate(frame).ToBool();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value FunctionCallExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-12-11 21:12:34 +01:00
|
|
|
Object::Ptr self;
|
|
|
|
Value funcName;
|
|
|
|
|
|
|
|
if (!m_IName.empty()) {
|
|
|
|
Value result = m_IName[0]->Evaluate(frame);
|
|
|
|
|
|
|
|
if (m_IName.size() == 2) {
|
|
|
|
if (!result.IsObject())
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Tried to invoke method on something that is not an Object.", GetDebugInfo()));
|
|
|
|
|
|
|
|
self = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 1; i < m_IName.size(); i++) {
|
|
|
|
if (result.IsEmpty())
|
|
|
|
return Empty;
|
|
|
|
|
|
|
|
Value index = m_IName[i]->Evaluate(frame);
|
|
|
|
result = VMOps::GetField(result, index);
|
|
|
|
|
|
|
|
if (i == m_IName.size() - 2) {
|
|
|
|
if (!result.IsObject())
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Tried to invoke method on something that is not an Object.", GetDebugInfo()));
|
|
|
|
|
|
|
|
self = result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
funcName = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_FName)
|
|
|
|
funcName = m_FName->Evaluate(frame);
|
2014-04-02 09:04:17 +02:00
|
|
|
|
2014-03-19 10:51:09 +01:00
|
|
|
std::vector<Value> arguments;
|
2014-11-09 19:48:28 +01:00
|
|
|
BOOST_FOREACH(Expression *arg, m_Args) {
|
2014-11-22 12:21:28 +01:00
|
|
|
arguments.push_back(arg->Evaluate(frame));
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
2013-10-20 15:06:05 +02:00
|
|
|
|
2014-12-11 21:12:34 +01:00
|
|
|
return VMOps::FunctionCall(frame, self, funcName, arguments);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
2013-10-20 15:06:05 +02:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value ArrayExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr result = new Array();
|
2014-03-18 15:29:04 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
BOOST_FOREACH(Expression *aexpr, m_Expressions) {
|
2014-11-22 12:21:28 +01:00
|
|
|
result->Add(aexpr->Evaluate(frame));
|
2013-10-20 15:06:05 +02:00
|
|
|
}
|
2014-03-22 10:29:45 +01:00
|
|
|
|
|
|
|
return result;
|
2013-11-03 10:41:30 +01:00
|
|
|
}
|
2014-03-23 11:27:40 +01:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value DictExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-23 11:27:40 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
VMFrame *dframe;
|
|
|
|
VMFrame rframe;
|
2014-03-23 11:27:40 +01:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
if (!m_Inline) {
|
|
|
|
dframe = &rframe;
|
|
|
|
rframe.Locals = frame.Locals;
|
|
|
|
rframe.Self = new Dictionary();
|
|
|
|
} else {
|
|
|
|
dframe = &frame;
|
|
|
|
}
|
2014-03-24 09:10:37 +01:00
|
|
|
|
2014-11-23 09:53:28 +01:00
|
|
|
Value result;
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
BOOST_FOREACH(Expression *aexpr, m_Expressions) {
|
2014-11-23 09:53:28 +01:00
|
|
|
result = aexpr->Evaluate(*dframe, dhint);
|
2014-03-23 11:27:40 +01:00
|
|
|
}
|
|
|
|
|
2014-11-23 09:53:28 +01:00
|
|
|
if (m_Inline)
|
|
|
|
return result;
|
|
|
|
else
|
|
|
|
return dframe->Self;
|
2014-03-23 11:27:40 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value SetExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-23 11:27:40 +01:00
|
|
|
{
|
2014-11-17 10:34:11 +01:00
|
|
|
DebugHint *psdhint = dhint;
|
|
|
|
DebugHint sdhint;
|
2014-08-16 22:12:40 +02:00
|
|
|
|
2014-11-04 15:19:33 +01:00
|
|
|
Value parent, object;
|
|
|
|
String index;
|
2014-08-12 15:31:47 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
for (Array::SizeType i = 0; i < m_Indexer.size(); i++) {
|
|
|
|
Expression *indexExpr = m_Indexer[i];
|
2014-11-23 15:20:51 +01:00
|
|
|
|
|
|
|
String tempindex;
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
VariableExpression *vexpr = dynamic_cast<VariableExpression *>(indexExpr);
|
|
|
|
|
|
|
|
if (!vexpr) {
|
|
|
|
object = indexExpr->Evaluate(frame, dhint);
|
2014-11-24 17:02:22 +01:00
|
|
|
|
|
|
|
if (!object)
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Left-hand side argument must not be null.", GetDebugInfo()));
|
2014-11-24 17:02:22 +01:00
|
|
|
|
2014-11-23 15:20:51 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tempindex = vexpr->GetVariable();
|
|
|
|
} else
|
|
|
|
tempindex = indexExpr->Evaluate(frame, dhint);
|
2014-03-23 11:27:40 +01:00
|
|
|
|
2014-11-17 10:34:11 +01:00
|
|
|
if (psdhint) {
|
|
|
|
sdhint = psdhint->GetChild(tempindex);
|
|
|
|
psdhint = &sdhint;
|
|
|
|
}
|
2014-03-23 17:26:31 +01:00
|
|
|
|
2014-11-15 12:17:59 +01:00
|
|
|
if (i == 0)
|
2014-11-22 12:21:28 +01:00
|
|
|
parent = m_Local ? frame.Locals : frame.Self;
|
2014-11-15 12:17:59 +01:00
|
|
|
else
|
2014-11-04 15:19:33 +01:00
|
|
|
parent = object;
|
2014-03-23 17:26:31 +01:00
|
|
|
|
2014-11-15 12:17:59 +01:00
|
|
|
if (i == m_Indexer.size() - 1) {
|
|
|
|
index = tempindex;
|
2014-03-24 09:44:18 +01:00
|
|
|
|
2014-11-15 12:17:59 +01:00
|
|
|
/* No need to look up the last indexer's value if this is a direct set */
|
|
|
|
if (m_Op == OpSetLiteral)
|
|
|
|
break;
|
2014-11-04 15:19:33 +01:00
|
|
|
}
|
2014-03-23 19:58:24 +01:00
|
|
|
|
2014-11-23 15:20:51 +01:00
|
|
|
object = VMOps::GetField(parent, tempindex);
|
2014-08-16 22:12:40 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
if (i != m_Indexer.size() - 1 && object.IsEmpty()) {
|
2014-11-08 21:17:16 +01:00
|
|
|
object = new Dictionary();
|
2014-03-24 09:44:18 +01:00
|
|
|
|
2014-11-15 08:20:22 +01:00
|
|
|
VMOps::SetField(parent, tempindex, object);
|
2014-11-04 15:19:33 +01:00
|
|
|
}
|
2014-03-24 11:34:41 +01:00
|
|
|
}
|
2014-03-24 09:44:18 +01:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value right = m_Operand2->Evaluate(frame, dhint);
|
2014-08-12 15:31:47 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
if (m_Op != OpSetLiteral) {
|
|
|
|
Expression *lhs = MakeLiteral(object);
|
|
|
|
Expression *rhs = MakeLiteral(right);
|
2014-03-23 11:27:40 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
switch (m_Op) {
|
2014-11-04 15:19:33 +01:00
|
|
|
case OpSetAdd:
|
2014-11-22 12:21:28 +01:00
|
|
|
right = AddExpression(lhs, rhs, m_DebugInfo).Evaluate(frame, dhint);
|
2014-11-04 15:19:33 +01:00
|
|
|
break;
|
|
|
|
case OpSetSubtract:
|
2014-11-22 12:21:28 +01:00
|
|
|
right = SubtractExpression(lhs, rhs, m_DebugInfo).Evaluate(frame, dhint);
|
2014-11-04 15:19:33 +01:00
|
|
|
break;
|
|
|
|
case OpSetMultiply:
|
2014-11-22 12:21:28 +01:00
|
|
|
right = MultiplyExpression(lhs, rhs, m_DebugInfo).Evaluate(frame, dhint);
|
2014-11-04 15:19:33 +01:00
|
|
|
break;
|
|
|
|
case OpSetDivide:
|
2014-11-22 12:21:28 +01:00
|
|
|
right = DivideExpression(lhs, rhs, m_DebugInfo).Evaluate(frame, dhint);
|
2014-11-04 15:19:33 +01:00
|
|
|
break;
|
2014-11-25 17:04:20 +01:00
|
|
|
case OpSetModulo:
|
|
|
|
right = ModuloExpression(lhs, rhs, m_DebugInfo).Evaluate(frame, dhint);
|
|
|
|
break;
|
|
|
|
case OpSetXor:
|
|
|
|
right = XorExpression(lhs, rhs, m_DebugInfo).Evaluate(frame, dhint);
|
|
|
|
break;
|
|
|
|
case OpSetBinaryAnd:
|
|
|
|
right = BinaryAndExpression(lhs, rhs, m_DebugInfo).Evaluate(frame, dhint);
|
|
|
|
break;
|
|
|
|
case OpSetBinaryOr:
|
|
|
|
right = BinaryOrExpression(lhs, rhs, m_DebugInfo).Evaluate(frame, dhint);
|
|
|
|
break;
|
2014-11-04 15:19:33 +01:00
|
|
|
default:
|
|
|
|
VERIFY(!"Invalid opcode.");
|
|
|
|
}
|
2014-03-24 11:34:41 +01:00
|
|
|
}
|
2014-03-24 09:44:18 +01:00
|
|
|
|
2014-11-15 08:20:22 +01:00
|
|
|
VMOps::SetField(parent, index, right);
|
2014-08-16 22:12:40 +02:00
|
|
|
|
2014-11-17 10:34:11 +01:00
|
|
|
if (psdhint)
|
|
|
|
psdhint->AddMessage("=", m_DebugInfo);
|
2014-08-12 15:31:47 +02:00
|
|
|
|
2014-11-04 15:19:33 +01:00
|
|
|
return right;
|
2014-03-23 11:27:40 +01:00
|
|
|
}
|
2014-03-23 17:26:31 +01:00
|
|
|
|
2014-11-24 09:53:45 +01:00
|
|
|
Value ConditionalExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
|
|
|
{
|
|
|
|
if (m_Condition->Evaluate(frame, dhint))
|
|
|
|
return m_TrueBranch->Evaluate(frame, dhint);
|
|
|
|
else if (m_FalseBranch)
|
|
|
|
return m_FalseBranch->Evaluate(frame, dhint);
|
2014-11-24 18:24:47 +01:00
|
|
|
|
|
|
|
return Empty;
|
2014-11-24 09:53:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-24 00:04:26 +01:00
|
|
|
Value ReturnExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
|
|
|
{
|
|
|
|
BOOST_THROW_EXCEPTION(InterruptExecutionError(m_Operand->Evaluate(frame)));
|
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value IndexerExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-23 17:26:31 +01:00
|
|
|
{
|
2014-11-23 15:20:51 +01:00
|
|
|
return VMOps::Indexer(frame, m_Indexer);
|
2014-03-23 17:26:31 +01:00
|
|
|
}
|
2014-03-27 12:30:24 +01:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value ImportExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-27 12:30:24 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
String type = VMOps::GetField(frame.Self, "type");
|
|
|
|
Value name = m_Name->Evaluate(frame);
|
2014-03-27 12:30:24 +01:00
|
|
|
|
|
|
|
ConfigItem::Ptr item = ConfigItem::GetObject(type, name);
|
|
|
|
|
|
|
|
if (!item)
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Import references unknown template: '" + name + "'", GetDebugInfo()));
|
2014-03-27 12:30:24 +01:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
item->GetExpression()->Evaluate(frame, dhint);
|
2014-03-27 12:30:24 +01:00
|
|
|
|
|
|
|
return Empty;
|
|
|
|
}
|
2014-03-30 15:04:53 +02:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value FunctionExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-30 15:04:53 +02:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return VMOps::NewFunction(frame, m_Name, m_Args, m_ClosedVars, m_Expression);
|
2014-11-20 06:53:57 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value SlotExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-11-20 06:53:57 +01:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return VMOps::NewSlot(frame, m_Signal, m_Slot->Evaluate(frame));
|
2014-11-20 06:53:57 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value ApplyExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-30 15:04:53 +02:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
return VMOps::NewApply(frame, m_Type, m_Target, m_Name->Evaluate(frame), m_Filter,
|
|
|
|
m_FKVar, m_FVVar, m_FTerm, m_ClosedVars, m_Expression, m_DebugInfo);
|
2014-03-30 15:04:53 +02:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value ObjectExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-03-30 15:04:53 +02:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
String name;
|
2014-03-30 15:04:53 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
if (m_Name)
|
2014-11-22 12:21:28 +01:00
|
|
|
name = m_Name->Evaluate(frame, dhint);
|
2014-03-30 15:04:53 +02:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
return VMOps::NewObject(frame, m_Abstract, m_Type, name, m_Filter, m_Zone,
|
|
|
|
m_ClosedVars, m_Expression, m_DebugInfo);
|
2014-04-02 09:04:17 +02:00
|
|
|
}
|
2014-05-10 11:26:56 +02:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Value ForExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
2014-05-10 11:26:56 +02:00
|
|
|
{
|
2014-11-22 12:21:28 +01:00
|
|
|
Value value = m_Value->Evaluate(frame, dhint);
|
2014-05-10 11:26:56 +02:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
return VMOps::For(frame, m_FKVar, m_FVVar, m_Value->Evaluate(frame), m_Expression, m_DebugInfo);
|
2014-11-06 19:35:47 +01:00
|
|
|
}
|
|
|
|
|