shamblett / cbor

A CBOR implementation for Dart
MIT License
36 stars 15 forks source link

Update Bitwise Complement Operator and Type Checks for Web Compatibility #58

Closed dangfan closed 6 months ago

dangfan commented 6 months ago

This pull request introduces changes to the handling of the bitwise complement operator and type checks to accommodate differences in behavior between native and web environments. The changes are detailed below:

  1. Modification of Bitwise Complement (~) Operation:

    • In web environments, the behavior of ~4 is not equivalent to -5 as it is in native environments. To address this discrepancy, the implementation of the ~ operator in _ArgInt has been updated.
    • Now, it checks if the code is running on the web (using kIsWeb) and, if so, applies -value - 1 instead of the native ~value. This change ensures consistent results across platforms.
  2. Type Checking for Integer Objects:

    • Due to the dual nature of integers on the web, where they can be represented as both int and double, the existing type check was inadequate.
    • A new utility function isWebDouble has been introduced to accurately determine if an object is a double type representing an integer on the web.
    • Consequently, the condition for encoding an object as CborFloat in CborValue has been updated to include a check using isWebDouble, ensuring correct type interpretation on the web.
  3. Implications for Encoding Float-like Integers and Unaffected Native Behavior:

    • Post this update, encoding a float-like integer on the web requires manual handling. This change is necessary to align with the modified type-checking logic and maintain accuracy in data representation.
    • It's important to note that these changes do not impact the native code, ensuring that existing functionality remains consistent for native applications.

Affected Files:

dangfan commented 6 months ago

After discussing with @Harry-Chen , I've changed the order to detect the type of an object. It works because "On the web, the underlying int type is like a subtype of double: it’s a double-precision value without a fractional part. In fact, a type check on the web of the form x is int returns true if x is a number (double) with a zero-valued fractional part." Also, using toSigned(32) works on web.

codecov-commenter commented 6 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (cd21a16) 70.97% compared to head (2e6f64b) 70.97%.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #58 +/- ## ======================================= Coverage 70.97% 70.97% ======================================= Files 23 23 Lines 1237 1237 ======================================= Hits 878 878 Misses 359 359 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

shamblett commented 6 months ago

Great addition thanks. Package re published at version 6.2.0.