Hi,
How do I go about to map variables from a cmod to pike?
I've found map_variable( "name", "type", flags, offset, pike_type ) but it comes along with a few questions. Can I only map struct object|string|array|... stuff from my Foo_program struct, or c stuff as well? like a foo * (or foo[]) treated as an array (guess not, so I'm about to make a struct array of my foo[])..
I'm only needing it for read only, but what happens if written to?
What do you mean by "map variables"?
If you are asking how to make variables in your CMOD visible from Pike, you should use code like "PIKEVAR int x" to create an integer that is visible from Pike.
Marcus Agehall (PacketFront) @ Pike (-) developers forum wrote:
What do you mean by "map variables"?
If you are asking how to make variables in your CMOD visible from Pike, you should use code like "PIKEVAR int x" to create an integer that is visible from Pike.
Oh, right :) that was it. I'm still a bit confused about the array stuff though. But I think I'll get it soon enough =) Like, how do I initialize a pike array from my cmod? (i.e. I want to copy a c array to pike).
That was discussed a while back. Look in array.h to see exactly how it's to be done in a safe manner.
If you know the size in advance that's not the optimal method, though.
struct array *make_str_array( char *x, int size ) { int i; struct array *x = allocate_array_no_init( size ); struct svalue *ptr = x->item; for( i=size; i>0; i-- ) { ptr->type = PIKE_T_STRING; ptr->subtype = 0; ptr->u.string = make_shared_string( *x ); x++; ptr++; } return x; }
Per Hedbor () @ Pike (-) developers forum wrote:
Oh well. Needless to say, I have not actually tested the code. :-)
Thanks alot for the examples. I've actually already came to use something like Per recommended, since I don't need/use the stack to build the array. One question though, what defines the size of the array? Thought it was v->size. So, what's good/bad with my attempt this far (was about to test if it works or not, but you guys are too fast for me ;P)
THIS->acs_map = allocate_array( 512 ); add_ref( THIS->acs_map );
int i; for( i = 0; acs_map[i]; i++ ) { if( i > 512 ) // should grow, will fix later... Pike_error( "Unexpected large acs_map!" );
THIS->acs_map->item[ i ].u.integer = acs_map[ i ]; }
THIS->acs_map->size = i;
I'm aware of the allocate_array_no_init(0, 512) alternative, but the I would have to set the PIKE_T_INT type and stuff, right? so this feels a bit cleaner... but I'm not really sure about what I'm doing, just feels like it should work :ob
THIS->acs_map = allocate_array( 512 ); add_ref( THIS->acs_map );
...
Pike_error( "Unexpected large acs_map!" );
This is exactly why you want to use the stack. You need to free the array before you throw the error, otherwise. If you call anything that can throw an error you need to catch that too, and free the array in that case as well.
Marcus Agehall (PacketFront) @ Pike (-) developers forum wrote:
What do you mean by "map variables"?
If you are asking how to make variables in your CMOD visible from Pike, you should use code like "PIKEVAR int x" to create an integer that is visible from Pike.
Is this only possible inside a PIKECLASS? It works there, but when I add a PIKEVAR int foo; to the main module (ends up in the _struct) i get a segfault right after the PIKE_MODULE_INIT has run.
Kaos wrote:
Marcus Agehall (PacketFront) @ Pike (-) developers forum wrote:
What do you mean by "map variables"?
If you are asking how to make variables in your CMOD visible from Pike, you should use code like "PIKEVAR int x" to create an integer that is visible from Pike.
Is this only possible inside a PIKECLASS? It works there, but when I add a PIKEVAR int foo; to the main module (ends up in the _struct) i get a segfault right after the PIKE_MODULE_INIT has run.
I don't get the ADD_STORAGE( struct _struct ) I would expect, as I've seen for the classes that has these struct's added (but with their name prefixed, though). Don't I have to call ADD_STORAGE for the module's _struct? (or precompile) as is done for the classes it contains.
Kaos wrote:
Kaos wrote:
Marcus Agehall (PacketFront) @ Pike (-) developers forum wrote:
What do you mean by "map variables"?
If you are asking how to make variables in your CMOD visible from Pike, you should use code like "PIKEVAR int x" to create an integer that is visible from Pike.
Is this only possible inside a PIKECLASS? It works there, but when I add a PIKEVAR int foo; to the main module (ends up in the _struct) i get a segfault right after the PIKE_MODULE_INIT has run.
I don't get the ADD_STORAGE( struct _struct ) I would expect, as I've seen for the classes that has these struct's added (but with their name prefixed, though). Don't I have to call ADD_STORAGE for the module's _struct? (or precompile) as is done for the classes it contains.
Ok, I'll answer myself this time :) I tried to add a
_storage_offset = ADD_STORAGE( struct _struct );
right before my INIT; and this solved the seg faulting !!
/me goes back to coding tha stuff ;)
pike-devel@lists.lysator.liu.se