<commit_before>// Copyright (c) 2015, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// This library defines the operations that define and manipulate Dart
/// classes.  Included in this are:
///   - Generics
///   - Class metadata
///   - Extension methods
///

// TODO(leafp): Consider splitting some of this out.
part of dart._runtime;

/// Returns a new type that mixes members from base and the mixin.
void applyMixin(to, from) {
  JS('', '#[#] = #', to, _mixin, from);
  var toProto = JS('', '#.prototype', to);
  var fromProto = JS('', '#.prototype', from);
  _copyMembers(toProto, fromProto);
  _mixinSignature(to, from, _methodSig);
  _mixinSignature(to, from, _fieldSig);
  _mixinSignature(to, from, _getterSig);
  _mixinSignature(to, from, _setterSig);
  var mixinOnFn = JS('', '#[#]', from, mixinOn);
  if (mixinOnFn != null) {
    var proto = JS('', '#(#.__proto__).prototype', mixinOnFn, to);
    _copyMembers(toProto, proto);
  }
}

void _copyMembers(to, from) {
  var names = getOwnNamesAndSymbols(from);
  for (int i = 0, n = JS('!', '#.length', names); i < n; ++i) {
    String name = JS('', '#[#]', names, i);
    if (name == 'constructor') continue;
    _copyMember(to, from, name);
  }
  return to;
}

void _copyMember(to, from, name) {
  var desc = getOwnPropertyDescriptor(from, name);
  if (JS('!', '# == Symbol.iterator', name)) {
    // On native types, Symbol.iterator may already be present.
    // TODO(jmesserly): investigate if we still need this.
    // If so, we need to find a better solution.
    // See https://github.com/dart-lang/sdk/issues/28324
    var existing = getOwnPropertyDescriptor(to, name);
    if (existing != null) {
      if (JS('!', '#.writable', existing)) {
        JS('', '#[#] = #.value', to, name, desc);
      }
      return;
    }
  }
  var getter = JS('', '#.get', desc);
  var setter = JS('', '#.set', desc);
  if (getter != null) {
    if (setter == null) {
      var obj = JS<Object>(
          '!',
          '#.set = { __proto__: #.__proto__, '
              'set [#](x) { return super[#] = x; } }',
          desc,
          to,
          name,
          name);
      JS<Object>(
          '!', '#.set = #.set', desc, getOwnPropertyDescriptor(obj, name));
    }
  } else if (setter != null) {
    if (getter == null) {
      var obj = JS<Object>(
          '!',
          '#.get = { __proto__: #.__proto__, '
              'get [#]() { return super[#]; } }',
          desc,
          to,
          name,
          name);
      JS<Object>(
          '!', '#.get = #.get', desc, getOwnPropertyDescriptor(obj, name));
    }
  }
  defineProperty(to, name, desc);
}

void _mixinSignature(to, from, kind) {
  JS('', '#[#] = #', to, kind, () {
    var baseMembers = _getMembers(JS('', '#.__proto__', to), kind);
    var fromMembers = _getMembers(from, kind);
    if (fromMembers == null) return baseMembers;
    var toSignature = JS('', '{ __proto__: # }', baseMembers);
    copyProperties(toSignature, fromMembers);
    return toSignature;
  });
}

final _mixin = JS('', 'Symbol("mixin")');

getMixin(clazz) => JS('', 'Object.hasOwnProperty.call(#, #) ? #[#] : null',
    clazz, _mixin, clazz, _mixin);

final mixinOn = JS('', 'Symbol("mixinOn")');

@JSExportName('implements')
final implements_ = JS('', 'Symbol("implements")');

/// Either `null` if `clazz` doesn't directly implement any interfaces or a
/// list of type objects if it does.  Note, indirectly (e.g., via superclass)
/// implemented interfaces aren't included here.
/// See compiler.dart for when/how it is emitted.
List Function()? getImplements(clazz) => JS(
    '',
    'Object.hasOwnProperty.call(#, #) ? #[#] : null',
    clazz,
    implements_,
    clazz,
    implements_);

/// The Symbol for storing type arguments on a specialized generic type.
final _typeArguments = JS('', 'Symbol("typeArguments")');

final _variances = JS('', 'Symbol("variances")');

final _originalDeclaration = JS('', 'Symbol("originalDeclaration")');

final mixinNew = JS('', 'Symbol("dart.mixinNew")');

