Skip to content

Commit 93014c5

Browse files
committed
fixup! JS TextEncoder: encodeInto method
1 parent 93a9794 commit 93014c5

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed

crates/extensions/tedge_flows/src/js_lib/text_encoder.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rquickjs::JsLifetime;
66
use rquickjs::Object;
77
use rquickjs::Result;
88
use rquickjs::TypedArray;
9-
use rquickjs::Value;
109

1110
#[derive(Clone, Trace, JsLifetime)]
1211
#[rquickjs::class(frozen)]
@@ -29,19 +28,23 @@ impl<'js> TextEncoder {
2928
"utf-8"
3029
}
3130

32-
pub fn encode(&self, ctx: Ctx<'js>, text: Value<'js>) -> Result<TypedArray<'js, u8>> {
33-
let string = Self::string_from(text)?;
31+
pub fn encode(
32+
&self,
33+
ctx: Ctx<'js>,
34+
text: rquickjs::String<'js>,
35+
) -> Result<TypedArray<'js, u8>> {
36+
let string = text.to_string()?;
3437
TypedArray::new(ctx.clone(), string.as_bytes())
3538
}
3639

3740
#[qjs(rename = "encodeInto")]
3841
pub fn encode_into(
3942
&self,
4043
ctx: Ctx<'js>,
41-
text: Value<'js>,
44+
text: rquickjs::String<'js>,
4245
array: TypedArray<'js, u8>,
4346
) -> Result<Object<'js>> {
44-
let string = Self::string_from(text)?;
47+
let string = text.to_string()?;
4548
let offset: usize = array.get("byteOffset").unwrap_or_default();
4649
let buffer = array
4750
.arraybuffer()?
@@ -73,19 +76,3 @@ impl<'js> TextEncoder {
7376
Ok(obj)
7477
}
7578
}
76-
77-
impl TextEncoder {
78-
pub fn string_from(text: Value<'_>) -> Result<String> {
79-
let string = match text.as_string() {
80-
None => text
81-
.as_object()
82-
.and_then(|object| object.as_typed_array::<u8>())
83-
.and_then(|array| array.as_bytes())
84-
.and_then(|bytes| std::str::from_utf8(bytes).ok())
85-
.map(|s| s.to_string())
86-
.unwrap_or_default(),
87-
Some(js_string) => js_string.to_string()?,
88-
};
89-
Ok(string)
90-
}
91-
}

0 commit comments

Comments
 (0)