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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | 1x 1x 1x 1x 1x 1x 1x 1x 1521x 1521x 1521x 1521x 1521x 1521x 1521x 357x 1493x 1521x 247x 1492x 1651x 1510x 203x 9087x 1493x 1487x 1487x 1330x 1486x 1486x 1x 1522x 1522x 1522x 1325x 1325x 1325x 1325x 1325x 1325x 197x 156x 41x 40x 1x 1325x 1325x 94x 1325x 1325x 1301x 1301x 1301x 1559x 1559x 1301x 1301x 1301x 1559x 1301x 1301x 44x 44x 44x 7x 7x 7x 42x 42x 38x 4x 4x 4x 4x 4x 2x 2x 2x 2x 40x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 44x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 1x 36x 44x 44x 6x 6x 6x 6x 6x 3x 6x 6x 6x 36x 36x 36x 36x 36x 36x 36x 2x 36x 36x 36x 36x 36x 4x 32x 31x 31x 1x 1x 1x 156x 156x 155x 1x | 'use strict'; const { isGenericListType, makeWrappedResultEnvelope, responseEnvelopeContainsError, traverseZList } = require( './utils.js' ); const { ZEnvelopeWrapper, ZWrapper } = require( './ZWrapper.js' ); const { convertItemArrayToZList } = require( '../function-schemata/javascript/src/utils.js' ); const { error, makeErrorInNormalForm } = require( '../function-schemata/javascript/src/error.js' ); const { validatesAsReference } = require( '../function-schemata/javascript/src/schema.js' ); /** * Error class for throwing a Z22/'Evaluation response' (envelope) that * contains an error (in Z22K2/metadata). */ class ZResponseError extends Error { constructor( message, envelope ) { super( message ); this.name = 'ZResponseError'; this.envelope = envelope; } } class EvaluatorError extends Error { } class Implementation { constructor( Z14, ZID ) { this.invariants_ = null; this.lazyVariables_ = new Set(); this.lazyReturn_ = false; this.doValidate_ = true; this.Z14_ = Z14; // Persistent ID for the implementation; null if there is none this.ZID_ = ZID; Object.defineProperty( this, 'invariants', { get: function () { return this.invariants_; }, set: function ( invariants ) { this.invariants_ = invariants; } } ); Object.defineProperty( this, 'doValidate', { get: function () { return this.doValidate_; }, set: function ( doValidate ) { this.doValidate_ = doValidate; } } ); } hasLazyVariable( variableName ) { return this.lazyVariables_.has( variableName ); } returnsLazy() { return this.lazyReturn_; } getZID() { return this.ZID_; } getZ14() { return this.Z14_; } async execute( zobject, argumentList ) { const executionResult = await this.executeInternal( zobject, argumentList ); try { if ( !( executionResult instanceof ZEnvelopeWrapper ) ) { // TODO (T365194): This extra call to ZWrapper.create is just // to "verify" (-ish) that the Z22K2/Metadata is in normal form // and doesn't contain anything weird. If it does contain // something weird, ZWrapper.create() throws an error here. // We should either strengthen the evaluator's post-conditions // to obviate this or do a proper validation check here. ZWrapper.create( executionResult.Z22K2, this.getZ14().scope_, this.getZ14().parent_ ); } const result = ZWrapper.create( executionResult, this.getZ14().scope_, this.getZ14().parent_ ); return result; } catch ( err ) { return makeWrappedResultEnvelope( null, makeErrorInNormalForm( error.invalid_format, [ JSON.stringify( executionResult ) ] ) ); } } /** * Creates and returns a function implementation for the given Z14, * as an instance of one of the subclasses Composition, Evaluated, * or BuiltIn. If an error occurs, an instance of subclass * ImplementationError is returned. * * invariants and doValidate are used locally in this method. Otherwise, they * can be set and retrieved via their accessors (e.g. invariants). * * @param {Object} Z14 the implementation * @param {Object} Z14Envelope * @param {string} ZID * @return {Implementation} * @throws {ZResponseError} If the call to resolve() returns an error */ static async create( Z14, Z14Envelope, ZID ) { Iif ( typeof Z14 === 'undefined' ) { return null; } Iif ( responseEnvelopeContainsError( Z14Envelope ) ) { throw new ZResponseError( 'Error returned from call to resolve', Z14Envelope ); } if ( Z14.Z14K4 !== undefined ) { const BuiltInZID = Z14.Z14K4.Z6K1; const { getFunction, getLazyVariables, getLazyReturn } = require( './builtins.js' ); const builtin = getFunction( BuiltInZID ); const lazyVariables = getLazyVariables( BuiltInZID ); const lazyReturn = getLazyReturn( BuiltInZID ); // eslint-disable-next-line no-use-before-define return new BuiltIn( Z14, BuiltInZID, builtin, lazyVariables, lazyReturn ); } if ( Z14.Z14K2 !== undefined ) { // eslint-disable-next-line no-use-before-define return new Composition( Z14, ZID ); } if ( Z14.Z14K3 !== undefined ) { // eslint-disable-next-line no-use-before-define return new Evaluated( Z14, ZID ); } throw new ZResponseError( 'Implementation did not specify Z14K[234]', Z14Envelope ); } } class BuiltIn extends Implementation { constructor( Z14, ZID, functor, lazyVariables, lazyReturn ) { super( Z14, ZID ); for ( const variable of lazyVariables ) { this.lazyVariables_.add( variable ); } this.lazyReturn_ = lazyReturn; this.functor_ = functor; } /** * Calls this implementation's functor with the provided arguments. * * @param {Object} zobject * @param {Array} argumentList * @return {Object} the result of calling this.functor_ with provided arguments */ executeInternal( zobject, argumentList ) { const keys = []; const nameToArgument = new Map(); for ( const argumentDict of argumentList ) { keys.push( argumentDict.name ); nameToArgument.set( argumentDict.name, argumentDict.argument ); } keys.sort(); const callArgs = []; for ( const key of keys ) { callArgs.push( nameToArgument.get( key ) ); } callArgs.push( this.invariants_ ); return this.functor_( ...callArgs ); } } /** * Used for resolving code in an implementation * * @param {Object} codeObject an unresolved ZObject * @param {Object} implementation the implementation object of the ZObject to resolve * @return {Object} resolved ZObject */ async function resolveCodeObject( codeObject, implementation ) { // Code string. const envelope = await codeObject.resolveEphemeral( [ 'Z16K1', 'Z61K1' ], implementation.invariants, /* ignoreList= */ null, /* resolveInternals= */ false, implementation.doValidate ); Iif ( responseEnvelopeContainsError( envelope ) ) { return envelope; } // Programming language string. return await codeObject.resolveEphemeral( [ 'Z16K2' ], implementation.invariants, /* ignoreList= */ null, /* resolveInternals= */ false, implementation.doValidate ); } /** * Used for resolving objects in a List Type during implementation * * @param {Object} codeConverterList the List object * @param {string} converterKey e.g. 'Z64K1' * @param {Object} implementation the implementation object of the ZObject to resolve * @return {Promise<Array[Object]>} the result of resolving objects and keys in the list, * caching them for future calls, and finally running callback on the List elements */ async function resolveCodeConverterList( codeConverterList, converterKey, implementation ) { return await traverseZList( codeConverterList, async ( tail ) => { await tail.resolveEphemeral( [ 'K1', converterKey ], implementation.invariants, /* ignoreList= */ null, /* resolveInternals= */ false, implementation.doValidate ); // TODO (T360243): Error early here if resolution failed. await resolveCodeObject( tail.getNameEphemeral( 'K1' ).getNameEphemeral( converterKey ), implementation ); } ); } /** * Resolves any remaining unresolved elements in List type, * returning null or the Object containing error if any arise * * @param {Object} sourceZWrapper ZObject, an instance of ZWrapper * @param {Object} destinationObject * @param {string} destinationObjectKey * @param {string} codeConverterListKey * @param {string} converterKey * @param {Object} implementation the implementation object of the ZObject to resolve * @return {Object|null} null or ZObject with an error */ async function maybeResolveListElementType( sourceZWrapper, destinationObject, destinationObjectKey, codeConverterListKey, converterKey, implementation ) { const typeKey = sourceZWrapper.asJSON(); if ( !isGenericListType( typeKey ) ) { return null; } const elementTypeEnvelope = await sourceZWrapper.Z4K2.K1.Z3K1.resolve( implementation.invariants, /* ignoreList= */ null, /* resolveInternals= */ true, implementation.doValidate ); Iif ( responseEnvelopeContainsError( elementTypeEnvelope ) ) { return elementTypeEnvelope; } const elementType = elementTypeEnvelope.Z22K1; // destinationObject[ destinationObjectKey ].Z4K2.K1.Z3K1 = elementType.asJSONEphemeral(); const codeConverterList = elementType[ codeConverterListKey ]; if ( codeConverterList === undefined ) { // Type does not have type converters; return. return null; } await resolveCodeConverterList( codeConverterList, converterKey, implementation ); destinationObject[ destinationObjectKey ].Z4K2.K1.Z3K1[ codeConverterListKey ] = codeConverterList.asJSONEphemeral(); return null; } class Evaluated extends Implementation { constructor( Z14, ZID ) { super( Z14, ZID ); } /** * Calls this implementation's functor with the provided arguments. * * @param {Object} zobject * @param {Array} argumentList * @return {Object} the result of calling this.functor_ with provided arguments */ async executeInternal( zobject, argumentList ) { // Arguments should already be fully resolved, but any other attributes // of the Z7 which are Z9s/Z18s must be resolved before dispatching // to the function evaluator. const Z7 = {}; Z7.Z1K1 = zobject.Z1K1.asJSON(); await ( zobject.resolveEphemeral( [ 'Z7K1', 'Z8K2' ], this.invariants, /* ignoreList= */ null, /* resolveInternals= */ true, this.doValidate ) ); const Z7K1Envelope = await ( zobject.Z7K1.resolve( this.invariants, /* ignoreList= */ null, /* resolveInternals= */ true, this.doValidate ) ); const Z7K1 = Z7K1Envelope.Z22K1; Z7.Z7K1 = Z7K1.asJSONEphemeral(); // TODO: Eliminate this back-and-forth ZWrapper conversion if possible. const Z8K4 = ZWrapper.create( convertItemArrayToZList( [ this.getZ14().asJSONEphemeral() ] ), this.getZ14().scope_, this.getZ14().parent_ ); const implementation = this; Z7.Z7K1.Z8K4 = Z8K4.asJSONEphemeral(); // Return type may be a function call and must be resolved to allow for binary encoding. const returnTypeEnvelope = await ( Z7K1.Z8K2.resolve( this.invariants, /* ignoreList= */ null, /* resolveInternals= */ true, this.doValidate ) ); Iif ( responseEnvelopeContainsError( returnTypeEnvelope ) ) { return returnTypeEnvelope; } Z7.Z7K1.Z8K2 = returnTypeEnvelope.Z22K1.asJSONEphemeral(); for ( const argumentDict of argumentList ) { Z7[ argumentDict.name ] = argumentDict.argument.asJSON(); } // Get the implementation, which may need to be dereferenced. let firstImplementation = this.Z14_; // Code implementation. await ( firstImplementation.resolveEphemeral( [ 'Z14K3' ], implementation.invariants, /* ignoreList= */ null, /* resolveInternals= */ false, implementation.doValidate ) ); const codeObjectEnvelope = await resolveCodeObject( firstImplementation.Z14K3, implementation ); Iif ( responseEnvelopeContainsError( codeObjectEnvelope ) ) { return codeObjectEnvelope; } Z7.Z7K1.Z8K4.K1 = firstImplementation.asJSONEphemeral(); firstImplementation = Z7.Z7K1.Z8K4.K1; const codeObject = firstImplementation.Z14K3; const codingLanguageObject = codeObject.Z16K1; const referenceObj = codingLanguageObject.Z9K1; const referencedExplicitObj = this.invariants.mappedProgrammingLangObj( codingLanguageObject ); const programmingLanguage = validatesAsReference( codingLanguageObject ).isValid() ? referenceObj : referencedExplicitObj; const evaluator = this.invariants.evaluatorFor( programmingLanguage ); if ( evaluator === null ) { throw new EvaluatorError( `No evaluator is available for ${ programmingLanguage }` ); } // Do type conversion of inputs, if necessary. for ( const argumentDict of argumentList ) { const argumentType = argumentDict.argument.Z1K1; if ( argumentType instanceof ZWrapper ) { const typeResult = await argumentDict.argument.Z1K1.resolve( implementation.invariants_ ); Iif ( responseEnvelopeContainsError( typeResult ) ) { return typeResult; } const theType = typeResult.Z22K1; const toCodeConverterList = theType.getNameEphemeral( 'Z4K7' ); if ( toCodeConverterList !== undefined ) { await resolveCodeConverterList( toCodeConverterList, 'Z46K3', implementation ); } Z7[ argumentDict.name ].Z1K1 = theType.asJSONEphemeral(); const listResolutionEnvelope = await maybeResolveListElementType( argumentDict.argument.Z1K1, Z7[ argumentDict.name ], 'Z1K1', 'Z4K7', 'Z46K3', this ); Iif ( listResolutionEnvelope !== null && responseEnvelopeContainsError( listResolutionEnvelope ) ) { return listResolutionEnvelope; } } } const returnType = Z7K1.getNameEphemeral( 'Z8K2' ); Eif ( returnType instanceof ZWrapper ) { const typeResult = await returnType.resolve( implementation.invariants ); Iif ( responseEnvelopeContainsError( typeResult ) ) { return typeResult; } const theType = typeResult.Z22K1; const fromCodeConverterList = theType.getNameEphemeral( 'Z4K8' ); if ( fromCodeConverterList !== undefined ) { await resolveCodeConverterList( fromCodeConverterList, 'Z64K3', implementation ); } Z7.Z7K1.Z8K2 = theType.asJSONEphemeral(); const listResolutionEnvelope = await maybeResolveListElementType( zobject.Z7K1.Z8K2, Z7.Z7K1, 'Z8K2', 'Z4K8', 'Z64K3', this ); Iif ( listResolutionEnvelope !== null && responseEnvelopeContainsError( listResolutionEnvelope ) ) { return listResolutionEnvelope; } } let fetchedResult; try { fetchedResult = await evaluator.evaluate( Z7 ); } catch ( e ) { throw new EvaluatorError( e.message ); } if ( fetchedResult.ok ) { // Assume the evaluator is returning Z22s. const resultEnvelope = await fetchedResult.json(); return resultEnvelope; } const statusCode = fetchedResult.status; const errorText = await fetchedResult.text(); throw new EvaluatorError( `Function evaluation failed with status ${ statusCode }: ${ errorText }` ); } } class Composition extends Implementation { constructor( Z14, ZID ) { super( Z14, ZID ); this.composition_ = Z14.Z14K2.asJSON(); } /** * Creates a ZObject, i.e. a ZWrapper instance, and resolves it. * * @return {Promise<ZWrapper>} A result envelope zobject representing the result. */ async executeInternal() { return await ZWrapper.create( this.composition_, this.getZ14().scope_, this.getZ14().parent_ ).resolve( this.invariants_, /* ignoreList= */ null, /* resolveInternals= */ true, /* doValidate= */ this.doValidate_ ); } } module.exports = { BuiltIn, Composition, Evaluated, EvaluatorError, Implementation, ZResponseError }; |