0

Here is code that use this eval method in Dart platform.

This is done via reflection.

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.

I ask question here becuase I cannot ask it at Dart mail lists.

P.S.

As we can see it static private method in mirrors_impl.dart:

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

Does anyone want that this method should be public? (this is not a question but just a thought aloud).

mezoni
  • 10,684
  • 4
  • 32
  • 54

1 Answers1

7

According to the Dart FAQ a pure string eval like that is not likely to make it into the language, even though other dynamic features will likely be added:

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.

Pixel Elephant
  • 20,649
  • 9
  • 66
  • 83
  • Generally it was not a question of knowledge, answering that you could show off their knowledge over the others people. No, it was mostly a question of curiosity. And creative response to it would be more appropriate than a simple citation of information, which essentially gives no answers, but rather tempting to think about "what kind of useful information would the authors of this text (FAQ) eventually bring to all?". As well as the author of this answer. – mezoni Feb 09 '14 at 10:14