TooTallNate / ref

Turn Buffer instances into "pointers"
http://tootallnate.github.com/ref
453 stars 141 forks source link

How to proper working with Union and Sctruct nested #52

Closed rodrigopandini closed 7 years ago

rodrigopandini commented 8 years ago

I´m trying to create some types that will be used with node-ffi, but I have some doubts about how to use Union and Struct type nested. Should I define first the nested fields as new types, or can I does directly like this:

var ref = require('ref');
var Struct = require('ref-struct');
var Union = require('ref-union');
var ffi = require('ffi');

// typedefs
var int = ref.types.int;
var double = ref.types.double;
var long = ref.types.long;
var char = ref.types.char;
var string = ref.types.CString;

/*
struct px_val {
  char isnull;
  int type;
  union {
    long lval;
    double dval;
    struct {
      char *val;
      int len;
    } str;
  } value;
};
*/

var px_val = Struct({
  isnull: char,
  type: int,
  value: Union({
    lval: long,
    dval: double,
    str: Struct({
      val: string,
      len: int
    })
  })
});
TooTallNate commented 7 years ago

Looks ok to me. Re-open if you had issues.