Merge pull request #20743 from keroiber:prefix_js_function_bindings_with_namespace

* Prefix global javascript functions with sub-namespaces

* js: handle 'namespace_prefix_override', update filtering

- avoid functions override with same name but different namespace

Co-authored-by: Alexander Alekhin <alexander.a.alekhin@gmail.com>
pull/20805/head^2
keroiber 4 years ago committed by GitHub
parent 24f43e7ae9
commit f11f2bfb56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      modules/js/generator/CMakeLists.txt
  2. 51
      modules/js/generator/embindgen.py
  3. 24
      platforms/js/opencv_js.config.py

@ -60,6 +60,7 @@ add_custom_command(
${JS_SOURCE_DIR}/src/core_bindings.cpp ${JS_SOURCE_DIR}/src/core_bindings.cpp
${CMAKE_CURRENT_SOURCE_DIR}/embindgen.py ${CMAKE_CURRENT_SOURCE_DIR}/embindgen.py
${CMAKE_CURRENT_SOURCE_DIR}/templates.py ${CMAKE_CURRENT_SOURCE_DIR}/templates.py
"${OPENCV_JS_WHITELIST_FILE}"
${scripts_hdr_parser} ${scripts_hdr_parser}
#(not needed - generated by CMake) ${CMAKE_CURRENT_BINARY_DIR}/headers.txt #(not needed - generated by CMake) ${CMAKE_CURRENT_BINARY_DIR}/headers.txt
${opencv_hdrs} ${opencv_hdrs}

@ -104,6 +104,9 @@ def makeWhiteList(module_list):
return wl return wl
white_list = None white_list = None
namespace_prefix_override = {
'dnn' : ''
}
# Features to be exported # Features to be exported
export_enums = False export_enums = False
@ -271,6 +274,8 @@ class FuncVariant(object):
class FuncInfo(object): class FuncInfo(object):
def __init__(self, class_name, name, cname, namespace, isconstructor): def __init__(self, class_name, name, cname, namespace, isconstructor):
self.name_id = '_'.join([namespace] + ([class_name] if class_name else []) + [name]) # unique id for dict key
self.class_name = class_name self.class_name = class_name
self.name = name self.name = name
self.cname = cname self.cname = cname
@ -295,9 +300,9 @@ class JSWrapperGenerator(object):
self.bindings = [] self.bindings = []
self.wrapper_funcs = [] self.wrapper_funcs = []
self.classes = {} self.classes = {} # FIXIT 'classes' should belong to 'namespaces'
self.namespaces = {} self.namespaces = {}
self.enums = {} self.enums = {} # FIXIT 'enums' should belong to 'namespaces'
self.parser = hdr_parser.CppHeaderParser() self.parser = hdr_parser.CppHeaderParser()
self.class_idx = 0 self.class_idx = 0
@ -419,7 +424,8 @@ class JSWrapperGenerator(object):
else: else:
func_map = self.namespaces.setdefault(namespace, Namespace()).funcs func_map = self.namespaces.setdefault(namespace, Namespace()).funcs
func = func_map.setdefault(name, FuncInfo(class_name, name, cpp_name, namespace, is_constructor)) fi = FuncInfo(class_name, name, cpp_name, namespace, is_constructor)
func = func_map.setdefault(fi.name_id, fi)
variant = FuncVariant(class_name, name, decl, is_constructor, is_class_method, is_const_method, variant = FuncVariant(class_name, name, decl, is_constructor, is_class_method, is_const_method,
is_virtual_method, is_pure_virtual_method, ref_return, const_return) is_virtual_method, is_pure_virtual_method, ref_return, const_return)
@ -430,7 +436,7 @@ class JSWrapperGenerator(object):
f.write(buf.getvalue()) f.write(buf.getvalue())
f.close() f.close()
def gen_function_binding_with_wrapper(self, func, class_info): def gen_function_binding_with_wrapper(self, func, ns_name, class_info):
binding_text = None binding_text = None
wrapper_func_text = None wrapper_func_text = None
@ -488,8 +494,23 @@ class JSWrapperGenerator(object):
# Wrapper function # Wrapper function
wrap_func_name = (func.class_name+"_" if class_info != None else "") + func.name.split("::")[-1] + "_wrapper" if ns_name != None and ns_name != "cv":
js_func_name = func.name ns_parts = ns_name.split(".")
if ns_parts[0] == "cv":
ns_parts = ns_parts[1:]
ns_part = "_".join(ns_parts) + "_"
ns_id = '_'.join(ns_parts)
ns_prefix = namespace_prefix_override.get(ns_id, ns_id)
if ns_prefix:
ns_prefix = ns_prefix + '_'
else:
ns_prefix = ''
if class_info == None:
js_func_name = ns_prefix + func.name
wrap_func_name = js_func_name + "_wrapper"
else:
wrap_func_name = ns_prefix + func.class_name + "_" + func.name + "_wrapper"
js_func_name = func.name
# TODO: Name functions based wrap directives or based on arguments list # TODO: Name functions based wrap directives or based on arguments list
if index > 0: if index > 0:
@ -740,12 +761,22 @@ class JSWrapperGenerator(object):
# step 2: generate bindings # step 2: generate bindings
# Global functions # Global functions
for ns_name, ns in sorted(self.namespaces.items()): for ns_name, ns in sorted(self.namespaces.items()):
if ns_name.split('.')[0] != 'cv': ns_parts = ns_name.split('.')
if ns_parts[0] != 'cv':
print('Ignore namespace: {}'.format(ns_name))
continue continue
for name, func in sorted(ns.funcs.items()): else:
ns_parts = ns_parts[1:]
ns_id = '_'.join(ns_parts)
ns_prefix = namespace_prefix_override.get(ns_id, ns_id)
for name_id, func in sorted(ns.funcs.items()):
name = func.name
if ns_prefix:
name = ns_prefix + '_' + name
if name in ignore_list: if name in ignore_list:
continue continue
if not name in white_list['']: if not name in white_list['']:
#print('Not in whitelist: "{}" from ns={}'.format(name, ns_name))
continue continue
ext_cnst = False ext_cnst = False
@ -769,7 +800,7 @@ class JSWrapperGenerator(object):
continue continue
if with_wrapped_functions: if with_wrapped_functions:
binding, wrapper = self.gen_function_binding_with_wrapper(func, class_info=None) binding, wrapper = self.gen_function_binding_with_wrapper(func, ns_name, class_info=None)
self.bindings += binding self.bindings += binding
self.wrapper_funcs += wrapper self.wrapper_funcs += wrapper
else: else:
@ -802,7 +833,7 @@ class JSWrapperGenerator(object):
class_bindings.append(constructor_template.substitute(signature=', '.join(args))) class_bindings.append(constructor_template.substitute(signature=', '.join(args)))
else: else:
if with_wrapped_functions and (len(method.variants) > 1 or len(method.variants[0].args)>0 or "String" in method.variants[0].rettype): if with_wrapped_functions and (len(method.variants) > 1 or len(method.variants[0].args)>0 or "String" in method.variants[0].rettype):
binding, wrapper = self.gen_function_binding_with_wrapper(method, class_info=class_info) binding, wrapper = self.gen_function_binding_with_wrapper(method, None, class_info=class_info)
self.wrapper_funcs = self.wrapper_funcs + wrapper self.wrapper_funcs = self.wrapper_funcs + wrapper
class_bindings = class_bindings + binding class_bindings = class_bindings + binding
else: else:

@ -55,8 +55,26 @@ features2d = {'Feature2D': ['detect', 'compute', 'detectAndCompute', 'descriptor
'BFMatcher': ['isMaskSupported', 'create'], 'BFMatcher': ['isMaskSupported', 'create'],
'': ['drawKeypoints', 'drawMatches', 'drawMatchesKnn']} '': ['drawKeypoints', 'drawMatches', 'drawMatchesKnn']}
calib3d = {'': ['findHomography', 'calibrateCameraExtended', 'drawFrameAxes', 'estimateAffine2D', \ calib3d = {
'getDefaultNewCameraMatrix', 'initUndistortRectifyMap', 'Rodrigues', \ '': [
'solvePnP', 'solvePnPRansac', 'solvePnPRefineLM']} 'findHomography',
'calibrateCameraExtended',
'drawFrameAxes',
'estimateAffine2D',
'getDefaultNewCameraMatrix',
'initUndistortRectifyMap',
'Rodrigues',
'solvePnP',
'solvePnPRansac',
'solvePnPRefineLM',
'projectPoints',
# cv::fisheye namespace
'fisheye_initUndistortRectifyMap',
'fisheye_projectPoints',
],
}
white_list = makeWhiteList([core, imgproc, objdetect, video, dnn, features2d, calib3d]) white_list = makeWhiteList([core, imgproc, objdetect, video, dnn, features2d, calib3d])
# namespace_prefix_override['dnn'] = '' # compatibility stuff (enabled by default)

Loading…
Cancel
Save