Skip to content

fix(napi): texSubImage2D from a WebGPU canvas throws instead of panicking - #188

Merged
triniwiz merged 1 commit into
NativeScript:masterfrom
avijit213:fix/napi-texsubimage2d-webgpu-source
Nov 7, 2026
Merged

triniwiz merged 1 commit into
NativeScript:masterfrom
avijit213:fix/napi-texsubimage2d-webgpu-source

Conversation

@avijit213

@avijit213 avijit213 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Problem

texSubImage2D on a WebGL or WebGL2 context panics when the pixel source is a WebGPU canvas. That arm of the source match in crates/canvas-napi/src/gl/base.rs was still todo!("Implement drawImage for GPUContext"). A panic inside a Node-API callback isn't a JS exception the caller can catch, and it can bring down the host process (NativeScript Windows, Node).

texImage2D already handles the same case without panicking: it returns Error::from_reason("texImage2D from a WebGPU canvas is not supported").

Fix

The Either4::D(_) (WebGPU) arm of texSubImage2D now returns the matching error:

return Err(Error::from_reason("texSubImage2D from a WebGPU canvas is not supported"));

tex_sub_image_2_d already returns Result<()>, so the caller gets a thrown JS Error instead of a crash. Nothing else changes. This was the only todo!() / unimplemented!() in the Node-API crates.

Related to #131 (Node-API support). It doesn't close it.

Validation

  • I reviewed the change. It is one match arm in a function returning Result<()>, and it uses the same Error::from_reason form as the texImage2D arm about 200 lines above.
  • Not compiled locally. cargo check -p canvas-napi on macOS stopped while building skia-bindings from source, because ninja wasn't installed. That failure is unrelated to this change. I'm relying on CI for the build.
  • Not run. The crates/canvas-napi/__test__ source tests skip on anything other than Windows (process.platform !== 'win32'), and I don't have a Windows host. A test in canvas-sources.test.mjs that expects texSubImage2D(…, webgpuCanvas) to throw would cover this. I can add one if you'd like it in this PR.

Summary by CodeRabbit

  • Bug Fixes
    • texSubImage2D now reports an error for unsupported WebGPU canvas sources instead of crashing.

…king

The WebGPU-canvas arm of texSubImage2D was a todo!(), so the call panicked
inside the Node-API callback, which can take down the host process.
Return the same "not supported" error texImage2D already returns for a
WebGPU canvas source.
@coderabbitai

coderabbitai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

When texSubImage2D receives a WebGPU canvas source, it now returns a Node-API error stating that the source is unsupported, rather than reaching todo!() and panicking.

Changes

WebGPU canvas source handling

Layer / File(s) Summary
Return an error for unsupported sources
crates/canvas-napi/src/gl/base.rs
The WebGPU-canvas branch of texSubImage2D returns an error stating that the source is unsupported.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Change: Bug fix

Suggested reviewers: triniwiz

Merge Risk: Minimal · up to 88992

The change is intended to report unsupported WebGPU canvas sources as errors instead of panicking. No merge-blocking issue is established; build and Windows tests remain unverified.

Pre-merge checks: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title accurately describes the change from a panic to an error for a WebGPU canvas source.
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 1 files.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

Clippy (1.99.1)

Clippy execution timed out

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the canvas glow,
A WebGPU source says, “No.”
texSubImage2D returns an error,
No panic springs from the burrow.
The rabbit hops along its way.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 34fb7993-e0e2-4b48-935e-998ed21ec1e9
Commits

Reviewing files that changed from the base of the PR and between bb8c013 and 88992c5.

Files selected for processing: 1

  • crates/canvas-napi/src/gl/base.rs

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@avijit213

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@triniwiz
triniwiz merged commit 4053cdc into NativeScript:master Nov 7, 2026
12 of 19 checks passed
triniwiz added a commit that referenced this pull request Nov 7, 2026
A WebGPU canvas used as a WebGL texture source threw (texImage2D) or, before
#188, panicked (texSubImage2D). Both now read the canvas's frame (the current
texture if one is out, else the last presented frame) as unpremultiplied RGBA8
and upload it, like a 2D canvas source.

canvas-c: toDataURL's copy-and-map step is now with_mapped_texture, shared with
the new read_rgba8; canvas_native_webgl_tex_{,sub_}image2d_webgpu return false
when there is no 8-bit frame, which the binding throws for.

Specs: toDataURL pixel check for a WebGPU canvas (all platforms), and
texImage2D/texSubImage2D from a WebGPU canvas (Windows for now).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants