From 6a35c58df469710d3df3abe4c21de7f8abe13be8 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Sun, 14 Nov 2021 23:05:34 +0800 Subject: [PATCH] 2 bugs fixed: empty string will be true in conditional expressions,but now it is false(and string that is not empty and is not numerable will be true) foreach(var i;func(){return []}()); will cause sigsegv because codegen generates error op_pop and op_cntpop,now the counter in_foreach and in_forindex change after generating foreach/forindex expression before block generation. --- nasal_codegen.h | 14 ++++++++------ nasal_vm.h | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/nasal_codegen.h b/nasal_codegen.h index 40fd460..64a28fb 100644 --- a/nasal_codegen.h +++ b/nasal_codegen.h @@ -749,10 +749,10 @@ void nasal_codegen::loop_gen(const nasal_ast& ast) break_ptr.push_front(std::vector()); switch(ast.type()) { - case ast_while: while_gen(ast); break; - case ast_for: for_gen(ast); break; - case ast_forindex: ++in_forindex;forindex_gen(ast);--in_forindex; break; - case ast_foreach: ++in_foreach; foreach_gen(ast); --in_foreach; break; + case ast_while: while_gen(ast); break; + case ast_for: for_gen(ast); break; + case ast_forindex:forindex_gen(ast);break; + case ast_foreach: foreach_gen(ast); break; } } @@ -872,8 +872,9 @@ void nasal_codegen::forindex_gen(const nasal_ast& ast) gen(op_meq,0,ast[0].line()); gen(op_pop,0,0); } - + ++in_forindex; block_gen(ast[2]); + --in_forindex; gen(op_jmp,ptr,0); code[ptr].num=code.size(); load_continue_break(code.size()-1,code.size()); @@ -899,8 +900,9 @@ void nasal_codegen::foreach_gen(const nasal_ast& ast) gen(op_meq,0,ast[0].line()); gen(op_pop,0,0); } - + ++in_foreach; block_gen(ast[2]); + --in_foreach; gen(op_jmp,ptr,0); code[ptr].num=code.size(); load_continue_break(code.size()-1,code.size()); diff --git a/nasal_vm.h b/nasal_vm.h index 51a6e65..97fefeb 100644 --- a/nasal_vm.h +++ b/nasal_vm.h @@ -284,7 +284,7 @@ inline bool nasal_vm::condition(nasal_ref val) { double num=str2num(val.str()->c_str()); if(std::isnan(num)) - return val.str()->empty(); + return !val.str()->empty(); return num; } return false;