| 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 contains 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 void PrintUsage() { |
| 189 const char* script_name = reinterpret_cast<const char*>(data); | 196 fprintf(stderr, |
| 197 "dart [<vm-flags>] " |
| 198 "[<dart-script-file>]\n"); |
| 199 } |
| 200 |
| 201 |
| 202 int main(int argc, char** argv) { |
| 203 CommandLineOptions vm_options(argc); |
| 204 |
| 205 // Initialize the URL mapping array. |
| 206 CommandLineOptions url_mapping_array(argc); |
| 207 url_mapping = &url_mapping_array; |
| 208 |
| 209 // Parse command line arguments. |
| 210 if (ParseArguments(argc, |
| 211 argv, |
| 212 &vm_options, |
| 213 &app_script_name) < 0) { |
| 214 PrintUsage(); |
| 215 return 255; |
| 216 } |
| 217 |
| 218 if (snapshot_filename == NULL) { |
| 219 fprintf(stderr, "No snapshot output file specified\n"); |
| 220 return 255; |
| 221 } |
| 222 |
| 223 // Initialize the Dart VM. |
| 224 // Note: We don't expect isolates to be created from dart code during |
| 225 // snapshot generation. |
| 226 Dart_Initialize(vm_options.count(), vm_options.arguments(), NULL); |
| 227 |
| 228 char* error; |
| 229 Dart_Isolate isolate = Dart_CreateIsolate(NULL, NULL, &error); |
| 230 if (isolate == NULL) { |
| 231 fprintf(stderr, "%s", error); |
| 232 free(error); |
| 233 exit(255); |
| 234 } |
| 235 |
| 190 Dart_Handle result; | 236 Dart_Handle result; |
| 191 Dart_Handle library; | 237 Dart_Handle library; |
| 192 Dart_EnterScope(); | 238 Dart_EnterScope(); |
| 193 | 239 |
| 194 ASSERT(snapshot_filename != NULL); | 240 ASSERT(snapshot_filename != NULL); |
| 195 | |
| 196 // Load up the script before a snapshot is created. | 241 // Load up the script before a snapshot is created. |
| 197 if (script_name != NULL) { | 242 if (app_script_name != NULL) { |
| 198 // Load the specified script. | 243 // Load the specified script. |
| 199 library = LoadSnapshotCreationScript(script_name); | 244 library = LoadSnapshotCreationScript(app_script_name); |
| 200 } else { | 245 } else { |
| 201 // This is a generic dart snapshot which needs builtin library setup. | 246 // This is a generic dart snapshot which needs builtin library setup. |
| 202 library = LoadGenericSnapshotCreationScript(); | 247 library = LoadGenericSnapshotCreationScript(); |
| 203 } | 248 } |
| 204 if (Dart_IsError(library)) { | 249 if (Dart_IsError(library)) { |
| 205 const char* err_msg = Dart_GetError(library); | 250 const char* err_msg = Dart_GetError(library); |
| 206 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg); | 251 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg); |
| 207 Dart_ExitScope(); | 252 Dart_ExitScope(); |
| 253 Dart_ShutdownIsolate(); |
| 208 exit(255); | 254 exit(255); |
| 209 } | 255 } |
| 210 ASSERT(Dart_IsLibrary(library)); | 256 ASSERT(Dart_IsLibrary(library)); |
| 211 uint8_t* buffer = NULL; | 257 uint8_t* buffer = NULL; |
| 212 intptr_t size = 0; | 258 intptr_t size = 0; |
| 213 // First create the snapshot. | 259 // First create the snapshot. |
| 214 result = Dart_CreateSnapshot(&buffer, &size); | 260 result = Dart_CreateSnapshot(&buffer, &size); |
| 215 if (Dart_IsError(result)) { | 261 if (Dart_IsError(result)) { |
| 216 const char* err_msg = Dart_GetError(result); | 262 const char* err_msg = Dart_GetError(result); |
| 217 fprintf(stderr, "Error while creating snapshot: %s\n", err_msg); | 263 fprintf(stderr, "Error while creating snapshot: %s\n", err_msg); |
| 218 Dart_ExitScope(); | 264 Dart_ExitScope(); |
| 265 Dart_ShutdownIsolate(); |
| 219 exit(255); | 266 exit(255); |
| 220 } | 267 } |
| 221 // Now write the snapshot out to specified file and exit. | 268 // Now write the snapshot out to specified file and exit. |
| 222 WriteSnapshotFile(buffer, size); | 269 WriteSnapshotFile(buffer, size); |
| 223 Dart_ExitScope(); | 270 Dart_ExitScope(); |
| 224 return data; | |
| 225 } | |
| 226 | |
| 227 | |
| 228 static void PrintUsage() { | |
| 229 fprintf(stderr, | |
| 230 "dart [<vm-flags>] " | |
| 231 "[<dart-script-file>]\n"); | |
| 232 } | |
| 233 | |
| 234 | |
| 235 int main(int argc, char** argv) { | |
| 236 CommandLineOptions vm_options(argc); | |
| 237 char* script_name; | |
| 238 | |
| 239 // Initialize the URL mapping array. | |
| 240 CommandLineOptions url_mapping_array(argc); | |
| 241 url_mapping = &url_mapping_array; | |
| 242 | |
| 243 // Parse command line arguments. | |
| 244 if (ParseArguments(argc, | |
| 245 argv, | |
| 246 &vm_options, | |
| 247 &script_name) < 0) { | |
| 248 PrintUsage(); | |
| 249 return 255; | |
| 250 } | |
| 251 | |
| 252 if (snapshot_filename == NULL) { | |
| 253 fprintf(stderr, "No snapshot output file specified\n"); | |
| 254 return 255; | |
| 255 } | |
| 256 | |
| 257 // Initialize the Dart VM (TODO(asiva) - remove const_cast once | |
| 258 // dart API is fixed to take a const char** in Dart_Initialize). | |
| 259 Dart_Initialize(vm_options.count(), | |
| 260 vm_options.arguments(), | |
| 261 SnapshotCreateCallback); | |
| 262 | |
| 263 // Create an isolate. As a side effect, SnapshotCreateCallback | |
| 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) { | |
| 268 return 255; | |
| 269 } | |
| 270 | 271 |
| 271 // Shutdown the isolate. | 272 // Shutdown the isolate. |
| 272 Dart_ShutdownIsolate(); | 273 Dart_ShutdownIsolate(); |
| 273 return 0; | 274 return 0; |
| 274 } | 275 } |
| OLD | NEW |