Shaka Player Embedded
any.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 
15 #include "src/mapping/any.h"
16 
17 #include <cmath>
18 
19 #include "src/memory/heap_tracer.h"
20 
21 namespace shaka {
22 
23 Any::Any() : value_(JsUndefined()), is_number_(false) {}
24 Any::Any(std::nullptr_t) : value_(JsNull()), is_number_(false) {}
26 
27 Any::Any(const Any&) = default;
28 Any::Any(Any&&) = default;
29 Any& Any::operator=(const Any&) = default;
30 Any& Any::operator=(Any&&) = default;
31 
32 bool Any::IsTruthy() const {
33  switch (GetValueType(value_.handle())) {
36  return false;
38  return !ConvertToString(value_.handle()).empty();
40  return BooleanFromValue(value_.handle());
42  const double val = NumberFromValue(value_.handle());
43  return !std::isnan(val) && val != 0;
44  }
45  default:
46  return true;
47  }
48 }
49 
50 bool Any::TryConvert(Handle<JsValue> value) {
51  value_ = value;
52  is_number_ = GetValueType(value_.handle()) == proto::ValueType::Number;
53  return true;
54 }
55 
56 ReturnVal<JsValue> Any::ToJsValue() const {
57  return value_.value();
58 }
59 
60 void Any::Trace(memory::HeapTracer* tracer) const {
61  // V8 doesn't seem to support tracing numbers. Other primitives are okay to
62  // trace, so only ignore if this is a number.
63  if (!is_number_)
64  tracer->Trace(&value_);
65 }
66 
67 } // namespace shaka
Definition: any.h:31
ReturnVal< JsValue > JsUndefined()
Definition: js_wrappers.cc:284
ReturnVal< JsValue > JsNull()
Definition: js_wrappers.cc:288
~Any() override
Definition: any.cc:25
bool BooleanFromValue(Handle< JsValue > value)
Definition: js_wrappers.cc:394
Any()
Definition: any.cc:23
bool IsTruthy() const
Definition: any.cc:32
ReturnVal< JsValue > value() const
Definition: weak_js_ptr.h:97
void Trace(const Traceable *ptr)
Definition: heap_tracer.cc:43
proto::ValueType GetValueType(Handle< JsValue > value)
Definition: js_wrappers.cc:329
double NumberFromValue(Handle< JsValue > value)
Definition: js_wrappers.cc:385
void Trace(memory::HeapTracer *tracer) const override
Definition: any.cc:60
Any & operator=(const Any &)
std::string ConvertToString(Handle< JsValue > value)
Definition: js_wrappers.cc:203
ReturnVal< JsValue > ToJsValue() const override
Definition: any.cc:56
Handle< T > handle() const
Definition: weak_js_ptr.h:87
bool TryConvert(Handle< JsValue > value) override
Definition: any.cc:50