Skip to content

Commit

Permalink
Throw TypeError when fata decoding is specified.
Browse files Browse the repository at this point in the history
  • Loading branch information
saulecabrera committed Apr 11, 2024
1 parent a126ede commit a918d3b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
5 changes: 3 additions & 2 deletions crates/apis/src/text_encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str;
use anyhow::{anyhow, bail, Error, Result};
use javy::{
hold, hold_and_release,
quickjs::{context::EvalOptions, Function, String as JSString, TypedArray, Value},
quickjs::{context::EvalOptions, Exception, Function, String as JSString, TypedArray, Value},
to_js_error, Args, Runtime,
};

Expand Down Expand Up @@ -86,7 +86,8 @@ fn decode<'js>(args: Args<'js>) -> Result<Value<'js>> {
let js_string = if fatal {
JSString::from_str(
cx.clone(),
str::from_utf8(view).map_err(|_| anyhow!("The encoded data was not valid utf-8"))?,
str::from_utf8(view)
.map_err(|_| Exception::throw_type(&cx, "The encoded data was not valid utf-8"))?,
)
} else {
let str = String::from_utf8_lossy(view);
Expand Down
3 changes: 2 additions & 1 deletion crates/core/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use javy::{quickjs::Module, Runtime};
use javy::{from_js_error, quickjs::Module, Runtime};
use once_cell::sync::OnceCell;
use std::io::{self, Read};
use std::slice;
Expand Down Expand Up @@ -29,6 +29,7 @@ pub extern "C" fn init() {
unsafe { Module::unsafe_declare(this.clone(), FUNCTION_MODULE_NAME, contents) }?
.write_object_le()
})
.map_err(|e| runtime.context().with(|cx| from_js_error(cx.clone(), e)))
.unwrap();

unsafe {
Expand Down
8 changes: 4 additions & 4 deletions crates/javy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub mod json;
/// Print the given JS value.
///
/// The implementation matches the default JavaScript display format for each value.
pub fn print<'js>(val: &Value<'js>, sink: &mut String) -> Result<()> {
pub fn print(val: &Value, sink: &mut String) -> Result<()> {
match val.type_of() {
Type::Undefined => write!(sink, "undefined").map_err(Into::into),
Type::Null => write!(sink, "null").map_err(Into::into),
Expand Down Expand Up @@ -89,7 +89,7 @@ pub fn print<'js>(val: &Value<'js>, sink: &mut String) -> Result<()> {
}
Type::Object => write!(sink, "[object Object]").map_err(Into::into),
// TODO: Implement the rest.
_ => unimplemented!(),
x => unimplemented!("{x}"),
}
}

Expand All @@ -113,15 +113,15 @@ impl<'js> Args<'js> {
}
}

/// Alias for `Args::hold(cx, args).release()
/// Alias for `Args::hold(cx, args).release()`
#[macro_export]
macro_rules! hold_and_release {
($cx:expr, $args:expr) => {
Args::hold($cx, $args).release()
};
}

/// Alias for `Args::hold(cx, args)
/// Alias for [Args::hold]
#[macro_export]
macro_rules! hold {
($cx:expr, $args:expr) => {
Expand Down

0 comments on commit a918d3b

Please sign in to comment.