15 #ifndef SHAKA_EMBEDDED_CORE_JS_OBJECT_WRAPPER_H_ 16 #define SHAKA_EMBEDDED_CORE_JS_OBJECT_WRAPPER_H_ 21 #include <type_traits> 56 template <
typename Ret,
typename =
void>
62 Handle<JsValue> result) {
65 return Error(
"Invalid return value from " + name +
"().");
71 template <
typename Dummy>
91 template <
typename Ret,
typename... Args>
93 Args&&... args)
const {
94 return CallMethodCommon<Ret>(&
object_,
name, std::forward<Args>(args)...);
106 template <
typename Ret,
typename... Args>
108 const std::vector<std::string>& global_path, Args&&... args) {
109 std::vector<std::string> obj_path{global_path.begin(),
110 global_path.end() - 1};
111 return CallMethodCommon<Ret>(std::move(obj_path), global_path.back(),
112 std::forward<Args>(args)...);
121 template <
typename T>
123 const std::vector<std::string>& global_path) {
124 auto callback = std::bind(&GetFieldRaw<T>, global_path);
126 std::move(callback));
135 void Init(Handle<JsObject>
object);
144 Handle<JsObject> that,
const std::string&
name,
int argc,
145 LocalVar<JsValue>* argv, LocalVar<JsValue>* result);
157 const std::string& name, std::function<
void(
const Error&)> on_error,
158 std::function<
void(Handle<JsObject> event)> handler)
const;
167 template <
typename T>
169 const std::vector<std::string>& global_path) {
170 LocalVar<JsValue> value =
185 template <
typename Ret,
typename... Args>
188 variant<
const Global<JsObject>*, std::vector<std::string>> that,
189 const std::string& name, Args&&... args) {
191 LocalVar<JsObject> that_obj;
193 that_obj = *get<const Global<JsObject>*>(that);
195 LocalVar<JsValue> temp =
197 get<std::vector<std::string>>(that));
199 p->set_value(
Error(
"Unable to find object."));
202 that_obj = UnsafeJsCast<JsObject>(temp);
205 LocalVar<JsValue> result;
209 if (holds_alternative<Error>(error)) {
210 p->set_value(get<Error>(error));
215 if (holds_alternative<Error>(js_promise)) {
220 get<Promise>(js_promise)
223 LocalVar<JsValue> value = res.
ToJsValue();
227 LocalVar<JsValue> val = except.ToJsValue();
238 template <
typename T>
253 template <
typename Ret,
typename... Args>
255 variant<
const Global<JsObject>*, std::vector<std::string>> that,
256 const std::string& name, Args&&... args) {
258 std::make_shared<std::promise<typename Converter<Ret>::variant_type>>();
260 CallMethodRaw<Ret>(promise, std::move(that),
name,
261 std::forward<Args>(args)...);
265 std::move(that), name, std::forward<Args>(args)...);
269 return promise->get_future().share();
277 #endif // SHAKA_EMBEDDED_CORE_JS_OBJECT_WRAPPER_H_
typename AsyncResults< Ret >::variant_type variant_type
ReturnVal< JsValue > JsUndefined()
bool IsObject(Handle< JsValue > value)
bool FromJsValue(Handle< JsValue > source, T *dest)
ReturnVal< JsValue > GetDescendant(Handle< JsObject > root, const std::vector< std::string > &names)
Converter< Ret >::future_type CallMethod(const std::string &name, Args &&... args) const
static Converter< T >::future_type GetGlobalField(const std::vector< std::string > &global_path)
ReturnVal< JsValue > ToJsValue(T &&source)
Global< JsObject > object_
static Converter< void >::variant_type CallMemberFunction(Handle< JsObject > that, const std::string &name, int argc, LocalVar< JsValue > *argv, LocalVar< JsValue > *result)
static void CallMethodRaw(std::shared_ptr< std::promise< typename Converter< Ret >::variant_type >> p, variant< const Global< JsObject > *, std::vector< std::string >> that, const std::string &name, Args &&... args)
std::shared_future< variant_type > future_type
std::shared_future< impl::RetOf< Func > > InvokeOrSchedule(Func &&callback)
std::shared_future< variant_type > future_type
static JsManagerImpl * Instance()
static Converter< Ret >::future_type CallMethodCommon(variant< const Global< JsObject > *, std::vector< std::string >> that, const std::string &name, Args &&... args)
static variant_type Convert(const std::string &, Handle< JsValue >)
SHAKA_NON_COPYABLE_OR_MOVABLE_TYPE(JsObjectWrapper)
typename std::add_const< typename std::remove_reference< T >::type >::type & bind_forward
static variant_type Convert(const std::string &name, Handle< JsValue > result)
Converter< void >::variant_type AttachEventListener(const std::string &name, std::function< void(const Error &)> on_error, std::function< void(Handle< JsObject > event)> handler) const
std::shared_ptr< ThreadEvent< impl::RetOf< Func > > > AddInternalTask(TaskPriority priority, const std::string &name, Func &&callback)
static Converter< T >::variant_type GetFieldRaw(const std::vector< std::string > &global_path)
static Converter< Ret >::future_type CallGlobalMethod(const std::vector< std::string > &global_path, Args &&... args)
ReturnVal< JsValue > ToJsValue() const override
static Error ConvertError(Handle< JsValue > except)
bool BelongsToCurrentThread() const
bool holds_alternative(const variant< Choices... > &variant)
void Init(Handle< JsObject > object)
TaskRunner * MainThread()