bug fixed & add os.platform
This commit is contained in:
parent
85bb502191
commit
5a80258d20
|
@ -1068,7 +1068,7 @@ Linux(`.so`):
|
|||
|
||||
`clang++ -shared -o libfib.so fib.o`
|
||||
|
||||
Mac: same as Linux, but remember to generate `.dylib`
|
||||
Mac(`.so` & `.dylib`): same as Linux.
|
||||
|
||||
Windows(`.dll`):
|
||||
|
||||
|
|
22
lib.nas
22
lib.nas
|
@ -48,6 +48,23 @@ var io=
|
|||
eof: func(filehandle){return __builtin_eof(filehandle);}
|
||||
};
|
||||
|
||||
var fstat=func(filename){
|
||||
var s=io.stat(filename);
|
||||
return {
|
||||
st_dev: s[0],
|
||||
st_ino: s[1],
|
||||
st_mode: s[2],
|
||||
st_nlink:s[3],
|
||||
st_uid: s[4],
|
||||
st_gid: s[5],
|
||||
st_rdev: s[6],
|
||||
st_size: s[7],
|
||||
st_atime:s[8],
|
||||
st_mtime:s[9],
|
||||
st_ctime:s[10]
|
||||
};
|
||||
}
|
||||
|
||||
var bits=
|
||||
{
|
||||
bitxor: func(a,b){return __builtin_xor(a,b); },
|
||||
|
@ -121,3 +138,8 @@ var dylib=
|
|||
dlclose: func(lib){return __builtin_dlclose; },
|
||||
dlcall: func(funcptr,args...){return __builtin_dlcall}
|
||||
};
|
||||
|
||||
var os=
|
||||
{
|
||||
platform: func(){return __builtin_platform;}
|
||||
};
|
1
makefile
1
makefile
|
@ -22,6 +22,7 @@ test:nasal
|
|||
./nasal -t test/loop.nas
|
||||
./nasal -c test/mandel.nas
|
||||
./nasal -t test/mandelbrot.nas
|
||||
# ./nasal -t test/module_test.nas
|
||||
./nasal test/nasal_test.nas
|
||||
./nasal -t test/pi.nas
|
||||
./nasal -t test/prime.nas
|
||||
|
|
|
@ -11,3 +11,17 @@ extern "C" nasal_ref fib(std::vector<nasal_ref>& args,nasal_gc& gc){
|
|||
return builtin_err("extern_fib","\"num\" must be number");
|
||||
return {vm_num,fibonaci(num.to_number())};
|
||||
}
|
||||
extern "C" nasal_ref quick_fib(std::vector<nasal_ref>& args,nasal_gc& gc){
|
||||
nasal_ref num=args[0];
|
||||
if(num.type!=vm_num)
|
||||
return builtin_err("extern_fib","\"num\" must be number");
|
||||
if(num.num()<2)
|
||||
return num;
|
||||
double a=1,b=1,res=0;
|
||||
for(double i=1;i<num.num();i+=1){
|
||||
res=a+b;
|
||||
a=b;
|
||||
b=res;
|
||||
}
|
||||
return {vm_num,res};
|
||||
}
|
|
@ -83,6 +83,7 @@ nas_native(builtin_dlopen);
|
|||
nas_native(builtin_dlsym);
|
||||
nas_native(builtin_dlclose);
|
||||
nas_native(builtin_dlcall);
|
||||
nas_native(builtin_platform);
|
||||
|
||||
nasal_ref builtin_err(const char* func_name,std::string info)
|
||||
{
|
||||
|
@ -164,6 +165,7 @@ struct
|
|||
{"__builtin_dlsym", builtin_dlsym },
|
||||
{"__builtin_dlclose", builtin_dlclose },
|
||||
{"__builtin_dlcall", builtin_dlcall },
|
||||
{"__builtin_platform",builtin_platform},
|
||||
{nullptr, nullptr }
|
||||
};
|
||||
|
||||
|
@ -796,7 +798,7 @@ nasal_ref builtin_readln(std::vector<nasal_ref>& local,nasal_gc& gc)
|
|||
nasal_ref builtin_stat(std::vector<nasal_ref>& local,nasal_gc& gc)
|
||||
{
|
||||
nasal_ref filename=local[1];
|
||||
if(filename!=vm_str)
|
||||
if(filename.type!=vm_str)
|
||||
return builtin_err("stat","\"filename\" must be string");
|
||||
struct stat buf;
|
||||
if(stat(filename.str()->c_str(),&buf)<0)
|
||||
|
@ -1013,11 +1015,10 @@ nasal_ref builtin_dlsym(std::vector<nasal_ref>& local,nasal_gc& gc)
|
|||
return builtin_err("dlsym","\"lib\" is not a correct dynamic lib entry");
|
||||
if(sym.type!=vm_str)
|
||||
return builtin_err("dlsym","\"sym\" must be string");
|
||||
void* func=nullptr;
|
||||
#ifdef _WIN32
|
||||
func=(void*)GetProcAddress((HMODULE)libptr.obj()->ptr,sym.str()->c_str());
|
||||
void* func=(void*)GetProcAddress((HMODULE)libptr.obj()->ptr,sym.str()->c_str());
|
||||
#else
|
||||
func=dlsym(libptr.obj()->ptr,sym.str()->c_str());
|
||||
void* func=dlsym(libptr.obj()->ptr,sym.str()->c_str());
|
||||
#endif
|
||||
if(!func)
|
||||
return builtin_err("dlsym","cannot find symbol \""+*sym.str()+"\"");
|
||||
|
@ -1048,4 +1049,17 @@ nasal_ref builtin_dlcall(std::vector<nasal_ref>& local,nasal_gc& gc)
|
|||
extern_func func=(extern_func)funcptr.obj()->ptr;
|
||||
return func(args.vec()->elems,gc);
|
||||
}
|
||||
nasal_ref builtin_platform(std::vector<nasal_ref>& local,nasal_gc& gc)
|
||||
{
|
||||
nasal_ref ret=gc.alloc(vm_str);
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
*ret.str()="windows";
|
||||
#elif defined __linux__
|
||||
*ret.str()="linux";
|
||||
#elif defined __APPLE__
|
||||
*ret.str()="macOS";
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
|
@ -125,7 +125,7 @@ struct nasal_val
|
|||
{
|
||||
uint8_t mark;
|
||||
uint8_t type;
|
||||
uint8_t unmut;
|
||||
uint8_t unmut; // used toe mark if a string is mutable
|
||||
union
|
||||
{
|
||||
std::string* str;
|
||||
|
@ -407,7 +407,7 @@ void nasal_gc::init(const std::vector<std::string>& s)
|
|||
|
||||
zero={vm_num,(double)0}; // init constant 0
|
||||
one ={vm_num,(double)1}; // init constant 1
|
||||
nil ={vm_nil,(double)0}; // init constant nil
|
||||
nil ={vm_nil,nullptr}; // init constant nil
|
||||
|
||||
// init constant strings
|
||||
strs.resize(s.size());
|
||||
|
|
|
@ -139,7 +139,7 @@ void nasal_vm::valinfo(nasal_ref& val)
|
|||
case vm_nil: printf("\t| nil |\n");break;
|
||||
case vm_num: printf("\t| num | %lf\n",val.num());break;
|
||||
case vm_str: printf("\t| str | <0x%lx> %s\n",(uint64_t)p,rawstr(*val.str()).c_str());break;
|
||||
case vm_func: printf("\t| func | <0x%lx> func{entry=0x%x}\n",(uint64_t)p,val.func()->entry);break;
|
||||
case vm_func: printf("\t| func | <0x%lx> entry=0x%x\n",(uint64_t)p,val.func()->entry);break;
|
||||
case vm_vec: printf("\t| vec | <0x%lx> [%lu val]\n",(uint64_t)p,val.vec()->elems.size());break;
|
||||
case vm_hash: printf("\t| hash | <0x%lx> {%lu member}\n",(uint64_t)p,val.hash()->elems.size());break;
|
||||
case vm_obj: printf("\t| obj | <0x%lx>\n",(uint64_t)p);break;
|
||||
|
@ -174,10 +174,8 @@ void nasal_vm::traceback()
|
|||
continue;
|
||||
}
|
||||
if(same)
|
||||
{
|
||||
printf("\t0x%.8x: %d same call(s)\n",last,same);
|
||||
same=0;
|
||||
}
|
||||
bytecodeinfo(point);
|
||||
}
|
||||
if(same)
|
||||
|
|
17
stl/lib.nas
17
stl/lib.nas
|
@ -48,6 +48,23 @@ var io=
|
|||
eof: func(filehandle){return __builtin_eof(filehandle);}
|
||||
};
|
||||
|
||||
var fstat=func(filename){
|
||||
var s=io.stat(filename);
|
||||
return {
|
||||
st_dev: s[0],
|
||||
st_ino: s[1],
|
||||
st_mode: s[2],
|
||||
st_nlink:s[3],
|
||||
st_uid: s[4],
|
||||
st_gid: s[5],
|
||||
st_rdev: s[6],
|
||||
st_size: s[7],
|
||||
st_atime:s[8],
|
||||
st_mtime:s[9],
|
||||
st_ctime:s[10]
|
||||
};
|
||||
}
|
||||
|
||||
var bits=
|
||||
{
|
||||
bitxor: func(a,b){return __builtin_xor(a,b); },
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
import("lib.nas");
|
||||
|
||||
var libfib=func(){
|
||||
var dl=dylib.dlopen("./module/libfib.so");
|
||||
var fib=dylib.dlsym(dl,"fib");
|
||||
var qfib=dylib.dlsym(dl,"quick_fib");
|
||||
var call=dylib.dlcall;
|
||||
return
|
||||
{
|
||||
fib: func(x){return call(fib,x)},
|
||||
qfib:func(x){return call(qfib,x)}
|
||||
};
|
||||
}();
|
||||
|
||||
println(libfib);
|
||||
println(libfib.fib(29));
|
||||
println(libfib.qfib(29));
|
Loading…
Reference in New Issue