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

Side by Side Diff: runtime/bin/dartutils.cc

Issue 15689013: - Modify dart_api.h to be a proper C API. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/dartutils.h ('k') | runtime/bin/directory.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 "bin/dartutils.h" 5 #include "bin/dartutils.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 8
9 #include "platform/assert.h" 9 #include "platform/assert.h"
10 #include "platform/globals.h" 10 #include "platform/globals.h"
(...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 result = Dart_StringToCString(library_url, &library_url_string); 569 result = Dart_StringToCString(library_url, &library_url_string);
570 if (Dart_IsError(result)) { 570 if (Dart_IsError(result)) {
571 return result; 571 return result;
572 } 572 }
573 573
574 bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string); 574 bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string);
575 bool is_io_library = DartUtils::IsDartIOLibURL(library_url_string); 575 bool is_io_library = DartUtils::IsDartIOLibURL(library_url_string);
576 bool is_dart_extension_url = DartUtils::IsDartExtensionSchemeURL(url_string); 576 bool is_dart_extension_url = DartUtils::IsDartExtensionSchemeURL(url_string);
577 577
578 // Handle URI canonicalization requests. 578 // Handle URI canonicalization requests.
579 if (tag == kCanonicalizeUrl) { 579 if (tag == Dart_kCanonicalizeUrl) {
580 // If this is a Dart Scheme URL or 'part' of a io library 580 // If this is a Dart Scheme URL or 'part' of a io library
581 // then it is not modified as it will be handled internally. 581 // then it is not modified as it will be handled internally.
582 if (is_dart_scheme_url || is_io_library) { 582 if (is_dart_scheme_url || is_io_library) {
583 return url; 583 return url;
584 } 584 }
585 // Resolve the url within the context of the library's URL. 585 // Resolve the url within the context of the library's URL.
586 Dart_Handle builtin_lib = 586 Dart_Handle builtin_lib =
587 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); 587 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
588 Dart_Handle library_url = Dart_LibraryUrl(library); 588 Dart_Handle library_url = Dart_LibraryUrl(library);
589 if (Dart_IsError(library_url)) { 589 if (Dart_IsError(library_url)) {
590 return library_url; 590 return library_url;
591 } 591 }
592 return ResolveUri(library_url, url, builtin_lib); 592 return ResolveUri(library_url, url, builtin_lib);
593 } 593 }
594 594
595 // Handle 'import' of dart scheme URIs (i.e they start with 'dart:'). 595 // Handle 'import' of dart scheme URIs (i.e they start with 'dart:').
596 if (is_dart_scheme_url) { 596 if (is_dart_scheme_url) {
597 if (tag == kImportTag) { 597 if (tag == Dart_kImportTag) {
598 // Handle imports of other built-in libraries present in the SDK. 598 // Handle imports of other built-in libraries present in the SDK.
599 if (DartUtils::IsDartIOLibURL(url_string)) { 599 if (DartUtils::IsDartIOLibURL(url_string)) {
600 return Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); 600 return Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary);
601 } 601 }
602 return Dart_Error("Do not know how to load '%s'", url_string); 602 return Dart_Error("Do not know how to load '%s'", url_string);
603 } else { 603 } else {
604 ASSERT(tag == kSourceTag); 604 ASSERT(tag == Dart_kSourceTag);
605 return Dart_Error("Unable to load source '%s' ", url_string); 605 return Dart_Error("Unable to load source '%s' ", url_string);
606 } 606 }
607 } 607 }
608 608
609 // Handle 'part' of IO library. 609 // Handle 'part' of IO library.
610 if (is_io_library) { 610 if (is_io_library) {
611 if (tag == kSourceTag) { 611 if (tag == Dart_kSourceTag) {
612 return Dart_LoadSource( 612 return Dart_LoadSource(
613 library, url, Builtin::PartSource(Builtin::kIOLibrary, url_string)); 613 library, url, Builtin::PartSource(Builtin::kIOLibrary, url_string));
614 } else { 614 } else {
615 ASSERT(tag == kImportTag); 615 ASSERT(tag == Dart_kImportTag);
616 return Dart_Error("Unable to import '%s' ", url_string); 616 return Dart_Error("Unable to import '%s' ", url_string);
617 } 617 }
618 } 618 }
619 619
620 // Handle 'import' or 'part' requests for all other URIs. 620 // Handle 'import' or 'part' requests for all other URIs.
621 // Get the file path out of the url. 621 // Get the file path out of the url.
622 Dart_Handle builtin_lib = 622 Dart_Handle builtin_lib =
623 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); 623 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
624 Dart_Handle file_path = FilePathFromUri(url, builtin_lib); 624 Dart_Handle file_path = FilePathFromUri(url, builtin_lib);
625 if (Dart_IsError(file_path)) { 625 if (Dart_IsError(file_path)) {
626 return file_path; 626 return file_path;
627 } 627 }
628 Dart_StringToCString(file_path, &url_string); 628 Dart_StringToCString(file_path, &url_string);
629 if (is_dart_extension_url) { 629 if (is_dart_extension_url) {
630 if (tag != kImportTag) { 630 if (tag != Dart_kImportTag) {
631 return Dart_Error("Dart extensions must use import: '%s'", url_string); 631 return Dart_Error("Dart extensions must use import: '%s'", url_string);
632 } 632 }
633 return Extensions::LoadExtension(url_string, library); 633 return Extensions::LoadExtension(url_string, library);
634 } 634 }
635 result = DartUtils::LoadSource(NULL, 635 result = DartUtils::LoadSource(NULL,
636 library, 636 library,
637 url, 637 url,
638 tag, 638 tag,
639 url_string); 639 url_string);
640 return result; 640 return result;
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
771 source = DartUtils::ReadStringFromHttp(url_string); 771 source = DartUtils::ReadStringFromHttp(url_string);
772 } else { 772 } else {
773 // Read the file. 773 // Read the file.
774 source = DartUtils::ReadStringFromFile(url_string); 774 source = DartUtils::ReadStringFromFile(url_string);
775 } 775 }
776 if (Dart_IsError(source)) { 776 if (Dart_IsError(source)) {
777 return source; // source contains the error string. 777 return source; // source contains the error string.
778 } 778 }
779 // The tag is either an import or a source tag. 779 // The tag is either an import or a source tag.
780 // Load it according to the specified tag. 780 // Load it according to the specified tag.
781 if (tag == kImportTag) { 781 if (tag == Dart_kImportTag) {
782 // Return library object or an error string. 782 // Return library object or an error string.
783 return Dart_LoadLibrary(url, source); 783 return Dart_LoadLibrary(url, source);
784 } else if (tag == kSourceTag) { 784 } else if (tag == Dart_kSourceTag) {
785 return Dart_LoadSource(library, url, source); 785 return Dart_LoadSource(library, url, source);
786 } 786 }
787 return Dart_Error("wrong tag"); 787 return Dart_Error("wrong tag");
788 } 788 }
789 789
790 790
791 Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root, 791 Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root,
792 Dart_Handle builtin_lib) { 792 Dart_Handle builtin_lib) {
793 // Setup the corelib 'print' function. 793 // Setup the corelib 'print' function.
794 Dart_Handle print = Dart_Invoke( 794 Dart_Handle print = Dart_Invoke(
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); 869 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject());
870 } 870 }
871 871
872 872
873 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { 873 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) {
874 // Post a message with the integer value. 874 // Post a message with the integer value.
875 int32_t min = 0xc0000000; // -1073741824 875 int32_t min = 0xc0000000; // -1073741824
876 int32_t max = 0x3fffffff; // 1073741823 876 int32_t max = 0x3fffffff; // 1073741823
877 ASSERT(min <= value && value < max); 877 ASSERT(min <= value && value < max);
878 Dart_CObject object; 878 Dart_CObject object;
879 object.type = Dart_CObject::kInt32; 879 object.type = Dart_CObject_kInt32;
880 object.value.as_int32 = value; 880 object.value.as_int32 = value;
881 return Dart_PostCObject(port_id, &object); 881 return Dart_PostCObject(port_id, &object);
882 } 882 }
883 883
884 884
885 Dart_Handle DartUtils::GetDartClass(const char* library_url, 885 Dart_Handle DartUtils::GetDartClass(const char* library_url,
886 const char* class_name) { 886 const char* class_name) {
887 return Dart_GetClass(Dart_LookupLibrary(NewString(library_url)), 887 return Dart_GetClass(Dart_LookupLibrary(NewString(library_url)),
888 NewString(class_name)); 888 NewString(class_name));
889 } 889 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
946 946
947 void DartUtils::SetOriginalWorkingDirectory() { 947 void DartUtils::SetOriginalWorkingDirectory() {
948 original_working_directory = Directory::Current(); 948 original_working_directory = Directory::Current();
949 } 949 }
950 950
951 951
952 // Statically allocated Dart_CObject instances for immutable 952 // Statically allocated Dart_CObject instances for immutable
953 // objects. As these will be used by different threads the use of 953 // objects. As these will be used by different threads the use of
954 // these depends on the fact that the marking internally in the 954 // these depends on the fact that the marking internally in the
955 // Dart_CObject structure is not marking simple value objects. 955 // Dart_CObject structure is not marking simple value objects.
956 Dart_CObject CObject::api_null_ = { Dart_CObject::kNull , { 0 } }; 956 Dart_CObject CObject::api_null_ = { Dart_CObject_kNull , { 0 } };
957 Dart_CObject CObject::api_true_ = { Dart_CObject::kBool , { true } }; 957 Dart_CObject CObject::api_true_ = { Dart_CObject_kBool , { true } };
958 Dart_CObject CObject::api_false_ = { Dart_CObject::kBool, { false } }; 958 Dart_CObject CObject::api_false_ = { Dart_CObject_kBool, { false } };
959 CObject CObject::null_ = CObject(&api_null_); 959 CObject CObject::null_ = CObject(&api_null_);
960 CObject CObject::true_ = CObject(&api_true_); 960 CObject CObject::true_ = CObject(&api_true_);
961 CObject CObject::false_ = CObject(&api_false_); 961 CObject CObject::false_ = CObject(&api_false_);
962 962
963 963
964 CObject* CObject::Null() { 964 CObject* CObject::Null() {
965 return &null_; 965 return &null_;
966 } 966 }
967 967
968 968
969 CObject* CObject::True() { 969 CObject* CObject::True() {
970 return &true_; 970 return &true_;
971 } 971 }
972 972
973 973
974 CObject* CObject::False() { 974 CObject* CObject::False() {
975 return &false_; 975 return &false_;
976 } 976 }
977 977
978 978
979 CObject* CObject::Bool(bool value) { 979 CObject* CObject::Bool(bool value) {
980 return value ? &true_ : &false_; 980 return value ? &true_ : &false_;
981 } 981 }
982 982
983 983
984 Dart_CObject* CObject::New(Dart_CObject::Type type, int additional_bytes) { 984 Dart_CObject* CObject::New(Dart_CObject_Type type, int additional_bytes) {
985 Dart_CObject* cobject = reinterpret_cast<Dart_CObject*>( 985 Dart_CObject* cobject = reinterpret_cast<Dart_CObject*>(
986 Dart_ScopeAllocate(sizeof(Dart_CObject) + additional_bytes)); 986 Dart_ScopeAllocate(sizeof(Dart_CObject) + additional_bytes));
987 cobject->type = type; 987 cobject->type = type;
988 return cobject; 988 return cobject;
989 } 989 }
990 990
991 991
992 Dart_CObject* CObject::NewInt32(int32_t value) { 992 Dart_CObject* CObject::NewInt32(int32_t value) {
993 Dart_CObject* cobject = New(Dart_CObject::kInt32); 993 Dart_CObject* cobject = New(Dart_CObject_kInt32);
994 cobject->value.as_int32 = value; 994 cobject->value.as_int32 = value;
995 return cobject; 995 return cobject;
996 } 996 }
997 997
998 998
999 Dart_CObject* CObject::NewInt64(int64_t value) { 999 Dart_CObject* CObject::NewInt64(int64_t value) {
1000 Dart_CObject* cobject = New(Dart_CObject::kInt64); 1000 Dart_CObject* cobject = New(Dart_CObject_kInt64);
1001 cobject->value.as_int64 = value; 1001 cobject->value.as_int64 = value;
1002 return cobject; 1002 return cobject;
1003 } 1003 }
1004 1004
1005 1005
1006 Dart_CObject* CObject::NewIntptr(intptr_t value) { 1006 Dart_CObject* CObject::NewIntptr(intptr_t value) {
1007 // Pointer values passed as intptr_t are always send as int64_t. 1007 // Pointer values passed as intptr_t are always send as int64_t.
1008 Dart_CObject* cobject = New(Dart_CObject::kInt64); 1008 Dart_CObject* cobject = New(Dart_CObject_kInt64);
1009 cobject->value.as_int64 = value; 1009 cobject->value.as_int64 = value;
1010 return cobject; 1010 return cobject;
1011 } 1011 }
1012 1012
1013 1013
1014 Dart_CObject* CObject::NewDouble(double value) { 1014 Dart_CObject* CObject::NewDouble(double value) {
1015 Dart_CObject* cobject = New(Dart_CObject::kDouble); 1015 Dart_CObject* cobject = New(Dart_CObject_kDouble);
1016 cobject->value.as_double = value; 1016 cobject->value.as_double = value;
1017 return cobject; 1017 return cobject;
1018 } 1018 }
1019 1019
1020 1020
1021 Dart_CObject* CObject::NewString(int length) { 1021 Dart_CObject* CObject::NewString(int length) {
1022 Dart_CObject* cobject = New(Dart_CObject::kString, length + 1); 1022 Dart_CObject* cobject = New(Dart_CObject_kString, length + 1);
1023 cobject->value.as_string = reinterpret_cast<char*>(cobject + 1); 1023 cobject->value.as_string = reinterpret_cast<char*>(cobject + 1);
1024 return cobject; 1024 return cobject;
1025 } 1025 }
1026 1026
1027 1027
1028 Dart_CObject* CObject::NewString(const char* str) { 1028 Dart_CObject* CObject::NewString(const char* str) {
1029 int length = strlen(str); 1029 int length = strlen(str);
1030 Dart_CObject* cobject = NewString(length); 1030 Dart_CObject* cobject = NewString(length);
1031 memmove(cobject->value.as_string, str, length + 1); 1031 memmove(cobject->value.as_string, str, length + 1);
1032 return cobject; 1032 return cobject;
1033 } 1033 }
1034 1034
1035 1035
1036 Dart_CObject* CObject::NewArray(int length) { 1036 Dart_CObject* CObject::NewArray(int length) {
1037 Dart_CObject* cobject = 1037 Dart_CObject* cobject =
1038 New(Dart_CObject::kArray, length * sizeof(Dart_CObject*)); // NOLINT 1038 New(Dart_CObject_kArray, length * sizeof(Dart_CObject*)); // NOLINT
1039 cobject->value.as_array.length = length; 1039 cobject->value.as_array.length = length;
1040 cobject->value.as_array.values = 1040 cobject->value.as_array.values =
1041 reinterpret_cast<Dart_CObject**>(cobject + 1); 1041 reinterpret_cast<Dart_CObject**>(cobject + 1);
1042 return cobject; 1042 return cobject;
1043 } 1043 }
1044 1044
1045 1045
1046 Dart_CObject* CObject::NewUint8Array(int length) { 1046 Dart_CObject* CObject::NewUint8Array(int length) {
1047 Dart_CObject* cobject = New(Dart_CObject::kTypedData, length); 1047 Dart_CObject* cobject = New(Dart_CObject_kTypedData, length);
1048 cobject->value.as_typed_data.type = Dart_CObject::kUint8Array; 1048 cobject->value.as_typed_data.type = Dart_TypedData_kUint8;
1049 cobject->value.as_typed_data.length = length; 1049 cobject->value.as_typed_data.length = length;
1050 cobject->value.as_typed_data.values = reinterpret_cast<uint8_t*>(cobject + 1); 1050 cobject->value.as_typed_data.values = reinterpret_cast<uint8_t*>(cobject + 1);
1051 return cobject; 1051 return cobject;
1052 } 1052 }
1053 1053
1054 1054
1055 Dart_CObject* CObject::NewExternalUint8Array( 1055 Dart_CObject* CObject::NewExternalUint8Array(
1056 int64_t length, uint8_t* data, void* peer, 1056 int64_t length, uint8_t* data, void* peer,
1057 Dart_WeakPersistentHandleFinalizer callback) { 1057 Dart_WeakPersistentHandleFinalizer callback) {
1058 Dart_CObject* cobject = New(Dart_CObject::kExternalTypedData); 1058 Dart_CObject* cobject = New(Dart_CObject_kExternalTypedData);
1059 cobject->value.as_external_typed_data.type = Dart_CObject::kUint8Array; 1059 cobject->value.as_external_typed_data.type = Dart_TypedData_kUint8;
1060 cobject->value.as_external_typed_data.length = length; 1060 cobject->value.as_external_typed_data.length = length;
1061 cobject->value.as_external_typed_data.data = data; 1061 cobject->value.as_external_typed_data.data = data;
1062 cobject->value.as_external_typed_data.peer = peer; 1062 cobject->value.as_external_typed_data.peer = peer;
1063 cobject->value.as_external_typed_data.callback = callback; 1063 cobject->value.as_external_typed_data.callback = callback;
1064 return cobject; 1064 return cobject;
1065 } 1065 }
1066 1066
1067 1067
1068 Dart_CObject* CObject::NewIOBuffer(int64_t length) { 1068 Dart_CObject* CObject::NewIOBuffer(int64_t length) {
1069 uint8_t* data = IOBuffer::Allocate(length); 1069 uint8_t* data = IOBuffer::Allocate(length);
1070 return NewExternalUint8Array(length, data, data, IOBuffer::Finalizer); 1070 return NewExternalUint8Array(length, data, data, IOBuffer::Finalizer);
1071 } 1071 }
1072 1072
1073 1073
1074 void CObject::FreeIOBufferData(Dart_CObject* cobject) { 1074 void CObject::FreeIOBufferData(Dart_CObject* cobject) {
1075 ASSERT(cobject->type == Dart_CObject::kExternalTypedData); 1075 ASSERT(cobject->type == Dart_CObject_kExternalTypedData);
1076 cobject->value.as_external_typed_data.callback( 1076 cobject->value.as_external_typed_data.callback(
1077 NULL, cobject->value.as_external_typed_data.peer); 1077 NULL, cobject->value.as_external_typed_data.peer);
1078 cobject->value.as_external_typed_data.data = NULL; 1078 cobject->value.as_external_typed_data.data = NULL;
1079 } 1079 }
1080 1080
1081 1081
1082 CObject* CObject::IllegalArgumentError() { 1082 CObject* CObject::IllegalArgumentError() {
1083 CObjectArray* result = new CObjectArray(CObject::NewArray(1)); 1083 CObjectArray* result = new CObjectArray(CObject::NewArray(1));
1084 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kArgumentError))); 1084 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kArgumentError)));
1085 return result; 1085 return result;
(...skipping 17 matching lines...) Expand all
1103 new CObjectString(CObject::NewString(os_error->message())); 1103 new CObjectString(CObject::NewString(os_error->message()));
1104 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); 1104 CObjectArray* result = new CObjectArray(CObject::NewArray(3));
1105 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); 1105 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError)));
1106 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); 1106 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code())));
1107 result->SetAt(2, error_message); 1107 result->SetAt(2, error_message);
1108 return result; 1108 return result;
1109 } 1109 }
1110 1110
1111 } // namespace bin 1111 } // namespace bin
1112 } // namespace dart 1112 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/bin/dartutils.h ('k') | runtime/bin/directory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698