[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

@ -1,6 +1,6 @@
use crate::{Class, ClassID, ContextRef, Error, ValueRef, ValueResult};
use crate::{throw_string, Class, ClassID, ContextRef, Error, ValueRef, ValueResult};
use oden_js_sys as sys;
use std::ffi::{c_int, CString};
use std::ffi::c_int;
use std::panic::catch_unwind;
pub trait Callback: Fn(&ContextRef, &ValueRef, &[&ValueRef]) -> ValueResult {}
@ -25,40 +25,6 @@ impl<T: Callback> Class for CallbackObject<T> {
}
}
fn throw_string(context: &ContextRef, message: String) -> sys::JSValue {
let ctx = context.ctx;
match context.new_string(&message) {
Ok(e) => unsafe {
// Because context.new_string yields an owned Value, and will
// clean it up on the way out, we need to explicitly DupValue a
// reference for the `Throw` to own.
let err = sys::JS_NewError(ctx);
if sys::JS_ValueGetTag(err) == sys::JS_TAG_EXCEPTION {
// GIVE UP; this is out of memory anyway things probably went
// wrong because of that.
return err;
}
sys::JS_DupValue(ctx, e.val); // SetProperty takes ownership.
let prop = CString::new("message").unwrap();
if sys::JS_SetPropertyStr(ctx, err, prop.as_ptr(), e.val) == -1 {
// Also an out of memory but we need to free the error object
// on our way out.
sys::JS_FreeValue(ctx, err);
return sys::JS_MakeException(); // JS_EXCEPTION
}
sys::JS_Throw(ctx, err)
},
Err(_) => unsafe {
sys::JS_Throw(
ctx,
sys::JS_NewString(ctx, "Errors within errors: embedded nulls in the description of the error that occurred".as_bytes().as_ptr() as *const i8),
)
},
}
}
fn callback_impl<F>(
ctx: *mut sys::JSContext,
_this: sys::JSValue,
@ -103,7 +69,7 @@ where
*ret
}
}
Err(Error::Exception(e)) => unsafe {
Err(Error::Exception(e, _)) => unsafe {
// If we returned `Error::Exception` then we're propagating an
// exception through the JS stack, just flip it.
let exc = &e.val;