That approach should work, but your code has a simple bug that isn't WASM
specific:
void* EMSCRIPTEN_KEEPALIVE InitializeLibrary(){
SomeStruct s;
s.k = 18;
s.t = 21;
return (void*)&s;
}
You're returning the address of a stack variable (SomeStruct s). When your
function returns that struct will be gone and the pointer you return will
point to some random address which previously was occupied by "SomeStruct
s" (e.g. the pointer is "dangling" now).
You need to make 's' outlive the function, either by allocating via malloc,
or turning it into a global variable via 'static' or moving the struct out
of the function into the "global scope".
On Monday, 14 September 2020 12:51:24 UTC+2, CebWebXer wrote:
>
> I had successfuly, built a 3rd party library in webasm and I am trying to
> test it.
> however I am stuck on how to proceed as I cant find documentation or
> tutorial related to it.
>
> the library I ported is initialized by calling a Create() function and
> returns a struct pointer like a context, and this struct pointer is passed
> everytime a function on that libraryy is called.
>
> I am confuse on how to handle this in webasm as the returning struct
> pointer is neither used outside of the library but just recieved and passed
> thru.
> I have tried settingthe return value as 'number' in the call to initialize
> method and just pass back these number to the next method but to no lock.
>
> here is a sample mini code I made
>
> C++
> void* EMSCRIPTEN_KEEPALIVE InitializeLibrary(){
> SomeStruct s;
> s.k = 18;
> s.t = 21;
> return (void*)&s;
> }
>
> int EMSCRIPTEN_KEEPALIVE Add(void* b) {
> SomeStruct* bg = (SomeStruct*)b;
> return bg->k + bg->t;
> }
>
> and in Javascript/nodejs side
>
> const ptr = Module.ccall("InitializeLibrary", 'number', null);
> const result = Module.ccall("Add", 'number', ['number'],[ptr]);
> console.log(result);
>
> to no avail,
> How to effectively approach this?
>
> I have yet to try (and will try next), creating a C/C++ adapter that will
> serve as the middleman between the library and JS instead and keep the
> pointer/context in there, but im not sure if its possible.
>
> Any help is appreaciated.
>
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/emscripten-discuss/ff149fb5-7537-41db-9e72-90998117afa7o%40googlegroups.com.