Skip to content

Commit 3893ae9

Browse files
committed
Bug fix for calling data object method
1 parent ca32a5d commit 3893ae9

File tree

14 files changed

+120552
-119070
lines changed

14 files changed

+120552
-119070
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Brings support for DCI programming to TypeScript. For more information on DCI, s
44

55
Documentation is forthcoming. In the meantime, check out the samples/dci folder.
66

7+
All modifications to the official TypeScript source code are commented with "DCI"
8+
79

810
# TypeScript
911

bin/tsc

100644100755
File mode changed.

bin/tsc.js

Lines changed: 57036 additions & 56298 deletions
Large diffs are not rendered by default.

bin/typescript.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28956,15 +28956,7 @@ var TypeScript;
2895628956
}
2895728957
this.writeLineToOutput(") {");
2895828958

28959-
var members = funcDecl.block.statements.members;
28960-
var member;
28961-
for (var i = 0; i < members.length; i++) {
28962-
member = members[i];
28963-
if (member instanceof TypeScript.RoleDeclaration) {
28964-
funcDecl.isDCIContext = true;
28965-
break;
28966-
}
28967-
}
28959+
funcDecl.isDCIContext = (Object.keys(funcDecl.roleDeclarations).length > 0);
2896828960

2896928961
if (funcDecl.isDCIContext) {
2897028962
this.indenter.increaseIndent();
@@ -28973,6 +28965,12 @@ var TypeScript;
2897328965
this.indenter.decreaseIndent();
2897428966
}
2897528967

28968+
this.indenter.increaseIndent();
28969+
for (var roleName in funcDecl.roleDeclarations) {
28970+
funcDecl.roleDeclarations[roleName].emit(this);
28971+
}
28972+
this.indenter.decreaseIndent();
28973+
2897628974
if (funcDecl.isConstructor) {
2897728975
this.recordSourceMappingNameStart("constructor");
2897828976
} else if (funcDecl.isGetAccessor()) {
@@ -29190,17 +29188,24 @@ var TypeScript;
2919029188
if (initVal instanceof TypeScript.FunctionDeclaration) {
2919129189
if (Object.keys((initVal).roleDeclarations).length) {
2919229190
hasDCIContext = true;
29191+
return false;
2919329192
}
2919429193
} else if (initVal instanceof TypeScript.InvocationExpression) {
2919529194
(initVal).arguments.members.forEach(function (arg) {
2919629195
if (arg instanceof TypeScript.FunctionDeclaration) {
2919729196
if (Object.keys((arg).roleDeclarations).length) {
2919829197
hasDCIContext = true;
29198+
return false;
2919929199
}
2920029200
}
2920129201
});
2920229202
}
2920329203
});
29204+
} else if (member instanceof TypeScript.FunctionDeclaration) {
29205+
if (Object.keys((member).roleDeclarations).length) {
29206+
hasDCIContext = true;
29207+
return false;
29208+
}
2920429209
}
2920529210
});
2920629211

@@ -29992,7 +29997,7 @@ var TypeScript;
2999229997
};
2999329998

