use crate::derive::{Object, ObjectRef};
use crate::error::Result;
use crate::function::Function;
use crate::object::{Object, ObjectArc};
use crate::string::String;
use tvm_ffi_sys::TVMFFITypeIndex as TypeIndex;
#[repr(C)]
#[derive(Object)]
#[object(type_key = "ffi.Module", unsafe type_index = TypeIndex::kTVMFFIModule)]
pub struct ModuleObj {
object: Object,
}
#[repr(C)]
#[derive(ObjectRef, Clone)]
#[object_ref(skip_deref)]
pub struct Module {
data: ObjectArc<ModuleObj>,
}
impl Module {
pub fn load_from_file<Str: AsRef<str>>(file_name: Str) -> Result<Module> {
let file_name = crate::string::String::from(file_name.as_ref());
crate::cached_global_func!("ffi.ModuleLoadFromFile")
.call_tuple((file_name,))?
.try_into()
}
pub fn get_function<Str: AsRef<str>>(&self, name: Str) -> Result<Function> {
self.lookup_function(name.as_ref(), false)
}
pub fn get_function_with_imports<Str: AsRef<str>>(&self, name: Str) -> Result<Function> {
self.lookup_function(name.as_ref(), true)
}
fn lookup_function(&self, name: &str, query_imports: bool) -> Result<Function> {
let function = crate::cached_global_func!("ffi.ModuleGetFunction").call_tuple((
self,
name,
query_imports,
))?;
if function.is_none() {
crate::bail!(
crate::error::VALUE_ERROR,
"Module has no function '{}'",
name
);
}
function.try_into()
}
pub fn implements_function<Str: AsRef<str>>(&self, name: Str) -> Result<bool> {
let name = String::from(name.as_ref());
crate::cached_global_func!("ffi.ModuleImplementsFunction")
.call_tuple((self, name, false))?
.try_into()
}
pub fn implements_function_with_imports<Str: AsRef<str>>(&self, name: Str) -> Result<bool> {
let name = String::from(name.as_ref());
crate::cached_global_func!("ffi.ModuleImplementsFunction")
.call_tuple((self, name, true))?
.try_into()
}
pub fn get_function_metadata<Str: AsRef<str>>(&self, name: Str) -> Result<Option<String>> {
let name = String::from(name.as_ref());
crate::cached_global_func!("ffi.ModuleGetFunctionMetadata")
.call_tuple((self, name, false))?
.try_into()
}
pub fn get_function_metadata_with_imports<Str: AsRef<str>>(
&self,
name: Str,
) -> Result<Option<String>> {
let name = String::from(name.as_ref());
crate::cached_global_func!("ffi.ModuleGetFunctionMetadata")
.call_tuple((self, name, true))?
.try_into()
}
pub fn get_function_doc<Str: AsRef<str>>(&self, name: Str) -> Result<Option<String>> {
let name = String::from(name.as_ref());
crate::cached_global_func!("ffi.ModuleGetFunctionDoc")
.call_tuple((self, name, false))?
.try_into()
}
pub fn get_function_doc_with_imports<Str: AsRef<str>>(
&self,
name: Str,
) -> Result<Option<String>> {
let name = String::from(name.as_ref());
crate::cached_global_func!("ffi.ModuleGetFunctionDoc")
.call_tuple((self, name, true))?
.try_into()
}
}