Skip to content

Commit d7ed5a3

Browse files
committedJul 21, 2019
convert return value of wrapped C++ function with respect to class ptr_traits
A wrapped function may return a wrapped C++ object, so we need to use appropriate converter specialization (e.g. `convert<std::shared_ptr<T>`) for such a wrapped class `T`. Reusing `call_from_v8_traits<F>::arg_converter<return_type, Traits>` meta-function to get the converter type in `forward_ret()` for the certain function result type. Extracted `call_from_v8_traits<F>::arg_converter<Arg, Traits>` to use a type instead of argument index. Added optional `Traits = raw_ptr_traits` template argument to `wrap_function()` and `wrap_function_template()` in order to allow `forward_function()` instantiation for the specified `Traits` Work in context of issue #113
1 parent 6af1f0f commit d7ed5a3

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed
 

‎v8pp/call_from_v8.hpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,11 @@ struct call_from_v8_traits
3939
using type = void;
4040
};
4141

42-
template<size_t Index>
43-
using arg_type = typename tuple_element<Index + is_mem_fun,
44-
Index < (arg_count + Offset)>::type;
45-
46-
template<size_t Index, typename Traits, typename Arg = arg_type<Index>,
42+
template<typename Arg, typename Traits,
4743
typename T = typename std::remove_reference<Arg>::type,
4844
typename U = typename std::remove_pointer<T>::type
4945
>
50-
using arg_convert = typename std::conditional<
46+
using arg_converter = typename std::conditional<
5147
is_wrapped_class<typename std::remove_cv<U>::type>::value,
5248
typename std::conditional<
5349
std::is_pointer<T>::value,
@@ -57,6 +53,13 @@ struct call_from_v8_traits
5753
convert<typename std::remove_cv<T>::type>
5854
>::type;
5955

56+
template<size_t Index>
57+
using arg_type = typename tuple_element<Index + is_mem_fun,
58+
Index < (arg_count + Offset)>::type;
59+
60+
template<size_t Index, typename Traits>
61+
using arg_convert = arg_converter<arg_type<Index>, Traits>;
62+
6063
template<size_t Index, typename Traits>
6164
static decltype(arg_convert<Index, Traits>::from_v8(std::declval<v8::Isolate*>(), std::declval<v8::Local<v8::Value>>()))
6265
arg_from_v8(v8::FunctionCallbackInfo<v8::Value> const& args)

‎v8pp/function.hpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ void forward_ret(v8::FunctionCallbackInfo<v8::Value> const& args, std::true_type
153153
template<typename Traits, typename F>
154154
void forward_ret(v8::FunctionCallbackInfo<v8::Value> const& args, std::false_type /*is_void_return*/)
155155
{
156-
args.GetReturnValue().Set(to_v8(args.GetIsolate(),
156+
using return_type = typename function_traits<F>::return_type;
157+
using converter = call_from_v8_traits<F>::arg_converter<return_type, Traits>;
158+
args.GetReturnValue().Set(converter::to_v8(args.GetIsolate(),
157159
invoke<Traits, F>(args, std::is_member_function_pointer<F>())));
158160
}
159161

@@ -179,25 +181,25 @@ void forward_function(v8::FunctionCallbackInfo<v8::Value> const& args)
179181
} // namespace detail
180182

181183
/// Wrap C++ function into new V8 function template
182-
template<typename F>
184+
template<typename F, typename Traits = raw_ptr_traits>
183185
v8::Local<v8::FunctionTemplate> wrap_function_template(v8::Isolate* isolate, F&& func)
184186
{
185187
using F_type = typename std::decay<F>::type;
186188
return v8::FunctionTemplate::New(isolate,
187-
&detail::forward_function<raw_ptr_traits, F_type>,
189+
&detail::forward_function<Traits, F_type>,
188190
detail::set_external_data(isolate, std::forward<F_type>(func)));
189191
}
190192

191193
/// Wrap C++ function into new V8 function
192194
/// Set nullptr or empty string for name
193195
/// to make the function anonymous
194-
template<typename F>
196+
template<typename F, typename Traits = raw_ptr_traits>
195197
v8::Local<v8::Function> wrap_function(v8::Isolate* isolate,
196-
char const* name, F&& func)
198+
char const* name, F && func)
197199
{
198200
using F_type = typename std::decay<F>::type;
199201
v8::Local<v8::Function> fn = v8::Function::New(isolate->GetCurrentContext(),
200-
&detail::forward_function<raw_ptr_traits, F_type>,
202+
&detail::forward_function<Traits, F_type>,
201203
detail::set_external_data(isolate, std::forward<F_type>(func))).ToLocalChecked();
202204
if (name && *name)
203205
{

0 commit comments

Comments
 (0)
Please sign in to comment.