I allocate the buffer at startup time and keep it around for all kinds of 
messages. So there is always a buffer available to send data back and 
forth. The same buffer can be used for different data structs.

To access the buffer from C you can store the pointer to it in a local 
variable, e.g.

#define msgInit 0
#define msgMyStruct 1
void *trainBuf = 0L;

int sendMessage(int msgType, void* param){ 
  switch (msgType){
    case msgInit:
      trainBuf = param;
      break;
    case msgMyStruct:
      //see above
      break;
  }

So at init time you allocate the buffer on the js side, then send a msgInit 
to store the pointer (which is an int only) in a local variable. From there 
on you can use it from both sides:

int msgToJS(float a, float b, float c){
  myStructPtr    p = (myStructPtr)trainBuf;
    p->a    = a;
    p->b    = b;
    p->c    = c; 
  //then you notify the JS side:
#ifdef __EMSCRIPTEN__
            EM_ASM_({
//whatever you want to do there. The data will be in central.trainBuf and 
can be extracted the same way you inserted it when sending the message.
           
           });
#endif //__EMSCRIPTEN__

Regarding your second question:
As said, I keep this buffer alive because I'm sending lots of data back and 
forth. But of course, you can free the buffer as soon as you have read its 
contents every time.
ale2...@gmail.com schrieb am Mittwoch, 9. November 2022 um 18:08:18 UTC+1:

> thank you very much the example works.
> I have two question:
>
> if I want send a c struct to javascript how can do it? For example the 
> same data struct 
>
> typedef struct {
>     float a,b,c;
> }myStruct,*myStructPtr;
>
> question number 2:
> in preview example who and when can free this memory? 
> this.trainBuf    = malloc(2048);
>
> Thanks in advance
> Il giorno mercoledì 9 novembre 2022 alle 12:20:02 UTC+1 Dieter Weidenbrück 
> ha scritto:
>
>> sorry, the struct should contain floats:
>>
>> typedef struct {
>>     float a,b,c;
>> }myStruct,*myStructPtr; 
>>
>> Dieter Weidenbrück schrieb am Dienstag, 8. November 2022 um 17:09:47 
>> UTC+1:
>>
>>> Hi,
>>>
>>> I am using this method:
>>>
>>> on the JS side allocate a buffer to store things
>>>
>>> export class Central {
>>>     constructor(app){
>>>         this.mApp    = app;
>>>         this.trainBuf    = malloc(2048); //or whatever you need
>>>     }
>>>
>>> //a function to fill the buffer
>>> sendData(a,b,c){
>>>         let i = this.trainBuf >> 2;
>>>         HEAPF32[ i + 0]  = a;
>>>         HEAPF32[ i + 1]  = b;
>>>         HEAPF32[ i + 2]  = c;
>>>      //sendMessage neds to exist on the C side, too
>>>     sendMessage(this.trainBuf); //inform you c code that data has been 
>>> sent
>>>     }
>>> };
>>>
>>> Then in the c code you accept the event like this:
>>>
>>> typedef struct {
>>>     int a,b,c;
>>> }myStruct,*myStructPtr;
>>>
>>> myStruct    d;
>>>
>>> int sendMessage(void* param){
>>>     myStructPtr    p = (myStructPtr)param;
>>>
>>>     d.a    = p->a;
>>>     d.b    = p->b;
>>>     d.c    = p->c;
>>> }
>>>
>>> ale2...@gmail.com schrieb am Dienstag, 8. November 2022 um 16:16:49 
>>> UTC+1:
>>>
>>>> Hello, 
>>>> I want define some data struct as message and send from emscripten to 
>>>> javascript and viceversa, because i need to comunicate between opengl 
>>>> canvas (on c++ side) to html gui.
>>>> What's the best way to do that?
>>>>
>>>

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to emscripten-discuss+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/emscripten-discuss/705811d9-39dd-4157-b576-44e6b7bb71f9n%40googlegroups.com.

Reply via email to