11 #include <fmt/ostream.h> 13 #include <libplatform/libplatform.h> 21 #if defined __has_include 22 #if __has_include(<cxxabi.h>) 23 # define V8TOOLKIT_DEMANGLE_NAMES 31 template<
class MemberT,
class ClassT>
33 return std::is_const_v<MemberT>;
73 v8::Isolate * isolate = v8::Isolate::GetCurrent();
74 return v8::String::NewFromUtf8(isolate,
string);
77 v8::Isolate * isolate = v8::Isolate::GetCurrent();
78 return v8::Number::New(isolate, number);
81 v8::Isolate * isolate = v8::Isolate::GetCurrent();
82 return v8::Integer::New(isolate, number);
89 template<
class ReturnType,
class...
Args,
class... Ts>
91 const v8::FunctionCallbackInfo<v8::Value> & info,
92 Ts&&... ts) -> ReturnType {
93 return function(std::forward<Args>(ts)...);
97 template<
class ReturnType,
class...
Args,
class Callable,
class... Ts>
99 const v8::FunctionCallbackInfo<v8::Value> & info,
100 Ts&&... ts) -> ReturnType {
101 return callable(std::forward<Args>(ts)...);
113 template<
class... Ts>
115 virtual const char *
what() const noexcept
override {
return reason.c_str();}
118 template<
typename T,
typename =
void>
131 #define V8TOOLKIT_COMMA , 134 #define V8TOOLKIT_MACRO_TYPE(...) __VA_ARGS__ 147 std::atomic<bool> cache_set =
false;
152 static std::mutex mutex;
154 std::lock_guard<std::mutex> lock_guard(mutex);
157 std::string constness = std::is_const<T>::value ?
"const " :
"";
158 std::string volatility = std::is_volatile<T>::value ?
"volatile " :
"";
159 cached_name = constness + volatility + demangled_name;
167 template<class Destination, class Source, std::enable_if_t<std::is_polymorphic<Source>::value,
int> = 0>
169 static_assert(std::is_pointer<Destination>::value,
"must be a pointer type");
170 static_assert(!std::is_pointer<std::remove_pointer_t<Destination>>::value,
"must be a single pointer type");
172 return dynamic_cast<Destination
>(source);
177 template<
class Destination>
183 template<class Destination, class Source, std::enable_if_t<!std::is_polymorphic<Source>::value,
int> = 0>
185 static_assert(std::is_pointer<Destination>::value,
"must be a pointer type");
186 static_assert(!std::is_pointer<std::remove_pointer_t<Destination>>::value,
"must be a single pointer type");
192 #define SAFE_MOVE_CONSTRUCTOR_SFINAE !std::is_const<T>::value && std::is_move_constructible<T>::value 193 template<
class T, std::enable_if_t<SAFE_MOVE_CONSTRUCTOR_SFINAE,
int> = 0>
195 return std::make_unique<T>(
std::move(original));
197 template<
class T, std::enable_if_t<!(SAFE_MOVE_CONSTRUCTOR_SFINAE),
int> = 0>
200 return std::unique_ptr<T>();
205 template<
typename Test,
template<
typename...>
class Ref>
208 template<
template<
typename...>
class Ref,
typename...
Args>
216 template<
class R,
class...
Args>
217 static auto go(R(T::*)(
Args...)const)->func::function<R(
Args...)>;
219 template<
class R,
class...
Args>
220 static auto go(R(T::*)(
Args...))->func::function<R(
Args...)>;
222 template<
class R,
class...
Args>
223 static auto go(R(T::*)(
Args...)const &)->func::function<R(
Args...)>;
225 template<
class R,
class...
Args>
226 static auto go(R(T::*)(
Args...) &)->func::function<R(
Args...)>;
232 template<
class R,
class...
Args>
233 static auto go(R(T::*)(
Args...)const &&)->func::function<R(
Args...)>;
235 template<
class R,
class...
Args>
236 static auto go(R(T::*)(
Args...) &&)->func::function<R(
Args...)>;
242 template <
class... >
struct TypeList {};
245 template <
class R,
class... Ts>
249 template <
class R,
class Head,
class... Tail>
253 template <
class... Ts>
256 template <
class... Ts>
258 return std::tuple<Ts...>(std::forward<Ts>(ts)...);
264 template<
bool... tail>
267 template<
bool... tail>
278 template <
bool... tail>
282 template <
bool... tail>
298 virtual const char *
what() const noexcept
override {
return message.c_str();}
311 virtual const char *
what() const noexcept
override {
return message.c_str();}
321 virtual const char *
what() const noexcept
override {
return message.c_str();}
340 bool own_properties_only =
true);
348 template<
class Callable>
351 if (value->IsArray()) {
354 for (
int i = 0; i < length; i++) {
355 callable(array->Get(context, i).ToLocalChecked());
401 auto own_properties =
object->GetOwnPropertyNames(context).ToLocalChecked();
403 auto property_value =
object->Get(context, property_name);
405 callable(property_name, property_value.ToLocalChecked());
416 template<
class Container,
426 template<
template <
typename,
typename...>
class Container,
432 using Results = Container<typename std::result_of<Callable(Data)>::type>;
436 for (
auto && element : container) {
438 results.push_back(callable(std::forward<decltype(element)>(element)));
457 using ResultPair =
typename std::result_of<Callable(typename Source::value_type)>::type;
458 using Results = std::map<typename ResultPair::T1, typename ResultPair::T2>;
464 for (
auto && element : container) {
465 results.insert(callable(std::forward<decltype(element)>(element)));
478 template <
class Container,
class Callable>
485 template <
class Callable,
487 auto reducer(
const Container & container, Callable callable) ->
488 std::vector<typename std::result_of<Callable(typename Container::value_type)>::type>
490 using ResultType =
typename std::result_of<Callable(typename Container::value_type)>::type;
491 std::vector<ResultType> results;
492 for(
auto && pair : container) {
493 results.push_back(callable(std::forward<decltype(pair)>(pair)));
509 #define ANYBASE_DEBUG 518 #define ANYBASE_PRINT(format_string, ...) \ 519 if (AnybaseDebugPrintFlag) { \ 520 std::cerr << fmt::format(format_string, ##__VA_ARGS__) << std::endl; \ 523 #define ANYBASE_PRINT(format_string, ...) 545 template<
class T,
class =
void>
550 struct AnyPtr<T,
std::enable_if_t<!std::is_pointer<T>::value && !std::is_reference<T>::value>> :
public AnyBase {
554 T *
get() {
return data;}
572 static std::unique_ptr<Stuff<T>>
stuffer(T && t) {
return std::make_unique<Stuff<T>>(
std::move(t));}
573 static std::unique_ptr<Stuff<T>>
stuffer(T
const & t) {
return std::make_unique<Stuff<T>>(
std::move(const_cast<T &>(t)));}
575 T *
get(){
return stuffed.get();}
589 T
get() {
return data;}
596 if (std::is_same<T, v8::Function>::value) {
597 valid = value->IsFunction();
598 }
else if (std::is_same<T, v8::Object>::value) {
599 valid = value->IsObject();
600 }
else if (std::is_same<T, v8::Array>::value) {
601 valid = value->IsArray();
602 }
else if (std::is_same<T, v8::String>::value) {
603 valid = value->IsString();
604 }
else if (std::is_same<T, v8::Boolean>::value) {
605 valid = value->IsBoolean();
606 }
else if (std::is_same<T, v8::Number>::value) {
607 valid = value->IsNumber();
608 }
else if (std::is_same<T, v8::Value>::value) {
627 return get_value_as<T>(value.Get(isolate));
636 auto isolate = context->GetIsolate();
638 auto get_maybe =
object->Get(context, v8::String::NewFromUtf8(isolate, key.c_str()));
640 if(get_maybe.IsEmpty() || get_maybe.ToLocalChecked()->IsUndefined()) {
648 return get_value_as<T>(get_maybe.ToLocalChecked());
655 return get_key_as<T>(context, get_value_as<v8::Object>(object), key);
670 bool show_all_properties=
false,
684 if (!value->IsObject()) {
686 throw CastException(fmt::format(
"Value sent to CastToNative<{} &&> wasn't a JavaScript object, it was: '{}'", class_name, *v8::String::Utf8Value(value)));
689 auto object = value->ToObject();
691 if (object->InternalFieldCount() == 0) {
692 throw CastException(fmt::format(
"Object sent to CastToNative<{} &&> wasn't a wrapped native object, it was a pure JavaScript object: '{}'", class_name, *v8::String::Utf8Value(value)));
bool_constant< true > true_type
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
bool_constant< false > false_type
bool Value(const T &value, M matcher)
internal::KeyMatcher< M > Key(M inner_matcher)
const T & move(const T &t)
internal::RefMatcher< T & > Ref(T &x)