dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.09k stars 1.56k forks source link

Support external operators in extensions on JS interop classes #48535

Open rileyporter opened 2 years ago

rileyporter commented 2 years ago

Currently we disallow all operators in JS interop classes. If we wanted to add support for them as instance members, the compilers would each need to implement each operator we wanted to allow.

However, we could support external operators in extensions, by lowering to js_util methods for some specific operators. For example, indexing could be lowered to js_util.getProperty(foo, index) and js_util.setProperty(foo, index, value). This issue is to track what other operators we would want to support in extensions.

simolus3 commented 2 years ago

In #48515, I was trying to bind to the BigInt wrapper object to deal with JavaScript APIs only supporting big ints.

For that, it would be great to have support for (all?) overridable Dart operators except for ~/ which doesn't exist in JS. There's also a question about ** which doesn't exist as an operator in Dart.

At the moment I'm implementing operators like this:

@JS('eval')
external Object _eval(String s);
bool Function(Object, Object) _leq =_eval('(a,b)=>a<=b') as bool Function(Object, Object);

This works but I really don't like it. Having these operators as binary functions in js_util would already help a lot.