Skip to content

Commit 4575a92

Browse files
committed
WebIDL binder: Added 'BindTo' extended attribute
This attribute can be used to change the C++ function that will be called. It can be used expose functions that have multiple overloads with the same number of parameters: Example C++ code: class BindToTest { public: int test(const char*) { return 1; } int test(int) { return 2; } }; IDL: interface BindToTest { void BindToTest(); [BindTo="test"] long testString([Const] DOMString arg); [BindTo="test"] long testInt(long arg); }; Now the two overloaded functions can be called from JavaScript.
1 parent b53978e commit 4575a92

File tree

8 files changed

+31
-5
lines changed

8 files changed

+31
-5
lines changed

test/webidl/post.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,5 +317,9 @@ if (isMemoryGrowthAllowed) {
317317
}
318318
}
319319

320+
let bindTo = new TheModule.BindToTest();
321+
console.log('testString: ' + bindTo.testString('hello'));
322+
console.log('testInt: ' + bindTo.testInt(10));
323+
320324
console.log('\ndone.')
321325
})();

test/webidl/test.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,9 @@ class ArrayArgumentTest {
242242
private:
243243
const char* m_array;
244244
};
245+
246+
class BindToTest {
247+
public:
248+
int test(const char*) { return 1; }
249+
int test(int) { return 2; }
250+
};

test/webidl/test.idl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,9 @@ interface JSArrayArgumentTest {
212212
boolean byteArrayTest([Const] byte[] arg);
213213
boolean domStringTest([Const] DOMString arg);
214214
};
215+
216+
interface BindToTest {
217+
void BindToTest();
218+
[BindTo="test"] long testString([Const] DOMString arg);
219+
[BindTo="test"] long testInt(long arg);
220+
};

test/webidl/test_ALL.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,5 @@ Aborted(Assertion failed: [CHECK FAILED] StringUser::Print(anotherString:another
114114
123
115115
true
116116
true
117+
testString: 1
118+
testInt: 2

test/webidl/test_DEFAULT.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,7 @@ Parent:42
113113
123
114114
true
115115
true
116+
testString: 1
117+
testInt: 2
116118

117119
done.

test/webidl/test_FAST.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,7 @@ Parent:42
113113
123
114114
true
115115
true
116+
testString: 1
117+
testInt: 2
116118

117119
done.

third_party/WebIDL.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3443,7 +3443,8 @@ def handleExtendedAttribute(self, attr):
34433443
identifier == "Value" or
34443444
identifier == "Operator" or
34453445
identifier == "Const" or
3446-
identifier == "WebGLHandlesContextLoss"):
3446+
identifier == "WebGLHandlesContextLoss" or
3447+
identifier == "BindTo"):
34473448
# Known attributes that we don't need to do anything with here
34483449
pass
34493450
else:

tools/webidl_binder.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ def type_to_cdec(raw):
388388

389389
def render_function(class_name, func_name, sigs, return_type, non_pointer,
390390
copy, operator, constructor, is_static, func_scope,
391-
call_content=None, const=False, array_attribute=False):
391+
call_content=None, const=False, array_attribute=False,
392+
bind_to=None):
392393
legacy_mode = CHECKS not in ['ALL', 'FAST']
393394
all_checks = CHECKS == 'ALL'
394395

@@ -601,9 +602,10 @@ def make_call_args(i):
601602
elif call_content is not None:
602603
call = call_content
603604
else:
604-
call = func_name + '(' + call_args + ')'
605+
if not bind_to:
606+
bind_to = func_name
607+
call = bind_to + '(' + call_args + ')'
605608
if is_static:
606-
607609
call = c_class_name + '::' + call
608610
else:
609611
call = 'self->' + call
@@ -771,7 +773,8 @@ def add_bounds_check_impl():
771773
constructor,
772774
is_static=m.isStatic(),
773775
func_scope=m.parentScope.identifier.name,
774-
const=m.getExtendedAttribute('Const'))
776+
const=m.getExtendedAttribute('Const'),
777+
bind_to=(m.getExtendedAttribute('BindTo') or [None])[0])
775778
mid_js += ['\n']
776779
if constructor:
777780
mid_js += build_constructor(name)

0 commit comments

Comments
 (0)