Skip to content

Commit f9d6718

Browse files
committed
Context methods now working
1 parent 84f60cf commit f9d6718

File tree

4 files changed

+110
-50
lines changed

4 files changed

+110
-50
lines changed

samples/dci/_goal.html

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
var isNodeJs = (typeof window == 'undefined' && typeof global != 'undefined');
55
var globalNamespace = isNodeJs ? global: window;
66

7+
var DCI = {};
8+
79
//This function is for handling calls beginning with `this`; it calls a method on the current role player,
810
//which could be either a role method or a method on the data object.
911
//
1012
// TODO test data object methods that aren't overridden by the role
1113
//
12-
function callMethodOnCurrentRolePlayer(context, player, roleName, methodName, args) {
14+
DCI.callMethodOnCurrentRolePlayer = function callMethodOnCurrentRolePlayer(context, player, roleName, methodName, args) {
1315
//if `this` is not equal to the current role player
1416
if (player != context[roleName]) {
1517
//support functions within role methods for which `this` is unbound - but make sure we
@@ -37,6 +39,21 @@
3739
return player[methodName].apply(player, args);
3840
}
3941

42+
globalNamespace.DCI = DCI;
43+
44+
//Compiled by TS
45+
DCI.Context = function Context(callback) {
46+
return function () {
47+
var args = [];
48+
for (var _i = 0; _i < (arguments.length - 0); _i++) {
49+
args[_i] = arguments[_i + 0];
50+
}
51+
var context = new callback();
52+
context.bindRoles.apply(callback, arguments);
53+
return context;
54+
};
55+
};
56+
4057

4158
//for class version
4259

samples/dci/test.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,27 @@ var DCI = {
1212
}
1313
};
1414

15-
//TODO DCI.Context.extend() -- DCI.Context should be a Typescript class
16-
var TransferMoney = DCI.Context(function () {
15+
function TransferMoney(sourceAcct, destinationAcct) {
1716
var __context = this;
18-
this.bindRoles = function (sourceAcct, destinationAcct) {
19-
__context.SourceAccount = sourceAcct;
20-
__context.DestinationAccount = destinationAcct;
21-
};
22-
this.execute = function () {
23-
SourceAccount.transferOut();
24-
};
17+
//Role binding
18+
__context.__context.SourceAccount = sourceAcct;
19+
__context.__context.DestinationAccount = destinationAcct;
20+
21+
//Execute the use case
22+
transferOut.call(__context.SourceAccount);
2523
this.__$SourceAccount = { transferOut: function () {
2624
//TODO test calling role methods this way:
2725
//this['withdraw']();
2826
DCI.callMethodOnCurrentRolePlayer(__context, this, 'SourceAccount', 'withdraw');
2927
__context.__$DestinationAccount.deposit.call(__context.DestinationAccount);
3028
}
3129
,withdraw: function () {
30+
console.log('withdraw');
3231
}
3332
}
3433
this.__$DestinationAccount = { deposit: function () {
3534
console.log('deposit');
3635
}
3736
}
38-
});
37+
}
3938
var ctx = TransferMoney({}, {});
40-
ctx.execute();

samples/dci/test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,38 @@ var DCI = {
88
}
99
};
1010

11+
function TransferMoney(sourceAcct, destinationAcct) {
12+
//Role binding
13+
SourceAccount <- sourceAcct;
14+
DestinationAccount <- destinationAcct;
15+
16+
//Execute the use case
17+
SourceAccount.transferOut();
18+
19+
role SourceAccount {
20+
transferOut() {
21+
//TODO test calling role methods this way:
22+
//this['withdraw']();
23+
24+
this.withdraw();
25+
DestinationAccount.deposit();
26+
}
27+
28+
withdraw() {
29+
console.log('withdraw');
30+
}
31+
}
32+
33+
role DestinationAccount {
34+
deposit() {
35+
console.log('deposit');
36+
}
37+
}
38+
}
39+
40+
var ctx = TransferMoney({}, {});
41+
42+
/*
1143
//TODO DCI.Context.extend() -- DCI.Context should be a Typescript class
1244
var TransferMoney = DCI.Context(function() {
1345
@@ -30,7 +62,7 @@ var TransferMoney = DCI.Context(function() {
3062
}
3163
3264
withdraw() {
33-
65+
console.log('withdraw');
3466
}
3567
}
3668
@@ -41,9 +73,9 @@ var TransferMoney = DCI.Context(function() {
4173
}
4274
});
4375
44-
4576
var ctx = TransferMoney({}, {});
4677
ctx.execute();
78+
*/
4779

