Use std::unique_ptr for Expression objects

This commit is contained in:
Gunnar Beutner 2017-12-15 05:34:46 +01:00
parent 27a3329265
commit ab8386cf5c
21 changed files with 384 additions and 616 deletions

View File

@ -176,20 +176,16 @@ static int Main(void)
String initconfig = Application::GetSysconfDir() + "/icinga2/init.conf"; String initconfig = Application::GetSysconfDir() + "/icinga2/init.conf";
if (Utility::PathExists(initconfig)) { if (Utility::PathExists(initconfig)) {
Expression *expression; std::unique_ptr<Expression> expression;
try { try {
expression = ConfigCompiler::CompileFile(initconfig); expression = ConfigCompiler::CompileFile(initconfig);
ScriptFrame frame; ScriptFrame frame;
expression->Evaluate(frame); expression->Evaluate(frame);
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
delete expression;
Log(LogCritical, "config", DiagnosticInformation(ex)); Log(LogCritical, "config", DiagnosticInformation(ex));
return EXIT_FAILURE; return EXIT_FAILURE;
} }
delete expression;
} }
if (!autocomplete) if (!autocomplete)

View File

@ -68,7 +68,7 @@ extern "C" void dbg_inspect_object(Object *obj)
extern "C" void dbg_eval(const char *text) extern "C" void dbg_eval(const char *text)
{ {
Expression *expr = nullptr; std::unique_ptr<Expression> expr;
try { try {
ScriptFrame frame; ScriptFrame frame;
@ -78,13 +78,11 @@ extern "C" void dbg_eval(const char *text)
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
std::cout << "Error: " << DiagnosticInformation(ex) << "\n"; std::cout << "Error: " << DiagnosticInformation(ex) << "\n";
} }
delete expr;
} }
extern "C" void dbg_eval_with_value(const Value& value, const char *text) extern "C" void dbg_eval_with_value(const Value& value, const char *text)
{ {
Expression *expr = nullptr; std::unique_ptr<Expression> expr;
try { try {
ScriptFrame frame; ScriptFrame frame;
@ -96,13 +94,11 @@ extern "C" void dbg_eval_with_value(const Value& value, const char *text)
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
std::cout << "Error: " << DiagnosticInformation(ex) << "\n"; std::cout << "Error: " << DiagnosticInformation(ex) << "\n";
} }
delete expr;
} }
extern "C" void dbg_eval_with_object(Object *object, const char *text) extern "C" void dbg_eval_with_object(Object *object, const char *text)
{ {
Expression *expr = nullptr; std::unique_ptr<Expression> expr;
try { try {
ScriptFrame frame; ScriptFrame frame;
@ -114,8 +110,6 @@ extern "C" void dbg_eval_with_object(Object *object, const char *text)
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
std::cout << "Error: " << DiagnosticInformation(ex) << "\n"; std::cout << "Error: " << DiagnosticInformation(ex) << "\n";
} }
delete expr;
} }
void ConsoleCommand::BreakpointHandler(ScriptFrame& frame, ScriptError *ex, const DebugInfo& di) void ConsoleCommand::BreakpointHandler(ScriptFrame& frame, ScriptError *ex, const DebugInfo& di)
@ -400,7 +394,7 @@ incomplete:
command += line; command += line;
Expression *expr = nullptr; std::unique_ptr<Expression> expr;
try { try {
lines[fileName] = command; lines[fileName] = command;
@ -412,7 +406,7 @@ incomplete:
/* This relies on the fact that - for syntax errors - CompileText() /* This relies on the fact that - for syntax errors - CompileText()
* returns an AST where the top-level expression is a 'throw'. */ * returns an AST where the top-level expression is a 'throw'. */
if (!syntaxOnly || dynamic_cast<ThrowExpression *>(expr)) { if (!syntaxOnly || dynamic_cast<ThrowExpression *>(expr.get())) {
if (syntaxOnly) if (syntaxOnly)
std::cerr << " => " << command << std::endl; std::cerr << " => " << command << std::endl;
result = Serialize(expr->Evaluate(scriptFrame), 0); result = Serialize(expr->Evaluate(scriptFrame), 0);
@ -502,8 +496,6 @@ incomplete:
if (!commandOnce.IsEmpty()) if (!commandOnce.IsEmpty())
return EXIT_FAILURE; return EXIT_FAILURE;
} }
delete expr;
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -51,9 +51,9 @@ static void IncludeZoneDirRecursive(const String& path, const String& package, b
/* register this zone path for cluster config sync */ /* register this zone path for cluster config sync */
ConfigCompiler::RegisterZoneDir("_etc", path, zoneName); ConfigCompiler::RegisterZoneDir("_etc", path, zoneName);
std::vector<Expression *> expressions; std::vector<std::unique_ptr<Expression> > expressions;
Utility::GlobRecursive(path, "*.conf", std::bind(&ConfigCompiler::CollectIncludes, std::ref(expressions), _1, zoneName, package), GlobFile); Utility::GlobRecursive(path, "*.conf", std::bind(&ConfigCompiler::CollectIncludes, std::ref(expressions), _1, zoneName, package), GlobFile);
DictExpression expr(expressions); DictExpression expr(std::move(expressions));
if (!ExecuteExpression(&expr)) if (!ExecuteExpression(&expr))
success = false; success = false;
} }
@ -74,9 +74,9 @@ static void IncludeNonLocalZone(const String& zonePath, const String& package, b
return; return;
} }
std::vector<Expression *> expressions; std::vector<std::unique_ptr<Expression> > expressions;
Utility::GlobRecursive(zonePath, "*.conf", std::bind(&ConfigCompiler::CollectIncludes, std::ref(expressions), _1, zoneName, package), GlobFile); Utility::GlobRecursive(zonePath, "*.conf", std::bind(&ConfigCompiler::CollectIncludes, std::ref(expressions), _1, zoneName, package), GlobFile);
DictExpression expr(expressions); DictExpression expr(std::move(expressions));
if (!ExecuteExpression(&expr)) if (!ExecuteExpression(&expr))
success = false; success = false;
} }
@ -88,13 +88,11 @@ static void IncludePackage(const String& packagePath, bool& success)
String packageName = Utility::BaseName(packagePath); String packageName = Utility::BaseName(packagePath);
if (Utility::PathExists(packagePath + "/include.conf")) { if (Utility::PathExists(packagePath + "/include.conf")) {
Expression *expr = ConfigCompiler::CompileFile(packagePath + "/include.conf", std::unique_ptr<Expression> expr = ConfigCompiler::CompileFile(packagePath + "/include.conf",
String(), packageName); String(), packageName);
if (!ExecuteExpression(expr)) if (!ExecuteExpression(&*expr))
success = false; success = false;
delete expr;
} }
} }
@ -107,9 +105,8 @@ bool DaemonUtility::ValidateConfigFiles(const std::vector<std::string>& configs,
if (!configs.empty()) { if (!configs.empty()) {
for (const String& configPath : configs) { for (const String& configPath : configs) {
try { try {
Expression *expression = ConfigCompiler::CompileFile(configPath, String(), "_etc"); std::unique_ptr<Expression> expression = ConfigCompiler::CompileFile(configPath, String(), "_etc");
success = ExecuteExpression(expression); success = ExecuteExpression(&*expression);
delete expression;
if (!success) if (!success)
return false; return false;
} catch (const std::exception& ex) { } catch (const std::exception& ex) {

View File

@ -68,7 +68,7 @@ using namespace icinga;
template<typename T> template<typename T>
static void MakeRBinaryOp(Expression** result, Expression *left, Expression *right, const DebugInfo& diLeft, const DebugInfo& diRight) static void MakeRBinaryOp(Expression** result, Expression *left, Expression *right, const DebugInfo& diLeft, const DebugInfo& diRight)
{ {
*result = new T(left, right, DebugInfoRange(diLeft, diRight)); *result = new T(std::unique_ptr<Expression>(left), std::unique_ptr<Expression>(right), DebugInfoRange(diLeft, diRight));
} }
%} %}
@ -80,7 +80,7 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig
%error-verbose %error-verbose
%glr-parser %glr-parser
%parse-param { std::vector<std::pair<Expression *, EItemInfo> > *llist } %parse-param { std::vector<std::pair<std::unique_ptr<Expression>, EItemInfo> > *llist }
%parse-param { ConfigCompiler *context } %parse-param { ConfigCompiler *context }
%lex-param { void *scanner } %lex-param { void *scanner }
@ -92,12 +92,12 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig
icinga::DictExpression *dexpr; icinga::DictExpression *dexpr;
CombinedSetOp csop; CombinedSetOp csop;
std::vector<String> *slist; std::vector<String> *slist;
std::vector<std::pair<Expression *, EItemInfo> > *llist; std::vector<std::pair<std::unique_ptr<Expression>, EItemInfo> > *llist;
std::vector<Expression *> *elist; std::vector<std::unique_ptr<Expression> > *elist;
std::vector<std::pair<Expression *, Expression *> > *ebranchlist; std::vector<std::pair<std::unique_ptr<Expression>, std::unique_ptr<Expression> > > *ebranchlist;
std::pair<Expression *, Expression *> *ebranch; std::pair<std::unique_ptr<Expression>, std::unique_ptr<Expression> > *ebranch;
std::pair<String, Expression *> *cvitem; std::pair<String, std::unique_ptr<Expression> > *cvitem;
std::map<String, Expression *> *cvlist; std::map<String, std::unique_ptr<Expression> > *cvlist;
icinga::ScopeSpecifier scope; icinga::ScopeSpecifier scope;
} }
@ -238,13 +238,13 @@ int yylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
extern int yydebug; extern int yydebug;
void yyerror(const YYLTYPE *locp, std::vector<std::pair<Expression *, EItemInfo> > *, ConfigCompiler *context, const char *err) void yyerror(const YYLTYPE *locp, std::vector<std::pair<std::unique_ptr<Expression>, EItemInfo> > *, ConfigCompiler *context, const char *err)
{ {
bool incomplete = context && context->m_Eof && (context->m_OpenBraces > 0); bool incomplete = context && context->m_Eof && (context->m_OpenBraces > 0);
BOOST_THROW_EXCEPTION(ScriptError(err, *locp, incomplete)); BOOST_THROW_EXCEPTION(ScriptError(err, *locp, incomplete));
} }
int yyparse(std::vector<std::pair<Expression *, EItemInfo> > *llist, ConfigCompiler *context); int yyparse(std::vector<std::pair<std::unique_ptr<Expression>, EItemInfo> > *llist, ConfigCompiler *context);
static void BeginFlowControlBlock(ConfigCompiler *compiler, int allowedTypes, bool inherit) static void BeginFlowControlBlock(ConfigCompiler *compiler, int allowedTypes, bool inherit)
{ {
@ -267,9 +267,9 @@ static void UseFlowControl(ConfigCompiler *compiler, FlowControlType type, const
BOOST_THROW_EXCEPTION(ScriptError("Invalid flow control statement.", location)); BOOST_THROW_EXCEPTION(ScriptError("Invalid flow control statement.", location));
} }
Expression *ConfigCompiler::Compile(void) std::unique_ptr<Expression> ConfigCompiler::Compile(void)
{ {
std::vector<std::pair<Expression *, EItemInfo> > llist; std::vector<std::pair<std::unique_ptr<Expression>, EItemInfo> > llist;
//yydebug = 1; //yydebug = 1;
@ -282,19 +282,19 @@ Expression *ConfigCompiler::Compile(void)
EndFlowControlBlock(this); EndFlowControlBlock(this);
m_IgnoreNewlines.pop(); m_IgnoreNewlines.pop();
std::vector<Expression *> dlist; std::vector<std::unique_ptr<Expression> > dlist;
std::vector<std::pair<Expression *, EItemInfo> >::size_type num = 0; decltype(llist.size()) num = 0;
for (const auto& litem : llist) { for (auto& litem : llist) {
if (!litem.second.SideEffect && num != llist.size() - 1) { if (!litem.second.SideEffect && num != llist.size() - 1) {
yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used."); yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used.");
} }
dlist.push_back(litem.first); dlist.push_back(std::move(litem.first));
num++; num++;
} }
DictExpression *expr = new DictExpression(dlist); std::unique_ptr<DictExpression> expr{new DictExpression(std::move(dlist))};
expr->MakeInline(); expr->MakeInline();
return expr; return std::move(expr);
} }
#define scanner (context->GetScanner()) #define scanner (context->GetScanner())
@ -318,7 +318,7 @@ statements: newlines lterm_items
lterm_items: /* empty */ lterm_items: /* empty */
{ {
$$ = new std::vector<std::pair<Expression *, EItemInfo> >(); $$ = new std::vector<std::pair<std::unique_ptr<Expression>, EItemInfo> >();
} }
| lterm_items_inner | lterm_items_inner
| lterm_items_inner sep | lterm_items_inner sep
@ -326,26 +326,26 @@ lterm_items: /* empty */
lterm_items_inner: lterm %dprec 2 lterm_items_inner: lterm %dprec 2
{ {
$$ = new std::vector<std::pair<Expression *, EItemInfo> >(); $$ = new std::vector<std::pair<std::unique_ptr<Expression>, EItemInfo> >();
EItemInfo info = { true, @1 }; EItemInfo info = { true, @1 };
$$->emplace_back($1, info); $$->emplace_back(std::unique_ptr<Expression>($1), info);
} }
| rterm_no_side_effect | rterm_no_side_effect
{ {
$$ = new std::vector<std::pair<Expression *, EItemInfo> >(); $$ = new std::vector<std::pair<std::unique_ptr<Expression>, EItemInfo> >();
EItemInfo info = { false, @1 }; EItemInfo info = { false, @1 };
$$->emplace_back($1, info); $$->emplace_back(std::unique_ptr<Expression>($1), info);
} }
| lterm_items_inner sep lterm %dprec 1 | lterm_items_inner sep lterm %dprec 1
{ {
if ($1) if ($1)
$$ = $1; $$ = $1;
else else
$$ = new std::vector<std::pair<Expression *, EItemInfo> >(); $$ = new std::vector<std::pair<std::unique_ptr<Expression>, EItemInfo> >();
if ($3) { if ($3) {
EItemInfo info = { true, @3 }; EItemInfo info = { true, @3 };
$$->emplace_back($3, info); $$->emplace_back(std::unique_ptr<Expression>($3), info);
} }
} }
| lterm_items_inner sep rterm_no_side_effect %dprec 1 | lterm_items_inner sep rterm_no_side_effect %dprec 1
@ -353,11 +353,11 @@ lterm_items_inner: lterm %dprec 2
if ($1) if ($1)
$$ = $1; $$ = $1;
else else
$$ = new std::vector<std::pair<Expression *, EItemInfo> >(); $$ = new std::vector<std::pair<std::unique_ptr<Expression>, EItemInfo> >();
if ($3) { if ($3) {
EItemInfo info = { false, @3 }; EItemInfo info = { false, @3 };
$$->emplace_back($3, info); $$->emplace_back(std::unique_ptr<Expression>($3), info);
} }
} }
; ;
@ -396,26 +396,28 @@ object:
bool seen_ignore = context->m_SeenIgnore.top(); bool seen_ignore = context->m_SeenIgnore.top();
context->m_SeenIgnore.pop(); context->m_SeenIgnore.pop();
Expression *ignore = context->m_Ignore.top(); std::unique_ptr<Expression> ignore{std::move(context->m_Ignore.top())};
context->m_Ignore.pop(); context->m_Ignore.pop();
Expression *assign = context->m_Assign.top(); std::unique_ptr<Expression> assign{std::move(context->m_Assign.top())};
context->m_Assign.pop(); context->m_Assign.pop();
Expression *filter = NULL; std::unique_ptr<Expression> filter;
if (seen_assign) { if (seen_assign) {
if (ignore) { if (ignore) {
Expression *rex = new LogicalNegateExpression(ignore, DebugInfoRange(@2, @5)); std::unique_ptr<Expression> rex{new LogicalNegateExpression(std::move(ignore), DebugInfoRange(@2, @5))};
filter = new LogicalAndExpression(assign, rex, DebugInfoRange(@2, @5)); filter.reset(new LogicalAndExpression(std::move(assign), std::move(rex), DebugInfoRange(@2, @5)));
} else } else
filter = assign; filter.swap(assign);
} else if (seen_ignore) { } else if (seen_ignore) {
BOOST_THROW_EXCEPTION(ScriptError("object rule 'ignore where' cannot be used without 'assign where'", DebugInfoRange(@2, @4))); BOOST_THROW_EXCEPTION(ScriptError("object rule 'ignore where' cannot be used without 'assign where'", DebugInfoRange(@2, @4)));
} }
$$ = new ObjectExpression(abstract, $3, $4, filter, context->GetZone(), context->GetPackage(), $5, $6, $7, $9, DebugInfoRange(@2, @7)); $$ = new ObjectExpression(abstract, std::unique_ptr<Expression>($3), std::unique_ptr<Expression>($4),
std::move(filter), context->GetZone(), context->GetPackage(), std::move(*$5), $6, $7, std::unique_ptr<Expression>($9), DebugInfoRange(@2, @7));
delete $5;
} }
; ;
@ -468,15 +470,15 @@ combined_set_op: T_SET
lterm: T_LIBRARY rterm lterm: T_LIBRARY rterm
{ {
$$ = new LibraryExpression($2, @$); $$ = new LibraryExpression(std::unique_ptr<Expression>($2), @$);
} }
| rterm combined_set_op rterm | rterm combined_set_op rterm
{ {
$$ = new SetExpression($1, $2, $3, @$); $$ = new SetExpression(std::unique_ptr<Expression>($1), $2, std::unique_ptr<Expression>($3), @$);
} }
| T_INCLUDE rterm | T_INCLUDE rterm
{ {
$$ = new IncludeExpression(Utility::DirName(context->GetPath()), $2, NULL, NULL, IncludeRegular, false, context->GetZone(), context->GetPackage(), @$); $$ = new IncludeExpression(Utility::DirName(context->GetPath()), std::unique_ptr<Expression>($2), NULL, NULL, IncludeRegular, false, context->GetZone(), context->GetPackage(), @$);
} }
| T_INCLUDE T_STRING_ANGLE | T_INCLUDE T_STRING_ANGLE
{ {
@ -485,23 +487,23 @@ lterm: T_LIBRARY rterm
} }
| T_INCLUDE_RECURSIVE rterm | T_INCLUDE_RECURSIVE rterm
{ {
$$ = new IncludeExpression(Utility::DirName(context->GetPath()), $2, MakeLiteral("*.conf"), NULL, IncludeRecursive, false, context->GetZone(), context->GetPackage(), @$); $$ = new IncludeExpression(Utility::DirName(context->GetPath()), std::unique_ptr<Expression>($2), MakeLiteral("*.conf"), NULL, IncludeRecursive, false, context->GetZone(), context->GetPackage(), @$);
} }
| T_INCLUDE_RECURSIVE rterm ',' rterm | T_INCLUDE_RECURSIVE rterm ',' rterm
{ {
$$ = new IncludeExpression(Utility::DirName(context->GetPath()), $2, $4, NULL, IncludeRecursive, false, context->GetZone(), context->GetPackage(), @$); $$ = new IncludeExpression(Utility::DirName(context->GetPath()), std::unique_ptr<Expression>($2), std::unique_ptr<Expression>($4), NULL, IncludeRecursive, false, context->GetZone(), context->GetPackage(), @$);
} }
| T_INCLUDE_ZONES rterm ',' rterm | T_INCLUDE_ZONES rterm ',' rterm
{ {
$$ = new IncludeExpression(Utility::DirName(context->GetPath()), $4, MakeLiteral("*.conf"), $2, IncludeZones, false, context->GetZone(), context->GetPackage(), @$); $$ = new IncludeExpression(Utility::DirName(context->GetPath()), std::unique_ptr<Expression>($4), MakeLiteral("*.conf"), std::unique_ptr<Expression>($2), IncludeZones, false, context->GetZone(), context->GetPackage(), @$);
} }
| T_INCLUDE_ZONES rterm ',' rterm ',' rterm | T_INCLUDE_ZONES rterm ',' rterm ',' rterm
{ {
$$ = new IncludeExpression(Utility::DirName(context->GetPath()), $4, $6, $2, IncludeZones, false, context->GetZone(), context->GetPackage(), @$); $$ = new IncludeExpression(Utility::DirName(context->GetPath()), std::unique_ptr<Expression>($4), std::unique_ptr<Expression>($6), std::unique_ptr<Expression>($2), IncludeZones, false, context->GetZone(), context->GetPackage(), @$);
} }
| T_IMPORT rterm | T_IMPORT rterm
{ {
$$ = new ImportExpression($2, @$); $$ = new ImportExpression(std::unique_ptr<Expression>($2), @$);
} }
| T_ASSIGN T_WHERE | T_ASSIGN T_WHERE
{ {
@ -517,11 +519,11 @@ lterm: T_LIBRARY rterm
context->m_SeenAssign.top() = true; context->m_SeenAssign.top() = true;
if (context->m_Assign.top()) if (context->m_Assign.top())
context->m_Assign.top() = new LogicalOrExpression(context->m_Assign.top(), $4, @$); context->m_Assign.top() = new LogicalOrExpression(std::unique_ptr<Expression>(context->m_Assign.top()), std::unique_ptr<Expression>($4), @$);
else else
context->m_Assign.top() = $4; context->m_Assign.top() = $4;
$$ = MakeLiteral(); $$ = MakeLiteralRaw();
} }
| T_ASSIGN T_WHERE rterm %dprec 1 | T_ASSIGN T_WHERE rterm %dprec 1
{ {
@ -533,11 +535,11 @@ lterm: T_LIBRARY rterm
context->m_SeenAssign.top() = true; context->m_SeenAssign.top() = true;
if (context->m_Assign.top()) if (context->m_Assign.top())
context->m_Assign.top() = new LogicalOrExpression(context->m_Assign.top(), $3, @$); context->m_Assign.top() = new LogicalOrExpression(std::unique_ptr<Expression>(context->m_Assign.top()), std::unique_ptr<Expression>($3), @$);
else else
context->m_Assign.top() = $3; context->m_Assign.top() = $3;
$$ = MakeLiteral(); $$ = MakeLiteralRaw();
} }
| T_IGNORE T_WHERE | T_IGNORE T_WHERE
{ {
@ -553,11 +555,11 @@ lterm: T_LIBRARY rterm
context->m_SeenIgnore.top() = true; context->m_SeenIgnore.top() = true;
if (context->m_Ignore.top()) if (context->m_Ignore.top())
context->m_Ignore.top() = new LogicalOrExpression(context->m_Ignore.top(), $4, @$); context->m_Ignore.top() = new LogicalOrExpression(std::unique_ptr<Expression>(context->m_Ignore.top()), std::unique_ptr<Expression>($4), @$);
else else
context->m_Ignore.top() = $4; context->m_Ignore.top() = $4;
$$ = MakeLiteral(); $$ = MakeLiteralRaw();
} }
| T_IGNORE T_WHERE rterm %dprec 1 | T_IGNORE T_WHERE rterm %dprec 1
{ {
@ -569,16 +571,16 @@ lterm: T_LIBRARY rterm
context->m_SeenIgnore.top() = true; context->m_SeenIgnore.top() = true;
if (context->m_Ignore.top()) if (context->m_Ignore.top())
context->m_Ignore.top() = new LogicalOrExpression(context->m_Ignore.top(), $3, @$); context->m_Ignore.top() = new LogicalOrExpression(std::unique_ptr<Expression>(context->m_Ignore.top()), std::unique_ptr<Expression>($3), @$);
else else
context->m_Ignore.top() = $3; context->m_Ignore.top() = $3;
$$ = MakeLiteral(); $$ = MakeLiteralRaw();
} }
| T_RETURN optional_rterm | T_RETURN optional_rterm
{ {
UseFlowControl(context, FlowControlReturn, @$); UseFlowControl(context, FlowControlReturn, @$);
$$ = new ReturnExpression($2, @$); $$ = new ReturnExpression(std::unique_ptr<Expression>($2), @$);
} }
| T_BREAK | T_BREAK
{ {
@ -596,7 +598,7 @@ lterm: T_LIBRARY rterm
} }
| T_USING rterm | T_USING rterm
{ {
$$ = new UsingExpression($2, @$); $$ = new UsingExpression(std::unique_ptr<Expression>($2), @$);
} }
| apply | apply
| object | object
@ -608,7 +610,7 @@ lterm: T_LIBRARY rterm
{ {
EndFlowControlBlock(context); EndFlowControlBlock(context);
$$ = new ForExpression(*$3, *$5, $7, $10, @$); $$ = new ForExpression(*$3, *$5, std::unique_ptr<Expression>($7), std::unique_ptr<Expression>($10), @$);
delete $3; delete $3;
delete $5; delete $5;
} }
@ -620,7 +622,7 @@ lterm: T_LIBRARY rterm
{ {
EndFlowControlBlock(context); EndFlowControlBlock(context);
$$ = new ForExpression(*$3, "", $5, $8, @$); $$ = new ForExpression(*$3, "", std::unique_ptr<Expression>($5), std::unique_ptr<Expression>($8), @$);
delete $3; delete $3;
} }
| T_FUNCTION identifier '(' identifier_items ')' use_specifier | T_FUNCTION identifier '(' identifier_items ')' use_specifier
@ -631,28 +633,29 @@ lterm: T_LIBRARY rterm
{ {
EndFlowControlBlock(context); EndFlowControlBlock(context);
FunctionExpression *fexpr = new FunctionExpression(*$2, *$4, $6, $8, @$); std::unique_ptr<FunctionExpression> fexpr{new FunctionExpression(*$2, *$4, std::move(*$6), std::unique_ptr<Expression>($8), @$)};
delete $4; delete $4;
delete $6;
$$ = new SetExpression(MakeIndexer(ScopeThis, *$2), OpSetLiteral, fexpr, @$); $$ = new SetExpression(MakeIndexer(ScopeThis, *$2), OpSetLiteral, std::move(fexpr), @$);
delete $2; delete $2;
} }
| T_CONST T_IDENTIFIER T_SET rterm | T_CONST T_IDENTIFIER T_SET rterm
{ {
$$ = new SetExpression(MakeIndexer(ScopeGlobal, *$2), OpSetLiteral, $4); $$ = new SetExpression(MakeIndexer(ScopeGlobal, *$2), OpSetLiteral, std::unique_ptr<Expression>($4));
delete $2; delete $2;
} }
| T_VAR rterm | T_VAR rterm
{ {
Expression *expr = $2; std::unique_ptr<Expression> expr{$2};
BindToScope(expr, ScopeLocal); BindToScope(expr, ScopeLocal);
$$ = new SetExpression(expr, OpSetLiteral, MakeLiteral(), @$); $$ = new SetExpression(std::move(expr), OpSetLiteral, MakeLiteral(), @$);
} }
| T_VAR rterm combined_set_op rterm | T_VAR rterm combined_set_op rterm
{ {
Expression *expr = $2; std::unique_ptr<Expression> expr{$2};
BindToScope(expr, ScopeLocal); BindToScope(expr, ScopeLocal);
$$ = new SetExpression(expr, $3, $4, @$); $$ = new SetExpression(std::move(expr), $3, std::unique_ptr<Expression>($4), @$);
} }
| T_WHILE '(' rterm ')' | T_WHILE '(' rterm ')'
{ {
@ -662,22 +665,22 @@ lterm: T_LIBRARY rterm
{ {
EndFlowControlBlock(context); EndFlowControlBlock(context);
$$ = new WhileExpression($3, $6, @$); $$ = new WhileExpression(std::unique_ptr<Expression>($3), std::unique_ptr<Expression>($6), @$);
} }
| T_THROW rterm | T_THROW rterm
{ {
$$ = new ThrowExpression($2, false, @$); $$ = new ThrowExpression(std::unique_ptr<Expression>($2), false, @$);
} }
| T_TRY rterm_scope T_EXCEPT rterm_scope | T_TRY rterm_scope T_EXCEPT rterm_scope
{ {
$$ = new TryExceptExpression($2, $4, @$); $$ = new TryExceptExpression(std::unique_ptr<Expression>($2), std::unique_ptr<Expression>($4), @$);
} }
| rterm_side_effect | rterm_side_effect
; ;
rterm_items: /* empty */ rterm_items: /* empty */
{ {
$$ = new std::vector<Expression *>(); $$ = new std::vector<std::unique_ptr<Expression> >();
} }
| rterm_items_inner | rterm_items_inner
| rterm_items_inner ',' | rterm_items_inner ','
@ -687,13 +690,13 @@ rterm_items: /* empty */
rterm_items_inner: rterm rterm_items_inner: rterm
{ {
$$ = new std::vector<Expression *>(); $$ = new std::vector<std::unique_ptr<Expression> >();
$$->push_back($1); $$->emplace_back($1);
} }
| rterm_items_inner arraysep rterm | rterm_items_inner arraysep rterm
{ {
$$ = $1; $$ = $1;
$$->push_back($3); $$->emplace_back($3);
} }
; ;
@ -704,7 +707,7 @@ rterm_array: '['
newlines rterm_items ']' newlines rterm_items ']'
{ {
context->m_OpenBraces--; context->m_OpenBraces--;
$$ = new ArrayExpression(*$4, @$); $$ = new ArrayExpression(std::move(*$4), @$);
delete $4; delete $4;
} }
| '[' | '['
@ -714,7 +717,7 @@ rterm_array: '['
rterm_items ']' rterm_items ']'
{ {
context->m_OpenBraces--; context->m_OpenBraces--;
$$ = new ArrayExpression(*$3, @$); $$ = new ArrayExpression(std::move(*$3), @$);
delete $3; delete $3;
} }
; ;
@ -730,17 +733,14 @@ rterm_dict: '{'
EndFlowControlBlock(context); EndFlowControlBlock(context);
context->m_OpenBraces--; context->m_OpenBraces--;
context->m_IgnoreNewlines.pop(); context->m_IgnoreNewlines.pop();
std::vector<Expression *> dlist; std::vector<std::unique_ptr<Expression> > dlist;
typedef std::pair<Expression *, EItemInfo> EListItem; for (auto& litem : *$3) {
int num = 0;
for (const EListItem& litem : *$3) {
if (!litem.second.SideEffect) if (!litem.second.SideEffect)
yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used."); yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used.");
dlist.push_back(litem.first); dlist.push_back(std::move(litem.first));
num++;
} }
delete $3; delete $3;
$$ = new DictExpression(dlist, @$); $$ = new DictExpression(std::move(dlist), @$);
} }
; ;
@ -753,17 +753,14 @@ rterm_scope_require_side_effect: '{'
{ {
context->m_OpenBraces--; context->m_OpenBraces--;
context->m_IgnoreNewlines.pop(); context->m_IgnoreNewlines.pop();
std::vector<Expression *> dlist; std::vector<std::unique_ptr<Expression> > dlist;
typedef std::pair<Expression *, EItemInfo> EListItem; for (auto& litem : *$3) {
int num = 0;
for (const EListItem& litem : *$3) {
if (!litem.second.SideEffect) if (!litem.second.SideEffect)
yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used."); yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used.");
dlist.push_back(litem.first); dlist.push_back(std::move(litem.first));
num++;
} }
delete $3; delete $3;
$$ = new DictExpression(dlist, @$); $$ = new DictExpression(std::move(dlist), @$);
$$->MakeInline(); $$->MakeInline();
} }
; ;
@ -777,101 +774,102 @@ rterm_scope: '{'
{ {
context->m_OpenBraces--; context->m_OpenBraces--;
context->m_IgnoreNewlines.pop(); context->m_IgnoreNewlines.pop();
std::vector<Expression *> dlist; std::vector<std::unique_ptr<Expression> > dlist;
typedef std::pair<Expression *, EItemInfo> EListItem; decltype($3->size()) num = 0;
std::vector<std::pair<Expression *, EItemInfo> >::size_type num = 0; for (auto& litem : *$3) {
for (const EListItem& litem : *$3) {
if (!litem.second.SideEffect && num != $3->size() - 1) if (!litem.second.SideEffect && num != $3->size() - 1)
yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used."); yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used.");
dlist.push_back(litem.first); dlist.push_back(std::move(litem.first));
num++; num++;
} }
delete $3; delete $3;
$$ = new DictExpression(dlist, @$); $$ = new DictExpression(std::move(dlist), @$);
$$->MakeInline(); $$->MakeInline();
} }
; ;
else_if_branch: T_ELSE T_IF '(' rterm ')' rterm_scope else_if_branch: T_ELSE T_IF '(' rterm ')' rterm_scope
{ {
$$ = new std::pair<Expression *, Expression *>($4, $6); $$ = new std::pair<std::unique_ptr<Expression>, std::unique_ptr<Expression> >(std::unique_ptr<Expression>($4), std::unique_ptr<Expression>($6));
} }
; ;
else_if_branches: /* empty */ else_if_branches: /* empty */
{ {
$$ = new std::vector<std::pair<Expression *, Expression *> >(); $$ = new std::vector<std::pair<std::unique_ptr<Expression>, std::unique_ptr<Expression> > >();
} }
| else_if_branches else_if_branch | else_if_branches else_if_branch
{ {
$$ = $1; $$ = $1;
$$->push_back(*$2); $$->push_back(std::move(*$2));
delete $2; delete $2;
} }
; ;
rterm_side_effect: rterm '(' rterm_items ')' rterm_side_effect: rterm '(' rterm_items ')'
{ {
$$ = new FunctionCallExpression($1, *$3, @$); $$ = new FunctionCallExpression(std::unique_ptr<Expression>($1), std::move(*$3), @$);
delete $3; delete $3;
} }
| T_IF '(' rterm ')' rterm_scope else_if_branches | T_IF '(' rterm ')' rterm_scope else_if_branches
{ {
std::vector<std::pair<Expression *, Expression *> > ebranches = *$6; std::vector<std::pair<std::unique_ptr<Expression>, std::unique_ptr<Expression> > > ebranches;
$6->swap(ebranches);
delete $6; delete $6;
Expression *afalse = NULL; std::unique_ptr<Expression> afalse;
for (int i = ebranches.size() - 1; i >= 0; i--) { for (int i = ebranches.size() - 1; i >= 0; i--) {
const std::pair<Expression *, Expression *>& ebranch = ebranches[i]; auto& ebranch = ebranches[i];
afalse = new ConditionalExpression(ebranch.first, ebranch.second, afalse, @6); afalse.reset(new ConditionalExpression(std::move(ebranch.first), std::move(ebranch.second), std::move(afalse), @6));
} }
$$ = new ConditionalExpression($3, $5, afalse, @$); $$ = new ConditionalExpression(std::unique_ptr<Expression>($3), std::unique_ptr<Expression>($5), std::move(afalse), @$);
} }
| T_IF '(' rterm ')' rterm_scope else_if_branches T_ELSE rterm_scope | T_IF '(' rterm ')' rterm_scope else_if_branches T_ELSE rterm_scope
{ {
std::vector<std::pair<Expression *, Expression *> > ebranches = *$6; std::vector<std::pair<std::unique_ptr<Expression>, std::unique_ptr<Expression> > > ebranches;
$6->swap(ebranches);
delete $6; delete $6;
$8->MakeInline(); $8->MakeInline();
Expression *afalse = $8; std::unique_ptr<Expression> afalse{$8};
for (int i = ebranches.size() - 1; i >= 0; i--) { for (int i = ebranches.size() - 1; i >= 0; i--) {
const std::pair<Expression *, Expression *>& ebranch = ebranches[i]; auto& ebranch = ebranches[i];
afalse = new ConditionalExpression(ebranch.first, ebranch.second, afalse, @6); afalse.reset(new ConditionalExpression(std::move(ebranch.first), std::move(ebranch.second), std::move(afalse), @6));
} }
$$ = new ConditionalExpression($3, $5, afalse, @$); $$ = new ConditionalExpression(std::unique_ptr<Expression>($3), std::unique_ptr<Expression>($5), std::move(afalse), @$);
} }
; ;
rterm_no_side_effect_no_dict: T_STRING rterm_no_side_effect_no_dict: T_STRING
{ {
$$ = MakeLiteral(*$1); $$ = MakeLiteralRaw(*$1);
delete $1; delete $1;
} }
| T_NUMBER | T_NUMBER
{ {
$$ = MakeLiteral($1); $$ = MakeLiteralRaw($1);
} }
| T_BOOLEAN | T_BOOLEAN
{ {
$$ = MakeLiteral($1); $$ = MakeLiteralRaw($1);
} }
| T_NULL | T_NULL
{ {
$$ = MakeLiteral(); $$ = MakeLiteralRaw();
} }
| rterm '.' T_IDENTIFIER %dprec 2 | rterm '.' T_IDENTIFIER %dprec 2
{ {
$$ = new IndexerExpression($1, MakeLiteral(*$3), @$); $$ = new IndexerExpression(std::unique_ptr<Expression>($1), MakeLiteral(*$3), @$);
delete $3; delete $3;
} }
| rterm '[' rterm ']' | rterm '[' rterm ']'
{ {
$$ = new IndexerExpression($1, $3, @$); $$ = new IndexerExpression(std::unique_ptr<Expression>($1), std::unique_ptr<Expression>($3), @$);
} }
| T_IDENTIFIER | T_IDENTIFIER
{ {
@ -880,11 +878,11 @@ rterm_no_side_effect_no_dict: T_STRING
} }
| '!' rterm | '!' rterm
{ {
$$ = new LogicalNegateExpression($2, @$); $$ = new LogicalNegateExpression(std::unique_ptr<Expression>($2), @$);
} }
| '~' rterm | '~' rterm
{ {
$$ = new NegateExpression($2, @$); $$ = new NegateExpression(std::unique_ptr<Expression>($2), @$);
} }
| T_PLUS rterm %prec UNARY_PLUS | T_PLUS rterm %prec UNARY_PLUS
{ {
@ -892,7 +890,7 @@ rterm_no_side_effect_no_dict: T_STRING
} }
| T_MINUS rterm %prec UNARY_MINUS | T_MINUS rterm %prec UNARY_MINUS
{ {
$$ = new SubtractExpression(MakeLiteral(0), $2, @$); $$ = new SubtractExpression(MakeLiteral(0), std::unique_ptr<Expression>($2), @$);
} }
| T_THIS | T_THIS
{ {
@ -908,11 +906,11 @@ rterm_no_side_effect_no_dict: T_STRING
} }
| T_CURRENT_FILENAME | T_CURRENT_FILENAME
{ {
$$ = MakeLiteral(@$.Path); $$ = MakeLiteralRaw(@$.Path);
} }
| T_CURRENT_LINE | T_CURRENT_LINE
{ {
$$ = MakeLiteral(@$.FirstLine); $$ = MakeLiteralRaw(@$.FirstLine);
} }
| identifier T_FOLLOWS | identifier T_FOLLOWS
{ {
@ -926,7 +924,7 @@ rterm_no_side_effect_no_dict: T_STRING
args.push_back(*$1); args.push_back(*$1);
delete $1; delete $1;
$$ = new FunctionExpression("<anonymous>", args, new std::map<String, Expression *>(), $4, @$); $$ = new FunctionExpression("<anonymous>", args, {}, std::unique_ptr<Expression>($4), @$);
} }
| identifier T_FOLLOWS rterm %dprec 1 | identifier T_FOLLOWS rterm %dprec 1
{ {
@ -936,7 +934,7 @@ rterm_no_side_effect_no_dict: T_STRING
args.push_back(*$1); args.push_back(*$1);
delete $1; delete $1;
$$ = new FunctionExpression("<anonymous>", args, new std::map<String, Expression *>(), $3, @$); $$ = new FunctionExpression("<anonymous>", args, {}, std::unique_ptr<Expression>($3), @$);
} }
| '(' identifier_items ')' T_FOLLOWS | '(' identifier_items ')' T_FOLLOWS
{ {
@ -946,14 +944,14 @@ rterm_no_side_effect_no_dict: T_STRING
{ {
EndFlowControlBlock(context); EndFlowControlBlock(context);
$$ = new FunctionExpression("<anonymous>", *$2, new std::map<String, Expression *>(), $6, @$); $$ = new FunctionExpression("<anonymous>", *$2, {}, std::unique_ptr<Expression>($6), @$);
delete $2; delete $2;
} }
| '(' identifier_items ')' T_FOLLOWS rterm %dprec 1 | '(' identifier_items ')' T_FOLLOWS rterm %dprec 1
{ {
ASSERT(!dynamic_cast<DictExpression *>($5)); ASSERT(!dynamic_cast<DictExpression *>($5));
$$ = new FunctionExpression("<anonymous>", *$2, new std::map<String, Expression *>(), $5, @$); $$ = new FunctionExpression("<anonymous>", *$2, {}, std::unique_ptr<Expression>($5), @$);
delete $2; delete $2;
} }
| rterm_array | rterm_array
@ -994,8 +992,9 @@ rterm_no_side_effect_no_dict: T_STRING
{ {
EndFlowControlBlock(context); EndFlowControlBlock(context);
$$ = new FunctionExpression("<anonymous>", *$3, $5, $7, @$); $$ = new FunctionExpression("<anonymous>", *$3, std::move(*$5), std::unique_ptr<Expression>($7), @$);
delete $3; delete $3;
delete $5;
} }
| T_NULLARY_LAMBDA_BEGIN | T_NULLARY_LAMBDA_BEGIN
{ {
@ -1005,20 +1004,19 @@ rterm_no_side_effect_no_dict: T_STRING
{ {
EndFlowControlBlock(context); EndFlowControlBlock(context);
std::vector<Expression *> dlist; std::vector<std::unique_ptr<Expression> > dlist;
typedef std::pair<Expression *, EItemInfo> EListItem; decltype(dlist.size()) num = 0;
std::vector<std::pair<Expression *, EItemInfo> >::size_type num = 0; for (auto& litem : *$3) {
for (const EListItem& litem : *$3) {
if (!litem.second.SideEffect && num != $3->size() - 1) if (!litem.second.SideEffect && num != $3->size() - 1)
yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used."); yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used.");
dlist.push_back(litem.first); dlist.push_back(std::move(litem.first));
num++; num++;
} }
delete $3; delete $3;
DictExpression *aexpr = new DictExpression(dlist, @$); std::unique_ptr<DictExpression> aexpr{new DictExpression(std::move(dlist), @$)};
aexpr->MakeInline(); aexpr->MakeInline();
$$ = new FunctionExpression("<anonymous>", std::vector<String>(), NULL, aexpr, @$); $$ = new FunctionExpression("<anonymous>", {}, {}, std::move(aexpr), @$);
} }
; ;
@ -1026,9 +1024,9 @@ rterm_no_side_effect:
rterm_no_side_effect_no_dict %dprec 1 rterm_no_side_effect_no_dict %dprec 1
| rterm_dict %dprec 2 | rterm_dict %dprec 2
{ {
Expression *expr = $1; std::unique_ptr<Expression> expr{$1};
BindToScope(expr, ScopeThis); BindToScope(expr, ScopeThis);
$$ = expr; $$ = expr.release();
} }
; ;
@ -1069,7 +1067,7 @@ ignore_specifier: /* empty */
use_specifier: /* empty */ use_specifier: /* empty */
{ {
$$ = NULL; $$ = new std::map<String, std::unique_ptr<Expression> >();
} }
| T_USE '(' use_specifier_items ')' | T_USE '(' use_specifier_items ')'
{ {
@ -1079,26 +1077,26 @@ use_specifier: /* empty */
use_specifier_items: use_specifier_item use_specifier_items: use_specifier_item
{ {
$$ = new std::map<String, Expression *>(); $$ = new std::map<String, std::unique_ptr<Expression> >();
$$->insert(*$1); $$->insert(std::move(*$1));
delete $1; delete $1;
} }
| use_specifier_items ',' use_specifier_item | use_specifier_items ',' use_specifier_item
{ {
$$ = $1; $$ = $1;
$$->insert(*$3); $$->insert(std::move(*$3));
delete $3; delete $3;
} }
; ;
use_specifier_item: identifier use_specifier_item: identifier
{ {
$$ = new std::pair<String, Expression *>(*$1, new VariableExpression(*$1, @1)); $$ = new std::pair<String, std::unique_ptr<Expression> >(*$1, std::unique_ptr<Expression>(new VariableExpression(*$1, @1)));
delete $1; delete $1;
} }
| identifier T_SET rterm | identifier T_SET rterm
{ {
$$ = new std::pair<String, Expression *>(*$1, $3); $$ = new std::pair<String, std::unique_ptr<Expression> >(*$1, std::unique_ptr<Expression>($3));
delete $1; delete $1;
} }
; ;
@ -1127,7 +1125,7 @@ apply_for_specifier: /* empty */
optional_rterm: /* empty */ optional_rterm: /* empty */
{ {
$$ = MakeLiteral(); $$ = MakeLiteralRaw();
} }
| rterm | rterm
; ;
@ -1189,26 +1187,26 @@ apply:
if (!seen_assign && !context->m_FTerm.top()) if (!seen_assign && !context->m_FTerm.top())
BOOST_THROW_EXCEPTION(ScriptError("'apply' is missing 'assign'/'for'", DebugInfoRange(@2, @3))); BOOST_THROW_EXCEPTION(ScriptError("'apply' is missing 'assign'/'for'", DebugInfoRange(@2, @3)));
Expression *ignore = context->m_Ignore.top(); std::unique_ptr<Expression> ignore{context->m_Ignore.top()};
context->m_Ignore.pop(); context->m_Ignore.pop();
Expression *assign; std::unique_ptr<Expression> assign;
if (!seen_assign) if (!seen_assign)
assign = MakeLiteral(true); assign = MakeLiteral(true);
else else
assign = context->m_Assign.top(); assign.reset(context->m_Assign.top());
context->m_Assign.pop(); context->m_Assign.pop();
Expression *filter; std::unique_ptr<Expression> filter;
if (ignore) { if (ignore) {
Expression *rex = new LogicalNegateExpression(ignore, DebugInfoRange(@2, @5)); std::unique_ptr<Expression>rex{new LogicalNegateExpression(std::move(ignore), DebugInfoRange(@2, @5))};
filter = new LogicalAndExpression(assign, rex, DebugInfoRange(@2, @5)); filter.reset(new LogicalAndExpression(std::move(assign), std::move(rex), DebugInfoRange(@2, @5)));
} else } else
filter = assign; filter.swap(assign);
String fkvar = context->m_FKVar.top(); String fkvar = context->m_FKVar.top();
context->m_FKVar.pop(); context->m_FKVar.pop();
@ -1216,10 +1214,11 @@ apply:
String fvvar = context->m_FVVar.top(); String fvvar = context->m_FVVar.top();
context->m_FVVar.pop(); context->m_FVVar.pop();
Expression *fterm = context->m_FTerm.top(); std::unique_ptr<Expression> fterm{context->m_FTerm.top()};
context->m_FTerm.pop(); context->m_FTerm.pop();
$$ = new ApplyExpression(type, target, $4, filter, context->GetPackage(), fkvar, fvvar, fterm, $7, $8, $10, DebugInfoRange(@2, @8)); $$ = new ApplyExpression(type, target, std::unique_ptr<Expression>($4), std::move(filter), context->GetPackage(), fkvar, fvvar, std::move(fterm), std::move(*$7), $8, std::unique_ptr<Expression>($10), DebugInfoRange(@2, @8));
delete $7;
} }
; ;

View File

@ -109,12 +109,11 @@ String ConfigCompiler::GetPackage(void) const
return m_Package; return m_Package;
} }
void ConfigCompiler::CollectIncludes(std::vector<Expression *>& expressions, void ConfigCompiler::CollectIncludes(std::vector<std::unique_ptr<Expression> >& expressions,
const String& file, const String& zone, const String& package) const String& file, const String& zone, const String& package)
{ {
try { try {
Expression *expr = CompileFile(file, zone, package); expressions.emplace_back(CompileFile(file, zone, package));
expressions.push_back(expr);
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
Log(LogWarning, "ConfigCompiler") Log(LogWarning, "ConfigCompiler")
<< "Cannot compile file '" << "Cannot compile file '"
@ -130,7 +129,7 @@ void ConfigCompiler::CollectIncludes(std::vector<Expression *>& expressions,
* @param search Whether to search global include dirs. * @param search Whether to search global include dirs.
* @param debuginfo Debug information. * @param debuginfo Debug information.
*/ */
Expression *ConfigCompiler::HandleInclude(const String& relativeBase, const String& path, std::unique_ptr<Expression> ConfigCompiler::HandleInclude(const String& relativeBase, const String& path,
bool search, const String& zone, const String& package, const DebugInfo& debuginfo) bool search, const String& zone, const String& package, const DebugInfo& debuginfo)
{ {
String upath; String upath;
@ -153,7 +152,7 @@ Expression *ConfigCompiler::HandleInclude(const String& relativeBase, const Stri
} }
} }
std::vector<Expression *> expressions; std::vector<std::unique_ptr<Expression> > expressions;
if (!Utility::Glob(includePath, std::bind(&ConfigCompiler::CollectIncludes, std::ref(expressions), _1, zone, package), GlobFile) && includePath.FindFirstOf("*?") == String::NPos) { if (!Utility::Glob(includePath, std::bind(&ConfigCompiler::CollectIncludes, std::ref(expressions), _1, zone, package), GlobFile) && includePath.FindFirstOf("*?") == String::NPos) {
std::ostringstream msgbuf; std::ostringstream msgbuf;
@ -161,9 +160,9 @@ Expression *ConfigCompiler::HandleInclude(const String& relativeBase, const Stri
BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debuginfo)); BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debuginfo));
} }
DictExpression *expr = new DictExpression(expressions); std::unique_ptr<DictExpression> expr{new DictExpression(std::move(expressions))};
expr->MakeInline(); expr->MakeInline();
return expr; return std::move(expr);
} }
/** /**
@ -174,7 +173,7 @@ Expression *ConfigCompiler::HandleInclude(const String& relativeBase, const Stri
* @param pattern The file pattern. * @param pattern The file pattern.
* @param debuginfo Debug information. * @param debuginfo Debug information.
*/ */
Expression *ConfigCompiler::HandleIncludeRecursive(const String& relativeBase, const String& path, std::unique_ptr<Expression> ConfigCompiler::HandleIncludeRecursive(const String& relativeBase, const String& path,
const String& pattern, const String& zone, const String& package, const DebugInfo&) const String& pattern, const String& zone, const String& package, const DebugInfo&)
{ {
String ppath; String ppath;
@ -184,15 +183,15 @@ Expression *ConfigCompiler::HandleIncludeRecursive(const String& relativeBase, c
else else
ppath = relativeBase + "/" + path; ppath = relativeBase + "/" + path;
std::vector<Expression *> expressions; std::vector<std::unique_ptr<Expression> > expressions;
Utility::GlobRecursive(ppath, pattern, std::bind(&ConfigCompiler::CollectIncludes, std::ref(expressions), _1, zone, package), GlobFile); Utility::GlobRecursive(ppath, pattern, std::bind(&ConfigCompiler::CollectIncludes, std::ref(expressions), _1, zone, package), GlobFile);
DictExpression *dict = new DictExpression(expressions); std::unique_ptr<DictExpression> dict{new DictExpression(std::move(expressions))};
dict->MakeInline(); dict->MakeInline();
return dict; return std::move(dict);
} }
void ConfigCompiler::HandleIncludeZone(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, std::vector<Expression *>& expressions) void ConfigCompiler::HandleIncludeZone(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, std::vector<std::unique_ptr<Expression> >& expressions)
{ {
String zoneName = Utility::BaseName(path); String zoneName = Utility::BaseName(path);
@ -217,7 +216,7 @@ void ConfigCompiler::HandleIncludeZone(const String& relativeBase, const String&
* @param pattern The file pattern. * @param pattern The file pattern.
* @param debuginfo Debug information. * @param debuginfo Debug information.
*/ */
Expression *ConfigCompiler::HandleIncludeZones(const String& relativeBase, const String& tag, std::unique_ptr<Expression> ConfigCompiler::HandleIncludeZones(const String& relativeBase, const String& tag,
const String& path, const String& pattern, const String& package, const DebugInfo&) const String& path, const String& pattern, const String& package, const DebugInfo&)
{ {
String ppath; String ppath;
@ -230,9 +229,9 @@ Expression *ConfigCompiler::HandleIncludeZones(const String& relativeBase, const
newRelativeBase = "."; newRelativeBase = ".";
} }
std::vector<Expression *> expressions; std::vector<std::unique_ptr<Expression> > expressions;
Utility::Glob(ppath + "/*", std::bind(&ConfigCompiler::HandleIncludeZone, newRelativeBase, tag, _1, pattern, package, std::ref(expressions)), GlobDirectory); Utility::Glob(ppath + "/*", std::bind(&ConfigCompiler::HandleIncludeZone, newRelativeBase, tag, _1, pattern, package, std::ref(expressions)), GlobDirectory);
return new DictExpression(expressions); return std::unique_ptr<Expression>(new DictExpression(std::move(expressions)));
} }
/** /**
@ -242,7 +241,7 @@ Expression *ConfigCompiler::HandleIncludeZones(const String& relativeBase, const
* @param stream The input stream. * @param stream The input stream.
* @returns Configuration items. * @returns Configuration items.
*/ */
Expression *ConfigCompiler::CompileStream(const String& path, std::unique_ptr<Expression> ConfigCompiler::CompileStream(const String& path,
std::istream *stream, const String& zone, const String& package) std::istream *stream, const String& zone, const String& package)
{ {
CONTEXT("Compiling configuration stream with name '" + path + "'"); CONTEXT("Compiling configuration stream with name '" + path + "'");
@ -254,9 +253,9 @@ Expression *ConfigCompiler::CompileStream(const String& path,
try { try {
return ctx.Compile(); return ctx.Compile();
} catch (const ScriptError& ex) { } catch (const ScriptError& ex) {
return new ThrowExpression(MakeLiteral(ex.what()), ex.IsIncompleteExpression(), ex.GetDebugInfo()); return std::unique_ptr<Expression>(new ThrowExpression(MakeLiteral(ex.what()), ex.IsIncompleteExpression(), ex.GetDebugInfo()));
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
return new ThrowExpression(MakeLiteral(DiagnosticInformation(ex)), false); return std::unique_ptr<Expression>(new ThrowExpression(MakeLiteral(DiagnosticInformation(ex)), false));
} }
} }
@ -266,7 +265,7 @@ Expression *ConfigCompiler::CompileStream(const String& path,
* @param path The path. * @param path The path.
* @returns Configuration items. * @returns Configuration items.
*/ */
Expression *ConfigCompiler::CompileFile(const String& path, const String& zone, std::unique_ptr<Expression> ConfigCompiler::CompileFile(const String& path, const String& zone,
const String& package) const String& package)
{ {
CONTEXT("Compiling configuration file '" + path + "'"); CONTEXT("Compiling configuration file '" + path + "'");
@ -292,7 +291,7 @@ Expression *ConfigCompiler::CompileFile(const String& path, const String& zone,
* @param text The text. * @param text The text.
* @returns Configuration items. * @returns Configuration items.
*/ */
Expression *ConfigCompiler::CompileText(const String& path, const String& text, std::unique_ptr<Expression> ConfigCompiler::CompileText(const String& path, const String& text,
const String& zone, const String& package) const String& zone, const String& package)
{ {
std::stringstream stream(text); std::stringstream stream(text);

View File

@ -90,13 +90,13 @@ public:
const String& zone = String(), const String& package = String()); const String& zone = String(), const String& package = String());
virtual ~ConfigCompiler(void); virtual ~ConfigCompiler(void);
Expression *Compile(void); std::unique_ptr<Expression> Compile(void);
static Expression *CompileStream(const String& path, std::istream *stream, static std::unique_ptr<Expression>CompileStream(const String& path, std::istream *stream,
const String& zone = String(), const String& package = String()); const String& zone = String(), const String& package = String());
static Expression *CompileFile(const String& path, const String& zone = String(), static std::unique_ptr<Expression>CompileFile(const String& path, const String& zone = String(),
const String& package = String()); const String& package = String());
static Expression *CompileText(const String& path, const String& text, static std::unique_ptr<Expression>CompileText(const String& path, const String& text,
const String& zone = String(), const String& package = String()); const String& zone = String(), const String& package = String());
static void AddIncludeSearchDir(const String& dir); static void AddIncludeSearchDir(const String& dir);
@ -109,14 +109,14 @@ public:
void SetPackage(const String& package); void SetPackage(const String& package);
String GetPackage(void) const; String GetPackage(void) const;
static void CollectIncludes(std::vector<Expression *>& expressions, static void CollectIncludes(std::vector<std::unique_ptr<Expression> >& expressions,
const String& file, const String& zone, const String& package); const String& file, const String& zone, const String& package);
static Expression *HandleInclude(const String& relativeBase, const String& path, bool search, static std::unique_ptr<Expression> HandleInclude(const String& relativeBase, const String& path, bool search,
const String& zone, const String& package, const DebugInfo& debuginfo = DebugInfo()); const String& zone, const String& package, const DebugInfo& debuginfo = DebugInfo());
static Expression *HandleIncludeRecursive(const String& relativeBase, const String& path, static std::unique_ptr<Expression> HandleIncludeRecursive(const String& relativeBase, const String& path,
const String& pattern, const String& zone, const String& package, const DebugInfo& debuginfo = DebugInfo()); const String& pattern, const String& zone, const String& package, const DebugInfo& debuginfo = DebugInfo());
static Expression *HandleIncludeZones(const String& relativeBase, const String& tag, static std::unique_ptr<Expression> HandleIncludeZones(const String& relativeBase, const String& tag,
const String& path, const String& pattern, const String& package, const DebugInfo& debuginfo = DebugInfo()); const String& path, const String& pattern, const String& package, const DebugInfo& debuginfo = DebugInfo());
size_t ReadInput(char *buffer, size_t max_bytes); size_t ReadInput(char *buffer, size_t max_bytes);
@ -144,7 +144,7 @@ private:
void InitializeScanner(void); void InitializeScanner(void);
void DestroyScanner(void); void DestroyScanner(void);
static void HandleIncludeZone(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, std::vector<Expression *>& expressions); static void HandleIncludeZone(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, std::vector<std::unique_ptr<Expression> >& expressions);
static bool IsAbsolutePath(const String& path); static bool IsAbsolutePath(const String& path);

View File

@ -28,7 +28,7 @@
#define REGISTER_CONFIG_FRAGMENT(name, fragment) \ #define REGISTER_CONFIG_FRAGMENT(name, fragment) \
INITIALIZE_ONCE_WITH_PRIORITY([]() { \ INITIALIZE_ONCE_WITH_PRIORITY([]() { \
icinga::Expression *expression = icinga::ConfigCompiler::CompileText(name, fragment); \ std::unique_ptr<icinga::Expression> expression = icinga::ConfigCompiler::CompileText(name, fragment); \
VERIFY(expression); \ VERIFY(expression); \
try { \ try { \
icinga::ScriptFrame frame; \ icinga::ScriptFrame frame; \
@ -37,7 +37,6 @@
std::cerr << icinga::DiagnosticInformation(ex) << std::endl; \ std::cerr << icinga::DiagnosticInformation(ex) << std::endl; \
icinga::Application::Exit(1); \ icinga::Application::Exit(1); \
} \ } \
delete expression; \
}, 5) }, 5)
#endif /* CONFIGFRAGMENT_H */ #endif /* CONFIGFRAGMENT_H */

View File

@ -580,7 +580,7 @@ bool ConfigItem::ActivateItems(WorkQueue& upq, const std::vector<ConfigItem::Ptr
if (withModAttrs) { if (withModAttrs) {
/* restore modified attributes */ /* restore modified attributes */
if (Utility::PathExists(Application::GetModAttrPath())) { if (Utility::PathExists(Application::GetModAttrPath())) {
Expression *expression = ConfigCompiler::CompileFile(Application::GetModAttrPath()); std::unique_ptr<Expression> expression = ConfigCompiler::CompileFile(Application::GetModAttrPath());
if (expression) { if (expression) {
try { try {
@ -590,8 +590,6 @@ bool ConfigItem::ActivateItems(WorkQueue& upq, const std::vector<ConfigItem::Ptr
Log(LogCritical, "config", DiagnosticInformation(ex)); Log(LogCritical, "config", DiagnosticInformation(ex));
} }
} }
delete expression;
} }
} }

View File

@ -70,7 +70,7 @@ void ConfigItemBuilder::SetPackage(const String& package)
void ConfigItemBuilder::AddExpression(Expression *expr) void ConfigItemBuilder::AddExpression(Expression *expr)
{ {
m_Expressions.push_back(expr); m_Expressions.emplace_back(expr);
} }
void ConfigItemBuilder::SetFilter(const std::shared_ptr<Expression>& filter) void ConfigItemBuilder::SetFilter(const std::shared_ptr<Expression>& filter)
@ -110,24 +110,20 @@ ConfigItem::Ptr ConfigItemBuilder::Compile(void)
BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), m_DebugInfo)); BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), m_DebugInfo));
} }
std::vector<Expression *> exprs; std::vector<std::unique_ptr<Expression> > exprs;
Array::Ptr templateArray = new Array(); Array::Ptr templateArray = new Array();
templateArray->Add(m_Name); templateArray->Add(m_Name);
exprs.push_back(new SetExpression(MakeIndexer(ScopeThis, "templates"), OpSetAdd, exprs.emplace_back(new SetExpression(MakeIndexer(ScopeThis, "templates"), OpSetAdd,
new LiteralExpression(templateArray), m_DebugInfo)); std::unique_ptr<LiteralExpression>(new LiteralExpression(templateArray)), m_DebugInfo));
DictExpression *dexpr = new DictExpression(m_Expressions, m_DebugInfo);
dexpr->MakeInline();
exprs.push_back(dexpr);
#ifdef I2_DEBUG #ifdef I2_DEBUG
if (!m_Abstract) { if (!m_Abstract) {
bool foundDefaultImport = false; bool foundDefaultImport = false;
for (Expression *expr : m_Expressions) { for (const std::unique_ptr<Expression>& expr : m_Expressions) {
if (dynamic_cast<ImportDefaultTemplatesExpression *>(expr)) { if (dynamic_cast<ImportDefaultTemplatesExpression *>(expr.get())) {
foundDefaultImport = true; foundDefaultImport = true;
break; break;
} }
@ -137,7 +133,11 @@ ConfigItem::Ptr ConfigItemBuilder::Compile(void)
} }
#endif /* I2_DEBUG */ #endif /* I2_DEBUG */
std::shared_ptr<DictExpression> exprl = std::make_shared<DictExpression>(exprs, m_DebugInfo); DictExpression *dexpr = new DictExpression(std::move(m_Expressions), m_DebugInfo);
dexpr->MakeInline();
exprs.emplace_back(dexpr);
std::shared_ptr<DictExpression> exprl = std::make_shared<DictExpression>(std::move(exprs), m_DebugInfo);
exprl->MakeInline(); exprl->MakeInline();
return new ConfigItem(m_Type, m_Name, m_Abstract, exprl, m_Filter, return new ConfigItem(m_Type, m_Name, m_Abstract, exprl, m_Filter,

View File

@ -60,7 +60,7 @@ private:
Type::Ptr m_Type; /**< The object type. */ Type::Ptr m_Type; /**< The object type. */
String m_Name; /**< The name. */ String m_Name; /**< The name. */
bool m_Abstract; /**< Whether the item is abstract. */ bool m_Abstract; /**< Whether the item is abstract. */
std::vector<Expression *> m_Expressions; /**< Expressions for this item. */ std::vector<std::unique_ptr<Expression> > m_Expressions; /**< Expressions for this item. */
std::shared_ptr<Expression> m_Filter; /**< Filter expression. */ std::shared_ptr<Expression> m_Filter; /**< Filter expression. */
DebugInfo m_DebugInfo; /**< Debug information. */ DebugInfo m_DebugInfo; /**< Debug information. */
Dictionary::Ptr m_Scope; /**< variable scope. */ Dictionary::Ptr m_Scope; /**< variable scope. */

View File

@ -90,10 +90,10 @@ const DebugInfo& Expression::GetDebugInfo(void) const
return debugInfo; return debugInfo;
} }
Expression *icinga::MakeIndexer(ScopeSpecifier scopeSpec, const String& index) std::unique_ptr<Expression> icinga::MakeIndexer(ScopeSpecifier scopeSpec, const String& index)
{ {
Expression *scope = new GetScopeExpression(scopeSpec); std::unique_ptr<Expression> scope{new GetScopeExpression(scopeSpec)};
return new IndexerExpression(scope, MakeLiteral(index)); return std::unique_ptr<Expression>(new IndexerExpression(std::move(scope), MakeLiteral(index)));
} }
void DictExpression::MakeInline(void) void DictExpression::MakeInline(void)
@ -429,7 +429,7 @@ ExpressionResult FunctionCallExpression::DoEvaluate(ScriptFrame& frame, DebugHin
if (vfunc.IsObjectType<Type>()) { if (vfunc.IsObjectType<Type>()) {
std::vector<Value> arguments; std::vector<Value> arguments;
arguments.reserve(m_Args.size()); arguments.reserve(m_Args.size());
for (Expression *arg : m_Args) { for (const auto& arg : m_Args) {
ExpressionResult argres = arg->Evaluate(frame); ExpressionResult argres = arg->Evaluate(frame);
CHECK_RESULT(argres); CHECK_RESULT(argres);
@ -449,7 +449,7 @@ ExpressionResult FunctionCallExpression::DoEvaluate(ScriptFrame& frame, DebugHin
std::vector<Value> arguments; std::vector<Value> arguments;
arguments.reserve(m_Args.size()); arguments.reserve(m_Args.size());
for (Expression *arg : m_Args) { for (const auto& arg : m_Args) {
ExpressionResult argres = arg->Evaluate(frame); ExpressionResult argres = arg->Evaluate(frame);
CHECK_RESULT(argres); CHECK_RESULT(argres);
@ -464,7 +464,7 @@ ExpressionResult ArrayExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhin
Array::Ptr result = new Array(); Array::Ptr result = new Array();
result->Reserve(m_Expressions.size()); result->Reserve(m_Expressions.size());
for (Expression *aexpr : m_Expressions) { for (const auto& aexpr : m_Expressions) {
ExpressionResult element = aexpr->Evaluate(frame); ExpressionResult element = aexpr->Evaluate(frame);
CHECK_RESULT(element); CHECK_RESULT(element);
@ -486,7 +486,7 @@ ExpressionResult DictExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint
Value result; Value result;
try { try {
for (Expression *aexpr : m_Expressions) { for (const auto& aexpr : m_Expressions) {
ExpressionResult element = aexpr->Evaluate(frame, m_Inline ? dhint : nullptr); ExpressionResult element = aexpr->Evaluate(frame, m_Inline ? dhint : nullptr);
CHECK_RESULT(element); CHECK_RESULT(element);
result = element.GetValue(); result = element.GetValue();
@ -683,18 +683,18 @@ bool IndexerExpression::GetReference(ScriptFrame& frame, bool init_dict, Value *
return true; return true;
} }
void icinga::BindToScope(Expression *& expr, ScopeSpecifier scopeSpec) void icinga::BindToScope(std::unique_ptr<Expression>& expr, ScopeSpecifier scopeSpec)
{ {
DictExpression *dexpr = dynamic_cast<DictExpression *>(expr); DictExpression *dexpr = dynamic_cast<DictExpression *>(expr.get());
if (dexpr) { if (dexpr) {
for (Expression *& expr : dexpr->m_Expressions) for (auto& expr : dexpr->m_Expressions)
BindToScope(expr, scopeSpec); BindToScope(expr, scopeSpec);
return; return;
} }
SetExpression *aexpr = dynamic_cast<SetExpression *>(expr); SetExpression *aexpr = dynamic_cast<SetExpression *>(expr.get());
if (aexpr) { if (aexpr) {
BindToScope(aexpr->m_Operand1, scopeSpec); BindToScope(aexpr->m_Operand1, scopeSpec);
@ -702,27 +702,25 @@ void icinga::BindToScope(Expression *& expr, ScopeSpecifier scopeSpec)
return; return;
} }
IndexerExpression *iexpr = dynamic_cast<IndexerExpression *>(expr); IndexerExpression *iexpr = dynamic_cast<IndexerExpression *>(expr.get());
if (iexpr) { if (iexpr) {
BindToScope(iexpr->m_Operand1, scopeSpec); BindToScope(iexpr->m_Operand1, scopeSpec);
return; return;
} }
LiteralExpression *lexpr = dynamic_cast<LiteralExpression *>(expr); LiteralExpression *lexpr = dynamic_cast<LiteralExpression *>(expr.get());
if (lexpr && lexpr->GetValue().IsString()) { if (lexpr && lexpr->GetValue().IsString()) {
Expression *scope = new GetScopeExpression(scopeSpec); std::unique_ptr<Expression> scope{new GetScopeExpression(scopeSpec)};
expr = new IndexerExpression(scope, lexpr, lexpr->GetDebugInfo()); expr.reset(new IndexerExpression(std::move(scope), std::move(expr), lexpr->GetDebugInfo()));
} }
VariableExpression *vexpr = dynamic_cast<VariableExpression *>(expr); VariableExpression *vexpr = dynamic_cast<VariableExpression *>(expr.get());
if (vexpr) { if (vexpr) {
Expression *scope = new GetScopeExpression(scopeSpec); std::unique_ptr<Expression> scope{new GetScopeExpression(scopeSpec)};
Expression *new_expr = new IndexerExpression(scope, MakeLiteral(vexpr->GetVariable()), vexpr->GetDebugInfo()); expr.reset(new IndexerExpression(std::move(scope), MakeLiteral(vexpr->GetVariable()), vexpr->GetDebugInfo()));
delete expr;
expr = new_expr;
} }
} }
@ -852,7 +850,7 @@ ExpressionResult IncludeExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dh
if (frame.Sandboxed) if (frame.Sandboxed)
BOOST_THROW_EXCEPTION(ScriptError("Includes are not allowed in sandbox mode.", m_DebugInfo)); BOOST_THROW_EXCEPTION(ScriptError("Includes are not allowed in sandbox mode.", m_DebugInfo));
Expression *expr; std::unique_ptr<Expression> expr;
String name, path, pattern; String name, path, pattern;
switch (m_Type) { switch (m_Type) {
@ -910,12 +908,9 @@ ExpressionResult IncludeExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dh
try { try {
res = expr->Evaluate(frame, dhint); res = expr->Evaluate(frame, dhint);
} catch (const std::exception&) { } catch (const std::exception&) {
delete expr;
throw; throw;
} }
delete expr;
return res; return res;
} }

View File

@ -211,7 +211,7 @@ public:
static void ScriptBreakpoint(ScriptFrame& frame, ScriptError *ex, const DebugInfo& di); static void ScriptBreakpoint(ScriptFrame& frame, ScriptError *ex, const DebugInfo& di);
}; };
I2_CONFIG_API Expression *MakeIndexer(ScopeSpecifier scopeSpec, const String& index); I2_CONFIG_API std::unique_ptr<Expression> MakeIndexer(ScopeSpecifier scopeSpec, const String& index);
class I2_CONFIG_API OwnedExpression : public Expression class I2_CONFIG_API OwnedExpression : public Expression
{ {
@ -252,11 +252,16 @@ private:
Value m_Value; Value m_Value;
}; };
inline LiteralExpression *MakeLiteral(const Value& literal = Value()) inline LiteralExpression *MakeLiteralRaw(const Value& literal = Value())
{ {
return new LiteralExpression(literal); return new LiteralExpression(literal);
} }
inline std::unique_ptr<LiteralExpression> MakeLiteral(const Value& literal = Value())
{
return std::unique_ptr<LiteralExpression>(MakeLiteralRaw(literal));
}
class I2_CONFIG_API DebuggableExpression : public Expression class I2_CONFIG_API DebuggableExpression : public Expression
{ {
public: public:
@ -273,35 +278,24 @@ protected:
class I2_CONFIG_API UnaryExpression : public DebuggableExpression class I2_CONFIG_API UnaryExpression : public DebuggableExpression
{ {
public: public:
UnaryExpression(Expression *operand, const DebugInfo& debugInfo = DebugInfo()) UnaryExpression(std::unique_ptr<Expression> operand, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_Operand(operand) : DebuggableExpression(debugInfo), m_Operand(std::move(operand))
{ } { }
~UnaryExpression(void)
{
delete m_Operand;
}
protected: protected:
Expression *m_Operand; std::unique_ptr<Expression> m_Operand;
}; };
class I2_CONFIG_API BinaryExpression : public DebuggableExpression class I2_CONFIG_API BinaryExpression : public DebuggableExpression
{ {
public: public:
BinaryExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) BinaryExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_Operand1(operand1), m_Operand2(operand2) : DebuggableExpression(debugInfo), m_Operand1(std::move(operand1)), m_Operand2(std::move(operand2))
{ } { }
~BinaryExpression(void)
{
delete m_Operand1;
delete m_Operand2;
}
protected: protected:
Expression *m_Operand1; std::unique_ptr<Expression> m_Operand1;
Expression *m_Operand2; std::unique_ptr<Expression> m_Operand2;
}; };
class I2_CONFIG_API VariableExpression : public DebuggableExpression class I2_CONFIG_API VariableExpression : public DebuggableExpression
@ -323,14 +317,14 @@ protected:
private: private:
String m_Variable; String m_Variable;
friend I2_CONFIG_API void BindToScope(Expression *& expr, ScopeSpecifier scopeSpec); friend I2_CONFIG_API void BindToScope(std::unique_ptr<Expression>& expr, ScopeSpecifier scopeSpec);
}; };
class I2_CONFIG_API NegateExpression : public UnaryExpression class I2_CONFIG_API NegateExpression : public UnaryExpression
{ {
public: public:
NegateExpression(Expression *operand, const DebugInfo& debugInfo = DebugInfo()) NegateExpression(std::unique_ptr<Expression> operand, const DebugInfo& debugInfo = DebugInfo())
: UnaryExpression(operand, debugInfo) : UnaryExpression(std::move(operand), debugInfo)
{ } { }
protected: protected:
@ -340,8 +334,8 @@ protected:
class I2_CONFIG_API LogicalNegateExpression : public UnaryExpression class I2_CONFIG_API LogicalNegateExpression : public UnaryExpression
{ {
public: public:
LogicalNegateExpression(Expression *operand, const DebugInfo& debugInfo = DebugInfo()) LogicalNegateExpression(std::unique_ptr<Expression> operand, const DebugInfo& debugInfo = DebugInfo())
: UnaryExpression(operand, debugInfo) : UnaryExpression(std::move(operand), debugInfo)
{ } { }
protected: protected:
@ -351,8 +345,8 @@ protected:
class I2_CONFIG_API AddExpression : public BinaryExpression class I2_CONFIG_API AddExpression : public BinaryExpression
{ {
public: public:
AddExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) AddExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -362,8 +356,8 @@ protected:
class I2_CONFIG_API SubtractExpression : public BinaryExpression class I2_CONFIG_API SubtractExpression : public BinaryExpression
{ {
public: public:
SubtractExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) SubtractExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -373,8 +367,8 @@ protected:
class I2_CONFIG_API MultiplyExpression : public BinaryExpression class I2_CONFIG_API MultiplyExpression : public BinaryExpression
{ {
public: public:
MultiplyExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) MultiplyExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -384,8 +378,8 @@ protected:
class I2_CONFIG_API DivideExpression : public BinaryExpression class I2_CONFIG_API DivideExpression : public BinaryExpression
{ {
public: public:
DivideExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) DivideExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -395,8 +389,8 @@ protected:
class I2_CONFIG_API ModuloExpression : public BinaryExpression class I2_CONFIG_API ModuloExpression : public BinaryExpression
{ {
public: public:
ModuloExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) ModuloExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -406,8 +400,8 @@ protected:
class I2_CONFIG_API XorExpression : public BinaryExpression class I2_CONFIG_API XorExpression : public BinaryExpression
{ {
public: public:
XorExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) XorExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -417,8 +411,8 @@ protected:
class I2_CONFIG_API BinaryAndExpression : public BinaryExpression class I2_CONFIG_API BinaryAndExpression : public BinaryExpression
{ {
public: public:
BinaryAndExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) BinaryAndExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -428,8 +422,8 @@ protected:
class I2_CONFIG_API BinaryOrExpression : public BinaryExpression class I2_CONFIG_API BinaryOrExpression : public BinaryExpression
{ {
public: public:
BinaryOrExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) BinaryOrExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -439,8 +433,8 @@ protected:
class I2_CONFIG_API ShiftLeftExpression : public BinaryExpression class I2_CONFIG_API ShiftLeftExpression : public BinaryExpression
{ {
public: public:
ShiftLeftExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) ShiftLeftExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -450,8 +444,8 @@ protected:
class I2_CONFIG_API ShiftRightExpression : public BinaryExpression class I2_CONFIG_API ShiftRightExpression : public BinaryExpression
{ {
public: public:
ShiftRightExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) ShiftRightExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -461,8 +455,8 @@ protected:
class I2_CONFIG_API EqualExpression : public BinaryExpression class I2_CONFIG_API EqualExpression : public BinaryExpression
{ {
public: public:
EqualExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) EqualExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -472,8 +466,8 @@ protected:
class I2_CONFIG_API NotEqualExpression : public BinaryExpression class I2_CONFIG_API NotEqualExpression : public BinaryExpression
{ {
public: public:
NotEqualExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) NotEqualExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -483,8 +477,8 @@ protected:
class I2_CONFIG_API LessThanExpression : public BinaryExpression class I2_CONFIG_API LessThanExpression : public BinaryExpression
{ {
public: public:
LessThanExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) LessThanExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -494,8 +488,8 @@ protected:
class I2_CONFIG_API GreaterThanExpression : public BinaryExpression class I2_CONFIG_API GreaterThanExpression : public BinaryExpression
{ {
public: public:
GreaterThanExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) GreaterThanExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -505,8 +499,8 @@ protected:
class I2_CONFIG_API LessThanOrEqualExpression : public BinaryExpression class I2_CONFIG_API LessThanOrEqualExpression : public BinaryExpression
{ {
public: public:
LessThanOrEqualExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) LessThanOrEqualExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -516,8 +510,8 @@ protected:
class I2_CONFIG_API GreaterThanOrEqualExpression : public BinaryExpression class I2_CONFIG_API GreaterThanOrEqualExpression : public BinaryExpression
{ {
public: public:
GreaterThanOrEqualExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) GreaterThanOrEqualExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -527,8 +521,8 @@ protected:
class I2_CONFIG_API InExpression : public BinaryExpression class I2_CONFIG_API InExpression : public BinaryExpression
{ {
public: public:
InExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) InExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -538,8 +532,8 @@ protected:
class I2_CONFIG_API NotInExpression : public BinaryExpression class I2_CONFIG_API NotInExpression : public BinaryExpression
{ {
public: public:
NotInExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) NotInExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -549,8 +543,8 @@ protected:
class I2_CONFIG_API LogicalAndExpression : public BinaryExpression class I2_CONFIG_API LogicalAndExpression : public BinaryExpression
{ {
public: public:
LogicalAndExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) LogicalAndExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -560,8 +554,8 @@ protected:
class I2_CONFIG_API LogicalOrExpression : public BinaryExpression class I2_CONFIG_API LogicalOrExpression : public BinaryExpression
{ {
public: public:
LogicalOrExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) LogicalOrExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
@ -571,76 +565,56 @@ 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(std::unique_ptr<Expression> fname, std::vector<std::unique_ptr<Expression> >&& args, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_FName(fname), m_Args(args) : DebuggableExpression(debugInfo), m_FName(std::move(fname)), m_Args(std::move(args))
{ } { }
~FunctionCallExpression(void)
{
delete m_FName;
for (Expression *expr : m_Args)
delete expr;
}
protected: protected:
virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
public: public:
Expression *m_FName; std::unique_ptr<Expression> m_FName;
std::vector<Expression *> m_Args; std::vector<std::unique_ptr<Expression> > m_Args;
}; };
class I2_CONFIG_API ArrayExpression : public DebuggableExpression class I2_CONFIG_API ArrayExpression : public DebuggableExpression
{ {
public: public:
ArrayExpression(const std::vector<Expression *>& expressions, const DebugInfo& debugInfo = DebugInfo()) ArrayExpression(std::vector<std::unique_ptr<Expression > >&& expressions, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_Expressions(expressions) : DebuggableExpression(debugInfo), m_Expressions(std::move(expressions))
{ } { }
~ArrayExpression(void)
{
for (Expression *expr : m_Expressions)
delete expr;
}
protected: protected:
virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
private: private:
std::vector<Expression *> m_Expressions; std::vector<std::unique_ptr<Expression> > m_Expressions;
}; };
class I2_CONFIG_API DictExpression : public DebuggableExpression class I2_CONFIG_API DictExpression : public DebuggableExpression
{ {
public: public:
DictExpression(const std::vector<Expression *>& expressions = std::vector<Expression *>(), const DebugInfo& debugInfo = DebugInfo()) DictExpression(std::vector<std::unique_ptr<Expression> >&& expressions = {}, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_Expressions(expressions), m_Inline(false) : DebuggableExpression(debugInfo), m_Expressions(std::move(expressions)), m_Inline(false)
{ } { }
~DictExpression(void)
{
for (Expression *expr : m_Expressions)
delete expr;
}
void MakeInline(void); void MakeInline(void);
protected: protected:
virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
private: private:
std::vector<Expression *> m_Expressions; std::vector<std::unique_ptr<Expression> > m_Expressions;
bool m_Inline; bool m_Inline;
friend I2_CONFIG_API void BindToScope(Expression *& expr, ScopeSpecifier scopeSpec); friend I2_CONFIG_API void BindToScope(std::unique_ptr<Expression>& expr, ScopeSpecifier scopeSpec);
}; };
class I2_CONFIG_API SetExpression : public BinaryExpression class I2_CONFIG_API SetExpression : public BinaryExpression
{ {
public: public:
SetExpression(Expression *operand1, CombinedSetOp op, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) SetExpression(std::unique_ptr<Expression> operand1, CombinedSetOp op, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo), m_Op(op) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo), m_Op(op)
{ } { }
protected: protected:
@ -649,59 +623,46 @@ protected:
private: private:
CombinedSetOp m_Op; CombinedSetOp m_Op;
friend I2_CONFIG_API void BindToScope(Expression *& expr, ScopeSpecifier scopeSpec); friend I2_CONFIG_API void BindToScope(std::unique_ptr<Expression>& expr, ScopeSpecifier scopeSpec);
}; };
class I2_CONFIG_API ConditionalExpression : public DebuggableExpression class I2_CONFIG_API ConditionalExpression : public DebuggableExpression
{ {
public: public:
ConditionalExpression(Expression *condition, Expression *true_branch, Expression *false_branch, const DebugInfo& debugInfo = DebugInfo()) ConditionalExpression(std::unique_ptr<Expression> condition, std::unique_ptr<Expression> true_branch, std::unique_ptr<Expression> false_branch, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_Condition(condition), m_TrueBranch(true_branch), m_FalseBranch(false_branch) : DebuggableExpression(debugInfo), m_Condition(std::move(condition)), m_TrueBranch(std::move(true_branch)), m_FalseBranch(std::move(false_branch))
{ } { }
~ConditionalExpression(void)
{
delete m_Condition;
delete m_TrueBranch;
delete m_FalseBranch;
}
protected: protected:
virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
private: private:
Expression *m_Condition; std::unique_ptr<Expression> m_Condition;
Expression *m_TrueBranch; std::unique_ptr<Expression> m_TrueBranch;
Expression *m_FalseBranch; std::unique_ptr<Expression> m_FalseBranch;
}; };
class I2_CONFIG_API WhileExpression : public DebuggableExpression class I2_CONFIG_API WhileExpression : public DebuggableExpression
{ {
public: public:
WhileExpression(Expression *condition, Expression *loop_body, const DebugInfo& debugInfo = DebugInfo()) WhileExpression(std::unique_ptr<Expression> condition, std::unique_ptr<Expression> loop_body, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_Condition(condition), m_LoopBody(loop_body) : DebuggableExpression(debugInfo), m_Condition(std::move(condition)), m_LoopBody(std::move(loop_body))
{ } { }
~WhileExpression(void)
{
delete m_Condition;
delete m_LoopBody;
}
protected: protected:
virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
private: private:
Expression *m_Condition; std::unique_ptr<Expression> m_Condition;
Expression *m_LoopBody; std::unique_ptr<Expression> m_LoopBody;
}; };
class I2_CONFIG_API ReturnExpression : public UnaryExpression class I2_CONFIG_API ReturnExpression : public UnaryExpression
{ {
public: public:
ReturnExpression(Expression *expression, const DebugInfo& debugInfo = DebugInfo()) ReturnExpression(std::unique_ptr<Expression> expression, const DebugInfo& debugInfo = DebugInfo())
: UnaryExpression(expression, debugInfo) : UnaryExpression(std::move(expression), debugInfo)
{ } { }
protected: protected:
@ -747,56 +708,46 @@ private:
class I2_CONFIG_API IndexerExpression : public BinaryExpression class I2_CONFIG_API IndexerExpression : public BinaryExpression
{ {
public: public:
IndexerExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) IndexerExpression(std::unique_ptr<Expression> operand1, std::unique_ptr<Expression> operand2, const DebugInfo& debugInfo = DebugInfo())
: BinaryExpression(operand1, operand2, debugInfo) : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo)
{ } { }
protected: protected:
virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
virtual bool GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const override; virtual bool GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const override;
friend I2_CONFIG_API void BindToScope(Expression *& expr, ScopeSpecifier scopeSpec); friend I2_CONFIG_API void BindToScope(std::unique_ptr<Expression>& expr, ScopeSpecifier scopeSpec);
}; };
I2_CONFIG_API void BindToScope(Expression *& expr, ScopeSpecifier scopeSpec); I2_CONFIG_API void BindToScope(std::unique_ptr<Expression>& expr, ScopeSpecifier scopeSpec);
class I2_CONFIG_API ThrowExpression : public DebuggableExpression class I2_CONFIG_API ThrowExpression : public DebuggableExpression
{ {
public: public:
ThrowExpression(Expression *message, bool incompleteExpr, const DebugInfo& debugInfo = DebugInfo()) ThrowExpression(std::unique_ptr<Expression> message, bool incompleteExpr, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_Message(message), m_IncompleteExpr(incompleteExpr) : DebuggableExpression(debugInfo), m_Message(std::move(message)), m_IncompleteExpr(incompleteExpr)
{ } { }
~ThrowExpression(void)
{
delete m_Message;
}
protected: protected:
virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
private: private:
Expression *m_Message; std::unique_ptr<Expression> m_Message;
bool m_IncompleteExpr; bool m_IncompleteExpr;
}; };
class I2_CONFIG_API ImportExpression : public DebuggableExpression class I2_CONFIG_API ImportExpression : public DebuggableExpression
{ {
public: public:
ImportExpression(Expression *name, const DebugInfo& debugInfo = DebugInfo()) ImportExpression(std::unique_ptr<Expression> name, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_Name(name) : DebuggableExpression(debugInfo), m_Name(std::move(name))
{ } { }
~ImportExpression(void)
{
delete m_Name;
}
protected: protected:
virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
private: private:
Expression *m_Name; std::unique_ptr<Expression> m_Name;
}; };
class I2_CONFIG_API ImportDefaultTemplatesExpression : public DebuggableExpression class I2_CONFIG_API ImportDefaultTemplatesExpression : public DebuggableExpression
@ -814,145 +765,99 @@ class I2_CONFIG_API FunctionExpression : public DebuggableExpression
{ {
public: public:
FunctionExpression(const String& name, const std::vector<String>& args, FunctionExpression(const String& name, const std::vector<String>& args,
std::map<String, Expression *> *closedVars, Expression *expression, const DebugInfo& debugInfo = DebugInfo()) std::map<String, std::unique_ptr<Expression> >&& closedVars, std::unique_ptr<Expression> expression, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_Name(name), m_Args(args), m_ClosedVars(closedVars), m_Expression(expression) : DebuggableExpression(debugInfo), m_Name(name), m_Args(args), m_ClosedVars(std::move(closedVars)), m_Expression(std::move(expression))
{ } { }
~FunctionExpression(void)
{
if (m_ClosedVars) {
typedef std::pair<String, Expression *> kv_pair;
for (const kv_pair& kv : *m_ClosedVars) {
delete kv.second;
}
}
delete m_ClosedVars;
}
protected: protected:
virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
private: private:
String m_Name; String m_Name;
std::vector<String> m_Args; std::vector<String> m_Args;
std::map<String, Expression *> *m_ClosedVars; std::map<String, std::unique_ptr<Expression> > m_ClosedVars;
std::shared_ptr<Expression> m_Expression; std::shared_ptr<Expression> m_Expression;
}; };
class I2_CONFIG_API ApplyExpression : public DebuggableExpression class I2_CONFIG_API ApplyExpression : public DebuggableExpression
{ {
public: public:
ApplyExpression(const String& type, const String& target, Expression *name, ApplyExpression(const String& type, const String& target, std::unique_ptr<Expression> name,
Expression *filter, const String& package, const String& fkvar, const String& fvvar, std::unique_ptr<Expression> filter, const String& package, const String& fkvar, const String& fvvar,
Expression *fterm, std::map<String, Expression *> *closedVars, bool ignoreOnError, std::unique_ptr<Expression> fterm, std::map<String, std::unique_ptr<Expression> >&& closedVars, bool ignoreOnError,
Expression *expression, const DebugInfo& debugInfo = DebugInfo()) std::unique_ptr<Expression> expression, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_Type(type), m_Target(target), : DebuggableExpression(debugInfo), m_Type(type), m_Target(target),
m_Name(name), m_Filter(filter), m_Package(package), m_FKVar(fkvar), m_FVVar(fvvar), m_Name(std::move(name)), m_Filter(std::move(filter)), m_Package(package), m_FKVar(fkvar), m_FVVar(fvvar),
m_FTerm(fterm), m_IgnoreOnError(ignoreOnError), m_ClosedVars(closedVars), m_FTerm(std::move(fterm)), m_IgnoreOnError(ignoreOnError), m_ClosedVars(std::move(closedVars)),
m_Expression(expression) m_Expression(std::move(expression))
{ } { }
~ApplyExpression(void)
{
delete m_Name;
if (m_ClosedVars) {
typedef std::pair<String, Expression *> kv_pair;
for (const kv_pair& kv : *m_ClosedVars) {
delete kv.second;
}
}
delete m_ClosedVars;
}
protected: protected:
virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
private: private:
String m_Type; String m_Type;
String m_Target; String m_Target;
Expression *m_Name; std::unique_ptr<Expression> m_Name;
std::shared_ptr<Expression> m_Filter; std::shared_ptr<Expression> m_Filter;
String m_Package; String m_Package;
String m_FKVar; String m_FKVar;
String m_FVVar; String m_FVVar;
std::shared_ptr<Expression> m_FTerm; std::shared_ptr<Expression> m_FTerm;
bool m_IgnoreOnError; bool m_IgnoreOnError;
std::map<String, Expression *> *m_ClosedVars; std::map<String, std::unique_ptr<Expression> > m_ClosedVars;
std::shared_ptr<Expression> m_Expression; std::shared_ptr<Expression> m_Expression;
}; };
class I2_CONFIG_API ObjectExpression : public DebuggableExpression class I2_CONFIG_API ObjectExpression : public DebuggableExpression
{ {
public: public:
ObjectExpression(bool abstract, Expression *type, Expression *name, Expression *filter, ObjectExpression(bool abstract, std::unique_ptr<Expression> type, std::unique_ptr<Expression> name, std::unique_ptr<Expression> filter,
const String& zone, const String& package, std::map<String, Expression *> *closedVars, const String& zone, const String& package, std::map<String, std::unique_ptr<Expression> >&& closedVars,
bool defaultTmpl, bool ignoreOnError, Expression *expression, const DebugInfo& debugInfo = DebugInfo()) bool defaultTmpl, bool ignoreOnError, std::unique_ptr<Expression> expression, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_Abstract(abstract), m_Type(type), : DebuggableExpression(debugInfo), m_Abstract(abstract), m_Type(std::move(type)),
m_Name(name), m_Filter(filter), m_Zone(zone), m_Package(package), m_DefaultTmpl(defaultTmpl), m_Name(std::move(name)), m_Filter(std::move(filter)), m_Zone(zone), m_Package(package), m_DefaultTmpl(defaultTmpl),
m_IgnoreOnError(ignoreOnError), m_ClosedVars(closedVars), m_Expression(expression) m_IgnoreOnError(ignoreOnError), m_ClosedVars(std::move(closedVars)), m_Expression(std::move(expression))
{ } { }
~ObjectExpression(void)
{
delete m_Name;
if (m_ClosedVars) {
typedef std::pair<String, Expression *> kv_pair;
for (const kv_pair& kv : *m_ClosedVars) {
delete kv.second;
}
}
delete m_ClosedVars;
}
protected: protected:
virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
private: private:
bool m_Abstract; bool m_Abstract;
Expression *m_Type; std::unique_ptr<Expression> m_Type;
Expression *m_Name; std::unique_ptr<Expression> m_Name;
std::shared_ptr<Expression> m_Filter; std::shared_ptr<Expression> m_Filter;
String m_Zone; String m_Zone;
String m_Package; String m_Package;
bool m_DefaultTmpl; bool m_DefaultTmpl;
bool m_IgnoreOnError; bool m_IgnoreOnError;
std::map<String, Expression *> *m_ClosedVars; std::map<String, std::unique_ptr<Expression> > m_ClosedVars;
std::shared_ptr<Expression> m_Expression; std::shared_ptr<Expression> m_Expression;
}; };
class I2_CONFIG_API ForExpression : public DebuggableExpression class I2_CONFIG_API ForExpression : public DebuggableExpression
{ {
public: public:
ForExpression(const String& fkvar, const String& fvvar, Expression *value, Expression *expression, const DebugInfo& debugInfo = DebugInfo()) ForExpression(const String& fkvar, const String& fvvar, std::unique_ptr<Expression> value, std::unique_ptr<Expression> expression, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_FKVar(fkvar), m_FVVar(fvvar), m_Value(value), m_Expression(expression) : DebuggableExpression(debugInfo), m_FKVar(fkvar), m_FVVar(fvvar), m_Value(std::move(value)), m_Expression(std::move(expression))
{ } { }
~ForExpression(void)
{
delete m_Value;
delete m_Expression;
}
protected: protected:
virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
private: private:
String m_FKVar; String m_FKVar;
String m_FVVar; String m_FVVar;
Expression *m_Value; std::unique_ptr<Expression> m_Value;
Expression *m_Expression; std::unique_ptr<Expression> m_Expression;
}; };
class I2_CONFIG_API LibraryExpression : public UnaryExpression class I2_CONFIG_API LibraryExpression : public UnaryExpression
{ {
public: public:
LibraryExpression(Expression *expression, const DebugInfo& debugInfo = DebugInfo()) LibraryExpression(std::unique_ptr<Expression> expression, const DebugInfo& debugInfo = DebugInfo())
: UnaryExpression(expression, debugInfo) : UnaryExpression(std::move(expression), debugInfo)
{ } { }
protected: protected:
@ -969,27 +874,20 @@ enum IncludeType
class I2_CONFIG_API IncludeExpression : public DebuggableExpression class I2_CONFIG_API IncludeExpression : public DebuggableExpression
{ {
public: public:
IncludeExpression(const String& relativeBase, Expression *path, Expression *pattern, Expression *name, IncludeExpression(const String& relativeBase, std::unique_ptr<Expression> path, std::unique_ptr<Expression> pattern, std::unique_ptr<Expression> name,
IncludeType type, bool searchIncludes, const String& zone, const String& package, const DebugInfo& debugInfo = DebugInfo()) IncludeType type, bool searchIncludes, const String& zone, const String& package, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_RelativeBase(relativeBase), m_Path(path), m_Pattern(pattern), : DebuggableExpression(debugInfo), m_RelativeBase(relativeBase), m_Path(std::move(path)), m_Pattern(std::move(pattern)),
m_Name(name), m_Type(type), m_SearchIncludes(searchIncludes), m_Zone(zone), m_Package(package) m_Name(std::move(name)), m_Type(type), m_SearchIncludes(searchIncludes), m_Zone(zone), m_Package(package)
{ } { }
~IncludeExpression(void)
{
delete m_Path;
delete m_Pattern;
delete m_Name;
}
protected: protected:
virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
private: private:
String m_RelativeBase; String m_RelativeBase;
Expression *m_Path; std::unique_ptr<Expression> m_Path;
Expression *m_Pattern; std::unique_ptr<Expression> m_Pattern;
Expression *m_Name; std::unique_ptr<Expression> m_Name;
IncludeType m_Type; IncludeType m_Type;
bool m_SearchIncludes; bool m_SearchIncludes;
String m_Zone; String m_Zone;
@ -1010,41 +908,30 @@ protected:
class I2_CONFIG_API UsingExpression : public DebuggableExpression class I2_CONFIG_API UsingExpression : public DebuggableExpression
{ {
public: public:
UsingExpression(Expression *name, const DebugInfo& debugInfo = DebugInfo()) UsingExpression(std::unique_ptr<Expression> name, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_Name(name) : DebuggableExpression(debugInfo), m_Name(std::move(name))
{ } { }
~UsingExpression(void)
{
delete m_Name;
}
protected: protected:
virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
private: private:
Expression *m_Name; std::unique_ptr<Expression> m_Name;
}; };
class I2_CONFIG_API TryExceptExpression : public DebuggableExpression class I2_CONFIG_API TryExceptExpression : public DebuggableExpression
{ {
public: public:
TryExceptExpression(Expression *tryBody, Expression *exceptBody, const DebugInfo& debugInfo = DebugInfo()) TryExceptExpression(std::unique_ptr<Expression> tryBody, std::unique_ptr<Expression> exceptBody, const DebugInfo& debugInfo = DebugInfo())
: DebuggableExpression(debugInfo), m_TryBody(tryBody), m_ExceptBody(exceptBody) : DebuggableExpression(debugInfo), m_TryBody(std::move(tryBody)), m_ExceptBody(std::move(exceptBody))
{ } { }
~TryExceptExpression(void)
{
delete m_TryBody;
delete m_ExceptBody;
}
protected: protected:
virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override;
private: private:
Expression *m_TryBody; std::unique_ptr<Expression> m_TryBody;
Expression *m_ExceptBody; std::unique_ptr<Expression> m_ExceptBody;
}; };
} }

View File

@ -109,7 +109,7 @@ public:
} }
static inline Value NewFunction(ScriptFrame& frame, const String& name, const std::vector<String>& argNames, static inline Value NewFunction(ScriptFrame& frame, const String& name, const std::vector<String>& argNames,
std::map<String, Expression *> *closedVars, const std::shared_ptr<Expression>& expression) const std::map<String, std::unique_ptr<Expression> >& closedVars, const std::shared_ptr<Expression>& expression)
{ {
auto evaluatedClosedVars = EvaluateClosedVars(frame, closedVars); auto evaluatedClosedVars = EvaluateClosedVars(frame, closedVars);
@ -132,7 +132,7 @@ public:
} }
static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const std::shared_ptr<Expression>& filter, static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const std::shared_ptr<Expression>& filter,
const String& package, const String& fkvar, const String& fvvar, const std::shared_ptr<Expression>& fterm, std::map<String, Expression *> *closedVars, const String& package, const String& fkvar, const String& fvvar, const std::shared_ptr<Expression>& fterm, const std::map<String, std::unique_ptr<Expression> >& closedVars,
bool ignoreOnError, const std::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo()) bool ignoreOnError, const std::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
{ {
ApplyRule::AddRule(type, target, name, expression, filter, package, fkvar, ApplyRule::AddRule(type, target, name, expression, filter, package, fkvar,
@ -142,7 +142,7 @@ public:
} }
static inline Value NewObject(ScriptFrame& frame, bool abstract, const Type::Ptr& type, const String& name, const std::shared_ptr<Expression>& filter, static inline Value NewObject(ScriptFrame& frame, bool abstract, const Type::Ptr& type, const String& name, const std::shared_ptr<Expression>& filter,
const String& zone, const String& package, bool defaultTmpl, bool ignoreOnError, std::map<String, Expression *> *closedVars, const std::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo()) const String& zone, const String& package, bool defaultTmpl, bool ignoreOnError, const std::map<String, std::unique_ptr<Expression> >& closedVars, const std::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
{ {
ConfigItemBuilder::Ptr item = new ConfigItemBuilder(debugInfo); ConfigItemBuilder::Ptr item = new ConfigItemBuilder(debugInfo);
@ -190,7 +190,7 @@ public:
return Empty; return Empty;
} }
static inline ExpressionResult For(ScriptFrame& frame, const String& fkvar, const String& fvvar, const Value& value, Expression *expression, const DebugInfo& debugInfo = DebugInfo()) static inline ExpressionResult For(ScriptFrame& frame, const String& fkvar, const String& fvvar, const Value& value, const std::unique_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
{ {
if (value.IsObjectType<Array>()) { if (value.IsObjectType<Array>()) {
if (!fvvar.IsEmpty()) if (!fvvar.IsEmpty())
@ -251,15 +251,14 @@ public:
} }
private: private:
static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, std::map<String, Expression *> *closedVars) static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, const std::map<String, std::unique_ptr<Expression> >& closedVars)
{ {
Dictionary::Ptr locals; Dictionary::Ptr locals;
if (closedVars) { if (!closedVars.empty()) {
locals = new Dictionary(); locals = new Dictionary();
typedef std::pair<String, Expression *> ClosedVar; for (const auto& cvar : closedVars) {
for (const ClosedVar& cvar : *closedVars) {
locals->Set(cvar.first, cvar.second->Evaluate(frame)); locals->Set(cvar.first, cvar.second->Evaluate(frame));
} }
} }

View File

@ -124,15 +124,14 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full
fp << config; fp << config;
fp.close(); fp.close();
Expression *expr = ConfigCompiler::CompileFile(path, String(), "_api"); std::unique_ptr<Expression> expr = ConfigCompiler::CompileFile(path, String(), "_api");
try { try {
ActivationScope ascope; ActivationScope ascope;
ScriptFrame frame; ScriptFrame frame;
expr->Evaluate(frame); expr->Evaluate(frame);
delete expr; expr.reset();
expr = nullptr;
WorkQueue upq; WorkQueue upq;
std::vector<ConfigItem::Ptr> newItems; std::vector<ConfigItem::Ptr> newItems;
@ -156,8 +155,6 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full
ApiListener::UpdateObjectAuthority(); ApiListener::UpdateObjectAuthority();
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
delete expr;
if (unlink(path.CStr()) < 0 && errno != ENOENT) { if (unlink(path.CStr()) < 0 && errno != ENOENT) {
BOOST_THROW_EXCEPTION(posix_error() BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("unlink") << boost::errinfo_api_function("unlink")

View File

@ -123,7 +123,7 @@ bool ConsoleHandler::ExecuteScriptHelper(HttpRequest& request, HttpResponse& res
Array::Ptr results = new Array(); Array::Ptr results = new Array();
Dictionary::Ptr resultInfo = new Dictionary(); Dictionary::Ptr resultInfo = new Dictionary();
Expression *expr = nullptr; std::unique_ptr<Expression> expr;
Value exprResult; Value exprResult;
try { try {
@ -160,11 +160,7 @@ bool ConsoleHandler::ExecuteScriptHelper(HttpRequest& request, HttpResponse& res
debugInfo->Set("last_line", di.LastLine); debugInfo->Set("last_line", di.LastLine);
debugInfo->Set("last_column", di.LastColumn); debugInfo->Set("last_column", di.LastColumn);
resultInfo->Set("debug_info", debugInfo); resultInfo->Set("debug_info", debugInfo);
} catch (...) {
delete expr;
throw;
} }
delete expr;
results->Add(resultInfo); results->Add(resultInfo);
@ -301,13 +297,12 @@ std::vector<String> ConsoleHandler::GetAutocompletionSuggestions(const String& w
Value value; Value value;
try { try {
Expression *expr = ConfigCompiler::CompileText("temp", pword); std::unique_ptr<Expression> expr = ConfigCompiler::CompileText("temp", pword);
if (expr) if (expr)
value = expr->Evaluate(frame); value = expr->Evaluate(frame);
AddSuggestions(matches, word, pword, true, value); AddSuggestions(matches, word, pword, true, value);
} catch (...) { /* Ignore the exception */ } } catch (...) { /* Ignore the exception */ }
} }

View File

@ -25,14 +25,9 @@
using namespace icinga; using namespace icinga;
EventQueue::EventQueue(const String& name) EventQueue::EventQueue(const String& name)
: m_Name(name), m_Filter(nullptr) : m_Name(name)
{ } { }
EventQueue::~EventQueue(void)
{
delete m_Filter;
}
bool EventQueue::CanProcessEvent(const String& type) const bool EventQueue::CanProcessEvent(const String& type) const
{ {
boost::mutex::scoped_lock lock(m_Mutex); boost::mutex::scoped_lock lock(m_Mutex);
@ -46,7 +41,7 @@ void EventQueue::ProcessEvent(const Dictionary::Ptr& event)
frame.Sandboxed = true; frame.Sandboxed = true;
try { try {
if (!FilterUtility::EvaluateFilter(frame, m_Filter, event, "event")) if (!FilterUtility::EvaluateFilter(frame, &*m_Filter, event, "event"))
return; return;
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
Log(LogWarning, "EventQueue") Log(LogWarning, "EventQueue")
@ -93,11 +88,10 @@ void EventQueue::SetTypes(const std::set<String>& types)
m_Types = types; m_Types = types;
} }
void EventQueue::SetFilter(Expression *filter) void EventQueue::SetFilter(std::unique_ptr<Expression> filter)
{ {
boost::mutex::scoped_lock lock(m_Mutex); boost::mutex::scoped_lock lock(m_Mutex);
delete m_Filter; m_Filter.swap(filter);
m_Filter = filter;
} }
Dictionary::Ptr EventQueue::WaitForEvent(void *client, double timeout) Dictionary::Ptr EventQueue::WaitForEvent(void *client, double timeout)

View File

@ -38,7 +38,6 @@ public:
DECLARE_PTR_TYPEDEFS(EventQueue); DECLARE_PTR_TYPEDEFS(EventQueue);
EventQueue(const String& name); EventQueue(const String& name);
~EventQueue(void);
bool CanProcessEvent(const String& type) const; bool CanProcessEvent(const String& type) const;
void ProcessEvent(const Dictionary::Ptr& event); void ProcessEvent(const Dictionary::Ptr& event);
@ -46,7 +45,7 @@ public:
void RemoveClient(void *client); void RemoveClient(void *client);
void SetTypes(const std::set<String>& types); void SetTypes(const std::set<String>& types);
void SetFilter(Expression *filter); void SetFilter(std::unique_ptr<Expression> filter);
Dictionary::Ptr WaitForEvent(void *client, double timeout = 5); Dictionary::Ptr WaitForEvent(void *client, double timeout = 5);
@ -64,7 +63,7 @@ private:
boost::condition_variable m_CV; boost::condition_variable m_CV;
std::set<String> m_Types; std::set<String> m_Types;
Expression *m_Filter; std::unique_ptr<Expression> m_Filter;
std::map<void *, std::deque<Dictionary::Ptr> > m_Events; std::map<void *, std::deque<Dictionary::Ptr> > m_Events;
}; };

View File

@ -66,7 +66,7 @@ bool EventsHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request
String filter = HttpUtility::GetLastParameter(params, "filter"); String filter = HttpUtility::GetLastParameter(params, "filter");
Expression *ufilter = nullptr; std::unique_ptr<Expression> ufilter;
if (!filter.IsEmpty()) if (!filter.IsEmpty())
ufilter = ConfigCompiler::CompileText("<API query>", filter); ufilter = ConfigCompiler::CompileText("<API query>", filter);
@ -80,7 +80,7 @@ bool EventsHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request
} }
queue->SetTypes(types->ToSet<String>()); queue->SetTypes(types->ToSet<String>());
queue->SetFilter(ufilter); queue->SetFilter(std::move(ufilter));
queue->AddClient(&request); queue->AddClient(&request);

View File

@ -162,14 +162,15 @@ void FilterUtility::CheckPermission(const ApiUser::Ptr& user, const String& perm
foundPermission = true; foundPermission = true;
if (filter && permissionFilter) { if (filter && permissionFilter) {
std::vector<Expression *> args; std::vector<std::unique_ptr<Expression> > args;
args.push_back(new GetScopeExpression(ScopeLocal)); args.emplace_back(new GetScopeExpression(ScopeLocal));
FunctionCallExpression *fexpr = new FunctionCallExpression(new IndexerExpression(MakeLiteral(filter), MakeLiteral("call")), args); std::unique_ptr<Expression> indexer{new IndexerExpression(std::unique_ptr<Expression>(MakeLiteral(filter)), std::unique_ptr<Expression>(MakeLiteral("call")))};
FunctionCallExpression *fexpr = new FunctionCallExpression(std::move(indexer), std::move(args));
if (!*permissionFilter) if (!*permissionFilter)
*permissionFilter = fexpr; *permissionFilter = fexpr;
else else
*permissionFilter = new LogicalOrExpression(*permissionFilter, fexpr); *permissionFilter = new LogicalOrExpression(std::unique_ptr<Expression>(*permissionFilter), std::unique_ptr<Expression>(fexpr));
} }
} }
} }
@ -250,7 +251,7 @@ std::vector<Value> FilterUtility::GetFilterTargets(const QueryDescription& qd, c
frame.Sandboxed = true; frame.Sandboxed = true;
Dictionary::Ptr uvars = new Dictionary(); Dictionary::Ptr uvars = new Dictionary();
Expression *ufilter = nullptr; std::unique_ptr<Expression> ufilter;
if (query->Contains("filter")) { if (query->Contains("filter")) {
String filter = HttpUtility::GetLastParameter(query, "filter"); String filter = HttpUtility::GetLastParameter(query, "filter");
@ -267,16 +268,9 @@ std::vector<Value> FilterUtility::GetFilterTargets(const QueryDescription& qd, c
frame.Self = uvars; frame.Self = uvars;
try {
provider->FindTargets(type, std::bind(&FilteredAddTarget, provider->FindTargets(type, std::bind(&FilteredAddTarget,
std::ref(permissionFrame), permissionFilter, std::ref(permissionFrame), permissionFilter,
std::ref(frame), ufilter, std::ref(result), variableName, _1)); std::ref(frame), &*ufilter, std::ref(result), variableName, _1));
} catch (const std::exception&) {
delete ufilter;
throw;
}
delete ufilter;
} }
return result; return result;

View File

@ -28,308 +28,236 @@ BOOST_AUTO_TEST_SUITE(config_ops)
BOOST_AUTO_TEST_CASE(simple) BOOST_AUTO_TEST_CASE(simple)
{ {
ScriptFrame frame; ScriptFrame frame;
Expression *expr; std::unique_ptr<Expression> expr;
Dictionary::Ptr dict; Dictionary::Ptr dict;
expr = ConfigCompiler::CompileText("<test>", ""); expr = ConfigCompiler::CompileText("<test>", "");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == Empty); BOOST_CHECK(expr->Evaluate(frame).GetValue() == Empty);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "\n3"); expr = ConfigCompiler::CompileText("<test>", "\n3");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "{ 3\n\n5 }"); expr = ConfigCompiler::CompileText("<test>", "{ 3\n\n5 }");
BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "1 + 3"); expr = ConfigCompiler::CompileText("<test>", "1 + 3");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 4); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 4);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "3 - 1"); expr = ConfigCompiler::CompileText("<test>", "3 - 1");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 2); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 2);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "5m * 10"); expr = ConfigCompiler::CompileText("<test>", "5m * 10");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3000); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3000);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "5m / 5"); expr = ConfigCompiler::CompileText("<test>", "5m / 5");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 60); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 60);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "7 & 3"); expr = ConfigCompiler::CompileText("<test>", "7 & 3");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "2 | 3"); expr = ConfigCompiler::CompileText("<test>", "2 | 3");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "true && false"); expr = ConfigCompiler::CompileText("<test>", "true && false");
BOOST_CHECK(!expr->Evaluate(frame).GetValue()); BOOST_CHECK(!expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "true || false"); expr = ConfigCompiler::CompileText("<test>", "true || false");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "3 < 5"); expr = ConfigCompiler::CompileText("<test>", "3 < 5");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "3 > 5"); expr = ConfigCompiler::CompileText("<test>", "3 > 5");
BOOST_CHECK(!expr->Evaluate(frame).GetValue()); BOOST_CHECK(!expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "3 <= 3"); expr = ConfigCompiler::CompileText("<test>", "3 <= 3");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "3 >= 3"); expr = ConfigCompiler::CompileText("<test>", "3 >= 3");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "2 + 3 * 4"); expr = ConfigCompiler::CompileText("<test>", "2 + 3 * 4");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 14); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 14);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "(2 + 3) * 4"); expr = ConfigCompiler::CompileText("<test>", "(2 + 3) * 4");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 20); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 20);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "2 * - 3"); expr = ConfigCompiler::CompileText("<test>", "2 * - 3");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == -6); BOOST_CHECK(expr->Evaluate(frame).GetValue() == -6);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "-(2 + 3)"); expr = ConfigCompiler::CompileText("<test>", "-(2 + 3)");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == -5); BOOST_CHECK(expr->Evaluate(frame).GetValue() == -5);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "- 2 * 2 - 2 * 3 - 4 * - 5"); expr = ConfigCompiler::CompileText("<test>", "- 2 * 2 - 2 * 3 - 4 * - 5");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 10); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 10);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "!0 == true"); expr = ConfigCompiler::CompileText("<test>", "!0 == true");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "~0"); expr = ConfigCompiler::CompileText("<test>", "~0");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == (double)~(long)0); BOOST_CHECK(expr->Evaluate(frame).GetValue() == (double)~(long)0);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "4 << 8"); expr = ConfigCompiler::CompileText("<test>", "4 << 8");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 1024); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 1024);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "1024 >> 4"); expr = ConfigCompiler::CompileText("<test>", "1024 >> 4");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 64); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 64);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "2 << 3 << 4"); expr = ConfigCompiler::CompileText("<test>", "2 << 3 << 4");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 256); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 256);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "256 >> 4 >> 3"); expr = ConfigCompiler::CompileText("<test>", "256 >> 4 >> 3");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 2); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 2);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "\"hello\" == \"hello\""); expr = ConfigCompiler::CompileText("<test>", "\"hello\" == \"hello\"");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "\"hello\" != \"hello\""); expr = ConfigCompiler::CompileText("<test>", "\"hello\" != \"hello\"");
BOOST_CHECK(!expr->Evaluate(frame).GetValue()); BOOST_CHECK(!expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "\"foo\" in [ \"foo\", \"bar\" ]"); expr = ConfigCompiler::CompileText("<test>", "\"foo\" in [ \"foo\", \"bar\" ]");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "\"foo\" in [ \"bar\", \"baz\" ]"); expr = ConfigCompiler::CompileText("<test>", "\"foo\" in [ \"bar\", \"baz\" ]");
BOOST_CHECK(!expr->Evaluate(frame).GetValue()); BOOST_CHECK(!expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "\"foo\" in null"); expr = ConfigCompiler::CompileText("<test>", "\"foo\" in null");
BOOST_CHECK(!expr->Evaluate(frame).GetValue()); BOOST_CHECK(!expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "\"foo\" in \"bar\""); expr = ConfigCompiler::CompileText("<test>", "\"foo\" in \"bar\"");
BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in [ \"bar\", \"baz\" ]"); expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in [ \"bar\", \"baz\" ]");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in [ \"foo\", \"bar\" ]"); expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in [ \"foo\", \"bar\" ]");
BOOST_CHECK(!expr->Evaluate(frame).GetValue()); BOOST_CHECK(!expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in null"); expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in null");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in \"bar\""); expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in \"bar\"");
BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "{ a += 3 }"); expr = ConfigCompiler::CompileText("<test>", "{ a += 3 }");
dict = expr->Evaluate(frame).GetValue(); dict = expr->Evaluate(frame).GetValue();
delete expr;
BOOST_CHECK(dict->GetLength() == 1); BOOST_CHECK(dict->GetLength() == 1);
BOOST_CHECK(dict->Get("a") == 3); BOOST_CHECK(dict->Get("a") == 3);
expr = ConfigCompiler::CompileText("<test>", "test"); expr = ConfigCompiler::CompileText("<test>", "test");
BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "null + 3"); expr = ConfigCompiler::CompileText("<test>", "null + 3");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "3 + null"); expr = ConfigCompiler::CompileText("<test>", "3 + null");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "\"test\" + 3"); expr = ConfigCompiler::CompileText("<test>", "\"test\" + 3");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == "test3"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == "test3");
delete expr;
expr = ConfigCompiler::CompileText("<test>", "\"\\\"te\\\\st\""); expr = ConfigCompiler::CompileText("<test>", "\"\\\"te\\\\st\"");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == "\"te\\st"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == "\"te\\st");
delete expr;
expr = ConfigCompiler::CompileText("<test>", "\"\\'test\""); expr = ConfigCompiler::CompileText("<test>", "\"\\'test\"");
BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "({ a = 3\nb = 3 })"); expr = ConfigCompiler::CompileText("<test>", "({ a = 3\nb = 3 })");
BOOST_CHECK(expr->Evaluate(frame).GetValue().IsObjectType<Dictionary>()); BOOST_CHECK(expr->Evaluate(frame).GetValue().IsObjectType<Dictionary>());
delete expr;
} }
BOOST_AUTO_TEST_CASE(advanced) BOOST_AUTO_TEST_CASE(advanced)
{ {
ScriptFrame frame; ScriptFrame frame;
Expression *expr; std::unique_ptr<Expression> expr;
Function::Ptr func; Function::Ptr func;
expr = ConfigCompiler::CompileText("<test>", "regex(\"^Hello\", \"Hello World\")"); expr = ConfigCompiler::CompileText("<test>", "regex(\"^Hello\", \"Hello World\")");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "__boost_test()"); expr = ConfigCompiler::CompileText("<test>", "__boost_test()");
BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError);
delete expr;
Object::Ptr self = new Object(); Object::Ptr self = new Object();
ScriptFrame frame2(self); ScriptFrame frame2(self);
expr = ConfigCompiler::CompileText("<test>", "this"); expr = ConfigCompiler::CompileText("<test>", "this");
BOOST_CHECK(expr->Evaluate(frame2).GetValue() == Value(self)); BOOST_CHECK(expr->Evaluate(frame2).GetValue() == Value(self));
delete expr;
expr = ConfigCompiler::CompileText("<test>", "var v = 7; v"); expr = ConfigCompiler::CompileText("<test>", "var v = 7; v");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "{ a = 3 }.a"); expr = ConfigCompiler::CompileText("<test>", "{ a = 3 }.a");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "[ 2, 3 ][1]"); expr = ConfigCompiler::CompileText("<test>", "[ 2, 3 ][1]");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "var v = { a = 3}; v.a"); expr = ConfigCompiler::CompileText("<test>", "var v = { a = 3}; v.a");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "a = 3 b = 3"); expr = ConfigCompiler::CompileText("<test>", "a = 3 b = 3");
BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "function() { 3 }()"); expr = ConfigCompiler::CompileText("<test>", "function() { 3 }()");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "function() { return 3, 5 }()"); expr = ConfigCompiler::CompileText("<test>", "function() { return 3, 5 }()");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "typeof([]) == Array"); expr = ConfigCompiler::CompileText("<test>", "typeof([]) == Array");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "typeof({}) == Dictionary"); expr = ConfigCompiler::CompileText("<test>", "typeof({}) == Dictionary");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "typeof(3) == Number"); expr = ConfigCompiler::CompileText("<test>", "typeof(3) == Number");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "typeof(\"test\") == String"); expr = ConfigCompiler::CompileText("<test>", "typeof(\"test\") == String");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "(7 | 8) == 15"); expr = ConfigCompiler::CompileText("<test>", "(7 | 8) == 15");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "(7 ^ 8) == 15"); expr = ConfigCompiler::CompileText("<test>", "(7 ^ 8) == 15");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "(7 & 15) == 7"); expr = ConfigCompiler::CompileText("<test>", "(7 & 15) == 7");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "7 in [7] == true"); expr = ConfigCompiler::CompileText("<test>", "7 in [7] == true");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "7 !in [7] == false"); expr = ConfigCompiler::CompileText("<test>", "7 !in [7] == false");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "(7 | 8) > 14"); expr = ConfigCompiler::CompileText("<test>", "(7 | 8) > 14");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "(7 ^ 8) > 14"); expr = ConfigCompiler::CompileText("<test>", "(7 ^ 8) > 14");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "(7 & 15) > 6"); expr = ConfigCompiler::CompileText("<test>", "(7 & 15) > 6");
BOOST_CHECK(expr->Evaluate(frame).GetValue()); BOOST_CHECK(expr->Evaluate(frame).GetValue());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "\"a\" = 3"); expr = ConfigCompiler::CompileText("<test>", "\"a\" = 3");
BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "3 = 3"); expr = ConfigCompiler::CompileText("<test>", "3 = 3");
BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "var e; e"); expr = ConfigCompiler::CompileText("<test>", "var e; e");
BOOST_CHECK(expr->Evaluate(frame).GetValue().IsEmpty()); BOOST_CHECK(expr->Evaluate(frame).GetValue().IsEmpty());
delete expr;
expr = ConfigCompiler::CompileText("<test>", "var e = 3; e"); expr = ConfigCompiler::CompileText("<test>", "var e = 3; e");
BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "Array.x"); expr = ConfigCompiler::CompileText("<test>", "Array.x");
BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError);
delete expr;
expr = ConfigCompiler::CompileText("<test>", "{{ 3 }}"); expr = ConfigCompiler::CompileText("<test>", "{{ 3 }}");
func = expr->Evaluate(frame).GetValue(); func = expr->Evaluate(frame).GetValue();
BOOST_CHECK(func->Invoke() == 3); BOOST_CHECK(func->Invoke() == 3);
delete expr;
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

View File

@ -58,7 +58,7 @@ apply Service "livestatus" {
} }
)CONFIG"; )CONFIG";
Expression *expr = ConfigCompiler::CompileText("<livestatus>", config); std::unique_ptr<Expression> expr = ConfigCompiler::CompileText("<livestatus>", config);
expr->Evaluate(*ScriptFrame::GetCurrentFrame()); expr->Evaluate(*ScriptFrame::GetCurrentFrame());
} }
}; };