Shaka Player Embedded
backing_object.cc
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 
16 
19 
20 namespace shaka {
21 
23  memory::ObjectTracker::Instance()->RegisterObject(this);
24 }
25 
27 #ifdef USING_JSC
28  // If a short-lived object is destroyed, the private data in the JavaScript
29  // object will still point to this object, but it will be invalid.
30  if (!js_this_.empty())
31  JSObjectSetPrivate(js_this_.handle(), nullptr);
32 #endif
33 }
34 
36  // Even though we can re-create the object later we need to trace it.
37  // JSC will not reset the weak pointer once the object is freed,
38  // giving us an invalid pointer. So we need to trace it so the pointer
39  // remains valid.
40  tracer->Trace(&js_this_);
41 }
42 
44 #ifdef USING_JSC
45  return !js_this_.empty();
46 #else
47  return false;
48 #endif
49 }
50 
51 std::string BackingObject::name() const {
52  return factory()->name();
53 }
54 
55 bool BackingObject::DerivedFrom(const std::string& base) {
56  return factory()->DerivedFrom(base);
57 }
58 
59 ReturnVal<JsValue> BackingObject::JsThis() const {
60  // This allows a wrapper to be deleted or this object to be created in C++
61  // where |js_this_| is initially empty.
62  if (js_this_.empty()) {
63  // NOTE: WrapInstance will call the JavaScript constructor which will call
64  // SetJsThis, so no need to use the return value.
65  factory()->WrapInstance(const_cast<BackingObject*>(this));
66  DCHECK(!js_this_.empty());
67  }
68  return js_this_.value();
69 }
70 
71 void BackingObject::SetJsThis(Handle<JsObject> this_) {
72  js_this_ = this_;
73 }
74 
75 } // namespace shaka
virtual BackingObjectFactoryBase * factory() const =0
bool empty() const
Definition: weak_js_ptr.h:78
void SetJsThis(Handle< JsObject > this_)
void Trace(memory::HeapTracer *tracer) const override
bool IsRootedAlive() const override
ReturnVal< JsValue > value() const
Definition: weak_js_ptr.h:97
void Trace(const Traceable *ptr)
Definition: heap_tracer.cc:43
const std::string & name() const
ReturnVal< JsValue > JsThis() const
bool DerivedFrom(const std::string &base)
bool DerivedFrom(const std::string &name) const
Handle< T > handle() const
Definition: weak_js_ptr.h:87
ReturnVal< JsValue > WrapInstance(BackingObject *object)
std::string name() const