Skip to content

Commit

Permalink
useActive in executor
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkar598 committed Jun 28, 2024
1 parent 1fbf2d8 commit a3ea001
Showing 1 changed file with 53 additions and 46 deletions.
99 changes: 53 additions & 46 deletions src/vm/executor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EventEmitter, Injectable, Output } from '@angular/core';
import { CommandQueueService } from './commandQueue.service';
import { EmulatorService } from './emulator.service';
import { once } from '../utils/misc';
import { ByondService } from './byond.service';

@Injectable({
providedIn: 'root',
Expand All @@ -16,60 +17,66 @@ export class ExecutorService {
constructor(
private commandQueue: CommandQueueService,
private emulator: EmulatorService,
private byondService: ByondService,
) {}

public async executeImmediate(
code: string,
signal?: AbortSignal,
): Promise<void> {
const filename = `tmp-${Date.now()}`;
let stageAbort: Function | undefined = undefined;
return this.byondService.useActive((path) => {
if (path == null)
return Promise.reject(new Error('No active byond version'));

this.reset.emit();
const promise = new CancelablePromise<void>((resolve) => {
resolve(this.emulator.start());
})
.then(() => {
return this.emulator.sendFile(filename + '.dme', code);
})
.then(() => {
return this.commandQueue.runProcess(
'/byond/bin/DreamMaker',
'/mnt/host/' + filename + '.dme',
new Map([['LD_LIBRARY_PATH', '/byond/bin']]),
);
})
.then((compiler) => {
this.output.emit('=== Compile stage ===\n');
compiler.stdout.subscribe((val) => this.output.emit(val));
stageAbort = compiler.kill;
return once(compiler.exit);
})
.then(() => {
return this.commandQueue.runProcess(
'/byond/bin/DreamDaemon',
`/mnt/host/${filename}.dmb\0-trusted`,
new Map([['LD_LIBRARY_PATH', '/byond/bin']]),
);
})
.then((server) => {
this.output.emit('\n=== Run stage ===\n');
server.stderr.subscribe((val) => this.output.emit(val));
stageAbort = server.kill;
return once(server.exit);
})
.then(() => {
stageAbort = undefined;
const filename = `tmp-${Date.now()}`;
let stageAbort: Function | undefined = undefined;

this.reset.emit();
const promise = new CancelablePromise<void>((resolve) => {
resolve(this.emulator.start());
})
.finally(() => {
if (stageAbort) stageAbort();
this.commandQueue.runProcess(
'/bin/rm',
`/mnt/host/${filename}.dme\0/mnt/host/${filename}.dmb`,
);
}, true);
.then(() => {
return this.emulator.sendFile(filename + '.dme', code);
})
.then(() => {
return this.commandQueue.runProcess(
path + 'DreamMaker',
'/mnt/host/' + filename + '.dme',
new Map([['LD_LIBRARY_PATH', path]]),
);
})
.then((compiler) => {
this.output.emit('=== Compile stage ===\n');
compiler.stdout.subscribe((val) => this.output.emit(val));
stageAbort = compiler.kill;
return once(compiler.exit);
})
.then(() => {
return this.commandQueue.runProcess(
path + 'DreamDaemon',
`/mnt/host/${filename}.dmb\0-trusted`,
new Map([['LD_LIBRARY_PATH', path]]),
);
})
.then((server) => {
this.output.emit('\n=== Run stage ===\n');
server.stderr.subscribe((val) => this.output.emit(val));
stageAbort = server.kill;
return once(server.exit);
})
.then(() => {
stageAbort = undefined;
})
.finally(() => {
if (stageAbort) stageAbort();
this.commandQueue.runProcess(
'/bin/rm',
`/mnt/host/${filename}.dme\0/mnt/host/${filename}.dmb`,
);
}, true);

signal?.addEventListener('abort', () => promise.cancel());
return promise;
signal?.addEventListener('abort', () => promise.cancel());
return promise;
});
}
}

0 comments on commit a3ea001

Please sign in to comment.