Flutter Engine
The Flutter Engine
dart_entry.h
Go to the documentation of this file.
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_ENTRY_H_
6#define RUNTIME_VM_DART_ENTRY_H_
7
8#include "vm/allocation.h"
9#include "vm/growable_array.h"
10#include "vm/object.h"
11#include "vm/raw_object.h"
12
13namespace dart {
14
15// Forward declarations.
16class Array;
17class Closure;
18class Function;
19class Instance;
20class Integer;
21class Library;
22class Object;
23class String;
24
25// An arguments descriptor array consists of the type argument vector length (0
26// if none); total argument count (not counting type argument vector); total
27// arguments size (not counting type argument vector); the positional argument
28// count; a sequence of (name, position) pairs, sorted by name, for each named
29// optional argument; and a terminating null to simplify iterating in generated
30// code.
32 public:
33 explicit ArgumentsDescriptor(const Array& array);
34
35 // Accessors.
36 intptr_t TypeArgsLen() const; // 0 if no type argument vector is passed.
37 intptr_t FirstArgIndex() const { return TypeArgsLen() > 0 ? 1 : 0; }
38 intptr_t CountWithTypeArgs() const { return FirstArgIndex() + Count(); }
39 intptr_t Count() const; // Excluding type arguments vector.
40 intptr_t Size() const; // Excluding type arguments vector.
41 intptr_t SizeWithTypeArgs() const { return FirstArgIndex() + Size(); }
42 intptr_t PositionalCount() const; // Excluding type arguments vector.
43 intptr_t NamedCount() const { return Count() - PositionalCount(); }
44 StringPtr NameAt(intptr_t i) const;
45 intptr_t PositionAt(intptr_t i) const;
46 bool MatchesNameAt(intptr_t i, const String& other) const;
47 // Returns array of argument names in the arguments order.
48 ArrayPtr GetArgumentNames() const;
49 void PrintTo(BaseTextBuffer* buffer, bool show_named_positions = false) const;
50 const char* ToCString() const;
51
52 // Generated code support.
53 static intptr_t type_args_len_offset() {
54 return Array::element_offset(kTypeArgsLenIndex);
55 }
56
57 static intptr_t count_offset() { return Array::element_offset(kCountIndex); }
58
59 static intptr_t size_offset() { return Array::element_offset(kSizeIndex); }
60
61 static intptr_t positional_count_offset() {
62 return Array::element_offset(kPositionalCountIndex);
63 }
64
65 static intptr_t first_named_entry_offset() {
66 return Array::element_offset(kFirstNamedEntryIndex);
67 }
68
69 static intptr_t name_offset() { return kNameOffset * kCompressedWordSize; }
70 static intptr_t position_offset() {
71 return kPositionOffset * kCompressedWordSize;
72 }
73 static intptr_t named_entry_size() {
74 return kNamedEntrySize * kCompressedWordSize;
75 }
76
77 // Constructs an argument descriptor where all arguments are boxed and
78 // therefore number of parameters equals parameter size.
79 //
80 // Right now this is for example the case for all closure functions.
81 // Functions marked as entry-points may also be created by NewUnboxed because
82 // we rely that TFA will mark the arguments as nullable for such cases.
83 static ArrayPtr NewBoxed(intptr_t type_args_len,
84 intptr_t num_arguments,
85 const Array& optional_arguments_names,
86 Heap::Space space = Heap::kOld) {
87 return New(type_args_len, num_arguments, num_arguments,
88 optional_arguments_names, space);
89 }
90
91 // Allocate and return an arguments descriptor. The first
92 // (num_arguments - optional_arguments_names.Length()) arguments are
93 // positional and the remaining ones are named optional arguments.
94 // The presence of a type argument vector as first argument (not counted in
95 // num_arguments) is indicated by a non-zero type_args_len.
96 static ArrayPtr New(intptr_t type_args_len,
97 intptr_t num_arguments,
98 intptr_t size_arguments,
99 const Array& optional_arguments_names,
100 Heap::Space space = Heap::kOld);
101
102 // Constructs an argument descriptor where all arguments are boxed and
103 // therefore number of parameters equals parameter size.
104 //
105 // Right now this is for example the case for all closure functions.
106 static ArrayPtr NewBoxed(intptr_t type_args_len,
107 intptr_t num_arguments,
108 Heap::Space space = Heap::kOld) {
109 return New(type_args_len, num_arguments, num_arguments, space);
110 }
111
112 // Allocate and return an arguments descriptor that has no optional
113 // arguments. All arguments are positional. The presence of a type argument
114 // vector as first argument (not counted in num_arguments) is indicated
115 // by a non-zero type_args_len.
116 static ArrayPtr New(intptr_t type_args_len,
117 intptr_t num_arguments,
118 intptr_t size_arguments,
119 Heap::Space space = Heap::kOld);
120
121 // Initialize the preallocated fixed length arguments descriptors cache.
122 static void Init();
123
124 // Clear the preallocated fixed length arguments descriptors cache.
125 static void Cleanup();
126
128
129 // For creating ArgumentDescriptor Slots.
130 static constexpr bool ContainsCompressedPointers() {
131 // Use the same state as the backing store.
133 }
134
135 private:
136 // Absolute indices into the array.
137 // Keep these in sync with the constants in invocation_mirror_patch.dart.
138 enum {
139 kTypeArgsLenIndex,
140 kCountIndex,
141 kSizeIndex,
142 kPositionalCountIndex,
143 kFirstNamedEntryIndex,
144 };
145
146 private:
147 // Relative indexes into each named argument entry.
148 enum {
149 kNameOffset,
150 // The least significant bit of the entry in 'kPositionOffset' (second
151 // least-significant after Smi-encoding) holds the strong-mode checking bit
152 // for the named argument.
153 kPositionOffset,
154 kNamedEntrySize,
155 };
156
157 static intptr_t LengthFor(intptr_t num_named_arguments) {
158 // Add 1 for the terminating null.
159 return kFirstNamedEntryIndex + (kNamedEntrySize * num_named_arguments) + 1;
160 }
161
162 static ArrayPtr NewNonCached(intptr_t type_args_len,
163 intptr_t num_arguments,
164 intptr_t size_arguments,
165 bool canonicalize,
166 Heap::Space space);
167
168 // Used by Simulator to parse argument descriptors.
169 static intptr_t name_index(intptr_t index) {
170 return kFirstNamedEntryIndex + (index * kNamedEntrySize) + kNameOffset;
171 }
172
173 static intptr_t position_index(intptr_t index) {
174 return kFirstNamedEntryIndex + (index * kNamedEntrySize) + kPositionOffset;
175 }
176
177 const Array& array_;
178
179 // A cache of VM heap allocated arguments descriptors.
180 static ArrayPtr cached_args_descriptors_[kCachedDescriptorCount];
181
185};
186
187// DartEntry abstracts functionality needed to resolve dart functions
188// and invoke them from C++.
189class DartEntry : public AllStatic {
190 public:
191 // Invokes the specified instance function or static function.
192 // The first argument of an instance function is the receiver.
193 // On success, returns an InstancePtr. On failure, an ErrorPtr.
194 // This is used when there is no type argument vector and
195 // no named arguments in the call.
197 const Array& arguments);
198
199#if defined(TESTING)
200 // Invokes the specified code as if it was a Dart function.
201 // On success, returns an InstancePtr. On failure, an ErrorPtr.
202 static ObjectPtr InvokeCode(const Code& code,
203 const Array& arguments_descriptor,
204 const Array& arguments,
205 Thread* thread);
206#endif
207
208 // Invokes the specified instance, static, or closure function.
209 // On success, returns an InstancePtr. On failure, an ErrorPtr.
211 const Array& arguments,
212 const Array& arguments_descriptor);
213
214 // Invokes the first argument in the provided arguments array as a callable
215 // object, performing any needed dynamic checks if the callable cannot receive
216 // dynamic invocation.
217 //
218 // On success, returns an InstancePtr. On failure, an ErrorPtr.
219 //
220 // Used when an ArgumentsDescriptor is not required, that is, when there
221 // are no type arguments or named arguments.
222 static ObjectPtr InvokeClosure(Thread* thread, const Array& arguments);
223
224 // Invokes the first argument in the provided arguments array as a callable
225 // object, performing any needed dynamic checks if the callable cannot receive
226 // dynamic invocation.
227 //
228 // On success, returns an InstancePtr. On failure, an ErrorPtr.
229 static ObjectPtr InvokeClosure(Thread* thread,
230 const Array& arguments,
231 const Array& arguments_descriptor);
232
233 // Invokes the noSuchMethod instance function on the receiver.
234 // On success, returns an InstancePtr. On failure, an ErrorPtr.
235 static ObjectPtr InvokeNoSuchMethod(Thread* thread,
236 const Instance& receiver,
237 const String& target_name,
238 const Array& arguments,
239 const Array& arguments_descriptor);
240
241 private:
242 // Resolves the first argument in the provided arguments array to a callable
243 // compatible with the arguments. Helper method used within InvokeClosure.
244 //
245 // If no errors occur, the first argument is changed to be either the resolved
246 // callable or, if Function::null() is returned, an appropriate target for
247 // invoking noSuchMethod.
248 //
249 // On success, returns a FunctionPtr. On failure, an ErrorPtr.
250 static ObjectPtr ResolveCallable(Thread* thread,
251 const Array& arguments,
252 const Array& arguments_descriptor);
253
254 // Invokes a function returned by ResolveCallable, performing any dynamic
255 // checks needed if the function cannot receive dynamic invocation. Helper
256 // method used within InvokeClosure.
257 //
258 // On success, returns an InstancePtr. On failure, an ErrorPtr.
259 static ObjectPtr InvokeCallable(Thread* thread,
260 const Function& callable_function,
261 const Array& arguments,
262 const Array& arguments_descriptor);
263};
264
265// Utility functions to call from VM into Dart bootstrap libraries.
266// Each may return an exception object.
268 public:
269 // On success, returns an InstancePtr. On failure, an ErrorPtr.
270 static ObjectPtr InstanceCreate(const Library& library,
271 const String& exception_name,
272 const String& constructor_name,
273 const Array& arguments);
274
275 // On success, returns an InstancePtr. On failure, an ErrorPtr.
276 static ObjectPtr ToString(const Instance& receiver);
277
278 // On success, returns an InstancePtr. On failure, an ErrorPtr.
279 static ObjectPtr HashCode(const Instance& receiver);
280
281 // On success, returns an InstancePtr. On failure, an ErrorPtr.
282 static ObjectPtr Equals(const Instance& left, const Instance& right);
283
284 // Returns the handler if one has been registered for this port id.
285 static ObjectPtr LookupHandler(Dart_Port port_id);
286
287 // Returns handler on success, an ErrorPtr on failure, null if can't find
288 // handler for this port id.
289 static ObjectPtr HandleMessage(Dart_Port port_id, const Instance& message);
290
291 // Invokes the finalizer to run its callbacks.
292 static ObjectPtr HandleFinalizerMessage(const FinalizerBase& finalizer);
293
294 // Returns a list of open ReceivePorts.
295 static ObjectPtr LookupOpenPorts();
296
297 // Returns null on success, an ErrorPtr on failure.
299
300 // Ensures that the isolate's _pendingImmediateCallback is set to
301 // _startMicrotaskLoop from dart:async.
302 // Returns null on success, an ErrorPtr on failure.
304
305 // Runs the `_rehashObjects()` function in `dart:collection`.
307 Thread* thread,
308 const Object& array_or_growable_array);
309
310 // Runs the `_rehashObjects()` function in `dart:core`.
312 Thread* thread,
313 const Object& array_or_growable_array);
314};
315
316} // namespace dart
317
318#endif // RUNTIME_VM_DART_ENTRY_H_
static intptr_t type_args_len_offset()
Definition: dart_entry.h:53
intptr_t PositionalCount() const
Definition: dart_entry.cc:371
bool MatchesNameAt(intptr_t i, const String &other) const
Definition: dart_entry.cc:389
static intptr_t position_offset()
Definition: dart_entry.h:70
intptr_t NamedCount() const
Definition: dart_entry.h:43
ArgumentsDescriptor(const Array &array)
Definition: dart_entry.cc:357
const char * ToCString() const
Definition: dart_entry.cc:438
static intptr_t named_entry_size()
Definition: dart_entry.h:73
static intptr_t size_offset()
Definition: dart_entry.h:59
static intptr_t positional_count_offset()
Definition: dart_entry.h:61
static intptr_t count_offset()
Definition: dart_entry.h:57
static ArrayPtr New(intptr_t type_args_len, intptr_t num_arguments, intptr_t size_arguments, const Array &optional_arguments_names, Heap::Space space=Heap::kOld)
Definition: dart_entry.cc:444
intptr_t Count() const
Definition: dart_entry.cc:363
static ArrayPtr NewBoxed(intptr_t type_args_len, intptr_t num_arguments, Heap::Space space=Heap::kOld)
Definition: dart_entry.h:106
void PrintTo(BaseTextBuffer *buffer, bool show_named_positions=false) const
Definition: dart_entry.cc:414
ArrayPtr GetArgumentNames() const
Definition: dart_entry.cc:394
intptr_t SizeWithTypeArgs() const
Definition: dart_entry.h:41
static constexpr bool ContainsCompressedPointers()
Definition: dart_entry.h:130
static intptr_t name_offset()
Definition: dart_entry.h:69
static ArrayPtr NewBoxed(intptr_t type_args_len, intptr_t num_arguments, const Array &optional_arguments_names, Heap::Space space=Heap::kOld)
Definition: dart_entry.h:83
static intptr_t first_named_entry_offset()
Definition: dart_entry.h:65
intptr_t CountWithTypeArgs() const
Definition: dart_entry.h:38
intptr_t FirstArgIndex() const
Definition: dart_entry.h:37
intptr_t Size() const
Definition: dart_entry.cc:367
intptr_t TypeArgsLen() const
Definition: dart_entry.cc:359
intptr_t PositionAt(intptr_t i) const
Definition: dart_entry.cc:383
StringPtr NameAt(intptr_t i) const
Definition: dart_entry.cc:375
static intptr_t element_offset(intptr_t index)
Definition: object.h:10838
static ObjectPtr InvokeNoSuchMethod(Thread *thread, const Instance &receiver, const String &target_name, const Array &arguments, const Array &arguments_descriptor)
Definition: dart_entry.cc:307
static ObjectPtr InvokeClosure(Thread *thread, const Array &arguments)
Definition: dart_entry.cc:282
static ObjectPtr InvokeFunction(const Function &function, const Array &arguments)
Definition: dart_entry.cc:31
static ObjectPtr HashCode(const Instance &receiver)
Definition: dart_entry.cc:631
static ObjectPtr HandleMessage(Dart_Port port_id, const Instance &message)
Definition: dart_entry.cc:701
static ObjectPtr RehashObjectsInDartCollection(Thread *thread, const Object &array_or_growable_array)
Definition: dart_entry.cc:792
static ObjectPtr LookupHandler(Dart_Port port_id)
Definition: dart_entry.cc:664
static ObjectPtr EnsureScheduleImmediate()
Definition: dart_entry.cc:763
static ObjectPtr HandleFinalizerMessage(const FinalizerBase &finalizer)
Definition: dart_entry.cc:721
static ObjectPtr RehashObjectsInDartCore(Thread *thread, const Object &array_or_growable_array)
Definition: dart_entry.cc:801
static ObjectPtr InstanceCreate(const Library &library, const String &exception_name, const String &constructor_name, const Array &arguments)
Definition: dart_entry.cc:583
static ObjectPtr LookupOpenPorts()
Definition: dart_entry.cc:679
static ObjectPtr Equals(const Instance &left, const Instance &right)
Definition: dart_entry.cc:647
static ObjectPtr ToString(const Instance &receiver)
Definition: dart_entry.cc:615
static ObjectPtr DrainMicrotaskQueue()
Definition: dart_entry.cc:750
@ kOld
Definition: heap.h:39
static constexpr bool ContainsCompressedPointers()
Definition: object.h:329
int64_t Dart_Port
Definition: dart_api.h:1525
Dart_NativeFunction function
Definition: fuchsia.cc:51
Win32Message message
Definition: dart_vm.cc:33
static constexpr intptr_t kCompressedWordSize
Definition: globals.h:42
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: globals.h:581