OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Generate a snapshot file after loading all the scripts specified on the | 5 // Generate a snapshot file after loading all the scripts specified on the |
6 // command line. | 6 // command line. |
7 | 7 |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 #include <string.h> | 9 #include <string.h> |
10 #include <stdio.h> | 10 #include <stdio.h> |
11 | 11 |
12 #include "include/dart_api.h" | 12 #include "include/dart_api.h" |
13 | 13 |
14 #include "bin/builtin.h" | 14 #include "bin/builtin.h" |
15 #include "bin/dartutils.h" | 15 #include "bin/dartutils.h" |
16 #include "bin/file.h" | 16 #include "bin/file.h" |
17 #include "bin/globals.h" | 17 #include "bin/globals.h" |
18 | 18 |
19 // Global state that indicates whether a snapshot is to be created and | 19 // Global state that indicates whether a snapshot is to be created and |
20 // if so which file to write the snapshot into. | 20 // if so which file to write the snapshot into. |
21 static const char* snapshot_filename = NULL; | 21 static const char* snapshot_filename = NULL; |
22 | 22 |
| 23 |
| 24 // Global state which containts a pointer to the script name for which |
| 25 // a snapshot needs to be created (NULL would result in the creation |
| 26 // of a generic snapshot that contains only the corelibs). |
| 27 static char* app_script_name = NULL; |
| 28 |
| 29 |
23 // Global state that captures the URL mappings specified on the command line. | 30 // Global state that captures the URL mappings specified on the command line. |
24 static CommandLineOptions* url_mapping = NULL; | 31 static CommandLineOptions* url_mapping = NULL; |
25 | 32 |
26 static bool IsValidFlag(const char* name, | 33 static bool IsValidFlag(const char* name, |
27 const char* prefix, | 34 const char* prefix, |
28 intptr_t prefix_length) { | 35 intptr_t prefix_length) { |
29 intptr_t name_length = strlen(name); | 36 intptr_t name_length = strlen(name); |
30 return ((name_length > prefix_length) && | 37 return ((name_length > prefix_length) && |
31 (strncmp(name, prefix, prefix_length) == 0)); | 38 (strncmp(name, prefix, prefix_length) == 0)); |
32 } | 39 } |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 if (tag == kCanonicalizeUrl) { | 172 if (tag == kCanonicalizeUrl) { |
166 return url; | 173 return url; |
167 } | 174 } |
168 return Dart_Error("unsupported url encountered %s", url_string); | 175 return Dart_Error("unsupported url encountered %s", url_string); |
169 } | 176 } |
170 return Dart_Error("unexpected tag encountered %d", tag); | 177 return Dart_Error("unexpected tag encountered %d", tag); |
171 } | 178 } |
172 | 179 |
173 | 180 |
174 static Dart_Handle LoadGenericSnapshotCreationScript() { | 181 static Dart_Handle LoadGenericSnapshotCreationScript() { |
175 Dart_Handle source = Builtin_Source(); | 182 Dart_Handle source = Builtin::Source(); |
176 if (Dart_IsError(source)) { | 183 if (Dart_IsError(source)) { |
177 return source; // source contains the error string. | 184 return source; // source contains the error string. |
178 } | 185 } |
179 Dart_Handle url = Dart_NewString(DartUtils::kBuiltinLibURL); | 186 Dart_Handle url = Dart_NewString(DartUtils::kBuiltinLibURL); |
180 Dart_Handle lib = Dart_LoadScript(url, source, BuiltinLibraryTagHandler); | 187 Dart_Handle lib = Dart_LoadScript(url, source, BuiltinLibraryTagHandler); |
181 if (!Dart_IsError(lib)) { | 188 if (!Dart_IsError(lib)) { |
182 Builtin_SetupLibrary(lib); | 189 Builtin::SetupLibrary(lib); |
183 } | 190 } |
184 return lib; | 191 return lib; |
185 } | 192 } |
186 | 193 |
187 | 194 |
188 static void* SnapshotCreateCallback(void* data) { | 195 static bool DoNotCreateIsolate(void* data, Dart_ErrorBuffer error) { |
189 const char* script_name = reinterpret_cast<const char*>(data); | 196 snprintf(const_cast<char*>(error.buffer), error.length, |
190 Dart_Handle result; | 197 "Isolates are not expected to be created from dart" |
191 Dart_Handle library; | 198 " while generating snapshots"); |
192 Dart_EnterScope(); | 199 UNREACHABLE(); |
193 | 200 return false; |
194 ASSERT(snapshot_filename != NULL); | |
195 | |
196 // Load up the script before a snapshot is created. | |
197 if (script_name != NULL) { | |
198 // Load the specified script. | |
199 library = LoadSnapshotCreationScript(script_name); | |
200 } else { | |
201 // This is a generic dart snapshot which needs builtin library setup. | |
202 library = LoadGenericSnapshotCreationScript(); | |
203 } | |
204 if (Dart_IsError(library)) { | |
205 const char* err_msg = Dart_GetError(library); | |
206 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg); | |
207 Dart_ExitScope(); | |
208 exit(255); | |
209 } | |
210 ASSERT(Dart_IsLibrary(library)); | |
211 uint8_t* buffer = NULL; | |
212 intptr_t size = 0; | |
213 // First create the snapshot. | |
214 result = Dart_CreateSnapshot(&buffer, &size); | |
215 if (Dart_IsError(result)) { | |
216 const char* err_msg = Dart_GetError(result); | |
217 fprintf(stderr, "Error while creating snapshot: %s\n", err_msg); | |
218 Dart_ExitScope(); | |
219 exit(255); | |
220 } | |
221 // Now write the snapshot out to specified file and exit. | |
222 WriteSnapshotFile(buffer, size); | |
223 Dart_ExitScope(); | |
224 return data; | |
225 } | 201 } |
226 | 202 |
227 | 203 |
228 static void PrintUsage() { | 204 static void PrintUsage() { |
229 fprintf(stderr, | 205 fprintf(stderr, |
230 "dart [<vm-flags>] " | 206 "dart [<vm-flags>] " |
231 "[<dart-script-file>]\n"); | 207 "[<dart-script-file>]\n"); |
232 } | 208 } |
233 | 209 |
234 | 210 |
235 int main(int argc, char** argv) { | 211 int main(int argc, char** argv) { |
236 CommandLineOptions vm_options(argc); | 212 CommandLineOptions vm_options(argc); |
237 char* script_name; | |
238 | 213 |
239 // Initialize the URL mapping array. | 214 // Initialize the URL mapping array. |
240 CommandLineOptions url_mapping_array(argc); | 215 CommandLineOptions url_mapping_array(argc); |
241 url_mapping = &url_mapping_array; | 216 url_mapping = &url_mapping_array; |
242 | 217 |
243 // Parse command line arguments. | 218 // Parse command line arguments. |
244 if (ParseArguments(argc, | 219 if (ParseArguments(argc, |
245 argv, | 220 argv, |
246 &vm_options, | 221 &vm_options, |
247 &script_name) < 0) { | 222 &app_script_name) < 0) { |
248 PrintUsage(); | 223 PrintUsage(); |
249 return 255; | 224 return 255; |
250 } | 225 } |
251 | 226 |
252 if (snapshot_filename == NULL) { | 227 if (snapshot_filename == NULL) { |
253 fprintf(stderr, "No snapshot output file specified\n"); | 228 fprintf(stderr, "No snapshot output file specified\n"); |
254 return 255; | 229 return 255; |
255 } | 230 } |
256 | 231 |
257 // Initialize the Dart VM (TODO(asiva) - remove const_cast once | 232 // Initialize the Dart VM (TODO(asiva) - remove const_cast once |
258 // dart API is fixed to take a const char** in Dart_Initialize). | 233 // dart API is fixed to take a const char** in Dart_Initialize). |
| 234 // Note: We don't expect isolates to be created from dart code during |
| 235 // snapshot generation. |
259 Dart_Initialize(vm_options.count(), | 236 Dart_Initialize(vm_options.count(), |
260 vm_options.arguments(), | 237 vm_options.arguments(), |
261 SnapshotCreateCallback); | 238 DoNotCreateIsolate); |
262 | 239 |
263 // Create an isolate. As a side effect, SnapshotCreateCallback | 240 Dart_ErrorBuffer error; |
264 // gets called, which loads the script (if one is specified), its libraries | 241 static const int kErrorMsgLength = 256; |
265 // and writes out a snapshot. | 242 error.buffer = reinterpret_cast<char*>(malloc(kErrorMsgLength)); |
266 Dart_Isolate isolate = Dart_CreateIsolate(NULL, script_name); | 243 error.length = kErrorMsgLength; |
| 244 Dart_Isolate isolate = Dart_CreateIsolate(NULL, NULL, error); |
267 if (isolate == NULL) { | 245 if (isolate == NULL) { |
268 return 255; | 246 fprintf(stderr, "%s", error.buffer); |
| 247 free(error.buffer); |
| 248 exit(255); |
269 } | 249 } |
| 250 free(error.buffer); |
| 251 |
| 252 Dart_Handle result; |
| 253 Dart_Handle library; |
| 254 Dart_EnterScope(); |
| 255 |
| 256 ASSERT(snapshot_filename != NULL); |
| 257 // Load up the script before a snapshot is created. |
| 258 if (app_script_name != NULL) { |
| 259 // Load the specified script. |
| 260 library = LoadSnapshotCreationScript(app_script_name); |
| 261 } else { |
| 262 // This is a generic dart snapshot which needs builtin library setup. |
| 263 library = LoadGenericSnapshotCreationScript(); |
| 264 } |
| 265 if (Dart_IsError(library)) { |
| 266 const char* err_msg = Dart_GetError(library); |
| 267 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg); |
| 268 Dart_ExitScope(); |
| 269 Dart_ShutdownIsolate(); |
| 270 exit(255); |
| 271 } |
| 272 ASSERT(Dart_IsLibrary(library)); |
| 273 uint8_t* buffer = NULL; |
| 274 intptr_t size = 0; |
| 275 // First create the snapshot. |
| 276 result = Dart_CreateSnapshot(&buffer, &size); |
| 277 if (Dart_IsError(result)) { |
| 278 const char* err_msg = Dart_GetError(result); |
| 279 fprintf(stderr, "Error while creating snapshot: %s\n", err_msg); |
| 280 Dart_ExitScope(); |
| 281 Dart_ShutdownIsolate(); |
| 282 exit(255); |
| 283 } |
| 284 // Now write the snapshot out to specified file and exit. |
| 285 WriteSnapshotFile(buffer, size); |
| 286 Dart_ExitScope(); |
270 | 287 |
271 // Shutdown the isolate. | 288 // Shutdown the isolate. |
272 Dart_ShutdownIsolate(); | 289 Dart_ShutdownIsolate(); |
273 return 0; | 290 return 0; |
274 } | 291 } |
OLD | NEW |