blob: 8e8c629c34fc7b53ccb647215167498000b27396 [file] [log] [blame]
John McCutchan080c33e2015-07-16 12:35:35 -07001// 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 McCutchanc27a98c2015-07-21 11:25:24 -07005#ifndef TONIC_DART_VALUE_H_
6#define TONIC_DART_VALUE_H_
John McCutchan080c33e2015-07-16 12:35:35 -07007
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 McCutchandeace8f2015-07-21 08:01:40 -070016namespace tonic {
John McCutchan080c33e2015-07-16 12:35:35 -070017
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.
23class 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 McCutchandeace8f2015-07-21 08:01:40 -070063} // namespace tonic
John McCutchan080c33e2015-07-16 12:35:35 -070064
John McCutchanc27a98c2015-07-21 11:25:24 -070065#endif // TONIC_DART_VALUE_H_