Skip to content

Commit 6ba18b3

Browse files
committed
fixed various bugs
SQUASHED: AUTO-COMMIT-demos-tom-babel-plugin-tracer.js,AUTO-COMMIT-demos-tom-copyAST.js,AUTO-COMMIT-demos-tom-Events.js,AUTO-COMMIT-demos-tom-playground.js,AUTO-COMMIT-demos-tom-plugin-backup.js,AUTO-COMMIT-demos-tom-plugin-explorer-worker.js,AUTO-COMMIT-demos-tom-plugin-load-promise.js,AUTO-COMMIT-demos-tom-trace.js,AUTO-COMMIT-demos-tom-TraceLogParser.js,AUTO-COMMIT-demos-tom-wrapAST.js,AUTO-COMMIT-src-client-reactive-babel-plugin-active-expression-rewriting-index.js,AUTO-COMMIT-src-components-tools-lively-plugin-explorer-playground.workspace,AUTO-COMMIT-src-components-tools-trace-visualization.js,
1 parent 8953be1 commit 6ba18b3

File tree

12 files changed

+144
-87
lines changed

12 files changed

+144
-87
lines changed

demos/tom/Events.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class ASTChangeEvent {
5151
}
5252

5353
getNode(id, astNode) {
54-
if (astNode.type) {
54+
if (astNode && astNode.type) {
5555
const isSearchedNode = value => value && value.traceID !== undefined && value.traceID.nodeID === id.nodeID;
5656

5757
if (isSearchedNode(astNode)) {
@@ -97,6 +97,9 @@ export class ASTChangeEvent {
9797
}
9898

9999
resolve(copy, ast) {
100+
if (!copy) {
101+
return copy;
102+
}
100103

101104
// Todo: optimize by caching: traceID -> ASTNode
102105
if(copy.isTraceID) {

demos/tom/TraceLogParser.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ export default class TraceLogParser {
7676
const returnEntry = this.consume();
7777
section.addEntry(this.instantiateEvent(returnEntry));
7878

79-
if(returnEntry.position === this.peek().position) {
80-
this.match('left');
81-
}
82-
debugger
83-
8479
throw new EarlyReturn('function');
8580
} else if(this.matchPeek('leave')) {
8681
const leaveEntry = this.consume();
@@ -91,10 +86,8 @@ export default class TraceLogParser {
9186
}
9287
else if(this.matchPeek('enterPlugin')) {
9388
section.addEntry(this.parsePlugin());
94-
continue;
9589
} else if(this.matchPeek('aboutToEnter')) {
9690
this.parseFunction(section, [...higherSections, section]);
97-
continue;
9891
} else if(this.matchPeek('enterFunction')) {
9992
if(!section.isFunction() || (section.isFunction() && section._name !== this.peek().data)) {
10093
// entered a function that got called from native code or babel
@@ -103,10 +96,8 @@ export default class TraceLogParser {
10396
} else {
10497
this.match('enterFunction');
10598
}
106-
continue;
10799
} else if(this.matchPeek('beginCondition')) {
108100
this.parseCondition(section, [...higherSections, section]);
109-
continue;
110101
} else if(this.matchPeek('astChangeEvent')) {
111102
const change = this.consumeAsEvent();
112103

@@ -115,18 +106,17 @@ export default class TraceLogParser {
115106
}
116107

117108
section.addChange(change);
118-
continue;
119109
} else if(this.matchPeek('error')) {
120110
section.addEntry(this.consumeAsEvent());
121-
// break out from nesting
111+
122112
throw new EarlyReturn('error');
123113
} else if(this.match('endCondition')) {
124114
return section;
125115
} else if(this.match('left')) {
126116
return section;
117+
} else {
118+
section.addEntry(this.consumeAsEvent());
127119
}
128-
129-
section.addEntry(this.consumeAsEvent());
130120
}
131121

132122
return section;
@@ -165,6 +155,11 @@ export default class TraceLogParser {
165155
}
166156
}
167157

158+
// Functions that got entered from native code or babel get not left event
159+
if(fun.position === this.peek().position) {
160+
this.match('left');
161+
}
162+
168163
return fun;
169164
}
170165

demos/tom/babel-plugin-tracer.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ export default function({ types: t }) {
2222
}
2323

2424
function callOnTrace(methodName, args = [], shouldBeStatement = false) {
25-
let call = t.callExpression(t.memberExpression(t.identifier(Trace.traceIdentifierName), t.identifier(methodName)),
26-
args);
25+
let call = t.callExpression(t.memberExpression(t.identifier(Trace.traceIdentifierName), t.identifier(methodName)), args);
2726
if (shouldBeStatement) {
2827
call = t.expressionStatement(call);
2928
}
@@ -74,7 +73,7 @@ export default function({ types: t }) {
7473
},
7574
post() {
7675
if(pluginDefinedTrace) {
77-
delete window[Trace.traceIdentifierName];
76+
window[Trace.traceIdentifierName] = undefined;
7877
}
7978
},
8079
visitor: {
@@ -86,32 +85,41 @@ export default function({ types: t }) {
8685
return;
8786
}
8887

89-
9088
path.node.alreadyVisited = true;
9189
let callee = path.get('callee');
9290
let name;
9391

9492
if (t.isMemberExpression(callee)) {
95-
callee.node.computed = true;
96-
callee = path.get('callee.property');
93+
if(callee.node.computed) {
94+
callee = path.get('callee.property');
9795

98-
name = callee.node.name || 'anonymous function';
96+
name = callee.node;
97+
} else {
98+
callee.node.computed = true;
99+
callee = path.get('callee.property');
100+
101+
name = t.stringLiteral(callee.node.name || 'anonymous function');
102+
}
103+
104+
105+
106+
const loc = location(callee.node, this);
99107

100108
const aboutToEnter = callOnTrace('aboutToEnter',
101109
[
102-
location(callee.node, this),
103-
t.stringLiteral(name)
110+
loc,
111+
name
104112
]);
105-
callee.replaceWith(t.stringLiteral(name));
113+
callee.replaceWith(name);
106114
callee.insertBefore(aboutToEnter);
107115

108116
const left = callOnTrace('left',
109117
[
110-
location(path.node, this),
118+
loc,
111119
path.node
112120
]);
113121

114-
path.replaceWith(left)
122+
path.replaceWith(left)
115123
return;
116124
} else if (t.isFunctionExpression(callee)) {
117125
name = callee.node.id.name || 'anonymous function';
@@ -154,7 +162,7 @@ export default function({ types: t }) {
154162
]));
155163
} else {
156164
modifyFunction('anonymous function', path, this);
157-
}
165+
}
158166
},
159167
"ClassMethod|ObjectMethod"(path) {
160168
const key = path.node.key;

demos/tom/copyAST.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
1-
function copyArrayOfRound(arr, roundNumber) {
2-
const copy = [];
3-
4-
for(const entry of arr) {
5-
copy.push(copyASTPartsOfRound(entry, roundNumber));
6-
}
7-
8-
return copy;
1+
import { wrapCurrentASTNode } from 'demos/tom/wrapAST.js';
2+
3+
function copyArray(array, observer) {
4+
return array.map(elm => copyAndWrapUnkownSubtree(elm, observer));
95
}
106

11-
// Todo: merge wrap and copy => copy all nodes without traceID
12-
// roundNumber could result in unncessary copies as in one round there could be multiple edits
13-
export default function copyASTPartsOfRound(object, roundNumber) {
7+
// Copies subtree of new ASTNodes. Every ASTNode it visits gets wrapped and a traceID assigned.
8+
export default function copyAndWrapUnkownSubtree(object, observer) {
149
// simply check if the object is an astNode
1510
if (object && object.type) {
1611
// if already in AST return only a reference
17-
if (object.traceID && object.traceID.pluginRoundID !== roundNumber) {
12+
if (object.traceID) {
1813
return object.traceID;
1914
}
2015

16+
wrapCurrentASTNode(object, observer);
17+
2118
const objectCopy = {};
2219
const keys = Object.keys(object).filter(key => key[0] !== '_');
2320
for (const key of keys) {
2421
const value = object[key];
2522

2623
if (Array.isArray(value)) {
27-
objectCopy[key] = copyArrayOfRound(value, roundNumber);
24+
objectCopy[key] = copyArray(value, observer);
2825

2926
continue;
3027
}
3128

32-
if (value === null) {
29+
if (!value) {
3330
objectCopy[key] = value;
3431
continue;
3532
}
@@ -39,7 +36,7 @@ export default function copyASTPartsOfRound(object, roundNumber) {
3936
// ignore functions
4037
break;
4138
case 'object':
42-
objectCopy[key] = copyASTPartsOfRound(value, roundNumber);
39+
objectCopy[key] = copyAndWrapUnkownSubtree(value, observer);
4340
break;
4441
default:
4542
objectCopy[key] = value;
@@ -49,7 +46,9 @@ export default function copyASTPartsOfRound(object, roundNumber) {
4946
return objectCopy;
5047
} else {
5148
if(Array.isArray(object)) {
52-
return copyArrayOfRound(object, roundNumber);
49+
return copyArray(object, observer);
50+
} else if (!object) {
51+
return object;
5352
} else {
5453
// do a simple copy
5554
return JSON.parse(JSON.stringify(object));

demos/tom/playground.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ export default function({types: t}) {
44
visitor: {
55
Conditional(path) {
66
debugger
7-
path.get('test').insertAfter(t.stringLiteral('afterTest'));
7+
const string = t.stringLiteral('afterTest');
8+
path.get('test')
9+
.insertAfter(string);
810
},
911

1012
AssignmentExpression(path) {
11-
debugger
12-
path.node.loc.start
13+
console.log(path.node.loc.start);
1314
}
1415
}
1516
}

demos/tom/plugin-backup.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ export default function({ types: t }) {
2222
}
2323

2424
function callOnTrace(methodName, args = [], shouldBeStatement = false) {
25-
let call = t.callExpression(t.memberExpression(t.identifier(Trace.traceIdentifierName), t.identifier(methodName)),
26-
args);
25+
let call = t.callExpression(t.memberExpression(t.identifier(Trace.traceIdentifierName), t.identifier(methodName)), args);
2726
if (shouldBeStatement) {
2827
call = t.expressionStatement(call);
2928
}
@@ -89,24 +88,35 @@ export default function({ types: t }) {
8988
path.node.alreadyVisited = true;
9089
let callee = path.get('callee');
9190
let name;
91+
debugger
9292

9393
if (t.isMemberExpression(callee)) {
94-
callee.node.computed = true;
95-
callee = path.get('callee.property');
94+
if(callee.node.computed) {
95+
callee = path.get('callee.property');
9696

97-
name = callee.node.name || 'anonymous function';
97+
name = callee.node;
98+
} else {
99+
callee.node.computed = true;
100+
callee = path.get('callee.property');
101+
102+
name = t.stringLiteral(callee.node.name || 'anonymous function');
103+
}
104+
105+
106+
107+
const loc = location(callee.node, this);
98108

99109
const aboutToEnter = callOnTrace('aboutToEnter',
100110
[
101-
location(callee.node, this),
102-
t.stringLiteral(name)
111+
loc,
112+
name
103113
]);
104-
callee.replaceWith(t.stringLiteral(name));
114+
callee.replaceWith(name);
105115
callee.insertBefore(aboutToEnter);
106116

107117
const left = callOnTrace('left',
108118
[
109-
location(path.node, this),
119+
loc,
110120
path.node
111121
]);
112122

demos/tom/plugin-explorer-worker.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ async function unloadModule(path) {
5454
System.registry.delete(normalizedPath);
5555
// #Hack #issue in SystemJS babel syntax errors do not clear errors
5656
System['@@registerRegistry'][normalizedPath] = undefined;
57-
debugger
5857
delete System.loads[normalizedPath]
5958
}
6059

@@ -109,7 +108,6 @@ self.onmessage = function(msg) {
109108

110109
const preloadedPlugins = new Set(Object.keys(System['@@registerRegistry']));
111110

112-
debugger
113111

114112
const trace = new Trace();
115113
// make it globally available for use in plugins
@@ -145,7 +143,8 @@ self.onmessage = function(msg) {
145143
result = null;
146144
trace.error(e);
147145
}
148-
debugger;
146+
147+
debugger
149148

150149
const pluginsToUnload = Object.keys(System['@@registerRegistry']).filter(plugin => !
151150
preloadedPlugins.has(plugin));
@@ -154,8 +153,6 @@ self.onmessage = function(msg) {
154153
.then(_ => {
155154
postMessage({
156155
oldAST: oldASTAsString,
157-
transformedAST: JSON.stringify(result && result.ast),
158-
transformedCode: result && result.code,
159156
trace: trace.serialize()
160157
});
161158
})

demos/tom/plugin-load-promise.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
const worker = new Worker('demos/tom/plugin-explorer-worker.js')
2-
31
export default function(source, urls) {
42
return new Promise((resolve, reject) => {
3+
const worker = new Worker('demos/tom/plugin-explorer-worker.js')
54
worker.onmessage = function(msg) {
6-
resolve(msg.data)
5+
resolve(msg.data);
6+
worker.terminate();
77
}
88
worker.onerror = function(msg) {
9-
reject(msg)
9+
reject(msg);
10+
worker.terminate();
1011
}
1112
worker.postMessage({source, urls})
1213
})

demos/tom/trace.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ export default class Trace {
7171
const obj = {
7272
locations: data.locations,
7373
oldAST: JSON.parse(data.oldAST),
74-
transformedAST: JSON.parse(data.transformedAST || '{}'),
7574
trace: Trace.deserializedFrom(data.trace),
76-
transformedCode: data.transformedCode
7775
};
7876

7977
const trace = obj.trace;

0 commit comments

Comments
 (0)