lib/union.js
function Union () {
  debug('defining new union "type"')

  function UnionType (arg, data) {
    if (!(this instanceof UnionType)) {
      return new UnionType(arg, data)
    }
    debug('creating new union instance')
    var store
    if (Buffer.isBuffer(arg)) {
      debug('using passed-in Buffer instance to back the union', arg)
      assert(arg.length >= UnionType.size, 'Buffer instance must be at least '
          + UnionType.size + ' bytes to back this untion type')
      store = arg
      arg = data
    } else {
      debug('creating new Buffer instance to back the union (size: %d)', UnionType.size)
      store = new Buffer(UnionType.size)
    }

    // set the backing Buffer store
    store.type = UnionType
    this['ref.buffer'] = store

    // initialise the union with values supplied
    if (arg) {
      //TODO: Sanity check - e.g. (Object.keys(arg).length == 1)
      for (var key in arg) {
        // hopefully hit the union setters
        this[key] = arg[key]
      }
    }
    UnionType._instanceCreated = true
  }

  // make instances inherit from `proto`
  UnionType.prototype = Object.create(proto, {
    constructor: {
        value: UnionType
      , enumerable: false
      , writable: true
      , configurable: true
    }
  })

  UnionType.defineProperty = defineProperty
  UnionType.toString = toString
  UnionType.fields = {}

  // comply with ref's "type" interface
  UnionType.size = 0
  UnionType.alignment = 0
  UnionType.indirection = 1
  UnionType.get = get
  UnionType.set = set

  // Read the fields list
  var arg = arguments[0]
  if (typeof arg === 'object') {
    Object.keys(arg).forEach(function (name) {
      var type = arg[name];
      UnionType.defineProperty(name, type);
    })
  }

  return UnionType
}
function Union () {
  debug('defining new union "type"')

  function UnionType (arg, data) {
    if (!(this instanceof UnionType)) {
      return new UnionType(arg, data)
    }
    debug('creating new union instance')
    var store
    if (Buffer.isBuffer(arg)) {
      debug('using passed-in Buffer instance to back the union', arg)
      assert(arg.length >= UnionType.size, 'Buffer instance must be at least '
          + UnionType.size + ' bytes to back this untion type')
      store = arg
      arg = data
    } else {
      debug('creating new Buffer instance to back the union (size: %d)', UnionType.size)
      store = new Buffer(UnionType.size)
    }

    // set the backing Buffer store
    store.type = UnionType
    this['ref.buffer'] = store

    // initialise the union with values supplied
    if (arg) {
      //TODO: Sanity check - e.g. (Object.keys(arg).length == 1)
      for (var key in arg) {
        // hopefully hit the union setters
        this[key] = arg[key]
      }
    }
    UnionType._instanceCreated = true
  }

  // make instances inherit from `proto`
  UnionType.prototype = Object.create(proto, {
    constructor: {
        value: UnionType
      , enumerable: false
      , writable: true
      , configurable: true
    }
  })

  UnionType.defineProperty = defineProperty
  UnionType.toString = toString
  UnionType.fields = {}

  // comply with ref's "type" interface
  UnionType.size = 0
  UnionType.alignment = 0
  UnionType.indirection = 1
  UnionType.get = get
  UnionType.set = set

  // Read the fields list
  var arg = arguments[0]
  if (typeof arg === 'object') {
    Object.keys(arg).forEach(function (name) {
      var type = arg[name];
      UnionType.defineProperty(name, type);
    })
  }

  return UnionType
}
function Union ( ) { debug ( 'defining new union "type"' ) function UnionType ( arg , data ) { if ( ! ( this instanceof UnionType ) ) { return new UnionType ( arg , data ) } debug ( 'creating new union instance' ) var store if ( Buffer . isBuffer ( arg ) ) { debug ( 'using passed-in Buffer instance to back the union' , arg ) assert ( arg . length >= UnionType . size , 'Buffer instance must be at least ' + UnionType . size + ' bytes to back this untion type' ) store = arg arg = data } else { debug ( 'creating new Buffer instance to back the union (size: %d)' , UnionType . size ) store = new Buffer ( UnionType . size ) } // set the backing Buffer store store . type = UnionType this [ 'ref.buffer' ] = store // initialise the union with values supplied if ( arg ) { //TODO: Sanity check - e.g. (Object.keys(arg).length == 1) for ( var key in arg ) { // hopefully hit the union setters this [ key ] = arg [ key ] } } UnionType . _instanceCreated = true } // make instances inherit from `proto` UnionType . prototype = Object . create ( proto , { constructor : { value : UnionType , enumerable : false , writable : true , configurable : true } } ) UnionType . defineProperty = defineProperty UnionType . toString = toString UnionType . fields = { } // comply with ref's "type" interface UnionType . size = 0 UnionType . alignment = 0 UnionType . indirection = 1 UnionType . get = get UnionType . set = set // Read the fields list var arg = arguments [ 0 ] if ( typeof arg === 'object' ) { Object . keys ( arg ) . forEach ( function ( name ) { var type = arg [ name ] ; UnionType . defineProperty ( name , type ) ; } ) } return UnionType }
The "Union" type constructor.
The Union type constructor .