Skip to content

Commit f55fbe7

Browse files
committed
Initial support for automatically require()ing DCI library
1 parent f9d6718 commit f55fbe7

File tree

10 files changed

+234
-227
lines changed

10 files changed

+234
-227
lines changed

samples/dci/_goal.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//
1212
// TODO test data object methods that aren't overridden by the role
1313
//
14-
DCI.callMethodOnCurrentRolePlayer = function callMethodOnCurrentRolePlayer(context, player, roleName, methodName, args) {
14+
DCI.callMethodOnSelf = function callMethodOnSelf(context, player, roleName, methodName, args) {
1515
//if `this` is not equal to the current role player
1616
if (player != context[roleName]) {
1717
//support functions within role methods for which `this` is unbound - but make sure we

samples/dci/test.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
1+
//import dci = require('dci');
2+
//console.log(dci);
3+
/*
14
var DCI = {
2-
Context: function Context(callback) {
3-
return function () {
4-
var args = [];
5-
for (var _i = 0; _i < (arguments.length - 0); _i++) {
6-
args[_i] = arguments[_i + 0];
7-
}
8-
var context = new callback();
9-
context.bindRoles.apply(callback, arguments);
10-
return context;
11-
};
12-
}
5+
Context: function Context(callback) {
6+
return function(...args : any[]) {
7+
var context = new callback();
8+
context.bindRoles.apply(callback, arguments);
9+
return context;
10+
}
11+
}
1312
};
14-
13+
*/
1514
function TransferMoney(sourceAcct, destinationAcct) {
1615
var __context = this;
1716
//Role binding
18-
__context.__context.SourceAccount = sourceAcct;
19-
__context.__context.DestinationAccount = destinationAcct;
20-
17+
__context.SourceAccount = sourceAcct;
18+
__context.DestinationAccount = destinationAcct;
2119
//Execute the use case
22-
transferOut.call(__context.SourceAccount);
20+
__context.__$SourceAccount.call(__context.SourceAccount);
2321
this.__$SourceAccount = { transferOut: function () {
2422
//TODO test calling role methods this way:
2523
//this['withdraw']();
26-
DCI.callMethodOnCurrentRolePlayer(__context, this, 'SourceAccount', 'withdraw');
27-
__context.__$DestinationAccount.deposit.call(__context.DestinationAccount);
24+
DCI.callMethodOnSelf(__context, this, 'SourceAccount', 'withdraw');
25+
__context.__$DestinationAccount.call(__context.DestinationAccount);
2826
}
2927
,withdraw: function () {
3028
console.log('withdraw');

samples/dci/test.ts

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//import dci = require('dci');
2+
//console.log(dci);
3+
4+
/*
15
var DCI = {
26
Context: function Context(callback) {
37
return function(...args : any[]) {
@@ -7,29 +11,30 @@ var DCI = {
711
}
812
}
913
};
14+
*/
1015

1116
function TransferMoney(sourceAcct, destinationAcct) {
1217
//Role binding
1318
SourceAccount <- sourceAcct;
1419
DestinationAccount <- destinationAcct;
15-
20+
1621
//Execute the use case
17-
SourceAccount.transferOut();
18-
22+
SourceAccount.transferOut();
23+
1924
role SourceAccount {
2025
transferOut() {
2126
//TODO test calling role methods this way:
2227
//this['withdraw']();
23-
28+
2429
this.withdraw();
2530
DestinationAccount.deposit();
2631
}
27-
32+
2833
withdraw() {
2934
console.log('withdraw');
3035
}
3136
}
32-
37+
3338
role DestinationAccount {
3439
deposit() {
3540
console.log('deposit');
@@ -39,77 +44,77 @@ function TransferMoney(sourceAcct, destinationAcct) {
3944

4045
var ctx = TransferMoney({}, {});
4146

42-
/*
43-
//TODO DCI.Context.extend() -- DCI.Context should be a Typescript class
44-
var TransferMoney = DCI.Context(function() {
47+
/*
48+
//TODO DCI.Context.extend() -- DCI.Context should be a Typescript class
49+
var TransferMoney = DCI.Context(function() {
4550
46-
this.bindRoles = function(sourceAcct, destinationAcct) {
47-
SourceAccount <- sourceAcct;
48-
DestinationAccount <- destinationAcct;
49-
}
51+
this.bindRoles = function(sourceAcct, destinationAcct) {
52+
SourceAccount <- sourceAcct;
53+
DestinationAccount <- destinationAcct;
54+
}
5055
51-
this.execute = function() {
52-
SourceAccount.transferOut();
53-
}
56+
this.execute = function() {
57+
SourceAccount.transferOut();
58+
}
5459
55-
role SourceAccount {
56-
transferOut() {
57-
//TODO test calling role methods this way:
58-
//this['withdraw']();
60+
role SourceAccount {
61+
transferOut() {
62+
//TODO test calling role methods this way:
63+
//this['withdraw']();
5964
60-
this.withdraw();
61-
DestinationAccount.deposit();
62-
}
65+
this.withdraw();
66+
DestinationAccount.deposit();
67+
}
6368
64-
withdraw() {
65-
console.log('withdraw');
69+
withdraw() {
70+
console.log('withdraw');
71+
}
6672
}
67-
}
6873
69-
role DestinationAccount {
70-
deposit() {
71-
console.log('deposit');
74+
role DestinationAccount {
75+
deposit() {
76+
console.log('deposit');
77+
}
7278
}
73-
}
74-
});
79+
});
7580
76-
var ctx = TransferMoney({}, {});
77-
ctx.execute();
78-
*/
81+
var ctx = TransferMoney({}, {});
82+
ctx.execute();
83+
*/
7984

80-
/*
81-
function TransferMoney(sourceAcct, destinationAcct) {
82-
83-
this.bindRoles = function(a1, a2) {
84-
SourceAccount <- a1;
85-
DestinationAccount <- a2;
86-
}
87-
this.bindRoles(sourceAcct, destinationAcct);
85+
/*
86+
function TransferMoney(sourceAcct, destinationAcct) {
8887
89-
this.execute = function() {
90-
SourceAccount.transferOut();
91-
}
88+
this.bindRoles = function(a1, a2) {
89+
SourceAccount <- a1;
90+
DestinationAccount <- a2;
91+
}
92+
this.bindRoles(sourceAcct, destinationAcct);
9293
93-
role SourceAccount {
94-
transferOut() {
95-
this.withdraw();
96-
DestinationAccount.deposit();
94+
this.execute = function() {
95+
SourceAccount.transferOut();
9796
}
97+
98+
role SourceAccount {
99+
transferOut() {
100+
this.withdraw();
101+
DestinationAccount.deposit();
102+
}
98103
99-
withdraw() {
104+
withdraw() {
100105
106+
}
101107
}
102-
}
103108
104-
role DestinationAccount {
105-
deposit() {
106-
console.log('deposit');
109+
role DestinationAccount {
110+
deposit() {
111+
console.log('deposit');
112+
}
107113
}
108114
}
109-
}
110115
111-
var ctx = new TransferMoney({}, {});
112-
ctx.execute();
116+
var ctx = new TransferMoney({}, {});
117+
ctx.execute();
113118
*/
114119

115120
/*

samples/dci/tmp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
var a, b;
2-
;
1+
eval('try { require("dci"); } catch(e) { console.error("ERROR"); }');
2+

src/compiler/ast.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,9 @@ module TypeScript {
249249

250250
public emit(emitter: Emitter) {
251251
//DCI
252-
if (emitter.thisFunctionDeclaration && emitter.thisFunctionDeclaration.isDCIContext) {
253-
if (this.actualText in emitter.thisFunctionDeclaration.roleDeclarations) {
252+
if (emitter.thisFunctionNode && emitter.thisFunctionNode.isDCIContext) {
253+
//If we're in a role method
254+
if (this.actualText in emitter.thisFunctionNode.roleDeclarations) {
254255
emitter.writeToOutput("__context.");
255256
}
256257
}
@@ -314,7 +315,7 @@ module TypeScript {
314315
}
315316

316317
public emitWorker(emitter: Emitter) {
317-
if (emitter.thisFunctionDeclaration && (hasFlag(emitter.thisFunctionDeclaration.getFunctionFlags(), FunctionFlags.IsFatArrowFunction))) {
318+
if (emitter.thisFunctionNode && (hasFlag(emitter.thisFunctionNode.getFunctionFlags(), FunctionFlags.IsFatArrowFunction))) {
318319
emitter.writeToOutput("_this");
319320
}
320321
else {
@@ -1225,7 +1226,11 @@ module TypeScript {
12251226
var operand1 = (<BinaryExpression>this.expression).operand1;
12261227
var operand2 = (<BinaryExpression>this.expression).operand2;
12271228

1228-
emitter.writeToOutput("__context.");
1229+
if (!emitter.thisFunctionNode.isDCIContext) {
1230+
//For the case where role binding is done in a context method, not the context function itself
1231+
//Otherwise, we don't need to output "__context" because Identifier.emit() has already done so
1232+
emitter.writeToOutput("__context.");
1233+
}
12291234
operand1.emit(emitter);
12301235
emitter.writeToOutput(" = ");
12311236

src/compiler/astWalker.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,7 @@ module TypeScript {
266266
}
267267
}
268268

269-
//DCI
270269
export function walkRoleAssignmentExpressionChildren(preAst: RoleAssignmentExpression, parent: AST, walker: IAstWalker): void {
271-
//DCI TODO remove this
272-
console.log('walkRoleAssignmentExpressionChildren');
273-
274270
if (preAst.roleName) {
275271
preAst.roleName = walker.walk(preAst.roleName, preAst);
276272
}

0 commit comments

Comments
 (0)