Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(766)

Unified Diff: lib/src/codegen/js_codegen.dart

Issue 1224083017: Add support for Enums (Closed) Base URL: https://github.com/dart-lang/dev_compiler.git@master
Patch Set: Use interpolation Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/codegen/expect/fieldtest.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/codegen/js_codegen.dart
diff --git a/lib/src/codegen/js_codegen.dart b/lib/src/codegen/js_codegen.dart
index 1eab31a1d0a5ac6acd9e24d86159018afcde097b..3dfbe257d4fe0d3be49cc5232386e3f67b5a27e5 100644
--- a/lib/src/codegen/js_codegen.dart
+++ b/lib/src/codegen/js_codegen.dart
@@ -420,8 +420,57 @@ class JSCodegenVisitor extends GeneralizingAstVisitor {
}
@override
- JS.Statement visitEnumDeclaration(EnumDeclaration node) =>
- _unimplementedCall("Unimplemented enum: $node").toStatement();
+ JS.Statement visitEnumDeclaration(EnumDeclaration node) {
+ var element = node.element;
+ var type = element.type;
+ var name = js.string(type.name);
+ var id = new JS.Identifier(type.name);
+
+ // Generate a class per section 13 of the spec.
+ // TODO(vsm): Generate any accompanying metadata
+
+ // Create constructor and initialize index
+ var constructor =
+ new JS.Method(name, js.call('function(index) { this.index = index; }'));
+ var fields = new List<ConstFieldElementImpl>.from(
+ element.fields.where((f) => f.type == type));
+
+ // Create toString() method
+ var properties = new List<JS.Property>();
+ for (var i = 0; i < fields.length; ++i) {
+ properties.add(new JS.Property(
+ js.number(i), js.string('${type.name}.${fields[i].name}')));
+ }
+ var nameMap = new JS.ObjectInitializer(properties, multiline: true);
+ var toStringF = new JS.Method(js.string('toString'),
+ js.call('function () { return #[this.index]; }', nameMap));
+
+ // Create enum class
+ var classExpr = new JS.ClassExpression(
+ id, _classHeritage(element), [constructor, toStringF]);
+ var result = [js.statement('#', classExpr)];
+
+ // Create static fields for each enum value
+ for (var i = 0; i < fields.length; ++i) {
+ result.add(js.statement('#.# = dart.const(new #(#));', [
+ id,
+ fields[i].name,
+ id,
+ js.number(i)
+ ]));
+ }
+
+ // Create static values list
+ var values = new JS.ArrayInitializer(
+ fields.map((f) => js.call('#.#', [id, f.name])).toList());
+ result.add(js.statement('#.values = dart.const(dart.list(#, #));', [
+ id,
+ values,
+ _emitTypeName(type)
+ ]));
+
+ return _statement(result);
+ }
/// Given a class element and body, complete the class declaration.
/// This handles generic type parameters, laziness (in library-cycle cases),
« no previous file with comments | « no previous file | test/codegen/expect/fieldtest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698