| 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 "bin/extensions.h" | 5 #include "bin/extensions.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 | 8 |
| 9 #include "bin/dartutils.h" | 9 #include "bin/dartutils.h" |
| 10 #include "bin/file.h" | 10 #include "bin/file.h" |
| 11 #include "bin/platform.h" | 11 #include "bin/platform.h" |
| 12 #include "bin/utils.h" | 12 #include "bin/utils.h" |
| 13 #include "include/dart_api.h" | 13 #include "include/dart_api.h" |
| 14 #include "platform/assert.h" | 14 #include "platform/assert.h" |
| 15 #include "platform/globals.h" | 15 #include "platform/globals.h" |
| 16 | 16 |
| 17 namespace dart { | 17 namespace dart { |
| 18 namespace bin { | 18 namespace bin { |
| 19 | 19 |
| 20 static char PathSeparator() { | 20 static char PathSeparator() { |
| 21 const char* sep = File::PathSeparator(); | 21 const char* sep = File::PathSeparator(); |
| 22 ASSERT(strlen(sep) == 1); | 22 ASSERT(strlen(sep) == 1); |
| 23 return sep[0]; | 23 return sep[0]; |
| 24 } | 24 } |
| 25 | 25 |
| 26 | |
| 27 void* Extensions::MakePathAndResolve(const char* dir, const char* name) { | 26 void* Extensions::MakePathAndResolve(const char* dir, const char* name) { |
| 28 // First try to find the library with a suffix specifying the architecture. | 27 // First try to find the library with a suffix specifying the architecture. |
| 29 { | 28 { |
| 30 const char* path_components[] = { | 29 const char* path_components[] = { |
| 31 dir, | 30 dir, |
| 32 Platform::LibraryPrefix(), | 31 Platform::LibraryPrefix(), |
| 33 name, | 32 name, |
| 34 "-", | 33 "-", |
| 35 Platform::HostArchitecture(), // arm | 34 Platform::HostArchitecture(), // arm |
| 36 ".", | 35 ".", |
| (...skipping 15 matching lines...) Expand all Loading... |
| 52 name, | 51 name, |
| 53 ".", | 52 ".", |
| 54 Platform::LibraryExtension(), // so | 53 Platform::LibraryExtension(), // so |
| 55 NULL, | 54 NULL, |
| 56 }; | 55 }; |
| 57 const char* library_file = Concatenate(path_components); | 56 const char* library_file = Concatenate(path_components); |
| 58 return LoadExtensionLibrary(library_file); | 57 return LoadExtensionLibrary(library_file); |
| 59 } | 58 } |
| 60 } | 59 } |
| 61 | 60 |
| 62 | |
| 63 // IMPORTANT: In the absolute path case, do not extract the filename and search | 61 // IMPORTANT: In the absolute path case, do not extract the filename and search |
| 64 // for that by passing it to LoadLibrary. That can lead to confusion in | 62 // for that by passing it to LoadLibrary. That can lead to confusion in |
| 65 // which the absolute path is wrong, and a different version of the library is | 63 // which the absolute path is wrong, and a different version of the library is |
| 66 // loaded from the standard location. | 64 // loaded from the standard location. |
| 67 void* Extensions::ResolveAbsPathExtension(const char* extension_path) { | 65 void* Extensions::ResolveAbsPathExtension(const char* extension_path) { |
| 68 const char* last_slash = strrchr(extension_path, PathSeparator()) + 1; | 66 const char* last_slash = strrchr(extension_path, PathSeparator()) + 1; |
| 69 char* name = strdup(last_slash); | 67 char* name = strdup(last_slash); |
| 70 char* dir = StringUtils::StrNDup(extension_path, last_slash - extension_path); | 68 char* dir = StringUtils::StrNDup(extension_path, last_slash - extension_path); |
| 71 void* library_handle = MakePathAndResolve(dir, name); | 69 void* library_handle = MakePathAndResolve(dir, name); |
| 72 free(dir); | 70 free(dir); |
| 73 free(name); | 71 free(name); |
| 74 return library_handle; | 72 return library_handle; |
| 75 } | 73 } |
| 76 | 74 |
| 77 | |
| 78 void* Extensions::ResolveExtension(const char* extension_directory, | 75 void* Extensions::ResolveExtension(const char* extension_directory, |
| 79 const char* extension_name) { | 76 const char* extension_name) { |
| 80 // If the path following dart-ext is an absolute path, then only look for the | 77 // If the path following dart-ext is an absolute path, then only look for the |
| 81 // library there. | 78 // library there. |
| 82 if (File::IsAbsolutePath(extension_name)) { | 79 if (File::IsAbsolutePath(extension_name)) { |
| 83 return ResolveAbsPathExtension(extension_name); | 80 return ResolveAbsPathExtension(extension_name); |
| 84 } | 81 } |
| 85 | 82 |
| 86 // If the path following dart-ext is just a file name, first look next to | 83 // If the path following dart-ext is just a file name, first look next to |
| 87 // the importing Dart library. | 84 // the importing Dart library. |
| 88 void* library_handle = | 85 void* library_handle = |
| 89 MakePathAndResolve(extension_directory, extension_name); | 86 MakePathAndResolve(extension_directory, extension_name); |
| 90 if (library_handle != NULL) { | 87 if (library_handle != NULL) { |
| 91 return library_handle; | 88 return library_handle; |
| 92 } | 89 } |
| 93 | 90 |
| 94 // Then pass the library name down to the platform. E.g. dlopen will do its | 91 // Then pass the library name down to the platform. E.g. dlopen will do its |
| 95 // own search in standard search locations. | 92 // own search in standard search locations. |
| 96 return MakePathAndResolve("", extension_name); | 93 return MakePathAndResolve("", extension_name); |
| 97 } | 94 } |
| 98 | 95 |
| 99 | |
| 100 Dart_Handle Extensions::LoadExtension(const char* extension_directory, | 96 Dart_Handle Extensions::LoadExtension(const char* extension_directory, |
| 101 const char* extension_name, | 97 const char* extension_name, |
| 102 Dart_Handle parent_library) { | 98 Dart_Handle parent_library) { |
| 103 void* library_handle = ResolveExtension(extension_directory, extension_name); | 99 void* library_handle = ResolveExtension(extension_directory, extension_name); |
| 104 if (library_handle == NULL) { | 100 if (library_handle == NULL) { |
| 105 return GetError(); | 101 return GetError(); |
| 106 } | 102 } |
| 107 | 103 |
| 108 const char* extension = extension_name; | 104 const char* extension = extension_name; |
| 109 if (File::IsAbsolutePath(extension_name)) { | 105 if (File::IsAbsolutePath(extension_name)) { |
| 110 extension = strrchr(extension_name, PathSeparator()) + 1; | 106 extension = strrchr(extension_name, PathSeparator()) + 1; |
| 111 } | 107 } |
| 112 | 108 |
| 113 const char* strings[] = {extension, "_Init", NULL}; | 109 const char* strings[] = {extension, "_Init", NULL}; |
| 114 const char* init_function_name = Concatenate(strings); | 110 const char* init_function_name = Concatenate(strings); |
| 115 void* init_function = ResolveSymbol(library_handle, init_function_name); | 111 void* init_function = ResolveSymbol(library_handle, init_function_name); |
| 116 Dart_Handle result = GetError(); | 112 Dart_Handle result = GetError(); |
| 117 if (Dart_IsError(result)) { | 113 if (Dart_IsError(result)) { |
| 118 return result; | 114 return result; |
| 119 } | 115 } |
| 120 ASSERT(init_function != NULL); | 116 ASSERT(init_function != NULL); |
| 121 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); | 117 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); |
| 122 InitFunctionType fn = reinterpret_cast<InitFunctionType>(init_function); | 118 InitFunctionType fn = reinterpret_cast<InitFunctionType>(init_function); |
| 123 return (*fn)(parent_library); | 119 return (*fn)(parent_library); |
| 124 } | 120 } |
| 125 | 121 |
| 126 | |
| 127 // Concatenates a NULL terminated array of strings. | 122 // Concatenates a NULL terminated array of strings. |
| 128 // The returned string is scope allocated. | 123 // The returned string is scope allocated. |
| 129 const char* Extensions::Concatenate(const char** strings) { | 124 const char* Extensions::Concatenate(const char** strings) { |
| 130 int size = 1; // null termination. | 125 int size = 1; // null termination. |
| 131 for (int i = 0; strings[i] != NULL; i++) { | 126 for (int i = 0; strings[i] != NULL; i++) { |
| 132 size += strlen(strings[i]); | 127 size += strlen(strings[i]); |
| 133 } | 128 } |
| 134 char* result = reinterpret_cast<char*>(Dart_ScopeAllocate(size)); | 129 char* result = reinterpret_cast<char*>(Dart_ScopeAllocate(size)); |
| 135 int index = 0; | 130 int index = 0; |
| 136 for (int i = 0; strings[i] != NULL; i++) { | 131 for (int i = 0; strings[i] != NULL; i++) { |
| 137 index += snprintf(result + index, size - index, "%s", strings[i]); | 132 index += snprintf(result + index, size - index, "%s", strings[i]); |
| 138 } | 133 } |
| 139 ASSERT(index == size - 1); | 134 ASSERT(index == size - 1); |
| 140 ASSERT(result[size - 1] == '\0'); | 135 ASSERT(result[size - 1] == '\0'); |
| 141 return result; | 136 return result; |
| 142 } | 137 } |
| 143 | 138 |
| 144 } // namespace bin | 139 } // namespace bin |
| 145 } // namespace dart | 140 } // namespace dart |
| OLD | NEW |