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 #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 Loading... | |
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 Loading... | |
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 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
588 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); | 643 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); |
589 Instance& instance = Instance::Handle(); | 644 Instance& instance = Instance::Handle(); |
590 instance ^= reader.ReadObject(); | 645 instance ^= reader.ReadObject(); |
591 return instance.raw(); | 646 return instance.raw(); |
592 } | 647 } |
593 | 648 |
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 Isolate* isolate = Isolate::Current(); |
654 DARTSCOPE(isolate); | |
siva
2011/11/24 00:52:31
The isolate variable is not used in this method is
turnidge
2011/11/29 01:01:31
Fixed.
| |
599 | 655 |
600 const Instance& msg = Instance::Handle(DeserializeMessage(dart_message)); | 656 const Instance& msg = Instance::Handle(DeserializeMessage(dart_message)); |
601 const String& class_name = | 657 const String& class_name = |
602 String::Handle(String::NewSymbol("ReceivePortImpl")); | 658 String::Handle(String::NewSymbol("ReceivePortImpl")); |
603 const String& function_name = | 659 const String& function_name = |
604 String::Handle(String::NewSymbol("handleMessage_")); | 660 String::Handle(String::NewSymbol("_handleMessage")); |
605 const int kNumArguments = 3; | 661 const int kNumArguments = 3; |
606 const Array& kNoArgumentNames = Array::Handle(); | 662 const Array& kNoArgumentNames = Array::Handle(); |
607 const Function& function = Function::Handle( | 663 const Function& function = Function::Handle( |
608 Resolver::ResolveStatic(Library::Handle(Library::CoreLibrary()), | 664 Resolver::ResolveStatic(Library::Handle(Library::CoreLibrary()), |
609 class_name, | 665 class_name, |
610 function_name, | 666 function_name, |
611 kNumArguments, | 667 kNumArguments, |
612 kNoArgumentNames, | 668 kNoArgumentNames, |
613 Resolver::kIsQualified)); | 669 Resolver::kIsQualified)); |
614 GrowableArray<const Object*> arguments(kNumArguments); | 670 GrowableArray<const Object*> arguments(kNumArguments); |
615 arguments.Add(&Integer::Handle(Integer::New(dest_port))); | 671 arguments.Add(&Integer::Handle(Integer::New(dest_port))); |
616 arguments.Add(&Integer::Handle(Integer::New(reply_port))); | 672 arguments.Add(&Integer::Handle(Integer::New(reply_port))); |
617 arguments.Add(&msg); | 673 arguments.Add(&msg); |
674 // TODO(turnidge): This call should be wrapped in a longjmp | |
618 const Object& result = Object::Handle( | 675 const Object& result = Object::Handle( |
619 DartEntry::InvokeStatic(function, arguments, kNoArgumentNames)); | 676 DartEntry::InvokeStatic(function, arguments, kNoArgumentNames)); |
620 if (result.IsUnhandledException()) { | 677 if (result.IsUnhandledException()) { |
621 return Api::ErrorFromException(result); | 678 return Api::ErrorFromException(result); |
622 } | 679 } |
623 ASSERT(result.IsNull()); | 680 ASSERT(result.IsNull()); |
624 return Api::Success(); | 681 return Api::Success(); |
625 } | 682 } |
626 | 683 |
627 | 684 |
628 DART_EXPORT Dart_Handle Dart_RunLoop() { | 685 DART_EXPORT Dart_Handle Dart_RunLoop() { |
629 Isolate* isolate = Isolate::Current(); | 686 Isolate* isolate = Isolate::Current(); |
630 DARTSCOPE(isolate); | 687 DARTSCOPE(isolate); |
631 | 688 |
632 LongJump* base = isolate->long_jump_base(); | 689 LongJump* base = isolate->long_jump_base(); |
633 LongJump jump; | 690 LongJump jump; |
634 Dart_Handle result; | 691 Dart_Handle result; |
635 isolate->set_long_jump_base(&jump); | 692 isolate->set_long_jump_base(&jump); |
636 if (setjmp(*jump.Set()) == 0) { | 693 if (setjmp(*jump.Set()) == 0) { |
637 isolate->StandardRunLoop(); | 694 isolate->StandardRunLoop(); |
638 result = Api::Success(); | 695 result = Api::Success(); |
639 } else { | 696 } else { |
640 SetupErrorResult(&result); | 697 SetupErrorResult(&result); |
641 } | 698 } |
642 isolate->set_long_jump_base(base); | 699 isolate->set_long_jump_base(base); |
643 return result; | 700 return result; |
644 } | 701 } |
645 | 702 |
646 | 703 |
704 DART_EXPORT bool Dart_HasLivePorts() { | |
705 Isolate* isolate = Isolate::Current(); | |
706 DARTSCOPE(isolate); | |
707 const String& class_name = | |
708 String::Handle(String::NewSymbol("ReceivePortImpl")); | |
709 const String& function_name = | |
710 String::Handle(String::NewSymbol("_getNumLivePorts")); | |
711 const int kNumArguments = 0; | |
712 const Array& kNoArgumentNames = Array::Handle(); | |
713 const Function& function = Function::Handle( | |
714 Resolver::ResolveStatic(Library::Handle(Library::CoreLibrary()), | |
715 class_name, | |
716 function_name, | |
717 kNumArguments, | |
718 kNoArgumentNames, | |
719 Resolver::kIsQualified)); | |
720 GrowableArray<const Object*> arguments(kNumArguments); | |
721 | |
722 Dart_EnterScope(); | |
siva
2011/11/24 00:52:31
Why is a Dart_EnterScope needed here? Normally we
turnidge
2011/11/29 01:01:31
Completely redid how this works.
| |
723 Dart_Handle result; | |
724 InvokeStatic(isolate, function, arguments, &result); | |
725 const Integer& count = Api::UnwrapIntegerHandle(result); | |
726 Dart_ExitScope(); | |
727 | |
728 return count.AsInt64Value() > 0; | |
729 } | |
730 | |
731 | |
647 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { | 732 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); | 733 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); |
649 return reinterpret_cast<uint8_t*>(new_ptr); | 734 return reinterpret_cast<uint8_t*>(new_ptr); |
650 } | 735 } |
651 | 736 |
652 | 737 |
653 DART_EXPORT bool Dart_PostIntArray(Dart_Port port, | 738 DART_EXPORT bool Dart_PostIntArray(Dart_Port port, |
654 intptr_t len, | 739 intptr_t len, |
655 intptr_t* data) { | 740 intptr_t* data) { |
656 uint8_t* buffer = NULL; | 741 uint8_t* buffer = NULL; |
(...skipping 10 matching lines...) Expand all Loading... | |
667 DARTSCOPE(Isolate::Current()); | 752 DARTSCOPE(Isolate::Current()); |
668 const Object& object = Object::Handle(Api::UnwrapHandle(handle)); | 753 const Object& object = Object::Handle(Api::UnwrapHandle(handle)); |
669 uint8_t* data = NULL; | 754 uint8_t* data = NULL; |
670 SnapshotWriter writer(false, &data, &allocator); | 755 SnapshotWriter writer(false, &data, &allocator); |
671 writer.WriteObject(object.raw()); | 756 writer.WriteObject(object.raw()); |
672 writer.FinalizeBuffer(); | 757 writer.FinalizeBuffer(); |
673 return PortMap::PostMessage(port, kNoReplyPort, data); | 758 return PortMap::PostMessage(port, kNoReplyPort, data); |
674 } | 759 } |
675 | 760 |
676 | 761 |
762 DART_EXPORT Dart_Handle Dart_NewSendPort(Dart_Port port) { | |
763 Isolate* isolate = Isolate::Current(); | |
764 DARTSCOPE(isolate); | |
765 const String& class_name = String::Handle(String::NewSymbol("SendPortImpl")); | |
766 const String& function_name = String::Handle(String::NewSymbol("_create")); | |
767 const int kNumArguments = 1; | |
768 const Array& kNoArgumentNames = Array::Handle(); | |
769 const Function& function = Function::Handle( | |
770 Resolver::ResolveStatic(Library::Handle(Library::CoreLibrary()), | |
771 class_name, | |
772 function_name, | |
773 kNumArguments, | |
774 kNoArgumentNames, | |
775 Resolver::kIsQualified)); | |
776 GrowableArray<const Object*> arguments(kNumArguments); | |
777 arguments.Add(&Integer::Handle(Integer::New(port))); | |
778 Dart_Handle result; | |
779 InvokeStatic(isolate, function, arguments, &result); | |
780 return result; | |
781 } | |
782 | |
783 | |
784 DART_EXPORT Dart_Handle Dart_NewReceivePort(Dart_Port port) { | |
785 Isolate* isolate = Isolate::Current(); | |
786 DARTSCOPE(isolate); | |
787 const String& class_name = | |
788 String::Handle(String::NewSymbol("ReceivePortImpl")); | |
789 const String& function_name = String::Handle(String::NewSymbol("_create")); | |
790 const int kNumArguments = 1; | |
791 const Array& kNoArgumentNames = Array::Handle(); | |
792 const Function& function = Function::Handle( | |
793 Resolver::ResolveStatic(Library::Handle(Library::CoreLibrary()), | |
794 class_name, | |
795 function_name, | |
796 kNumArguments, | |
797 kNoArgumentNames, | |
798 Resolver::kIsQualified)); | |
siva
2011/11/24 00:52:31
The function resolution code seems to be identical
turnidge
2011/11/29 01:01:31
Added a TODO. Is that okay?
On 2011/11/24 00:52:
| |
799 GrowableArray<const Object*> arguments(kNumArguments); | |
800 arguments.Add(&Integer::Handle(Integer::New(port))); | |
801 Dart_Handle result; | |
802 InvokeStatic(isolate, function, arguments, &result); | |
803 return result; | |
804 } | |
805 | |
806 | |
807 DART_EXPORT Dart_Port Dart_GetMainPort() { | |
808 Isolate* isolate = Isolate::Current(); | |
809 ASSERT(isolate); | |
810 return isolate->main_port(); | |
811 } | |
812 | |
677 // --- Scopes ---- | 813 // --- Scopes ---- |
678 | 814 |
679 | 815 |
680 DART_EXPORT void Dart_EnterScope() { | 816 DART_EXPORT void Dart_EnterScope() { |
681 Isolate* isolate = Isolate::Current(); | 817 Isolate* isolate = Isolate::Current(); |
682 ASSERT(isolate != NULL); | 818 ASSERT(isolate != NULL); |
683 ApiState* state = isolate->api_state(); | 819 ApiState* state = isolate->api_state(); |
684 ASSERT(state != NULL); | 820 ASSERT(state != NULL); |
685 ApiLocalScope* new_scope = new ApiLocalScope(state->top_scope(), | 821 ApiLocalScope* new_scope = new ApiLocalScope(state->top_scope(), |
686 reinterpret_cast<uword>(&state)); | 822 reinterpret_cast<uword>(&state)); |
(...skipping 855 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1542 DARTSCOPE(Isolate::Current()); | 1678 DARTSCOPE(Isolate::Current()); |
1543 const Closure& obj = Closure::CheckedHandle(Api::UnwrapHandle(object)); | 1679 const Closure& obj = Closure::CheckedHandle(Api::UnwrapHandle(object)); |
1544 const Integer& smrck = Integer::Handle(Integer::New(value)); | 1680 const Integer& smrck = Integer::Handle(Integer::New(value)); |
1545 obj.set_smrck(smrck); | 1681 obj.set_smrck(smrck); |
1546 } | 1682 } |
1547 | 1683 |
1548 | 1684 |
1549 // --- Methods and Fields --- | 1685 // --- Methods and Fields --- |
1550 | 1686 |
1551 | 1687 |
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, | 1688 DART_EXPORT Dart_Handle Dart_InvokeStatic(Dart_Handle library_in, |
1608 Dart_Handle class_name_in, | 1689 Dart_Handle class_name_in, |
1609 Dart_Handle function_name_in, | 1690 Dart_Handle function_name_in, |
1610 int number_of_arguments, | 1691 int number_of_arguments, |
1611 Dart_Handle* arguments) { | 1692 Dart_Handle* arguments) { |
1612 Isolate* isolate = Isolate::Current(); | 1693 Isolate* isolate = Isolate::Current(); |
1613 DARTSCOPE(isolate); | 1694 DARTSCOPE(isolate); |
1614 // Finalize all classes. | 1695 // Finalize all classes. |
1615 const char* msg = CheckIsolateState(isolate); | 1696 const char* msg = CheckIsolateState(isolate); |
1616 if (msg != NULL) { | 1697 if (msg != NULL) { |
(...skipping 678 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2295 } | 2376 } |
2296 delete debug_region; | 2377 delete debug_region; |
2297 } else { | 2378 } else { |
2298 *buffer = NULL; | 2379 *buffer = NULL; |
2299 *buffer_size = 0; | 2380 *buffer_size = 0; |
2300 } | 2381 } |
2301 } | 2382 } |
2302 | 2383 |
2303 | 2384 |
2304 } // namespace dart | 2385 } // namespace dart |
OLD | NEW |