Skip to content

Commit dbe976a

Browse files
Make casts to int safe in dart2wasm. (#1416)
Co-authored-by: Kevin Moore <[email protected]>
1 parent 1694d6f commit dbe976a

File tree

51 files changed

+551
-423
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+551
-423
lines changed

_test_yaml/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dev_dependencies:
1111
build_verify: ^3.0.0
1212
checked_yaml: any
1313
dart_flutter_team_lints: ^2.0.0
14-
json_annotation: ^4.7.0
14+
json_annotation: ^4.8.1
1515
json_serializable: any
1616
path: ^1.8.2
1717
test: ^1.6.0

_test_yaml/test/src/build_config.g.dart

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/example.g.dart

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/generic_response_class_example.g.dart

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/json_converter_example.g.dart

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/tuple_example.g.dart

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_annotation/lib/src/json_serializable.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class JsonSerializable {
181181
/// T Function(Object json) fromJsonT,
182182
/// ) {
183183
/// return Response<T>()
184-
/// ..status = json['status'] as int
184+
/// ..status = (json['status'] as num).toInt()
185185
/// ..value = fromJsonT(json['value']);
186186
/// }
187187
///

json_serializable/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
## 6.8.0-wip
22

33
- Add type arguments to `Map` literals used for `Record` serialization.
4-
- Added support for generating `ExampleJsonKeys`, exposing a secured way to access the json keys from the properties.
4+
- Added support for generating `ExampleJsonKeys`, exposing a secured way to
5+
access the json keys from the properties.
56
([#1164](https://github.com/google/json_serializable.dart/pull/1164))
7+
- Handle decoding an `int` value from a `double` literal.
8+
This now matches the behavior of `double` values being encoded as `int`.
69

710
## 6.7.1
811

json_serializable/lib/src/lambda_result.dart

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ class LambdaResult {
2020
final String lambda;
2121
final DartType? asContent;
2222

23-
String get _asContent => asContent == null ? '' : _asStatement(asContent!);
24-
25-
String get _fullExpression => '$expression$_asContent';
23+
String get _fullExpression =>
24+
asContent != null ? _cast(expression, asContent!) : expression;
2625

2726
LambdaResult(this.expression, this.lambda, {this.asContent});
2827

@@ -35,29 +34,35 @@ class LambdaResult {
3534
: '($closureArg) => $subField';
3635
}
3736

38-
String _asStatement(DartType type) {
39-
if (type.isLikeDynamic) {
40-
return '';
37+
String _cast(String expression, DartType targetType) {
38+
if (targetType.isLikeDynamic) {
39+
return expression;
4140
}
4241

43-
final nullableSuffix = type.isNullableType ? '?' : '';
42+
final nullableSuffix = targetType.isNullableType ? '?' : '';
4443

45-
if (coreIterableTypeChecker.isAssignableFromType(type)) {
46-
final itemType = coreIterableGenericType(type);
44+
if (coreIterableTypeChecker.isAssignableFromType(targetType)) {
45+
final itemType = coreIterableGenericType(targetType);
4746
if (itemType.isLikeDynamic) {
48-
return ' as List$nullableSuffix';
47+
return '$expression as List$nullableSuffix';
4948
}
5049
}
5150

52-
if (coreMapTypeChecker.isAssignableFromType(type)) {
53-
final args = type.typeArgumentsOf(coreMapTypeChecker)!;
51+
if (coreMapTypeChecker.isAssignableFromType(targetType)) {
52+
final args = targetType.typeArgumentsOf(coreMapTypeChecker)!;
5453
assert(args.length == 2);
5554

5655
if (args.every((e) => e.isLikeDynamic)) {
57-
return ' as Map$nullableSuffix';
56+
return '$expression as Map$nullableSuffix';
5857
}
5958
}
6059

61-
final typeCode = typeToCode(type);
62-
return ' as $typeCode';
60+
final defaultDecodeValue = defaultDecodeLogic(targetType, expression);
61+
62+
if (defaultDecodeValue != null) {
63+
return defaultDecodeValue;
64+
}
65+
66+
final typeCode = typeToCode(targetType);
67+
return '$expression as $typeCode';
6368
}

json_serializable/lib/src/type_helpers/duration_helper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class DurationHelper extends TypeHelper {
4646

4747
return DefaultContainer(
4848
expression,
49-
'Duration(microseconds: $expression as int)',
49+
'Duration(microseconds: ($expression as num).toInt())',
5050
);
5151
}
5252
}

0 commit comments

Comments
 (0)