Thanks, that actually got me much closer, however now I'm getting back an
incorrect value now.
This is the console output:
{ _pointer: <Buffer@0x98d6bf0 e8 26 69 08 2e 00 00 00> } //the first f
struct I instantiated, then I call the library setT method
This is a test: 6 //printf test from within the C library to show that the
correct value is stored in the struct
{ _pointer: <Buffer@0x98d6bf0 05 00 00 00 88 cd f6 bf> } //the returned f
struct with the correct first variable set to 5 and the second a pointer
{ _pointer: <SlowBuffer@0xbff6cd88 fd b0 7f b6> } //this is the returned
buffer from calling _f.t_p.deref()
-1074344436 //this is t_i which should be 6
within my C library I'm setting the struct variables like this:
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);
}
On Wednesday, September 5, 2012 1:02:40 PM UTC-4, Nathan Rajlich wrote:
>
> You examples are a little confusing, but I believe you are needing to
> call .deref() in these cases.
>
> Like here: `_f.t_p` that would be a pointer (a Buffer instance) to a
> "t" struct, so to turn that back into the "t" struct instance to you
> need to call `_f.t_p.deref()`. Then you can access `t_i` as expected.
>
> Let me know if that's what you needed!
>
> On Tue, Sep 4, 2012 at 10:09 PM, rhasson <[email protected] <javascript:>>
> 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]<javascript:>
> > To unsubscribe from this group, send email to
> > [email protected] <javascript:>
> > For more options, visit this group at
> > http://groups.google.com/group/nodejs?hl=en?hl=en
>
--
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