Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 295c811

Browse files
author
Olivier Chafik
committed
Create local alias for super class in --closure mode (issue #312)
1 parent 9e88452 commit 295c811

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

lib/runtime/dart/_runtime.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,8 @@ dart_library.library('dart/_runtime', null, /* Imports */[
726726
const _mixins = Symbol("mixins");
727727
const implements$ = Symbol("implements");
728728
const metadata = Symbol("metadata");
729-
const TypeRep = class TypeRep extends LazyTagged(() => core.Type) {
729+
const _TypeRepBase = LazyTagged(() => core.Type);
730+
const TypeRep = class TypeRep extends _TypeRepBase {
730731
get name() {
731732
return this.toString();
732733
}

lib/src/codegen/js_codegen.dart

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,10 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
375375
}
376376
}
377377

378-
var classDecl = new JS.ClassDeclaration(new JS.ClassExpression(
379-
new JS.Identifier(element.name), _classHeritage(element), body));
378+
var classExpr = new JS.ClassExpression(
379+
new JS.Identifier(element.name), _classHeritage(element), body);
380380

381-
return _finishClassDef(element.type, classDecl);
381+
return _finishClassDef(element.type, _emitClassDeclaration(classExpr));
382382
}
383383

384384
JS.Statement _emitJsType(String dartClassName, DartObject jsName) {
@@ -669,6 +669,25 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
669669
}
670670
}
671671

672+
_isQualifiedPath(JS.Expression node) =>
673+
node is JS.Identifier ||
674+
node is JS.PropertyAccess &&
675+
_isQualifiedPath(node.receiver) &&
676+
node.selector is JS.LiteralString;
677+
678+
JS.Statement _emitClassDeclaration(JS.ClassExpression cls) {
679+
var body = <JS.Statement>[];
680+
if (options.closure &&
681+
cls.heritage != null && !_isQualifiedPath(cls.heritage)) {
682+
// Workaround for Closure: super classes must be qualified paths.
683+
var superVar = new JS.Identifier(cls.name.name + r'$super');
684+
body.add(js.statement('const # = #;', [superVar, cls.heritage]));
685+
cls = new JS.ClassExpression(cls.name, superVar, cls.methods);
686+
}
687+
body.add(new JS.ClassDeclaration(cls));
688+
return _statement(body);
689+
}
690+
672691
/// Emit class members that need to come after the class declaration, such
673692
/// as static fields. See [_emitClassMethods] for things that are emitted
674693
/// inside the ES6 `class { ... }` node.
@@ -696,7 +715,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
696715
}
697716
}
698717

699-
body.add(new JS.ClassDeclaration(cls));
718+
body.add(_emitClassDeclaration(cls));
700719

701720
// TODO(jmesserly): we should really just extend native Array.
702721
if (jsPeerName != null && classElem.typeParameters.isNotEmpty) {

test/codegen/expect/closure.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ dart_library.library('closure', null, /* Imports */[
117117
/** @type {string} */
118118
Foo.some_static_var = "abc";
119119
class Bar extends core.Object {}
120-
class Baz extends dart.mixin(Foo$(core.int), Bar) {
120+
const Baz$super = dart.mixin(Foo$(core.int), Bar);
121+
class Baz extends Baz$super {
121122
/** @param {?number} i */
122123
Baz(i) {
123124
super.Foo(i, 123);

tool/input_sdk/private/types.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ final metadata = JS('', 'Symbol("metadata")');
3838
/// String toString();
3939
///
4040
///
41+
final _TypeRepBase = JS('', '$LazyTagged(() => $Type)');
4142
final TypeRep = JS('', '''
42-
class TypeRep extends $LazyTagged(() => $Type) {
43+
class TypeRep extends $_TypeRepBase {
4344
get name() {return this.toString();}
4445
}
4546
''');

0 commit comments

Comments
 (0)