Skip to content

Commit 84f60cf

Browse files
committed
Role assignment now being output correctly
1 parent a7b27d8 commit 84f60cf

File tree

5 files changed

+85
-42
lines changed

5 files changed

+85
-42
lines changed

samples/dci/_goal.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"use strict";
33

44
var isNodeJs = (typeof window == 'undefined' && typeof global != 'undefined');
5-
if (!isNodeJs) var global = window;
5+
var globalNamespace = isNodeJs ? global: window;
66

77
//This function is for handling calls beginning with `this`; it calls a method on the current role player,
88
//which could be either a role method or a method on the data object.
@@ -14,7 +14,7 @@
1414
if (player != context[roleName]) {
1515
//support functions within role methods for which `this` is unbound - but make sure we
1616
//don't wrongly assume `this` refers to the current role
17-
if (player != undefined && player != global) {
17+
if (player != undefined && player != globalNamespace) {
1818
//not a role method; call normally
1919
return player[methodName].apply(player, args);
2020
}

samples/dci/test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ var DCI = {
1616
var TransferMoney = DCI.Context(function () {
1717
var __context = this;
1818
this.bindRoles = function (sourceAcct, destinationAcct) {
19-
__context.SourceAccount = undefined;
20-
__context.DestinationAccount = undefined;
19+
__context.SourceAccount = sourceAcct;
20+
__context.DestinationAccount = destinationAcct;
2121
};
2222
this.execute = function () {
2323
SourceAccount.transferOut();
2424
};
2525
this.__$SourceAccount = { transferOut: function () {
2626
//TODO test calling role methods this way:
2727
//this['withdraw']();
28-
DCI.callRolePlayerMethod(__context, this, 'SourceAccount', 'withdraw');
28+
DCI.callMethodOnCurrentRolePlayer(__context, this, 'SourceAccount', 'withdraw');
2929
__context.__$DestinationAccount.deposit.call(__context.DestinationAccount);
3030
}
3131
,withdraw: function () {
3232
}
3333
}
3434
this.__$DestinationAccount = { deposit: function () {
35-
__context.__$console.log.call(__context.console, 'deposit');
35+
console.log('deposit');
3636
}
3737
}
3838
});

src/compiler/ast.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,7 @@ module TypeScript {
250250
public emit(emitter: Emitter) {
251251
//DCI
252252
if (emitter.thisFunctionDeclaration && emitter.thisFunctionDeclaration.isDCIContext) {
253-
//DCI TODO
254-
//if (emitter.thisFunctionDeclaration.roleDeclarations[this.actualText]) {
255-
if (true) {
253+
if (this.actualText in emitter.thisFunctionDeclaration.roleDeclarations) {
256254
emitter.writeToOutput("__context.");
257255
}
258256
}
@@ -885,7 +883,8 @@ module TypeScript {
885883
public classDecl: ClassDeclaration = null;
886884
//DCI
887885
public isDCIContext = false;
888-
public roleDeclarations = {};
886+
//DCI TODO would MapStringTo<RoleDeclaration> = {} be better here?
887+
public roleDeclarations: { [roleName: string]: RoleDeclaration} = {};
889888

890889
public returnStatementsWithExpressions: ReturnStatement[];
891890

@@ -1223,16 +1222,17 @@ module TypeScript {
12231222
public emitWorker(emitter: Emitter) {
12241223
//DCI
12251224
if (this.expression.nodeType() == NodeType.RoleAssignmentExpression) {
1226-
//DCI TODO
1227-
//Is this the right place to add the role to the collection?
1228-
//Maybe do this in parser and simply do nothing here
12291225
var operand1 = (<BinaryExpression>this.expression).operand1;
12301226
var operand2 = (<BinaryExpression>this.expression).operand2;
1231-
var roleName = (<Identifier>operand1).actualText;
1232-
var rolePlayerName = (<Identifier>operand2).actualText;
1233-
console.log('adding role ' + roleName);
12341227

1235-
emitter.writeToOutput("__context."+roleName+" = " + rolePlayerName + ";");
1228+
emitter.writeToOutput("__context.");
1229+
operand1.emit(emitter);
1230+
emitter.writeToOutput(" = ");
1231+
1232+
//DCI TODO figure out why operand2 is being parsed as a UnaryExpression;
1233+
//ideally we'd just do operand2.emit(emitter);
1234+
(<UnaryExpression>operand2).operand.emit(emitter);
1235+
emitter.writeToOutput(";");
12361236

12371237
return;
12381238
}

src/compiler/emitter.ts

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,8 @@ module TypeScript {
565565
var binaryExpressionTarget: BinaryExpression,
566566
isCallToSelf = false,
567567
operand1: Identifier,
568-
operand2: Identifier;
568+
operand2: Identifier,
569+
roleName: string;
569570

570571
if (target.nodeType() === NodeType.FunctionDeclaration) {
571572
this.writeToOutput("(");
@@ -585,36 +586,49 @@ module TypeScript {
585586
if (this.thisRoleNode) {
586587
if (this.thisRoleNode && binaryExpressionTarget.operand1 instanceof ThisExpression) {
587588
isCallToSelf = true;
588-
this.writeToOutput("DCI.callRolePlayerMethod");
589+
this.writeToOutput("DCI.callMethodOnCurrentRolePlayer");
589590
}
590-
//DCI TODO we should only do this if it's actually a role
591591
else {
592-
this.writeToOutput("__context.__$");
593-
this.writeToOutput(operand1.actualText);
594-
//binaryExpressionTarget.operand1.emit(this); //role name
595-
this.writeToOutput(".");
592+
roleName = operand1.actualText;
593+
if (roleName in this.thisFunctionDeclaration.roleDeclarations) {
594+
this.writeToOutput("__context.__$");
595+
this.writeToOutput(roleName);
596+
//binaryExpressionTarget.operand1.emit(this); //role name
597+
this.writeToOutput(".");
598+
}
599+
else {
600+
this.emitJavascript(target, false);
601+
}
596602
}
597603
}
598604
else {
599-
//DCI TODO determine if operand1 is the name of a role
600-
if (false) {
601-
605+
roleName = operand1.actualText;
606+
if (roleName in this.thisFunctionDeclaration.roleDeclarations) {
607+
608+
609+
//DCI TODO
610+
611+
612+
602613
}
603614
else {
604615
this.emitJavascript(target, false);
605616
}
606617
}
607618
}
608619
}
609-
else this.emitJavascript(target, false);
620+
else {
621+
//DCI TODO - context methods
622+
//Check parent function to see if it's a DCI context
623+
624+
this.emitJavascript(target, false);
625+
}
610626
}
611627
if (target.nodeType() === NodeType.FunctionDeclaration) {
612628
this.writeToOutput(")");
613629
}
614630
this.recordSourceMappingStart(args);
615631

616-
//DCI TODO - context methods, check if role exists
617-
618632
//DCI
619633
//if currently inside a role
620634
if (this.thisRoleNode) {
@@ -626,15 +640,34 @@ module TypeScript {
626640
//binaryExpressionTarget.operand2.emit(this);
627641
this.writeToOutput("')");
628642
} else {
629-
this.writeToOutput(operand2.actualText + ".call(__context." + operand1.actualText);
630-
//binaryExpressionTarget.operand2.emit(this);
631-
//this.writeToOutput(".call(");
632-
//binaryExpressionTarget.operand1.emit(this);
633-
634-
if (args.members.length > 0)
635-
this.writeToOutput(", ");
636-
this.emitCommaSeparatedList(args);
637-
this.writeToOutput(")");
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+
}
638671
}
639672
}
640673
else {

src/compiler/syntaxTreeToAstVisitor.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,6 @@ module TypeScript {
611611
return false;
612612
}
613613

614-
//DCI TODO this should probably have the same code as visitFunctionExpression()
615-
//
616614
public visitFunctionDeclaration(node: FunctionDeclarationSyntax): FunctionDeclaration {
617615
var start = this.position;
618616

@@ -635,6 +633,18 @@ module TypeScript {
635633
var result = new FunctionDeclaration(name, block, false, typeParameters, parameters, returnType, this.hasDotDotDotParameter(node.callSignature.parameterList.parameters));
636634
this.setCommentsAndSpan(result, start, node);
637635

636+
//DCI
637+
//Add RoleDeclarations to FunctionDeclaration object
638+
if (block) {
639+
var members = block.statements.members;
640+
for (var i = 0; i < members.length; i++) {
641+
if (members[i] instanceof RoleDeclaration) {
642+
var roleDecl = members[i];
643+
result.roleDeclarations[roleDecl.name.actualText] = roleDecl;
644+
}
645+
}
646+
}
647+
638648
if (node.semicolonToken) {
639649
result.setFunctionFlags(result.getFunctionFlags() | FunctionFlags.Signature);
640650
}
@@ -2125,7 +2135,7 @@ module TypeScript {
21252135
if (block) {
21262136
var members = block.statements.members;
21272137
for (var i = 0; i < members.length; i++) {
2128-
if (members[i] instanceof TypeScript.RoleDeclaration) {
2138+
if (members[i] instanceof RoleDeclaration) {
21292139
var roleDecl = members[i];
21302140
result.roleDeclarations[roleDecl.name.actualText] = roleDecl;
21312141
}

0 commit comments

Comments
 (0)