@@ -754,25 +754,38 @@ module TypeScript {
754
754
this . writeLineToOutput ( ") {" ) ;
755
755
756
756
//DCI
757
+ funcDecl . isDCIContext = ( Object . keys ( funcDecl . roleDeclarations ) . length > 0 ) ;
758
+
759
+ if ( funcDecl . isDCIContext ) {
760
+ this . indenter . increaseIndent ( ) ;
761
+ this . writeLineToOutput ( "var __context = this;" ) ;
762
+ //this.writeLineToOutput("__context.__rolePlayers = {};");
763
+ this . indenter . decreaseIndent ( ) ;
764
+ }
765
+
766
+ //output the role methods
767
+ this . indenter . increaseIndent ( ) ;
768
+ for ( var roleName in funcDecl . roleDeclarations ) {
769
+ funcDecl . roleDeclarations [ roleName ] . emit ( this ) ;
770
+ }
771
+ this . indenter . decreaseIndent ( ) ;
772
+
773
+ /*
757
774
var members = funcDecl.block.statements.members;
758
775
var member: AST;
776
+ this.indenter.increaseIndent();
759
777
for (var i=0; i < members.length; i++) {
760
778
member = members[i];
761
779
if (member instanceof RoleDeclaration) {
762
780
funcDecl.isDCIContext = true;
763
- break ;
781
+
782
+ //output the role methods
783
+ this.emitIndent();
784
+ member.emit(this);
764
785
}
765
786
}
766
-
767
- if ( funcDecl . isDCIContext ) {
768
- this . indenter . increaseIndent ( ) ;
769
- this . writeLineToOutput ( "var __context = this;" ) ;
770
- //this.writeLineToOutput("__context.__rolePlayers = {};");
771
- this . indenter . decreaseIndent ( ) ;
772
- }
773
-
774
- //DCI TODO
775
- //Roles should be output here, at the top of the function, not wherever they happen to have been declared in the source
787
+ this.indenter.decreaseIndent();
788
+ */
776
789
777
790
if ( funcDecl . isConstructor ) {
778
791
this . recordSourceMappingNameStart ( "constructor" ) ;
@@ -991,7 +1004,7 @@ module TypeScript {
991
1004
//DCI
992
1005
//Determine whether this module contains a DCI context
993
1006
//
994
- //DCI TODO Detect other types of context declarations; this only detects `var myContext = ...`
1007
+ //DCI TODO Detect other types of context declarations; this only detects `var myContext = ...` or `function myContext(...`
995
1008
//Also, should this search recursively?
996
1009
997
1010
var hasDCIContext = false ;
@@ -1003,19 +1016,27 @@ module TypeScript {
1003
1016
if ( initVal instanceof FunctionDeclaration ) {
1004
1017
if ( Object . keys ( ( < FunctionDeclaration > initVal ) . roleDeclarations ) . length ) {
1005
1018
hasDCIContext = true ;
1019
+ return false ; //exit the loop
1006
1020
}
1007
1021
}
1008
1022
else if ( initVal instanceof InvocationExpression ) {
1009
1023
( < InvocationExpression > initVal ) . arguments . members . forEach ( function ( arg ) {
1010
1024
if ( arg instanceof FunctionDeclaration ) {
1011
1025
if ( Object . keys ( ( < FunctionDeclaration > arg ) . roleDeclarations ) . length ) {
1012
1026
hasDCIContext = true ;
1027
+ return false ; //exit the loop
1013
1028
}
1014
1029
}
1015
1030
} ) ;
1016
1031
}
1017
1032
} ) ;
1018
1033
}
1034
+ else if ( member instanceof FunctionDeclaration ) {
1035
+ if ( Object . keys ( ( < FunctionDeclaration > member ) . roleDeclarations ) . length ) {
1036
+ hasDCIContext = true ;
1037
+ return false ; //exit the loop
1038
+ }
1039
+ }
1019
1040
} ) ;
1020
1041
1021
1042
// if the external module has an "export =" identifier, we'll
@@ -1915,7 +1936,11 @@ module TypeScript {
1915
1936
1916
1937
// tokenId is the id the preceding token
1917
1938
public emitJavascript ( ast : AST , startLine : boolean ) {
1918
- if ( ast === null ) {
1939
+ //DCI
1940
+ //Roles should be output at the top of functions, or on the prototype,
1941
+ //not just wherever they happen to have been declared, so we don't ouptut them here
1942
+ if ( ast === null || ast instanceof RoleDeclaration ) {
1943
+ //if (ast === null) {
1919
1944
return ;
1920
1945
}
1921
1946
@@ -2111,7 +2136,7 @@ module TypeScript {
2111
2136
this . emitRoleMembers ( roleDecl ) ;
2112
2137
2113
2138
this . indenter . decreaseIndent ( ) ;
2114
- this . writeToOutput ( "}" ) ;
2139
+ this . writeLineToOutput ( "}; " ) ;
2115
2140
2116
2141
this . recordSourceMappingEnd ( roleDecl ) ;
2117
2142
this . emitComments ( roleDecl , false ) ;
0 commit comments