diff --git a/README.md b/README.md index 74ff242..b11feb9 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ If you think `-O3` isn't that safe and stable, you could choose: > > mingw32-make stable-release-mingw -You could create project in `Visual Studio` by this way: [CLICK](./doc/vs.md). +You could create project in `Visual Studio` by this way: [__CLICK__](./doc/vs.md). ## __How to Use__ @@ -679,7 +679,7 @@ Then we write a test nasal file to run this fib function, using `os.platform()` ```javascript import("lib.nas"); -var dlhandle=dylib.dlopen("./module/libfib."~(os.platform()=="windows"?"dll":"so")); +var dlhandle=dylib.dlopen("libfib."~(os.platform()=="windows"?"dll":"so")); var fib=dylib.dlsym(dlhandle,"fib"); for(var i=1;i<30;i+=1) println(dylib.dlcall(fib,i)); diff --git a/doc/README_zh.md b/doc/README_zh.md index f5fa575..57d2f48 100644 --- a/doc/README_zh.md +++ b/doc/README_zh.md @@ -92,7 +92,7 @@ __`linux/macOS/Unix`__ 平台直接使用make即可: > > mingw32-make stable-release-mingw -你可以在`Visual Studio`中用这种方式来创建项目:[点击跳转](../doc/vs.md)。 +你可以在`Visual Studio`中用这种方式来创建项目:[__点击跳转__](../doc/vs.md)。 ## __使用方法__ @@ -643,7 +643,7 @@ Windows(`.dll`): ```javascript import("lib.nas"); -var dlhandle=dylib.dlopen("./module/libfib."~(os.platform()=="windows"?"dll":"so")); +var dlhandle=dylib.dlopen("libfib."~(os.platform()=="windows"?"dll":"so")); var fib=dylib.dlsym(dlhandle,"fib"); for(var i=1;i<30;i+=1) println(dylib.dlcall(fib,i)); diff --git a/lib.nas b/lib.nas index a850a15..8621639 100644 --- a/lib.nas +++ b/lib.nas @@ -427,16 +427,18 @@ var dylib= { # open dynamic lib. dlopen: func(libname){ + # find dynamic lib from local dir first + if(io.exists(libname)) + return __dlopen(libname); + # find dynamic lib through PATH var envpath=split(os.platform()=="windows"?";":":",unix.getenv("PATH")); - var path=os.platform()=="windows"?["\\","\\module\\"]:["/","/module/"]; + # first find ./module + append(envpath,"."); + var path=os.platform()=="windows"?"\\module\\":"/module/"; foreach(var p;envpath){ - p=[p~path[0]~libname,p~path[1]~libname]; - if(io.exists(p[0])){ - libname=p[0]; - break; - } - if(io.exists(p[1])){ - libname=p[1]; + p~=path~libname; + if(io.exists(p)){ + libname=p; break; } } diff --git a/main.cpp b/main.cpp index 203dc05..edb4914 100644 --- a/main.cpp +++ b/main.cpp @@ -24,26 +24,23 @@ void help() <<"option:\n" <<" -h, --help | get this help.\n" <<" -v, --version | get version of nasal interpreter.\n\n" - <<"nasal \n" - <<"file:\n" - <<" input file name to execute script file.\n\n" <<"nasal [option...] [argv...]\n" <<"option:\n" <<" -l, --lex | view token info.\n" <<" -a, --ast | view abstract syntax tree.\n" <<" -c, --code | view bytecode.\n" <<" -e, --exec | execute.\n" - <<" -t, --time | execute and get the running time.\n" - <<" -o, --opcnt | execute and count used operands.\n" - <<" | this options is available in debug mode.\n" - <<" -d, --detail | execute and get detail crash info.\n" + <<" -t, --time | get the running time.\n" + <<" -o, --opcnt | count used operands.\n" + <<" | available in debug mode.\n" + <<" -d, --detail | get detail crash info.\n" <<" | get garbage collector info if didn't crash.\n" <<" -op, --optimize| use optimizer(beta).\n" <<" | if want to use -op and run, please use -op -e/-t/-d.\n" <<" -dbg, --debug | debug mode (this will ignore -t -d).\n" <<" -cp, --chkpath | show path if linker cannot find files.\n" <<"file:\n" - <<" input file name to execute script file.\n" + <<" input file name to execute.\n" <<"argv:\n" <<" command line arguments used in program.\n"; } diff --git a/module/libfib.nas b/module/libfib.nas index f9a576e..07f6d63 100644 --- a/module/libfib.nas +++ b/module/libfib.nas @@ -1,5 +1,5 @@ var libfib=func(){ - var dl=dylib.dlopen("./module/libfib."~(os.platform()=="windows"?"dll":"so")); + var dl=dylib.dlopen("libfib."~(os.platform()=="windows"?"dll":"so")); var fib=dylib.dlsym(dl,"fib"); var qfib=dylib.dlsym(dl,"quick_fib"); var call=dylib.dlcall; diff --git a/module/libkey.nas b/module/libkey.nas index 2b11735..114799a 100644 --- a/module/libkey.nas +++ b/module/libkey.nas @@ -1,5 +1,5 @@ var libkey=func(){ - var lib=dylib.dlopen("./module/libkey"~(os.platform()=="windows"?".dll":".so")); + var lib=dylib.dlopen("libkey"~(os.platform()=="windows"?".dll":".so")); var kb=dylib.dlsym(lib,"nas_kbhit"); var gt=dylib.dlsym(lib,"nas_getch"); var nb=dylib.dlsym(lib,"nas_noblock"); diff --git a/module/libsock.nas b/module/libsock.nas index c7fb8df..e187fce 100644 --- a/module/libsock.nas +++ b/module/libsock.nas @@ -1,5 +1,5 @@ var socket=func(){ - var lib=dylib.dlopen("./module/libnasock"~(os.platform()=="windows"?".dll":".so")); + var lib=dylib.dlopen("libnasock"~(os.platform()=="windows"?".dll":".so")); var sock=dylib.dlsym(lib,"nas_socket"); var closesocket=dylib.dlsym(lib,"nas_closesocket"); var shutdown=dylib.dlsym(lib,"nas_shutdown"); diff --git a/nasal_ast.h b/nasal_ast.h index 3529a4c..19aab06 100644 --- a/nasal_ast.h +++ b/nasal_ast.h @@ -214,12 +214,9 @@ void nasal_ast::print(int depth,bool last=false) for(auto& i:intentation) std::cout< filepath={fname,"stl\\"+fname}; -#else - std::vector filepath={fname,"stl/"+fname}; -#endif + std::vector filepath={fname}; for(auto&p:envpath) { #ifdef _WIN32 filepath.push_back(p+"\\"+fname); - filepath.push_back(p+"\\stl\\"+fname); #else filepath.push_back(p+"/"+fname); - filepath.push_back(p+"/stl/"+fname); #endif } for(auto& i:filepath) if(access(i.c_str(),F_OK)!=-1) return i; + if(fname=="lib.nas") +#ifdef _WIN32 + return findf("stl\\lib.nas"); +#else + return findf("stl/lib.nas"); +#endif if(!show_path) { nerr.err("link","cannot find file <"+fname+">"); diff --git a/stl/lib.nas b/stl/lib.nas index a850a15..8621639 100644 --- a/stl/lib.nas +++ b/stl/lib.nas @@ -427,16 +427,18 @@ var dylib= { # open dynamic lib. dlopen: func(libname){ + # find dynamic lib from local dir first + if(io.exists(libname)) + return __dlopen(libname); + # find dynamic lib through PATH var envpath=split(os.platform()=="windows"?";":":",unix.getenv("PATH")); - var path=os.platform()=="windows"?["\\","\\module\\"]:["/","/module/"]; + # first find ./module + append(envpath,"."); + var path=os.platform()=="windows"?"\\module\\":"/module/"; foreach(var p;envpath){ - p=[p~path[0]~libname,p~path[1]~libname]; - if(io.exists(p[0])){ - libname=p[0]; - break; - } - if(io.exists(p[1])){ - libname=p[1]; + p~=path~libname; + if(io.exists(p)){ + libname=p; break; } } diff --git a/test/module_test.nas b/test/module_test.nas index ea11ab7..7f87809 100644 --- a/test/module_test.nas +++ b/test/module_test.nas @@ -3,7 +3,7 @@ var libfib=func(){ return { open:func(){ if(dd==nil){ - dd=dylib.dlopen("./module/libfib."~(os.platform()=="windows"?"dll":"so")); + dd=dylib.dlopen("libfib."~(os.platform()=="windows"?"dll":"so")); fib=dylib.dlsym(dd,"fib"); qfib=dylib.dlsym(dd,"quick_fib"); }else{ diff --git a/test/occupation.nas b/test/occupation.nas index 6c5d38a..59c6131 100644 --- a/test/occupation.nas +++ b/test/occupation.nas @@ -18,7 +18,7 @@ var cpu_stat=func(){ } var cpu_occupation=func(){ var cpu0=cpu_stat(); - unix.sleep(1); + unix.sleep(0.5); var cpu1=cpu_stat(); var t0=cpu0.user+cpu0.nice+cpu0.system+cpu0.idle+cpu0.iowait+cpu0.irq+cpu0.softirq; var t1=cpu1.user+cpu1.nice+cpu1.system+cpu1.idle+cpu1.iowait+cpu1.irq+cpu1.softirq;