Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(477)

Side by Side Diff: bin/gen_snapshot.cc

Issue 8673002: - Refactor the isolate callback mechanism to also include creation of the (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: '' Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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_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");
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_Isolate isolate = Dart_CreateIsolate(NULL, 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) { 241 if (isolate == NULL) {
268 return 255; 242 fprintf(stderr, "Creation/Bootstrap of Isolate failed");
243 exit(255);
269 } 244 }
270 245
246 Dart_Handle result;
247 Dart_Handle library;
248 Dart_EnterScope();
249
250 ASSERT(snapshot_filename != NULL);
251 // Load up the script before a snapshot is created.
252 if (app_script_name != NULL) {
253 // Load the specified script.
254 library = LoadSnapshotCreationScript(app_script_name);
255 } else {
256 // This is a generic dart snapshot which needs builtin library setup.
257 library = LoadGenericSnapshotCreationScript();
258 }
259 if (Dart_IsError(library)) {
260 const char* err_msg = Dart_GetError(library);
261 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg);
262 Dart_ExitScope();
263 Dart_ShutdownIsolate();
264 exit(255);
265 }
266 ASSERT(Dart_IsLibrary(library));
267 uint8_t* buffer = NULL;
268 intptr_t size = 0;
269 // First create the snapshot.
270 result = Dart_CreateSnapshot(&buffer, &size);
271 if (Dart_IsError(result)) {
272 const char* err_msg = Dart_GetError(result);
273 fprintf(stderr, "Error while creating snapshot: %s\n", err_msg);
274 Dart_ExitScope();
275 Dart_ShutdownIsolate();
276 exit(255);
277 }
278 // Now write the snapshot out to specified file and exit.
279 WriteSnapshotFile(buffer, size);
280 Dart_ExitScope();
281
271 // Shutdown the isolate. 282 // Shutdown the isolate.
272 Dart_ShutdownIsolate(); 283 Dart_ShutdownIsolate();
273 return 0; 284 return 0;
274 } 285 }
OLDNEW
« bin/builtin.cc ('K') | « bin/builtin_nolib.cc ('k') | bin/main.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698