2020
2121namespace casbin {
2222 bool ExprtkEvaluator::Eval (const std::string& expression_string) {
23- expression.register_symbol_table (symbol_table);
24- // replace (&& -> and), (|| -> or)
25- auto replaced_string = std::regex_replace (expression_string, std::regex (" &&" ), " and" );
26- replaced_string = std::regex_replace (replaced_string, std::regex (" \\ |{2}" ), " or" );
27- // replace string "" -> ''
28- replaced_string = std::regex_replace (replaced_string, std::regex (" \" " ), " \' " );
23+ if (this ->expression_string_ != expression_string) {
24+ this ->expression_string_ = expression_string;
25+ // replace (&& -> and), (|| -> or)
26+ auto replaced_string = std::regex_replace (expression_string, std::regex (" &&" ), " and" );
27+ replaced_string = std::regex_replace (replaced_string, std::regex (" \\ |{2}" ), " or" );
28+ // replace string "" -> ''
29+ replaced_string = std::regex_replace (replaced_string, std::regex (" \" " ), " \' " );
30+
31+ return parser.compile (replaced_string, expression);
32+ }
2933
30- return parser.compile (replaced_string, expression) ;
34+ return this -> parser .error_count () == 0 ;
3135 }
3236
33- void ExprtkEvaluator::InitialObject (std::string identifier) {
37+ void ExprtkEvaluator::InitialObject (const std::string& identifier) {
3438 // symbol_table.add_stringvar("");
3539 }
3640
37- void ExprtkEvaluator::PushObjectString (std::string target, std::string proprity, const std::string& var) {
41+ void ExprtkEvaluator::PushObjectString (const std::string& target, const std::string& proprity, const std::string& var) {
3842 auto identifier = target + " ." + proprity;
39- this ->symbol_table .add_stringvar (identifier, const_cast <std::string&>(var));
43+
44+ if (!symbol_table.symbol_exists (identifier)) {
45+ identifiers_[identifier] = std::make_unique<std::string>(" " );
46+ this ->symbol_table .add_stringvar (identifier, *identifiers_[identifier]);
47+ }
48+ symbol_table.get_stringvar (identifier)->ref () = var;
4049 }
4150
42- void ExprtkEvaluator::PushObjectJson (std::string target, std::string proprity, const nlohmann::json& var) {
51+ void ExprtkEvaluator::PushObjectJson (const std::string& target, const std::string& proprity, const nlohmann::json& var) {
4352 auto identifier = target + " ." + proprity;
4453 // this->symbol_table.add_stringvar(identifier, const_cast<std::string&>(var));
4554 }
4655
4756 void ExprtkEvaluator::LoadFunctions () {
48-
57+ AddFunction (" keyMatch" , ExprtkFunctionFactory::GetExprtkFunction (ExprtkFunctionType::KeyMatch, 2 ));
58+ AddFunction (" keyMatch2" , ExprtkFunctionFactory::GetExprtkFunction (ExprtkFunctionType::KeyMatch2, 2 ));
59+ AddFunction (" keyMatch3" , ExprtkFunctionFactory::GetExprtkFunction (ExprtkFunctionType::KeyMatch3, 2 ));
60+ AddFunction (" regexMatch" , ExprtkFunctionFactory::GetExprtkFunction (ExprtkFunctionType::RegexMatch, 2 ));
61+ AddFunction (" ipMatch" , ExprtkFunctionFactory::GetExprtkFunction (ExprtkFunctionType::IpMatch, 2 ));
4962 }
5063
5164 void ExprtkEvaluator::LoadGFunction (std::shared_ptr<RoleManager> rm, const std::string& name, int narg) {
@@ -69,31 +82,29 @@ namespace casbin {
6982 }
7083
7184 bool ExprtkEvaluator::GetBoolen () {
72- return expression. value ( );
85+ return bool ( this -> expression );
7386 }
7487
7588 float ExprtkEvaluator::GetFloat () {
7689 return expression.value ();
7790 }
7891
79- void ExprtkEvaluator::Clean (AssertionMap& section) {
80- for (auto & [assertion_name, assertion]: section.assertion_map ) {
81- std::vector<std::string> raw_tokens = assertion->tokens ;
82-
83- for (int j = 0 ; j < raw_tokens.size () ; j++) {
84- size_t index = raw_tokens[j].find (" _" );
85- std::string token = raw_tokens[j].substr (index + 1 );
86- auto identifier = assertion_name + " ." + token;
87- if (symbol_table.get_stringvar (identifier) != nullptr ) {
88- symbol_table.remove_stringvar (identifier);
89- }
90- }
92+ void ExprtkEvaluator::Clean (AssertionMap& section, bool after_enforce) {
93+ if (after_enforce == false ) {
94+ return ;
9195 }
96+
97+ this ->symbol_table .clear ();
98+ this ->expression_string_ = " " ;
99+ this ->Functions .clear ();
100+ this ->identifiers_ .clear ();
92101 }
93102
94103 void ExprtkEvaluator::AddFunction (const std::string& func_name, std::shared_ptr<exprtk_func_t > func) {
95- this ->Functions .push_back (func);
96- symbol_table.add_function (func_name, *func);
104+ if (func != nullptr ) {
105+ this ->Functions .push_back (func);
106+ symbol_table.add_function (func_name, *func);
107+ }
97108 }
98109
99110 void ExprtkEvaluator::PrintSymbol () {
@@ -104,21 +115,24 @@ namespace casbin {
104115 for (auto & var: var_list) {
105116 printf (" %s: %s\n " , var.c_str (), symbol_table.get_stringvar (var)->ref ().c_str ());
106117 }
118+ printf (" Current error: %s\n " , parser.error ().c_str ());
119+ // printf("Current exprsio string: %s\n", parser.current_token);
120+ printf (" Current value: %d\n " , bool (this ->expression ));
107121 }
108122
109123 bool DuktapeEvaluator::Eval (const std::string& expression) {
110124 return casbin::Eval (scope, expression);
111125 }
112126
113- void DuktapeEvaluator::InitialObject (std::string identifier) {
127+ void DuktapeEvaluator::InitialObject (const std::string& identifier) {
114128 PushObject (scope, identifier);
115129 }
116130
117- void DuktapeEvaluator::PushObjectString (std::string target, std::string proprity, const std::string& var) {
131+ void DuktapeEvaluator::PushObjectString (const std::string& target, const std::string& proprity, const std::string& var) {
118132 PushStringPropToObject (scope, target, var, proprity);
119133 }
120134
121- void DuktapeEvaluator::PushObjectJson (std::string target, std::string proprity, const nlohmann::json& var) {
135+ void DuktapeEvaluator::PushObjectJson (const std::string& target, const std::string& proprity, const nlohmann::json& var) {
122136 PushObject (scope, proprity);
123137 PushObjectPropFromJson (scope, var, proprity);
124138 PushObjectPropToObject (scope, target, proprity);
@@ -183,7 +197,7 @@ namespace casbin {
183197 return casbin::GetFloat (scope);
184198 }
185199
186- void DuktapeEvaluator::Clean (AssertionMap& section) {
200+ void DuktapeEvaluator::Clean (AssertionMap& section, bool after_enforce ) {
187201 if (scope != nullptr ) {
188202 for (auto & [assertion_name, assertion]: section.assertion_map ) {
189203 std::vector<std::string> raw_tokens = assertion->tokens ;
0 commit comments