From 3266e73128bd7512111551ebc3ef233953b1045d Mon Sep 17 00:00:00 2001 From: Johnni Winther Date: Wed, 17 Aug 2016 10:17:38 +0200 Subject: [PATCH] Support encoding of NaN, infinity and negative infinity. BUG= R=sigmund@google.com Review URL: https://codereview.chromium.org/2250643002 . --- .../src/serialization/json_serializer.dart | 23 ++++++++++++++++--- .../lib/src/serialization/serialization.dart | 10 +++++++- .../dart2js/serialization/test_data.dart | 8 +++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/pkg/compiler/lib/src/serialization/json_serializer.dart b/pkg/compiler/lib/src/serialization/json_serializer.dart index 52e9f9604d6..ba995bde28b 100644 --- a/pkg/compiler/lib/src/serialization/json_serializer.dart +++ b/pkg/compiler/lib/src/serialization/json_serializer.dart @@ -15,8 +15,13 @@ class JsonSerializationEncoder implements SerializationEncoder { const JsonSerializationEncoder(); String encode(ObjectValue objectValue) { - return new JsonEncoder.withIndent(' ') - .convert(const JsonValueEncoder().convert(objectValue)); + try { + return new JsonEncoder.withIndent(' ') + .convert(const JsonValueEncoder().convert(objectValue)); + } on JsonUnsupportedObjectError catch (e) { + throw 'Error encoding `${e.unsupportedObject}` ' + '(${e.unsupportedObject.runtimeType})'; + } } } @@ -48,7 +53,19 @@ class JsonValueEncoder implements ValueVisitor { visitConstant(ConstantValue value, arg) => visit(value.id); @override - double visitDouble(DoubleValue value, arg) => value.value; + visitDouble(DoubleValue value, arg) { + double d = value.value; + if (d.isNaN) { + return 'NaN'; + } else if (d.isInfinite) { + if (d.isNegative) { + return '-Infinity'; + } else { + return 'Infinity'; + } + } + return d; + } @override visitElement(ElementValue value, arg) => visit(value.id); diff --git a/pkg/compiler/lib/src/serialization/serialization.dart b/pkg/compiler/lib/src/serialization/serialization.dart index b1418f64907..27baa606aac 100644 --- a/pkg/compiler/lib/src/serialization/serialization.dart +++ b/pkg/compiler/lib/src/serialization/serialization.dart @@ -540,13 +540,21 @@ abstract class AbstractDecoder { /// If no value is associated with [key], then if [isOptional] is `true`, /// [defaultValue] is returned, otherwise an exception is thrown. double getDouble(K key, {bool isOptional: false, double defaultValue}) { - double value = _map[_getKeyValue(key)]; + var value = _map[_getKeyValue(key)]; if (value == null) { if (isOptional || defaultValue != null) { return defaultValue; } throw new StateError("double value '$key' not found in $_map."); } + // Support alternative encoding of NaN and +/- infinity for JSON. + if (value == 'NaN') { + return double.NAN; + } else if (value == '-Infinity') { + return double.NEGATIVE_INFINITY; + } else if (value == 'Infinity') { + return double.INFINITY; + } return value; } diff --git a/tests/compiler/dart2js/serialization/test_data.dart b/tests/compiler/dart2js/serialization/test_data.dart index 73d088fd0d5..065bf660d0d 100644 --- a/tests/compiler/dart2js/serialization/test_data.dart +++ b/tests/compiler/dart2js/serialization/test_data.dart @@ -683,6 +683,14 @@ main() => y; }, preserializedSourceFiles: const { 'a.dart': ''' var x, y = 2; +''', + }), + + const Test('Double values', const {}, + preserializedSourceFiles: const { + 'main.dart': ''' +const a = 1e+400; +main() => a; ''', }), ];