-
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
problem with pointers #69
Comments
The library has no conversion support for primitive pointer types (e.g. Sure, one would add such a converter: // helper meta function
template<typename T>
using is_character_type = std::integral_constant<bool,
std::is_same<T, char>::value ||
std::is_same<T, char16_t>::value ||
std::is_same<T, wchar_t>::value>;
// converter for non-const T* that is not wrapped and not character type
template<typename T>
struct convert<T*, typename std::enable_if<
!is_wrapped_class<T>::value &&
!is_character_type<T>::value &&
!std::is_const<T>::value>::type>
{
using from_type = T*;
//using to_type = decltype(convert<T>::to_v8(std::declval<v8::Isolate*>(), std::declval<T>()));
using to_type = v8::Local<v8::Value>;
static bool is_valid(v8::Isolate*, v8::Local<v8::Value> value)
{
return true;
}
static from_type from_v8(v8::Isolate* isolate, v8::Local<v8::Value> value)
{
if (value.IsEmpty() || value->IsNullOrUndefined())
{
return nullptr;
}
return &(convert<T>::from_v8(isolate, value)); // (1)
}
static to_type to_v8(v8::Isolate* isolate, T* value)
{
if (!value) return v8::Null(isolate); // (2)
else return convert<T>::to_v8(isolate, *value);
}
}; But there are 2 issues in this converter:
In my opinion, usage of |
Sorry for my late response if it is too late. I completely agree with you, that retuning I didnt talk about exactly providing a way to deal with raw pointers inside javascrpt. Just to do in javascript something like (in a context of optionality: "null or a value") this:
whereas in C++ I have an external API like this:
I know, according to v8pp documentation, that there is a way to wrap So, may be it would be better to ask, "if there in v8pp a convertion of an About the lines (1) and (2) you wrote: (1)
I'm not sure about exact index, but in my opinion, conceptually this is the right direction to solve the optionality problem. (2) For the most cases that is assumed, that T* is the pointer to an object of T the type (struct or a class). Unfortunately for primitives (such as int*, for example) the tiny workaround required:
And so, if T* == int*, and at the (2) line Remark: Is there all clear in my reasoning, or I've missed somthing important (or have a mistake)? |
Well, this |
Ок. This is not exactly what I would like to know, but it is a fact, so I have nothing to do than to close this issue, because of impossibility to solve it in a convenient way. You spent a time during answer for my questions, so thank you not for a solution, but for your answers. |
In the most cases pointers are converted to an array/vector/string (e.t.c), but sometimes that is wrong behavior and I have found no way to workaround it. It is so, when pointers are used as an optional references (when null-value is a possible; as something like references in a Java or a C#, for example).
Following pseudocode snippet shows what am I talking about:
For class WithOptionalData,
const T*
means, that it mostly returns data, but "null-value" is possible.Using
std::optional
instead ofT*
sometimes impossible or inefficient:In fact, with class WithOptionalData, while using:
compile time error occur with v8pp::to_v8, deep inside in v8pp library:
forward_ret
(second overload)r_property_impl<Get, Set, true>::get_impl
(first overload)Maybe I am doing wrong something, but there is no documented way to deal with methods, that returns pointers to requested values (when pointer means "optional", not an array).
Other (second) problem with pointers looks like this:
While binding, using
v8pp::class_
:It looks like that code above should work, but it doesnt. Despite that
getMatrix
returns an array and it is a correct behavior (and v8pp can deal with arrays), something goes wrong and it looks like thatconst float*
processed as a special kind of a string (where character represented as a float-value). Evidently, that it is a constant pointer to the internal array, not a some kind of string. But deeply inside the library,const float *
is processed as a string, not an array of float, that is wrong and leads to compile time error deep inside v8pp, somewhere with to_v8.Although there is a way to workaround this (second problem), using static functions/lamdas, like
but it is unusual and inconvenient way to do such things.
Anyway, is there a way to deal with pointers using v8pp, when:
1)Pointer points to an optional data (when data or nullptr is returned)
2)Pointer is a const primitive pointer to an array and it looks like that it is processed as a some kind of string (instead of a correct behavior, when it is processed as an array/vector, like in example with a
const float*
)?
Thank you in advance for your answer.
The text was updated successfully, but these errors were encountered: