Support enum keys in maps in messages.

Make enum classes implement hashCode, rather than use the identity hash code, so that they can be portably serialized.

BUG=dart:21675
R=asiva@google.com, lrn@google.com

Review URL: https://codereview.chromium.org//752123003

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@44278 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
koda@google.com 2015-03-06 02:57:34 +00:00
parent b9baf4be94
commit d73d6de73d
3 changed files with 35 additions and 0 deletions

View file

@ -24,6 +24,7 @@ class _EnumHelper {
// below can access it because it uses the same name suffix.
static const List<String> _enum_names = null;
String toString() => _enum_names[index];
int get hashCode => _enum_names[index].hashCode;
}

View file

@ -4649,6 +4649,13 @@ void Parser::ParseEnumDefinition(const Class& cls) {
to_string_func = to_string_func.Clone(cls);
enum_members.AddFunction(to_string_func);
// Clone the hashCode getter function from the helper class.
Function& hash_code_func = Function::Handle(I,
helper_class.LookupDynamicFunctionAllowPrivate(Symbols::hashCode()));
ASSERT(!hash_code_func.IsNull());
hash_code_func = hash_code_func.Clone(cls);
enum_members.AddFunction(hash_code_func);
cls.AddFields(enum_members.fields());
const Array& functions =
Array::Handle(Z, Array::MakeArray(enum_members.functions()));

View file

@ -0,0 +1,27 @@
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// SharedOptions=--enable-enum
import 'package:expect/expect.dart';
import "dart:isolate";
enum Foo { BAR, BAZ }
main() {
var p;
p = new RawReceivePort((map) {
Expect.equals(1, map.keys.length);
Expect.equals(42, map.values.first);
var key = map.keys.first;
Expect.equals(42, map[key]);
p.close();
});
Isolate.spawn(sendIt, p.sendPort);
}
void sendIt(port) {
var map = { Foo.BAR: 42 };
port.send(map);
}