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

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

Issue 1473403003: Move ApiLocalScope out of class ApiState into class Thread so that the API local handles and zone e… (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: self-code-review Created 5 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
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 #include "include/dart_mirrors_api.h" 6 #include "include/dart_mirrors_api.h"
7 #include "include/dart_native_api.h" 7 #include "include/dart_native_api.h"
8 8
9 #include "platform/assert.h" 9 #include "platform/assert.h"
10 #include "vm/class_finalizer.h" 10 #include "vm/class_finalizer.h"
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 static bool GetNativeStringArgument(NativeArguments* arguments, 167 static bool GetNativeStringArgument(NativeArguments* arguments,
168 int arg_index, 168 int arg_index,
169 Dart_Handle* str, 169 Dart_Handle* str,
170 void** peer) { 170 void** peer) {
171 ASSERT(peer != NULL); 171 ASSERT(peer != NULL);
172 if (Api::StringGetPeerHelper(arguments, arg_index, peer)) { 172 if (Api::StringGetPeerHelper(arguments, arg_index, peer)) {
173 *str = NULL; 173 *str = NULL;
174 return true; 174 return true;
175 } 175 }
176 Thread* thread = arguments->thread(); 176 Thread* thread = arguments->thread();
177 Isolate* isolate = thread->isolate(); 177 ASSERT(thread == Thread::Current());
178 ASSERT(isolate == Isolate::Current());
179 *peer = NULL; 178 *peer = NULL;
180 REUSABLE_OBJECT_HANDLESCOPE(thread); 179 REUSABLE_OBJECT_HANDLESCOPE(thread);
181 Object& obj = thread->ObjectHandle(); 180 Object& obj = thread->ObjectHandle();
182 obj = arguments->NativeArgAt(arg_index); 181 obj = arguments->NativeArgAt(arg_index);
183 if (RawObject::IsStringClassId(obj.GetClassId())) { 182 if (RawObject::IsStringClassId(obj.GetClassId())) {
184 ASSERT(isolate->api_state() && 183 ASSERT(thread->api_top_scope() != NULL);
185 isolate->api_state()->top_scope() != NULL); 184 *str = Api::NewHandle(thread, obj.raw());
186 *str = Api::NewHandle(isolate, obj.raw());
187 return true; 185 return true;
188 } 186 }
189 if (obj.IsNull()) { 187 if (obj.IsNull()) {
190 *str = Api::Null(); 188 *str = Api::Null();
191 return true; 189 return true;
192 } 190 }
193 return false; 191 return false;
194 } 192 }
195 193
196 194
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 if (function.IsNull()) { 338 if (function.IsNull()) {
341 return ApiError::New(String::Handle(String::New(""))); 339 return ApiError::New(String::Handle(String::New("")));
342 } 340 }
343 const Array& args = Array::Handle(Array::New(kNumArgs)); 341 const Array& args = Array::Handle(Array::New(kNumArgs));
344 args.SetAt(0, receiver); 342 args.SetAt(0, receiver);
345 args.SetAt(1, argument); 343 args.SetAt(1, argument);
346 return DartEntry::InvokeFunction(function, args); 344 return DartEntry::InvokeFunction(function, args);
347 } 345 }
348 346
349 347
350 Dart_Handle Api::InitNewHandle(Isolate* isolate, RawObject* raw) { 348 Dart_Handle Api::InitNewHandle(Thread* thread, RawObject* raw) {
351 LocalHandles* local_handles = Api::TopScope(isolate)->local_handles(); 349 LocalHandles* local_handles = Api::TopScope(thread)->local_handles();
352 ASSERT(local_handles != NULL); 350 ASSERT(local_handles != NULL);
353 LocalHandle* ref = local_handles->AllocateHandle(); 351 LocalHandle* ref = local_handles->AllocateHandle();
354 ref->set_raw(raw); 352 ref->set_raw(raw);
355 return ref->apiHandle(); 353 return ref->apiHandle();
356 } 354 }
357 355
358 356
359 Dart_Handle Api::NewHandle(Isolate* isolate, RawObject* raw) { 357 Dart_Handle Api::NewHandle(Thread* thread, RawObject* raw) {
360 if (raw == Object::null()) { 358 if (raw == Object::null()) {
361 return Null(); 359 return Null();
362 } 360 }
363 if (raw == Bool::True().raw()) { 361 if (raw == Bool::True().raw()) {
364 return True(); 362 return True();
365 } 363 }
366 if (raw == Bool::False().raw()) { 364 if (raw == Bool::False().raw()) {
367 return False(); 365 return False();
368 } 366 }
369 return InitNewHandle(isolate, raw); 367 return InitNewHandle(thread, raw);
370 } 368 }
371 369
372 370
373 RawObject* Api::UnwrapHandle(Dart_Handle object) { 371 RawObject* Api::UnwrapHandle(Dart_Handle object) {
374 #if defined(DEBUG) 372 #if defined(DEBUG)
375 ASSERT(Thread::Current()->IsMutatorThread()); 373 Thread* thread = Thread::Current();
376 Isolate* isolate = Isolate::Current(); 374 ASSERT(thread->IsMutatorThread());
375 Isolate* isolate = thread->isolate();
zra 2015/11/25 17:36:56 Just noting that isolate is only used for the ASSE
siva 2015/11/25 18:25:08 Made it ASSERT(thread->isolate() != NULL);
377 ASSERT(isolate != NULL); 376 ASSERT(isolate != NULL);
378 ApiState* state = isolate->api_state();
379 ASSERT(state != NULL);
380 ASSERT(!FLAG_verify_handles || 377 ASSERT(!FLAG_verify_handles ||
381 state->IsValidLocalHandle(object) || 378 thread->IsValidLocalHandle(object) ||
382 Dart::IsReadOnlyApiHandle(object)); 379 Dart::IsReadOnlyApiHandle(object));
383 ASSERT(FinalizablePersistentHandle::raw_offset() == 0 && 380 ASSERT(FinalizablePersistentHandle::raw_offset() == 0 &&
384 PersistentHandle::raw_offset() == 0 && 381 PersistentHandle::raw_offset() == 0 &&
385 LocalHandle::raw_offset() == 0); 382 LocalHandle::raw_offset() == 0);
386 #endif 383 #endif
387 return (reinterpret_cast<LocalHandle*>(object))->raw(); 384 return (reinterpret_cast<LocalHandle*>(object))->raw();
388 } 385 }
389 386
390 387
391 #define DEFINE_UNWRAP(type) \ 388 #define DEFINE_UNWRAP(type) \
(...skipping 24 matching lines...) Expand all
416 const ReusableObjectHandleScope& reuse, Dart_Handle dart_handle) { 413 const ReusableObjectHandleScope& reuse, Dart_Handle dart_handle) {
417 Object& ref = reuse.Handle(); 414 Object& ref = reuse.Handle();
418 ref = Api::UnwrapHandle(dart_handle); 415 ref = Api::UnwrapHandle(dart_handle);
419 if (ref.IsInstance()) { 416 if (ref.IsInstance()) {
420 return Instance::Cast(ref); 417 return Instance::Cast(ref);
421 } 418 }
422 return Object::null_instance(); 419 return Object::null_instance();
423 } 420 }
424 421
425 422
426 Dart_Handle Api::CheckAndFinalizePendingClasses(Isolate* isolate) { 423 Dart_Handle Api::CheckAndFinalizePendingClasses(Thread* thread) {
424 Isolate* isolate = thread->isolate();
427 if (!isolate->AllowClassFinalization()) { 425 if (!isolate->AllowClassFinalization()) {
428 // Class finalization is blocked for the isolate. Do nothing. 426 // Class finalization is blocked for the isolate. Do nothing.
429 return Api::Success(); 427 return Api::Success();
430 } 428 }
431 if (ClassFinalizer::ProcessPendingClasses()) { 429 if (ClassFinalizer::ProcessPendingClasses()) {
432 return Api::Success(); 430 return Api::Success();
433 } 431 }
434 ASSERT(isolate->object_store()->sticky_error() != Object::null()); 432 ASSERT(isolate->object_store()->sticky_error() != Object::null());
435 return Api::NewHandle(isolate, isolate->object_store()->sticky_error()); 433 return Api::NewHandle(thread, isolate->object_store()->sticky_error());
436 } 434 }
437 435
438 436
439 Dart_Isolate Api::CastIsolate(Isolate* isolate) { 437 Dart_Isolate Api::CastIsolate(Isolate* isolate) {
440 return reinterpret_cast<Dart_Isolate>(isolate); 438 return reinterpret_cast<Dart_Isolate>(isolate);
441 } 439 }
442 440
443 441
444 Dart_Handle Api::NewError(const char* format, ...) { 442 Dart_Handle Api::NewError(const char* format, ...) {
445 DARTSCOPE(Thread::Current()); 443 DARTSCOPE(Thread::Current());
446 CHECK_CALLBACK_STATE(T); 444 CHECK_CALLBACK_STATE(T);
447 445
448 va_list args; 446 va_list args;
449 va_start(args, format); 447 va_start(args, format);
450 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 448 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
451 va_end(args); 449 va_end(args);
452 450
453 char* buffer = Z->Alloc<char>(len + 1); 451 char* buffer = Z->Alloc<char>(len + 1);
454 va_list args2; 452 va_list args2;
455 va_start(args2, format); 453 va_start(args2, format);
456 OS::VSNPrint(buffer, (len + 1), format, args2); 454 OS::VSNPrint(buffer, (len + 1), format, args2);
457 va_end(args2); 455 va_end(args2);
458 456
459 const String& message = String::Handle(Z, String::New(buffer)); 457 const String& message = String::Handle(Z, String::New(buffer));
460 return Api::NewHandle(I, ApiError::New(message)); 458 return Api::NewHandle(T, ApiError::New(message));
461 } 459 }
462 460
463 461
464 void Api::SetupAcquiredError(Isolate* isolate) { 462 void Api::SetupAcquiredError(Isolate* isolate) {
465 ASSERT(isolate != NULL); 463 ASSERT(isolate != NULL);
466 ApiState* state = isolate->api_state(); 464 ApiState* state = isolate->api_state();
467 ASSERT(state != NULL); 465 ASSERT(state != NULL);
468 state->SetupAcquiredError(); 466 state->SetupAcquiredError();
469 } 467 }
470 468
471 469
472 Dart_Handle Api::AcquiredError(Isolate* isolate) { 470 Dart_Handle Api::AcquiredError(Isolate* isolate) {
473 ASSERT(isolate != NULL); 471 ASSERT(isolate != NULL);
474 ApiState* state = isolate->api_state(); 472 ApiState* state = isolate->api_state();
475 ASSERT(state != NULL); 473 ASSERT(state != NULL);
476 PersistentHandle* acquired_error_handle = state->AcquiredError(); 474 PersistentHandle* acquired_error_handle = state->AcquiredError();
477 return reinterpret_cast<Dart_Handle>(acquired_error_handle); 475 return reinterpret_cast<Dart_Handle>(acquired_error_handle);
478 } 476 }
479 477
480 478
481 ApiLocalScope* Api::TopScope(Isolate* isolate) { 479 ApiLocalScope* Api::TopScope(Thread* thread) {
482 ASSERT(isolate != NULL); 480 ASSERT(thread != NULL);
483 ApiState* state = isolate->api_state(); 481 ApiLocalScope* scope = thread->api_top_scope();
484 ASSERT(state != NULL);
485 ApiLocalScope* scope = state->top_scope();
486 ASSERT(scope != NULL); 482 ASSERT(scope != NULL);
487 return scope; 483 return scope;
488 } 484 }
489 485
490 486
491 void Api::InitOnce() { 487 void Api::InitOnce() {
492 ASSERT(api_native_key_ == OSThread::kUnsetThreadLocalKey); 488 ASSERT(api_native_key_ == OSThread::kUnsetThreadLocalKey);
493 api_native_key_ = OSThread::CreateThreadLocal(); 489 api_native_key_ = OSThread::CreateThreadLocal();
494 ASSERT(api_native_key_ != OSThread::kUnsetThreadLocalKey); 490 ASSERT(api_native_key_ != OSThread::kUnsetThreadLocalKey);
495 } 491 }
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
742 } 738 }
743 739
744 740
745 DART_EXPORT const char* Dart_GetError(Dart_Handle handle) { 741 DART_EXPORT const char* Dart_GetError(Dart_Handle handle) {
746 DARTSCOPE(Thread::Current()); 742 DARTSCOPE(Thread::Current());
747 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle)); 743 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle));
748 if (obj.IsError()) { 744 if (obj.IsError()) {
749 const Error& error = Error::Cast(obj); 745 const Error& error = Error::Cast(obj);
750 const char* str = error.ToErrorCString(); 746 const char* str = error.ToErrorCString();
751 intptr_t len = strlen(str) + 1; 747 intptr_t len = strlen(str) + 1;
752 char* str_copy = Api::TopScope(I)->zone()->Alloc<char>(len); 748 char* str_copy = Api::TopScope(T)->zone()->Alloc<char>(len);
753 strncpy(str_copy, str, len); 749 strncpy(str_copy, str, len);
754 // Strip a possible trailing '\n'. 750 // Strip a possible trailing '\n'.
755 if ((len > 1) && (str_copy[len - 2] == '\n')) { 751 if ((len > 1) && (str_copy[len - 2] == '\n')) {
756 str_copy[len - 2] = '\0'; 752 str_copy[len - 2] = '\0';
757 } 753 }
758 return str_copy; 754 return str_copy;
759 } else { 755 } else {
760 return ""; 756 return "";
761 } 757 }
762 } 758 }
763 759
764 760
765 DART_EXPORT bool Dart_ErrorHasException(Dart_Handle handle) { 761 DART_EXPORT bool Dart_ErrorHasException(Dart_Handle handle) {
766 DARTSCOPE(Thread::Current()); 762 DARTSCOPE(Thread::Current());
767 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle)); 763 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle));
768 return obj.IsUnhandledException(); 764 return obj.IsUnhandledException();
769 } 765 }
770 766
771 767
772 DART_EXPORT Dart_Handle Dart_ErrorGetException(Dart_Handle handle) { 768 DART_EXPORT Dart_Handle Dart_ErrorGetException(Dart_Handle handle) {
773 DARTSCOPE(Thread::Current()); 769 DARTSCOPE(Thread::Current());
774 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle)); 770 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle));
775 if (obj.IsUnhandledException()) { 771 if (obj.IsUnhandledException()) {
776 const UnhandledException& error = UnhandledException::Cast(obj); 772 const UnhandledException& error = UnhandledException::Cast(obj);
777 return Api::NewHandle(I, error.exception()); 773 return Api::NewHandle(T, error.exception());
778 } else if (obj.IsError()) { 774 } else if (obj.IsError()) {
779 return Api::NewError("This error is not an unhandled exception error."); 775 return Api::NewError("This error is not an unhandled exception error.");
780 } else { 776 } else {
781 return Api::NewError("Can only get exceptions from error handles."); 777 return Api::NewError("Can only get exceptions from error handles.");
782 } 778 }
783 } 779 }
784 780
785 781
786 DART_EXPORT Dart_Handle Dart_ErrorGetStacktrace(Dart_Handle handle) { 782 DART_EXPORT Dart_Handle Dart_ErrorGetStacktrace(Dart_Handle handle) {
787 DARTSCOPE(Thread::Current()); 783 DARTSCOPE(Thread::Current());
788 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle)); 784 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle));
789 if (obj.IsUnhandledException()) { 785 if (obj.IsUnhandledException()) {
790 const UnhandledException& error = UnhandledException::Cast(obj); 786 const UnhandledException& error = UnhandledException::Cast(obj);
791 return Api::NewHandle(I, error.stacktrace()); 787 return Api::NewHandle(T, error.stacktrace());
792 } else if (obj.IsError()) { 788 } else if (obj.IsError()) {
793 return Api::NewError("This error is not an unhandled exception error."); 789 return Api::NewError("This error is not an unhandled exception error.");
794 } else { 790 } else {
795 return Api::NewError("Can only get stacktraces from error handles."); 791 return Api::NewError("Can only get stacktraces from error handles.");
796 } 792 }
797 } 793 }
798 794
799 795
800 // TODO(turnidge): This clones Api::NewError. I need to use va_copy to 796 // TODO(turnidge): This clones Api::NewError. I need to use va_copy to
801 // fix this but not sure if it available on all of our builds. 797 // fix this but not sure if it available on all of our builds.
802 DART_EXPORT Dart_Handle Dart_NewApiError(const char* error) { 798 DART_EXPORT Dart_Handle Dart_NewApiError(const char* error) {
803 DARTSCOPE(Thread::Current()); 799 DARTSCOPE(Thread::Current());
804 CHECK_CALLBACK_STATE(T); 800 CHECK_CALLBACK_STATE(T);
805 801
806 const String& message = String::Handle(Z, String::New(error)); 802 const String& message = String::Handle(Z, String::New(error));
807 return Api::NewHandle(I, ApiError::New(message)); 803 return Api::NewHandle(T, ApiError::New(message));
808 } 804 }
809 805
810 806
811 DART_EXPORT Dart_Handle Dart_NewUnhandledExceptionError(Dart_Handle exception) { 807 DART_EXPORT Dart_Handle Dart_NewUnhandledExceptionError(Dart_Handle exception) {
812 DARTSCOPE(Thread::Current()); 808 DARTSCOPE(Thread::Current());
813 CHECK_CALLBACK_STATE(T); 809 CHECK_CALLBACK_STATE(T);
814 810
815 Instance& obj = Instance::Handle(Z); 811 Instance& obj = Instance::Handle(Z);
816 intptr_t class_id = Api::ClassId(exception); 812 intptr_t class_id = Api::ClassId(exception);
817 if ((class_id == kApiErrorCid) || (class_id == kLanguageErrorCid)) { 813 if ((class_id == kApiErrorCid) || (class_id == kLanguageErrorCid)) {
818 obj = String::New(::Dart_GetError(exception)); 814 obj = String::New(::Dart_GetError(exception));
819 } else { 815 } else {
820 obj = Api::UnwrapInstanceHandle(Z, exception).raw(); 816 obj = Api::UnwrapInstanceHandle(Z, exception).raw();
821 if (obj.IsNull()) { 817 if (obj.IsNull()) {
822 RETURN_TYPE_ERROR(Z, exception, Instance); 818 RETURN_TYPE_ERROR(Z, exception, Instance);
823 } 819 }
824 } 820 }
825 const Stacktrace& stacktrace = Stacktrace::Handle(Z); 821 const Stacktrace& stacktrace = Stacktrace::Handle(Z);
826 return Api::NewHandle(I, UnhandledException::New(obj, stacktrace)); 822 return Api::NewHandle(T, UnhandledException::New(obj, stacktrace));
827 } 823 }
828 824
829 825
830 DART_EXPORT Dart_Handle Dart_PropagateError(Dart_Handle handle) { 826 DART_EXPORT Dart_Handle Dart_PropagateError(Dart_Handle handle) {
831 Thread* thread = Thread::Current(); 827 Thread* thread = Thread::Current();
832 Isolate* isolate = thread->isolate();
833 { 828 {
834 const Object& obj = Object::Handle(thread->zone(), 829 const Object& obj = Object::Handle(thread->zone(),
835 Api::UnwrapHandle(handle)); 830 Api::UnwrapHandle(handle));
836 if (!obj.IsError()) { 831 if (!obj.IsError()) {
837 return Api::NewError( 832 return Api::NewError(
838 "%s expects argument 'handle' to be an error handle. " 833 "%s expects argument 'handle' to be an error handle. "
839 "Did you forget to check Dart_IsError first?", 834 "Did you forget to check Dart_IsError first?",
840 CURRENT_FUNC); 835 CURRENT_FUNC);
841 } 836 }
842 } 837 }
843 if (thread->top_exit_frame_info() == 0) { 838 if (thread->top_exit_frame_info() == 0) {
844 // There are no dart frames on the stack so it would be illegal to 839 // There are no dart frames on the stack so it would be illegal to
845 // propagate an error here. 840 // propagate an error here.
846 return Api::NewError("No Dart frames on stack, cannot propagate error."); 841 return Api::NewError("No Dart frames on stack, cannot propagate error.");
847 } 842 }
848 843
849 // Unwind all the API scopes till the exit frame before propagating. 844 // Unwind all the API scopes till the exit frame before propagating.
850 ApiState* state = isolate->api_state();
851 ASSERT(state != NULL);
852 const Error* error; 845 const Error* error;
853 { 846 {
854 // We need to preserve the error object across the destruction of zones 847 // We need to preserve the error object across the destruction of zones
855 // when the ApiScopes are unwound. By using NoSafepointScope, we can ensure 848 // when the ApiScopes are unwound. By using NoSafepointScope, we can ensure
856 // that GC won't touch the raw error object before creating a valid 849 // that GC won't touch the raw error object before creating a valid
857 // handle for it in the surviving zone. 850 // handle for it in the surviving zone.
858 NoSafepointScope no_safepoint; 851 NoSafepointScope no_safepoint;
859 RawError* raw_error = Api::UnwrapErrorHandle(thread->zone(), handle).raw(); 852 RawError* raw_error = Api::UnwrapErrorHandle(thread->zone(), handle).raw();
860 state->UnwindScopes(thread->top_exit_frame_info()); 853 thread->UnwindScopes(thread->top_exit_frame_info());
861 // Note that thread's zone is different here than at the beginning of this 854 // Note that thread's zone is different here than at the beginning of this
862 // function. 855 // function.
863 error = &Error::Handle(thread->zone(), raw_error); 856 error = &Error::Handle(thread->zone(), raw_error);
864 } 857 }
865 Exceptions::PropagateError(*error); 858 Exceptions::PropagateError(*error);
866 UNREACHABLE(); 859 UNREACHABLE();
867 return Api::NewError("Cannot reach here. Internal error."); 860 return Api::NewError("Cannot reach here. Internal error.");
868 } 861 }
869 862
870 863
871 DART_EXPORT void _Dart_ReportErrorHandle(const char* file, 864 DART_EXPORT void _Dart_ReportErrorHandle(const char* file,
872 int line, 865 int line,
873 const char* handle, 866 const char* handle,
874 const char* message) { 867 const char* message) {
875 fprintf(stderr, "%s:%d: error handle: '%s':\n '%s'\n", 868 fprintf(stderr, "%s:%d: error handle: '%s':\n '%s'\n",
876 file, line, handle, message); 869 file, line, handle, message);
877 OS::Abort(); 870 OS::Abort();
878 } 871 }
879 872
880 873
881 DART_EXPORT Dart_Handle Dart_ToString(Dart_Handle object) { 874 DART_EXPORT Dart_Handle Dart_ToString(Dart_Handle object) {
882 DARTSCOPE(Thread::Current()); 875 DARTSCOPE(Thread::Current());
883 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(object)); 876 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(object));
884 if (obj.IsString()) { 877 if (obj.IsString()) {
885 return Api::NewHandle(I, obj.raw()); 878 return Api::NewHandle(T, obj.raw());
886 } else if (obj.IsInstance()) { 879 } else if (obj.IsInstance()) {
887 CHECK_CALLBACK_STATE(T); 880 CHECK_CALLBACK_STATE(T);
888 const Instance& receiver = Instance::Cast(obj); 881 const Instance& receiver = Instance::Cast(obj);
889 return Api::NewHandle(I, DartLibraryCalls::ToString(receiver)); 882 return Api::NewHandle(T, DartLibraryCalls::ToString(receiver));
890 } else { 883 } else {
891 CHECK_CALLBACK_STATE(T); 884 CHECK_CALLBACK_STATE(T);
892 // This is a VM internal object. Call the C++ method of printing. 885 // This is a VM internal object. Call the C++ method of printing.
893 return Api::NewHandle(I, String::New(obj.ToCString())); 886 return Api::NewHandle(T, String::New(obj.ToCString()));
894 } 887 }
895 } 888 }
896 889
897 890
898 DART_EXPORT bool Dart_IdentityEquals(Dart_Handle obj1, Dart_Handle obj2) { 891 DART_EXPORT bool Dart_IdentityEquals(Dart_Handle obj1, Dart_Handle obj2) {
899 DARTSCOPE(Thread::Current()); 892 DARTSCOPE(Thread::Current());
900 { 893 {
901 NoSafepointScope no_safepoint_scope; 894 NoSafepointScope no_safepoint_scope;
902 if (Api::UnwrapHandle(obj1) == Api::UnwrapHandle(obj2)) { 895 if (Api::UnwrapHandle(obj1) == Api::UnwrapHandle(obj2)) {
903 return true; 896 return true;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
949 if (bigint.FitsIntoUint64()) { 942 if (bigint.FitsIntoUint64()) {
950 return bigint.AsUint64Value(); 943 return bigint.AsUint64Value();
951 } 944 }
952 } 945 }
953 return 0; 946 return 0;
954 } 947 }
955 948
956 949
957 DART_EXPORT Dart_Handle Dart_HandleFromPersistent( 950 DART_EXPORT Dart_Handle Dart_HandleFromPersistent(
958 Dart_PersistentHandle object) { 951 Dart_PersistentHandle object) {
959 Isolate* isolate = Isolate::Current(); 952 Thread* thread = Thread::Current();
953 Isolate* isolate = thread->isolate();
960 CHECK_ISOLATE(isolate); 954 CHECK_ISOLATE(isolate);
961 ApiState* state = isolate->api_state(); 955 ApiState* state = isolate->api_state();
962 ASSERT(state != NULL); 956 ASSERT(state != NULL);
963 PersistentHandle* ref = PersistentHandle::Cast(object); 957 PersistentHandle* ref = PersistentHandle::Cast(object);
964 return Api::NewHandle(isolate, ref->raw()); 958 return Api::NewHandle(thread, ref->raw());
965 } 959 }
966 960
967 961
968 DART_EXPORT Dart_Handle Dart_HandleFromWeakPersistent( 962 DART_EXPORT Dart_Handle Dart_HandleFromWeakPersistent(
969 Dart_WeakPersistentHandle object) { 963 Dart_WeakPersistentHandle object) {
970 Isolate* isolate = Isolate::Current(); 964 Thread* thread = Thread::Current();
965 Isolate* isolate = thread->isolate();
971 CHECK_ISOLATE(isolate); 966 CHECK_ISOLATE(isolate);
972 ApiState* state = isolate->api_state(); 967 ApiState* state = isolate->api_state();
973 ASSERT(state != NULL); 968 ASSERT(state != NULL);
974 FinalizablePersistentHandle* weak_ref = 969 FinalizablePersistentHandle* weak_ref =
975 FinalizablePersistentHandle::Cast(object); 970 FinalizablePersistentHandle::Cast(object);
976 return Api::NewHandle(isolate, weak_ref->raw()); 971 return Api::NewHandle(thread, weak_ref->raw());
977 } 972 }
978 973
979 974
980 DART_EXPORT Dart_PersistentHandle Dart_NewPersistentHandle(Dart_Handle object) { 975 DART_EXPORT Dart_PersistentHandle Dart_NewPersistentHandle(Dart_Handle object) {
981 DARTSCOPE(Thread::Current()); 976 DARTSCOPE(Thread::Current());
977 Isolate* I = T->isolate();
982 ApiState* state = I->api_state(); 978 ApiState* state = I->api_state();
983 ASSERT(state != NULL); 979 ASSERT(state != NULL);
984 const Object& old_ref = Object::Handle(Z, Api::UnwrapHandle(object)); 980 const Object& old_ref = Object::Handle(Z, Api::UnwrapHandle(object));
985 PersistentHandle* new_ref = state->persistent_handles().AllocateHandle(); 981 PersistentHandle* new_ref = state->persistent_handles().AllocateHandle();
986 new_ref->set_raw(old_ref); 982 new_ref->set_raw(old_ref);
987 return new_ref->apiHandle(); 983 return new_ref->apiHandle();
988 } 984 }
989 985
990 986
991 DART_EXPORT void Dart_SetPersistentHandle(Dart_PersistentHandle obj1, 987 DART_EXPORT void Dart_SetPersistentHandle(Dart_PersistentHandle obj1,
992 Dart_Handle obj2) { 988 Dart_Handle obj2) {
993 DARTSCOPE(Thread::Current()); 989 DARTSCOPE(Thread::Current());
990 Isolate* I = T->isolate();
994 ApiState* state = I->api_state(); 991 ApiState* state = I->api_state();
995 ASSERT(state != NULL); 992 ASSERT(state != NULL);
996 ASSERT(state->IsValidPersistentHandle(obj1)); 993 ASSERT(state->IsValidPersistentHandle(obj1));
997 const Object& obj2_ref = Object::Handle(Z, Api::UnwrapHandle(obj2)); 994 const Object& obj2_ref = Object::Handle(Z, Api::UnwrapHandle(obj2));
998 PersistentHandle* obj1_ref = PersistentHandle::Cast(obj1); 995 PersistentHandle* obj1_ref = PersistentHandle::Cast(obj1);
999 obj1_ref->set_raw(obj2_ref); 996 obj1_ref->set_raw(obj2_ref);
1000 } 997 }
1001 998
1002 999
1003 static Dart_WeakPersistentHandle AllocateFinalizableHandle( 1000 static Dart_WeakPersistentHandle AllocateFinalizableHandle(
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
1281 FATAL1("%s expects argument 'isolate' to be non-null.", CURRENT_FUNC); 1278 FATAL1("%s expects argument 'isolate' to be non-null.", CURRENT_FUNC);
1282 } 1279 }
1283 // TODO(16615): Validate isolate parameter. 1280 // TODO(16615): Validate isolate parameter.
1284 Isolate* iso = reinterpret_cast<Isolate*>(isolate); 1281 Isolate* iso = reinterpret_cast<Isolate*>(isolate);
1285 return iso->init_callback_data(); 1282 return iso->init_callback_data();
1286 } 1283 }
1287 1284
1288 1285
1289 DART_EXPORT Dart_Handle Dart_DebugName() { 1286 DART_EXPORT Dart_Handle Dart_DebugName() {
1290 DARTSCOPE(Thread::Current()); 1287 DARTSCOPE(Thread::Current());
1291 return Api::NewHandle(I, String::New(I->name())); 1288 Isolate* I = T->isolate();
1289 return Api::NewHandle(T, String::New(I->name()));
1292 } 1290 }
1293 1291
1294 1292
1295 1293
1296 DART_EXPORT void Dart_EnterIsolate(Dart_Isolate isolate) { 1294 DART_EXPORT void Dart_EnterIsolate(Dart_Isolate isolate) {
1297 CHECK_NO_ISOLATE(Isolate::Current()); 1295 CHECK_NO_ISOLATE(Isolate::Current());
1298 // TODO(16615): Validate isolate parameter. 1296 // TODO(16615): Validate isolate parameter.
1299 Isolate* iso = reinterpret_cast<Isolate*>(isolate); 1297 Isolate* iso = reinterpret_cast<Isolate*>(isolate);
1300 if (iso->HasMutatorThread()) { 1298 if (iso->HasMutatorThread()) {
1301 FATAL("Multiple mutators within one isolate is not supported."); 1299 FATAL("Multiple mutators within one isolate is not supported.");
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1341 "%s expects that the value is set to true only.", CURRENT_FUNC); 1339 "%s expects that the value is set to true only.", CURRENT_FUNC);
1342 } 1340 }
1343 Isolate::Current()->set_strict_compilation(); 1341 Isolate::Current()->set_strict_compilation();
1344 return Api::Null(); 1342 return Api::Null();
1345 } 1343 }
1346 1344
1347 1345
1348 static uint8_t* ApiReallocate(uint8_t* ptr, 1346 static uint8_t* ApiReallocate(uint8_t* ptr,
1349 intptr_t old_size, 1347 intptr_t old_size,
1350 intptr_t new_size) { 1348 intptr_t new_size) {
1351 return Api::TopScope(Isolate::Current())->zone()->Realloc<uint8_t>( 1349 return Api::TopScope(Thread::Current())->zone()->Realloc<uint8_t>(
1352 ptr, old_size, new_size); 1350 ptr, old_size, new_size);
1353 } 1351 }
1354 1352
1355 1353
1356 DART_EXPORT Dart_Handle Dart_CreateSnapshot( 1354 DART_EXPORT Dart_Handle Dart_CreateSnapshot(
1357 uint8_t** vm_isolate_snapshot_buffer, 1355 uint8_t** vm_isolate_snapshot_buffer,
1358 intptr_t* vm_isolate_snapshot_size, 1356 intptr_t* vm_isolate_snapshot_size,
1359 uint8_t** isolate_snapshot_buffer, 1357 uint8_t** isolate_snapshot_buffer,
1360 intptr_t* isolate_snapshot_size) { 1358 intptr_t* isolate_snapshot_size) {
1361 ASSERT(FLAG_load_deferred_eagerly); 1359 ASSERT(FLAG_load_deferred_eagerly);
1362 DARTSCOPE(Thread::Current()); 1360 DARTSCOPE(Thread::Current());
1361 Isolate* I = T->isolate();
1363 if (vm_isolate_snapshot_buffer != NULL && 1362 if (vm_isolate_snapshot_buffer != NULL &&
1364 vm_isolate_snapshot_size == NULL) { 1363 vm_isolate_snapshot_size == NULL) {
1365 RETURN_NULL_ERROR(vm_isolate_snapshot_size); 1364 RETURN_NULL_ERROR(vm_isolate_snapshot_size);
1366 } 1365 }
1367 if (isolate_snapshot_buffer == NULL) { 1366 if (isolate_snapshot_buffer == NULL) {
1368 RETURN_NULL_ERROR(isolate_snapshot_buffer); 1367 RETURN_NULL_ERROR(isolate_snapshot_buffer);
1369 } 1368 }
1370 if (isolate_snapshot_size == NULL) { 1369 if (isolate_snapshot_size == NULL) {
1371 RETURN_NULL_ERROR(isolate_snapshot_size); 1370 RETURN_NULL_ERROR(isolate_snapshot_size);
1372 } 1371 }
1373 // Finalize all classes if needed. 1372 // Finalize all classes if needed.
1374 Dart_Handle state = Api::CheckAndFinalizePendingClasses(I); 1373 Dart_Handle state = Api::CheckAndFinalizePendingClasses(T);
1375 if (::Dart_IsError(state)) { 1374 if (::Dart_IsError(state)) {
1376 return state; 1375 return state;
1377 } 1376 }
1378 I->heap()->CollectAllGarbage(); 1377 I->heap()->CollectAllGarbage();
1379 #if defined(DEBUG) 1378 #if defined(DEBUG)
1380 FunctionVisitor check_canonical(T); 1379 FunctionVisitor check_canonical(T);
1381 I->heap()->IterateObjects(&check_canonical); 1380 I->heap()->IterateObjects(&check_canonical);
1382 #endif // #if defined(DEBUG). 1381 #endif // #if defined(DEBUG).
1383 1382
1384 // Since this is only a snapshot the root library should not be set. 1383 // Since this is only a snapshot the root library should not be set.
1385 I->object_store()->set_root_library(Library::Handle(Z)); 1384 I->object_store()->set_root_library(Library::Handle(Z));
1386 FullSnapshotWriter writer(vm_isolate_snapshot_buffer, 1385 FullSnapshotWriter writer(vm_isolate_snapshot_buffer,
1387 isolate_snapshot_buffer, 1386 isolate_snapshot_buffer,
1388 NULL, /* instructions_snapshot_buffer */ 1387 NULL, /* instructions_snapshot_buffer */
1389 ApiReallocate, 1388 ApiReallocate,
1390 false, /* snapshot_code */ 1389 false, /* snapshot_code */
1391 true /* vm_isolate_is_symbolic */); 1390 true /* vm_isolate_is_symbolic */);
1392 writer.WriteFullSnapshot(); 1391 writer.WriteFullSnapshot();
1393 *vm_isolate_snapshot_size = writer.VmIsolateSnapshotSize(); 1392 *vm_isolate_snapshot_size = writer.VmIsolateSnapshotSize();
1394 *isolate_snapshot_size = writer.IsolateSnapshotSize(); 1393 *isolate_snapshot_size = writer.IsolateSnapshotSize();
1395 return Api::Success(); 1394 return Api::Success();
1396 } 1395 }
1397 1396
1398 1397
1399 static Dart_Handle createLibrarySnapshot(Dart_Handle library, 1398 static Dart_Handle createLibrarySnapshot(Dart_Handle library,
1400 uint8_t** buffer, 1399 uint8_t** buffer,
1401 intptr_t* size) { 1400 intptr_t* size) {
1402 DARTSCOPE(Thread::Current()); 1401 DARTSCOPE(Thread::Current());
1402 Isolate* I = T->isolate();
1403 if (buffer == NULL) { 1403 if (buffer == NULL) {
1404 RETURN_NULL_ERROR(buffer); 1404 RETURN_NULL_ERROR(buffer);
1405 } 1405 }
1406 if (size == NULL) { 1406 if (size == NULL) {
1407 RETURN_NULL_ERROR(size); 1407 RETURN_NULL_ERROR(size);
1408 } 1408 }
1409 // Finalize all classes if needed. 1409 // Finalize all classes if needed.
1410 Dart_Handle state = Api::CheckAndFinalizePendingClasses(I); 1410 Dart_Handle state = Api::CheckAndFinalizePendingClasses(T);
1411 if (::Dart_IsError(state)) { 1411 if (::Dart_IsError(state)) {
1412 return state; 1412 return state;
1413 } 1413 }
1414 Library& lib = Library::Handle(Z); 1414 Library& lib = Library::Handle(Z);
1415 if (library == Dart_Null()) { 1415 if (library == Dart_Null()) {
1416 lib ^= I->object_store()->root_library(); 1416 lib ^= I->object_store()->root_library();
1417 } else { 1417 } else {
1418 lib ^= Api::UnwrapHandle(library); 1418 lib ^= Api::UnwrapHandle(library);
1419 } 1419 }
1420 I->heap()->CollectAllGarbage(); 1420 I->heap()->CollectAllGarbage();
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1489 ASSERT(data->monitor != NULL); 1489 ASSERT(data->monitor != NULL);
1490 MonitorLocker ml(data->monitor); 1490 MonitorLocker ml(data->monitor);
1491 data->done = true; 1491 data->done = true;
1492 ml.Notify(); 1492 ml.Notify();
1493 } 1493 }
1494 1494
1495 1495
1496 DART_EXPORT Dart_Handle Dart_RunLoop() { 1496 DART_EXPORT Dart_Handle Dart_RunLoop() {
1497 Thread* T = Thread::Current(); 1497 Thread* T = Thread::Current();
1498 Isolate* I = T->isolate(); 1498 Isolate* I = T->isolate();
1499 CHECK_ISOLATE_SCOPE(I); 1499 CHECK_API_SCOPE(T);
1500 CHECK_CALLBACK_STATE(T); 1500 CHECK_CALLBACK_STATE(T);
1501 Monitor monitor; 1501 Monitor monitor;
1502 MonitorLocker ml(&monitor); 1502 MonitorLocker ml(&monitor);
1503 { 1503 {
1504 // The message handler run loop does not expect to have a current isolate 1504 // The message handler run loop does not expect to have a current isolate
1505 // so we exit the isolate here and enter it again after the runloop is done. 1505 // so we exit the isolate here and enter it again after the runloop is done.
1506 Thread::ExitIsolate(); 1506 Thread::ExitIsolate();
1507 RunLoopData data; 1507 RunLoopData data;
1508 data.monitor = &monitor; 1508 data.monitor = &monitor;
1509 data.done = false; 1509 data.done = false;
1510 I->message_handler()->Run( 1510 I->message_handler()->Run(
1511 Dart::thread_pool(), 1511 Dart::thread_pool(),
1512 NULL, RunLoopDone, reinterpret_cast<uword>(&data)); 1512 NULL, RunLoopDone, reinterpret_cast<uword>(&data));
1513 while (!data.done) { 1513 while (!data.done) {
1514 ml.Wait(); 1514 ml.Wait();
1515 } 1515 }
1516 Thread::EnterIsolate(I); 1516 Thread::EnterIsolate(I);
1517 } 1517 }
1518 if (I->object_store()->sticky_error() != Object::null()) { 1518 if (I->object_store()->sticky_error() != Object::null()) {
1519 Dart_Handle error = Api::NewHandle(I, I->object_store()->sticky_error()); 1519 Dart_Handle error = Api::NewHandle(T, I->object_store()->sticky_error());
1520 I->object_store()->clear_sticky_error(); 1520 I->object_store()->clear_sticky_error();
1521 return error; 1521 return error;
1522 } 1522 }
1523 if (FLAG_print_class_table) { 1523 if (FLAG_print_class_table) {
1524 HANDLESCOPE(T); 1524 HANDLESCOPE(T);
1525 I->class_table()->Print(); 1525 I->class_table()->Print();
1526 } 1526 }
1527 return Api::Success(); 1527 return Api::Success();
1528 } 1528 }
1529 1529
1530 1530
1531 DART_EXPORT Dart_Handle Dart_HandleMessage() { 1531 DART_EXPORT Dart_Handle Dart_HandleMessage() {
1532 Thread* thread = Thread::Current(); 1532 Thread* thread = Thread::Current();
1533 Isolate* isolate = thread->isolate(); 1533 Isolate* isolate = thread->isolate();
1534 CHECK_ISOLATE_SCOPE(isolate); 1534 CHECK_API_SCOPE(thread);
zra 2015/11/25 17:36:56 Sometimes this is called "thread" and sometimes "T
siva 2015/11/25 18:25:07 I have changed all the CHECK_API_SCOPE spots, mayb
1535 CHECK_CALLBACK_STATE(thread); 1535 CHECK_CALLBACK_STATE(thread);
1536 if (isolate->message_handler()->HandleNextMessage() != MessageHandler::kOK) { 1536 if (isolate->message_handler()->HandleNextMessage() != MessageHandler::kOK) {
1537 Dart_Handle error = Api::NewHandle(isolate, 1537 Dart_Handle error = Api::NewHandle(thread,
1538 isolate->object_store()->sticky_error()); 1538 isolate->object_store()->sticky_error());
1539 isolate->object_store()->clear_sticky_error(); 1539 isolate->object_store()->clear_sticky_error();
1540 return error; 1540 return error;
1541 } 1541 }
1542 return Api::Success(); 1542 return Api::Success();
1543 } 1543 }
1544 1544
1545 1545
1546 DART_EXPORT bool Dart_HandleServiceMessages() { 1546 DART_EXPORT bool Dart_HandleServiceMessages() {
1547 Thread* thread = Thread::Current(); 1547 Thread* thread = Thread::Current();
1548 Isolate* isolate = thread->isolate(); 1548 Isolate* isolate = thread->isolate();
1549 CHECK_ISOLATE_SCOPE(isolate); 1549 CHECK_API_SCOPE(thread);
1550 CHECK_CALLBACK_STATE(thread); 1550 CHECK_CALLBACK_STATE(thread);
1551 1551
1552 ASSERT(isolate->GetAndClearResumeRequest() == false); 1552 ASSERT(isolate->GetAndClearResumeRequest() == false);
1553 MessageHandler::MessageStatus status = 1553 MessageHandler::MessageStatus status =
1554 isolate->message_handler()->HandleOOBMessages(); 1554 isolate->message_handler()->HandleOOBMessages();
1555 bool resume = isolate->GetAndClearResumeRequest(); 1555 bool resume = isolate->GetAndClearResumeRequest();
1556 return (status != MessageHandler::kOK) || resume; 1556 return (status != MessageHandler::kOK) || resume;
1557 } 1557 }
1558 1558
1559 1559
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1593 1593
1594 1594
1595 DART_EXPORT Dart_Handle Dart_NewSendPort(Dart_Port port_id) { 1595 DART_EXPORT Dart_Handle Dart_NewSendPort(Dart_Port port_id) {
1596 DARTSCOPE(Thread::Current()); 1596 DARTSCOPE(Thread::Current());
1597 CHECK_CALLBACK_STATE(T); 1597 CHECK_CALLBACK_STATE(T);
1598 if (port_id == ILLEGAL_PORT) { 1598 if (port_id == ILLEGAL_PORT) {
1599 return Api::NewError("%s: illegal port_id %" Pd64 ".", 1599 return Api::NewError("%s: illegal port_id %" Pd64 ".",
1600 CURRENT_FUNC, 1600 CURRENT_FUNC,
1601 port_id); 1601 port_id);
1602 } 1602 }
1603 return Api::NewHandle(I, SendPort::New(port_id)); 1603 return Api::NewHandle(T, SendPort::New(port_id));
1604 } 1604 }
1605 1605
1606 1606
1607 DART_EXPORT Dart_Handle Dart_SendPortGetId(Dart_Handle port, 1607 DART_EXPORT Dart_Handle Dart_SendPortGetId(Dart_Handle port,
1608 Dart_Port* port_id) { 1608 Dart_Port* port_id) {
1609 DARTSCOPE(Thread::Current()); 1609 DARTSCOPE(Thread::Current());
1610 CHECK_CALLBACK_STATE(T); 1610 CHECK_CALLBACK_STATE(T);
1611 const SendPort& send_port = Api::UnwrapSendPortHandle(Z, port); 1611 const SendPort& send_port = Api::UnwrapSendPortHandle(Z, port);
1612 if (send_port.IsNull()) { 1612 if (send_port.IsNull()) {
1613 RETURN_TYPE_ERROR(Z, port, SendPort); 1613 RETURN_TYPE_ERROR(Z, port, SendPort);
(...skipping 12 matching lines...) Expand all
1626 return isolate->main_port(); 1626 return isolate->main_port();
1627 } 1627 }
1628 1628
1629 1629
1630 // --- Scopes ---- 1630 // --- Scopes ----
1631 1631
1632 DART_EXPORT void Dart_EnterScope() { 1632 DART_EXPORT void Dart_EnterScope() {
1633 Thread* thread = Thread::Current(); 1633 Thread* thread = Thread::Current();
1634 Isolate* isolate = thread->isolate(); 1634 Isolate* isolate = thread->isolate();
1635 CHECK_ISOLATE(isolate); 1635 CHECK_ISOLATE(isolate);
1636 ApiState* state = isolate->api_state(); 1636 ApiLocalScope* new_scope = thread->api_reusable_scope();
1637 ASSERT(state != NULL);
1638 ApiLocalScope* new_scope = state->reusable_scope();
1639 if (new_scope == NULL) { 1637 if (new_scope == NULL) {
1640 new_scope = new ApiLocalScope(state->top_scope(), 1638 new_scope = new ApiLocalScope(thread->api_top_scope(),
1641 thread->top_exit_frame_info()); 1639 thread->top_exit_frame_info());
1642 ASSERT(new_scope != NULL); 1640 ASSERT(new_scope != NULL);
1643 } else { 1641 } else {
1644 new_scope->Reinit(thread, 1642 new_scope->Reinit(thread,
1645 state->top_scope(), 1643 thread->api_top_scope(),
1646 thread->top_exit_frame_info()); 1644 thread->top_exit_frame_info());
1647 state->set_reusable_scope(NULL); 1645 thread->set_api_reusable_scope(NULL);
1648 } 1646 }
1649 state->set_top_scope(new_scope); // New scope is now the top scope. 1647 thread->set_api_top_scope(new_scope); // New scope is now the top scope.
1650 } 1648 }
1651 1649
1652 1650
1653 DART_EXPORT void Dart_ExitScope() { 1651 DART_EXPORT void Dart_ExitScope() {
1654 Thread* thread = Thread::Current(); 1652 Thread* thread = Thread::Current();
1655 Isolate* isolate = thread->isolate(); 1653 CHECK_API_SCOPE(thread);
1656 CHECK_ISOLATE_SCOPE(isolate); 1654 ApiLocalScope* scope = thread->api_top_scope();
1657 ApiState* state = isolate->api_state(); 1655 ApiLocalScope* reusable_scope = thread->api_reusable_scope();
1658 ApiLocalScope* scope = state->top_scope(); 1656 thread->set_api_top_scope(scope->previous()); // Reset top scope to previous.
1659 ApiLocalScope* reusable_scope = state->reusable_scope();
1660 state->set_top_scope(scope->previous()); // Reset top scope to previous.
1661 if (reusable_scope == NULL) { 1657 if (reusable_scope == NULL) {
1662 scope->Reset(thread); // Reset the old scope which we just exited. 1658 scope->Reset(thread); // Reset the old scope which we just exited.
1663 state->set_reusable_scope(scope); 1659 thread->set_api_reusable_scope(scope);
1664 } else { 1660 } else {
1665 ASSERT(reusable_scope != scope); 1661 ASSERT(reusable_scope != scope);
1666 delete scope; 1662 delete scope;
1667 } 1663 }
1668 } 1664 }
1669 1665
1670 1666
1671 DART_EXPORT uint8_t* Dart_ScopeAllocate(intptr_t size) { 1667 DART_EXPORT uint8_t* Dart_ScopeAllocate(intptr_t size) {
1672 Zone* zone; 1668 Zone* zone;
1673 Isolate* isolate = Isolate::Current(); 1669 Thread* thread = Thread::Current();
1674 if (isolate != NULL) { 1670 if (thread != NULL) {
1675 ApiState* state = isolate->api_state(); 1671 ApiLocalScope* scope = thread->api_top_scope();
1676 if (state == NULL) return NULL;
1677 ApiLocalScope* scope = state->top_scope();
1678 zone = scope->zone(); 1672 zone = scope->zone();
1679 } else { 1673 } else {
1680 ApiNativeScope* scope = ApiNativeScope::Current(); 1674 ApiNativeScope* scope = ApiNativeScope::Current();
1681 if (scope == NULL) return NULL; 1675 if (scope == NULL) return NULL;
1682 zone = scope->zone(); 1676 zone = scope->zone();
1683 } 1677 }
1684 return reinterpret_cast<uint8_t*>(zone->AllocUnsafe(size)); 1678 return reinterpret_cast<uint8_t*>(zone->AllocUnsafe(size));
1685 } 1679 }
1686 1680
1687 1681
(...skipping 23 matching lines...) Expand all
1711 const Instance& expected = 1705 const Instance& expected =
1712 Instance::CheckedHandle(Z, Api::UnwrapHandle(obj1)); 1706 Instance::CheckedHandle(Z, Api::UnwrapHandle(obj1));
1713 const Instance& actual = 1707 const Instance& actual =
1714 Instance::CheckedHandle(Z, Api::UnwrapHandle(obj2)); 1708 Instance::CheckedHandle(Z, Api::UnwrapHandle(obj2));
1715 const Object& result = 1709 const Object& result =
1716 Object::Handle(Z, DartLibraryCalls::Equals(expected, actual)); 1710 Object::Handle(Z, DartLibraryCalls::Equals(expected, actual));
1717 if (result.IsBool()) { 1711 if (result.IsBool()) {
1718 *value = Bool::Cast(result).value(); 1712 *value = Bool::Cast(result).value();
1719 return Api::Success(); 1713 return Api::Success();
1720 } else if (result.IsError()) { 1714 } else if (result.IsError()) {
1721 return Api::NewHandle(I, result.raw()); 1715 return Api::NewHandle(T, result.raw());
1722 } else { 1716 } else {
1723 return Api::NewError("Expected boolean result from =="); 1717 return Api::NewError("Expected boolean result from ==");
1724 } 1718 }
1725 } 1719 }
1726 1720
1727 1721
1728 // TODO(iposva): This call actually implements IsInstanceOfClass. 1722 // TODO(iposva): This call actually implements IsInstanceOfClass.
1729 // Do we also need a real Dart_IsInstanceOf, which should take an instance 1723 // Do we also need a real Dart_IsInstanceOf, which should take an instance
1730 // rather than an object? 1724 // rather than an object?
1731 DART_EXPORT Dart_Handle Dart_ObjectIsType(Dart_Handle object, 1725 DART_EXPORT Dart_Handle Dart_ObjectIsType(Dart_Handle object,
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
1886 1880
1887 DART_EXPORT bool Dart_IsByteBuffer(Dart_Handle handle) { 1881 DART_EXPORT bool Dart_IsByteBuffer(Dart_Handle handle) {
1888 TRACE_API_CALL(CURRENT_FUNC); 1882 TRACE_API_CALL(CURRENT_FUNC);
1889 return Api::ClassId(handle) == kByteBufferCid; 1883 return Api::ClassId(handle) == kByteBufferCid;
1890 } 1884 }
1891 1885
1892 1886
1893 DART_EXPORT bool Dart_IsFuture(Dart_Handle handle) { 1887 DART_EXPORT bool Dart_IsFuture(Dart_Handle handle) {
1894 TRACE_API_CALL(CURRENT_FUNC); 1888 TRACE_API_CALL(CURRENT_FUNC);
1895 DARTSCOPE(Thread::Current()); 1889 DARTSCOPE(Thread::Current());
1890 Isolate* I = T->isolate();
1896 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle)); 1891 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle));
1897 if (obj.IsInstance()) { 1892 if (obj.IsInstance()) {
1898 const Class& future_class = 1893 const Class& future_class =
1899 Class::Handle(I->object_store()->future_class()); 1894 Class::Handle(I->object_store()->future_class());
1900 ASSERT(!future_class.IsNull()); 1895 ASSERT(!future_class.IsNull());
1901 const Class& obj_class = Class::Handle(Z, obj.clazz()); 1896 const Class& obj_class = Class::Handle(Z, obj.clazz());
1902 Error& malformed_type_error = Error::Handle(Z); 1897 Error& malformed_type_error = Error::Handle(Z);
1903 bool is_future = obj_class.IsSubtypeOf(Object::null_type_arguments(), 1898 bool is_future = obj_class.IsSubtypeOf(Object::null_type_arguments(),
1904 future_class, 1899 future_class,
1905 Object::null_type_arguments(), 1900 Object::null_type_arguments(),
1906 &malformed_type_error); 1901 &malformed_type_error);
1907 ASSERT(malformed_type_error.IsNull()); // Type is a raw Future. 1902 ASSERT(malformed_type_error.IsNull()); // Type is a raw Future.
1908 return is_future; 1903 return is_future;
1909 } 1904 }
1910 return false; 1905 return false;
1911 } 1906 }
1912 1907
1913 1908
1914 // --- Instances ---- 1909 // --- Instances ----
1915 1910
1916 DART_EXPORT Dart_Handle Dart_InstanceGetType(Dart_Handle instance) { 1911 DART_EXPORT Dart_Handle Dart_InstanceGetType(Dart_Handle instance) {
1917 DARTSCOPE(Thread::Current()); 1912 DARTSCOPE(Thread::Current());
1913 Isolate* I = T->isolate();
1918 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(instance)); 1914 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(instance));
1919 if (obj.IsNull()) { 1915 if (obj.IsNull()) {
1920 return Api::NewHandle(I, I->object_store()->null_type()); 1916 return Api::NewHandle(T, I->object_store()->null_type());
1921 } 1917 }
1922 if (!obj.IsInstance()) { 1918 if (!obj.IsInstance()) {
1923 RETURN_TYPE_ERROR(Z, instance, Instance); 1919 RETURN_TYPE_ERROR(Z, instance, Instance);
1924 } 1920 }
1925 const Type& type = Type::Handle(Instance::Cast(obj).GetType()); 1921 const Type& type = Type::Handle(Instance::Cast(obj).GetType());
1926 return Api::NewHandle(I, type.Canonicalize()); 1922 return Api::NewHandle(T, type.Canonicalize());
1927 } 1923 }
1928 1924
1929 1925
1930 // --- Numbers, Integers and Doubles ---- 1926 // --- Numbers, Integers and Doubles ----
1931 1927
1932 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoInt64(Dart_Handle integer, 1928 DART_EXPORT Dart_Handle Dart_IntegerFitsIntoInt64(Dart_Handle integer,
1933 bool* fits) { 1929 bool* fits) {
1934 // Fast path for Smis and Mints. 1930 // Fast path for Smis and Mints.
1935 Thread* thread = Thread::Current(); 1931 Thread* thread = Thread::Current();
1936 Isolate* isolate = thread->isolate(); 1932 Isolate* isolate = thread->isolate();
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1978 } 1974 }
1979 1975
1980 1976
1981 DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value) { 1977 DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value) {
1982 // Fast path for Smis. 1978 // Fast path for Smis.
1983 Thread* thread = Thread::Current(); 1979 Thread* thread = Thread::Current();
1984 Isolate* isolate = thread->isolate(); 1980 Isolate* isolate = thread->isolate();
1985 CHECK_ISOLATE(isolate); 1981 CHECK_ISOLATE(isolate);
1986 if (Smi::IsValid(value)) { 1982 if (Smi::IsValid(value)) {
1987 NOHANDLESCOPE(thread); 1983 NOHANDLESCOPE(thread);
1988 return Api::NewHandle(isolate, Smi::New(static_cast<intptr_t>(value))); 1984 return Api::NewHandle(thread, Smi::New(static_cast<intptr_t>(value)));
1989 } 1985 }
1990 // Slow path for Mints and Bigints. 1986 // Slow path for Mints and Bigints.
1991 DARTSCOPE(thread); 1987 DARTSCOPE(thread);
1992 CHECK_CALLBACK_STATE(thread); 1988 CHECK_CALLBACK_STATE(thread);
1993 return Api::NewHandle(isolate, Integer::New(value)); 1989 return Api::NewHandle(thread, Integer::New(value));
1994 } 1990 }
1995 1991
1996 1992
1997 DART_EXPORT Dart_Handle Dart_NewIntegerFromUint64(uint64_t value) { 1993 DART_EXPORT Dart_Handle Dart_NewIntegerFromUint64(uint64_t value) {
1998 DARTSCOPE(Thread::Current()); 1994 DARTSCOPE(Thread::Current());
1999 CHECK_CALLBACK_STATE(T); 1995 CHECK_CALLBACK_STATE(T);
2000 return Api::NewHandle(I, Integer::NewFromUint64(value)); 1996 return Api::NewHandle(T, Integer::NewFromUint64(value));
2001 } 1997 }
2002 1998
2003 1999
2004 DART_EXPORT Dart_Handle Dart_NewIntegerFromHexCString(const char* str) { 2000 DART_EXPORT Dart_Handle Dart_NewIntegerFromHexCString(const char* str) {
2005 DARTSCOPE(Thread::Current()); 2001 DARTSCOPE(Thread::Current());
2006 CHECK_CALLBACK_STATE(T); 2002 CHECK_CALLBACK_STATE(T);
2007 const String& str_obj = String::Handle(Z, String::New(str)); 2003 const String& str_obj = String::Handle(Z, String::New(str));
2008 return Api::NewHandle(I, Integer::New(str_obj)); 2004 return Api::NewHandle(T, Integer::New(str_obj));
2009 } 2005 }
2010 2006
2011 2007
2012 DART_EXPORT Dart_Handle Dart_IntegerToInt64(Dart_Handle integer, 2008 DART_EXPORT Dart_Handle Dart_IntegerToInt64(Dart_Handle integer,
2013 int64_t* value) { 2009 int64_t* value) {
2014 // Fast path for Smis. 2010 // Fast path for Smis.
2015 Thread* thread = Thread::Current(); 2011 Thread* thread = Thread::Current();
2016 Isolate* isolate = thread->isolate(); 2012 Isolate* isolate = thread->isolate();
2017 CHECK_ISOLATE(isolate); 2013 CHECK_ISOLATE(isolate);
2018 if (Api::IsSmi(integer)) { 2014 if (Api::IsSmi(integer)) {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
2071 *value = bigint.AsUint64Value(); 2067 *value = bigint.AsUint64Value();
2072 return Api::Success(); 2068 return Api::Success();
2073 } 2069 }
2074 } 2070 }
2075 return Api::NewError("%s: Integer %s cannot be represented as a uint64_t.", 2071 return Api::NewError("%s: Integer %s cannot be represented as a uint64_t.",
2076 CURRENT_FUNC, int_obj.ToCString()); 2072 CURRENT_FUNC, int_obj.ToCString());
2077 } 2073 }
2078 2074
2079 2075
2080 static uword BigintAllocate(intptr_t size) { 2076 static uword BigintAllocate(intptr_t size) {
2081 return Api::TopScope(Isolate::Current())->zone()->AllocUnsafe(size); 2077 return Api::TopScope(Thread::Current())->zone()->AllocUnsafe(size);
2082 } 2078 }
2083 2079
2084 2080
2085 DART_EXPORT Dart_Handle Dart_IntegerToHexCString(Dart_Handle integer, 2081 DART_EXPORT Dart_Handle Dart_IntegerToHexCString(Dart_Handle integer,
2086 const char** value) { 2082 const char** value) {
2087 DARTSCOPE(Thread::Current()); 2083 DARTSCOPE(Thread::Current());
2088 const Integer& int_obj = Api::UnwrapIntegerHandle(Z, integer); 2084 const Integer& int_obj = Api::UnwrapIntegerHandle(Z, integer);
2089 if (int_obj.IsNull()) { 2085 if (int_obj.IsNull()) {
2090 RETURN_TYPE_ERROR(Z, integer, Integer); 2086 RETURN_TYPE_ERROR(Z, integer, Integer);
2091 } 2087 }
2092 if (int_obj.IsSmi() || int_obj.IsMint()) { 2088 if (int_obj.IsSmi() || int_obj.IsMint()) {
2093 const Bigint& bigint = Bigint::Handle(Z, 2089 const Bigint& bigint = Bigint::Handle(Z,
2094 Bigint::NewFromInt64(int_obj.AsInt64Value())); 2090 Bigint::NewFromInt64(int_obj.AsInt64Value()));
2095 *value = bigint.ToHexCString(BigintAllocate); 2091 *value = bigint.ToHexCString(BigintAllocate);
2096 } else { 2092 } else {
2097 *value = Bigint::Cast(int_obj).ToHexCString(BigintAllocate); 2093 *value = Bigint::Cast(int_obj).ToHexCString(BigintAllocate);
2098 } 2094 }
2099 return Api::Success(); 2095 return Api::Success();
2100 } 2096 }
2101 2097
2102 2098
2103 DART_EXPORT Dart_Handle Dart_NewDouble(double value) { 2099 DART_EXPORT Dart_Handle Dart_NewDouble(double value) {
2104 DARTSCOPE(Thread::Current()); 2100 DARTSCOPE(Thread::Current());
2105 CHECK_CALLBACK_STATE(T); 2101 CHECK_CALLBACK_STATE(T);
2106 return Api::NewHandle(I, Double::New(value)); 2102 return Api::NewHandle(T, Double::New(value));
2107 } 2103 }
2108 2104
2109 2105
2110 DART_EXPORT Dart_Handle Dart_DoubleValue(Dart_Handle double_obj, 2106 DART_EXPORT Dart_Handle Dart_DoubleValue(Dart_Handle double_obj,
2111 double* value) { 2107 double* value) {
2112 DARTSCOPE(Thread::Current()); 2108 DARTSCOPE(Thread::Current());
2113 const Double& obj = Api::UnwrapDoubleHandle(Z, double_obj); 2109 const Double& obj = Api::UnwrapDoubleHandle(Z, double_obj);
2114 if (obj.IsNull()) { 2110 if (obj.IsNull()) {
2115 RETURN_TYPE_ERROR(Z, double_obj, Double); 2111 RETURN_TYPE_ERROR(Z, double_obj, Double);
2116 } 2112 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
2167 return Api::Success(); 2163 return Api::Success();
2168 } 2164 }
2169 2165
2170 2166
2171 DART_EXPORT Dart_Handle Dart_NewStringFromCString(const char* str) { 2167 DART_EXPORT Dart_Handle Dart_NewStringFromCString(const char* str) {
2172 DARTSCOPE(Thread::Current()); 2168 DARTSCOPE(Thread::Current());
2173 if (str == NULL) { 2169 if (str == NULL) {
2174 RETURN_NULL_ERROR(str); 2170 RETURN_NULL_ERROR(str);
2175 } 2171 }
2176 CHECK_CALLBACK_STATE(T); 2172 CHECK_CALLBACK_STATE(T);
2177 return Api::NewHandle(I, String::New(str)); 2173 return Api::NewHandle(T, String::New(str));
2178 } 2174 }
2179 2175
2180 2176
2181 DART_EXPORT Dart_Handle Dart_NewStringFromUTF8(const uint8_t* utf8_array, 2177 DART_EXPORT Dart_Handle Dart_NewStringFromUTF8(const uint8_t* utf8_array,
2182 intptr_t length) { 2178 intptr_t length) {
2183 DARTSCOPE(Thread::Current()); 2179 DARTSCOPE(Thread::Current());
2184 if (utf8_array == NULL && length != 0) { 2180 if (utf8_array == NULL && length != 0) {
2185 RETURN_NULL_ERROR(utf8_array); 2181 RETURN_NULL_ERROR(utf8_array);
2186 } 2182 }
2187 CHECK_LENGTH(length, String::kMaxElements); 2183 CHECK_LENGTH(length, String::kMaxElements);
2188 if (!Utf8::IsValid(utf8_array, length)) { 2184 if (!Utf8::IsValid(utf8_array, length)) {
2189 return Api::NewError("%s expects argument 'str' to be valid UTF-8.", 2185 return Api::NewError("%s expects argument 'str' to be valid UTF-8.",
2190 CURRENT_FUNC); 2186 CURRENT_FUNC);
2191 } 2187 }
2192 CHECK_CALLBACK_STATE(T); 2188 CHECK_CALLBACK_STATE(T);
2193 return Api::NewHandle(I, String::FromUTF8(utf8_array, length)); 2189 return Api::NewHandle(T, String::FromUTF8(utf8_array, length));
2194 } 2190 }
2195 2191
2196 2192
2197 DART_EXPORT Dart_Handle Dart_NewStringFromUTF16(const uint16_t* utf16_array, 2193 DART_EXPORT Dart_Handle Dart_NewStringFromUTF16(const uint16_t* utf16_array,
2198 intptr_t length) { 2194 intptr_t length) {
2199 DARTSCOPE(Thread::Current()); 2195 DARTSCOPE(Thread::Current());
2200 if (utf16_array == NULL && length != 0) { 2196 if (utf16_array == NULL && length != 0) {
2201 RETURN_NULL_ERROR(utf16_array); 2197 RETURN_NULL_ERROR(utf16_array);
2202 } 2198 }
2203 CHECK_LENGTH(length, String::kMaxElements); 2199 CHECK_LENGTH(length, String::kMaxElements);
2204 CHECK_CALLBACK_STATE(T); 2200 CHECK_CALLBACK_STATE(T);
2205 return Api::NewHandle(I, String::FromUTF16(utf16_array, length)); 2201 return Api::NewHandle(T, String::FromUTF16(utf16_array, length));
2206 } 2202 }
2207 2203
2208 2204
2209 DART_EXPORT Dart_Handle Dart_NewStringFromUTF32(const int32_t* utf32_array, 2205 DART_EXPORT Dart_Handle Dart_NewStringFromUTF32(const int32_t* utf32_array,
2210 intptr_t length) { 2206 intptr_t length) {
2211 DARTSCOPE(Thread::Current()); 2207 DARTSCOPE(Thread::Current());
2212 if (utf32_array == NULL && length != 0) { 2208 if (utf32_array == NULL && length != 0) {
2213 RETURN_NULL_ERROR(utf32_array); 2209 RETURN_NULL_ERROR(utf32_array);
2214 } 2210 }
2215 CHECK_LENGTH(length, String::kMaxElements); 2211 CHECK_LENGTH(length, String::kMaxElements);
2216 CHECK_CALLBACK_STATE(T); 2212 CHECK_CALLBACK_STATE(T);
2217 return Api::NewHandle(I, String::FromUTF32(utf32_array, length)); 2213 return Api::NewHandle(T, String::FromUTF32(utf32_array, length));
2218 } 2214 }
2219 2215
2220 2216
2221 DART_EXPORT Dart_Handle Dart_NewExternalLatin1String( 2217 DART_EXPORT Dart_Handle Dart_NewExternalLatin1String(
2222 const uint8_t* latin1_array, 2218 const uint8_t* latin1_array,
2223 intptr_t length, 2219 intptr_t length,
2224 void* peer, 2220 void* peer,
2225 Dart_PeerFinalizer cback) { 2221 Dart_PeerFinalizer cback) {
2226 DARTSCOPE(Thread::Current()); 2222 DARTSCOPE(Thread::Current());
2223 Isolate* I = T->isolate();
zra 2015/11/25 17:36:56 In a few places it looks like these isolates are o
siva 2015/11/25 18:25:08 Done.
2227 if (latin1_array == NULL && length != 0) { 2224 if (latin1_array == NULL && length != 0) {
2228 RETURN_NULL_ERROR(latin1_array); 2225 RETURN_NULL_ERROR(latin1_array);
2229 } 2226 }
2230 CHECK_LENGTH(length, String::kMaxElements); 2227 CHECK_LENGTH(length, String::kMaxElements);
2231 CHECK_CALLBACK_STATE(T); 2228 CHECK_CALLBACK_STATE(T);
2232 return Api::NewHandle(I, 2229 return Api::NewHandle(T,
2233 String::NewExternal(latin1_array, 2230 String::NewExternal(latin1_array,
2234 length, 2231 length,
2235 peer, 2232 peer,
2236 cback, 2233 cback,
2237 SpaceForExternal(I, length))); 2234 SpaceForExternal(I, length)));
2238 } 2235 }
2239 2236
2240 2237
2241 DART_EXPORT Dart_Handle Dart_NewExternalUTF16String(const uint16_t* utf16_array, 2238 DART_EXPORT Dart_Handle Dart_NewExternalUTF16String(const uint16_t* utf16_array,
2242 intptr_t length, 2239 intptr_t length,
2243 void* peer, 2240 void* peer,
2244 Dart_PeerFinalizer cback) { 2241 Dart_PeerFinalizer cback) {
2245 DARTSCOPE(Thread::Current()); 2242 DARTSCOPE(Thread::Current());
2243 Isolate* I = T->isolate();
2246 if (utf16_array == NULL && length != 0) { 2244 if (utf16_array == NULL && length != 0) {
2247 RETURN_NULL_ERROR(utf16_array); 2245 RETURN_NULL_ERROR(utf16_array);
2248 } 2246 }
2249 CHECK_LENGTH(length, String::kMaxElements); 2247 CHECK_LENGTH(length, String::kMaxElements);
2250 CHECK_CALLBACK_STATE(T); 2248 CHECK_CALLBACK_STATE(T);
2251 intptr_t bytes = length * sizeof(*utf16_array); 2249 intptr_t bytes = length * sizeof(*utf16_array);
2252 return Api::NewHandle(I, 2250 return Api::NewHandle(T,
2253 String::NewExternal(utf16_array, 2251 String::NewExternal(utf16_array,
2254 length, 2252 length,
2255 peer, 2253 peer,
2256 cback, 2254 cback,
2257 SpaceForExternal(I, bytes))); 2255 SpaceForExternal(I, bytes)));
2258 } 2256 }
2259 2257
2260 2258
2261 DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object, 2259 DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object,
2262 const char** cstr) { 2260 const char** cstr) {
2263 DARTSCOPE(Thread::Current()); 2261 DARTSCOPE(Thread::Current());
2264 if (cstr == NULL) { 2262 if (cstr == NULL) {
2265 RETURN_NULL_ERROR(cstr); 2263 RETURN_NULL_ERROR(cstr);
2266 } 2264 }
2267 const String& str_obj = Api::UnwrapStringHandle(Z, object); 2265 const String& str_obj = Api::UnwrapStringHandle(Z, object);
2268 if (str_obj.IsNull()) { 2266 if (str_obj.IsNull()) {
2269 RETURN_TYPE_ERROR(Z, object, String); 2267 RETURN_TYPE_ERROR(Z, object, String);
2270 } 2268 }
2271 intptr_t string_length = Utf8::Length(str_obj); 2269 intptr_t string_length = Utf8::Length(str_obj);
2272 char* res = Api::TopScope(I)->zone()->Alloc<char>(string_length + 1); 2270 char* res = Api::TopScope(T)->zone()->Alloc<char>(string_length + 1);
2273 if (res == NULL) { 2271 if (res == NULL) {
2274 return Api::NewError("Unable to allocate memory"); 2272 return Api::NewError("Unable to allocate memory");
2275 } 2273 }
2276 const char* string_value = str_obj.ToCString(); 2274 const char* string_value = str_obj.ToCString();
2277 memmove(res, string_value, string_length + 1); 2275 memmove(res, string_value, string_length + 1);
2278 ASSERT(res[string_length] == '\0'); 2276 ASSERT(res[string_length] == '\0');
2279 *cstr = res; 2277 *cstr = res;
2280 return Api::Success(); 2278 return Api::Success();
2281 } 2279 }
2282 2280
2283 2281
2284 DART_EXPORT Dart_Handle Dart_StringToUTF8(Dart_Handle str, 2282 DART_EXPORT Dart_Handle Dart_StringToUTF8(Dart_Handle str,
2285 uint8_t** utf8_array, 2283 uint8_t** utf8_array,
2286 intptr_t* length) { 2284 intptr_t* length) {
2287 DARTSCOPE(Thread::Current()); 2285 DARTSCOPE(Thread::Current());
2288 if (utf8_array == NULL) { 2286 if (utf8_array == NULL) {
2289 RETURN_NULL_ERROR(utf8_array); 2287 RETURN_NULL_ERROR(utf8_array);
2290 } 2288 }
2291 if (length == NULL) { 2289 if (length == NULL) {
2292 RETURN_NULL_ERROR(length); 2290 RETURN_NULL_ERROR(length);
2293 } 2291 }
2294 const String& str_obj = Api::UnwrapStringHandle(Z, str); 2292 const String& str_obj = Api::UnwrapStringHandle(Z, str);
2295 if (str_obj.IsNull()) { 2293 if (str_obj.IsNull()) {
2296 RETURN_TYPE_ERROR(Z, str, String); 2294 RETURN_TYPE_ERROR(Z, str, String);
2297 } 2295 }
2298 intptr_t str_len = Utf8::Length(str_obj); 2296 intptr_t str_len = Utf8::Length(str_obj);
2299 *utf8_array = Api::TopScope(I)->zone()->Alloc<uint8_t>(str_len); 2297 *utf8_array = Api::TopScope(T)->zone()->Alloc<uint8_t>(str_len);
2300 if (*utf8_array == NULL) { 2298 if (*utf8_array == NULL) {
2301 return Api::NewError("Unable to allocate memory"); 2299 return Api::NewError("Unable to allocate memory");
2302 } 2300 }
2303 str_obj.ToUTF8(*utf8_array, str_len); 2301 str_obj.ToUTF8(*utf8_array, str_len);
2304 *length = str_len; 2302 *length = str_len;
2305 return Api::Success(); 2303 return Api::Success();
2306 } 2304 }
2307 2305
2308 2306
2309 DART_EXPORT Dart_Handle Dart_StringToLatin1(Dart_Handle str, 2307 DART_EXPORT Dart_Handle Dart_StringToLatin1(Dart_Handle str,
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
2409 ASSERT(str_obj.IsTwoByteString()); 2407 ASSERT(str_obj.IsTwoByteString());
2410 ASSERT(length >= (copy_len * str_obj.CharSize())); 2408 ASSERT(length >= (copy_len * str_obj.CharSize()));
2411 uint16_t* utf16_array = reinterpret_cast<uint16_t*>(array); 2409 uint16_t* utf16_array = reinterpret_cast<uint16_t*>(array);
2412 for (intptr_t i = 0; i < copy_len; i++) { 2410 for (intptr_t i = 0; i < copy_len; i++) {
2413 utf16_array[i] = str_obj.CharAt(i); 2411 utf16_array[i] = str_obj.CharAt(i);
2414 } 2412 }
2415 TwoByteString::SetPeer(str_obj, peer, cback); 2413 TwoByteString::SetPeer(str_obj, peer, cback);
2416 } 2414 }
2417 return str; 2415 return str;
2418 } 2416 }
2419 return Api::NewHandle(I, str_obj.MakeExternal(array, length, peer, cback)); 2417 return Api::NewHandle(T, str_obj.MakeExternal(array, length, peer, cback));
2420 } 2418 }
2421 2419
2422 2420
2423 DART_EXPORT Dart_Handle Dart_StringGetProperties(Dart_Handle object, 2421 DART_EXPORT Dart_Handle Dart_StringGetProperties(Dart_Handle object,
2424 intptr_t* char_size, 2422 intptr_t* char_size,
2425 intptr_t* str_len, 2423 intptr_t* str_len,
2426 void** peer) { 2424 void** peer) {
2427 Thread* thread = Thread::Current(); 2425 Thread* thread = Thread::Current();
2428 CHECK_ISOLATE(thread->isolate()); 2426 CHECK_ISOLATE(thread->isolate());
2429 ReusableObjectHandleScope reused_obj_handle(thread); 2427 ReusableObjectHandleScope reused_obj_handle(thread);
(...skipping 13 matching lines...) Expand all
2443 return Api::Success(); 2441 return Api::Success();
2444 } 2442 }
2445 2443
2446 2444
2447 // --- Lists --- 2445 // --- Lists ---
2448 2446
2449 DART_EXPORT Dart_Handle Dart_NewList(intptr_t length) { 2447 DART_EXPORT Dart_Handle Dart_NewList(intptr_t length) {
2450 DARTSCOPE(Thread::Current()); 2448 DARTSCOPE(Thread::Current());
2451 CHECK_LENGTH(length, Array::kMaxElements); 2449 CHECK_LENGTH(length, Array::kMaxElements);
2452 CHECK_CALLBACK_STATE(T); 2450 CHECK_CALLBACK_STATE(T);
2453 return Api::NewHandle(I, Array::New(length)); 2451 return Api::NewHandle(T, Array::New(length));
2454 } 2452 }
2455 2453
2456 2454
2457 #define GET_LIST_LENGTH(zone, type, obj, len) \ 2455 #define GET_LIST_LENGTH(zone, type, obj, len) \
2458 type& array = type::Handle(zone); \ 2456 type& array = type::Handle(zone); \
2459 array ^= obj.raw(); \ 2457 array ^= obj.raw(); \
2460 *len = array.Length(); \ 2458 *len = array.Length(); \
2461 return Api::Success(); \ 2459 return Api::Success(); \
2462 2460
2463 2461
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
2517 if (bigint.FitsIntoInt64()) { 2515 if (bigint.FitsIntoInt64()) {
2518 int64_t bigint_value = bigint.AsInt64Value(); 2516 int64_t bigint_value = bigint.AsInt64Value();
2519 if (bigint_value >= kIntptrMin && bigint_value <= kIntptrMax) { 2517 if (bigint_value >= kIntptrMin && bigint_value <= kIntptrMax) {
2520 *len = static_cast<intptr_t>(bigint_value); 2518 *len = static_cast<intptr_t>(bigint_value);
2521 } 2519 }
2522 } 2520 }
2523 } 2521 }
2524 return Api::NewError("Length of List object is greater than the " 2522 return Api::NewError("Length of List object is greater than the "
2525 "maximum value that 'len' parameter can hold"); 2523 "maximum value that 'len' parameter can hold");
2526 } else if (retval.IsError()) { 2524 } else if (retval.IsError()) {
2527 return Api::NewHandle(I, retval.raw()); 2525 return Api::NewHandle(T, retval.raw());
2528 } else { 2526 } else {
2529 return Api::NewError("Length of List object is not an integer"); 2527 return Api::NewError("Length of List object is not an integer");
2530 } 2528 }
2531 } 2529 }
2532 2530
2533 2531
2534 #define GET_LIST_ELEMENT(isolate, type, obj, index) \ 2532 #define GET_LIST_ELEMENT(thread, type, obj, index) \
2535 const type& array_obj = type::Cast(obj); \ 2533 const type& array_obj = type::Cast(obj); \
2536 if ((index >= 0) && (index < array_obj.Length())) { \ 2534 if ((index >= 0) && (index < array_obj.Length())) { \
2537 return Api::NewHandle(isolate, array_obj.At(index)); \ 2535 return Api::NewHandle(thread, array_obj.At(index)); \
2538 } \ 2536 } \
2539 return Api::NewError("Invalid index passed in to access list element"); \ 2537 return Api::NewError("Invalid index passed in to access list element"); \
2540 2538
2541 2539
2542 DART_EXPORT Dart_Handle Dart_ListGetAt(Dart_Handle list, intptr_t index) { 2540 DART_EXPORT Dart_Handle Dart_ListGetAt(Dart_Handle list, intptr_t index) {
2543 DARTSCOPE(Thread::Current()); 2541 DARTSCOPE(Thread::Current());
2544 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(list)); 2542 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(list));
2545 if (obj.IsArray()) { 2543 if (obj.IsArray()) {
2546 GET_LIST_ELEMENT(I, Array, obj, index); 2544 GET_LIST_ELEMENT(T, Array, obj, index);
2547 } else if (obj.IsGrowableObjectArray()) { 2545 } else if (obj.IsGrowableObjectArray()) {
2548 GET_LIST_ELEMENT(I, GrowableObjectArray, obj, index); 2546 GET_LIST_ELEMENT(T, GrowableObjectArray, obj, index);
2549 } else if (obj.IsError()) { 2547 } else if (obj.IsError()) {
2550 return list; 2548 return list;
2551 } else { 2549 } else {
2552 CHECK_CALLBACK_STATE(T); 2550 CHECK_CALLBACK_STATE(T);
2553 // Check and handle a dart object that implements the List interface. 2551 // Check and handle a dart object that implements the List interface.
2554 const Instance& instance = Instance::Handle(Z, GetListInstance(Z, obj)); 2552 const Instance& instance = Instance::Handle(Z, GetListInstance(Z, obj));
2555 if (!instance.IsNull()) { 2553 if (!instance.IsNull()) {
2556 return Api::NewHandle(I, Send1Arg( 2554 return Api::NewHandle(T, Send1Arg(
2557 instance, 2555 instance,
2558 Symbols::IndexToken(), 2556 Symbols::IndexToken(),
2559 Instance::Handle(Z, Integer::New(index)))); 2557 Instance::Handle(Z, Integer::New(index))));
2560 } 2558 }
2561 return Api::NewError("Object does not implement the 'List' interface"); 2559 return Api::NewError("Object does not implement the 'List' interface");
2562 } 2560 }
2563 } 2561 }
2564 2562
2565 2563
2566 #define GET_LIST_RANGE(isolate, type, obj, offset, length) \ 2564 #define GET_LIST_RANGE(thread, type, obj, offset, length) \
2567 const type& array_obj = type::Cast(obj); \ 2565 const type& array_obj = type::Cast(obj); \
2568 if ((offset >= 0) && (offset + length <= array_obj.Length())) { \ 2566 if ((offset >= 0) && (offset + length <= array_obj.Length())) { \
2569 for (intptr_t index = 0; index < length; ++index) { \ 2567 for (intptr_t index = 0; index < length; ++index) { \
2570 result[index] = Api::NewHandle(isolate, array_obj.At(index + offset)); \ 2568 result[index] = Api::NewHandle(thread, array_obj.At(index + offset)); \
2571 } \ 2569 } \
2572 return Api::Success(); \ 2570 return Api::Success(); \
2573 } \ 2571 } \
2574 return Api::NewError("Invalid offset/length passed in to access list"); \ 2572 return Api::NewError("Invalid offset/length passed in to access list"); \
2575 2573
2576 2574
2577 DART_EXPORT Dart_Handle Dart_ListGetRange(Dart_Handle list, 2575 DART_EXPORT Dart_Handle Dart_ListGetRange(Dart_Handle list,
2578 intptr_t offset, 2576 intptr_t offset,
2579 intptr_t length, 2577 intptr_t length,
2580 Dart_Handle* result) { 2578 Dart_Handle* result) {
2581 DARTSCOPE(Thread::Current()); 2579 DARTSCOPE(Thread::Current());
2582 if (result == NULL) { 2580 if (result == NULL) {
2583 RETURN_NULL_ERROR(result); 2581 RETURN_NULL_ERROR(result);
2584 } 2582 }
2585 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(list)); 2583 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(list));
2586 if (obj.IsArray()) { 2584 if (obj.IsArray()) {
2587 GET_LIST_RANGE(I, Array, obj, offset, length); 2585 GET_LIST_RANGE(T, Array, obj, offset, length);
2588 } else if (obj.IsGrowableObjectArray()) { 2586 } else if (obj.IsGrowableObjectArray()) {
2589 GET_LIST_RANGE(I, GrowableObjectArray, obj, offset, length); 2587 GET_LIST_RANGE(T, GrowableObjectArray, obj, offset, length);
2590 } else if (obj.IsError()) { 2588 } else if (obj.IsError()) {
2591 return list; 2589 return list;
2592 } else { 2590 } else {
2593 CHECK_CALLBACK_STATE(T); 2591 CHECK_CALLBACK_STATE(T);
2594 // Check and handle a dart object that implements the List interface. 2592 // Check and handle a dart object that implements the List interface.
2595 const Instance& instance = Instance::Handle(Z, GetListInstance(Z, obj)); 2593 const Instance& instance = Instance::Handle(Z, GetListInstance(Z, obj));
2596 if (!instance.IsNull()) { 2594 if (!instance.IsNull()) {
2597 const intptr_t kNumArgs = 2; 2595 const intptr_t kNumArgs = 2;
2598 ArgumentsDescriptor args_desc( 2596 ArgumentsDescriptor args_desc(
2599 Array::Handle(ArgumentsDescriptor::New(kNumArgs))); 2597 Array::Handle(ArgumentsDescriptor::New(kNumArgs)));
2600 const Function& function = Function::Handle(Z, 2598 const Function& function = Function::Handle(Z,
2601 Resolver::ResolveDynamic(instance, 2599 Resolver::ResolveDynamic(instance,
2602 Symbols::AssignIndexToken(), 2600 Symbols::AssignIndexToken(),
2603 args_desc)); 2601 args_desc));
2604 if (!function.IsNull()) { 2602 if (!function.IsNull()) {
2605 const Array& args = Array::Handle(Array::New(kNumArgs)); 2603 const Array& args = Array::Handle(Array::New(kNumArgs));
2606 args.SetAt(0, instance); 2604 args.SetAt(0, instance);
2607 Instance& index = Instance::Handle(Z); 2605 Instance& index = Instance::Handle(Z);
2608 for (intptr_t i = 0; i < length; ++i) { 2606 for (intptr_t i = 0; i < length; ++i) {
2609 index = Integer::New(i); 2607 index = Integer::New(i);
2610 args.SetAt(1, index); 2608 args.SetAt(1, index);
2611 Dart_Handle value = Api::NewHandle(I, 2609 Dart_Handle value = Api::NewHandle(T,
2612 DartEntry::InvokeFunction(function, args)); 2610 DartEntry::InvokeFunction(function, args));
2613 if (::Dart_IsError(value)) 2611 if (::Dart_IsError(value))
2614 return value; 2612 return value;
2615 result[i] = value; 2613 result[i] = value;
2616 } 2614 }
2617 return Api::Success(); 2615 return Api::Success();
2618 } 2616 }
2619 } 2617 }
2620 return Api::NewError("Object does not implement the 'List' interface"); 2618 return Api::NewError("Object does not implement the 'List' interface");
2621 } 2619 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
2664 if (!function.IsNull()) { 2662 if (!function.IsNull()) {
2665 const Integer& index_obj = Integer::Handle(Z, Integer::New(index)); 2663 const Integer& index_obj = Integer::Handle(Z, Integer::New(index));
2666 const Object& value_obj = Object::Handle(Z, Api::UnwrapHandle(value)); 2664 const Object& value_obj = Object::Handle(Z, Api::UnwrapHandle(value));
2667 if (!value_obj.IsNull() && !value_obj.IsInstance()) { 2665 if (!value_obj.IsNull() && !value_obj.IsInstance()) {
2668 RETURN_TYPE_ERROR(Z, value, Instance); 2666 RETURN_TYPE_ERROR(Z, value, Instance);
2669 } 2667 }
2670 const Array& args = Array::Handle(Z, Array::New(kNumArgs)); 2668 const Array& args = Array::Handle(Z, Array::New(kNumArgs));
2671 args.SetAt(0, instance); 2669 args.SetAt(0, instance);
2672 args.SetAt(1, index_obj); 2670 args.SetAt(1, index_obj);
2673 args.SetAt(2, value_obj); 2671 args.SetAt(2, value_obj);
2674 return Api::NewHandle(I, DartEntry::InvokeFunction(function, 2672 return Api::NewHandle(T, DartEntry::InvokeFunction(function,
2675 args)); 2673 args));
zra 2015/11/25 17:36:56 Indentation looks a bit weird to me here.
siva 2015/11/25 18:25:07 Done.
2676 } 2674 }
2677 } 2675 }
2678 return Api::NewError("Object does not implement the 'List' interface"); 2676 return Api::NewError("Object does not implement the 'List' interface");
2679 } 2677 }
2680 } 2678 }
2681 2679
2682 2680
2683 static RawObject* ResolveConstructor(const char* current_func, 2681 static RawObject* ResolveConstructor(const char* current_func,
2684 const Class& cls, 2682 const Class& cls,
2685 const String& class_name, 2683 const String& class_name,
2686 const String& dotted_name, 2684 const String& dotted_name,
2687 int num_args); 2685 int num_args);
2688 2686
2689 2687
2690 static RawObject* ThrowArgumentError(const char* exception_message) { 2688 static RawObject* ThrowArgumentError(const char* exception_message) {
2691 Thread* thread = Thread::Current(); 2689 Thread* thread = Thread::Current();
2692 Isolate* isolate = thread->isolate();
2693 Zone* zone = thread->zone(); 2690 Zone* zone = thread->zone();
2694 // Lookup the class ArgumentError in dart:core. 2691 // Lookup the class ArgumentError in dart:core.
2695 const String& lib_url = String::Handle(String::New("dart:core")); 2692 const String& lib_url = String::Handle(String::New("dart:core"));
2696 const String& class_name = String::Handle(String::New("ArgumentError")); 2693 const String& class_name = String::Handle(String::New("ArgumentError"));
2697 const Library& lib = 2694 const Library& lib =
2698 Library::Handle(zone, Library::LookupLibrary(lib_url)); 2695 Library::Handle(zone, Library::LookupLibrary(lib_url));
2699 if (lib.IsNull()) { 2696 if (lib.IsNull()) {
2700 const String& message = String::Handle( 2697 const String& message = String::Handle(
2701 String::NewFormatted("%s: library '%s' not found.", 2698 String::NewFormatted("%s: library '%s' not found.",
2702 CURRENT_FUNC, lib_url.ToCString())); 2699 CURRENT_FUNC, lib_url.ToCString()));
(...skipping 28 matching lines...) Expand all
2731 2728
2732 if (thread->top_exit_frame_info() == 0) { 2729 if (thread->top_exit_frame_info() == 0) {
2733 // There are no dart frames on the stack so it would be illegal to 2730 // There are no dart frames on the stack so it would be illegal to
2734 // throw an exception here. 2731 // throw an exception here.
2735 const String& message = String::Handle( 2732 const String& message = String::Handle(
2736 String::New("No Dart frames on stack, cannot throw exception")); 2733 String::New("No Dart frames on stack, cannot throw exception"));
2737 return ApiError::New(message); 2734 return ApiError::New(message);
2738 } 2735 }
2739 // Unwind all the API scopes till the exit frame before throwing an 2736 // Unwind all the API scopes till the exit frame before throwing an
2740 // exception. 2737 // exception.
2741 ApiState* state = isolate->api_state();
2742 ASSERT(state != NULL);
2743 const Instance* saved_exception; 2738 const Instance* saved_exception;
2744 { 2739 {
2745 NoSafepointScope no_safepoint; 2740 NoSafepointScope no_safepoint;
2746 RawInstance* raw_exception = exception.raw(); 2741 RawInstance* raw_exception = exception.raw();
2747 state->UnwindScopes(thread->top_exit_frame_info()); 2742 thread->UnwindScopes(thread->top_exit_frame_info());
2748 saved_exception = &Instance::Handle(raw_exception); 2743 saved_exception = &Instance::Handle(raw_exception);
2749 } 2744 }
2750 Exceptions::Throw(thread, *saved_exception); 2745 Exceptions::Throw(thread, *saved_exception);
2751 const String& message = String::Handle( 2746 const String& message = String::Handle(
2752 String::New("Exception was not thrown, internal error")); 2747 String::New("Exception was not thrown, internal error"));
2753 return ApiError::New(message); 2748 return ApiError::New(message);
2754 } 2749 }
2755 2750
2756 // TODO(sgjesse): value should always be smaller then 0xff. Add error handling. 2751 // TODO(sgjesse): value should always be smaller then 0xff. Add error handling.
2757 #define GET_LIST_ELEMENT_AS_BYTES(type, obj, native_array, offset, length) \ 2752 #define GET_LIST_ELEMENT_AS_BYTES(type, obj, native_array, offset, length) \
2758 const type& array = type::Cast(obj); \ 2753 const type& array = type::Cast(obj); \
2759 if (Utils::RangeCheck(offset, length, array.Length())) { \ 2754 if (Utils::RangeCheck(offset, length, array.Length())) { \
2760 Object& element = Object::Handle(Z); \ 2755 Object& element = Object::Handle(Z); \
2761 for (int i = 0; i < length; i++) { \ 2756 for (int i = 0; i < length; i++) { \
2762 element = array.At(offset + i); \ 2757 element = array.At(offset + i); \
2763 if (!element.IsInteger()) { \ 2758 if (!element.IsInteger()) { \
2764 return Api::NewHandle(I, \ 2759 return Api::NewHandle(T, \
2765 ThrowArgumentError("List contains non-int elements")); \ 2760 ThrowArgumentError("List contains non-int elements")); \
2766 \ 2761 \
2767 } \ 2762 } \
2768 const Integer& integer = Integer::Cast(element); \ 2763 const Integer& integer = Integer::Cast(element); \
2769 native_array[i] = static_cast<uint8_t>(integer.AsInt64Value() & 0xff); \ 2764 native_array[i] = static_cast<uint8_t>(integer.AsInt64Value() & 0xff); \
2770 ASSERT(integer.AsInt64Value() <= 0xff); \ 2765 ASSERT(integer.AsInt64Value() <= 0xff); \
2771 } \ 2766 } \
2772 return Api::Success(); \ 2767 return Api::Success(); \
2773 } \ 2768 } \
2774 return Api::NewError("Invalid length passed in to access array elements"); \ 2769 return Api::NewError("Invalid length passed in to access array elements"); \
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
2859 Object& result = Object::Handle(Z); 2854 Object& result = Object::Handle(Z);
2860 Integer& intobj = Integer::Handle(Z); 2855 Integer& intobj = Integer::Handle(Z);
2861 const Array& args = Array::Handle(Z, Array::New(kNumArgs)); 2856 const Array& args = Array::Handle(Z, Array::New(kNumArgs));
2862 args.SetAt(0, instance); // Set up the receiver as the first argument. 2857 args.SetAt(0, instance); // Set up the receiver as the first argument.
2863 for (int i = 0; i < length; i++) { 2858 for (int i = 0; i < length; i++) {
2864 HANDLESCOPE(T); 2859 HANDLESCOPE(T);
2865 intobj = Integer::New(offset + i); 2860 intobj = Integer::New(offset + i);
2866 args.SetAt(1, intobj); 2861 args.SetAt(1, intobj);
2867 result = DartEntry::InvokeFunction(function, args); 2862 result = DartEntry::InvokeFunction(function, args);
2868 if (result.IsError()) { 2863 if (result.IsError()) {
2869 return Api::NewHandle(I, result.raw()); 2864 return Api::NewHandle(T, result.raw());
2870 } 2865 }
2871 if (!result.IsInteger()) { 2866 if (!result.IsInteger()) {
2872 return Api::NewError("%s expects the argument 'list' to be " 2867 return Api::NewError("%s expects the argument 'list' to be "
2873 "a List of int", CURRENT_FUNC); 2868 "a List of int", CURRENT_FUNC);
2874 } 2869 }
2875 const Integer& integer_result = Integer::Cast(result); 2870 const Integer& integer_result = Integer::Cast(result);
2876 ASSERT(integer_result.AsInt64Value() <= 0xff); 2871 ASSERT(integer_result.AsInt64Value() <= 0xff);
2877 // TODO(hpayer): value should always be smaller then 0xff. Add error 2872 // TODO(hpayer): value should always be smaller then 0xff. Add error
2878 // handling. 2873 // handling.
2879 native_array[i] = 2874 native_array[i] =
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
2948 const Array& args = Array::Handle(Z, Array::New(kNumArgs)); 2943 const Array& args = Array::Handle(Z, Array::New(kNumArgs));
2949 args.SetAt(0, instance); // Set up the receiver as the first argument. 2944 args.SetAt(0, instance); // Set up the receiver as the first argument.
2950 for (int i = 0; i < length; i++) { 2945 for (int i = 0; i < length; i++) {
2951 indexobj = Integer::New(offset + i); 2946 indexobj = Integer::New(offset + i);
2952 valueobj = Integer::New(native_array[i]); 2947 valueobj = Integer::New(native_array[i]);
2953 args.SetAt(1, indexobj); 2948 args.SetAt(1, indexobj);
2954 args.SetAt(2, valueobj); 2949 args.SetAt(2, valueobj);
2955 const Object& result = Object::Handle(Z, 2950 const Object& result = Object::Handle(Z,
2956 DartEntry::InvokeFunction(function, args)); 2951 DartEntry::InvokeFunction(function, args));
2957 if (result.IsError()) { 2952 if (result.IsError()) {
2958 return Api::NewHandle(I, result.raw()); 2953 return Api::NewHandle(T, result.raw());
2959 } 2954 }
2960 } 2955 }
2961 return Api::Success(); 2956 return Api::Success();
2962 } 2957 }
2963 } 2958 }
2964 return Api::NewError("Object does not implement the 'List' interface"); 2959 return Api::NewError("Object does not implement the 'List' interface");
2965 } 2960 }
2966 2961
2967 2962
2968 // --- Maps --- 2963 // --- Maps ---
2969 2964
2970 DART_EXPORT Dart_Handle Dart_MapGetAt(Dart_Handle map, Dart_Handle key) { 2965 DART_EXPORT Dart_Handle Dart_MapGetAt(Dart_Handle map, Dart_Handle key) {
2971 DARTSCOPE(Thread::Current()); 2966 DARTSCOPE(Thread::Current());
2972 CHECK_CALLBACK_STATE(T); 2967 CHECK_CALLBACK_STATE(T);
2973 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(map)); 2968 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(map));
2974 const Instance& instance = Instance::Handle(Z, GetMapInstance(Z, obj)); 2969 const Instance& instance = Instance::Handle(Z, GetMapInstance(Z, obj));
2975 if (!instance.IsNull()) { 2970 if (!instance.IsNull()) {
2976 const Object& key_obj = Object::Handle(Api::UnwrapHandle(key)); 2971 const Object& key_obj = Object::Handle(Api::UnwrapHandle(key));
2977 if (!(key_obj.IsInstance() || key_obj.IsNull())) { 2972 if (!(key_obj.IsInstance() || key_obj.IsNull())) {
2978 return Api::NewError("Key is not an instance"); 2973 return Api::NewError("Key is not an instance");
2979 } 2974 }
2980 return Api::NewHandle(I, 2975 return Api::NewHandle(T,
2981 Send1Arg(instance, Symbols::IndexToken(), Instance::Cast(key_obj))); 2976 Send1Arg(instance, Symbols::IndexToken(), Instance::Cast(key_obj)));
2982 } 2977 }
2983 return Api::NewError("Object does not implement the 'Map' interface"); 2978 return Api::NewError("Object does not implement the 'Map' interface");
2984 } 2979 }
2985 2980
2986 2981
2987 DART_EXPORT Dart_Handle Dart_MapContainsKey(Dart_Handle map, Dart_Handle key) { 2982 DART_EXPORT Dart_Handle Dart_MapContainsKey(Dart_Handle map, Dart_Handle key) {
2988 DARTSCOPE(Thread::Current()); 2983 DARTSCOPE(Thread::Current());
2989 CHECK_CALLBACK_STATE(T); 2984 CHECK_CALLBACK_STATE(T);
2990 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(map)); 2985 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(map));
2991 const Instance& instance = Instance::Handle(Z, GetMapInstance(Z, obj)); 2986 const Instance& instance = Instance::Handle(Z, GetMapInstance(Z, obj));
2992 if (!instance.IsNull()) { 2987 if (!instance.IsNull()) {
2993 const Object& key_obj = Object::Handle(Z, Api::UnwrapHandle(key)); 2988 const Object& key_obj = Object::Handle(Z, Api::UnwrapHandle(key));
2994 if (!(key_obj.IsInstance() || key_obj.IsNull())) { 2989 if (!(key_obj.IsInstance() || key_obj.IsNull())) {
2995 return Api::NewError("Key is not an instance"); 2990 return Api::NewError("Key is not an instance");
2996 } 2991 }
2997 return Api::NewHandle(I, Send1Arg( 2992 return Api::NewHandle(T, Send1Arg(
2998 instance, 2993 instance,
2999 String::Handle(Z, String::New("containsKey")), 2994 String::Handle(Z, String::New("containsKey")),
3000 Instance::Cast(key_obj))); 2995 Instance::Cast(key_obj)));
3001 } 2996 }
3002 return Api::NewError("Object does not implement the 'Map' interface"); 2997 return Api::NewError("Object does not implement the 'Map' interface");
3003 } 2998 }
3004 2999
3005 3000
3006 DART_EXPORT Dart_Handle Dart_MapKeys(Dart_Handle map) { 3001 DART_EXPORT Dart_Handle Dart_MapKeys(Dart_Handle map) {
3007 DARTSCOPE(Thread::Current()); 3002 DARTSCOPE(Thread::Current());
3008 CHECK_CALLBACK_STATE(T); 3003 CHECK_CALLBACK_STATE(T);
3009 Object& obj = Object::Handle(Z, Api::UnwrapHandle(map)); 3004 Object& obj = Object::Handle(Z, Api::UnwrapHandle(map));
3010 Instance& instance = Instance::Handle(Z, GetMapInstance(Z, obj)); 3005 Instance& instance = Instance::Handle(Z, GetMapInstance(Z, obj));
3011 if (!instance.IsNull()) { 3006 if (!instance.IsNull()) {
3012 const Object& iterator = Object::Handle(Send0Arg( 3007 const Object& iterator = Object::Handle(Send0Arg(
3013 instance, String::Handle(Z, String::New("get:keys")))); 3008 instance, String::Handle(Z, String::New("get:keys"))));
3014 if (!iterator.IsInstance()) { 3009 if (!iterator.IsInstance()) {
3015 return Api::NewHandle(I, iterator.raw()); 3010 return Api::NewHandle(T, iterator.raw());
3016 } 3011 }
3017 return Api::NewHandle(I, Send0Arg( 3012 return Api::NewHandle(T, Send0Arg(
3018 Instance::Cast(iterator), 3013 Instance::Cast(iterator),
3019 String::Handle(String::New("toList")))); 3014 String::Handle(String::New("toList"))));
3020 } 3015 }
3021 return Api::NewError("Object does not implement the 'Map' interface"); 3016 return Api::NewError("Object does not implement the 'Map' interface");
3022 } 3017 }
3023 3018
3024 3019
3025 // --- Typed Data --- 3020 // --- Typed Data ---
3026 3021
3027 // Helper method to get the type of a TypedData object. 3022 // Helper method to get the type of a TypedData object.
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
3144 return ResolveConstructor(CURRENT_FUNC, 3139 return ResolveConstructor(CURRENT_FUNC,
3145 cls, 3140 cls,
3146 Symbols::ByteData(), 3141 Symbols::ByteData(),
3147 constructor_name, 3142 constructor_name,
3148 num_args); 3143 num_args);
3149 } 3144 }
3150 3145
3151 3146
3152 static Dart_Handle NewByteData(Thread* thread, intptr_t length) { 3147 static Dart_Handle NewByteData(Thread* thread, intptr_t length) {
3153 CHECK_LENGTH(length, TypedData::MaxElements(kTypedDataInt8ArrayCid)); 3148 CHECK_LENGTH(length, TypedData::MaxElements(kTypedDataInt8ArrayCid));
3154 Isolate* isolate = thread->isolate();
3155 Zone* zone = thread->zone(); 3149 Zone* zone = thread->zone();
3156 Object& result = Object::Handle(zone); 3150 Object& result = Object::Handle(zone);
3157 result = GetByteDataConstructor(thread, Symbols::ByteDataDot(), 1); 3151 result = GetByteDataConstructor(thread, Symbols::ByteDataDot(), 1);
3158 ASSERT(!result.IsNull()); 3152 ASSERT(!result.IsNull());
3159 ASSERT(result.IsFunction()); 3153 ASSERT(result.IsFunction());
3160 const Function& factory = Function::Cast(result); 3154 const Function& factory = Function::Cast(result);
3161 ASSERT(!factory.IsGenerativeConstructor()); 3155 ASSERT(!factory.IsGenerativeConstructor());
3162 3156
3163 // Create the argument list. 3157 // Create the argument list.
3164 const Array& args = Array::Handle(zone, Array::New(2)); 3158 const Array& args = Array::Handle(zone, Array::New(2));
3165 // Factories get type arguments. 3159 // Factories get type arguments.
3166 args.SetAt(0, Object::null_type_arguments()); 3160 args.SetAt(0, Object::null_type_arguments());
3167 args.SetAt(1, Smi::Handle(zone, Smi::New(length))); 3161 args.SetAt(1, Smi::Handle(zone, Smi::New(length)));
3168 3162
3169 // Invoke the constructor and return the new object. 3163 // Invoke the constructor and return the new object.
3170 result = DartEntry::InvokeFunction(factory, args); 3164 result = DartEntry::InvokeFunction(factory, args);
3171 ASSERT(result.IsInstance() || result.IsNull() || result.IsError()); 3165 ASSERT(result.IsInstance() || result.IsNull() || result.IsError());
3172 return Api::NewHandle(isolate, result.raw()); 3166 return Api::NewHandle(thread, result.raw());
3173 } 3167 }
3174 3168
3175 3169
3176 static Dart_Handle NewTypedData(Isolate* isolate, 3170 static Dart_Handle NewTypedData(Thread* thread,
3177 intptr_t cid, 3171 intptr_t cid,
3178 intptr_t length) { 3172 intptr_t length) {
3179 CHECK_LENGTH(length, TypedData::MaxElements(cid)); 3173 CHECK_LENGTH(length, TypedData::MaxElements(cid));
3180 return Api::NewHandle(isolate, TypedData::New(cid, length)); 3174 return Api::NewHandle(thread, TypedData::New(cid, length));
3181 } 3175 }
3182 3176
3183 3177
3184 static Dart_Handle NewExternalTypedData( 3178 static Dart_Handle NewExternalTypedData(
3185 Thread* thread, intptr_t cid, void* data, intptr_t length) { 3179 Thread* thread, intptr_t cid, void* data, intptr_t length) {
3186 CHECK_LENGTH(length, ExternalTypedData::MaxElements(cid)); 3180 CHECK_LENGTH(length, ExternalTypedData::MaxElements(cid));
3187 Isolate* isolate = thread->isolate(); 3181 Isolate* isolate = thread->isolate();
3188 Zone* zone = thread->zone(); 3182 Zone* zone = thread->zone();
3189 intptr_t bytes = length * ExternalTypedData::ElementSizeInBytes(cid); 3183 intptr_t bytes = length * ExternalTypedData::ElementSizeInBytes(cid);
3190 const ExternalTypedData& result = ExternalTypedData::Handle( 3184 const ExternalTypedData& result = ExternalTypedData::Handle(
3191 zone, 3185 zone,
3192 ExternalTypedData::New(cid, 3186 ExternalTypedData::New(cid,
3193 reinterpret_cast<uint8_t*>(data), 3187 reinterpret_cast<uint8_t*>(data),
3194 length, 3188 length,
3195 SpaceForExternal(isolate, bytes))); 3189 SpaceForExternal(isolate, bytes)));
3196 return Api::NewHandle(isolate, result.raw()); 3190 return Api::NewHandle(thread, result.raw());
3197 } 3191 }
3198 3192
3199 3193
3200 static Dart_Handle NewExternalByteData( 3194 static Dart_Handle NewExternalByteData(
3201 Thread* thread, void* data, intptr_t length) { 3195 Thread* thread, void* data, intptr_t length) {
3202 Zone* zone = thread->zone(); 3196 Zone* zone = thread->zone();
3203 Isolate* isolate = thread->isolate();
3204 Dart_Handle ext_data = NewExternalTypedData( 3197 Dart_Handle ext_data = NewExternalTypedData(
3205 thread, kExternalTypedDataUint8ArrayCid, data, length); 3198 thread, kExternalTypedDataUint8ArrayCid, data, length);
3206 if (::Dart_IsError(ext_data)) { 3199 if (::Dart_IsError(ext_data)) {
3207 return ext_data; 3200 return ext_data;
3208 } 3201 }
3209 Object& result = Object::Handle(zone); 3202 Object& result = Object::Handle(zone);
3210 result = GetByteDataConstructor(thread, Symbols::ByteDataDot_view(), 3); 3203 result = GetByteDataConstructor(thread, Symbols::ByteDataDot_view(), 3);
3211 ASSERT(!result.IsNull()); 3204 ASSERT(!result.IsNull());
3212 ASSERT(result.IsFunction()); 3205 ASSERT(result.IsFunction());
3213 const Function& factory = Function::Cast(result); 3206 const Function& factory = Function::Cast(result);
3214 ASSERT(!factory.IsGenerativeConstructor()); 3207 ASSERT(!factory.IsGenerativeConstructor());
3215 3208
3216 // Create the argument list. 3209 // Create the argument list.
3217 const intptr_t num_args = 3; 3210 const intptr_t num_args = 3;
3218 const Array& args = Array::Handle(zone, Array::New(num_args + 1)); 3211 const Array& args = Array::Handle(zone, Array::New(num_args + 1));
3219 // Factories get type arguments. 3212 // Factories get type arguments.
3220 args.SetAt(0, Object::null_type_arguments()); 3213 args.SetAt(0, Object::null_type_arguments());
3221 const ExternalTypedData& array = 3214 const ExternalTypedData& array =
3222 Api::UnwrapExternalTypedDataHandle(zone, ext_data); 3215 Api::UnwrapExternalTypedDataHandle(zone, ext_data);
3223 args.SetAt(1, array); 3216 args.SetAt(1, array);
3224 Smi& smi = Smi::Handle(zone); 3217 Smi& smi = Smi::Handle(zone);
3225 smi = Smi::New(0); 3218 smi = Smi::New(0);
3226 args.SetAt(2, smi); 3219 args.SetAt(2, smi);
3227 smi = Smi::New(length); 3220 smi = Smi::New(length);
3228 args.SetAt(3, smi); 3221 args.SetAt(3, smi);
3229 3222
3230 // Invoke the constructor and return the new object. 3223 // Invoke the constructor and return the new object.
3231 result = DartEntry::InvokeFunction(factory, args); 3224 result = DartEntry::InvokeFunction(factory, args);
3232 ASSERT(result.IsNull() || result.IsInstance() || result.IsError()); 3225 ASSERT(result.IsNull() || result.IsInstance() || result.IsError());
3233 return Api::NewHandle(isolate, result.raw()); 3226 return Api::NewHandle(thread, result.raw());
3234 } 3227 }
3235 3228
3236 3229
3237 DART_EXPORT Dart_Handle Dart_NewTypedData(Dart_TypedData_Type type, 3230 DART_EXPORT Dart_Handle Dart_NewTypedData(Dart_TypedData_Type type,
3238 intptr_t length) { 3231 intptr_t length) {
3239 DARTSCOPE(Thread::Current()); 3232 DARTSCOPE(Thread::Current());
3240 CHECK_CALLBACK_STATE(T); 3233 CHECK_CALLBACK_STATE(T);
3241 switch (type) { 3234 switch (type) {
3242 case Dart_TypedData_kByteData : 3235 case Dart_TypedData_kByteData :
3243 return NewByteData(T, length); 3236 return NewByteData(T, length);
3244 case Dart_TypedData_kInt8 : 3237 case Dart_TypedData_kInt8 :
3245 return NewTypedData(I, kTypedDataInt8ArrayCid, length); 3238 return NewTypedData(T, kTypedDataInt8ArrayCid, length);
3246 case Dart_TypedData_kUint8 : 3239 case Dart_TypedData_kUint8 :
3247 return NewTypedData(I, kTypedDataUint8ArrayCid, length); 3240 return NewTypedData(T, kTypedDataUint8ArrayCid, length);
3248 case Dart_TypedData_kUint8Clamped : 3241 case Dart_TypedData_kUint8Clamped :
3249 return NewTypedData(I, kTypedDataUint8ClampedArrayCid, length); 3242 return NewTypedData(T, kTypedDataUint8ClampedArrayCid, length);
3250 case Dart_TypedData_kInt16 : 3243 case Dart_TypedData_kInt16 :
3251 return NewTypedData(I, kTypedDataInt16ArrayCid, length); 3244 return NewTypedData(T, kTypedDataInt16ArrayCid, length);
3252 case Dart_TypedData_kUint16 : 3245 case Dart_TypedData_kUint16 :
3253 return NewTypedData(I, kTypedDataUint16ArrayCid, length); 3246 return NewTypedData(T, kTypedDataUint16ArrayCid, length);
3254 case Dart_TypedData_kInt32 : 3247 case Dart_TypedData_kInt32 :
3255 return NewTypedData(I, kTypedDataInt32ArrayCid, length); 3248 return NewTypedData(T, kTypedDataInt32ArrayCid, length);
3256 case Dart_TypedData_kUint32 : 3249 case Dart_TypedData_kUint32 :
3257 return NewTypedData(I, kTypedDataUint32ArrayCid, length); 3250 return NewTypedData(T, kTypedDataUint32ArrayCid, length);
3258 case Dart_TypedData_kInt64 : 3251 case Dart_TypedData_kInt64 :
3259 return NewTypedData(I, kTypedDataInt64ArrayCid, length); 3252 return NewTypedData(T, kTypedDataInt64ArrayCid, length);
3260 case Dart_TypedData_kUint64 : 3253 case Dart_TypedData_kUint64 :
3261 return NewTypedData(I, kTypedDataUint64ArrayCid, length); 3254 return NewTypedData(T, kTypedDataUint64ArrayCid, length);
3262 case Dart_TypedData_kFloat32 : 3255 case Dart_TypedData_kFloat32 :
3263 return NewTypedData(I, kTypedDataFloat32ArrayCid, length); 3256 return NewTypedData(T, kTypedDataFloat32ArrayCid, length);
3264 case Dart_TypedData_kFloat64 : 3257 case Dart_TypedData_kFloat64 :
3265 return NewTypedData(I, kTypedDataFloat64ArrayCid, length); 3258 return NewTypedData(T, kTypedDataFloat64ArrayCid, length);
3266 case Dart_TypedData_kFloat32x4: 3259 case Dart_TypedData_kFloat32x4:
3267 return NewTypedData(I, kTypedDataFloat32x4ArrayCid, length); 3260 return NewTypedData(T, kTypedDataFloat32x4ArrayCid, length);
3268 default: 3261 default:
3269 return Api::NewError("%s expects argument 'type' to be of 'TypedData'", 3262 return Api::NewError("%s expects argument 'type' to be of 'TypedData'",
3270 CURRENT_FUNC); 3263 CURRENT_FUNC);
3271 } 3264 }
3272 UNREACHABLE(); 3265 UNREACHABLE();
3273 return Api::Null(); 3266 return Api::Null();
3274 } 3267 }
3275 3268
3276 3269
3277 DART_EXPORT Dart_Handle Dart_NewExternalTypedData( 3270 DART_EXPORT Dart_Handle Dart_NewExternalTypedData(
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
3370 // Create the argument list. 3363 // Create the argument list.
3371 const Array& args = Array::Handle(Z, Array::New(2)); 3364 const Array& args = Array::Handle(Z, Array::New(2));
3372 // Factories get type arguments. 3365 // Factories get type arguments.
3373 args.SetAt(0, Object::null_type_arguments()); 3366 args.SetAt(0, Object::null_type_arguments());
3374 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(typed_data)); 3367 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(typed_data));
3375 args.SetAt(1, obj); 3368 args.SetAt(1, obj);
3376 3369
3377 // Invoke the factory constructor and return the new object. 3370 // Invoke the factory constructor and return the new object.
3378 result = DartEntry::InvokeFunction(factory, args); 3371 result = DartEntry::InvokeFunction(factory, args);
3379 ASSERT(result.IsInstance() || result.IsNull() || result.IsError()); 3372 ASSERT(result.IsInstance() || result.IsNull() || result.IsError());
3380 return Api::NewHandle(I, result.raw()); 3373 return Api::NewHandle(T, result.raw());
3381 } 3374 }
3382 3375
3383 3376
3384 // Structure to record acquired typed data for verification purposes. 3377 // Structure to record acquired typed data for verification purposes.
3385 class AcquiredData { 3378 class AcquiredData {
3386 public: 3379 public:
3387 AcquiredData(void* data, intptr_t size_in_bytes, bool copy) 3380 AcquiredData(void* data, intptr_t size_in_bytes, bool copy)
3388 : size_in_bytes_(size_in_bytes), data_(data), data_copy_(NULL) { 3381 : size_in_bytes_(size_in_bytes), data_(data), data_copy_(NULL) {
3389 if (copy) { 3382 if (copy) {
3390 data_copy_ = malloc(size_in_bytes_); 3383 data_copy_ = malloc(size_in_bytes_);
(...skipping 21 matching lines...) Expand all
3412 3405
3413 DISALLOW_COPY_AND_ASSIGN(AcquiredData); 3406 DISALLOW_COPY_AND_ASSIGN(AcquiredData);
3414 }; 3407 };
3415 3408
3416 3409
3417 DART_EXPORT Dart_Handle Dart_TypedDataAcquireData(Dart_Handle object, 3410 DART_EXPORT Dart_Handle Dart_TypedDataAcquireData(Dart_Handle object,
3418 Dart_TypedData_Type* type, 3411 Dart_TypedData_Type* type,
3419 void** data, 3412 void** data,
3420 intptr_t* len) { 3413 intptr_t* len) {
3421 DARTSCOPE(Thread::Current()); 3414 DARTSCOPE(Thread::Current());
3415 Isolate* I = T->isolate();
3422 intptr_t class_id = Api::ClassId(object); 3416 intptr_t class_id = Api::ClassId(object);
3423 if (!RawObject::IsExternalTypedDataClassId(class_id) && 3417 if (!RawObject::IsExternalTypedDataClassId(class_id) &&
3424 !RawObject::IsTypedDataViewClassId(class_id) && 3418 !RawObject::IsTypedDataViewClassId(class_id) &&
3425 !RawObject::IsTypedDataClassId(class_id)) { 3419 !RawObject::IsTypedDataClassId(class_id)) {
3426 RETURN_TYPE_ERROR(Z, object, 'TypedData'); 3420 RETURN_TYPE_ERROR(Z, object, 'TypedData');
3427 } 3421 }
3428 if (type == NULL) { 3422 if (type == NULL) {
3429 RETURN_NULL_ERROR(type); 3423 RETURN_NULL_ERROR(type);
3430 } 3424 }
3431 if (data == NULL) { 3425 if (data == NULL) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
3501 data_tmp = ad->GetData(); 3495 data_tmp = ad->GetData();
3502 } 3496 }
3503 *data = data_tmp; 3497 *data = data_tmp;
3504 *len = length; 3498 *len = length;
3505 return Api::Success(); 3499 return Api::Success();
3506 } 3500 }
3507 3501
3508 3502
3509 DART_EXPORT Dart_Handle Dart_TypedDataReleaseData(Dart_Handle object) { 3503 DART_EXPORT Dart_Handle Dart_TypedDataReleaseData(Dart_Handle object) {
3510 DARTSCOPE(Thread::Current()); 3504 DARTSCOPE(Thread::Current());
3505 Isolate* I = T->isolate();
3511 intptr_t class_id = Api::ClassId(object); 3506 intptr_t class_id = Api::ClassId(object);
3512 if (!RawObject::IsExternalTypedDataClassId(class_id) && 3507 if (!RawObject::IsExternalTypedDataClassId(class_id) &&
3513 !RawObject::IsTypedDataViewClassId(class_id) && 3508 !RawObject::IsTypedDataViewClassId(class_id) &&
3514 !RawObject::IsTypedDataClassId(class_id)) { 3509 !RawObject::IsTypedDataClassId(class_id)) {
3515 RETURN_TYPE_ERROR(Z, object, 'TypedData'); 3510 RETURN_TYPE_ERROR(Z, object, 'TypedData');
3516 } 3511 }
3517 if (!RawObject::IsExternalTypedDataClassId(class_id)) { 3512 if (!RawObject::IsExternalTypedDataClassId(class_id)) {
3518 T->DecrementNoSafepointScopeDepth(); 3513 T->DecrementNoSafepointScopeDepth();
3519 END_NO_CALLBACK_SCOPE(T); 3514 END_NO_CALLBACK_SCOPE(T);
3520 } 3515 }
(...skipping 16 matching lines...) Expand all
3537 Thread* thread = Thread::Current(); 3532 Thread* thread = Thread::Current();
3538 Zone* zone = thread->zone(); 3533 Zone* zone = thread->zone();
3539 Isolate* isolate = thread->isolate(); 3534 Isolate* isolate = thread->isolate();
3540 CHECK_ISOLATE(isolate); 3535 CHECK_ISOLATE(isolate);
3541 intptr_t class_id = Api::ClassId(object); 3536 intptr_t class_id = Api::ClassId(object);
3542 if (class_id != kByteBufferCid) { 3537 if (class_id != kByteBufferCid) {
3543 RETURN_TYPE_ERROR(zone, object, 'ByteBuffer'); 3538 RETURN_TYPE_ERROR(zone, object, 'ByteBuffer');
3544 } 3539 }
3545 const Instance& instance = Api::UnwrapInstanceHandle(zone, object); 3540 const Instance& instance = Api::UnwrapInstanceHandle(zone, object);
3546 ASSERT(!instance.IsNull()); 3541 ASSERT(!instance.IsNull());
3547 return Api::NewHandle(isolate, ByteBuffer::Data(instance)); 3542 return Api::NewHandle(thread, ByteBuffer::Data(instance));
3548 } 3543 }
3549 3544
3550 3545
3551 // --- Invoking Constructors, Methods, and Field accessors --- 3546 // --- Invoking Constructors, Methods, and Field accessors ---
3552 3547
3553 static RawObject* ResolveConstructor(const char* current_func, 3548 static RawObject* ResolveConstructor(const char* current_func,
3554 const Class& cls, 3549 const Class& cls,
3555 const String& class_name, 3550 const String& class_name,
3556 const String& constr_name, 3551 const String& constr_name,
3557 int num_args) { 3552 int num_args) {
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
3648 3643
3649 // Resolve the constructor. 3644 // Resolve the constructor.
3650 String& constr_name = 3645 String& constr_name =
3651 String::Handle(String::Concat(base_constructor_name, dot_name)); 3646 String::Handle(String::Concat(base_constructor_name, dot_name));
3652 result = ResolveConstructor("Dart_New", 3647 result = ResolveConstructor("Dart_New",
3653 cls, 3648 cls,
3654 base_constructor_name, 3649 base_constructor_name,
3655 constr_name, 3650 constr_name,
3656 number_of_arguments); 3651 number_of_arguments);
3657 if (result.IsError()) { 3652 if (result.IsError()) {
3658 return Api::NewHandle(I, result.raw()); 3653 return Api::NewHandle(T, result.raw());
3659 } 3654 }
3660 ASSERT(result.IsFunction()); 3655 ASSERT(result.IsFunction());
3661 Function& constructor = Function::Handle(Z); 3656 Function& constructor = Function::Handle(Z);
3662 constructor ^= result.raw(); 3657 constructor ^= result.raw();
3663 3658
3664 Instance& new_object = Instance::Handle(Z); 3659 Instance& new_object = Instance::Handle(Z);
3665 if (constructor.IsRedirectingFactory()) { 3660 if (constructor.IsRedirectingFactory()) {
3666 ClassFinalizer::ResolveRedirectingFactory(cls, constructor); 3661 ClassFinalizer::ResolveRedirectingFactory(cls, constructor);
3667 Type& redirect_type = Type::Handle(constructor.RedirectionType()); 3662 Type& redirect_type = Type::Handle(constructor.RedirectionType());
3668 constructor = constructor.RedirectionTarget(); 3663 constructor = constructor.RedirectionTarget();
3669 if (constructor.IsNull()) { 3664 if (constructor.IsNull()) {
3670 ASSERT(redirect_type.IsMalformed()); 3665 ASSERT(redirect_type.IsMalformed());
3671 return Api::NewHandle(I, redirect_type.error()); 3666 return Api::NewHandle(T, redirect_type.error());
3672 } 3667 }
3673 3668
3674 if (!redirect_type.IsInstantiated()) { 3669 if (!redirect_type.IsInstantiated()) {
3675 // The type arguments of the redirection type are instantiated from the 3670 // The type arguments of the redirection type are instantiated from the
3676 // type arguments of the type argument. 3671 // type arguments of the type argument.
3677 Error& bound_error = Error::Handle(); 3672 Error& bound_error = Error::Handle();
3678 redirect_type ^= redirect_type.InstantiateFrom(type_arguments, 3673 redirect_type ^= redirect_type.InstantiateFrom(type_arguments,
3679 &bound_error); 3674 &bound_error);
3680 if (!bound_error.IsNull()) { 3675 if (!bound_error.IsNull()) {
3681 return Api::NewHandle(I, bound_error.raw()); 3676 return Api::NewHandle(T, bound_error.raw());
3682 } 3677 }
3683 redirect_type ^= redirect_type.Canonicalize(); 3678 redirect_type ^= redirect_type.Canonicalize();
3684 } 3679 }
3685 3680
3686 type_obj = redirect_type.raw(); 3681 type_obj = redirect_type.raw();
3687 type_arguments = redirect_type.arguments(); 3682 type_arguments = redirect_type.arguments();
3688 3683
3689 cls = type_obj.type_class(); 3684 cls = type_obj.type_class();
3690 } 3685 }
3691 if (constructor.IsGenerativeConstructor()) { 3686 if (constructor.IsGenerativeConstructor()) {
(...skipping 18 matching lines...) Expand all
3710 args.SetAt(arg_index++, Smi::Handle(Z, Smi::New(Function::kCtorPhaseAll))); 3705 args.SetAt(arg_index++, Smi::Handle(Z, Smi::New(Function::kCtorPhaseAll)));
3711 } else { 3706 } else {
3712 // Factories get type arguments. 3707 // Factories get type arguments.
3713 args.SetAt(arg_index++, type_arguments); 3708 args.SetAt(arg_index++, type_arguments);
3714 } 3709 }
3715 Object& argument = Object::Handle(Z); 3710 Object& argument = Object::Handle(Z);
3716 for (int i = 0; i < number_of_arguments; i++) { 3711 for (int i = 0; i < number_of_arguments; i++) {
3717 argument = Api::UnwrapHandle(arguments[i]); 3712 argument = Api::UnwrapHandle(arguments[i]);
3718 if (!argument.IsNull() && !argument.IsInstance()) { 3713 if (!argument.IsNull() && !argument.IsInstance()) {
3719 if (argument.IsError()) { 3714 if (argument.IsError()) {
3720 return Api::NewHandle(I, argument.raw()); 3715 return Api::NewHandle(T, argument.raw());
3721 } else { 3716 } else {
3722 return Api::NewError( 3717 return Api::NewError(
3723 "%s expects arguments[%d] to be an Instance handle.", 3718 "%s expects arguments[%d] to be an Instance handle.",
3724 CURRENT_FUNC, i); 3719 CURRENT_FUNC, i);
3725 } 3720 }
3726 } 3721 }
3727 args.SetAt(arg_index++, argument); 3722 args.SetAt(arg_index++, argument);
3728 } 3723 }
3729 3724
3730 // Invoke the constructor and return the new object. 3725 // Invoke the constructor and return the new object.
3731 result = DartEntry::InvokeFunction(constructor, args); 3726 result = DartEntry::InvokeFunction(constructor, args);
3732 if (result.IsError()) { 3727 if (result.IsError()) {
3733 return Api::NewHandle(I, result.raw()); 3728 return Api::NewHandle(T, result.raw());
3734 } 3729 }
3735 3730
3736 if (constructor.IsGenerativeConstructor()) { 3731 if (constructor.IsGenerativeConstructor()) {
3737 ASSERT(result.IsNull()); 3732 ASSERT(result.IsNull());
3738 } else { 3733 } else {
3739 ASSERT(result.IsNull() || result.IsInstance()); 3734 ASSERT(result.IsNull() || result.IsInstance());
3740 new_object ^= result.raw(); 3735 new_object ^= result.raw();
3741 } 3736 }
3742 return Api::NewHandle(I, new_object.raw()); 3737 return Api::NewHandle(T, new_object.raw());
3743 } 3738 }
3744 3739
3745 3740
3746 static RawInstance* AllocateObject(Thread* thread, const Class& cls) { 3741 static RawInstance* AllocateObject(Thread* thread, const Class& cls) {
3747 if (!cls.is_fields_marked_nullable()) { 3742 if (!cls.is_fields_marked_nullable()) {
3748 // Mark all fields as nullable. 3743 // Mark all fields as nullable.
3749 Zone* zone = thread->zone(); 3744 Zone* zone = thread->zone();
3750 Class& iterate_cls = Class::Handle(zone, cls.raw()); 3745 Class& iterate_cls = Class::Handle(zone, cls.raw());
3751 Field& field = Field::Handle(zone); 3746 Field& field = Field::Handle(zone);
3752 Array& fields = Array::Handle(zone); 3747 Array& fields = Array::Handle(zone);
(...skipping 28 matching lines...) Expand all
3781 } 3776 }
3782 const Class& cls = Class::Handle(Z, type_obj.type_class()); 3777 const Class& cls = Class::Handle(Z, type_obj.type_class());
3783 #if defined(DEBUG) 3778 #if defined(DEBUG)
3784 if (!cls.is_allocated() && Dart::IsRunningPrecompiledCode()) { 3779 if (!cls.is_allocated() && Dart::IsRunningPrecompiledCode()) {
3785 return Api::NewError("Precompilation dropped '%s'", cls.ToCString()); 3780 return Api::NewError("Precompilation dropped '%s'", cls.ToCString());
3786 } 3781 }
3787 #endif 3782 #endif
3788 const Error& error = Error::Handle(Z, cls.EnsureIsFinalized(T)); 3783 const Error& error = Error::Handle(Z, cls.EnsureIsFinalized(T));
3789 if (!error.IsNull()) { 3784 if (!error.IsNull()) {
3790 // An error occurred, return error object. 3785 // An error occurred, return error object.
3791 return Api::NewHandle(I, error.raw()); 3786 return Api::NewHandle(T, error.raw());
3792 } 3787 }
3793 return Api::NewHandle(I, AllocateObject(T, cls)); 3788 return Api::NewHandle(T, AllocateObject(T, cls));
3794 } 3789 }
3795 3790
3796 3791
3797 DART_EXPORT Dart_Handle Dart_AllocateWithNativeFields( 3792 DART_EXPORT Dart_Handle Dart_AllocateWithNativeFields(
3798 Dart_Handle type, 3793 Dart_Handle type,
3799 intptr_t num_native_fields, 3794 intptr_t num_native_fields,
3800 const intptr_t* native_fields) { 3795 const intptr_t* native_fields) {
3801 DARTSCOPE(Thread::Current()); 3796 DARTSCOPE(Thread::Current());
3802 CHECK_CALLBACK_STATE(T); 3797 CHECK_CALLBACK_STATE(T);
3803 3798
3804 const Type& type_obj = Api::UnwrapTypeHandle(Z, type); 3799 const Type& type_obj = Api::UnwrapTypeHandle(Z, type);
3805 // Get the class to instantiate. 3800 // Get the class to instantiate.
3806 if (type_obj.IsNull()) { 3801 if (type_obj.IsNull()) {
3807 RETURN_TYPE_ERROR(Z, type, Type); 3802 RETURN_TYPE_ERROR(Z, type, Type);
3808 } 3803 }
3809 if (native_fields == NULL) { 3804 if (native_fields == NULL) {
3810 RETURN_NULL_ERROR(native_fields); 3805 RETURN_NULL_ERROR(native_fields);
3811 } 3806 }
3812 const Class& cls = Class::Handle(Z, type_obj.type_class()); 3807 const Class& cls = Class::Handle(Z, type_obj.type_class());
3813 #if defined(DEBUG) 3808 #if defined(DEBUG)
3814 if (!cls.is_allocated() && Dart::IsRunningPrecompiledCode()) { 3809 if (!cls.is_allocated() && Dart::IsRunningPrecompiledCode()) {
3815 return Api::NewError("Precompilation dropped '%s'", cls.ToCString()); 3810 return Api::NewError("Precompilation dropped '%s'", cls.ToCString());
3816 } 3811 }
3817 #endif 3812 #endif
3818 const Error& error = Error::Handle(Z, cls.EnsureIsFinalized(T)); 3813 const Error& error = Error::Handle(Z, cls.EnsureIsFinalized(T));
3819 if (!error.IsNull()) { 3814 if (!error.IsNull()) {
3820 // An error occurred, return error object. 3815 // An error occurred, return error object.
3821 return Api::NewHandle(I, error.raw()); 3816 return Api::NewHandle(T, error.raw());
3822 } 3817 }
3823 if (num_native_fields != cls.num_native_fields()) { 3818 if (num_native_fields != cls.num_native_fields()) {
3824 return Api::NewError( 3819 return Api::NewError(
3825 "%s: invalid number of native fields %" Pd " passed in, expected %d", 3820 "%s: invalid number of native fields %" Pd " passed in, expected %d",
3826 CURRENT_FUNC, num_native_fields, cls.num_native_fields()); 3821 CURRENT_FUNC, num_native_fields, cls.num_native_fields());
3827 } 3822 }
3828 const Instance& instance = Instance::Handle(Z, AllocateObject(T, cls)); 3823 const Instance& instance = Instance::Handle(Z, AllocateObject(T, cls));
3829 instance.SetNativeFields(num_native_fields, native_fields); 3824 instance.SetNativeFields(num_native_fields, native_fields);
3830 return Api::NewHandle(I, instance.raw()); 3825 return Api::NewHandle(T, instance.raw());
3831 } 3826 }
3832 3827
3833 3828
3834 static Dart_Handle SetupArguments(Thread* thread, 3829 static Dart_Handle SetupArguments(Thread* thread,
3835 int num_args, 3830 int num_args,
3836 Dart_Handle* arguments, 3831 Dart_Handle* arguments,
3837 int extra_args, 3832 int extra_args,
3838 Array* args) { 3833 Array* args) {
3839 Zone* zone = thread->zone(); 3834 Zone* zone = thread->zone();
3840 Isolate* isolate = thread->isolate();
3841 // Check for malformed arguments in the arguments list. 3835 // Check for malformed arguments in the arguments list.
3842 *args = Array::New(num_args + extra_args); 3836 *args = Array::New(num_args + extra_args);
3843 Object& arg = Object::Handle(zone); 3837 Object& arg = Object::Handle(zone);
3844 for (int i = 0; i < num_args; i++) { 3838 for (int i = 0; i < num_args; i++) {
3845 arg = Api::UnwrapHandle(arguments[i]); 3839 arg = Api::UnwrapHandle(arguments[i]);
3846 if (!arg.IsNull() && !arg.IsInstance()) { 3840 if (!arg.IsNull() && !arg.IsInstance()) {
3847 *args = Array::null(); 3841 *args = Array::null();
3848 if (arg.IsError()) { 3842 if (arg.IsError()) {
3849 return Api::NewHandle(isolate, arg.raw()); 3843 return Api::NewHandle(thread, arg.raw());
3850 } else { 3844 } else {
3851 return Api::NewError( 3845 return Api::NewError(
3852 "%s expects arguments[%d] to be an Instance handle.", 3846 "%s expects arguments[%d] to be an Instance handle.",
3853 "Dart_Invoke", i); 3847 "Dart_Invoke", i);
3854 } 3848 }
3855 } 3849 }
3856 args->SetAt((i + extra_args), arg); 3850 args->SetAt((i + extra_args), arg);
3857 } 3851 }
3858 return Api::Success(); 3852 return Api::Success();
3859 } 3853 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
3917 Dart_Handle result; 3911 Dart_Handle result;
3918 Array& args = Array::Handle(Z); 3912 Array& args = Array::Handle(Z);
3919 result = SetupArguments(T, 3913 result = SetupArguments(T,
3920 number_of_arguments, arguments, extra_args, &args); 3914 number_of_arguments, arguments, extra_args, &args);
3921 if (!::Dart_IsError(result)) { 3915 if (!::Dart_IsError(result)) {
3922 args.SetAt(0, instance); 3916 args.SetAt(0, instance);
3923 args.SetAt(1, Smi::Handle(Z, Smi::New(Function::kCtorPhaseAll))); 3917 args.SetAt(1, Smi::Handle(Z, Smi::New(Function::kCtorPhaseAll)));
3924 const Object& retval = Object::Handle(Z, 3918 const Object& retval = Object::Handle(Z,
3925 DartEntry::InvokeFunction(constructor, args)); 3919 DartEntry::InvokeFunction(constructor, args));
3926 if (retval.IsError()) { 3920 if (retval.IsError()) {
3927 result = Api::NewHandle(I, retval.raw()); 3921 result = Api::NewHandle(T, retval.raw());
3928 } else { 3922 } else {
3929 result = Api::NewHandle(I, instance.raw()); 3923 result = Api::NewHandle(T, instance.raw());
3930 } 3924 }
3931 } 3925 }
3932 return result; 3926 return result;
3933 } 3927 }
3934 return Api::NewError( 3928 return Api::NewError(
3935 "%s expects argument 'name' to be a valid constructor.", 3929 "%s expects argument 'name' to be a valid constructor.",
3936 CURRENT_FUNC); 3930 CURRENT_FUNC);
3937 } 3931 }
3938 3932
3939 3933
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
3975 if (function.IsNull()) { 3969 if (function.IsNull()) {
3976 const String& cls_name = String::Handle(Z, cls.Name()); 3970 const String& cls_name = String::Handle(Z, cls.Name());
3977 return Api::NewError("%s: did not find static method '%s.%s'.", 3971 return Api::NewError("%s: did not find static method '%s.%s'.",
3978 CURRENT_FUNC, 3972 CURRENT_FUNC,
3979 cls_name.ToCString(), 3973 cls_name.ToCString(),
3980 function_name.ToCString()); 3974 function_name.ToCString());
3981 } 3975 }
3982 // Setup args and check for malformed arguments in the arguments list. 3976 // Setup args and check for malformed arguments in the arguments list.
3983 result = SetupArguments(T, number_of_arguments, arguments, 0, &args); 3977 result = SetupArguments(T, number_of_arguments, arguments, 0, &args);
3984 if (!::Dart_IsError(result)) { 3978 if (!::Dart_IsError(result)) {
3985 result = Api::NewHandle(I, DartEntry::InvokeFunction(function, args)); 3979 result = Api::NewHandle(T, DartEntry::InvokeFunction(function, args));
3986 } 3980 }
3987 return result; 3981 return result;
3988 } else if (obj.IsNull() || obj.IsInstance()) { 3982 } else if (obj.IsNull() || obj.IsInstance()) {
3989 // Since we have allocated an object it would mean that the type of the 3983 // Since we have allocated an object it would mean that the type of the
3990 // receiver is already resolved and finalized, hence it is not necessary 3984 // receiver is already resolved and finalized, hence it is not necessary
3991 // to check here. 3985 // to check here.
3992 Instance& instance = Instance::Handle(Z); 3986 Instance& instance = Instance::Handle(Z);
3993 instance ^= obj.raw(); 3987 instance ^= obj.raw();
3994 ArgumentsDescriptor args_desc( 3988 ArgumentsDescriptor args_desc(
3995 Array::Handle(Z, ArgumentsDescriptor::New(number_of_arguments + 1))); 3989 Array::Handle(Z, ArgumentsDescriptor::New(number_of_arguments + 1)));
3996 const Function& function = Function::Handle(Z, 3990 const Function& function = Function::Handle(Z,
3997 Resolver::ResolveDynamic(instance, function_name, args_desc)); 3991 Resolver::ResolveDynamic(instance, function_name, args_desc));
3998 if (function.IsNull()) { 3992 if (function.IsNull()) {
3999 // Setup args and check for malformed arguments in the arguments list. 3993 // Setup args and check for malformed arguments in the arguments list.
4000 result = SetupArguments(T, 3994 result = SetupArguments(T,
4001 number_of_arguments, 3995 number_of_arguments,
4002 arguments, 3996 arguments,
4003 1, 3997 1,
4004 &args); 3998 &args);
4005 if (!::Dart_IsError(result)) { 3999 if (!::Dart_IsError(result)) {
4006 args.SetAt(0, instance); 4000 args.SetAt(0, instance);
4007 const Array& args_descriptor = 4001 const Array& args_descriptor =
4008 Array::Handle(Z, ArgumentsDescriptor::New(args.Length())); 4002 Array::Handle(Z, ArgumentsDescriptor::New(args.Length()));
4009 result = Api::NewHandle(I, 4003 result = Api::NewHandle(T,
4010 DartEntry::InvokeNoSuchMethod(instance, 4004 DartEntry::InvokeNoSuchMethod(instance,
4011 function_name, 4005 function_name,
4012 args, 4006 args,
4013 args_descriptor)); 4007 args_descriptor));
4014 } 4008 }
4015 return result; 4009 return result;
4016 } 4010 }
4017 // Setup args and check for malformed arguments in the arguments list. 4011 // Setup args and check for malformed arguments in the arguments list.
4018 result = SetupArguments(T, number_of_arguments, arguments, 1, &args); 4012 result = SetupArguments(T, number_of_arguments, arguments, 1, &args);
4019 if (!::Dart_IsError(result)) { 4013 if (!::Dart_IsError(result)) {
4020 args.SetAt(0, instance); 4014 args.SetAt(0, instance);
4021 result = Api::NewHandle(I, DartEntry::InvokeFunction(function, args)); 4015 result = Api::NewHandle(T, DartEntry::InvokeFunction(function, args));
4022 } 4016 }
4023 return result; 4017 return result;
4024 } else if (obj.IsLibrary()) { 4018 } else if (obj.IsLibrary()) {
4025 // Check whether class finalization is needed. 4019 // Check whether class finalization is needed.
4026 const Library& lib = Library::Cast(obj); 4020 const Library& lib = Library::Cast(obj);
4027 4021
4028 // Check that the library is loaded. 4022 // Check that the library is loaded.
4029 if (!lib.Loaded()) { 4023 if (!lib.Loaded()) {
4030 return Api::NewError( 4024 return Api::NewError(
4031 "%s expects library argument 'target' to be loaded.", 4025 "%s expects library argument 'target' to be loaded.",
(...skipping 14 matching lines...) Expand all
4046 0, 4040 0,
4047 &error_message)) { 4041 &error_message)) {
4048 return Api::NewError("%s: wrong argument count for function '%s': %s.", 4042 return Api::NewError("%s: wrong argument count for function '%s': %s.",
4049 CURRENT_FUNC, 4043 CURRENT_FUNC,
4050 function_name.ToCString(), 4044 function_name.ToCString(),
4051 error_message.ToCString()); 4045 error_message.ToCString());
4052 } 4046 }
4053 // Setup args and check for malformed arguments in the arguments list. 4047 // Setup args and check for malformed arguments in the arguments list.
4054 result = SetupArguments(T, number_of_arguments, arguments, 0, &args); 4048 result = SetupArguments(T, number_of_arguments, arguments, 0, &args);
4055 if (!::Dart_IsError(result)) { 4049 if (!::Dart_IsError(result)) {
4056 result = Api::NewHandle(I, DartEntry::InvokeFunction(function, args)); 4050 result = Api::NewHandle(T, DartEntry::InvokeFunction(function, args));
4057 } 4051 }
4058 return result; 4052 return result;
4059 } else { 4053 } else {
4060 return Api::NewError( 4054 return Api::NewError(
4061 "%s expects argument 'target' to be an object, type, or library.", 4055 "%s expects argument 'target' to be an object, type, or library.",
4062 CURRENT_FUNC); 4056 CURRENT_FUNC);
4063 } 4057 }
4064 } 4058 }
4065 4059
4066 4060
(...skipping 17 matching lines...) Expand all
4084 Object& obj = Object::Handle(Z); 4078 Object& obj = Object::Handle(Z);
4085 args.SetAt(0, closure_obj); 4079 args.SetAt(0, closure_obj);
4086 for (int i = 0; i < number_of_arguments; i++) { 4080 for (int i = 0; i < number_of_arguments; i++) {
4087 obj = Api::UnwrapHandle(arguments[i]); 4081 obj = Api::UnwrapHandle(arguments[i]);
4088 if (!obj.IsNull() && !obj.IsInstance()) { 4082 if (!obj.IsNull() && !obj.IsInstance()) {
4089 RETURN_TYPE_ERROR(Z, arguments[i], Instance); 4083 RETURN_TYPE_ERROR(Z, arguments[i], Instance);
4090 } 4084 }
4091 args.SetAt(i + 1, obj); 4085 args.SetAt(i + 1, obj);
4092 } 4086 }
4093 // Now try to invoke the closure. 4087 // Now try to invoke the closure.
4094 return Api::NewHandle(I, DartEntry::InvokeClosure(args)); 4088 return Api::NewHandle(T, DartEntry::InvokeClosure(args));
4095 } 4089 }
4096 4090
4097 4091
4098 DART_EXPORT Dart_Handle Dart_GetField(Dart_Handle container, Dart_Handle name) { 4092 DART_EXPORT Dart_Handle Dart_GetField(Dart_Handle container, Dart_Handle name) {
4099 DARTSCOPE(Thread::Current()); 4093 DARTSCOPE(Thread::Current());
4100 CHECK_CALLBACK_STATE(T); 4094 CHECK_CALLBACK_STATE(T);
4101 4095
4102 const String& field_name = Api::UnwrapStringHandle(Z, name); 4096 const String& field_name = Api::UnwrapStringHandle(Z, name);
4103 if (field_name.IsNull()) { 4097 if (field_name.IsNull()) {
4104 RETURN_TYPE_ERROR(Z, name, String); 4098 RETURN_TYPE_ERROR(Z, name, String);
(...skipping 17 matching lines...) Expand all
4122 4116
4123 field = cls.LookupStaticField(field_name); 4117 field = cls.LookupStaticField(field_name);
4124 if (field.IsNull() || field.IsUninitialized()) { 4118 if (field.IsNull() || field.IsUninitialized()) {
4125 const String& getter_name = 4119 const String& getter_name =
4126 String::Handle(Z, Field::GetterName(field_name)); 4120 String::Handle(Z, Field::GetterName(field_name));
4127 getter = cls.LookupStaticFunctionAllowPrivate(getter_name); 4121 getter = cls.LookupStaticFunctionAllowPrivate(getter_name);
4128 } 4122 }
4129 4123
4130 if (!getter.IsNull()) { 4124 if (!getter.IsNull()) {
4131 // Invoke the getter and return the result. 4125 // Invoke the getter and return the result.
4132 return Api::NewHandle(I, 4126 return Api::NewHandle(T,
4133 DartEntry::InvokeFunction(getter, Object::empty_array())); 4127 DartEntry::InvokeFunction(getter, Object::empty_array()));
4134 } else if (!field.IsNull()) { 4128 } else if (!field.IsNull()) {
4135 return Api::NewHandle(I, field.StaticValue()); 4129 return Api::NewHandle(T, field.StaticValue());
4136 } else { 4130 } else {
4137 return Api::NewError("%s: did not find static field '%s'.", 4131 return Api::NewError("%s: did not find static field '%s'.",
4138 CURRENT_FUNC, field_name.ToCString()); 4132 CURRENT_FUNC, field_name.ToCString());
4139 } 4133 }
4140 4134
4141 } else if (obj.IsInstance()) { 4135 } else if (obj.IsInstance()) {
4142 // Every instance field has a getter Function. Try to find the 4136 // Every instance field has a getter Function. Try to find the
4143 // getter in any superclass and use that function to access the 4137 // getter in any superclass and use that function to access the
4144 // field. 4138 // field.
4145 const Instance& instance = Instance::Cast(obj); 4139 const Instance& instance = Instance::Cast(obj);
4146 Class& cls = Class::Handle(Z, instance.clazz()); 4140 Class& cls = Class::Handle(Z, instance.clazz());
4147 String& getter_name = String::Handle(Z, Field::GetterName(field_name)); 4141 String& getter_name = String::Handle(Z, Field::GetterName(field_name));
4148 while (!cls.IsNull()) { 4142 while (!cls.IsNull()) {
4149 getter = cls.LookupDynamicFunctionAllowPrivate(getter_name); 4143 getter = cls.LookupDynamicFunctionAllowPrivate(getter_name);
4150 if (!getter.IsNull()) { 4144 if (!getter.IsNull()) {
4151 break; 4145 break;
4152 } 4146 }
4153 cls = cls.SuperClass(); 4147 cls = cls.SuperClass();
4154 } 4148 }
4155 4149
4156 // Invoke the getter and return the result. 4150 // Invoke the getter and return the result.
4157 const int kNumArgs = 1; 4151 const int kNumArgs = 1;
4158 const Array& args = Array::Handle(Z, Array::New(kNumArgs)); 4152 const Array& args = Array::Handle(Z, Array::New(kNumArgs));
4159 args.SetAt(0, instance); 4153 args.SetAt(0, instance);
4160 if (getter.IsNull()) { 4154 if (getter.IsNull()) {
4161 const Array& args_descriptor = 4155 const Array& args_descriptor =
4162 Array::Handle(Z, ArgumentsDescriptor::New(args.Length())); 4156 Array::Handle(Z, ArgumentsDescriptor::New(args.Length()));
4163 return Api::NewHandle(I, 4157 return Api::NewHandle(T,
4164 DartEntry::InvokeNoSuchMethod(instance, 4158 DartEntry::InvokeNoSuchMethod(instance,
4165 getter_name, 4159 getter_name,
4166 args, 4160 args,
4167 args_descriptor)); 4161 args_descriptor));
4168 } 4162 }
4169 return Api::NewHandle(I, DartEntry::InvokeFunction(getter, args)); 4163 return Api::NewHandle(T, DartEntry::InvokeFunction(getter, args));
4170 4164
4171 } else if (obj.IsLibrary()) { 4165 } else if (obj.IsLibrary()) {
4172 // To access a top-level we may need to use the Field or the 4166 // To access a top-level we may need to use the Field or the
4173 // getter Function. The getter function may either be in the 4167 // getter Function. The getter function may either be in the
4174 // library or in the field's owner class, depending. 4168 // library or in the field's owner class, depending.
4175 const Library& lib = Library::Cast(obj); 4169 const Library& lib = Library::Cast(obj);
4176 // Check that the library is loaded. 4170 // Check that the library is loaded.
4177 if (!lib.Loaded()) { 4171 if (!lib.Loaded()) {
4178 return Api::NewError( 4172 return Api::NewError(
4179 "%s expects library argument 'container' to be loaded.", 4173 "%s expects library argument 'container' to be loaded.",
4180 CURRENT_FUNC); 4174 CURRENT_FUNC);
4181 } 4175 }
4182 field = lib.LookupFieldAllowPrivate(field_name); 4176 field = lib.LookupFieldAllowPrivate(field_name);
4183 if (field.IsNull()) { 4177 if (field.IsNull()) {
4184 // No field found and no ambiguity error. Check for a getter in the lib. 4178 // No field found and no ambiguity error. Check for a getter in the lib.
4185 const String& getter_name = 4179 const String& getter_name =
4186 String::Handle(Z, Field::GetterName(field_name)); 4180 String::Handle(Z, Field::GetterName(field_name));
4187 getter = lib.LookupFunctionAllowPrivate(getter_name); 4181 getter = lib.LookupFunctionAllowPrivate(getter_name);
4188 } else if (!field.IsNull() && field.IsUninitialized()) { 4182 } else if (!field.IsNull() && field.IsUninitialized()) {
4189 // A field was found. Check for a getter in the field's owner classs. 4183 // A field was found. Check for a getter in the field's owner classs.
4190 const Class& cls = Class::Handle(Z, field.owner()); 4184 const Class& cls = Class::Handle(Z, field.owner());
4191 const String& getter_name = String::Handle(Z, 4185 const String& getter_name = String::Handle(Z,
4192 Field::GetterName(field_name)); 4186 Field::GetterName(field_name));
4193 getter = cls.LookupStaticFunctionAllowPrivate(getter_name); 4187 getter = cls.LookupStaticFunctionAllowPrivate(getter_name);
4194 } 4188 }
4195 4189
4196 if (!getter.IsNull()) { 4190 if (!getter.IsNull()) {
4197 // Invoke the getter and return the result. 4191 // Invoke the getter and return the result.
4198 return Api::NewHandle(I, 4192 return Api::NewHandle(T,
4199 DartEntry::InvokeFunction(getter, Object::empty_array())); 4193 DartEntry::InvokeFunction(getter, Object::empty_array()));
4200 } 4194 }
4201 if (!field.IsNull()) { 4195 if (!field.IsNull()) {
4202 return Api::NewHandle(I, field.StaticValue()); 4196 return Api::NewHandle(T, field.StaticValue());
4203 } 4197 }
4204 return Api::NewError("%s: did not find top-level variable '%s'.", 4198 return Api::NewError("%s: did not find top-level variable '%s'.",
4205 CURRENT_FUNC, field_name.ToCString()); 4199 CURRENT_FUNC, field_name.ToCString());
4206 4200
4207 } else if (obj.IsError()) { 4201 } else if (obj.IsError()) {
4208 return container; 4202 return container;
4209 } else { 4203 } else {
4210 return Api::NewError( 4204 return Api::NewError(
4211 "%s expects argument 'container' to be an object, type, or library.", 4205 "%s expects argument 'container' to be an object, type, or library.",
4212 CURRENT_FUNC); 4206 CURRENT_FUNC);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
4257 } 4251 }
4258 4252
4259 if (!setter.IsNull()) { 4253 if (!setter.IsNull()) {
4260 // Invoke the setter and return the result. 4254 // Invoke the setter and return the result.
4261 const int kNumArgs = 1; 4255 const int kNumArgs = 1;
4262 const Array& args = Array::Handle(Z, Array::New(kNumArgs)); 4256 const Array& args = Array::Handle(Z, Array::New(kNumArgs));
4263 args.SetAt(0, value_instance); 4257 args.SetAt(0, value_instance);
4264 const Object& result = Object::Handle(Z, 4258 const Object& result = Object::Handle(Z,
4265 DartEntry::InvokeFunction(setter, args)); 4259 DartEntry::InvokeFunction(setter, args));
4266 if (result.IsError()) { 4260 if (result.IsError()) {
4267 return Api::NewHandle(I, result.raw()); 4261 return Api::NewHandle(T, result.raw());
4268 } else { 4262 } else {
4269 return Api::Success(); 4263 return Api::Success();
4270 } 4264 }
4271 } else if (!field.IsNull()) { 4265 } else if (!field.IsNull()) {
4272 if (field.is_final()) { 4266 if (field.is_final()) {
4273 return Api::NewError("%s: cannot set final field '%s'.", 4267 return Api::NewError("%s: cannot set final field '%s'.",
4274 CURRENT_FUNC, field_name.ToCString()); 4268 CURRENT_FUNC, field_name.ToCString());
4275 } else { 4269 } else {
4276 field.SetStaticValue(value_instance); 4270 field.SetStaticValue(value_instance);
4277 return Api::Success(); 4271 return Api::Success();
(...skipping 24 matching lines...) Expand all
4302 } 4296 }
4303 4297
4304 // Invoke the setter and return the result. 4298 // Invoke the setter and return the result.
4305 const int kNumArgs = 2; 4299 const int kNumArgs = 2;
4306 const Array& args = Array::Handle(Z, Array::New(kNumArgs)); 4300 const Array& args = Array::Handle(Z, Array::New(kNumArgs));
4307 args.SetAt(0, instance); 4301 args.SetAt(0, instance);
4308 args.SetAt(1, value_instance); 4302 args.SetAt(1, value_instance);
4309 if (setter.IsNull()) { 4303 if (setter.IsNull()) {
4310 const Array& args_descriptor = 4304 const Array& args_descriptor =
4311 Array::Handle(Z, ArgumentsDescriptor::New(args.Length())); 4305 Array::Handle(Z, ArgumentsDescriptor::New(args.Length()));
4312 return Api::NewHandle(I, DartEntry::InvokeNoSuchMethod(instance, 4306 return Api::NewHandle(T, DartEntry::InvokeNoSuchMethod(instance,
4313 setter_name, 4307 setter_name,
4314 args, 4308 args,
4315 args_descriptor)); 4309 args_descriptor));
4316 } 4310 }
4317 return Api::NewHandle(I, DartEntry::InvokeFunction(setter, args)); 4311 return Api::NewHandle(T, DartEntry::InvokeFunction(setter, args));
4318 4312
4319 } else if (obj.IsLibrary()) { 4313 } else if (obj.IsLibrary()) {
4320 // To access a top-level we may need to use the Field or the 4314 // To access a top-level we may need to use the Field or the
4321 // setter Function. The setter function may either be in the 4315 // setter Function. The setter function may either be in the
4322 // library or in the field's owner class, depending. 4316 // library or in the field's owner class, depending.
4323 const Library& lib = Library::Cast(obj); 4317 const Library& lib = Library::Cast(obj);
4324 // Check that the library is loaded. 4318 // Check that the library is loaded.
4325 if (!lib.Loaded()) { 4319 if (!lib.Loaded()) {
4326 return Api::NewError( 4320 return Api::NewError(
4327 "%s expects library argument 'container' to be loaded.", 4321 "%s expects library argument 'container' to be loaded.",
4328 CURRENT_FUNC); 4322 CURRENT_FUNC);
4329 } 4323 }
4330 field = lib.LookupFieldAllowPrivate(field_name); 4324 field = lib.LookupFieldAllowPrivate(field_name);
4331 if (field.IsNull()) { 4325 if (field.IsNull()) {
4332 const String& setter_name = 4326 const String& setter_name =
4333 String::Handle(Z, Field::SetterName(field_name)); 4327 String::Handle(Z, Field::SetterName(field_name));
4334 setter ^= lib.LookupFunctionAllowPrivate(setter_name); 4328 setter ^= lib.LookupFunctionAllowPrivate(setter_name);
4335 } 4329 }
4336 4330
4337 if (!setter.IsNull()) { 4331 if (!setter.IsNull()) {
4338 // Invoke the setter and return the result. 4332 // Invoke the setter and return the result.
4339 const int kNumArgs = 1; 4333 const int kNumArgs = 1;
4340 const Array& args = Array::Handle(Z, Array::New(kNumArgs)); 4334 const Array& args = Array::Handle(Z, Array::New(kNumArgs));
4341 args.SetAt(0, value_instance); 4335 args.SetAt(0, value_instance);
4342 const Object& result = 4336 const Object& result =
4343 Object::Handle(Z, DartEntry::InvokeFunction(setter, args)); 4337 Object::Handle(Z, DartEntry::InvokeFunction(setter, args));
4344 if (result.IsError()) { 4338 if (result.IsError()) {
4345 return Api::NewHandle(I, result.raw()); 4339 return Api::NewHandle(T, result.raw());
4346 } 4340 }
4347 return Api::Success(); 4341 return Api::Success();
4348 } 4342 }
4349 if (!field.IsNull()) { 4343 if (!field.IsNull()) {
4350 if (field.is_final()) { 4344 if (field.is_final()) {
4351 return Api::NewError("%s: cannot set final top-level variable '%s'.", 4345 return Api::NewError("%s: cannot set final top-level variable '%s'.",
4352 CURRENT_FUNC, field_name.ToCString()); 4346 CURRENT_FUNC, field_name.ToCString());
4353 } 4347 }
4354 field.SetStaticValue(value_instance); 4348 field.SetStaticValue(value_instance);
4355 return Api::Success(); 4349 return Api::Success();
(...skipping 25 matching lines...) Expand all
4381 } 4375 }
4382 } 4376 }
4383 if (thread->top_exit_frame_info() == 0) { 4377 if (thread->top_exit_frame_info() == 0) {
4384 // There are no dart frames on the stack so it would be illegal to 4378 // There are no dart frames on the stack so it would be illegal to
4385 // throw an exception here. 4379 // throw an exception here.
4386 return Api::NewError("No Dart frames on stack, cannot throw exception"); 4380 return Api::NewError("No Dart frames on stack, cannot throw exception");
4387 } 4381 }
4388 4382
4389 // Unwind all the API scopes till the exit frame before throwing an 4383 // Unwind all the API scopes till the exit frame before throwing an
4390 // exception. 4384 // exception.
4391 ApiState* state = isolate->api_state();
4392 ASSERT(state != NULL);
4393 const Instance* saved_exception; 4385 const Instance* saved_exception;
4394 { 4386 {
4395 NoSafepointScope no_safepoint; 4387 NoSafepointScope no_safepoint;
4396 RawInstance* raw_exception = 4388 RawInstance* raw_exception =
4397 Api::UnwrapInstanceHandle(zone, exception).raw(); 4389 Api::UnwrapInstanceHandle(zone, exception).raw();
4398 state->UnwindScopes(thread->top_exit_frame_info()); 4390 thread->UnwindScopes(thread->top_exit_frame_info());
4399 saved_exception = &Instance::Handle(raw_exception); 4391 saved_exception = &Instance::Handle(raw_exception);
4400 } 4392 }
4401 Exceptions::Throw(thread, *saved_exception); 4393 Exceptions::Throw(thread, *saved_exception);
4402 return Api::NewError("Exception was not thrown, internal error"); 4394 return Api::NewError("Exception was not thrown, internal error");
4403 } 4395 }
4404 4396
4405 4397
4406 DART_EXPORT Dart_Handle Dart_ReThrowException(Dart_Handle exception, 4398 DART_EXPORT Dart_Handle Dart_ReThrowException(Dart_Handle exception,
4407 Dart_Handle stacktrace) { 4399 Dart_Handle stacktrace) {
4408 Thread* thread = Thread::Current(); 4400 Thread* thread = Thread::Current();
(...skipping 12 matching lines...) Expand all
4421 } 4413 }
4422 } 4414 }
4423 if (thread->top_exit_frame_info() == 0) { 4415 if (thread->top_exit_frame_info() == 0) {
4424 // There are no dart frames on the stack so it would be illegal to 4416 // There are no dart frames on the stack so it would be illegal to
4425 // throw an exception here. 4417 // throw an exception here.
4426 return Api::NewError("No Dart frames on stack, cannot throw exception"); 4418 return Api::NewError("No Dart frames on stack, cannot throw exception");
4427 } 4419 }
4428 4420
4429 // Unwind all the API scopes till the exit frame before throwing an 4421 // Unwind all the API scopes till the exit frame before throwing an
4430 // exception. 4422 // exception.
4431 ApiState* state = isolate->api_state();
4432 ASSERT(state != NULL);
4433 const Instance* saved_exception; 4423 const Instance* saved_exception;
4434 const Stacktrace* saved_stacktrace; 4424 const Stacktrace* saved_stacktrace;
4435 { 4425 {
4436 NoSafepointScope no_safepoint; 4426 NoSafepointScope no_safepoint;
4437 RawInstance* raw_exception = 4427 RawInstance* raw_exception =
4438 Api::UnwrapInstanceHandle(zone, exception).raw(); 4428 Api::UnwrapInstanceHandle(zone, exception).raw();
4439 RawStacktrace* raw_stacktrace = 4429 RawStacktrace* raw_stacktrace =
4440 Api::UnwrapStacktraceHandle(zone, stacktrace).raw(); 4430 Api::UnwrapStacktraceHandle(zone, stacktrace).raw();
4441 state->UnwindScopes(thread->top_exit_frame_info()); 4431 thread->UnwindScopes(thread->top_exit_frame_info());
4442 saved_exception = &Instance::Handle(raw_exception); 4432 saved_exception = &Instance::Handle(raw_exception);
4443 saved_stacktrace = &Stacktrace::Handle(raw_stacktrace); 4433 saved_stacktrace = &Stacktrace::Handle(raw_stacktrace);
4444 } 4434 }
4445 Exceptions::ReThrow(thread, *saved_exception, *saved_stacktrace); 4435 Exceptions::ReThrow(thread, *saved_exception, *saved_stacktrace);
4446 return Api::NewError("Exception was not re thrown, internal error"); 4436 return Api::NewError("Exception was not re thrown, internal error");
4447 } 4437 }
4448 4438
4449 4439
4450 // --- Native fields and functions --- 4440 // --- Native fields and functions ---
4451 4441
(...skipping 15 matching lines...) Expand all
4467 } 4457 }
4468 CHECK_CALLBACK_STATE(T); 4458 CHECK_CALLBACK_STATE(T);
4469 4459
4470 String& cls_symbol = String::Handle(Z, Symbols::New(cls_name)); 4460 String& cls_symbol = String::Handle(Z, Symbols::New(cls_name));
4471 const Class& cls = Class::Handle(Z, 4461 const Class& cls = Class::Handle(Z,
4472 Class::NewNativeWrapper(lib, cls_symbol, field_count)); 4462 Class::NewNativeWrapper(lib, cls_symbol, field_count));
4473 if (cls.IsNull()) { 4463 if (cls.IsNull()) {
4474 return Api::NewError( 4464 return Api::NewError(
4475 "Unable to create native wrapper class : already exists"); 4465 "Unable to create native wrapper class : already exists");
4476 } 4466 }
4477 return Api::NewHandle(I, cls.RareType()); 4467 return Api::NewHandle(T, cls.RareType());
4478 } 4468 }
4479 4469
4480 4470
4481 DART_EXPORT Dart_Handle Dart_GetNativeInstanceFieldCount(Dart_Handle obj, 4471 DART_EXPORT Dart_Handle Dart_GetNativeInstanceFieldCount(Dart_Handle obj,
4482 int* count) { 4472 int* count) {
4483 Thread* thread = Thread::Current(); 4473 Thread* thread = Thread::Current();
4484 CHECK_ISOLATE(thread->isolate()); 4474 CHECK_ISOLATE(thread->isolate());
4485 ReusableObjectHandleScope reused_obj_handle(thread); 4475 ReusableObjectHandleScope reused_obj_handle(thread);
4486 const Instance& instance = Api::UnwrapInstanceHandle(reused_obj_handle, obj); 4476 const Instance& instance = Api::UnwrapInstanceHandle(reused_obj_handle, obj);
4487 if (instance.IsNull()) { 4477 if (instance.IsNull()) {
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
4647 native_value->as_native_fields.num_fields, 4637 native_value->as_native_fields.num_fields,
4648 native_value->as_native_fields.values, 4638 native_value->as_native_fields.values,
4649 CURRENT_FUNC); 4639 CURRENT_FUNC);
4650 if (result != Api::Success()) { 4640 if (result != Api::Success()) {
4651 return result; 4641 return result;
4652 } 4642 }
4653 break; 4643 break;
4654 } 4644 }
4655 4645
4656 case Dart_NativeArgument_kInstance: { 4646 case Dart_NativeArgument_kInstance: {
4657 Isolate* isolate = arguments->thread()->isolate(); 4647 ASSERT(arguments->thread() == Thread::Current());
4658 ASSERT(isolate == Isolate::Current()); 4648 ASSERT(arguments->thread()->api_top_scope() != NULL);
4659 ASSERT(isolate->api_state() && 4649 native_value->as_instance = Api::NewHandle(
4660 isolate->api_state()->top_scope() != NULL); 4650 arguments->thread(), arguments->NativeArgAt(arg_index));
4661 native_value->as_instance =
4662 Api::NewHandle(isolate, arguments->NativeArgAt(arg_index));
4663 break; 4651 break;
4664 } 4652 }
4665 4653
4666 default: 4654 default:
4667 return Api::NewError("%s: invalid argument type %d.", 4655 return Api::NewError("%s: invalid argument type %d.",
4668 CURRENT_FUNC, arg_type); 4656 CURRENT_FUNC, arg_type);
4669 } 4657 }
4670 } 4658 }
4671 return Api::Success(); 4659 return Api::Success();
4672 } 4660 }
4673 4661
4674 4662
4675 DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, 4663 DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args,
4676 int index) { 4664 int index) {
4677 TRACE_API_CALL(CURRENT_FUNC); 4665 TRACE_API_CALL(CURRENT_FUNC);
4678 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); 4666 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
4679 if ((index < 0) || (index >= arguments->NativeArgCount())) { 4667 if ((index < 0) || (index >= arguments->NativeArgCount())) {
4680 return Api::NewError( 4668 return Api::NewError(
4681 "%s: argument 'index' out of range. Expected 0..%d but saw %d.", 4669 "%s: argument 'index' out of range. Expected 0..%d but saw %d.",
4682 CURRENT_FUNC, arguments->NativeArgCount() - 1, index); 4670 CURRENT_FUNC, arguments->NativeArgCount() - 1, index);
4683 } 4671 }
4684 return Api::NewHandle(arguments->thread()->isolate(), 4672 return Api::NewHandle(arguments->thread(),
4685 arguments->NativeArgAt(index)); 4673 arguments->NativeArgAt(index));
4686 } 4674 }
4687 4675
4688 4676
4689 DART_EXPORT int Dart_GetNativeArgumentCount(Dart_NativeArguments args) { 4677 DART_EXPORT int Dart_GetNativeArgumentCount(Dart_NativeArguments args) {
4690 TRACE_API_CALL(CURRENT_FUNC); 4678 TRACE_API_CALL(CURRENT_FUNC);
4691 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); 4679 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
4692 return arguments->NativeArgCount(); 4680 return arguments->NativeArgCount();
4693 } 4681 }
4694 4682
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
4824 } 4812 }
4825 4813
4826 4814
4827 // --- Environment --- 4815 // --- Environment ---
4828 RawString* Api::CallEnvironmentCallback(Thread* thread, const String& name) { 4816 RawString* Api::CallEnvironmentCallback(Thread* thread, const String& name) {
4829 Isolate* isolate = thread->isolate(); 4817 Isolate* isolate = thread->isolate();
4830 Scope api_scope(thread); 4818 Scope api_scope(thread);
4831 Dart_EnvironmentCallback callback = isolate->environment_callback(); 4819 Dart_EnvironmentCallback callback = isolate->environment_callback();
4832 String& result = String::Handle(thread->zone()); 4820 String& result = String::Handle(thread->zone());
4833 if (callback != NULL) { 4821 if (callback != NULL) {
4834 Dart_Handle response = callback(Api::NewHandle(isolate, name.raw())); 4822 Dart_Handle response = callback(Api::NewHandle(thread, name.raw()));
4835 if (::Dart_IsString(response)) { 4823 if (::Dart_IsString(response)) {
4836 result ^= Api::UnwrapHandle(response); 4824 result ^= Api::UnwrapHandle(response);
4837 } else if (::Dart_IsError(response)) { 4825 } else if (::Dart_IsError(response)) {
4838 const Object& error = Object::Handle( 4826 const Object& error = Object::Handle(
4839 thread->zone(), Api::UnwrapHandle(response)); 4827 thread->zone(), Api::UnwrapHandle(response));
4840 Exceptions::ThrowArgumentError( 4828 Exceptions::ThrowArgumentError(
4841 String::Handle(String::New(Error::Cast(error).ToErrorCString()))); 4829 String::Handle(String::New(Error::Cast(error).ToErrorCString())));
4842 } else if (!::Dart_IsNull(response)) { 4830 } else if (!::Dart_IsNull(response)) {
4843 // At this point everything except null are invalid environment values. 4831 // At this point everything except null are invalid environment values.
4844 Exceptions::ThrowArgumentError( 4832 Exceptions::ThrowArgumentError(
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
4918 Dart_Handle* result) { 4906 Dart_Handle* result) {
4919 bool update_lib_status = (script.kind() == RawScript::kScriptTag || 4907 bool update_lib_status = (script.kind() == RawScript::kScriptTag ||
4920 script.kind() == RawScript::kLibraryTag); 4908 script.kind() == RawScript::kLibraryTag);
4921 if (update_lib_status) { 4909 if (update_lib_status) {
4922 lib.SetLoadInProgress(); 4910 lib.SetLoadInProgress();
4923 } 4911 }
4924 ASSERT(thread != NULL); 4912 ASSERT(thread != NULL);
4925 const Error& error = 4913 const Error& error =
4926 Error::Handle(thread->zone(), Compiler::Compile(lib, script)); 4914 Error::Handle(thread->zone(), Compiler::Compile(lib, script));
4927 if (error.IsNull()) { 4915 if (error.IsNull()) {
4928 *result = Api::NewHandle(thread->isolate(), lib.raw()); 4916 *result = Api::NewHandle(thread, lib.raw());
4929 } else { 4917 } else {
4930 *result = Api::NewHandle(thread->isolate(), error.raw()); 4918 *result = Api::NewHandle(thread, error.raw());
4931 // Compilation errors are not Dart instances, so just mark the library 4919 // Compilation errors are not Dart instances, so just mark the library
4932 // as having failed to load without providing an error instance. 4920 // as having failed to load without providing an error instance.
4933 lib.SetLoadError(Object::null_instance()); 4921 lib.SetLoadError(Object::null_instance());
4934 } 4922 }
4935 } 4923 }
4936 4924
4937 4925
4938 DART_EXPORT Dart_Handle Dart_LoadScript(Dart_Handle url, 4926 DART_EXPORT Dart_Handle Dart_LoadScript(Dart_Handle url,
4939 Dart_Handle source, 4927 Dart_Handle source,
4940 intptr_t line_offset, 4928 intptr_t line_offset,
4941 intptr_t column_offset) { 4929 intptr_t column_offset) {
4942 DARTSCOPE(Thread::Current()); 4930 DARTSCOPE(Thread::Current());
4931 Isolate* I = T->isolate();
4943 const String& url_str = Api::UnwrapStringHandle(Z, url); 4932 const String& url_str = Api::UnwrapStringHandle(Z, url);
4944 if (url_str.IsNull()) { 4933 if (url_str.IsNull()) {
4945 RETURN_TYPE_ERROR(Z, url, String); 4934 RETURN_TYPE_ERROR(Z, url, String);
4946 } 4935 }
4947 const String& source_str = Api::UnwrapStringHandle(Z, source); 4936 const String& source_str = Api::UnwrapStringHandle(Z, source);
4948 if (source_str.IsNull()) { 4937 if (source_str.IsNull()) {
4949 RETURN_TYPE_ERROR(Z, source, String); 4938 RETURN_TYPE_ERROR(Z, source, String);
4950 } 4939 }
4951 Library& library = Library::Handle(Z, I->object_store()->root_library()); 4940 Library& library = Library::Handle(Z, I->object_store()->root_library());
4952 if (!library.IsNull()) { 4941 if (!library.IsNull()) {
(...skipping 24 matching lines...) Expand all
4977 script.SetLocationOffset(line_offset, column_offset); 4966 script.SetLocationOffset(line_offset, column_offset);
4978 Dart_Handle result; 4967 Dart_Handle result;
4979 CompileSource(T, library, script, &result); 4968 CompileSource(T, library, script, &result);
4980 return result; 4969 return result;
4981 } 4970 }
4982 4971
4983 4972
4984 DART_EXPORT Dart_Handle Dart_LoadScriptFromSnapshot(const uint8_t* buffer, 4973 DART_EXPORT Dart_Handle Dart_LoadScriptFromSnapshot(const uint8_t* buffer,
4985 intptr_t buffer_len) { 4974 intptr_t buffer_len) {
4986 DARTSCOPE(Thread::Current()); 4975 DARTSCOPE(Thread::Current());
4976 Isolate* I = T->isolate();
4987 StackZone zone(T); 4977 StackZone zone(T);
4988 if (buffer == NULL) { 4978 if (buffer == NULL) {
4989 RETURN_NULL_ERROR(buffer); 4979 RETURN_NULL_ERROR(buffer);
4990 } 4980 }
4991 NoHeapGrowthControlScope no_growth_control; 4981 NoHeapGrowthControlScope no_growth_control;
4992 4982
4993 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 4983 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
4994 if (!snapshot->IsScriptSnapshot()) { 4984 if (!snapshot->IsScriptSnapshot()) {
4995 return Api::NewError("%s expects parameter 'buffer' to be a script type" 4985 return Api::NewError("%s expects parameter 'buffer' to be a script type"
4996 " snapshot.", CURRENT_FUNC); 4986 " snapshot.", CURRENT_FUNC);
4997 } 4987 }
4998 if (snapshot->length() != buffer_len) { 4988 if (snapshot->length() != buffer_len) {
4999 return Api::NewError("%s: 'buffer_len' of %" Pd " is not equal to %" Pd 4989 return Api::NewError("%s: 'buffer_len' of %" Pd " is not equal to %" Pd
5000 " which is the expected length in the snapshot.", 4990 " which is the expected length in the snapshot.",
5001 CURRENT_FUNC, buffer_len, snapshot->length()); 4991 CURRENT_FUNC, buffer_len, snapshot->length());
5002 } 4992 }
5003 Library& library = Library::Handle(Z, I->object_store()->root_library()); 4993 Library& library = Library::Handle(Z, I->object_store()->root_library());
5004 if (!library.IsNull()) { 4994 if (!library.IsNull()) {
5005 const String& library_url = String::Handle(Z, library.url()); 4995 const String& library_url = String::Handle(Z, library.url());
5006 return Api::NewError("%s: A script has already been loaded from '%s'.", 4996 return Api::NewError("%s: A script has already been loaded from '%s'.",
5007 CURRENT_FUNC, library_url.ToCString()); 4997 CURRENT_FUNC, library_url.ToCString());
5008 } 4998 }
5009 CHECK_CALLBACK_STATE(T); 4999 CHECK_CALLBACK_STATE(T);
5010 CHECK_COMPILATION_ALLOWED(I); 5000 CHECK_COMPILATION_ALLOWED(I);
5011 5001
5012 ASSERT(snapshot->kind() == Snapshot::kScript); 5002 ASSERT(snapshot->kind() == Snapshot::kScript);
5013 ScriptSnapshotReader reader(snapshot->content(), snapshot->length(), T); 5003 ScriptSnapshotReader reader(snapshot->content(), snapshot->length(), T);
5014 const Object& tmp = Object::Handle(Z, reader.ReadScriptSnapshot()); 5004 const Object& tmp = Object::Handle(Z, reader.ReadScriptSnapshot());
5015 if (tmp.IsError()) { 5005 if (tmp.IsError()) {
5016 return Api::NewHandle(I, tmp.raw()); 5006 return Api::NewHandle(T, tmp.raw());
5017 } 5007 }
5018 library ^= tmp.raw(); 5008 library ^= tmp.raw();
5019 library.set_debuggable(true); 5009 library.set_debuggable(true);
5020 I->object_store()->set_root_library(library); 5010 I->object_store()->set_root_library(library);
5021 return Api::NewHandle(I, library.raw()); 5011 return Api::NewHandle(T, library.raw());
5022 } 5012 }
5023 5013
5024 5014
5025 DART_EXPORT Dart_Handle Dart_RootLibrary() { 5015 DART_EXPORT Dart_Handle Dart_RootLibrary() {
5026 Isolate* isolate = Isolate::Current(); 5016 Thread* thread = Thread::Current();
5017 Isolate* isolate = thread->isolate();
5027 CHECK_ISOLATE(isolate); 5018 CHECK_ISOLATE(isolate);
5028 return Api::NewHandle(isolate, isolate->object_store()->root_library()); 5019 return Api::NewHandle(thread, isolate->object_store()->root_library());
5029 } 5020 }
5030 5021
5031 5022
5032 DART_EXPORT Dart_Handle Dart_SetRootLibrary(Dart_Handle library) { 5023 DART_EXPORT Dart_Handle Dart_SetRootLibrary(Dart_Handle library) {
5033 DARTSCOPE(Thread::Current()); 5024 DARTSCOPE(Thread::Current());
5034 const Library& lib = Api::UnwrapLibraryHandle(Z, library); 5025 const Library& lib = Api::UnwrapLibraryHandle(Z, library);
5035 if (lib.IsNull()) { 5026 if (lib.IsNull()) {
5036 RETURN_TYPE_ERROR(Z, library, Library); 5027 RETURN_TYPE_ERROR(Z, library, Library);
5037 } 5028 }
5038 Isolate* isolate = Isolate::Current(); 5029 Isolate* isolate = Isolate::Current();
(...skipping 13 matching lines...) Expand all
5052 if (cls_name.IsNull()) { 5043 if (cls_name.IsNull()) {
5053 RETURN_TYPE_ERROR(Z, class_name, String); 5044 RETURN_TYPE_ERROR(Z, class_name, String);
5054 } 5045 }
5055 const Class& cls = Class::Handle(Z, lib.LookupClassAllowPrivate(cls_name)); 5046 const Class& cls = Class::Handle(Z, lib.LookupClassAllowPrivate(cls_name));
5056 if (cls.IsNull()) { 5047 if (cls.IsNull()) {
5057 // TODO(turnidge): Return null or error in this case? 5048 // TODO(turnidge): Return null or error in this case?
5058 const String& lib_name = String::Handle(Z, lib.name()); 5049 const String& lib_name = String::Handle(Z, lib.name());
5059 return Api::NewError("Class '%s' not found in library '%s'.", 5050 return Api::NewError("Class '%s' not found in library '%s'.",
5060 cls_name.ToCString(), lib_name.ToCString()); 5051 cls_name.ToCString(), lib_name.ToCString());
5061 } 5052 }
5062 return Api::NewHandle(I, cls.RareType()); 5053 return Api::NewHandle(T, cls.RareType());
5063 } 5054 }
5064 5055
5065 5056
5066 DART_EXPORT Dart_Handle Dart_GetType(Dart_Handle library, 5057 DART_EXPORT Dart_Handle Dart_GetType(Dart_Handle library,
5067 Dart_Handle class_name, 5058 Dart_Handle class_name,
5068 intptr_t number_of_type_arguments, 5059 intptr_t number_of_type_arguments,
5069 Dart_Handle* type_arguments) { 5060 Dart_Handle* type_arguments) {
5070 DARTSCOPE(Thread::Current()); 5061 DARTSCOPE(Thread::Current());
5071 5062
5072 // Validate the input arguments. 5063 // Validate the input arguments.
(...skipping 14 matching lines...) Expand all
5087 if (cls.IsNull()) { 5078 if (cls.IsNull()) {
5088 const String& lib_name = String::Handle(Z, lib.name()); 5079 const String& lib_name = String::Handle(Z, lib.name());
5089 return Api::NewError("Type '%s' not found in library '%s'.", 5080 return Api::NewError("Type '%s' not found in library '%s'.",
5090 name_str.ToCString(), lib_name.ToCString()); 5081 name_str.ToCString(), lib_name.ToCString());
5091 } 5082 }
5092 if (cls.NumTypeArguments() == 0) { 5083 if (cls.NumTypeArguments() == 0) {
5093 if (number_of_type_arguments != 0) { 5084 if (number_of_type_arguments != 0) {
5094 return Api::NewError("Invalid number of type arguments specified, " 5085 return Api::NewError("Invalid number of type arguments specified, "
5095 "got %" Pd " expected 0", number_of_type_arguments); 5086 "got %" Pd " expected 0", number_of_type_arguments);
5096 } 5087 }
5097 return Api::NewHandle(I, Type::NewNonParameterizedType(cls)); 5088 return Api::NewHandle(T, Type::NewNonParameterizedType(cls));
5098 } 5089 }
5099 intptr_t num_expected_type_arguments = cls.NumTypeParameters(); 5090 intptr_t num_expected_type_arguments = cls.NumTypeParameters();
5100 TypeArguments& type_args_obj = TypeArguments::Handle(); 5091 TypeArguments& type_args_obj = TypeArguments::Handle();
5101 if (number_of_type_arguments > 0) { 5092 if (number_of_type_arguments > 0) {
5102 if (type_arguments == NULL) { 5093 if (type_arguments == NULL) {
5103 RETURN_NULL_ERROR(type_arguments); 5094 RETURN_NULL_ERROR(type_arguments);
5104 } 5095 }
5105 if (num_expected_type_arguments != number_of_type_arguments) { 5096 if (num_expected_type_arguments != number_of_type_arguments) {
5106 return Api::NewError("Invalid number of type arguments specified, " 5097 return Api::NewError("Invalid number of type arguments specified, "
5107 "got %" Pd " expected %" Pd, 5098 "got %" Pd " expected %" Pd,
(...skipping 17 matching lines...) Expand all
5125 type_arg ^= array.At(i); 5116 type_arg ^= array.At(i);
5126 type_args_obj.SetTypeAt(i, type_arg); 5117 type_args_obj.SetTypeAt(i, type_arg);
5127 } 5118 }
5128 } 5119 }
5129 5120
5130 // Construct the type object, canonicalize it and return. 5121 // Construct the type object, canonicalize it and return.
5131 Type& instantiated_type = Type::Handle( 5122 Type& instantiated_type = Type::Handle(
5132 Type::New(cls, type_args_obj, Scanner::kNoSourcePos)); 5123 Type::New(cls, type_args_obj, Scanner::kNoSourcePos));
5133 instantiated_type ^= ClassFinalizer::FinalizeType( 5124 instantiated_type ^= ClassFinalizer::FinalizeType(
5134 cls, instantiated_type, ClassFinalizer::kCanonicalize); 5125 cls, instantiated_type, ClassFinalizer::kCanonicalize);
5135 return Api::NewHandle(I, instantiated_type.raw()); 5126 return Api::NewHandle(T, instantiated_type.raw());
5136 } 5127 }
5137 5128
5138 5129
5139 DART_EXPORT Dart_Handle Dart_LibraryUrl(Dart_Handle library) { 5130 DART_EXPORT Dart_Handle Dart_LibraryUrl(Dart_Handle library) {
5140 DARTSCOPE(Thread::Current()); 5131 DARTSCOPE(Thread::Current());
5141 const Library& lib = Api::UnwrapLibraryHandle(Z, library); 5132 const Library& lib = Api::UnwrapLibraryHandle(Z, library);
5142 if (lib.IsNull()) { 5133 if (lib.IsNull()) {
5143 RETURN_TYPE_ERROR(Z, library, Library); 5134 RETURN_TYPE_ERROR(Z, library, Library);
5144 } 5135 }
5145 const String& url = String::Handle(Z, lib.url()); 5136 const String& url = String::Handle(Z, lib.url());
5146 ASSERT(!url.IsNull()); 5137 ASSERT(!url.IsNull());
5147 return Api::NewHandle(I, url.raw()); 5138 return Api::NewHandle(T, url.raw());
5148 } 5139 }
5149 5140
5150 5141
5151 DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url) { 5142 DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url) {
5152 DARTSCOPE(Thread::Current()); 5143 DARTSCOPE(Thread::Current());
5153 const String& url_str = Api::UnwrapStringHandle(Z, url); 5144 const String& url_str = Api::UnwrapStringHandle(Z, url);
5154 if (url_str.IsNull()) { 5145 if (url_str.IsNull()) {
5155 RETURN_TYPE_ERROR(Z, url, String); 5146 RETURN_TYPE_ERROR(Z, url, String);
5156 } 5147 }
5157 const Library& library = Library::Handle(Z, Library::LookupLibrary(url_str)); 5148 const Library& library = Library::Handle(Z, Library::LookupLibrary(url_str));
5158 if (library.IsNull()) { 5149 if (library.IsNull()) {
5159 return Api::NewError("%s: library '%s' not found.", 5150 return Api::NewError("%s: library '%s' not found.",
5160 CURRENT_FUNC, url_str.ToCString()); 5151 CURRENT_FUNC, url_str.ToCString());
5161 } else { 5152 } else {
5162 return Api::NewHandle(I, library.raw()); 5153 return Api::NewHandle(T, library.raw());
5163 } 5154 }
5164 } 5155 }
5165 5156
5166 5157
5167 DART_EXPORT Dart_Handle Dart_LibraryHandleError(Dart_Handle library_in, 5158 DART_EXPORT Dart_Handle Dart_LibraryHandleError(Dart_Handle library_in,
5168 Dart_Handle error_in) { 5159 Dart_Handle error_in) {
5169 DARTSCOPE(Thread::Current()); 5160 DARTSCOPE(Thread::Current());
5161 Isolate* I = T->isolate();
5170 5162
5171 const Library& lib = Api::UnwrapLibraryHandle(Z, library_in); 5163 const Library& lib = Api::UnwrapLibraryHandle(Z, library_in);
5172 if (lib.IsNull()) { 5164 if (lib.IsNull()) {
5173 RETURN_TYPE_ERROR(Z, library_in, Library); 5165 RETURN_TYPE_ERROR(Z, library_in, Library);
5174 } 5166 }
5175 const Instance& err = Api::UnwrapInstanceHandle(Z, error_in); 5167 const Instance& err = Api::UnwrapInstanceHandle(Z, error_in);
5176 if (err.IsNull()) { 5168 if (err.IsNull()) {
5177 RETURN_TYPE_ERROR(Z, error_in, Instance); 5169 RETURN_TYPE_ERROR(Z, error_in, Instance);
5178 } 5170 }
5179 CHECK_CALLBACK_STATE(T); 5171 CHECK_CALLBACK_STATE(T);
5180 5172
5181 const GrowableObjectArray& pending_deferred_loads = 5173 const GrowableObjectArray& pending_deferred_loads =
5182 GrowableObjectArray::Handle(Z, 5174 GrowableObjectArray::Handle(Z,
5183 I->object_store()->pending_deferred_loads()); 5175 I->object_store()->pending_deferred_loads());
5184 for (intptr_t i = 0; i < pending_deferred_loads.Length(); i++) { 5176 for (intptr_t i = 0; i < pending_deferred_loads.Length(); i++) {
5185 if (pending_deferred_loads.At(i) == lib.raw()) { 5177 if (pending_deferred_loads.At(i) == lib.raw()) {
5186 lib.SetLoadError(err); 5178 lib.SetLoadError(err);
5187 return Api::Null(); 5179 return Api::Null();
5188 } 5180 }
5189 } 5181 }
5190 return error_in; 5182 return error_in;
5191 } 5183 }
5192 5184
5193 5185
5194 DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle url, 5186 DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle url,
5195 Dart_Handle source, 5187 Dart_Handle source,
5196 intptr_t line_offset, 5188 intptr_t line_offset,
5197 intptr_t column_offset) { 5189 intptr_t column_offset) {
5198 DARTSCOPE(Thread::Current()); 5190 DARTSCOPE(Thread::Current());
5191 Isolate* I = T->isolate();
5199 const String& url_str = Api::UnwrapStringHandle(Z, url); 5192 const String& url_str = Api::UnwrapStringHandle(Z, url);
5200 if (url_str.IsNull()) { 5193 if (url_str.IsNull()) {
5201 RETURN_TYPE_ERROR(Z, url, String); 5194 RETURN_TYPE_ERROR(Z, url, String);
5202 } 5195 }
5203 const String& source_str = Api::UnwrapStringHandle(Z, source); 5196 const String& source_str = Api::UnwrapStringHandle(Z, source);
5204 if (source_str.IsNull()) { 5197 if (source_str.IsNull()) {
5205 RETURN_TYPE_ERROR(Z, source, String); 5198 RETURN_TYPE_ERROR(Z, source, String);
5206 } 5199 }
5207 if (line_offset < 0) { 5200 if (line_offset < 0) {
5208 return Api::NewError("%s: argument 'line_offset' must be positive number", 5201 return Api::NewError("%s: argument 'line_offset' must be positive number",
(...skipping 26 matching lines...) Expand all
5235 Dart_Handle result; 5228 Dart_Handle result;
5236 CompileSource(T, library, script, &result); 5229 CompileSource(T, library, script, &result);
5237 // Propagate the error out right now. 5230 // Propagate the error out right now.
5238 if (::Dart_IsError(result)) { 5231 if (::Dart_IsError(result)) {
5239 return result; 5232 return result;
5240 } 5233 }
5241 5234
5242 // If this is the dart:_builtin library, register it with the VM. 5235 // If this is the dart:_builtin library, register it with the VM.
5243 if (url_str.Equals("dart:_builtin")) { 5236 if (url_str.Equals("dart:_builtin")) {
5244 I->object_store()->set_builtin_library(library); 5237 I->object_store()->set_builtin_library(library);
5245 Dart_Handle state = Api::CheckAndFinalizePendingClasses(I); 5238 Dart_Handle state = Api::CheckAndFinalizePendingClasses(T);
5246 if (::Dart_IsError(state)) { 5239 if (::Dart_IsError(state)) {
5247 return state; 5240 return state;
5248 } 5241 }
5249 } 5242 }
5250 return result; 5243 return result;
5251 } 5244 }
5252 5245
5253 5246
5254 DART_EXPORT Dart_Handle Dart_LibraryImportLibrary(Dart_Handle library, 5247 DART_EXPORT Dart_Handle Dart_LibraryImportLibrary(Dart_Handle library,
5255 Dart_Handle import, 5248 Dart_Handle import,
5256 Dart_Handle prefix) { 5249 Dart_Handle prefix) {
5257 DARTSCOPE(Thread::Current()); 5250 DARTSCOPE(Thread::Current());
5251 Isolate* I = T->isolate();
5258 const Library& library_vm = Api::UnwrapLibraryHandle(Z, library); 5252 const Library& library_vm = Api::UnwrapLibraryHandle(Z, library);
5259 if (library_vm.IsNull()) { 5253 if (library_vm.IsNull()) {
5260 RETURN_TYPE_ERROR(Z, library, Library); 5254 RETURN_TYPE_ERROR(Z, library, Library);
5261 } 5255 }
5262 const Library& import_vm = Api::UnwrapLibraryHandle(Z, import); 5256 const Library& import_vm = Api::UnwrapLibraryHandle(Z, import);
5263 if (import_vm.IsNull()) { 5257 if (import_vm.IsNull()) {
5264 RETURN_TYPE_ERROR(Z, import, Library); 5258 RETURN_TYPE_ERROR(Z, import, Library);
5265 } 5259 }
5266 const Object& prefix_object = Object::Handle(Z, Api::UnwrapHandle(prefix)); 5260 const Object& prefix_object = Object::Handle(Z, Api::UnwrapHandle(prefix));
5267 const String& prefix_vm = prefix_object.IsNull() 5261 const String& prefix_vm = prefix_object.IsNull()
(...skipping 24 matching lines...) Expand all
5292 return Api::Success(); 5286 return Api::Success();
5293 } 5287 }
5294 5288
5295 5289
5296 DART_EXPORT Dart_Handle Dart_LoadSource(Dart_Handle library, 5290 DART_EXPORT Dart_Handle Dart_LoadSource(Dart_Handle library,
5297 Dart_Handle url, 5291 Dart_Handle url,
5298 Dart_Handle source, 5292 Dart_Handle source,
5299 intptr_t line_offset, 5293 intptr_t line_offset,
5300 intptr_t column_offset) { 5294 intptr_t column_offset) {
5301 DARTSCOPE(Thread::Current()); 5295 DARTSCOPE(Thread::Current());
5296 Isolate* I = T->isolate();
5302 const Library& lib = Api::UnwrapLibraryHandle(Z, library); 5297 const Library& lib = Api::UnwrapLibraryHandle(Z, library);
5303 if (lib.IsNull()) { 5298 if (lib.IsNull()) {
5304 RETURN_TYPE_ERROR(Z, library, Library); 5299 RETURN_TYPE_ERROR(Z, library, Library);
5305 } 5300 }
5306 const String& url_str = Api::UnwrapStringHandle(Z, url); 5301 const String& url_str = Api::UnwrapStringHandle(Z, url);
5307 if (url_str.IsNull()) { 5302 if (url_str.IsNull()) {
5308 RETURN_TYPE_ERROR(Z, url, String); 5303 RETURN_TYPE_ERROR(Z, url, String);
5309 } 5304 }
5310 const String& source_str = Api::UnwrapStringHandle(Z, source); 5305 const String& source_str = Api::UnwrapStringHandle(Z, source);
5311 if (source_str.IsNull()) { 5306 if (source_str.IsNull()) {
(...skipping 18 matching lines...) Expand all
5330 Dart_Handle result; 5325 Dart_Handle result;
5331 CompileSource(T, lib, script, &result); 5326 CompileSource(T, lib, script, &result);
5332 return result; 5327 return result;
5333 } 5328 }
5334 5329
5335 5330
5336 DART_EXPORT Dart_Handle Dart_LibraryLoadPatch(Dart_Handle library, 5331 DART_EXPORT Dart_Handle Dart_LibraryLoadPatch(Dart_Handle library,
5337 Dart_Handle url, 5332 Dart_Handle url,
5338 Dart_Handle patch_source) { 5333 Dart_Handle patch_source) {
5339 DARTSCOPE(Thread::Current()); 5334 DARTSCOPE(Thread::Current());
5335 Isolate* I = T->isolate();
5340 const Library& lib = Api::UnwrapLibraryHandle(Z, library); 5336 const Library& lib = Api::UnwrapLibraryHandle(Z, library);
5341 if (lib.IsNull()) { 5337 if (lib.IsNull()) {
5342 RETURN_TYPE_ERROR(Z, library, Library); 5338 RETURN_TYPE_ERROR(Z, library, Library);
5343 } 5339 }
5344 const String& url_str = Api::UnwrapStringHandle(Z, url); 5340 const String& url_str = Api::UnwrapStringHandle(Z, url);
5345 if (url_str.IsNull()) { 5341 if (url_str.IsNull()) {
5346 RETURN_TYPE_ERROR(Z, url, String); 5342 RETURN_TYPE_ERROR(Z, url, String);
5347 } 5343 }
5348 const String& source_str = Api::UnwrapStringHandle(Z, patch_source); 5344 const String& source_str = Api::UnwrapStringHandle(Z, patch_source);
5349 if (source_str.IsNull()) { 5345 if (source_str.IsNull()) {
5350 RETURN_TYPE_ERROR(Z, patch_source, String); 5346 RETURN_TYPE_ERROR(Z, patch_source, String);
5351 } 5347 }
5352 CHECK_CALLBACK_STATE(T); 5348 CHECK_CALLBACK_STATE(T);
5353 CHECK_COMPILATION_ALLOWED(I); 5349 CHECK_COMPILATION_ALLOWED(I);
5354 5350
5355 NoHeapGrowthControlScope no_growth_control; 5351 NoHeapGrowthControlScope no_growth_control;
5356 5352
5357 const Script& script = Script::Handle(Z, 5353 const Script& script = Script::Handle(Z,
5358 Script::New(url_str, source_str, RawScript::kPatchTag)); 5354 Script::New(url_str, source_str, RawScript::kPatchTag));
5359 Dart_Handle result; 5355 Dart_Handle result;
5360 CompileSource(T, lib, script, &result); 5356 CompileSource(T, lib, script, &result);
5361 return result; 5357 return result;
5362 } 5358 }
5363 5359
5364 5360
5365 // Finalizes classes and invokes Dart core library function that completes 5361 // Finalizes classes and invokes Dart core library function that completes
5366 // futures of loadLibrary calls (deferred library loading). 5362 // futures of loadLibrary calls (deferred library loading).
5367 DART_EXPORT Dart_Handle Dart_FinalizeLoading(bool complete_futures) { 5363 DART_EXPORT Dart_Handle Dart_FinalizeLoading(bool complete_futures) {
5368 DARTSCOPE(Thread::Current()); 5364 DARTSCOPE(Thread::Current());
5365 Isolate* I = T->isolate();
5369 CHECK_CALLBACK_STATE(T); 5366 CHECK_CALLBACK_STATE(T);
5370 5367
5371 I->DoneLoading(); 5368 I->DoneLoading();
5372 5369
5373 // TODO(hausner): move the remaining code below (finalization and 5370 // TODO(hausner): move the remaining code below (finalization and
5374 // invoing of _completeDeferredLoads) into Isolate::DoneLoading(). 5371 // invoing of _completeDeferredLoads) into Isolate::DoneLoading().
5375 5372
5376 // Finalize all classes if needed. 5373 // Finalize all classes if needed.
5377 Dart_Handle state = Api::CheckAndFinalizePendingClasses(I); 5374 Dart_Handle state = Api::CheckAndFinalizePendingClasses(T);
5378 if (::Dart_IsError(state)) { 5375 if (::Dart_IsError(state)) {
5379 return state; 5376 return state;
5380 } 5377 }
5381 5378
5382 // Now that the newly loaded classes are finalized, notify the debugger 5379 // Now that the newly loaded classes are finalized, notify the debugger
5383 // that new code has been loaded. If there are latent breakpoints in 5380 // that new code has been loaded. If there are latent breakpoints in
5384 // the new code, the debugger convert them to unresolved source breakpoints. 5381 // the new code, the debugger convert them to unresolved source breakpoints.
5385 // The code that completes the futures (invoked below) may call into the 5382 // The code that completes the futures (invoked below) may call into the
5386 // newly loaded code and trigger one of these breakpoints. 5383 // newly loaded code and trigger one of these breakpoints.
5387 I->debugger()->NotifyDoneLoading(); 5384 I->debugger()->NotifyDoneLoading();
(...skipping 11 matching lines...) Expand all
5399 String::Handle(Z, String::New("_completeDeferredLoads")); 5396 String::Handle(Z, String::New("_completeDeferredLoads"));
5400 const Function& function = Function::Handle(Z, 5397 const Function& function = Function::Handle(Z,
5401 corelib.LookupFunctionAllowPrivate(function_name)); 5398 corelib.LookupFunctionAllowPrivate(function_name));
5402 ASSERT(!function.IsNull()); 5399 ASSERT(!function.IsNull());
5403 const Array& args = Array::empty_array(); 5400 const Array& args = Array::empty_array();
5404 5401
5405 const Object& res = 5402 const Object& res =
5406 Object::Handle(Z, DartEntry::InvokeFunction(function, args)); 5403 Object::Handle(Z, DartEntry::InvokeFunction(function, args));
5407 I->object_store()->clear_pending_deferred_loads(); 5404 I->object_store()->clear_pending_deferred_loads();
5408 if (res.IsError() || res.IsUnhandledException()) { 5405 if (res.IsError() || res.IsUnhandledException()) {
5409 return Api::NewHandle(I, res.raw()); 5406 return Api::NewHandle(T, res.raw());
5410 } 5407 }
5411 } 5408 }
5412 return Api::Success(); 5409 return Api::Success();
5413 } 5410 }
5414 5411
5415 5412
5416 DART_EXPORT Dart_Handle Dart_SetNativeResolver( 5413 DART_EXPORT Dart_Handle Dart_SetNativeResolver(
5417 Dart_Handle library, 5414 Dart_Handle library,
5418 Dart_NativeEntryResolver resolver, 5415 Dart_NativeEntryResolver resolver,
5419 Dart_NativeEntrySymbol symbol) { 5416 Dart_NativeEntrySymbol symbol) {
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
5536 Service::SetEmbedderStreamCallbacks(listen_callback, cancel_callback); 5533 Service::SetEmbedderStreamCallbacks(listen_callback, cancel_callback);
5537 return Api::Success(); 5534 return Api::Success();
5538 } 5535 }
5539 5536
5540 5537
5541 DART_EXPORT Dart_Handle Dart_ServiceSendDataEvent(const char* stream_id, 5538 DART_EXPORT Dart_Handle Dart_ServiceSendDataEvent(const char* stream_id,
5542 const char* event_kind, 5539 const char* event_kind,
5543 const uint8_t* bytes, 5540 const uint8_t* bytes,
5544 intptr_t bytes_length) { 5541 intptr_t bytes_length) {
5545 DARTSCOPE(Thread::Current()); 5542 DARTSCOPE(Thread::Current());
5543 Isolate* I = T->isolate();
5546 if (stream_id == NULL) { 5544 if (stream_id == NULL) {
5547 RETURN_NULL_ERROR(stream_id); 5545 RETURN_NULL_ERROR(stream_id);
5548 } 5546 }
5549 if (event_kind == NULL) { 5547 if (event_kind == NULL) {
5550 RETURN_NULL_ERROR(event_kind); 5548 RETURN_NULL_ERROR(event_kind);
5551 } 5549 }
5552 if (bytes == NULL) { 5550 if (bytes == NULL) {
5553 RETURN_NULL_ERROR(bytes); 5551 RETURN_NULL_ERROR(bytes);
5554 } 5552 }
5555 if (bytes_length < 0) { 5553 if (bytes_length < 0) {
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
5863 } 5861 }
5864 5862
5865 5863
5866 DART_EXPORT Dart_Handle Dart_Precompile( 5864 DART_EXPORT Dart_Handle Dart_Precompile(
5867 Dart_QualifiedFunctionName entry_points[], 5865 Dart_QualifiedFunctionName entry_points[],
5868 bool reset_fields) { 5866 bool reset_fields) {
5869 DARTSCOPE(Thread::Current()); 5867 DARTSCOPE(Thread::Current());
5870 if (!FLAG_precompilation) { 5868 if (!FLAG_precompilation) {
5871 return Dart_NewApiError("Flag --precompilation was not specified."); 5869 return Dart_NewApiError("Flag --precompilation was not specified.");
5872 } 5870 }
5873 Dart_Handle result = Api::CheckAndFinalizePendingClasses(I); 5871 Dart_Handle result = Api::CheckAndFinalizePendingClasses(T);
5874 if (::Dart_IsError(result)) { 5872 if (::Dart_IsError(result)) {
5875 return result; 5873 return result;
5876 } 5874 }
5877 CHECK_CALLBACK_STATE(T); 5875 CHECK_CALLBACK_STATE(T);
5878 const Error& error = Error::Handle(Precompiler::CompileAll(entry_points, 5876 const Error& error = Error::Handle(Precompiler::CompileAll(entry_points,
5879 reset_fields)); 5877 reset_fields));
5880 if (!error.IsNull()) { 5878 if (!error.IsNull()) {
5881 return Api::NewHandle(I, error.raw()); 5879 return Api::NewHandle(T, error.raw());
5882 } 5880 }
5883 return Api::Success(); 5881 return Api::Success();
5884 } 5882 }
5885 5883
5886 5884
5887 DART_EXPORT Dart_Handle Dart_CreatePrecompiledSnapshot( 5885 DART_EXPORT Dart_Handle Dart_CreatePrecompiledSnapshot(
5888 uint8_t** vm_isolate_snapshot_buffer, 5886 uint8_t** vm_isolate_snapshot_buffer,
5889 intptr_t* vm_isolate_snapshot_size, 5887 intptr_t* vm_isolate_snapshot_size,
5890 uint8_t** isolate_snapshot_buffer, 5888 uint8_t** isolate_snapshot_buffer,
5891 intptr_t* isolate_snapshot_size, 5889 intptr_t* isolate_snapshot_size,
5892 uint8_t** instructions_snapshot_buffer, 5890 uint8_t** instructions_snapshot_buffer,
5893 intptr_t* instructions_snapshot_size) { 5891 intptr_t* instructions_snapshot_size) {
5894 ASSERT(FLAG_load_deferred_eagerly); 5892 ASSERT(FLAG_load_deferred_eagerly);
5895 DARTSCOPE(Thread::Current()); 5893 DARTSCOPE(Thread::Current());
5894 Isolate* I = T->isolate();
5896 if (I->compilation_allowed()) { 5895 if (I->compilation_allowed()) {
5897 return Dart_NewApiError("Isolate is not precompiled. " 5896 return Dart_NewApiError("Isolate is not precompiled. "
5898 "Did you forget to call Dart_Precompile?"); 5897 "Did you forget to call Dart_Precompile?");
5899 } 5898 }
5900 if (vm_isolate_snapshot_buffer == NULL) { 5899 if (vm_isolate_snapshot_buffer == NULL) {
5901 RETURN_NULL_ERROR(vm_isolate_snapshot_buffer); 5900 RETURN_NULL_ERROR(vm_isolate_snapshot_buffer);
5902 } 5901 }
5903 if (vm_isolate_snapshot_size == NULL) { 5902 if (vm_isolate_snapshot_size == NULL) {
5904 RETURN_NULL_ERROR(vm_isolate_snapshot_size); 5903 RETURN_NULL_ERROR(vm_isolate_snapshot_size);
5905 } 5904 }
5906 if (isolate_snapshot_buffer == NULL) { 5905 if (isolate_snapshot_buffer == NULL) {
5907 RETURN_NULL_ERROR(isolate_snapshot_buffer); 5906 RETURN_NULL_ERROR(isolate_snapshot_buffer);
5908 } 5907 }
5909 if (isolate_snapshot_size == NULL) { 5908 if (isolate_snapshot_size == NULL) {
5910 RETURN_NULL_ERROR(isolate_snapshot_size); 5909 RETURN_NULL_ERROR(isolate_snapshot_size);
5911 } 5910 }
5912 if (instructions_snapshot_buffer == NULL) { 5911 if (instructions_snapshot_buffer == NULL) {
5913 RETURN_NULL_ERROR(instructions_snapshot_buffer); 5912 RETURN_NULL_ERROR(instructions_snapshot_buffer);
5914 } 5913 }
5915 if (instructions_snapshot_size == NULL) { 5914 if (instructions_snapshot_size == NULL) {
5916 RETURN_NULL_ERROR(instructions_snapshot_size); 5915 RETURN_NULL_ERROR(instructions_snapshot_size);
5917 } 5916 }
5918 // Finalize all classes if needed. 5917 // Finalize all classes if needed.
5919 Dart_Handle state = Api::CheckAndFinalizePendingClasses(I); 5918 Dart_Handle state = Api::CheckAndFinalizePendingClasses(T);
5920 if (::Dart_IsError(state)) { 5919 if (::Dart_IsError(state)) {
5921 return state; 5920 return state;
5922 } 5921 }
5923 I->heap()->CollectAllGarbage(); 5922 I->heap()->CollectAllGarbage();
5924 PrecompiledSnapshotWriter writer(vm_isolate_snapshot_buffer, 5923 PrecompiledSnapshotWriter writer(vm_isolate_snapshot_buffer,
5925 isolate_snapshot_buffer, 5924 isolate_snapshot_buffer,
5926 instructions_snapshot_buffer, 5925 instructions_snapshot_buffer,
5927 ApiReallocate); 5926 ApiReallocate);
5928 writer.WriteFullSnapshot(); 5927 writer.WriteFullSnapshot();
5929 *vm_isolate_snapshot_size = writer.VmIsolateSnapshotSize(); 5928 *vm_isolate_snapshot_size = writer.VmIsolateSnapshotSize();
5930 *isolate_snapshot_size = writer.IsolateSnapshotSize(); 5929 *isolate_snapshot_size = writer.IsolateSnapshotSize();
5931 *instructions_snapshot_size = writer.InstructionsSnapshotSize(); 5930 *instructions_snapshot_size = writer.InstructionsSnapshotSize();
5932 5931
5933 return Api::Success(); 5932 return Api::Success();
5934 } 5933 }
5935 5934
5936 } // namespace dart 5935 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698