-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
History of Transformations based on operation instances #157
base: staging
Are you sure you want to change the base?
Changes from all commits
4aa5edb
8b61d9e
e96221b
fd55bcf
d1c61d7
024d523
339767b
8a58471
7ec1d6f
8a2a0bc
98c0f8f
4b8673a
c108726
b2e6f62
cf8c41e
12e1b0e
19a9d88
c46da77
d4242ac
15689ee
b00b872
5e7bc65
e12f3d3
c061a99
08cd0de
133a0c2
cd948e9
1d1844d
f0dd10e
9a0aab7
4172c53
957f904
13159e4
8c49046
789b726
6e87725
b07b0de
51f8691
236b31a
e7481dc
c363c12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { EventEmitter } from "events"; | ||
|
||
const eventListener = new EventEmitter(); | ||
|
||
export default eventListener; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Joinpoint } from "../../Joinpoints.js"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file can be moved to the lara-framework repo. It needs to have a generic parameter for the join point that extends LaraJoinPoint, then the weaver generator can specialize to the join point of the specific compiler it is generating code for. |
||
|
||
export enum EventTime { | ||
BEFORE = "Before", | ||
AFTER = "After", | ||
} | ||
|
||
export class Event { | ||
public timing: EventTime; | ||
public description: string; | ||
public mainJP: Joinpoint; | ||
public returnValue?: any; | ||
public inputs: unknown[]; | ||
|
||
constructor( | ||
timing: EventTime, | ||
description: string, | ||
mainJP: Joinpoint, | ||
returnJP?: Joinpoint, | ||
...inputs: unknown[] | ||
) { | ||
this.timing = timing; | ||
this.description = description; | ||
this.mainJP = mainJP; | ||
this.returnValue = returnJP; | ||
this.inputs = inputs; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
import { registerSourceCode } from "lara-js/jest/jestHelpers"; | ||
import Clava from "../Clava"; | ||
import { FunctionJp, Joinpoint, Loop, ReturnStmt } from "../../Joinpoints"; | ||
import Query from "lara-js/api/weaver/Query"; | ||
import ophistory from "./History"; | ||
import {jest} from '@jest/globals' | ||
|
||
|
||
const code: string = `void func() { | ||
for (int i = 0; i < 1; i++){ | ||
i++; | ||
} | ||
for (int i = 0; i < 2; i++){ | ||
i++; | ||
} | ||
} | ||
|
||
void test() {} | ||
|
||
int main(int argc, char *argv[]) { | ||
func(); | ||
return 0; | ||
} | ||
`; | ||
|
||
describe("Transformation History: Multiple operations", () => { | ||
registerSourceCode(code); | ||
ophistory.checkpoint(); | ||
|
||
it("Inserts and detaches code comparison", () => { | ||
const a: string = Clava.getProgram().code; | ||
|
||
const loopStmt1 = Query.search(Loop).get().at(0) as Joinpoint; | ||
const loopStmt2 = Query.search(Loop).get().at(1) as Joinpoint; | ||
loopStmt1.insertBefore(loopStmt2.deepCopy()); | ||
loopStmt2.insertAfter(loopStmt1.deepCopy()); | ||
|
||
loopStmt1.detach(); | ||
loopStmt2.detach(); | ||
|
||
const b: string = Clava.getProgram().code; | ||
|
||
ophistory.rollback(4); | ||
const c: string = Clava.getProgram().code; | ||
|
||
expect(a).toEqual(c); | ||
expect(b).not.toEqual(c); | ||
}); | ||
|
||
it("Replaces and detach code comparison", () => { | ||
const a: string = Clava.getProgram().code; | ||
|
||
const loopStmt1 = Query.search(Loop).get().at(0) as Joinpoint; | ||
const returnStmt = Query.search(ReturnStmt).get().at(0) as Joinpoint; | ||
|
||
let cur = loopStmt1.replaceWith(returnStmt.deepCopy()); | ||
cur = cur.replaceWith("aaaaa"); | ||
cur = cur.replaceWithStrings(["aaaaa", "bbbbb", "ccccc"]); | ||
cur = cur.toComment(); | ||
cur.detach(); | ||
|
||
const b: string = Clava.getProgram().code; | ||
|
||
ophistory.rollback(5); | ||
const c: string = Clava.getProgram().code; | ||
|
||
expect(a).toEqual(c); | ||
expect(b).not.toEqual(c); | ||
}); | ||
|
||
it("Children set and removes code comparison", () => { | ||
const a: string = Clava.getProgram().code; | ||
|
||
const loopStmt1 = Query.search(Loop).get().at(0) as Joinpoint; | ||
const testFunc = Query.search(FunctionJp).get().at(1) as FunctionJp; | ||
const returnStmt = Query.search(ReturnStmt).get().at(0) as Joinpoint; | ||
|
||
testFunc.body.setFirstChild(loopStmt1.deepCopy()); | ||
testFunc.body.setLastChild(returnStmt.deepCopy()); | ||
testFunc.body.setFirstChild(loopStmt1.deepCopy()); | ||
testFunc.body.setLastChild(returnStmt.deepCopy()); | ||
|
||
const b: string = Clava.getProgram().code; | ||
|
||
ophistory.rollback(4); | ||
const c: string = Clava.getProgram().code; | ||
|
||
expect(a).toEqual(c); | ||
expect(b).not.toEqual(c); | ||
}); | ||
|
||
it("Log an error message on undo operation (single rollback)", () => { | ||
const errorSpy = jest.spyOn(global.console, "error") | ||
.mockImplementation(() => {}); | ||
|
||
const loopStmt1 = Query.search(Loop).get().at(0) as Joinpoint; | ||
|
||
loopStmt1.replaceWith("aaaa"); | ||
loopStmt1.detach(); | ||
|
||
ophistory.rollback(2); | ||
|
||
expect(errorSpy).toHaveBeenCalledTimes(1); | ||
|
||
errorSpy.mockRestore(); | ||
}); | ||
|
||
it("Checkpoints code comparison", () => { | ||
|
||
const loopStmt1 = Query.search(Loop).get().at(0) as Joinpoint; | ||
const loopStmt2 = Query.search(Loop).get().at(1) as Joinpoint; | ||
loopStmt1.insertBefore(loopStmt2.deepCopy()); | ||
loopStmt2.insertAfter(loopStmt1.deepCopy()); | ||
|
||
ophistory.checkpoint(); | ||
const a: string = Clava.getProgram().code; | ||
|
||
loopStmt1.detach(); | ||
loopStmt2.detach(); | ||
|
||
const b: string = Clava.getProgram().code; | ||
|
||
ophistory.returnToLastCheckpoint(); | ||
const c: string = Clava.getProgram().code; | ||
|
||
expect(a).toEqual(c); | ||
expect(b).not.toEqual(c); | ||
}); | ||
|
||
it("Log an error message on undo operation (checkpoint rollback)", () => { | ||
const errorSpy = jest.spyOn(global.console, "error") | ||
.mockImplementation(() => {}); | ||
|
||
const loopStmt1 = Query.search(Loop).get().at(0) as Joinpoint; | ||
|
||
ophistory.checkpoint(); | ||
loopStmt1.replaceWith("aaaa"); | ||
loopStmt1.detach(); | ||
|
||
ophistory.returnToLastCheckpoint(); | ||
|
||
expect(errorSpy).toHaveBeenCalledTimes(1); | ||
|
||
errorSpy.mockRestore(); | ||
}); | ||
|
||
it("Start and stop history recording", () => { | ||
ophistory.stop(); | ||
|
||
const a: string = Clava.getProgram().code; | ||
|
||
const loopStmt1 = Query.search(Loop).get().at(0) as Joinpoint; | ||
loopStmt1.replaceWith("aaaa"); | ||
|
||
const b: string = Clava.getProgram().code; | ||
|
||
ophistory.start(); | ||
|
||
const returnStmt = Query.search(Loop).get().at(0) as Joinpoint; | ||
const comment = returnStmt.toComment(); | ||
ophistory.checkpoint(); | ||
const c: string = Clava.getProgram().code; | ||
|
||
comment.detach(); | ||
const d: string = Clava.getProgram().code; | ||
|
||
ophistory.returnToLastCheckpoint(); | ||
const e: string = Clava.getProgram().code; | ||
|
||
expect(c).toEqual(e); | ||
expect(a).not.toEqual(b); | ||
expect(a).not.toEqual(c); | ||
expect(a).not.toEqual(d); | ||
expect(b).not.toEqual(c); | ||
expect(b).not.toEqual(d); | ||
expect(c).not.toEqual(d); | ||
}); | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Operation } from "./Operations.js" | ||
import "./OperationEvents.js" | ||
|
||
class OperationHistory { | ||
private operations: Operation[]; | ||
private locked: boolean; | ||
|
||
constructor() { | ||
this.operations = []; | ||
this.locked = true; | ||
} | ||
|
||
private lock() { | ||
this.locked = true; | ||
} | ||
|
||
private unlock() { | ||
this.locked = false; | ||
} | ||
|
||
private undo() { | ||
const op = this.operations.pop(); | ||
if (op !== undefined) { | ||
try { | ||
this.lock(); | ||
op.undo(); | ||
} catch (error) { | ||
console.error("Failed to undo operation:", error); | ||
} finally { | ||
this.unlock(); | ||
} | ||
} | ||
} | ||
|
||
start() { | ||
this.operations.length = 0; | ||
this.unlock(); | ||
} | ||
|
||
stop() { | ||
this.lock(); | ||
this.operations.length = 0; | ||
} | ||
|
||
newOperation(operation: Operation) { | ||
if (!this.locked) { | ||
this.operations.push(operation); | ||
} | ||
} | ||
|
||
rollback(n: number = 1) { | ||
if (n > 0){ | ||
while (n--){ | ||
this.undo(); | ||
} | ||
} | ||
} | ||
|
||
checkpoint() { | ||
this.operations.length = 0; | ||
this.unlock(); | ||
} | ||
|
||
returnToLastCheckpoint() { | ||
while (this.operations.length > 0) { | ||
this.undo(); | ||
} | ||
} | ||
} | ||
|
||
const ophistory = new OperationHistory(); | ||
|
||
export default ophistory; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be a global event listener, instantiated by a class in in the lara-framework repo, and other libraries/code register events to that event listener.
Only a small part of this file should be moved to the lara-framework, the part that imports the EventEmitter and instantiates it, it should provide an EventListener class (or LaraEventListener) with a static method that returns the global event listener, and possibly some utility methods (if it makes sense).