Hi Eric,

On Wed, Apr 23, 2014 at 7:07 PM, Eric.Yang <[email protected]> wrote:

> Hi all,
>
> I have a class called Security and it is used for data encryption.
>
> I want to use embind to export functions for js.
>
> class Security {
> public:
> Security ();
> bool encryptData(int encryptType, char * data, int dataLen);
> bool decryptData(int encryptType, char * data, int dataLen);
> }
>

Are 'data' and 'dataLen' in encryptData and decryptData input or output
parameters?  If input parameters, perhaps you should mark 'data' as const
char*.  But that's tangential to your question...

The easiest way to get a binary blob of data from JavaScript into C++ is to
pass a std::string, which is simply a typedef of std::basic_string<char>.
std::string is roughly equivalent to a std::vector<char>, so it's commonly
used as a blob of binary data.

Thus, I would recommend writing your bindings as such:

bool encryptDataWrapper(Security& this_, int encryptType, const
std::string& data) {
    return this_.encryptData(encryptType, data.data(), data.size());
}

bool decryptDataWrapper(Security& this_, int encryptType, const
std::string& data) {
    return this_.decryptData(encryptType, data.data(), data.size());
}

EMSCRIPTEN_BINDINGS(security) {
    class_<Security>("Security")
        .constructor<>()
        .function("encryptData", &encryptDataWrapper)
        .function("decryptData", &decryptDataWrapper)
        ;
}

2. How do I use encrypData/decrypData in JS ?
>      var data = "dataString";
>      var sec =  new Module.Security();
>      sec.encrypData(1,   data,   data.length);
>

With the bindings above, you could write:

var data = "dataString";
var sec = new Module.Security();
sec.encryptData(1, data);

Does that help?

-- 
Chad Austin
Technical Director, IMVU
http://engineering.imvu.com <http://www.imvu.com/members/Chad/>
http://chadaustin.me

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

Reply via email to