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 CreateIsolateAndSetup(Dart_IsolateError 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"); |
turnidge
2011/11/23 18:05:12
Should we put an UNREACHABLE here?
siva
2011/11/23 18:24:40
Done.
| |
192 Dart_EnterScope(); | 199 return false; |
193 | |
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 } | 200 } |
226 | 201 |
227 | 202 |
228 static void PrintUsage() { | 203 static void PrintUsage() { |
229 fprintf(stderr, | 204 fprintf(stderr, |
230 "dart [<vm-flags>] " | 205 "dart [<vm-flags>] " |
231 "[<dart-script-file>]\n"); | 206 "[<dart-script-file>]\n"); |
232 } | 207 } |
233 | 208 |
234 | 209 |
235 int main(int argc, char** argv) { | 210 int main(int argc, char** argv) { |
236 CommandLineOptions vm_options(argc); | 211 CommandLineOptions vm_options(argc); |
237 char* script_name; | |
238 | 212 |
239 // Initialize the URL mapping array. | 213 // Initialize the URL mapping array. |
240 CommandLineOptions url_mapping_array(argc); | 214 CommandLineOptions url_mapping_array(argc); |
241 url_mapping = &url_mapping_array; | 215 url_mapping = &url_mapping_array; |
242 | 216 |
243 // Parse command line arguments. | 217 // Parse command line arguments. |
244 if (ParseArguments(argc, | 218 if (ParseArguments(argc, |
245 argv, | 219 argv, |
246 &vm_options, | 220 &vm_options, |
247 &script_name) < 0) { | 221 &app_script_name) < 0) { |
248 PrintUsage(); | 222 PrintUsage(); |
249 return 255; | 223 return 255; |
250 } | 224 } |
251 | 225 |
252 if (snapshot_filename == NULL) { | 226 if (snapshot_filename == NULL) { |
253 fprintf(stderr, "No snapshot output file specified\n"); | 227 fprintf(stderr, "No snapshot output file specified\n"); |
254 return 255; | 228 return 255; |
255 } | 229 } |
256 | 230 |
257 // Initialize the Dart VM (TODO(asiva) - remove const_cast once | 231 // Initialize the Dart VM (TODO(asiva) - remove const_cast once |
258 // dart API is fixed to take a const char** in Dart_Initialize). | 232 // dart API is fixed to take a const char** in Dart_Initialize). |
233 // Note: We don't expect isolates to be created from dart code during | |
234 // snapshot generation. | |
turnidge
2011/11/23 18:05:12
Instead of having this comment you could consider
siva
2011/11/23 18:24:40
I renamed the callback to "DoNotCreateIsolate" but
turnidge
2011/11/23 19:03:33
About the NULL, I don't really have a strong opini
siva
2011/11/23 22:37:27
I have added a check in Dart_Initialize to avoid N
| |
259 Dart_Initialize(vm_options.count(), | 235 Dart_Initialize(vm_options.count(), |
260 vm_options.arguments(), | 236 vm_options.arguments(), |
261 SnapshotCreateCallback); | 237 CreateIsolateAndSetup); |
262 | 238 |
263 // Create an isolate. As a side effect, SnapshotCreateCallback | 239 Dart_Isolate isolate = Dart_CreateIsolate(NULL); |
264 // gets called, which loads the script (if one is specified), its libraries | |
265 // and writes out a snapshot. | |
266 Dart_Isolate isolate = Dart_CreateIsolate(NULL, script_name); | |
267 if (isolate == NULL) { | 240 if (isolate == NULL) { |
268 return 255; | 241 fprintf(stderr, "Creation/Bootstrap of Isolate failed"); |
242 exit(255); | |
269 } | 243 } |
270 | 244 |
245 Dart_Handle result; | |
246 Dart_Handle library; | |
247 Dart_EnterScope(); | |
248 | |
249 ASSERT(snapshot_filename != NULL); | |
250 // Load up the script before a snapshot is created. | |
251 if (app_script_name != NULL) { | |
252 // Load the specified script. | |
253 library = LoadSnapshotCreationScript(app_script_name); | |
254 } else { | |
255 // This is a generic dart snapshot which needs builtin library setup. | |
256 library = LoadGenericSnapshotCreationScript(); | |
257 } | |
258 if (Dart_IsError(library)) { | |
259 const char* err_msg = Dart_GetError(library); | |
260 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg); | |
261 Dart_ExitScope(); | |
262 Dart_ShutdownIsolate(); | |
263 exit(255); | |
264 } | |
265 ASSERT(Dart_IsLibrary(library)); | |
266 uint8_t* buffer = NULL; | |
267 intptr_t size = 0; | |
268 // First create the snapshot. | |
269 result = Dart_CreateSnapshot(&buffer, &size); | |
270 if (Dart_IsError(result)) { | |
271 const char* err_msg = Dart_GetError(result); | |
272 fprintf(stderr, "Error while creating snapshot: %s\n", err_msg); | |
273 Dart_ExitScope(); | |
274 Dart_ShutdownIsolate(); | |
275 exit(255); | |
276 } | |
277 // Now write the snapshot out to specified file and exit. | |
278 WriteSnapshotFile(buffer, size); | |
279 Dart_ExitScope(); | |
280 | |
271 // Shutdown the isolate. | 281 // Shutdown the isolate. |
272 Dart_ShutdownIsolate(); | 282 Dart_ShutdownIsolate(); |
273 return 0; | 283 return 0; |
274 } | 284 } |
OLD | NEW |