Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minmax #866

Closed
wants to merge 420 commits into from
Closed

Minmax #866

wants to merge 420 commits into from

Conversation

UrielCh
Copy link
Contributor

@UrielCh UrielCh commented Mar 9, 2023

  • add cv.min
  • add cv.max
  • integrate native-node-utils code inside cc/native-node-utils
  • add comments in native-node-utils

UrielCh and others added 28 commits January 9, 2023 18:12
In some scenarios the JS files of the library may be reloaded, such as when loaded by Jest. It seems that the underlying node extension is not reloaded in this case. This causes an exception to be thrown when rebuilding the exported API object:

> TypeError: Cannot read properties of undefined (reading 'constructor')

This happens in the `isAsyncFn` function inside `promisify.js` because `fn.prototype` is undefined. This code works fine the first time, so why does it fail when run again? The reason is that the functions attached to the `cv` namespace are all defined in C++ and are thus essentially the equivalent of a `FunctionExpression`, which have a `prototype.constructor.name`. Calling `loadOpenCV` calls `getOpenCV` which loads the node extension, then calls `promisify` on that namespace, and finally calls `extendWithJsSources`. This is where we run into problems.

These are the functions that get added to the `cv` namespace from JS:

```
drawTextBox
drawDetection
toMatTypeName
getScoreMax
dropOverlappingZone
```

Almost all of these are defined as `ArrowFunctionExpression`s which DO NOT have a `prototype`. So after the JS files get cleared from `require.cache` by Jest or some other means, loading this library again REUSES the `cv` namespace because the node extension is not cleared from memory. But this time it has those extra functions attached, causing the exception mentioned above in `isAsyncFn`.

I changed two things to fix this:
1. make `promisify.ts` not assume that all functions have prototypes; they don't.
2. define the extra from-JS functions as `FunctionExpression`s so they will have prototypes (and names).
@UrielCh
Copy link
Contributor Author

UrielCh commented Mar 9, 2023

oups

@UrielCh UrielCh closed this Mar 9, 2023
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.

7 participants