1 | // Copyright 2013 The Flutter 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 | |
5 | #include "tonic/dart_persistent_value.h" |
6 | |
7 | #include "tonic/dart_state.h" |
8 | #include "tonic/scopes/dart_isolate_scope.h" |
9 | |
10 | namespace tonic { |
11 | |
12 | DartPersistentValue::DartPersistentValue() : value_(nullptr) {} |
13 | |
14 | DartPersistentValue::DartPersistentValue(DartPersistentValue&& other) |
15 | : dart_state_(other.dart_state_), value_(other.value_) { |
16 | other.dart_state_.reset(); |
17 | other.value_ = nullptr; |
18 | } |
19 | |
20 | DartPersistentValue::DartPersistentValue(DartState* dart_state, |
21 | Dart_Handle value) |
22 | : value_(nullptr) { |
23 | Set(dart_state, value); |
24 | } |
25 | |
26 | DartPersistentValue::~DartPersistentValue() { |
27 | Clear(); |
28 | } |
29 | |
30 | void DartPersistentValue::Set(DartState* dart_state, Dart_Handle value) { |
31 | TONIC_DCHECK(is_empty()); |
32 | dart_state_ = dart_state->GetWeakPtr(); |
33 | value_ = Dart_NewPersistentHandle(value); |
34 | } |
35 | |
36 | void DartPersistentValue::Clear() { |
37 | if (!value_) { |
38 | return; |
39 | } |
40 | |
41 | auto dart_state = dart_state_.lock(); |
42 | if (!dart_state) { |
43 | return; |
44 | } |
45 | |
46 | if (Dart_CurrentIsolateGroup()) { |
47 | Dart_DeletePersistentHandle(value_); |
48 | } else { |
49 | DartIsolateScope scope(dart_state->isolate()); |
50 | Dart_DeletePersistentHandle(value_); |
51 | } |
52 | dart_state_.reset(); |
53 | value_ = nullptr; |
54 | } |
55 | |
56 | Dart_Handle DartPersistentValue::Get() { |
57 | if (!value_) |
58 | return nullptr; |
59 | return Dart_HandleFromPersistent(value_); |
60 | } |
61 | |
62 | Dart_Handle DartPersistentValue::Release() { |
63 | Dart_Handle local = Get(); |
64 | Clear(); |
65 | return local; |
66 | } |
67 | } // namespace tonic |
68 |