Shaka Player Embedded
jsc_utils.cc
Go to the documentation of this file.
1 // Copyright 2017 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 
16 
17 #include <vector>
18 
19 #include "src/mapping/js_engine.h"
21 
22 namespace shaka {
23 
24 JSContextRef GetContext() {
25  return JsEngine::Instance()->context();
26 }
27 
28 void OnUncaughtException(JSValueRef exception, bool in_promise) {
29  if (!exception)
30  return;
31 
32  if (in_promise)
33  LOG(ERROR) << "Uncaught (in promise): " << ConvertToString(exception);
34  else
35  LOG(ERROR) << "Uncaught: " << ConvertToString(exception);
36 
37  // TODO: Print stack trace?
38 }
39 
40 JSValueRef CreateNativeObject(const std::string& name, JSValueRef* args,
41  size_t argc) {
42  JSContextRef cx = GetContext();
43  LocalVar<JsObject> global = JSContextGetGlobalObject(cx);
44  JSValueRef ctor = GetMemberRaw(global, name);
45  DCHECK(ctor && IsObject(ctor));
46  LocalVar<JsObject> ctor_obj = UnsafeJsCast<JsObject>(ctor);
47 
48  LocalVar<JsValue> ret;
49  std::vector<LocalVar<JsValue>> local_args{args, args + argc};
50  CHECK(InvokeConstructor(ctor_obj, argc, local_args.data(), &ret));
51  return ret;
52 }
53 
54 
55 } // namespace shaka
bool IsObject(Handle< JsValue > value)
Definition: js_wrappers.cc:315
const char * name
JSValueRef CreateNativeObject(const std::string &name, JSValueRef *args, size_t argc)
Definition: jsc_utils.cc:40
std::string ConvertToString(Handle< JsValue > value)
Definition: js_wrappers.cc:203
JSContextRef GetContext()
Definition: jsc_utils.cc:24
void OnUncaughtException(JSValueRef exception, bool in_promise)
Definition: jsc_utils.cc:28
bool InvokeConstructor(Handle< JsFunction > ctor, int argc, LocalVar< JsValue > *argv, LocalVar< JsValue > *result_or_except)
Definition: js_wrappers.cc:165
ReturnVal< JsValue > GetMemberRaw(Handle< JsObject > object, const std::string &name, LocalVar< JsValue > *exception=nullptr)
Definition: js_wrappers.cc:136