add unix.time unix.chdir unix.sleep unix.getcwd unix.getenv

This commit is contained in:
ValKmjolnir 2021-10-29 19:52:49 +08:00
parent 4bfce37f40
commit e4ea34db51
5 changed files with 157 additions and 17 deletions

View File

@ -1117,4 +1117,75 @@ trace back:
0x00000008: callv 0x0 (a.nas line 3)
vm stack(limit 10):
num | 0.000000
```
Use command `-d` or `--detail` the trace back info will be this:
```javascript
hello world
[vm] error: exception test
[vm] native function error.
trace back:
0x0000008f: callb 0x22 <__builtin_die> (<lib.nas> line 20)
0x00000214: callfv 0x1 (<test/exception.nas> line 16)
0x00000248: callfv 0x0 (<test/exception.nas> line 39)
vm stack(limit 10):
null |
func | <0x23bc3f0> func{entry=0x8f}
func | <0x23bdc50> func{entry=0x20e}
mcall address: 0x24a4b88
global:
[0] func | <0x23d3960> func{entry=0x5}
[1] func | <0x23bb8b0> func{entry=0xc}
[2] func | <0x23bb950> func{entry=0x14}
[3] func | <0x23bb9f0> func{entry=0x1c}
[4] func | <0x23bba90> func{entry=0x23}
[5] func | <0x23bbb30> func{entry=0x29}
[6] func | <0x23bbbd0> func{entry=0x30}
[7] func | <0x23bbc70> func{entry=0x38}
[8] func | <0x23bbd10> func{entry=0x40}
[9] func | <0x23bbdb0> func{entry=0x47}
[10] func | <0x23bbe50> func{entry=0x4e}
[11] func | <0x23bbef0> func{entry=0x55}
[12] func | <0x23bbf90> func{entry=0x5c}
[13] func | <0x23bc030> func{entry=0x63}
[14] func | <0x23bc0d0> func{entry=0x6a}
[15] func | <0x23bc170> func{entry=0x72}
[16] func | <0x23bc210> func{entry=0x7a}
[17] func | <0x23bc2b0> func{entry=0x81}
[18] func | <0x23bc350> func{entry=0x88}
[19] func | <0x23bc3f0> func{entry=0x8f}
[20] func | <0x23bc490> func{entry=0x96}
[21] func | <0x23bc530> func{entry=0x9f}
[22] func | <0x23bc5d0> func{entry=0xa7}
[23] func | <0x23bc670> func{entry=0xaf}
[24] func | <0x23bc710> func{entry=0xb7}
[25] func | <0x23bc7b0> func{entry=0xbf}
[26] func | <0x23bc850> func{entry=0xc6}
[27] func | <0x23bc8f0> func{entry=0xcd}
[28] hash | <0x248ae70> {14 member}
[29] hash | <0x248aed0> {9 member}
[30] hash | <0x248af30> {12 member}
[31] num | 0.017453
[32] num | 0.592500
[33] num | 0.304800
[34] num | 3.785400
[35] num | 0.025400
[36] num | 2.204600
[37] num | 1.687800
[38] num | 0.514400
[39] num | 0.264200
[40] num | 0.453600
[41] num | 3.280800
[42] num | 39.370100
[43] num | 0.000540
[44] num | 1.943800
[45] num | 1852.000000
[46] num | 57.295780
[47] hash | <0x248af90> {3 member}
[48] func | <0x23bdcf0> func{entry=0x21e}
[49] func | <0x23bdd90> func{entry=0x22d}
[50] func | <0x23bde30> func{entry=0x237}
local:
[0] nil |
[1] str | <0x249abd0> exception test
```

23
lib.nas
View File

@ -4,7 +4,6 @@ var append= func(vec,elems...){return __builtin_append(vec,elems);}
var setsize= func(vec,size){return __builtin_setsize(vec,size);}
var system= func(str){return __builtin_system(str);}
var input= func(){return __builtin_input();}
var sleep= func(duration){return __builtin_sleep(duration);}
var split= func(deli,str){return __builtin_split(deli,str);}
var rand= func(seed=nil){return __builtin_rand(seed);}
var id= func(object){return __builtin_id(object);}
@ -30,6 +29,7 @@ var println=func(elems...){
elems=['\n'];
return __builtin_print(elems);
}
var io=
{
SEEK_SET:0,
@ -76,7 +76,6 @@ var math=
nan: 0/0,
isnan: func(x) {return __builtin_isnan(x); }
};
var D2R=math.pi/180;
var FPS2KT=0.5925;
var FT2M=0.3048;
@ -92,4 +91,22 @@ var M2IN=39.3701;
var M2NM=0.00054;
var MPS2KT=1.9438;
var NM2M=1852;
var R2D=180/math.pi;
var R2D=180/math.pi;
var unix=
{
pipe: func(){die("not supported yet");},
fork: func(){die("not supported yet");},
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");},
opendir: func(path){die("not supported yet");},
readdir: func(handle){die("not supported yet");},
closedir: func(handle){die("not supported yet");},
time: func(){return time(0);},
sleep: func(secs){return __builtin_sleep(secs);},
chdir: func(path){return __builtin_chdir(path);},
environ: func(){die("not supported yet");},
getcwd: func(){return __builtin_getcwd();},
getenv: func(envvar){return __builtin_getenv(envvar);}
};

View File

@ -20,6 +20,7 @@
#include <unordered_map>
#include <sys/stat.h>
#include <fcntl.h>
inline double hex_to_double(const char* str)
{

View File

@ -19,7 +19,6 @@ nas_native(builtin_append);
nas_native(builtin_setsize);
nas_native(builtin_system);
nas_native(builtin_input);
nas_native(builtin_sleep);
nas_native(builtin_fin);
nas_native(builtin_fout);
nas_native(builtin_split);
@ -69,6 +68,10 @@ nas_native(builtin_fld);
nas_native(builtin_sfld);
nas_native(builtin_setfld);
nas_native(builtin_buf);
nas_native(builtin_sleep);
nas_native(builtin_chdir);
nas_native(builtin_getcwd);
nas_native(builtin_getenv);
nasal_ref builtin_err(const char* func_name,std::string info)
{
@ -89,7 +92,6 @@ struct
{"__builtin_setsize", builtin_setsize },
{"__builtin_system", builtin_system },
{"__builtin_input", builtin_input },
{"__builtin_sleep", builtin_sleep },
{"__builtin_fin", builtin_fin },
{"__builtin_fout", builtin_fout },
{"__builtin_split", builtin_split },
@ -139,6 +141,10 @@ struct
{"__builtin_sfld", builtin_sfld },
{"__builtin_setfld", builtin_setfld },
{"__builtin_buf", builtin_buf },
{"__builtin_sleep", builtin_sleep },
{"__builtin_chdir", builtin_chdir },
{"__builtin_getcwd", builtin_getcwd },
{"__builtin_getenv", builtin_getenv },
{nullptr, nullptr }
};
@ -202,15 +208,6 @@ nasal_ref builtin_input(std::vector<nasal_ref>& local,nasal_gc& gc)
std::cin>>*ret.str();
return ret;
}
nasal_ref builtin_sleep(std::vector<nasal_ref>& local,nasal_gc& gc)
{
nasal_ref val=local[1];
if(val.type!=vm_num)
return builtin_err("sleep","\"duration\" must be number");
// sleep in unistd.h will make this progress sleep sleep_time seconds.
sleep((unsigned int)val.num());
return gc.nil;
}
nasal_ref builtin_fin(std::vector<nasal_ref>& local,nasal_gc& gc)
{
nasal_ref val=local[1];
@ -890,4 +887,41 @@ nasal_ref builtin_buf(std::vector<nasal_ref>& local,nasal_gc& gc)
s.resize(length.num(),'\0');
return str;
}
nasal_ref builtin_sleep(std::vector<nasal_ref>& local,nasal_gc& gc)
{
nasal_ref val=local[1];
if(val.type!=vm_num)
return builtin_err("sleep","\"duration\" must be number");
usleep((useconds_t)(val.num()*1e6));
return gc.nil;
}
nasal_ref builtin_chdir(std::vector<nasal_ref>& local,nasal_gc& gc)
{
nasal_ref path=local[1];
if(path.type!=vm_str)
return builtin_err("chdir","\"path\" must be string");
if(chdir(path.str()->c_str())<0)
return builtin_err("chdir","failed to execute chdir");
return gc.nil;
}
nasal_ref builtin_getcwd(std::vector<nasal_ref>& local,nasal_gc& gc)
{
char buf[1024];
getcwd(buf,sizeof(buf));
nasal_ref str=gc.alloc(vm_str);
*str.str()=buf;
return str;
}
nasal_ref builtin_getenv(std::vector<nasal_ref>& local,nasal_gc& gc)
{
nasal_ref envvar=local[1];
if(envvar.type!=vm_str)
return builtin_err("getenv","\"envvar\" must be string");
char* res=getenv(envvar.str()->c_str());
if(!res)
return gc.nil;
nasal_ref str=gc.alloc(vm_str);
*str.str()=res;
return str;
}
#endif

View File

@ -4,7 +4,6 @@ var append= func(vec,elems...){return __builtin_append(vec,elems);}
var setsize= func(vec,size){return __builtin_setsize(vec,size);}
var system= func(str){return __builtin_system(str);}
var input= func(){return __builtin_input();}
var sleep= func(duration){return __builtin_sleep(duration);}
var split= func(deli,str){return __builtin_split(deli,str);}
var rand= func(seed=nil){return __builtin_rand(seed);}
var id= func(object){return __builtin_id(object);}
@ -30,6 +29,7 @@ var println=func(elems...){
elems=['\n'];
return __builtin_print(elems);
}
var io=
{
SEEK_SET:0,
@ -76,7 +76,6 @@ var math=
nan: 0/0,
isnan: func(x) {return __builtin_isnan(x); }
};
var D2R=math.pi/180;
var FPS2KT=0.5925;
var FT2M=0.3048;
@ -92,4 +91,22 @@ var M2IN=39.3701;
var M2NM=0.00054;
var MPS2KT=1.9438;
var NM2M=1852;
var R2D=180/math.pi;
var R2D=180/math.pi;
var unix=
{
pipe: func(){die("not supported yet");},
fork: func(){die("not supported yet");},
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");},
opendir: func(path){die("not supported yet");},
readdir: func(handle){die("not supported yet");},
closedir: func(handle){die("not supported yet");},
time: func(){return time(0);},
sleep: func(secs){return __builtin_sleep(secs);},
chdir: func(path){return __builtin_chdir(path);},
environ: func(){die("not supported yet");},
getcwd: func(){return __builtin_getcwd();},
getenv: func(envvar){return __builtin_getenv(envvar);}
};