Skip to content

Commit eda304a

Browse files
committed
Allowing all C++ operators in WebIDL
1 parent 10cb9d4 commit eda304a

File tree

7 files changed

+18
-6
lines changed

7 files changed

+18
-6
lines changed

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: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -614,12 +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:
618-
call = '(*%s %s %s%s)' % (cast_self, operator, maybe_deref, args[0])
619-
elif operator == '[]':
617+
if operator == '[]':
620618
call = '((*%s)[%s%s])' % (cast_self, maybe_deref, args[0])
621619
else:
622-
raise Exception('unfamiliar operator ' + operator)
620+
call = '(*%s %s %s%s)' % (cast_self, operator, maybe_deref, args[0])
623621

624622
pre = ''
625623

0 commit comments

Comments
 (0)