Thanks to the awesome help from Benvie, we figured out the issue.
When declaring the structs, I wanted to create a property that is a pointer
to another struct. Following my intuition and my understanding of
ref-struct I did:
pst_desc_tree.defineProperty('next', ref.refType(pst_desc_tree)); The
assumption here is that ref.refType(struct_name) will return a pointer to
that struct. However struct_name was already a pointer to the struct so in
fast ref.refType was returning a pointer to a pointer to a struct.
Therefore the correct definition is: pst_desc_tree.defineProperty('next',
pst_desc_tree); which means "next" is a pointer to struct named
pst_desc_tree which is defined by simply: var pst_desc_tree = new Struct();
Anyways, I thought I'd circle back and provide the answer in case anyone
else is interested in delving into the awesome world of FFI.
Roy
On Thursday, September 6, 2012 1:21:59 AM UTC-4, rhasson wrote:
>
> So the example I used here was just for the sake of understanding how ref
> and struct work. I think I get it now.
>
> Now I started all this because I wanted to create FFI bindings to libpst (
> http://www.five-ten-sg.com/libpst/). I started by creating all the
> necessary structs and made a call to the first function which opens the
> .PST file (outlook personal file), followed by calls to create the indexes.
> The basic members of the struct are populated fine, however the nested
> structs are not. They come back with strange memory addresses
> which obviously cause seg fault when I try to access them.
>
> The main code is here
> https://github.com/rhasson/node-libpst/blob/master/ffi_nodepst.js#L1153-L1175scrolling
> to the top of the source you will see all the struct declarations.
>
> Here is the output of GDB:
>
> (gdb) run
> Starting program: /usr/local/bin/node ffi_nodepst.js
> Reading symbols for shared libraries
> ++++.........................................................................................................................................
>
> done
> Reading symbols for shared libraries . done
> Reading symbols for shared libraries . done
> Reading symbols for shared libraries . done
> File outlook.pst was opened successfully //outlook.pst is the file I'm
> opening and is populated within the pst_file struct and returned correctly
> Loading index...
> index ret: 0 //0 means that the indexes were loaded correctly, however
> here is where the nested structs are malloced and populated by the library
> Loading extended attributes...
> extended ret: 1 //1 means that the extended attributes where loaded
> correctly, again I can't access them
> f: { _pointer: <Buffer@0x1010644f0 a0 30 69 75 ff 7f 00 00 00 14 81 02 01
> 00 00 00 d0 04 20 02 01 00 00 00 00 00 00 00 00 00 00 00 90 0d 20 02 01 00
> 00 00 30 12 20 02 01 00 00 00 60 12 20 ...> }
> i_head: false //this is just a ref.isNull() test on each of the nested
> structs
> i_tail: false
> d_head: false
> d_tail: false
> x_head: false
> block_head: false
>
> Program received signal EXC_BAD_ACCESS, Could not access memory. //here
> I try to do f.i_head.deref() which fails since the memory location is not
> valid, but I can't figure out why.
> Reason: KERN_INVALID_ADDRESS at address: 0x0000000000007600
> 0x0000000100167dcc in
> v8::internal::ElementsAccessorBase<v8::internal::ExternalUnsignedByteElementsAccessor,
>
> v8::internal::ElementsKindTraits<(v8::internal::ElementsKind)9> >::Get ()
> (gdb)
>
>
> On Wednesday, September 5, 2012 5:12:55 PM UTC-4, Nathan Rajlich wrote:
>>
>> Glad you're figuring it out on your own :p
>>
>> I don't get your latest question: you're already passing a struct in
>> by reference in the setT() function so I'm not sure what's different.
>>
>> On Wed, Sep 5, 2012 at 1:44 PM, rhasson <[email protected]> wrote:
>> > Now, the issue I'm having is in trying to port this methodology to an
>> > external library out of my control (libpst) which does not use malloc
>> on
>> > local structs that then have pointers assigned to the main struct that
>> was
>> > passed in by reference which are then overwritten. Is there a way to
>> > address this using node-ref and node-struct ?
>> >
>> >
>> > 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
>>
>
--
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