DART VM本身在“ Dart:Mirrors”中实现了`eart',开发人员使用它.计划公开这种方法



这是在DART平台中使用此eval方法的代码。

这是通过反射完成的。

Runtime/Lib/Mirrors_impl.dart

_getFieldSlow(unwrapped) {
      // ..... Skipped  
      var atPosition = unwrapped.indexOf('@');
      if (atPosition == -1) {
        // Public symbol.
        f = _eval('(x) => x.$unwrapped', null);
      } else {
        // Private symbol.
        var withoutKey = unwrapped.substring(0, atPosition);
        var privateKey = unwrapped.substring(atPosition);
        f = _eval('(x) => x.$withoutKey', privateKey);
      }
      // ..... Skipped
  }
  static _eval(expression, privateKey)
      native "Mirrors_evalInLibraryWithPrivateKey";

Runtime/Lib/Mirrors.cc

DEFINE_NATIVE_ENTRY(Mirrors_evalInLibraryWithPrivateKey, 2) {
  GET_NON_NULL_NATIVE_ARGUMENT(String, expression, arguments->NativeArgAt(0));
  GET_NATIVE_ARGUMENT(String, private_key, arguments->NativeArgAt(1));
  const GrowableObjectArray& libraries =
      GrowableObjectArray::Handle(isolate->object_store()->libraries());
  const int num_libraries = libraries.Length();
  Library& each_library = Library::Handle();
  Library& ctxt_library = Library::Handle();
  String& library_key = String::Handle();
  if (library_key.IsNull()) {
    ctxt_library = Library::CoreLibrary();
  } else {
    for (int i = 0; i < num_libraries; i++) {
      each_library ^= libraries.At(i);
      library_key = each_library.private_key();
      if (library_key.Equals(private_key)) {
        ctxt_library = each_library.raw();
        break;
      }
    }
  }
  ASSERT(!ctxt_library.IsNull());
  return ctxt_library.Evaluate(expression);

Runtime/vm/bootstrap_natives.h

V(Mirrors_evalInLibraryWithPrivateKey, 2)                                    

P.S。

我在这里问问题,因为我不能在Dart Mail列表中询问。

P.S。

正如我们在mirrors_impl.dart中看到的static private method

static _eval(expression, privateKey) native "Mirrors_evalInLibraryWithPrivateKey";

有人希望这种方法应该公开吗?(this is not a question but just a thought aloud)。

根据飞镖常见问题的纯字符串评估,即使可能会添加其他动态功能:

因此,例如,Dart不太可能支持评估字符串为 在当前上下文中的代码,但它可能支持加载该代码 动态成一个新的分离株。飞镖不太可能支持添加 字段达到一个值,但它可以(通过镜像系统)支持添加 一堂课的字段,您可以使用 nosuchmethod()。使用这些功能将具有运行时的成本;它是 对我们来说很重要,以最大程度地减少不使用程序的程序的成本。

该领域仍在开发中,因此我们欢迎您对 从运行时动态中需要什么。

最新更新