icinga2/lib/config/expression.cpp

894 lines
25 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2016-01-12 08:29:59 +01:00
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
* *
* 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"
#include "config/configcompiler.hpp"
#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"
#include "base/exception.hpp"
#include "base/scriptglobal.hpp"
#include "base/loader.hpp"
#include <boost/foreach.hpp>
#include <boost/exception_ptr.hpp>
#include <boost/exception/errinfo_nested_exception.hpp>
using namespace icinga;
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)
{ }
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;
}
}
ExpressionResult Expression::Evaluate(ScriptFrame& frame, DebugHint *dhint) const
{
try {
2014-12-19 12:19:28 +01:00
#ifdef I2_DEBUG
/* std::ostringstream msgbuf;
ShowCodeLocation(msgbuf, GetDebugInfo(), false);
2014-11-09 19:48:28 +01:00
Log(LogDebug, "Expression")
<< "Executing:\n" << msgbuf.str();*/
2014-12-19 12:19:28 +01:00
#endif /* I2_DEBUG */
frame.IncreaseStackDepth();
ExpressionResult result = DoEvaluate(frame, dhint);
frame.DecreaseStackDepth();
return result;
} catch (ScriptError& ex) {
frame.DecreaseStackDepth();
ScriptBreakpoint(frame, &ex, GetDebugInfo());
throw;
} catch (const std::exception& ex) {
frame.DecreaseStackDepth();
BOOST_THROW_EXCEPTION(ScriptError("Error while evaluating expression: " + String(ex.what()), GetDebugInfo())
<< boost::errinfo_nested_exception(boost::current_exception()));
}
frame.DecreaseStackDepth();
}
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-11-09 19:48:28 +01:00
static DebugInfo debugInfo;
return debugInfo;
}
Expression *icinga::MakeIndexer(ScopeSpecifier scopeSpec, const String& index)
{
Expression *scope = new GetScopeExpression(scopeSpec);
return new IndexerExpression(scope, MakeLiteral(index));
}
2014-11-09 19:48:28 +01:00
void DictExpression::MakeInline(void)
{
2014-11-09 19:48:28 +01:00
m_Inline = true;
}
2014-11-09 19:48:28 +01:00
LiteralExpression::LiteralExpression(const Value& value)
: m_Value(value)
{ }
ExpressionResult LiteralExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Value;
}
2014-11-09 19:48:28 +01:00
const DebugInfo& DebuggableExpression::GetDebugInfo(void) const
{
2014-11-09 19:48:28 +01:00
return m_DebugInfo;
}
ExpressionResult VariableExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
Value value;
if (frame.Locals && frame.Locals->Get(m_Variable, &value))
return value;
else if (frame.Self.IsObject() && frame.Locals != static_cast<Object::Ptr>(frame.Self) && static_cast<Object::Ptr>(frame.Self)->HasOwnField(m_Variable))
return VMOps::GetField(frame.Self, m_Variable, frame.Sandboxed, m_DebugInfo);
else
return ScriptGlobal::Get(m_Variable);
}
bool VariableExpression::GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const
{
*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) && static_cast<Object::Ptr>(frame.Self)->HasOwnField(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
*parent = frame.Self;
return true;
}
ExpressionResult NegateExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand = m_Operand->Evaluate(frame);
CHECK_RESULT(operand);
return ~(long)operand.GetValue();
}
ExpressionResult LogicalNegateExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand = m_Operand->Evaluate(frame);
CHECK_RESULT(operand);
return !operand.GetValue().ToBool();
}
ExpressionResult AddExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() + operand2.GetValue();
}
ExpressionResult SubtractExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() - operand2.GetValue();
}
ExpressionResult MultiplyExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() * operand2.GetValue();
}
ExpressionResult DivideExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() / operand2.GetValue();
}
ExpressionResult ModuloExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() % operand2.GetValue();
}
ExpressionResult XorExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() ^ operand2.GetValue();
}
ExpressionResult BinaryAndExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() & operand2.GetValue();
}
ExpressionResult BinaryOrExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() | operand2.GetValue();
}
ExpressionResult ShiftLeftExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() << operand2.GetValue();
}
ExpressionResult ShiftRightExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() >> operand2.GetValue();
}
ExpressionResult EqualExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() == operand2.GetValue();
}
ExpressionResult NotEqualExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() != operand2.GetValue();
}
ExpressionResult LessThanExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() < operand2.GetValue();
}
ExpressionResult GreaterThanExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() > operand2.GetValue();
}
ExpressionResult LessThanOrEqualExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() <= operand2.GetValue();
}
ExpressionResult GreaterThanOrEqualExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand1.GetValue() >= operand2.GetValue();
}
ExpressionResult InExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
if (operand2.GetValue().IsEmpty())
2014-03-28 12:17:22 +01:00
return false;
else if (!operand2.GetValue().IsObjectType<Array>())
BOOST_THROW_EXCEPTION(ScriptError("Invalid right side argument for 'in' operator: " + JsonEncode(operand2.GetValue()), m_DebugInfo));
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1)
Array::Ptr arr = operand2.GetValue();
return arr->Contains(operand1.GetValue());
}
ExpressionResult NotInExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
2014-11-09 19:48:28 +01:00
if (operand2.GetValue().IsEmpty())
return true;
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
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
2014-11-09 19:48:28 +01:00
Array::Ptr arr = operand2.GetValue();
return !arr->Contains(operand1.GetValue());
}
ExpressionResult LogicalAndExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
if (!operand1.GetValue().ToBool())
return operand1;
else {
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand2.GetValue();
}
}
ExpressionResult LogicalOrExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
CHECK_RESULT(operand1);
if (operand1.GetValue().ToBool())
return operand1;
else {
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
CHECK_RESULT(operand2);
return operand2.GetValue();
}
}
ExpressionResult FunctionCallExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
Value self, vfunc;
String index;
if (m_FName->GetReference(frame, false, &self, &index))
vfunc = VMOps::GetField(self, index, frame.Sandboxed, m_DebugInfo);
else {
ExpressionResult vfuncres = m_FName->Evaluate(frame);
CHECK_RESULT(vfuncres);
vfunc = vfuncres.GetValue();
}
if (vfunc.IsObjectType<Type>()) {
std::vector<Value> arguments;
BOOST_FOREACH(Expression *arg, m_Args) {
ExpressionResult argres = arg->Evaluate(frame);
CHECK_RESULT(argres);
arguments.push_back(argres.GetValue());
}
return VMOps::ConstructorCall(vfunc, arguments, m_DebugInfo);
}
if (!vfunc.IsObjectType<Function>())
BOOST_THROW_EXCEPTION(ScriptError("Argument is not a callable object.", m_DebugInfo));
Function::Ptr func = vfunc;
if (!func->IsSideEffectFree() && frame.Sandboxed)
BOOST_THROW_EXCEPTION(ScriptError("Function is not marked as safe for sandbox mode.", m_DebugInfo));
std::vector<Value> arguments;
2014-11-09 19:48:28 +01:00
BOOST_FOREACH(Expression *arg, m_Args) {
ExpressionResult argres = arg->Evaluate(frame);
CHECK_RESULT(argres);
arguments.push_back(argres.GetValue());
}
return VMOps::FunctionCall(frame, self, func, arguments);
}
ExpressionResult ArrayExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
Array::Ptr result = new Array();
2014-11-09 19:48:28 +01:00
BOOST_FOREACH(Expression *aexpr, m_Expressions) {
ExpressionResult element = aexpr->Evaluate(frame);
CHECK_RESULT(element);
result->Add(element.GetValue());
}
return result;
2013-11-03 10:41:30 +01:00
}
ExpressionResult DictExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
Value self;
if (!m_Inline) {
self = frame.Self;
frame.Self = new Dictionary();
}
2014-03-24 09:10:37 +01:00
Value result;
try {
BOOST_FOREACH(Expression *aexpr, m_Expressions) {
ExpressionResult element = aexpr->Evaluate(frame, dhint);
CHECK_RESULT(element);
result = element.GetValue();
}
} catch (...) {
if (!m_Inline)
std::swap(self, frame.Self);
throw;
}
if (m_Inline)
return result;
else {
std::swap(self, frame.Self);
return self;
}
}
ExpressionResult GetScopeExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
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.");
}
ExpressionResult SetExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
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;
Value parent;
String index;
if (!m_Operand1->GetReference(frame, true, &parent, &index, &psdhint))
BOOST_THROW_EXCEPTION(ScriptError("Expression cannot be assigned to.", m_DebugInfo));
ExpressionResult operand2 = m_Operand2->Evaluate(frame, dhint);
CHECK_RESULT(operand2);
2014-11-09 19:48:28 +01:00
if (m_Op != OpSetLiteral) {
Value object = VMOps::GetField(parent, index, frame.Sandboxed, m_DebugInfo);
2014-11-09 19:48:28 +01:00
switch (m_Op) {
case OpSetAdd:
operand2 = object + operand2;
break;
case OpSetSubtract:
operand2 = object - operand2;
break;
case OpSetMultiply:
operand2 = object * operand2;
break;
case OpSetDivide:
operand2 = object / operand2;
break;
case OpSetModulo:
operand2 = object % operand2;
break;
case OpSetXor:
operand2 = object ^ operand2;
break;
case OpSetBinaryAnd:
operand2 = object & operand2;
break;
case OpSetBinaryOr:
operand2 = object | operand2;
break;
default:
VERIFY(!"Invalid opcode.");
}
}
VMOps::SetField(parent, index, operand2.GetValue(), m_DebugInfo);
if (psdhint) {
2014-11-17 10:34:11 +01:00
psdhint->AddMessage("=", m_DebugInfo);
if (psdhint != dhint)
delete psdhint;
}
return Empty;
}
ExpressionResult ConditionalExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult condition = m_Condition->Evaluate(frame, dhint);
CHECK_RESULT(condition);
if (condition.GetValue().ToBool())
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;
}
ExpressionResult WhileExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
if (frame.Sandboxed)
BOOST_THROW_EXCEPTION(ScriptError("While loops are not allowed in sandbox mode.", m_DebugInfo));
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);
}
return Empty;
}
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
{
return ExpressionResult(Empty, ResultContinue);
2014-11-24 00:04:26 +01:00
}
ExpressionResult IndexerExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult operand1 = m_Operand1->Evaluate(frame, dhint);
CHECK_RESULT(operand1);
ExpressionResult operand2 = m_Operand2->Evaluate(frame, dhint);
CHECK_RESULT(operand2);
return VMOps::GetField(operand1.GetValue(), operand2.GetValue(), frame.Sandboxed, m_DebugInfo);
}
bool IndexerExpression::GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const
{
Value vparent;
String vindex;
DebugHint *psdhint = NULL;
bool free_psd = false;
if (dhint)
psdhint = *dhint;
if (frame.Sandboxed)
init_dict = false;
if (m_Operand1->GetReference(frame, init_dict, &vparent, &vindex, &psdhint)) {
if (init_dict) {
Value old_value = VMOps::GetField(vparent, vindex, frame.Sandboxed, m_Operand1->GetDebugInfo());
if (old_value.IsEmpty() && !old_value.IsString())
VMOps::SetField(vparent, vindex, new Dictionary(), m_Operand1->GetDebugInfo());
}
*parent = VMOps::GetField(vparent, vindex, frame.Sandboxed, m_DebugInfo);
free_psd = true;
} else {
ExpressionResult operand1 = m_Operand1->Evaluate(frame);
*parent = operand1.GetValue();
}
ExpressionResult operand2 = m_Operand2->Evaluate(frame);
*index = operand2.GetValue();
if (dhint) {
if (psdhint)
*dhint = new DebugHint(psdhint->GetChild(*index));
else
*dhint = NULL;
}
if (free_psd)
delete psdhint;
return true;
}
void icinga::BindToScope(Expression *& expr, ScopeSpecifier scopeSpec)
{
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;
}
IndexerExpression *iexpr = dynamic_cast<IndexerExpression *>(expr);
if (iexpr) {
BindToScope(iexpr->m_Operand1, scopeSpec);
return;
}
LiteralExpression *lexpr = dynamic_cast<LiteralExpression *>(expr);
ScriptFrame frame;
if (lexpr && lexpr->Evaluate(frame).GetValue().IsString()) {
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;
}
}
ExpressionResult ThrowExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ExpressionResult messageres = m_Message->Evaluate(frame);
CHECK_RESULT(messageres);
Value message = messageres.GetValue();
BOOST_THROW_EXCEPTION(ScriptError(message, m_DebugInfo, m_IncompleteExpr));
}
ExpressionResult ImportExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
if (frame.Sandboxed)
BOOST_THROW_EXCEPTION(ScriptError("Imports are not allowed in sandbox mode.", m_DebugInfo));
String type = VMOps::GetField(frame.Self, "type", frame.Sandboxed, m_DebugInfo);
ExpressionResult nameres = m_Name->Evaluate(frame);
CHECK_RESULT(nameres);
Value name = nameres.GetValue();
if (!name.IsString())
BOOST_THROW_EXCEPTION(ScriptError("Template/object name must be a string", m_DebugInfo));
ConfigItem::Ptr item = ConfigItem::GetByTypeAndName(type, name);
if (!item)
BOOST_THROW_EXCEPTION(ScriptError("Import references unknown template: '" + name + "'", m_DebugInfo));
ExpressionResult result = item->GetExpression()->Evaluate(frame, dhint);
CHECK_RESULT(result);
return Empty;
}
ExpressionResult FunctionExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
return VMOps::NewFunction(frame, m_Args, m_ClosedVars, m_Expression);
}
ExpressionResult ApplyExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
if (frame.Sandboxed)
BOOST_THROW_EXCEPTION(ScriptError("Apply rules are not allowed in sandbox mode.", m_DebugInfo));
ExpressionResult nameres = m_Name->Evaluate(frame);
CHECK_RESULT(nameres);
return VMOps::NewApply(frame, m_Type, m_Target, nameres.GetValue(), m_Filter,
m_Package, m_FKVar, m_FVVar, m_FTerm, m_ClosedVars, m_IgnoreOnError, m_Expression, m_DebugInfo);
}
ExpressionResult ObjectExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
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;
if (m_Name) {
ExpressionResult nameres = m_Name->Evaluate(frame, dhint);
CHECK_RESULT(nameres);
name = nameres.GetValue();
}
return VMOps::NewObject(frame, m_Abstract, m_Type, name, m_Filter, m_Zone,
m_Package, m_IgnoreOnError, m_ClosedVars, m_Expression, m_DebugInfo);
}
2014-05-10 11:26:56 +02:00
ExpressionResult ForExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
2014-05-10 11:26:56 +02:00
{
if (frame.Sandboxed)
BOOST_THROW_EXCEPTION(ScriptError("For loops are not allowed in sandbox mode.", m_DebugInfo));
ExpressionResult valueres = m_Value->Evaluate(frame, dhint);
CHECK_RESULT(valueres);
2014-05-10 11:26:56 +02:00
return VMOps::For(frame, m_FKVar, m_FVVar, valueres.GetValue(), m_Expression, m_DebugInfo);
2014-11-06 19:35:47 +01: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;
}
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;
}
ExpressionResult BreakpointExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
ScriptBreakpoint(frame, NULL, GetDebugInfo());
return Empty;
}