WebIDL issue with callback to JS

85 views
Skip to first unread message

Scott Watson

unread,
Feb 13, 2018, 3:05:42 PM2/13/18
to emscripten-discuss
Hi folks,

I'm trying to create a callback mechanism to call into JS from C++.  I have a pure virtual C++ class that defines the callback interface defined as: 

class MediaPlayerDelegate {
public:
   
virtual void PlayerStateChanged(PlayerState state, void *context)=0;
   
virtual void PlayerStatus(StatusInfo *status, void *context)=0;
   
virtual void PlayerErrorEncountered(PlayerError error, void *context)=0;
};



and I've described it in the IDL file as:

[Prefix="Arris::", NoDelete]
callback
interface MediaPlayerDelegate {
   
void PlayerStateChanged(PlayerState state, any context);
   
void PlayerStatus(StatusInfo status, any context);
   
void PlayerErrorEncountered(PlayerError error, any context);
};


[JSImplementation="MediaPlayerDelegate"]
interface MediaPlayerJSDelegate {
   
void MediaPlayerJSDelegate();
   
void PlayerStateChanged(PlayerState state, any context);
   
void PlayerStatus(StatusInfo status, any context);
   
void PlayerErrorEncountered(PlayerError error, any context);
};



But the generated code for the PlayerStatus method won't compile.  My glue.cpp file looks like this:

  void PlayerStatus(Arris::StatusInfo arg0, void* arg1) {
    EM_ASM_INT
({
     
var self = Module['getCache'](Module['MediaPlayerJSDelegate'])[$0];
     
if (!self.hasOwnProperty('PlayerStatus')) throw 'a JSImplementation must implement all functions, you forgot MediaPlayerJSDelegate::PlayerStatus.';
     
self['PlayerStatus']($1,$2);
   
}, (int)this, (int)arg0, arg1);
 
}


Why isn't arg0 a StatusInfo *?

If I make the change manually it builds but making the callback generates an exception.  The other two methods work fine.  

Any suggestions?

-------
Scott

Alon Zakai

unread,
Feb 14, 2018, 6:34:47 PM2/14/18
to emscripten-discuss
How did you define StatusInfo in the idl file?

--
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+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Scott Watson

unread,
Feb 14, 2018, 6:44:53 PM2/14/18
to emscripten-discuss
Hi Alon,

StatusInfo looks like this:

[Prefix="Arris::"]
interface StatusInfo {  
   
readonly attribute unsigned long bitrate;
   
readonly attribute unsigned long networkthroughput;
   
readonly attribute unsigned long optimalbitrate;

   
readonly attribute unsigned long droppedframes;
   
readonly attribute unsigned long stalls;

   
readonly attribute double        decodeFPS;
   
readonly attribute double        renderFPS;

   
readonly attribute unsigned long audiosegments;
   
readonly attribute unsigned long videosegments;
   
readonly attribute unsigned long h264frames;
   
readonly attribute unsigned long rgbframes;
   
readonly attribute unsigned long pcmframes;

   
readonly attribute double        seconds;
   
readonly attribute double        duration;
   
readonly attribute double        cacheduration;

    readonly attribute PlayerState   playerstate;
   
readonly attribute boolean       frontendrunning;
   
readonly attribute boolean       audiodemuxerrunning;
   
readonly attribute boolean       videodemuxerrunning;
   
readonly attribute boolean       decoderrunning;
   
readonly attribute boolean       audiorendererrunning;
   
readonly attribute boolean       videorendererrunning;
   
readonly attribute PlayerError   lasterror;
};

Alon Zakai

unread,
Feb 15, 2018, 2:20:09 PM2/15/18
to emscripten-discuss
I see, thanks. Yeah, looking into this some more, that does seem like surprising behavior - I would also expect a pointer there, not the object itself. Seems surprising we didn't notice it til now, but it appears bullet and box2d use references in most of their APIs, not pointers.

Moving forward, it's probably not worth changing the current behavior. We could add a new attribute, "Ptr" perhaps, to parallel "Ref".

To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Scott Watson

unread,
Feb 15, 2018, 2:29:51 PM2/15/18
to emscripte...@googlegroups.com
Ok, there’s really no reason I couldn’t use a reference in the meantime.  

On the Javascript side is there anything special I need to do the “unwrap” the reference?

--------------------------------------
Scott Watson

c: 714-317-2100
e: wswa...@gmail.com


You received this message because you are subscribed to a topic in the Google Groups "emscripten-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/emscripten-discuss/RydKhN7fxqg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to emscripten-disc...@googlegroups.com.

Alon Zakai

unread,
Feb 16, 2018, 12:39:45 PM2/16/18
to emscripten-discuss
The wrappers are JS objects with a .ptr property with the pointer to the C++ object. Usually though you can just pass the wrappers around and not need to look at the raw pointers.

To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


-- 

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+unsubscri...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


-- 
You received this message because you are subscribed to a topic in the Google Groups "emscripten-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/emscripten-discuss/RydKhN7fxqg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to emscripten-discuss+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages