From d324e2cfe22024d230cc2407d9874464d4d7172f Mon Sep 17 00:00:00 2001 From: Fan DANG Date: Fri, 19 Jan 2024 14:10:44 +0800 Subject: [PATCH 1/3] Fix int bit-wise operation issues and double type issues on web --- lib/src/utils/arg.dart | 4 +++- lib/src/utils/utils.dart | 6 ++++++ lib/src/value/value.dart | 5 +++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/src/utils/arg.dart b/lib/src/utils/arg.dart index 8bd19ad..67159c0 100644 --- a/lib/src/utils/arg.dart +++ b/lib/src/utils/arg.dart @@ -5,6 +5,8 @@ * Copyright : S.Hamblett */ +import 'utils.dart'; + /// The information encoded by additional Arg and the following bytes. abstract class Arg { const factory Arg.int(int arg) = _ArgInt; @@ -45,7 +47,7 @@ class _ArgInt implements Arg { final int value; @override - _ArgInt operator ~() => _ArgInt(~value); + _ArgInt operator ~() => _ArgInt(kIsWeb ? -value - 1 : ~value); @override final bool isIndefiniteLength = false; diff --git a/lib/src/utils/utils.dart b/lib/src/utils/utils.dart index 598d351..fbc4ed6 100644 --- a/lib/src/utils/utils.dart +++ b/lib/src/utils/utils.dart @@ -137,3 +137,9 @@ bool isExpectConversion(int tag) { return false; } + +const bool kIsWeb = identical(0, 0.0); + +bool isWebDouble(Object a) { + return (a as double).toInt() == a; +} diff --git a/lib/src/value/value.dart b/lib/src/value/value.dart index bf0766a..9cbd1da 100644 --- a/lib/src/value/value.dart +++ b/lib/src/value/value.dart @@ -8,6 +8,7 @@ import 'dart:typed_data'; import 'package:cbor/cbor.dart'; +import 'package:cbor/src/utils/utils.dart'; import 'package:meta/meta.dart'; import '../encoder/sink.dart'; @@ -61,8 +62,8 @@ abstract class CborValue { return CborNull(); } else if (object is CborValue) { return object; - } else if (object is double) { - return CborFloat(object); + } else if (!kIsWeb && object is double || kIsWeb && isWebDouble(object)) { + return CborFloat(object as double); } else if (object is int) { return CborSmallInt(object); } else if (object is BigInt) { From 00fdb8429c029072a3e0f5031ce6bb384b139c12 Mon Sep 17 00:00:00 2001 From: Fan DANG Date: Fri, 19 Jan 2024 15:58:06 +0800 Subject: [PATCH 2/3] fix type detection error --- lib/src/utils/utils.dart | 2 +- lib/src/value/value.dart | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/src/utils/utils.dart b/lib/src/utils/utils.dart index fbc4ed6..e20ca26 100644 --- a/lib/src/utils/utils.dart +++ b/lib/src/utils/utils.dart @@ -141,5 +141,5 @@ bool isExpectConversion(int tag) { const bool kIsWeb = identical(0, 0.0); bool isWebDouble(Object a) { - return (a as double).toInt() == a; + return a is double && a.toInt() == a; } diff --git a/lib/src/value/value.dart b/lib/src/value/value.dart index 9cbd1da..d5a4d21 100644 --- a/lib/src/value/value.dart +++ b/lib/src/value/value.dart @@ -62,8 +62,8 @@ abstract class CborValue { return CborNull(); } else if (object is CborValue) { return object; - } else if (!kIsWeb && object is double || kIsWeb && isWebDouble(object)) { - return CborFloat(object as double); + } else if (object is double && (!kIsWeb || kIsWeb && isWebDouble(object))) { + return CborFloat(object); } else if (object is int) { return CborSmallInt(object); } else if (object is BigInt) { From 2e6f64b5a297033d565d15365052ce701aced6b2 Mon Sep 17 00:00:00 2001 From: Fan DANG Date: Fri, 19 Jan 2024 22:02:12 +0800 Subject: [PATCH 3/3] A better way to be compatible with web --- lib/src/utils/arg.dart | 4 +--- lib/src/utils/utils.dart | 6 ------ lib/src/value/value.dart | 5 ++--- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/lib/src/utils/arg.dart b/lib/src/utils/arg.dart index 67159c0..7e1b303 100644 --- a/lib/src/utils/arg.dart +++ b/lib/src/utils/arg.dart @@ -5,8 +5,6 @@ * Copyright : S.Hamblett */ -import 'utils.dart'; - /// The information encoded by additional Arg and the following bytes. abstract class Arg { const factory Arg.int(int arg) = _ArgInt; @@ -47,7 +45,7 @@ class _ArgInt implements Arg { final int value; @override - _ArgInt operator ~() => _ArgInt(kIsWeb ? -value - 1 : ~value); + _ArgInt operator ~() => _ArgInt((~value).toSigned(32)); @override final bool isIndefiniteLength = false; diff --git a/lib/src/utils/utils.dart b/lib/src/utils/utils.dart index e20ca26..598d351 100644 --- a/lib/src/utils/utils.dart +++ b/lib/src/utils/utils.dart @@ -137,9 +137,3 @@ bool isExpectConversion(int tag) { return false; } - -const bool kIsWeb = identical(0, 0.0); - -bool isWebDouble(Object a) { - return a is double && a.toInt() == a; -} diff --git a/lib/src/value/value.dart b/lib/src/value/value.dart index d5a4d21..5d68af5 100644 --- a/lib/src/value/value.dart +++ b/lib/src/value/value.dart @@ -8,7 +8,6 @@ import 'dart:typed_data'; import 'package:cbor/cbor.dart'; -import 'package:cbor/src/utils/utils.dart'; import 'package:meta/meta.dart'; import '../encoder/sink.dart'; @@ -62,12 +61,12 @@ abstract class CborValue { return CborNull(); } else if (object is CborValue) { return object; - } else if (object is double && (!kIsWeb || kIsWeb && isWebDouble(object))) { - return CborFloat(object); } else if (object is int) { return CborSmallInt(object); } else if (object is BigInt) { return CborInt(object); + } else if (object is double) { + return CborFloat(object); } else if (object is bool) { return CborBool(object); } else if (object is Uint8List) {