Embedding Dart in an executable, still possible?

202 views
Skip to first unread message

Jeff Ward

unread,
Apr 18, 2016, 11:13:36 PM4/18/16
to Dart Misc
I've been trying to embed the Dart VM in an executable for a few days and I haven't been having much luck, and I'm wondering if this is still a supported use case.

It would be nice if the following worked:
  • Pull the dart source and build libdart
  • Link in libdart, include dart_api.h and have that work.
Unfortunately that doesn't work.  But, here's what I've tried and still haven't had much success.
  • Pull the dart source and build the full dart executable
  • link in libdart, libdart_lib, libdart_vm, libdart_vm_precompiled_runtime, and libdouble_conversion
With this approach, I'm able to build, but when the CreateIsolate callback gets called for the "vm-service" isolate, I call "CreateIsolate" which ASSERTS and exits (the assert is "runtime/vm/hash_table.h:378: error: expected: !data_->IsNull()", where the most relevant function in the callstack is Symbols::AddPredefinedSymbolsToIsolate)

It appears that even though the documentation says Dart_Iniitialize should support supplying a NULL for both the vm_isolate_snapshot and that Dart_CreateIsolate should support supplying a NULL isolate_snapshot, this is no longer the case.

So, I added in the generated snapshot files to my project source (dart_sdk_src\sdk\build\DebugX64\obj\global_intermediate\snapshot_gen.cc) and re-ran. This gets past CreateIsolate, but fails attempting to load the core library. ("Dart_LookupLibrary: library 'dart:builtin' not found.").

I've tried various combinations of compiling in generated files and compiled libs but can't find a combination that links cleanly and supplies a version of dart:builtin.

Obviously there's some amount of secret sauce here I'm missing, but I can't figure out the magic combination to make it work.

--
Jeff

Florian Schneider

unread,
Apr 19, 2016, 6:32:45 AM4/19/16
to mi...@dartlang.org
On Tue, Apr 19, 2016 at 5:13 AM, Jeff Ward <je...@fuzzybinary.com> wrote:
I've been trying to embed the Dart VM in an executable for a few days and I haven't been having much luck, and I'm wondering if this is still a supported use case.

It would be nice if the following worked:
  • Pull the dart source and build libdart
  • Link in libdart, include dart_api.h and have that work.
Unfortunately that doesn't work.  But, here's what I've tried and still haven't had much success.
  • Pull the dart source and build the full dart executable
  • link in libdart, libdart_lib, libdart_vm, libdart_vm_precompiled_runtime, and libdouble_conversion

Normally you should only need libdart_vm, and not libdart_vm_precompiled_runtime.
 
With this approach, I'm able to build, but when the CreateIsolate callback gets called for the "vm-service" isolate, I call "CreateIsolate" which ASSERTS and exits (the assert is "runtime/vm/hash_table.h:378: error: expected: !data_->IsNull()", where the most relevant function in the callstack is Symbols::AddPredefinedSymbolsToIsolate)

It appears that even though the documentation says Dart_Iniitialize should support supplying a NULL for both the vm_isolate_snapshot and that Dart_CreateIsolate should support supplying a NULL isolate_snapshot, this is no longer the case.

So, I added in the generated snapshot files to my project source (dart_sdk_src\sdk\build\DebugX64\obj\global_intermediate\snapshot_gen.cc) and re-ran. This gets past CreateIsolate, but fails attempting to load the core library. ("Dart_LookupLibrary: library 'dart:builtin' not found.").

I've tried various combinations of compiling in generated files and compiled libs but can't find a combination that links cleanly and supplies a version of dart:builtin.


Hard to say what exactly is going wrong. Take a close look at runtime/bin/main.cc as an example of how to embed libdart_vm.
 
Obviously there's some amount of secret sauce here I'm missing, but I can't figure out the magic combination to make it work.

--
Jeff

--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Jeff Ward

unread,
Apr 19, 2016, 11:27:19 AM4/19/16
to mi...@dartlang.org
Hey Florian, thanks for the response!

What's in libdart_vm_precompiled_runtime?

Also, is my conclusion about Dart_CreateIsolate no longer supporting NULL vm / isolate snapshots correct?

I did actually made some progress on this last night (by consulting main.cc, I'll outline the fixes at the end for future generations) but still having some issues.

Once I have a hello_world.dart file loaded, I attempt to invoke main using Dart_Invoke, and I'm getting an error message along the lines of "Dart_Invoke expects the library in 'target' to be loaded". I'm not sure what to call to load the "library" other than call Dart_LoadScript (or Dart_LoadLibrary, both do the same thing). I've added a Dart_RunLoop (which main.cc does) but that doesn't seem to fix the issue.

Any thoughts?

How I Fixed My Dumb Issues:
First, I made sure that I'm not doing any extra work fro the vm-service isolate that gets created when you call Dart_Initialize. In the creation callbacks I checked for Dart_IsServiceIssolate and break out if that's the case.
Second, the dart:builtin module is now called dart:_builtin. Loading that library worked.

You received this message because you are subscribed to a topic in the Google Groups "Dart Misc" group.
To unsubscribe from this topic, visit https://groups.google.com/a/dartlang.org/d/topic/misc/BvUbN7qRhX4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to misc+uns...@dartlang.org.

Florian Schneider

unread,
Apr 19, 2016, 11:46:17 AM4/19/16
to mi...@dartlang.org
On Tue, Apr 19, 2016 at 5:26 PM, Jeff Ward <je...@fuzzybinary.com> wrote:
Hey Florian, thanks for the response!

What's in libdart_vm_precompiled_runtime?

libdart_vm_precompiled_runtime is used to run programs from a precompiled instruction snapshot - basically a Dart app compiled into a .so file.
 

Also, is my conclusion about Dart_CreateIsolate no longer supporting NULL vm / isolate snapshots correct?

 
Passing NULL is supported. For example, see runtime/bin/gen_snapshot.cc - it loads all sources from source files instead of a snapshot. Maybe gen_snapshot is actually a better (simpler) example to start from than main.cc -- but instead of creating and writing a snapshot you would invoke main there.
Reply all
Reply to author
Forward
0 new messages