diff --git a/README.md b/README.md index 8af9ccb..edffdb5 100644 --- a/README.md +++ b/README.md @@ -674,10 +674,21 @@ dylib.dlclose(dlhandle); `dylib.dlsym` is used to get the function address. -`dylib.dlcall` is used to call the function, the first argument is the function address, make sure this argument is vm_obj and type=obj_extern. +`dylib.dlcall` is used to call the function, the first argument is the function address, make sure this argument is `vm_obj` and `type=obj_extern`. `dylib.dlclose` is used to unload the library, at the moment that you call the function, all the function addresses that got from it are invalid. +`dylib.limitcall` is used to get `dlcall` function that has limited parameter size, this function will prove the performance of your code because it does not use `vm_vec` to store the arguments, instead it uses local scope to store them, so this could avoid frequently garbage collecting. And the code above could also be written like this: + +```javascript +var dlhandle=dylib.dlopen("libfib."~(os.platform()=="windows"?"dll":"so")); +var fib=dylib.dlsym(dlhandle,"fib"); +var invoke=dylib.limitcall(1); # this means the called function has only one parameter +for(var i=1;i<30;i+=1) + println(invoke(fib,i)); +dylib.dlclose(dlhandle); +``` + If get this, Congratulations! ```bash diff --git a/doc/README_zh.md b/doc/README_zh.md index a4c154a..4a4624c 100644 --- a/doc/README_zh.md +++ b/doc/README_zh.md @@ -647,10 +647,21 @@ dylib.dlclose(dlhandle); `dylib.dlsym`通过符号从动态库中获得函数地址。 -`dylib.dlcall`用于调用函数,第一个参数是动态库函数的地址,这是个特殊类型,一定要保证这个参数是vm_obj类型并且type=obj_extern。 +`dylib.dlcall`用于调用函数,第一个参数是动态库函数的地址,这是个特殊类型,一定要保证这个参数是`vm_obj`类型并且`type=obj_extern`。 `dylib.dlclose`用于卸载动态库,当然,在这个函数调用之后,所有从该库中获取的函数都作废。 +`dylib.limitcall`用于获取使用固定长度传参的 `dlcall` 函数,这种函数可以提高你的程序运行效率,因为它不需要用 `vm_vec` 来存储传入参数,而是使用局部作用域来直接存储,从而避免了频繁调用可能导致的频繁垃圾收集。所以上面展示的代码同样可以这样写: + +```javascript +var dlhandle=dylib.dlopen("libfib."~(os.platform()=="windows"?"dll":"so")); +var fib=dylib.dlsym(dlhandle,"fib"); +var invoke=dylib.limitcall(1); # 这说明我们要调用的函数只有一个参数 +for(var i=1;i<30;i+=1) + println(invoke(fib,i)); +dylib.dlclose(dlhandle); +``` + 如果接下来你看到了这个运行结果,恭喜你! ```bash diff --git a/lib.nas b/lib.nas index 0a9db19..e8e0785 100644 --- a/lib.nas +++ b/lib.nas @@ -280,8 +280,7 @@ var md5=func(str){ return __md5(str); } -var io= -{ +var io={ SEEK_SET:0, SEEK_CUR:1, SEEK_END:2, @@ -333,8 +332,7 @@ var fstat=func(filename){ # functions that do bitwise calculation. # carefully use it, all the calculations are based on integer. -var bits= -{ +var bits={ # i32 xor i32_xor: func(a,b){return __i32xor(a,b); }, # i32 and @@ -378,8 +376,7 @@ var bits= }; # mostly used math functions and special constants, you know. -var math= -{ +var math={ e: 2.7182818284590452354, pi: 3.14159265358979323846264338327950288, D2R: 2.7182818284590452354/180, @@ -402,8 +399,7 @@ var math= min: func(x,y){return x=1;--i)// load arguments local[i]=local[i-1]; local[0]=func.local[0];// load "me" diff --git a/stl/lib.nas b/stl/lib.nas index 0a9db19..e8e0785 100644 --- a/stl/lib.nas +++ b/stl/lib.nas @@ -280,8 +280,7 @@ var md5=func(str){ return __md5(str); } -var io= -{ +var io={ SEEK_SET:0, SEEK_CUR:1, SEEK_END:2, @@ -333,8 +332,7 @@ var fstat=func(filename){ # functions that do bitwise calculation. # carefully use it, all the calculations are based on integer. -var bits= -{ +var bits={ # i32 xor i32_xor: func(a,b){return __i32xor(a,b); }, # i32 and @@ -378,8 +376,7 @@ var bits= }; # mostly used math functions and special constants, you know. -var math= -{ +var math={ e: 2.7182818284590452354, pi: 3.14159265358979323846264338327950288, D2R: 2.7182818284590452354/180, @@ -402,8 +399,7 @@ var math= min: func(x,y){return x