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

🐛 Fix serializations when handling numbers #89

Merged
merged 4 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion packages/agent_dart/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
17 changes: 4 additions & 13 deletions packages/agent_dart_base/lib/agent/utils/leb128.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ List<T> safeRead<T>(BufferPipe<T> 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.');
}
Expand Down Expand Up @@ -63,13 +58,9 @@ BigInt lebDecode<T>(BufferPipe<T> 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;
Expand Down
25 changes: 12 additions & 13 deletions packages/agent_dart_base/lib/candid/idl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -699,11 +699,6 @@ class FixedNatClass extends PrimitiveType<dynamic> {
} 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;
}
Expand Down Expand Up @@ -904,7 +899,11 @@ class RecordClass extends ConstructType<Map> {
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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/agent_dart_base/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions packages/agent_dart_base/test/agent/utils/leb128.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ void leb128Test() {
lebEncode(BigInt.from(60000000000000000)).toHex(),
'808098f4e9b5ca6a',
);
expect(lebEncode('1').toHex(), '01');

expect(lebDecode(BufferPipe<int>(Uint8List.fromList([0]))), BigInt.zero);
expect(lebDecode(BufferPipe<int>(Uint8List.fromList([1]))), BigInt.one);
Expand Down Expand Up @@ -60,7 +59,6 @@ void leb128Test() {
slebEncode(BigInt.parse('60000000000000000')).toHex(),
'808098f4e9b5caea00',
);
expect(slebEncode('1').toHex(), '01');

expect(
slebDecode(BufferPipe<int>(Uint8List.fromList([0x7f]))),
Expand Down
2 changes: 1 addition & 1 deletion packages/agent_dart_ffi/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading