Skip to content

Commit 68986ba

Browse files
committed
changed data format
SQUASHED: AUTO-COMMIT-demos-tom-babel-plugin-tracer.js,AUTO-COMMIT-demos-tom-defect-demo-plugin.js,AUTO-COMMIT-demos-tom-plugin-backup.js,AUTO-COMMIT-demos-tom-plugin-explorer-worker.js,AUTO-COMMIT-demos-tom-trace.js,AUTO-COMMIT-src-components-tools-lively-plugin-explorer-playground.workspace,AUTO-COMMIT-src-components-tools-trace-visualization.js,
1 parent 27631a5 commit 68986ba

File tree

7 files changed

+121
-101
lines changed

7 files changed

+121
-101
lines changed

demos/tom/babel-plugin-tracer.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Trace from 'demos/tom/trace.js';
22
import wrapAST from 'demos/tom/wrapAST.js';
33

4+
let pluginDefinedTrace = false;
5+
46
export default function({ types: t }) {
57
function error(path, message) {
68
throw path.buildCodeFrameError(message);
@@ -29,7 +31,7 @@ export default function({ types: t }) {
2931
}
3032

3133
function location(astNode, state) {
32-
const id = Trace.register(astNode, state);
34+
const id = window[Trace.traceIdenifierName].register(astNode, state);
3335

3436
return t.numericLiteral(id);
3537
}
@@ -64,6 +66,17 @@ export default function({ types: t }) {
6466

6567
return {
6668
name: 'tracer',
69+
pre() {
70+
if(!window[Trace.traceIdenifierName]) {
71+
window[Trace.traceIdenifierName] = new Trace();
72+
pluginDefinedTrace = true;
73+
}
74+
},
75+
post() {
76+
if(pluginDefinedTrace) {
77+
delete window[Trace.traceIdenifierName];
78+
}
79+
},
6780
visitor: {
6881
Program(path) {
6982
// wrapAST(path.node, {notify(){console.log(...arguments)}})

demos/tom/defect-demo-plugin.js

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,3 @@ export default function({ types: t }) {
1919
}
2020
}
2121
}
22-
23-
24-
25-
26-
27-
28-
29-
30-
31-
32-
33-
34-
35-
36-
37-
38-
39-
40-
41-
42-
43-
44-
45-
46-
47-
48-
49-
50-
51-
52-
53-
54-
/*
55-
if(path.isGenerated()) {
56-
return;
57-
}
58-
*/
59-

demos/tom/plugin-backup.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Trace from 'demos/tom/trace.js';
22
import wrapAST from 'demos/tom/wrapAST.js';
33

4+
let pluginDefinedTrace = false;
5+
46
export default function({ types: t }) {
57
function error(path, message) {
68
throw path.buildCodeFrameError(message);
@@ -29,7 +31,7 @@ export default function({ types: t }) {
2931
}
3032

3133
function location(astNode, state) {
32-
const id = Trace.register(astNode, state);
34+
const id = window[Trace.traceIdenifierName].register(astNode, state);
3335

3436
return t.numericLiteral(id);
3537
}
@@ -64,6 +66,17 @@ export default function({ types: t }) {
6466

6567
return {
6668
name: 'tracer',
69+
pre() {
70+
if(!window[Trace.traceIdenifierName]) {
71+
window[Trace.traceIdenifierName] = new Trace();
72+
pluginDefinedTrace = true;
73+
}
74+
},
75+
post() {
76+
if(pluginDefinedTrace) {
77+
delete window[Trace.traceIdenifierName];
78+
}
79+
},
6780
visitor: {
6881
Program(path) {
6982
// wrapAST(path.node, {notify(){console.log(...arguments)}})

demos/tom/plugin-explorer-worker.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,10 @@ self.onmessage = function(msg) {
145145
oldAST: oldASTAsString,
146146
transformedAST: JSON.stringify(result && result.ast),
147147
transformedCode: result && result.code,
148-
trace: JSON.stringify(trace),
149-
locations: Trace.locations
148+
trace: trace.serialize()
150149
});
151150

152-
for (const url of msg.data.urls) {
153-
unloadModule(url);
154-
}
151+
msg.data.urls.forEach(unloadModule)
155152
})
156153
})
157154
}

demos/tom/trace.js

Lines changed: 83 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ class ASTChangeEvent {
4242
}
4343

4444
getNode(id, astNode) {
45-
45+
4646

4747
if (astNode.type) {
4848
const isSearchedNode = value => value && value.traceID !== undefined && value.traceID.nodeID === id.nodeID;
49-
50-
if(isSearchedNode(astNode)) {
49+
50+
if (isSearchedNode(astNode)) {
5151
return astNode;
5252
}
53-
53+
5454
const keys = Object.keys(astNode).filter(key => !excludedProperties.includes(key));
5555
for (const key of keys) {
5656
const value = astNode[key];
@@ -59,7 +59,7 @@ class ASTChangeEvent {
5959
if (Array.isArray(value)) {
6060
for (const entry of value) {
6161
const node = this.getNode(id, entry)
62-
if(isSearchedNode(node)) {
62+
if (isSearchedNode(node)) {
6363
return node;
6464
}
6565
}
@@ -78,7 +78,7 @@ class ASTChangeEvent {
7878
case 'object':
7979
// assume it is an astNode
8080
const node = this.getNode(id, value)
81-
if(isSearchedNode(node)) {
81+
if (isSearchedNode(node)) {
8282
return node;
8383
}
8484
// fallthrough as we want to know if a node is replaced
@@ -93,7 +93,7 @@ class ASTChangeEvent {
9393
let astNode = this.getNode(this.objectID, ast);
9494
if (this.arrayProperty) {
9595
astNode = astNode[this.arrayProperty];
96-
}
96+
}
9797
astNode[this.propertyName] = this.newValue;
9898
}
9999

@@ -170,10 +170,11 @@ class Trace {
170170
constructor() {
171171
this._log = [];
172172
this.counter = 0;
173-
}
174173

175-
register(name) {
176-
console.log(name);
174+
this.locations = [];
175+
this.filenames = [];
176+
this.fileRegistry = new Map();
177+
this.astNodeRegistry = new Map();
177178
}
178179

179180
log(event) {
@@ -190,6 +191,73 @@ class Trace {
190191
nodeID: this.counter++
191192
}
192193
}
194+
195+
serialize() {
196+
const serialized = {};
197+
198+
serialized._log = JSON.stringify(this._log);
199+
serialized.locations = JSON.stringify(this.locations);
200+
serialized.filenames = JSON.stringify(this.filenames);
201+
serialized.astNodeRegistry = JSON.stringify([...this.astNodeRegistry]);
202+
203+
return serialized;
204+
}
205+
206+
static deserializedFrom(obj) {
207+
const trace = new Trace();
208+
209+
trace._log = JSON.parse(obj._log);
210+
trace.locations = JSON.parse(obj.locations);
211+
trace.filenames = JSON.parse(obj.filenames);
212+
trace.astNodeRegistry = new Map(JSON.parse(obj.astNodeRegistry));
213+
214+
return trace;
215+
}
216+
217+
/* */
218+
219+
register(astNode, state) {
220+
if (this.astNodeRegistry.has(astNode)) {
221+
return this.astNodeRegistry.get(astNode);
222+
}
223+
224+
const filename = state.file.opts.filename;
225+
let fileID;
226+
227+
if (this.fileRegistry.has(filename)) {
228+
fileID = this.fileRegistry.get(filename)
229+
} else {
230+
fileID = this.filenames.push(filename) - 1;
231+
this.fileRegistry.set(filename, fileID);
232+
}
233+
234+
const start = astNode.loc.start;
235+
const end = astNode.loc.end;
236+
237+
const location = [
238+
fileID,
239+
start.line,
240+
start.column,
241+
end.line,
242+
end.column
243+
];
244+
245+
const id = this.locations.push(location) - 1;
246+
this.astNodeRegistry.set(astNode, id);
247+
248+
return id;
249+
}
250+
251+
resolve(locationID) {
252+
const location = this.locations[locationID];
253+
return {
254+
filename: this.filenames[location[0]],
255+
startLine: location[1],
256+
startColumn: location[2],
257+
endLine: location[3],
258+
endColumn: location[4]
259+
}
260+
}
193261

194262
/* AST changes */
195263

@@ -239,12 +307,12 @@ class Trace {
239307
nextLoopIteration(position, ...args) {
240308
this.log(new Event('nextLoopIteration', args.map(clone), position));
241309
}
242-
310+
243311
forIterator(position, iterator) {
244312
this.log(new Event('forIterator', iterator, position));
245313
return iterator;
246314
}
247-
315+
248316
forKeys(position, keys) {
249317
this.log(new Event('forKeys', keys, position));
250318
return keys;
@@ -335,56 +403,22 @@ class Trace {
335403
}
336404

337405
Trace.traceIdenifierName = '__currentTrace__';
338-
Trace.locations = [];
339-
Trace.fileRegistry = new Map();
340-
Trace.astNodeRegistry = new Map();
341-
342-
Trace.register = function(astNode, state) {
343-
if (Trace.astNodeRegistry.has(astNode)) {
344-
return Trace.astNodeRegistry.get(astNode);
345-
}
346-
347-
const filename = state.file.opts.filename;
348-
349-
const start = astNode.loc.start;
350-
const end = astNode.loc.end;
351-
352-
const locationObject = {
353-
filename,
354-
startLine: start.line,
355-
startColumn: start.column,
356-
endLine: end.line,
357-
endColumn: end.column
358-
}
359-
360-
const id = Trace.locations.push(locationObject) - 1;
361-
Trace.astNodeRegistry.set(astNode, id);
362-
363-
return id;
364-
}
365-
366-
Trace.resolve = (number, locations) => locations[number];
367406

368407
Trace.on = async function(source, pluginsUrls) {
369-
const data = await loadPlugin(source, pluginsUrls)
408+
const data = await loadPlugin(source, pluginsUrls);
370409
const obj = {
371410
locations: data.locations,
372411
oldAST: JSON.parse(data.oldAST),
373412
transformedAST: JSON.parse(data.transformedAST || '{}'),
374-
trace: Object.assign(new Trace(), JSON.parse(data.trace)),
413+
trace: Trace.deserializedFrom(data.trace),
375414
transformedCode: data.transformedCode
376415
};
377416

378417
const trace = obj.trace;
379418

380-
for (const entry of trace._log) {
381-
entry.position = obj.locations[entry.position];
382-
//console.log(entry)
383-
}
384-
385419
trace.oldAST = obj.oldAST;
386420
trace.transformedAST = obj.transformedAST;
387-
421+
388422

389423
trace.analyze();
390424
return trace;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"source":"/src/components/tools/lively-ast-explorer-example-source.js","plugin":"https://lively-kernel.org/lively4/lively4-tom/demos/tom/defect-demo-plugin.js","options":{"autoUpdateAST":true,"autoUpdateTransformation":true,"autoExecute":true,"systemJS":false,"autoRunTests":false,"autoSaveWorkspace":false},"pluginSelection":[{"url":"https://lively-kernel.org/lively4/lively4-tom/demos/tom/defect-demo-plugin.js"}]}
1+
{"source":"/src/components/tools/lively-ast-explorer-example-source.js","plugin":"https://lively-kernel.org/lively4/lively4-tom/src/client/reactive/babel-plugin-active-expression-rewriting/index.js","options":{"autoUpdateAST":true,"autoUpdateTransformation":true,"autoExecute":true,"systemJS":false,"autoRunTests":false,"autoSaveWorkspace":false},"pluginSelection":[{"url":"https://lively-kernel.org/lively4/lively4-tom/src/client/reactive/babel-plugin-active-expression-rewriting/index.js"}]}

src/components/tools/trace-visualization.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ export default class TraceVisualization extends Morph {
1414

1515

1616
this.currentURL = null;
17-
17+
18+
// declare variables used later on for documentation
19+
this.trace;
20+
this.curAST;
21+
this.nextAST;
1822
}
1923

2024

@@ -87,7 +91,7 @@ export default class TraceVisualization extends Morph {
8791

8892
me.updateAST(section, elm);
8993

90-
const position = elm.position;
94+
const position = me.trace.resolve(elm.position);
9195
if(!position) {
9296
return;
9397
}
@@ -96,8 +100,6 @@ export default class TraceVisualization extends Morph {
96100
await me.editor.loadFile();
97101
}
98102

99-
100-
101103
me.editorDoc.scrollIntoView({
102104
line: position.startLine - 1,
103105
ch: position.startColumn
@@ -121,8 +123,6 @@ export default class TraceVisualization extends Morph {
121123
subEntry.innerText = 'Section' + traceSection.name;
122124
}
123125
});
124-
125-
126126
}
127127
} else {
128128
body.innerHTML = '';
@@ -162,6 +162,7 @@ export default class TraceVisualization extends Morph {
162162
updateAST(section, entry) {
163163
this.curAST = JSON.parse(JSON.stringify(this.trace.oldAST));
164164
this.nextAST;
165+
165166
const sections = this.trace.sections;
166167
for (const sec of sections) {
167168
if (sec === section) {

0 commit comments

Comments
 (0)