[oden] Native Modules

This commit is contained in:
John Doty 2023-06-21 06:19:14 -07:00
parent 3b02faf9b4
commit c574fd8cb8
8 changed files with 453 additions and 79 deletions

View file

@ -125,7 +125,7 @@ impl ValueRef {
let mut res: i32 = 0;
let ret = sys::JS_ToInt32(ctx.ctx, &mut res, self.val);
if ret < 0 {
Err(Error::Exception(ctx.exception()))
Err(ctx.exception_error())
} else {
Ok(res)
}
@ -137,7 +137,9 @@ impl ValueRef {
let mut res: u32 = 0;
let ret = sys::JS_ToUint32(ctx.ctx, &mut res, self.val);
if ret < 0 {
Err(Error::Exception(ctx.exception()))
let exc = ctx.exception();
let desc = exc.to_string(&ctx).unwrap_or_else(|_| String::new());
Err(Error::Exception(exc, desc))
} else {
Ok(res)
}
@ -149,7 +151,7 @@ impl ValueRef {
let mut res: i64 = 0;
let ret = sys::JS_ToInt64(ctx.ctx, &mut res, self.val);
if ret < 0 {
Err(Error::Exception(ctx.exception()))
Err(ctx.exception_error())
} else {
Ok(res)
}
@ -189,7 +191,7 @@ impl ValueRef {
let mut res: f64 = 0.0;
let ret = sys::JS_ToFloat64(ctx.ctx, &mut res, self.val);
if ret < 0 {
Err(Error::Exception(ctx.exception()))
Err(ctx.exception_error())
} else {
Ok(res)
}
@ -257,7 +259,7 @@ impl ValueRef {
sys::JS_DupValue(ctx.ctx, val.val);
let result = sys::JS_SetProperty(ctx.ctx, self.val, prop.atom, val.val);
if result == -1 {
Err(Error::Exception(ctx.exception()))
Err(ctx.exception_error())
} else {
Ok(())
}
@ -290,7 +292,7 @@ impl ValueRef {
let cstr = unsafe {
let ptr = sys::JS_ToCStringLen2(ctx.ctx, std::ptr::null_mut(), self.val, 0);
if ptr.is_null() {
return Err(Error::Exception(ctx.exception()));
return Err(ctx.exception_error());
}
CStr::from_ptr(ptr)
};
@ -318,14 +320,10 @@ impl ValueRef {
});
}
let c_value = match CString::new(export) {
Ok(cs) => Ok(cs),
Err(_) => Err(Error::UnexpectedNul),
}?;
let c_value = CString::new(export)?;
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()))
let module = sys::JS_VALUE_GET_PTR(self.val) as *mut sys::JSModuleDef;
ctx.check_exception(sys::JS_GetModuleExport(ctx.ctx, module, c_value.as_ptr()))
}
}
@ -342,7 +340,7 @@ impl ValueRef {
}
}
impl<'ctx> fmt::Debug for ValueRef {
impl fmt::Debug for ValueRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Value")
.field("v", &self.val)