Skip to content

Commit 9eaefcf

Browse files
committed
Roles now outputting at top of function
1 parent 8fc8867 commit 9eaefcf

File tree

4 files changed

+56
-61
lines changed

4 files changed

+56
-61
lines changed

dci-doc/tmp--old-tips.txt

Lines changed: 0 additions & 38 deletions
This file was deleted.

samples/dci/test.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
var __dci_internal__ = require('typescript-dci/dci');
2+
3+
14
function TransferMoney(sourceAcct, destinationAcct) {
25
var __context = this;
3-
//Role binding
4-
__context.SourceAccount = sourceAcct;
5-
__context.DestinationAccount = destinationAcct;
6-
//Execute the use case
7-
__context.__$SourceAccount.transferOut.call(__context.SourceAccount);
8-
this.__$SourceAccount = { transferOut: function () {
6+
this.__$SourceAccount = { transferOut: function () {
97
//TODO test calling role methods this way:
108
//this['withdraw']();
119
__dci_internal__.callMethodOnSelf(__context, this, 'SourceAccount', 'withdraw');
@@ -14,10 +12,19 @@ var __context = this;
1412
,withdraw: function () {
1513
console.log('withdraw');
1614
}
17-
}
18-
this.__$DestinationAccount = { deposit: function () {
15+
};
16+
this.__$DestinationAccount = { deposit: function () {
1917
console.log('deposit');
2018
}
21-
}
19+
};
20+
//Role binding
21+
__context.SourceAccount = sourceAcct;
22+
__context.DestinationAccount = destinationAcct;
23+
//Execute the use case
24+
__context.__$SourceAccount.transferOut.call(__context.SourceAccount);
25+
26+
2227
}
2328
var ctx = TransferMoney({}, {});
29+
module.exports = TransferMoney;
30+

samples/dci/test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export = TransferMoney;
12

23
function TransferMoney(sourceAcct, destinationAcct) {
34
//Role binding

src/compiler/emitter.ts

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -754,25 +754,38 @@ module TypeScript {
754754
this.writeLineToOutput(") {");
755755

756756
//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+
/*
757774
var members = funcDecl.block.statements.members;
758775
var member: AST;
776+
this.indenter.increaseIndent();
759777
for (var i=0; i < members.length; i++) {
760778
member = members[i];
761779
if (member instanceof RoleDeclaration) {
762780
funcDecl.isDCIContext = true;
763-
break;
781+
782+
//output the role methods
783+
this.emitIndent();
784+
member.emit(this);
764785
}
765786
}
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+
*/
776789

777790
if (funcDecl.isConstructor) {
778791
this.recordSourceMappingNameStart("constructor");
@@ -991,7 +1004,7 @@ module TypeScript {
9911004
//DCI
9921005
//Determine whether this module contains a DCI context
9931006
//
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(...`
9951008
//Also, should this search recursively?
9961009

9971010
var hasDCIContext = false;
@@ -1003,19 +1016,27 @@ module TypeScript {
10031016
if (initVal instanceof FunctionDeclaration) {
10041017
if (Object.keys((<FunctionDeclaration>initVal).roleDeclarations).length) {
10051018
hasDCIContext = true;
1019+
return false; //exit the loop
10061020
}
10071021
}
10081022
else if (initVal instanceof InvocationExpression) {
10091023
(<InvocationExpression>initVal).arguments.members.forEach(function(arg) {
10101024
if (arg instanceof FunctionDeclaration) {
10111025
if (Object.keys((<FunctionDeclaration>arg).roleDeclarations).length) {
10121026
hasDCIContext = true;
1027+
return false; //exit the loop
10131028
}
10141029
}
10151030
});
10161031
}
10171032
});
10181033
}
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+
}
10191040
});
10201041

10211042
// if the external module has an "export =" identifier, we'll
@@ -1915,7 +1936,11 @@ module TypeScript {
19151936

19161937
// tokenId is the id the preceding token
19171938
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) {
19191944
return;
19201945
}
19211946

@@ -2111,7 +2136,7 @@ module TypeScript {
21112136
this.emitRoleMembers(roleDecl);
21122137

21132138
this.indenter.decreaseIndent();
2114-
this.writeToOutput("}");
2139+
this.writeLineToOutput("};");
21152140

21162141
this.recordSourceMappingEnd(roleDecl);
21172142
this.emitComments(roleDecl, false);

0 commit comments

Comments
 (0)