Fix lifetime of objects in ranged for loops

Currently this is going to incur a performance cost, but it's correct.

It may need to be reevaluated.

Closes #392
This commit is contained in:
Jason Turner 2018-05-29 09:26:59 -06:00
parent d5d5561d74
commit ac0d7ce949
2 changed files with 13 additions and 7 deletions

View File

@ -925,10 +925,16 @@ namespace chaiscript
const auto do_loop = [&loop_var_name, &t_ss, this](const auto &ranged_thing){ const auto do_loop = [&loop_var_name, &t_ss, this](const auto &ranged_thing){
try { try {
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss); for (auto &&loop_var : ranged_thing) {
Boxed_Value &obj = t_ss.add_get_object(loop_var_name, void_var()); // This scope push and pop might not be the best thing for perf
for (auto loop_var : ranged_thing) { // but we know it's 100% correct
obj = Boxed_Value(std::move(loop_var)); chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
/// to-do make this if-constexpr with C++17 branch
if (!std::is_same<std::decay_t<decltype(loop_var)>, Boxed_Value>::value) {
t_ss.add_get_object(loop_var_name, Boxed_Value(std::ref(loop_var)));
} else {
t_ss.add_get_object(loop_var_name, Boxed_Value(loop_var));
}
try { try {
this->children[2]->eval(t_ss); this->children[2]->eval(t_ss);
} catch (detail::Continue_Loop &) { } catch (detail::Continue_Loop &) {
@ -952,10 +958,9 @@ namespace chaiscript
try { try {
const auto range_obj = call_function(range_funcs, range_expression_result); const auto range_obj = call_function(range_funcs, range_expression_result);
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
Boxed_Value &obj = t_ss.add_get_object(loop_var_name, void_var());
while (!boxed_cast<bool>(call_function(empty_funcs, range_obj))) { while (!boxed_cast<bool>(call_function(empty_funcs, range_obj))) {
obj = call_function(front_funcs, range_obj); chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
t_ss.add_get_object(loop_var_name, call_function(front_funcs, range_obj));
try { try {
this->children[2]->eval(t_ss); this->children[2]->eval(t_ss);
} catch (detail::Continue_Loop &) { } catch (detail::Continue_Loop &) {

View File

@ -14,6 +14,7 @@ Current Version: 6.1.0
* Support for C++17 compilers! * Support for C++17 compilers!
* Support for UTF8 BOM #439 @AlekMosingiewicz @MarioLiebisch * Support for UTF8 BOM #439 @AlekMosingiewicz @MarioLiebisch
### Changes since 5.8.6 ### Changes since 5.8.6
*6.0.0 is a massive rework compared to 5.x. It now requires a C++14 enabled compiler* *6.0.0 is a massive rework compared to 5.x. It now requires a C++14 enabled compiler*