pmed / v8pp

Bind C++ functions and classes into V8 JavaScript engine
http://pmed.github.io/v8pp/
Other
886 stars 118 forks source link

Customization of std::reference_wrapper and std::optional #186

Closed deadlocklogic closed 1 year ago

deadlocklogic commented 1 year ago

Hi, why not considering a customization for std::reference_wrapper and std::optional?

Is there a mechanism to pass std::vector as reference and avoid copies? Edit: I should use v8::External. But still why not providing an automatic way of doing so with all necessary accessors?

uniqss commented 1 year ago

Could you please show the code you are using that works with the v8::External to show what you want v8pp to do to avoid the copy of std::vector? v8's memory management is very hard to handle, it have its own memory, and it's very hard to get memory out or pass memory pointer or reference in. Every operation with v8 is copy, copy, copy. I have tested many functions using v8pp, and have a conclusion currently:

function js call c++ c++ call js
return/param int64 1 1
return/param pointer of int64 x x
return/param reference of int64 const & both & / const & ok
return/param uint64 1 1
return/param pointer of uint64 x x
return/param reference of uint64 const & both & / const & ok
return/param int 1 1
return/param pointer of int x x
return/param reference of int only const int& both & / const & ok
return/param string 1 1
return/param pointer of string x x
return/param reference of string only const string& / const char* both & / const & ok
return/param user type 1 stuck... usertype cannot be used as param/return, only tuple can
return/param pointer of user type 1
return/param reference of user type 1
return/param constreference of user type 1
return/param array of int 1
return/param consts reference of array of int 1
return/param array of string 1
return/param consts reference of array of string 1
return/param array of user type 1
return/param consts reference of array of user type 1
return/param set of xxx new Set not allowed, use Map<xxx, bool>
return/param map of int => int 1
return/param consts reference of map of int => int 1
return/param map of int => string 1
return/param consts reference of map of int => string 1
return/param map of string => int 1
return/param consts reference of map of string => int 1
return/param map of string => string 1
return/param consts reference of map of string => string 1
return/param map of int => user type 1
return/param consts reference of map of int => user type 1
return/param map of string => user type 1
return/param consts reference of map of string => user type 1

And now I'm stuck with user_type when calling a js function from c++, both using user_type as param and return value.

pmed commented 1 year ago

Hi, why not considering a customization for std::reference_wrapper and std::optional?

Is there a mechanism to pass std::vector as reference and avoid copies? Edit: I should use v8::External. But still why not providing an automatic way of doing so with all necessary accessors?

Hi,

I will glad to review and accept a pull request that implements it!