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 static char* BuildIsolateName(const char* script_uri, | 634 DART_EXPORT Dart_Isolate Dart_CreateIsolate(const char* name_prefix, |
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, | |
668 const uint8_t* snapshot, | 635 const uint8_t* snapshot, |
669 void* callback_data, | 636 void* callback_data, |
670 char** error) { | 637 char** error) { |
671 char* isolate_name = BuildIsolateName(script_uri, main); | 638 Isolate* isolate = Dart::CreateIsolate(name_prefix); |
672 Isolate* isolate = Dart::CreateIsolate(isolate_name); | |
673 free(isolate_name); | |
674 { | 639 { |
675 DARTSCOPE_NOCHECKS(isolate); | 640 DARTSCOPE_NOCHECKS(isolate); |
676 const Error& error_obj = | 641 const Error& error_obj = |
677 Error::Handle(isolate, | 642 Error::Handle(isolate, |
678 Dart::InitializeIsolate(snapshot, callback_data)); | 643 Dart::InitializeIsolate(snapshot, callback_data)); |
679 if (error_obj.IsNull()) { | 644 if (error_obj.IsNull()) { |
680 START_TIMER(time_total_runtime); | 645 START_TIMER(time_total_runtime); |
681 return reinterpret_cast<Dart_Isolate>(isolate); | 646 return reinterpret_cast<Dart_Isolate>(isolate); |
682 } | 647 } |
683 *error = strdup(error_obj.ToErrorCString()); | 648 *error = strdup(error_obj.ToErrorCString()); |
(...skipping 2304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2988 | 2953 |
2989 DART_EXPORT Dart_Handle Dart_SetLibraryTagHandler( | 2954 DART_EXPORT Dart_Handle Dart_SetLibraryTagHandler( |
2990 Dart_LibraryTagHandler handler) { | 2955 Dart_LibraryTagHandler handler) { |
2991 Isolate* isolate = Isolate::Current(); | 2956 Isolate* isolate = Isolate::Current(); |
2992 CHECK_ISOLATE(isolate); | 2957 CHECK_ISOLATE(isolate); |
2993 isolate->set_library_tag_handler(handler); | 2958 isolate->set_library_tag_handler(handler); |
2994 return Api::Success(isolate); | 2959 return Api::Success(isolate); |
2995 } | 2960 } |
2996 | 2961 |
2997 | 2962 |
2998 DART_EXPORT Dart_Handle Dart_SetImportMap(Dart_Handle import_map) { | |
2999 Isolate* isolate = Isolate::Current(); | |
3000 DARTSCOPE(isolate); | |
3001 const Array& mapping_array = Api::UnwrapArrayHandle(isolate, import_map); | |
3002 if (mapping_array.IsNull()) { | |
3003 RETURN_TYPE_ERROR(isolate, import_map, Array); | |
3004 } | |
3005 isolate->object_store()->set_import_map(mapping_array); | |
3006 return Api::Success(isolate); | |
3007 } | |
3008 | |
3009 | |
3010 // NOTE: Need to pass 'result' as a parameter here in order to avoid | 2963 // NOTE: Need to pass 'result' as a parameter here in order to avoid |
3011 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork' | 2964 // warning: variable 'result' might be clobbered by 'longjmp' or 'vfork' |
3012 // which shows up because of the use of setjmp. | 2965 // which shows up because of the use of setjmp. |
3013 static void CompileSource(Isolate* isolate, | 2966 static void CompileSource(Isolate* isolate, |
3014 const Library& lib, | 2967 const Library& lib, |
3015 const String& url, | 2968 const String& url, |
3016 const String& source, | 2969 const String& source, |
3017 RawScript::Kind kind, | 2970 RawScript::Kind kind, |
3018 Dart_Handle* result) { | 2971 Dart_Handle* result) { |
3019 bool update_lib_status = (kind == RawScript::kScript || | 2972 bool update_lib_status = (kind == RawScript::kScript || |
(...skipping 13 matching lines...) Expand all Loading... |
3033 } else { | 2986 } else { |
3034 *result = Api::NewHandle(isolate, error.raw()); | 2987 *result = Api::NewHandle(isolate, error.raw()); |
3035 if (update_lib_status) { | 2988 if (update_lib_status) { |
3036 lib.SetLoadError(); | 2989 lib.SetLoadError(); |
3037 } | 2990 } |
3038 } | 2991 } |
3039 } | 2992 } |
3040 | 2993 |
3041 | 2994 |
3042 DART_EXPORT Dart_Handle Dart_LoadScript(Dart_Handle url, | 2995 DART_EXPORT Dart_Handle Dart_LoadScript(Dart_Handle url, |
3043 Dart_Handle source) { | 2996 Dart_Handle source, |
| 2997 Dart_Handle import_map) { |
3044 TIMERSCOPE(time_script_loading); | 2998 TIMERSCOPE(time_script_loading); |
3045 Isolate* isolate = Isolate::Current(); | 2999 Isolate* isolate = Isolate::Current(); |
3046 DARTSCOPE(isolate); | 3000 DARTSCOPE(isolate); |
3047 const String& url_str = Api::UnwrapStringHandle(isolate, url); | 3001 const String& url_str = Api::UnwrapStringHandle(isolate, url); |
3048 if (url_str.IsNull()) { | 3002 if (url_str.IsNull()) { |
3049 RETURN_TYPE_ERROR(isolate, url, String); | 3003 RETURN_TYPE_ERROR(isolate, url, String); |
3050 } | 3004 } |
3051 const String& source_str = Api::UnwrapStringHandle(isolate, source); | 3005 const String& source_str = Api::UnwrapStringHandle(isolate, source); |
3052 if (source_str.IsNull()) { | 3006 if (source_str.IsNull()) { |
3053 RETURN_TYPE_ERROR(isolate, source, String); | 3007 RETURN_TYPE_ERROR(isolate, source, String); |
3054 } | 3008 } |
| 3009 const Array& mapping_array = Api::UnwrapArrayHandle(isolate, import_map); |
3055 Library& library = | 3010 Library& library = |
3056 Library::Handle(isolate, isolate->object_store()->root_library()); | 3011 Library::Handle(isolate, isolate->object_store()->root_library()); |
3057 if (!library.IsNull()) { | 3012 if (!library.IsNull()) { |
3058 const String& library_url = String::Handle(isolate, library.url()); | 3013 const String& library_url = String::Handle(isolate, library.url()); |
3059 return Api::NewError("%s: A script has already been loaded from '%s'.", | 3014 return Api::NewError("%s: A script has already been loaded from '%s'.", |
3060 CURRENT_FUNC, library_url.ToCString()); | 3015 CURRENT_FUNC, library_url.ToCString()); |
3061 } | 3016 } |
3062 library = Library::New(url_str); | 3017 library = Library::New(url_str); |
| 3018 if (mapping_array.IsNull()) { |
| 3019 library.set_import_map(Array::Handle(isolate, Array::Empty())); |
| 3020 } else { |
| 3021 library.set_import_map(mapping_array); |
| 3022 } |
3063 library.Register(); | 3023 library.Register(); |
3064 isolate->object_store()->set_root_library(library); | 3024 isolate->object_store()->set_root_library(library); |
3065 Dart_Handle result; | 3025 Dart_Handle result; |
3066 CompileSource(isolate, | 3026 CompileSource(isolate, |
3067 library, | 3027 library, |
3068 url_str, | 3028 url_str, |
3069 source_str, | 3029 source_str, |
3070 RawScript::kScript, | 3030 RawScript::kScript, |
3071 &result); | 3031 &result); |
3072 return result; | 3032 return result; |
(...skipping 25 matching lines...) Expand all Loading... |
3098 if (!tmp.IsLibrary()) { | 3058 if (!tmp.IsLibrary()) { |
3099 return Api::NewError("%s: Unable to deserialize snapshot correctly.", | 3059 return Api::NewError("%s: Unable to deserialize snapshot correctly.", |
3100 CURRENT_FUNC); | 3060 CURRENT_FUNC); |
3101 } | 3061 } |
3102 library ^= tmp.raw(); | 3062 library ^= tmp.raw(); |
3103 isolate->object_store()->set_root_library(library); | 3063 isolate->object_store()->set_root_library(library); |
3104 return Api::NewHandle(isolate, library.raw()); | 3064 return Api::NewHandle(isolate, library.raw()); |
3105 } | 3065 } |
3106 | 3066 |
3107 | 3067 |
3108 DART_EXPORT Dart_Handle Dart_RootLibrary() { | |
3109 Isolate* isolate = Isolate::Current(); | |
3110 DARTSCOPE(isolate); | |
3111 Library& library = | |
3112 Library::Handle(isolate, isolate->object_store()->root_library()); | |
3113 return Api::NewHandle(isolate, library.raw()); | |
3114 } | |
3115 | |
3116 | |
3117 static void CompileAll(Isolate* isolate, Dart_Handle* result) { | 3068 static void CompileAll(Isolate* isolate, Dart_Handle* result) { |
3118 ASSERT(isolate != NULL); | 3069 ASSERT(isolate != NULL); |
3119 const Error& error = Error::Handle(isolate, Library::CompileAll()); | 3070 const Error& error = Error::Handle(isolate, Library::CompileAll()); |
3120 if (error.IsNull()) { | 3071 if (error.IsNull()) { |
3121 *result = Api::Success(isolate); | 3072 *result = Api::Success(isolate); |
3122 } else { | 3073 } else { |
3123 *result = Api::NewHandle(isolate, error.raw()); | 3074 *result = Api::NewHandle(isolate, error.raw()); |
3124 } | 3075 } |
3125 } | 3076 } |
3126 | 3077 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3192 if (library.IsNull()) { | 3143 if (library.IsNull()) { |
3193 return Api::NewError("%s: library '%s' not found.", | 3144 return Api::NewError("%s: library '%s' not found.", |
3194 CURRENT_FUNC, url_str.ToCString()); | 3145 CURRENT_FUNC, url_str.ToCString()); |
3195 } else { | 3146 } else { |
3196 return Api::NewHandle(isolate, library.raw()); | 3147 return Api::NewHandle(isolate, library.raw()); |
3197 } | 3148 } |
3198 } | 3149 } |
3199 | 3150 |
3200 | 3151 |
3201 DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle url, | 3152 DART_EXPORT Dart_Handle Dart_LoadLibrary(Dart_Handle url, |
3202 Dart_Handle source) { | 3153 Dart_Handle source, |
| 3154 Dart_Handle import_map) { |
3203 TIMERSCOPE(time_script_loading); | 3155 TIMERSCOPE(time_script_loading); |
3204 Isolate* isolate = Isolate::Current(); | 3156 Isolate* isolate = Isolate::Current(); |
3205 DARTSCOPE(isolate); | 3157 DARTSCOPE(isolate); |
3206 const String& url_str = Api::UnwrapStringHandle(isolate, url); | 3158 const String& url_str = Api::UnwrapStringHandle(isolate, url); |
3207 if (url_str.IsNull()) { | 3159 if (url_str.IsNull()) { |
3208 RETURN_TYPE_ERROR(isolate, url, String); | 3160 RETURN_TYPE_ERROR(isolate, url, String); |
3209 } | 3161 } |
3210 const String& source_str = Api::UnwrapStringHandle(isolate, source); | 3162 const String& source_str = Api::UnwrapStringHandle(isolate, source); |
3211 if (source_str.IsNull()) { | 3163 if (source_str.IsNull()) { |
3212 RETURN_TYPE_ERROR(isolate, source, String); | 3164 RETURN_TYPE_ERROR(isolate, source, String); |
3213 } | 3165 } |
| 3166 const Array& mapping_array = Api::UnwrapArrayHandle(isolate, import_map); |
3214 Library& library = Library::Handle(isolate, Library::LookupLibrary(url_str)); | 3167 Library& library = Library::Handle(isolate, Library::LookupLibrary(url_str)); |
3215 if (library.IsNull()) { | 3168 if (library.IsNull()) { |
3216 library = Library::New(url_str); | 3169 library = Library::New(url_str); |
| 3170 if (mapping_array.IsNull()) { |
| 3171 library.set_import_map(Array::Handle(isolate, Array::Empty())); |
| 3172 } else { |
| 3173 library.set_import_map(mapping_array); |
| 3174 } |
3217 library.Register(); | 3175 library.Register(); |
3218 } else if (!library.LoadNotStarted()) { | 3176 } else if (!library.LoadNotStarted()) { |
3219 // The source for this library has either been loaded or is in the | 3177 // The source for this library has either been loaded or is in the |
3220 // process of loading. Return an error. | 3178 // process of loading. Return an error. |
3221 return Api::NewError("%s: library '%s' has already been loaded.", | 3179 return Api::NewError("%s: library '%s' has already been loaded.", |
3222 CURRENT_FUNC, url_str.ToCString()); | 3180 CURRENT_FUNC, url_str.ToCString()); |
3223 } | 3181 } |
3224 Dart_Handle result; | 3182 Dart_Handle result; |
3225 CompileSource(isolate, | 3183 CompileSource(isolate, |
3226 library, | 3184 library, |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3325 *buffer = NULL; | 3283 *buffer = NULL; |
3326 } | 3284 } |
3327 delete debug_region; | 3285 delete debug_region; |
3328 } else { | 3286 } else { |
3329 *buffer = NULL; | 3287 *buffer = NULL; |
3330 *buffer_size = 0; | 3288 *buffer_size = 0; |
3331 } | 3289 } |
3332 } | 3290 } |
3333 | 3291 |
3334 } // namespace dart | 3292 } // namespace dart |
OLD | NEW |