mirror of
https://github.com/compiler-explorer/compiler-explorer.git
synced 2026-12-04 07:04:56 -06:00
Refactor and return 404 from AsmDocs APIs (#3143)
Refactors the handler for the AsmDocs API to reduce code duplication. **Breaking changes proposed** - API will now return 404. This used to return 200 with "Unknown opcode" which is what led to issue #3117. Following cases has been changed: 1. archiecture was not found 2. instruction opcode was not found - API will now return 406 not acceptable when the accepted content type is not text or json - There is no longer a `result` field on a 200 request, instead the assembly documentation object is returned. - There is no longer a `found` field on any request, the HTTP codes indicate whether it was found or not (200 means found) **New features** - The frontend will only request the opcode for an instruction set at most once, caching every single outgoing request. - The "There was an error fetching the documentation for this opcode" has been adjusted to contain what went wrong. Relevant tests have been added and updated.
This commit is contained in:
+4
-19
@@ -28,14 +28,10 @@ import _ from 'underscore';
|
||||
|
||||
import { ClientStateNormalizer } from '../clientstate-normalizer';
|
||||
import { logger } from '../logger';
|
||||
import * as props from '../properties';
|
||||
import { getShortenerTypeByKey } from '../shortener';
|
||||
import * as utils from '../utils';
|
||||
|
||||
import { AsmDocsHandler as AsmDocsHandlerAarch64 } from './asm-docs-api-aarch64';
|
||||
import { AsmDocsHandler as AsmDocsHandlerAmd64 } from './asm-docs-api-amd64';
|
||||
import { AsmDocsHandler as AsmDocsHandlerArm32 } from './asm-docs-api-arm32';
|
||||
import { AsmDocsHandler as AsmDocsHandlerJava } from './asm-docs-api-java';
|
||||
import * as assemblyDocumentation from './assembly-documentation/router';
|
||||
import { FormattingHandler } from './formatting';
|
||||
|
||||
export class ApiHandler {
|
||||
@@ -45,6 +41,7 @@ export class ApiHandler {
|
||||
this.usedLangIds = [];
|
||||
this.options = null;
|
||||
this.storageHandler = storageHandler;
|
||||
/** @type {e.Router} */
|
||||
this.handle = express.Router();
|
||||
const cacheHeader = `public, max-age=${ceProps('apiMaxAgeSecs', 24 * 60 * 60)}`;
|
||||
this.handle.use((req, res, next) => {
|
||||
@@ -63,20 +60,8 @@ export class ApiHandler {
|
||||
this.handle.get('/libraries/:language', this.handleLangLibraries.bind(this));
|
||||
this.handle.get('/libraries', this.handleAllLibraries.bind(this));
|
||||
|
||||
const asmStaticAge = props.propsFor('asm-docs')('staticMaxAgeSecs', 10);
|
||||
const asmArch = {
|
||||
amd64: new AsmDocsHandlerAmd64(),
|
||||
arm32: new AsmDocsHandlerArm32(),
|
||||
aarch64: new AsmDocsHandlerAarch64(),
|
||||
java: new AsmDocsHandlerJava(),
|
||||
};
|
||||
this.handle.get('/asm/:arch/:opcode', (req, res) => {
|
||||
const handler = asmArch[req.params.arch];
|
||||
if (handler)
|
||||
return handler.handle(req, res);
|
||||
res.setHeader('Cache-Control', 'public, max-age=' + asmStaticAge);
|
||||
res.send('Unknown architecture');
|
||||
});
|
||||
// Binding for assembly documentation
|
||||
assemblyDocumentation.setup(this.handle);
|
||||
// Legacy binding for old clients.
|
||||
this.handle.get('/asm/:opcode',
|
||||
(req, res) => res.redirect(`amd64/${req.params.opcode}`),
|
||||
|
||||
@@ -22,29 +22,21 @@
|
||||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
// POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
import * as props from '../properties';
|
||||
import { getAsmOpcode } from '../asm-docs-amd64';
|
||||
import { BaseAssemblyDocumentationHandler } from '../base-assembly-documentation-handler';
|
||||
|
||||
import { getAsmOpcode } from './asm-docs-java';
|
||||
const ATT_SUFFIX_REMOVER = /^([a-z]+)[blqw]$/i;
|
||||
|
||||
export class AsmDocsHandler {
|
||||
constructor() {
|
||||
const asmProps = props.propsFor('asm-docs');
|
||||
this.staticMaxAgeSecs = asmProps('staticMaxAgeSecs', 10);
|
||||
}
|
||||
|
||||
handle(req, res) {
|
||||
const info = getAsmOpcode(req.params.opcode);
|
||||
if (this.staticMaxAgeSecs) {
|
||||
res.setHeader('Cache-Control', `public, max-age=${this.staticMaxAgeSecs}`);
|
||||
}
|
||||
if (req.accepts(['text', 'json']) === 'json') {
|
||||
res.send({found: !!info, result: info});
|
||||
} else {
|
||||
if (info) {
|
||||
res.send(info.html);
|
||||
} else {
|
||||
res.send('Unknown opcode');
|
||||
export class Amd64DocumentationHandler extends BaseAssemblyDocumentationHandler {
|
||||
getInstructionInformation(instruction) {
|
||||
// Try both default and with AT&T suffix removed
|
||||
let info = getAsmOpcode(instruction);
|
||||
if (!info) {
|
||||
const alternativeInstruction = ATT_SUFFIX_REMOVER.exec(instruction);
|
||||
if (alternativeInstruction) {
|
||||
info = getAsmOpcode(alternativeInstruction[1]);
|
||||
}
|
||||
}
|
||||
return info || null;
|
||||
}
|
||||
}
|
||||
@@ -22,18 +22,12 @@
|
||||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
// POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
import * as props from '../properties';
|
||||
import { getAsmOpcode } from '../asm-docs-arm32';
|
||||
import { BaseAssemblyDocumentationHandler } from '../base-assembly-documentation-handler';
|
||||
|
||||
import {getAsmOpcode} from './asm-docs-arm32';
|
||||
|
||||
const conditionalRe = /^([A-Za-z]+?)(EQ|NE|CS|CC|MI|PL|VS|VC|HI|LS|GE|LT|GT|LE|AL)$/;
|
||||
|
||||
export class AsmDocsHandler {
|
||||
constructor() {
|
||||
const asmProps = props.propsFor('asm-docs');
|
||||
this.staticMaxAgeSecs = asmProps('staticMaxAgeSecs', 10);
|
||||
}
|
||||
const CONDITIONAL_INSTRUCTION_REGEXP = /^([A-Za-z]+?)(EQ|NE|CS|CC|MI|PL|VS|VC|HI|LS|GE|LT|GT|LE|AL)$/;
|
||||
|
||||
export class Arm32DocumentationHandler extends BaseAssemblyDocumentationHandler {
|
||||
// Notes for conditionals:
|
||||
// https://developer.arm.com/documentation/dui0473/m/condition-codes/condition-code-suffixes-and-related-flags
|
||||
getOpcodeConditional(opcode) {
|
||||
@@ -56,7 +50,7 @@ export class AsmDocsHandler {
|
||||
LE: 'If signed less than or equal, ',
|
||||
};
|
||||
|
||||
const matches = opcode.match(conditionalRe);
|
||||
const matches = opcode.match(CONDITIONAL_INSTRUCTION_REGEXP);
|
||||
if (matches) {
|
||||
const opcodeDescription = getAsmOpcode(matches[1]);
|
||||
if (!opcodeDescription) return;
|
||||
@@ -70,19 +64,8 @@ export class AsmDocsHandler {
|
||||
}
|
||||
}
|
||||
|
||||
handle(req, res) {
|
||||
const opcode = req.params.opcode.toUpperCase();
|
||||
const info = getAsmOpcode(opcode) || this.getOpcodeConditional(opcode);
|
||||
if (this.staticMaxAgeSecs) {
|
||||
res.setHeader('Cache-Control', 'public, max-age=' + this.staticMaxAgeSecs);
|
||||
}
|
||||
if (req.accepts(['text', 'json']) === 'json') {
|
||||
res.send({found: !!info, result: info});
|
||||
} else {
|
||||
if (info)
|
||||
res.send(info.html);
|
||||
else
|
||||
res.send('Unknown opcode');
|
||||
}
|
||||
getInstructionInformation(instruction) {
|
||||
const info = getAsmOpcode(instruction) || this.getOpcodeConditional(instruction);
|
||||
return info || null;
|
||||
}
|
||||
}
|
||||
@@ -22,19 +22,11 @@
|
||||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
// POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
import * as props from '../properties';
|
||||
import { getAsmOpcode } from '../asm-docs-java';
|
||||
import { BaseAssemblyDocumentationHandler } from '../base-assembly-documentation-handler';
|
||||
|
||||
export class AsmDocsHandler {
|
||||
constructor() {
|
||||
const asmProps = props.propsFor('asm-docs');
|
||||
this.staticMaxAgeSecs = asmProps('staticMaxAgeSecs', 10);
|
||||
}
|
||||
|
||||
handle(req, res) {
|
||||
if (this.staticMaxAgeSecs) {
|
||||
res.setHeader('Cache-Control', 'public, max-age=' + this.staticMaxAgeSecs);
|
||||
}
|
||||
|
||||
res.send('Unknown opcode');
|
||||
export class JavaDocumentationHandler extends BaseAssemblyDocumentationHandler {
|
||||
getInstructionInformation(instruction) {
|
||||
return getAsmOpcode(instruction) || null;
|
||||
}
|
||||
}
|
||||
+27
-31
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2017, Compiler Explorer Authors
|
||||
// Copyright (c) 2021, Compiler Explorer Authors
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
@@ -22,37 +22,33 @@
|
||||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
// POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
import * as props from '../properties';
|
||||
import { propsFor } from '../../properties';
|
||||
|
||||
import { getAsmOpcode } from './asm-docs-amd64';
|
||||
import { Amd64DocumentationHandler } from './amd64';
|
||||
import { Arm32DocumentationHandler } from './arm32';
|
||||
import { JavaDocumentationHandler } from './java';
|
||||
|
||||
export class AsmDocsHandler {
|
||||
constructor() {
|
||||
const asmProps = props.propsFor('asm-docs');
|
||||
this.staticMaxAgeSecs = asmProps('staticMaxAgeSecs', 10);
|
||||
this.atAndTSuffixRemover = /^([a-z]+)[blqw]$/i;
|
||||
/** @type {Record.<string, BaseAssemblyDocumentationHandler>} */
|
||||
const ASSEMBLY_DOCUMENTATION_HANDLERS = {
|
||||
amd64: new Amd64DocumentationHandler(),
|
||||
arm32: new Arm32DocumentationHandler(),
|
||||
java: new JavaDocumentationHandler(),
|
||||
};
|
||||
|
||||
const MAX_STATIC_AGE = propsFor('asm-docs')('staticMaxAgeSecs', 10);
|
||||
|
||||
/**
|
||||
* Initialize all Assembly Docs routes
|
||||
*
|
||||
* @param {e.Router} router
|
||||
*/
|
||||
export const setup = (router) => router.get('/asm/:arch/:opcode', (req, res) => {
|
||||
if (MAX_STATIC_AGE > 0) {
|
||||
res.setHeader('Cache-Control', `public, max-age=${MAX_STATIC_AGE}`);
|
||||
}
|
||||
|
||||
handle(req, res) {
|
||||
let info = getAsmOpcode(req.params.opcode);
|
||||
if (!info) {
|
||||
// If the opcode ends with an AT&T suffix, try removing that and giving it another go.
|
||||
// Ideally, we'd be smarter here, but this is a quick win.
|
||||
const suffixRemoved = this.atAndTSuffixRemover.exec(req.params.opcode);
|
||||
if (suffixRemoved) {
|
||||
info = getAsmOpcode(suffixRemoved[1]);
|
||||
}
|
||||
}
|
||||
if (this.staticMaxAgeSecs) {
|
||||
res.setHeader('Cache-Control', 'public, max-age=' + this.staticMaxAgeSecs);
|
||||
}
|
||||
if (req.accepts(['text', 'json']) === 'json') {
|
||||
res.send({found: !!info, result: info});
|
||||
} else {
|
||||
if (info)
|
||||
res.send(info.html);
|
||||
else
|
||||
res.send('Unknown opcode');
|
||||
}
|
||||
const handler = ASSEMBLY_DOCUMENTATION_HANDLERS[req.params.arch];
|
||||
if (handler !== undefined) {
|
||||
return handler.handle(req, res);
|
||||
}
|
||||
}
|
||||
res.status(404).send({ error: 'Unknown architecture '});
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
// Copyright (c) 2021, 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.
|
||||
|
||||
/**
|
||||
* @typedef {object} AssemblyInstructionInfo
|
||||
* @property {string} url - URL to more in-depth documentation
|
||||
* @property {string} html - Raw HTML to embed into the popup
|
||||
* @property {string} tooltip - Text snippet to show in the tooltip
|
||||
*/
|
||||
|
||||
export class BaseAssemblyDocumentationHandler {
|
||||
/**
|
||||
* Gather the assembly instruction information by the instruction name.
|
||||
*
|
||||
* Implementors should return null if the instruction is not supported.
|
||||
*
|
||||
* @param {string} instruction
|
||||
* @returns {AssemblyInstructionInfo | null}
|
||||
*/
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
getInstructionInformation(instruction) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a request for assembly instruction documentation.
|
||||
*
|
||||
* Implementors should not have to override this.
|
||||
*
|
||||
* @param {e.Request} request
|
||||
* @param {e.Response} response
|
||||
*/
|
||||
handle(request, response) {
|
||||
// If the request had no opcode parameter, we should fail. This assumes
|
||||
// no assembly language has a __unknown_opcode instruction.
|
||||
const instruction = (request.params.opcode || '__UNKNOWN_OPCODE').toUpperCase();
|
||||
const information = this.getInstructionInformation(instruction);
|
||||
if (information === null) {
|
||||
return response.status(404).send({ error: 'Unknown opcode' });
|
||||
}
|
||||
// Accept either JSON or Plaintext Content-Type
|
||||
const requestedContentType = request.accepts(['text', 'json']);
|
||||
switch (requestedContentType) {
|
||||
case 'text': response.send(information.html); break;
|
||||
case 'json': response.send(information); break;
|
||||
default: response.status(406).send({ error: 'Not Acceptable' }); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// Copyright (c) 2021, 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 { AssemblyDocumentationResponse, AssemblyDocumentationRequest } from './assemblydocumentation.interfaces';
|
||||
|
||||
/** Type wrapper allowing .json() to resolve to a concrete type */
|
||||
type TypedResponse<R> = Response & {
|
||||
json(): Promise<R>
|
||||
}
|
||||
|
||||
/** Lightweight fetch() wrapper for CE API urls */
|
||||
const request = async <R>(uri: string, options?: RequestInit): Promise<TypedResponse<R>> => fetch(
|
||||
`${window.location.origin}${window.httpRoot}api${uri}`,
|
||||
{
|
||||
...options,
|
||||
headers: {
|
||||
...options?.headers,
|
||||
'Accept': 'application/json',
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
export const getAssemblyDocumentation = async (
|
||||
options: AssemblyDocumentationRequest
|
||||
) => request<AssemblyDocumentationResponse>(`/asm/${options.instructionSet}/${options.opcode}`);
|
||||
@@ -0,0 +1,41 @@
|
||||
// Copyright (c) 2021, 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.
|
||||
|
||||
export interface AssemblyDocumentationRequest {
|
||||
/** Specifies which instruction set to look for */
|
||||
instructionSet: 'amd64' | 'arm32' | 'java';
|
||||
/** Instruction set opcode to look for */
|
||||
opcode: string;
|
||||
}
|
||||
|
||||
export interface AssemblyDocumentationResponse {
|
||||
tooltip: string;
|
||||
html: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
export interface AssemblyDocumentationError {
|
||||
/** Explanatory error string */
|
||||
error: string;
|
||||
}
|
||||
+19
-16
@@ -44,6 +44,7 @@ var CompilerPicker = require('../compiler-picker').CompilerPicker;
|
||||
var Settings = require('../settings');
|
||||
var utils = require('../utils');
|
||||
var LibUtils = require('../lib-utils').LibUtils;
|
||||
var getAssemblyDocumentation = require('../api/api').getAssemblyDocumentation;
|
||||
|
||||
require('../modes/asm-mode');
|
||||
require('../modes/asmruby-mode');
|
||||
@@ -2264,24 +2265,26 @@ function getAsmInfo(opcode, instructionSet) {
|
||||
var cacheName = 'asm/' + (instructionSet ? (instructionSet + '/') : '') + opcode;
|
||||
var cached = OpcodeCache.get(cacheName);
|
||||
if (cached) {
|
||||
return Promise.resolve(cached.found ? cached.result : null);
|
||||
if (cached.found) {
|
||||
return Promise.resolve(cached.data);
|
||||
}
|
||||
return Promise.reject(cached.data);
|
||||
}
|
||||
var base = window.httpRoot;
|
||||
return new Promise(function (resolve, reject) {
|
||||
$.ajax({
|
||||
type: 'GET',
|
||||
url: window.location.origin + base + 'api/asm/' + (instructionSet ? (instructionSet + '/') : '') + opcode,
|
||||
dataType: 'json', // Expected,
|
||||
contentType: 'text/plain', // Sent
|
||||
success: function (result) {
|
||||
OpcodeCache.set(cacheName, result);
|
||||
resolve(result.found ? result.result : null);
|
||||
},
|
||||
error: function (result) {
|
||||
reject(result);
|
||||
},
|
||||
cache: true,
|
||||
});
|
||||
getAssemblyDocumentation({ opcode: opcode, instructionSet: instructionSet })
|
||||
.then(function (response) {
|
||||
response.json().then(function (body) {
|
||||
if (response.status === 200) {
|
||||
OpcodeCache.set(cacheName, { found: true, data: body });
|
||||
resolve(body);
|
||||
} else {
|
||||
OpcodeCache.set(cacheName, { found: false, data: body.error });
|
||||
reject(body.error);
|
||||
}
|
||||
});
|
||||
}).catch(function (error) {
|
||||
reject('Fetch error: ' + error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -173,7 +173,6 @@ describe('API handling', () => {
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.json;
|
||||
res.body.found.should.be.true;
|
||||
})
|
||||
.catch(err => {
|
||||
throw err;
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
// Copyright (c) 2017, 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 express from 'express';
|
||||
|
||||
import { AsmDocsHandler } from '../../lib/handlers/asm-docs-api-amd64';
|
||||
import { chai } from '../utils';
|
||||
|
||||
describe('Assembly documents', () => {
|
||||
let app;
|
||||
|
||||
before(() => {
|
||||
app = express();
|
||||
const handler = new AsmDocsHandler();
|
||||
app.use('/asm/:opcode', handler.handle.bind(handler));
|
||||
});
|
||||
|
||||
// We don't serve a 404 for unknown opcodes as it allows the not-an-opcode to be cached.
|
||||
it('should respond with "unknown opcode" for unknown opcodes', () => {
|
||||
return chai.request(app)
|
||||
.get('/asm/NOTANOPCODE')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.html;
|
||||
res.text.should.equal('Unknown opcode');
|
||||
})
|
||||
.catch(function (err) {
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
|
||||
it('should respond to text requests', () => {
|
||||
return chai.request(app)
|
||||
.get('/asm/mov')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.html;
|
||||
res.text.should.contain('Copies the second operand');
|
||||
})
|
||||
.catch(function (err) {
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
|
||||
it('should respond to json requests', () => {
|
||||
return chai.request(app)
|
||||
.get('/asm/mov')
|
||||
.set('Accept', 'application/json')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.json;
|
||||
res.body.found.should.equal(true);
|
||||
res.body.result.html.should.contain('Copies the second operand');
|
||||
res.body.result.tooltip.should.contain('Copies the second operand');
|
||||
res.body.result.url.should.contain('www.felixcloutier.com');
|
||||
})
|
||||
.catch(function (err) {
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
it('should respond to json for unknown opcodes', () => {
|
||||
return chai.request(app)
|
||||
.get('/asm/NOANOPCODE')
|
||||
.set('Accept', 'application/json')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.json;
|
||||
res.body.found.should.equal(false);
|
||||
})
|
||||
.catch(function (err) {
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle at&t syntax', () => {
|
||||
return chai.request(app)
|
||||
.get('/asm/addq')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.html;
|
||||
res.text.should.contain('Adds the destination operand');
|
||||
})
|
||||
.catch(function (err) {
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
// Copyright (c) 2021, 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 express from 'express';
|
||||
|
||||
import { Amd64DocumentationHandler } from '../../../lib/handlers/assembly-documentation/amd64';
|
||||
import { chai } from '../../utils';
|
||||
|
||||
describe('amd64 assembly documentation', () => {
|
||||
let app;
|
||||
before(() => {
|
||||
app = express();
|
||||
const handler = new Amd64DocumentationHandler();
|
||||
app.use('/asm/:opcode', handler.handle.bind(handler));
|
||||
});
|
||||
|
||||
it('returns 404 for unknown opcodes', () => {
|
||||
return chai.request(app).get('/asm/mov_oh_wait')
|
||||
.then(res => {
|
||||
res.should.have.status(404);
|
||||
res.should.be.json;
|
||||
res.body.should.deep.equal({ error: 'Unknown opcode' });
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('responds to accept=text requests', () => {
|
||||
return chai.request(app).get('/asm/mov')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.html;
|
||||
res.text.should.contain('Copies the second operand');
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('responds to accept=json requests', () => {
|
||||
return chai.request(app).get('/asm/mov')
|
||||
.set('Accept', 'application/json')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.json;
|
||||
res.body.html.should.contain('Copies the second operand');
|
||||
res.body.tooltip.should.contain('Copies the second operand');
|
||||
res.body.url.should.contain('www.felixcloutier.com');
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('should respond to json for unknown opcodes', () => {
|
||||
return chai.request(app)
|
||||
.get('/asm/NOANOPCODE')
|
||||
.set('Accept', 'application/json')
|
||||
.then(res => {
|
||||
res.should.have.status(404);
|
||||
res.should.be.json;
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('should return 406 on bad accept type', () => {
|
||||
return chai.request(app).get('/asm/mov')
|
||||
.set('Accept', 'application/pdf')
|
||||
.then(res => {
|
||||
res.should.have.status(406);
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('should handle at&t syntax', () => {
|
||||
return chai.request(app)
|
||||
.get('/asm/addq')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.html;
|
||||
res.text.should.contain('Adds the destination operand');
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
});
|
||||
+36
-56
@@ -24,73 +24,63 @@
|
||||
|
||||
import express from 'express';
|
||||
|
||||
import { AsmDocsHandler } from '../../lib/handlers/asm-docs-api-arm32';
|
||||
import { chai } from '../utils';
|
||||
import { Arm32DocumentationHandler } from '../../../lib/handlers/assembly-documentation/arm32';
|
||||
import { chai } from '../../utils';
|
||||
|
||||
describe('Assembly documents', () => {
|
||||
describe('arm32 assembly documentation', () => {
|
||||
let app;
|
||||
|
||||
before(() => {
|
||||
app = express();
|
||||
const handler = new AsmDocsHandler();
|
||||
const handler = new Arm32DocumentationHandler();
|
||||
app.use('/asm/:opcode', handler.handle.bind(handler));
|
||||
});
|
||||
|
||||
// We don't serve a 404 for unknown opcodes as it allows the not-an-opcode to be cached.
|
||||
it('should respond with "unknown opcode" for unknown opcodes', () => {
|
||||
return chai.request(app)
|
||||
.get('/asm/NOTANOPCODE')
|
||||
it('returns 404 for unknown opcodes', () => {
|
||||
return chai.request(app).get('/asm/mov_oh_wait')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.html;
|
||||
res.text.should.equal('Unknown opcode');
|
||||
})
|
||||
.catch(function (err) {
|
||||
throw err;
|
||||
});
|
||||
res.should.have.status(404);
|
||||
res.should.be.json;
|
||||
res.body.should.deep.equal({ error: 'Unknown opcode' });
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('should respond to text requests', () => {
|
||||
return chai.request(app)
|
||||
.get('/asm/mov')
|
||||
it('responds to accept=text requests', () => {
|
||||
return chai.request(app).get('/asm/mov')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.html;
|
||||
res.text.should.contain('writes an immediate value to the destination register');
|
||||
})
|
||||
.catch(function (err) {
|
||||
throw err;
|
||||
});
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('should respond to json requests', () => {
|
||||
return chai.request(app)
|
||||
.get('/asm/mov')
|
||||
it('responds to accept=json requests', () => {
|
||||
return chai.request(app).get('/asm/mov')
|
||||
.set('Accept', 'application/json')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.json;
|
||||
res.body.found.should.equal(true);
|
||||
res.body.result.html.should.contain('writes an immediate value');
|
||||
res.body.result.tooltip.should.contain('writes an immediate value');
|
||||
res.body.result.url.should.contain('https://developer.arm.com/documentation/');
|
||||
})
|
||||
.catch(function (err) {
|
||||
throw err;
|
||||
});
|
||||
res.body.html.should.contain('writes an immediate value');
|
||||
res.body.tooltip.should.contain('writes an immediate value');
|
||||
res.body.url.should.contain('https://developer.arm.com/documentation/');
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('should respond to json for unknown opcodes', () => {
|
||||
return chai.request(app)
|
||||
.get('/asm/NOANOPCODE')
|
||||
.set('Accept', 'application/json')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.have.status(404);
|
||||
res.should.be.json;
|
||||
res.body.found.should.equal(false);
|
||||
})
|
||||
.catch(function (err) {
|
||||
throw err;
|
||||
});
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('should return 406 on bad accept type', () => {
|
||||
return chai.request(app).get('/asm/mov')
|
||||
.set('Accept', 'application/pdf')
|
||||
.then(res => {
|
||||
res.should.have.status(406);
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('should handle opcodes with a conditional type suffix that are not conditionals', () => {
|
||||
@@ -100,10 +90,7 @@ describe('Assembly documents', () => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.html;
|
||||
res.text.should.contain('adds an immediate value and the Carry flag');
|
||||
})
|
||||
.catch(function (err) {
|
||||
throw err;
|
||||
});
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('should handle conditional opcodes', () => {
|
||||
@@ -113,23 +100,16 @@ describe('Assembly documents', () => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.html;
|
||||
res.text.should.contain('If equal, <p>Branch causes a branch');
|
||||
})
|
||||
.catch(function (err) {
|
||||
throw err;
|
||||
});
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('should respond with "unknown opcode" for unknown opcodes that can be parsed as conditional', () => {
|
||||
return chai.request(app)
|
||||
.get('/asm/jne')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.html;
|
||||
res.text.should.equal('Unknown opcode');
|
||||
})
|
||||
.catch(function (err) {
|
||||
throw err;
|
||||
});
|
||||
res.should.have.status(404);
|
||||
res.should.be.json;
|
||||
res.body.should.deep.equal({ error: 'Unknown opcode' });
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
});
|
||||
+34
-35
@@ -24,63 +24,62 @@
|
||||
|
||||
import express from 'express';
|
||||
|
||||
import { AsmDocsHandler } from '../../lib/handlers/asm-docs-api-java';
|
||||
import { chai } from '../utils';
|
||||
import { JavaDocumentationHandler } from '../../../lib/handlers/assembly-documentation/java';
|
||||
import { chai } from '../../utils';
|
||||
|
||||
describe('Assembly documents', () => {
|
||||
describe('jvm assembly documentation', () => {
|
||||
let app;
|
||||
|
||||
before(() => {
|
||||
app = express();
|
||||
const handler = new AsmDocsHandler();
|
||||
const handler = new JavaDocumentationHandler();
|
||||
app.use('/asm/:opcode', handler.handle.bind(handler));
|
||||
});
|
||||
|
||||
it('should respond with "unknown opcode" for unknown opcodes', () => {
|
||||
return chai.request(app).get('/asm/NOTANOPCODE')
|
||||
it('returns 404 for unknown opcodes', () => {
|
||||
return chai.request(app).get('/asm/mov_oh_wait')
|
||||
.then(res => {
|
||||
res.should.have.status(404);
|
||||
res.should.be.json;
|
||||
res.body.should.deep.equal({ error: 'Unknown opcode' });
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('responds to accept=text requests', () => {
|
||||
return chai.request(app).get('/asm/iload_0')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.html;
|
||||
res.text.should.equal('Unknown opcode');
|
||||
})
|
||||
.catch(err => { throw err; });
|
||||
res.text.should.contain('Load int from local variable');
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('should respond to text requests', () => {
|
||||
return chai.request(app)
|
||||
.get('/asm/aaload')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.html;
|
||||
res.text.should.contain('Load reference from array');
|
||||
})
|
||||
.catch(err => { throw err; });
|
||||
});
|
||||
|
||||
it('should respond to json requests', () => {
|
||||
return chai.request(app)
|
||||
.get('/asm/aaload')
|
||||
it('responds to accept=json requests', () => {
|
||||
return chai.request(app).get('/asm/iload_0')
|
||||
.set('Accept', 'application/json')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.be.json;
|
||||
res.body.found.should.equal(true);
|
||||
res.body.result.html.should.contain('Load reference from array');
|
||||
res.body.result.tooltip.should.contain('Load reference from array');
|
||||
res.body.result.url.should.contain('https://docs.oracle.com/javase/specs/');
|
||||
})
|
||||
.catch(err => { throw err; });
|
||||
res.body.html.should.contain('Load int from local variable');
|
||||
res.body.tooltip.should.contain('Load int from local variable');
|
||||
res.body.url.should.contain('https://docs.oracle.com/javase/specs/jvms/se16/html/');
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('should respond to json for unknown opcodes', () => {
|
||||
return chai.request(app)
|
||||
.get('/asm/NOTANOPCODE')
|
||||
.get('/asm/NOANOPCODE')
|
||||
.set('Accept', 'application/json')
|
||||
.then(res => {
|
||||
res.should.have.status(200);
|
||||
res.should.have.status(404);
|
||||
res.should.be.json;
|
||||
res.body.found.should.equal(false);
|
||||
})
|
||||
.catch(err => { throw err; });
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
|
||||
it('should return 406 on bad accept type', () => {
|
||||
return chai.request(app).get('/asm/iload_0')
|
||||
.set('Accept', 'application/pdf')
|
||||
.then(res => {
|
||||
res.should.have.status(406);
|
||||
}).catch(e => { throw e; });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user