2013-10-20 15:06:05 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 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"
|
2015-10-22 09:46:31 +02:00
|
|
|
#include "config/configcompiler.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-18 15:11:57 +01:00
|
|
|
#include "base/exception.hpp"
|
2014-12-14 11:33:45 +01:00
|
|
|
#include "base/scriptglobal.hpp"
|
2015-08-27 14:47:31 +02:00
|
|
|
#include "base/loader.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;
|
|
|
|
|
2015-11-05 14:29:45 +01:00
|
|
|
boost::signals2::signal<void (ScriptFrame&, ScriptError *ex, const DebugInfo&)> Expression::OnBreakpoint;
|
|
|
|
boost::thread_specific_ptr<bool> l_InBreakpointHandler;
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Expression::~Expression(void)
|
2014-03-22 10:29:45 +01:00
|
|
|
{ }
|
|
|
|
|
2015-11-05 14:29:45 +01:00
|
|
|
void Expression::ScriptBreakpoint(ScriptFrame& frame, ScriptError *ex, const DebugInfo& di)
|
|
|
|
{
|
|
|
|
bool *inHandler = l_InBreakpointHandler.get();
|
|
|
|
if (!inHandler || !*inHandler) {
|
|
|
|
inHandler = new bool(true);
|
|
|
|
l_InBreakpointHandler.reset(inHandler);
|
|
|
|
OnBreakpoint(frame, ex, di);
|
|
|
|
*inHandler = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult Expression::Evaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2013-10-20 15:06:05 +02:00
|
|
|
{
|
2014-03-22 10:29:45 +01:00
|
|
|
try {
|
2014-12-19 12:19:28 +01:00
|
|
|
#ifdef I2_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-12-19 12:19:28 +01:00
|
|
|
#endif /* I2_DEBUG */
|
2014-03-29 13:48:04 +01:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
return DoEvaluate(frame, dhint);
|
2015-11-05 14:29:45 +01:00
|
|
|
} catch (ScriptError& ex) {
|
|
|
|
ScriptBreakpoint(frame, &ex, GetDebugInfo());
|
2014-12-10 15:06:09 +01:00
|
|
|
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-12-14 11:33:45 +01:00
|
|
|
bool Expression::GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-12-14 11:33:45 +01:00
|
|
|
Expression *icinga::MakeIndexer(ScopeSpecifier scopeSpec, const String& index)
|
2014-03-23 19:58:24 +01:00
|
|
|
{
|
2014-12-14 11:33:45 +01:00
|
|
|
Expression *scope = new GetScopeExpression(scopeSpec);
|
|
|
|
return new IndexerExpression(scope, MakeLiteral(index));
|
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)
|
|
|
|
{ }
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult LiteralExpression::DoEvaluate(ScriptFrame& 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
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult VariableExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-03-31 11:45:38 +02:00
|
|
|
Value value;
|
|
|
|
|
|
|
|
if (frame.Locals && frame.Locals->Get(m_Variable, &value))
|
|
|
|
return value;
|
2014-12-15 12:57:40 +01:00
|
|
|
else if (frame.Self.IsObject() && frame.Locals != static_cast<Object::Ptr>(frame.Self) && VMOps::HasField(frame.Self, m_Variable))
|
2015-10-26 10:41:00 +01:00
|
|
|
return VMOps::GetField(frame.Self, m_Variable, frame.Sandboxed, m_DebugInfo);
|
2014-12-15 12:57:40 +01:00
|
|
|
else
|
|
|
|
return ScriptGlobal::Get(m_Variable);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-12-14 11:33:45 +01:00
|
|
|
bool VariableExpression::GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const
|
|
|
|
{
|
2014-12-15 12:57:40 +01:00
|
|
|
*index = m_Variable;
|
|
|
|
|
|
|
|
if (frame.Locals && frame.Locals->Contains(m_Variable)) {
|
|
|
|
*parent = frame.Locals;
|
|
|
|
|
|
|
|
if (dhint)
|
|
|
|
*dhint = NULL;
|
|
|
|
} else if (frame.Self.IsObject() && frame.Locals != static_cast<Object::Ptr>(frame.Self) && VMOps::HasField(frame.Self, m_Variable)) {
|
|
|
|
*parent = frame.Self;
|
|
|
|
|
|
|
|
if (dhint && *dhint)
|
|
|
|
*dhint = new DebugHint((*dhint)->GetChild(m_Variable));
|
|
|
|
} else if (ScriptGlobal::Exists(m_Variable)) {
|
|
|
|
*parent = ScriptGlobal::GetGlobals();
|
|
|
|
|
|
|
|
if (dhint)
|
|
|
|
*dhint = NULL;
|
|
|
|
} else
|
2014-12-18 11:21:11 +01:00
|
|
|
*parent = frame.Self;
|
2014-12-15 12:57:40 +01:00
|
|
|
|
|
|
|
return true;
|
2014-12-14 11:33:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult NegateExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand = m_Operand->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand);
|
|
|
|
|
|
|
|
return ~(long)operand.GetValue();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult LogicalNegateExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-28 13:25:40 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand = m_Operand->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand);
|
|
|
|
|
|
|
|
return !operand.GetValue().ToBool();
|
2014-03-28 13:25:40 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult AddExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() + operand2.GetValue();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult SubtractExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() - operand2.GetValue();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult MultiplyExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() * operand2.GetValue();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult DivideExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() / operand2.GetValue();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult ModuloExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-11-25 17:04:20 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() % operand2.GetValue();
|
2014-11-25 17:04:20 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult XorExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-11-25 17:04:20 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() ^ operand2.GetValue();
|
2014-11-25 17:04:20 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult BinaryAndExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() & operand2.GetValue();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult BinaryOrExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() | operand2.GetValue();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult ShiftLeftExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() << operand2.GetValue();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult ShiftRightExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() >> operand2.GetValue();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult EqualExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() == operand2.GetValue();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult NotEqualExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() != operand2.GetValue();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult LessThanExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() < operand2.GetValue();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult GreaterThanExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() > operand2.GetValue();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult LessThanOrEqualExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() <= operand2.GetValue();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult GreaterThanOrEqualExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand1.GetValue() >= operand2.GetValue();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult InExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2013-10-20 15:06:05 +02:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
2014-03-22 10:29:45 +01:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
if (operand2.GetValue().IsEmpty())
|
2014-03-28 12:17:22 +01:00
|
|
|
return false;
|
2015-02-19 12:57:52 +01:00
|
|
|
else if (!operand2.GetValue().IsObjectType<Array>())
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Invalid right side argument for 'in' operator: " + JsonEncode(operand2.GetValue()), m_DebugInfo));
|
2014-03-22 10:29:45 +01:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1)
|
2014-11-15 08:20:22 +01:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
Array::Ptr arr = operand2.GetValue();
|
|
|
|
return arr->Contains(operand1.GetValue());
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult NotInExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
2014-11-09 19:48:28 +01:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
if (operand2.GetValue().IsEmpty())
|
2014-11-23 09:52:17 +01:00
|
|
|
return true;
|
2015-02-19 12:57:52 +01:00
|
|
|
else if (!operand2.GetValue().IsObjectType<Array>())
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Invalid right side argument for 'in' operator: " + JsonEncode(operand2.GetValue()), m_DebugInfo));
|
2014-11-09 19:48:28 +01:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
2014-11-09 19:48:28 +01:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
Array::Ptr arr = operand2.GetValue();
|
|
|
|
return !arr->Contains(operand1.GetValue());
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult LogicalAndExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
2015-01-27 08:40:19 +01:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
if (!operand1.GetValue().ToBool())
|
|
|
|
return operand1;
|
|
|
|
else {
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand2.GetValue();
|
|
|
|
}
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult LogicalOrExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand1);
|
2015-01-27 08:40:19 +01:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
if (operand1.GetValue().ToBool())
|
|
|
|
return operand1;
|
|
|
|
else {
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
|
|
|
return operand2.GetValue();
|
|
|
|
}
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult FunctionCallExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-12-12 20:40:24 +01:00
|
|
|
Value self, vfunc;
|
2014-12-14 11:33:45 +01:00
|
|
|
String index;
|
2014-12-11 21:12:34 +01:00
|
|
|
|
2014-12-14 11:33:45 +01:00
|
|
|
if (m_FName->GetReference(frame, false, &self, &index))
|
2015-10-26 10:41:00 +01:00
|
|
|
vfunc = VMOps::GetField(self, index, frame.Sandboxed, m_DebugInfo);
|
2015-02-19 12:57:52 +01:00
|
|
|
else {
|
|
|
|
ExpressionResult vfuncres = m_FName->Evaluate(frame);
|
|
|
|
CHECK_RESULT(vfuncres);
|
|
|
|
|
|
|
|
vfunc = vfuncres.GetValue();
|
|
|
|
}
|
2014-12-12 20:40:24 +01:00
|
|
|
|
2015-03-21 22:48:23 +01:00
|
|
|
if (vfunc.IsObjectType<Type>()) {
|
|
|
|
if (m_Args.empty())
|
|
|
|
return VMOps::ConstructorCall(vfunc, m_DebugInfo);
|
|
|
|
else if (m_Args.size() == 1) {
|
|
|
|
ExpressionResult argres = m_Args[0]->Evaluate(frame);
|
|
|
|
CHECK_RESULT(argres);
|
|
|
|
|
|
|
|
return VMOps::CopyConstructorCall(vfunc, argres.GetValue(), m_DebugInfo);
|
|
|
|
} else
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Too many arguments for constructor.", m_DebugInfo));
|
|
|
|
}
|
|
|
|
|
2015-01-21 08:47:45 +01:00
|
|
|
if (!vfunc.IsObjectType<Function>())
|
2014-12-12 21:21:04 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Argument is not a callable object.", m_DebugInfo));
|
2014-12-12 20:40:24 +01:00
|
|
|
|
2015-01-21 08:47:45 +01:00
|
|
|
Function::Ptr func = vfunc;
|
2014-04-02 09:04:17 +02:00
|
|
|
|
2015-04-16 08:47:26 +02:00
|
|
|
if (!func->IsSideEffectFree() && frame.Sandboxed)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Function is not marked as safe for sandbox mode.", m_DebugInfo));
|
|
|
|
|
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) {
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult argres = arg->Evaluate(frame);
|
|
|
|
CHECK_RESULT(argres);
|
|
|
|
|
|
|
|
arguments.push_back(argres.GetValue());
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
2013-10-20 15:06:05 +02:00
|
|
|
|
2014-12-12 20:40:24 +01:00
|
|
|
return VMOps::FunctionCall(frame, self, func, arguments);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
2013-10-20 15:06:05 +02:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult ArrayExpression::DoEvaluate(ScriptFrame& 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) {
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult element = aexpr->Evaluate(frame);
|
|
|
|
CHECK_RESULT(element);
|
|
|
|
|
|
|
|
result->Add(element.GetValue());
|
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
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult DictExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-23 11:27:40 +01:00
|
|
|
{
|
2015-03-31 12:37:31 +02:00
|
|
|
Value self;
|
2014-03-23 11:27:40 +01:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
if (!m_Inline) {
|
2015-03-31 12:37:31 +02:00
|
|
|
self = frame.Self;
|
|
|
|
frame.Self = new Dictionary();
|
2014-11-22 12:21:28 +01:00
|
|
|
}
|
2014-03-24 09:10:37 +01:00
|
|
|
|
2014-11-23 09:53:28 +01:00
|
|
|
Value result;
|
|
|
|
|
2015-03-31 12:37:31 +02:00
|
|
|
try {
|
|
|
|
BOOST_FOREACH(Expression *aexpr, m_Expressions) {
|
|
|
|
ExpressionResult element = aexpr->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(element);
|
|
|
|
result = element.GetValue();
|
|
|
|
}
|
|
|
|
} catch (...) {
|
2015-07-30 20:22:02 +02:00
|
|
|
if (!m_Inline)
|
|
|
|
std::swap(self, frame.Self);
|
2015-03-31 12:37:31 +02:00
|
|
|
throw;
|
2014-03-23 11:27:40 +01:00
|
|
|
}
|
|
|
|
|
2014-11-23 09:53:28 +01:00
|
|
|
if (m_Inline)
|
|
|
|
return result;
|
2015-03-31 12:37:31 +02:00
|
|
|
else {
|
|
|
|
std::swap(self, frame.Self);
|
|
|
|
return self;
|
|
|
|
}
|
2014-03-23 11:27:40 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult GetScopeExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-12-14 11:33:45 +01:00
|
|
|
{
|
|
|
|
if (m_ScopeSpec == ScopeLocal)
|
|
|
|
return frame.Locals;
|
|
|
|
else if (m_ScopeSpec == ScopeThis)
|
|
|
|
return frame.Self;
|
|
|
|
else if (m_ScopeSpec == ScopeGlobal)
|
|
|
|
return ScriptGlobal::GetGlobals();
|
|
|
|
else
|
2015-03-04 08:26:15 +01:00
|
|
|
VERIFY(!"Invalid scope.");
|
2014-12-14 11:33:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult SetExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-23 11:27:40 +01:00
|
|
|
{
|
2015-04-16 08:47:26 +02:00
|
|
|
if (frame.Sandboxed)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Assignments are not allowed in sandbox mode.", m_DebugInfo));
|
|
|
|
|
2014-11-17 10:34:11 +01:00
|
|
|
DebugHint *psdhint = dhint;
|
2014-08-16 22:12:40 +02:00
|
|
|
|
2014-12-14 11:33:45 +01:00
|
|
|
Value parent;
|
2014-11-04 15:19:33 +01:00
|
|
|
String index;
|
2014-08-12 15:31:47 +02:00
|
|
|
|
2014-12-14 11:33:45 +01:00
|
|
|
if (!m_Operand1->GetReference(frame, true, &parent, &index, &psdhint))
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Expression cannot be assigned to.", m_DebugInfo));
|
2014-03-24 09:44:18 +01:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(operand2);
|
2014-08-12 15:31:47 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
if (m_Op != OpSetLiteral) {
|
2015-10-26 10:41:00 +01:00
|
|
|
Value object = VMOps::GetField(parent, index, frame.Sandboxed, m_DebugInfo);
|
2014-12-14 11:33:45 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
switch (m_Op) {
|
2014-11-04 15:19:33 +01:00
|
|
|
case OpSetAdd:
|
2015-09-23 10:36:57 +02:00
|
|
|
operand2 = object + operand2;
|
2014-11-04 15:19:33 +01:00
|
|
|
break;
|
|
|
|
case OpSetSubtract:
|
2015-09-23 10:36:57 +02:00
|
|
|
operand2 = object - operand2;
|
2014-11-04 15:19:33 +01:00
|
|
|
break;
|
|
|
|
case OpSetMultiply:
|
2015-09-23 10:36:57 +02:00
|
|
|
operand2 = object * operand2;
|
2014-11-04 15:19:33 +01:00
|
|
|
break;
|
|
|
|
case OpSetDivide:
|
2015-09-23 10:36:57 +02:00
|
|
|
operand2 = object / operand2;
|
2014-11-04 15:19:33 +01:00
|
|
|
break;
|
2014-11-25 17:04:20 +01:00
|
|
|
case OpSetModulo:
|
2015-09-23 10:36:57 +02:00
|
|
|
operand2 = object % operand2;
|
2014-11-25 17:04:20 +01:00
|
|
|
break;
|
|
|
|
case OpSetXor:
|
2015-09-23 10:36:57 +02:00
|
|
|
operand2 = object ^ operand2;
|
2014-11-25 17:04:20 +01:00
|
|
|
break;
|
|
|
|
case OpSetBinaryAnd:
|
2015-09-23 10:36:57 +02:00
|
|
|
operand2 = object & operand2;
|
2014-11-25 17:04:20 +01:00
|
|
|
break;
|
|
|
|
case OpSetBinaryOr:
|
2015-09-23 10:36:57 +02:00
|
|
|
operand2 = object | operand2;
|
2014-11-25 17:04:20 +01:00
|
|
|
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
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
VMOps::SetField(parent, index, operand2.GetValue(), m_DebugInfo);
|
2014-08-16 22:12:40 +02:00
|
|
|
|
2014-12-14 11:33:45 +01:00
|
|
|
if (psdhint) {
|
2014-11-17 10:34:11 +01:00
|
|
|
psdhint->AddMessage("=", m_DebugInfo);
|
2014-08-12 15:31:47 +02:00
|
|
|
|
2014-12-14 11:33:45 +01:00
|
|
|
if (psdhint != dhint)
|
|
|
|
delete psdhint;
|
|
|
|
}
|
|
|
|
|
2015-01-12 08:55:16 +01:00
|
|
|
return Empty;
|
2014-03-23 11:27:40 +01:00
|
|
|
}
|
2014-03-23 17:26:31 +01:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult ConditionalExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-11-24 09:53:45 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult condition = m_Condition->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(condition);
|
|
|
|
|
|
|
|
if (condition.GetValue().ToBool())
|
2014-11-24 09:53:45 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult WhileExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2015-01-23 15:54:27 +01:00
|
|
|
{
|
2015-04-16 08:47:26 +02:00
|
|
|
if (frame.Sandboxed)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("While loops are not allowed in sandbox mode.", m_DebugInfo));
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
for (;;) {
|
|
|
|
ExpressionResult condition = m_Condition->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(condition);
|
|
|
|
|
|
|
|
if (!condition.GetValue().ToBool())
|
|
|
|
break;
|
|
|
|
|
|
|
|
ExpressionResult loop_body = m_LoopBody->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT_LOOP(loop_body);
|
|
|
|
}
|
2015-01-23 15:54:27 +01:00
|
|
|
|
|
|
|
return Empty;
|
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult ReturnExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
|
|
|
{
|
|
|
|
ExpressionResult operand = m_Operand->Evaluate(frame);
|
|
|
|
CHECK_RESULT(operand);
|
|
|
|
|
|
|
|
return ExpressionResult(operand.GetValue(), ResultReturn);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExpressionResult BreakExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
|
|
|
{
|
|
|
|
return ExpressionResult(Empty, ResultBreak);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExpressionResult ContinueExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-11-24 00:04:26 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
return ExpressionResult(Empty, ResultContinue);
|
2014-11-24 00:04:26 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult IndexerExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-23 17:26:31 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(operand1);
|
|
|
|
|
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(operand2);
|
|
|
|
|
2015-10-26 10:41:00 +01:00
|
|
|
return VMOps::GetField(operand1.GetValue(), operand2.GetValue(), frame.Sandboxed, m_DebugInfo);
|
2014-12-14 11:33:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IndexerExpression::GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const
|
|
|
|
{
|
|
|
|
Value vparent;
|
|
|
|
String vindex;
|
2015-03-04 08:00:38 +01:00
|
|
|
DebugHint *psdhint = NULL;
|
2015-03-05 13:18:15 +01:00
|
|
|
bool free_psd = false;
|
|
|
|
|
|
|
|
if (dhint)
|
|
|
|
psdhint = *dhint;
|
2014-12-14 11:33:45 +01:00
|
|
|
|
2015-04-16 08:47:26 +02:00
|
|
|
if (frame.Sandboxed)
|
|
|
|
init_dict = false;
|
|
|
|
|
2015-03-04 08:00:38 +01:00
|
|
|
if (m_Operand1->GetReference(frame, init_dict, &vparent, &vindex, &psdhint)) {
|
2015-08-05 07:45:10 +02:00
|
|
|
if (init_dict) {
|
2015-10-26 10:41:00 +01:00
|
|
|
Value old_value = VMOps::GetField(vparent, vindex, frame.Sandboxed, m_Operand1->GetDebugInfo());
|
2015-08-05 07:45:10 +02:00
|
|
|
|
|
|
|
if (old_value.IsEmpty() && !old_value.IsString())
|
|
|
|
VMOps::SetField(vparent, vindex, new Dictionary(), m_Operand1->GetDebugInfo());
|
|
|
|
}
|
2014-12-14 11:33:45 +01:00
|
|
|
|
2015-10-26 10:41:00 +01:00
|
|
|
*parent = VMOps::GetField(vparent, vindex, frame.Sandboxed, m_DebugInfo);
|
2015-03-05 13:18:15 +01:00
|
|
|
free_psd = true;
|
2015-02-19 12:57:52 +01:00
|
|
|
} else {
|
|
|
|
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
|
|
|
|
*parent = operand1.GetValue();
|
|
|
|
}
|
2014-12-14 11:33:45 +01:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
|
|
|
|
*index = operand2.GetValue();
|
2014-12-14 11:33:45 +01:00
|
|
|
|
2015-10-26 18:00:42 +01:00
|
|
|
if (dhint) {
|
|
|
|
if (psdhint)
|
|
|
|
*dhint = new DebugHint(psdhint->GetChild(*index));
|
|
|
|
else
|
|
|
|
*dhint = NULL;
|
|
|
|
}
|
2015-03-04 08:00:38 +01:00
|
|
|
|
2015-03-05 13:18:15 +01:00
|
|
|
if (free_psd)
|
|
|
|
delete psdhint;
|
2014-12-14 11:33:45 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void icinga::BindToScope(Expression *& expr, ScopeSpecifier scopeSpec)
|
|
|
|
{
|
2014-12-15 12:57:40 +01:00
|
|
|
DictExpression *dexpr = dynamic_cast<DictExpression *>(expr);
|
|
|
|
|
|
|
|
if (dexpr) {
|
|
|
|
BOOST_FOREACH(Expression *& expr, dexpr->m_Expressions)
|
|
|
|
BindToScope(expr, scopeSpec);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetExpression *aexpr = dynamic_cast<SetExpression *>(expr);
|
|
|
|
|
|
|
|
if (aexpr) {
|
|
|
|
BindToScope(aexpr->m_Operand1, scopeSpec);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-14 11:33:45 +01:00
|
|
|
IndexerExpression *iexpr = dynamic_cast<IndexerExpression *>(expr);
|
|
|
|
|
|
|
|
if (iexpr) {
|
|
|
|
BindToScope(iexpr->m_Operand1, scopeSpec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LiteralExpression *lexpr = dynamic_cast<LiteralExpression *>(expr);
|
2014-12-14 11:55:44 +01:00
|
|
|
ScriptFrame frame;
|
2014-12-14 11:33:45 +01:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
if (lexpr && lexpr->Evaluate(frame).GetValue().IsString()) {
|
2014-12-14 11:33:45 +01:00
|
|
|
Expression *scope = new GetScopeExpression(scopeSpec);
|
|
|
|
expr = new IndexerExpression(scope, lexpr, lexpr->GetDebugInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
VariableExpression *vexpr = dynamic_cast<VariableExpression *>(expr);
|
|
|
|
|
|
|
|
if (vexpr) {
|
|
|
|
Expression *scope = new GetScopeExpression(scopeSpec);
|
|
|
|
Expression *new_expr = new IndexerExpression(scope, MakeLiteral(vexpr->GetVariable()), vexpr->GetDebugInfo());
|
|
|
|
delete expr;
|
|
|
|
expr = new_expr;
|
|
|
|
}
|
2014-03-23 17:26:31 +01:00
|
|
|
}
|
2014-03-27 12:30:24 +01:00
|
|
|
|
2015-08-24 11:04:26 +02:00
|
|
|
ExpressionResult ThrowExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
|
|
|
{
|
|
|
|
ExpressionResult messageres = m_Message->Evaluate(frame);
|
|
|
|
CHECK_RESULT(messageres);
|
|
|
|
Value message = messageres.GetValue();
|
2015-10-26 13:04:03 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError(message, m_DebugInfo, m_IncompleteExpr));
|
2015-08-24 11:04:26 +02:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult ImportExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-27 12:30:24 +01:00
|
|
|
{
|
2015-04-16 08:47:26 +02:00
|
|
|
if (frame.Sandboxed)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Imports are not allowed in sandbox mode.", m_DebugInfo));
|
|
|
|
|
2015-10-26 10:41:00 +01:00
|
|
|
String type = VMOps::GetField(frame.Self, "type", frame.Sandboxed, m_DebugInfo);
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult nameres = m_Name->Evaluate(frame);
|
|
|
|
CHECK_RESULT(nameres);
|
|
|
|
Value name = nameres.GetValue();
|
2014-03-27 12:30:24 +01:00
|
|
|
|
2014-12-20 11:09:21 +01:00
|
|
|
if (!name.IsString())
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Template/object name must be a string", m_DebugInfo));
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
ConfigItem::Ptr item = ConfigItem::GetByTypeAndName(type, name);
|
2014-03-27 12:30:24 +01:00
|
|
|
|
|
|
|
if (!item)
|
2014-12-12 21:21:04 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Import references unknown template: '" + name + "'", m_DebugInfo));
|
2014-03-27 12:30:24 +01:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult result = item->GetExpression()->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(result);
|
2014-03-27 12:30:24 +01:00
|
|
|
|
|
|
|
return Empty;
|
|
|
|
}
|
2014-03-30 15:04:53 +02:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult FunctionExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-30 15:04:53 +02:00
|
|
|
{
|
2014-12-13 17:48:58 +01:00
|
|
|
return VMOps::NewFunction(frame, m_Args, m_ClosedVars, m_Expression);
|
2014-11-20 06:53:57 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult ApplyExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-30 15:04:53 +02:00
|
|
|
{
|
2015-04-16 08:47:26 +02:00
|
|
|
if (frame.Sandboxed)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Apply rules are not allowed in sandbox mode.", m_DebugInfo));
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult nameres = m_Name->Evaluate(frame);
|
|
|
|
CHECK_RESULT(nameres);
|
|
|
|
|
|
|
|
return VMOps::NewApply(frame, m_Type, m_Target, nameres.GetValue(), m_Filter,
|
2015-10-05 12:44:11 +02:00
|
|
|
m_Package, m_FKVar, m_FVVar, m_FTerm, m_ClosedVars, m_IgnoreOnError, m_Expression, m_DebugInfo);
|
2014-03-30 15:04:53 +02:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult ObjectExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-03-30 15:04:53 +02:00
|
|
|
{
|
2015-04-16 08:47:26 +02:00
|
|
|
if (frame.Sandboxed)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Object definitions are not allowed in sandbox mode.", m_DebugInfo));
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
String name;
|
2014-03-30 15:04:53 +02:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
if (m_Name) {
|
|
|
|
ExpressionResult nameres = m_Name->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(nameres);
|
|
|
|
|
|
|
|
name = nameres.GetValue();
|
|
|
|
}
|
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,
|
2015-10-05 12:44:11 +02:00
|
|
|
m_Package, m_IgnoreOnError, m_ClosedVars, m_Expression, m_DebugInfo);
|
2014-04-02 09:04:17 +02:00
|
|
|
}
|
2014-05-10 11:26:56 +02:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult ForExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
2014-05-10 11:26:56 +02:00
|
|
|
{
|
2015-04-16 08:47:26 +02:00
|
|
|
if (frame.Sandboxed)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("For loops are not allowed in sandbox mode.", m_DebugInfo));
|
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult valueres = m_Value->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(valueres);
|
2014-05-10 11:26:56 +02:00
|
|
|
|
2015-02-19 12:57:52 +01:00
|
|
|
return VMOps::For(frame, m_FKVar, m_FVVar, valueres.GetValue(), m_Expression, m_DebugInfo);
|
2014-11-06 19:35:47 +01:00
|
|
|
}
|
|
|
|
|
2015-08-27 14:47:31 +02:00
|
|
|
ExpressionResult LibraryExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
|
|
|
{
|
|
|
|
if (frame.Sandboxed)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Loading libraries is not allowed in sandbox mode.", m_DebugInfo));
|
|
|
|
|
|
|
|
ExpressionResult libres = m_Operand->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(libres);
|
|
|
|
|
|
|
|
Loader::LoadExtensionLibrary(libres.GetValue());
|
|
|
|
|
|
|
|
return Empty;
|
|
|
|
}
|
|
|
|
|
2015-10-22 09:46:31 +02:00
|
|
|
ExpressionResult IncludeExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
|
|
|
{
|
|
|
|
if (frame.Sandboxed)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Includes are not allowed in sandbox mode.", m_DebugInfo));
|
|
|
|
|
|
|
|
Expression *expr;
|
|
|
|
String name, path, pattern;
|
|
|
|
|
|
|
|
switch (m_Type) {
|
|
|
|
case IncludeRegular:
|
|
|
|
{
|
|
|
|
ExpressionResult pathres = m_Path->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(pathres);
|
|
|
|
path = pathres.GetValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
expr = ConfigCompiler::HandleInclude(m_RelativeBase, path, m_SearchIncludes, m_Zone, m_Package, m_DebugInfo);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IncludeRecursive:
|
|
|
|
{
|
|
|
|
ExpressionResult pathres = m_Path->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(pathres);
|
|
|
|
path = pathres.GetValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ExpressionResult patternres = m_Pattern->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(patternres);
|
|
|
|
pattern = patternres.GetValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
expr = ConfigCompiler::HandleIncludeRecursive(m_RelativeBase, path, pattern, m_Zone, m_Package, m_DebugInfo);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IncludeZones:
|
|
|
|
{
|
|
|
|
ExpressionResult nameres = m_Name->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(nameres);
|
|
|
|
name = nameres.GetValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ExpressionResult pathres = m_Path->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(pathres);
|
|
|
|
path = pathres.GetValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ExpressionResult patternres = m_Pattern->Evaluate(frame, dhint);
|
|
|
|
CHECK_RESULT(patternres);
|
|
|
|
pattern = patternres.GetValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
expr = ConfigCompiler::HandleIncludeZones(m_RelativeBase, name, path, pattern, m_Package, m_DebugInfo);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExpressionResult res(Empty);
|
|
|
|
|
|
|
|
try {
|
|
|
|
res = expr->Evaluate(frame, dhint);
|
|
|
|
} catch (const std::exception&) {
|
|
|
|
delete expr;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2015-11-05 14:29:45 +01:00
|
|
|
|
|
|
|
ExpressionResult BreakpointExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
|
|
|
{
|
|
|
|
ScriptBreakpoint(frame, NULL, GetDebugInfo());
|
|
|
|
|
|
|
|
return Empty;
|
|
|
|
}
|
|
|
|
|