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 #include "bin/file.h" |
| 14 | 14 |
| 15 | 15 |
| 16 namespace dart { | 16 namespace dart { |
| 17 namespace bin { | 17 namespace bin { |
| 18 | 18 |
| 19 Dart_Handle Extensions::LoadExtension(const char* extension_path, | 19 Dart_Handle Extensions::LoadExtension(const char* extension_directory, |
| 20 const char* extension_file, | |
| 21 const char* extension_name, | |
| 20 Dart_Handle parent_library) { | 22 Dart_Handle parent_library) { |
|
Søren Gjesse
2014/03/04 13:09:02
extension_directory can be a http/https URI :-)
Bill Hesse
2014/03/04 14:36:31
Added back the check for http, added https.
Done.
| |
| 21 if (strncmp(extension_path, "http://", 7) == 0) { | 23 const char* library_strings[] = { extension_directory, extension_file, NULL }; |
| 22 return Dart_NewApiError("Cannot load native extensions over http:"); | 24 char* library_file = Concatenate(library_strings); |
| 23 } | 25 void* library_handle = LoadExtensionLibrary(library_file); |
| 24 | 26 free(library_file); |
| 25 char* library_path = strdup(extension_path); | |
| 26 | |
| 27 if (library_path == NULL) { | |
| 28 return Dart_NewApiError("Out of memory in LoadExtension"); | |
| 29 } | |
| 30 | |
| 31 // Extract the directory and the extension name from the path. | |
| 32 char* last_path_separator = strrchr(library_path, '/'); | |
| 33 if (last_path_separator == NULL) { | |
| 34 last_path_separator = strrchr(library_path, '\\'); | |
| 35 } | |
| 36 if (last_path_separator == NULL) { | |
| 37 free(library_path); | |
| 38 return Dart_NewApiError("Cannot find extension library directory"); | |
| 39 } | |
| 40 char* extension_name = last_path_separator + 1; | |
| 41 | |
| 42 *last_path_separator = '\0'; // Terminate library_path at last separator. | |
| 43 | |
| 44 void* library_handle = LoadExtensionLibrary(library_path, extension_name); | |
| 45 if (library_handle == NULL) { | 27 if (library_handle == NULL) { |
| 46 free(library_path); | |
| 47 return Dart_NewApiError("Cannot find extension library"); | 28 return Dart_NewApiError("Cannot find extension library"); |
| 48 } | 29 } |
| 49 | 30 |
| 50 const char* strings[] = { extension_name, "_Init", NULL }; | 31 const char* strings[] = { extension_name, "_Init", NULL }; |
| 51 char* init_function_name = Concatenate(strings); | 32 char* init_function_name = Concatenate(strings); |
| 52 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); | 33 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); |
| 53 InitFunctionType fn = reinterpret_cast<InitFunctionType>( | 34 InitFunctionType fn = reinterpret_cast<InitFunctionType>( |
| 54 ResolveSymbol(library_handle, init_function_name)); | 35 ResolveSymbol(library_handle, init_function_name)); |
| 55 free(init_function_name); | 36 free(init_function_name); |
| 56 free(library_path); | |
| 57 | 37 |
| 58 if (fn == NULL) { | 38 if (fn == NULL) { |
| 59 return Dart_NewApiError("Cannot find initialization function in extension"); | 39 return Dart_NewApiError("Cannot find initialization function in extension"); |
| 60 } | 40 } |
| 61 return (*fn)(parent_library); | 41 return (*fn)(parent_library); |
| 62 } | 42 } |
| 63 | 43 |
| 64 | 44 |
| 65 // Concatenates a NULL terminated array of strings. | 45 // Concatenates a NULL terminated array of strings. |
| 66 // The returned string must be freed. | 46 // The returned string must be freed. |
| 67 char* Extensions::Concatenate(const char** strings) { | 47 char* Extensions::Concatenate(const char** strings) { |
| 68 int size = 1; // null termination. | 48 int size = 1; // null termination. |
| 69 for (int i = 0; strings[i] != NULL; i++) { | 49 for (int i = 0; strings[i] != NULL; i++) { |
| 70 size += strlen(strings[i]); | 50 size += strlen(strings[i]); |
| 71 } | 51 } |
| 72 char* result = reinterpret_cast<char*>(malloc(size)); | 52 char* result = reinterpret_cast<char*>(malloc(size)); |
| 73 int index = 0; | 53 int index = 0; |
| 74 for (int i = 0; strings[i] != NULL; i++) { | 54 for (int i = 0; strings[i] != NULL; i++) { |
| 75 index += snprintf(result + index, size - index, "%s", strings[i]); | 55 index += snprintf(result + index, size - index, "%s", strings[i]); |
| 76 } | 56 } |
| 77 ASSERT(index == size - 1); | 57 ASSERT(index == size - 1); |
| 78 ASSERT(result[size - 1] == '\0'); | 58 ASSERT(result[size - 1] == '\0'); |
| 79 return result; | 59 return result; |
| 80 } | 60 } |
| 81 | 61 |
| 82 } // namespace bin | 62 } // namespace bin |
| 83 } // namespace dart | 63 } // namespace dart |
| OLD | NEW |