Emscripten getting element points (?) into C++.

23 views
Skip to first unread message

Nicholas Ingrassellino

unread,
May 12, 2023, 6:53:40 PM5/12/23
to emscripten-discuss
Good morning all! Or good afternoon or have a great night. You know, wherever you are.

I have the following code:
void* createElement( const char* type ) {
    return EM_ASM_PTR( {
        return document.createElement( UTF8ToString( $0 ) );
    }, type );
};

I am calling it like this:
std::cout << static_cast< char* >( cppquery::html::createElement( "div" ) ) << std::endl;

It spits out emscgT �� which I thought was a good sign. Turns out trying to create two different elements at two different times always says that when cast to anything other than either void* or char*.

What I am trying to do is crate an element (not yet added to the DOM), store whatever (a pointer, I guess) to it, and use it later with other C++-stored JavaScript element objects.

I could just do all the work on the JavaScript side and JSON.stringyify everything but that sounds like way too much.

Nicholas Ingrassellino

unread,
May 12, 2023, 7:22:32 PM5/12/23
to emscripten-discuss

Sorry, I do not see an edit button but I made a mistake. Here is the correction:
It spits out emscgT �� which I thought was a good sign. Turns out trying to create two different elements at two different times always says that when cast to either void* or char* (all over types just give me 0).

Sam Clegg

unread,
May 12, 2023, 8:00:07 PM5/12/23
to emscripte...@googlegroups.com
Your code looks like it will correctly create the element but you cannot return a JS object to native code like that.   

You can only return basic types like numbers unless you use some kind of higher level binding system such as embind. 

See https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html for more information on connecting the JS and C++ worlds.

--
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-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/caeb2610-9aaa-4c30-8d4a-4feaad7a0f0en%40googlegroups.com.

Nicholas Ingrassellino

unread,
May 13, 2023, 1:55:08 AM5/13/23
to emscripten-discuss
I have been looking at Embind for the last few days. Thank you for the suggestion but I do not think it is what I am looking for (although I could be wrong there, there is so much reading to learn from).

Really my problem boils down to this: I want to create and manage the HTML elements (I am not interested in Canvas) via C++ in order to create a new head-only library that is the jQuery for C++/Emscripten. This is my goal, any way. Going to be real slow going since I am only one guy (will invite pull requests once I am happy with it). Especially if I remain stuck for a while here.

Thanks for any help and/or pointing me to more information!

Nicholas Ingrassellino

unread,
May 13, 2023, 1:58:05 AM5/13/23
to emscripten-discuss
By the way, I already have some functionality completed. WebSockets, for example, now works 100% from C++ (actually I stole and improved some code from an old game I had written that I wanted to release on WIndows, Linux, Web).

Sam Clegg

unread,
May 13, 2023, 3:25:41 AM5/13/23
to emscripte...@googlegroups.com
The main thing to remember in this kind of endeavor is that C++ code can only reason about numeric types.  So whenever you are detailing with JS object, such as DOM elements, you need to refer to them via some kind of index in the table that your (or somebody) maintains on the JS side.  Direct DOM access is not possible from C++ so it all has to happen at arm's length.

Reply all
Reply to author
Forward
0 new messages