Chromium Code Reviews| 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 "include/dart_api.h" | 9 #include "include/dart_api.h" |
| 10 #include "platform/assert.h" | 10 #include "platform/assert.h" |
| 11 #include "platform/globals.h" | 11 #include "platform/globals.h" |
| 12 #include "bin/dartutils.h" | 12 #include "bin/dartutils.h" |
| 13 #include "bin/file.h" | |
| 13 | 14 |
| 14 Dart_Handle Extensions::LoadExtension(const char* extension_url, | 15 Dart_Handle Extensions::LoadExtension(const char* extension_url, |
| 15 Dart_Handle parent_library) { | 16 Dart_Handle parent_library) { |
| 16 ASSERT(DartUtils::IsDartExtensionSchemeURL(extension_url)); | 17 ASSERT(DartUtils::IsDartExtensionSchemeURL(extension_url)); |
| 17 const char* library_name = | 18 // Make a mutable copy of the extension url, without the "dart:" scheme. |
| 19 const char* path_component = | |
| 18 extension_url + strlen(DartUtils::kDartExtensionScheme); | 20 extension_url + strlen(DartUtils::kDartExtensionScheme); |
| 19 if (strchr(library_name, '/') != NULL || | 21 #ifdef TARGET_OS_WINDOWS |
|
Ivan Posva
2012/03/15 16:18:20
Please no ifdefs, they are generally a sign of bad
Bill Hesse
2012/04/17 14:46:31
Done.
| |
| 20 strchr(library_name, '\\') != NULL) { | 22 // Remove initial '/' from a uri formatted absolute path "/C:/path/to/dll" |
| 21 return Dart_Error("path components not allowed in extension library name"); | 23 ASSERT(path_component[0] == '/'); |
| 24 path_component++; | |
| 25 #endif | |
| 26 char* library_path = strdup(path_component); | |
| 27 if (!library_path || !File::IsAbsolutePath(library_path)) { | |
| 28 free(library_path); | |
| 29 return Dart_Error("unexpected error in library path"); | |
| 22 } | 30 } |
| 23 void* library_handle = LoadExtensionLibrary(library_name); | 31 // Extract the path and the extension name from the url. |
| 32 char* last_path_separator = strrchr(library_path, '/'); | |
| 33 char* extension_name = last_path_separator + 1; | |
| 34 *last_path_separator = '\0'; // Terminate library_path at last separator. | |
| 35 | |
| 36 void* library_handle = LoadExtensionLibrary(library_path, extension_name); | |
| 24 if (!library_handle) { | 37 if (!library_handle) { |
| 38 free(library_path); | |
| 25 return Dart_Error("cannot find extension library"); | 39 return Dart_Error("cannot find extension library"); |
| 26 } | 40 } |
| 27 | 41 |
| 28 const char* strings[3] = { library_name, "_Init", NULL }; | 42 const char* strings[3] = { extension_name, "_Init", NULL }; |
| 29 char* init_function_name = Concatenate(strings); | 43 char* init_function_name = Concatenate(strings); |
| 30 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); | 44 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); |
| 31 InitFunctionType fn = reinterpret_cast<InitFunctionType>( | 45 InitFunctionType fn = reinterpret_cast<InitFunctionType>( |
| 32 ResolveSymbol(library_handle, init_function_name)); | 46 ResolveSymbol(library_handle, init_function_name)); |
| 33 free(init_function_name); | 47 free(init_function_name); |
| 48 free(library_path); | |
| 34 | 49 |
| 35 if (fn == NULL) { | 50 if (fn == NULL) { |
| 36 return Dart_Error("cannot find initialization function in extension"); | 51 return Dart_Error("cannot find initialization function in extension"); |
| 37 } | 52 } |
| 38 return (*fn)(parent_library); | 53 return (*fn)(parent_library); |
| 39 } | 54 } |
| 40 | 55 |
| 41 | 56 |
| 42 // Concatenates a NULL terminated array of strings. | 57 // Concatenates a NULL terminated array of strings. |
| 43 // The returned string must be freed. | 58 // The returned string must be freed. |
| 44 char* Extensions::Concatenate(const char** strings) { | 59 char* Extensions::Concatenate(const char** strings) { |
| 45 int size = 1; // null termination. | 60 int size = 1; // null termination. |
| 46 for (int i = 0; strings[i] != NULL; i++) { | 61 for (int i = 0; strings[i] != NULL; i++) { |
| 47 size += strlen(strings[i]); | 62 size += strlen(strings[i]); |
| 48 } | 63 } |
| 49 char* result = reinterpret_cast<char*>(malloc(size)); | 64 char* result = reinterpret_cast<char*>(malloc(size)); |
| 50 int index = 0; | 65 int index = 0; |
| 51 for (int i = 0; strings[i] != NULL; i++) { | 66 for (int i = 0; strings[i] != NULL; i++) { |
| 52 index += snprintf(result + index, size - index, "%s", strings[i]); | 67 index += snprintf(result + index, size - index, "%s", strings[i]); |
| 53 } | 68 } |
| 54 ASSERT(index == size - 1); | 69 ASSERT(index == size - 1); |
| 55 ASSERT(result[size - 1] == '\0'); | 70 ASSERT(result[size - 1] == '\0'); |
| 56 return result; | 71 return result; |
| 57 } | 72 } |
| OLD | NEW |