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_state.h" |
6 | |
7 | #include "tonic/converter/dart_converter.h" |
8 | #include "tonic/dart_class_library.h" |
9 | #include "tonic/dart_message_handler.h" |
10 | #include "tonic/file_loader/file_loader.h" |
11 | |
12 | namespace tonic { |
13 | |
14 | DartState::Scope::Scope(DartState* dart_state) |
15 | : scope_(dart_state->isolate()) {} |
16 | |
17 | DartState::Scope::Scope(std::shared_ptr<DartState> dart_state) |
18 | : scope_(dart_state->isolate()) {} |
19 | |
20 | DartState::Scope::~Scope() {} |
21 | |
22 | DartState::DartState(int dirfd, |
23 | std::function<void(Dart_Handle)> message_epilogue) |
24 | : isolate_(nullptr), |
25 | private_constructor_name_(), |
26 | class_library_(new DartClassLibrary), |
27 | message_handler_(new DartMessageHandler()), |
28 | file_loader_(new FileLoader(dirfd)), |
29 | message_epilogue_(message_epilogue), |
30 | has_set_return_code_(false) {} |
31 | |
32 | DartState::~DartState() {} |
33 | |
34 | void DartState::SetIsolate(Dart_Isolate isolate) { |
35 | isolate_ = isolate; |
36 | |
37 | if (!isolate_) |
38 | return; |
39 | |
40 | private_constructor_name_.Clear(); |
41 | Dart_EnterScope(); |
42 | private_constructor_name_.Set( |
43 | this, Dart_NewPersistentHandle(Dart_NewStringFromCString("_" ))); |
44 | Dart_ExitScope(); |
45 | |
46 | DidSetIsolate(); |
47 | } |
48 | |
49 | DartState* DartState::From(Dart_Isolate isolate) { |
50 | auto isolate_data = |
51 | static_cast<std::shared_ptr<DartState>*>(Dart_IsolateData(isolate)); |
52 | return isolate_data->get(); |
53 | } |
54 | |
55 | DartState* DartState::Current() { |
56 | auto isolate_data = |
57 | static_cast<std::shared_ptr<DartState>*>(Dart_CurrentIsolateData()); |
58 | return isolate_data->get(); |
59 | } |
60 | |
61 | std::weak_ptr<DartState> DartState::GetWeakPtr() { |
62 | return shared_from_this(); |
63 | } |
64 | |
65 | void DartState::SetReturnCode(uint32_t return_code) { |
66 | if (set_return_code_callback_) { |
67 | set_return_code_callback_(return_code); |
68 | } |
69 | has_set_return_code_ = true; |
70 | } |
71 | |
72 | void DartState::SetReturnCodeCallback(std::function<void(uint32_t)> callback) { |
73 | set_return_code_callback_ = callback; |
74 | } |
75 | |
76 | void DartState::DidSetIsolate() {} |
77 | |
78 | Dart_Handle DartState::HandleLibraryTag(Dart_LibraryTag tag, |
79 | Dart_Handle library, |
80 | Dart_Handle url) { |
81 | return Current()->file_loader().HandleLibraryTag(tag, library, url); |
82 | } |
83 | |
84 | } // namespace tonic |
85 | |