FlutterDart

如何找到flutter external声明方法的实现

2020-12-29  本文已影响0人  HawkFlying
@patch
class 类名 {
  ...
  @patch
  external声明的方法名
  ...
}

external声明的方法,通过@patch注解实现
external关键字介绍请移步flutter external关键字详解

移动端external声明方法实现在vm目录下:

flutter sdk目录/bin/cache/dart-sdk/lib/_internal/vm/lib

web端external声明方法实现在js_runtime目录下:

flutter sdk目录/bin/cache/dart-sdk/lib/_internal/js_runtime/lib

external方法的实现文件一般命名为xxx_patch.dart,如在vm/lib目录下,可以看到各种xxx_patch.dart文件:


patch文件.png

可以在终端通过grep搜索命令找到对应类里external方法实现的xxx_patch.dart文件:

grep -n "class 类名" -r ./* --color=auto

以查找Object类里external方法的实现为例:
1、Object类定义如下:

class Object {
  const Object();
  external bool operator ==(other);
  external int get hashCode;
  external String toString();
  @pragma("vm:entry-point")
  external dynamic noSuchMethod(Invocation invocation);
  external Type get runtimeType;
}

可以看到Object类里有很多方法都是用external声明

2、在flutter sdk目录/bin/cache/dart-sdk/lib/_internal目录下,执行查找class Object命令:

grep -n "class Object" -r ./* --color=auto
查找Object.png

由此可知:web端Object实现文件是./js_runtime/lib/core_patch.dart
移动端Object实现文件是./vm/lib/object_patch.dart

打开web端Object实现文件./js_runtime/lib/core_patch.dart,如下:

// Patch for Object implementation.
@patch
class Object {
  @patch
  bool operator ==(Object other) => identical(this, other);

  @patch
  int get hashCode => Primitives.objectHashCode(this);

  @patch
  String toString() => Primitives.objectToHumanReadableString(this);

  @patch
  dynamic noSuchMethod(Invocation invocation) {
    throw new NoSuchMethodError(this, invocation.memberName,
        invocation.positionalArguments, invocation.namedArguments);
  }

  @patch
  Type get runtimeType => getRuntimeType(this);
}

打开移动端Object实现文件./vm/lib/object_patch.dart,如下:

// part of "core_patch.dart";

@pragma("vm:exact-result-type", "dart:core#_Smi")
int _getHash(obj) native "Object_getHash";
void _setHash(obj, hash) native "Object_setHash";

@patch
@pragma("vm:entry-point")
class Object {
  // The VM has its own implementation of equals.
  @patch
  @pragma("vm:exact-result-type", bool)
  @pragma("vm:prefer-inline")
  bool operator ==(Object other) native "Object_equals";

  // Helpers used to implement hashCode. If a hashCode is used, we remember it
  // in a weak table in the VM (32 bit) or in the header of the object (64
  // bit). A new hashCode value is calculated using a random number generator.
  static final _hashCodeRnd = new Random();

  static int _objectHashCode(obj) {
    var result = _getHash(obj);
    if (result == 0) {
      // We want the hash to be a Smi value greater than 0.
      result = _hashCodeRnd.nextInt(0x40000000);
      do {
        result = _hashCodeRnd.nextInt(0x40000000);
      } while (result == 0);
      _setHash(obj, result);
    }
    return result;
  }

  @patch
  int get hashCode => _objectHashCode(this);
  int get _identityHashCode => _objectHashCode(this);

  @patch
  String toString() native "Object_toString";
  // A statically dispatched version of Object.toString.
  static String _toString(obj) native "Object_toString";

  @patch
  @pragma("vm:entry-point", "call")
  dynamic noSuchMethod(Invocation invocation) {
    // TODO(regis): Remove temp constructor identifier 'withInvocation'.
    throw new NoSuchMethodError.withInvocation(this, invocation);
  }

  @patch
  @pragma("vm:exact-result-type", "dart:core#_Type")
  Type get runtimeType native "Object_runtimeType";

  @pragma("vm:entry-point", "call")
  @pragma("vm:exact-result-type", bool)
  static bool _haveSameRuntimeType(a, b) native "Object_haveSameRuntimeType";

  // Call this function instead of inlining instanceof, thus collecting
  // type feedback and reducing code size of unoptimized code.
  @pragma("vm:entry-point", "call")
  bool _instanceOf(instantiatorTypeArguments, functionTypeArguments, type)
      native "Object_instanceOf";

  // Group of functions for implementing fast simple instance of.
  @pragma("vm:entry-point", "call")
  bool _simpleInstanceOf(type) native "Object_simpleInstanceOf";
  @pragma("vm:entry-point", "call")
  bool _simpleInstanceOfTrue(type) => true;
  @pragma("vm:entry-point", "call")
  bool _simpleInstanceOfFalse(type) => false;
}

可以看到Object里各种external声明方法对应的@patch注解实现方法

上一篇 下一篇

猜你喜欢

热点阅读