Shaka Player Embedded
js_engine.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 
15 #include "src/mapping/js_engine.h"
16 
18 #include "src/memory/heap_tracer.h"
20 
21 namespace shaka {
22 
23 namespace {
24 constexpr const uint64_t kGcIntervalMs = 30 * 1000;
25 } // namespace
26 
27 // \cond Doxygen_Skip
28 
30  : context_(JSGlobalContextCreate(nullptr)),
31  thread_id_(std::this_thread::get_id()) {
32  auto task = []() {
33  VLOG(1) << "Begin GC run";
34  auto* object_tracker = memory::ObjectTracker::Instance();
35  auto* heap_tracer = JsManagerImpl::Instance()->HeapTracer();
36  heap_tracer->BeginPass();
37  heap_tracer->TraceAll(object_tracker->GetAliveObjects());
38  object_tracker->FreeDeadObjects(heap_tracer->alive());
39 
40  // This will signal to JSC that we have just destroyed a lot of objects.
41  // See http://bugs.webkit.org/show_bug.cgi?id=84476
42  JSGarbageCollect(GetContext());
43 
44  VLOG(1) << "End GC run";
45  };
46 
47  // If the engine was created as part of a test, then don't create the timer
48  // since we don't need to GC.
49  JsManagerImpl* impl = JsManagerImpl::InstanceOrNull();
50  if (impl)
51  impl->MainThread()->AddRepeatedTimer(kGcIntervalMs, std::move(task));
52 }
53 
55  JSGlobalContextRelease(context_);
56 }
57 
58 Handle<JsObject> JsEngine::global_handle() {
59  return JSContextGetGlobalObject(context());
60 }
61 
62 ReturnVal<JsValue> JsEngine::global_value() {
63  return JSContextGetGlobalObject(context());
64 }
65 
66 JSContextRef JsEngine::context() const {
67  // TODO: Consider asserting we are on the correct thread. Unlike other
68  // JavaScript engines, JSC allows access from any thread and will just
69  // serialize the requests. We can't assert as of now since the C++ API's
70  // Player will unref in the destructor.
71  return context_;
72 }
73 
76 
77 // \endcond Doxygen_Skip
78 
79 } // namespace shaka
Handle< JsObject > global_handle()
static JsManagerImpl * InstanceOrNull()
memory::HeapTracer * HeapTracer()
ReturnVal< JsValue > global_value()
JSContextRef GetContext()
Definition: jsc_utils.cc:24