Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jan 20, 2024

Conversation

dangfan
Copy link
Contributor

@dangfan dangfan commented Jan 19, 2024

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:

  • lib/src/utils/arg.dart
  • lib/src/utils/utils.dart
  • lib/src/value/value.dart

@dangfan
Copy link
Contributor Author

dangfan commented Jan 19, 2024

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
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

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

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

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@shamblett shamblett merged commit 1e65430 into shamblett:master Jan 20, 2024
3 checks passed
@shamblett
Copy link
Owner

Great addition thanks. Package re published at version 6.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants