Skip to content

Commit b60b213

Browse files
WangdahaiWangdahai
authored andcommitted
conductor interface
1 parent bc6a7fb commit b60b213

File tree

124 files changed

+2091
-4
lines changed

Some content is hidden

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

124 files changed

+2091
-4
lines changed

src/common/Constant.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const enum Constant {
2+
PROTOCOL_VERSION = 0,
3+
PROTOCOL_MIN_VERSION = 0,
4+
}

src/common/ds/MessageQueue.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Queue } from "./Queue";
2+
3+
export class MessageQueue<T> {
4+
private readonly __inputQueue: Queue<T> = new Queue();
5+
private readonly __promiseQueue: Queue<Function> = new Queue();
6+
7+
push(item: T) {
8+
if (this.__promiseQueue.length !== 0) this.__promiseQueue.pop()(item);
9+
else this.__inputQueue.push(item);
10+
}
11+
12+
async pop(): Promise<T> {
13+
if (this.__inputQueue.length !== 0) return this.__inputQueue.pop();
14+
return new Promise((resolve, _reject) => {
15+
this.__promiseQueue.push(resolve);
16+
});
17+
}
18+
19+
tryPop(): T | undefined {
20+
if (this.__inputQueue.length !== 0) return this.__inputQueue.pop();
21+
return undefined;
22+
}
23+
24+
constructor() {
25+
this.push = this.push.bind(this);
26+
}
27+
}

src/common/ds/Queue.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* A stack-based queue implementation.
3+
* `push` and `pop` run in amortized constant time.
4+
*/
5+
export class Queue<T> {
6+
/** The output stack. */
7+
private __s1: T[] = [];
8+
/** The input stack. */
9+
private __s2: T[] = [];
10+
11+
/**
12+
* Adds an item to the queue.
13+
* @param item The item to be added to the queue.
14+
*/
15+
push(item: T) {
16+
this.__s2.push(item);
17+
}
18+
19+
/**
20+
* Removes an item from the queue.
21+
* @returns The item removed from the queue.
22+
* @throws If the queue is empty.
23+
*/
24+
pop(): T {
25+
if (this.__s1.length === 0) {
26+
if (this.__s2.length === 0) throw new Error("queue is empty");
27+
let temp = this.__s1;
28+
this.__s1 = this.__s2.reverse();
29+
this.__s2 = temp;
30+
}
31+
return this.__s1.pop()!; // as the length is nonzero
32+
}
33+
34+
/**
35+
* The length of the queue.
36+
*/
37+
get length() {
38+
return this.__s1.length + this.__s2.length;
39+
}
40+
41+
/**
42+
* Makes a copy of the queue.
43+
* @returns A copy of the queue.
44+
*/
45+
clone(): Queue<T> {
46+
const newQueue = new Queue<T>();
47+
newQueue.__s1 = [...this.__s1];
48+
newQueue.__s2 = [...this.__s2];
49+
return newQueue;
50+
}
51+
}

src/common/ds/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { MessageQueue } from "./MessageQueue";
2+
export { Queue } from "./Queue";
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { ErrorType } from "./ErrorType";
2+
3+
/**
4+
* Generic Conductor Error.
5+
*/
6+
export class ConductorError extends Error {
7+
override name = "ConductorError";
8+
readonly errorType: ErrorType | string = ErrorType.UNKNOWN;
9+
10+
constructor(message: string) {
11+
super(message);
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ConductorError } from "./ConductorError";
2+
import { ErrorType } from "./ErrorType";
3+
4+
/**
5+
* Conductor internal error, probably caused by developer oversight.
6+
*/
7+
export class ConductorInternalError extends ConductorError {
8+
override name = "ConductorInternalError";
9+
override readonly errorType: ErrorType | string = ErrorType.INTERNAL;
10+
11+
constructor(message: string) {
12+
super(message);
13+
}
14+
}

src/common/errors/ErrorType.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const enum ErrorType {
2+
UNKNOWN = "__unknown",
3+
INTERNAL = "__internal",
4+
EVALUATOR = "__evaluator",
5+
EVALUATOR_SYNTAX = "__evaluator_syntax",
6+
EVALUATOR_TYPE = "__evaluator_type",
7+
EVALUATOR_RUNTIME = "__evaluator_runtime",
8+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ConductorError } from "./ConductorError";
2+
import { ErrorType } from "./ErrorType";
3+
4+
/**
5+
* Generic evaluation error, caused by a problem in user code.
6+
*/
7+
export class EvaluatorError extends ConductorError {
8+
override name = "EvaluatorError";
9+
override readonly errorType: ErrorType | string = ErrorType.EVALUATOR;
10+
11+
readonly rawMessage: string;
12+
readonly line?: number;
13+
readonly column?: number;
14+
readonly fileName?: string
15+
16+
constructor(message: string, line?: number, column?: number, fileName?: string) {
17+
const location = line !== undefined
18+
? `${fileName ? fileName + ":" : ""}${line}${column !== undefined ? ":" + column : ""}: `
19+
: "";
20+
super(`${location}${message}`);
21+
this.rawMessage = message;
22+
this.line = line;
23+
this.column = column;
24+
this.fileName = fileName
25+
}
26+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ErrorType } from "./ErrorType";
2+
import { EvaluatorError } from "./EvaluatorError";
3+
4+
/**
5+
* Evaluator runtime error - some problem occurred while running the user code.
6+
*/
7+
export class EvaluatorRuntimeError extends EvaluatorError {
8+
override name = "EvaluatorRuntimeError";
9+
override readonly errorType: ErrorType | string = ErrorType.EVALUATOR_RUNTIME;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ErrorType } from "./ErrorType";
2+
import { EvaluatorError } from "./EvaluatorError";
3+
4+
/**
5+
* Evaluator syntax error - the user code does not follow the evaluator's prescribed syntax.
6+
*/
7+
export class EvaluatorSyntaxError extends EvaluatorError {
8+
override name = "EvaluatorSyntaxError";
9+
override readonly errorType: ErrorType | string = ErrorType.EVALUATOR_SYNTAX;
10+
}

0 commit comments

Comments
 (0)