Skip to content

Commit 4a120e8

Browse files
committed
tweaks to emitCall() method
1 parent c064b32 commit 4a120e8

File tree

6 files changed

+90
-25
lines changed

6 files changed

+90
-25
lines changed

samples/dci/js/Dijkstra/CalculateShortestPath.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var CalculateShortestPath = DCI.Context.extend(function() {
6060
}
6161

6262
relaxDistances() {
63-
this.getNeighbors().each(function(node) {
63+
self.getNeighbors().each(function(node) {
6464
Neighbor <- node;
6565
if (Neighbor.visited()) return;
6666

@@ -80,11 +80,11 @@ var CalculateShortestPath = DCI.Context.extend(function() {
8080
role Graph {
8181
distance(from, to) {
8282
if(from === to) return 0;
83-
return this.nodes.get(from).get(to) || Infinity;
83+
return self.nodes.get(from).get(to) || Infinity;
8484
}
8585

8686
neighbors(node) {
87-
return this.nodes.get(node);
87+
return self.nodes.get(node);
8888
}
8989
}
9090

@@ -95,7 +95,7 @@ var CalculateShortestPath = DCI.Context.extend(function() {
9595
findNearest() {
9696
var nearest = undefined,
9797
distance = Infinity;
98-
this.each(function(node) {
98+
self.each(function(node) {
9999
var dist = Tentative.get(node);
100100
if(dist < distance) {
101101
nearest = node;
@@ -111,7 +111,7 @@ var CalculateShortestPath = DCI.Context.extend(function() {
111111
var path = [to],
112112
cur = to;
113113
while (cur != Initial) {
114-
cur = this.get(cur);
114+
cur = self.get(cur);
115115
path.unshift(cur);
116116
if (cur === undefined) {
117117
return undefined;

samples/dci/js/TransferMoney/TransferMoney.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,35 @@ var DCI = require('../../DCI');
77
*
88
* @constructor
99
* @this {TransferMoney}
10-
* @param {Account} source
11-
* @param {Account} destination
10+
* @param {Account} sourceAcct
11+
* @param {Account} destinationAcct
1212
* @param {number} amount
1313
*/
1414
var TransferMoney = DCI.Context.extend(function () {
1515
var __context = this;
16-
this.__$SourceAccount = { transferOut: function () {
17-
__dci_internal__.callMethodOnSelf(__context, this, 'SourceAccount', 'withdraw');
16+
this.__$SourceAccount = { foo: function () {
17+
var test = {
18+
problemIsHere: function () {
19+
__dci_internal__.callMethodOnSelf(__context, this, 'SourceAccount', 'bar');
20+
},
21+
bar: function () {
22+
}
23+
};
24+
}
25+
,bar: function () {
26+
}
27+
28+
,transferOut: //transfer out of this account and into the destination account
29+
function () {
30+
__context.__$SourceAccount.withdraw.call(__context.SourceAccount);
1831
__context.__$DestinationAccount.deposit.call(__context.DestinationAccount);
1932
}
2033
,withdraw: function () {
21-
__dci_internal__.callMethodOnSelf(__context, this, 'SourceAccount', 'decreaseBalance', [__context.Amount]);
34+
__context.SourceAccount.decreaseBalance(__context.Amount);
2235
}
2336
};
2437
this.__$DestinationAccount = { deposit: function () {
25-
__dci_internal__.callMethodOnSelf(__context, this, 'DestinationAccount', 'increaseBalance', [__context.Amount]);
38+
__context.DestinationAccount.increaseBalance(__context.Amount);
2639
}
2740
};
2841
this.__$Amount = {};
@@ -31,6 +44,7 @@ this.__$Amount = {};
3144
__context.DestinationAccount = destinationAcct;
3245
__context.Amount = amount;
3346
};
47+
//Run the use case
3448
this.run = function () {
3549
__context.__$SourceAccount.transferOut.call(__context.SourceAccount);
3650
};

samples/dci/js/TransferMoney/TransferMoney.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ var TransferMoney = DCI.Context.extend(function() {
2525
role SourceAccount {
2626
//transfer out of this account and into the destination account
2727
transferOut() {
28-
this.withdraw();
28+
self.withdraw();
2929
DestinationAccount.deposit();
3030
}
3131

3232
withdraw() {
33-
this.decreaseBalance(Amount);
33+
self.decreaseBalance(Amount);
3434
}
3535
}
3636

3737
role DestinationAccount {
3838
deposit() {
39-
this.increaseBalance(Amount);
39+
self.increaseBalance(Amount);
4040
}
4141
}
4242

samples/dci/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var __context = this;
66
this.__$SourceAccount = { transferOut: function () {
77
//TODO test calling role methods this way:
88
//this['withdraw']();
9-
__dci_internal__.callMethodOnSelf(__context, this, 'SourceAccount', 'withdraw');
9+
__context.__$SourceAccount.withdraw.call(__context.SourceAccount);
1010
__context.__$DestinationAccount.deposit.call(__context.DestinationAccount);
1111
}
1212
,withdraw: function () {

samples/dci/typescript/TransferMoney/TransferMoney.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@ export class TransferMoney extends DCI.Context
1313
Amount <- amount;
1414
}
1515

16-
role SourceAccount {
16+
role SourceAccount {
17+
1718
transferOut() {
18-
this.withdraw();
19+
self.withdraw();
1920
DestinationAccount.deposit();
2021
}
2122

2223
withdraw() {
23-
this.decreaseBalance(Amount);
24+
self.decreaseBalance(Amount);
2425
}
2526
}
2627

2728
role DestinationAccount {
2829
deposit() {
29-
this.increaseBalance(Amount);
30+
self.increaseBalance(Amount);
3031
}
3132
}
3233

src/compiler/emitter.ts

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ module TypeScript {
569569
operand2: Identifier,
570570
dciContext: DCIContext,
571571
isCallToRoleMethod = false,
572-
//isCallToSelf = false,
572+
isCallToSelf = false,
573573
roleName: string;
574574

575575
//DCI
@@ -587,7 +587,7 @@ module TypeScript {
587587
if (this.thisRoleNode) {
588588
if (this.thisRoleNode && (operand1 instanceof ThisExpression || operand1.actualText == 'self') ) {
589589
roleName = this.thisRoleNode.name.actualText;
590-
//isCallToSelf = true;
590+
isCallToSelf = true;
591591
}
592592
}
593593

@@ -611,16 +611,66 @@ module TypeScript {
611611
}
612612

613613
//DCI
614-
if (isCallToRoleMethod) {
615-
this.writeToOutput("__context.__$" + roleName + "." + operand2.actualText + ".");
616-
this.writeToOutput("call(__context." + roleName);
614+
if (isCallToRoleMethod || isCallToSelf) {
615+
//If the call is within a role and begins with `this` or `self`, it could be either a role method or a data object method
616+
var isCallToThis = false;
617+
if (isCallToSelf) {
618+
if (operand1 instanceof ThisExpression) {
619+
isCallToThis = true;
620+
621+
//If `this` was used, we can't be sure it's pointing to a role method even if the given method name is found on the role.
622+
//Consider:
623+
/*
624+
role SomeRole {
625+
foo() {
626+
var test = {
627+
problemIsHere: function() {
628+
this.bar();
629+
},
630+
631+
bar: function() {}
632+
}
633+
}
634+
635+
bar() {}
636+
}
637+
638+
There may be a way we could know for sure by examining the AST in more detail, but in the current implementation we just take care of this
639+
at run-time via the callMethodOnSelf() function.
640+
*/
641+
642+
this.writeToOutput("__dci_internal__.callMethodOnSelf");
643+
//this.writeToOutput("DCI.callMethodOnSelf");
644+
this.writeToOutput("(__context, this, '" + roleName + "'");
645+
this.writeToOutput(", '" + operand2.actualText + "'");
646+
647+
if (args && args.members.length) this.writeToOutput(", ");
648+
}
649+
else { //`self` was used, not `this`
650+
if (isCallToRoleMethod) {
651+
this.writeToOutput("__context.__$" + roleName + "." + operand2.actualText + ".");
652+
this.writeToOutput("call(__context." + roleName);
617653

618-
if (args && args.members.length) this.writeToOutput(", ");
654+
if (args && args.members.length) this.writeToOutput(", ");
655+
}
656+
else { //call to data object method
657+
this.writeToOutput("__context." + roleName + "." + operand2.actualText + "(");
658+
}
659+
}
660+
}
661+
else { //it's a call to a method in one role from a method in another, or from a context method
662+
this.writeToOutput("__context.__$" + roleName + "." + operand2.actualText + ".");
663+
this.writeToOutput("call(__context." + roleName);
664+
665+
if (args && args.members.length) this.writeToOutput(", ");
666+
}
619667

620668
this.recordSourceMappingStart(args);
621669

622670
if (args && args.members.length) {
671+
if (isCallToThis) this.writeToOutput("[");
623672
this.emitCommaSeparatedList(args);
673+
if (isCallToThis) this.writeToOutput("]");
624674
}
625675
}
626676
else {

0 commit comments

Comments
 (0)