1 | // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #ifndef RUNTIME_VM_DART_H_ |
6 | #define RUNTIME_VM_DART_H_ |
7 | |
8 | #include "include/dart_api.h" |
9 | #include "vm/allocation.h" |
10 | #include "vm/snapshot.h" |
11 | |
12 | namespace dart { |
13 | |
14 | // Forward declarations. |
15 | class DebugInfo; |
16 | class Isolate; |
17 | class LocalHandle; |
18 | class ReadOnlyHandles; |
19 | class ThreadPool; |
20 | namespace kernel { |
21 | class Program; |
22 | } |
23 | |
24 | class Dart : public AllStatic { |
25 | public: |
26 | // Returns null if initialization succeeds, otherwise returns an error message |
27 | // (caller owns error message and has to free it). |
28 | static char* Init(const uint8_t* vm_snapshot_data, |
29 | const uint8_t* vm_snapshot_instructions, |
30 | Dart_IsolateGroupCreateCallback create_group, |
31 | Dart_InitializeIsolateCallback initialize_isolate, |
32 | Dart_IsolateShutdownCallback shutdown, |
33 | Dart_IsolateCleanupCallback cleanup, |
34 | Dart_IsolateGroupCleanupCallback cleanup_group, |
35 | Dart_ThreadExitCallback thread_exit, |
36 | Dart_FileOpenCallback file_open, |
37 | Dart_FileReadCallback file_read, |
38 | Dart_FileWriteCallback file_write, |
39 | Dart_FileCloseCallback file_close, |
40 | Dart_EntropySource entropy_source, |
41 | Dart_GetVMServiceAssetsArchive get_service_assets, |
42 | bool start_kernel_isolate, |
43 | Dart_CodeObserver* observer); |
44 | |
45 | // Returns null if cleanup succeeds, otherwise returns an error message |
46 | // (caller owns error message and has to free it). |
47 | static char* Cleanup(); |
48 | |
49 | static Isolate* CreateIsolate(const char* name_prefix, |
50 | const Dart_IsolateFlags& api_flags, |
51 | IsolateGroup* isolate_group); |
52 | |
53 | // Initialize an isolate, either from a snapshot, from a Kernel binary, or |
54 | // from SDK library sources. If the snapshot_buffer is non-NULL, |
55 | // initialize from a snapshot or a Kernel binary depending on the value of |
56 | // from_kernel. Otherwise, initialize from sources. |
57 | static ErrorPtr InitializeIsolate(const uint8_t* snapshot_data, |
58 | const uint8_t* snapshot_instructions, |
59 | const uint8_t* kernel_buffer, |
60 | intptr_t kernel_buffer_size, |
61 | IsolateGroup* source_isolate_group, |
62 | void* data); |
63 | static ErrorPtr InitIsolateFromSnapshot(Thread* T, |
64 | Isolate* I, |
65 | const uint8_t* snapshot_data, |
66 | const uint8_t* snapshot_instructions, |
67 | const uint8_t* kernel_buffer, |
68 | intptr_t kernel_buffer_size); |
69 | |
70 | static bool DetectNullSafety(const char* script_uri, |
71 | const uint8_t* snapshot_data, |
72 | const uint8_t* snapshot_instructions, |
73 | const uint8_t* kernel_buffer, |
74 | intptr_t kernel_buffer_size, |
75 | const char* package_config, |
76 | const char* original_working_directory); |
77 | |
78 | static void RunShutdownCallback(); |
79 | static void ShutdownIsolate(Isolate* isolate); |
80 | static void ShutdownIsolate(); |
81 | |
82 | static Isolate* vm_isolate() { return vm_isolate_; } |
83 | static ThreadPool* thread_pool() { return thread_pool_; } |
84 | static bool VmIsolateNameEquals(const char* name); |
85 | |
86 | static int64_t UptimeMicros(); |
87 | static int64_t UptimeMillis() { |
88 | return UptimeMicros() / kMicrosecondsPerMillisecond; |
89 | } |
90 | |
91 | static void set_pprof_symbol_generator(DebugInfo* value) { |
92 | pprof_symbol_generator_ = value; |
93 | } |
94 | static DebugInfo* pprof_symbol_generator() { return pprof_symbol_generator_; } |
95 | |
96 | static LocalHandle* AllocateReadOnlyApiHandle(); |
97 | static bool IsReadOnlyApiHandle(Dart_Handle handle); |
98 | |
99 | static uword AllocateReadOnlyHandle(); |
100 | static bool IsReadOnlyHandle(uword address); |
101 | |
102 | static const char* FeaturesString(Isolate* isolate, |
103 | bool is_vm_snapshot, |
104 | Snapshot::Kind kind); |
105 | static Snapshot::Kind vm_snapshot_kind() { return vm_snapshot_kind_; } |
106 | |
107 | static Dart_ThreadExitCallback thread_exit_callback() { |
108 | return thread_exit_callback_; |
109 | } |
110 | static void set_thread_exit_callback(Dart_ThreadExitCallback cback) { |
111 | thread_exit_callback_ = cback; |
112 | } |
113 | static void SetFileCallbacks(Dart_FileOpenCallback file_open, |
114 | Dart_FileReadCallback file_read, |
115 | Dart_FileWriteCallback file_write, |
116 | Dart_FileCloseCallback file_close) { |
117 | file_open_callback_ = file_open; |
118 | file_read_callback_ = file_read; |
119 | file_write_callback_ = file_write; |
120 | file_close_callback_ = file_close; |
121 | } |
122 | |
123 | static Dart_FileOpenCallback file_open_callback() { |
124 | return file_open_callback_; |
125 | } |
126 | static Dart_FileReadCallback file_read_callback() { |
127 | return file_read_callback_; |
128 | } |
129 | static Dart_FileWriteCallback file_write_callback() { |
130 | return file_write_callback_; |
131 | } |
132 | static Dart_FileCloseCallback file_close_callback() { |
133 | return file_close_callback_; |
134 | } |
135 | |
136 | static void set_entropy_source_callback(Dart_EntropySource entropy_source) { |
137 | entropy_source_callback_ = entropy_source; |
138 | } |
139 | static Dart_EntropySource entropy_source_callback() { |
140 | return entropy_source_callback_; |
141 | } |
142 | |
143 | private: |
144 | static constexpr const char* kVmIsolateName = "vm-isolate" ; |
145 | |
146 | static void WaitForIsolateShutdown(); |
147 | static void WaitForApplicationIsolateShutdown(); |
148 | |
149 | static Isolate* vm_isolate_; |
150 | static int64_t start_time_micros_; |
151 | static ThreadPool* thread_pool_; |
152 | static DebugInfo* pprof_symbol_generator_; |
153 | static ReadOnlyHandles* predefined_handles_; |
154 | static Snapshot::Kind vm_snapshot_kind_; |
155 | static Dart_ThreadExitCallback thread_exit_callback_; |
156 | static Dart_FileOpenCallback file_open_callback_; |
157 | static Dart_FileReadCallback file_read_callback_; |
158 | static Dart_FileWriteCallback file_write_callback_; |
159 | static Dart_FileCloseCallback file_close_callback_; |
160 | static Dart_EntropySource entropy_source_callback_; |
161 | }; |
162 | |
163 | } // namespace dart |
164 | |
165 | #endif // RUNTIME_VM_DART_H_ |
166 | |