Skip to content

Commit

Permalink
Allowing all binary C++ operators in WebIDL
Browse files Browse the repository at this point in the history
  • Loading branch information
jrouwe committed Oct 21, 2024
1 parent 10cb9d4 commit 99e4da2
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 7 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 operators that contain ``=``: ``+=``, ``*=``, ``-=`` etc., binary operators like ``+``, ``-``, ``*``, ``/`` etc., 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
6 changes: 2 additions & 4 deletions tools/webidl_binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,12 +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:
call = '(*%s %s %s%s)' % (cast_self, operator, maybe_deref, args[0])
elif operator == '[]':
if operator == '[]':
call = '((*%s)[%s%s])' % (cast_self, maybe_deref, args[0])
else:
raise Exception('unfamiliar operator ' + operator)
call = '(*%s %s %s%s)' % (cast_self, operator, maybe_deref, args[0])

pre = ''

Expand Down

0 comments on commit 99e4da2

Please sign in to comment.