All files Invariants.js

95.45% Statements 42/44
90% Branches 9/10
90% Functions 9/10
95.35% Lines 41/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109    1x 1x 1x 1x                 241x 241x 241x 241x 241x 241x 241x         241x 241x 239x   241x 241x 259x 259x 6625x 6625x     259x     241x   3517x     241x   18884x     241x   11796x     241x   327x     241x         241x   11445x                         37x 37x 1x   36x                   37x 37x   37x           36x       1x  
'use strict';
 
const { dataDir } = require( '../function-schemata/javascript/src/fileUtils.js' );
const { rateLimits } = require( './rateLimits.js' );
const { readJSON } = require( './fileUtils.js' );
const softwareLanguages = readJSON( dataDir( 'definitions/softwareLanguages.json' ) );
 
/**
 * Encapsulates objects which will not change over the course of a function execution.
 */
class Invariants {
	constructor(
		resolver, evaluators, orchestratorConfig, getRemainingTime,
		requestId, logger, req = null, rateLimiter = null ) {
		this.resolver_ = resolver;
		this.languageToEvaluatorIndex_ = new Map();
		this.orchestratorConfig_ = Object.freeze( orchestratorConfig );
		this.getRemainingTime_ = getRemainingTime;
		this.requestId_ = requestId;
		this.logger_ = logger;
		this.req_ = req;
 
		// We rely on the ordering of these evaluators, so we prohibit modifications
		// of the list.
		// evaluators get decided based on idx/mapping
		this.evaluators_ = Object.freeze( evaluators );
		if ( rateLimiter === null ) {
			rateLimiter = rateLimits;
		}
		this.rateLimiter_ = rateLimiter;
		for ( let i = 0; i < evaluators.length; ++i ) {
			const evaluator = this.evaluators_[ i ];
			for ( const programmingLanguage of evaluator.programmingLanguages ) {
				this.languageToEvaluatorIndex_.set( programmingLanguage, i );
				this.languageToEvaluatorIndex_.set( softwareLanguages[ programmingLanguage ], i );
			}
			// Evaluators need access to invariants for re-entrant calls.
			evaluator.setInvariants( this );
		}
		// Resolver wraps MediaWiki for the purpose of calling wikilambda-fetch.
		Object.defineProperty( this, 'resolver', {
			get: function () {
				return resolver;
			}
		} );
		Object.defineProperty( this, 'orchestratorConfig', {
			get: function () {
				return this.orchestratorConfig_;
			}
		} );
		Object.defineProperty( this, 'requestId', {
			get: function () {
				return this.requestId_;
			}
		} );
		Object.defineProperty( this, 'logger', {
			get: function () {
				return this.logger_;
			}
		} );
		Object.defineProperty( this, 'req', {
			get: function () {
				return this.req_;
			}
		} );
		Object.defineProperty( this, 'rateLimiter', {
			get: function () {
				return this.rateLimiter_;
			}
		} );
	}
 
	/**
	 * Finds the corresponding Evaluator for the given programming language
	 * and returns the Evaluator object
	 *
	 * @param {string} programmingLanguage e.g. 'javascript'
	 * @return {Object|null} Evaluator object or null if none found
	 */
	evaluatorFor( programmingLanguage ) {
		const evaluatorIndex = this.languageToEvaluatorIndex_.get( programmingLanguage );
		if ( evaluatorIndex === undefined ) {
			return null;
		}
		return this.evaluators_[ evaluatorIndex ];
	}
 
	/**
	 * Finds the mapped/corresponding ZID of the programming language literal.
	 *
	 * @param {Object} codingLanguageObject a ZObject
	 * @return {string|null} String the ZID of the programming language (e.g. 'Z600') or null
	 */
	mappedProgrammingLangObj( codingLanguageObject ) {
		const explicitStringLiteral = codingLanguageObject.Z61K1.Z6K1;
		Eif ( explicitStringLiteral !== null ) {
			// map string literal to its ZID via schemata
			return softwareLanguages[ explicitStringLiteral ] || null;
		}
		return null;
	}
 
	getRemainingTime() {
		return this.getRemainingTime_();
	}
}
 
module.exports = { Invariants };