gpt4 book ai didi

dart - Dart VM本身在 `eval`中实现 `dart:mirrors`,开发人员可以使用它。是否计划公开此方法?

转载 作者:行者123 更新时间:2023-12-03 04:09:23 25 4
gpt4 key购买 nike

这是在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)                                    \

附言

我在这里提出问题,因为我无法在Dart邮件列表中提出问题。

附言

如我们所见 static private method中的 mirrors_impl.dart:

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

是否有人希望该方法公开? ( this is not a question but just a thought aloud)。

最佳答案

根据Dart FAQ,即使可能会添加其他动态功能,像这样的纯字符串eval也不太可能将其纳入语言中:

So, for example, Dart isn’t likely to support evaluating a string as code in the current context, but it may support loading that code dynamically into a new isolate. Dart isn’t likely to support adding fields to a value, but it may (through a mirror system) support adding fields to a class, and you can effectively add methods using noSuchMethod(). Using these features will have a runtime cost; it’s important to us to minimize the cost for programs that don’t use them.

This area is still under development, so we welcome your thoughts on what you need from runtime dynamism.

关于dart - Dart VM本身在 `eval`中实现 `dart:mirrors`,开发人员可以使用它。是否计划公开此方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21631974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com