add destructor for obj_file & change arguments in test/tetris.nas
This commit is contained in:
parent
6a1338bb23
commit
82e9e97a26
|
@ -732,6 +732,11 @@ nasal_ref builtin_chr(nasal_ref* local,nasal_gc& gc)
|
||||||
ret.str()=" ";
|
ret.str()=" ";
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
void obj_file_destructor(void* ptr)
|
||||||
|
{
|
||||||
|
fclose((FILE*)ptr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
nasal_ref builtin_open(nasal_ref* local,nasal_gc& gc)
|
nasal_ref builtin_open(nasal_ref* local,nasal_gc& gc)
|
||||||
{
|
{
|
||||||
nasal_ref filename=local[1];
|
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);
|
nasal_ref ret=gc.alloc(vm_obj);
|
||||||
ret.obj().type=obj_file;
|
ret.obj().type=obj_file;
|
||||||
ret.obj().ptr=(void*)res;
|
ret.obj().ptr=(void*)res;
|
||||||
|
ret.obj().destructor=obj_file_destructor;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
nasal_ref builtin_close(nasal_ref* local,nasal_gc& gc)
|
nasal_ref builtin_close(nasal_ref* local,nasal_gc& gc)
|
||||||
{
|
{
|
||||||
nasal_ref filehandle=local[1];
|
nasal_ref fd=local[1];
|
||||||
if(filehandle.type!=vm_obj || filehandle.obj().type!=obj_file)
|
if(fd.type!=vm_obj || fd.obj().type!=obj_file)
|
||||||
return builtin_err("close","not a correct filehandle");
|
return builtin_err("close","not a correct filehandle");
|
||||||
fclose((FILE*)filehandle.obj().ptr);
|
fclose((FILE*)fd.obj().ptr);
|
||||||
|
fd.obj().ptr=nullptr;
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
nasal_ref builtin_read(nasal_ref* local,nasal_gc& gc)
|
nasal_ref builtin_read(nasal_ref* local,nasal_gc& gc)
|
||||||
|
|
15
nasal_gc.h
15
nasal_gc.h
|
@ -148,16 +148,21 @@ struct nasal_upval
|
||||||
|
|
||||||
struct nasal_obj
|
struct nasal_obj
|
||||||
{
|
{
|
||||||
|
/* RAII constructor */
|
||||||
|
/* new object is initialized when creating */
|
||||||
uint32_t type;
|
uint32_t type;
|
||||||
void* ptr;
|
void* ptr;
|
||||||
void* destructor;
|
|
||||||
|
/* RAII destroyer */
|
||||||
|
typedef void (*dest)(void*);
|
||||||
|
dest destructor;
|
||||||
|
|
||||||
nasal_obj():ptr(nullptr),destructor(nullptr){}
|
nasal_obj():ptr(nullptr),destructor(nullptr){}
|
||||||
~nasal_obj(){clear();}
|
~nasal_obj(){clear();}
|
||||||
void clear()
|
void clear()
|
||||||
{
|
{
|
||||||
typedef void (*func)(void*);
|
if(destructor && ptr)
|
||||||
if(destructor)
|
destructor(ptr);
|
||||||
(func(destructor))(ptr);
|
|
||||||
ptr=nullptr;
|
ptr=nullptr;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -238,6 +238,7 @@ var mapgen=func(mapx,mapy){
|
||||||
}
|
}
|
||||||
|
|
||||||
var checkmap=func(){
|
var checkmap=func(){
|
||||||
|
var lines=1;
|
||||||
for(var y=mapy-1;y>=0;y-=1){
|
for(var y=mapy-1;y>=0;y-=1){
|
||||||
# check if this line is full of blocks
|
# check if this line is full of blocks
|
||||||
var tmp=0;
|
var tmp=0;
|
||||||
|
@ -249,7 +250,8 @@ var mapgen=func(mapx,mapy){
|
||||||
# if is full, clear this line and
|
# if is full, clear this line and
|
||||||
# all the lines above fall one block
|
# all the lines above fall one block
|
||||||
if(x==mapx){
|
if(x==mapx){
|
||||||
score+=tmp;
|
score+=lines*tmp;
|
||||||
|
lines*=2;
|
||||||
for(var t=y;t>=1;t-=1)
|
for(var t=y;t>=1;t-=1)
|
||||||
for(var x=0;x<mapx;x+=1)
|
for(var x=0;x<mapx;x+=1)
|
||||||
map[t][x]=map[t-1][x];
|
map[t][x]=map[t-1][x];
|
||||||
|
@ -296,7 +298,7 @@ var main=func(){
|
||||||
libkey.getch();
|
libkey.getch();
|
||||||
print("\ec");
|
print("\ec");
|
||||||
|
|
||||||
var counter=15;
|
var counter=30;
|
||||||
while(1){
|
while(1){
|
||||||
# nonblock input one character
|
# nonblock input one character
|
||||||
var ch=libkey.nonblock();
|
var ch=libkey.nonblock();
|
||||||
|
@ -326,9 +328,9 @@ var main=func(){
|
||||||
map.checkmap();
|
map.checkmap();
|
||||||
if(map.gameover())
|
if(map.gameover())
|
||||||
break;
|
break;
|
||||||
counter=15;
|
counter=30;
|
||||||
}
|
}
|
||||||
unix.sleep(0.04);
|
unix.sleep(0.02);
|
||||||
counter-=1;
|
counter-=1;
|
||||||
}
|
}
|
||||||
libkey.close();
|
libkey.close();
|
||||||
|
|
Loading…
Reference in New Issue