Skip to content

Commit 72fccee

Browse files
committed
Merge branch 'gh-pages' of https://github.com/LivelyKernel/lively4-core into gh-pages
2 parents d526a5c + ccdfc43 commit 72fccee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+4506
-2645
lines changed

demos/tom/Events.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
const excludedProperties = ['end', 'loc', 'start', 'traceID', 'type'];
2+
3+
export class Event {
4+
constructor(type, data, position) {
5+
this.type = type;
6+
this.position = position;
7+
this.data = data;
8+
9+
this.changes = [];
10+
this.hasChanges = false;
11+
12+
this.__type__ = 'Event';
13+
}
14+
15+
addChange(changeEvent) {
16+
this.hasChanges = true;
17+
this.changes.push(changeEvent);
18+
}
19+
20+
apply(ast) {
21+
for (const change of this.changes) {
22+
change.apply(ast);
23+
}
24+
}
25+
26+
visit(object) {
27+
object.visitEvent(this);
28+
}
29+
}
30+
31+
export class ErrorEvent extends Event {
32+
constructor() {
33+
super(...arguments);
34+
this.__type__ = 'ErrorEvent';
35+
}
36+
37+
visit(object) {
38+
object.visitErrorEvent(this);
39+
}
40+
}
41+
42+
export class ASTChangeEvent {
43+
constructor(id, propertyName, oldValue, newValue) {
44+
this.type = 'astChangeEvent';
45+
this.objectID = id;
46+
this.propertyName = propertyName;
47+
this.oldValue = oldValue;
48+
this.newValue = newValue;
49+
50+
this.__type__ = 'ASTChangeEvent';
51+
}
52+
53+
getNode(id, astNode) {
54+
if (astNode.type) {
55+
const isSearchedNode = value => value && value.traceID !== undefined && value.traceID.nodeID === id.nodeID;
56+
57+
if (isSearchedNode(astNode)) {
58+
return astNode;
59+
}
60+
61+
const keys = Object.keys(astNode).filter(key => !excludedProperties.includes(key));
62+
for (const key of keys) {
63+
const value = astNode[key];
64+
65+
66+
if (Array.isArray(value)) {
67+
for (const entry of value) {
68+
const node = this.getNode(id, entry)
69+
if (isSearchedNode(node)) {
70+
return node;
71+
}
72+
}
73+
74+
continue;
75+
}
76+
77+
if (!value) {
78+
continue;
79+
}
80+
81+
switch (typeof value) {
82+
case 'function':
83+
// ignore functions
84+
break;
85+
case 'object':
86+
// assume it is an astNode
87+
const node = this.getNode(id, value)
88+
if (isSearchedNode(node)) {
89+
return node;
90+
}
91+
break;
92+
default:
93+
// ignore value
94+
}
95+
}
96+
}
97+
}
98+
99+
resolve(copy, ast) {
100+
101+
// Todo: optimize by caching: traceID -> ASTNode
102+
if(copy.isTraceID) {
103+
return this.getNode(copy, ast);
104+
} else if(Array.isArray(copy)) {
105+
const result = [];
106+
for(const entry of copy) {
107+
result.push(this.resolve(entry, ast));
108+
}
109+
return result;
110+
} else if(copy.traceID) { // is astNode
111+
const result = {};
112+
113+
for(const key in copy) {
114+
// do not expand the reference of the object itself
115+
if(key === 'traceID') {
116+
result[key] = copy[key];
117+
continue;
118+
}
119+
result[key] = this.resolve(copy[key], ast);
120+
}
121+
122+
return result;
123+
} else {
124+
return copy;
125+
}
126+
}
127+
128+
apply(ast) {
129+
let astNode = this.getNode(this.objectID, ast);
130+
if (this.arrayProperty) {
131+
astNode = astNode[this.arrayProperty];
132+
}
133+
astNode[this.propertyName] = this.resolve(this.newValue, ast);
134+
}
135+
136+
visit(object) {
137+
object.visitASTChangeEvent(this);
138+
}
139+
}
140+
141+
export const eventTypes = {
142+
ASTChangeEvent: ASTChangeEvent,
143+
Event: Event,
144+
ErrorEvent: ErrorEvent
145+
};

demos/tom/Sections.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { Event } from 'demos/tom/Events.js';
2+
3+
export class TraceSection {
4+
constructor(name, entries = []) {
5+
this._name = name;
6+
this.entries = entries;
7+
8+
this.nodes = new Map();
9+
10+
this.changes = [];
11+
this.hasChanges = false;
12+
13+
// gets set appropriately to the associated node
14+
this.position;
15+
}
16+
17+
get name() {
18+
return this._name;
19+
}
20+
21+
get isSection() {
22+
return true;
23+
}
24+
25+
addEntry(entry) {
26+
this.entries.push(entry);
27+
}
28+
29+
privateAddChange(changeEvent) {
30+
this.hasChanges = true;
31+
this.changes.push(changeEvent);
32+
}
33+
34+
addChange(changeEvent) {
35+
if (this.entries.last) {
36+
this.entries.last.addChange(changeEvent);
37+
} else {
38+
const stub = new Event('stub');
39+
this.entries.push(stub);
40+
stub.addChange(changeEvent);
41+
}
42+
43+
this.privateAddChange(changeEvent);
44+
}
45+
46+
enterFunction() {}
47+
48+
somethingHappened() {
49+
return this.hasChanges || this.entries.length > 0;
50+
}
51+
52+
apply(ast) {
53+
for (const change of this.changes) {
54+
change.apply(ast);
55+
}
56+
}
57+
58+
visit(object) {
59+
object.visitTraceSection(this);
60+
}
61+
62+
isFunction() {
63+
return false;
64+
}
65+
}
66+
67+
export class PluginSection extends TraceSection {
68+
69+
}
70+
71+
export class FunctionSection extends TraceSection {
72+
constructor() {
73+
super(...arguments);
74+
this.enteredFunction = false;
75+
this.wasCalledByNativeCode = false;
76+
}
77+
78+
get name() {
79+
let name = '';
80+
81+
if(this.wasCalledByNativeCode) {
82+
name += 'called ';
83+
}
84+
if(!this.enteredFunction){
85+
name += 'native ';
86+
}
87+
88+
return name + `function: ${this._name}`;
89+
}
90+
91+
enterFunction() {
92+
this.enteredFunction = true;
93+
}
94+
95+
calledByNativeCode() {
96+
this.enteredFunction = true;
97+
this.wasCalledByNativeCode = true;
98+
}
99+
100+
addChange(changeEvent) {
101+
if(this.enteredFunction) {
102+
if (this.entries.last) {
103+
this.entries.last.addChange(changeEvent);
104+
} else {
105+
const stub = new Event('stub');
106+
this.entries.push(stub);
107+
stub.addChange(changeEvent);
108+
}
109+
}
110+
111+
this.privateAddChange(changeEvent);
112+
}
113+
114+
isFunction() {
115+
return true;
116+
}
117+
}

0 commit comments

Comments
 (0)