Overview
Comment: | OFNumber: Never access ivars after init
This is a requirement for tagged pointer numbers, which cannot have |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | tagged-pointers |
Files: | files | file ages | folders |
SHA3-256: |
bc9998913aed615bb977b7e2dea9a8c0 |
User & Date: | js on 2020-08-30 16:22:17 |
Other Links: | branch diff | manifest | tags |
Context
2020-09-04
| ||
16:39 | OFNumber: Add support for tagged pointers check-in: 8c32791836 user: js tags: tagged-pointers | |
2020-08-30
| ||
16:22 | OFNumber: Never access ivars after init check-in: bc9998913a user: js tags: tagged-pointers | |
15:40 | Merge trunk into branch "tagged-pointers" check-in: ff6eb7b6c3 user: js tags: tagged-pointers | |
Changes
Modified src/OFNumber.h from [5740ff4672] to [ff0ae5f35c].
︙ | ︙ | |||
49 50 51 52 53 54 55 | OFJSONRepresentation, OFMessagePackRepresentation> { union of_number_value { double float_; long long signed_; unsigned long long unsigned_; } _value; | < < < < < | 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | OFJSONRepresentation, OFMessagePackRepresentation> { union of_number_value { double float_; long long signed_; unsigned long long unsigned_; } _value; const char *_typeEncoding; } /*! * @brief The OFNumber as a `bool`. */ @property (readonly, nonatomic) bool boolValue; |
︙ | ︙ |
Modified src/OFNumber.m from [17a849254e] to [b25cee6a5c].
︙ | ︙ | |||
66 67 68 69 70 71 72 73 74 75 76 77 78 79 | SINGLETON(unsignedShortZeroNumber, initWithUnsignedShort:, 0) SINGLETON(unsignedIntZeroNumber, initWithUnsignedInt:, 0) SINGLETON(unsignedLongZeroNumber, initWithUnsignedLong:, 0) SINGLETON(unsignedLongLongZeroNumber, initWithUnsignedLongLong:, 0) SINGLETON(floatZeroNumber, initWithFloat:, 0) SINGLETON(doubleZeroNumber, initWithDouble:, 0) #undef SINGLETON @implementation OFNumberPlaceholder - (instancetype)initWithBool: (bool)value { if (value) { static of_once_t once = OF_ONCE_INIT; of_once(&once, trueNumberInit); | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | SINGLETON(unsignedShortZeroNumber, initWithUnsignedShort:, 0) SINGLETON(unsignedIntZeroNumber, initWithUnsignedInt:, 0) SINGLETON(unsignedLongZeroNumber, initWithUnsignedLong:, 0) SINGLETON(unsignedLongLongZeroNumber, initWithUnsignedLongLong:, 0) SINGLETON(floatZeroNumber, initWithFloat:, 0) SINGLETON(doubleZeroNumber, initWithDouble:, 0) #undef SINGLETON static bool isUnsigned(OFNumber *number) { switch (*number.objCType) { case 'B': return true; case 'C': return true; case 'S': return true; case 'I': return true; case 'L': return true; case 'Q': return true; default: return false; } } static bool isSigned(OFNumber *number) { switch (*number.objCType) { case 'c': return true; case 's': return true; case 'i': return true; case 'l': return true; case 'q': return true; default: return false; } } static bool isFloat(OFNumber *number) { switch (*number.objCType) { case 'f': return true; case 'd': return true; default: return false; } } @implementation OFNumberPlaceholder - (instancetype)initWithBool: (bool)value { if (value) { static of_once_t once = OF_ONCE_INIT; of_once(&once, trueNumberInit); |
︙ | ︙ | |||
335 336 337 338 339 340 341 | } - (instancetype)initWithBool: (bool)value { self = [super init]; _value.unsigned_ = value; | < < < < < < < < < < < < < < < < | 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 | } - (instancetype)initWithBool: (bool)value { self = [super init]; _value.unsigned_ = value; _typeEncoding = @encode(bool); return self; } - (instancetype)initWithChar: (signed char)value { self = [super init]; _value.signed_ = value; _typeEncoding = @encode(signed char); return self; } - (instancetype)initWithShort: (short)value { self = [super init]; _value.signed_ = value; _typeEncoding = @encode(short); return self; } - (instancetype)initWithInt: (int)value { self = [super init]; _value.signed_ = value; _typeEncoding = @encode(int); return self; } - (instancetype)initWithLong: (long)value { self = [super init]; _value.signed_ = value; _typeEncoding = @encode(long); return self; } - (instancetype)initWithLongLong: (long long)value { self = [super init]; _value.signed_ = value; _typeEncoding = @encode(long long); return self; } - (instancetype)initWithUnsignedChar: (unsigned char)value { self = [super init]; _value.unsigned_ = value; _typeEncoding = @encode(unsigned long); return self; } - (instancetype)initWithUnsignedShort: (unsigned short)value { self = [super init]; _value.unsigned_ = value; _typeEncoding = @encode(unsigned short); return self; } - (instancetype)initWithUnsignedInt: (unsigned int)value { self = [super init]; _value.unsigned_ = value; _typeEncoding = @encode(unsigned int); return self; } - (instancetype)initWithUnsignedLong: (unsigned long)value { self = [super init]; _value.unsigned_ = value; _typeEncoding = @encode(unsigned long); return self; } - (instancetype)initWithUnsignedLongLong: (unsigned long long)value { self = [super init]; _value.unsigned_ = value; _typeEncoding = @encode(unsigned long long); return self; } - (instancetype)initWithPtrDiff: (ptrdiff_t)value { self = [super init]; _value.signed_ = value; _typeEncoding = @encode(ptrdiff_t); return self; } - (instancetype)initWithIntPtr: (intptr_t)value { self = [super init]; _value.signed_ = value; _typeEncoding = @encode(intptr_t); return self; } - (instancetype)initWithUIntPtr: (uintptr_t)value { self = [super init]; _value.unsigned_ = value; _typeEncoding = @encode(uintptr_t); return self; } - (instancetype)initWithFloat: (float)value { self = [super init]; _value.float_ = value; _typeEncoding = @encode(float); return self; } - (instancetype)initWithDouble: (double)value { self = [super init]; _value.float_ = value; _typeEncoding = @encode(double); return self; } - (instancetype)initWithSerialization: (OFXMLElement *)element { |
︙ | ︙ | |||
562 563 564 565 566 567 568 | { return _typeEncoding; } - (void)getValue: (void *)value size: (size_t)size { | | | | | | | | | | | | | | | | | > > > > > > > > > > > | > > | > > > > > > > > | > > > | | | | | | | | > | | | | < < | < < < | | | | < < < < < < < < | < < < < | < | < | | < | 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 | { return _typeEncoding; } - (void)getValue: (void *)value size: (size_t)size { switch (*self.objCType) { #define CASE(enc, type, property) \ case enc: { \ type tmp = (type)self.property; \ \ if (size != sizeof(type)) \ @throw [OFOutOfRangeException exception]; \ \ memcpy(value, &tmp, size); \ break; \ } CASE('B', bool, unsignedLongLongValue) CASE('c', signed char, longLongValue) CASE('s', short, longLongValue) CASE('i', int, longLongValue) CASE('l', long, longLongValue) CASE('q', long long, longLongValue) CASE('C', unsigned char, unsignedLongLongValue) CASE('S', unsigned short, unsignedLongLongValue) CASE('I', unsigned int, unsignedLongLongValue) CASE('L', unsigned long, unsignedLongLongValue) CASE('Q', unsigned long long, unsignedLongLongValue) CASE('f', float, doubleValue) CASE('d', double, doubleValue) #undef CASE default: @throw [OFInvalidFormatException exception]; } } - (long long)longLongValue { if (isFloat(self)) return _value.float_; else if (isSigned(self)) return _value.signed_; else if (isUnsigned(self)) return _value.unsigned_; else @throw [OFInvalidFormatException exception]; } - (unsigned long long)unsignedLongLongValue { if (isFloat(self)) return _value.float_; else if (isSigned(self)) return _value.signed_; else if (isUnsigned(self)) return _value.unsigned_; else @throw [OFInvalidFormatException exception]; } - (double)doubleValue { if (isFloat(self)) return _value.float_; else if (isSigned(self)) return _value.signed_; else if (isUnsigned(self)) return _value.unsigned_; else @throw [OFInvalidFormatException exception]; } - (bool)boolValue { return (bool)self.unsignedLongLongValue; } - (signed char)charValue { return (signed char)self.longLongValue; } - (short)shortValue { return (short)self.longLongValue; } - (int)intValue { return (int)self.longLongValue; } - (long)longValue { return (long)self.longLongValue; } - (unsigned char)unsignedCharValue { return (unsigned char)self.unsignedLongLongValue; } - (unsigned short)unsignedShortValue { return (unsigned short)self.unsignedLongLongValue; } - (unsigned int)unsignedIntValue { return (unsigned int)self.unsignedLongLongValue; } - (unsigned long)unsignedLongValue { return (unsigned long)self.unsignedLongLongValue; } - (float)floatValue { return (float)self.doubleValue; } - (bool)isEqual: (id)object { OFNumber *number; if (object == self) return true; if (![object isKindOfClass: [OFNumber class]]) return false; number = object; if (isFloat(self) || isFloat(number)) { double value1 = number.doubleValue; double value2 = self.doubleValue; if (isnan(value1) && isnan(value2)) return true; if (isnan(value1) || isnan(value2)) return false; return (value1 == value2); } if (isSigned(self) || isSigned(number)) return (number.longLongValue == self.longLongValue); return (number.unsignedLongLongValue == self.unsignedLongLongValue); } - (of_comparison_result_t)compare: (id <OFComparing>)object { OFNumber *number; if (![(id)object isKindOfClass: [OFNumber class]]) @throw [OFInvalidArgumentException exception]; number = (OFNumber *)object; if (isFloat(self) || isFloat(number)) { double double1 = self.doubleValue; double double2 = number.doubleValue; if (double1 > double2) return OF_ORDERED_DESCENDING; if (double1 < double2) return OF_ORDERED_ASCENDING; return OF_ORDERED_SAME; } else if (isSigned(self) || isSigned(number)) { long long int1 = self.longLongValue; long long int2 = number.longLongValue; if (int1 > int2) return OF_ORDERED_DESCENDING; if (int1 < int2) return OF_ORDERED_ASCENDING; |
︙ | ︙ | |||
747 748 749 750 751 752 753 | return OF_ORDERED_SAME; } } - (uint32_t)hash { | < | | < | 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 | return OF_ORDERED_SAME; } } - (uint32_t)hash { uint32_t hash; OF_HASH_INIT(hash); if (isFloat(self)) { double d; if (isnan(self.doubleValue)) return 0; d = OF_BSWAP_DOUBLE_IF_BE(self.doubleValue); for (uint_fast8_t i = 0; i < sizeof(double); i++) OF_HASH_ADD(hash, ((char *)&d)[i]); } else if (isSigned(self) || isUnsigned(self)) { unsigned long long value = self.unsignedLongLongValue; while (value != 0) { OF_HASH_ADD(hash, value & 0xFF); value >>= 8; } } else |
︙ | ︙ | |||
790 791 792 793 794 795 796 | - (OFString *)description { return [self stringValue]; } - (OFString *)stringValue { | | | | | | | | | > | | | | | | 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 | - (OFString *)description { return [self stringValue]; } - (OFString *)stringValue { if (*self.objCType == 'B') return (self.boolValue ? @"true" : @"false"); if (isFloat(self)) return [OFString stringWithFormat: @"%g", self.doubleValue]; if (isSigned(self)) return [OFString stringWithFormat: @"%lld", self.longLongValue]; if (isUnsigned(self)) return [OFString stringWithFormat: @"%llu", self.unsignedLongLongValue]; @throw [OFInvalidFormatException exception]; } - (OFXMLElement *)XMLElementBySerializing { void *pool = objc_autoreleasePoolPush(); OFXMLElement *element; element = [OFXMLElement elementWithName: self.className namespace: OF_SERIALIZATION_NS stringValue: self.description]; if (*self.objCType == 'B') [element addAttributeWithName: @"type" stringValue: @"bool"]; else if (isFloat(self)) { [element addAttributeWithName: @"type" stringValue: @"float"]; element.stringValue = [OFString stringWithFormat: @"%016" PRIx64, OF_BSWAP64_IF_LE(OF_DOUBLE_TO_INT_RAW(OF_BSWAP_DOUBLE_IF_LE( self.doubleValue)))]; } else if (isSigned(self)) [element addAttributeWithName: @"type" stringValue: @"signed"]; else if (isUnsigned(self)) [element addAttributeWithName: @"type" stringValue: @"unsigned"]; else @throw [OFInvalidFormatException exception]; [element retain]; |
︙ | ︙ | |||
854 855 856 857 858 859 860 | } - (OFString *)of_JSONRepresentationWithOptions: (int)options depth: (size_t)depth { double doubleValue; | | | > | | | | | | | | 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 | } - (OFString *)of_JSONRepresentationWithOptions: (int)options depth: (size_t)depth { double doubleValue; if (*self.objCType == 'B') return (self.boolValue ? @"true" : @"false"); doubleValue = self.doubleValue; if (isinf(doubleValue)) { if (options & OF_JSON_REPRESENTATION_JSON5) { if (doubleValue > 0) return @"Infinity"; else return @"-Infinity"; } else @throw [OFInvalidArgumentException exception]; } return self.description; } - (OFData *)messagePackRepresentation { OFMutableData *data; const char *typeEncoding = self.objCType; if (*typeEncoding == 'B') { uint8_t type = (self.boolValue ? 0xC3 : 0xC2); data = [OFMutableData dataWithItems: &type count: 1]; } else if (*typeEncoding == 'f') { uint8_t type = 0xCA; float tmp = OF_BSWAP_FLOAT_IF_LE(self.floatValue); data = [OFMutableData dataWithItemSize: 1 capacity: 5]; [data addItem: &type]; [data addItems: &tmp count: sizeof(tmp)]; } else if (*typeEncoding == 'd') { uint8_t type = 0xCB; double tmp = OF_BSWAP_DOUBLE_IF_LE(self.doubleValue); data = [OFMutableData dataWithItemSize: 1 capacity: 9]; [data addItem: &type]; [data addItems: &tmp count: sizeof(tmp)]; } else if (isSigned(self)) { long long value = self.longLongValue; if (value >= -32 && value < 0) { uint8_t tmp = 0xE0 | ((uint8_t)(value - 32) & 0x1F); data = [OFMutableData dataWithItems: &tmp count: 1]; |
︙ | ︙ | |||
949 950 951 952 953 954 955 | capacity: 9]; [data addItem: &type]; [data addItems: &tmp count: sizeof(tmp)]; } else @throw [OFOutOfRangeException exception]; | | | 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 | capacity: 9]; [data addItem: &type]; [data addItems: &tmp count: sizeof(tmp)]; } else @throw [OFOutOfRangeException exception]; } else if (isUnsigned(self)) { unsigned long long value = self.unsignedLongLongValue; if (value <= 127) { uint8_t tmp = ((uint8_t)value & 0x7F); data = [OFMutableData dataWithItems: &tmp count: 1]; |
︙ | ︙ |