🐛 fix bug of incorrectly searching paths of `lib.nas` and dynamic libs.

`dylib.dlopen` now only needs file name of dynamic lib, not the real path.
This commit is contained in:
ValKmjolnir 2022-07-29 22:49:50 +08:00
parent 006ed644e6
commit 854850d9b1
12 changed files with 44 additions and 46 deletions

View File

@ -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));

View File

@ -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));

18
lib.nas
View File

@ -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;
}
}

View File

@ -24,26 +24,23 @@ void help()
<<"option:\n"
<<" -h, --help | get this help.\n"
<<" -v, --version | get version of nasal interpreter.\n\n"
<<"nasal <file>\n"
<<"file:\n"
<<" input file name to execute script file.\n\n"
<<"nasal [option...] <file> [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";
}

View File

@ -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;

View File

@ -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");

View File

@ -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");

View File

@ -214,12 +214,9 @@ void nasal_ast::print(int depth,bool last=false)
for(auto& i:intentation)
std::cout<<i;
std::cout<<ast_name[_type];
if(
_type==ast_str ||
_type==ast_id ||
_type==ast_default ||
_type==ast_dynamic ||
_type==ast_callh)
if(_type==ast_str || _type==ast_id ||
_type==ast_default || _type==ast_dynamic ||
_type==ast_callh)
std::cout<<":"<<rawstr(_str);
else if(_type==ast_num || _type==ast_file)
std::cout<<":"<<_num;

View File

@ -64,24 +64,24 @@ string nasal_import::path(const nasal_ast& node)
string nasal_import::findf(const string& fname)
{
#ifdef _WIN32
std::vector<string> filepath={fname,"stl\\"+fname};
#else
std::vector<string> filepath={fname,"stl/"+fname};
#endif
std::vector<string> 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+">");

View File

@ -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;
}
}

View File

@ -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{

View File

@ -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;