Skip to content

Commit 94e5f14

Browse files
committed
Merge branch 'gh-pages' of https://github.com/LivelyKernel/lively4-core into gh-pages
2 parents cfdc173 + acf1d0d commit 94e5f14

19 files changed

+841
-532
lines changed

demos/tom/Events.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
55+
56+
if (astNode.type) {
57+
const isSearchedNode = value => value && value.traceID !== undefined && value.traceID.nodeID === id.nodeID;
58+
59+
if (isSearchedNode(astNode)) {
60+
return astNode;
61+
}
62+
63+
const keys = Object.keys(astNode).filter(key => !excludedProperties.includes(key));
64+
for (const key of keys) {
65+
const value = astNode[key];
66+
67+
68+
if (Array.isArray(value)) {
69+
for (const entry of value) {
70+
const node = this.getNode(id, entry)
71+
if (isSearchedNode(node)) {
72+
return node;
73+
}
74+
}
75+
76+
continue;
77+
}
78+
79+
if (!value) {
80+
continue;
81+
}
82+
83+
switch (typeof value) {
84+
case 'function':
85+
// ignore functions
86+
break;
87+
case 'object':
88+
// assume it is an astNode
89+
const node = this.getNode(id, value)
90+
if (isSearchedNode(node)) {
91+
return node;
92+
}
93+
// fallthrough as we want to know if a node is replaced
94+
default:
95+
// ignore value
96+
}
97+
}
98+
}
99+
}
100+
101+
apply(ast) {
102+
let astNode = this.getNode(this.objectID, ast);
103+
if (this.arrayProperty) {
104+
astNode = astNode[this.arrayProperty];
105+
}
106+
astNode[this.propertyName] = this.newValue;
107+
}
108+
109+
revert(ast) {
110+
111+
}
112+
113+
visit(object) {
114+
object.visitASTChangeEvent(this);
115+
}
116+
}
117+
118+
export const eventTypes = {
119+
ASTChangeEvent: ASTChangeEvent,
120+
Event: Event,
121+
ErrorEvent: ErrorEvent
122+
};

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)