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 613 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
624 if (Flags::Lookup(flag_name) != NULL) { | 624 if (Flags::Lookup(flag_name) != NULL) { |
625 return true; | 625 return true; |
626 } | 626 } |
627 return false; | 627 return false; |
628 } | 628 } |
629 | 629 |
630 | 630 |
631 // --- Isolates --- | 631 // --- Isolates --- |
632 | 632 |
633 | 633 |
634 DART_EXPORT Dart_Isolate Dart_CreateIsolate(const char* name_prefix, | 634 static char* BuildIsolateName(const char* script_uri, |
| 635 const char* main) { |
| 636 if (script_uri == NULL) { |
| 637 // Just use the main as the name. |
| 638 if (main == NULL) { |
| 639 return strdup("isolate"); |
| 640 } else { |
| 641 return strdup(main); |
| 642 } |
| 643 } |
| 644 |
| 645 // Skip past any slashes and backslashes in the script uri. |
| 646 const char* last_slash = strrchr(script_uri, '/'); |
| 647 if (last_slash != NULL) { |
| 648 script_uri = last_slash + 1; |
| 649 } |
| 650 const char* last_backslash = strrchr(script_uri, '\\'); |
| 651 if (last_backslash != NULL) { |
| 652 script_uri = last_backslash + 1; |
| 653 } |
| 654 if (main == NULL) { |
| 655 main = "main"; |
| 656 } |
| 657 |
| 658 char* chars = NULL; |
| 659 intptr_t len = OS::SNPrint(NULL, 0, "%s/%s", script_uri, main) + 1; |
| 660 chars = reinterpret_cast<char*>(malloc(len)); |
| 661 OS::SNPrint(chars, len, "%s/%s", script_uri, main); |
| 662 return chars; |
| 663 } |
| 664 |
| 665 |
| 666 DART_EXPORT Dart_Isolate Dart_CreateIsolate(const char* script_uri, |
| 667 const char* main, |
635 const uint8_t* snapshot, | 668 const uint8_t* snapshot, |
636 void* callback_data, | 669 void* callback_data, |
637 char** error) { | 670 char** error) { |
638 Isolate* isolate = Dart::CreateIsolate(name_prefix); | 671 char* isolate_name = BuildIsolateName(script_uri, main); |
| 672 Isolate* isolate = Dart::CreateIsolate(isolate_name); |
| 673 free(isolate_name); |
639 { | 674 { |
640 DARTSCOPE_NOCHECKS(isolate); | 675 DARTSCOPE_NOCHECKS(isolate); |
641 const Error& error_obj = | 676 const Error& error_obj = |
642 Error::Handle(isolate, | 677 Error::Handle(isolate, |
643 Dart::InitializeIsolate(snapshot, callback_data)); | 678 Dart::InitializeIsolate(snapshot, callback_data)); |
644 if (error_obj.IsNull()) { | 679 if (error_obj.IsNull()) { |
645 START_TIMER(time_total_runtime); | 680 START_TIMER(time_total_runtime); |
646 return reinterpret_cast<Dart_Isolate>(isolate); | 681 return reinterpret_cast<Dart_Isolate>(isolate); |
647 } | 682 } |
648 *error = strdup(error_obj.ToErrorCString()); | 683 *error = strdup(error_obj.ToErrorCString()); |
(...skipping 2285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2934 | 2969 |
2935 DART_EXPORT Dart_Handle Dart_SetLibraryTagHandler( | 2970 DART_EXPORT Dart_Handle Dart_SetLibraryTagHandler( |
2936 Dart_LibraryTagHandler handler) { | 2971 Dart_LibraryTagHandler handler) { |
2937 Isolate* isolate = Isolate::Current(); | 2972 Isolate* isolate = Isolate::Current(); |
2938 CHECK_ISOLATE(isolate); | 2973 CHECK_ISOLATE(isolate); |
2939 isolate->set_library_tag_handler(handler); | 2974 isolate->set_library_tag_handler(handler); |
2940 return Api::Success(isolate); | 2975 return Api::Success(isolate); |
2941 } | 2976 } |
2942 | 2977 |
2943 | 2978 |
| 2979 DART_EXPORT Dart_Handle Dart_SetImportMap(Dart_Handle import_map) { |
| 2980 Isolate* isolate = Isolate::Current(); |
| 2981 DARTSCOPE(isolate); |
| 2982 const Array& mapping_array = Api::UnwrapArrayHandle(isolate, import_map); |
| 2983 if (mapping_array.IsNull()) { |
| 2984 RETURN_TYPE_ERROR(isolate, import_map, Array); |
| 2985 } |
| 2986 isolate->object_store()->set_import_map(mapping_array); |
| 2987 return Api::Success(isolate); |
| 2988 } |
| 2989 |
| 2990 |
2944 // NOTE: Need to pass 'result' as a parameter here in order to avoid | 2991 // NOTE: Need to pass 'result' as a parameter here in order to avoid |
2945 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork' | 2992 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork' |
2946 // which shows up because of the use of setjmp. | 2993 // which shows up because of the use of setjmp. |
2947 static void CompileSource(Isolate* isolate, | 2994 static void CompileSource(Isolate* isolate, |
2948 const Library& lib, | 2995 const Library& lib, |
2949 const String& url, | 2996 const String& url, |
2950 const String& source, | 2997 const String& source, |
2951 RawScript::Kind kind, | 2998 RawScript::Kind kind, |
2952 Dart_Handle* result) { | 2999 Dart_Handle* result) { |
2953 bool update_lib_status = (kind == RawScript::kScript || | 3000 bool update_lib_status = (kind == RawScript::kScript || |
(...skipping 13 matching lines...) Expand all Loading... |
2967 } else { | 3014 } else { |
2968 *result = Api::NewHandle(isolate, error.raw()); | 3015 *result = Api::NewHandle(isolate, error.raw()); |
2969 if (update_lib_status) { | 3016 if (update_lib_status) { |
2970 lib.SetLoadError(); | 3017 lib.SetLoadError(); |
2971 } | 3018 } |
2972 } | 3019 } |
2973 } | 3020 } |
2974 | 3021 |
2975 | 3022 |
2976 DART_EXPORT Dart_Handle Dart_LoadScript(Dart_Handle url, | 3023 DART_EXPORT Dart_Handle Dart_LoadScript(Dart_Handle url, |
2977 Dart_Handle source, | 3024 Dart_Handle source) { |
2978 Dart_Handle import_map) { | |
2979 TIMERSCOPE(time_script_loading); | 3025 TIMERSCOPE(time_script_loading); |
2980 Isolate* isolate = Isolate::Current(); | 3026 Isolate* isolate = Isolate::Current(); |
2981 DARTSCOPE(isolate); | 3027 DARTSCOPE(isolate); |
2982 const String& url_str = Api::UnwrapStringHandle(isolate, url); | 3028 const String& url_str = Api::UnwrapStringHandle(isolate, url); |
2983 if (url_str.IsNull()) { | 3029 if (url_str.IsNull()) { |
2984 RETURN_TYPE_ERROR(isolate, url, String); | 3030 RETURN_TYPE_ERROR(isolate, url, String); |
2985 } | 3031 } |
2986 const String& source_str = Api::UnwrapStringHandle(isolate, source); | 3032 const String& source_str = Api::UnwrapStringHandle(isolate, source); |
2987 if (source_str.IsNull()) { | 3033 if (source_str.IsNull()) { |
2988 RETURN_TYPE_ERROR(isolate, source, String); | 3034 RETURN_TYPE_ERROR(isolate, source, String); |
2989 } | 3035 } |
2990 const Array& mapping_array = Api::UnwrapArrayHandle(isolate, import_map); | |
2991 Library& library = | 3036 Library& library = |
2992 Library::Handle(isolate, isolate->object_store()->root_library()); | 3037 Library::Handle(isolate, isolate->object_store()->root_library()); |
2993 if (!library.IsNull()) { | 3038 if (!library.IsNull()) { |
2994 const String& library_url = String::Handle(isolate, library.url()); | 3039 const String& library_url = String::Handle(isolate, library.url()); |
2995 return Api::NewError("%s: A script has already been loaded from '%s'.", | 3040 return Api::NewError("%s: A script has already been loaded from '%s'.", |
2996 CURRENT_FUNC, library_url.ToCString()); | 3041 CURRENT_FUNC, library_url.ToCString()); |
2997 } | 3042 } |
2998 library = Library::New(url_str); | 3043 library = Library::New(url_str); |
2999 if (mapping_array.IsNull()) { | |
3000 library.set_import_map(Array::Handle(isolate, Array::Empty())); | |
3001 } else { | |
3002 library.set_import_map(mapping_array); | |
3003 } | |
3004 library.Register(); | 3044 library.Register(); |
3005 isolate->object_store()->set_root_library(library); | 3045 isolate->object_store()->set_root_library(library); |
3006 Dart_Handle result; | 3046 Dart_Handle result; |
3007 CompileSource(isolate, | 3047 CompileSource(isolate, |
3008 library, | 3048 library, |
3009 url_str, | 3049 url_str, |
3010 source_str, | 3050 source_str, |
3011 RawScript::kScript, | 3051 RawScript::kScript, |
3012 &result); | 3052 &result); |
3013 return result; | 3053 return result; |
(...skipping 25 matching lines...) Expand all Loading... |
3039 if (!tmp.IsLibrary()) { | 3079 if (!tmp.IsLibrary()) { |
3040 return Api::NewError("%s: Unable to deserialize snapshot correctly.", | 3080 return Api::NewError("%s: Unable to deserialize snapshot correctly.", |
3041 CURRENT_FUNC); | 3081 CURRENT_FUNC); |
3042 } | 3082 } |
3043 library ^= tmp.raw(); | 3083 library ^= tmp.raw(); |
3044 isolate->object_store()->set_root_library(library); | 3084 isolate->object_store()->set_root_library(library); |
3045 return Api::NewHandle(isolate, library.raw()); | 3085 return Api::NewHandle(isolate, library.raw()); |
3046 } | 3086 } |
3047 | 3087 |
3048 | 3088 |
| 3089 DART_EXPORT Dart_Handle Dart_RootLibrary() { |
| 3090 Isolate* isolate = Isolate::Current(); |
| 3091 DARTSCOPE(isolate); |
| 3092 Library& library = |
| 3093 Library::Handle(isolate, isolate->object_store()->root_library()); |
| 3094 return Api::NewHandle(isolate, library.raw()); |
| 3095 } |
| 3096 |
| 3097 |
3049 static void CompileAll(Isolate* isolate, Dart_Handle* result) { | 3098 static void CompileAll(Isolate* isolate, Dart_Handle* result) { |
3050 ASSERT(isolate != NULL); | 3099 ASSERT(isolate != NULL); |
3051 const Error& error = Error::Handle(isolate, Library::CompileAll()); | 3100 const Error& error = Error::Handle(isolate, Library::CompileAll()); |
3052 if (error.IsNull()) { | 3101 if (error.IsNull()) { |
3053 *result = Api::Success(isolate); | 3102 *result = Api::Success(isolate); |
3054 } else { | 3103 } else { |
3055 *result = Api::NewHandle(isolate, error.raw()); | 3104 *result = Api::NewHandle(isolate, error.raw()); |
3056 } | 3105 } |
3057 } | 3106 } |
3058 | 3107 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3124 if (library.IsNull()) { | 3173 if (library.IsNull()) { |
3125 return Api::NewError("%s: library '%s' not found.", | 3174 return Api::NewError("%s: library '%s' not found.", |
3126 CURRENT_FUNC, url_str.ToCString()); | 3175 CURRENT_FUNC, url_str.ToCString()); |
3127 } else { | 3176 } else { |
3128 return Api::NewHandle(isolate, library.raw()); | 3177 return Api::NewHandle(isolate, library.raw()); |
3129 } | 3178 } |
3130 } | 3179 } |
3131 | 3180 |
3132 | 3181 |
3133 DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle url, | 3182 DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle url, |
3134 Dart_Handle source, | 3183 Dart_Handle source) { |
3135 Dart_Handle import_map) { | |
3136 TIMERSCOPE(time_script_loading); | 3184 TIMERSCOPE(time_script_loading); |
3137 Isolate* isolate = Isolate::Current(); | 3185 Isolate* isolate = Isolate::Current(); |
3138 DARTSCOPE(isolate); | 3186 DARTSCOPE(isolate); |
3139 const String& url_str = Api::UnwrapStringHandle(isolate, url); | 3187 const String& url_str = Api::UnwrapStringHandle(isolate, url); |
3140 if (url_str.IsNull()) { | 3188 if (url_str.IsNull()) { |
3141 RETURN_TYPE_ERROR(isolate, url, String); | 3189 RETURN_TYPE_ERROR(isolate, url, String); |
3142 } | 3190 } |
3143 const String& source_str = Api::UnwrapStringHandle(isolate, source); | 3191 const String& source_str = Api::UnwrapStringHandle(isolate, source); |
3144 if (source_str.IsNull()) { | 3192 if (source_str.IsNull()) { |
3145 RETURN_TYPE_ERROR(isolate, source, String); | 3193 RETURN_TYPE_ERROR(isolate, source, String); |
3146 } | 3194 } |
3147 const Array& mapping_array = Api::UnwrapArrayHandle(isolate, import_map); | |
3148 Library& library = Library::Handle(isolate, Library::LookupLibrary(url_str)); | 3195 Library& library = Library::Handle(isolate, Library::LookupLibrary(url_str)); |
3149 if (library.IsNull()) { | 3196 if (library.IsNull()) { |
3150 library = Library::New(url_str); | 3197 library = Library::New(url_str); |
3151 if (mapping_array.IsNull()) { | |
3152 library.set_import_map(Array::Handle(isolate, Array::Empty())); | |
3153 } else { | |
3154 library.set_import_map(mapping_array); | |
3155 } | |
3156 library.Register(); | 3198 library.Register(); |
3157 } else if (!library.LoadNotStarted()) { | 3199 } else if (!library.LoadNotStarted()) { |
3158 // The source for this library has either been loaded or is in the | 3200 // The source for this library has either been loaded or is in the |
3159 // process of loading. Return an error. | 3201 // process of loading. Return an error. |
3160 return Api::NewError("%s: library '%s' has already been loaded.", | 3202 return Api::NewError("%s: library '%s' has already been loaded.", |
3161 CURRENT_FUNC, url_str.ToCString()); | 3203 CURRENT_FUNC, url_str.ToCString()); |
3162 } | 3204 } |
3163 Dart_Handle result; | 3205 Dart_Handle result; |
3164 CompileSource(isolate, | 3206 CompileSource(isolate, |
3165 library, | 3207 library, |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3264 *buffer = NULL; | 3306 *buffer = NULL; |
3265 } | 3307 } |
3266 delete debug_region; | 3308 delete debug_region; |
3267 } else { | 3309 } else { |
3268 *buffer = NULL; | 3310 *buffer = NULL; |
3269 *buffer_size = 0; | 3311 *buffer_size = 0; |
3270 } | 3312 } |
3271 } | 3313 } |
3272 | 3314 |
3273 } // namespace dart | 3315 } // namespace dart |
OLD | NEW |