Skip to content

Commit 386ac90

Browse files
fix: should throw TypeError if drawImage param mismatched (#1190)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 4dba74e commit 386ac90

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

__test__/draw.spec.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,70 @@ test('drawImage-another-Canvas', async (t) => {
433433
await snapshotImage(t)
434434
})
435435

436+
test('drawImage-throws-TypeError-for-invalid-image-type', (t) => {
437+
const { ctx } = t.context
438+
439+
// Test with plain object
440+
t.throws(
441+
() => {
442+
// @ts-expect-error - Testing invalid type
443+
ctx.drawImage({}, 0, 0)
444+
},
445+
{
446+
instanceOf: TypeError,
447+
message: /Value is not one of these types/,
448+
},
449+
)
450+
451+
// Test with number
452+
t.throws(
453+
() => {
454+
// @ts-expect-error - Testing invalid type
455+
ctx.drawImage(42, 0, 0)
456+
},
457+
{
458+
instanceOf: TypeError,
459+
message: /Value is not one of these types/,
460+
},
461+
)
462+
463+
// Test with string
464+
t.throws(
465+
() => {
466+
// @ts-expect-error - Testing invalid type
467+
ctx.drawImage('not an image', 0, 0)
468+
},
469+
{
470+
instanceOf: TypeError,
471+
message: /Value is not one of these types/,
472+
},
473+
)
474+
475+
// Test with null
476+
t.throws(
477+
() => {
478+
// @ts-expect-error - Testing invalid type
479+
ctx.drawImage(null, 0, 0)
480+
},
481+
{
482+
instanceOf: TypeError,
483+
message: /Value is not one of these types/,
484+
},
485+
)
486+
487+
// Test with undefined
488+
t.throws(
489+
() => {
490+
// @ts-expect-error - Testing invalid type
491+
ctx.drawImage(undefined, 0, 0)
492+
},
493+
{
494+
instanceOf: TypeError,
495+
message: /Value is not one of these types/,
496+
},
497+
)
498+
})
499+
436500
test('ellipse', async (t) => {
437501
const { ctx } = t.context
438502
// Draw the ellipse

src/ctx.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,7 @@ impl CanvasRenderingContext2D {
19101910
pub fn draw_image(
19111911
&mut self,
19121912
env: &Env,
1913-
image: Either3<&mut CanvasElement, &mut SVGCanvas, &mut Image>,
1913+
image: Unknown,
19141914
sx: Option<f64>,
19151915
sy: Option<f64>,
19161916
s_width: Option<f64>,
@@ -1920,6 +1920,17 @@ impl CanvasRenderingContext2D {
19201920
d_width: Option<f64>,
19211921
d_height: Option<f64>,
19221922
) -> Result<()> {
1923+
let Ok(image) = (unsafe {
1924+
<Either3<&mut CanvasElement, &mut SVGCanvas, &mut Image> as FromNapiValue>::from_napi_value(
1925+
env.raw(),
1926+
image.raw(),
1927+
)
1928+
}) else {
1929+
return env.throw_type_error(
1930+
"Value is not one of these types: `CanvasElement`, `SVGCanvas`, `Image`",
1931+
Some("InvalidArg"),
1932+
);
1933+
};
19231934
let bitmap = match image {
19241935
Either3::A(canvas) => BitmapRef::Owned(canvas.ctx.as_ref().context.surface.get_bitmap()),
19251936
Either3::B(svg) => BitmapRef::Owned(svg.ctx.as_ref().context.surface.get_bitmap()),

0 commit comments

Comments
 (0)