slang-users mailing list

[2013 Date Index] [2013 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

[slang-users] creating struct and list from C


Hi,

I'm trying to create and fill heterogeneous data objects -- structs and
lists -- from the S-Lang C library (because I'd ultimately like to work
out a JSON-module for S-Lang), but I don't know how to do that.
If somebody had experience with that, I'd appreciate any advice.
[Otherwise, I apologize for the long mail that follows... ;-)]

When trying to push an exemplary structure like
> struct { stri="Hello, World!", stru=struct { i=42, d=3.14 } }
from my module's C code onto the S-Lang stack, I had no problems with
the inner struct -- following the example on `Interpreter Structures'
from http://www.jedsoft.org/slang/doc/html/cslang-5.html#ss5.5 or
slexcept.c:get_exception_info_intrinsic from S-Lang's source code:

> static void create_inner_struct(void)
> {
>   char* field_names[2] = { "i", "d" };
>   SLtype field_types[2] = { SLANG_INT_TYPE, SLANG_DOUBLE_TYPE };
>   int i[1] = { 42 };
>   double d[1] = { 3.14 };
>   VOID_STAR field_values[2] = { i, d };
> 
>   SLstruct_create_struct(2, field_names, field_types, field_values);
> }

However, for the outer struct, I don't quite know how to provide the
value of the "stru" field of type SLANG_STRUCT_TYPE. slang.h defines
> typedef struct _pSLang_Struct_Type SLang_Struct_Type;
but I cannot work with SLang_Struct_Type unless I copy some definitions
"for S-Lang internal structures that users do not (should not) need."
(namely for _pSLang_Struct_Type and the types it refers to) from
_slang.h to my module. After that, the following code seems to work:

> static void create_outer_struct(void)
> {
>   char* field_names[2] = { "stri", "stru" };
>   SLtype field_types[2] = { SLANG_STRING_TYPE, SLANG_STRUCT_TYPE };
>   char* stri[1] = { "Hello, World!" };
>   SLang_Struct_Type* stru = (SLang_Struct_Type*) malloc(sizeof(SLang_Struct_Type));
>   VOID_STAR field_values[2] = { stri, stru };
> 
>   create_inner_struct();
>   SLang_pop_struct(stru);
>   SLstruct_create_struct(2, field_names, field_types, field_values);
> }

And for creating lists, the only way I found involves calling S-Lang
intrinsic functions (list_new, list_insert) -- e.g. for `{ 3.14 }':

> static void create_list(void)
> {
>    SLang_load_string("list_new");
>    SLdup_n(1);
>    SLang_push_double(3.14);
>    SLang_load_string("list_insert");
> }

Would that be the favored way? In this case, I could think of a similar
solution for the structure example above, involving `set_struct_fields':

> static void create_outer_struct2(void)
> {
>   char* field_names[2] = { "stri", "stru" };
>   SLtype field_types[2] = { SLANG_STRING_TYPE, SLANG_STRUCT_TYPE };
>   char* stri[1] = { "Hello, World!" };
>   VOID_STAR field_values[2] = { stri, NULL };
> 
>   SLstruct_create_struct(2, field_names, field_types, field_values);
>   SLdup_n(1);
>   SLang_push_string(field_names[1]);
>   create_inner_struct();
>   SLang_load_string("set_struct_field");
> }

In general, would one want to have intrinsic S-Lang functions like
list_new, list_insert, and set_struct_field also available in the public
C API defined by slang.h? In this case, I could also try to prepare the
corresponding patch and send it to John.

Thanks for any hints and for reading so far! :-)

Cheers,

Manfred



[2013 date index] [2013 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]