Skip to content

Commit b53978e

Browse files
authored
WebIDL Binder: Allow all binary C++ operators (#22770)
This expands the list of supported operators by the 'Operator' WebIDL attribute to all binary operators that are supported by C++. Helps #9415
1 parent f3a5481 commit b53978e

File tree

8 files changed

+21
-4
lines changed

8 files changed

+21
-4
lines changed

site/source/docs/porting/connecting_cpp_and_javascript/WebIDL-Binder.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ You can bind to C++ operators using ``[Operator=]``:
318318
.. note::
319319

320320
- The operator name can be anything (``add`` is just an example).
321-
- Support is currently limited to operators that contain ``=``: ``+=``, ``*=``, ``-=`` etc., and to the array indexing operator ``[]``.
321+
- Support is currently limited to the following binary operators: ``+``, ``-``, ``*``, ``/``, ``%``, ``^``, ``&``, ``|``, ``=``, ``<``, ``>``, ``+=``, ``-=``, ``*=``, ``/=``, ``%=``, ``^=``, ``&=``, ``|=``, ``<<``, ``>>``, ``>>=``, ``<<=``, ``==``, ``!=``, ``<=``, ``>=``, ``<=>``, ``&&``, ``||``, and to the array indexing operator ``[]``.
322322

323323

324324
enums

test/webidl/post.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ console.log(new TheModule.Inner().get());
144144
console.log('getAsArray: ' + new TheModule.Inner().getAsArray(12));
145145
new TheModule.Inner().mul(2);
146146
new TheModule.Inner().incInPlace(new TheModule.Inner());
147+
console.log('add: ' + new TheModule.Inner(1).add(new TheModule.Inner(2)).get_value());
148+
console.log('mul2: ' + new TheModule.Inner(10).mul2(5));
147149

148150
console.log(TheModule.enum_value1);
149151
console.log(TheModule.enum_value2);

test/webidl/test.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,17 @@ struct VoidPointerUser {
9595
namespace Space {
9696
struct Inner {
9797
int value;
98-
Inner() : value(1) {}
98+
Inner(int x = 1) : value(x) {}
9999
int get() { return 198; }
100+
int get_value() { return value; }
100101
Inner& operator*=(float x) { return *this; }
101102
int operator[](int x) { return x*2; }
102103
void operator+=(const Inner& other) {
103104
value += other.value;
104105
printf("Inner::+= => %d\n", value);
105106
}
107+
Inner operator+(const Inner& other) { return Inner(value + other.value); }
108+
int operator*(int x) { return value * x; }
106109
};
107110

108111
// We test compilation of abstract base classes in a namespace here.

test/webidl/test.idl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,14 @@ interface VoidPointerUser {
8585

8686
[Prefix="Space::"]
8787
interface Inner {
88-
void Inner();
88+
void Inner(optional long value);
8989
long get();
90+
long get_value();
9091
[Operator="*=", Ref] Inner mul(float x);
9192
[Operator="[]"] long getAsArray(long x);
9293
[Operator="+="] void incInPlace([Const, Ref] Inner i);
94+
[Operator="+", Value] Inner add([Const, Ref] Inner i);
95+
[Operator="*"] long mul2(long x);
9396
};
9497

9598
[Prefix = "Space::"]

test/webidl/test_ALL.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ object
7171
198
7272
getAsArray: 24
7373
Inner::+= => 2
74+
add: 3
75+
mul2: 50
7476
0
7577
1
7678
34,34

test/webidl/test_DEFAULT.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ object
7171
198
7272
getAsArray: 24
7373
Inner::+= => 2
74+
add: 3
75+
mul2: 50
7476
0
7577
1
7678
34,34

test/webidl/test_FAST.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ object
7171
198
7272
getAsArray: 24
7373
Inner::+= => 2
74+
add: 3
75+
mul2: 50
7476
0
7577
1
7678
34,34

tools/webidl_binder.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,10 @@ def make_call_args(i):
614614
# this function comes from an ancestor class; for operators, we must cast it
615615
cast_self = 'dynamic_cast<' + type_to_c(func_scope) + '>(' + cast_self + ')'
616616
maybe_deref = deref_if_nonpointer(raw[0])
617-
if '=' in operator:
617+
operator = operator.strip()
618+
if operator in ["+", "-", "*", "/", "%", "^", "&", "|", "=",
619+
"<", ">", "+=", "-=", "*=", "/=", "%=", "^=", "&=", "|=", "<<", ">>", ">>=",
620+
"<<=", "==", "!=", "<=", ">=", "<=>", "&&", "||"]:
618621
call = '(*%s %s %s%s)' % (cast_self, operator, maybe_deref, args[0])
619622
elif operator == '[]':
620623
call = '((*%s)[%s%s])' % (cast_self, maybe_deref, args[0])

0 commit comments

Comments
 (0)