Make inline maps const. Add test for malformed inline map that causes crash

This commit is contained in:
Jason Turner
2010-10-02 22:50:09 +00:00
parent fa2a7045a7
commit 3ab91356e5
3 changed files with 7 additions and 8 deletions

View File

@@ -396,6 +396,7 @@ namespace detail {
m->add(user_type<MapType>(), type); m->add(user_type<MapType>(), type);
typedef typename MapType::mapped_type &(MapType::*elemaccess)(const typename MapType::key_type &); typedef typename MapType::mapped_type &(MapType::*elemaccess)(const typename MapType::key_type &);
m->add(fun(static_cast<elemaccess>(&MapType::operator[])), "[]"); m->add(fun(static_cast<elemaccess>(&MapType::operator[])), "[]");
container_type<MapType>(type, m); container_type<MapType>(type, m);

View File

@@ -411,22 +411,18 @@ namespace chaiscript
*/ */
Boxed_Value Inline_Map_AST_Node::eval(Dispatch_Engine &ss) { Boxed_Value Inline_Map_AST_Node::eval(Dispatch_Engine &ss) {
try { try {
Boxed_Value retval = ss.call_function("Map"); std::map<std::string, Boxed_Value> retval;
for (size_t i = 0; i < this->children[0]->children.size(); ++i) { for (size_t i = 0; i < this->children[0]->children.size(); ++i) {
try { try {
Boxed_Value slot retval[boxed_cast<std::string>(this->children[0]->children[i]->children[0]->eval(ss))]
= ss.call_function("[]", retval, this->children[0]->children[i]->children[0]->eval(ss)); = ss.call_function("clone", this->children[0]->children[i]->children[1]->eval(ss));
ss.call_function("=", slot, this->children[0]->children[i]->children[1]->eval(ss));
}
catch (const dispatch_error &) {
throw Eval_Error("Can not find appropriate '=' for map init");
} }
catch(Eval_Error &ee) { catch(Eval_Error &ee) {
ee.call_stack.push_back(this->children[0]->children[i]); ee.call_stack.push_back(this->children[0]->children[i]);
throw; throw;
} }
} }
return retval; return const_var(retval);
} }
catch (const dispatch_error &) { catch (const dispatch_error &) {
throw Eval_Error("Can not find appropriate 'Map()'"); throw Eval_Error("Can not find appropriate 'Map()'");

View File

@@ -0,0 +1,2 @@
assert_throws("Parse failure", fun() { eval("[\"hello\":5,\"j\",\"k\"]") } );