Skip to content

Commit 6d1452b

Browse files
committed
big steps toward presentation
SQUASHED: AUTO-COMMIT-demos-tom-babel-plugin-tracer.js,AUTO-COMMIT-demos-tom-defect-demo-plugin.js,AUTO-COMMIT-demos-tom-enumerationPlugin.js,AUTO-COMMIT-demos-tom-plugin-explorer-worker.js,AUTO-COMMIT-demos-tom-trace.js,AUTO-COMMIT-demos-tom-wrapAST.js,AUTO-COMMIT-src-components-tools-lively-plugin-explorer.html,AUTO-COMMIT-src-components-tools-lively-plugin-explorer.js,AUTO-COMMIT-src-components-tools-lively-plugin-explorer-playground.workspace,AUTO-COMMIT-src-components-tools-plugin-selector-list.json,AUTO-COMMIT-src-components-tools-trace-visualization.js,AUTO-COMMIT-src-external-babel-regenerator-runtime.js,
1 parent 2967f17 commit 6d1452b

12 files changed

+907
-94
lines changed

demos/tom/babel-plugin-tracer.js

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,54 @@ export default function({ types: t }) {
1212
return;
1313
}
1414
path.node.alreadyVisited = true;
15-
15+
1616
const returnValue = path.node.argument ? path.node.argument : t.identifier('undefined');
1717
const returnNode = callOnTrace('return', [location(path.node, state), returnValue]);
1818
path.node.argument = returnNode;
1919
}
2020
}
2121

