Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an API to directly execute dart code? #6

Open
arcticfox1919 opened this issue Mar 20, 2023 · 1 comment
Open

Add an API to directly execute dart code? #6

arcticfox1919 opened this issue Mar 20, 2023 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@arcticfox1919
Copy link

First of all thank you very much for your work, I happen to have the need to embed the Dart VM to execute the code. Here is the code I read in dart_api_impl_test.cc and it seems that the VM can execute Dart code directly in string form:

TEST_CASE(DartAPI_InstanceOf) {
  const char* kScriptChars =
      "class OtherClass {\n"
      "  static returnNull() { return null; }\n"
      "}\n"
      "class InstanceOfTest {\n"
      "  InstanceOfTest() {}\n"
      "  static InstanceOfTest testMain() {\n"
      "    return new InstanceOfTest();\n"
      "  }\n"
      "}\n";
  Dart_Handle result;
  // Create a test library and Load up a test script in it.
  Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);

  // Fetch InstanceOfTest class.
  Dart_Handle type =
      Dart_GetNonNullableType(lib, NewString("InstanceOfTest"), 0, NULL);
  EXPECT_VALID(type);

  // Invoke a function which returns an object of type InstanceOf..
  Dart_Handle instanceOfTestObj =
      Dart_Invoke(type, NewString("testMain"), 0, NULL);
  EXPECT_VALID(instanceOfTestObj);

}

Can you add an API like TestCase::LoadTestScript?

@fuzzybinary
Copy link
Owner

Hi @arcticfox1919 ,

I did a brief look and it looks possible, but will need to take advantage of some internal methods. This is really the magic method that's doing most of the work.

So with some work it could be probably be added, and I'd accept a PR to do so, but it's not in my use case so I likely won't get around to it any time soon.

@fuzzybinary fuzzybinary added enhancement New feature or request help wanted Extra attention is needed labels Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants