OLD | NEW |
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 "include/dart_api.h" | 5 #include "include/dart_api.h" |
6 | 6 |
7 #include "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
10 #include "vm/dart.h" | 10 #include "vm/dart.h" |
(...skipping 619 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
630 if (Flags::Lookup(flag_name) != NULL) { | 630 if (Flags::Lookup(flag_name) != NULL) { |
631 return true; | 631 return true; |
632 } | 632 } |
633 return false; | 633 return false; |
634 } | 634 } |
635 | 635 |
636 | 636 |
637 // --- Isolates --- | 637 // --- Isolates --- |
638 | 638 |
639 | 639 |
640 DART_EXPORT Dart_Isolate Dart_CreateIsolate(const char* name_prefix, | 640 static char* BuildIsolateName(const char* script_uri, |
| 641 const char* main) { |
| 642 if (script_uri == NULL) { |
| 643 // Just use the main as the name. |
| 644 if (main == NULL) { |
| 645 return strdup("isolate"); |
| 646 } else { |
| 647 return strdup(main); |
| 648 } |
| 649 } |
| 650 |
| 651 // Skip past any slashes and backslashes in the script uri. |
| 652 const char* last_slash = strrchr(script_uri, '/'); |
| 653 if (last_slash != NULL) { |
| 654 script_uri = last_slash + 1; |
| 655 } |
| 656 const char* last_backslash = strrchr(script_uri, '\\'); |
| 657 if (last_backslash != NULL) { |
| 658 script_uri = last_backslash + 1; |
| 659 } |
| 660 if (main == NULL) { |
| 661 main = "main"; |
| 662 } |
| 663 |
| 664 char* chars = NULL; |
| 665 intptr_t len = OS::SNPrint(NULL, 0, "%s/%s", script_uri, main) + 1; |
| 666 chars = reinterpret_cast<char*>(malloc(len)); |
| 667 OS::SNPrint(chars, len, "%s/%s", script_uri, main); |
| 668 return chars; |
| 669 } |
| 670 |
| 671 |
| 672 DART_EXPORT Dart_Isolate Dart_CreateIsolate(const char* script_uri, |
| 673 const char* main, |
641 const uint8_t* snapshot, | 674 const uint8_t* snapshot, |
642 void* callback_data, | 675 void* callback_data, |
643 char** error) { | 676 char** error) { |
644 Isolate* isolate = Dart::CreateIsolate(name_prefix); | 677 char* isolate_name = BuildIsolateName(script_uri, main); |
| 678 Isolate* isolate = Dart::CreateIsolate(isolate_name); |
| 679 free(isolate_name); |
645 { | 680 { |
646 DARTSCOPE_NOCHECKS(isolate); | 681 DARTSCOPE_NOCHECKS(isolate); |
647 const Error& error_obj = | 682 const Error& error_obj = |
648 Error::Handle(isolate, | 683 Error::Handle(isolate, |
649 Dart::InitializeIsolate(snapshot, callback_data)); | 684 Dart::InitializeIsolate(snapshot, callback_data)); |
650 if (error_obj.IsNull()) { | 685 if (error_obj.IsNull()) { |
651 START_TIMER(time_total_runtime); | 686 START_TIMER(time_total_runtime); |
652 return reinterpret_cast<Dart_Isolate>(isolate); | 687 return reinterpret_cast<Dart_Isolate>(isolate); |
653 } | 688 } |
654 *error = strdup(error_obj.ToErrorCString()); | 689 *error = strdup(error_obj.ToErrorCString()); |
(...skipping 2341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2996 | 3031 |
2997 DART_EXPORT Dart_Handle Dart_SetLibraryTagHandler( | 3032 DART_EXPORT Dart_Handle Dart_SetLibraryTagHandler( |
2998 Dart_LibraryTagHandler handler) { | 3033 Dart_LibraryTagHandler handler) { |
2999 Isolate* isolate = Isolate::Current(); | 3034 Isolate* isolate = Isolate::Current(); |
3000 CHECK_ISOLATE(isolate); | 3035 CHECK_ISOLATE(isolate); |
3001 isolate->set_library_tag_handler(handler); | 3036 isolate->set_library_tag_handler(handler); |
3002 return Api::Success(isolate); | 3037 return Api::Success(isolate); |
3003 } | 3038 } |
3004 | 3039 |
3005 | 3040 |
| 3041 DART_EXPORT Dart_Handle Dart_SetImportMap(Dart_Handle import_map) { |
| 3042 Isolate* isolate = Isolate::Current(); |
| 3043 DARTSCOPE(isolate); |
| 3044 const Array& mapping_array = Api::UnwrapArrayHandle(isolate, import_map); |
| 3045 if (mapping_array.IsNull()) { |
| 3046 RETURN_TYPE_ERROR(isolate, import_map, Array); |
| 3047 } |
| 3048 isolate->object_store()->set_import_map(mapping_array); |
| 3049 return Api::Success(isolate); |
| 3050 } |
| 3051 |
| 3052 |
3006 // NOTE: Need to pass 'result' as a parameter here in order to avoid | 3053 // NOTE: Need to pass 'result' as a parameter here in order to avoid |
3007 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork' | 3054 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork' |
3008 // which shows up because of the use of setjmp. | 3055 // which shows up because of the use of setjmp. |
3009 static void CompileSource(Isolate* isolate, | 3056 static void CompileSource(Isolate* isolate, |
3010 const Library& lib, | 3057 const Library& lib, |
3011 const String& url, | 3058 const String& url, |
3012 const String& source, | 3059 const String& source, |
3013 RawScript::Kind kind, | 3060 RawScript::Kind kind, |
3014 Dart_Handle* result) { | 3061 Dart_Handle* result) { |
3015 bool update_lib_status = (kind == RawScript::kScript || | 3062 bool update_lib_status = (kind == RawScript::kScript || |
(...skipping 13 matching lines...) Expand all Loading... |
3029 } else { | 3076 } else { |
3030 *result = Api::NewHandle(isolate, error.raw()); | 3077 *result = Api::NewHandle(isolate, error.raw()); |
3031 if (update_lib_status) { | 3078 if (update_lib_status) { |
3032 lib.SetLoadError(); | 3079 lib.SetLoadError(); |
3033 } | 3080 } |
3034 } | 3081 } |
3035 } | 3082 } |
3036 | 3083 |
3037 | 3084 |
3038 DART_EXPORT Dart_Handle Dart_LoadScript(Dart_Handle url, | 3085 DART_EXPORT Dart_Handle Dart_LoadScript(Dart_Handle url, |
3039 Dart_Handle source, | 3086 Dart_Handle source) { |
3040 Dart_Handle import_map) { | |
3041 TIMERSCOPE(time_script_loading); | 3087 TIMERSCOPE(time_script_loading); |
3042 Isolate* isolate = Isolate::Current(); | 3088 Isolate* isolate = Isolate::Current(); |
3043 DARTSCOPE(isolate); | 3089 DARTSCOPE(isolate); |
3044 const String& url_str = Api::UnwrapStringHandle(isolate, url); | 3090 const String& url_str = Api::UnwrapStringHandle(isolate, url); |
3045 if (url_str.IsNull()) { | 3091 if (url_str.IsNull()) { |
3046 RETURN_TYPE_ERROR(isolate, url, String); | 3092 RETURN_TYPE_ERROR(isolate, url, String); |
3047 } | 3093 } |
3048 const String& source_str = Api::UnwrapStringHandle(isolate, source); | 3094 const String& source_str = Api::UnwrapStringHandle(isolate, source); |
3049 if (source_str.IsNull()) { | 3095 if (source_str.IsNull()) { |
3050 RETURN_TYPE_ERROR(isolate, source, String); | 3096 RETURN_TYPE_ERROR(isolate, source, String); |
3051 } | 3097 } |
3052 const Array& mapping_array = Api::UnwrapArrayHandle(isolate, import_map); | |
3053 Library& library = | 3098 Library& library = |
3054 Library::Handle(isolate, isolate->object_store()->root_library()); | 3099 Library::Handle(isolate, isolate->object_store()->root_library()); |
3055 if (!library.IsNull()) { | 3100 if (!library.IsNull()) { |
3056 const String& library_url = String::Handle(isolate, library.url()); | 3101 const String& library_url = String::Handle(isolate, library.url()); |
3057 return Api::NewError("%s: A script has already been loaded from '%s'.", | 3102 return Api::NewError("%s: A script has already been loaded from '%s'.", |
3058 CURRENT_FUNC, library_url.ToCString()); | 3103 CURRENT_FUNC, library_url.ToCString()); |
3059 } | 3104 } |
3060 library = Library::New(url_str); | 3105 library = Library::New(url_str); |
3061 if (mapping_array.IsNull()) { | |
3062 library.set_import_map(Array::Handle(isolate, Array::Empty())); | |
3063 } else { | |
3064 library.set_import_map(mapping_array); | |
3065 } | |
3066 library.Register(); | 3106 library.Register(); |
3067 isolate->object_store()->set_root_library(library); | 3107 isolate->object_store()->set_root_library(library); |
3068 Dart_Handle result; | 3108 Dart_Handle result; |
3069 CompileSource(isolate, | 3109 CompileSource(isolate, |
3070 library, | 3110 library, |
3071 url_str, | 3111 url_str, |
3072 source_str, | 3112 source_str, |
3073 RawScript::kScript, | 3113 RawScript::kScript, |
3074 &result); | 3114 &result); |
3075 return result; | 3115 return result; |
(...skipping 25 matching lines...) Expand all Loading... |
3101 if (!tmp.IsLibrary()) { | 3141 if (!tmp.IsLibrary()) { |
3102 return Api::NewError("%s: Unable to deserialize snapshot correctly.", | 3142 return Api::NewError("%s: Unable to deserialize snapshot correctly.", |
3103 CURRENT_FUNC); | 3143 CURRENT_FUNC); |
3104 } | 3144 } |
3105 library ^= tmp.raw(); | 3145 library ^= tmp.raw(); |
3106 isolate->object_store()->set_root_library(library); | 3146 isolate->object_store()->set_root_library(library); |
3107 return Api::NewHandle(isolate, library.raw()); | 3147 return Api::NewHandle(isolate, library.raw()); |
3108 } | 3148 } |
3109 | 3149 |
3110 | 3150 |
| 3151 DART_EXPORT Dart_Handle Dart_RootLibrary() { |
| 3152 Isolate* isolate = Isolate::Current(); |
| 3153 DARTSCOPE(isolate); |
| 3154 Library& library = |
| 3155 Library::Handle(isolate, isolate->object_store()->root_library()); |
| 3156 return Api::NewHandle(isolate, library.raw()); |
| 3157 } |
| 3158 |
| 3159 |
3111 static void CompileAll(Isolate* isolate, Dart_Handle* result) { | 3160 static void CompileAll(Isolate* isolate, Dart_Handle* result) { |
3112 ASSERT(isolate != NULL); | 3161 ASSERT(isolate != NULL); |
3113 const Error& error = Error::Handle(isolate, Library::CompileAll()); | 3162 const Error& error = Error::Handle(isolate, Library::CompileAll()); |
3114 if (error.IsNull()) { | 3163 if (error.IsNull()) { |
3115 *result = Api::Success(isolate); | 3164 *result = Api::Success(isolate); |
3116 } else { | 3165 } else { |
3117 *result = Api::NewHandle(isolate, error.raw()); | 3166 *result = Api::NewHandle(isolate, error.raw()); |
3118 } | 3167 } |
3119 } | 3168 } |
3120 | 3169 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3189 if (library.IsNull()) { | 3238 if (library.IsNull()) { |
3190 return Api::NewError("%s: library '%s' not found.", | 3239 return Api::NewError("%s: library '%s' not found.", |
3191 CURRENT_FUNC, url_str.ToCString()); | 3240 CURRENT_FUNC, url_str.ToCString()); |
3192 } else { | 3241 } else { |
3193 return Api::NewHandle(isolate, library.raw()); | 3242 return Api::NewHandle(isolate, library.raw()); |
3194 } | 3243 } |
3195 } | 3244 } |
3196 | 3245 |
3197 | 3246 |
3198 DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle url, | 3247 DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle url, |
3199 Dart_Handle source, | 3248 Dart_Handle source) { |
3200 Dart_Handle import_map) { | |
3201 TIMERSCOPE(time_script_loading); | 3249 TIMERSCOPE(time_script_loading); |
3202 Isolate* isolate = Isolate::Current(); | 3250 Isolate* isolate = Isolate::Current(); |
3203 DARTSCOPE(isolate); | 3251 DARTSCOPE(isolate); |
3204 const String& url_str = Api::UnwrapStringHandle(isolate, url); | 3252 const String& url_str = Api::UnwrapStringHandle(isolate, url); |
3205 if (url_str.IsNull()) { | 3253 if (url_str.IsNull()) { |
3206 RETURN_TYPE_ERROR(isolate, url, String); | 3254 RETURN_TYPE_ERROR(isolate, url, String); |
3207 } | 3255 } |
3208 const String& source_str = Api::UnwrapStringHandle(isolate, source); | 3256 const String& source_str = Api::UnwrapStringHandle(isolate, source); |
3209 if (source_str.IsNull()) { | 3257 if (source_str.IsNull()) { |
3210 RETURN_TYPE_ERROR(isolate, source, String); | 3258 RETURN_TYPE_ERROR(isolate, source, String); |
3211 } | 3259 } |
3212 const Array& mapping_array = Api::UnwrapArrayHandle(isolate, import_map); | |
3213 Library& library = Library::Handle(isolate, Library::LookupLibrary(url_str)); | 3260 Library& library = Library::Handle(isolate, Library::LookupLibrary(url_str)); |
3214 if (library.IsNull()) { | 3261 if (library.IsNull()) { |
3215 library = Library::New(url_str); | 3262 library = Library::New(url_str); |
3216 if (mapping_array.IsNull()) { | |
3217 library.set_import_map(Array::Handle(isolate, Array::Empty())); | |
3218 } else { | |
3219 library.set_import_map(mapping_array); | |
3220 } | |
3221 library.Register(); | 3263 library.Register(); |
3222 } else if (!library.LoadNotStarted()) { | 3264 } else if (!library.LoadNotStarted()) { |
3223 // The source for this library has either been loaded or is in the | 3265 // The source for this library has either been loaded or is in the |
3224 // process of loading. Return an error. | 3266 // process of loading. Return an error. |
3225 return Api::NewError("%s: library '%s' has already been loaded.", | 3267 return Api::NewError("%s: library '%s' has already been loaded.", |
3226 CURRENT_FUNC, url_str.ToCString()); | 3268 CURRENT_FUNC, url_str.ToCString()); |
3227 } | 3269 } |
3228 Dart_Handle result; | 3270 Dart_Handle result; |
3229 CompileSource(isolate, | 3271 CompileSource(isolate, |
3230 library, | 3272 library, |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3329 *buffer = NULL; | 3371 *buffer = NULL; |
3330 } | 3372 } |
3331 delete debug_region; | 3373 delete debug_region; |
3332 } else { | 3374 } else { |
3333 *buffer = NULL; | 3375 *buffer = NULL; |
3334 *buffer_size = 0; | 3376 *buffer_size = 0; |
3335 } | 3377 } |
3336 } | 3378 } |
3337 | 3379 |
3338 } // namespace dart | 3380 } // namespace dart |
OLD | NEW |