v8toolkit  0.0.1
Utility library for embedding V8 Javascript engine in a c++ program
unspecified_parameter_value.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <type_traits>
5 #include <tuple>
6 
7 
8 #include <v8.h>
9 
10 #include "v8helpers.h"
11 #include "casts.hpp"
12 
13 namespace v8toolkit {
14 
15 
16 template <class T, class = void>
18  std::result_of_t<CastToNative<T>(v8::Isolate *, v8::Local<v8::Value>)> operator()(const v8::FunctionCallbackInfo<v8::Value> & info, int i) const {
19  std::cerr << fmt::format("") << std::endl;
20  throw InvalidCallException(fmt::format("Not enough javascript parameters for function call - "
21  "requires {} but only {} were specified, missing {}",
22  i+1, info.Length(), demangle<T>()));
23  }
24 };
25 
26 
27 // if the CastToNative is callable with only an isolate, no v8::Value
28 template <class T>
29 struct cast_to_native_no_value<T, std::enable_if_t<std::result_of_t<CastToNative<T>()>::value>> {
30 
31  T && operator()(const v8::FunctionCallbackInfo<v8::Value> & info, int i) const {
32  return CastToNative<T>()();
33  }
34 };
35 
36 
37 template<class T, int default_arg_position = -1, class DefaultArgsTuple = std::tuple<>>
38 T * get_default_parameter(const v8::FunctionCallbackInfo<v8::Value> & info, int & i, std::vector<std::unique_ptr<StuffBase>> & stuff,
39  DefaultArgsTuple && default_args_tuple) {
40 
41  // prioritize the default_args_tuple value if available
42  if constexpr(default_arg_position >= 0) {
43  return &std::get<default_arg_position>(default_args_tuple);
44  } else if constexpr(cast_to_native_supports_default_value_v<T>) {
45  stuff.push_back(Stuff<T>::stuffer(CastToNative<T>()(info.GetIsolate())));
46  return static_cast<Stuff<T>&>(stuff.back()).get();
47  } else {
48  throw CastException("No default value available for type {}", demangle<T>());
49  }
50 };
51 
52 //
53 //// Helper function for when a required parameter isn't specified in javascript but may have a "global" default value for the type
54 //template <int default_arg_position = -1, class NoRefT, class DefaultArgsTuple = std::tuple<>>
55 //NoRefT & set_unspecified_parameter_value(const v8::FunctionCallbackInfo<v8::Value> & info, int & i, std::vector<std::unique_ptr<StuffBase>> & stuff,
56 // DefaultArgsTuple && default_args_tuple)
57 //{
58 // throw CastException("No function-declared default value or 'global' default value for type: {}", demangle<NoRefT>());
59 //}
60 //
61 //
62 //// Helper function for when a required parameter isn't specified in javascript but may have a "global" default value for the type
63 //template <int default_arg_position = -1, class NoRefT, class DefaultArgsTuple = std::tuple<>>
64 //std::enable_if_t<default_arg_position < 0,
65 // std::result_of_t<cast_to_native_no_value<NoRefT>(const v8::FunctionCallbackInfo<v8::Value> &, int)> &>
66 //
67 //set_unspecified_parameter_value(const v8::FunctionCallbackInfo<v8::Value> & info, int & i, std::vector<std::unique_ptr<StuffBase>> & stuff,
68 // DefaultArgsTuple && default_args_tuple) {
69 //
70 // // for string types, the result returned may be a unique_ptr in order to save memory for it, this is that type
71 // using ResultT = std::remove_reference_t<decltype(cast_to_native_no_value<NoRefT>()(info, i++))>;
72 // stuff.emplace_back(std::make_unique<Stuff<ResultT>>(std::move(cast_to_native_no_value<NoRefT>()(info, i++))));
73 //
74 // // stuff.back() returns a unique_ptr<StuffBase>
75 // return *((static_cast<Stuff<ResultT> *>(stuff.back().get()))->get());
76 //}
77 //
78 //
79 //// Helper function for when a required parameter isn't specified in javascript but has a function-specific default value specified for it
80 //template <int default_arg_position = -1, class NoRefT, class DefaultArgsTuple = std::tuple<>>
81 //
82 //std::enable_if_t<
83 // (default_arg_position >= 0),
84 //std::result_of_t<cast_to_native_no_value<NoRefT>(const v8::FunctionCallbackInfo<v8::Value> &, int)> &>
85 //
86 //set_unspecified_parameter_value(const v8::FunctionCallbackInfo<v8::Value> & info,
87 // int & i,
88 // std::vector<std::unique_ptr<StuffBase>> & stuff,
89 // DefaultArgsTuple && default_args_tuple) {
90 //
91 // // Gets the type returned by CastToNative - it's not always the same type as requested
92 // using ResultT = std::remove_reference_t<decltype(cast_to_native_no_value<NoRefT>()(info, i++))>;
93 //
94 // // This should be genealized to not be hardcoded as to what types have a different CastToNative return type
95 // if constexpr(std::is_same_v<NoRefT, char*> || std::is_same_v<NoRefT, char const *>) {
96 // // get the value out of the default value tuple
97 //
98 //
99 // char const * string = std::get<(std::size_t) default_arg_position>(std::move(default_args_tuple));
100 // int length = strlen(string);
101 // std::unique_ptr<char[]> unique_ptr(new char[length + 1]);
102 // strcpy(unique_ptr.get(), string);
103 //
104 // stuff.emplace_back(
105 // std::make_unique<Stuff < ResultT>>(std::unique_ptr<char[]>(std::move(unique_ptr))));
106 // } else {
107 // static_assert(std::is_same_v<ResultT, std::remove_reference_t<NoRefT>>,
108 // "Unexpected (i.e. not char*) situation where CastToNative doesn't return same type as requested ");
109 // // get the value out of the default value tuple
110 // stuff.emplace_back(
111 // std::make_unique<Stuff<ResultT>>
112 // (std::get<(std::size_t) default_arg_position>(std::move(default_args_tuple)))
113 // );
114 // }
115 //
116 // return *static_cast<Stuff<ResultT> *>(stuff.back().get())->get();
117 //
118 //// // converting the value to a javascript value and then back is an ugly hack to deal with some parameter types changing
119 //// // types when being returned in order to store the memory somewhere it will be cleaned up (like char*)
120 //// auto temporary_js_value = v8toolkit::CastToJS<NoRefT>()(info.GetIsolate(), std::get<(std::size_t)default_arg_position>(std::move(default_args_tuple)));
121 //// stuff.emplace_back(std::make_unique<Stuff<ResultT>>(CastToNative<NoRefT>()(info.GetIsolate(), temporary_js_value)));
122 ////
123 //// return *((static_cast<Stuff<ResultT> *>(stuff.back().get()))->get());
124 //}
125 
126 } // v8toolkit
STL namespace.
T * get_default_parameter(const v8::FunctionCallbackInfo< v8::Value > &info, int &i, std::vector< std::unique_ptr< StuffBase >> &stuff, DefaultArgsTuple &&default_args_tuple)