add module.nas to safely use dylib
This commit is contained in:
parent
a0b341deb5
commit
ca9b8581b4
1
lib.nas
1
lib.nas
|
@ -284,6 +284,7 @@ var unix=
|
||||||
};
|
};
|
||||||
|
|
||||||
# dylib is the core hashmap for developers to load their own library.
|
# dylib is the core hashmap for developers to load their own library.
|
||||||
|
# for safe using dynamic library, you could use 'module' in stl/module.nas
|
||||||
var dylib=
|
var dylib=
|
||||||
{
|
{
|
||||||
# open dynamic lib.
|
# open dynamic lib.
|
||||||
|
|
|
@ -8,6 +8,8 @@ double fibonaci(double x){
|
||||||
}
|
}
|
||||||
extern "C" nasal_ref fib(std::vector<nasal_ref>& args,nasal_gc& gc){
|
extern "C" nasal_ref fib(std::vector<nasal_ref>& args,nasal_gc& gc){
|
||||||
std::cout<<"[mod] this is the first test module of nasal\n";
|
std::cout<<"[mod] this is the first test module of nasal\n";
|
||||||
|
if(!args.size())
|
||||||
|
return builtin_err("fib","lack arguments");
|
||||||
nasal_ref num=args[0];
|
nasal_ref num=args[0];
|
||||||
if(num.type!=vm_num)
|
if(num.type!=vm_num)
|
||||||
return builtin_err("extern_fib","\"num\" must be number");
|
return builtin_err("extern_fib","\"num\" must be number");
|
||||||
|
@ -15,6 +17,8 @@ extern "C" nasal_ref fib(std::vector<nasal_ref>& args,nasal_gc& gc){
|
||||||
}
|
}
|
||||||
extern "C" nasal_ref quick_fib(std::vector<nasal_ref>& args,nasal_gc& gc){
|
extern "C" nasal_ref quick_fib(std::vector<nasal_ref>& args,nasal_gc& gc){
|
||||||
std::cout<<"[mod] this is the first test module of nasal\n";
|
std::cout<<"[mod] this is the first test module of nasal\n";
|
||||||
|
if(!args.size())
|
||||||
|
return builtin_err("fib","lack arguments");
|
||||||
nasal_ref num=args[0];
|
nasal_ref num=args[0];
|
||||||
if(num.type!=vm_num)
|
if(num.type!=vm_num)
|
||||||
return builtin_err("extern_quick_fib","\"num\" must be number");
|
return builtin_err("extern_quick_fib","\"num\" must be number");
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <crt_externs.h>
|
#include <crt_externs.h>
|
||||||
#define environ (*_NSGetEnviron())
|
#define environ (*_NSGetEnviron())
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
builtin functions must be called inside a function like this:
|
builtin functions must be called inside a function like this:
|
||||||
var print=func(elems...){
|
var print=func(elems...){
|
||||||
|
|
12
nasal_gc.h
12
nasal_gc.h
|
@ -137,8 +137,16 @@ struct nasal_obj
|
||||||
{
|
{
|
||||||
uint32_t type;
|
uint32_t type;
|
||||||
void* ptr;
|
void* ptr;
|
||||||
nasal_obj():ptr(nullptr){}
|
void* destructor;
|
||||||
void clear(){ptr=nullptr;}
|
nasal_obj():ptr(nullptr),destructor(nullptr){}
|
||||||
|
~nasal_obj(){clear();}
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
typedef void (*func)(void*);
|
||||||
|
if(destructor)
|
||||||
|
(func(destructor))(ptr);
|
||||||
|
ptr=nullptr;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const uint8_t GC_UNCOLLECTED=0;
|
const uint8_t GC_UNCOLLECTED=0;
|
||||||
|
|
|
@ -284,6 +284,7 @@ var unix=
|
||||||
};
|
};
|
||||||
|
|
||||||
# dylib is the core hashmap for developers to load their own library.
|
# dylib is the core hashmap for developers to load their own library.
|
||||||
|
# for safe using dynamic library, you could use 'module' in stl/module.nas
|
||||||
var dylib=
|
var dylib=
|
||||||
{
|
{
|
||||||
# open dynamic lib.
|
# open dynamic lib.
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
# lib module.nas
|
||||||
|
# ValKmjolnir 2022/3/5
|
||||||
|
|
||||||
|
# this provides safe usage of dylib
|
||||||
|
# when dylib is closed,
|
||||||
|
# all the invalid functions cannot be called
|
||||||
|
import("lib.nas");
|
||||||
|
|
||||||
|
var module_call_func=func(fptr,args){
|
||||||
|
return __builtin_dlcall;
|
||||||
|
}
|
||||||
|
var extern={
|
||||||
|
new: func(fptr){
|
||||||
|
return {
|
||||||
|
fptr:fptr,
|
||||||
|
isopen:1,
|
||||||
|
parents:[extern]
|
||||||
|
};
|
||||||
|
},
|
||||||
|
call: func(args...){
|
||||||
|
return (!me.isopen)?nil:module_call_func(me.fptr,args);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var module={
|
||||||
|
new: func(name){
|
||||||
|
return {
|
||||||
|
name:name,
|
||||||
|
lib:dylib.dlopen(name),
|
||||||
|
f:{},
|
||||||
|
parents:[module]
|
||||||
|
};
|
||||||
|
},
|
||||||
|
get: func(symbol){
|
||||||
|
if(contains(me.f,symbol))
|
||||||
|
return me.f[symbol];
|
||||||
|
var f=extern.new(dylib.dlsym(me.lib,symbol));
|
||||||
|
me.f[symbol]=f;
|
||||||
|
return f;
|
||||||
|
},
|
||||||
|
close: func(){
|
||||||
|
foreach(var i;keys(me.f))
|
||||||
|
me.f[i].isopen=0;
|
||||||
|
dylib.dlclose(me.lib);
|
||||||
|
}
|
||||||
|
};
|
Loading…
Reference in New Issue