Skip to content

Commit

Permalink
PR-746: using primordials to detect Buffer constructor during instanc…
Browse files Browse the repository at this point in the history
…eof test

pr(746): Using primordial for buffer instanceof test, and returning error on js string to rust string cast failure.
  • Loading branch information
lewisgcm committed Dec 18, 2024
1 parent cd0dd7b commit 5d6bd72
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions modules/llrt_buffer/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use rquickjs::{
function::{Constructor, Opt},
module::{Declarations, Exports, ModuleDef},
prelude::{Func, This},
Array, ArrayBuffer, Coerced, Ctx, Exception, Function, IntoJs, JsLifetime, Object, Result,
TypedArray, Value,
Array, ArrayBuffer, Coerced, Ctx, Exception, IntoJs, JsLifetime, Object, Result, TypedArray,
Value,
};

#[derive(JsLifetime)]
Expand Down Expand Up @@ -121,19 +121,18 @@ fn byte_length<'js>(ctx: Ctx<'js>, value: Value<'js>, encoding: Opt<String>) ->
}

fn is_buffer<'js>(ctx: Ctx<'js>, value: Value<'js>) -> Result<bool> {
let class: Function = ctx.globals().get(stringify!(Buffer))?;
if let Some(object) = value.as_object() {
return Ok(object.is_instance_of(class));
let constructor = BufferPrimordials::get(&ctx)?;
return Ok(object.is_instance_of(&constructor.constructor));
}

Ok(false)
}

fn is_encoding(value: Value) -> Result<bool> {
if let Some(js_string) = value.as_string() {
if let Ok(std_string) = js_string.to_string() {
return Ok(Encoder::from_str(std_string.as_str()).is_ok());
}
let std_string = js_string.to_string()?;
return Ok(Encoder::from_str(std_string.as_str()).is_ok());
}

Ok(false)
Expand Down

0 comments on commit 5d6bd72

Please sign in to comment.