[js_util] Make callMethod take an Object method.

CoreLibraryReviewExempt: Minor refactor of web only library.
Change-Id: I7afc4a00501ac12c1aa0248305001a978f2e9f8f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/288641
Reviewed-by: Srujan Gaddam <srujzs@google.com>
Commit-Queue: Joshua Litt <joshualitt@google.com>
This commit is contained in:
Joshua Litt 2023-03-23 00:23:00 +00:00 committed by Commit Queue
parent 929fec660b
commit 884f42a4f4
5 changed files with 28 additions and 17 deletions

View file

@ -163,6 +163,8 @@
- `jsify` is now permissive and has inverse semantics to `dartify`.
- `jsify` and `dartify` both handle types they understand natively more
efficiently.
- Signature of `callMethod` has been aligned with the other methods and
now takes `Object` instead of `String`.
### Tools

View file

@ -101,13 +101,13 @@ T _setPropertyUnchecked<T>(Object o, Object name, T? value) {
}
@patch
T callMethod<T>(Object o, String method, List<Object?> args) {
T callMethod<T>(Object o, Object method, List<Object?> args) {
assertInteropArgs(args);
return JS<dynamic>('Object|Null', '#[#].apply(#, #)', o, method, o, args);
}
/// Similar to [callMethod] but introduces an unsound implicit cast to `T`.
T _callMethodTrustType<T>(Object o, String method, List<Object?> args) {
T _callMethodTrustType<T>(Object o, Object method, List<Object?> args) {
assertInteropArgs(args);
return JS<T>('Object|Null', '#[#].apply(#, #)', o, method, o, args);
}
@ -118,7 +118,7 @@ T _callMethodTrustType<T>(Object o, String method, List<Object?> args) {
/// body of this method. Edit `ProgramCompiler.visitStaticInvocation` if you
/// edit this method.
@pragma('dart2js:tryInline')
T _callMethodUnchecked0<T>(Object o, String method) {
T _callMethodUnchecked0<T>(Object o, Object method) {
return JS<dynamic>('Object|Null', '#[#]()', o, method);
}
@ -129,7 +129,7 @@ T _callMethodUnchecked0<T>(Object o, String method) {
/// body of this method. Edit `ProgramCompiler.visitStaticInvocation` if you
/// edit this method.
@pragma('dart2js:tryInline')
T _callMethodUncheckedTrustType0<T>(Object o, String method) {
T _callMethodUncheckedTrustType0<T>(Object o, Object method) {
return JS<T>('Object|Null', '#[#]()', o, method);
}
@ -139,7 +139,7 @@ T _callMethodUncheckedTrustType0<T>(Object o, String method) {
/// body of this method. Edit `ProgramCompiler.visitStaticInvocation` if you
/// edit this method.
@pragma('dart2js:tryInline')
T _callMethodUnchecked1<T>(Object o, String method, Object? arg1) {
T _callMethodUnchecked1<T>(Object o, Object method, Object? arg1) {
return JS<dynamic>('Object|Null', '#[#](#)', o, method, arg1);
}
@ -150,7 +150,7 @@ T _callMethodUnchecked1<T>(Object o, String method, Object? arg1) {
/// body of this method. Edit `ProgramCompiler.visitStaticInvocation` if you
/// edit this method.
@pragma('dart2js:tryInline')
T _callMethodUncheckedTrustType1<T>(Object o, String method, Object? arg1) {
T _callMethodUncheckedTrustType1<T>(Object o, Object method, Object? arg1) {
return JS<T>('Object|Null', '#[#](#)', o, method, arg1);
}
@ -161,7 +161,7 @@ T _callMethodUncheckedTrustType1<T>(Object o, String method, Object? arg1) {
/// edit this method.
@pragma('dart2js:tryInline')
T _callMethodUnchecked2<T>(
Object o, String method, Object? arg1, Object? arg2) {
Object o, Object method, Object? arg1, Object? arg2) {
return JS<dynamic>('Object|Null', '#[#](#, #)', o, method, arg1, arg2);
}
@ -173,7 +173,7 @@ T _callMethodUnchecked2<T>(
/// edit this method.
@pragma('dart2js:tryInline')
T _callMethodUncheckedTrustType2<T>(
Object o, String method, Object? arg1, Object? arg2) {
Object o, Object method, Object? arg1, Object? arg2) {
return JS<T>('Object|Null', '#[#](#, #)', o, method, arg1, arg2);
}
@ -184,7 +184,7 @@ T _callMethodUncheckedTrustType2<T>(
/// edit this method.
@pragma('dart2js:tryInline')
T _callMethodUnchecked3<T>(
Object o, String method, Object? arg1, Object? arg2, Object? arg3) {
Object o, Object method, Object? arg1, Object? arg2, Object? arg3) {
return JS<dynamic>(
'Object|Null', '#[#](#, #, #)', o, method, arg1, arg2, arg3);
}
@ -197,7 +197,7 @@ T _callMethodUnchecked3<T>(
/// edit this method.
@pragma('dart2js:tryInline')
T _callMethodUncheckedTrustType3<T>(
Object o, String method, Object? arg1, Object? arg2, Object? arg3) {
Object o, Object method, Object? arg1, Object? arg2, Object? arg3) {
return JS<T>('Object|Null', '#[#](#, #, #)', o, method, arg1, arg2, arg3);
}
@ -207,7 +207,7 @@ T _callMethodUncheckedTrustType3<T>(
/// body of this method. Edit `ProgramCompiler.visitStaticInvocation` if you
/// edit this method.
@pragma('dart2js:tryInline')
T _callMethodUnchecked4<T>(Object o, String method, Object? arg1, Object? arg2,
T _callMethodUnchecked4<T>(Object o, Object method, Object? arg1, Object? arg2,
Object? arg3, Object? arg4) {
return JS<dynamic>(
'Object|Null', '#[#](#, #, #, #)', o, method, arg1, arg2, arg3, arg4);
@ -220,7 +220,7 @@ T _callMethodUnchecked4<T>(Object o, String method, Object? arg1, Object? arg2,
/// body of this method. Edit `ProgramCompiler.visitStaticInvocation` if you
/// edit this method.
@pragma('dart2js:tryInline')
T _callMethodUncheckedTrustType4<T>(Object o, String method, Object? arg1,
T _callMethodUncheckedTrustType4<T>(Object o, Object method, Object? arg1,
Object? arg2, Object? arg3, Object? arg4) {
return JS<T>(
'Object|Null', '#[#](#, #, #, #)', o, method, arg1, arg2, arg3, arg4);

View file

@ -86,7 +86,7 @@ T setProperty<T>(Object o, Object name, T? value) =>
as T;
@patch
T callMethod<T>(Object o, String method, List<Object?> args) => dartifyRaw(
T callMethod<T>(Object o, Object method, List<Object?> args) => dartifyRaw(
callMethodVarArgsRaw(jsifyRaw(o), jsifyRaw(method), jsifyRaw(args))) as T;
@patch

View file

@ -46,7 +46,7 @@ external T setProperty<T>(Object o, Object name, T? value);
// A CFE transformation may optimize calls to `callMethod` when [args] is a
// a list literal or const list containing at most 4 values, all of which are
// statically known to be non-functions.
external T callMethod<T>(Object o, String method, List<Object?> args);
external T callMethod<T>(Object o, Object method, List<Object?> args);
/// Check whether [o] is an instance of [type].
///

View file

@ -371,17 +371,25 @@ class _JSSymbol {
@JS()
external _JSSymbol get symbol;
@JS()
external _JSSymbol get symbol2;
@JS()
external JSString methodWithSymbol(_JSSymbol s);
void symbolTest() {
eval(r'''
var s = Symbol.for('symbol');
globalThis.symbol = s;
globalThis[s] = 'boo';
var s1 = Symbol.for('symbol');
globalThis.symbol = s1;
globalThis[s1] = 'boo';
globalThis.methodWithSymbol = function(s) {
return Symbol.keyFor(s);
}
var symbol2 = Symbol.for('symbolMethod');
globalThis[symbol2] = function() {
return 'hello world';
}
globalThis.symbol2 = symbol2;
''');
Expect.equals(
_JSSymbol.keyFor(_JSSymbol._for('symbol'.toJS)).toDart, 'symbol');
@ -394,6 +402,7 @@ void symbolTest() {
Expect.equals(
_JSSymbol.keyFor(getProperty<_JSSymbol>(globalThis, 'symbol')).toDart,
'symbol');
Expect.equals(callMethod<String>(globalThis, symbol2, []), 'hello world');
}
void main() async {