SillyTavern/public/scripts/slash-commands/SlashCommandDebugController.js

81 lines
2.3 KiB
JavaScript
Raw Normal View History

2024-06-18 20:29:29 +02:00
import { SlashCommandClosure } from './SlashCommandClosure.js';
import { SlashCommandExecutor } from './SlashCommandExecutor.js';
export class SlashCommandDebugController {
2024-06-20 19:06:58 +02:00
/**@type {SlashCommandClosure[]} */ stack = [];
2024-06-22 16:44:34 +02:00
/**@type {SlashCommandExecutor[]} */ cmdStack = [];
2024-06-20 21:53:30 +02:00
/**@type {boolean[]} */ stepStack = [];
2024-06-18 20:29:29 +02:00
/**@type {boolean} */ isStepping = false;
/**@type {boolean} */ isSteppingInto = false;
2024-06-20 21:53:30 +02:00
/**@type {boolean} */ isSteppingOut = false;
2024-06-18 20:29:29 +02:00
/**@type {Promise<boolean>} */ continuePromise;
/**@type {(boolean)=>void} */ continueResolver;
/**@type {(closure:SlashCommandClosure, executor:SlashCommandExecutor)=>Promise<boolean>} */ onBreakPoint;
2024-06-20 19:06:58 +02:00
2024-06-20 21:53:30 +02:00
testStepping(closure) {
return this.stepStack[this.stack.indexOf(closure)];
}
2024-06-20 19:06:58 +02:00
down(closure) {
this.stack.push(closure);
2024-06-20 21:53:30 +02:00
if (this.stepStack.length < this.stack.length) {
this.stepStack.push(this.isSteppingInto);
}
2024-06-20 19:06:58 +02:00
}
up() {
this.stack.pop();
2024-06-22 16:44:34 +02:00
while (this.cmdStack.length > this.stack.length) this.cmdStack.pop();
2024-06-20 21:53:30 +02:00
this.stepStack.pop();
2024-06-20 19:06:58 +02:00
}
2024-06-22 16:44:34 +02:00
setExecutor(executor) {
this.cmdStack[this.stack.length - 1] = executor;
}
2024-06-20 19:06:58 +02:00
2024-06-18 20:29:29 +02:00
resume() {
this.continueResolver?.(false);
this.continuePromise = null;
2024-06-20 21:53:30 +02:00
this.stepStack.forEach((_,idx)=>this.stepStack[idx] = false);
2024-06-18 20:29:29 +02:00
}
step() {
2024-06-20 21:53:30 +02:00
this.stepStack[this.stepStack.length - 1] = true;
2024-06-18 20:29:29 +02:00
this.continueResolver?.(true);
this.continuePromise = null;
}
stepInto() {
this.isSteppingInto = true;
2024-06-20 21:53:30 +02:00
this.stepStack.forEach((_,idx)=>this.stepStack[idx] = true);
2024-06-18 20:29:29 +02:00
this.continueResolver?.(true);
this.continuePromise = null;
}
2024-06-20 21:53:30 +02:00
stepOut() {
this.isSteppingOut = true;
this.stepStack[this.stepStack.length - 1] = false;
this.continueResolver?.(false);
this.continuePromise = null;
}
2024-06-18 20:29:29 +02:00
async awaitContinue() {
this.continuePromise ??= new Promise(resolve=>{
this.continueResolver = resolve;
});
this.isStepping = await this.continuePromise;
return this.isStepping;
}
async awaitBreakPoint(closure, executor) {
this.isStepping = await this.onBreakPoint(closure, executor);
return this.isStepping;
}
}