Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(visitor): check type for the underlying js object #684

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions napi/src/transformer.rs
Original file line number Diff line number Diff line change
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
Loading