diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a94781d..83001afc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ that can be found in the LICENSE file. --> # Changelog +## 1.0.0-dev.33 + +- Use an explicit serialization method rather than `toJson`. +- Revert #88. +- Revert part of #87. + ## 1.0.0-dev.32 - Make parsable big number string as covariant of Fixed classes. diff --git a/packages/agent_dart/pubspec.yaml b/packages/agent_dart/pubspec.yaml index 7058f19e..6c7ff0f7 100644 --- a/packages/agent_dart/pubspec.yaml +++ b/packages/agent_dart/pubspec.yaml @@ -1,5 +1,5 @@ name: agent_dart -version: 1.0.0-dev.32 +version: 1.0.0-dev.33 description: | An agent library built for Internet Computer, diff --git a/packages/agent_dart_base/lib/agent/utils/leb128.dart b/packages/agent_dart_base/lib/agent/utils/leb128.dart index b0183146..5f86dc85 100644 --- a/packages/agent_dart_base/lib/agent/utils/leb128.dart +++ b/packages/agent_dart_base/lib/agent/utils/leb128.dart @@ -22,12 +22,7 @@ List safeRead(BufferPipe pipe, int ref) { /// nearest integer. /// @param value The number to encode. Uint8List lebEncode(dynamic value) { - BigInt bn = switch (value) { - BigInt() => value, - num() => BigInt.from(value), - String() => BigInt.parse(value), - _ => throw ArgumentError('Invalid big number: $value', 'lebEncode'), - }; + BigInt bn = value is BigInt ? value : BigInt.from(value); if (bn < BigInt.zero) { throw StateError('Cannot leb-encode negative values.'); } @@ -63,13 +58,9 @@ BigInt lebDecode(BufferPipe pipe) { /// Encode a number (or bigint) into a Buffer, with support for negative numbers. /// The number will be floored to the nearest integer. /// @param value The number to encode. -Uint8List slebEncode(dynamic value) { - BigInt bn = switch (value) { - BigInt() => value, - num() => BigInt.from(value), - String() => BigInt.parse(value), - _ => throw ArgumentError('Invalid big number: $value', 'slebEncode'), - }; +Uint8List slebEncode(Comparable value) { + BigInt bn = value is BigInt ? value : BigInt.from(value as num); + final isNeg = bn < BigInt.zero; if (isNeg) { bn = -bn - BigInt.one; diff --git a/packages/agent_dart_base/lib/candid/idl.dart b/packages/agent_dart_base/lib/candid/idl.dart index 779ce55e..5238ee20 100644 --- a/packages/agent_dart_base/lib/candid/idl.dart +++ b/packages/agent_dart_base/lib/candid/idl.dart @@ -55,7 +55,12 @@ Uint8List? tryToJson(CType type, dynamic value) { // obj may be a map, must be ignore. value is! Map) { try { - return type.encodeValue(value.toJson()); + try { + value = value.toIDLSerializable(); + } on NoSuchMethodError { + value = value.toJson(); + } + return type.encodeValue(value); } catch (e) { return null; } @@ -528,9 +533,7 @@ class NatClass extends PrimitiveType { @override bool covariant(x) { - return (x is BigInt && x >= BigInt.zero) || - (x is int && x >= 0) || - (x is String && BigInt.parse(x) >= BigInt.zero); + return (x is BigInt && x >= BigInt.zero) || (x is int && x >= 0); } @override @@ -636,9 +639,6 @@ class FixedIntClass extends PrimitiveType { } else if (x is int) { final v = BigInt.from(x); return v >= min && v <= max; - } else if (x is String && BigInt.tryParse(x) != null) { - final v = BigInt.parse(x); - return v >= min && v <= max; } else { return false; } @@ -699,11 +699,6 @@ class FixedNatClass extends PrimitiveType { } else if (x is int && x >= 0) { final v = BigInt.from(x); return v < max; - } else if (x is String && - BigInt.tryParse(x) != null && - BigInt.parse(x) >= BigInt.zero) { - final v = BigInt.parse(x); - return v < max; } else { return false; } @@ -904,7 +899,11 @@ class RecordClass extends ConstructType { bool covariant(dynamic x) { if (x is! Map) { try { - x = x.toJson(); + try { + x = x.toIDLSerializable(); + } on NoSuchMethodError { + x = x.toJson(); + } } catch (e) { return false; } diff --git a/packages/agent_dart_base/pubspec.yaml b/packages/agent_dart_base/pubspec.yaml index 7efc1494..fc11eaca 100644 --- a/packages/agent_dart_base/pubspec.yaml +++ b/packages/agent_dart_base/pubspec.yaml @@ -1,5 +1,5 @@ name: agent_dart_base -version: 1.0.0-dev.32 +version: 1.0.0-dev.33 description: The Dart plugin that bridges Rust implementation for agent_dart. repository: https://github.com/AstroxNetwork/agent_dart diff --git a/packages/agent_dart_base/test/agent/utils/leb128.dart b/packages/agent_dart_base/test/agent/utils/leb128.dart index 831e949a..072a97aa 100644 --- a/packages/agent_dart_base/test/agent/utils/leb128.dart +++ b/packages/agent_dart_base/test/agent/utils/leb128.dart @@ -24,7 +24,6 @@ void leb128Test() { lebEncode(BigInt.from(60000000000000000)).toHex(), '808098f4e9b5ca6a', ); - expect(lebEncode('1').toHex(), '01'); expect(lebDecode(BufferPipe(Uint8List.fromList([0]))), BigInt.zero); expect(lebDecode(BufferPipe(Uint8List.fromList([1]))), BigInt.one); @@ -60,7 +59,6 @@ void leb128Test() { slebEncode(BigInt.parse('60000000000000000')).toHex(), '808098f4e9b5caea00', ); - expect(slebEncode('1').toHex(), '01'); expect( slebDecode(BufferPipe(Uint8List.fromList([0x7f]))), diff --git a/packages/agent_dart_ffi/pubspec.yaml b/packages/agent_dart_ffi/pubspec.yaml index 5401f0af..af9f0b01 100644 --- a/packages/agent_dart_ffi/pubspec.yaml +++ b/packages/agent_dart_ffi/pubspec.yaml @@ -1,5 +1,5 @@ name: agent_dart_ffi -version: 1.0.0-dev.32 +version: 1.0.0-dev.33 description: The FFI plugin that bridges Rust implementation for agent_dart. repository: https://github.com/AstroxNetwork/agent_dart