Hints on how to wrap a C++ function with system dependencies

113 views
Skip to first unread message

Jesper Taxbøl

unread,
Sep 15, 2021, 3:40:01 AM9/15/21
to emscripten-discuss
Hi,

I am very new to emscriptem and I have been struggeling with porting a function from c++ to javascript.

I have created a public repo with my sample function, and I aim to implement feedback into it.


My overall mission is that I have some vector mathematics, that I would like to implement once and use on c++ and javascript platforms. I am using GLM as the vector-math-backbone, and I am under the impression that json is a good compromise for the languages involved. I have therefore chosen nlohmann's json library.


From what I can read, one strategy to communicate between js and c++, is using a shared memory buffer. I have therefore created an example function that takes a string and length as input and does, deserilization, computation and finally serialization to the same buffer before returning.

The c++ part works fine, but i tend to get stuck on how to create a .wasm file I can use inside the javascript of a browser.

I am on Ubuntu 20.04, and since the apt-get version of emscriptem seems to be ancient, I have based my sample on a docker container. 

emscripten/emsdk

I have given it a shot by following 


The guide seems to be somewhat outdated, and my attempts crumble as soon as I include my json library I intend to use. So I suspect I need to deal with some bindings between the json library and system. My docker instance does unfortunately not contain the tool wasm-objdump, which I suspect is needed to dig into said dependencies.

That was a long intro, so thanx for having it made through it.

I am seeking some pointers on how I would go from here.

I hope to implement the advice in the repo for others to learn from.

Kind regards

Jesper


Shlomi Fish

unread,
Sep 15, 2021, 5:41:31 AM9/15/21
to Jesper Taxbøl, emscripte...@googlegroups.com
Hi Jesper,

On Wed, 15 Sep 2021 00:40:01 -0700 (PDT)
Jesper Taxbøl <jes...@taxboel.dk> wrote:

> Hi,
>
> I am very new to emscriptem and I have been struggeling with porting a
> function from c++ to javascript.
>
> I have created a public repo with my sample function, and I aim to
> implement feedback into it.
>
> https://github.com/taxfromdk/wasm-glm-nlohmann
>
> My overall mission is that I have some vector mathematics, that I would
> like to implement once and use on c++ and javascript platforms. I am using
> GLM as the vector-math-backbone, and I am under the impression that json is
> a good compromise for the languages involved. I have therefore chosen
> nlohmann's json library.
>

I prepared a fixed version here:

https://github.com/taxfromdk/wasm-glm-nlohmann/compare/master...shlomif:shlomif1?expand=1

hope that helps.

> https://github.com/g-truc/glm
> https://github.com/nlohmann/json
>
> From what I can read, one strategy to communicate between js and c++, is
> using a shared memory buffer. I have therefore created an example function
> that takes a string and length as input and does, deserilization,
> computation and finally serialization to the same buffer before returning.
>
> The c++ part works fine, but i tend to get stuck on how to create a .wasm
> file I can use inside the javascript of a browser.
>
> I am on Ubuntu 20.04, and since the apt-get version of emscriptem seems to
> be ancient, I have based my sample on a docker container.
>
> emscripten/emsdk
>
> I have given it a shot by following
>
> https://wasmbyexample.dev/examples/strings/strings.c.en-us.html
>
> The guide seems to be somewhat outdated, and my attempts crumble as soon as
> I include my json library I intend to use. So I suspect I need to deal with
> some bindings between the json library and system. My docker instance does
> unfortunately not contain the tool wasm-objdump, which I suspect is needed
> to dig into said dependencies.
>
> That was a long intro, so thanx for having it made through it.
>
> I am seeking some pointers on how I would go from here.
>
> I hope to implement the advice in the repo for others to learn from.
>
> Kind regards
>
> Jesper
>
>



--

Shlomi Fish https://www.shlomifish.org/
“So, who the hell is Qoheleth?” - https://shlom.in/qoheleth

The KGB used to torture their victims by having them look at scrolling XSLT
code.
https://www.shlomifish.org/humour/bits/facts/XSLT/

Please reply to list if it's a mailing list post - https://shlom.in/reply .

Shlomi Fish

unread,
Sep 15, 2021, 6:05:01 AM9/15/21
to Jesper Taxbøl, emscripte...@googlegroups.com
On Wed, 15 Sep 2021 12:41:24 +0300
Shlomi Fish <shl...@shlomifish.org> wrote:

> Hi Jesper,
>
> On Wed, 15 Sep 2021 00:40:01 -0700 (PDT)
> Jesper Taxbøl <jes...@taxboel.dk> wrote:
>
> > Hi,
> >
> > I am very new to emscriptem and I have been struggeling with porting a
> > function from c++ to javascript.
> >
> > I have created a public repo with my sample function, and I aim to
> > implement feedback into it.
> >
> > https://github.com/taxfromdk/wasm-glm-nlohmann
> >
> > My overall mission is that I have some vector mathematics, that I would
> > like to implement once and use on c++ and javascript platforms. I am using
> > GLM as the vector-math-backbone, and I am under the impression that json is
> > a good compromise for the languages involved. I have therefore chosen
> > nlohmann's json library.
> >
>
> I prepared a fixed version here:
>
> https://github.com/taxfromdk/wasm-glm-nlohmann/compare/master...shlomif:shlomif1?expand=1
>
> hope that helps.
>

Some additional notes:

1. The active file is compute-tweaked.html.

2. Your source files had some trailing whitespaces and mixed "\t"-and-space
indentation:

https://github.com/shlomif/Freenode-programming-channel-FAQ/blob/master/FAQ_with_ToC__generated.md#what-are-some-best-practices-in-programming-that-i-should-adopt

-- Shlomi
Perl Humour - https://perl-begin.org/humour/

ANSI C - There is usually one way to do it, but there’s more than one way to
optimise it.
https://www.shlomifish.org/humour/ways_to_do_it.html

Jesper Taxbøl

unread,
Sep 15, 2021, 12:46:43 PM9/15/21
to emscripten-discuss
Thank you!!! Shlomi

I have just merged. (My first merge by the way)

This far exceed what I could expect from a forum post.

Thank you very much, you have just made my evening, as I have spent two full evenings with no success, and after your hint you basically solved my problem.

I need to learn some of the more advanced javascript, but I feel I can continue from here.

Thank you again. :)

Jesper

Shlomi Fish

unread,
Sep 16, 2021, 1:06:01 AM9/16/21
to Jesper Taxbøl, emscripte...@googlegroups.com
Hi Jesper!

On Wed, 15 Sep 2021 09:46:42 -0700 (PDT)
Jesper Taxbøl <jes...@taxboel.dk> wrote:

> Thank you!!! Shlomi
>
> I have just merged. (My first merge by the way)
>
> This far exceed what I could expect from a forum post.
>
> Thank you very much, you have just made my evening, as I have spent two
> full evenings with no success, and after your hint you basically solved my
> problem.
>
> I need to learn some of the more advanced javascript, but I feel I can
> continue from here.
>
> Thank you again. :)
>

You're welcome. Also see
https://emscripten.org/docs/api_reference/preamble.js.html#UTF8ToString and
note that I think strings are immutable in JS.

> Jesper
>


--

Shlomi Fish https://www.shlomifish.org/
Let’s talk about restores instead of backups - https://is.gd/WatQqu

Tomorrow never dies, unless Chuck Norris volunteers to take it out of its
misery.
https://www.shlomifish.org/humour/bits/facts/Chuck-Norris/
Reply all
Reply to author
Forward
0 new messages