In my application I generate big dart classes. Right now I compile them on the server, which takes CPU time. It would be much better to compile the Dart code within the browser. The code is then loaded via spawnURI.
Is it possible to invoke the dart2js compiler from within Dart code in a supported way as it is done in try.dartlang.org or do I need to copy the compiler into my project?
Compiling Dart to JavaScript will be faster on the server, because you can run dart2js via Dart VM. try.dartlang.org is running in a special version of dart2js, which has not been merged into the main source code.
Related
Is there a way to get compiled down version of a dart code for a particular target? Say the following code compiled for Dart VM.
I'm new to Dart and quite often come across high level abstract code and wonder how it is translated in VM.
names.forEach(print);
Dart is not compiled to something like Java-bytecode or DotNet IL.
Dart is compiled to machine code by the VM either ahead of time (AoT) or just in time (JiT)
https://mrale.ph/dartvm/
The name "Dart VM" is historical. Dart VM is a virtual machine in a
sense that it provides an execution environment for a high-level
programming language, however it does not imply that Dart is always
interpreted or JIT-compiled, when executing on Dart VM. For example,
Dart code can be compiled into machine code using Dart VM AOT pipeline
and then executed within a stripped version of the Dart VM, called
precompiled runtime, which does not contain any compiler components
and is incapable of loading Dart source code dynamically.
Dart 2 uses Kernel AST though generated by the common front-end (CFE)
There is some abstraction happening from the Dart language though
https://github.com/dart-lang/sdk/blob/master/pkg/kernel/README.md
Dart Kernel is a small high-level language derived from Dart. It is
designed for use as an intermediate format for whole-program analysis
and transformations, and to be consumed by codegen and execution
backends.
The kernel language has an in-memory representation in Dart and can be
serialized as binary or text.
Both the kernel language and its implementations are unstable and are under development.
See also https://github.com/dart-lang/sdk/blob/master/pkg/kernel/binary.md
As of this year, how do I test google dart for web development?
I downloaded a special chromium version and the dart sdk. I know I can convert dart code to javascript as well but is there any way for dart to directly interact with my html code? Like append dart in my html script and let it do its job.
Im coming from javascript and I really want to learn this language and support its growth.
Dartium is
a special Chromium version, that can execute Dart code directly
shipped with the Dart SDK.
for development only, for faster edit-reload cycles
For production Dart always needs to be compiled to JS.
I want to compile dart code to JS on-the-fly without invoking dart2js at the command line. Eg., (written in Dart) read in some dart code from a file and transform it to JS (must be in memory, filesystem is not writable).
I thought maybe dart2js would effectively just be a cli over a pub package I can call manually, but I can't find any information on doing this at runtime :(
(note: I know this idea sucks and it'll be very slow; it's just for something I'm prototyping and will ultimately use dart2js normally, I just can't address that yet)
https://try-dart-lang.appspot.com/ does this. The source is available. Its basically dart2js run through dart2js.
Not sure if this is the right repository https://github.com/peter-ahe-google/orphan-try
I guess Peter would be ok with pinging him about more information.
The project was replaced by pub.dartlang.org which uses a service running on the server https://github.com/dart-lang/dart-services where source is posted to for dart2js translation.
A few questions about code generation with Dart:
Can dart generate a class at runtime with Dart for code running on the Dart VM?
Is there any sort of code generation equivalent to Java annotation processing at compile / pre run time?
1) Dart VM and dart2js don't support generating code at runtime.
One workaround is to generate code to a file and load it in a new isolate (can also be a data URI). The application can communicate with the generated code running in another isolate only by message passing.
2) This is what transformers are for, but transformers are only applied to dart2js (or pub serve at development time), but not to code run on the server side Dart VM.
https://github.com/dart-lang/build (currently only in developer preview) can be used for code generation for browser and server-side code. It's a tool that monitors source files and updates generated code when source file change.
I've now discovered that the dart team has a library for generating sources. It seems useful for generating sources pre-compile time: https://pub.dartlang.org/packages/source_gen
I have played a little with Dart and I think it's great. I understand that it can output native JS and that the VM will likely be supported by Google in their browser. As it is possible that other browser suppliers won't support the Dart VM, is it at all possible to install the Dart VM on client machines for use in foreign browsers?
is it at all possible to install the Dart VM on client machines for use in foreign browsers?
It is, however it is easier to supply the Dart VM yourself.
Javascript is perfectly able to:
Find a script of a specific type
Convert the script into Javascript
Execute the compiled script
While this is technically not a Dart Virtual Machine, it will get your Dart code executed at full speed. However, you do have to wait for the compilation to complete. The usual way is to do the compilation on the server (once), and only send the compiled javascript to clients.
Another option is interpreted code. Instead of compiling to javascript, the Dart instructions are executed one-by-one. Dart is not a machine-level language, so it needs parsing, but what follows is interpretation. The downside is reduced performance. This will get you as close to having a full-blown virtual machine (separate from the Javascript one) as possible.
Normally, you don't care which one you get (maybe you'll even get a just-in-time compiler), but it does make a difference in terms of a Dart virtual machine being present (rather than just getting your code executed).
The Dart compiler needs to be present on the page somehow (unless you precompile).
The easiest way is to just write <script src="path/to/your/dart-compiler.js"></script> into the head.
The Dartium browser does support Dart natively, but it is not designed for common use. Wikipedia says:
In the Dartium Browser: The Dart SDK ships with a version of the Chromium web browser modified to include a Dart virtual machine. This browser can run Dart code directly without compilation to Javascript. It is currently not intended for general-purpose use, but rather as a development tool for Dart applications.[7] When embedding Dart code into web apps, the current recommended procedure is to load a bootstrap JavaScript file, "dart.js", which will detect the presence or absence of the Dart VM and load the corresponding Dart or compiled Javascript code, respectively,[8] therefore guaranteeing browser compatibility with or without the custom Dart VM.
If you want the ability to run Dart be dependent on the client machine rather than on the page, there are a few ways too.
One way is to include the compiler as a user-script. This will work in all both modern desktop browsers. However, I'm not sure if there's an existing way to add user-script support to Internet Explorer.
One way is to add a browser extension. All modern desktop browsers support extensions, and Internet Explorer has Browser Helper Objects.
All of these will require the extra Javascript step. If you want native interpretation that bypasses Javascript, you need a plugin. Plugins require a specific mime-type to run (not sure if the script type counts), but you can install an extension that will trigger the use of the plugin. However, DOM manipulation still needs the extra Javascript step. There is no way around it.
A desktop installer can definitely install a plugin into a browser. Indeed, this is the way plugins normally get installed. Installing extensions from a desktop installer might be possible as well, but I can't confirm or deny this last claim for now.
As far as I know there is no way to just simply install a plugin (like Flash) for Dart. For Internet Explorer one could install Chrome-frame, but I haven't seen something similar for Firefox and Safari.