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

Side by Side Diff: runtime/vm/dart_api_impl.cc

Issue 8588040: Add a mid-sized integration test for the Dart Embedding Api which (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 static void SetupErrorResult(Dart_Handle* handle) { 78 static void SetupErrorResult(Dart_Handle* handle) {
79 // Make a copy of the error message as the original message string 79 // Make a copy of the error message as the original message string
80 // may get deallocated when we return back from the Dart API call. 80 // may get deallocated when we return back from the Dart API call.
81 const String& error = String::Handle( 81 const String& error = String::Handle(
82 Isolate::Current()->object_store()->sticky_error()); 82 Isolate::Current()->object_store()->sticky_error());
83 const Object& obj = Object::Handle(ApiError::New(error)); 83 const Object& obj = Object::Handle(ApiError::New(error));
84 *handle = Api::NewLocalHandle(obj); 84 *handle = Api::NewLocalHandle(obj);
85 } 85 }
86 86
87 87
88 // NOTE: Need to pass 'result' as a parameter here in order to avoid
89 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork'
90 // which shows up because of the use of setjmp.
91 static void InvokeStatic(Isolate* isolate,
92 const Function& function,
93 GrowableArray<const Object*>& args,
94 Dart_Handle* result) {
95 ASSERT(isolate != NULL);
96 LongJump* base = isolate->long_jump_base();
97 LongJump jump;
98 isolate->set_long_jump_base(&jump);
99 if (setjmp(*jump.Set()) == 0) {
100 const Array& kNoArgumentNames = Array::Handle();
101 const Instance& retval = Instance::Handle(
102 DartEntry::InvokeStatic(function, args, kNoArgumentNames));
103 if (retval.IsUnhandledException()) {
104 *result = Api::ErrorFromException(retval);
105 } else {
106 *result = Api::NewLocalHandle(retval);
107 }
108 } else {
109 SetupErrorResult(result);
110 }
111 isolate->set_long_jump_base(base);
112 }
113
114
115 // NOTE: Need to pass 'result' as a parameter here in order to avoid
116 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork'
117 // which shows up because of the use of setjmp.
118 static void InvokeDynamic(Isolate* isolate,
119 const Instance& receiver,
120 const Function& function,
121 GrowableArray<const Object*>& args,
122 Dart_Handle* result) {
123 ASSERT(isolate != NULL);
124 LongJump* base = isolate->long_jump_base();
125 LongJump jump;
126 isolate->set_long_jump_base(&jump);
127 if (setjmp(*jump.Set()) == 0) {
128 const Array& kNoArgumentNames = Array::Handle();
129 const Instance& retval = Instance::Handle(
130 DartEntry::InvokeDynamic(receiver, function, args, kNoArgumentNames));
131 if (retval.IsUnhandledException()) {
132 *result = Api::ErrorFromException(retval);
133 } else {
134 *result = Api::NewLocalHandle(retval);
135 }
136 } else {
137 SetupErrorResult(result);
138 }
139 isolate->set_long_jump_base(base);
140 }
141
142
88 Dart_Handle Api::NewLocalHandle(const Object& object) { 143 Dart_Handle Api::NewLocalHandle(const Object& object) {
89 Isolate* isolate = Isolate::Current(); 144 Isolate* isolate = Isolate::Current();
90 ASSERT(isolate != NULL); 145 ASSERT(isolate != NULL);
91 ApiState* state = isolate->api_state(); 146 ApiState* state = isolate->api_state();
92 ASSERT(state != NULL); 147 ASSERT(state != NULL);
93 ApiLocalScope* scope = state->top_scope(); 148 ApiLocalScope* scope = state->top_scope();
94 ASSERT(scope != NULL); 149 ASSERT(scope != NULL);
95 LocalHandles* local_handles = scope->local_handles(); 150 LocalHandles* local_handles = scope->local_handles();
96 ASSERT(local_handles != NULL); 151 ASSERT(local_handles != NULL);
97 LocalHandle* ref = local_handles->AllocateHandle(); 152 LocalHandle* ref = local_handles->AllocateHandle();
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 return Api::NewLocalHandle(stacktrace); 407 return Api::NewLocalHandle(stacktrace);
353 } else { 408 } else {
354 return Api::Error("This error is not an unhandled exception error."); 409 return Api::Error("This error is not an unhandled exception error.");
355 } 410 }
356 } else { 411 } else {
357 return Api::Error("Can only get stacktraces from error handles."); 412 return Api::Error("Can only get stacktraces from error handles.");
358 } 413 }
359 } 414 }
360 415
361 416
362 // TODO(turnidge): This clonse Api::Error. I need to use va_copy to 417 // TODO(turnidge): This clones Api::Error. I need to use va_copy to
363 // fix this but not sure if it available on all of our builds. 418 // fix this but not sure if it available on all of our builds.
364 DART_EXPORT Dart_Handle Dart_Error(const char* format, ...) { 419 DART_EXPORT Dart_Handle Dart_Error(const char* format, ...) {
365 DARTSCOPE(Isolate::Current()); 420 DARTSCOPE(Isolate::Current());
366 421
367 va_list args; 422 va_list args;
368 va_start(args, format); 423 va_start(args, format);
369 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 424 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
370 va_end(args); 425 va_end(args);
371 426
372 char* buffer = reinterpret_cast<char*>(zone.Allocate(len + 1)); 427 char* buffer = reinterpret_cast<char*>(zone.Allocate(len + 1));
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 649
595 DART_EXPORT Dart_Handle Dart_HandleMessage(Dart_Port dest_port, 650 DART_EXPORT Dart_Handle Dart_HandleMessage(Dart_Port dest_port,
596 Dart_Port reply_port, 651 Dart_Port reply_port,
597 Dart_Message dart_message) { 652 Dart_Message dart_message) {
598 DARTSCOPE(Isolate::Current()); 653 DARTSCOPE(Isolate::Current());
599 654
600 const Instance& msg = Instance::Handle(DeserializeMessage(dart_message)); 655 const Instance& msg = Instance::Handle(DeserializeMessage(dart_message));
601 const String& class_name = 656 const String& class_name =
602 String::Handle(String::NewSymbol("ReceivePortImpl")); 657 String::Handle(String::NewSymbol("ReceivePortImpl"));
603 const String& function_name = 658 const String& function_name =
604 String::Handle(String::NewSymbol("handleMessage_")); 659 String::Handle(String::NewSymbol("_handleMessage"));
605 const int kNumArguments = 3; 660 const int kNumArguments = 3;
606 const Array& kNoArgumentNames = Array::Handle(); 661 const Array& kNoArgumentNames = Array::Handle();
607 const Function& function = Function::Handle( 662 const Function& function = Function::Handle(
608 Resolver::ResolveStatic(Library::Handle(Library::CoreLibrary()), 663 Resolver::ResolveStatic(Library::Handle(Library::CoreLibrary()),
609 class_name, 664 class_name,
610 function_name, 665 function_name,
611 kNumArguments, 666 kNumArguments,
612 kNoArgumentNames, 667 kNoArgumentNames,
613 Resolver::kIsQualified)); 668 Resolver::kIsQualified));
614 GrowableArray<const Object*> arguments(kNumArguments); 669 GrowableArray<const Object*> arguments(kNumArguments);
615 arguments.Add(&Integer::Handle(Integer::New(dest_port))); 670 arguments.Add(&Integer::Handle(Integer::New(dest_port)));
616 arguments.Add(&Integer::Handle(Integer::New(reply_port))); 671 arguments.Add(&Integer::Handle(Integer::New(reply_port)));
617 arguments.Add(&msg); 672 arguments.Add(&msg);
673 // TODO(turnidge): This call should be wrapped in a longjmp
618 const Object& result = Object::Handle( 674 const Object& result = Object::Handle(
619 DartEntry::InvokeStatic(function, arguments, kNoArgumentNames)); 675 DartEntry::InvokeStatic(function, arguments, kNoArgumentNames));
620 if (result.IsUnhandledException()) { 676 if (result.IsUnhandledException()) {
621 return Api::ErrorFromException(result); 677 return Api::ErrorFromException(result);
622 } 678 }
623 ASSERT(result.IsNull()); 679 ASSERT(result.IsNull());
624 return Api::Success(); 680 return Api::Success();
625 } 681 }
626 682
627 683
628 DART_EXPORT Dart_Handle Dart_RunLoop() { 684 DART_EXPORT Dart_Handle Dart_RunLoop() {
629 Isolate* isolate = Isolate::Current(); 685 Isolate* isolate = Isolate::Current();
630 DARTSCOPE(isolate); 686 DARTSCOPE(isolate);
631 687
632 LongJump* base = isolate->long_jump_base(); 688 LongJump* base = isolate->long_jump_base();
633 LongJump jump; 689 LongJump jump;
634 Dart_Handle result; 690 Dart_Handle result;
635 isolate->set_long_jump_base(&jump); 691 isolate->set_long_jump_base(&jump);
636 if (setjmp(*jump.Set()) == 0) { 692 if (setjmp(*jump.Set()) == 0) {
637 isolate->StandardRunLoop(); 693 isolate->StandardRunLoop();
638 result = Api::Success(); 694 result = Api::Success();
639 } else { 695 } else {
640 SetupErrorResult(&result); 696 SetupErrorResult(&result);
641 } 697 }
642 isolate->set_long_jump_base(base); 698 isolate->set_long_jump_base(base);
643 return result; 699 return result;
644 } 700 }
645 701
646 702
703 DART_EXPORT bool Dart_HasLivePorts() {
704 Isolate* isolate = Isolate::Current();
705 ASSERT(isolate);
706 return isolate->live_ports() > 0;
707 }
708
709
647 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { 710 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) {
648 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); 711 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size);
649 return reinterpret_cast<uint8_t*>(new_ptr); 712 return reinterpret_cast<uint8_t*>(new_ptr);
650 } 713 }
651 714
652 715
653 DART_EXPORT bool Dart_PostIntArray(Dart_Port port, 716 DART_EXPORT bool Dart_PostIntArray(Dart_Port port,
654 intptr_t len, 717 intptr_t len,
655 intptr_t* data) { 718 intptr_t* data) {
656 uint8_t* buffer = NULL; 719 uint8_t* buffer = NULL;
(...skipping 10 matching lines...) Expand all
667 DARTSCOPE(Isolate::Current()); 730 DARTSCOPE(Isolate::Current());
668 const Object& object = Object::Handle(Api::UnwrapHandle(handle)); 731 const Object& object = Object::Handle(Api::UnwrapHandle(handle));
669 uint8_t* data = NULL; 732 uint8_t* data = NULL;
670 SnapshotWriter writer(false, &data, &allocator); 733 SnapshotWriter writer(false, &data, &allocator);
671 writer.WriteObject(object.raw()); 734 writer.WriteObject(object.raw());
672 writer.FinalizeBuffer(); 735 writer.FinalizeBuffer();
673 return PortMap::PostMessage(port, kNoReplyPort, data); 736 return PortMap::PostMessage(port, kNoReplyPort, data);
674 } 737 }
675 738
676 739
740 DART_EXPORT Dart_Handle Dart_NewSendPort(Dart_Port port) {
siva 2011/11/29 19:50:27 I am still not clear why this should not be Dart_G
741 Isolate* isolate = Isolate::Current();
742 DARTSCOPE(isolate);
743 const String& class_name = String::Handle(String::NewSymbol("SendPortImpl"));
744 const String& function_name = String::Handle(String::NewSymbol("_create"));
745 const int kNumArguments = 1;
746 const Array& kNoArgumentNames = Array::Handle();
747 // TODO(turnidge): Consider adding a helper function to make
748 // function resolution by class name and function name more concise.
749 const Function& function = Function::Handle(
750 Resolver::ResolveStatic(Library::Handle(Library::CoreLibrary()),
751 class_name,
752 function_name,
753 kNumArguments,
754 kNoArgumentNames,
755 Resolver::kIsQualified));
756 GrowableArray<const Object*> arguments(kNumArguments);
757 arguments.Add(&Integer::Handle(Integer::New(port)));
758 Dart_Handle result;
759 InvokeStatic(isolate, function, arguments, &result);
760 return result;
761 }
762
763
764 DART_EXPORT Dart_Handle Dart_NewReceivePort(Dart_Port port) {
siva 2011/11/29 19:50:27 Ditto comment about why not Dart_GetReceivePort(po
765 Isolate* isolate = Isolate::Current();
766 DARTSCOPE(isolate);
767 const String& class_name =
768 String::Handle(String::NewSymbol("ReceivePortImpl"));
769 const String& function_name = String::Handle(String::NewSymbol("_create"));
770 const int kNumArguments = 1;
771 const Array& kNoArgumentNames = Array::Handle();
772 const Function& function = Function::Handle(
773 Resolver::ResolveStatic(Library::Handle(Library::CoreLibrary()),
774 class_name,
775 function_name,
776 kNumArguments,
777 kNoArgumentNames,
778 Resolver::kIsQualified));
779 GrowableArray<const Object*> arguments(kNumArguments);
780 arguments.Add(&Integer::Handle(Integer::New(port)));
781 Dart_Handle result;
782 InvokeStatic(isolate, function, arguments, &result);
783 return result;
784 }
785
786
787 DART_EXPORT Dart_Port Dart_GetMainPort() {
788 Isolate* isolate = Isolate::Current();
789 ASSERT(isolate);
790 return isolate->main_port();
791 }
792
677 // --- Scopes ---- 793 // --- Scopes ----
678 794
679 795
680 DART_EXPORT void Dart_EnterScope() { 796 DART_EXPORT void Dart_EnterScope() {
681 Isolate* isolate = Isolate::Current(); 797 Isolate* isolate = Isolate::Current();
682 ASSERT(isolate != NULL); 798 ASSERT(isolate != NULL);
683 ApiState* state = isolate->api_state(); 799 ApiState* state = isolate->api_state();
684 ASSERT(state != NULL); 800 ASSERT(state != NULL);
685 ApiLocalScope* new_scope = new ApiLocalScope(state->top_scope(), 801 ApiLocalScope* new_scope = new ApiLocalScope(state->top_scope(),
686 reinterpret_cast<uword>(&state)); 802 reinterpret_cast<uword>(&state));
(...skipping 855 matching lines...) Expand 10 before | Expand all | Expand 10 after
1542 DARTSCOPE(Isolate::Current()); 1658 DARTSCOPE(Isolate::Current());
1543 const Closure& obj = Closure::CheckedHandle(Api::UnwrapHandle(object)); 1659 const Closure& obj = Closure::CheckedHandle(Api::UnwrapHandle(object));
1544 const Integer& smrck = Integer::Handle(Integer::New(value)); 1660 const Integer& smrck = Integer::Handle(Integer::New(value));
1545 obj.set_smrck(smrck); 1661 obj.set_smrck(smrck);
1546 } 1662 }
1547 1663
1548 1664
1549 // --- Methods and Fields --- 1665 // --- Methods and Fields ---
1550 1666
1551 1667
1552 // NOTE: Need to pass 'result' as a parameter here in order to avoid
1553 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork'
1554 // which shows up because of the use of setjmp.
1555 static void InvokeStatic(Isolate* isolate,
1556 const Function& function,
1557 GrowableArray<const Object*>& args,
1558 Dart_Handle* result) {
1559 ASSERT(isolate != NULL);
1560 LongJump* base = isolate->long_jump_base();
1561 LongJump jump;
1562 isolate->set_long_jump_base(&jump);
1563 if (setjmp(*jump.Set()) == 0) {
1564 const Array& kNoArgumentNames = Array::Handle();
1565 const Instance& retval = Instance::Handle(
1566 DartEntry::InvokeStatic(function, args, kNoArgumentNames));
1567 if (retval.IsUnhandledException()) {
1568 *result = Api::ErrorFromException(retval);
1569 } else {
1570 *result = Api::NewLocalHandle(retval);
1571 }
1572 } else {
1573 SetupErrorResult(result);
1574 }
1575 isolate->set_long_jump_base(base);
1576 }
1577
1578
1579 // NOTE: Need to pass 'result' as a parameter here in order to avoid
1580 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork'
1581 // which shows up because of the use of setjmp.
1582 static void InvokeDynamic(Isolate* isolate,
1583 const Instance& receiver,
1584 const Function& function,
1585 GrowableArray<const Object*>& args,
1586 Dart_Handle* result) {
1587 ASSERT(isolate != NULL);
1588 LongJump* base = isolate->long_jump_base();
1589 LongJump jump;
1590 isolate->set_long_jump_base(&jump);
1591 if (setjmp(*jump.Set()) == 0) {
1592 const Array& kNoArgumentNames = Array::Handle();
1593 const Instance& retval = Instance::Handle(
1594 DartEntry::InvokeDynamic(receiver, function, args, kNoArgumentNames));
1595 if (retval.IsUnhandledException()) {
1596 *result = Api::ErrorFromException(retval);
1597 } else {
1598 *result = Api::NewLocalHandle(retval);
1599 }
1600 } else {
1601 SetupErrorResult(result);
1602 }
1603 isolate->set_long_jump_base(base);
1604 }
1605
1606
1607 DART_EXPORT Dart_Handle Dart_InvokeStatic(Dart_Handle library_in, 1668 DART_EXPORT Dart_Handle Dart_InvokeStatic(Dart_Handle library_in,
1608 Dart_Handle class_name_in, 1669 Dart_Handle class_name_in,
1609 Dart_Handle function_name_in, 1670 Dart_Handle function_name_in,
1610 int number_of_arguments, 1671 int number_of_arguments,
1611 Dart_Handle* arguments) { 1672 Dart_Handle* arguments) {
1612 Isolate* isolate = Isolate::Current(); 1673 Isolate* isolate = Isolate::Current();
1613 DARTSCOPE(isolate); 1674 DARTSCOPE(isolate);
1614 // Finalize all classes. 1675 // Finalize all classes.
1615 const char* msg = CheckIsolateState(isolate); 1676 const char* msg = CheckIsolateState(isolate);
1616 if (msg != NULL) { 1677 if (msg != NULL) {
(...skipping 678 matching lines...) Expand 10 before | Expand all | Expand 10 after
2295 } 2356 }
2296 delete debug_region; 2357 delete debug_region;
2297 } else { 2358 } else {
2298 *buffer = NULL; 2359 *buffer = NULL;
2299 *buffer_size = 0; 2360 *buffer_size = 0;
2300 } 2361 }
2301 } 2362 }
2302 2363
2303 2364
2304 } // namespace dart 2365 } // namespace dart
OLDNEW
« runtime/lib/isolate.cc ('K') | « runtime/vm/dart.cc ('k') | runtime/vm/isolate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698