Make vector inplace construction consistent with map

- Clone elements into both vector and map
  - Be sure to drop dependencies after elements are cloned in
This commit is contained in:
Jason Turner
2012-05-20 07:04:22 -06:00
parent 6c57729779
commit 349425fe8a
4 changed files with 40 additions and 8 deletions

View File

@@ -185,7 +185,7 @@ if(BUILD_TESTING)
add_executable(short_comparison_test unittests/short_comparison_test.cpp)
target_link_libraries(short_comparison_test ${LIBS})
add_test(NAME short_comparison_test COMMAND short_comparison_test)
add_test(NAME Short_Comparison_Test COMMAND short_comparison_test)
add_executable(multifile_test unittests/multifile_test_main.cpp unittests/multifile_test_chai.cpp

View File

@@ -831,14 +831,21 @@ namespace chaiscript
AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Inline_Array_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
std::vector<Boxed_Value> vec;
if (this->children.size() > 0) {
for (size_t i = 0; i < this->children[0]->children.size(); ++i) {
vec.push_back(this->children[0]->children[i]->eval(t_ss));
try {
std::vector<Boxed_Value> vec;
if (this->children.size() > 0) {
for (size_t i = 0; i < this->children[0]->children.size(); ++i) {
Boxed_Value bv = t_ss.call_function("clone", this->children[0]->children[i]->eval(t_ss));
bv.clear_dependencies();
vec.push_back(bv);
}
}
return const_var(vec);
}
catch (const exception::dispatch_error &) {
throw exception::eval_error("Can not find appropriate 'clone' or copy constructor for vector elements");
}
return const_var(vec);
}
};
@@ -852,13 +859,15 @@ namespace chaiscript
try {
std::map<std::string, Boxed_Value> retval;
for (size_t i = 0; i < this->children[0]->children.size(); ++i) {
Boxed_Value bv = t_ss.call_function("clone", this->children[0]->children[i]->children[1]->eval(t_ss));
bv.clear_dependencies();
retval[boxed_cast<std::string>(this->children[0]->children[i]->children[0]->eval(t_ss))]
= t_ss.call_function("clone", this->children[0]->children[i]->children[1]->eval(t_ss));
= bv;
}
return const_var(retval);
}
catch (const exception::dispatch_error &) {
throw exception::eval_error("Can not find appropriate 'Map()'");
throw exception::eval_error("Can not find appropriate 'clone' or copy constructor for map elements");
}
}

View File

@@ -1,3 +1,14 @@
var x = ["bob":1, "fred":2]
assert_equal(2, x.size());
// Make sure vector elements are copied into place for consistency with
// map inplace construction
var i = 1;
var y = ["a":i];
assert_equal(1, y["a"]);
i = 3;
assert_equal(3, i);
assert_equal(1, y["a"]);

View File

@@ -1,2 +1,14 @@
var x = [1, 2, 3]
assert_equal(3, x.size())
// Make sure vector elements are copied into place for consistency with
// map inplace construction
var i = 1;
var y = [i];
assert_equal(1, y[0]);
i = 3;
assert_equal(3, i);
assert_equal(1, y[0]);