[oden] Garbage assets, need to rewrite to IO

This commit is contained in:
John Doty 2023-06-27 17:16:37 -07:00
parent 75fcc427ac
commit 17805fa4a6
17 changed files with 240 additions and 94 deletions

View file

@ -69,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;

View file

@ -308,7 +308,12 @@ impl ContextRef {
pub(crate) fn exception_error(&self) -> Error {
let exc = self.exception();
let desc = exc.to_string(&self).unwrap_or_else(|_| String::new());
Error::Exception(exc, desc)
let stack = exc
.get_property(&self, "stack")
.and_then(|stack| stack.to_string(&self))
.unwrap_or_else(|_| String::new());
Error::Exception(exc, desc, stack)
}
}

View file

@ -1,4 +1,4 @@
use crate::{ContextRef, Error, Result, Value, ValueRef};
use crate::{ContextRef, Error, Result, Value, ValueRef, ValueType};
use std::num::TryFromIntError;
pub trait TryFromValue: Sized {
@ -105,10 +105,24 @@ impl TryFromValue for Value {
}
}
impl TryFromValue for () {
#[inline]
fn try_from_value(value: &ValueRef, _ctx: &ContextRef) -> Result<Self> {
if value.is_undefined() {
Ok(())
} else {
Err(Error::InvalidType {
expected: ValueType::Undefined,
found: value.value_type(),
})
}
}
}
impl<T: TryFromValue> TryFromValue for Option<T> {
#[inline]
fn try_from_value(value: &ValueRef, ctx: &ContextRef) -> Result<Self> {
if value.is_undefined() {
if value.is_null() || value.is_undefined() {
Ok(None)
} else {
Ok(Some(T::try_from_value(value, ctx)?))

View file

@ -7,21 +7,21 @@ pub trait TryIntoValue {
impl TryIntoValue for u8 {
#[inline]
fn try_into_value(self, ctx: &ContextRef) -> ValueResult {
ctx.new_u64(self)
ctx.new_u32(self)
}
}
impl TryIntoValue for u16 {
#[inline]
fn try_into_value(self, ctx: &ContextRef) -> ValueResult {
ctx.new_u64(self)
ctx.new_u32(self)
}
}
impl TryIntoValue for u32 {
#[inline]
fn try_into_value(self, ctx: &ContextRef) -> ValueResult {
ctx.new_u64(self)
ctx.new_u32(self)
}
}
@ -116,7 +116,7 @@ impl TryIntoValue for Error {
}
Error::ConversionError(e) => Err(Error::ConversionError(e)),
Error::RustFunctionError(e) => Err(Error::RustFunctionError(e)),
Error::Exception(v, d) => Err(Error::Exception(v.dup(ctx), d)),
Error::Exception(v, d, s) => Err(Error::Exception(v.dup(ctx), d, s)),
Error::OutOfMemory => Err(Error::OutOfMemory),
Error::IOError(e) => Err(Error::IOError(e)),
Error::ParseError(name, err) => Err(Error::ParseError(name, err)),
@ -135,8 +135,15 @@ impl<T: TryIntoValue> TryIntoValue for Option<T> {
#[inline]
fn try_into_value(self, ctx: &ContextRef) -> ValueResult {
match self {
None => Ok(ctx.undefined()),
None => Ok(ctx.null()),
Some(v) => v.try_into_value(ctx),
}
}
}
impl TryIntoValue for () {
#[inline]
fn try_into_value(self, ctx: &ContextRef) -> ValueResult {
Ok(ctx.undefined())
}
}

View file

@ -18,7 +18,7 @@ pub use conversion::*;
pub use runtime::Runtime;
pub use value::{Value, ValueRef, ValueType};
#[derive(Error, Debug)]
#[derive(Debug, Error)]
pub enum Error {
#[error("too many classes have been registered")]
TooManyClasses,
@ -39,8 +39,8 @@ pub enum Error {
ConversionError(String),
#[error("an error occurred calling a rust function: {0}")]
RustFunctionError(String),
#[error("an exception was thrown during evaluation: {1}")]
Exception(Value, String),
#[error("an exception was thrown during evaluation: {1}\nStack: {2}")]
Exception(Value, String, String),
#[error("out of memory")]
OutOfMemory,
#[error("an io error occurred: {0}")]
@ -66,7 +66,7 @@ pub type ValueResult = core::result::Result<Value, Error>;
pub(crate) fn throw_error(context: &ContextRef, error: Error) -> sys::JSValue {
match error {
Error::Exception(v, _) => unsafe {
Error::Exception(v, _, _) => unsafe {
sys::JS_DupValue(context.ctx, v.val);
sys::JS_Throw(context.ctx, v.val)
},

View file

@ -164,7 +164,7 @@ unsafe extern "C" fn init_func<T: NativeModule>(
let context = ContextRef::from_raw(ctx);
match NativeModuleState::<T>::define(&context, m) {
Ok(_) => 0,
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;

View file

@ -137,9 +137,7 @@ impl ValueRef {
let mut res: u32 = 0;
let ret = sys::JS_ToUint32(ctx.ctx, &mut res, self.val);
if ret < 0 {
let exc = ctx.exception();
let desc = exc.to_string(&ctx).unwrap_or_else(|_| String::new());
Err(Error::Exception(exc, desc))
Err(ctx.exception_error())
} else {
Ok(res)
}