Skip to content

Prefix global javascript functions with sub-namespaces#20743

Merged
alalek merged 2 commits intoopencv:3.4from
keroiber:prefix_js_function_bindings_with_namespace
Oct 4, 2021
Merged

Prefix global javascript functions with sub-namespaces#20743
alalek merged 2 commits intoopencv:3.4from
keroiber:prefix_js_function_bindings_with_namespace

Conversation

@keroiber
Copy link
Copy Markdown
Contributor

@keroiber keroiber commented Sep 24, 2021

Description

Fixes #15299
Both the global namespace and the fisheye namespace contain the function projectPoints. The bindings generator exports both functions with the original name globally. This solution adds the namespace as prefix to global function names, resulting in distict function names. In this case: projectPoints and fisheye_projectPoints.

Incompatibilities

All functions in namespaces will get a prefix in js. From the modules that are exported by default the dnn module is affected.

Personally I would prefer to make javscript objects for namespaces and add all enums, classes and functions there - but that would be a bigger change.

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 other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to 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
force_builders=Custom
build_image:Docs=docs-js:18.04
buildworker:Docs=linux-1,linux-2
build_image:Custom=javascript
buildworker:Custom=linux-4,linux-6

Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contribution!

This change also replaces names of "dnn" module calls which can break existed user code. So it can't be accepted in the current form.

Could you please reimplement namespaces_dict from Java binding?
Prefix is applied to fisheye through config file

@keroiber
Copy link
Copy Markdown
Contributor Author

keroiber commented Sep 29, 2021

@alalek as I mentioned in the PR I am aware of that problem and didn't expect the PR to be accepted. Thanks for the namespaces_dict hint, that was exactly what I was searching for. I will implement it that way. However, I am tempted to do it the other way around: Export with namespace by default and put the dnn as an exception in the dictionary.

Nevertheless I still would prefer to see the namespaces reflected in the js interface as described in the PR. That can avoid future name clashes. Assume a new function in dnn that has a namesake in another module. This cannot be solved with the namespace dictionary approach.
Would there be a chance to get that? Maybe as an alternative or additional js build result?

@alalek
Copy link
Copy Markdown
Member

alalek commented Sep 29, 2021

I take a look on original issue one more time.
JS functions are filtered through whitelist (to avoid very large build artifacts).
Example of such configuration is here: https://github.com/opencv/opencv/blob/master/platforms/js/opencv_js.config.py
Perhaps we should improve that to properly handle namespaces too.

P.S. need to check if embindgen could emulate "namespaces" (to avoid "ns_" prefixes).

@keroiber
Copy link
Copy Markdown
Contributor Author

Example of such configuration is here: https://github.com/opencv/opencv/blob/master/platforms/js/opencv_js.config.py

That's where I intend to add the namespaces_dict - this is the correct place, isn't it?

P.S. need to check if embindgen could emulate "namespaces" (to avoid "ns_" prefixes).

I also didn't check that til now. It seens that embind doesn't support that, see also this issue

@alalek
Copy link
Copy Markdown
Member

alalek commented Sep 29, 2021

Yes. this is correct place.
Currently binding generator processes 2 instances from cv and cv::fisheye namespaces.

Probably we can fix filtering process (and exclude cv::fisheye) to resolve the original issue. It would be a quick fix, so we can return to nested namespaces problem later (looks like that patch would be large enough).

@alalek
Copy link
Copy Markdown
Member

alalek commented Oct 3, 2021

Implemented filtering based on namespace_prefix_override.
Rebased to 3.4 branch.

Copy link
Copy Markdown
Contributor Author

@keroiber keroiber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good


if js_func_name == "_findHomography1":
os.error(ns_name, ns_parts, ns_part)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had already a commit prepared for removing the debug output, but you were faster...


white_list = makeWhiteList([core, imgproc, objdetect, video, dnn, features2d, calib3d])

# namespace_prefix_override['dnn'] = '' # compatibility stuff (enabled by default)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be clearer to define the compatibility behaviour here and leave namespace_prefix_override empty in embindgen.py?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is a sample of opencv.js configuration.
Users of custom opencv.js builds already have own configurations, so we need guarantee compatibility for them.

'fisheye_initUndistortRectifyMap',
'fisheye_projectPoints',
],
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now this is the only list in the file that's formatted this way

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 item per-line formatting is for easy merging between branches (it is already partially used on the master/4.x branch).

@alalek alalek merged commit f11f2bf into opencv:3.4 Oct 4, 2021
@alalek alalek mentioned this pull request Oct 4, 2021
@keroiber
Copy link
Copy Markdown
Contributor Author

keroiber commented Oct 8, 2021

@alalek thank you for finishing the compatibility parts

@alalek alalek mentioned this pull request Oct 15, 2021
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.

Building OpenCV.js with projectPoints + solvePnP

3 participants