Shaka Player Embedded
callback.h
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_MAPPING_CALLBACK_H_
16 #define SHAKA_EMBEDDED_MAPPING_CALLBACK_H_
17 
18 #include <string>
19 #include <utility>
20 
21 #include "src/mapping/convert_js.h"
23 #include "src/mapping/js_engine.h"
26 #include "src/util/macros.h"
27 
28 namespace shaka {
29 
39  public:
40  static std::string name() {
41  return "function";
42  }
43 
44  Callback();
45  ~Callback() override;
46 
47  Callback(const Callback&);
48  Callback(Callback&&);
49  Callback& operator=(const Callback&);
51 
52  bool empty() const {
53  return callback_.empty();
54  }
55 
56  bool operator==(const Callback& other) const {
57  return callback_ == other.callback_;
58  }
59  bool operator!=(const Callback& other) const {
60  return callback_ != other.callback_;
61  }
62 
63  template <typename... Args>
64  void operator()(Args&&... args) const {
65  const auto exception = CallInternal(JsEngine::Instance()->global_handle(),
66  std::forward<Args>(args)...);
67  if (holds_alternative<js::JsError>(exception)) {
68  OnUncaughtException(get<js::JsError>(exception).error(),
69  /* in_promise */ false);
70  }
71  }
72 
73  template <typename T, typename... Args>
75  Args&&... args) const {
76  LocalVar<JsValue> that_val = ::shaka::ToJsValue(that);
77  return CallInternal(that_val, std::forward<Args>(args)...);
78  }
79 
80  bool TryConvert(Handle<JsValue> given) override;
81  ReturnVal<JsValue> ToJsValue() const override;
82  void Trace(memory::HeapTracer* tracer) const override;
83 
84  private:
85  template <typename... Args>
86  ExceptionOr<void> CallInternal(Handle<JsValue> that, Args&&... args) const {
87  DCHECK(!empty());
88  DCHECK(IsObject(that));
89 
90  LocalVar<JsObject> that_obj = UnsafeJsCast<JsObject>(that);
91  // Add another element to avoid a 0-length array with no arguments. This
92  // won't change the number of arguments passed in JavaScript.
93  LocalVar<JsValue> arguments[] = {
94  ::shaka::ToJsValue(std::forward<Args>(args))..., JsUndefined()};
95  LocalVar<JsValue> except;
96  if (!InvokeMethod(callback_.handle(), that_obj, sizeof...(Args), arguments,
97  &except)) {
98  return js::JsError::Rethrow(except);
99  }
100  return {};
101  }
102 
103  WeakJsPtr<JsFunction> callback_;
104 };
105 
106 } // namespace shaka
107 
108 #endif // SHAKA_EMBEDDED_MAPPING_CALLBACK_H_
bool empty() const
Definition: weak_js_ptr.h:78
ReturnVal< JsValue > JsUndefined()
Definition: js_wrappers.cc:284
bool IsObject(Handle< JsValue > value)
Definition: js_wrappers.cc:315
static JsError Rethrow(Handle< JsValue > error)
Definition: js_error.cc:105
bool empty() const
Definition: callback.h:52
bool InvokeMethod(Handle< JsFunction > method, Handle< JsObject > that, int argc, LocalVar< JsValue > *argv, LocalVar< JsValue > *result_or_except)
Definition: js_wrappers.cc:182
bool operator==(const Callback &other) const
Definition: callback.h:56
ReturnVal< JsValue > ToJsValue(T &&source)
Definition: convert_js.h:381
Callback & operator=(const Callback &)
ReturnVal< JsValue > ToJsValue() const override
Definition: callback.cc:36
#define MUST_USE_RESULT
Definition: macros.h:52
bool operator!=(const Callback &other) const
Definition: callback.h:59
void Trace(memory::HeapTracer *tracer) const override
Definition: callback.cc:40
bool TryConvert(Handle< JsValue > given) override
Definition: callback.cc:29
MUST_USE_RESULT ExceptionOr< void > CallWithThis(T &&that, Args &&... args) const
Definition: callback.h:74
static std::string name()
Definition: callback.h:40
Handle< T > handle() const
Definition: weak_js_ptr.h:87
void OnUncaughtException(JSValueRef exception, bool in_promise)
Definition: jsc_utils.cc:28
~Callback() override
Definition: callback.cc:22
void operator()(Args &&... args) const
Definition: callback.h:64