lib/union.js
function defineProperty (name, type) {
  debug('defining new union type field', name)

  // allow string types for convenience
  type = ref.coerceType(type)

  assert(!this._instanceCreated, 'an instance of this Union type has already '
      + 'been created, cannot add new data members anymore')
  assert.equal('string', typeof name, 'expected a "string" field name')
  assert(type && /object|function/i.test(typeof type) && 'size' in type &&
      'indirection' in type
      , 'expected a "type" object describing the field type: "' + type + '"')
  assert(!(name in this.prototype), 'the field "' + name
      + '" already exists in this Union type')

  // define the getter/setter property
  Object.defineProperty(this.prototype, name, {
      enumerable: true
    , configurable: true
    , get: get
    , set: set
  });

  var field = {
    type: type
  }
  this.fields[name] = field

  // calculate the new size and alignment
  recalc(this);

  function get () {
    debug('getting "%s" union field (length: %d)', name, type.size)
    return ref.get(this['ref.buffer'], 0, type)
  }

  function set (value) {
    debug('setting "%s" union field (length: %d)', name, type.size, value)
    return ref.set(this['ref.buffer'], 0, value, type)
  }
}
function defineProperty (name, type) {
  debug('defining new union type field', name)

  // allow string types for convenience
  type = ref.coerceType(type)

  assert(!this._instanceCreated, 'an instance of this Union type has already '
      + 'been created, cannot add new data members anymore')
  assert.equal('string', typeof name, 'expected a "string" field name')
  assert(type && /object|function/i.test(typeof type) && 'size' in type &&
      'indirection' in type
      , 'expected a "type" object describing the field type: "' + type + '"')
  assert(!(name in this.prototype), 'the field "' + name
      + '" already exists in this Union type')

  // define the getter/setter property
  Object.defineProperty(this.prototype, name, {
      enumerable: true
    , configurable: true
    , get: get
    , set: set
  });

  var field = {
    type: type
  }
  this.fields[name] = field

  // calculate the new size and alignment
  recalc(this);

  function get () {
    debug('getting "%s" union field (length: %d)', name, type.size)
    return ref.get(this['ref.buffer'], 0, type)
  }

  function set (value) {
    debug('setting "%s" union field (length: %d)', name, type.size, value)
    return ref.set(this['ref.buffer'], 0, value, type)
  }
}
function defineProperty ( name , type ) { debug ( 'defining new union type field' , name ) // allow string types for convenience type = ref . coerceType ( type ) assert ( ! this . _instanceCreated , 'an instance of this Union type has already ' + 'been created, cannot add new data members anymore' ) assert . equal ( 'string' , typeof name , 'expected a "string" field name' ) assert ( type && / object|function / i . test ( typeof type ) && 'size' in type && 'indirection' in type , 'expected a "type" object describing the field type: "' + type + '"' ) assert ( ! ( name in this . prototype ) , 'the field "' + name + '" already exists in this Union type' ) // define the getter/setter property Object . defineProperty ( this . prototype , name , { enumerable : true , configurable : true , get : get , set : set } ) ; var field = { type : type } this . fields [ name ] = field // calculate the new size and alignment recalc ( this ) ; function get ( ) { debug ( 'getting "%s" union field (length: %d)' , name , type . size ) return ref . get ( this [ 'ref.buffer' ] , 0 , type ) } function set ( value ) { debug ( 'setting "%s" union field (length: %d)' , name , type . size , value ) return ref . set ( this [ 'ref.buffer' ] , 0 , value , type ) } }
Adds a new field to the union instance with the given name and type.
Note that this function will throw an Error if any instances of the union
type have already been created, therefore this function must be called at the
beginning, before any instances are created.
Adds a new field to the union instance with the given name and type . Note that this function will throw an Error if any instances of the union type have already been created therefore this function must be called at the beginning before any instances are created .