Skip to content

Commit fd5e968

Browse files
authored
🐛 Fix serializations when handling numbers (#89)
1 parent 69cd117 commit fd5e968

File tree

7 files changed

+25
-31
lines changed

7 files changed

+25
-31
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ that can be found in the LICENSE file. -->
44

55
# Changelog
66

7+
## 1.0.0-dev.33
8+
9+
- Use an explicit serialization method rather than `toJson`.
10+
- Revert #88.
11+
- Revert part of #87.
12+
713
## 1.0.0-dev.32
814

915
- Make parsable big number string as covariant of Fixed classes.

packages/agent_dart/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: agent_dart
2-
version: 1.0.0-dev.32
2+
version: 1.0.0-dev.33
33

44
description: |
55
An agent library built for Internet Computer,

packages/agent_dart_base/lib/agent/utils/leb128.dart

+4-13
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ List<T> safeRead<T>(BufferPipe<T> pipe, int ref) {
2222
/// nearest integer.
2323
/// @param value The number to encode.
2424
Uint8List lebEncode(dynamic value) {
25-
BigInt bn = switch (value) {
26-
BigInt() => value,
27-
num() => BigInt.from(value),
28-
String() => BigInt.parse(value),
29-
_ => throw ArgumentError('Invalid big number: $value', 'lebEncode'),
30-
};
25+
BigInt bn = value is BigInt ? value : BigInt.from(value);
3126
if (bn < BigInt.zero) {
3227
throw StateError('Cannot leb-encode negative values.');
3328
}
@@ -63,13 +58,9 @@ BigInt lebDecode<T>(BufferPipe<T> pipe) {
6358
/// Encode a number (or bigint) into a Buffer, with support for negative numbers.
6459
/// The number will be floored to the nearest integer.
6560
/// @param value The number to encode.
66-
Uint8List slebEncode(dynamic value) {
67-
BigInt bn = switch (value) {
68-
BigInt() => value,
69-
num() => BigInt.from(value),
70-
String() => BigInt.parse(value),
71-
_ => throw ArgumentError('Invalid big number: $value', 'slebEncode'),
72-
};
61+
Uint8List slebEncode(Comparable value) {
62+
BigInt bn = value is BigInt ? value : BigInt.from(value as num);
63+
7364
final isNeg = bn < BigInt.zero;
7465
if (isNeg) {
7566
bn = -bn - BigInt.one;

packages/agent_dart_base/lib/candid/idl.dart

+12-13
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ Uint8List? tryToJson(CType type, dynamic value) {
5555
// obj may be a map, must be ignore.
5656
value is! Map) {
5757
try {
58-
return type.encodeValue(value.toJson());
58+
try {
59+
value = value.toIDLSerializable();
60+
} on NoSuchMethodError {
61+
value = value.toJson();
62+
}
63+
return type.encodeValue(value);
5964
} catch (e) {
6065
return null;
6166
}
@@ -528,9 +533,7 @@ class NatClass extends PrimitiveType {
528533

529534
@override
530535
bool covariant(x) {
531-
return (x is BigInt && x >= BigInt.zero) ||
532-
(x is int && x >= 0) ||
533-
(x is String && BigInt.parse(x) >= BigInt.zero);
536+
return (x is BigInt && x >= BigInt.zero) || (x is int && x >= 0);
534537
}
535538

536539
@override
@@ -636,9 +639,6 @@ class FixedIntClass extends PrimitiveType {
636639
} else if (x is int) {
637640
final v = BigInt.from(x);
638641
return v >= min && v <= max;
639-
} else if (x is String && BigInt.tryParse(x) != null) {
640-
final v = BigInt.parse(x);
641-
return v >= min && v <= max;
642642
} else {
643643
return false;
644644
}
@@ -699,11 +699,6 @@ class FixedNatClass extends PrimitiveType<dynamic> {
699699
} else if (x is int && x >= 0) {
700700
final v = BigInt.from(x);
701701
return v < max;
702-
} else if (x is String &&
703-
BigInt.tryParse(x) != null &&
704-
BigInt.parse(x) >= BigInt.zero) {
705-
final v = BigInt.parse(x);
706-
return v < max;
707702
} else {
708703
return false;
709704
}
@@ -904,7 +899,11 @@ class RecordClass extends ConstructType<Map> {
904899
bool covariant(dynamic x) {
905900
if (x is! Map) {
906901
try {
907-
x = x.toJson();
902+
try {
903+
x = x.toIDLSerializable();
904+
} on NoSuchMethodError {
905+
x = x.toJson();
906+
}
908907
} catch (e) {
909908
return false;
910909
}

packages/agent_dart_base/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: agent_dart_base
2-
version: 1.0.0-dev.32
2+
version: 1.0.0-dev.33
33

44
description: The Dart plugin that bridges Rust implementation for agent_dart.
55
repository: https://github.com/AstroxNetwork/agent_dart

packages/agent_dart_base/test/agent/utils/leb128.dart

-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ void leb128Test() {
2424
lebEncode(BigInt.from(60000000000000000)).toHex(),
2525
'808098f4e9b5ca6a',
2626
);
27-
expect(lebEncode('1').toHex(), '01');
2827

2928
expect(lebDecode(BufferPipe<int>(Uint8List.fromList([0]))), BigInt.zero);
3029
expect(lebDecode(BufferPipe<int>(Uint8List.fromList([1]))), BigInt.one);
@@ -60,7 +59,6 @@ void leb128Test() {
6059
slebEncode(BigInt.parse('60000000000000000')).toHex(),
6160
'808098f4e9b5caea00',
6261
);
63-
expect(slebEncode('1').toHex(), '01');
6462

6563
expect(
6664
slebDecode(BufferPipe<int>(Uint8List.fromList([0x7f]))),

packages/agent_dart_ffi/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: agent_dart_ffi
2-
version: 1.0.0-dev.32
2+
version: 1.0.0-dev.33
33

44
description: The FFI plugin that bridges Rust implementation for agent_dart.
55
repository: https://github.com/AstroxNetwork/agent_dart

0 commit comments

Comments
 (0)