Repository: opencv
Issue: OpenCV.js 4.X with `--build_wasm` `--threads` `--build_test` Runtime Throws Errors
Labels: bug,category: javascript (js)

### System Information

OpenCV version: 4.10.0
Operating System / Platform: Darwin 23.5.0 Darwin Kernel Version 23.5.0 - Sonoma 14.5 (23F79)
Compiler & Compiler version: emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 2.0.13 (681cdf3e7edeef79855adc2f2a04a1a2a44ec24f)
Browser: Chrome Version 126.0.6478.183 (Official Build) (x86_64)
npx: 10.2.3

### Detailed description

In `platforms/js` when building with flags

```bash
python3 build_js.py --build_wasm --threads --build_test --build_loader --clean_build_dir  build_js
```

The build succeeds, however the runtime is broken in a couple ways: 

1. With a "SharedArrayBuffer is not defined" error; many resources on this error indicated a security policy standard with [COOP and COEP](https://web.dev/articles/coop-coep) headers. Enabling these headers gets past the SharedArrayBuffer issue; Or, `<sometag crossorigin="">...` works around the QUint problem as well.  
![Screenshot 2024-07-25 at 3 35 52 PM](https://github.com/user-attachments/assets/0eadeae0-ee72-45d0-8891-35670b4d8939)

2. If (1) is fixed, 

```TypeError: cv is not a function```

``` [post-exception status] Exception thrown, see JavaScript console``` 

```pthread sent an error! http://localhost:8080/opencv_js.worker.js:1: Uncaught TypeError: cv is not a function``` 

get throw many times:

![Screenshot 2024-07-25 at 3 37 07 PM](https://github.com/user-attachments/assets/1fc4bf29-f64d-49b4-869f-58ef6c7b19a0)

<details><summary>cv.getBuildInformation()</summary>

```

General configuration for OpenCV 4.10.0-dev =====================================
  Version control:               4.10.0-130-g53a5b85d9f-dirty

  Platform:
    Timestamp:                   2024-07-25T22:00:03Z
    Host:                        Darwin 23.5.0 x86_64
    Target:                      Emscripten 1 x86
    CMake:                       3.30.0
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               Release
    Algorithm Hint:              ALGO_HINT_ACCURATE

  CPU/HW features:
    Baseline:

  C/C++:
    Built as dynamic libs?:      NO
    C++ standard:                11
    C++ Compiler:                /Users/gblikas/projects/opencvjs/emsdk/upstream/emscripten/em++  (ver 13.0.0)
    C++ flags (Release):         -s WASM=1 -s SINGLE_FILE=1 -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=4 -s EXPORTED_FUNCTIONS="['_malloc', '_free']"   -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -pthread -Qunused-arguments -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -s WASM=1 -s SINGLE_FILE=1 -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=4 -s EXPORTED_FUNCTIONS="['_malloc', '_free']"   -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -pthread -Qunused-arguments -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /Users/gblikas/projects/opencvjs/emsdk/upstream/emscripten/emcc
    C flags (Release):           -s WASM=1 -s SINGLE_FILE=1 -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=4 -s EXPORTED_FUNCTIONS="['_malloc', '_free']"   -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -pthread -Qunused-arguments -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -s WASM=1 -s SINGLE_FILE=1 -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=4 -s EXPORTED_FUNCTIONS="['_malloc', '_free']"   -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -pthread -Qunused-arguments -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,--gc-sections  
    Linker flags (Debug):        -Wl,--gc-sections  
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:
    3rdparty dependencies:       zlib libprotobuf

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann imgproc js objdetect photo video
    Disabled:                    highgui imgcodecs ml stitching videoio world
    Disabled by dependency:      ts
    Unavailable:                 gapi java python2 python3
    Applications:                examples
    Documentation:               js
    Non-free algorithms:         NO

  GUI: 

  Media I/O: 
    ZLib:                        build (ver 1.3.1)
    JPEG 2000:                   build (ver 2.5.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:

  Parallel framework:            pthreads

  Other third-party libraries:
    VA:                          NO
    Custom HAL:                  NO
    Protobuf:                    build (3.19.1)
    Flatbuffers:                 builtin/3rdparty (23.5.9)

  Python (for build):            /usr/local/opt/python@3.12/bin/python3.12

  Install to:                    /usr/local
-----------------------------------------------------------------

```

</details>


The weird thing is that the tests all succeed, but many errors are thrown... I have confirmed that disabling `--threads` does allow it to work with no errors (this is to be expected). Because the errors thrown, I'm unsure if my built `wasm` is actually running with threads or not... 

### Steps to reproduce

1. Follow the installation instructions at https://docs.opencv.org/3.4/d4/da1/tutorial_js_setup.html 
2. Run the server with `npx http-server .`
5. Modify the headers with COOP COEP, or include `<script crossorigin="anonymous"` 
6. Open `localhost:8080/tests.html` in Chrome



### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [ ] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)