diff --git a/nasal_builtin.h b/nasal_builtin.h index 28c2fa8..cf6149e 100644 --- a/nasal_builtin.h +++ b/nasal_builtin.h @@ -732,6 +732,11 @@ nasal_ref builtin_chr(nasal_ref* local,nasal_gc& gc) ret.str()=" "; return ret; } +void obj_file_destructor(void* ptr) +{ + fclose((FILE*)ptr); + return; +} nasal_ref builtin_open(nasal_ref* local,nasal_gc& gc) { nasal_ref filename=local[1]; @@ -746,14 +751,16 @@ nasal_ref builtin_open(nasal_ref* local,nasal_gc& gc) nasal_ref ret=gc.alloc(vm_obj); ret.obj().type=obj_file; ret.obj().ptr=(void*)res; + ret.obj().destructor=obj_file_destructor; return ret; } nasal_ref builtin_close(nasal_ref* local,nasal_gc& gc) { - nasal_ref filehandle=local[1]; - if(filehandle.type!=vm_obj || filehandle.obj().type!=obj_file) + nasal_ref fd=local[1]; + if(fd.type!=vm_obj || fd.obj().type!=obj_file) return builtin_err("close","not a correct filehandle"); - fclose((FILE*)filehandle.obj().ptr); + fclose((FILE*)fd.obj().ptr); + fd.obj().ptr=nullptr; return nil; } nasal_ref builtin_read(nasal_ref* local,nasal_gc& gc) diff --git a/nasal_gc.h b/nasal_gc.h index 2709468..125acbe 100644 --- a/nasal_gc.h +++ b/nasal_gc.h @@ -148,16 +148,21 @@ struct nasal_upval struct nasal_obj { + /* RAII constructor */ + /* new object is initialized when creating */ uint32_t type; void* ptr; - void* destructor; + + /* RAII destroyer */ + typedef void (*dest)(void*); + dest destructor; + nasal_obj():ptr(nullptr),destructor(nullptr){} ~nasal_obj(){clear();} void clear() - { - typedef void (*func)(void*); - if(destructor) - (func(destructor))(ptr); + { + if(destructor && ptr) + destructor(ptr); ptr=nullptr; } }; diff --git a/test/tetris.nas b/test/tetris.nas index d962405..5cde440 100644 --- a/test/tetris.nas +++ b/test/tetris.nas @@ -238,6 +238,7 @@ var mapgen=func(mapx,mapy){ } var checkmap=func(){ + var lines=1; for(var y=mapy-1;y>=0;y-=1){ # check if this line is full of blocks var tmp=0; @@ -249,7 +250,8 @@ var mapgen=func(mapx,mapy){ # if is full, clear this line and # all the lines above fall one block if(x==mapx){ - score+=tmp; + score+=lines*tmp; + lines*=2; for(var t=y;t>=1;t-=1) for(var x=0;x