John McCutchan | 080c33e | 2015-07-16 12:35:35 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
John McCutchan | c27a98c | 2015-07-21 11:25:24 -0700 | [diff] [blame] | 5 | #ifndef TONIC_DART_VALUE_H_ |
| 6 | #define TONIC_DART_VALUE_H_ |
John McCutchan | 080c33e | 2015-07-16 12:35:35 -0700 | [diff] [blame] | 7 | |
| 8 | #include "base/logging.h" |
| 9 | #include "dart/runtime/include/dart_api.h" |
| 10 | #include "tonic/dart_persistent_value.h" |
| 11 | #include "tonic/dart_state.h" |
| 12 | #include "sky/engine/wtf/PassRefPtr.h" |
| 13 | #include "sky/engine/wtf/RefCounted.h" |
| 14 | #include "sky/engine/wtf/RefPtr.h" |
| 15 | |
John McCutchan | deace8f | 2015-07-21 08:01:40 -0700 | [diff] [blame] | 16 | namespace tonic { |
John McCutchan | 080c33e | 2015-07-16 12:35:35 -0700 | [diff] [blame] | 17 | |
| 18 | // DartValue is a convience wrapper around DartPersistentValue that lets clients |
| 19 | // use RefPtr to keep track of the number of references to the underlying Dart |
| 20 | // object. Be careful when retaining RefPtr<DartValue> in the heap because the |
| 21 | // VM's garbage collector cannot break cycles that involve the C++ heap, which |
| 22 | // can lead to memory leaks. |
| 23 | class DartValue : public RefCounted<DartValue> { |
| 24 | public: |
| 25 | static PassRefPtr<DartValue> Create(DartState* dart_state, |
| 26 | Dart_Handle value) { |
| 27 | return adoptRef(new DartValue(dart_state, value)); |
| 28 | } |
| 29 | |
| 30 | static PassRefPtr<DartValue> Create() { return adoptRef(new DartValue()); } |
| 31 | |
| 32 | ~DartValue(); |
| 33 | |
| 34 | Dart_Handle dart_value() const { return dart_value_.value(); } |
| 35 | const base::WeakPtr<DartState>& dart_state() const { |
| 36 | return dart_value_.dart_state(); |
| 37 | } |
| 38 | |
| 39 | bool is_empty() const { return !dart_value(); } |
| 40 | |
| 41 | bool is_null() const { |
| 42 | DCHECK(!is_empty()); |
| 43 | return Dart_IsNull(dart_value()); |
| 44 | } |
| 45 | |
| 46 | bool is_function() const { |
| 47 | DCHECK(!is_empty()); |
| 48 | return Dart_IsClosure(dart_value()); |
| 49 | } |
| 50 | |
| 51 | bool Equals(DartValue* other) const; |
| 52 | void Clear(); |
| 53 | |
| 54 | private: |
| 55 | DartValue(); |
| 56 | DartValue(DartState* dart_state, Dart_Handle value); |
| 57 | |
| 58 | DartPersistentValue dart_value_; |
| 59 | |
| 60 | DISALLOW_COPY_AND_ASSIGN(DartValue); |
| 61 | }; |
| 62 | |
John McCutchan | deace8f | 2015-07-21 08:01:40 -0700 | [diff] [blame] | 63 | } // namespace tonic |
John McCutchan | 080c33e | 2015-07-16 12:35:35 -0700 | [diff] [blame] | 64 | |
John McCutchan | c27a98c | 2015-07-21 11:25:24 -0700 | [diff] [blame] | 65 | #endif // TONIC_DART_VALUE_H_ |