[oden-js] Get module exports

This commit is contained in:
John Doty 2023-06-19 13:24:35 -07:00
parent 0c8367d29f
commit 16e6f1304c
4 changed files with 56 additions and 2 deletions

View file

@ -1,6 +1,6 @@
use crate::{AtomRef, ContextRef, Error, Result, Runtime, RustFunction};
use oden_js_sys as sys;
use std::ffi::CStr;
use std::ffi::{CStr, CString};
use std::fmt;
use std::ops::{Deref, DerefMut};
@ -309,6 +309,25 @@ impl ValueRef {
Ok(result)
}
pub fn get_module_export(&self, ctx: &ContextRef, export: &str) -> Result<Value> {
if self.value_type() != ValueType::Module {
return Err(Error::InvalidType {
expected: ValueType::Bool,
found: self.value_type(),
});
}
let c_value = match CString::new(export) {
Ok(cs) => Ok(cs),
Err(_) => Err(Error::UnexpectedNul),
}?;
unsafe {
let module = sys::JS_ValueGetPtr(self.val) as *mut sys::JSModuleDef;
ctx.check_exception(sys::JS_GetModuleExport(ctx.ctx, module, c_value.into_raw()))
}
}
}
impl<'ctx> fmt::Debug for ValueRef {