/// Normalizes `FutureOr` types when they are constructed at runtime.
///
/// This normalization should mirror the normalization performed at compile time
/// in the method named `_normalizeFutureOr()`.
///
/// **NOTE** Normalization of FutureOr<T?>? --> FutureOr<T?> is handled in
/// [nullable].
normalizeFutureOr(typeConstructor, setBaseClass) {
  // The canonical version of the generic FutureOr type constructor.
  var genericFutureOrType =
      JS('!', '#', generic(typeConstructor, setBaseClass));

  normalize(typeArg) {
    // Normalize raw FutureOr --> dynamic
    if (JS<bool>('!', '# == void 0', typeArg)) return _dynamic;

    // FutureOr<dynamic|void|Object?|Object*|Object> -->
    //   dynamic|void|Object?|Object*|Object
    if (_isTop(typeArg) ||
        _equalType(typeArg, Object) ||
        (_jsInstanceOf(typeArg, LegacyType) &&
            JS<bool>('!', '#.type === #', typeArg, Object))) {
      return typeArg;
    }

    // FutureOr<Never> --> Future<Never>
    if (_equalType(typeArg, Never)) {
      return JS('!', '#(#)', getGenericClassStatic<Future>(), typeArg);
    }
    // FutureOr<Null> --> Future<Null>?
    if (_equalType(typeArg, Null)) {
      return nullable(
          JS('!', '#(#)', getGenericClassStatic<Future>(), typeArg));
    }
    // Otherwise, create the FutureOr<T> type as a normal generic type.
    var genericType = JS('!', '#(#)', genericFutureOrType, typeArg);
    // Overwrite the original declaration so that it correctly points back to
    // this method. This ensures that the we can test a type value returned here
    // as a FutureOr because it is equal to 'async.FutureOr` (in the JS).
    JS('!', '#[#] = #', genericType, _originalDeclaration, normalize);
    JS('!', '#(#)', addTypeCaches, genericType);
    // Add FutureOr specific is and as methods.
    is_FutureOr(obj) =>
        JS<bool>('!', '#.is(#)', typeArg, obj) ||
        JS<bool>(
            '!', '#(#).is(#)', getGenericClassStatic<Future>(), typeArg, obj);
    JS('!', '#.is = #', genericType, is_FutureOr);

    as_FutureOr(obj) {
      // Special test to handle case for mixed mode non-nullable FutureOr of a
      // legacy type. This allows casts like `null as FutureOr<int*>` to work
      // in weak and sound mode.
      if (obj == null && _jsInstanceOf(typeArg, LegacyType)) {
        return obj;
      }

      if (JS<bool>('!', '#.is(#)', typeArg, obj) ||
          JS<bool>('!', '#(#).is(#)', getGenericClassStatic<Future>(), typeArg,
              obj)) {
        return obj;
      }
      return cast(
          obj, JS('!', '#(#)', getGenericClassStatic<FutureOr>(), typeArg));
    }

    JS('!', '#.as = #', genericType, as_FutureOr);

    return genericType;
  }<commit_msg>[ddc] remove extra call of `addTypeCaches`<commit_after>// Copyright (c) 2015, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// This library defines the operations that define and manipulate Dart
/// classes.  Included in this are:
///   - Generics
///   - Class metadata
///   - Extension methods
///

// TODO(leafp): Consider splitting some of this out.
part of dart._runtime;

/// Returns a new type that mixes members from base and the mixin.
void applyMixin(to, from) {
  JS('', '#[#] = #', to, _mixin, from);
  var toProto = JS('', '#.prototype', to);
  var fromProto = JS('', '#.prototype', from);
  _copyMembers(toProto, fromProto);
  _mixinSignature(to, from, _methodSig);
  _mixinSignature(to, from, _fieldSig);
  _mixinSignature(to, from, _getterSig);
  _mixinSignature(to, from, _setterSig);
  var mixinOnFn = JS('', '#[#]', from, mixinOn);
  if (mixinOnFn != null) {
    var proto = JS('', '#(#.__proto__).prototype', mixinOnFn, to);
    _copyMembers(toProto, proto);
  }
}

void _copyMembers(to, from) {
  var names = getOwnNamesAndSymbols(from);
  for (int i = 0, n = JS('!', '#.length', names); i < n; ++i) {
    String name = JS('', '#[#]', names, i);
    if (name == 'constructor') continue;
    _copyMember(to, from, name);
  }
  return to;
}

void _copyMember(to, from, name) {
  var desc = getOwnPropertyDescriptor(from, name);
  if (JS('!', '# == Symbol.iterator', name)) {
    // On native types, Symbol.iterator may already be present.
    // TODO(jmesserly): investigate if we still need this.
    // If so, we need to find a better solution.
    // See https://github.com/dart-lang/sdk/issues/28324
    var existing = getOwnPropertyDescriptor(to, name);
    if (existing != null) {
      if (JS('!', '#.writable', existing)) {
        JS('', '#[#] = #.value', to, name, desc);
      }
      return;
    }
  }
  var getter = JS('', '#.get', desc);
  var setter = JS('', '#.set', desc);
  if (getter != null) {
    if (setter == null) {
      var obj = JS<Object>(
          '!',
          '#.set = { __proto__: #.__proto__, '
              'set [#](x) { return super[#] = x; } }',
          desc,
          to,
          name,
          name);
      JS<Object>(
          '!', '#.set = #.set', desc, getOwnPropertyDescriptor(obj, name));
    }
  } else if (setter != null) {
    if (getter == null) {
      var obj = JS<Object>(
          '!',
          '#.get = { __proto__: #.__proto__, '
              'get [#]() { return super[#]; } }',
          desc,
          to,
          name,
          name);
      JS<Object>(
          '!', '#.get = #.get', desc, getOwnPropertyDescriptor(obj, name));
    }
  }
  defineProperty(to, name, desc);
}

void _mixinSignature(to, from, kind) {
  JS('', '#[#] = #', to, kind, () {
    var baseMembers = _getMembers(JS('', '#.__proto__', to), kind);
    var fromMembers = _getMembers(from, kind);
    if (fromMembers == null) return baseMembers;
    var toSignature = JS('', '{ __proto__: # }', baseMembers);
    copyProperties(toSignature, fromMembers);
    return toSignature;
  });
}

final _mixin = JS('', 'Symbol("mixin")');

getMixin(clazz) => JS('', 'Object.hasOwnProperty.call(#, #) ? #[#] : null',
    clazz, _mixin, clazz, _mixin);

final mixinOn = JS('', 'Symbol("mixinOn")');

@JSExportName('implements')
final implements_ = JS('', 'Symbol("implements")');

/// Either `null` if `clazz` doesn't directly implement any interfaces or a
/// list of type objects if it does.  Note, indirectly (e.g., via superclass)
/// implemented interfaces aren't included here.
/// See compiler.dart for when/how it is emitted.
List Function()? getImplements(clazz) => JS(
    '',
    'Object.hasOwnProperty.call(#, #) ? #[#] : null',
    clazz,
    implements_,
    clazz,
    implements_);

/// The Symbol for storing type arguments on a specialized generic type.
final _typeArguments = JS('', 'Symbol("typeArguments")');

final _variances = JS('', 'Symbol("variances")');

final _originalDeclaration = JS('', 'Symbol("originalDeclaration")');

final mixinNew = JS('', 'Symbol("dart.mixinNew")');

/// Normalizes `FutureOr` types when they are constructed at runtime.
///
/// This normalization should mirror the normalization performed at compile time
/// in the method named `_normalizeFutureOr()`.
///
/// **NOTE** Normalization of FutureOr<T?>? --> FutureOr<T?> is handled in
/// [nullable].
normalizeFutureOr(typeConstructor, setBaseClass) {
  // The canonical version of the generic FutureOr type constructor.
  var genericFutureOrType =
      JS('!', '#', generic(typeConstructor, setBaseClass));

  normalize(typeArg) {
    // Normalize raw FutureOr --> dynamic
    if (JS<bool>('!', '# == void 0', typeArg)) return _dynamic;

    // FutureOr<dynamic|void|Object?|Object*|Object> -->
    //   dynamic|void|Object?|Object*|Object
    if (_isTop(typeArg) ||
        _equalType(typeArg, Object) ||
        (_jsInstanceOf(typeArg, LegacyType) &&
            JS<bool>('!', '#.type === #', typeArg, Object))) {
      return typeArg;
    }

    // FutureOr<Never> --> Future<Never>
    if (_equalType(typeArg, Never)) {
      return JS('!', '#(#)', getGenericClassStatic<Future>(), typeArg);
    }
    // FutureOr<Null> --> Future<Null>?
    if (_equalType(typeArg, Null)) {
      return nullable(
          JS('!', '#(#)', getGenericClassStatic<Future>(), typeArg));
    }
    // Otherwise, create the FutureOr<T> type as a normal generic type.
    var genericType = JS('!', '#(#)', genericFutureOrType, typeArg);
    // Overwrite the original declaration so that it correctly points back to
    // this method. This ensures that the we can test a type value returned here
    // as a FutureOr because it is equal to 'async.FutureOr` (in the JS).
    JS('!', '#[#] = #', genericType, _originalDeclaration, normalize);
    // Add FutureOr specific is and as methods.
    is_FutureOr(obj) =>
        JS<bool>('!', '#.is(#)', typeArg, obj) ||
        JS<bool>(
            '!', '#(#).is(#)', getGenericClassStatic<Future>(), typeArg, obj);
    JS('!', '#.is = #', genericType, is_FutureOr);

    as_FutureOr(obj) {
      // Special test to handle case for mixed mode non-nullable FutureOr of a
      // legacy type. This allows casts like `null as FutureOr<int*>` to work
      // in weak and sound mode.
      if (obj == null && _jsInstanceOf(typeArg, LegacyType)) {
        return obj;
      }

      if (JS<bool>('!', '#.is(#)', typeArg, obj) ||
          JS<bool>('!', '#(#).is(#)', getGenericClassStatic<Future>(), typeArg,
              obj)) {
        return obj;
      }
      return cast(
          obj, JS('!', '#(#)', getGenericClassStatic<FutureOr>(), typeArg));
    }

    JS('!', '#.as = #', genericType, as_FutureOr);

    return genericType;
  }
{'fraction_non_alphanumeric': 0.17887774385811891, 'fraction_numerical': 0.0015990696322139846, 'mean_word_length': 2.9560092006900516, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 67, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4230042', 'n_tokens_mistral': 4434, 'n_tokens_neox': 4318, 'n_words': 1274}