// Copyright (c) 2022, Compiler Explorer Authors // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. import {Container} from 'golden-layout'; import $ from 'jquery'; import * as monaco from 'monaco-editor'; import TomSelect from 'tom-select'; import {CompilationResult} from '../../types/compilation/compilation.interfaces.js'; import {CompilerInfo} from '../../types/compiler.interfaces.js'; import {ResultLine} from '../../types/resultline/resultline.interfaces.js'; import {Hub} from '../hub.js'; import {DiffState, DiffType} from './diff.interfaces.js'; import {MonacoPaneState} from './pane.interfaces.js'; import {MonacoPane} from './pane.js'; type DiffTypeAndExtra = { difftype: DiffType; extraoption?: string; }; function encodeSelectizeValue(value: DiffTypeAndExtra): string { if (value.extraoption) { return value.difftype.toString() + `:${value.extraoption}`; } return value.difftype.toString(); } function decodeSelectizeValue(value: string): DiffTypeAndExtra { const opts = value.split(':'); if (opts.length > 1) { return { difftype: Number.parseInt(opts[0], 10), extraoption: opts[1], }; } return { difftype: Number.parseInt(value, 10), extraoption: '', }; } type DiffOption = { id: string; name: string; }; class DiffStateObject { // can be undefined if there are no compilers / executors id?: number | string; model: monaco.editor.ITextModel; compiler: CompilerEntry | null; result?: CompilationResult; difftype: DiffType; extraoption?: string; constructor( id: number | string | undefined, model: monaco.editor.ITextModel, difftype: DiffType, extraoption?: string, ) { this.id = id; this.model = model; this.compiler = null; this.result = undefined; this.difftype = difftype; this.extraoption = extraoption; } update(id: number | string, compiler: CompilerInfo, result: CompilationResult) { if (this.id !== id) return false; // Handle the case where compiler hasn't been initialized yet // In a race condition, we might receive results before the compiler is registered if (!this.compiler) { // Ignore the update - the result will be requested again when the compiler is registered return false; } this.compiler.compiler = compiler; this.result = result; this.refresh(); return true; } refresh() { let output: {text: string}[] = []; if (this.result) { switch (this.difftype) { case DiffType.ASM: output = this.result.asm ? (this.result.asm as ResultLine[]) : this.result.result?.asm ? (this.result.result?.asm as ResultLine[]) : []; break; case DiffType.CompilerStdOut: output = this.result.stdout; break; case DiffType.CompilerStdErr: output = this.result.stderr; break; case DiffType.ExecStdOut: if (this.result.execResult) { output = this.result.execResult.stdout; } else { output = [{text: ""}]; } break; case DiffType.ExecStdErr: if (this.result.execResult) { output = this.result.execResult.stderr; } else { output = [{text: ""}]; } break; case DiffType.GNAT_ExpandedCode: output = this.result.gnatDebugOutput || []; break; case DiffType.GNAT_Tree: output = this.result.gnatDebugTreeOutput || []; break; case DiffType.DeviceView: if (this.result.devices && this.extraoption && this.extraoption in this.result.devices) { output = this.result.devices[this.extraoption].asm as ResultLine[]; } break; case DiffType.AstOutput: output = this.result.astOutput || [{text: ""}, ]; break; case DiffType.RustMirOutput: output = this.result.rustMirOutput || [ {text: ""}, ]; break; case DiffType.RustHirOutput: output = this.result.rustHirOutput || [ {text: ""}, ]; break; case DiffType.YulOutput: output = this.result.yulOutput || [ {text: "