/**************************************************************************** Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd. http://www.cocos.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #pragma once #include #include #include #include #include "bindings/manual/jsb_conversions.h" #include "bindings/manual/jsb_global.h" #include "base/std/container/array.h" #include "base/std/container/string.h" #include "base/std/container/unordered_map.h" #include "base/std/container/vector.h" namespace sebind { struct ThisObject {}; using SeCallbackType = bool(se::State &); using SeCallbackFnPtr = bool (*)(se::State &); namespace intl { template struct FunctionExactor; template struct FunctionExactor { using type = R(ARGS...); using return_type = R; static std::function bind(const se::Value &fnVal) { std::function func = [=](ARGS... args) { se::AutoHandleScope scope; se::ValueArray jsArgs; jsArgs.resize(sizeof...(ARGS)); nativevalue_to_se_args_v(jsArgs, std::forward(args)...); se::Value rval; bool succ = fnVal.toObject()->call(jsArgs, nullptr, &rval); if constexpr (!std::is_void_v) { R result; sevalue_to_native(rval, &result, nullptr); return result; } }; return func; } static R call(se::Object *jsThisObject, const se::Value &fnVal, ARGS &&...args) { se::AutoHandleScope scope; se::ValueArray jsArgs; jsArgs.resize(sizeof...(ARGS)); nativevalue_to_se_args_v(jsArgs, std::forward(args)...); se::Value rval; bool succ = fnVal.toObject()->call(jsArgs, jsThisObject, &rval); if constexpr (!std::is_void_v) { R result; sevalue_to_native(rval, &result, jsThisObject); return result; } } }; template struct TypeList; template struct TypeList { constexpr static size_t COUNT = 1 + sizeof...(OTHERS); using head = T; using tail = TypeList; using tuple_type = std::tuple; using tuple_type_mutable = std::tuple::type>::type, typename std::remove_reference::type>::type...>; }; template <> struct TypeList<> { constexpr static size_t COUNT = 0; using head = void; using tail = TypeList<>; using tuple_type = std::tuple<>; using tuple_type_mutable = std::tuple<>; }; template struct Cons; template struct Cons> { using type = TypeList; }; template struct FunctionWrapper; template struct FunctionWrapper { using type = R (*)(C *, ARGS...); using return_type = R; using arg_list = TypeList; static constexpr size_t ARG_N = sizeof...(ARGS); template inline static R invoke(type func, C *self, ARGS2 &&...args) { return (*func)(self, std::forward(args)...); } }; template struct FunctionWrapper { using type = R (C::*)(ARGS...); using return_type = R; using arg_list = TypeList; static constexpr size_t ARG_N = sizeof...(ARGS); template inline static R invoke(type func, C *self, ARGS2 &&...args) { return (self->*func)(std::forward(args)...); } }; template struct FunctionWrapper { using type = R (C::*)(ARGS...) const; using return_type = R; using arg_list = TypeList; static constexpr size_t ARG_N = sizeof...(ARGS); template inline static R invoke(type func, C *self, ARGS2 &&...args) { return (self->*func)(std::forward(args)...); } }; template <> struct FunctionWrapper { using type = std::nullptr_t; using return_type = std::nullptr_t; using arg_list = TypeList<>; static constexpr size_t ARG_N = 0; template static void invoke(type /*func*/, C * /*self*/, ARGS &&.../*args*/) { } }; template struct StaticFunctionWrapper; template struct StaticFunctionWrapper { using type = R (*)(ARGS...); using return_type = R; using arg_list = TypeList; static constexpr size_t ARG_N = sizeof...(ARGS); template inline static R invoke(type func, ARGS2 &&...args) { return (*func)(std::forward(args)...); } }; template <> struct StaticFunctionWrapper { using type = std::nullptr_t; using return_type = void; using arg_list = TypeList<>; static constexpr size_t ARG_N = 0; template static void invoke(type /*func*/, ARGS &&.../*args*/) { } }; template struct IsConstructibleWithTypeList; template struct IsConstructibleWithTypeList> { // NOLINTNEXTLINE constexpr static bool value = std::is_constructible::value; }; template struct MapTypeListToTuple; template struct MapTypeListToTuple> { using tuple = std::tuple::value>...>; }; template struct IsThisObject { // NOLINTNEXTLINE constexpr static bool value = std::is_same::type>::type>::type>::value; }; template struct FilterThisObject; template struct FilterThisObject { using filtered_types = std::conditional_t::value, typename FilterThisObject::filtered_types, typename Cons::filtered_types>::type>; using mapped_types = typename Cons::value, se::Object *, T>, typename FilterThisObject::mapped_types>::type; }; template <> struct FilterThisObject<> { using filtered_types = TypeList<>; using mapped_types = TypeList<>; }; template struct MapArg { constexpr static size_t FROM = From; constexpr static size_t TO = To; // NOLINT constexpr static bool SKIP = Skip; }; template struct GenMapArgImpl { using list = typename Cons, typename GenMapArgImpl::list>::type; }; template struct GenMapArgImpl { using list = TypeList<>; }; template struct GenMapArg { using list = typename GenMapArgImpl<0, N>::list; }; template struct TypeListMapImpl { constexpr static bool SHOULD_SKIP = IsThisObject::value; constexpr static int NEXT_INCOMPLETE = SHOULD_SKIP ? IncompleteIdx : IncompleteIdx + 1; constexpr static int NEXT_INCOMPLETE_REMAIN = SHOULD_SKIP ? IncompleteRemain : IncompleteRemain - 1; using full_tail = typename FullTypeList::tail; using incomplete_tail = std::conditional_t; using map_value = MapArg; using map_list = typename Cons::map_list>::type; }; template struct TypeListMapImpl { using map_list = TypeList<>; }; template struct TypeListMapImpl { static_assert(FullRemain == FullTypeList::COUNT, "Parameter count incorrect!"); using map_list = typename GenMapArg::list; }; template struct TypeListMap { using map_list = typename TypeListMapImpl<0, 0, FullTypeList::COUNT, IncompleteTypeList::COUNT, FullTypeList, IncompleteTypeList>::map_list; }; template struct TypeMapping; template struct TypeMapping> { using declare_types = TypeList; using input_types = typename FilterThisObject::filtered_types; using result_types = typename FilterThisObject::mapped_types; using result_types_tuple = typename result_types::tuple_type; using result_types_tuple_mutable = typename result_types::tuple_type_mutable; using input_types_tuple = typename input_types::tuple_type; using mapping_list = typename TypeListMap::map_list; static constexpr size_t FULL_ARGN = sizeof...(ARGS); static constexpr size_t NEW_ARGN = input_types::COUNT; static constexpr bool NEED_REMAP = FULL_ARGN != NEW_ARGN; }; template struct TypeMapReturnSwitch; template struct TypeMapReturnSwitch { template static se::Object *select(se::Object *self, V & /*unused*/) { return self; } }; template struct TypeMapReturnSwitch { template static auto select(se::Object * /*unused*/, V &tuple) { return std::get(tuple).value(); } }; struct ArgumentFilter { template static auto forward(se::Object *self, Tuple &tuple) { constexpr static MapTuple TUPLE_VAL; using map_arg = std::remove_reference_t(TUPLE_VAL))>; return TypeMapReturnSwitch::select(self, tuple); } }; template ResultType mapTupleArguments(se::Object *self, TupleIn &input, std::index_sequence /*args*/) { using map_tuple = typename Mapping::mapping_list::tuple_type; using result_type = typename Mapping::result_types_tuple_mutable; static_assert(std::is_same::value, "result_type mismatch"); // if constexpr (std::tuple_size::value > 0) { return result_type(ArgumentFilter::forward(self, input)...); //} // return result_type(); } template struct BoolArrayAndAll { static constexpr bool cal(const ccstd::array &bools) { return bools[M - 1] && BoolArrayAndAll::cal(bools); } }; template struct BoolArrayAndAll<0, N> { static constexpr bool cal(const ccstd::array & /*unused*/) { return true; } }; template // NOLINTNEXTLINE bool convert_js_args_to_tuple(const se::ValueArray &jsArgs, std::tuple &args, se::Object *thisObj, std::index_sequence) { constexpr static size_t ARG_N = sizeof...(ARGS); ccstd::array all = {sevalue_to_native(jsArgs[indexes], &std::get(args).data, thisObj)...}; return BoolArrayAndAll::cal(all); } template // NOLINTNEXTLINE bool convert_js_args_to_tuple(const se::ValueArray &jsArgs, std::tuple<> &args, se::Object *ctx, std::index_sequence) { return true; } struct ConstructorBase { size_t argCount = 0; SeCallbackFnPtr bfnPtr{nullptr}; virtual bool construct(se::State &state) { if (bfnPtr) { return (*bfnPtr)(state); } return false; } }; struct InstanceMethodBase { ccstd::string className; ccstd::string methodName; size_t argCount = 0; SeCallbackFnPtr bfnPtr{nullptr}; virtual bool invoke(se::State &state) const { if (bfnPtr) { return (*bfnPtr)(state); } return false; } }; struct FinalizerBase { virtual void finalize(void * /*unused*/) {} }; struct InstanceFieldBase { ccstd::string className; ccstd::string attrName; SeCallbackFnPtr bfnSetPtr{nullptr}; SeCallbackFnPtr bfnGetPtr{nullptr}; virtual bool get(se::State &state) const { if (bfnGetPtr) return (*bfnGetPtr)(state); return false; } virtual bool set(se::State &state) const { if (bfnSetPtr) return (*bfnSetPtr)(state); return false; } }; struct InstanceAttributeBase { ccstd::string className; ccstd::string attrName; SeCallbackFnPtr bfnSetPtr{nullptr}; SeCallbackFnPtr bfnGetPtr{nullptr}; virtual bool get(se::State &state) const { if (bfnGetPtr) return (*bfnGetPtr)(state); return false; } virtual bool set(se::State &state) const { if (bfnSetPtr) return (*bfnSetPtr)(state); return false; } }; struct StaticMethodBase { ccstd::string className; ccstd::string methodName; size_t argCount = 0; SeCallbackFnPtr bfnPtr{nullptr}; virtual bool invoke(se::State &state) const { if (bfnPtr) return (*bfnPtr)(state); return false; } }; struct StaticAttributeBase { ccstd::string className; ccstd::string attrName; SeCallbackFnPtr bfnSetPtr{nullptr}; SeCallbackFnPtr bfnGetPtr{nullptr}; virtual bool get(se::State &state) const { if (bfnGetPtr) return (*bfnGetPtr)(state); return false; } virtual bool set(se::State &state) const { if (bfnSetPtr) return (*bfnSetPtr)(state); return false; } }; template struct Constructor; template struct InstanceField; template struct InstanceMethod; template struct InstanceAttribute; template struct StaticMethod; template struct StaticAttribute; template struct Constructor> : ConstructorBase { // no `if constexpr`, more code is needed... bool construct(se::State &state) override { using type_mapping = TypeMapping>; using args_holder_type = typename MapTypeListToTuple::tuple; constexpr auto indexes = std::make_index_sequence{}; se::PrivateObjectBase *self{nullptr}; se::Object *thisObj = state.thisObject(); args_holder_type args{}; const auto &jsArgs = state.args(); convert_js_args_to_tuple(jsArgs, args, thisObj, std::make_index_sequence()); if constexpr (type_mapping::NEED_REMAP) { using type_mapping = TypeMapping>; using map_list_type = typename type_mapping::mapping_list; using map_tuple_type = typename type_mapping::result_types_tuple_mutable; static_assert(map_list_type::COUNT == sizeof...(ARGS), "type mapping incorrect"); map_tuple_type remapArgs = mapTupleArguments(thisObj, args, std::make_index_sequence{}); self = constructWithTuple(remapArgs, indexes); } else { self = constructWithTupleValue(args, indexes); } state.thisObject()->setPrivateObject(self); return true; } template se::PrivateObjectBase *constructWithTuple(std::tuple &args, std::index_sequence /*unused*/) { return JSB_MAKE_PRIVATE_OBJECT(T, std::get(args)...); } template se::PrivateObjectBase *constructWithTupleValue(std::tuple &args, std::index_sequence /*unused*/) { return JSB_MAKE_PRIVATE_OBJECT(T, std::get(args).value()...); } }; template struct Constructor : ConstructorBase { using type = T *(*)(ARGS...); type func{nullptr}; bool construct(se::State &state) override { if ((sizeof...(ARGS)) != state.args().size()) { return false; } std::tuple::value>...> args{}; const auto &jsArgs = state.args(); se::Object *thisObj = state.thisObject(); convert_js_args_to_tuple(jsArgs, args, thisObj, std::make_index_sequence()); T *ptr = constructWithTuple(args, std::make_index_sequence()); state.thisObject()->setPrivateData(ptr); return true; } template T *constructWithTuple(std::tuple &args, std::index_sequence /*unused*/) { return (*func)(std::get(args).value()...); } }; template struct Finalizer : FinalizerBase { using type = void (*)(T *); using arg_type = T; type func{nullptr}; void finalize(void *ptr) override { (*func)(reinterpret_cast(ptr)); } }; template struct InstanceMethod : InstanceMethodBase { using type = R (T::*)(ARGS...); using return_type = R; using class_type = std::remove_cv_t; constexpr static size_t ARG_N = sizeof...(ARGS); constexpr static bool RETURN_VOID = std::is_same::value; type func{nullptr}; template R callWithTuple(T *self, std::tuple &args, std::index_sequence /*unused*/) const { return ((reinterpret_cast(self))->*func)(std::get(args).value()...); } bool invoke(se::State &state) const override { constexpr auto indexes{std::make_index_sequence()}; T *self = reinterpret_cast(state.nativeThisObject()); se::Object *thisObject = state.thisObject(); const auto &jsArgs = state.args(); if (ARG_N != jsArgs.size()) { SE_LOGE("incorret argument size %d, expect %d\n", static_cast(jsArgs.size()), static_cast(ARG_N)); return false; } std::tuple::value>...> args{}; convert_js_args_to_tuple(jsArgs, args, thisObject, indexes); if constexpr (RETURN_VOID) { callWithTuple(self, args, indexes); } else { nativevalue_to_se(callWithTuple(self, args, indexes), state.rval(), state.thisObject()); } return true; } }; template struct InstanceMethod : InstanceMethodBase { using type = R (T::*)(ARGS...) const; using return_type = R; using class_type = std::remove_cv_t; constexpr static size_t ARG_N = sizeof...(ARGS); constexpr static bool RETURN_VOID = std::is_same::value; type func{nullptr}; template R callWithTuple(T *self, std::tuple &args, std::index_sequence /*unused*/) const { return ((reinterpret_cast(self))->*func)(std::get(args).value()...); } bool invoke(se::State &state) const override { constexpr auto indexes{std::make_index_sequence()}; T *self = reinterpret_cast(state.nativeThisObject()); se::Object *thisObject = state.thisObject(); const auto &jsArgs = state.args(); if (ARG_N != jsArgs.size()) { SE_LOGE("incorret argument size %d, expect %d\n", static_cast(jsArgs.size()), static_cast(ARG_N)); return false; } std::tuple::value>...> args{}; convert_js_args_to_tuple(jsArgs, args, thisObject, indexes); if constexpr (RETURN_VOID) { callWithTuple(self, args, indexes); } else { nativevalue_to_se(callWithTuple(self, args, indexes), state.rval(), state.thisObject()); } return true; } }; template struct InstanceMethod : InstanceMethodBase { using type = R (*)(T *, ARGS...); using return_type = R; using class_type = std::remove_cv_t; constexpr static size_t ARG_N = sizeof...(ARGS); constexpr static bool RETURN_VOID = std::is_same::value; type func{nullptr}; template R callWithTuple(T *self, std::tuple &args, std::index_sequence /*unused*/) const { return (*func)(reinterpret_cast(self), std::get(args).value()...); } bool invoke(se::State &state) const override { constexpr auto indexes{std::make_index_sequence()}; T *self = reinterpret_cast(state.nativeThisObject()); se::Object *thisObject = state.thisObject(); const auto &jsArgs = state.args(); if (ARG_N != jsArgs.size()) { SE_LOGE("incorret argument size %d, expect %d\n", static_cast(jsArgs.size()), static_cast(ARG_N)); return false; } std::tuple::value>...> args{}; convert_js_args_to_tuple(jsArgs, args, thisObject, indexes); if constexpr (RETURN_VOID) { callWithTuple(self, args, indexes); } else { nativevalue_to_se(callWithTuple(self, args, indexes), state.rval(), state.thisObject()); } return true; } }; struct InstanceMethodOverloaded : InstanceMethodBase { ccstd::vector functions; bool invoke(se::State &state) const override { bool ret = false; auto argCount = state.args().size(); for (auto *method : functions) { if (method->argCount == -1 || method->argCount == argCount) { ret = method->invoke(state); if (ret) return true; } } return false; } }; template struct InstanceField : InstanceFieldBase { using type = F(T::*); using class_type = T; using return_type = std::remove_cv_t; type func{nullptr}; bool get(se::State &state) const override { T *self = reinterpret_cast(state.nativeThisObject()); se::Object *thisObject = state.thisObject(); return nativevalue_to_se((self->*func), state.rval(), thisObject); } bool set(se::State &state) const override { T *self = reinterpret_cast(state.nativeThisObject()); se::Object *thisObject = state.thisObject(); const auto &args = state.args(); return sevalue_to_native(args[0], &(self->*func), thisObject); } }; template struct AttributeAccessor; template struct AccessorGet { }; template struct AccessorSet { }; template <> struct AccessorGet { using class_type = std::nullptr_t; using type = std::nullptr_t; using return_type = std::nullptr_t; }; template <> struct AccessorSet { using class_type = std::nullptr_t; using type = std::nullptr_t; using value_type = std::nullptr_t; }; template struct AccessorGet { using class_type = T; using type = R (T::*)(); using return_type = R; static_assert(!std::is_void::value, "Getter should return a value!"); }; template struct AccessorSet { using class_type = T; using type = R (T::*)(F); using value_type = F; using ignored_return_type = R; }; template struct AccessorGet { using class_type = T; using type = R (T::*)() const; using return_type = R; static_assert(!std::is_void::value, "Getter should return a value"); }; template struct AccessorSet { using class_type = T; using type = R (*)(T *, F); using value_type = F; using ignored_return_type = R; }; template struct AccessorGet { using class_type = T; using type = R (*)(T *); using return_type = R; static_assert(!std::is_void::value, "Getter should return a value"); }; template struct InstanceAttribute> : InstanceAttributeBase { using type = T; using get_accessor = AccessorGet; using set_accessor = AccessorSet; using getter_type = typename get_accessor::type; using setter_type = typename set_accessor::type; using set_value_type = std::remove_reference_t>; using get_value_type = std::remove_reference_t>; using getter_class_type = typename get_accessor::class_type; using setter_class_type = typename set_accessor::class_type; constexpr static bool HAS_GETTER = !std::is_null_pointer::value; constexpr static bool HAS_SETTER = !std::is_null_pointer::value; constexpr static bool GETTER_IS_MEMBER_FN = HAS_GETTER && std::is_member_function_pointer::value; constexpr static bool SETTER_IS_MEMBER_FN = HAS_SETTER && std::is_member_function_pointer::value; static_assert(!HAS_GETTER || std::is_base_of::value, "Getter class type is not valid!"); static_assert(!HAS_SETTER || std::is_base_of::value, "Setter class type is not valid!"); setter_type setterPtr; getter_type getterPtr; bool get(se::State &state) const override { if constexpr (HAS_GETTER) { T *self = reinterpret_cast(state.nativeThisObject()); se::Object *thisObject = state.thisObject(); using func_type = FunctionWrapper; static_assert(!std::is_void::value, "should return a value"); return nativevalue_to_se(func_type::invoke(getterPtr, self), state.rval(), thisObject); } return false; } bool set(se::State &state) const override { if constexpr (HAS_SETTER) { T *self = reinterpret_cast(state.nativeThisObject()); se::Object *thisObject = state.thisObject(); const auto &args = state.args(); HolderType::value> temp; sevalue_to_native(args[0], &(temp.data), thisObject); using func_type = FunctionWrapper; func_type::invoke(setterPtr, self, temp.value()); return true; } return false; } }; template struct StaticMethod : StaticMethodBase { using type = R (*)(ARGS...); using return_type = R; constexpr static size_t ARG_N = sizeof...(ARGS); constexpr static bool RETURN_VOID = std::is_same::value; type func{nullptr}; template R callWithTuple(std::tuple &args, std::index_sequence /*unused*/) const { return (*func)(std::get(args).value()...); } bool invoke(se::State &state) const override { constexpr auto indexes{std::make_index_sequence()}; const auto &jsArgs = state.args(); if (ARG_N != jsArgs.size()) { SE_LOGE("incorret argument size %d, expect %d\n", static_cast(jsArgs.size()), static_cast(ARG_N)); return false; } std::tuple::value>...> args{}; convert_js_args_to_tuple(jsArgs, args, nullptr, indexes); if constexpr (RETURN_VOID) { callWithTuple(args, indexes); } else { nativevalue_to_se(callWithTuple(args, indexes), state.rval(), nullptr); } return true; } }; struct StaticMethodOverloaded : StaticMethodBase { ccstd::vector functions; bool invoke(se::State &state) const override { bool ret = false; auto argCount = state.args().size(); for (auto *method : functions) { if (method->argCount == -1 || method->argCount == argCount) { ret = method->invoke(state); if (ret) return true; } } return false; } }; template struct SAttributeAccessor; template struct SAccessorGet { using type = void; using return_type = void; }; template struct SAccessorSet { using type = void; using value_type = void; }; template <> struct SAccessorGet { using type = std::nullptr_t; using return_type = std::nullptr_t; }; template <> struct SAccessorSet { using type = std::nullptr_t; using value_type = std::nullptr_t; }; template struct SAccessorGet { using type = R(*); using return_type = R; static_assert(!std::is_void::value, "Getter should return value"); }; template struct SAccessorSet { using type = R (*)(F); using value_type = F; using ignored_return_type = R; }; template struct StaticAttribute> : StaticAttributeBase { using type = T; using get_accessor = SAccessorGet; using set_accessor = SAccessorSet; using getter_type = typename get_accessor::type; using setter_type = typename set_accessor::type; using set_value_type = std::remove_reference_t>; using get_value_type = std::remove_reference_t>; constexpr static bool HAS_GETTER = !std::is_null_pointer::value; constexpr static bool HAS_SETTER = !std::is_null_pointer::value; static_assert(HAS_GETTER || HAS_SETTER, "Either getter or setter should be set"); setter_type setterPtr; getter_type getterPtr; bool get(se::State &state) const override { if constexpr (HAS_GETTER) { using func_type = StaticFunctionWrapper; static_assert(!std::is_void::value, "should return a value"); return nativevalue_to_se(func_type::invoke(getterPtr), state.rval(), nullptr); } return false; } bool set(se::State &state) const override { if constexpr (HAS_SETTER) { const auto &args = state.args(); HolderType::value> temp; sevalue_to_native(args[0], &(temp.data), nullptr); using func_type = StaticFunctionWrapper; func_type::invoke(setterPtr, temp.value()); return true; } return false; } }; // NOLINTNEXTLINE class ContextDB { public: // NOLINTNEXTLINE struct Context { ccstd::vector>> properties; ccstd::vector>> fields; ccstd::vector>> functions; ccstd::vector>> staticFunctions; ccstd::vector>> staticProperties; ccstd::vector> constructors; ccstd::vector> finalizeCallbacks; ccstd::string className; se::Class *kls = nullptr; // se::Object * nsObject = nullptr; se::Object *parentProto = nullptr; }; Context *operator[](const char *key); static ContextDB &instance(); static void reset(); private: ccstd::unordered_map> _contexts; }; } // namespace intl template auto bindFunction(const se::Value &fnVal) { assert(fnVal.isObject() && fnVal.toObject()->isFunction()); return intl::FunctionExactor::bind(fnVal); } template R callFunction(se::Object *jsThisObject, const se::Value &fnVal, ARGS... args) { using T = R(ARGS...); assert(fnVal.isObject() && fnVal.toObject()->isFunction()); if constexpr (!std::is_void_v) { return intl::FunctionExactor::call(jsThisObject, fnVal, std::forward(args)...); } else { intl::FunctionExactor::call(jsThisObject, fnVal, std::forward(args)...); } } template R callFunction(const se::Value &fnVal, ARGS... args) { return callFunction(nullptr, fnVal, args...); } } // namespace sebind