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

Make casts to int safe in dart2wasm. #1416

Merged
merged 6 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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
7 changes: 5 additions & 2 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
## 6.8.0-wip
## 6.8.0

- 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
2 changes: 1 addition & 1 deletion json_serializable/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_serializable
version: 6.8.0-wip
version: 6.8.0
description: >-
Automatically generate code for converting to and from JSON by annotating
Dart classes.
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
Loading