mirror of https://github.com/Icinga/icinga2.git
parent
ce66a0b649
commit
a9e2bc4b40
|
@ -26,7 +26,6 @@
|
||||||
#include "base/utility.hpp"
|
#include "base/utility.hpp"
|
||||||
#include "base/json.hpp"
|
#include "base/json.hpp"
|
||||||
#include "base/objectlock.hpp"
|
#include "base/objectlock.hpp"
|
||||||
#include "base/scriptfunction.hpp"
|
|
||||||
#include <mmatch.h>
|
#include <mmatch.h>
|
||||||
#include <boost/lexical_cast.hpp>
|
#include <boost/lexical_cast.hpp>
|
||||||
#include <boost/foreach.hpp>
|
#include <boost/foreach.hpp>
|
||||||
|
@ -58,9 +57,6 @@ using namespace icinga;
|
||||||
boost::thread_specific_ptr<String> Utility::m_ThreadName;
|
boost::thread_specific_ptr<String> Utility::m_ThreadName;
|
||||||
boost::thread_specific_ptr<unsigned int> Utility::m_RandSeed;
|
boost::thread_specific_ptr<unsigned int> Utility::m_RandSeed;
|
||||||
|
|
||||||
REGISTER_SCRIPTFUNCTION(escape, &Utility::EscapeString);
|
|
||||||
REGISTER_SCRIPTFUNCTION(unescape, &Utility::UnescapeString);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Demangles a symbol name.
|
* Demangles a symbol name.
|
||||||
*
|
*
|
||||||
|
|
|
@ -35,6 +35,7 @@ set(config_SOURCES
|
||||||
configcompilercontext.cpp configcompiler.cpp configitembuilder.cpp
|
configcompilercontext.cpp configcompiler.cpp configitembuilder.cpp
|
||||||
configitem.cpp ${FLEX_config_lexer_OUTPUTS} ${BISON_config_parser_OUTPUTS}
|
configitem.cpp ${FLEX_config_lexer_OUTPUTS} ${BISON_config_parser_OUTPUTS}
|
||||||
configtype.cpp expression.cpp objectrule.cpp typerule.cpp typerulelist.cpp
|
configtype.cpp expression.cpp objectrule.cpp typerule.cpp typerulelist.cpp
|
||||||
|
vmframe.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
if(ICINGA2_UNITY_BUILD)
|
if(ICINGA2_UNITY_BUILD)
|
||||||
|
|
|
@ -787,9 +787,15 @@ rterm_without_indexer: T_STRING
|
||||||
{
|
{
|
||||||
$$ = MakeLiteral();
|
$$ = MakeLiteral();
|
||||||
}
|
}
|
||||||
|
| indexer '(' rterm_items ')'
|
||||||
|
{
|
||||||
|
$$ = new FunctionCallExpression(*$1, NULL, *$3, DebugInfoRange(@1, @4));
|
||||||
|
delete $1;
|
||||||
|
delete $3;
|
||||||
|
}
|
||||||
| rterm '(' rterm_items ')'
|
| rterm '(' rterm_items ')'
|
||||||
{
|
{
|
||||||
$$ = new FunctionCallExpression($1, *$3, DebugInfoRange(@1, @4));
|
$$ = new FunctionCallExpression(MakeIndexer("this"), $1, *$3, DebugInfoRange(@1, @4));
|
||||||
delete $3;
|
delete $3;
|
||||||
}
|
}
|
||||||
| T_IDENTIFIER
|
| T_IDENTIFIER
|
||||||
|
|
|
@ -224,14 +224,46 @@ Value LogicalOrExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
||||||
|
|
||||||
Value FunctionCallExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
Value FunctionCallExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
||||||
{
|
{
|
||||||
Value funcName = m_FName->Evaluate(frame);
|
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);
|
||||||
|
|
||||||
std::vector<Value> arguments;
|
std::vector<Value> arguments;
|
||||||
BOOST_FOREACH(Expression *arg, m_Args) {
|
BOOST_FOREACH(Expression *arg, m_Args) {
|
||||||
arguments.push_back(arg->Evaluate(frame));
|
arguments.push_back(arg->Evaluate(frame));
|
||||||
}
|
}
|
||||||
|
|
||||||
return VMOps::FunctionCall(frame, funcName, arguments);
|
return VMOps::FunctionCall(frame, self, funcName, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value ArrayExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
Value ArrayExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
|
||||||
|
|
|
@ -516,12 +516,15 @@ protected:
|
||||||
class I2_CONFIG_API FunctionCallExpression : public DebuggableExpression
|
class I2_CONFIG_API FunctionCallExpression : public DebuggableExpression
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
FunctionCallExpression(Expression *fname, const std::vector<Expression *>& args, const DebugInfo& debugInfo = DebugInfo())
|
FunctionCallExpression(const std::vector<Expression *> iname, Expression *fname, const std::vector<Expression *>& args, const DebugInfo& debugInfo = DebugInfo())
|
||||||
: DebuggableExpression(debugInfo), m_FName(fname), m_Args(args)
|
: DebuggableExpression(debugInfo), m_IName(iname), m_FName(fname), m_Args(args)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
~FunctionCallExpression(void)
|
~FunctionCallExpression(void)
|
||||||
{
|
{
|
||||||
|
BOOST_FOREACH(Expression *expr, m_IName)
|
||||||
|
delete expr;
|
||||||
|
|
||||||
delete m_FName;
|
delete m_FName;
|
||||||
|
|
||||||
BOOST_FOREACH(Expression *expr, m_Args)
|
BOOST_FOREACH(Expression *expr, m_Args)
|
||||||
|
@ -532,6 +535,7 @@ protected:
|
||||||
virtual Value DoEvaluate(VMFrame& frame, DebugHint *dhint) const;
|
virtual Value DoEvaluate(VMFrame& frame, DebugHint *dhint) const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
std::vector<Expression *> m_IName;
|
||||||
Expression *m_FName;
|
Expression *m_FName;
|
||||||
std::vector<Expression *> m_Args;
|
std::vector<Expression *> m_Args;
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
/******************************************************************************
|
||||||
|
* Icinga 2 *
|
||||||
|
* Copyright (C) 2012-2014 Icinga Development Team (http://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. *
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include "config/vmframe.hpp"
|
||||||
|
|
||||||
|
using namespace icinga;
|
||||||
|
|
||||||
|
boost::thread_specific_ptr<VMFrame *> VMFrame::m_CurrentFrame;
|
|
@ -1,27 +1,28 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* Icinga 2 *
|
||||||
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
||||||
* *
|
* *
|
||||||
* This program is free software; you can redistribute it and/or *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
* as published by the Free Software Foundation; either version 2 *
|
* as published by the Free Software Foundation; either version 2 *
|
||||||
* of the License, or (at your option) any later version. *
|
* of the License, or (at your option) any later version. *
|
||||||
* *
|
* *
|
||||||
* This program is distributed in the hope that it will be useful, *
|
* This program is distributed in the hope that it will be useful, *
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||||
* GNU General Public License for more details. *
|
* GNU General Public License for more details. *
|
||||||
* *
|
* *
|
||||||
* You should have received a copy of the GNU General Public License *
|
* You should have received a copy of the GNU General Public License *
|
||||||
* along with this program; if not, write to the Free Software Foundation *
|
* along with this program; if not, write to the Free Software Foundation *
|
||||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
#ifndef VMFRAME_H
|
#ifndef VMFRAME_H
|
||||||
#define VMFRAME_H
|
#define VMFRAME_H
|
||||||
|
|
||||||
#include "config/i2-config.hpp"
|
#include "config/i2-config.hpp"
|
||||||
#include "base/dictionary.hpp"
|
#include "base/dictionary.hpp"
|
||||||
|
#include <boost/thread/tss.hpp>
|
||||||
|
|
||||||
namespace icinga
|
namespace icinga
|
||||||
{
|
{
|
||||||
|
@ -30,14 +31,45 @@ struct VMFrame
|
||||||
{
|
{
|
||||||
Dictionary::Ptr Locals;
|
Dictionary::Ptr Locals;
|
||||||
Object::Ptr Self;
|
Object::Ptr Self;
|
||||||
|
VMFrame *NextFrame;
|
||||||
|
|
||||||
VMFrame(void)
|
VMFrame(void)
|
||||||
: Locals(new Dictionary()), Self(Locals)
|
: Locals(new Dictionary()), Self(Locals)
|
||||||
{ }
|
{
|
||||||
|
NextFrame = GetCurrentFrame();
|
||||||
|
SetCurrentFrame(this);
|
||||||
|
}
|
||||||
|
|
||||||
VMFrame(const Object::Ptr& self)
|
VMFrame(const Object::Ptr& self)
|
||||||
: Locals(new Dictionary()), Self(self)
|
: Locals(new Dictionary()), Self(self)
|
||||||
{ }
|
{
|
||||||
|
NextFrame = GetCurrentFrame();
|
||||||
|
SetCurrentFrame(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
~VMFrame(void)
|
||||||
|
{
|
||||||
|
ASSERT(GetCurrentFrame() == this);
|
||||||
|
SetCurrentFrame(NextFrame);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline VMFrame *GetCurrentFrame(void)
|
||||||
|
{
|
||||||
|
VMFrame **pframe = m_CurrentFrame.get();
|
||||||
|
|
||||||
|
if (pframe)
|
||||||
|
return *pframe;
|
||||||
|
else
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static boost::thread_specific_ptr<VMFrame *> m_CurrentFrame;
|
||||||
|
|
||||||
|
static inline void SetCurrentFrame(VMFrame *frame)
|
||||||
|
{
|
||||||
|
m_CurrentFrame.reset(new VMFrame *(frame));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,7 @@
|
||||||
#include "base/convert.hpp"
|
#include "base/convert.hpp"
|
||||||
#include "base/objectlock.hpp"
|
#include "base/objectlock.hpp"
|
||||||
#include <boost/foreach.hpp>
|
#include <boost/foreach.hpp>
|
||||||
|
#include <boost/smart_ptr/make_shared.hpp>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -57,7 +58,7 @@ public:
|
||||||
return ScriptVariable::Get(name);
|
return ScriptVariable::Get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline Value FunctionCall(VMFrame& frame, const Value& funcName, const std::vector<Value>& arguments)
|
static inline Value FunctionCall(VMFrame& frame, const Object::Ptr& self, const Value& funcName, const std::vector<Value>& arguments)
|
||||||
{
|
{
|
||||||
ScriptFunction::Ptr func;
|
ScriptFunction::Ptr func;
|
||||||
|
|
||||||
|
@ -69,6 +70,13 @@ public:
|
||||||
if (!func)
|
if (!func)
|
||||||
BOOST_THROW_EXCEPTION(ScriptError("Function '" + funcName + "' does not exist."));
|
BOOST_THROW_EXCEPTION(ScriptError("Function '" + funcName + "' does not exist."));
|
||||||
|
|
||||||
|
boost::shared_ptr<VMFrame> vframe;
|
||||||
|
|
||||||
|
if (self)
|
||||||
|
vframe = boost::make_shared<VMFrame>(self); /* passes self to the callee using a TLS variable */
|
||||||
|
else
|
||||||
|
vframe = boost::make_shared<VMFrame>();
|
||||||
|
|
||||||
return func->Invoke(arguments);
|
return func->Invoke(arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -287,7 +295,8 @@ private:
|
||||||
if (arguments.size() < funcargs.size())
|
if (arguments.size() < funcargs.size())
|
||||||
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
|
||||||
|
|
||||||
VMFrame frame;
|
VMFrame *vframe = VMFrame::GetCurrentFrame();
|
||||||
|
VMFrame frame(vframe->Self);
|
||||||
|
|
||||||
if (closedVars)
|
if (closedVars)
|
||||||
closedVars->CopyTo(frame.Locals);
|
closedVars->CopyTo(frame.Locals);
|
||||||
|
|
Loading…
Reference in New Issue