Skip to content

Commit

Permalink
Make casts to int safe in dart2wasm. (#1416)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Moore <[email protected]>
  • Loading branch information
eyebrowsoffire and kevmoo authored Apr 24, 2024
1 parent 1694d6f commit dbe976a
Show file tree
Hide file tree
Showing 51 changed files with 551 additions and 423 deletions.
2 changes: 1 addition & 1 deletion _test_yaml/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dev_dependencies:
build_verify: ^3.0.0
checked_yaml: any
dart_flutter_team_lints: ^2.0.0
json_annotation: ^4.7.0
json_annotation: ^4.8.1
json_serializable: any
path: ^1.8.2
test: ^1.6.0
Expand Down
4 changes: 2 additions & 2 deletions _test_yaml/test/src/build_config.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions example/lib/example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions example/lib/generic_response_class_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions example/lib/json_converter_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions example/lib/tuple_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion json_annotation/lib/src/json_serializable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class JsonSerializable {
/// T Function(Object json) fromJsonT,
/// ) {
/// return Response<T>()
/// ..status = json['status'] as int
/// ..status = (json['status'] as num).toInt()
/// ..value = fromJsonT(json['value']);
/// }
///
Expand Down
5 changes: 4 additions & 1 deletion json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
## 6.8.0-wip

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

## 6.7.1

Expand Down
35 changes: 20 additions & 15 deletions json_serializable/lib/src/lambda_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ class LambdaResult {
final String lambda;
final DartType? asContent;

String get _asContent => asContent == null ? '' : _asStatement(asContent!);

String get _fullExpression => '$expression$_asContent';
String get _fullExpression =>
asContent != null ? _cast(expression, asContent!) : expression;

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

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

String _asStatement(DartType type) {
if (type.isLikeDynamic) {
return '';
String _cast(String expression, DartType targetType) {
if (targetType.isLikeDynamic) {
return expression;
}

final nullableSuffix = type.isNullableType ? '?' : '';
final nullableSuffix = targetType.isNullableType ? '?' : '';

if (coreIterableTypeChecker.isAssignableFromType(type)) {
final itemType = coreIterableGenericType(type);
if (coreIterableTypeChecker.isAssignableFromType(targetType)) {
final itemType = coreIterableGenericType(targetType);
if (itemType.isLikeDynamic) {
return ' as List$nullableSuffix';
return '$expression as List$nullableSuffix';
}
}

if (coreMapTypeChecker.isAssignableFromType(type)) {
final args = type.typeArgumentsOf(coreMapTypeChecker)!;
if (coreMapTypeChecker.isAssignableFromType(targetType)) {
final args = targetType.typeArgumentsOf(coreMapTypeChecker)!;
assert(args.length == 2);

if (args.every((e) => e.isLikeDynamic)) {
return ' as Map$nullableSuffix';
return '$expression as Map$nullableSuffix';
}
}

final typeCode = typeToCode(type);
return ' as $typeCode';
final defaultDecodeValue = defaultDecodeLogic(targetType, expression);

if (defaultDecodeValue != null) {
return defaultDecodeValue;
}

final typeCode = typeToCode(targetType);
return '$expression as $typeCode';
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DurationHelper extends TypeHelper {

return DefaultContainer(
expression,
'Duration(microseconds: $expression as int)',
'Duration(microseconds: ($expression as num).toInt())',
);
}
}
Expand Down
22 changes: 3 additions & 19 deletions json_serializable/lib/src/type_helpers/value_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/element/type.dart';
import 'package:source_helper/source_helper.dart';

import '../shared_checkers.dart';
import '../type_helper.dart';
Expand Down Expand Up @@ -35,22 +34,7 @@ class ValueHelper extends TypeHelper {
String expression,
TypeHelperContext context,
bool defaultProvided,
) {
if (targetType.isDartCoreObject && !targetType.isNullableType) {
final question = defaultProvided ? '?' : '';
return '$expression as Object$question';
} else if (targetType.isDartCoreObject || targetType is DynamicType) {
// just return it as-is. We'll hope it's safe.
return expression;
} else if (targetType.isDartCoreDouble) {
final targetTypeNullable = defaultProvided || targetType.isNullableType;
final question = targetTypeNullable ? '?' : '';
return '($expression as num$question)$question.toDouble()';
} else if (simpleJsonTypeChecker.isAssignableFromType(targetType)) {
final typeCode = typeToCode(targetType, forceNullable: defaultProvided);
return '$expression as $typeCode';
}

return null;
}
) =>
defaultDecodeLogic(targetType, expression,
defaultProvided: defaultProvided);
}
28 changes: 28 additions & 0 deletions json_serializable/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:json_annotation/json_annotation.dart';
import 'package:source_gen/source_gen.dart';
import 'package:source_helper/source_helper.dart';

import 'shared_checkers.dart';
import 'type_helpers/config_types.dart';

const _jsonKeyChecker = TypeChecker.fromRuntime(JsonKey);
Expand Down Expand Up @@ -220,6 +221,33 @@ String typeToCode(
throw UnimplementedError('(${type.runtimeType}) $type');
}

String? defaultDecodeLogic(
DartType targetType,
String expression, {
bool defaultProvided = false,
}) {
if (targetType.isDartCoreObject && !targetType.isNullableType) {
final question = defaultProvided ? '?' : '';
return '$expression as Object$question';
} else if (targetType.isDartCoreObject || targetType is DynamicType) {
// just return it as-is. We'll hope it's safe.
return expression;
} else if (targetType.isDartCoreDouble) {
final targetTypeNullable = defaultProvided || targetType.isNullableType;
final question = targetTypeNullable ? '?' : '';
return '($expression as num$question)$question.toDouble()';
} else if (targetType.isDartCoreInt) {
final targetTypeNullable = defaultProvided || targetType.isNullableType;
final question = targetTypeNullable ? '?' : '';
return '($expression as num$question)$question.toInt()';
} else if (simpleJsonTypeChecker.isAssignableFromType(targetType)) {
final typeCode = typeToCode(targetType, forceNullable: defaultProvided);
return '$expression as $typeCode';
}

return null;
}

extension ExecutableElementExtension on ExecutableElement {
/// Returns the name of `this` qualified with the class name if it's a
/// [MethodElement].
Expand Down
10 changes: 5 additions & 5 deletions json_serializable/test/default_value/default_value.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dbe976a

Please sign in to comment.