@@ -983,31 +983,67 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
983983
984984 if (c.enclosingLibrary == _coreTypes.asyncLibrary &&
985985 c == _coreTypes.futureOrClass) {
986- var typeParam =
987- TypeParameterType (c.typeParameters[0 ], Nullability .legacy);
988- var typeT = visitTypeParameterType (typeParam);
989- var futureOfT = visitInterfaceType (InterfaceType (
990- _coreTypes.futureClass, Nullability .legacy, [typeParam]));
991- body.add (js.statement ('''
992- #.is = function is_FutureOr(o) {
993- return #.is(o) || #.is(o);
994- }
995- ''' , [className, typeT, futureOfT]));
996- // TODO(jmesserly): remove the fallback to `dart.as`. It's only for the
997- // _ignoreTypeFailure logic.
998- body.add (js.statement ('''
999- #.as = function as_FutureOr(o) {
1000- if (o == null || #.is(o) || #.is(o)) return o;
1001- return #.as(o, this, false);
1002- }
1003- ''' , [className, typeT, futureOfT, runtimeModule]));
1004- body.add (js.statement ('''
1005- #._check = function check_FutureOr(o) {
1006- if (o == null || #.is(o) || #.is(o)) return o;
1007- return #.as(o, this, true);
1008- }
1009- ''' , [className, typeT, futureOfT, runtimeModule]));
1010- return null ;
986+ // These methods are difficult to place in the runtime or patch files.
987+ // * They need to be callable from the class but they can't be static
988+ // methods on the FutureOr class in Dart because they reference the
989+ // generic type parameter.
990+ // * There isn't an obvious place in dart:_runtime were we could place a
991+ // method that adds these type tests (similar to addTypeTests()) because
992+ // in the bootstrap ordering the Future class hasn't been defined yet.
993+ if (_options.nonNullableEnabled) {
994+ // TODO(nshahan) Update FutureOr type tests for NNBD
995+ var typeParam =
996+ TypeParameterType (c.typeParameters[0 ], Nullability .legacy);
997+ var typeT = visitTypeParameterType (typeParam);
998+ var futureOfT = visitInterfaceType (InterfaceType (
999+ _coreTypes.futureClass, Nullability .legacy, [typeParam]));
1000+ body.add (js.statement ('''
1001+ #.is = function is_FutureOr(o) {
1002+ return #.is(o) || #.is(o);
1003+ }
1004+ ''' , [className, typeT, futureOfT]));
1005+ // TODO(jmesserly): remove the fallback to `dart.as`. It's only for the
1006+ // _ignoreTypeFailure logic.
1007+ body.add (js.statement ('''
1008+ #.as = function as_FutureOr(o) {
1009+ if (o == null || #.is(o) || #.is(o)) return o;
1010+ return #.as(o, this, false);
1011+ }
1012+ ''' , [className, typeT, futureOfT, runtimeModule]));
1013+ body.add (js.statement ('''
1014+ #._check = function check_FutureOr(o) {
1015+ if (o == null || #.is(o) || #.is(o)) return o;
1016+ return #.as(o, this, true);
1017+ }
1018+ ''' , [className, typeT, futureOfT, runtimeModule]));
1019+ return null ;
1020+ } else {
1021+ var typeParam =
1022+ TypeParameterType (c.typeParameters[0 ], Nullability .legacy);
1023+ var typeT = visitTypeParameterType (typeParam);
1024+ var futureOfT = visitInterfaceType (InterfaceType (
1025+ _coreTypes.futureClass, Nullability .legacy, [typeParam]));
1026+ body.add (js.statement ('''
1027+ #.is = function is_FutureOr(o) {
1028+ return #.is(o) || #.is(o);
1029+ }
1030+ ''' , [className, typeT, futureOfT]));
1031+ // TODO(jmesserly): remove the fallback to `dart.as`. It's only for the
1032+ // _ignoreTypeFailure logic.
1033+ body.add (js.statement ('''
1034+ #.as = function as_FutureOr(o) {
1035+ if (o == null || #.is(o) || #.is(o)) return o;
1036+ return #.as(o, this, false);
1037+ }
1038+ ''' , [className, typeT, futureOfT, runtimeModule]));
1039+ body.add (js.statement ('''
1040+ #._check = function check_FutureOr(o) {
1041+ if (o == null || #.is(o) || #.is(o)) return o;
1042+ return #.as(o, this, true);
1043+ }
1044+ ''' , [className, typeT, futureOfT, runtimeModule]));
1045+ return null ;
1046+ }
10111047 }
10121048
10131049 body.add (runtimeStatement ('addTypeTests(#)' , [className]));
0 commit comments