Dart (programming language): Difference between revisions

Content deleted Content added
m Deploying apps: <syntaxhighlight lang="console">
m Fixed the formatting of several paragraphs
Line 114:
 
==== JavaScript ====
: To run in mainstream [[web browser]]s, Dart relies on a [[source-to-source compiler]] to [[JavaScript]]. This makes Dart apps compatible with all major browsers. Dart optimizes the compiled JavaScript output to avoid expensive checks and operations. This results in JavaScript code that can run faster than equivalent code handwritten in plain JavaScript.<ref name="dartlang.org">{{cite web |url=http://www.dartlang.org/slides/2012/10/jsconfeu/javascript-as-compilation-target-florian-loitsch.pdf |title=JavaScript as a compilation target: Making it fast |publisher=Dartlang.org |access-date=2013-08-18 |archive-url=https://web.archive.org/web/20160702204820/http://www.dartlang.org/slides/2012/10/jsconfeu/javascript-as-compilation-target-florian-loitsch.pdf |archive-date=2016-07-02}}</ref>
 
: The first Dart-to-JavaScript compiler was <code>dartc</code>. It was deprecated in Dart 2.0.
:The first Dart-to-JavaScript compiler was <code>dartc</code>. It was deprecated in Dart 2.0.

The second Dart-to-JavaScript compiler was frog.<ref>{{Cite web |title=Towards a single Dart to JavaScript compiler |date=10 September 2019 |url=https://news.dartlang.org/2012/02/towards-single-dart-to-javascript.html |access-date=2023-05-13 |language=en}}</ref> Written in Dart, it was introduced in 2013 and deprecated in 2020. This should not be confused with Dart Frog,<ref>{{Cite web |title=Dart Frog |url=https://dartfrog.vgv.dev/ |access-date=2023-05-13 |website=dartfrog.vgv.dev |language=en}}</ref> an open-source Dart framework for building backend systems from [[Very Good Ventures]].
 
:The third Dart-to-JavaScript compiler is <code>dart2js</code>. Introduced in Dart 2.0,<ref>{{Cite web |last=Moore |first=Kevin |date=2018-08-08 |title=Announcing Dart 2 Stable and the Dart Web Platform |url=https://medium.com/dartlang/dart-2-stable-and-the-dart-web-platform-3775d5f8eac7 |access-date=2023-05-13 |website=Dart |language=en}}</ref> the Dart-based <code>dart2js</code> evolved from earlier compilers. It intended to implement the full Dart language specification and semantics. Developers use this compiler for production builds. It compiles to [[Minification (programming)|minified JavaScript]].
The third Dart-to-JavaScript compiler is <code>dart2js</code>. Introduced in Dart 2.0,<ref>{{Cite web |last=Moore |first=Kevin |date=2018-08-08 |title=Announcing Dart 2 Stable and the Dart Web Platform |url=https://medium.com/dartlang/dart-2-stable-and-the-dart-web-platform-3775d5f8eac7 |access-date=2023-05-13 |website=Dart |language=en}}</ref> the Dart-based <code>dart2js</code> evolved from earlier compilers. It intended to implement the full Dart language specification and semantics. Developers use this compiler for production builds. It compiles to [[Minification (programming)|minified JavaScript]].

The fourth Dart-to-JavaScript compiler is <code>dartdevc</code>.<ref>{{Cite web |title=dartdevc: The Dart development compiler |url=https://dart.dev/tools/dartdevc.html |access-date=2023-05-13 |website=dart.dev |language=en }}{{Dead link|date=April 2024 |bot=InternetArchiveBot |fix-attempted=yes }}</ref> Developers could use this compiler for development builds. It compiles to human-readable JavaScript. On March 28, 2013, the Dart team posted an update on their blog addressing Dart code compiled to JavaScript with the <code>dart2js</code> compiler,<ref>{{cite web |last=Ladd |first=Seth |date=2013-03-28 |title=Dart News & Updates: Why dart2js produces faster JavaScript code from Dart |url=http://news.dartlang.org/2013/03/why-dart2js-produces-faster-javascript.html |access-date=2013-07-21 |website=News.dartlang.org.}}</ref> stating that it now runs faster than handwritten JavaScript on [[V8 (JavaScript engine)|Chrome's V8 JavaScript engine]] for the DeltaBlue benchmark.<ref>{{cite web |title=Dart Performance |url=http://www.dartlang.org/performance/ |url-status=dead |archive-url=https://web.archive.org/web/20170103041945/http://www.dartlang.org/performance/ |archive-date=2017-01-03 |access-date=2013-07-21 |website=Dartlang.org.}}</ref>
 
:Prior to Dart 2.18, both <code>dart2js</code> and <code>dartdevc</code> could be called from the command line. Dart 2.18 folded these functions into the Dart SDK. This removed the direct command line wrappers but kept the two compilers. The <code>webdev serve</code> command calls the <code>dartdevc</code> compiler. The <code>webdev build</code> command calls the <code>dart2js</code> compiler.
:The Dart SDK compiles to JavaScript in two ways.
 
:To debug code, run <code>webdev serve</code> to compile a larger JavaScript file with human-readable code. Dart-generated JavaScript can be debugged using [[Google Chrome|Chrome]] only.
:The Dart SDK compiles to JavaScript in two ways.
 
:To debug code, run <code>webdev serve</code> to compile a larger JavaScript file with human-readable code. Dart-generated JavaScript can be debugged using [[Google Chrome|Chrome]] only.
<syntaxhighlight lang="text">
$ cd <dart_app_directory>
Line 132 ⟶ 139:
 
==== WebAssembly ====
 
:With the Dart 3.22 release, Google announced support for compiling Dart code to [[WebAssembly]].<ref name=":3" /> Full support for [[WebAssembly|Wasm]] requires adoption of the WasmGC<ref>{{Citation |title=GC Proposal for WebAssembly |date=2023-05-12 |url=https://github.com/WebAssembly/gc/blob/5431d631547c8af09a6377e29fee5126219f33c5/proposals/gc/Overview.md |access-date=2023-05-13 |publisher=WebAssembly}}</ref> feature into the Wasm standard. Chrome 119<ref>{{Cite web |title=WebAssembly Garbage Collection (WasmGC) now enabled by default in Chrome {{!}} Blog |url=https://developer.chrome.com/blog/wasmgc |access-date=2024-05-17 |website=Chrome for Developers |language=en}}</ref> supports WasmGC. [[Firefox]]<ref>{{Cite web |date=2023-02-16 |title=SpiderMonkey Newsletter (Firefox 110-111) |url=https://spidermonkey.dev/blog/2023/02/16/newsletter-firefox-110-111.html |access-date=2023-05-13 |website=SpiderMonkey JavaScript/WebAssembly Engine |language=en-US}}</ref> 120 and later could support WasmGC, but a current bug is blocking compatibility.<ref>{{Cite web |title=1788206 - OffscreenCanvas.transferToImageBitmap incurs a copy |url=https://bugzilla.mozilla.org/show_bug.cgi?id=1788206 |access-date=2024-05-17 |website=bugzilla.mozilla.org |language=en}}</ref> [[Safari (web browser)|Safari]]<ref>{{Cite web |title=Safari Technology Preview 167 Release Notes |url=https://docs.developer.apple.com/documentation/safari-technology-preview-release-notes/stp-release-167 |access-date=2023-05-13 |website=Apple Developer Documentation |language=en}}</ref> and [[Microsoft Edge]] are integrating WasmGC support.
 
=== Deploying to native platforms ===
Line 138 ⟶ 146:
 
==== Self-contained executable ====
 
:Self-contained executables include native machine code compiled from the specified Dart code file, its dependencies, and a small Dart runtime. The runtime handles type checking and garbage collection. The compiler produces output specific to the architecture on which the developer compiled it. This file can be distributed as any other native executable.
<syntaxhighlight lang="console">
$ dart compile exe "source.dart" -o "target_app"
Line 146 ⟶ 155:
 
==== Ahead-of-time module ====
:When [[AOT compilation|compiled ahead of time]],<ref>{{Cite web |last=Obinna |first=Onuoha |date=2020-04-07 |title=How does JIT and AOT work in Dart? |url=https://onuoha.medium.com/how-does-jit-and-aot-work-in-dart-cab2f31d9cb5 |access-date=2023-06-20 |website=Medium |language=en}}</ref> Dart code produces performant and platform-specific modules. It includes all dependent libraries and packages the app needs. This increases its compilation time. The compiler outputs an app specific to the architecture on which it was compiled.
<syntaxhighlight lang="console">
$ dart compile aot-snapshot "source.dart"
Line 154 ⟶ 163:
 
==== Just-in-time module ====
:When [[Just-in-time compilation|compiled just in time]], Dart code produces performant modules that compile fast. This module needs the Dart VM included with the SDK to run. The compiler loads all parsed classes and compiled code into memory the first time the app runs. This speeds up any subsequent run of the app. The compiler outputs an app specific to the architecture on which it was compiled.
<syntaxhighlight lang="console">
$ dart compile jit-snapshot "source.dart"
Line 164 ⟶ 173:
 
==== Dart kernel module ====
:When compiled as a kernel module, Dart code produces a machine-independent format called the Dart Intermediate Representation (Dart IR). The Dart IR bytecode format can work on any architecture that has a Dart VM. This makes this format very portable and quick to compile, but less performant than other compilation outputs.
<syntaxhighlight lang="console">
$ dart compile kernel "source.dart"