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: adjust the order of onload calls #898

Merged
merged 5 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions example/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ async function main() {

const image = new Image()
image.src = file
await new Promise(r => image.onload = r)

const w = image.width
const h = image.height
Expand Down
55 changes: 25 additions & 30 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::str::FromStr;
use std::{ptr, str};

use base64_simd::STANDARD;
use napi::{bindgen_prelude::*, check_status, NapiRaw, NapiValue, Ref};
use napi::{bindgen_prelude::*, check_status, JsObject, NapiRaw, NapiValue, Ref};

use crate::error::SkError;
use crate::global_fonts::get_font;
Expand Down Expand Up @@ -433,44 +433,39 @@ impl Task for BitmapDecoder {
self_mut.width = output.width;
self_mut.height = output.height;
self_mut.complete = true;
self_mut.bitmap = None;

let mut err: Option<&str> = None;

match output.bitmap {
DecodeStatus::Ok(bitmap) => {
self_mut.src = self.data.take();
let onload = this.get_named_property_unchecked::<Unknown>("onload")?;
if onload.get_type()? == ValueType::Function {
let onload_func: Function<(), ()> = Function::from_unknown(onload)?;
onload_func.apply(this, ())?;
}
self_mut.is_svg = bitmap.is_svg;
self_mut.bitmap = Some(bitmap.data);
}
DecodeStatus::Empty => {
let onload = this.get_named_property_unchecked::<Unknown>("onload")?;
if onload.get_type()? == ValueType::Function {
let onload_func: Function<(), ()> = Function::from_unknown(onload)?;
onload_func.apply(this, ())?;
}
self_mut.bitmap = None;
}
DecodeStatus::Empty => {}
DecodeStatus::InvalidSvg => {
let on_error = this.get_named_property_unchecked::<Unknown>("onerror")?;
if on_error.get_type()? == ValueType::Function {
let onerror_func: Function<Unknown, ()> = Function::from_unknown(on_error)?;
onerror_func.apply(
this,
JsError::from(Error::new(Status::InvalidArg, "Invalid SVG image")).into_unknown(env),
)?;
}
err = Some("Invalid SVG image");
}
DecodeStatus::InvalidImage => {
let on_error = this.get_named_property_unchecked::<Unknown>("onerror")?;
if on_error.get_type()? == ValueType::Function {
let onerror_func: Function<Object, Unknown> = Function::from_unknown(on_error)?;
onerror_func.apply(
this,
env.create_error(Error::new(Status::InvalidArg, "Unsupported image type"))?,
)?;
}
err = Some("Unsupported image type");
}
}

if let Some(err_str) = err.take() {
let on_error = this.get_named_property_unchecked::<Unknown>("onerror")?;
if on_error.get_type()? == ValueType::Function {
let onerror_func: Function<Object, Unknown> = Function::from_unknown(on_error)?;
onerror_func.apply(
this,
env.create_error(Error::new(Status::InvalidArg, err_str))?,
)?;
}
} else {
let onload = this.get_named_property_unchecked::<Unknown>("onload")?;
if onload.get_type()? == ValueType::Function {
let onload_func: Function<(), ()> = Function::from_unknown(onload)?;
onload_func.apply(this, ())?;
}
}
Ok(())
Expand Down
Loading