I figured out my problem.  I was allocating on the stack and needed to use 
malloc.

Here is the correct C function:

void setT(f *i) {
  t *tt = malloc(sizeof(t));

  i->fp_i = 5;
  tt->t_i = 6;
  i->t_p = tt;
  printf("\tThis is a test: %i\n", i->t_p->t_i);
}

Here is the correct output:

{ _pointer: <Buffer@0xa14dbf0 e8 26 69 08 2e 00 00 00> }
This is a test: 6
{ _pointer: <Buffer@0xa14dbf0 05 00 00 00 f8 8a 12 0a> }
{ _pointer: <SlowBuffer@0xa128af8 06 00 00 00> }
6

Thanks,
Roy


On Wednesday, September 5, 2012 1:09:48 AM UTC-4, rhasson wrote:
>
> Nate, 
>
> I'm back with some more questions about node-struct and handling nested 
> structures.
>
> In the example below, I created one simple struct with a single Int and 
> another struct with an Int and a pointer to the first struct.
> What I noticed is that I can't access the int inside the nested structure. 
>  Another thing I noticed is that if I do something like this:
> var myStruct = Struct();
> myStruct.defineProperty('someProp', ref.refType('int'));
>
> var t = new myStruct();
>
> accessing t.someProp fails.  If I change the definition above to 
> ('someProp', ref.types.int), I can access t.someProp with no problem.
>
> Why is this and how to deal with this?
>
> Below you can see I'm running into the same issue, however since I'm 
> defining the property is a refType(struct_one) I can't figure out how to 
> access its propertied.  It seems like it's an unrecognized type.
>
> I have this .js file:
>
> var ffi = require('ffi');
> var ref = require('ref');
> var Struct = require('ref-struct');
>
> //define a simple struct
> var t = Struct({
>   't_i': 'int'
> });
>
> //define a second stuct with a pointer to an instance of the first struct
> var f = new Struct();
>   f.defineProperty('fp_i', ref.types.int);
>   f.defineProperty('t_p', ref.refType(t));
>
> var tPtr = ref.refType(t);
> var fPtr = ref.refType(f);
>
> var lib = './libffi.so.1.0.1';
>
> var l = ffi.Library(lib, {
>         'setT': ['void', [fPtr]]
>         });
>
> var _f = new f();
> var x = null, d = null;
> console.log(_f);  //I see the buffer that's created
> l.setT(_f.ref());
>
> console.log(_f);  //I see the updated buffer
>
> console.log('fp_i: ', _f.fp_i);  //works great, returns the expected value
>
> console.log('t_i: ', _f.t_p.t_i); //this is undefined, not sure how to 
> access the nested struct's members.
>
> My .c file looks like this:
>
> #include <stdio.h>
> #include <string.h>
>
> typedef struct t {
>   int t_i;
> } t;
>
> typedef struct f {
>   int fp_i;
>   struct t *t_p;
> } f;
>
> void setT(f *i) {
>   t *tt;
>   
>   i->fp_i = 5;
>   tt->t_i = 6;
>   i->t_p = tt;
>   printf("\tThis is a test: %i\n", i->t_p->t_i);  //successfully prints 6
> }
>
> void main(){}
>
>
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to