22-
function callOnTrace(methodName, args = []) {
23-
return t.callExpression(t.memberExpression(t.identifier(Trace.traceIdenifierName), t.identifier(methodName)),
22+
function callOnTrace(methodName, args = [], shouldBeStatement = false) {
23+
let call = t.callExpression(t.memberExpression(t.identifier(Trace.traceIdenifierName), t.identifier(methodName)),
2424
args);
25+
if (shouldBeStatement) {
26+
call = t.expressionStatement(call);
27+
}
28+
return call;
2529
}
26-
30+
2731
function location(astNode, state) {
2832
const filename = state.file.opts.filename;
29-
30-
const start = astNode.loc.start;
33+
34+
const start = astNode.loc.start;
3135
const end = astNode.loc.end;
32-
36+
3337
const locationObject = {
34-
filename,
35-
startLine: start.line,
36-
startColumn: start.column,
37-
endLine: end.line,
38+
filename,
39+
startLine: start.line,
40+
startColumn: start.column,
41+
endLine: end.line,
3842
endColumn: end.column
3943
}
40-
44+
4145
const id = Trace.register(locationObject);
42-
46+
4347
return t.numericLiteral(id);
4448
}
4549

4650
function modifyFunction(name, path, state) {
4751
const body = path.get('body');
48-
body.unshiftContainer('body', t.expressionStatement(callOnTrace( 'enterFunction', [location(path.node, state), t.stringLiteral(name)])));
52+
body.unshiftContainer('body', t.expressionStatement(callOnTrace('enterFunction', [location(path.node, state), t
53+
.stringLiteral(name)
54+
])));
4955
body.pushContainer('body', t.expressionStatement(callOnTrace('leave', [location(path.node, state)])));
5056
path.traverse(returnVisitor, state);
5157
}
5258

5359
function resolveName(callee) {
54-
if(callee.type === 'MemberExpression') {
60+
if (callee.type === 'MemberExpression') {
5561
return resolveName(callee.object) + `.${callee.property.name}`;
56-
} else if(callee.type === 'CallExpression') {
62+
} else if (callee.type === 'CallExpression') {
5763
return resolveName(callee.callee);
5864
} else {
5965
return callee.name;
@@ -73,16 +79,47 @@ export default function({ types: t }) {
7379
name: 'tracer',
7480
visitor: {
7581
Program(path) {
76-
// wrapAST(path.node, {notify(){console.log(...arguments)}})
82+
// wrapAST(path.node, {notify(){console.log(...arguments)}})
7783
},
7884
CallExpression(path) {
7985
if (path.node.alreadyVisited || path.isGenerated()) {
8086
return;
8187
}
82-
const name = nameFromCallExpression(path);
88+
89+
8390
path.node.alreadyVisited = true;
84-
const log = callOnTrace('aboutToEnter', [location(path.node, this), t.stringLiteral(name)]);
85-
path.insertBefore(log)
91+
let callee = path.get('callee');
92+
let name;
93+
94+
if (t.isMemberExpression(callee)) {
95+
callee.node.computed = true;
96+
callee = path.get('callee.property');
97+
98+
name = callee.node.name || 'anonymous function';
99+
100+
const aboutToEnter = callOnTrace('aboutToEnter',
101+
[
102+
location(callee.node, this),
103+
t.stringLiteral(name)
104+
]);
105+
callee.replaceWith(t.stringLiteral(name));
106+
callee.insertBefore(aboutToEnter);
107+
return;
108+
} else if (t.isFunctionExpression(callee)) {
109+
name = callee.node.id.name || 'anonymous function';
110+
} else { // identifier or anonymous
111+
name = callee.node.name || 'anonymous function';
112+
}
113+
114+
const aboutToEnter = callOnTrace('aboutToEnter',
115+
[
116+
location(callee.node, this),
117+
t.stringLiteral(name)
118+
]);
119+
120+
callee.insertBefore(aboutToEnter);
121+
122+
86123
},
87124
ArrowFunctionExpression(path) {
88125

@@ -101,42 +138,44 @@ export default function({ types: t }) {
101138
path.insertAfter(callOnTrace('endLoop', [location(path.node, this)]))
102139
},
103140
ForStatement(path) {
104-
path.get('body').unshiftContainer('body', callOnTrace('nextLoopIteration', [location(path.node, this), ...path.node.init.declarations.map(declaration => declaration.id)]));
141+
path.get('body').unshiftContainer('body', callOnTrace('nextLoopIteration', [location(path.node, this),
142+
...path.node.init.declarations.map(declaration => declaration.id)
143+
]));
105144
},
106145
ForOfStatement(path) {
107-
146+
108147
},
109148
ForInStatement(path) {
110-
149+
111150
},
112-
Conditional(path) {
113-
if(path.node.alreadyVisited) {
151+
Conditional(path) {
152+
if (path.node.alreadyVisited) {
114153
return;
115154
}
116-
155+
117156
path.node.alreadyVisited = true;
118-
157+
119158
path.node.test = callOnTrace('conditionTest', [
120-
location(path.node.test, this),
159+
location(path.node.test, this),
121160
path.node.test
122161
]);
123-
162+
124163
// do not log else ifs
125-
if(path.parent.type !== 'IfStatement') {
164+
if (path.parent.type !== 'IfStatement') {
126165
path.insertBefore(callOnTrace('beginCondition', [
127-
location(path.node, this),
166+
location(path.node, this),
128167
t.stringLiteral(path.type)
129168
]));
130169
path.insertAfter(callOnTrace('endCondition', [location(path.node, this)]));
131-
}
170+
}
132171
},
133-
172+
134173
AssignmentExpression(path) {
135-
if(path.isGenerated()) {
174+
if (path.isGenerated()) {
136175
return;
137176
}
138177
path.node.right = callOnTrace('assignment', [
139-
location(path.node, this),
178+
location(path.node, this),
140179
t.stringLiteral(resolveName(path.node.left)), path.node.right
141180
]);
142181
}

demos/tom/defect-demo-plugin.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ export default function({ types: t }) {
33
name: 'demo-plugin',
44
visitor: {
55
Conditional(path) {
6-
path.get('test').insertBefore(t.stringLiteral('start'));
7-
path.get('test').insertAfter(t.stringLiteral('after'));
6+
const startNode = t.stringLiteral('start');
7+
const endNode = t.stringLiteral('after');
8+
path.get('test').insertBefore(startNode);
9+
path.get('test').insertAfter(endNode);
810
},
911
AssignmentExpression(path) {
10-
alert(path.node.left.name)
12+
console.log(path.node.left.name)
1113
}
1214
}
1315
}

demos/tom/enumerationPlugin.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export default function() {
2+
3+
let counter = 0;
4+
5+
const visitor = {
6+
enter(path) {
7+
if(!path.node.traceid) {
8+
path.node.traceid = {round: 0, count: counter++};
9+
}
10+
}
11+
};
12+
13+
return {
14+
visitor: {
15+
Program: {
16+
enter(path) {
17+
path.node.traceid = counter++;
18+
path.traverse(visitor)
19+
},
20+
exit(path) {
21+
path.node.inspect()
22+
}
23+
}
24+
}
25+
}
26+
}

demos/tom/plugin-explorer-worker.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,27 @@ importScripts(lively4url + '/demos/tom/plugin-explorer-systemjs-config.js')
1212
// not sure why I need this but without it I cannot import the plugins
1313
System.import(pluginTransformationPlugin);
1414

15-
const enumerationPlugin = function() {
16-
17-
let counter = 0;
18-
15+
const enumerationPlugin = createTraceID => function() {
1916
const visitor = {
2017
enter(path) {
21-
path.node.traceid = counter++;
18+
if(!path.node.traceID) {
19+
path.node.traceID = createTraceID();
20+
}
2221
}
2322
};
2423

2524
return {
2625
visitor: {
2726
Program(path) {
28-
path.node.traceid = counter++;
27+
path.node.traceID = createTraceID();
2928
path.traverse(visitor)
3029
}
3130
}
3231
}
3332
}
3433

35-
const enumerationConfig = {
36-
plugins: [enumerationPlugin]
34+
const enumerationConfig = createTraceID => {
35+
return {plugins: [enumerationPlugin(createTraceID)]}
3736
}
3837

3938
async function importPlugin(url) {
@@ -87,6 +86,10 @@ self.onmessage = function(msg) {
8786
const trace = new Trace();
8887
// make it globally available for use in plugins
8988
window[Trace.traceIdenifierName] = trace;
89+
90+
function createTraceID() {
91+
return trace.createTraceID();
92+
}
9093

9194
importPlugins(msg.data.urls)
9295
.then(function(modules) {
@@ -103,11 +106,17 @@ self.onmessage = function(msg) {
103106
};
104107

105108
trace.startTraversion();
106-
const ast = babel.transform(msg.data.source, enumerationConfig).ast;
109+
const ast = babel.transform(msg.data.source, enumerationConfig(createTraceID)).ast;
107110
const oldASTAsString = JSON.stringify(ast);
108111

109112
wrapAST(ast, trace);
110-
const result = babel.transformFromAst(ast, undefined, config);
113+
let result
114+
//try {
115+
result = babel.transformFromAst(ast, undefined, config);
116+
/*} catch (e) {
117+
trace.error(e);
118+
}*/
119+
111120

112121
// const result = babel.transform(msg.data.source, config);
113122
postMessage({

0 commit comments

Comments
 (0)