4880
/*
4981
function TransferMoney(sourceAcct, destinationAcct) {

src/compiler/emitter.ts

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,10 @@ module TypeScript {
563563
if (!this.emitSuperCall(callNode)) {
564564
//DCI
565565
var binaryExpressionTarget: BinaryExpression,
566-
isCallToSelf = false,
567566
operand1: Identifier,
568567
operand2: Identifier,
568+
isRoleMethodCall = false,
569+
isCallToSelf = false,
569570
roleName: string;
570571

571572
if (target.nodeType() === NodeType.FunctionDeclaration) {
@@ -585,16 +586,19 @@ module TypeScript {
585586
//if currently inside a role
586587
if (this.thisRoleNode) {
587588
if (this.thisRoleNode && binaryExpressionTarget.operand1 instanceof ThisExpression) {
589+
isRoleMethodCall = true;
588590
isCallToSelf = true;
589591
this.writeToOutput("DCI.callMethodOnCurrentRolePlayer");
590592
}
591593
else {
592594
roleName = operand1.actualText;
593595
if (roleName in this.thisFunctionDeclaration.roleDeclarations) {
596+
isRoleMethodCall = true;
597+
594598
this.writeToOutput("__context.__$");
595599
this.writeToOutput(roleName);
596600
//binaryExpressionTarget.operand1.emit(this); //role name
597-
this.writeToOutput(".");
601+
this.writeToOutput(".");
598602
}
599603
else {
600604
this.emitJavascript(target, false);
@@ -604,7 +608,7 @@ module TypeScript {
604608
else {
605609
roleName = operand1.actualText;
606610
if (roleName in this.thisFunctionDeclaration.roleDeclarations) {
607-
611+
isRoleMethodCall = true;
608612

609613
//DCI TODO
610614

@@ -618,10 +622,39 @@ module TypeScript {
618622
}
619623
}
620624
else {
621-
//DCI TODO - context methods
622-
//Check parent function to see if it's a DCI context
625+
//DCI
626+
//Check to see if any of the parent functions to which this function belongs are a DCI context
627+
var declStack = this.declStack;
628+
var dciContext: FunctionDeclaration;
629+
for (var i=0; i < declStack.length; i++) {
630+
if (declStack[i] instanceof PullFunctionExpressionDecl) {
631+
var funcDecl = <FunctionDeclaration>declStack[i].ast;
632+
if (funcDecl.isDCIContext) {
633+
dciContext = funcDecl;
634+
break;
635+
}
636+
}
637+
}
638+
639+
//TEMP
640+
641+
if (dciContext) {
642+
binaryExpressionTarget = <BinaryExpression>target;
643+
operand1 = <Identifier>binaryExpressionTarget.operand1;
644+
operand2 = <Identifier>binaryExpressionTarget.operand2;
645+
roleName = operand1.actualText;
646+
}
623647

624-
this.emitJavascript(target, false);
648+
//TEMP
649+
650+
if (dciContext && roleName in dciContext.roleDeclarations) {
651+
isRoleMethodCall = true;
652+
653+
this.writeToOutput("__context.__$");
654+
this.writeToOutput(roleName);
655+
this.writeToOutput(".");
656+
}
657+
else this.emitJavascript(target, false);
625658
}
626659
}
627660
if (target.nodeType() === NodeType.FunctionDeclaration) {
@@ -630,8 +663,7 @@ module TypeScript {
630663
this.recordSourceMappingStart(args);
631664

632665
//DCI
633-
//if currently inside a role
634-
if (this.thisRoleNode) {
666+
if (isRoleMethodCall) {
635667
if (isCallToSelf) {
636668
this.writeToOutput("(__context, this, '");
637669
this.writeToOutput( (<Identifier>this.thisRoleNode.name).actualText );
@@ -640,34 +672,15 @@ module TypeScript {
640672
//binaryExpressionTarget.operand2.emit(this);
641673
this.writeToOutput("')");
642674
} else {
643-
if (roleName in this.thisFunctionDeclaration.roleDeclarations) {
644-
this.writeToOutput(operand2.actualText + ".call(__context." + operand1.actualText);
645-
//binaryExpressionTarget.operand2.emit(this);
646-
//this.writeToOutput(".call(");
647-
//binaryExpressionTarget.operand1.emit(this);
648-
649-
if (args.members.length > 0)
650-
this.writeToOutput(", ");
651-
this.emitCommaSeparatedList(args);
652-
this.writeToOutput(")");
653-
}
654-
else {
655-
656-
657-
//TEMP - copied from below
658-
659-
660-
this.writeToOutput("(");
661-
if (callNode.target.nodeType() === 32 /* SuperExpression */ && this.emitState.container === 4 /* Constructor */) {
662-
this.writeToOutput("this");
663-
if (args && args.members.length) {
664-
this.writeToOutput(", ");
665-
}
666-
}
667-
this.emitCommaSeparatedList(args);
668-
this.recordSourceMappingStart(callNode.closeParenSpan);
669-
this.writeToOutput(")");
670-
}
675+
this.writeToOutput(operand2.actualText + ".call(__context." + operand1.actualText);
676+
//binaryExpressionTarget.operand2.emit(this);
677+
//this.writeToOutput(".call(");
678+
//binaryExpressionTarget.operand1.emit(this);
679+
680+
if (args.members.length > 0)
681+
this.writeToOutput(", ");
682+
this.emitCommaSeparatedList(args);
683+
this.writeToOutput(")");
671684
}
672685
}
673686
else {

0 commit comments

Comments
 (0)