Skip to content

Commit

Permalink
WebIDL Binder: Allow all binary C++ operators (#22770)
Browse files Browse the repository at this point in the history
This expands the list of supported operators by the 'Operator' WebIDL
attribute to all binary operators that are supported by C++.

Helps #9415
  • Loading branch information
jrouwe authored Oct 22, 2024
1 parent f3a5481 commit b53978e
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ You can bind to C++ operators using ``[Operator=]``:
.. note::

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


enums
Expand Down
2 changes: 2 additions & 0 deletions test/webidl/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ console.log(new TheModule.Inner().get());
console.log('getAsArray: ' + new TheModule.Inner().getAsArray(12));
new TheModule.Inner().mul(2);
new TheModule.Inner().incInPlace(new TheModule.Inner());
console.log('add: ' + new TheModule.Inner(1).add(new TheModule.Inner(2)).get_value());
console.log('mul2: ' + new TheModule.Inner(10).mul2(5));

console.log(TheModule.enum_value1);
console.log(TheModule.enum_value2);
Expand Down
5 changes: 4 additions & 1 deletion test/webidl/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,17 @@ struct VoidPointerUser {
namespace Space {
struct Inner {
int value;
Inner() : value(1) {}
Inner(int x = 1) : value(x) {}
int get() { return 198; }
int get_value() { return value; }
Inner& operator*=(float x) { return *this; }
int operator[](int x) { return x*2; }
void operator+=(const Inner& other) {
value += other.value;
printf("Inner::+= => %d\n", value);
}
Inner operator+(const Inner& other) { return Inner(value + other.value); }
int operator*(int x) { return value * x; }
};

// We test compilation of abstract base classes in a namespace here.
Expand Down
5 changes: 4 additions & 1 deletion test/webidl/test.idl
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ interface VoidPointerUser {

[Prefix="Space::"]
interface Inner {
void Inner();
void Inner(optional long value);
long get();
long get_value();
[Operator="*=", Ref] Inner mul(float x);
[Operator="[]"] long getAsArray(long x);
[Operator="+="] void incInPlace([Const, Ref] Inner i);
[Operator="+", Value] Inner add([Const, Ref] Inner i);
[Operator="*"] long mul2(long x);
};

[Prefix = "Space::"]
Expand Down
2 changes: 2 additions & 0 deletions test/webidl/test_ALL.out
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ object
198
getAsArray: 24
Inner::+= => 2
add: 3
mul2: 50
0
1
34,34
Expand Down
2 changes: 2 additions & 0 deletions test/webidl/test_DEFAULT.out
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ object
198
getAsArray: 24
Inner::+= => 2
add: 3
mul2: 50
0
1
34,34
Expand Down
2 changes: 2 additions & 0 deletions test/webidl/test_FAST.out
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ object
198
getAsArray: 24
Inner::+= => 2
add: 3
mul2: 50
0
1
34,34
Expand Down
5 changes: 4 additions & 1 deletion tools/webidl_binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,10 @@ def make_call_args(i):
# this function comes from an ancestor class; for operators, we must cast it
cast_self = 'dynamic_cast<' + type_to_c(func_scope) + '>(' + cast_self + ')'
maybe_deref = deref_if_nonpointer(raw[0])
if '=' in operator:
operator = operator.strip()
if operator in ["+", "-", "*", "/", "%", "^", "&", "|", "=",
"<", ">", "+=", "-=", "*=", "/=", "%=", "^=", "&=", "|=", "<<", ">>", ">>=",
"<<=", "==", "!=", "<=", ">=", "<=>", "&&", "||"]:
call = '(*%s %s %s%s)' % (cast_self, operator, maybe_deref, args[0])
elif operator == '[]':
call = '((*%s)[%s%s])' % (cast_self, maybe_deref, args[0])
Expand Down

0 comments on commit b53978e

Please sign in to comment.