• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
3  * for details. All rights reserved. Use of this source code is governed by a
4  * BSD-style license that can be found in the LICENSE file.
5  */
6 
7 #ifndef RUNTIME_INCLUDE_DART_API_H_
8 #define RUNTIME_INCLUDE_DART_API_H_
9 
10 /** \mainpage Dart Embedding API Reference
11  *
12  * This reference describes the Dart Embedding API, which is used to embed the
13  * Dart Virtual Machine within C/C++ applications.
14  *
15  * This reference is generated from the header include/dart_api.h.
16  */
17 
18 /* __STDC_FORMAT_MACROS has to be defined before including <inttypes.h> to
19  * enable platform independent printf format specifiers. */
20 #ifndef __STDC_FORMAT_MACROS
21 #define __STDC_FORMAT_MACROS
22 #endif
23 
24 #include <assert.h>
25 #include <inttypes.h>
26 #include <stdbool.h>
27 
28 #ifdef __cplusplus
29 #define DART_EXTERN_C extern "C"
30 #else
31 #define DART_EXTERN_C
32 #endif
33 
34 #if defined(__CYGWIN__)
35 #error Tool chain and platform not supported.
36 #elif defined(_WIN32)
37 #if defined(DART_SHARED_LIB)
38 #define DART_EXPORT DART_EXTERN_C __declspec(dllexport)
39 #else
40 #define DART_EXPORT DART_EXTERN_C
41 #endif
42 #else
43 #if __GNUC__ >= 4
44 #if defined(DART_SHARED_LIB)
45 #define DART_EXPORT                                                            \
46   DART_EXTERN_C __attribute__((visibility("default"))) __attribute((used))
47 #else
48 #define DART_EXPORT DART_EXTERN_C
49 #endif
50 #else
51 #error Tool chain not supported.
52 #endif
53 #endif
54 
55 #if __GNUC__
56 #define DART_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
57 #elif _MSC_VER
58 #define DART_WARN_UNUSED_RESULT _Check_return_
59 #else
60 #define DART_WARN_UNUSED_RESULT
61 #endif
62 
63 /*
64  * =======
65  * Handles
66  * =======
67  */
68 
69 /**
70  * An isolate is the unit of concurrency in Dart. Each isolate has
71  * its own memory and thread of control. No state is shared between
72  * isolates. Instead, isolates communicate by message passing.
73  *
74  * Each thread keeps track of its current isolate, which is the
75  * isolate which is ready to execute on the current thread. The
76  * current isolate may be NULL, in which case no isolate is ready to
77  * execute. Most of the Dart apis require there to be a current
78  * isolate in order to function without error. The current isolate is
79  * set by any call to Dart_CreateIsolateGroup or Dart_EnterIsolate.
80  */
81 typedef struct _Dart_Isolate* Dart_Isolate;
82 typedef struct _Dart_IsolateGroup* Dart_IsolateGroup;
83 
84 /**
85  * An object reference managed by the Dart VM garbage collector.
86  *
87  * Because the garbage collector may move objects, it is unsafe to
88  * refer to objects directly. Instead, we refer to objects through
89  * handles, which are known to the garbage collector and updated
90  * automatically when the object is moved. Handles should be passed
91  * by value (except in cases like out-parameters) and should never be
92  * allocated on the heap.
93  *
94  * Most functions in the Dart Embedding API return a handle. When a
95  * function completes normally, this will be a valid handle to an
96  * object in the Dart VM heap. This handle may represent the result of
97  * the operation or it may be a special valid handle used merely to
98  * indicate successful completion. Note that a valid handle may in
99  * some cases refer to the null object.
100  *
101  * --- Error handles ---
102  *
103  * When a function encounters a problem that prevents it from
104  * completing normally, it returns an error handle (See Dart_IsError).
105  * An error handle has an associated error message that gives more
106  * details about the problem (See Dart_GetError).
107  *
108  * There are four kinds of error handles that can be produced,
109  * depending on what goes wrong:
110  *
111  * - Api error handles are produced when an api function is misused.
112  *   This happens when a Dart embedding api function is called with
113  *   invalid arguments or in an invalid context.
114  *
115  * - Unhandled exception error handles are produced when, during the
116  *   execution of Dart code, an exception is thrown but not caught.
117  *   Prototypically this would occur during a call to Dart_Invoke, but
118  *   it can occur in any function which triggers the execution of Dart
119  *   code (for example, Dart_ToString).
120  *
121  *   An unhandled exception error provides access to an exception and
122  *   stacktrace via the functions Dart_ErrorGetException and
123  *   Dart_ErrorGetStackTrace.
124  *
125  * - Compilation error handles are produced when, during the execution
126  *   of Dart code, a compile-time error occurs.  As above, this can
127  *   occur in any function which triggers the execution of Dart code.
128  *
129  * - Fatal error handles are produced when the system wants to shut
130  *   down the current isolate.
131  *
132  * --- Propagating errors ---
133  *
134  * When an error handle is returned from the top level invocation of
135  * Dart code in a program, the embedder must handle the error as they
136  * see fit.  Often, the embedder will print the error message produced
137  * by Dart_Error and exit the program.
138  *
139  * When an error is returned while in the body of a native function,
140  * it can be propagated up the call stack by calling
141  * Dart_PropagateError, Dart_SetReturnValue, or Dart_ThrowException.
142  * Errors should be propagated unless there is a specific reason not
143  * to.  If an error is not propagated then it is ignored.  For
144  * example, if an unhandled exception error is ignored, that
145  * effectively "catches" the unhandled exception.  Fatal errors must
146  * always be propagated.
147  *
148  * When an error is propagated, any current scopes created by
149  * Dart_EnterScope will be exited.
150  *
151  * Using Dart_SetReturnValue to propagate an exception is somewhat
152  * more convenient than using Dart_PropagateError, and should be
153  * preferred for reasons discussed below.
154  *
155  * Dart_PropagateError and Dart_ThrowException do not return.  Instead
156  * they transfer control non-locally using a setjmp-like mechanism.
157  * This can be inconvenient if you have resources that you need to
158  * clean up before propagating the error.
159  *
160  * When relying on Dart_PropagateError, we often return error handles
161  * rather than propagating them from helper functions.  Consider the
162  * following contrived example:
163  *
164  * 1    Dart_Handle isLongStringHelper(Dart_Handle arg) {
165  * 2      intptr_t* length = 0;
166  * 3      result = Dart_StringLength(arg, &length);
167  * 4      if (Dart_IsError(result)) {
168  * 5        return result;
169  * 6      }
170  * 7      return Dart_NewBoolean(length > 100);
171  * 8    }
172  * 9
173  * 10   void NativeFunction_isLongString(Dart_NativeArguments args) {
174  * 11     Dart_EnterScope();
175  * 12     AllocateMyResource();
176  * 13     Dart_Handle arg = Dart_GetNativeArgument(args, 0);
177  * 14     Dart_Handle result = isLongStringHelper(arg);
178  * 15     if (Dart_IsError(result)) {
179  * 16       FreeMyResource();
180  * 17       Dart_PropagateError(result);
181  * 18       abort();  // will not reach here
182  * 19     }
183  * 20     Dart_SetReturnValue(result);
184  * 21     FreeMyResource();
185  * 22     Dart_ExitScope();
186  * 23   }
187  *
188  * In this example, we have a native function which calls a helper
189  * function to do its work.  On line 5, the helper function could call
190  * Dart_PropagateError, but that would not give the native function a
191  * chance to call FreeMyResource(), causing a leak.  Instead, the
192  * helper function returns the error handle to the caller, giving the
193  * caller a chance to clean up before propagating the error handle.
194  *
195  * When an error is propagated by calling Dart_SetReturnValue, the
196  * native function will be allowed to complete normally and then the
197  * exception will be propagated only once the native call
198  * returns. This can be convenient, as it allows the C code to clean
199  * up normally.
200  *
201  * The example can be written more simply using Dart_SetReturnValue to
202  * propagate the error.
203  *
204  * 1    Dart_Handle isLongStringHelper(Dart_Handle arg) {
205  * 2      intptr_t* length = 0;
206  * 3      result = Dart_StringLength(arg, &length);
207  * 4      if (Dart_IsError(result)) {
208  * 5        return result
209  * 6      }
210  * 7      return Dart_NewBoolean(length > 100);
211  * 8    }
212  * 9
213  * 10   void NativeFunction_isLongString(Dart_NativeArguments args) {
214  * 11     Dart_EnterScope();
215  * 12     AllocateMyResource();
216  * 13     Dart_Handle arg = Dart_GetNativeArgument(args, 0);
217  * 14     Dart_SetReturnValue(isLongStringHelper(arg));
218  * 15     FreeMyResource();
219  * 16     Dart_ExitScope();
220  * 17   }
221  *
222  * In this example, the call to Dart_SetReturnValue on line 14 will
223  * either return the normal return value or the error (potentially
224  * generated on line 3).  The call to FreeMyResource on line 15 will
225  * execute in either case.
226  *
227  * --- Local and persistent handles ---
228  *
229  * Local handles are allocated within the current scope (see
230  * Dart_EnterScope) and go away when the current scope exits. Unless
231  * otherwise indicated, callers should assume that all functions in
232  * the Dart embedding api return local handles.
233  *
234  * Persistent handles are allocated within the current isolate. They
235  * can be used to store objects across scopes. Persistent handles have
236  * the lifetime of the current isolate unless they are explicitly
237  * deallocated (see Dart_DeletePersistentHandle).
238  * The type Dart_Handle represents a handle (both local and persistent).
239  * The type Dart_PersistentHandle is a Dart_Handle and it is used to
240  * document that a persistent handle is expected as a parameter to a call
241  * or the return value from a call is a persistent handle.
242  *
243  * FinalizableHandles are persistent handles which are auto deleted when
244  * the object is garbage collected. It is never safe to use these handles
245  * unless you know the object is still reachable.
246  *
247  * WeakPersistentHandles are persistent handles which are automatically set
248  * to point Dart_Null when the object is garbage collected. They are not auto
249  * deleted, so it is safe to use them after the object has become unreachable.
250  */
251 typedef struct _Dart_Handle* Dart_Handle;
252 typedef Dart_Handle Dart_PersistentHandle;
253 typedef struct _Dart_WeakPersistentHandle* Dart_WeakPersistentHandle;
254 typedef struct _Dart_FinalizableHandle* Dart_FinalizableHandle;
255 // These structs are versioned by DART_API_DL_MAJOR_VERSION, bump the
256 // version when changing this struct.
257 
258 typedef void (*Dart_HandleFinalizer)(void* isolate_callback_data, void* peer);
259 
260 /**
261  * Is this an error handle?
262  *
263  * Requires there to be a current isolate.
264  */
265 DART_EXPORT bool Dart_IsError(Dart_Handle handle);
266 
267 /**
268  * Is this an api error handle?
269  *
270  * Api error handles are produced when an api function is misused.
271  * This happens when a Dart embedding api function is called with
272  * invalid arguments or in an invalid context.
273  *
274  * Requires there to be a current isolate.
275  */
276 DART_EXPORT bool Dart_IsApiError(Dart_Handle handle);
277 
278 /**
279  * Is this an unhandled exception error handle?
280  *
281  * Unhandled exception error handles are produced when, during the
282  * execution of Dart code, an exception is thrown but not caught.
283  * This can occur in any function which triggers the execution of Dart
284  * code.
285  *
286  * See Dart_ErrorGetException and Dart_ErrorGetStackTrace.
287  *
288  * Requires there to be a current isolate.
289  */
290 DART_EXPORT bool Dart_IsUnhandledExceptionError(Dart_Handle handle);
291 
292 /**
293  * Is this a compilation error handle?
294  *
295  * Compilation error handles are produced when, during the execution
296  * of Dart code, a compile-time error occurs.  This can occur in any
297  * function which triggers the execution of Dart code.
298  *
299  * Requires there to be a current isolate.
300  */
301 DART_EXPORT bool Dart_IsCompilationError(Dart_Handle handle);
302 
303 /**
304  * Is this a fatal error handle?
305  *
306  * Fatal error handles are produced when the system wants to shut down
307  * the current isolate.
308  *
309  * Requires there to be a current isolate.
310  */
311 DART_EXPORT bool Dart_IsFatalError(Dart_Handle handle);
312 
313 /**
314  * Gets the error message from an error handle.
315  *
316  * Requires there to be a current isolate.
317  *
318  * \return A C string containing an error message if the handle is
319  *   error. An empty C string ("") if the handle is valid. This C
320  *   String is scope allocated and is only valid until the next call
321  *   to Dart_ExitScope.
322 */
323 DART_EXPORT const char* Dart_GetError(Dart_Handle handle);
324 
325 /**
326  * Is this an error handle for an unhandled exception?
327  */
328 DART_EXPORT bool Dart_ErrorHasException(Dart_Handle handle);
329 
330 /**
331  * Gets the exception Object from an unhandled exception error handle.
332  */
333 DART_EXPORT Dart_Handle Dart_ErrorGetException(Dart_Handle handle);
334 
335 /**
336  * Gets the stack trace Object from an unhandled exception error handle.
337  */
338 DART_EXPORT Dart_Handle Dart_ErrorGetStackTrace(Dart_Handle handle);
339 
340 /**
341  * Produces an api error handle with the provided error message.
342  *
343  * Requires there to be a current isolate.
344  *
345  * \param error the error message.
346  */
347 DART_EXPORT Dart_Handle Dart_NewApiError(const char* error);
348 DART_EXPORT Dart_Handle Dart_NewCompilationError(const char* error);
349 
350 /**
351  * Produces a new unhandled exception error handle.
352  *
353  * Requires there to be a current isolate.
354  *
355  * \param exception An instance of a Dart object to be thrown or
356  *        an ApiError or CompilationError handle.
357  *        When an ApiError or CompilationError handle is passed in
358  *        a string object of the error message is created and it becomes
359  *        the Dart object to be thrown.
360  */
361 DART_EXPORT Dart_Handle Dart_NewUnhandledExceptionError(Dart_Handle exception);
362 
363 /**
364  * Propagates an error.
365  *
366  * If the provided handle is an unhandled exception error, this
367  * function will cause the unhandled exception to be rethrown.  This
368  * will proceed in the standard way, walking up Dart frames until an
369  * appropriate 'catch' block is found, executing 'finally' blocks,
370  * etc.
371  *
372  * If the error is not an unhandled exception error, we will unwind
373  * the stack to the next C frame.  Intervening Dart frames will be
374  * discarded; specifically, 'finally' blocks will not execute.  This
375  * is the standard way that compilation errors (and the like) are
376  * handled by the Dart runtime.
377  *
378  * In either case, when an error is propagated any current scopes
379  * created by Dart_EnterScope will be exited.
380  *
381  * See the additional discussion under "Propagating Errors" at the
382  * beginning of this file.
383  *
384  * \param An error handle (See Dart_IsError)
385  *
386  * \return On success, this function does not return.  On failure, the
387  * process is terminated.
388  */
389 DART_EXPORT void Dart_PropagateError(Dart_Handle handle);
390 
391 /**
392  * Converts an object to a string.
393  *
394  * May generate an unhandled exception error.
395  *
396  * \return The converted string if no error occurs during
397  *   the conversion. If an error does occur, an error handle is
398  *   returned.
399  */
400 DART_EXPORT Dart_Handle Dart_ToString(Dart_Handle object);
401 
402 /**
403  * Checks to see if two handles refer to identically equal objects.
404  *
405  * If both handles refer to instances, this is equivalent to using the top-level
406  * function identical() from dart:core. Otherwise, returns whether the two
407  * argument handles refer to the same object.
408  *
409  * \param obj1 An object to be compared.
410  * \param obj2 An object to be compared.
411  *
412  * \return True if the objects are identically equal.  False otherwise.
413  */
414 DART_EXPORT bool Dart_IdentityEquals(Dart_Handle obj1, Dart_Handle obj2);
415 
416 /**
417  * Allocates a handle in the current scope from a persistent handle.
418  */
419 DART_EXPORT Dart_Handle Dart_HandleFromPersistent(Dart_PersistentHandle object);
420 
421 /**
422  * Allocates a handle in the current scope from a weak persistent handle.
423  *
424  * This will be a handle to Dart_Null if the object has been garbage collected.
425  */
426 DART_EXPORT Dart_Handle
427 Dart_HandleFromWeakPersistent(Dart_WeakPersistentHandle object);
428 
429 /**
430  * Allocates a persistent handle for an object.
431  *
432  * This handle has the lifetime of the current isolate unless it is
433  * explicitly deallocated by calling Dart_DeletePersistentHandle.
434  *
435  * Requires there to be a current isolate.
436  */
437 DART_EXPORT Dart_PersistentHandle Dart_NewPersistentHandle(Dart_Handle object);
438 
439 /**
440  * Assign value of local handle to a persistent handle.
441  *
442  * Requires there to be a current isolate.
443  *
444  * \param obj1 A persistent handle whose value needs to be set.
445  * \param obj2 An object whose value needs to be set to the persistent handle.
446  *
447  * \return Success if the persistent handle was set
448  *   Otherwise, returns an error.
449  */
450 DART_EXPORT void Dart_SetPersistentHandle(Dart_PersistentHandle obj1,
451                                           Dart_Handle obj2);
452 
453 /**
454  * Deallocates a persistent handle.
455  *
456  * Requires there to be a current isolate group.
457  */
458 DART_EXPORT void Dart_DeletePersistentHandle(Dart_PersistentHandle object);
459 
460 /**
461  * Allocates a weak persistent handle for an object.
462  *
463  * This handle has the lifetime of the current isolate. The handle can also be
464  * explicitly deallocated by calling Dart_DeleteWeakPersistentHandle.
465  *
466  * If the object becomes unreachable the callback is invoked with the peer as
467  * argument. The callback can be executed on any thread, will have a current
468  * isolate group, but will not have a current isolate. The callback can only
469  * call Dart_DeletePersistentHandle or Dart_DeleteWeakPersistentHandle. This
470  * gives the embedder the ability to cleanup data associated with the object.
471  * The handle will point to the Dart_Null object after the finalizer has been
472  * run. It is illegal to call into the VM with any other Dart_* functions from
473  * the callback. If the handle is deleted before the object becomes
474  * unreachable, the callback is never invoked.
475  *
476  * Requires there to be a current isolate.
477  *
478  * \param object An object.
479  * \param peer A pointer to a native object or NULL.  This value is
480  *   provided to callback when it is invoked.
481  * \param external_allocation_size The number of externally allocated
482  *   bytes for peer. Used to inform the garbage collector.
483  * \param callback A function pointer that will be invoked sometime
484  *   after the object is garbage collected, unless the handle has been deleted.
485  *   A valid callback needs to be specified it cannot be NULL.
486  *
487  * \return The weak persistent handle or NULL. NULL is returned in case of bad
488  *   parameters.
489  */
490 DART_EXPORT Dart_WeakPersistentHandle
491 Dart_NewWeakPersistentHandle(Dart_Handle object,
492                              void* peer,
493                              intptr_t external_allocation_size,
494                              Dart_HandleFinalizer callback);
495 
496 /**
497  * Deletes the given weak persistent [object] handle.
498  *
499  * Requires there to be a current isolate group.
500  */
501 DART_EXPORT void Dart_DeleteWeakPersistentHandle(
502     Dart_WeakPersistentHandle object);
503 
504 /**
505  * Updates the external memory size for the given weak persistent handle.
506  *
507  * May trigger garbage collection.
508  */
509 DART_EXPORT void Dart_UpdateExternalSize(Dart_WeakPersistentHandle object,
510                                          intptr_t external_allocation_size);
511 
512 /**
513  * Allocates a finalizable handle for an object.
514  *
515  * This handle has the lifetime of the current isolate group unless the object
516  * pointed to by the handle is garbage collected, in this case the VM
517  * automatically deletes the handle after invoking the callback associated
518  * with the handle. The handle can also be explicitly deallocated by
519  * calling Dart_DeleteFinalizableHandle.
520  *
521  * If the object becomes unreachable the callback is invoked with the
522  * the peer as argument. The callback can be executed on any thread, will have
523  * an isolate group, but will not have a current isolate. The callback can only
524  * call Dart_DeletePersistentHandle or Dart_DeleteWeakPersistentHandle.
525  * This gives the embedder the ability to cleanup data associated with the
526  * object and clear out any cached references to the handle. All references to
527  * this handle after the callback will be invalid. It is illegal to call into
528  * the VM with any other Dart_* functions from the callback. If the handle is
529  * deleted before the object becomes unreachable, the callback is never
530  * invoked.
531  *
532  * Requires there to be a current isolate.
533  *
534  * \param object An object.
535  * \param peer A pointer to a native object or NULL.  This value is
536  *   provided to callback when it is invoked.
537  * \param external_allocation_size The number of externally allocated
538  *   bytes for peer. Used to inform the garbage collector.
539  * \param callback A function pointer that will be invoked sometime
540  *   after the object is garbage collected, unless the handle has been deleted.
541  *   A valid callback needs to be specified it cannot be NULL.
542  *
543  * \return The finalizable handle or NULL. NULL is returned in case of bad
544  *   parameters.
545  */
546 DART_EXPORT Dart_FinalizableHandle
547 Dart_NewFinalizableHandle(Dart_Handle object,
548                           void* peer,
549                           intptr_t external_allocation_size,
550                           Dart_HandleFinalizer callback);
551 
552 /**
553  * Deletes the given finalizable [object] handle.
554  *
555  * The caller has to provide the actual Dart object the handle was created from
556  * to prove the object (and therefore the finalizable handle) is still alive.
557  *
558  * Requires there to be a current isolate.
559  */
560 DART_EXPORT void Dart_DeleteFinalizableHandle(Dart_FinalizableHandle object,
561                                               Dart_Handle strong_ref_to_object);
562 
563 /**
564  * Updates the external memory size for the given finalizable handle.
565  *
566  * The caller has to provide the actual Dart object the handle was created from
567  * to prove the object (and therefore the finalizable handle) is still alive.
568  *
569  * May trigger garbage collection.
570  */
571 DART_EXPORT void Dart_UpdateFinalizableExternalSize(
572     Dart_FinalizableHandle object,
573     Dart_Handle strong_ref_to_object,
574     intptr_t external_allocation_size);
575 
576 /*
577  * ==========================
578  * Initialization and Globals
579  * ==========================
580  */
581 
582 /**
583  * Gets the version string for the Dart VM.
584  *
585  * The version of the Dart VM can be accessed without initializing the VM.
586  *
587  * \return The version string for the embedded Dart VM.
588  */
589 DART_EXPORT const char* Dart_VersionString();
590 
591 typedef struct {
592   const char* library_uri;
593   const char* class_name;
594   const char* function_name;
595 } Dart_QualifiedFunctionName;
596 
597 /**
598  * Isolate specific flags are set when creating a new isolate using the
599  * Dart_IsolateFlags structure.
600  *
601  * Current version of flags is encoded in a 32-bit integer with 16 bits used
602  * for each part.
603  */
604 
605 #define DART_FLAGS_CURRENT_VERSION (0x0000000c)
606 
607 typedef struct {
608   int32_t version;
609   bool enable_asserts;
610   bool use_field_guards;
611   bool use_osr;
612   bool obfuscate;
613   Dart_QualifiedFunctionName* entry_points;
614   bool load_vmservice_library;
615   bool copy_parent_code;
616   bool null_safety;
617   bool is_system_isolate;
618 } Dart_IsolateFlags;
619 
620 /**
621  * Initialize Dart_IsolateFlags with correct version and default values.
622  */
623 DART_EXPORT void Dart_IsolateFlagsInitialize(Dart_IsolateFlags* flags);
624 
625 /**
626  * An isolate creation and initialization callback function.
627  *
628  * This callback, provided by the embedder, is called when the VM
629  * needs to create an isolate. The callback should create an isolate
630  * by calling Dart_CreateIsolateGroup and load any scripts required for
631  * execution.
632  *
633  * This callback may be called on a different thread than the one
634  * running the parent isolate.
635  *
636  * When the function returns NULL, it is the responsibility of this
637  * function to ensure that Dart_ShutdownIsolate has been called if
638  * required (for example, if the isolate was created successfully by
639  * Dart_CreateIsolateGroup() but the root library fails to load
640  * successfully, then the function should call Dart_ShutdownIsolate
641  * before returning).
642  *
643  * When the function returns NULL, the function should set *error to
644  * a malloc-allocated buffer containing a useful error message.  The
645  * caller of this function (the VM) will make sure that the buffer is
646  * freed.
647  *
648  * \param script_uri The uri of the main source file or snapshot to load.
649  *   Either the URI of the parent isolate set in Dart_CreateIsolateGroup for
650  *   Isolate.spawn, or the argument to Isolate.spawnUri canonicalized by the
651  *   library tag handler of the parent isolate.
652  *   The callback is responsible for loading the program by a call to
653  *   Dart_LoadScriptFromKernel.
654  * \param main The name of the main entry point this isolate will
655  *   eventually run.  This is provided for advisory purposes only to
656  *   improve debugging messages.  The main function is not invoked by
657  *   this function.
658  * \param package_root Ignored.
659  * \param package_config Uri of the package configuration file (either in format
660  *   of .packages or .dart_tool/package_config.json) for this isolate
661  *   to resolve package imports against. If this parameter is not passed the
662  *   package resolution of the parent isolate should be used.
663  * \param flags Default flags for this isolate being spawned. Either inherited
664  *   from the spawning isolate or passed as parameters when spawning the
665  *   isolate from Dart code.
666  * \param isolate_data The isolate data which was passed to the
667  *   parent isolate when it was created by calling Dart_CreateIsolateGroup().
668  * \param error A structure into which the embedder can place a
669  *   C string containing an error message in the case of failures.
670  *
671  * \return The embedder returns NULL if the creation and
672  *   initialization was not successful and the isolate if successful.
673  */
674 typedef Dart_Isolate (*Dart_IsolateGroupCreateCallback)(
675     const char* script_uri,
676     const char* main,
677     const char* package_root,
678     const char* package_config,
679     Dart_IsolateFlags* flags,
680     void* isolate_data,
681     char** error);
682 
683 /**
684  * An isolate initialization callback function.
685  *
686  * This callback, provided by the embedder, is called when the VM has created an
687  * isolate within an existing isolate group (i.e. from the same source as an
688  * existing isolate).
689  *
690  * The callback should setup native resolvers and might want to set a custom
691  * message handler via [Dart_SetMessageNotifyCallback] and mark the isolate as
692  * runnable.
693  *
694  * This callback may be called on a different thread than the one
695  * running the parent isolate.
696  *
697  * When the function returns `false`, it is the responsibility of this
698  * function to ensure that `Dart_ShutdownIsolate` has been called.
699  *
700  * When the function returns `false`, the function should set *error to
701  * a malloc-allocated buffer containing a useful error message.  The
702  * caller of this function (the VM) will make sure that the buffer is
703  * freed.
704  *
705  * \param child_isolate_data The callback data to associate with the new
706  *        child isolate.
707  * \param error A structure into which the embedder can place a
708  *   C string containing an error message in the case the initialization fails.
709  *
710  * \return The embedder returns true if the initialization was successful and
711  *         false otherwise (in which case the VM will terminate the isolate).
712  */
713 typedef bool (*Dart_InitializeIsolateCallback)(void** child_isolate_data,
714                                                char** error);
715 
716 /**
717  * An isolate unhandled exception callback function.
718  *
719  * This callback has been DEPRECATED.
720  */
721 typedef void (*Dart_IsolateUnhandledExceptionCallback)(Dart_Handle error);
722 
723 /**
724  * An isolate shutdown callback function.
725  *
726  * This callback, provided by the embedder, is called before the vm
727  * shuts down an isolate.  The isolate being shutdown will be the current
728  * isolate. It is safe to run Dart code.
729  *
730  * This function should be used to dispose of native resources that
731  * are allocated to an isolate in order to avoid leaks.
732  *
733  * \param isolate_group_data The same callback data which was passed to the
734  *   isolate group when it was created.
735  * \param isolate_data The same callback data which was passed to the isolate
736  *   when it was created.
737  */
738 typedef void (*Dart_IsolateShutdownCallback)(void* isolate_group_data,
739                                              void* isolate_data);
740 
741 /**
742  * An isolate cleanup callback function.
743  *
744  * This callback, provided by the embedder, is called after the vm
745  * shuts down an isolate. There will be no current isolate and it is *not*
746  * safe to run Dart code.
747  *
748  * This function should be used to dispose of native resources that
749  * are allocated to an isolate in order to avoid leaks.
750  *
751  * \param isolate_group_data The same callback data which was passed to the
752  *   isolate group when it was created.
753  * \param isolate_data The same callback data which was passed to the isolate
754  *   when it was created.
755  */
756 typedef void (*Dart_IsolateCleanupCallback)(void* isolate_group_data,
757                                             void* isolate_data);
758 
759 /**
760  * An isolate group cleanup callback function.
761  *
762  * This callback, provided by the embedder, is called after the vm
763  * shuts down an isolate group.
764  *
765  * This function should be used to dispose of native resources that
766  * are allocated to an isolate in order to avoid leaks.
767  *
768  * \param isolate_group_data The same callback data which was passed to the
769  *   isolate group when it was created.
770  *
771  */
772 typedef void (*Dart_IsolateGroupCleanupCallback)(void* isolate_group_data);
773 
774 /**
775  * A thread death callback function.
776  * This callback, provided by the embedder, is called before a thread in the
777  * vm thread pool exits.
778  * This function could be used to dispose of native resources that
779  * are associated and attached to the thread, in order to avoid leaks.
780  */
781 typedef void (*Dart_ThreadExitCallback)();
782 
783 /**
784  * Callbacks provided by the embedder for file operations. If the
785  * embedder does not allow file operations these callbacks can be
786  * NULL.
787  *
788  * Dart_FileOpenCallback - opens a file for reading or writing.
789  * \param name The name of the file to open.
790  * \param write A boolean variable which indicates if the file is to
791  *   opened for writing. If there is an existing file it needs to truncated.
792  *
793  * Dart_FileReadCallback - Read contents of file.
794  * \param data Buffer allocated in the callback into which the contents
795  *   of the file are read into. It is the responsibility of the caller to
796  *   free this buffer.
797  * \param file_length A variable into which the length of the file is returned.
798  *   In the case of an error this value would be -1.
799  * \param stream Handle to the opened file.
800  *
801  * Dart_FileWriteCallback - Write data into file.
802  * \param data Buffer which needs to be written into the file.
803  * \param length Length of the buffer.
804  * \param stream Handle to the opened file.
805  *
806  * Dart_FileCloseCallback - Closes the opened file.
807  * \param stream Handle to the opened file.
808  *
809  */
810 typedef void* (*Dart_FileOpenCallback)(const char* name, bool write);
811 
812 typedef void (*Dart_FileReadCallback)(uint8_t** data,
813                                       intptr_t* file_length,
814                                       void* stream);
815 
816 typedef void (*Dart_FileWriteCallback)(const void* data,
817                                        intptr_t length,
818                                        void* stream);
819 
820 typedef void (*Dart_FileCloseCallback)(void* stream);
821 
822 typedef bool (*Dart_EntropySource)(uint8_t* buffer, intptr_t length);
823 
824 /**
825  * Callback provided by the embedder that is used by the vmservice isolate
826  * to request the asset archive. The asset archive must be an uncompressed tar
827  * archive that is stored in a Uint8List.
828  *
829  * If the embedder has no vmservice isolate assets, the callback can be NULL.
830  *
831  * \return The embedder must return a handle to a Uint8List containing an
832  *   uncompressed tar archive or null.
833  */
834 typedef Dart_Handle (*Dart_GetVMServiceAssetsArchive)();
835 
836 /**
837  * The current version of the Dart_InitializeFlags. Should be incremented every
838  * time Dart_InitializeFlags changes in a binary incompatible way.
839  */
840 #define DART_INITIALIZE_PARAMS_CURRENT_VERSION (0x00000004)
841 
842 /** Forward declaration */
843 struct Dart_CodeObserver;
844 
845 /**
846  * Callback provided by the embedder that is used by the VM to notify on code
847  * object creation, *before* it is invoked the first time.
848  * This is useful for embedders wanting to e.g. keep track of PCs beyond
849  * the lifetime of the garbage collected code objects.
850  * Note that an address range may be used by more than one code object over the
851  * lifecycle of a process. Clients of this function should record timestamps for
852  * these compilation events and when collecting PCs to disambiguate reused
853  * address ranges.
854  */
855 typedef void (*Dart_OnNewCodeCallback)(struct Dart_CodeObserver* observer,
856                                        const char* name,
857                                        uintptr_t base,
858                                        uintptr_t size);
859 
860 typedef struct Dart_CodeObserver {
861   void* data;
862 
863   Dart_OnNewCodeCallback on_new_code;
864 } Dart_CodeObserver;
865 
866 /**
867  * Describes how to initialize the VM. Used with Dart_Initialize.
868  *
869  * \param version Identifies the version of the struct used by the client.
870  *   should be initialized to DART_INITIALIZE_PARAMS_CURRENT_VERSION.
871  * \param vm_isolate_snapshot A buffer containing a snapshot of the VM isolate
872  *   or NULL if no snapshot is provided. If provided, the buffer must remain
873  *   valid until Dart_Cleanup returns.
874  * \param instructions_snapshot A buffer containing a snapshot of precompiled
875  *   instructions, or NULL if no snapshot is provided. If provided, the buffer
876  *   must remain valid until Dart_Cleanup returns.
877  * \param initialize_isolate A function to be called during isolate
878  *   initialization inside an existing isolate group.
879  *   See Dart_InitializeIsolateCallback.
880  * \param create_group A function to be called during isolate group creation.
881  *   See Dart_IsolateGroupCreateCallback.
882  * \param shutdown A function to be called right before an isolate is shutdown.
883  *   See Dart_IsolateShutdownCallback.
884  * \param cleanup A function to be called after an isolate was shutdown.
885  *   See Dart_IsolateCleanupCallback.
886  * \param cleanup_group A function to be called after an isolate group is shutdown.
887  *   See Dart_IsolateGroupCleanupCallback.
888  * \param get_service_assets A function to be called by the service isolate when
889  *    it requires the vmservice assets archive.
890  *    See Dart_GetVMServiceAssetsArchive.
891  * \param code_observer An external code observer callback function.
892  *    The observer can be invoked as early as during the Dart_Initialize() call.
893  */
894 typedef struct {
895   int32_t version;
896   const uint8_t* vm_snapshot_data;
897   const uint8_t* vm_snapshot_instructions;
898   Dart_IsolateGroupCreateCallback create_group;
899   Dart_InitializeIsolateCallback initialize_isolate;
900   Dart_IsolateShutdownCallback shutdown_isolate;
901   Dart_IsolateCleanupCallback cleanup_isolate;
902   Dart_IsolateGroupCleanupCallback cleanup_group;
903   Dart_ThreadExitCallback thread_exit;
904   Dart_FileOpenCallback file_open;
905   Dart_FileReadCallback file_read;
906   Dart_FileWriteCallback file_write;
907   Dart_FileCloseCallback file_close;
908   Dart_EntropySource entropy_source;
909   Dart_GetVMServiceAssetsArchive get_service_assets;
910   bool start_kernel_isolate;
911   Dart_CodeObserver* code_observer;
912 } Dart_InitializeParams;
913 
914 /**
915  * Initializes the VM.
916  *
917  * \param params A struct containing initialization information. The version
918  *   field of the struct must be DART_INITIALIZE_PARAMS_CURRENT_VERSION.
919  *
920  * \return NULL if initialization is successful. Returns an error message
921  *   otherwise. The caller is responsible for freeing the error message.
922  */
923 DART_EXPORT DART_WARN_UNUSED_RESULT char* Dart_Initialize(
924     Dart_InitializeParams* params);
925 
926 /**
927  * Cleanup state in the VM before process termination.
928  *
929  * \return NULL if cleanup is successful. Returns an error message otherwise.
930  *   The caller is responsible for freeing the error message.
931  *
932  * NOTE: This function must not be called on a thread that was created by the VM
933  * itself.
934  */
935 DART_EXPORT DART_WARN_UNUSED_RESULT char* Dart_Cleanup();
936 
937 /**
938  * Sets command line flags. Should be called before Dart_Initialize.
939  *
940  * \param argc The length of the arguments array.
941  * \param argv An array of arguments.
942  *
943  * \return NULL if successful. Returns an error message otherwise.
944  *  The caller is responsible for freeing the error message.
945  *
946  * NOTE: This call does not store references to the passed in c-strings.
947  */
948 DART_EXPORT DART_WARN_UNUSED_RESULT char* Dart_SetVMFlags(int argc,
949                                                           const char** argv);
950 
951 /**
952  * Returns true if the named VM flag is of boolean type, specified, and set to
953  * true.
954  *
955  * \param flag_name The name of the flag without leading punctuation
956  *                  (example: "enable_asserts").
957  */
958 DART_EXPORT bool Dart_IsVMFlagSet(const char* flag_name);
959 
960 /*
961  * ========
962  * Isolates
963  * ========
964  */
965 
966 /**
967  * Creates a new isolate. The new isolate becomes the current isolate.
968  *
969  * A snapshot can be used to restore the VM quickly to a saved state
970  * and is useful for fast startup. If snapshot data is provided, the
971  * isolate will be started using that snapshot data. Requires a core snapshot or
972  * an app snapshot created by Dart_CreateSnapshot or
973  * Dart_CreatePrecompiledSnapshot* from a VM with the same version.
974  *
975  * Requires there to be no current isolate.
976  *
977  * \param script_uri The main source file or snapshot this isolate will load.
978  *   The VM will provide this URI to the Dart_IsolateGroupCreateCallback when a child
979  *   isolate is created by Isolate.spawn. The embedder should use a URI that
980  *   allows it to load the same program into such a child isolate.
981  * \param name A short name for the isolate to improve debugging messages.
982  *   Typically of the format 'foo.dart:main()'.
983  * \param isolate_snapshot_data
984  * \param isolate_snapshot_instructions Buffers containing a snapshot of the
985  *   isolate or NULL if no snapshot is provided. If provided, the buffers must
986  *   remain valid until the isolate shuts down.
987  * \param flags Pointer to VM specific flags or NULL for default flags.
988  * \param isolate_group_data Embedder group data. This data can be obtained
989  *   by calling Dart_IsolateGroupData and will be passed to the
990  *   Dart_IsolateShutdownCallback, Dart_IsolateCleanupCallback, and
991  *   Dart_IsolateGroupCleanupCallback.
992  * \param isolate_data Embedder data.  This data will be passed to
993  *   the Dart_IsolateGroupCreateCallback when new isolates are spawned from
994  *   this parent isolate.
995  * \param error Returns NULL if creation is successful, an error message
996  *   otherwise. The caller is responsible for calling free() on the error
997  *   message.
998  *
999  * \return The new isolate on success, or NULL if isolate creation failed.
1000  */
1001 DART_EXPORT Dart_Isolate
1002 Dart_CreateIsolateGroup(const char* script_uri,
1003                         const char* name,
1004                         const uint8_t* isolate_snapshot_data,
1005                         const uint8_t* isolate_snapshot_instructions,
1006                         Dart_IsolateFlags* flags,
1007                         void* isolate_group_data,
1008                         void* isolate_data,
1009                         char** error);
1010 /**
1011  * Creates a new isolate inside the isolate group of [group_member].
1012  *
1013  * Requires there to be no current isolate.
1014  *
1015  * \param group_member An isolate from the same group into which the newly created
1016  *   isolate should be born into. Other threads may not have entered / enter this
1017  *   member isolate.
1018  * \param name A short name for the isolate for debugging purposes.
1019  * \param shutdown_callback A callback to be called when the isolate is being
1020  *   shutdown (may be NULL).
1021  * \param cleanup_callback A callback to be called when the isolate is being
1022  *   cleaned up (may be NULL).
1023  * \param isolate_data The embedder-specific data associated with this isolate.
1024  * \param error Set to NULL if creation is successful, set to an error
1025  *   message otherwise. The caller is responsible for calling free() on the
1026  *   error message.
1027  *
1028  * \return The newly created isolate on success, or NULL if isolate creation
1029  *   failed.
1030  *
1031  * If successful, the newly created isolate will become the current isolate.
1032  */
1033 DART_EXPORT Dart_Isolate
1034 Dart_CreateIsolateInGroup(Dart_Isolate group_member,
1035                           const char* name,
1036                           Dart_IsolateShutdownCallback shutdown_callback,
1037                           Dart_IsolateCleanupCallback cleanup_callback,
1038                           void* child_isolate_data,
1039                           char** error);
1040 
1041 /* TODO(turnidge): Document behavior when there is already a current
1042  * isolate. */
1043 
1044 /**
1045  * Creates a new isolate from a Dart Kernel file. The new isolate
1046  * becomes the current isolate.
1047  *
1048  * Requires there to be no current isolate.
1049  *
1050  * \param script_uri The main source file or snapshot this isolate will load.
1051  *   The VM will provide this URI to the Dart_IsolateGroupCreateCallback when a child
1052  *   isolate is created by Isolate.spawn. The embedder should use a URI that
1053  *   allows it to load the same program into such a child isolate.
1054  * \param name A short name for the isolate to improve debugging messages.
1055  *   Typically of the format 'foo.dart:main()'.
1056  * \param kernel_buffer
1057  * \param kernel_buffer_size A buffer which contains a kernel/DIL program. Must
1058  *   remain valid until isolate shutdown.
1059  * \param flags Pointer to VM specific flags or NULL for default flags.
1060  * \param isolate_group_data Embedder group data. This data can be obtained
1061  *   by calling Dart_IsolateGroupData and will be passed to the
1062  *   Dart_IsolateShutdownCallback, Dart_IsolateCleanupCallback, and
1063  *   Dart_IsolateGroupCleanupCallback.
1064  * \param isolate_data Embedder data.  This data will be passed to
1065  *   the Dart_IsolateGroupCreateCallback when new isolates are spawned from
1066  *   this parent isolate.
1067  * \param error Returns NULL if creation is successful, an error message
1068  *   otherwise. The caller is responsible for calling free() on the error
1069  *   message.
1070  *
1071  * \return The new isolate on success, or NULL if isolate creation failed.
1072  */
1073 DART_EXPORT Dart_Isolate
1074 Dart_CreateIsolateGroupFromKernel(const char* script_uri,
1075                                   const char* name,
1076                                   const uint8_t* kernel_buffer,
1077                                   intptr_t kernel_buffer_size,
1078                                   Dart_IsolateFlags* flags,
1079                                   void* isolate_group_data,
1080                                   void* isolate_data,
1081                                   char** error);
1082 /**
1083  * Shuts down the current isolate. After this call, the current isolate is NULL.
1084  * Any current scopes created by Dart_EnterScope will be exited. Invokes the
1085  * shutdown callback and any callbacks of remaining weak persistent handles.
1086  *
1087  * Requires there to be a current isolate.
1088  */
1089 DART_EXPORT void Dart_ShutdownIsolate();
1090 /* TODO(turnidge): Document behavior when there is no current isolate. */
1091 
1092 /**
1093  * Returns the current isolate. Will return NULL if there is no
1094  * current isolate.
1095  */
1096 DART_EXPORT Dart_Isolate Dart_CurrentIsolate();
1097 
1098 /**
1099  * Returns the callback data associated with the current isolate. This
1100  * data was set when the isolate got created or initialized.
1101  */
1102 DART_EXPORT void* Dart_CurrentIsolateData();
1103 
1104 /**
1105  * Returns the callback data associated with the given isolate. This
1106  * data was set when the isolate got created or initialized.
1107  */
1108 DART_EXPORT void* Dart_IsolateData(Dart_Isolate isolate);
1109 
1110 /**
1111  * Returns the current isolate group. Will return NULL if there is no
1112  * current isolate group.
1113  */
1114 DART_EXPORT Dart_IsolateGroup Dart_CurrentIsolateGroup();
1115 
1116 /**
1117  * Returns the callback data associated with the current isolate group. This
1118  * data was passed to the isolate group when it was created.
1119  */
1120 DART_EXPORT void* Dart_CurrentIsolateGroupData();
1121 
1122 /**
1123  * Returns the callback data associated with the specified isolate group. This
1124  * data was passed to the isolate when it was created.
1125  * The embedder is responsible for ensuring the consistency of this data
1126  * with respect to the lifecycle of an isolate group.
1127  */
1128 DART_EXPORT void* Dart_IsolateGroupData(Dart_Isolate isolate);
1129 
1130 /**
1131  * Returns the debugging name for the current isolate.
1132  *
1133  * This name is unique to each isolate and should only be used to make
1134  * debugging messages more comprehensible.
1135  */
1136 DART_EXPORT Dart_Handle Dart_DebugName();
1137 
1138 /**
1139  * Returns the ID for an isolate which is used to query the service protocol.
1140  *
1141  * It is the responsibility of the caller to free the returned ID.
1142  */
1143 DART_EXPORT const char* Dart_IsolateServiceId(Dart_Isolate isolate);
1144 
1145 /**
1146  * Enters an isolate. After calling this function,
1147  * the current isolate will be set to the provided isolate.
1148  *
1149  * Requires there to be no current isolate. Multiple threads may not be in
1150  * the same isolate at once.
1151  */
1152 DART_EXPORT void Dart_EnterIsolate(Dart_Isolate isolate);
1153 
1154 /**
1155  * Kills the given isolate.
1156  *
1157  * This function has the same effect as dart:isolate's
1158  * Isolate.kill(priority:immediate).
1159  * It can interrupt ordinary Dart code but not native code. If the isolate is
1160  * in the middle of a long running native function, the isolate will not be
1161  * killed until control returns to Dart.
1162  *
1163  * Does not require a current isolate. It is safe to kill the current isolate if
1164  * there is one.
1165  */
1166 DART_EXPORT void Dart_KillIsolate(Dart_Isolate isolate);
1167 
1168 /**
1169  * Notifies the VM that the embedder expects |size| bytes of memory have become
1170  * unreachable. The VM may use this hint to adjust the garbage collector's
1171  * growth policy.
1172  *
1173  * Multiple calls are interpreted as increasing, not replacing, the estimate of
1174  * unreachable memory.
1175  *
1176  * Requires there to be a current isolate.
1177  */
1178 DART_EXPORT void Dart_HintFreed(intptr_t size);
1179 
1180 /**
1181  * Notifies the VM that the embedder expects to be idle until |deadline|. The VM
1182  * may use this time to perform garbage collection or other tasks to avoid
1183  * delays during execution of Dart code in the future.
1184  *
1185  * |deadline| is measured in microseconds against the system's monotonic time.
1186  * This clock can be accessed via Dart_TimelineGetMicros().
1187  *
1188  * Requires there to be a current isolate.
1189  */
1190 DART_EXPORT void Dart_NotifyIdle(int64_t deadline);
1191 
1192 /**
1193  * Notifies the VM that the system is running low on memory.
1194  *
1195  * Does not require a current isolate. Only valid after calling Dart_Initialize.
1196  */
1197 DART_EXPORT void Dart_NotifyLowMemory();
1198 
1199 /**
1200  * Starts the CPU sampling profiler.
1201  */
1202 DART_EXPORT void Dart_StartProfiling();
1203 
1204 /**
1205  * Stops the CPU sampling profiler.
1206  *
1207  * Note that some profile samples might still be taken after this fucntion
1208  * returns due to the asynchronous nature of the implementation on some
1209  * platforms.
1210  */
1211 DART_EXPORT void Dart_StopProfiling();
1212 
1213 /**
1214  * Notifies the VM that the current thread should not be profiled until a
1215  * matching call to Dart_ThreadEnableProfiling is made.
1216  *
1217  * NOTE: By default, if a thread has entered an isolate it will be profiled.
1218  * This function should be used when an embedder knows a thread is about
1219  * to make a blocking call and wants to avoid unnecessary interrupts by
1220  * the profiler.
1221  */
1222 DART_EXPORT void Dart_ThreadDisableProfiling();
1223 
1224 /**
1225  * Notifies the VM that the current thread should be profiled.
1226  *
1227  * NOTE: It is only legal to call this function *after* calling
1228  *   Dart_ThreadDisableProfiling.
1229  *
1230  * NOTE: By default, if a thread has entered an isolate it will be profiled.
1231  */
1232 DART_EXPORT void Dart_ThreadEnableProfiling();
1233 
1234 /**
1235  * Register symbol information for the Dart VM's profiler and crash dumps.
1236  *
1237  * This consumes the output of //topaz/runtime/dart/profiler_symbols, which
1238  * should be treated as opaque.
1239  */
1240 DART_EXPORT void Dart_AddSymbols(const char* dso_name,
1241                                  void* buffer,
1242                                  intptr_t buffer_size);
1243 
1244 /**
1245  * Exits an isolate. After this call, Dart_CurrentIsolate will
1246  * return NULL.
1247  *
1248  * Requires there to be a current isolate.
1249  */
1250 DART_EXPORT void Dart_ExitIsolate();
1251 /* TODO(turnidge): We don't want users of the api to be able to exit a
1252  * "pure" dart isolate. Implement and document. */
1253 
1254 /**
1255  * Creates a full snapshot of the current isolate heap.
1256  *
1257  * A full snapshot is a compact representation of the dart vm isolate heap
1258  * and dart isolate heap states. These snapshots are used to initialize
1259  * the vm isolate on startup and fast initialization of an isolate.
1260  * A Snapshot of the heap is created before any dart code has executed.
1261  *
1262  * Requires there to be a current isolate. Not available in the precompiled
1263  * runtime (check Dart_IsPrecompiledRuntime).
1264  *
1265  * \param buffer Returns a pointer to a buffer containing the
1266  *   snapshot. This buffer is scope allocated and is only valid
1267  *   until the next call to Dart_ExitScope.
1268  * \param size Returns the size of the buffer.
1269  * \param is_core Create a snapshot containing core libraries.
1270  *                Such snapshot should be agnostic to null safety mode.
1271  *
1272  * \return A valid handle if no error occurs during the operation.
1273  */
1274 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
1275 Dart_CreateSnapshot(uint8_t** vm_snapshot_data_buffer,
1276                     intptr_t* vm_snapshot_data_size,
1277                     uint8_t** isolate_snapshot_data_buffer,
1278                     intptr_t* isolate_snapshot_data_size,
1279                     bool is_core);
1280 
1281 /**
1282  * Returns whether the buffer contains a kernel file.
1283  *
1284  * \param buffer Pointer to a buffer that might contain a kernel binary.
1285  * \param buffer_size Size of the buffer.
1286  *
1287  * \return Whether the buffer contains a kernel binary (full or partial).
1288  */
1289 DART_EXPORT bool Dart_IsKernel(const uint8_t* buffer, intptr_t buffer_size);
1290 
1291 /**
1292  * Make isolate runnable.
1293  *
1294  * When isolates are spawned, this function is used to indicate that
1295  * the creation and initialization (including script loading) of the
1296  * isolate is complete and the isolate can start.
1297  * This function expects there to be no current isolate.
1298  *
1299  * \param isolate The isolate to be made runnable.
1300  *
1301  * \return NULL if successful. Returns an error message otherwise. The caller
1302  * is responsible for freeing the error message.
1303  */
1304 DART_EXPORT DART_WARN_UNUSED_RESULT char* Dart_IsolateMakeRunnable(
1305     Dart_Isolate isolate);
1306 
1307 /*
1308  * ==================
1309  * Messages and Ports
1310  * ==================
1311  */
1312 
1313 /**
1314  * A port is used to send or receive inter-isolate messages
1315  */
1316 typedef int64_t Dart_Port;
1317 
1318 /**
1319  * ILLEGAL_PORT is a port number guaranteed never to be associated with a valid
1320  * port.
1321  */
1322 #define ILLEGAL_PORT ((Dart_Port)0)
1323 
1324 /**
1325  * A message notification callback.
1326  *
1327  * This callback allows the embedder to provide an alternate wakeup
1328  * mechanism for the delivery of inter-isolate messages.  It is the
1329  * responsibility of the embedder to call Dart_HandleMessage to
1330  * process the message.
1331  */
1332 typedef void (*Dart_MessageNotifyCallback)(Dart_Isolate dest_isolate);
1333 
1334 /**
1335  * Allows embedders to provide an alternative wakeup mechanism for the
1336  * delivery of inter-isolate messages. This setting only applies to
1337  * the current isolate.
1338  *
1339  * Most embedders will only call this function once, before isolate
1340  * execution begins. If this function is called after isolate
1341  * execution begins, the embedder is responsible for threading issues.
1342  */
1343 DART_EXPORT void Dart_SetMessageNotifyCallback(
1344     Dart_MessageNotifyCallback message_notify_callback);
1345 /* TODO(turnidge): Consider moving this to isolate creation so that it
1346  * is impossible to mess up. */
1347 
1348 /**
1349  * Query the current message notify callback for the isolate.
1350  *
1351  * \return The current message notify callback for the isolate.
1352  */
1353 DART_EXPORT Dart_MessageNotifyCallback Dart_GetMessageNotifyCallback();
1354 
1355 /**
1356  * The VM's default message handler supports pausing an isolate before it
1357  * processes the first message and right after the it processes the isolate's
1358  * final message. This can be controlled for all isolates by two VM flags:
1359  *
1360  *   `--pause-isolates-on-start`
1361  *   `--pause-isolates-on-exit`
1362  *
1363  * Additionally, Dart_SetShouldPauseOnStart and Dart_SetShouldPauseOnExit can be
1364  * used to control this behaviour on a per-isolate basis.
1365  *
1366  * When an embedder is using a Dart_MessageNotifyCallback the embedder
1367  * needs to cooperate with the VM so that the service protocol can report
1368  * accurate information about isolates and so that tools such as debuggers
1369  * work reliably.
1370  *
1371  * The following functions can be used to implement pausing on start and exit.
1372  */
1373 
1374 /**
1375  * If the VM flag `--pause-isolates-on-start` was passed this will be true.
1376  *
1377  * \return A boolean value indicating if pause on start was requested.
1378  */
1379 DART_EXPORT bool Dart_ShouldPauseOnStart();
1380 
1381 /**
1382  * Override the VM flag `--pause-isolates-on-start` for the current isolate.
1383  *
1384  * \param should_pause Should the isolate be paused on start?
1385  *
1386  * NOTE: This must be called before Dart_IsolateMakeRunnable.
1387  */
1388 DART_EXPORT void Dart_SetShouldPauseOnStart(bool should_pause);
1389 
1390 /**
1391  * Is the current isolate paused on start?
1392  *
1393  * \return A boolean value indicating if the isolate is paused on start.
1394  */
1395 DART_EXPORT bool Dart_IsPausedOnStart();
1396 
1397 /**
1398  * Called when the embedder has paused the current isolate on start and when
1399  * the embedder has resumed the isolate.
1400  *
1401  * \param paused Is the isolate paused on start?
1402  */
1403 DART_EXPORT void Dart_SetPausedOnStart(bool paused);
1404 
1405 /**
1406  * If the VM flag `--pause-isolates-on-exit` was passed this will be true.
1407  *
1408  * \return A boolean value indicating if pause on exit was requested.
1409  */
1410 DART_EXPORT bool Dart_ShouldPauseOnExit();
1411 
1412 /**
1413  * Override the VM flag `--pause-isolates-on-exit` for the current isolate.
1414  *
1415  * \param should_pause Should the isolate be paused on exit?
1416  *
1417  */
1418 DART_EXPORT void Dart_SetShouldPauseOnExit(bool should_pause);
1419 
1420 /**
1421  * Is the current isolate paused on exit?
1422  *
1423  * \return A boolean value indicating if the isolate is paused on exit.
1424  */
1425 DART_EXPORT bool Dart_IsPausedOnExit();
1426 
1427 /**
1428  * Called when the embedder has paused the current isolate on exit and when
1429  * the embedder has resumed the isolate.
1430  *
1431  * \param paused Is the isolate paused on exit?
1432  */
1433 DART_EXPORT void Dart_SetPausedOnExit(bool paused);
1434 
1435 /**
1436  * Called when the embedder has caught a top level unhandled exception error
1437  * in the current isolate.
1438  *
1439  * NOTE: It is illegal to call this twice on the same isolate without first
1440  * clearing the sticky error to null.
1441  *
1442  * \param error The unhandled exception error.
1443  */
1444 DART_EXPORT void Dart_SetStickyError(Dart_Handle error);
1445 
1446 /**
1447  * Does the current isolate have a sticky error?
1448  */
1449 DART_EXPORT bool Dart_HasStickyError();
1450 
1451 /**
1452  * Gets the sticky error for the current isolate.
1453  *
1454  * \return A handle to the sticky error object or null.
1455  */
1456 DART_EXPORT Dart_Handle Dart_GetStickyError();
1457 
1458 /**
1459  * Handles the next pending message for the current isolate.
1460  *
1461  * May generate an unhandled exception error.
1462  *
1463  * \return A valid handle if no error occurs during the operation.
1464  */
1465 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_HandleMessage();
1466 
1467 /**
1468  * Drains the microtask queue, then blocks the calling thread until the current
1469  * isolate recieves a message, then handles all messages.
1470  *
1471  * \param timeout_millis When non-zero, the call returns after the indicated
1472           number of milliseconds even if no message was received.
1473  * \return A valid handle if no error occurs, otherwise an error handle.
1474  */
1475 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
1476 Dart_WaitForEvent(int64_t timeout_millis);
1477 
1478 /**
1479  * Handles any pending messages for the vm service for the current
1480  * isolate.
1481  *
1482  * This function may be used by an embedder at a breakpoint to avoid
1483  * pausing the vm service.
1484  *
1485  * This function can indirectly cause the message notify callback to
1486  * be called.
1487  *
1488  * \return true if the vm service requests the program resume
1489  * execution, false otherwise
1490  */
1491 DART_EXPORT bool Dart_HandleServiceMessages();
1492 
1493 /**
1494  * Does the current isolate have pending service messages?
1495  *
1496  * \return true if the isolate has pending service messages, false otherwise.
1497  */
1498 DART_EXPORT bool Dart_HasServiceMessages();
1499 
1500 /**
1501  * Processes any incoming messages for the current isolate.
1502  *
1503  * This function may only be used when the embedder has not provided
1504  * an alternate message delivery mechanism with
1505  * Dart_SetMessageCallbacks. It is provided for convenience.
1506  *
1507  * This function waits for incoming messages for the current
1508  * isolate. As new messages arrive, they are handled using
1509  * Dart_HandleMessage. The routine exits when all ports to the
1510  * current isolate are closed.
1511  *
1512  * \return A valid handle if the run loop exited successfully.  If an
1513  *   exception or other error occurs while processing messages, an
1514  *   error handle is returned.
1515  */
1516 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_RunLoop();
1517 
1518 /**
1519  * Lets the VM run message processing for the isolate.
1520  *
1521  * This function expects there to a current isolate and the current isolate
1522  * must not have an active api scope. The VM will take care of making the
1523  * isolate runnable (if not already), handles its message loop and will take
1524  * care of shutting the isolate down once it's done.
1525  *
1526  * \param errors_are_fatal Whether uncaught errors should be fatal.
1527  * \param on_error_port A port to notify on uncaught errors (or ILLEGAL_PORT).
1528  * \param on_exit_port A port to notify on exit (or ILLEGAL_PORT).
1529  * \param error A non-NULL pointer which will hold an error message if the call
1530  *   fails. The error has to be free()ed by the caller.
1531  *
1532  * \return If successfull the VM takes owernship of the isolate and takes care
1533  *   of its message loop. If not successful the caller retains owernship of the
1534  *   isolate.
1535  */
1536 DART_EXPORT DART_WARN_UNUSED_RESULT bool Dart_RunLoopAsync(
1537     bool errors_are_fatal,
1538     Dart_Port on_error_port,
1539     Dart_Port on_exit_port,
1540     char** error);
1541 
1542 /* TODO(turnidge): Should this be removed from the public api? */
1543 
1544 /**
1545  * Gets the main port id for the current isolate.
1546  */
1547 DART_EXPORT Dart_Port Dart_GetMainPortId();
1548 
1549 /**
1550  * Does the current isolate have live ReceivePorts?
1551  *
1552  * A ReceivePort is live when it has not been closed.
1553  */
1554 DART_EXPORT bool Dart_HasLivePorts();
1555 
1556 /**
1557  * Posts a message for some isolate. The message is a serialized
1558  * object.
1559  *
1560  * Requires there to be a current isolate.
1561  *
1562  * \param port The destination port.
1563  * \param object An object from the current isolate.
1564  *
1565  * \return True if the message was posted.
1566  */
1567 DART_EXPORT bool Dart_Post(Dart_Port port_id, Dart_Handle object);
1568 
1569 /**
1570  * Returns a new SendPort with the provided port id.
1571  *
1572  * \param port_id The destination port.
1573  *
1574  * \return A new SendPort if no errors occurs. Otherwise returns
1575  *   an error handle.
1576  */
1577 DART_EXPORT Dart_Handle Dart_NewSendPort(Dart_Port port_id);
1578 
1579 /**
1580  * Gets the SendPort id for the provided SendPort.
1581  * \param port A SendPort object whose id is desired.
1582  * \param port_id Returns the id of the SendPort.
1583  * \return Success if no error occurs. Otherwise returns
1584  *   an error handle.
1585  */
1586 DART_EXPORT Dart_Handle Dart_SendPortGetId(Dart_Handle port,
1587                                            Dart_Port* port_id);
1588 
1589 /*
1590  * ======
1591  * Scopes
1592  * ======
1593  */
1594 
1595 /**
1596  * Enters a new scope.
1597  *
1598  * All new local handles will be created in this scope. Additionally,
1599  * some functions may return "scope allocated" memory which is only
1600  * valid within this scope.
1601  *
1602  * Requires there to be a current isolate.
1603  */
1604 DART_EXPORT void Dart_EnterScope();
1605 
1606 /**
1607  * Exits a scope.
1608  *
1609  * The previous scope (if any) becomes the current scope.
1610  *
1611  * Requires there to be a current isolate.
1612  */
1613 DART_EXPORT void Dart_ExitScope();
1614 
1615 /**
1616  * The Dart VM uses "zone allocation" for temporary structures. Zones
1617  * support very fast allocation of small chunks of memory. The chunks
1618  * cannot be deallocated individually, but instead zones support
1619  * deallocating all chunks in one fast operation.
1620  *
1621  * This function makes it possible for the embedder to allocate
1622  * temporary data in the VMs zone allocator.
1623  *
1624  * Zone allocation is possible:
1625  *   1. when inside a scope where local handles can be allocated
1626  *   2. when processing a message from a native port in a native port
1627  *      handler
1628  *
1629  * All the memory allocated this way will be reclaimed either on the
1630  * next call to Dart_ExitScope or when the native port handler exits.
1631  *
1632  * \param size Size of the memory to allocate.
1633  *
1634  * \return A pointer to the allocated memory. NULL if allocation
1635  *   failed. Failure might due to is no current VM zone.
1636  */
1637 DART_EXPORT uint8_t* Dart_ScopeAllocate(intptr_t size);
1638 
1639 /*
1640  * =======
1641  * Objects
1642  * =======
1643  */
1644 
1645 /**
1646  * Returns the null object.
1647  *
1648  * \return A handle to the null object.
1649  */
1650 DART_EXPORT Dart_Handle Dart_Null();
1651 
1652 /**
1653  * Is this object null?
1654  */
1655 DART_EXPORT bool Dart_IsNull(Dart_Handle object);
1656 
1657 /**
1658  * Returns the empty string object.
1659  *
1660  * \return A handle to the empty string object.
1661  */
1662 DART_EXPORT Dart_Handle Dart_EmptyString();
1663 
1664 /**
1665  * Returns types that are not classes, and which therefore cannot be looked up
1666  * as library members by Dart_GetType.
1667  *
1668  * \return A handle to the dynamic, void or Never type.
1669  */
1670 DART_EXPORT Dart_Handle Dart_TypeDynamic();
1671 DART_EXPORT Dart_Handle Dart_TypeVoid();
1672 DART_EXPORT Dart_Handle Dart_TypeNever();
1673 
1674 /**
1675  * Checks if the two objects are equal.
1676  *
1677  * The result of the comparison is returned through the 'equal'
1678  * parameter. The return value itself is used to indicate success or
1679  * failure, not equality.
1680  *
1681  * May generate an unhandled exception error.
1682  *
1683  * \param obj1 An object to be compared.
1684  * \param obj2 An object to be compared.
1685  * \param equal Returns the result of the equality comparison.
1686  *
1687  * \return A valid handle if no error occurs during the comparison.
1688  */
1689 DART_EXPORT Dart_Handle Dart_ObjectEquals(Dart_Handle obj1,
1690                                           Dart_Handle obj2,
1691                                           bool* equal);
1692 
1693 /**
1694  * Is this object an instance of some type?
1695  *
1696  * The result of the test is returned through the 'instanceof' parameter.
1697  * The return value itself is used to indicate success or failure.
1698  *
1699  * \param object An object.
1700  * \param type A type.
1701  * \param instanceof Return true if 'object' is an instance of type 'type'.
1702  *
1703  * \return A valid handle if no error occurs during the operation.
1704  */
1705 DART_EXPORT Dart_Handle Dart_ObjectIsType(Dart_Handle object,
1706                                           Dart_Handle type,
1707                                           bool* instanceof);
1708 
1709 /**
1710  * Query object type.
1711  *
1712  * \param object Some Object.
1713  *
1714  * \return true if Object is of the specified type.
1715  */
1716 DART_EXPORT bool Dart_IsInstance(Dart_Handle object);
1717 DART_EXPORT bool Dart_IsNumber(Dart_Handle object);
1718 DART_EXPORT bool Dart_IsInteger(Dart_Handle object);
1719 DART_EXPORT bool Dart_IsDouble(Dart_Handle object);
1720 DART_EXPORT bool Dart_IsBoolean(Dart_Handle object);
1721 DART_EXPORT bool Dart_IsString(Dart_Handle object);
1722 DART_EXPORT bool Dart_IsStringLatin1(Dart_Handle object); /* (ISO-8859-1) */
1723 DART_EXPORT bool Dart_IsExternalString(Dart_Handle object);
1724 DART_EXPORT bool Dart_IsList(Dart_Handle object);
1725 DART_EXPORT bool Dart_IsMap(Dart_Handle object);
1726 DART_EXPORT bool Dart_IsLibrary(Dart_Handle object);
1727 DART_EXPORT bool Dart_IsType(Dart_Handle handle);
1728 DART_EXPORT bool Dart_IsFunction(Dart_Handle handle);
1729 DART_EXPORT bool Dart_IsVariable(Dart_Handle handle);
1730 DART_EXPORT bool Dart_IsTypeVariable(Dart_Handle handle);
1731 DART_EXPORT bool Dart_IsClosure(Dart_Handle object);
1732 DART_EXPORT bool Dart_IsTypedData(Dart_Handle object);
1733 DART_EXPORT bool Dart_IsByteBuffer(Dart_Handle object);
1734 DART_EXPORT bool Dart_IsFuture(Dart_Handle object);
1735 
1736 /*
1737  * =========
1738  * Instances
1739  * =========
1740  */
1741 
1742 /*
1743  * For the purposes of the embedding api, not all objects returned are
1744  * Dart language objects.  Within the api, we use the term 'Instance'
1745  * to indicate handles which refer to true Dart language objects.
1746  *
1747  * TODO(turnidge): Reorganize the "Object" section above, pulling down
1748  * any functions that more properly belong here. */
1749 
1750 /**
1751  * Gets the type of a Dart language object.
1752  *
1753  * \param instance Some Dart object.
1754  *
1755  * \return If no error occurs, the type is returned. Otherwise an
1756  *   error handle is returned.
1757  */
1758 DART_EXPORT Dart_Handle Dart_InstanceGetType(Dart_Handle instance);
1759 
1760 /**
1761  * Returns the name for the provided class type.
1762  *
1763  * \return A valid string handle if no error occurs during the
1764  *   operation.
1765  */
1766 DART_EXPORT Dart_Handle Dart_ClassName(Dart_Handle cls_type);
1767 
1768 /**
1769  * Returns the name for the provided function or method.
1770  *
1771  * \return A valid string handle if no error occurs during the
1772  *   operation.
1773  */
1774 DART_EXPORT Dart_Handle Dart_FunctionName(Dart_Handle function);
1775 
1776 /**
1777  * Returns a handle to the owner of a function.
1778  *
1779  * The owner of an instance method or a static method is its defining
1780  * class. The owner of a top-level function is its defining
1781  * library. The owner of the function of a non-implicit closure is the
1782  * function of the method or closure that defines the non-implicit
1783  * closure.
1784  *
1785  * \return A valid handle to the owner of the function, or an error
1786  *   handle if the argument is not a valid handle to a function.
1787  */
1788 DART_EXPORT Dart_Handle Dart_FunctionOwner(Dart_Handle function);
1789 
1790 /**
1791  * Determines whether a function handle referes to a static function
1792  * of method.
1793  *
1794  * For the purposes of the embedding API, a top-level function is
1795  * implicitly declared static.
1796  *
1797  * \param function A handle to a function or method declaration.
1798  * \param is_static Returns whether the function or method is declared static.
1799  *
1800  * \return A valid handle if no error occurs during the operation.
1801  */
1802 DART_EXPORT Dart_Handle Dart_FunctionIsStatic(Dart_Handle function,
1803                                               bool* is_static);
1804 
1805 /**
1806  * Is this object a closure resulting from a tear-off (closurized method)?
1807  *
1808  * Returns true for closures produced when an ordinary method is accessed
1809  * through a getter call. Returns false otherwise, in particular for closures
1810  * produced from local function declarations.
1811  *
1812  * \param object Some Object.
1813  *
1814  * \return true if Object is a tear-off.
1815  */
1816 DART_EXPORT bool Dart_IsTearOff(Dart_Handle object);
1817 
1818 /**
1819  * Retrieves the function of a closure.
1820  *
1821  * \return A handle to the function of the closure, or an error handle if the
1822  *   argument is not a closure.
1823  */
1824 DART_EXPORT Dart_Handle Dart_ClosureFunction(Dart_Handle closure);
1825 
1826 /**
1827  * Returns a handle to the library which contains class.
1828  *
1829  * \return A valid handle to the library with owns class, null if the class
1830  *   has no library or an error handle if the argument is not a valid handle
1831  *   to a class type.
1832  */
1833 DART_EXPORT Dart_Handle Dart_ClassLibrary(Dart_Handle cls_type);
1834 
1835 /*
1836  * =============================
1837  * Numbers, Integers and Doubles
1838  * =============================
1839  */
1840 
1841 /**
1842  * Does this Integer fit into a 64-bit signed integer?
1843  *
1844  * \param integer An integer.
1845  * \param fits Returns true if the integer fits into a 64-bit signed integer.
1846  *
1847  * \return A valid handle if no error occurs during the operation.
1848  */
1849 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoInt64(Dart_Handle integer,
1850                                                   bool* fits);
1851 
1852 /**
1853  * Does this Integer fit into a 64-bit unsigned integer?
1854  *
1855  * \param integer An integer.
1856  * \param fits Returns true if the integer fits into a 64-bit unsigned integer.
1857  *
1858  * \return A valid handle if no error occurs during the operation.
1859  */
1860 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoUint64(Dart_Handle integer,
1861                                                    bool* fits);
1862 
1863 /**
1864  * Returns an Integer with the provided value.
1865  *
1866  * \param value The value of the integer.
1867  *
1868  * \return The Integer object if no error occurs. Otherwise returns
1869  *   an error handle.
1870  */
1871 DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value);
1872 
1873 /**
1874  * Returns an Integer with the provided value.
1875  *
1876  * \param value The unsigned value of the integer.
1877  *
1878  * \return The Integer object if no error occurs. Otherwise returns
1879  *   an error handle.
1880  */
1881 DART_EXPORT Dart_Handle Dart_NewIntegerFromUint64(uint64_t value);
1882 
1883 /**
1884  * Returns an Integer with the provided value.
1885  *
1886  * \param value The value of the integer represented as a C string
1887  *   containing a hexadecimal number.
1888  *
1889  * \return The Integer object if no error occurs. Otherwise returns
1890  *   an error handle.
1891  */
1892 DART_EXPORT Dart_Handle Dart_NewIntegerFromHexCString(const char* value);
1893 
1894 /**
1895  * Gets the value of an Integer.
1896  *
1897  * The integer must fit into a 64-bit signed integer, otherwise an error occurs.
1898  *
1899  * \param integer An Integer.
1900  * \param value Returns the value of the Integer.
1901  *
1902  * \return A valid handle if no error occurs during the operation.
1903  */
1904 DART_EXPORT Dart_Handle Dart_IntegerToInt64(Dart_Handle integer,
1905                                             int64_t* value);
1906 
1907 /**
1908  * Gets the value of an Integer.
1909  *
1910  * The integer must fit into a 64-bit unsigned integer, otherwise an
1911  * error occurs.
1912  *
1913  * \param integer An Integer.
1914  * \param value Returns the value of the Integer.
1915  *
1916  * \return A valid handle if no error occurs during the operation.
1917  */
1918 DART_EXPORT Dart_Handle Dart_IntegerToUint64(Dart_Handle integer,
1919                                              uint64_t* value);
1920 
1921 /**
1922  * Gets the value of an integer as a hexadecimal C string.
1923  *
1924  * \param integer An Integer.
1925  * \param value Returns the value of the Integer as a hexadecimal C
1926  *   string. This C string is scope allocated and is only valid until
1927  *   the next call to Dart_ExitScope.
1928  *
1929  * \return A valid handle if no error occurs during the operation.
1930  */
1931 DART_EXPORT Dart_Handle Dart_IntegerToHexCString(Dart_Handle integer,
1932                                                  const char** value);
1933 
1934 /**
1935  * Returns a Double with the provided value.
1936  *
1937  * \param value A double.
1938  *
1939  * \return The Double object if no error occurs. Otherwise returns
1940  *   an error handle.
1941  */
1942 DART_EXPORT Dart_Handle Dart_NewDouble(double value);
1943 
1944 /**
1945  * Gets the value of a Double
1946  *
1947  * \param double_obj A Double
1948  * \param value Returns the value of the Double.
1949  *
1950  * \return A valid handle if no error occurs during the operation.
1951  */
1952 DART_EXPORT Dart_Handle Dart_DoubleValue(Dart_Handle double_obj, double* value);
1953 
1954 /**
1955  * Returns a closure of static function 'function_name' in the class 'class_name'
1956  * in the exported namespace of specified 'library'.
1957  *
1958  * \param library Library object
1959  * \param cls_type Type object representing a Class
1960  * \param function_name Name of the static function in the class
1961  *
1962  * \return A valid Dart instance if no error occurs during the operation.
1963  */
1964 DART_EXPORT Dart_Handle Dart_GetStaticMethodClosure(Dart_Handle library,
1965                                                     Dart_Handle cls_type,
1966                                                     Dart_Handle function_name);
1967 
1968 /*
1969  * ========
1970  * Booleans
1971  * ========
1972  */
1973 
1974 /**
1975  * Returns the True object.
1976  *
1977  * Requires there to be a current isolate.
1978  *
1979  * \return A handle to the True object.
1980  */
1981 DART_EXPORT Dart_Handle Dart_True();
1982 
1983 /**
1984  * Returns the False object.
1985  *
1986  * Requires there to be a current isolate.
1987  *
1988  * \return A handle to the False object.
1989  */
1990 DART_EXPORT Dart_Handle Dart_False();
1991 
1992 /**
1993  * Returns a Boolean with the provided value.
1994  *
1995  * \param value true or false.
1996  *
1997  * \return The Boolean object if no error occurs. Otherwise returns
1998  *   an error handle.
1999  */
2000 DART_EXPORT Dart_Handle Dart_NewBoolean(bool value);
2001 
2002 /**
2003  * Gets the value of a Boolean
2004  *
2005  * \param boolean_obj A Boolean
2006  * \param value Returns the value of the Boolean.
2007  *
2008  * \return A valid handle if no error occurs during the operation.
2009  */
2010 DART_EXPORT Dart_Handle Dart_BooleanValue(Dart_Handle boolean_obj, bool* value);
2011 
2012 /*
2013  * =======
2014  * Strings
2015  * =======
2016  */
2017 
2018 /**
2019  * Gets the length of a String.
2020  *
2021  * \param str A String.
2022  * \param length Returns the length of the String.
2023  *
2024  * \return A valid handle if no error occurs during the operation.
2025  */
2026 DART_EXPORT Dart_Handle Dart_StringLength(Dart_Handle str, intptr_t* length);
2027 
2028 /**
2029  * Returns a String built from the provided C string
2030  * (There is an implicit assumption that the C string passed in contains
2031  *  UTF-8 encoded characters and '\0' is considered as a termination
2032  *  character).
2033  *
2034  * \param value A C String
2035  *
2036  * \return The String object if no error occurs. Otherwise returns
2037  *   an error handle.
2038  */
2039 DART_EXPORT Dart_Handle Dart_NewStringFromCString(const char* str);
2040 /* TODO(turnidge): Document what happens when we run out of memory
2041  * during this call. */
2042 
2043 /**
2044  * Returns a String built from an array of UTF-8 encoded characters.
2045  *
2046  * \param utf8_array An array of UTF-8 encoded characters.
2047  * \param length The length of the codepoints array.
2048  *
2049  * \return The String object if no error occurs. Otherwise returns
2050  *   an error handle.
2051  */
2052 DART_EXPORT Dart_Handle Dart_NewStringFromUTF8(const uint8_t* utf8_array,
2053                                                intptr_t length);
2054 
2055 /**
2056  * Returns a String built from an array of UTF-16 encoded characters.
2057  *
2058  * \param utf16_array An array of UTF-16 encoded characters.
2059  * \param length The length of the codepoints array.
2060  *
2061  * \return The String object if no error occurs. Otherwise returns
2062  *   an error handle.
2063  */
2064 DART_EXPORT Dart_Handle Dart_NewStringFromUTF16(const uint16_t* utf16_array,
2065                                                 intptr_t length);
2066 
2067 /**
2068  * Returns a String built from an array of UTF-32 encoded characters.
2069  *
2070  * \param utf32_array An array of UTF-32 encoded characters.
2071  * \param length The length of the codepoints array.
2072  *
2073  * \return The String object if no error occurs. Otherwise returns
2074  *   an error handle.
2075  */
2076 DART_EXPORT Dart_Handle Dart_NewStringFromUTF32(const int32_t* utf32_array,
2077                                                 intptr_t length);
2078 
2079 /**
2080  * Returns a String which references an external array of
2081  * Latin-1 (ISO-8859-1) encoded characters.
2082  *
2083  * \param latin1_array Array of Latin-1 encoded characters. This must not move.
2084  * \param length The length of the characters array.
2085  * \param peer An external pointer to associate with this string.
2086  * \param external_allocation_size The number of externally allocated
2087  *   bytes for peer. Used to inform the garbage collector.
2088  * \param callback A callback to be called when this string is finalized.
2089  *
2090  * \return The String object if no error occurs. Otherwise returns
2091  *   an error handle.
2092  */
2093 DART_EXPORT Dart_Handle
2094 Dart_NewExternalLatin1String(const uint8_t* latin1_array,
2095                              intptr_t length,
2096                              void* peer,
2097                              intptr_t external_allocation_size,
2098                              Dart_HandleFinalizer callback);
2099 
2100 /**
2101  * Returns a String which references an external array of UTF-16 encoded
2102  * characters.
2103  *
2104  * \param utf16_array An array of UTF-16 encoded characters. This must not move.
2105  * \param length The length of the characters array.
2106  * \param peer An external pointer to associate with this string.
2107  * \param external_allocation_size The number of externally allocated
2108  *   bytes for peer. Used to inform the garbage collector.
2109  * \param callback A callback to be called when this string is finalized.
2110  *
2111  * \return The String object if no error occurs. Otherwise returns
2112  *   an error handle.
2113  */
2114 DART_EXPORT Dart_Handle
2115 Dart_NewExternalUTF16String(const uint16_t* utf16_array,
2116                             intptr_t length,
2117                             void* peer,
2118                             intptr_t external_allocation_size,
2119                             Dart_HandleFinalizer callback);
2120 
2121 /**
2122  * Gets the C string representation of a String.
2123  * (It is a sequence of UTF-8 encoded values with a '\0' termination.)
2124  *
2125  * \param str A string.
2126  * \param cstr Returns the String represented as a C string.
2127  *   This C string is scope allocated and is only valid until
2128  *   the next call to Dart_ExitScope.
2129  *
2130  * \return A valid handle if no error occurs during the operation.
2131  */
2132 DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle str,
2133                                              const char** cstr);
2134 
2135 /**
2136  * Gets a UTF-8 encoded representation of a String.
2137  *
2138  * Any unpaired surrogate code points in the string will be converted as
2139  * replacement characters (U+FFFD, 0xEF 0xBF 0xBD in UTF-8). If you need
2140  * to preserve unpaired surrogates, use the Dart_StringToUTF16 function.
2141  *
2142  * \param str A string.
2143  * \param utf8_array Returns the String represented as UTF-8 code
2144  *   units.  This UTF-8 array is scope allocated and is only valid
2145  *   until the next call to Dart_ExitScope.
2146  * \param length Used to return the length of the array which was
2147  *   actually used.
2148  *
2149  * \return A valid handle if no error occurs during the operation.
2150  */
2151 DART_EXPORT Dart_Handle Dart_StringToUTF8(Dart_Handle str,
2152                                           uint8_t** utf8_array,
2153                                           intptr_t* length);
2154 
2155 /**
2156  * Gets the data corresponding to the string object. This function returns
2157  * the data only for Latin-1 (ISO-8859-1) string objects. For all other
2158  * string objects it returns an error.
2159  *
2160  * \param str A string.
2161  * \param latin1_array An array allocated by the caller, used to return
2162  *   the string data.
2163  * \param length Used to pass in the length of the provided array.
2164  *   Used to return the length of the array which was actually used.
2165  *
2166  * \return A valid handle if no error occurs during the operation.
2167  */
2168 DART_EXPORT Dart_Handle Dart_StringToLatin1(Dart_Handle str,
2169                                             uint8_t* latin1_array,
2170                                             intptr_t* length);
2171 
2172 /**
2173  * Gets the UTF-16 encoded representation of a string.
2174  *
2175  * \param str A string.
2176  * \param utf16_array An array allocated by the caller, used to return
2177  *   the array of UTF-16 encoded characters.
2178  * \param length Used to pass in the length of the provided array.
2179  *   Used to return the length of the array which was actually used.
2180  *
2181  * \return A valid handle if no error occurs during the operation.
2182  */
2183 DART_EXPORT Dart_Handle Dart_StringToUTF16(Dart_Handle str,
2184                                            uint16_t* utf16_array,
2185                                            intptr_t* length);
2186 
2187 /**
2188  * Gets the storage size in bytes of a String.
2189  *
2190  * \param str A String.
2191  * \param length Returns the storage size in bytes of the String.
2192  *  This is the size in bytes needed to store the String.
2193  *
2194  * \return A valid handle if no error occurs during the operation.
2195  */
2196 DART_EXPORT Dart_Handle Dart_StringStorageSize(Dart_Handle str, intptr_t* size);
2197 
2198 /**
2199  * Retrieves some properties associated with a String.
2200  * Properties retrieved are:
2201  * - character size of the string (one or two byte)
2202  * - length of the string
2203  * - peer pointer of string if it is an external string.
2204  * \param str A String.
2205  * \param char_size Returns the character size of the String.
2206  * \param str_len Returns the length of the String.
2207  * \param peer Returns the peer pointer associated with the String or 0 if
2208  *   there is no peer pointer for it.
2209  * \return Success if no error occurs. Otherwise returns
2210  *   an error handle.
2211  */
2212 DART_EXPORT Dart_Handle Dart_StringGetProperties(Dart_Handle str,
2213                                                  intptr_t* char_size,
2214                                                  intptr_t* str_len,
2215                                                  void** peer);
2216 
2217 /*
2218  * =====
2219  * Lists
2220  * =====
2221  */
2222 
2223 /**
2224  * Returns a List<dynamic> of the desired length.
2225  *
2226  * \param length The length of the list.
2227  *
2228  * \return The List object if no error occurs. Otherwise returns
2229  *   an error handle.
2230  */
2231 DART_EXPORT Dart_Handle Dart_NewList(intptr_t length);
2232 
2233 typedef enum {
2234   Dart_CoreType_Dynamic,
2235   Dart_CoreType_Int,
2236   Dart_CoreType_String,
2237 } Dart_CoreType_Id;
2238 
2239 // TODO(bkonyi): convert this to use nullable types once NNBD is enabled.
2240 /**
2241  * Returns a List of the desired length with the desired legacy element type.
2242  *
2243  * \param element_type_id The type of elements of the list.
2244  * \param length The length of the list.
2245  *
2246  * \return The List object if no error occurs. Otherwise returns an error
2247  * handle.
2248  */
2249 DART_EXPORT Dart_Handle Dart_NewListOf(Dart_CoreType_Id element_type_id,
2250                                        intptr_t length);
2251 
2252 /**
2253  * Returns a List of the desired length with the desired element type.
2254  *
2255  * \param element_type Handle to a nullable type object. E.g., from
2256  * Dart_GetType or Dart_GetNullableType.
2257  *
2258  * \param length The length of the list.
2259  *
2260  * \return The List object if no error occurs. Otherwise returns
2261  *   an error handle.
2262  */
2263 DART_EXPORT Dart_Handle Dart_NewListOfType(Dart_Handle element_type,
2264                                            intptr_t length);
2265 
2266 /**
2267  * Returns a List of the desired length with the desired element type, filled
2268  * with the provided object.
2269  *
2270  * \param element_type Handle to a type object. E.g., from Dart_GetType.
2271  *
2272  * \param fill_object Handle to an object of type 'element_type' that will be
2273  * used to populate the list. This parameter can only be Dart_Null() if the
2274  * length of the list is 0 or 'element_type' is a nullable type.
2275  *
2276  * \param length The length of the list.
2277  *
2278  * \return The List object if no error occurs. Otherwise returns
2279  *   an error handle.
2280  */
2281 DART_EXPORT Dart_Handle Dart_NewListOfTypeFilled(Dart_Handle element_type,
2282                                                  Dart_Handle fill_object,
2283                                                  intptr_t length);
2284 
2285 /**
2286  * Gets the length of a List.
2287  *
2288  * May generate an unhandled exception error.
2289  *
2290  * \param list A List.
2291  * \param length Returns the length of the List.
2292  *
2293  * \return A valid handle if no error occurs during the operation.
2294  */
2295 DART_EXPORT Dart_Handle Dart_ListLength(Dart_Handle list, intptr_t* length);
2296 
2297 /**
2298  * Gets the Object at some index of a List.
2299  *
2300  * If the index is out of bounds, an error occurs.
2301  *
2302  * May generate an unhandled exception error.
2303  *
2304  * \param list A List.
2305  * \param index A valid index into the List.
2306  *
2307  * \return The Object in the List at the specified index if no error
2308  *   occurs. Otherwise returns an error handle.
2309  */
2310 DART_EXPORT Dart_Handle Dart_ListGetAt(Dart_Handle list, intptr_t index);
2311 
2312 /**
2313 * Gets a range of Objects from a List.
2314 *
2315 * If any of the requested index values are out of bounds, an error occurs.
2316 *
2317 * May generate an unhandled exception error.
2318 *
2319 * \param list A List.
2320 * \param offset The offset of the first item to get.
2321 * \param length The number of items to get.
2322 * \param result A pointer to fill with the objects.
2323 *
2324 * \return Success if no error occurs during the operation.
2325 */
2326 DART_EXPORT Dart_Handle Dart_ListGetRange(Dart_Handle list,
2327                                           intptr_t offset,
2328                                           intptr_t length,
2329                                           Dart_Handle* result);
2330 
2331 /**
2332  * Sets the Object at some index of a List.
2333  *
2334  * If the index is out of bounds, an error occurs.
2335  *
2336  * May generate an unhandled exception error.
2337  *
2338  * \param array A List.
2339  * \param index A valid index into the List.
2340  * \param value The Object to put in the List.
2341  *
2342  * \return A valid handle if no error occurs during the operation.
2343  */
2344 DART_EXPORT Dart_Handle Dart_ListSetAt(Dart_Handle list,
2345                                        intptr_t index,
2346                                        Dart_Handle value);
2347 
2348 /**
2349  * May generate an unhandled exception error.
2350  */
2351 DART_EXPORT Dart_Handle Dart_ListGetAsBytes(Dart_Handle list,
2352                                             intptr_t offset,
2353                                             uint8_t* native_array,
2354                                             intptr_t length);
2355 
2356 /**
2357  * May generate an unhandled exception error.
2358  */
2359 DART_EXPORT Dart_Handle Dart_ListSetAsBytes(Dart_Handle list,
2360                                             intptr_t offset,
2361                                             const uint8_t* native_array,
2362                                             intptr_t length);
2363 
2364 /*
2365  * ====
2366  * Maps
2367  * ====
2368  */
2369 
2370 /**
2371  * Gets the Object at some key of a Map.
2372  *
2373  * May generate an unhandled exception error.
2374  *
2375  * \param map A Map.
2376  * \param key An Object.
2377  *
2378  * \return The value in the map at the specified key, null if the map does not
2379  *   contain the key, or an error handle.
2380  */
2381 DART_EXPORT Dart_Handle Dart_MapGetAt(Dart_Handle map, Dart_Handle key);
2382 
2383 /**
2384  * Returns whether the Map contains a given key.
2385  *
2386  * May generate an unhandled exception error.
2387  *
2388  * \param map A Map.
2389  *
2390  * \return A handle on a boolean indicating whether map contains the key.
2391  *   Otherwise returns an error handle.
2392  */
2393 DART_EXPORT Dart_Handle Dart_MapContainsKey(Dart_Handle map, Dart_Handle key);
2394 
2395 /**
2396  * Gets the list of keys of a Map.
2397  *
2398  * May generate an unhandled exception error.
2399  *
2400  * \param map A Map.
2401  *
2402  * \return The list of key Objects if no error occurs. Otherwise returns an
2403  *   error handle.
2404  */
2405 DART_EXPORT Dart_Handle Dart_MapKeys(Dart_Handle map);
2406 
2407 /*
2408  * ==========
2409  * Typed Data
2410  * ==========
2411  */
2412 
2413 typedef enum {
2414   Dart_TypedData_kByteData = 0,
2415   Dart_TypedData_kInt8,
2416   Dart_TypedData_kUint8,
2417   Dart_TypedData_kUint8Clamped,
2418   Dart_TypedData_kInt16,
2419   Dart_TypedData_kUint16,
2420   Dart_TypedData_kInt32,
2421   Dart_TypedData_kUint32,
2422   Dart_TypedData_kInt64,
2423   Dart_TypedData_kUint64,
2424   Dart_TypedData_kFloat32,
2425   Dart_TypedData_kFloat64,
2426   Dart_TypedData_kInt32x4,
2427   Dart_TypedData_kFloat32x4,
2428   Dart_TypedData_kFloat64x2,
2429   Dart_TypedData_kInvalid
2430 } Dart_TypedData_Type;
2431 
2432 /**
2433  * Return type if this object is a TypedData object.
2434  *
2435  * \return kInvalid if the object is not a TypedData object or the appropriate
2436  *   Dart_TypedData_Type.
2437  */
2438 DART_EXPORT Dart_TypedData_Type Dart_GetTypeOfTypedData(Dart_Handle object);
2439 
2440 /**
2441  * Return type if this object is an external TypedData object.
2442  *
2443  * \return kInvalid if the object is not an external TypedData object or
2444  *   the appropriate Dart_TypedData_Type.
2445  */
2446 DART_EXPORT Dart_TypedData_Type
2447 Dart_GetTypeOfExternalTypedData(Dart_Handle object);
2448 
2449 /**
2450  * Returns a TypedData object of the desired length and type.
2451  *
2452  * \param type The type of the TypedData object.
2453  * \param length The length of the TypedData object (length in type units).
2454  *
2455  * \return The TypedData object if no error occurs. Otherwise returns
2456  *   an error handle.
2457  */
2458 DART_EXPORT Dart_Handle Dart_NewTypedData(Dart_TypedData_Type type,
2459                                           intptr_t length);
2460 
2461 /**
2462  * Returns a TypedData object which references an external data array.
2463  *
2464  * \param type The type of the data array.
2465  * \param data A data array. This array must not move.
2466  * \param length The length of the data array (length in type units).
2467  *
2468  * \return The TypedData object if no error occurs. Otherwise returns
2469  *   an error handle.
2470  */
2471 DART_EXPORT Dart_Handle Dart_NewExternalTypedData(Dart_TypedData_Type type,
2472                                                   void* data,
2473                                                   intptr_t length);
2474 
2475 /**
2476  * Returns a TypedData object which references an external data array.
2477  *
2478  * \param type The type of the data array.
2479  * \param data A data array. This array must not move.
2480  * \param length The length of the data array (length in type units).
2481  * \param peer A pointer to a native object or NULL.  This value is
2482  *   provided to callback when it is invoked.
2483  * \param external_allocation_size The number of externally allocated
2484  *   bytes for peer. Used to inform the garbage collector.
2485  * \param callback A function pointer that will be invoked sometime
2486  *   after the object is garbage collected, unless the handle has been deleted.
2487  *   A valid callback needs to be specified it cannot be NULL.
2488  *
2489  * \return The TypedData object if no error occurs. Otherwise returns
2490  *   an error handle.
2491  */
2492 DART_EXPORT Dart_Handle
2493 Dart_NewExternalTypedDataWithFinalizer(Dart_TypedData_Type type,
2494                                        void* data,
2495                                        intptr_t length,
2496                                        void* peer,
2497                                        intptr_t external_allocation_size,
2498                                        Dart_HandleFinalizer callback);
2499 
2500 /**
2501  * Returns a ByteBuffer object for the typed data.
2502  *
2503  * \param type_data The TypedData object.
2504  *
2505  * \return The ByteBuffer object if no error occurs. Otherwise returns
2506  *   an error handle.
2507  */
2508 DART_EXPORT Dart_Handle Dart_NewByteBuffer(Dart_Handle typed_data);
2509 
2510 /**
2511  * Acquires access to the internal data address of a TypedData object.
2512  *
2513  * \param object The typed data object whose internal data address is to
2514  *    be accessed.
2515  * \param type The type of the object is returned here.
2516  * \param data The internal data address is returned here.
2517  * \param len Size of the typed array is returned here.
2518  *
2519  * Notes:
2520  *   When the internal address of the object is acquired any calls to a
2521  *   Dart API function that could potentially allocate an object or run
2522  *   any Dart code will return an error.
2523  *
2524  *   Any Dart API functions for accessing the data should not be called
2525  *   before the corresponding release. In particular, the object should
2526  *   not be acquired again before its release. This leads to undefined
2527  *   behavior.
2528  *
2529  * \return Success if the internal data address is acquired successfully.
2530  *   Otherwise, returns an error handle.
2531  */
2532 DART_EXPORT Dart_Handle Dart_TypedDataAcquireData(Dart_Handle object,
2533                                                   Dart_TypedData_Type* type,
2534                                                   void** data,
2535                                                   intptr_t* len);
2536 
2537 /**
2538  * Releases access to the internal data address that was acquired earlier using
2539  * Dart_TypedDataAcquireData.
2540  *
2541  * \param object The typed data object whose internal data address is to be
2542  *   released.
2543  *
2544  * \return Success if the internal data address is released successfully.
2545  *   Otherwise, returns an error handle.
2546  */
2547 DART_EXPORT Dart_Handle Dart_TypedDataReleaseData(Dart_Handle object);
2548 
2549 /**
2550  * Returns the TypedData object associated with the ByteBuffer object.
2551  *
2552  * \param byte_buffer The ByteBuffer object.
2553  *
2554  * \return The TypedData object if no error occurs. Otherwise returns
2555  *   an error handle.
2556  */
2557 DART_EXPORT Dart_Handle Dart_GetDataFromByteBuffer(Dart_Handle byte_buffer);
2558 
2559 /*
2560  * ============================================================
2561  * Invoking Constructors, Methods, Closures and Field accessors
2562  * ============================================================
2563  */
2564 
2565 /**
2566  * Invokes a constructor, creating a new object.
2567  *
2568  * This function allows hidden constructors (constructors with leading
2569  * underscores) to be called.
2570  *
2571  * \param type Type of object to be constructed.
2572  * \param constructor_name The name of the constructor to invoke.  Use
2573  *   Dart_Null() or Dart_EmptyString() to invoke the unnamed constructor.
2574  *   This name should not include the name of the class.
2575  * \param number_of_arguments Size of the arguments array.
2576  * \param arguments An array of arguments to the constructor.
2577  *
2578  * \return If the constructor is called and completes successfully,
2579  *   then the new object. If an error occurs during execution, then an
2580  *   error handle is returned.
2581  */
2582 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
2583 Dart_New(Dart_Handle type,
2584          Dart_Handle constructor_name,
2585          int number_of_arguments,
2586          Dart_Handle* arguments);
2587 
2588 /**
2589  * Allocate a new object without invoking a constructor.
2590  *
2591  * \param type The type of an object to be allocated.
2592  *
2593  * \return The new object. If an error occurs during execution, then an
2594  *   error handle is returned.
2595  */
2596 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_Allocate(Dart_Handle type);
2597 
2598 /**
2599  * Allocate a new object without invoking a constructor, and sets specified
2600  *  native fields.
2601  *
2602  * \param type The type of an object to be allocated.
2603  * \param num_native_fields The number of native fields to set.
2604  * \param native_fields An array containing the value of native fields.
2605  *
2606  * \return The new object. If an error occurs during execution, then an
2607  *   error handle is returned.
2608  */
2609 DART_EXPORT Dart_Handle
2610 Dart_AllocateWithNativeFields(Dart_Handle type,
2611                               intptr_t num_native_fields,
2612                               const intptr_t* native_fields);
2613 
2614 /**
2615  * Invokes a method or function.
2616  *
2617  * The 'target' parameter may be an object, type, or library.  If
2618  * 'target' is an object, then this function will invoke an instance
2619  * method.  If 'target' is a type, then this function will invoke a
2620  * static method.  If 'target' is a library, then this function will
2621  * invoke a top-level function from that library.
2622  * NOTE: This API call cannot be used to invoke methods of a type object.
2623  *
2624  * This function ignores visibility (leading underscores in names).
2625  *
2626  * May generate an unhandled exception error.
2627  *
2628  * \param target An object, type, or library.
2629  * \param name The name of the function or method to invoke.
2630  * \param number_of_arguments Size of the arguments array.
2631  * \param arguments An array of arguments to the function.
2632  *
2633  * \return If the function or method is called and completes
2634  *   successfully, then the return value is returned. If an error
2635  *   occurs during execution, then an error handle is returned.
2636  */
2637 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
2638 Dart_Invoke(Dart_Handle target,
2639             Dart_Handle name,
2640             int number_of_arguments,
2641             Dart_Handle* arguments);
2642 /* TODO(turnidge): Document how to invoke operators. */
2643 
2644 /**
2645  * Invokes a Closure with the given arguments.
2646  *
2647  * May generate an unhandled exception error.
2648  *
2649  * \return If no error occurs during execution, then the result of
2650  *   invoking the closure is returned. If an error occurs during
2651  *   execution, then an error handle is returned.
2652  */
2653 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
2654 Dart_InvokeClosure(Dart_Handle closure,
2655                    int number_of_arguments,
2656                    Dart_Handle* arguments);
2657 
2658 /**
2659  * Invokes a Generative Constructor on an object that was previously
2660  * allocated using Dart_Allocate/Dart_AllocateWithNativeFields.
2661  *
2662  * The 'target' parameter must be an object.
2663  *
2664  * This function ignores visibility (leading underscores in names).
2665  *
2666  * May generate an unhandled exception error.
2667  *
2668  * \param target An object.
2669  * \param name The name of the constructor to invoke.
2670  *   Use Dart_Null() or Dart_EmptyString() to invoke the unnamed constructor.
2671  * \param number_of_arguments Size of the arguments array.
2672  * \param arguments An array of arguments to the function.
2673  *
2674  * \return If the constructor is called and completes
2675  *   successfully, then the object is returned. If an error
2676  *   occurs during execution, then an error handle is returned.
2677  */
2678 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
2679 Dart_InvokeConstructor(Dart_Handle object,
2680                        Dart_Handle name,
2681                        int number_of_arguments,
2682                        Dart_Handle* arguments);
2683 
2684 /**
2685  * Gets the value of a field.
2686  *
2687  * The 'container' parameter may be an object, type, or library.  If
2688  * 'container' is an object, then this function will access an
2689  * instance field.  If 'container' is a type, then this function will
2690  * access a static field.  If 'container' is a library, then this
2691  * function will access a top-level variable.
2692  * NOTE: This API call cannot be used to access fields of a type object.
2693  *
2694  * This function ignores field visibility (leading underscores in names).
2695  *
2696  * May generate an unhandled exception error.
2697  *
2698  * \param container An object, type, or library.
2699  * \param name A field name.
2700  *
2701  * \return If no error occurs, then the value of the field is
2702  *   returned. Otherwise an error handle is returned.
2703  */
2704 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
2705 Dart_GetField(Dart_Handle container, Dart_Handle name);
2706 
2707 /**
2708  * Sets the value of a field.
2709  *
2710  * The 'container' parameter may actually be an object, type, or
2711  * library.  If 'container' is an object, then this function will
2712  * access an instance field.  If 'container' is a type, then this
2713  * function will access a static field.  If 'container' is a library,
2714  * then this function will access a top-level variable.
2715  * NOTE: This API call cannot be used to access fields of a type object.
2716  *
2717  * This function ignores field visibility (leading underscores in names).
2718  *
2719  * May generate an unhandled exception error.
2720  *
2721  * \param container An object, type, or library.
2722  * \param name A field name.
2723  * \param value The new field value.
2724  *
2725  * \return A valid handle if no error occurs.
2726  */
2727 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
2728 Dart_SetField(Dart_Handle container, Dart_Handle name, Dart_Handle value);
2729 
2730 /*
2731  * ==========
2732  * Exceptions
2733  * ==========
2734  */
2735 
2736 /*
2737  * TODO(turnidge): Remove these functions from the api and replace all
2738  * uses with Dart_NewUnhandledExceptionError. */
2739 
2740 /**
2741  * Throws an exception.
2742  *
2743  * This function causes a Dart language exception to be thrown. This
2744  * will proceed in the standard way, walking up Dart frames until an
2745  * appropriate 'catch' block is found, executing 'finally' blocks,
2746  * etc.
2747  *
2748  * If an error handle is passed into this function, the error is
2749  * propagated immediately.  See Dart_PropagateError for a discussion
2750  * of error propagation.
2751  *
2752  * If successful, this function does not return. Note that this means
2753  * that the destructors of any stack-allocated C++ objects will not be
2754  * called. If there are no Dart frames on the stack, an error occurs.
2755  *
2756  * \return An error handle if the exception was not thrown.
2757  *   Otherwise the function does not return.
2758  */
2759 DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception);
2760 
2761 /**
2762  * Rethrows an exception.
2763  *
2764  * Rethrows an exception, unwinding all dart frames on the stack. If
2765  * successful, this function does not return. Note that this means
2766  * that the destructors of any stack-allocated C++ objects will not be
2767  * called. If there are no Dart frames on the stack, an error occurs.
2768  *
2769  * \return An error handle if the exception was not thrown.
2770  *   Otherwise the function does not return.
2771  */
2772 DART_EXPORT Dart_Handle Dart_ReThrowException(Dart_Handle exception,
2773                                               Dart_Handle stacktrace);
2774 
2775 /*
2776  * ===========================
2777  * Native fields and functions
2778  * ===========================
2779  */
2780 
2781 /**
2782  * Gets the number of native instance fields in an object.
2783  */
2784 DART_EXPORT Dart_Handle Dart_GetNativeInstanceFieldCount(Dart_Handle obj,
2785                                                          int* count);
2786 
2787 /**
2788  * Gets the value of a native field.
2789  *
2790  * TODO(turnidge): Document.
2791  */
2792 DART_EXPORT Dart_Handle Dart_GetNativeInstanceField(Dart_Handle obj,
2793                                                     int index,
2794                                                     intptr_t* value);
2795 
2796 /**
2797  * Sets the value of a native field.
2798  *
2799  * TODO(turnidge): Document.
2800  */
2801 DART_EXPORT Dart_Handle Dart_SetNativeInstanceField(Dart_Handle obj,
2802                                                     int index,
2803                                                     intptr_t value);
2804 
2805 /**
2806  * The arguments to a native function.
2807  *
2808  * This object is passed to a native function to represent its
2809  * arguments and return value. It allows access to the arguments to a
2810  * native function by index. It also allows the return value of a
2811  * native function to be set.
2812  */
2813 typedef struct _Dart_NativeArguments* Dart_NativeArguments;
2814 
2815 /**
2816  * Extracts current isolate group data from the native arguments structure.
2817  */
2818 DART_EXPORT void* Dart_GetNativeIsolateGroupData(Dart_NativeArguments args);
2819 
2820 typedef enum {
2821   Dart_NativeArgument_kBool = 0,
2822   Dart_NativeArgument_kInt32,
2823   Dart_NativeArgument_kUint32,
2824   Dart_NativeArgument_kInt64,
2825   Dart_NativeArgument_kUint64,
2826   Dart_NativeArgument_kDouble,
2827   Dart_NativeArgument_kString,
2828   Dart_NativeArgument_kInstance,
2829   Dart_NativeArgument_kNativeFields,
2830 } Dart_NativeArgument_Type;
2831 
2832 typedef struct _Dart_NativeArgument_Descriptor {
2833   uint8_t type;
2834   uint8_t index;
2835 } Dart_NativeArgument_Descriptor;
2836 
2837 typedef union _Dart_NativeArgument_Value {
2838   bool as_bool;
2839   int32_t as_int32;
2840   uint32_t as_uint32;
2841   int64_t as_int64;
2842   uint64_t as_uint64;
2843   double as_double;
2844   struct {
2845     Dart_Handle dart_str;
2846     void* peer;
2847   } as_string;
2848   struct {
2849     intptr_t num_fields;
2850     intptr_t* values;
2851   } as_native_fields;
2852   Dart_Handle as_instance;
2853 } Dart_NativeArgument_Value;
2854 
2855 enum {
2856   kNativeArgNumberPos = 0,
2857   kNativeArgNumberSize = 8,
2858   kNativeArgTypePos = kNativeArgNumberPos + kNativeArgNumberSize,
2859   kNativeArgTypeSize = 8,
2860 };
2861 
2862 #define BITMASK(size) ((1 << size) - 1)
2863 #define DART_NATIVE_ARG_DESCRIPTOR(type, position)                             \
2864   (((type & BITMASK(kNativeArgTypeSize)) << kNativeArgTypePos) |               \
2865    (position & BITMASK(kNativeArgNumberSize)))
2866 
2867 /**
2868  * Gets the native arguments based on the types passed in and populates
2869  * the passed arguments buffer with appropriate native values.
2870  *
2871  * \param args the Native arguments block passed into the native call.
2872  * \param num_arguments length of argument descriptor array and argument
2873  *   values array passed in.
2874  * \param arg_descriptors an array that describes the arguments that
2875  *   need to be retrieved. For each argument to be retrieved the descriptor
2876  *   contains the argument number (0, 1 etc.) and the argument type
2877  *   described using Dart_NativeArgument_Type, e.g:
2878  *   DART_NATIVE_ARG_DESCRIPTOR(Dart_NativeArgument_kBool, 1) indicates
2879  *   that the first argument is to be retrieved and it should be a boolean.
2880  * \param arg_values array into which the native arguments need to be
2881  *   extracted into, the array is allocated by the caller (it could be
2882  *   stack allocated to avoid the malloc/free performance overhead).
2883  *
2884  * \return Success if all the arguments could be extracted correctly,
2885  *   returns an error handle if there were any errors while extracting the
2886  *   arguments (mismatched number of arguments, incorrect types, etc.).
2887  */
2888 DART_EXPORT Dart_Handle
2889 Dart_GetNativeArguments(Dart_NativeArguments args,
2890                         int num_arguments,
2891                         const Dart_NativeArgument_Descriptor* arg_descriptors,
2892                         Dart_NativeArgument_Value* arg_values);
2893 
2894 /**
2895  * Gets the native argument at some index.
2896  */
2897 DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args,
2898                                                int index);
2899 /* TODO(turnidge): Specify the behavior of an out-of-bounds access. */
2900 
2901 /**
2902  * Gets the number of native arguments.
2903  */
2904 DART_EXPORT int Dart_GetNativeArgumentCount(Dart_NativeArguments args);
2905 
2906 /**
2907  * Gets all the native fields of the native argument at some index.
2908  * \param args Native arguments structure.
2909  * \param arg_index Index of the desired argument in the structure above.
2910  * \param num_fields size of the intptr_t array 'field_values' passed in.
2911  * \param field_values intptr_t array in which native field values are returned.
2912  * \return Success if the native fields where copied in successfully. Otherwise
2913  *   returns an error handle. On success the native field values are copied
2914  *   into the 'field_values' array, if the argument at 'arg_index' is a
2915  *   null object then 0 is copied as the native field values into the
2916  *   'field_values' array.
2917  */
2918 DART_EXPORT Dart_Handle
2919 Dart_GetNativeFieldsOfArgument(Dart_NativeArguments args,
2920                                int arg_index,
2921                                int num_fields,
2922                                intptr_t* field_values);
2923 
2924 /**
2925  * Gets the native field of the receiver.
2926  */
2927 DART_EXPORT Dart_Handle Dart_GetNativeReceiver(Dart_NativeArguments args,
2928                                                intptr_t* value);
2929 
2930 /**
2931  * Gets a string native argument at some index.
2932  * \param args Native arguments structure.
2933  * \param arg_index Index of the desired argument in the structure above.
2934  * \param peer Returns the peer pointer if the string argument has one.
2935  * \return Success if the string argument has a peer, if it does not
2936  *   have a peer then the String object is returned. Otherwise returns
2937  *   an error handle (argument is not a String object).
2938  */
2939 DART_EXPORT Dart_Handle Dart_GetNativeStringArgument(Dart_NativeArguments args,
2940                                                      int arg_index,
2941                                                      void** peer);
2942 
2943 /**
2944  * Gets an integer native argument at some index.
2945  * \param args Native arguments structure.
2946  * \param arg_index Index of the desired argument in the structure above.
2947  * \param value Returns the integer value if the argument is an Integer.
2948  * \return Success if no error occurs. Otherwise returns an error handle.
2949  */
2950 DART_EXPORT Dart_Handle Dart_GetNativeIntegerArgument(Dart_NativeArguments args,
2951                                                       int index,
2952                                                       int64_t* value);
2953 
2954 /**
2955  * Gets a boolean native argument at some index.
2956  * \param args Native arguments structure.
2957  * \param arg_index Index of the desired argument in the structure above.
2958  * \param value Returns the boolean value if the argument is a Boolean.
2959  * \return Success if no error occurs. Otherwise returns an error handle.
2960  */
2961 DART_EXPORT Dart_Handle Dart_GetNativeBooleanArgument(Dart_NativeArguments args,
2962                                                       int index,
2963                                                       bool* value);
2964 
2965 /**
2966  * Gets a double native argument at some index.
2967  * \param args Native arguments structure.
2968  * \param arg_index Index of the desired argument in the structure above.
2969  * \param value Returns the double value if the argument is a double.
2970  * \return Success if no error occurs. Otherwise returns an error handle.
2971  */
2972 DART_EXPORT Dart_Handle Dart_GetNativeDoubleArgument(Dart_NativeArguments args,
2973                                                      int index,
2974                                                      double* value);
2975 
2976 /**
2977  * Sets the return value for a native function.
2978  *
2979  * If retval is an Error handle, then error will be propagated once
2980  * the native functions exits. See Dart_PropagateError for a
2981  * discussion of how different types of errors are propagated.
2982  */
2983 DART_EXPORT void Dart_SetReturnValue(Dart_NativeArguments args,
2984                                      Dart_Handle retval);
2985 
2986 DART_EXPORT void Dart_SetWeakHandleReturnValue(Dart_NativeArguments args,
2987                                                Dart_WeakPersistentHandle rval);
2988 
2989 DART_EXPORT void Dart_SetBooleanReturnValue(Dart_NativeArguments args,
2990                                             bool retval);
2991 
2992 DART_EXPORT void Dart_SetIntegerReturnValue(Dart_NativeArguments args,
2993                                             int64_t retval);
2994 
2995 DART_EXPORT void Dart_SetDoubleReturnValue(Dart_NativeArguments args,
2996                                            double retval);
2997 
2998 /**
2999  * A native function.
3000  */
3001 typedef void (*Dart_NativeFunction)(Dart_NativeArguments arguments);
3002 
3003 /**
3004  * Native entry resolution callback.
3005  *
3006  * For libraries and scripts which have native functions, the embedder
3007  * can provide a native entry resolver. This callback is used to map a
3008  * name/arity to a Dart_NativeFunction. If no function is found, the
3009  * callback should return NULL.
3010  *
3011  * The parameters to the native resolver function are:
3012  * \param name a Dart string which is the name of the native function.
3013  * \param num_of_arguments is the number of arguments expected by the
3014  *   native function.
3015  * \param auto_setup_scope is a boolean flag that can be set by the resolver
3016  *   to indicate if this function needs a Dart API scope (see Dart_EnterScope/
3017  *   Dart_ExitScope) to be setup automatically by the VM before calling into
3018  *   the native function. By default most native functions would require this
3019  *   to be true but some light weight native functions which do not call back
3020  *   into the VM through the Dart API may not require a Dart scope to be
3021  *   setup automatically.
3022  *
3023  * \return A valid Dart_NativeFunction which resolves to a native entry point
3024  *   for the native function.
3025  *
3026  * See Dart_SetNativeResolver.
3027  */
3028 typedef Dart_NativeFunction (*Dart_NativeEntryResolver)(Dart_Handle name,
3029                                                         int num_of_arguments,
3030                                                         bool* auto_setup_scope);
3031 /* TODO(turnidge): Consider renaming to NativeFunctionResolver or
3032  * NativeResolver. */
3033 
3034 /**
3035  * Native entry symbol lookup callback.
3036  *
3037  * For libraries and scripts which have native functions, the embedder
3038  * can provide a callback for mapping a native entry to a symbol. This callback
3039  * maps a native function entry PC to the native function name. If no native
3040  * entry symbol can be found, the callback should return NULL.
3041  *
3042  * The parameters to the native reverse resolver function are:
3043  * \param nf A Dart_NativeFunction.
3044  *
3045  * \return A const UTF-8 string containing the symbol name or NULL.
3046  *
3047  * See Dart_SetNativeResolver.
3048  */
3049 typedef const uint8_t* (*Dart_NativeEntrySymbol)(Dart_NativeFunction nf);
3050 
3051 /*
3052  * ===========
3053  * Environment
3054  * ===========
3055  */
3056 
3057 /**
3058  * An environment lookup callback function.
3059  *
3060  * \param name The name of the value to lookup in the environment.
3061  *
3062  * \return A valid handle to a string if the name exists in the
3063  * current environment or Dart_Null() if not.
3064  */
3065 typedef Dart_Handle (*Dart_EnvironmentCallback)(Dart_Handle name);
3066 
3067 /**
3068  * Sets the environment callback for the current isolate. This
3069  * callback is used to lookup environment values by name in the
3070  * current environment. This enables the embedder to supply values for
3071  * the const constructors bool.fromEnvironment, int.fromEnvironment
3072  * and String.fromEnvironment.
3073  */
3074 DART_EXPORT Dart_Handle
3075 Dart_SetEnvironmentCallback(Dart_EnvironmentCallback callback);
3076 
3077 /**
3078  * Sets the callback used to resolve native functions for a library.
3079  *
3080  * \param library A library.
3081  * \param resolver A native entry resolver.
3082  *
3083  * \return A valid handle if the native resolver was set successfully.
3084  */
3085 DART_EXPORT Dart_Handle
3086 Dart_SetNativeResolver(Dart_Handle library,
3087                        Dart_NativeEntryResolver resolver,
3088                        Dart_NativeEntrySymbol symbol);
3089 /* TODO(turnidge): Rename to Dart_LibrarySetNativeResolver? */
3090 
3091 /**
3092  * Returns the callback used to resolve native functions for a library.
3093  *
3094  * \param library A library.
3095  * \param resolver a pointer to a Dart_NativeEntryResolver
3096  *
3097  * \return A valid handle if the library was found.
3098  */
3099 DART_EXPORT Dart_Handle
3100 Dart_GetNativeResolver(Dart_Handle library, Dart_NativeEntryResolver* resolver);
3101 
3102 /**
3103  * Returns the callback used to resolve native function symbols for a library.
3104  *
3105  * \param library A library.
3106  * \param resolver a pointer to a Dart_NativeEntrySymbol.
3107  *
3108  * \return A valid handle if the library was found.
3109  */
3110 DART_EXPORT Dart_Handle Dart_GetNativeSymbol(Dart_Handle library,
3111                                              Dart_NativeEntrySymbol* resolver);
3112 
3113 /*
3114  * =====================
3115  * Scripts and Libraries
3116  * =====================
3117  */
3118 
3119 typedef enum {
3120   Dart_kCanonicalizeUrl = 0,
3121   Dart_kImportTag,
3122   Dart_kKernelTag,
3123   Dart_kImportExtensionTag,
3124 } Dart_LibraryTag;
3125 
3126 /**
3127  * The library tag handler is a multi-purpose callback provided by the
3128  * embedder to the Dart VM. The embedder implements the tag handler to
3129  * provide the ability to load Dart scripts and imports.
3130  *
3131  * -- TAGS --
3132  *
3133  * Dart_kCanonicalizeUrl
3134  *
3135  * This tag indicates that the embedder should canonicalize 'url' with
3136  * respect to 'library'.  For most embedders, the
3137  * Dart_DefaultCanonicalizeUrl function is a sufficient implementation
3138  * of this tag.  The return value should be a string holding the
3139  * canonicalized url.
3140  *
3141  * Dart_kImportTag
3142  *
3143  * This tag is used to load a library from IsolateMirror.loadUri. The embedder
3144  * should call Dart_LoadLibraryFromKernel to provide the library to the VM. The
3145  * return value should be an error or library (the result from
3146  * Dart_LoadLibraryFromKernel).
3147  *
3148  * Dart_kKernelTag
3149  *
3150  * This tag is used to load the intermediate file (kernel) generated by
3151  * the Dart front end. This tag is typically used when a 'hot-reload'
3152  * of an application is needed and the VM is 'use dart front end' mode.
3153  * The dart front end typically compiles all the scripts, imports and part
3154  * files into one intermediate file hence we don't use the source/import or
3155  * script tags. The return value should be an error or a TypedData containing
3156  * the kernel bytes.
3157  *
3158  * Dart_kImportExtensionTag
3159  *
3160  * This tag is used to load an external import (shared object file). The
3161  * extension path must have the scheme 'dart-ext:'.
3162  */
3163 typedef Dart_Handle (*Dart_LibraryTagHandler)(
3164     Dart_LibraryTag tag,
3165     Dart_Handle library_or_package_map_url,
3166     Dart_Handle url);
3167 
3168 /**
3169  * Sets library tag handler for the current isolate. This handler is
3170  * used to handle the various tags encountered while loading libraries
3171  * or scripts in the isolate.
3172  *
3173  * \param handler Handler code to be used for handling the various tags
3174  *   encountered while loading libraries or scripts in the isolate.
3175  *
3176  * \return If no error occurs, the handler is set for the isolate.
3177  *   Otherwise an error handle is returned.
3178  *
3179  * TODO(turnidge): Document.
3180  */
3181 DART_EXPORT Dart_Handle
3182 Dart_SetLibraryTagHandler(Dart_LibraryTagHandler handler);
3183 
3184 /**
3185  * Handles deferred loading requests. When this handler is invoked, it should
3186  * eventually load the deferred loading unit with the given id and call
3187  * Dart_DeferredLoadComplete or Dart_DeferredLoadCompleteError. It is
3188  * recommended that the loading occur asynchronously, but it is permitted to
3189  * call Dart_DeferredLoadComplete or Dart_DeferredLoadCompleteError before the
3190  * handler returns.
3191  *
3192  * If an error is returned, it will be propogated through
3193  * `prefix.loadLibrary()`. This is useful for synchronous
3194  * implementations, which must propogate any unwind errors from
3195  * Dart_DeferredLoadComplete or Dart_DeferredLoadComplete. Otherwise the handler
3196  * should return a non-error such as `Dart_Null()`.
3197  */
3198 typedef Dart_Handle (*Dart_DeferredLoadHandler)(intptr_t loading_unit_id);
3199 
3200 /**
3201  * Sets the deferred load handler for the current isolate. This handler is
3202  * used to handle loading deferred imports in an AppJIT or AppAOT program.
3203  */
3204 DART_EXPORT Dart_Handle
3205 Dart_SetDeferredLoadHandler(Dart_DeferredLoadHandler handler);
3206 
3207 /**
3208  * Notifies the VM that a deferred load completed successfully. This function
3209  * will eventually cause the corresponding `prefix.loadLibrary()` futures to
3210  * complete.
3211  *
3212  * Requires the current isolate to be the same current isolate during the
3213  * invocation of the Dart_DeferredLoadHandler.
3214  */
3215 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3216 Dart_DeferredLoadComplete(intptr_t loading_unit_id,
3217                           const uint8_t* snapshot_data,
3218                           const uint8_t* snapshot_instructions);
3219 
3220 /**
3221  * Notifies the VM that a deferred load failed. This function
3222  * will eventually cause the corresponding `prefix.loadLibrary()` futures to
3223  * complete with an error.
3224  *
3225  * If `transient` is true, future invocations of `prefix.loadLibrary()` will
3226  * trigger new load requests. If false, futures invocation will complete with
3227  * the same error.
3228  *
3229  * Requires the current isolate to be the same current isolate during the
3230  * invocation of the Dart_DeferredLoadHandler.
3231  */
3232 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3233 Dart_DeferredLoadCompleteError(intptr_t loading_unit_id,
3234                                const char* error_message,
3235                                bool transient);
3236 
3237 /**
3238  * Canonicalizes a url with respect to some library.
3239  *
3240  * The url is resolved with respect to the library's url and some url
3241  * normalizations are performed.
3242  *
3243  * This canonicalization function should be sufficient for most
3244  * embedders to implement the Dart_kCanonicalizeUrl tag.
3245  *
3246  * \param base_url The base url relative to which the url is
3247  *                being resolved.
3248  * \param url The url being resolved and canonicalized.  This
3249  *            parameter is a string handle.
3250  *
3251  * \return If no error occurs, a String object is returned.  Otherwise
3252  *   an error handle is returned.
3253  */
3254 DART_EXPORT Dart_Handle Dart_DefaultCanonicalizeUrl(Dart_Handle base_url,
3255                                                     Dart_Handle url);
3256 
3257 /**
3258  * Loads the root library for the current isolate.
3259  *
3260  * Requires there to be no current root library.
3261  *
3262  * \param buffer A buffer which contains a kernel binary (see
3263  *     pkg/kernel/binary.md). Must remain valid until isolate group shutdown.
3264  * \param buffer_size Length of the passed in buffer.
3265  *
3266  * \return A handle to the root library, or an error.
3267  */
3268 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3269 Dart_LoadScriptFromKernel(const uint8_t* kernel_buffer, intptr_t kernel_size);
3270 
3271 /**
3272  * Gets the library for the root script for the current isolate.
3273  *
3274  * If the root script has not yet been set for the current isolate,
3275  * this function returns Dart_Null().  This function never returns an
3276  * error handle.
3277  *
3278  * \return Returns the root Library for the current isolate or Dart_Null().
3279  */
3280 DART_EXPORT Dart_Handle Dart_RootLibrary();
3281 
3282 /**
3283  * Sets the root library for the current isolate.
3284  *
3285  * \return Returns an error handle if `library` is not a library handle.
3286  */
3287 DART_EXPORT Dart_Handle Dart_SetRootLibrary(Dart_Handle library);
3288 
3289 /**
3290  * Lookup or instantiate a legacy type by name and type arguments from a
3291  * Library.
3292  *
3293  * \param library The library containing the class or interface.
3294  * \param class_name The class name for the type.
3295  * \param number_of_type_arguments Number of type arguments.
3296  *   For non parametric types the number of type arguments would be 0.
3297  * \param type_arguments Pointer to an array of type arguments.
3298  *   For non parameteric types a NULL would be passed in for this argument.
3299  *
3300  * \return If no error occurs, the type is returned.
3301  *   Otherwise an error handle is returned.
3302  */
3303 DART_EXPORT Dart_Handle Dart_GetType(Dart_Handle library,
3304                                      Dart_Handle class_name,
3305                                      intptr_t number_of_type_arguments,
3306                                      Dart_Handle* type_arguments);
3307 
3308 /**
3309  * Lookup or instantiate a nullable type by name and type arguments from
3310  * Library.
3311  *
3312  * \param library The library containing the class or interface.
3313  * \param class_name The class name for the type.
3314  * \param number_of_type_arguments Number of type arguments.
3315  *   For non parametric types the number of type arguments would be 0.
3316  * \param type_arguments Pointer to an array of type arguments.
3317  *   For non parameteric types a NULL would be passed in for this argument.
3318  *
3319  * \return If no error occurs, the type is returned.
3320  *   Otherwise an error handle is returned.
3321  */
3322 DART_EXPORT Dart_Handle Dart_GetNullableType(Dart_Handle library,
3323                                              Dart_Handle class_name,
3324                                              intptr_t number_of_type_arguments,
3325                                              Dart_Handle* type_arguments);
3326 
3327 /**
3328  * Lookup or instantiate a non-nullable type by name and type arguments from
3329  * Library.
3330  *
3331  * \param library The library containing the class or interface.
3332  * \param class_name The class name for the type.
3333  * \param number_of_type_arguments Number of type arguments.
3334  *   For non parametric types the number of type arguments would be 0.
3335  * \param type_arguments Pointer to an array of type arguments.
3336  *   For non parameteric types a NULL would be passed in for this argument.
3337  *
3338  * \return If no error occurs, the type is returned.
3339  *   Otherwise an error handle is returned.
3340  */
3341 DART_EXPORT Dart_Handle
3342 Dart_GetNonNullableType(Dart_Handle library,
3343                         Dart_Handle class_name,
3344                         intptr_t number_of_type_arguments,
3345                         Dart_Handle* type_arguments);
3346 
3347 /**
3348  * Creates a nullable version of the provided type.
3349  *
3350  * \param type The type to be converted to a nullable type.
3351  *
3352  * \return If no error occurs, a nullable type is returned.
3353  *   Otherwise an error handle is returned.
3354  */
3355 DART_EXPORT Dart_Handle Dart_TypeToNullableType(Dart_Handle type);
3356 
3357 /**
3358  * Creates a non-nullable version of the provided type.
3359  *
3360  * \param type The type to be converted to a non-nullable type.
3361  *
3362  * \return If no error occurs, a non-nullable type is returned.
3363  *   Otherwise an error handle is returned.
3364  */
3365 DART_EXPORT Dart_Handle Dart_TypeToNonNullableType(Dart_Handle type);
3366 
3367 /**
3368  * A type's nullability.
3369  *
3370  * \param type A Dart type.
3371  * \param result An out parameter containing the result of the check. True if
3372  * the type is of the specified nullability, false otherwise.
3373  *
3374  * \return Returns an error handle if type is not of type Type.
3375  */
3376 DART_EXPORT Dart_Handle Dart_IsNullableType(Dart_Handle type, bool* result);
3377 DART_EXPORT Dart_Handle Dart_IsNonNullableType(Dart_Handle type, bool* result);
3378 DART_EXPORT Dart_Handle Dart_IsLegacyType(Dart_Handle type, bool* result);
3379 
3380 /**
3381  * Lookup a class or interface by name from a Library.
3382  *
3383  * \param library The library containing the class or interface.
3384  * \param class_name The name of the class or interface.
3385  *
3386  * \return If no error occurs, the class or interface is
3387  *   returned. Otherwise an error handle is returned.
3388  */
3389 DART_EXPORT Dart_Handle Dart_GetClass(Dart_Handle library,
3390                                       Dart_Handle class_name);
3391 /* TODO(asiva): The above method needs to be removed once all uses
3392  * of it are removed from the embedder code. */
3393 
3394 /**
3395  * Returns an import path to a Library, such as "file:///test.dart" or
3396  * "dart:core".
3397  */
3398 DART_EXPORT Dart_Handle Dart_LibraryUrl(Dart_Handle library);
3399 
3400 /**
3401  * Returns a URL from which a Library was loaded.
3402  */
3403 DART_EXPORT Dart_Handle Dart_LibraryResolvedUrl(Dart_Handle library);
3404 
3405 /**
3406  * \return An array of libraries.
3407  */
3408 DART_EXPORT Dart_Handle Dart_GetLoadedLibraries();
3409 
3410 DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url);
3411 /* TODO(turnidge): Consider returning Dart_Null() when the library is
3412  * not found to distinguish that from a true error case. */
3413 
3414 /**
3415  * Report an loading error for the library.
3416  *
3417  * \param library The library that failed to load.
3418  * \param error The Dart error instance containing the load error.
3419  *
3420  * \return If the VM handles the error, the return value is
3421  * a null handle. If it doesn't handle the error, the error
3422  * object is returned.
3423  */
3424 DART_EXPORT Dart_Handle Dart_LibraryHandleError(Dart_Handle library,
3425                                                 Dart_Handle error);
3426 
3427 /**
3428  * Called by the embedder to load a partial program. Does not set the root
3429  * library.
3430  *
3431  * \param buffer A buffer which contains a kernel binary (see
3432  *     pkg/kernel/binary.md). Must remain valid until isolate shutdown.
3433  * \param buffer_size Length of the passed in buffer.
3434  *
3435  * \return A handle to the main library of the compilation unit, or an error.
3436  */
3437 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3438 Dart_LoadLibraryFromKernel(const uint8_t* kernel_buffer,
3439                            intptr_t kernel_buffer_size);
3440 
3441 /**
3442  * Returns a flattened list of pairs. The first element in each pair is the
3443  * importing library and and the second element is the imported library for each
3444  * import in the isolate of a library whose URI's scheme is [scheme].
3445  *
3446  * Requires there to be a current isolate.
3447  *
3448  * \return A handle to a list of flattened pairs of importer-importee.
3449  */
3450 DART_EXPORT Dart_Handle Dart_GetImportsOfScheme(Dart_Handle scheme);
3451 
3452 /**
3453  * Indicates that all outstanding load requests have been satisfied.
3454  * This finalizes all the new classes loaded and optionally completes
3455  * deferred library futures.
3456  *
3457  * Requires there to be a current isolate.
3458  *
3459  * \param complete_futures Specify true if all deferred library
3460  *  futures should be completed, false otherwise.
3461  *
3462  * \return Success if all classes have been finalized and deferred library
3463  *   futures are completed. Otherwise, returns an error.
3464  */
3465 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3466 Dart_FinalizeLoading(bool complete_futures);
3467 
3468 /*
3469  * =====
3470  * Peers
3471  * =====
3472  */
3473 
3474 /**
3475  * The peer field is a lazily allocated field intended for storage of
3476  * an uncommonly used values.  Most instances types can have a peer
3477  * field allocated.  The exceptions are subtypes of Null, num, and
3478  * bool.
3479  */
3480 
3481 /**
3482  * Returns the value of peer field of 'object' in 'peer'.
3483  *
3484  * \param object An object.
3485  * \param peer An out parameter that returns the value of the peer
3486  *   field.
3487  *
3488  * \return Returns an error if 'object' is a subtype of Null, num, or
3489  *   bool.
3490  */
3491 DART_EXPORT Dart_Handle Dart_GetPeer(Dart_Handle object, void** peer);
3492 
3493 /**
3494  * Sets the value of the peer field of 'object' to the value of
3495  * 'peer'.
3496  *
3497  * \param object An object.
3498  * \param peer A value to store in the peer field.
3499  *
3500  * \return Returns an error if 'object' is a subtype of Null, num, or
3501  *   bool.
3502  */
3503 DART_EXPORT Dart_Handle Dart_SetPeer(Dart_Handle object, void* peer);
3504 
3505 /*
3506  * ======
3507  * Kernel
3508  * ======
3509  */
3510 
3511 /**
3512  * Experimental support for Dart to Kernel parser isolate.
3513  *
3514  * TODO(hausner): Document finalized interface.
3515  *
3516  */
3517 
3518 // TODO(33433): Remove kernel service from the embedding API.
3519 
3520 typedef enum {
3521   Dart_KernelCompilationStatus_Unknown = -1,
3522   Dart_KernelCompilationStatus_Ok = 0,
3523   Dart_KernelCompilationStatus_Error = 1,
3524   Dart_KernelCompilationStatus_Crash = 2,
3525 } Dart_KernelCompilationStatus;
3526 
3527 typedef struct {
3528   Dart_KernelCompilationStatus status;
3529   bool null_safety;
3530   char* error;
3531   uint8_t* kernel;
3532   intptr_t kernel_size;
3533 } Dart_KernelCompilationResult;
3534 
3535 DART_EXPORT bool Dart_IsKernelIsolate(Dart_Isolate isolate);
3536 DART_EXPORT bool Dart_KernelIsolateIsRunning();
3537 DART_EXPORT Dart_Port Dart_KernelPort();
3538 
3539 /**
3540  * Compiles the given `script_uri` to a kernel file.
3541  *
3542  * \param platform_kernel A buffer containing the kernel of the platform (e.g.
3543  * `vm_platform_strong.dill`). The VM does not take ownership of this memory.
3544  *
3545  * \param platform_kernel_size The length of the platform_kernel buffer.
3546  *
3547  * \return Returns the result of the compilation.
3548  *
3549  * On a successful compilation the returned [Dart_KernelCompilationResult] has
3550  * a status of [Dart_KernelCompilationStatus_Ok] and the `kernel`/`kernel_size`
3551  * fields are set. The caller takes ownership of the malloc()ed buffer.
3552  *
3553  * On a failed compilation the `error` might be set describing the reason for
3554  * the failed compilation. The caller takes ownership of the malloc()ed
3555  * error.
3556  *
3557  * Requires there to be a current isolate.
3558  */
3559 DART_EXPORT Dart_KernelCompilationResult
3560 Dart_CompileToKernel(const char* script_uri,
3561                      const uint8_t* platform_kernel,
3562                      const intptr_t platform_kernel_size,
3563                      bool incremental_compile,
3564                      const char* package_config);
3565 
3566 typedef struct {
3567   const char* uri;
3568   const char* source;
3569 } Dart_SourceFile;
3570 
3571 DART_EXPORT Dart_KernelCompilationResult Dart_KernelListDependencies();
3572 
3573 /**
3574  * Sets the kernel buffer which will be used to load Dart SDK sources
3575  * dynamically at runtime.
3576  *
3577  * \param platform_kernel A buffer containing kernel which has sources for the
3578  * Dart SDK populated. Note: The VM does not take ownership of this memory.
3579  *
3580  * \param platform_kernel_size The length of the platform_kernel buffer.
3581  */
3582 DART_EXPORT void Dart_SetDartLibrarySourcesKernel(
3583     const uint8_t* platform_kernel,
3584     const intptr_t platform_kernel_size);
3585 
3586 /**
3587  * Detect the null safety opt-in status.
3588  *
3589  * When running from source, it is based on the opt-in status of `script_uri`.
3590  * When running from a kernel buffer, it is based on the mode used when
3591  *   generating `kernel_buffer`.
3592  * When running from an appJIT or AOT snapshot, it is based on the mode used
3593  *   when generating `snapshot_data`.
3594  *
3595  * \param script_uri Uri of the script that contains the source code
3596  *
3597  * \param package_config Uri of the package configuration file (either in format
3598  *   of .packages or .dart_tool/package_config.json) for the null safety
3599  *   detection to resolve package imports against. If this parameter is not
3600  *   passed the package resolution of the parent isolate should be used.
3601  *
3602  * \param original_working_directory current working directory when the VM
3603  *   process was launched, this is used to correctly resolve the path specified
3604  *   for package_config.
3605  *
3606  * \param snapshot_data
3607  *
3608  * \param snapshot_instructions Buffers containing a snapshot of the
3609  *   isolate or NULL if no snapshot is provided. If provided, the buffers must
3610  *   remain valid until the isolate shuts down.
3611  *
3612  * \param kernel_buffer
3613  *
3614  * \param kernel_buffer_size A buffer which contains a kernel/DIL program. Must
3615  *   remain valid until isolate shutdown.
3616  *
3617  * \return Returns true if the null safety is opted in by the input being
3618  *   run `script_uri`, `snapshot_data` or `kernel_buffer`.
3619  *
3620  */
3621 DART_EXPORT bool Dart_DetectNullSafety(const char* script_uri,
3622                                        const char* package_config,
3623                                        const char* original_working_directory,
3624                                        const uint8_t* snapshot_data,
3625                                        const uint8_t* snapshot_instructions,
3626                                        const uint8_t* kernel_buffer,
3627                                        intptr_t kernel_buffer_size);
3628 
3629 #define DART_KERNEL_ISOLATE_NAME "kernel-service"
3630 
3631 /*
3632  * =======
3633  * Service
3634  * =======
3635  */
3636 
3637 #define DART_VM_SERVICE_ISOLATE_NAME "vm-service"
3638 
3639 /**
3640  * Returns true if isolate is the service isolate.
3641  *
3642  * \param isolate An isolate
3643  *
3644  * \return Returns true if 'isolate' is the service isolate.
3645  */
3646 DART_EXPORT bool Dart_IsServiceIsolate(Dart_Isolate isolate);
3647 
3648 /**
3649  * Writes the CPU profile to the timeline as a series of 'instant' events.
3650  *
3651  * Note that this is an expensive operation.
3652  *
3653  * \param main_port The main port of the Isolate whose profile samples to write.
3654  * \param error An optional error, must be free()ed by caller.
3655  *
3656  * \return Returns true if the profile is successfully written and false
3657  *         otherwise.
3658  */
3659 DART_EXPORT bool Dart_WriteProfileToTimeline(Dart_Port main_port, char** error);
3660 
3661 /*
3662  * ====================
3663  * Compilation Feedback
3664  * ====================
3665  */
3666 
3667 /**
3668  * Record all functions which have been compiled in the current isolate.
3669  *
3670  * \param buffer Returns a pointer to a buffer containing the trace.
3671  *   This buffer is scope allocated and is only valid  until the next call to
3672  *   Dart_ExitScope.
3673  * \param size Returns the size of the buffer.
3674  * \return Returns an valid handle upon success.
3675  */
3676 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3677 Dart_SaveCompilationTrace(uint8_t** buffer, intptr_t* buffer_length);
3678 
3679 /**
3680  * Compile all functions from data from Dart_SaveCompilationTrace. Unlike JIT
3681  * feedback, this data is fuzzy: loading does not need to happen in the exact
3682  * program that was saved, the saver and loader do not need to agree on checked
3683  * mode versus production mode or debug/release/product.
3684  *
3685  * \return Returns an error handle if a compilation error was encountered.
3686  */
3687 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3688 Dart_LoadCompilationTrace(uint8_t* buffer, intptr_t buffer_length);
3689 
3690 /**
3691  * Record runtime feedback for the current isolate, including type feedback
3692  * and usage counters.
3693  *
3694  * \param buffer Returns a pointer to a buffer containing the trace.
3695  *   This buffer is scope allocated and is only valid  until the next call to
3696  *   Dart_ExitScope.
3697  * \param size Returns the size of the buffer.
3698  * \return Returns an valid handle upon success.
3699  */
3700 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3701 Dart_SaveTypeFeedback(uint8_t** buffer, intptr_t* buffer_length);
3702 
3703 /**
3704  * Compile functions using data from Dart_SaveTypeFeedback. The data must from a
3705  * VM with the same version and compiler flags.
3706  *
3707  * \return Returns an error handle if a compilation error was encountered or a
3708  *   version mismatch is detected.
3709  */
3710 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3711 Dart_LoadTypeFeedback(uint8_t* buffer, intptr_t buffer_length);
3712 
3713 /*
3714  * ==============
3715  * Precompilation
3716  * ==============
3717  */
3718 
3719 /**
3720  * Compiles all functions reachable from entry points and marks
3721  * the isolate to disallow future compilation.
3722  *
3723  * Entry points should be specified using `@pragma("vm:entry-point")`
3724  * annotation.
3725  *
3726  * \return An error handle if a compilation error or runtime error running const
3727  * constructors was encountered.
3728  */
3729 DART_EXPORT Dart_Handle Dart_Precompile();
3730 
3731 typedef void (*Dart_CreateLoadingUnitCallback)(
3732     void* callback_data,
3733     intptr_t loading_unit_id,
3734     void** write_callback_data,
3735     void** write_debug_callback_data);
3736 typedef void (*Dart_StreamingWriteCallback)(void* callback_data,
3737                                             const uint8_t* buffer,
3738                                             intptr_t size);
3739 typedef void (*Dart_StreamingCloseCallback)(void* callback_data);
3740 
3741 DART_EXPORT Dart_Handle Dart_LoadingUnitLibraryUris(intptr_t loading_unit_id);
3742 
3743 // On Darwin systems, 'dlsym' adds an '_' to the beginning of the symbol name.
3744 // Use the '...CSymbol' definitions for resolving through 'dlsym'. The actual
3745 // symbol names in the objects are given by the '...AsmSymbol' definitions.
3746 #if defined(__APPLE__)
3747 #define kSnapshotBuildIdCSymbol "kDartSnapshotBuildId"
3748 #define kVmSnapshotDataCSymbol "kDartVmSnapshotData"
3749 #define kVmSnapshotInstructionsCSymbol "kDartVmSnapshotInstructions"
3750 #define kVmSnapshotBssCSymbol "kDartVmSnapshotBss"
3751 #define kIsolateSnapshotDataCSymbol "kDartIsolateSnapshotData"
3752 #define kIsolateSnapshotInstructionsCSymbol "kDartIsolateSnapshotInstructions"
3753 #define kIsolateSnapshotBssCSymbol "kDartIsolateSnapshotBss"
3754 #else
3755 #define kSnapshotBuildIdCSymbol "_kDartSnapshotBuildId"
3756 #define kVmSnapshotDataCSymbol "_kDartVmSnapshotData"
3757 #define kVmSnapshotInstructionsCSymbol "_kDartVmSnapshotInstructions"
3758 #define kVmSnapshotBssCSymbol "_kDartVmSnapshotBss"
3759 #define kIsolateSnapshotDataCSymbol "_kDartIsolateSnapshotData"
3760 #define kIsolateSnapshotInstructionsCSymbol "_kDartIsolateSnapshotInstructions"
3761 #define kIsolateSnapshotBssCSymbol "_kDartIsolateSnapshotBss"
3762 #endif
3763 
3764 #define kSnapshotBuildIdAsmSymbol "_kDartSnapshotBuildId"
3765 #define kVmSnapshotDataAsmSymbol "_kDartVmSnapshotData"
3766 #define kVmSnapshotInstructionsAsmSymbol "_kDartVmSnapshotInstructions"
3767 #define kVmSnapshotBssAsmSymbol "_kDartVmSnapshotBss"
3768 #define kIsolateSnapshotDataAsmSymbol "_kDartIsolateSnapshotData"
3769 #define kIsolateSnapshotInstructionsAsmSymbol                                  \
3770   "_kDartIsolateSnapshotInstructions"
3771 #define kIsolateSnapshotBssAsmSymbol "_kDartIsolateSnapshotBss"
3772 
3773 /**
3774  *  Creates a precompiled snapshot.
3775  *   - A root library must have been loaded.
3776  *   - Dart_Precompile must have been called.
3777  *
3778  *  Outputs an assembly file defining the symbols listed in the definitions
3779  *  above.
3780  *
3781  *  The assembly should be compiled as a static or shared library and linked or
3782  *  loaded by the embedder. Running this snapshot requires a VM compiled with
3783  *  DART_PRECOMPILED_SNAPSHOT. The kDartVmSnapshotData and
3784  *  kDartVmSnapshotInstructions should be passed to Dart_Initialize. The
3785  *  kDartIsolateSnapshotData and kDartIsolateSnapshotInstructions should be
3786  *  passed to Dart_CreateIsolateGroup.
3787  *
3788  *  The callback will be invoked one or more times to provide the assembly code.
3789  *
3790  *  If stripped is true, then the assembly code will not include DWARF
3791  *  debugging sections.
3792  *
3793  *  If debug_callback_data is provided, debug_callback_data will be used with
3794  *  the callback to provide separate debugging information.
3795  *
3796  *  \return A valid handle if no error occurs during the operation.
3797  */
3798 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3799 Dart_CreateAppAOTSnapshotAsAssembly(Dart_StreamingWriteCallback callback,
3800                                     void* callback_data,
3801                                     bool stripped,
3802                                     void* debug_callback_data);
3803 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3804 Dart_CreateAppAOTSnapshotAsAssemblies(
3805     Dart_CreateLoadingUnitCallback next_callback,
3806     void* next_callback_data,
3807     bool stripped,
3808     Dart_StreamingWriteCallback write_callback,
3809     Dart_StreamingCloseCallback close_callback);
3810 
3811 /**
3812  *  Creates a precompiled snapshot.
3813  *   - A root library must have been loaded.
3814  *   - Dart_Precompile must have been called.
3815  *
3816  *  Outputs an ELF shared library defining the symbols
3817  *   - _kDartVmSnapshotData
3818  *   - _kDartVmSnapshotInstructions
3819  *   - _kDartIsolateSnapshotData
3820  *   - _kDartIsolateSnapshotInstructions
3821  *
3822  *  The shared library should be dynamically loaded by the embedder.
3823  *  Running this snapshot requires a VM compiled with DART_PRECOMPILED_SNAPSHOT.
3824  *  The kDartVmSnapshotData and kDartVmSnapshotInstructions should be passed to
3825  *  Dart_Initialize. The kDartIsolateSnapshotData and
3826  *  kDartIsolateSnapshotInstructions should be passed to Dart_CreateIsolate.
3827  *
3828  *  The callback will be invoked one or more times to provide the binary output.
3829  *
3830  *  If stripped is true, then the binary output will not include DWARF
3831  *  debugging sections.
3832  *
3833  *  If debug_callback_data is provided, debug_callback_data will be used with
3834  *  the callback to provide separate debugging information.
3835  *
3836  * \return A valid handle if no error occurs during the operation.
3837  */
3838 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3839 Dart_CreateAppAOTSnapshotAsElf(Dart_StreamingWriteCallback callback,
3840                                void* callback_data,
3841                                bool stripped,
3842                                void* debug_callback_data);
3843 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3844 Dart_CreateAppAOTSnapshotAsElfs(Dart_CreateLoadingUnitCallback next_callback,
3845                                 void* next_callback_data,
3846                                 bool stripped,
3847                                 Dart_StreamingWriteCallback write_callback,
3848                                 Dart_StreamingCloseCallback close_callback);
3849 
3850 /**
3851  *  Like Dart_CreateAppAOTSnapshotAsAssembly, but only includes
3852  *  kDartVmSnapshotData and kDartVmSnapshotInstructions. It also does
3853  *  not strip DWARF information from the generated assembly or allow for
3854  *  separate debug information.
3855  */
3856 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3857 Dart_CreateVMAOTSnapshotAsAssembly(Dart_StreamingWriteCallback callback,
3858                                    void* callback_data);
3859 
3860 /**
3861  * Sorts the class-ids in depth first traversal order of the inheritance
3862  * tree. This is a costly operation, but it can make method dispatch
3863  * more efficient and is done before writing snapshots.
3864  *
3865  * \return A valid handle if no error occurs during the operation.
3866  */
3867 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_SortClasses();
3868 
3869 /**
3870  *  Creates a snapshot that caches compiled code and type feedback for faster
3871  *  startup and quicker warmup in a subsequent process.
3872  *
3873  *  Outputs a snapshot in two pieces. The pieces should be passed to
3874  *  Dart_CreateIsolateGroup in a VM using the same VM snapshot pieces used in the
3875  *  current VM. The instructions piece must be loaded with read and execute
3876  *  permissions; the data piece may be loaded as read-only.
3877  *
3878  *   - Requires the VM to have not been started with --precompilation.
3879  *   - Not supported when targeting IA32.
3880  *   - The VM writing the snapshot and the VM reading the snapshot must be the
3881  *     same version, must be built in the same DEBUG/RELEASE/PRODUCT mode, must
3882  *     be targeting the same architecture, and must both be in checked mode or
3883  *     both in unchecked mode.
3884  *
3885  *  The buffers are scope allocated and are only valid until the next call to
3886  *  Dart_ExitScope.
3887  *
3888  * \return A valid handle if no error occurs during the operation.
3889  */
3890 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3891 Dart_CreateAppJITSnapshotAsBlobs(uint8_t** isolate_snapshot_data_buffer,
3892                                  intptr_t* isolate_snapshot_data_size,
3893                                  uint8_t** isolate_snapshot_instructions_buffer,
3894                                  intptr_t* isolate_snapshot_instructions_size);
3895 
3896 /**
3897  * Like Dart_CreateAppJITSnapshotAsBlobs, but also creates a new VM snapshot.
3898  */
3899 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3900 Dart_CreateCoreJITSnapshotAsBlobs(
3901     uint8_t** vm_snapshot_data_buffer,
3902     intptr_t* vm_snapshot_data_size,
3903     uint8_t** vm_snapshot_instructions_buffer,
3904     intptr_t* vm_snapshot_instructions_size,
3905     uint8_t** isolate_snapshot_data_buffer,
3906     intptr_t* isolate_snapshot_data_size,
3907     uint8_t** isolate_snapshot_instructions_buffer,
3908     intptr_t* isolate_snapshot_instructions_size);
3909 
3910 /**
3911  * Get obfuscation map for precompiled code.
3912  *
3913  * Obfuscation map is encoded as a JSON array of pairs (original name,
3914  * obfuscated name).
3915  *
3916  * \return Returns an error handler if the VM was built in a mode that does not
3917  * support obfuscation.
3918  */
3919 DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle
3920 Dart_GetObfuscationMap(uint8_t** buffer, intptr_t* buffer_length);
3921 
3922 /**
3923  *  Returns whether the VM only supports running from precompiled snapshots and
3924  *  not from any other kind of snapshot or from source (that is, the VM was
3925  *  compiled with DART_PRECOMPILED_RUNTIME).
3926  */
3927 DART_EXPORT bool Dart_IsPrecompiledRuntime();
3928 
3929 /**
3930  *  Print a native stack trace. Used for crash handling.
3931  *
3932  *  If context is NULL, prints the current stack trace. Otherwise, context
3933  *  should be a CONTEXT* (Windows) or ucontext_t* (POSIX) from a signal handler
3934  *  running on the current thread.
3935  */
3936 DART_EXPORT void Dart_DumpNativeStackTrace(void* context);
3937 
3938 /**
3939  *  Indicate that the process is about to abort, and the Dart VM should not
3940  *  attempt to cleanup resources.
3941  */
3942 DART_EXPORT void Dart_PrepareToAbort();
3943 
3944 #endif /* INCLUDE_DART_API_H_ */ /* NOLINT */
3945