From ae16f36baa22747e46c08dfbe2fbd6b4aa9232e1 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Thu, 17 Dec 2020 00:14:22 +0800 Subject: [PATCH] update --- nasal.h | 16 ++++++++-------- nasal_bytecode_vm.h | 14 ++++++++------ nasal_gc.h | 12 +++--------- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/nasal.h b/nasal.h index c4ef99b..9643c48 100644 --- a/nasal.h +++ b/nasal.h @@ -25,10 +25,10 @@ check if a string can be converted to a number if this string cannot be converted to a number,it will return nan */ -inline double hex_to_double(std::string str,int len) +inline double hex_to_double(std::string str) { double ret=0; - for(int i=2;i2 && str[0]=='0' && str[1]=='x') - ret_num=hex_to_double(str,len); - else if(len>2 && str[0]=='0' && str[1]=='o') - ret_num=oct_to_double(str,len); + if(len>1 && str[0]=='0' && str[1]=='x') + ret_num=hex_to_double(str); + else if(len>1 && str[0]=='0' && str[1]=='o') + ret_num=oct_to_double(str); else ret_num=dec_to_double(str,len); return is_negative*ret_num; diff --git a/nasal_bytecode_vm.h b/nasal_bytecode_vm.h index de69f1b..0a50db6 100644 --- a/nasal_bytecode_vm.h +++ b/nasal_bytecode_vm.h @@ -968,13 +968,14 @@ void nasal_bytecode_vm::opr_foreach() void nasal_bytecode_vm::opr_call() { int val_addr=-1; + std::string symbol=string_table[exec_code[ptr].index]; if(local_scope_stack.top()>=0) - val_addr=vm.gc_get(local_scope_stack.top()).get_closure().get_value_address(string_table[exec_code[ptr].index]); + val_addr=vm.gc_get(local_scope_stack.top()).get_closure().get_value_address(symbol); if(val_addr<0) - val_addr=vm.gc_get(global_scope_addr).get_closure().get_value_address(string_table[exec_code[ptr].index]); + val_addr=vm.gc_get(global_scope_addr).get_closure().get_value_address(symbol); if(val_addr<0) { - die("call: cannot find symbol named \""+string_table[exec_code[ptr].index]+"\""); + die("call: cannot find symbol named \""+symbol+"\""); return; } vm.add_reference(val_addr); @@ -1281,12 +1282,13 @@ void nasal_bytecode_vm::opr_slice2() void nasal_bytecode_vm::opr_mcall() { int* mem_addr=NULL; + std::string symbol=string_table[exec_code[ptr].index]; if(local_scope_stack.top()>=0) - mem_addr=vm.gc_get(local_scope_stack.top()).get_closure().get_mem_address(string_table[exec_code[ptr].index]); + mem_addr=vm.gc_get(local_scope_stack.top()).get_closure().get_mem_address(symbol); if(!mem_addr) - mem_addr=vm.gc_get(global_scope_addr).get_closure().get_mem_address(string_table[exec_code[ptr].index]); + mem_addr=vm.gc_get(global_scope_addr).get_closure().get_mem_address(symbol); if(!mem_addr) - die("mcall: cannot find symbol named \""+string_table[exec_code[ptr].index]+"\""); + die("mcall: cannot find symbol named \""+symbol+"\""); pointer_stack.push(mem_addr); return; } diff --git a/nasal_gc.h b/nasal_gc.h index 50c094f..69dd9af 100644 --- a/nasal_gc.h +++ b/nasal_gc.h @@ -131,12 +131,10 @@ class nasal_virtual_machine { struct gc_unit { - bool collected; int ref_cnt; nasal_scalar elem; gc_unit() { - collected=true; ref_cnt=0; return; } @@ -662,7 +660,6 @@ nasal_virtual_machine::~nasal_virtual_machine() if(garbage_collector_memory[i]->ref_cnt) { garbage_collector_memory[i]->ref_cnt=0; - garbage_collector_memory[i]->collected=true; garbage_collector_memory[i]->elem.clear(); } for(int i=0;iref_cnt) { garbage_collector_memory[i]->ref_cnt=0; - garbage_collector_memory[i]->collected=true; garbage_collector_memory[i]->elem.clear(); } for(int i=0;icollected) + if(0<=value_address) return garbage_collector_memory[value_address]->elem; return error_returned_value; } void nasal_virtual_machine::add_reference(int value_address) { - if(0<=value_address && !garbage_collector_memory[value_address]->collected) + if(0<=value_address) ++garbage_collector_memory[value_address]->ref_cnt; return; } void nasal_virtual_machine::del_reference(int value_address) { - if(0<=value_address && !garbage_collector_memory[value_address]->collected) + if(0<=value_address) --garbage_collector_memory[value_address]->ref_cnt; else return; if(!garbage_collector_memory[value_address]->ref_cnt) { - garbage_collector_memory[value_address]->collected=true; garbage_collector_memory[value_address]->elem.clear(); garbage_collector_free_space.push(value_address); }