Skip to content

Nested structures with array #31

@KunalSaini

Description

@KunalSaini

Hi, I am using ref, ref-struct & ref-array to emulate following c structure

struct InnerStruct
{
  int *data;
  int *size;
  int allocatedSize;
  int numDimensions;
  unsigned char canFreeData;
};
typedef struct {
  emxArray_real_T *myProp;
} OuterStruct;

In my javascript , i am trying to create a object, set values and then read them back. However unable to do that for int *data and int *size

const ref = require('ref');
const StructTypeFactory = require('ref-struct');
const ArrayTypeFactory = require('ref-array');

const inttype = ref.types.int;
const booltype = ref.types.bool;
const IntArraytype = ArrayTypeFactory(inttype);

const innerStruct = StructTypeFactory({
  data: IntArraytype,
  size: IntArraytype,
  allocatedSize: inttype,
  numDimensions: inttype,
  canFreeData: booltype,
});

const outerStructure = StructTypeFactory();
outerStructure.defineProperty('myProp', ref.refType(innerStruct));

const instance = new outerStructure();
const myPropInst = new innerStruct({
  data: new IntArraytype([12, 34, 23, 45]),
  size: new IntArraytype([0, 4]),
  allocatedSize: 16,
  numDimensions: 1,
  canFreeData: true,
});

instance.myProp = myPropInst.ref();

console.dir(instance.myProp.deref().allocatedSize); //WORKS fine, return 16
console.dir(instance.myProp.deref().numDimensions); //WORKS fine, return 1
console.dir(instance.myProp.deref().canFreeData); //WORKS fine, return true
console.dir(instance.myProp.deref().data); //Does NOT give back the expected array [12, 34, 23, 45]
console.dir(instance.myProp.deref().size); //Does NOT give back the expected array [0, 4]

What am i missing here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions