From 5f03b0f96d8de96f5e9943be77a0a1b7f09ab58c Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com.> Date: Tue, 27 Feb 2024 10:20:33 -0800 Subject: [PATCH] fix(visitor): check type for the underlying js object --- napi/src/transformer.rs | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/napi/src/transformer.rs b/napi/src/transformer.rs index 111ff2cd..044daf5d 100644 --- a/napi/src/transformer.rs +++ b/napi/src/transformer.rs @@ -99,7 +99,14 @@ impl Visitors { fn named(&self, stage: VisitStage, name: &str) -> Option { self .for_stage(stage) - .and_then(|m| m.get_named_property::(name).ok()) + .and_then(|m| m.get_named_property::(name).ok()) + .and_then(|v| { + if let Ok(ValueType::Function) = v.get_type() { + v.try_into().ok() + } else { + None + } + }) } fn custom(&self, stage: VisitStage, obj: &str, name: &str) -> Option { @@ -177,7 +184,14 @@ impl JsVisitor { let mut types = VisitTypes::empty(); macro_rules! get { ($name: literal, $( $t: ident )|+) => {{ - let res: Option = visitor.get_named_property($name).ok(); + let res: Option = visitor.get_named_property::($name).ok().and_then(|v| { + if let Ok(ValueType::Function) = v.get_type() { + v.try_into().ok() + } else { + None + } + }); + if res.is_some() { types |= $( VisitTypes::$t )|+; } @@ -190,12 +204,19 @@ impl JsVisitor { macro_rules! map { ($name: literal, $( $t: ident )|+) => {{ - if let Ok(obj) = visitor.get_named_property::($name) { + let obj: Option = visitor.get_named_property::($name).ok().and_then(|v| { + if let Ok(ValueType::Object) = v.get_type() { + v.try_into().ok() + } else { + None + } + }); + + if obj.is_some() { types |= $( VisitTypes::$t )|+; - env.create_reference(obj).ok() - } else { - None } + + obj.and_then(|obj| env.create_reference(obj).ok()) }}; }