Monday, 10 October 2011

Where can I Get the source

A brief summary on how to access the source code. You can also browse the repository or list the changes.
There are certain pre-requisites required for building Dart. If your machine is already setup to build Chromium, then you may skip this step. Otherwise prepare your machine according to these instructions - see PreparingYourMachine .
Committers: For all instructions below use https instead of http. Also use your Google Code Password when prompted.

Getting the source

Create a directory into which you want to checkout the Dart sources and change into that directory.

Getting everything

gclient config http://dart.googlecode.com/svn/branches/bleeding_edge/deps/all.deps
gclient sync
For instructions how to build and test refer to "Build everything".

Getting the standalone VM

gclient config http://dart.googlecode.com/svn/branches/bleeding_edge/deps/standalone.deps
gclient sync

HTMLConverter for DART

To make it easier to develop web applications in Dart, we propose adding a new mime type (application/dart) for HTML documents that allows embedding dart scripts. We wrote a script (htmlconverter.py) that will translate an HTML page with these new mime types into an HTML page with JavaScript code.

Using htmlconverter.py

To invoke this script, ensure that you have a checkout of the project, and that you have built the dartc compiler (see instructions in Building).
Then you can invoke the script as follows:
$ python dart/client/tools/htmlconverter.py input.html -o out/
The script will do the following:
  1. Invoke the dartc compiler on any code in the page, embedding the result on a script tag.
  2. Inline any css code
  3. Within css, embed images as a data-url using a base64 encoding.
If all images in your app are referenced from css files, the end result is a monolithic html file that can be used to deploy your application. The Dart code in your page will be executed asynchronously when the browser triggers the DOMContentLoaded event.

Work in progress

The following is still pending to be implemented in htmlconverter.py:
  • Support heavy isolates: currently the tool inlines all script tags in the html page. Heavy isolates can only be spawned from scripts that are loaded via a URL (a restriction derived from web workers).
  • Implement support for multiple isolates in the HTML page. This includes:
    • Allow multiple script tags in a page, mapping each script to an isolate.
    • Introduce an isolate discovery mechanism (e.g. a registry), so that top-level isolates don’t have to use window.postMessage to communicate.

Libraries

Dart will provide the following libraries to support web and web server development:
Core Library
Contains interfaces to support common data structures and operations.
DOM Library
Contains interfaces to the HTML5 DOM, loosely based on the proposed HTML5 standard as specified by the W3C/WHATWG. These interfaces will evolve in parallel with the HTML5 standards.
Here is an overview of the interface hierarchy for the current Core Library (subject to change):

MIME type

You will be able to embed a Dart program directly on an HTML page, or you will be able to use a #import or #source statement to pull in external files. Here is the proposed new MIME type, “application/dart”:
<html>
  <body>
    <script type="application/dart">
      main() {
        Element element = document.getElementById('message');
        element.innerHTML = 'Hello from Dart';
      }     
    </script>
    <div id="message"></div>
  </body>
</html> 

Run Dart code in several ways

You will be able to run Dart code in several ways:
  • Translate Dart code to JavaScript that can run in any modern browser:
    Chrome, Safari 5+, and Firefox 4+ (more browser support coming shortly).
  • Execute Dart code directly in a VM on the server side
  • Use Dartboard to write, modify, and execute small Dart programs within any browser window

Optional types

Dart provides, at the programmer’s option, a mixture of static and dynamic checking. When experimenting, the programmer can write untyped code for simple prototyping. As the application becomes larger and more stable, types can be added to aid debugging and impose structure where desired.
For example, here is a sample of untyped code in Dart that creates a new Point class that has parameters x and y and two methods: scale() and distance().
 
class Point {
  var x, y;
  Point(this.x, this.y);
  scale(factor) => new Point(x*factor, y*factor);
  distance() => Math.sqrt(x*x + y*y);
}

main() {
  var a = new Point(2,3).scale(10);
  print(a.distance());
}

Here is what this code looks like with types added that ensure that x, y, and factor are of type num, and that a Point contains two values of type num:

class Point {
  num x, y;
  Point(num this.x, num this.y);
  Point scale(num factor) => new Point(x*factor, y*factor);
  num distance() => Math.sqrt(x*x + y*y);
}

void main() {
  Point a = new Point(2,3).scale(10);
  print(a.distance());
}

Classes and interfaces

Dart’s interfaces and classes provide you with a set of reusable and extensible building blocks. An interface defines a basic set of methods and constants, sometimes by inheriting from other interfaces. A class can implement multiple interfaces, but it only inherits from a single superclass.
The following example defines an interface, along with a class and subclass that implement it:

interface Shape {
  num perimeter();
}

class Rectangle implements Shape {
  final num height, width; 
  Rectangle(num this.height, num this.width);  // Compact constructor syntax.
  num perimeter() => 2*height + 2*width;       // Short function syntax.
}

class Square extends Rectangle {
  Square(num size) : super(size, size);
}