OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dev_compiler.src.codegen.js_codegen; | 5 library dev_compiler.src.codegen.js_codegen; |
6 | 6 |
7 import 'dart:collection' show HashSet, HashMap, SplayTreeSet; | 7 import 'dart:collection' show HashSet, HashMap, SplayTreeSet; |
8 | 8 |
9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; | 9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; |
10 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; | 10 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 'dart.registerExtension(dart.global.#, #);', [ | 413 'dart.registerExtension(dart.global.#, #);', [ |
414 _propertyName(jsPeerName), | 414 _propertyName(jsPeerName), |
415 classElem.name | 415 classElem.name |
416 ]); | 416 ]); |
417 return _statement([result, copyMembers]); | 417 return _statement([result, copyMembers]); |
418 } | 418 } |
419 return result; | 419 return result; |
420 } | 420 } |
421 | 421 |
422 @override | 422 @override |
423 JS.Statement visitEnumDeclaration(EnumDeclaration node) => | 423 JS.Statement visitEnumDeclaration(EnumDeclaration node) { |
424 _unimplementedCall("Unimplemented enum: $node").toStatement(); | 424 var element = node.element; |
| 425 var type = element.type; |
| 426 var name = js.string(type.name); |
| 427 var id = new JS.Identifier(type.name); |
| 428 |
| 429 // Generate a class per section 13 of the spec. |
| 430 // TODO(vsm): Generate any accompanying metadata |
| 431 |
| 432 // Create constructor and initialize index |
| 433 var constructor = |
| 434 new JS.Method(name, js.call('function(index) { this.index = index; }')); |
| 435 var fields = new List<ConstFieldElementImpl>.from( |
| 436 element.fields.where((f) => f.type == type)); |
| 437 |
| 438 // Create toString() method |
| 439 var properties = new List<JS.Property>(); |
| 440 for (var i = 0; i < fields.length; ++i) { |
| 441 properties.add(new JS.Property( |
| 442 js.number(i), js.string('${type.name}.${fields[i].name}'))); |
| 443 } |
| 444 var nameMap = new JS.ObjectInitializer(properties, multiline: true); |
| 445 var toStringF = new JS.Method(js.string('toString'), |
| 446 js.call('function () { return #[this.index]; }', nameMap)); |
| 447 |
| 448 // Create enum class |
| 449 var classExpr = new JS.ClassExpression( |
| 450 id, _classHeritage(element), [constructor, toStringF]); |
| 451 var result = [js.statement('#', classExpr)]; |
| 452 |
| 453 // Create static fields for each enum value |
| 454 for (var i = 0; i < fields.length; ++i) { |
| 455 result.add(js.statement('#.# = dart.const(new #(#));', [ |
| 456 id, |
| 457 fields[i].name, |
| 458 id, |
| 459 js.number(i) |
| 460 ])); |
| 461 } |
| 462 |
| 463 // Create static values list |
| 464 var values = new JS.ArrayInitializer( |
| 465 fields.map((f) => js.call('#.#', [id, f.name])).toList()); |
| 466 result.add(js.statement('#.values = dart.const(dart.list(#, #));', [ |
| 467 id, |
| 468 values, |
| 469 _emitTypeName(type) |
| 470 ])); |
| 471 |
| 472 return _statement(result); |
| 473 } |
425 | 474 |
426 /// Given a class element and body, complete the class declaration. | 475 /// Given a class element and body, complete the class declaration. |
427 /// This handles generic type parameters, laziness (in library-cycle cases), | 476 /// This handles generic type parameters, laziness (in library-cycle cases), |
428 /// and ensuring dependencies are loaded first. | 477 /// and ensuring dependencies are loaded first. |
429 JS.Statement _finishClassDef(ParameterizedType type, JS.Statement body) { | 478 JS.Statement _finishClassDef(ParameterizedType type, JS.Statement body) { |
430 var name = type.name; | 479 var name = type.name; |
431 var genericName = '$name\$'; | 480 var genericName = '$name\$'; |
432 | 481 |
433 JS.Statement genericDef = null; | 482 JS.Statement genericDef = null; |
434 if (type.typeParameters.isNotEmpty) { | 483 if (type.typeParameters.isNotEmpty) { |
(...skipping 2388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2823 | 2872 |
2824 class _JsThisFinder extends JS.BaseVisitor { | 2873 class _JsThisFinder extends JS.BaseVisitor { |
2825 bool found = false; | 2874 bool found = false; |
2826 visitThis(JS.This node) { | 2875 visitThis(JS.This node) { |
2827 found = true; | 2876 found = true; |
2828 } | 2877 } |
2829 visitNode(JS.Node node) { | 2878 visitNode(JS.Node node) { |
2830 if (!found) super.visitNode(node); | 2879 if (!found) super.visitNode(node); |
2831 } | 2880 } |
2832 } | 2881 } |
OLD | NEW |