Discussion:
embind char point js usage
Eric.Yang
2014-04-24 02:07:48 UTC
Permalink
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);
}

EMSCRIPTEN_BINDING(test) {
class_<Security>("Security")
.constructor<>()
.function("encrypData",&CSecurity::encrypData,allow_raw_pointer<arg<1>>())
.function("decrypData",&CSecurity::decrypData,allow_raw_pointer<arg<1>>())
}

1. Do i write correct binding function? ex: allow_raw_pointer

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


Thanks a lot.
--
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-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/d/optout.
Chad Austin
2014-04-24 05:14:25 UTC
Permalink
Hi Eric,
Post by Eric.Yang
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 {
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 ?
Post by Eric.Yang
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 emscripten-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/d/optout.
Eric.Yang
2014-04-25 07:05:58 UTC
Permalink
Hi Chad,

Thank you for your prompt reply. : D

The second argument(char * data)'s content will be changed directly after
the method(encryptData/decryptData) called.

So second "data" argument is not only INPUT but also OUTPUT.(the others
[encryptType, Len] are INPUT only)

In your relay, you give me a great idea to deal with char point by wrapping
it.

And I write a test case for testing the idea.
------------------------------------------------------------
#include <emscripten/bind.h>
#include <iostream>
using namespace emscripten;

void test(char *s) {
s[0]=0x64; // change to lower case 'd'
}

bool charWrap(std::string &s) {
test(&s[0]);
return true;
}

int main () {
std::string data = "Data";
std::cout << data << "-"<< data.length()<<"\n";
charWrap(data);
std::cout << data << "-"<< data.length()<<"\n";
return 0;
}
------------------------------------------------
I run this in nodeJS and It works by wrapping it.
*Execute result:*
Data-4
data-4

But when I use embind to export charWrap function,I get error.
EMSCRIPTEN_BINDINGS(test) {
function("charWrap",&charWrap);
}
---------------------------------------------
emscripten/system/include/emscripten/bind.h:271:25: error: non-const lvalue
reference to type
'basic_string<[3 * ...]>' cannot bind to a temporary of type
'basic_string<[3 * ...]>'
internal::BindingType<Args>::fromWireType(args)...
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
emscripten/system/include/emscripten/bind.h:306:78: note: in instantiation
of member function
'emscripten::internal::Invoker<bool, std::__1::basic_string<char,
std::__1::char_traits<char>,
std::__1::allocator<char> > &>::invoke' requested here
reinterpret_cast<GenericFunction>(&Invoker<ReturnType,
Args...>::invoke),

^
cc.cpp:23:2: note: in instantiation of function template specialization
'emscripten::function<bool,
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > &, >' requested
here
function("charWrap",&charWrap);
^
1 error generated.
ERROR root: compiler frontend failed to generate LLVM bitcode, halting
-------------------------------------
Any comments?

Thanks a lot again.
--
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-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/d/optout.
Chad Austin
2014-04-29 20:43:40 UTC
Permalink
embind doesn't support references to std::string (because the C++ program
can't modify the JavaScript heap, and JavaScript strings are immutable
anyway).

Can you instead wrap it with a function that takes an input std::string and
returns an output std::string?
Post by Eric.Yang
Hi Chad,
Thank you for your prompt reply. : D
The second argument(char * data)'s content will be changed directly after
the method(encryptData/decryptData) called.
So second "data" argument is not only INPUT but also OUTPUT.(the others
[encryptType, Len] are INPUT only)
In your relay, you give me a great idea to deal with char point by
wrapping it.
And I write a test case for testing the idea.
------------------------------------------------------------
#include <emscripten/bind.h>
#include <iostream>
using namespace emscripten;
void test(char *s) {
s[0]=0x64; // change to lower case 'd'
}
bool charWrap(std::string &s) {
test(&s[0]);
return true;
}
int main () {
std::string data = "Data";
std::cout << data << "-"<< data.length()<<"\n";
charWrap(data);
std::cout << data << "-"<< data.length()<<"\n";
return 0;
}
------------------------------------------------
I run this in nodeJS and It works by wrapping it.
*Execute result:*
Data-4
data-4
But when I use embind to export charWrap function,I get error.
EMSCRIPTEN_BINDINGS(test) {
function("charWrap",&charWrap);
}
---------------------------------------------
emscripten/system/include/emscripten/bind.h:271:25: error: non-const
lvalue reference to type
'basic_string<[3 * ...]>' cannot bind to a temporary of type
'basic_string<[3 * ...]>'
internal::BindingType<Args>::fromWireType(args)...
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
emscripten/system/include/emscripten/bind.h:306:78: note: in instantiation
of member function
'emscripten::internal::Invoker<bool, std::__1::basic_string<char,
std::__1::char_traits<char>,
std::__1::allocator<char> > &>::invoke' requested here
reinterpret_cast<GenericFunction>(&Invoker<ReturnType,
Args...>::invoke),
^
cc.cpp:23:2: note: in instantiation of function template specialization
'emscripten::function<bool,
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > &, >' requested
here
function("charWrap",&charWrap);
^
1 error generated.
ERROR root: compiler frontend failed to generate LLVM bitcode, halting
-------------------------------------
Any comments?
Thanks a lot again.
--
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
For more options, visit https://groups.google.com/d/optout.
--
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 emscripten-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/d/optout.
Eric.Yang
2014-04-30 02:02:27 UTC
Permalink
Hi Chad

I use a "string" wrapper to wrap "char pointer" and it works now.

You helped me a lot.

Thanks a lot again. : D

--------------------------------------------------------------
Sample code:

void test(char *s) {
s[0]=0x64; // change first char to lowercase 'd'
}

std::string wrapperTest (std::string s) {
test( *&s[0]* );
return s;
}

EMSCRIPTEN_BINDINGS(test) {
function("wrapperTest",&wrapperTest);
}


var s = "Data";
var t = Module.wrapperTest(s);
console.log(t); // 'data'
--
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-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/d/optout.
Loading...