I’m writing a program where I need to copy a structure from C to mJS.
I found an example of how to use the s2o function but I don’t know how to complete the s
parameter from the function
// Assuming s
is a foreign pointer to an instance of my_struct
, obtained elsewhere.
let o = s2o(s, sd);
How do I pass s as a pointer to the energy
structure?
my C Code
> struct my_struct {
> int phases;
> float v1;
> float v2;
> float v3;
> float i1;
> float i2;
> float i3;
> } energy;
>
> static const struct mjs_c_struct_member my_struct_descr[] = {
> {"phases", offsetof(struct my_struct, phases), MJS_STRUCT_FIELD_TYPE_INT, NULL},
> {"v1", offsetof(struct my_struct, v1), MJS_STRUCT_FIELD_TYPE_FLOAT, NULL},
> {"v2", offsetof(struct my_struct, v2), MJS_STRUCT_FIELD_TYPE_FLOAT, NULL},
> {"v3", offsetof(struct my_struct, v3), MJS_STRUCT_FIELD_TYPE_FLOAT, NULL},
> {"i1", offsetof(struct my_struct, i1), MJS_STRUCT_FIELD_TYPE_FLOAT, NULL},
> {"i2", offsetof(struct my_struct, i2), MJS_STRUCT_FIELD_TYPE_FLOAT, NULL},
> {"i3", offsetof(struct my_struct, i3), MJS_STRUCT_FIELD_TYPE_FLOAT, NULL},
> {NULL, 0, MJS_STRUCT_FIELD_TYPE_INVALID, NULL},
> };
>
> const struct mjs_c_struct_member *get_my_struct_descr(void) {
> return my_struct_descr;
> };
my mJS Code
let sd = ffi('void *get_my_struct_descr(void)')();
let o = s2o(s, sd);
print(o.v1, o.i1);