@@ -17,7 +17,8 @@ var Enum = require("./enum"),
1717 encoder = require ( "./encoder" ) ,
1818 decoder = require ( "./decoder" ) ,
1919 verifier = require ( "./verifier" ) ,
20- converter = require ( "./converter" ) ;
20+ converter = require ( "./converter" ) ,
21+ wrappers = require ( "./wrappers" ) ;
2122
2223/**
2324 * Constructs a new reflected message type instance.
@@ -428,10 +429,13 @@ Type.prototype.create = function create(properties) {
428429Type . prototype . setup = function setup ( ) {
429430 // Sets up everything at once so that the prototype chain does not have to be re-evaluated
430431 // multiple times (V8, soft-deopt prototype-check).
432+
431433 var fullName = this . fullName ,
432434 types = [ ] ;
433435 for ( var i = 0 ; i < /* initializes */ this . fieldsArray . length ; ++ i )
434436 types . push ( this . _fieldsArray [ i ] . resolve ( ) . resolvedType ) ;
437+
438+ // Replace setup methods with type-specific generated functions
435439 this . encode = encoder ( this ) . eof ( fullName + "$encode" , {
436440 Writer : Writer ,
437441 types : types ,
@@ -450,14 +454,25 @@ Type.prototype.setup = function setup() {
450454 types : types ,
451455 util : util
452456 } ) ;
453- if ( this . options && this . options . __formObject )
454- this . fromObject = this . options . __formObject . bind ( { fromObject : this . fromObject } ) ;
455457 this . toObject = converter . toObject ( this ) . eof ( fullName + "$toObject" , {
456458 types : types ,
457459 util : util
458460 } ) ;
459- if ( this . options && this . options . __toObject )
460- this . toObject = this . options . __toObject . bind ( { toObject : this . toObject } ) ;
461+
462+ // Inject custom wrappers for common types
463+ var wrapper = wrappers [ fullName ] ;
464+ if ( wrapper ) {
465+ var originalThis = Object . create ( this ) ;
466+ // if (wrapper.fromObject) {
467+ originalThis . fromObject = this . fromObject ;
468+ this . fromObject = wrapper . fromObject . bind ( originalThis ) ;
469+ // }
470+ // if (wrapper.toObject) {
471+ originalThis . toObject = this . toObject ;
472+ this . toObject = wrapper . toObject . bind ( originalThis ) ;
473+ // }
474+ }
475+
461476 return this ;
462477} ;
463478
0 commit comments