| OLD | NEW |
| (Empty) |
| 1 Sky Module System | |
| 2 ================= | |
| 3 | |
| 4 This document describes the Sky module system. | |
| 5 | |
| 6 Overview | |
| 7 -------- | |
| 8 | |
| 9 The Sky module system is based on the ``import`` element. In its | |
| 10 most basic form, you import a module as follows: | |
| 11 | |
| 12 ```html | |
| 13 <import src="path/to/module.sky" /> | |
| 14 ``` | |
| 15 | |
| 16 As these ``import`` elements are inserted into a module's element | |
| 17 tree, the module's list of outstanding dependencies grows. When an | |
| 18 imported module completes, it is removed from the importing module's | |
| 19 list of outstanding dependencies. | |
| 20 | |
| 21 Before compiling script or inserting an element that is not already | |
| 22 registered, the parser waits until the list of outstanding | |
| 23 dependencies is empty. After the parser has finished parsing, the | |
| 24 module waits until its list of outstanding dependencies is empty | |
| 25 before marking itself complete. | |
| 26 | |
| 27 The ``as`` attribute on the ``import`` element binds a name to the | |
| 28 imported module: | |
| 29 | |
| 30 ```html | |
| 31 <import src="path/to/chocolate.sky" as="chocolate" /> | |
| 32 ``` | |
| 33 | |
| 34 Each module implicitly imports the [Built-In Elements | |
| 35 Module](builtins.md). | |
| 36 | |
| 37 When a module imports another, and the ``import`` element has no | |
| 38 ``as`` attribute, then any elements registered in that module whose | |
| 39 tag names do not begin with an underscore must be registered on the | |
| 40 importing module. (If multiple elements are registered with the same | |
| 41 name, that name gets marked as dead for that module and all the | |
| 42 registrations for that name are discarded.) | |
| 43 | |
| 44 TODO(ianh): decide if elements imported with "as" should be imported | |
| 45 but with the "as" name prefixed, as in ``<foo.button>`` | |
| 46 | |
| 47 | |
| 48 Module API | |
| 49 ---------- | |
| 50 | |
| 51 Each module consists of one or more libraries. The first library in a | |
| 52 module is the *element tree library*, which consists of the following | |
| 53 code for a Sky module: | |
| 54 | |
| 55 ```dart | |
| 56 import 'dart:sky'; | |
| 57 final Module module = new Module(); | |
| 58 ``` | |
| 59 | |
| 60 ...and the following code for a Sky application: | |
| 61 | |
| 62 ```dart | |
| 63 import 'dart:sky'; | |
| 64 final Module module = new Application(); | |
| 65 ``` | |
| 66 | |
| 67 The ``<script>`` elements found in the module's element tree create | |
| 68 the subsequent libraries. Each one first imports the ``dart:mirror`` | |
| 69 library, then the ``dart:sky`` module, then the first library | |
| 70 described above, then all the modules referenced by ``<import>`` | |
| 71 element up to that ``<script>`` element and all the libraries defined | |
| 72 by ``<script>`` elements up to that point, interleaved so as to | |
| 73 maintain the same relative order as those elements were first seen by | |
| 74 the parser. | |
| 75 | |
| 76 When a library imports a module, it actually imports all the libraries | |
| 77 that were declared by that module except the aforementioned element | |
| 78 tree library. If the ``as`` attribute is present on the ``import`` | |
| 79 element, all the libraries are bound to the same name. | |
| 80 | |
| 81 At the end of the ``<script>`` block's source, if it parsed correctly | |
| 82 and completely, the conceptual equivalent of the following code is | |
| 83 appended (but without affecting the library's list of declarations and | |
| 84 without any possibility of it clashing with identifiers described in | |
| 85 the library itself): | |
| 86 | |
| 87 ```dart | |
| 88 class _ { } | |
| 89 void main(ScriptElement script) { | |
| 90 LibraryMirror library = reflectClass(_).owner as LibraryMirror; | |
| 91 if (library.declarations.containsKey(#_init) && library.declarations[#_init] i
s MethodMirror) | |
| 92 _init(script); | |
| 93 AutomaticMetadata.runLibrary(library, module, script); | |
| 94 } | |
| 95 ``` | |
| 96 | |
| 97 Then, that ``main(script)`` function is called, with ``script`` set to | |
| 98 the ``ScriptElement`` object representing the relevant ``<script>`` | |
| 99 element. | |
| 100 | |
| 101 TODO(ianh): decide what URL and name we should give the libraries, as | |
| 102 exposed in MirrorSystem.getName(libraryMirror.qualifiedName) etc | |
| 103 | |
| 104 The ``Module`` class is defined in ``dart:sky`` as follows: | |
| 105 | |
| 106 ```dart | |
| 107 abstract class AbstractModule extends EventTarget { | |
| 108 AbstractModule({this.url, this.elements}); | |
| 109 | |
| 110 final String url; | |
| 111 | |
| 112 final Root elements; // O(1) | |
| 113 // the Root node of the module or application's element tree | |
| 114 | |
| 115 external Future<Module> import(String url); // O(Yikes) | |
| 116 // load and return the URL at the given Module | |
| 117 // if it's already loaded, the future will resolve immediately | |
| 118 // if loading fails, the future will have an error | |
| 119 | |
| 120 external List<Module> getImports(); // O(N) | |
| 121 // returns the Module objects of all the imported modules | |
| 122 | |
| 123 external void registerElement(String tagname, Type elementClass); // O(1) | |
| 124 // registers a tag name with the parser | |
| 125 // only useful during parse time | |
| 126 // verify that tagname isn't null or empty | |
| 127 // verify that elementClass is the Type of a class that extends Element (direc
tly or indirectly, but not via "implements" or "with") | |
| 128 // (see the @tagname code for an example of how to verify that from dart) | |
| 129 // verify that there's not already a class registered for this tag name | |
| 130 // if there is, then mark this tagname is broken, so that it acts as if it's n
ot registered in the parser, | |
| 131 // and, if this is the first time it was marked broken, log a console message
regarding the issue | |
| 132 // (mention the tag name but not the classes, so that it's not observable that
this currently happens out of order) | |
| 133 } | |
| 134 | |
| 135 class Module extends AbstractModule { | |
| 136 Module({String url, Root elements, this.application}) : | |
| 137 super(url: url, elements: elements); // O(1) | |
| 138 final Application application; // O(1) | |
| 139 } | |
| 140 | |
| 141 class Application extends AbstractModule { | |
| 142 Application({String url, Root elements, this.gestureManager}) : | |
| 143 super(url: url, elements: elements); // O(1) | |
| 144 external String get title; // O(1) | |
| 145 external void set title(String newValue); // O(1) | |
| 146 final GestureManager gestureManager; | |
| 147 } | |
| 148 ``` | |
| OLD | NEW |