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 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8753x 8753x 115x 43x 22x 21x 43x 10x 10x 10x 2x 8x 10x 5x 5x 5x 5x 5x 11x 11x 11x 2x 9x 5x 1x 1x 1x 2x 2x 1x 1x 1x 4x 4x 4x 4x 32x 18x 14x 14x 14x 28x 28x 14x 4x 14x 8x 6x 6x 6x 12x 12x 6x 2x 12x 12x 12x 12x 12x 12x 12x 17x 3x 3x 3x 3x 3x 14x 17x 3x 3x 3x 3x 3x 14x 35x 13x 22x 35x 2x 2x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 1x 2x 2x 1x 1x 1x 1x 1x 2x 2x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 38x 26x 26x 26x 38x 26x 26x 7x 7x 7x 21x 7x 7x 6x 6x 6x 146x 146x 146x 6x 4x 4x 4x 4x 34x 34x 34x 1x 1x 1x 33x 6x 3x 3x 2x 2x 2x 2x 102x 1x 101x 1x 1x 3x 3x 3x 3x 4x 4x 4x 4x 4x 3x 4x 4x 4x 4x 2x 2x 4x 3x 3x 5x 5x 5x 5x 1x 4x 4x 2x 3x 729x 725x 4x 3x 1x 27x 729x 729x 729x 729x 1040x 1040x 1040x 1036x 1x 1x 1035x 1039x 1039x 1039x 1x 1x 1038x 1038x 3x 729x 5x 5x 5x 729x 729x 729x 729x 729x 1040x 729x 4299x 4284x 4299x 4299x 4299x 107x 70x 70x 70x 16x 16x 16x 16x 16x 1x 1x 1x 1x 15x 15x 15x 15x 15x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1325x 1325x 1325x 1x 1x 1325x 1325x 1278x 1325x 1x 1x 1325x 1x 170x 1x 1x 169x 169x 1x 1x 1x 1x 1x 1x 1x 35x 35x 35x 1x 3x 3x 3x 3x 3x 1x 38x 38x 38x 76x 76x 38x 38x 38x 38x 1x 56x 56x 1x 40x 40x 1x 172x 172x 172x 172x 1516x 1516x 414x 1102x 1x | 'use strict'; const normalize = require( '../function-schemata/javascript/src/normalize.js' ); const { createSchema, createZObjectKey, isTrue, isZReference, makeBoolean, traverseZList, responseEnvelopeContainsError, unquoteBrashly, unquoteVeryCarefully } = require( './utils.js' ); const { makeErrorInNormalForm, error } = require( '../function-schemata/javascript/src/error.js' ); const { builtInTypes, convertArrayToKnownTypedList, convertZListToItemArray, findFunctionIdentity, findTypeIdentity, getHead, getTail, isEmptyZList, isGlobalKey, isString, kidFromGlobalKey, makeMappedResultEnvelope, setMetadataValue, wrapInZ6, wrapInZ9, wrapInKeyReference, wrapInQuote, makeTrue, makeFalse } = require( '../function-schemata/javascript/src/utils.js' ); const { readJSON } = require( './fileUtils.js' ); const ErrorFormatter = require( '../function-schemata/javascript/src/errorFormatter.js' ); const { validatesAsType, validatesAsReference, validatesAsFunctionCall, validatesAsBoolean } = require( '../function-schemata/javascript/src/schema.js' ); const { EmptyFrame } = require( './frame.js' ); const { Implementation } = require( './implementation.js' ); const { ZWrapper } = require( './ZWrapper.js' ); const fs = require( 'fs' ); /** * HELPER FUNCTIONS */ /** * Constructs a Z3 object with the given type and name. * * @param {Object} typeZ4 A Z4 object (in JSON form, not ZWrapper) * @param {Object} nameZ6 A Z6 object (in JSON form, not ZWrapper) * @param {Object} [labelZ12] A Z12 object (in JSON form, not ZWrapper) * @return {Object} Constructed Z3 / Key object (in JSON form, not ZWrapper) */ function Z3For( typeZ4, nameZ6, labelZ12 = undefined ) { return { Z1K1: wrapInZ9( 'Z3' ), Z3K1: typeZ4, Z3K2: nameZ6, Z3K3: labelZ12 === undefined ? Z12For( labelZ12 ) : labelZ12 }; } /** * Wraps English label in a Z12/Multilingual String object. * * @param {string} name The English label. * @return {Object} a Z12/Multilingual String containing a single Z11 * wrapping the label (in JSON form, not ZWrapper) */ function Z12For( name ) { return { Z1K1: wrapInZ9( 'Z12' ), Z12K1: convertArrayToKnownTypedList( [ { Z1K1: wrapInZ9( 'Z11' ), Z11K1: wrapInZ9( 'Z1002' ), Z11K2: wrapInZ6( name ) } ], wrapInZ9( 'Z11' ) ) }; } /** * BUILTINS */ function BUILTIN_ECHO_( input ) { return makeMappedResultEnvelope( input, null ); } function BUILTIN_IF_( antecedent, trueConsequent, falseConsequent ) { let result; if ( isTrue( antecedent ) ) { result = trueConsequent; } else { result = falseConsequent; } return makeMappedResultEnvelope( result, null ); } function BUILTIN_VALUE_BY_KEY_( ZKeyReference, ZObject ) { // TODO (T296667): Add test for error case. let goodResult = null, badResult = null; const key = ZKeyReference.Z39K1.Z6K1; if ( ZObject[ key ] === undefined ) { badResult = makeErrorInNormalForm( error.key_not_found, [ ZKeyReference.asJSON(), wrapInQuote( ZObject.asJSON() ) ] ); } else { goodResult = ZObject[ key ]; } return makeMappedResultEnvelope( goodResult, badResult ); } function BUILTIN_VALUES_BY_KEYS_( ZKeyReferences, ZObject ) { const keyrefs = ( ZKeyReferences === undefined ) ? [] : convertZListToItemArray( ZKeyReferences ); const pairType = { Z1K1: wrapInZ9( 'Z7' ), Z7K1: wrapInZ9( 'Z882' ), Z882K1: wrapInZ9( 'Z39' ), Z882K2: wrapInZ9( 'Z1' ) }; const pairArray = []; const missing = []; for ( const keyref of keyrefs ) { const key = keyref.Z39K1.Z6K1; const value = ZObject[ key ]; if ( value === undefined ) { missing.push( key ); } else { pairArray.push( { Z1K1: pairType, K1: keyref, K2: value } ); } } if ( missing.length > 0 ) { const quote = wrapInQuote( ZObject.asJSON() ); const notFoundErrors = []; for ( const key of missing ) { const keyRef = wrapInKeyReference( key ); notFoundErrors.push( makeErrorInNormalForm( error.key_not_found, [ keyRef, quote ] ) ); } let badResult; Iif ( missing.length === 1 ) { badResult = notFoundErrors[ 0 ]; } else { badResult = ErrorFormatter.createZErrorList( notFoundErrors ); } return makeMappedResultEnvelope( null, badResult ); } else { const pairList = convertArrayToKnownTypedList( pairArray, pairType ); const mapType = { Z1K1: wrapInZ9( 'Z7' ), Z7K1: wrapInZ9( 'Z883' ), Z883K1: wrapInZ9( 'Z39' ), Z883K2: wrapInZ9( 'Z1' ) }; const goodResult = { Z1K1: mapType, K1: pairList }; return makeMappedResultEnvelope( goodResult, null ); } } function reifyRecursive( targetZObject ) { if ( isString( targetZObject ) ) { return wrapInZ6( targetZObject ); } const pairType = { Z1K1: wrapInZ9( 'Z7' ), Z7K1: wrapInZ9( 'Z882' ), Z882K1: wrapInZ9( 'Z39' ), Z882K2: wrapInZ9( 'Z1' ) }; const result = []; for ( const key of targetZObject.keys() ) { const value = reifyRecursive( targetZObject.getName( key ) ); result.push( { Z1K1: pairType, K1: wrapInKeyReference( key ), K2: value } ); } return convertArrayToKnownTypedList( result, pairType ); } function BUILTIN_REIFY_( targetZObject ) { return makeMappedResultEnvelope( reifyRecursive( targetZObject ), null ); } function abstractRecursive( ZList ) { if ( ZList.Z1K1 === 'Z6' ) { return ZList.Z6K1; } const result = {}; const arrayOfPairs = convertZListToItemArray( ZList || [] ); for ( const pair of arrayOfPairs ) { const ZKeyReference = pair.K1; result[ ZKeyReference.Z39K1.Z6K1 ] = abstractRecursive( pair.K2 ); } return result; } function BUILTIN_ABSTRACT_( ZList ) { // TODO (T296666): Validate that List is a reified list, i.e. that all // elements are Pairs(Key, ZObject). return makeMappedResultEnvelope( abstractRecursive( ZList ), null ); } function BUILTIN_CONS_( ZObject, list ) { let itemType = wrapInZ9( 'Z1' ); // if validates as type, type is expanded, itemType is at list.Z1K1.Z4K1.Z881K1 Eif ( validatesAsType( list.Z1K1.asJSON() ).isValid() && ( list.Z1K1.Z4K1.Z7K1.Z9K1 === 'Z881' ) ) { itemType = list.Z1K1.Z4K1.Z881K1; } // if validates as function call, type is not expanded, itemType is at list.Z1K1.Z881K1 Iif ( validatesAsFunctionCall( list.Z1K1.asJSON() ).isValid() && ( list.Z1K1.Z7K1.Z9K1 === 'Z881' ) ) { itemType = list.Z1K1.Z881K1; } const typedList = convertArrayToKnownTypedList( [ ZObject ], itemType ); typedList.K2 = list; return makeMappedResultEnvelope( typedList, null ); } function BUILTIN_HEAD_( list ) { if ( isEmptyZList( list ) ) { const argument = wrapInKeyReference( 'Z811K1' ); Eif ( list instanceof ZWrapper ) { list = list.asJSON(); } const quote = wrapInQuote( list ); return makeMappedResultEnvelope( null, makeErrorInNormalForm( error.argument_value_error, [ argument, quote ] ) ); } return makeMappedResultEnvelope( list.K1, null ); } function BUILTIN_TAIL_( list ) { if ( isEmptyZList( list ) ) { const argument = wrapInKeyReference( 'Z812K1' ); Eif ( list instanceof ZWrapper ) { list = list.asJSON(); } const quote = wrapInQuote( list ); return makeMappedResultEnvelope( null, makeErrorInNormalForm( error.argument_value_error, [ argument, quote ] ) ); } return makeMappedResultEnvelope( list.K2, null ); } function BUILTIN_EMPTY_( list ) { let result; if ( isEmptyZList( list ) ) { result = makeTrue(); } else { result = makeFalse(); } return makeMappedResultEnvelope( result, null ); } function BUILTIN_FIRST_( ZTypedPair ) { return makeMappedResultEnvelope( ZTypedPair.K1, null ); } function BUILTIN_SECOND_( ZTypedPair ) { return makeMappedResultEnvelope( ZTypedPair.K2, null ); } async function BUILTIN_GET_ENVELOPE_( QuotedZ7, invariants ) { const { orchestrate } = require( './orchestrate.js' ); this.invariants_.logger.log( 'debug', { message: 'calling orchestrator from BUILTIN_GET_ENVELOPE_()...', requestId: this.invariants_.requestId } ); const envelope = await orchestrate( unquoteBrashly( QuotedZ7 ), invariants, /* implementationSelector= */ null ); this.invariants_.logger.log( 'debug', { message: '...finished calling orchestrator from BUILTIN_GET_ENVELOPE_()', requestId: this.invariants_.requestId } ); const pairType = { Z1K1: wrapInZ9( 'Z7' ), Z7K1: wrapInZ9( 'Z882' ), Z882K1: wrapInZ9( 'Z1' ), Z882K2: wrapInZ9( 'Z1' ) }; const pair = { Z1K1: pairType, K1: envelope.Z22K1, K2: envelope.Z22K2 }; return makeMappedResultEnvelope( pair, null ); } async function BUILTIN_FETCH_PERSISTENT_ZOBJECT_( quotedZ9, invariants ) { const referenceEnvelope = unquoteVeryCarefully( quotedZ9 ); Iif ( responseEnvelopeContainsError( referenceEnvelope ) ) { return referenceEnvelope; } const ZID = referenceEnvelope.Z22K1.Z9K1; // For Builtins, we cannot call dereferenceZObjects, because it returns a modified // Z2 constructed from resolveBuiltinReference(), which is missing keys such as Z2K3 // and fails validation. So we go straight to the definitions file. if ( resolveBuiltinReference( ZID ) !== null ) { return normalize( getPersistentZObjectFromFile( ZID ) ); } let envelope; try { envelope = ( await invariants.resolver.dereferenceZObjects( [ ZID ] ) ).get( ZID ); } catch ( e ) { const message = `Error thrown by dereferenceZObjects(): ${ e }.`; invariants.logger.log( 'error', { message: message, requestId: invariants.requestId } ); const zerror = ErrorFormatter.createZErrorInstance( error.unknown_error, { errorInformation: message } ); return makeMappedResultEnvelope( null, zerror ); } return envelope; } async function BUILTIN_FETCH_WIKIDATA_LEXEME_FORM_( Z6094, invariants ) { const LFID = Z6094.Z6094K1.Z6K1; return await invariants.resolver.dereferenceLexemeForm( LFID ); } async function BUILTIN_FETCH_WIKIDATA_LEXEME_( Z6095, invariants ) { const LID = Z6095.Z6095K1.Z6K1; return await invariants.resolver.dereferenceLexeme( LID ); } async function BUILTIN_FETCH_WIKIDATA_LEXEME_SENSE_( Z6096, invariants ) { const LSID = Z6096.Z6096K1.Z6K1; return await invariants.resolver.dereferenceLexemeSense( LSID ); } function BUILTIN_EQUALS_WIKIDATA_ITEM_( firstItem, secondItem ) { return makeMappedResultEnvelope( makeBoolean( ( firstItem.Z6001K1.Z6091K1.Z6K1 === secondItem.Z6001K1.Z6091K1.Z6K1 ) ), null ); } function BUILTIN_EQUALS_WIKIDATA_PROPERTY_( firstItem, secondItem ) { return makeMappedResultEnvelope( makeBoolean( ( firstItem.Z6002K1.Z6092K1.Z6K1 === secondItem.Z6002K1.Z6092K1.Z6K1 ) ), null ); } function BUILTIN_EQUALS_WIKIDATA_LEXEME_FORM( firstItem, secondItem ) { return makeMappedResultEnvelope( makeBoolean( ( firstItem.Z6004K1.Z6094K1.Z6K1 === secondItem.Z6004K1.Z6094K1.Z6K1 ) ), null ); } function BUILTIN_EQUALS_WIKIDATA_LEXEME_SENSE( firstItem, secondItem ) { return makeMappedResultEnvelope( makeBoolean( ( firstItem.Z6006K1.Z6096K1.Z6K1 === secondItem.Z6006K1.Z6096K1.Z6K1 ) ), null ); } function BUILTIN_EQUALS_WIKIDATA_LEXEME( firstItem, secondItem ) { return makeMappedResultEnvelope( makeBoolean( ( firstItem.Z6005K1.Z6095K1.Z6K1 === secondItem.Z6005K1.Z6095K1.Z6K1 ) ), null ); } function BUILTIN_EQUALS_BOOLEAN_( firstBoolean, secondBoolean ) { const isFirstTrue = isTrue( firstBoolean ); const isSecondTrue = isTrue( secondBoolean ); const result = ( isFirstTrue && isSecondTrue ) || ( !isFirstTrue && !isSecondTrue ); return makeMappedResultEnvelope( makeBoolean( result ), null ); } function getLanguageMap() { // TODO (T302342): switch to using require maybe? // TODO (T372692): Move getLanguageMap to utils.js & improve function coverage of builtins.js const path = 'function-schemata/data/definitions/naturalLanguages.json'; return JSON.parse( fs.readFileSync( path, { encoding: 'utf8' } ) ); } async function BUILTIN_LANGUAGE_CODE_TO_LANGUAGE_( ZString, invariants ) { const languages = getLanguageMap(); const languageCode = ZString.Z6K1; let result = null; Iif ( !( languageCode in languages ) ) { result = makeErrorInNormalForm( error.language_code_not_found, [ wrapInZ6( languageCode ) ] ); return makeMappedResultEnvelope( result ); } const zid = languages[ languageCode ]; result = wrapInZ9( zid ); const wrappedResult = ZWrapper.create( result, new EmptyFrame() ); return await wrappedResult.resolve( invariants, /* ignoreList= */ null, /* resolveInternals= */ false, /* doValidate= */ false ); } function BUILTIN_EQUALS_STRING_( firstZString, secondZString ) { return makeMappedResultEnvelope( makeBoolean( ( firstZString.Z6K1 === secondZString.Z6K1 ) ), null ); } function stringToCharsInternal( characterArray ) { const Z86Array = []; const typeZ86 = wrapInZ9( 'Z86' ); for ( const character of characterArray ) { Z86Array.push( { Z1K1: wrapInZ9( 'Z86' ), Z86K1: wrapInZ6( character ) } ); } return convertArrayToKnownTypedList( Z86Array, typeZ86 ); } function BUILTIN_STRING_TO_CHARS_( ZString ) { return makeMappedResultEnvelope( stringToCharsInternal( Array.from( ZString.Z6K1 ) ), null ); } function charsToStringInternal( list ) { const ZCodePoints = convertZListToItemArray( list || [] ); const result = []; for ( const ZCodePoint of ZCodePoints ) { result.push( ZCodePoint.Z6K1 || ZCodePoint.Z86K1.Z6K1 ); } return result; } function BUILTIN_CHARS_TO_STRING_( list ) { // TODO (T294482): Validate input is a List(Z86). return makeMappedResultEnvelope( wrapInZ6( charsToStringInternal( list ).join( '' ) ), null ); } async function applyFunctionToList( someFunction, itemArray, invariants ) { const { execute } = require( './execute.js' ); const promises = []; for ( const item of itemArray ) { const ZFunctionCall = { Z1K1: wrapInZ9( 'Z7' ), Z7K1: someFunction, K1: item }; const wrappedZFunctionCall = ZWrapper.create( ZFunctionCall, item.getScope() ); promises.push( execute( wrappedZFunctionCall, invariants ) ); } return await Promise.all( promises ); } async function BUILTIN_FILTER_FUNCTION_( someFunction, someList, invariants ) { const itemArray = convertZListToItemArray( someList || [] ); const results = await applyFunctionToList( someFunction, itemArray, invariants ); const goodResults = []; for ( let i = 0; i < results.length; ++i ) { const result = results[ i ]; Iif ( responseEnvelopeContainsError( result ) ) { return result; } if ( !validatesAsBoolean( result.Z22K1.asJSON() ).isValid() ) { // Provided equality function did not return Boolean const argument = wrapInKeyReference( 'Z872K1' ); const quotedValue = wrapInQuote( someFunction.asJSON() ); return makeMappedResultEnvelope( null, makeErrorInNormalForm( error.argument_value_error, [ argument, quotedValue ] ) ); } if ( isTrue( result.Z22K1 ) ) { goodResults.push( itemArray[ i ] ); } } // TODO (T338407): Create a list of the same type as the input. const resultList = convertArrayToKnownTypedList( goodResults, wrapInZ9( 'Z1' ) ); return makeMappedResultEnvelope( resultList ); } async function BUILTIN_MAP_FUNCTION_( someFunction, someList, invariants ) { const itemArray = convertZListToItemArray( someList || [] ); const results = await applyFunctionToList( someFunction, itemArray, invariants ); const goodResults = []; for ( const result of results ) { if ( responseEnvelopeContainsError( result ) ) { return result; } goodResults.push( result.Z22K1 ); } // TODO (T338407): Constrain list's type by return type of someFunction ... // TODO (T370028): ... BUT changing this will break a workaround currently // in use by the community, so tread carefully. const resultList = convertArrayToKnownTypedList( goodResults, wrapInZ9( 'Z1' ) ); return makeMappedResultEnvelope( resultList ); } async function BUILTIN_REDUCE_FUNCTION_( someFunction, someList, someObject, invariants ) { const { execute } = require( './execute.js' ); const itemArray = convertZListToItemArray( someList || [] ); let resultSoFar = someObject; for ( const otherParameter of itemArray ) { const ZFunctionCall = { Z1K1: wrapInZ9( 'Z7' ), Z7K1: someFunction, K1: resultSoFar, K2: otherParameter }; const wrappedZFunctionCall = ZWrapper.create( ZFunctionCall, resultSoFar.getScope() ); const resultEnvelope = await execute( wrappedZFunctionCall, invariants ); Iif ( responseEnvelopeContainsError( resultSoFar ) ) { return resultSoFar; } resultSoFar = resultEnvelope.Z22K1; } return makeMappedResultEnvelope( resultSoFar ); } function BUILTIN_TRIGGER_METADATA_( keyZ6, valueZ1 ) { const response = makeMappedResultEnvelope( null, null ); setMetadataValue( response, keyZ6, valueZ1 ); return response; } function BUILTIN_SAME_( firstZCodePoint, secondZCodePoint ) { let result; if ( firstZCodePoint.Z86K1.Z6K1 === secondZCodePoint.Z86K1.Z6K1 ) { result = makeTrue(); } else { result = makeFalse(); } return makeMappedResultEnvelope( result, null ); } async function BUILTIN_EQUALS_LIST_( list1, list2, ZFunction, invariants ) { const { execute } = require( './execute.js' ); while ( !isEmptyZList( list1 ) && !isEmptyZList( list2 ) ) { const headEqualityZ22 = await execute( ZWrapper.create( { Z1K1: wrapInZ9( 'Z7' ), Z7K1: ZFunction, K1: getHead( list1 ), K2: getHead( list2 ) }, new EmptyFrame() ), invariants ); Iif ( responseEnvelopeContainsError( headEqualityZ22 ) ) { return headEqualityZ22; } else Iif ( !validatesAsBoolean( headEqualityZ22.Z22K1.asJSON() ).isValid() ) { // Provided equality function did not return Boolean const argument = wrapInKeyReference( 'Z889K3' ); const quotedValue = wrapInQuote( ZFunction.asJSON() ); return makeMappedResultEnvelope( null, makeErrorInNormalForm( error.argument_value_error, [ argument, quotedValue ] ) ); } else if ( !isTrue( headEqualityZ22.Z22K1 ) ) { return makeMappedResultEnvelope( makeFalse() ); } list1 = getTail( list1 ); list2 = getTail( list2 ); } return makeMappedResultEnvelope( makeBoolean( isEmptyZList( list1 ) === isEmptyZList( list2 ) ) ); } function BUILTIN_UNQUOTE_( ZQuote ) { return unquoteVeryCarefully( ZQuote ); } function makeValidatorResultEnvelope( ZObject, errors ) { if ( errors.length === 0 ) { return makeMappedResultEnvelope( ZObject.asJSON(), null ); } else if ( errors.length === 1 ) { return makeMappedResultEnvelope( null, errors[ 0 ] ); } else { return makeMappedResultEnvelope( null, ErrorFormatter.createZErrorList( errors ) ); } } async function resolveAllZ4Keys_( ZType, invariants ) { const resolvedKeys = new Set(); const typesToResolve = []; typesToResolve.push( ZType ); while ( typesToResolve.length > 0 ) { const toResolve = typesToResolve.pop( 0 ); await traverseZList( toResolve.Z4K2, async ( Z3Tail ) => { await ( Z3Tail.resolveEphemeral( [ 'K1', 'Z3K1' ], invariants, /* ignoreList= */null, /* resolveInternals= */false ) ); const nextType = Z3Tail.K1.getNameEphemeral( 'Z3K1' ); const key = createZObjectKey( nextType ).asString(); if ( resolvedKeys.has( key ) ) { return; } resolvedKeys.add( key ); typesToResolve.push( nextType ); } ); } } async function BUILTIN_SCHEMA_VALIDATOR_( quotedObject, quotedType, invariants ) { // TODO (T290698): Use this instead of BUILTIN_EMPTY_VALIDATOR_. const ZObject = unquoteBrashly( quotedObject ); const ZType = ( await ( unquoteBrashly( quotedType ).resolve( invariants, /* originalObject= */null, /* ignoreList= */null, /* resolveInternals= */ false, /* doValidate= */ false ) ) ).Z22K1; // Ensure all internal type references are resolved. // TODO (T297904): Also need to resolve generic types. await resolveAllZ4Keys_( ZType, invariants ); const theSchema = createSchema( { Z1K1: ZType.asJSONEphemeral() } ); // TODO (T294289): Return validationStatus Z5s as Z22K2. const theStatus = theSchema.validateStatus( ZObject.asJSON() ); let errors; if ( theStatus.isValid() ) { errors = []; } else { errors = [ theStatus.getZ5() ]; } return makeValidatorResultEnvelope( ZObject, errors ); } function BUILTIN_EMPTY_VALIDATOR_( targetZObject ) { return makeMappedResultEnvelope( targetZObject, null ); } /** * Validates the keys of a normal Typed List. This functions looks for duplicate or non-sequential * keys and keys that don't follow the expected format of (Z)?<identity>Kn. * * @param {Object} list the Typed List being validated. * @param {Function} key a function to get the key of a list element. * @param {string} identity the identity of the list's parent. * * @return {Object} a Typed List of Z5/Error. */ function arrayValidator( list, key, identity ) { const keys = convertZListToItemArray( list || [] ).map( key ); const errorInfo = []; const seen = new Set(); for ( let i = 0; i < keys.length; ++i ) { const originalKey = keys[ i ]; let key = originalKey; if ( isGlobalKey( key ) ) { if ( !originalKey.startsWith( identity ) ) { errorInfo.push( { indexInt: i, key: originalKey } ); continue; } key = kidFromGlobalKey( key ); } const expectedIndex = i + 1; const actualIndex = Number( key.replace( 'K', '' ) ); if ( seen.has( originalKey ) ) { errorInfo.push( { indexInt: i, key: originalKey } ); continue; } else { seen.add( originalKey ); } if ( actualIndex !== expectedIndex ) { errorInfo.push( { indexInt: i, key: originalKey } ); } } return errorInfo.map( ( info ) => { const index = wrapInZ6( info.indexInt.toString() ); const innerError = makeErrorInNormalForm( error.invalid_key, [ wrapInZ6( info.key ) ] ); return makeErrorInNormalForm( error.array_element_not_well_formed, [ index, innerError ] ); } ); } function BUILTIN_FUNCTION_VALIDATOR_( ZQuote ) { const ZObject = unquoteBrashly( ZQuote ); let functionIdentity = findFunctionIdentity( ZObject ); Eif ( functionIdentity ) { functionIdentity = functionIdentity.Z9K1; } const errors = arrayValidator( ZObject.Z8K1, ( x ) => x.Z17K2.Z6K1, functionIdentity ); return makeValidatorResultEnvelope( ZQuote, errors ); } function BUILTIN_Z4_TYPE_VALIDATOR_( ZQuote ) { const ZObject = unquoteBrashly( ZQuote ); let typeIdentity = findTypeIdentity( ZObject ); if ( typeIdentity && isZReference( typeIdentity ) ) { typeIdentity = typeIdentity.Z9K1; } const errors = arrayValidator( ZObject.Z4K2, ( x ) => x.Z3K2.Z6K1, typeIdentity ); return makeValidatorResultEnvelope( ZQuote, errors ); } async function BUILTIN_FUNCTION_CALL_VALIDATOR_INTERNAL_( ZQuote, errors, invariants ) { const ZObject = unquoteBrashly( ZQuote ); const { getArgumentStates } = require( './execute.js' ); const argumentStates = await getArgumentStates( ZObject, invariants ); const dictDict = {}; for ( const argumentState of argumentStates ) { if ( argumentState.state === 'ERROR' ) { // This is probably because Z8K1 couldn't be dereferenced and is // fine. return; } const argumentDict = argumentState.argumentDict; dictDict[ argumentDict.name ] = argumentDict; const localKey = argumentDict.name.replace( /^Z\d+/, '' ); dictDict[ localKey ] = argumentDict; } const keysToSkip = new Set( [ 'Z1K1', 'Z7K1' ] ); // TODO (T296668): Also check declared arguments that are absent from the Z7. // TODO (T296668): Also check local keys. for ( const key of ZObject.keys() ) { if ( keysToSkip.has( key ) ) { continue; } const argumentDict = dictDict[ key ]; if ( argumentDict === undefined ) { errors.push( makeErrorInNormalForm( // Invalid key for function call error.invalid_key, [ wrapInZ6( key ) ] ) ); continue; } let type = ZObject[ key ].Z1K1.Z9K1 || ZObject[ key ].Z1K1; if ( type instanceof ZWrapper ) { type = type.asJSON(); } let declaredType = argumentDict.declaredType; if ( declaredType instanceof ZWrapper ) { declaredType = declaredType.asJSON(); } // TODO (T296669): Fix type semantics below; do something when declaredType is a Z4. if ( validatesAsType( declaredType ).isValid() ) { continue; } if ( validatesAsReference( declaredType ).isValid() ) { declaredType = declaredType.Z9K1; } // Type mismatches for Z7, Z9, and Z18 will be caught at runtime. const skippableTypes = new Set( [ 'Z18', 'Z9', 'Z7' ] ); // TODO (T296669): More intricate subtype semantics once we have generic // types (just checking for Z1 is not sufficient). if ( !( declaredType === type || declaredType === 'Z1' || skippableTypes.has( type ) ) ) { errors.push( makeErrorInNormalForm( error.argument_type_mismatch, [ declaredType, type, ZObject[ key ].asJSON() ] ) ); } } } async function BUILTIN_FUNCTION_CALL_VALIDATOR_( ZQuote, invariants ) { const errors = []; await BUILTIN_FUNCTION_CALL_VALIDATOR_INTERNAL_( ZQuote, errors, invariants ); return makeValidatorResultEnvelope( ZQuote, errors ); } async function BUILTIN_MULTILINGUAL_TEXT_VALIDATOR_( ZQuote, invariants ) { const ZObject = unquoteBrashly( ZQuote ); const errors = []; const ZMonolingualStrings = convertZListToItemArray( ZObject.Z12K1 || [] ); const languages = await Promise.all( ZMonolingualStrings.map( async ( ZMonolingualString ) => ( await ZMonolingualString.resolveKey( [ 'Z11K1', 'Z60K1', 'Z6K1' ], invariants ) ).Z22K1.Z6K1 ) ); const seen = new Set(); for ( let i = 0; i < languages.length; ++i ) { if ( seen.has( languages[ i ] ) ) { const argument = wrapInKeyReference( 'Z112K1' ); errors.push( makeErrorInNormalForm( error.argument_value_error, [ argument, ZQuote ] ) ); } seen.add( languages[ i ] ); } return makeValidatorResultEnvelope( ZQuote, errors ); } function BUILTIN_MULTILINGUAL_STRINGSET_VALIDATOR_( ZQuote ) { const ZObject = unquoteBrashly( ZQuote ); const errors = []; const ZMonolingualStringSets = convertZListToItemArray( ZObject.Z32K1 || [] ); const languages = ZMonolingualStringSets.map( ( ZMonolingualStringSet ) => ZMonolingualStringSet.Z31K1.Z60K1.Z6K1 ); const seen = new Set(); for ( let i = 0; i < languages.length; ++i ) { if ( seen.has( languages[ i ] ) ) { const argument = wrapInKeyReference( 'Z132K1' ); errors.push( makeErrorInNormalForm( error.argument_value_error, [ argument, ZQuote ] ) ); } seen.add( languages[ i ] ); } return makeValidatorResultEnvelope( ZQuote, errors ); } function BUILTIN_ERROR_TYPE_VALIDATOR_( ZQuote ) { return makeValidatorResultEnvelope( ZQuote, [] ); } function resolveListType( typeZ4 ) { if ( typeZ4 instanceof ZWrapper ) { typeZ4 = typeZ4.asJSON(); } const itsMe = { Z1K1: wrapInZ9( 'Z7' ), Z7K1: wrapInZ9( 'Z881' ), Z881K1: typeZ4 }; const ZType = { Z1K1: wrapInZ9( 'Z4' ), Z4K1: itsMe, Z4K2: convertArrayToKnownTypedList( [ Z3For( typeZ4, wrapInZ6( 'K1' ), Z12For( 'head' ) ), Z3For( itsMe, wrapInZ6( 'K2' ), Z12For( 'tail' ) ) ], wrapInZ9( 'Z3' ) ), Z4K3: wrapInZ9( 'Z831' ) }; return makeMappedResultEnvelope( ZType, null ); } function BUILTIN_GENERIC_LIST_TYPE_( typeZ4 ) { return resolveListType( typeZ4 ); } function BUILTIN_GENERIC_PAIR_TYPE_( firstType, secondType ) { const itsMe = { Z1K1: wrapInZ9( 'Z7' ), Z7K1: wrapInZ9( 'Z882' ), Z882K1: firstType, Z882K2: secondType }; const ZType = { Z1K1: wrapInZ9( 'Z4' ), Z4K1: itsMe, Z4K2: convertArrayToKnownTypedList( [ Z3For( firstType, wrapInZ6( 'K1' ), Z12For( 'first' ) ), Z3For( secondType, wrapInZ6( 'K2' ), Z12For( 'second' ) ) ], wrapInZ9( 'Z3' ) ), Z4K3: wrapInZ9( 'Z831' ) }; return makeMappedResultEnvelope( ZType, null ); } function BUILTIN_GENERIC_MAP_TYPE_( keyType, valueType, invariants ) { // TODO (T302015) When ZMap keys are extended beyond Z6/String, update accordingly const allowedKeyTypes = [ 'Z6', 'Z39' ]; let identity = keyType; while ( identity.Z4K1 !== undefined ) { identity = identity.Z4K1; } if ( !allowedKeyTypes.includes( identity.Z9K1 ) ) { const argument = wrapInKeyReference( 'Z883K1' ); const quotedValue = wrapInQuote( keyType.asJSON() ); const newError = makeErrorInNormalForm( error.argument_value_error, [ argument, quotedValue ] ); return makeMappedResultEnvelope( null, newError ); } const itsMe = { Z1K1: wrapInZ9( 'Z7' ), Z7K1: wrapInZ9( 'Z883' ), Z883K1: keyType, Z883K2: valueType }; const pairType = BUILTIN_GENERIC_PAIR_TYPE_( keyType, valueType ).Z22K1; const listType = BUILTIN_GENERIC_LIST_TYPE_( pairType ).Z22K1; const ZType = { Z1K1: wrapInZ9( 'Z4' ), Z4K1: itsMe, Z4K2: convertArrayToKnownTypedList( [ Z3For( listType, wrapInZ6( 'K1' ), Z12For( 'elements' ) ) ], wrapInZ9( 'Z3' ) ), Z4K3: wrapInZ9( 'Z831' ) }; return makeMappedResultEnvelope( ZType, null ); } async function BUILTIN_ERRORTYPE_TO_TYPE_( errorType, invariants ) { const resolutionEnvelope = await errorType.Z50K1.resolve( invariants, /* ignoreList= */ null, /* resolveInternals= */ false, /* doValidate= */ false ); const theKeys = resolutionEnvelope.Z22K1; const itsMe = { Z1K1: wrapInZ9( 'Z7' ), Z7K1: wrapInZ9( 'Z885' ), Z885K1: errorType }; const theType = { Z1K1: wrapInZ9( 'Z4' ), Z4K1: itsMe, Z4K2: theKeys, Z4K3: wrapInZ9( 'Z101' ) }; return makeMappedResultEnvelope( theType, null ); } const builtinFunctions = new Map(); builtinFunctions.set( 'Z901', BUILTIN_ECHO_ ); builtinFunctions.set( 'Z902', BUILTIN_IF_ ); builtinFunctions.set( 'Z903', BUILTIN_VALUE_BY_KEY_ ); builtinFunctions.set( 'Z904', BUILTIN_VALUES_BY_KEYS_ ); builtinFunctions.set( 'Z905', BUILTIN_REIFY_ ); builtinFunctions.set( 'Z908', BUILTIN_ABSTRACT_ ); builtinFunctions.set( 'Z910', BUILTIN_CONS_ ); builtinFunctions.set( 'Z911', BUILTIN_HEAD_ ); builtinFunctions.set( 'Z912', BUILTIN_TAIL_ ); builtinFunctions.set( 'Z913', BUILTIN_EMPTY_ ); builtinFunctions.set( 'Z920', BUILTIN_TRIGGER_METADATA_ ); builtinFunctions.set( 'Z921', BUILTIN_FIRST_ ); builtinFunctions.set( 'Z922', BUILTIN_SECOND_ ); builtinFunctions.set( 'Z923', BUILTIN_GET_ENVELOPE_ ); builtinFunctions.set( 'Z928', BUILTIN_FETCH_PERSISTENT_ZOBJECT_ ); builtinFunctions.set( 'Z931', BUILTIN_SCHEMA_VALIDATOR_ ); builtinFunctions.set( 'Z944', BUILTIN_EQUALS_BOOLEAN_ ); builtinFunctions.set( 'Z960', BUILTIN_LANGUAGE_CODE_TO_LANGUAGE_ ); builtinFunctions.set( 'Z966', BUILTIN_EQUALS_STRING_ ); builtinFunctions.set( 'Z968', BUILTIN_STRING_TO_CHARS_ ); builtinFunctions.set( 'Z972', BUILTIN_FILTER_FUNCTION_ ); builtinFunctions.set( 'Z973', BUILTIN_MAP_FUNCTION_ ); builtinFunctions.set( 'Z976', BUILTIN_REDUCE_FUNCTION_ ); builtinFunctions.set( 'Z981', BUILTIN_GENERIC_LIST_TYPE_ ); builtinFunctions.set( 'Z982', BUILTIN_GENERIC_PAIR_TYPE_ ); builtinFunctions.set( 'Z983', BUILTIN_GENERIC_MAP_TYPE_ ); builtinFunctions.set( 'Z985', BUILTIN_ERRORTYPE_TO_TYPE_ ); builtinFunctions.set( 'Z986', BUILTIN_CHARS_TO_STRING_ ); builtinFunctions.set( 'Z988', BUILTIN_SAME_ ); builtinFunctions.set( 'Z989', BUILTIN_EQUALS_LIST_ ); builtinFunctions.set( 'Z999', BUILTIN_UNQUOTE_ ); // Wikidata-related Builtins builtinFunctions.set( 'Z6924', BUILTIN_FETCH_WIKIDATA_LEXEME_FORM_ ); builtinFunctions.set( 'Z6925', BUILTIN_FETCH_WIKIDATA_LEXEME_ ); builtinFunctions.set( 'Z6926', BUILTIN_FETCH_WIKIDATA_LEXEME_SENSE_ ); builtinFunctions.set( 'Z6901', BUILTIN_EQUALS_WIKIDATA_ITEM_ ); builtinFunctions.set( 'Z6902', BUILTIN_EQUALS_WIKIDATA_PROPERTY_ ); builtinFunctions.set( 'Z6904', BUILTIN_EQUALS_WIKIDATA_LEXEME_FORM ); builtinFunctions.set( 'Z6905', BUILTIN_EQUALS_WIKIDATA_LEXEME ); builtinFunctions.set( 'Z6906', BUILTIN_EQUALS_WIKIDATA_LEXEME_SENSE ); // validators builtinFunctions.set( 'Z201', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z202', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z203', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z204', BUILTIN_Z4_TYPE_VALIDATOR_ ); builtinFunctions.set( 'Z205', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z206', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z207', BUILTIN_FUNCTION_CALL_VALIDATOR_ ); builtinFunctions.set( 'Z208', BUILTIN_FUNCTION_VALIDATOR_ ); builtinFunctions.set( 'Z209', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z211', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z212', BUILTIN_MULTILINGUAL_TEXT_VALIDATOR_ ); builtinFunctions.set( 'Z213', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z214', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z216', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z217', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z218', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z220', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z221', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z222', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z223', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z231', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z232', BUILTIN_MULTILINGUAL_STRINGSET_VALIDATOR_ ); builtinFunctions.set( 'Z239', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z240', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z241', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z242', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z250', BUILTIN_ERROR_TYPE_VALIDATOR_ ); builtinFunctions.set( 'Z260', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z261', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z270', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z280', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z286', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z289', BUILTIN_EMPTY_VALIDATOR_ ); builtinFunctions.set( 'Z299', BUILTIN_EMPTY_VALIDATOR_ ); /** * Retrieves an in-memory JS function implementing a builtin. * * @param {Object} ZID the function to retrieve an implementation for * @return {Implementation} an implementation */ function getFunction( ZID ) { const result = builtinFunctions.get( ZID ); Iif ( result === undefined ) { return null; } return result; } const lazyFunctions = new Map(); lazyFunctions.set( 'Z902', [ 'Z802K2', 'Z802K3' ] ); /** * Retrieves lazy variables for the given function. * * @param {string} ZID the function * @return {Array} an array of variables which are lazy for the given function */ function getLazyVariables( ZID ) { let lazy = lazyFunctions.get( ZID ); if ( lazy === undefined ) { lazy = []; } return lazy; } const lazyReturns = new Set(); lazyReturns.add( 'Z902' ); /** * For a given ZID, determine whether return value should be evaluated after execution. * * @param {string} ZID the function * @return {boolean} whether the function is lazy */ function getLazyReturn( ZID ) { return lazyReturns.has( ZID ); } const builtinReferences = new Map(); function definitionFileForZid( ZID ) { return `function-schemata/data/definitions/${ ZID }.json`; } function getPersistentZObjectFromFile( ZID ) { const fileName = definitionFileForZid( ZID ); return readJSON( fileName ); } function getDefinitionFromFile( ZID ) { const fileName = definitionFileForZid( ZID ); return readJSON( fileName ).Z2K2; } // Built-in implementations. const implementationZIDs = [ 'Z901', 'Z902', 'Z903', 'Z904', 'Z905', 'Z908', 'Z910', 'Z911', 'Z912', 'Z913', 'Z920', 'Z921', 'Z922', 'Z923', 'Z928', 'Z944', 'Z960', 'Z966', 'Z968', 'Z972', 'Z973', 'Z976', // TODO (T314383): Add these ZIDs to the list of implementations. See below. /* * 'Z981', 'Z982', */ // TODO (T314364): Add this ZID to the list of implementations. See below. /* * 'Z983', */ 'Z986', 'Z988', 'Z989', 'Z999', 'Z931', // Wikidata-related implementations 'Z6901', 'Z6902', 'Z6904', 'Z6905', 'Z6906', 'Z6924', 'Z6925', 'Z6926' ]; // Built-in functions. const functionZIDs = [ 'Z801', 'Z802', 'Z803', 'Z804', 'Z805', 'Z808', 'Z810', 'Z811', 'Z812', 'Z813', 'Z820', 'Z821', 'Z822', 'Z823', 'Z828', 'Z844', 'Z860', 'Z866', 'Z868', 'Z872', 'Z873', 'Z876', 'Z881', 'Z882', 'Z883', 'Z886', 'Z888', 'Z889', 'Z899', 'Z831', 'Z6801', 'Z6802', 'Z6804', 'Z6805', 'Z6806', 'Z6824', 'Z6825', 'Z6826' ]; // Validators for core types. const validatorZIDs = [ 'Z101', 'Z102', 'Z103', 'Z104', 'Z105', 'Z106', 'Z107', 'Z108', 'Z109', 'Z111', 'Z112', 'Z114', 'Z116', 'Z117', 'Z118', 'Z120', 'Z121', 'Z122', 'Z123', 'Z139', 'Z140', 'Z150', 'Z160', 'Z161', 'Z180', 'Z186', 'Z189', 'Z199', 'Z201', 'Z202', 'Z203', 'Z204', 'Z205', 'Z206', 'Z207', 'Z208', 'Z209', 'Z211', 'Z212', 'Z214', 'Z216', 'Z217', 'Z218', 'Z220', 'Z221', 'Z222', 'Z223', 'Z239', 'Z240', 'Z250', 'Z260', 'Z261', 'Z280', 'Z286', 'Z289', 'Z299' ]; ( function setBuiltinReferences() { const implementations = new Map(); const definitions = new Map(); for ( const ZID of implementationZIDs ) { const theDefinition = getDefinitionFromFile( ZID ); implementations.set( ZID, theDefinition ); definitions.set( ZID, theDefinition ); } // TODO (T314364): Undo this special case for Typed Map. // We do this because Map has some special validation logic which can't be // expressed in the JSON definition without Unions. UNIONIZE NOW // TODO (T314383): Undo these special cases for generic List and Pair. // Compositions don't currently execute in time for schema validation, so // the composition implementations of built-in generic functions produce // types which allow anything to validate. for ( const ZIDMod100 of [ 81, 82, 83 ] ) { const functionZID = 'Z' + ( 800 + ZIDMod100 ); const implementationZID = 'Z' + ( 900 + ZIDMod100 ); const theImplementation = { Z1K1: 'Z14', Z14K1: functionZID, Z14K4: wrapInZ6( implementationZID ) }; implementations.set( implementationZID, theImplementation ); definitions.set( implementationZID, theImplementation ); } for ( const ZID of functionZIDs ) { const theDefinition = getDefinitionFromFile( ZID ); const Z8K4 = []; for ( const element of theDefinition.Z8K4 ) { const implementationDefinition = implementations.get( element ); if ( implementationDefinition === undefined ) { Z8K4.push( element ); } else { Z8K4.push( implementationDefinition ); } } theDefinition.Z8K4 = Z8K4; definitions.set( ZID, theDefinition ); } for ( const ZID of validatorZIDs ) { const theDefinition = getDefinitionFromFile( ZID ); definitions.set( ZID, theDefinition ); } for ( const ZID of builtInTypes() ) { const theDefinition = getDefinitionFromFile( ZID ); definitions.set( ZID, theDefinition ); } for ( const entry of definitions.entries() ) { const ZID = entry[ 0 ]; const definition = entry[ 1 ]; const normalizedDefinition = ( normalize( definition ) ).Z22K1; builtinReferences.set( ZID, normalizedDefinition ); } }() ); /** * Creates a Z8 corresponding to a bulitin function. * * @param {string} ZID reference to a builtin function * @return {Object} a Z8 or null */ function resolveBuiltinReference( ZID ) { const result = builtinReferences.get( ZID ); if ( result === undefined ) { return null; } return result; } module.exports = { builtinReferences, getFunction, getLazyVariables, getLazyReturn, resolveBuiltinReference, resolveListType, getLanguageMap }; |