Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing all binary C++ operators in WebIDL #22770

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading