add unix.isdir and unix.isfile

This commit is contained in:
ValKmjolnir 2021-11-15 22:47:52 +08:00
parent 6a35c58df4
commit 52b49edbcf
6 changed files with 35 additions and 7 deletions

View File

@ -100,6 +100,8 @@ var unix=
dup2: func(fd0,fd1){die("not supported yet");},
exec: func(filename,argv,envp){die("not supported yet");},
waitpid: func(pid,nohang=0){die("not supported yet");},
isdir: func(path){return bits.bitand(io.stat(path)[2],0x4000);}, # S_IFDIR 0x4000
isfile: func(path){return bits.bitand(io.stat(path)[2],0x8000);}, # S_IFREG 0x8000
opendir: func(path){return __builtin_opendir;},
readdir: func(handle){return __builtin_readdir;},
closedir: func(handle){return __builtin_closedir;},

View File

@ -1231,7 +1231,7 @@ void nasal_codegen::print_op(uint32_t index)
case op_addeqc:case op_subeqc: case op_muleqc:case op_diveqc:
case op_lessc: case op_leqc: case op_grtc: case op_geqc:
case op_pnum:
printf("0x%x (%lf)\n",c.num,num_res[c.num]);break;
printf("0x%x (",c.num);std::cout<<num_res[c.num]<<")\n";break;
case op_callvi:case op_newv: case op_callfv:
case op_intg: case op_intl:
case op_newf: case op_jmp: case op_jt: case op_jf:

View File

@ -307,7 +307,7 @@ inline nasal_hash* nasal_ref::hash(){return value.gcobj->ptr.hash;}
inline nasal_func* nasal_ref::func(){return value.gcobj->ptr.func;}
inline nasal_obj* nasal_ref::obj (){return value.gcobj->ptr.obj; }
constexpr uint32_t STACK_MAX_DEPTH=2047;
constexpr uint32_t STACK_MAX_DEPTH=4095;
struct nasal_gc
{
nasal_ref zero;

View File

@ -188,7 +188,7 @@ void nasal_vm::traceback()
if(same)
printf("\t0x%.8x: %d same call(s) ...\n",last,same);
}
void nasal_vm::stackinfo(const uint32_t limit=10)
void nasal_vm::stackinfo(const uint32_t limit=20)
{
printf("vm stack(limit %d):\n",limit);
uint32_t same=0,global_size=bytecode[0].num;
@ -327,7 +327,7 @@ inline void nasal_vm::opr_pzero()
}
inline void nasal_vm::opr_pnil()
{
(++gc.top)[0].type=vm_nil;
(++gc.top)[0]={vm_nil,(double)0};
}
inline void nasal_vm::opr_pstr()
{
@ -394,7 +394,7 @@ inline void nasal_vm::opr_unot()
{
double num=str2num(val.str()->c_str());
if(std::isnan(num))
gc.top[0]=val.str()->empty()?gc.one:gc.zero;
gc.top[0]={vm_num,(double)val.str()->empty()};
else
gc.top[0]=num?gc.zero:gc.one;
}

View File

@ -100,6 +100,8 @@ var unix=
dup2: func(fd0,fd1){die("not supported yet");},
exec: func(filename,argv,envp){die("not supported yet");},
waitpid: func(pid,nohang=0){die("not supported yet");},
isdir: func(path){return bits.bitand(io.stat(path)[2],0x4000);}, # S_IFDIR 0x4000
isfile: func(path){return bits.bitand(io.stat(path)[2],0x8000);}, # S_IFREG 0x8000
opendir: func(path){return __builtin_opendir;},
readdir: func(handle){return __builtin_readdir;},
closedir: func(handle){return __builtin_closedir;},

View File

@ -7,6 +7,30 @@ io.close(fd);
println(io.stat("test/filesystem.nas"));
var dd=unix.opendir("test");
while((var name=unix.readdir(dd))!=nil)
while(var name=unix.readdir(dd))
println(name);
unix.closedir(dd);
unix.closedir(dd);
var files=func(dir){
var dd=unix.opendir(dir);
var res=[];
while(var n=unix.readdir(dd))
append(res,n);
unix.closedir(dd);
return res;
}
var prt=func(s,path){
foreach(var i;files(path)){
print(s,i);
if(unix.isdir(path~'/'~i)){
print(' <dir>\n');
if(i!='.' and i!='..')
prt(s~' |',path~'/'~i);
}
elsif(unix.isfile(path~'/'~i))
print(" <file>\n");
else
print(' <unknown>\n');
}
}
prt('',".");