Skip to content

Commit

Permalink
fix(visitor): check type for the underlying js object (#684)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Feb 27, 2024
1 parent 2f6b8d2 commit bdf4a77
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions napi/src/transformer.rs
Expand Up @@ -99,7 +99,14 @@ impl Visitors<JsObject> {
fn named(&self, stage: VisitStage, name: &str) -> Option<JsFunction> {
self
.for_stage(stage)
.and_then(|m| m.get_named_property::<JsFunction>(name).ok())
.and_then(|m| m.get_named_property::<JsUnknown>(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<JsFunction> {
Expand Down Expand Up @@ -177,7 +184,14 @@ impl JsVisitor {
let mut types = VisitTypes::empty();
macro_rules! get {
($name: literal, $( $t: ident )|+) => {{
let res: Option<JsFunction> = visitor.get_named_property($name).ok();
let res: Option<JsFunction> = visitor.get_named_property::<JsUnknown>($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 )|+;
}
Expand All @@ -190,12 +204,19 @@ impl JsVisitor {

macro_rules! map {
($name: literal, $( $t: ident )|+) => {{
if let Ok(obj) = visitor.get_named_property::<JsObject>($name) {
let obj: Option<JsObject> = visitor.get_named_property::<JsUnknown>($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())
}};
}

Expand Down

0 comments on commit bdf4a77

Please sign in to comment.