Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: pkg/serialization/lib/src/serialization_rule.dart

Issue 13982012: Update pkg/serialization to use Symbols and synchronous mirrors (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of serialization; 5 part of serialization;
6 6
7 // TODO(alanknight): We should have an example and tests for subclassing 7 // TODO(alanknight): We should have an example and tests for subclassing
8 // serialization rule rather than using the hard-coded ClosureToMap rule. And 8 // serialization rule rather than using the hard-coded ClosureToMap rule. And
9 // possibly an abstract superclass that's designed to be subclassed that way. 9 // possibly an abstract superclass that's designed to be subclassed that way.
10 /** 10 /**
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 /** 385 /**
386 * This rule handles the special case of Mirrors. It stores the mirror by its 386 * This rule handles the special case of Mirrors. It stores the mirror by its
387 * qualifiedName and attempts to look it up in both the namedObjects 387 * qualifiedName and attempts to look it up in both the namedObjects
388 * collection, or if it's not found there, by looking it up in the mirror 388 * collection, or if it's not found there, by looking it up in the mirror
389 * system. When reading, the user is responsible for supplying the appropriate 389 * system. When reading, the user is responsible for supplying the appropriate
390 * values in [namedObjects] or in the [externals] paramter to 390 * values in [namedObjects] or in the [externals] paramter to
391 * [Serialization.read]. 391 * [Serialization.read].
392 */ 392 */
393 class MirrorRule extends NamedObjectRule { 393 class MirrorRule extends NamedObjectRule {
394 bool appliesTo(object, Writer writer) => object is DeclarationMirror; 394 bool appliesTo(object, Writer writer) => object is DeclarationMirror;
395 nameFor(DeclarationMirror object, Writer writer) => object.qualifiedName; 395 nameFor(DeclarationMirror object, Writer writer) =>
396 MirrorSystem.getName(object.qualifiedName);
396 397
397 inflateEssential(state, Reader r) { 398 inflateEssential(state, Reader r) {
398 var qualifiedName = r.resolveReference(state.first); 399 var qualifiedName = r.resolveReference(state.first);
399 var lookupFull = r.objectNamed(qualifiedName, (x) => null); 400 var lookupFull = r.objectNamed(qualifiedName, (x) => null);
400 if (lookupFull != null) return lookupFull; 401 if (lookupFull != null) return lookupFull;
401 var separatorIndex = qualifiedName.lastIndexOf("."); 402 var separatorIndex = qualifiedName.lastIndexOf(".");
402 var lib = qualifiedName.substring(0, separatorIndex); 403 var lib = new Symbol(qualifiedName.substring(0, separatorIndex));
403 var type = qualifiedName.substring(separatorIndex + 1); 404 var type = qualifiedName.substring(separatorIndex + 1);
404 var lookup = r.objectNamed(type, (x) => null); 405 var lookup = r.objectNamed(type, (x) => null);
405 if (lookup != null) return lookup; 406 if (lookup != null) return lookup;
406 var libMirror = currentMirrorSystem().libraries[lib]; 407 var libMirror = currentMirrorSystem().libraries[lib];
407 return libMirror.classes[type]; 408 return libMirror.classes[new Symbol(type)];
408 } 409 }
409 } 410 }
410 411
411 /** 412 /**
412 * This provides an abstract superclass for writing your own rules specific to 413 * This provides an abstract superclass for writing your own rules specific to
413 * a class. It makes some assumptions about behaviour, and so can have a 414 * a class. It makes some assumptions about behaviour, and so can have a
414 * simpler set of methods that need to be implemented in order to subclass it. 415 * simpler set of methods that need to be implemented in order to subclass it.
415 * 416 *
416 */ 417 */
417 abstract class CustomRule extends SerializationRule { 418 abstract class CustomRule extends SerializationRule {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 void inflateNonEssential(state, object, Reader r) { 463 void inflateNonEssential(state, object, Reader r) {
463 setState(object, _lazy(state, r)); 464 setState(object, _lazy(state, r));
464 } 465 }
465 466
466 // We don't want to have to make the end user tell us how long the list is 467 // We don't want to have to make the end user tell us how long the list is
467 // separately, so write it out for each object, even though they're all 468 // separately, so write it out for each object, even though they're all
468 // expected to be the same length. 469 // expected to be the same length.
469 get hasVariableLengthEntries => true; 470 get hasVariableLengthEntries => true;
470 } 471 }
471 472
473 /** A hard-coded rule for serializing Symbols. */
474 class SymbolRule extends CustomRule {
475 bool appliesTo(instance, _) => instance is Symbol;
476 getState(instance) => [MirrorSystem.getName(instance)];
477 create(state) => new Symbol(state[0]);
478 setState(symbol, state) {}
479 int get dataLength => 1;
480 bool get hasVariableLengthEntries => false;
481 }
482
472 /** Create a lazy list/map that will inflate its items on demand in [r]. */ 483 /** Create a lazy list/map that will inflate its items on demand in [r]. */
473 _lazy(l, Reader r) { 484 _lazy(l, Reader r) {
474 if (l is List) return new _LazyList(l, r); 485 if (l is List) return new _LazyList(l, r);
475 if (l is Map) return new _LazyMap(l, r); 486 if (l is Map) return new _LazyMap(l, r);
476 throw new SerializationException("Invalid type: must be Map or List - $l"); 487 throw new SerializationException("Invalid type: must be Map or List - $l");
477 } 488 }
478 489
479 /** 490 /**
480 * This provides an implementation of Map that wraps a list which may 491 * This provides an implementation of Map that wraps a list which may
481 * contain references to (potentially) non-inflated objects. If these 492 * contain references to (potentially) non-inflated objects. If these
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 removeAll(x) => _throw(); 573 removeAll(x) => _throw();
563 retainAll(x) => _throw(); 574 retainAll(x) => _throw();
564 removeWhere(x) => _throw(); 575 removeWhere(x) => _throw();
565 retainWhere(x) => _throw(); 576 retainWhere(x) => _throw();
566 getRange(x, y) => _throw(); 577 getRange(x, y) => _throw();
567 setRange(x, y, z, [a]) => _throw(); 578 setRange(x, y, z, [a]) => _throw();
568 removeRange(x, y) => _throw(); 579 removeRange(x, y) => _throw();
569 get reversed => _throw(); 580 get reversed => _throw();
570 void set length(x) => _throw(); 581 void set length(x) => _throw();
571 } 582 }
OLDNEW
« no previous file with comments | « pkg/serialization/lib/src/mirrors_helpers.dart ('k') | pkg/serialization/test/serialization_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698