Skip to content

js: restore deep copy behavior for Mat.clone()#28216

Merged
asmorkalov merged 2 commits intoopencv:4.xfrom
happy-capybara-man:4.x
Dec 22, 2025
Merged

js: restore deep copy behavior for Mat.clone()#28216
asmorkalov merged 2 commits intoopencv:4.xfrom
happy-capybara-man:4.x

Conversation

@happy-capybara-man
Copy link
Copy Markdown
Contributor

@happy-capybara-man happy-capybara-man commented Dec 17, 2025

Problem

In OpenCV.js, cv.Mat.clone() may resolve to Embind ClassHandle.clone() (handle/shallow clone) instead of OpenCV deep copy.

Changes

  • modules/js/src/helpers.js: override cv.Mat.prototype.clone -> mat_clone after runtime init
  • modules/js/test/test_mat.js: update/extend tests to validate deep copy semantics of clone()

Fixes #27572
Related: PR #26643 (js_clone_fix), PR #27985 (documentation update)

Verification

  • Built OpenCV.js with Emscripten 2.0.10
  • QUnit: bin/tests.html
  • CoreMat: test_mat_creation (0 failures)
clone_fix

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

Emscripten 3.1.71+ introduced ClassHandle.clone() which performs a shallow copy.
This shadowed the original OpenCV clone() method which was intended for deep copying.

This patch:
1. Hooks into onRuntimeInitialized in helpers.js
2. Overrides cv.Mat.prototype.clone to point to mat_clone (deep copy)
3. Updates JS tests to use clone() to verify the fix
@asmorkalov asmorkalov requested a review from vrabaud December 18, 2025 06:59
@vrabaud
Copy link
Copy Markdown
Contributor

vrabaud commented Dec 18, 2025

I think it makes sense. Users will no more about OpenCV that emscripten and will therefore expect clone to behave like in C++. LGTM

@happy-capybara-man
Copy link
Copy Markdown
Contributor Author

Thanks for the review! I've removed the extra blank line at the end of helpers.js

@happy-capybara-man
Copy link
Copy Markdown
Contributor Author

@asmorkalov Hi, the CI seems to have failed due to network issues. Could you please help re-run the failed jobs? I'm hoping to include this in a report due this Tuesday. Thanks for your help!!

@happy-capybara-man
Copy link
Copy Markdown
Contributor Author

@asmorkalov Apologies for the extra notification.

I just wanted to flag that the CI re-run unfortunately hit the same Windows infrastructure error (EBUSY).

Since this is blocking the merge and my reporting deadline is tomorrow (Tuesday), I wanted to bring this to your attention one last time. I hope it's okay to ask for a merge based on the existing approval, given the CI instability.

Thanks again for your patience and help!

@asmorkalov asmorkalov added this to the 4.13.0 milestone Dec 22, 2025
@asmorkalov asmorkalov self-assigned this Dec 22, 2025
@asmorkalov asmorkalov merged commit 165b464 into opencv:4.x Dec 22, 2025
67 of 76 checks passed
@asmorkalov asmorkalov mentioned this pull request Feb 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

最新版本4.12.x中,clone似乎成了浅拷贝,我不得不使用copyTo

3 participants