-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: function that returns object by value #39
Comments
Hi Jean-Claude, Sure, it's ok to ask questions here. There is also a gitter chat: https://gitter.im/pmed/v8pp for this project. The short answer: because When your But the result of To return a wrapped C++ object available in JavaScript, you have to explicitly state this fact with There are also So working static X& test2(v8::Isolate* isolate)
{
// create a wrapped C++ object
v8::Local<v8::Object> x = v8pp::class_<X>::create_object(isolate, true);
// return a reference to the object
return v8pp::from_v8<X>(isolate, x);
// another option: to create an object and import it to v8pp
/*
X* x = new X(true);
v8pp::class_<X>::import_external(isolate, x);
return *x;
*/
} |
Hi Pavel , Thanks a lot for the detailed explanation. It's very clear now what is going on. In the real code, functions with similar structure than
It is clear that when a C++ function returns an object by value, it has to be copied or moved into a heap allocated object, which the helper function does. The objects returned by the C++ functions are cheap to copy or move (they should be if they are returned by value). In our code there are a lot of function returning objects by value. Writing all these helper functions is going to be tedious. When binding a function that returns an object by value, I can't see another sensible behaviour for the binding. Given it's a temporary, it's certainly hasn't been wrapped in JavaScript yet. So the only option I see is to make a heap allocated copy that is owned by v8. I have the impression that v8pp could be modified to have this behaviour as default, rather than returning |
I think it's possible to add something similar to Call Policies from Boost.Python to setup how to a C++ function result would be converted. Indeed, this approach will require more of template magic, and yes, I will accept this patch. If you wrap simple C++ structures with no member function bound, you can use specializations of v8pp::convert template for such structures: https://github.com/pmed/v8pp/blob/master/docs/convert.md#user-defined-types (there is also another example in #31) |
I've also been running into functions that return temporaries and came to the same conclusion, they should be heap-copied and wrapped. So I gave it a shot at patching v8pp for this in a local branch: The actual changes here look surprisingly simple: @pmed Is it really that simple, of did I miss something in this implementation? |
@tim-janik, yes this implementation is possible, but I prefer to have an explicit way to specify the returned object should be copied. And it seems this implementation don't work when a C++ class has deleted copy constructor. |
Thanks for the response @pmed. That leaves me with two questions:
|
Hi,
First, I'm not sure if the issue tracker is the right way to ask questions about the project. I haven't seen a mailing list, so I guess it's ok?
Please consider the example based on 'wrapping.md':
Executing
module.X(true)
gives[object X]
.Executing
module.test1()
givestrue
.Executing
module.test2()
givesundefined
.Why is
test2
not returning a wrapped object of typeX
?The text was updated successfully, but these errors were encountered: