Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit db378a2

Browse files
sigmundchcommit-bot@chromium.org
authored andcommitted
[ddc] let compiler generate instanceof and == of types directly.
Change-Id: I87cb00cc4592125001d50505e9023c2ab45d95c4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/138722 Commit-Queue: Sigmund Cherem <[email protected]> Reviewed-by: Nicholas Shahan <[email protected]> Reviewed-by: Mark Zhou <[email protected]>
1 parent 617b78a commit db378a2

File tree

5 files changed

+119
-81
lines changed

5 files changed

+119
-81
lines changed

pkg/dev_compiler/lib/src/kernel/compiler.dart

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4755,21 +4755,35 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
47554755
if (target.isFactory) return _emitFactoryInvocation(node);
47564756

47574757
// Optimize some internal SDK calls.
4758-
if (isSdkInternalRuntime(target.enclosingLibrary) &&
4759-
node.arguments.positional.length == 1) {
4760-
var name = target.name.name;
4761-
var firstArg = node.arguments.positional[0];
4762-
if (name == 'getGenericClass' && firstArg is TypeLiteral) {
4763-
var type = firstArg.type;
4764-
if (type is InterfaceType) {
4765-
return _emitTopLevelNameNoInterop(type.classNode, suffix: '\$');
4758+
if (isSdkInternalRuntime(target.enclosingLibrary)) {
4759+
if (node.arguments.positional.length == 1) {
4760+
var name = target.name.name;
4761+
var firstArg = node.arguments.positional[0];
4762+
if (name == 'getGenericClass' && firstArg is TypeLiteral) {
4763+
var type = firstArg.type;
4764+
if (type is InterfaceType) {
4765+
return _emitTopLevelNameNoInterop(type.classNode, suffix: '\$');
4766+
}
4767+
}
4768+
if (name == 'unwrapType' && firstArg is TypeLiteral) {
4769+
return _emitType(firstArg.type);
4770+
}
4771+
if (name == 'extensionSymbol' && firstArg is StringLiteral) {
4772+
return getExtensionSymbolInternal(firstArg.value);
4773+
}
4774+
} else if (node.arguments.positional.length == 2) {
4775+
var name = target.name.name;
4776+
var firstArg = node.arguments.positional[0];
4777+
var secondArg = node.arguments.positional[1];
4778+
if (name == '_jsInstanceOf' && secondArg is TypeLiteral) {
4779+
return js.call('# instanceof #',
4780+
[_visitExpression(firstArg), _emitType(secondArg.type)]);
4781+
}
4782+
4783+
if (name == '_equalType' && secondArg is TypeLiteral) {
4784+
return js.call('# === #',
4785+
[_visitExpression(firstArg), _emitType(secondArg.type)]);
47664786
}
4767-
}
4768-
if (name == 'unwrapType' && firstArg is TypeLiteral) {
4769-
return _emitType(firstArg.type);
4770-
}
4771-
if (name == 'extensionSymbol' && firstArg is StringLiteral) {
4772-
return getExtensionSymbolInternal(firstArg.value);
47734787
}
47744788
}
47754789
if (target == _coreTypes.identicalProcedure) {

sdk_nnbd/lib/_internal/js_dev_runtime/private/ddc_runtime/classes.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,17 @@ normalizeFutureOr(typeConstructor, setBaseClass) {
142142

143143
// FutureOr<dynamic|void|Object?|Object*> --> dynamic|void|Object?|Object*
144144
if (_isTop(typeArg) ||
145-
(_isLegacy(typeArg) &&
145+
(_jsInstanceOf(typeArg, LegacyType) &&
146146
JS<bool>('!', '#.type === #', typeArg, Object))) {
147147
return typeArg;
148148
}
149149

150150
// FutureOr<Never> --> Future<Never>
151-
if (typeArg == never_) {
151+
if (_equalType(typeArg, Never)) {
152152
return JS('!', '#(#)', getGenericClass(Future), typeArg);
153153
}
154154
// FutureOr<Null> --> Future<Null>?
155-
if (typeArg == unwrapType(Null)) {
155+
if (_equalType(typeArg, Null)) {
156156
return nullable(JS('!', '#(#)', getGenericClass(Future), typeArg));
157157
}
158158
// Otherwise, create the FutureOr<T> type as a normal generic type.
@@ -286,7 +286,7 @@ bool isJsInterop(obj) {
286286
// Note that it is still possible to call typed JS interop methods on
287287
// extension types but the calls must be statically typed.
288288
if (JS('!', '#[#] != null', obj, _extensionType)) return false;
289-
return JS('!', '!($obj instanceof $Object)');
289+
return !_jsInstanceOf(obj, Object);
290290
}
291291

292292
/// Get the type of a method from a type using the stored signature

sdk_nnbd/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ _checkAndCall(f, ftype, obj, typeArgs, args, named, displayName) =>
301301
}
302302
303303
// Apply type arguments
304-
if ($ftype instanceof $GenericFunctionType) {
304+
if (${_jsInstanceOf(ftype, GenericFunctionType)}) {
305305
let formalCount = $ftype.formalCount;
306306
307307
if ($typeArgs == null) {
@@ -405,17 +405,27 @@ dindex(obj, index) => callMethod(obj, '_get', null, [index], null, '[]');
405405
dsetindex(obj, index, value) =>
406406
callMethod(obj, '_set', null, [index, value], null, '[]=');
407407

408+
/// General implementation of the Dart `is` operator.
409+
///
410+
/// Some basic cases are handled directly by the `.is` methods that are attached
411+
/// directly on types, but any query that requires checking subtyping relations
412+
/// is handled here.
408413
@notNull
409414
@JSExportName('is')
410415
bool instanceOf(obj, type) {
411416
if (obj == null) {
412-
return identical(type, unwrapType(Null)) ||
417+
return _equalType(type, Null) ||
413418
_isTop(type) ||
414-
_isNullable(type);
419+
_jsInstanceOf(type, NullableType);
415420
}
416421
return isSubtypeOf(getReifiedType(obj), type);
417422
}
418423

424+
/// General implementation of the Dart `as` operator.
425+
///
426+
/// Some basic cases are handled directly by the `.as` methods that are attached
427+
/// directly on types, but any query that requires checking subtyping relations
428+
/// is handled here.
419429
@JSExportName('as')
420430
cast(obj, type, @notNull bool isImplicit) {
421431
// We hoist the common case where null is checked against another type here

sdk_nnbd/lib/_internal/js_dev_runtime/private/ddc_runtime/rtti.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ getReifiedType(obj) {
8888
switch (JS<String>('!', 'typeof #', obj)) {
8989
case "object":
9090
if (obj == null) return JS('', '#', Null);
91-
if (JS('!', '# instanceof #', obj, Object)) {
91+
if (_jsInstanceOf(obj, Object)) {
9292
return JS('', '#.constructor', obj);
9393
}
9494
var result = JS('', '#[#]', obj, _extensionType);

0 commit comments

Comments
 (0)