Someone has an example of how to implement a C++ function called from JS with cwrap, whose return type is a Uint8Array array?

41 views
Skip to first unread message

Lorenzo Portillo

unread,
Jul 21, 2022, 5:03:57 PM7/21/22
to emscripten-discuss
Someone has an example of how to implement a C++ function called from JS with cwrap, whose return type is a Uint8Array?

J Decker

unread,
Jul 21, 2022, 8:02:24 PM7/21/22
to emscripten-discuss
what you might get instead is returning a pointer to some array; which is an integer, then using that integer to get a uint8array from the U8HEAP variable, using the integer as the starting offset, and whatever length you expect...

all pointers to the heap in C/C++ are really just offsets...

On Thu, Jul 21, 2022 at 5:04 PM Lorenzo Portillo <bikini...@gmail.com> wrote:
Someone has an example of how to implement a C++ function called from JS with cwrap, whose return type is a Uint8Array?

--
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/068d37a5-d7d8-41fb-8e9b-88f44d33d022n%40googlegroups.com.

Lorenzo Portillo

unread,
Jul 22, 2022, 2:53:28 AM7/22/22
to emscripte...@googlegroups.com
Thanks for the answer.

My code looks like this, but I get nothing on return.
Am I missing something?

C++
extern "C"
{
    unsigned int const* convertStringToAscii(char* text)
    {
        std::string str(text);
        unsigned int* ptr = (unsigned int*)malloc(str.size());
       
        for (int i = 0; i < str.size(); i++)
        {
            ptr[i] = int(text[i]);
        }
       
        return ptr;
    }
}

Javascript
convertStringToAscii = module.cwrap('convertStringToAscii', 'string', ['array'])
console.log(convertStringToAscii("test"))  => return empty

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/__5mEg_IlRc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/CAA2GJqWwiJamY6C4r-J2fRodkBXVAwFvTMnkSVNfEYLCGm2Rcg%40mail.gmail.com.

Sam Clegg

unread,
Jul 22, 2022, 1:32:42 PM7/22/22
to emscripte...@googlegroups.com
On Thu, Jul 21, 2022 at 11:53 PM Lorenzo Portillo <bikini...@gmail.com> wrote:
Thanks for the answer.

My code looks like this, but I get nothing on return.
Am I missing something?

C++
extern "C"
{
    unsigned int const* convertStringToAscii(char* text)
    {
        std::string str(text);
        unsigned int* ptr = (unsigned int*)malloc(str.size());
       
        for (int i = 0; i < str.size(); i++)
        {
            ptr[i] = int(text[i]);
        }
       
        return ptr;
    }
}

Javascript
convertStringToAscii = module.cwrap('convertStringToAscii', 'string', ['array'])
console.log(convertStringToAscii("test"))  => return empty

The second argument to cwrap is the is the return type and the third argument are the parameter types, so I think you want do some something like `module.cwrap('convertStringToAscii', 'number', ['string'])`. 

The return type of your function is a pointer which is just a number (an offset into theheap).


El vie, 22 jul 2022 a las 2:02, J Decker (<d3c...@gmail.com>) escribió:
what you might get instead is returning a pointer to some array; which is an integer, then using that integer to get a uint8array from the U8HEAP variable, using the integer as the starting offset, and whatever length you expect...

all pointers to the heap in C/C++ are really just offsets...

On Thu, Jul 21, 2022 at 5:04 PM Lorenzo Portillo <bikini...@gmail.com> wrote:
Someone has an example of how to implement a C++ function called from JS with cwrap, whose return type is a Uint8Array?

--
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/068d37a5-d7d8-41fb-8e9b-88f44d33d022n%40googlegroups.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/__5mEg_IlRc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/CAA2GJqWwiJamY6C4r-J2fRodkBXVAwFvTMnkSVNfEYLCGm2Rcg%40mail.gmail.com.

--
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.
Reply all
Reply to author
Forward
0 new messages