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 | 1x 1x 1x 1x 1x 8149x 8149x 1881x 8149x 14377x 14377x 9083x 14377x 6268x 6268x 6268x 6268x 3x 6265x 5974x 5974x 291x 291x 291x 1211x 1211x 1090x 42420x 42420x 42420x 42420x 42420x 42420x 37639x 4781x 4781x 14307x 4781x 9526x 4781x 74121x 74121x 72947x 1174x 98x 1076x 243x 243x 1x 1x 1x 1x 1x 1x 1x 2x 978x 978x 723x 255x 481x 481x 1111x 1111x 1641x 27x 1614x 1x 1614x 1614x 1614x 610x 1614x 6138x 1614x 1111x 4236x 4236x 62358x 62358x 1x | 'use strict'; const { SchemaFactory, validatesAsZObject, validatesAsFunctionCall, validatesAsReference, ZObjectKeyFactory } = require( '../function-schemata/javascript/src/schema.js' ); const { isUserDefined, getHead, getTail, makeMappedResultEnvelope, isVoid, isZMap, getZMapValue } = require( '../function-schemata/javascript/src/utils' ); const normalFactory = SchemaFactory.NORMAL(); const Z6Validator = normalFactory.create( 'Z6_literal' ); const Z9Validator = normalFactory.create( 'Z9_literal' ); /** * Determines whether argument is a Z6 or Z9. These two types' Z1K1s are * strings instead of Z9s, so some checks below need to special-case their * logic. * * @param {Object} Z1 a ZObject * @return {bool} true if Z1 validates as either Z6 or Z7 */ async function isRefOrString( Z1 ) { const { ZWrapper } = require( './ZWrapper' ); if ( Z1 instanceof ZWrapper ) { Z1 = Z1.asJSON(); } return ( ( await Z6Validator.validate( Z1 ) ) || ( await Z9Validator.validate( Z1 ) ) ); } async function createZObjectKey( ZObject ) { const { ZWrapper } = require( './ZWrapper' ); if ( ZObject instanceof ZWrapper ) { ZObject = ZObject.asJSON(); } return await ZObjectKeyFactory.create( ZObject ); } async function createSchema( Z1 ) { // TODO (T302032): Use function-schemata version of findIdentity to improve // type inference here. let Z1K1 = Z1.Z1K1; const { ZWrapper } = require( './ZWrapper' ); Iif ( Z1K1 instanceof ZWrapper ) { Z1K1 = Z1K1.asJSON(); } if ( await isRefOrString( Z1 ) ) { return normalFactory.create( Z1K1 ); } if ( ( await validatesAsReference( Z1K1 ) ).isValid() ) { Iif ( isUserDefined( Z1K1.Z9K1 ) ) { throw new Error( `Tried to create schema for unrecognized ZID ${Z1K1.Z9K1}` ); } return normalFactory.create( Z1K1.Z9K1 ); } const result = await normalFactory.createUserDefined( [ Z1K1 ] ); const key = ( await createZObjectKey( Z1K1 ) ).asString(); return result.get( key ); } /** * Validates a ZObject against the Error schema. * * @param {Object} Z1 object to be validated * @return {bool} whether Z1 can validate as an Error */ function isError( Z1 ) { // TODO (T287921): Assay that Z1 validates as Z5 but not as Z9 or Z18. try { return Z1.Z1K1 === 'Z5' || Z1.Z1K1.Z9K1 === 'Z5'; } catch ( error ) { return false; } } /** * Validates a ZObject against the GENERIC schema. * * @param {Object} Z1 object to be validated * @return {bool} whether Z1 can validate as a generic type instantiation */ async function isGenericType( Z1 ) { // TODO (T296658): Use the GENERIC schema. try { let Z1K1 = Z1.Z1K1; const { ZWrapper } = require( './ZWrapper' ); Eif ( Z1 instanceof ZWrapper ) { Z1K1 = Z1.asJSON().Z1K1; } if ( !( await validatesAsFunctionCall( Z1K1 ) ).isValid() ) { return false; } const localKeyRegex = /K[1-9]\d*$/; for ( const key of Z1.keys() ) { if ( key === 'Z1K1' || key === 'Z7K1' ) { continue; } Iif ( key.match( localKeyRegex ) === null ) { return false; } } return true; } catch ( err ) { return false; } } /** * Determines whether a Z22 / Evaluation result contains an error. Works both with older * "basic" Z22s and with newer map-based Z22s. * * @param {Object} envelope a Z22 * @return {bool} true if Z22K2 contains an error; false otherwise */ function containsError( envelope ) { const metadata = envelope.Z22K2; if ( isVoid( metadata ) ) { return false; } else if ( isZMap( metadata ) ) { return ( getZMapValue( metadata, { Z1K1: 'Z6', Z6K1: 'errors' } ) !== undefined ); } else { return isError( metadata ); } } /** * Determines whether a pair contains a value (i.e., a non-Void first element). * The input pair should be in normal form. * * FIXME (T311055): containsValue might require normal form, as validateAsZObject * is a normal validator. Check and document. * * @param {Object} pair a Z22 * @return {bool} true if Z22K1 is not Z24 / Void; false otherwise */ async function containsValue( pair ) { const Z22K1 = pair.Z22K1.asJSON(); return ( ( await validatesAsZObject( Z22K1 ) ).isValid() && !( isVoid( Z22K1 ) ) ); } function makeBoolean( truthy = false, canonical = false ) { const zobject = {}; Iif ( canonical ) { zobject.Z1K1 = 'Z40'; } else { zobject.Z1K1 = { Z1K1: 'Z9', Z9K1: 'Z40' }; } Eif ( truthy ) { Iif ( canonical ) { zobject.Z40K1 = 'Z41'; } else { zobject.Z40K1 = { Z1K1: 'Z9', Z9K1: 'Z41' }; } } else { if ( canonical ) { zobject.Z40K1 = 'Z42'; } else { zobject.Z40K1 = { Z1K1: 'Z9', Z9K1: 'Z42' }; } } return zobject; } // TODO (T292650): This needs to generate an actual error instead of Z6s. function generateError( errorString = 'An unknown error occurred' ) { return { Z1K1: { Z1K1: 'Z9', Z9K1: 'Z5' }, Z5K2: { Z1K1: { Z1K1: 'Z7', Z7K1: 'Z881', Z881K1: 'Z6' }, K1: { Z1K1: { Z1K1: 'Z9', Z9K1: 'Z6' }, Z6K1: errorString }, K2: { Z1K1: { Z1K1: 'Z7', Z7K1: 'Z881', Z881K1: 'Z6' } } } }; } async function traverseZList( ZList, callback ) { let tail = ZList; if ( tail === undefined ) { return; } while ( getHead( tail ) !== undefined ) { await callback( tail ); tail = getTail( tail ); } } /** * Runs several functions in sequence; returns first one whose Z22K2 is an error. * * @param {Object} Z22 a Z22/ResultEnvelope * @param {Array} callTuples an array whose elements are also arrays of the form * [ function, argument list, name ] * every function accepts Z22 as its first argument and will be called with the * result of the previous function (starting with input Z22). If the resulting Z22 * contains an error (Z22K2), this function returns immediately; otherwise, it * calls the next function with the output of the previous. * @param {Function} callback optional callback to be called on every element of * callTuples; arguments are of the form ( current Z22, current call tuple) * @param {boolean} addZ22 whether to inject Z22.Z22K1 as first argument to callables * @return {Object} a Z22 */ async function returnOnFirstError( Z22, callTuples, callback = null, addZ22 = true ) { let currentPair = Z22; for ( const callTuple of callTuples ) { if ( containsError( currentPair ) || isVoid( currentPair.Z22K1 ) ) { break; } if ( callback !== null ) { await callback( currentPair, callTuple ); } const callable = callTuple[ 0 ]; const args = []; if ( addZ22 ) { args.push( currentPair.Z22K1 ); } for ( const arg of callTuple[ 1 ] ) { args.push( arg ); } currentPair = await callable( ...args ); } return currentPair; } function quoteZObject( ZObject ) { const { ZWrapper } = require( './ZWrapper' ); return ZWrapper.create( { Z1K1: { Z1K1: 'Z9', Z9K1: 'Z99' }, Z99K1: ZObject } ); } function makeWrappedResultEnvelope( ...args ) { const { ZWrapper } = require( './ZWrapper' ); return ZWrapper.create( makeMappedResultEnvelope( ...args ) ); } module.exports = { containsError, containsValue, createSchema, createZObjectKey, generateError, isError, isGenericType, isRefOrString, makeBoolean, makeWrappedResultEnvelope, quoteZObject, returnOnFirstError, traverseZList }; |