Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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 bool left(const SkPoint &p0, const SkPoint &p1)
static bool right(const SkPoint &p0, const SkPoint &p1)
static intptr_t type_args_len_offset()
Definition dart_entry.h:53
intptr_t PositionalCount() const
bool MatchesNameAt(intptr_t i, const String &other) const
static intptr_t position_offset()
Definition dart_entry.h:70
intptr_t NamedCount() const
Definition dart_entry.h:43
const char * ToCString() const
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)
intptr_t Count() const
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
ArrayPtr GetArgumentNames() const
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
intptr_t TypeArgsLen() const
intptr_t PositionAt(intptr_t i) const
StringPtr NameAt(intptr_t i) const
static intptr_t element_offset(intptr_t index)
Definition object.h:10817
static ObjectPtr InvokeNoSuchMethod(Thread *thread, const Instance &receiver, const String &target_name, const Array &arguments, const Array &arguments_descriptor)
static ObjectPtr InvokeClosure(Thread *thread, const Array &arguments)
static ObjectPtr InvokeFunction(const Function &function, const Array &arguments)
Definition dart_entry.cc:31
static ObjectPtr HashCode(const Instance &receiver)
static ObjectPtr HandleMessage(Dart_Port port_id, const Instance &message)
static ObjectPtr RehashObjectsInDartCollection(Thread *thread, const Object &array_or_growable_array)
static ObjectPtr LookupHandler(Dart_Port port_id)
static ObjectPtr EnsureScheduleImmediate()
static ObjectPtr HandleFinalizerMessage(const FinalizerBase &finalizer)
static ObjectPtr RehashObjectsInDartCore(Thread *thread, const Object &array_or_growable_array)
static ObjectPtr InstanceCreate(const Library &library, const String &exception_name, const String &constructor_name, const Array &arguments)
static ObjectPtr LookupOpenPorts()
static ObjectPtr Equals(const Instance &left, const Instance &right)
static ObjectPtr ToString(const Instance &receiver)
static ObjectPtr DrainMicrotaskQueue()
@ kOld
Definition heap.h:39
static constexpr bool ContainsCompressedPointers()
Definition object.h:329
int64_t Dart_Port
Definition dart_api.h:1524
static const uint8_t buffer[]
Dart_NativeFunction function
Definition fuchsia.cc:51
Win32Message message
static constexpr intptr_t kCompressedWordSize
Definition globals.h:42
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581