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

Side by Side Diff: bin/gen_snapshot.cc

Issue 8537023: Implement automatic loading of dart:core_native_fields library (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/file.h" 16 #include "bin/file.h"
16 #include "bin/globals.h" 17 #include "bin/globals.h"
17 #include "bin/process_script.h" 18 #include "bin/process_script.h"
18 19
19 // Global state that indicates whether a snapshot is to be created and 20 // Global state that indicates whether a snapshot is to be created and
20 // if so which file to write the snapshot into. 21 // if so which file to write the snapshot into.
21 static const char* snapshot_filename = NULL; 22 static const char* snapshot_filename = NULL;
22 23
23 // Global state that captures the URL mappings specified on the command line. 24 // Global state that captures the URL mappings specified on the command line.
24 static CommandLineOptions* url_mapping = NULL; 25 static CommandLineOptions* url_mapping = NULL;
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 Dart_Handle source = ReadStringFromFile(script_name); 214 Dart_Handle source = ReadStringFromFile(script_name);
214 if (Dart_IsError(source)) { 215 if (Dart_IsError(source)) {
215 return source; // source contains the error string. 216 return source; // source contains the error string.
216 } 217 }
217 Dart_Handle url = Dart_NewString(script_name); 218 Dart_Handle url = Dart_NewString(script_name);
218 219
219 return Dart_LoadScript(url, source, CreateSnapshotLibraryTagHandler); 220 return Dart_LoadScript(url, source, CreateSnapshotLibraryTagHandler);
220 } 221 }
221 222
222 223
224 static Dart_Handle BuiltinSnapshotLibraryTagHandler(Dart_LibraryTag tag,
225 Dart_Handle library,
226 Dart_Handle url) {
227 if (!Dart_IsLibrary(library)) {
228 return Dart_Error("not a library");
229 }
230 if (!Dart_IsString8(url)) {
231 return Dart_Error("url is not a string");
232 }
233 const char* url_chars = NULL;
234 Dart_Handle result = Dart_StringToCString(url, &url_chars);
235 if (Dart_IsError(result)) {
236 return Dart_Error("accessing url characters failed");
237 }
238 // We only support canonicalization of "dart:nativewrappers".
239 if (strcmp(url_chars, DartUtils::kCoreNativeWrappersLibURL) == 0) {
240 if (tag == kCanonicalizeUrl) {
241 return Dart_NewString(url_chars);
242 }
243 return Dart_Error("unexpected tag encountered %d", tag);
244 }
245 return Dart_Error("unsupported url encountered %s", url_chars);
246 }
247
248
249 static Dart_Handle LoadGenericSnapshotCreationScript() {
250 Dart_Handle source = Builtin_Source();
251 if (Dart_IsError(source)) {
252 return source; // source contains the error string.
253 }
254 Dart_Handle url = Dart_NewString(DartUtils::kBuiltinLibURL);
255 Dart_Handle lib = Dart_LoadScript(url,
256 source,
257 BuiltinSnapshotLibraryTagHandler);
258 if (!Dart_IsError(lib)) {
259 Builtin_SetupLibrary(lib);
260 }
261 return lib;
262 }
263
264
223 static void* SnapshotCreateCallback(void* data) { 265 static void* SnapshotCreateCallback(void* data) {
224 const char* script_name = reinterpret_cast<const char*>(data); 266 const char* script_name = reinterpret_cast<const char*>(data);
225 Dart_Handle result; 267 Dart_Handle result;
268 Dart_Handle library;
226 Dart_EnterScope(); 269 Dart_EnterScope();
227 270
228 ASSERT(snapshot_filename != NULL); 271 ASSERT(snapshot_filename != NULL);
229 272
230 // If a file is specified on the command line, load it up before a snapshot 273 // Load up the script before a snapshot is created.
231 // is created.
232 if (script_name != NULL) { 274 if (script_name != NULL) {
233 // Load the specified script. 275 // Load the specified script.
234 Dart_Handle library = LoadSnapshotCreationScript(script_name); 276 library = LoadSnapshotCreationScript(script_name);
235 if (Dart_IsError(library)) {
236 const char* err_msg = Dart_GetError(library);
237 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg);
238 Dart_ExitScope();
239 exit(255);
240 }
241
242 if (!Dart_IsLibrary(library)) {
243 fprintf(stderr,
244 "Expected a library when loading script: %s",
245 script_name);
246 Dart_ExitScope();
247 exit(255);
248 }
249 } else { 277 } else {
250 // Implicitly load builtin library. 278 // This is a generic dart snapshot which needs builtin library setup.
251 Builtin_LoadLibrary(); 279 library = LoadGenericSnapshotCreationScript();
252 // Setup the native resolver for built in library functions.
253 Builtin_SetNativeResolver();
254 } 280 }
255 281 if (Dart_IsError(library)) {
282 const char* err_msg = Dart_GetError(library);
283 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg);
284 Dart_ExitScope();
285 exit(255);
286 }
287 ASSERT(Dart_IsLibrary(library));
256 uint8_t* buffer = NULL; 288 uint8_t* buffer = NULL;
257 intptr_t size = 0; 289 intptr_t size = 0;
258 // First create the snapshot. 290 // First create the snapshot.
259 result = Dart_CreateSnapshot(&buffer, &size); 291 result = Dart_CreateSnapshot(&buffer, &size);
260 if (Dart_IsError(result)) { 292 if (Dart_IsError(result)) {
261 const char* err_msg = Dart_GetError(result); 293 const char* err_msg = Dart_GetError(result);
262 fprintf(stderr, "Error while creating snapshot: %s\n", err_msg); 294 fprintf(stderr, "Error while creating snapshot: %s\n", err_msg);
263 Dart_ExitScope(); 295 Dart_ExitScope();
264 exit(255); 296 exit(255);
265 } 297 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 // and writes out a snapshot. 342 // and writes out a snapshot.
311 Dart_Isolate isolate = Dart_CreateIsolate(NULL, script_name); 343 Dart_Isolate isolate = Dart_CreateIsolate(NULL, script_name);
312 if (isolate == NULL) { 344 if (isolate == NULL) {
313 return 255; 345 return 255;
314 } 346 }
315 347
316 // Shutdown the isolate. 348 // Shutdown the isolate.
317 Dart_ShutdownIsolate(); 349 Dart_ShutdownIsolate();
318 return 0; 350 return 0;
319 } 351 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698