From 1bb104206450d440991cdd6cc093ad31696be489 Mon Sep 17 00:00:00 2001 From: Yifeng Wang Date: Mon, 18 Jan 2021 15:25:04 +0800 Subject: [PATCH] feat: support naturalWidth/naturalHeight/complete/alt --- index.d.ts | 4 ++++ src/image.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index e49b93bf..368b11dc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -34,6 +34,10 @@ export class ImageData { export class Image { readonly width: number readonly height: number + readonly naturalWidth: number + readonly naturalHeight: number + readonly complete: boolean + alt: string src: Buffer } diff --git a/src/image.rs b/src/image.rs index dc984605..4bff0f63 100644 --- a/src/image.rs +++ b/src/image.rs @@ -122,6 +122,8 @@ fn image_data_constructor(ctx: CallContext) -> Result { pub struct Image { pub bitmap: Option, + pub complete: bool, + pub alt: String, } impl Image { @@ -136,9 +138,21 @@ impl Image { Property::new(&env, "height")? .with_setter(set_noop) .with_getter(get_height), + Property::new(&env, "naturalWidth")? + .with_setter(set_noop) + .with_getter(get_width), + Property::new(&env, "naturalHeight")? + .with_setter(set_noop) + .with_getter(get_height), + Property::new(&env, "complete")? + .with_setter(set_noop) + .with_getter(get_complete), + Property::new(&env, "alt")? + .with_setter(set_alt) + .with_getter(get_alt), Property::new(&env, "src")? .with_setter(set_src) - .with_getter(get_src), + .with_getter(set_noop), ], ) } @@ -146,9 +160,14 @@ impl Image { #[js_function] fn image_constructor(ctx: CallContext) -> Result { - let js_image = Image { bitmap: None }; + let js_image = Image { + complete: false, + bitmap: None, + alt: "".to_string(), + }; let mut this = ctx.this_unchecked::(); ctx.env.wrap(&mut this, js_image)?; + ctx.env.get_undefined() } @@ -172,14 +191,35 @@ fn get_height(ctx: CallContext) -> Result { .create_double(image.bitmap.as_ref().unwrap().height as f64) } +#[js_function] +fn get_complete(ctx: CallContext) -> Result { + let this = ctx.this_unchecked::(); + let image = ctx.env.unwrap::(&this)?; + + ctx.env.get_boolean(image.complete) +} + +#[js_function] +fn get_alt(ctx: CallContext) -> Result { + let this = ctx.this_unchecked::(); + let image = ctx.env.unwrap::(&this)?; + + ctx.env.create_string(image.alt.as_str()) +} + #[js_function(1)] -fn set_noop(ctx: CallContext) -> Result { +fn set_alt(ctx: CallContext) -> Result { + let this = ctx.this_unchecked::(); + let mut image = ctx.env.unwrap::(&this)?; + let arg = ctx.get::(0)?.into_utf8()?; + image.alt = arg.as_str()?.to_string(); + ctx.env.get_undefined() } -#[js_function] -fn get_src(ctx: CallContext) -> Result { - ctx.env.get_undefined() // TODO +#[js_function(1)] +fn set_noop(ctx: CallContext) -> Result { + ctx.env.get_undefined() } #[js_function(1)] @@ -197,6 +237,7 @@ fn set_src(ctx: CallContext) -> Result { } let length = src_data_ab.len(); + image.complete = true; image .bitmap .get_or_insert(Bitmap::from_buffer(src_data_ab.as_ptr() as *mut u8, length));