2999429999
Emitter.prototype.emitJavascript = function (ast, startLine) {
29995-
if (ast === null) {
30000+
if (ast === null || ast instanceof TypeScript.RoleDeclaration) {
2999630001
return;
2999730002
}
2999830003

@@ -30179,7 +30184,7 @@ var TypeScript;
3017930184
this.emitRoleMembers(roleDecl);
3018030185

3018130186
this.indenter.decreaseIndent();
30182-
this.writeToOutput("}");
30187+
this.writeLineToOutput("};");
3018330188

3018430189
this.recordSourceMappingEnd(roleDecl);
3018530190
this.emitComments(roleDecl, false);

bin/typescriptServices.js

Lines changed: 63369 additions & 62696 deletions
Large diffs are not rendered by default.

samples/dci/DCI.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
1-
var Context = (function () {
2-
function Context() {
3-
}
4-
Context.extend = //for plain JS version
5-
function (callback) {
6-
//return type isn't really void; void is used because otherwise TS gives this error when running `new MyContext(...`:
7-
//"Call signatures used in a 'new' expression must have a 'void' return type."
8-
return function () {
9-
var args = [];
10-
for (var _i = 0; _i < (arguments.length - 0); _i++) {
11-
args[_i] = arguments[_i + 0];
12-
}
13-
var context = new callback();
14-
if (!context.bindRoles)
15-
throw new Error('bindRoles() method not found');
1+
var exports = {
2+
Context: Context
3+
};
164

17-
context.bindRoles.apply(callback, arguments);
18-
return context;
19-
};
20-
};
21-
return Context;
22-
})();
23-
exports.Context = Context;
5+
module.exports = exports;
246

samples/dci/DCI.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
1+
var __dci_internal__ = require('typescript-dci/dci');
12

23

3-
function Account(initialBalance) {
4-
this._balance = initialBalance || 0;
4+
var Account = Context.extend(function () {
5+
var __context = this;
6+
this.__$Ledgers = { addEntry: function (message, amount) {
7+
__context.__$Ledgers.push.call(__context.Ledgers, new LedgerEntry(message, amount));
8+
}
9+
,getBalance: function () {
10+
var sum = 0;
11+
__context.__$Ledgers.each.call(__context.Ledgers, function (ledgerEntry) {
12+
sum += ledgerEntry.amount;
13+
});
14+
return sum;
15+
}
16+
};
17+
this.bindRoles = function (ledgers) {
18+
if (!ledgers)
19+
ledgers = [];
20+
__context.Ledgers = ledgers;
21+
};
22+
this.increaseBalance = function (amount) {
23+
__context.__$Ledgers.addEntry.call(__context.Ledgers, 'depositing', amount);
24+
};
25+
this.decreaseBalance = function (amount) {
26+
__context.__$Ledgers.addEntry.call(__context.Ledgers, 'withdrawing', 0 - amount);
27+
};
28+
this.getBalance = function () {
29+
return __context.__$Ledgers.getBalance.call(__context.Ledgers);
30+
};
31+
32+
//In ES5 environments, a native-like getter could be created:
33+
//(TypeScript doesn't support this syntax yet)
34+
// get balance() {
35+
// return this._balance;
36+
// }
37+
});
38+
function LedgerEntry(message, amount) {
39+
this.message = message;
40+
this.amount = amount;
541
}
642

7-
Account.prototype = {
8-
constructor: Account,
9-
increaseBalance: function (amount) {
10-
this._balance += amount;
11-
},
12-
decreaseBalance: function (amount) {
13-
this._balance -= amount;
14-
},
15-
getBalance: function () {
16-
return this._balance;
17-
}
18-
};
43+
//export the LedgerEntry constructor
44+
Account.LedgerEntry = LedgerEntry;
45+
46+
//TEMP
47+
var a = new Account();
1948
module.exports = Account;
2049

samples/dci/js/TransferMoney/Account.ts

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,65 @@
1+
///<reference path='../../DCI.d.ts'/>
2+
3+
//import DCI = require('../../DCI');
4+
5+
16
export = Account;
27

8+
var Account = Context.extend(function() {
9+
10+
this.bindRoles = function(ledgers) {
11+
if (!ledgers) ledgers = [];
12+
Ledgers <- ledgers;
13+
};
14+
15+
this.increaseBalance = function(amount) {
16+
Ledgers.addEntry('depositing', amount);
17+
};
18+
19+
this.decreaseBalance = function(amount) {
20+
Ledgers.addEntry('withdrawing', 0 - amount);
21+
};
22+
23+
this.getBalance = function() {
24+
return Ledgers.getBalance();
25+
}
26+
27+
role Ledgers {
28+
addEntry(message, amount) {
29+
Ledgers.push(new LedgerEntry(message, amount));
30+
}
31+
32+
getBalance() {
33+
var sum = 0;
34+
Ledgers.each(function(ledgerEntry) {
35+
sum += ledgerEntry.amount;
36+
});
37+
return sum;
38+
}
39+
}
40+
41+
//In ES5 environments, a native-like getter could be created:
42+
//(TypeScript doesn't support this syntax yet)
43+
// get balance() {
44+
// return this._balance;
45+
// }
46+
47+
});
48+
49+
function LedgerEntry(message, amount) {
50+
this.message = message;
51+
this.amount = amount;
52+
}
53+
54+
//export the LedgerEntry constructor
55+
Account.LedgerEntry = LedgerEntry;
56+
57+
//TEMP
58+
var a = new Account();
59+
60+
61+
62+
/*
363
function Account(initialBalance) {
464
this._balance = initialBalance || 0;
565
}
@@ -21,9 +81,8 @@ Account.prototype = {
2181
2282
//In ES5 environments, a more natural getter could be created:
2383
//(TypeScript doesn't support this syntax yet)
24-
/*
25-
get balance() {
26-
return this._balance;
27-
}
28-
*/
29-
}
84+
// get balance() {
85+
// return this._balance;
86+
// }
87+
}
88+
*/

samples/dci/js/TransferMoney/TransferMoney.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var __dci_internal__ = require('typescript-dci/dci');
2-
var DCI = require('../../DCI');
2+
var DCI = '../../DCI';
33

44

55
/**
@@ -31,7 +31,7 @@ this.__$Amount = {};
3131
__context.DestinationAccount = destinationAcct;
3232
__context.Amount = amount;
3333
};
34-
this.execute = function () {
34+
this.run = function () {
3535
__context.__$SourceAccount.transferOut.call(__context.SourceAccount);
3636
};
3737

samples/dci/js/TransferMoney/TransferMoney.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var TransferMoney = DCI.Context.extend(function() {
1717
Amount <- amount;
1818
}
1919

20-
this.execute = function() {
20+
this.run = function() {
2121
SourceAccount.transferOut();
2222
}
2323

samples/dci/js/TransferMoney/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ var dst = new Account(10);
66

77
var ctx = new TransferMoney(src, dst, 10);
88

9-
ctx.execute();
9+
//run the use case
10+
ctx.run();
1011

1112
//ctx.bindRoles(dst, src, 50);
1213
//ctx.execute();

samples/dci/js/TransferMoney/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ var dst = new Account(10);
66

77
var ctx = new TransferMoney(src, dst, 10);
88

9-
ctx.execute();
9+
//run the use case
10+
ctx.run();
1011

1112
//ctx.bindRoles(dst, src, 50);
1213
//ctx.execute();

src/compiler/typecheck/pullTypeResolution.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ module TypeScript {
509509
}
510510

511511
//DCI
512+
//DCI TODO - this may be where we need to modify the code to prevent parser errors when calling data methods on role players
512513
childDecls = decl.searchChildDecls(symbolName, PullElementKind.Role);
513514
if (childDecls.length) {
514515
return childDecls[0].getSymbol();
@@ -4641,8 +4642,15 @@ module TypeScript {
46414642
nameSymbol = this.getMemberSymbol(rhsName, PullElementKind.SomeValue, this.cachedObjectInterfaceType());
46424643
}
46434644

4644-
if (!nameSymbol) {
4645-
context.postError(this.unitPath, dottedNameAST.operand2.minChar, dottedNameAST.operand2.getLength(), DiagnosticCode.The_property_0_does_not_exist_on_value_of_type_1, [(<Identifier>dottedNameAST.operand2).actualText, lhsType.toString(enclosingDecl ? enclosingDecl.getSymbol() : null)], enclosingDecl)
4645+
if (!nameSymbol) {
4646+
//DCI TODO is it ok to be returning an ErrorTypeSymbol here?
4647+
4648+
//DCI
4649+
//Don't check whether or not the method exists, because it could be a data object method,
4650+
//and there's no way to check that at compile time without a role contract (data object interface), which we don't want to require
4651+
if (lhs.kind != PullElementKind.Role) {
4652+
context.postError(this.unitPath, dottedNameAST.operand2.minChar, dottedNameAST.operand2.getLength(), DiagnosticCode.The_property_0_does_not_exist_on_value_of_type_1, [(<Identifier>dottedNameAST.operand2).actualText, lhsType.toString(enclosingDecl ? enclosingDecl.getSymbol() : null)], enclosingDecl)
4653+
}
46464654
return this.getNewErrorTypeSymbol(null, rhsName);
46474655
}
46484656
}

0 commit comments

Comments
 (0)