diff --git a/lib/handlers/api.js b/lib/handlers/api.js index c8237eb07..876420bbc 100644 --- a/lib/handlers/api.js +++ b/lib/handlers/api.js @@ -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}`), diff --git a/lib/handlers/asm-docs-api-java.js b/lib/handlers/assembly-documentation/amd64.js similarity index 66% rename from lib/handlers/asm-docs-api-java.js rename to lib/handlers/assembly-documentation/amd64.js index 20d4816f9..5b2f6b7d6 100644 --- a/lib/handlers/asm-docs-api-java.js +++ b/lib/handlers/assembly-documentation/amd64.js @@ -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; } } diff --git a/lib/handlers/asm-docs-api-arm32.js b/lib/handlers/assembly-documentation/arm32.js similarity index 73% rename from lib/handlers/asm-docs-api-arm32.js rename to lib/handlers/assembly-documentation/arm32.js index 303ab86a2..66c680409 100644 --- a/lib/handlers/asm-docs-api-arm32.js +++ b/lib/handlers/assembly-documentation/arm32.js @@ -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; } } diff --git a/lib/handlers/asm-docs-api-aarch64.js b/lib/handlers/assembly-documentation/java.js similarity index 77% rename from lib/handlers/asm-docs-api-aarch64.js rename to lib/handlers/assembly-documentation/java.js index 4bcddd1ff..5578eecff 100644 --- a/lib/handlers/asm-docs-api-aarch64.js +++ b/lib/handlers/assembly-documentation/java.js @@ -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; } } diff --git a/lib/handlers/asm-docs-api-amd64.js b/lib/handlers/assembly-documentation/router.js similarity index 52% rename from lib/handlers/asm-docs-api-amd64.js rename to lib/handlers/assembly-documentation/router.js index e0db76077..9e0db4b36 100644 --- a/lib/handlers/asm-docs-api-amd64.js +++ b/lib/handlers/assembly-documentation/router.js @@ -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.} */ +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 '}); +}); diff --git a/lib/handlers/base-assembly-documentation-handler.js b/lib/handlers/base-assembly-documentation-handler.js new file mode 100644 index 000000000..ccd47a23f --- /dev/null +++ b/lib/handlers/base-assembly-documentation-handler.js @@ -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; + } + } +} diff --git a/static/api/api.ts b/static/api/api.ts new file mode 100644 index 000000000..f73ceee23 --- /dev/null +++ b/static/api/api.ts @@ -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 = Response & { + json(): Promise +} + +/** Lightweight fetch() wrapper for CE API urls */ +const request = async (uri: string, options?: RequestInit): Promise> => fetch( + `${window.location.origin}${window.httpRoot}api${uri}`, + { + ...options, + headers: { + ...options?.headers, + 'Accept': 'application/json', + } + }, +); + +export const getAssemblyDocumentation = async ( + options: AssemblyDocumentationRequest +) => request(`/asm/${options.instructionSet}/${options.opcode}`); diff --git a/static/api/assemblydocumentation.interfaces.ts b/static/api/assemblydocumentation.interfaces.ts new file mode 100644 index 000000000..21e390bdc --- /dev/null +++ b/static/api/assemblydocumentation.interfaces.ts @@ -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; +} diff --git a/static/panes/compiler.js b/static/panes/compiler.js index f383e0ef5..55c766578 100644 --- a/static/panes/compiler.js +++ b/static/panes/compiler.js @@ -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); + }); }); } diff --git a/test/handlers/api-tests.js b/test/handlers/api-tests.js index 8bf1f4ea6..816bf04b3 100644 --- a/test/handlers/api-tests.js +++ b/test/handlers/api-tests.js @@ -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; diff --git a/test/handlers/asm-docs-tests.js b/test/handlers/asm-docs-tests.js deleted file mode 100644 index 926b5e3dc..000000000 --- a/test/handlers/asm-docs-tests.js +++ /dev/null @@ -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; - }); - }); -}); diff --git a/test/handlers/assembly-documentation/amd64-tests.js b/test/handlers/assembly-documentation/amd64-tests.js new file mode 100644 index 000000000..c6a057704 --- /dev/null +++ b/test/handlers/assembly-documentation/amd64-tests.js @@ -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; }); + }); +}); diff --git a/test/handlers/asm-docs-tests-arm32.js b/test/handlers/assembly-documentation/arm32-tests.js similarity index 60% rename from test/handlers/asm-docs-tests-arm32.js rename to test/handlers/assembly-documentation/arm32-tests.js index db6a9fdb5..e76ca01f6 100644 --- a/test/handlers/asm-docs-tests-arm32.js +++ b/test/handlers/assembly-documentation/arm32-tests.js @@ -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,

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; }); }); - }); diff --git a/test/handlers/asm-docs-tests-java.js b/test/handlers/assembly-documentation/java-tests.js similarity index 57% rename from test/handlers/asm-docs-tests-java.js rename to test/handlers/assembly-documentation/java-tests.js index 316e4f32f..33f1f53f7 100644 --- a/test/handlers/asm-docs-tests-java.js +++ b/test/handlers/assembly-documentation/java-tests.js @@ -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; }); }); });