[oden] Native Modules
This commit is contained in:
parent
3b02faf9b4
commit
c574fd8cb8
8 changed files with 453 additions and 79 deletions
|
|
@ -1,3 +1,5 @@
|
|||
use oden_js_sys as sys;
|
||||
use std::ffi::{CString, NulError};
|
||||
use thiserror::Error;
|
||||
|
||||
mod atom;
|
||||
|
|
@ -5,6 +7,7 @@ mod callback;
|
|||
mod class;
|
||||
mod context;
|
||||
mod conversion;
|
||||
pub mod module;
|
||||
mod runtime;
|
||||
mod value;
|
||||
|
||||
|
|
@ -36,8 +39,51 @@ pub enum Error {
|
|||
ConversionError(String),
|
||||
#[error("an error occurred calling a rust function: {0}")]
|
||||
RustFunctionError(String),
|
||||
#[error("an exception was thrown during evaluation")]
|
||||
Exception(Value),
|
||||
#[error("an exception was thrown during evaluation: {1}")]
|
||||
Exception(Value, String),
|
||||
#[error("out of memory")]
|
||||
OutOfMemory,
|
||||
}
|
||||
|
||||
impl From<NulError> for Error {
|
||||
fn from(_: NulError) -> Self {
|
||||
Error::UnexpectedNul
|
||||
}
|
||||
}
|
||||
|
||||
pub type Result<T> = core::result::Result<T, Error>;
|
||||
pub type ValueResult = core::result::Result<Value, Error>;
|
||||
|
||||
pub 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),
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue