Flutter Engine
The Flutter Engine
loader.cc
Go to the documentation of this file.
1// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "bin/loader.h"
6
7#include "bin/builtin.h"
8#include "bin/dartutils.h"
9#include "bin/dfe.h"
10#include "bin/error_exit.h"
11#include "bin/file.h"
12#include "bin/gzip.h"
13#include "bin/lockers.h"
14#include "bin/snapshot_utils.h"
15#include "bin/utils.h"
18#include "platform/uri.h"
19#include "platform/utils.h"
20
21namespace dart {
22namespace bin {
23
24#if !defined(DART_PRECOMPILED_RUNTIME)
25extern DFE dfe;
26#endif
27
28Dart_Handle Loader::InitForSnapshot(const char* snapshot_uri,
29 IsolateData* isolate_data) {
30 ASSERT(isolate_data != nullptr);
31
32 return Loader::Init(isolate_data->packages_file(),
34}
35
36// Initialize package resolution state.
37Dart_Handle Loader::Init(const char* packages_file,
38 const char* working_directory,
39 const char* root_script_uri) {
40 const int kNumArgs = 3;
41 Dart_Handle dart_args[kNumArgs];
42 dart_args[0] = (packages_file == nullptr)
43 ? Dart_Null()
44 : Dart_NewStringFromCString(packages_file);
45 dart_args[1] = Dart_NewStringFromCString(working_directory);
46 dart_args[2] = (root_script_uri == nullptr)
47 ? Dart_Null()
48 : Dart_NewStringFromCString(root_script_uri);
50 DartUtils::NewString("_Init"), kNumArgs, dart_args);
51}
52
53#if !defined(DART_PRECOMPILED_RUNTIME)
54static void MallocFinalizer(void* isolate_callback_data, void* peer) {
55 free(peer);
56}
57static Dart_Handle WrapMallocedKernelBuffer(uint8_t* kernel_buffer,
58 intptr_t kernel_buffer_size) {
60 Dart_TypedData_kUint8, kernel_buffer, kernel_buffer_size, kernel_buffer,
61 kernel_buffer_size, MallocFinalizer);
62 if (Dart_IsError(result)) {
63 free(kernel_buffer);
64 }
65 return result;
66}
67#endif
68
70 Dart_Handle library,
71 Dart_Handle url) {
72 const char* url_string = nullptr;
73 Dart_Handle result = Dart_StringToCString(url, &url_string);
74 if (Dart_IsError(result)) {
75 return result;
76 }
77 if (tag == Dart_kCanonicalizeUrl) {
78 Dart_Handle library_url = Dart_LibraryUrl(library);
79 if (Dart_IsError(library_url)) {
80 return library_url;
81 }
82 const char* library_url_string = nullptr;
83 result = Dart_StringToCString(library_url, &library_url_string);
84 if (Dart_IsError(result)) {
85 return result;
86 }
87 bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string);
88 bool is_dart_library = DartUtils::IsDartSchemeURL(library_url_string);
89 if (is_dart_scheme_url || is_dart_library) {
90 return url;
91 }
92 const char* url_cstr;
93 result = Dart_StringToCString(url, &url_cstr);
94 if (Dart_IsError(result)) {
95 return result;
96 }
97 CStringUniquePtr resolved_uri = ResolveUri(url_cstr, library_url_string);
98 if (!resolved_uri) {
99 return DartUtils::NewError("%s: Unable to canonicalize uri '%s'.",
100 __FUNCTION__, url_cstr);
101 }
102 result = Dart_NewStringFromCString(resolved_uri.get());
103 return result;
104 }
105#if !defined(DART_PRECOMPILED_RUNTIME)
106 if (tag == Dart_kKernelTag) {
107 uint8_t* kernel_buffer = nullptr;
108 intptr_t kernel_buffer_size = 0;
109 if (!dfe.TryReadKernelFile(url_string, nullptr, &kernel_buffer,
110 &kernel_buffer_size)) {
111 return DartUtils::NewError("'%s' is not a kernel file", url_string);
112 }
113 return WrapMallocedKernelBuffer(kernel_buffer, kernel_buffer_size);
114 }
116 (tag == Dart_kImportTag)) {
117 // E.g., IsolateMirror.loadUri.
118 char* error = nullptr;
119 int exit_code = 0;
120 uint8_t* kernel_buffer = nullptr;
121 intptr_t kernel_buffer_size = -1;
122 dfe.CompileAndReadScript(url_string, &kernel_buffer, &kernel_buffer_size,
123 &error, &exit_code, nullptr,
124 /*for_snapshot=*/false, /*embed_sources=*/true);
125 if (exit_code == 0) {
126 return Dart_LoadLibrary(
127 WrapMallocedKernelBuffer(kernel_buffer, kernel_buffer_size));
128 } else if (exit_code == kCompilationErrorExitCode) {
130 free(error);
131 return result;
132 } else {
134 free(error);
135 return result;
136 }
137 }
138 return DartUtils::NewError("Invalid tag : %d '%s'", tag, url_string);
139#else // !defined(DART_PRECOMPILED_RUNTIME)
140 return DartUtils::NewError("Unimplemented tag : %d '%s'", tag, url_string);
141#endif // !defined(DART_PRECOMPILED_RUNTIME)
142}
143
144Dart_Handle Loader::DeferredLoadHandler(intptr_t loading_unit_id) {
145 // A synchronous implementation. An asynchronous implementation would be
146 // better, but the standalone embedder only implements AOT for testing.
147
148 auto isolate_group_data =
150 char* unit_url = Utils::SCreate(
151 "%s-%" Pd ".part.so", isolate_group_data->script_url, loading_unit_id);
152
153 AppSnapshot* loading_unit_snapshot = Snapshot::TryReadAppSnapshot(unit_url);
155 if (loading_unit_snapshot != nullptr) {
156 isolate_group_data->AddLoadingUnit(loading_unit_snapshot);
157 const uint8_t* isolate_snapshot_data = nullptr;
158 const uint8_t* isolate_snapshot_instructions = nullptr;
159 const uint8_t* ignore_vm_snapshot_data;
160 const uint8_t* ignore_vm_snapshot_instructions;
161 loading_unit_snapshot->SetBuffers(
162 &ignore_vm_snapshot_data, &ignore_vm_snapshot_instructions,
166 if (Dart_IsApiError(result)) {
167 result =
169 /*transient*/ false);
170 }
171 } else {
172 char* error_message = Utils::SCreate("Failed to load %s", unit_url);
173 result = Dart_DeferredLoadCompleteError(loading_unit_id, error_message,
174 /*transient*/ false);
175 free(error_message);
176 }
177
178 free(unit_url);
179 return result;
180}
181
183
184} // namespace bin
185} // namespace dart
static char * SCreate(const char *format,...) PRINTF_ATTRIBUTE(1
Definition: utils.cc:231
virtual void SetBuffers(const uint8_t **vm_data_buffer, const uint8_t **vm_instructions_buffer, const uint8_t **isolate_data_buffer, const uint8_t **isolate_instructions_buffer)=0
bool TryReadKernelFile(const char *script_uri, const AppSnapshot *app_snapshot, uint8_t **kernel_buffer, intptr_t *kernel_buffer_size, bool decode_uri=true, std::shared_ptr< uint8_t > *kernel_blob_ptr=nullptr)
Definition: dfe.cc:437
void CompileAndReadScript(const char *script_uri, uint8_t **kernel_buffer, intptr_t *kernel_buffer_size, char **error, int *exit_code, const char *package_config, bool for_snapshot, bool embed_sources)
Definition: dfe.cc:204
bool CanUseDartFrontend() const
Definition: dfe.cc:143
bool UseDartFrontend() const
Definition: dfe.h:42
static Dart_Handle LookupBuiltinLib()
Definition: dartutils.h:248
static const char * original_working_directory
Definition: dartutils.h:288
static Dart_Handle NewError(const char *format,...)
Definition: dartutils.cc:766
static Dart_Handle NewString(const char *str)
Definition: dartutils.h:214
static bool IsDartSchemeURL(const char *url_name)
Definition: dartutils.cc:215
const char * packages_file() const
Definition: isolate_data.h:149
static Dart_Handle InitForSnapshot(const char *snapshot_uri, IsolateData *isolate_data)
Definition: loader.cc:28
static Dart_Handle DeferredLoadHandler(intptr_t loading_unit_id)
Definition: loader.cc:144
static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, Dart_Handle library, Dart_Handle url)
Definition: loader.cc:69
static void InitOnce()
Definition: loader.cc:182
static AppSnapshot * TryReadAppSnapshot(const char *script_uri, bool force_load_elf_from_memory=false, bool decode_uri=true)
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
@ Dart_TypedData_kUint8
Definition: dart_api.h:2615
Dart_LibraryTag
Definition: dart_api.h:3419
@ Dart_kImportTag
Definition: dart_api.h:3421
@ Dart_kCanonicalizeUrl
Definition: dart_api.h:3420
@ Dart_kKernelTag
Definition: dart_api.h:3422
#define ASSERT(E)
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
constexpr int kCompilationErrorExitCode
Definition: error_exit.h:16
const uint8_t * isolate_snapshot_data
Definition: gen_snapshot.cc:69
DFE dfe
Definition: dfe.cc:59
static Dart_Handle WrapMallocedKernelBuffer(uint8_t *kernel_buffer, intptr_t kernel_buffer_size)
Definition: loader.cc:57
static void MallocFinalizer(void *isolate_callback_data, void *peer)
const uint8_t * isolate_snapshot_instructions
Definition: gen_snapshot.cc:70
Definition: dart_vm.cc:33
DART_EXPORT Dart_Handle Dart_Invoke(Dart_Handle target, Dart_Handle name, int number_of_arguments, Dart_Handle *arguments)
DART_EXPORT Dart_Handle Dart_NewExternalTypedDataWithFinalizer(Dart_TypedData_Type type, void *data, intptr_t length, void *peer, intptr_t external_allocation_size, Dart_HandleFinalizer callback)
DART_EXPORT const char * Dart_GetError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_DeferredLoadCompleteError(intptr_t loading_unit_id, const char *error_message, bool transient)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
CStringUniquePtr ResolveUri(const char *ref_uri, const char *base_uri)
Definition: uri.cc:432
DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle kernel_buffer)
DART_EXPORT void * Dart_CurrentIsolateGroupData()
DART_EXPORT Dart_Handle Dart_DeferredLoadComplete(intptr_t loading_unit_id, const uint8_t *snapshot_data, const uint8_t *snapshot_instructions)
DART_EXPORT Dart_Handle Dart_NewApiError(const char *error)
DART_EXPORT Dart_Handle Dart_NewCompilationError(const char *error)
DART_EXPORT bool Dart_IsApiError(Dart_Handle object)
DART_EXPORT Dart_Handle Dart_LibraryUrl(Dart_Handle library)
DART_EXPORT Dart_Handle Dart_Null()
DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object, const char **cstr)
DART_EXPORT Dart_Handle Dart_NewStringFromCString(const char *str)
#define Pd
Definition: globals.h:408