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

Side by Side Diff: vm/dart_api_impl.cc

Issue 8772061: First step towards generation of application script snapshots (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: '' Created 9 years 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 #include "include/dart_api.h" 5 #include "include/dart_api.h"
6 6
7 #include "vm/bigint_operations.h" 7 #include "vm/bigint_operations.h"
8 #include "vm/class_finalizer.h" 8 #include "vm/class_finalizer.h"
9 #include "vm/compiler.h" 9 #include "vm/compiler.h"
10 #include "vm/dart.h" 10 #include "vm/dart.h"
(...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 static uint8_t* ApiAllocator(uint8_t* ptr, 602 static uint8_t* ApiAllocator(uint8_t* ptr,
603 intptr_t old_size, 603 intptr_t old_size,
604 intptr_t new_size) { 604 intptr_t new_size) {
605 uword new_ptr = Api::Reallocate(reinterpret_cast<uword>(ptr), 605 uword new_ptr = Api::Reallocate(reinterpret_cast<uword>(ptr),
606 old_size, 606 old_size,
607 new_size); 607 new_size);
608 return reinterpret_cast<uint8_t*>(new_ptr); 608 return reinterpret_cast<uint8_t*>(new_ptr);
609 } 609 }
610 610
611 611
612 DART_EXPORT Dart_Handle Dart_CreateSnapshot(uint8_t** snapshot_buffer, 612 DART_EXPORT Dart_Handle Dart_CreateSnapshot(uint8_t** buffer,
613 intptr_t* snapshot_size) { 613 intptr_t* size) {
614 Isolate* isolate = Isolate::Current(); 614 Isolate* isolate = Isolate::Current();
615 DARTSCOPE(isolate); 615 DARTSCOPE(isolate);
616 if (snapshot_buffer == NULL || snapshot_size == NULL) { 616 if (buffer == NULL || size == NULL) {
617 return Api::Error("Invalid input parameters to Dart_CreateSnapshot"); 617 return Api::Error("Invalid input parameters to %s", CURRENT_FUNC);
turnidge 2011/12/05 18:49:00 If you are wanting to make this error message cons
siva 2011/12/05 21:35:37 Done.
618 } 618 }
619 const char* msg = CheckIsolateState(isolate, 619 const char* msg = CheckIsolateState(isolate,
620 ClassFinalizer::kGeneratingSnapshot); 620 ClassFinalizer::kGeneratingSnapshot);
621 if (msg != NULL) { 621 if (msg != NULL) {
622 return Api::Error(msg); 622 return Api::Error(msg);
623 } 623 }
624 // Since this is only a snapshot the root library should not be set. 624 // Since this is only a snapshot the root library should not be set.
625 isolate->object_store()->set_root_library(Library::Handle()); 625 isolate->object_store()->set_root_library(Library::Handle());
626 SnapshotWriter writer(true, snapshot_buffer, ApiAllocator); 626 SnapshotWriter writer(Snapshot::kFull, buffer, ApiAllocator);
627 writer.WriteFullSnapshot(); 627 writer.WriteFullSnapshot();
628 *snapshot_size = writer.Size(); 628 *size = writer.Size();
629 return Api::Success();
630 }
631
632
633 DART_EXPORT Dart_Handle Dart_CreateScriptSnapshot(Dart_Handle library,
634 uint8_t** buffer,
635 intptr_t* size) {
636 Isolate* isolate = Isolate::Current();
637 DARTSCOPE(isolate);
638 if (buffer == NULL || size == NULL) {
639 return Api::Error("Invalid input parameters to %s", CURRENT_FUNC);
turnidge 2011/12/05 18:49:00 Ditto above.
siva 2011/12/05 21:35:37 Done.
640 }
641 const Library& root_lib = Api::UnwrapLibraryHandle(library);
642 if (root_lib.IsNull()) {
643 RETURN_TYPE_ERROR(library, Library);
644 }
645 const char* msg = CheckIsolateState(isolate);
646 if (msg != NULL) {
647 return Api::Error(msg);
648 }
649 ScriptSnapshotWriter writer(root_lib, buffer, ApiAllocator);
650 writer.WriteScriptSnapshot();
651 *size = writer.Size();
629 return Api::Success(); 652 return Api::Success();
turnidge 2011/12/05 18:49:00 Once this works, it would be great to have a unit
siva 2011/12/05 21:35:37 I had already added a unit test in snapshot_test.c
630 } 653 }
631 654
632 655
633 // --- Messages and Ports --- 656 // --- Messages and Ports ---
634 657
635 658
636 DART_EXPORT void Dart_SetMessageCallbacks( 659 DART_EXPORT void Dart_SetMessageCallbacks(
637 Dart_PostMessageCallback post_message_callback, 660 Dart_PostMessageCallback post_message_callback,
638 Dart_ClosePortCallback close_port_callback) { 661 Dart_ClosePortCallback close_port_callback) {
639 Isolate* isolate = Isolate::Current(); 662 Isolate* isolate = Isolate::Current();
640 CHECK_ISOLATE(isolate); 663 CHECK_ISOLATE(isolate);
641 ASSERT(post_message_callback != NULL); 664 ASSERT(post_message_callback != NULL);
642 ASSERT(close_port_callback != NULL); 665 ASSERT(close_port_callback != NULL);
643 isolate->set_post_message_callback(post_message_callback); 666 isolate->set_post_message_callback(post_message_callback);
644 isolate->set_close_port_callback(close_port_callback); 667 isolate->set_close_port_callback(close_port_callback);
645 } 668 }
646 669
647 670
648 static RawInstance* DeserializeMessage(void* data) { 671 static RawInstance* DeserializeMessage(void* data) {
649 // Create a snapshot object using the buffer. 672 // Create a snapshot object using the buffer.
650 const Snapshot* snapshot = Snapshot::SetupFromBuffer(data); 673 const Snapshot* snapshot = Snapshot::SetupFromBuffer(data);
651 ASSERT(snapshot->IsPartialSnapshot()); 674 ASSERT(snapshot->IsMessageSnapshot());
652 675
653 // Read object back from the snapshot. 676 // Read object back from the snapshot.
654 Isolate* isolate = Isolate::Current(); 677 Isolate* isolate = Isolate::Current();
655 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); 678 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store());
656 Instance& instance = Instance::Handle(); 679 Instance& instance = Instance::Handle();
657 instance ^= reader.ReadObject(); 680 instance ^= reader.ReadObject();
658 return instance.raw(); 681 return instance.raw();
659 } 682 }
660 683
661 684
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
737 return PortMap::PostMessage(port_id, kNoReplyPort, Api::CastMessage(buffer)); 760 return PortMap::PostMessage(port_id, kNoReplyPort, Api::CastMessage(buffer));
738 } 761 }
739 762
740 763
741 DART_EXPORT bool Dart_Post(Dart_Port port_id, Dart_Handle handle) { 764 DART_EXPORT bool Dart_Post(Dart_Port port_id, Dart_Handle handle) {
742 Isolate* isolate = Isolate::Current(); 765 Isolate* isolate = Isolate::Current();
743 CHECK_ISOLATE(isolate); 766 CHECK_ISOLATE(isolate);
744 DARTSCOPE_NOCHECKS(isolate); 767 DARTSCOPE_NOCHECKS(isolate);
745 const Object& object = Object::Handle(Api::UnwrapHandle(handle)); 768 const Object& object = Object::Handle(Api::UnwrapHandle(handle));
746 uint8_t* data = NULL; 769 uint8_t* data = NULL;
747 SnapshotWriter writer(false, &data, &allocator); 770 SnapshotWriter writer(Snapshot::kMessage, &data, &allocator);
748 writer.WriteObject(object.raw()); 771 writer.WriteObject(object.raw());
749 writer.FinalizeBuffer(); 772 writer.FinalizeBuffer();
750 return PortMap::PostMessage(port_id, kNoReplyPort, Api::CastMessage(data)); 773 return PortMap::PostMessage(port_id, kNoReplyPort, Api::CastMessage(data));
751 } 774 }
752 775
753 776
754 DART_EXPORT Dart_Handle Dart_NewSendPort(Dart_Port port_id) { 777 DART_EXPORT Dart_Handle Dart_NewSendPort(Dart_Port port_id) {
755 Isolate* isolate = Isolate::Current(); 778 Isolate* isolate = Isolate::Current();
756 DARTSCOPE(isolate); 779 DARTSCOPE(isolate);
757 const String& class_name = String::Handle(String::NewSymbol("SendPortImpl")); 780 const String& class_name = String::Handle(String::NewSymbol("SendPortImpl"));
(...skipping 1414 matching lines...) Expand 10 before | Expand all | Expand 10 after
2172 CompileSource(isolate, 2195 CompileSource(isolate,
2173 library, 2196 library,
2174 url_str, 2197 url_str,
2175 source_str, 2198 source_str,
2176 RawScript::kScript, 2199 RawScript::kScript,
2177 &result); 2200 &result);
2178 return result; 2201 return result;
2179 } 2202 }
2180 2203
2181 2204
2205 DART_EXPORT Dart_Handle Dart_LoadScriptFromSnapshot(const uint8_t* data) {
2206 Isolate* isolate = Isolate::Current();
2207 DARTSCOPE(isolate);
2208 if (data == NULL) {
2209 return Api::Error("Invalid input parameters to %s", CURRENT_FUNC);
turnidge 2011/12/05 18:49:00 Ditto above.
siva 2011/12/05 21:35:37 Done.
2210 }
2211 const Snapshot* snapshot = Snapshot::SetupFromBuffer(data);
2212 if (!snapshot->IsScriptSnapshot()) {
2213 return Api::Error("Expected a script type snapshot to %s", CURRENT_FUNC);
turnidge 2011/12/05 18:49:00 "%s expects parameter 'data' to be a script snapsh
siva 2011/12/05 21:35:37 Done.
2214 }
turnidge 2011/12/05 18:49:00 Should we check to see if a script has already bee
siva 2011/12/05 21:35:37 Good point. On 2011/12/05 18:49:00, turnidge wrot
2215 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store());
2216 const Object& tmp = Object::Handle(reader.ReadObject());
2217 if (!tmp.IsLibrary()) {
2218 return Api::Error("Unable to deserialize snapshot passed to %s correctly",
2219 CURRENT_FUNC);
turnidge 2011/12/05 18:49:00 "%s: Unable to deserialize snapshot correctly.", C
siva 2011/12/05 21:35:37 Done.
2220 }
2221 Library& lib = Library::Handle();
2222 lib ^= tmp.raw();
2223 isolate->object_store()->set_root_library(lib);
2224 return Api::NewLocalHandle(lib);
2225 }
2226
2227
2182 DEFINE_FLAG(bool, compile_all, false, "Eagerly compile all code."); 2228 DEFINE_FLAG(bool, compile_all, false, "Eagerly compile all code.");
2183 2229
2184 static void CompileAll(Isolate* isolate, Dart_Handle* result) { 2230 static void CompileAll(Isolate* isolate, Dart_Handle* result) {
2185 *result = Api::Success(); 2231 *result = Api::Success();
2186 if (FLAG_compile_all) { 2232 if (FLAG_compile_all) {
2187 ASSERT(isolate != NULL); 2233 ASSERT(isolate != NULL);
2188 LongJump* base = isolate->long_jump_base(); 2234 LongJump* base = isolate->long_jump_base();
2189 LongJump jump; 2235 LongJump jump;
2190 isolate->set_long_jump_base(&jump); 2236 isolate->set_long_jump_base(&jump);
2191 if (setjmp(*jump.Set()) == 0) { 2237 if (setjmp(*jump.Set()) == 0) {
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
2377 } 2423 }
2378 delete debug_region; 2424 delete debug_region;
2379 } else { 2425 } else {
2380 *buffer = NULL; 2426 *buffer = NULL;
2381 *buffer_size = 0; 2427 *buffer_size = 0;
2382 } 2428 }
2383 } 2429 }
2384 2430
2385 2431
2386 } // namespace dart 2432 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698