| 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) { |
| 21 if (strncmp(extension_path, "http://", 7) == 0) { | 23 if (strncmp(extension_directory, "http://", 7) == 0 || |
| 24 strncmp(extension_directory, "https://", 8) == 0) { |
| 22 return Dart_NewApiError("Cannot load native extensions over http:"); | 25 return Dart_NewApiError("Cannot load native extensions over http:"); |
| 23 } | 26 } |
| 24 | 27 const char* library_strings[] = { extension_directory, extension_file, NULL }; |
| 25 char* library_path = strdup(extension_path); | 28 char* library_file = Concatenate(library_strings); |
| 26 | 29 void* library_handle = LoadExtensionLibrary(library_file); |
| 27 if (library_path == NULL) { | 30 free(library_file); |
| 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) { | 31 if (library_handle == NULL) { |
| 46 free(library_path); | |
| 47 return Dart_NewApiError("Cannot find extension library"); | 32 return Dart_NewApiError("Cannot find extension library"); |
| 48 } | 33 } |
| 49 | 34 |
| 50 const char* strings[] = { extension_name, "_Init", NULL }; | 35 const char* strings[] = { extension_name, "_Init", NULL }; |
| 51 char* init_function_name = Concatenate(strings); | 36 char* init_function_name = Concatenate(strings); |
| 52 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); | 37 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); |
| 53 InitFunctionType fn = reinterpret_cast<InitFunctionType>( | 38 InitFunctionType fn = reinterpret_cast<InitFunctionType>( |
| 54 ResolveSymbol(library_handle, init_function_name)); | 39 ResolveSymbol(library_handle, init_function_name)); |
| 55 free(init_function_name); | 40 free(init_function_name); |
| 56 free(library_path); | |
| 57 | 41 |
| 58 if (fn == NULL) { | 42 if (fn == NULL) { |
| 59 return Dart_NewApiError("Cannot find initialization function in extension"); | 43 return Dart_NewApiError("Cannot find initialization function in extension"); |
| 60 } | 44 } |
| 61 return (*fn)(parent_library); | 45 return (*fn)(parent_library); |
| 62 } | 46 } |
| 63 | 47 |
| 64 | 48 |
| 65 // Concatenates a NULL terminated array of strings. | 49 // Concatenates a NULL terminated array of strings. |
| 66 // The returned string must be freed. | 50 // The returned string must be freed. |
| 67 char* Extensions::Concatenate(const char** strings) { | 51 char* Extensions::Concatenate(const char** strings) { |
| 68 int size = 1; // null termination. | 52 int size = 1; // null termination. |
| 69 for (int i = 0; strings[i] != NULL; i++) { | 53 for (int i = 0; strings[i] != NULL; i++) { |
| 70 size += strlen(strings[i]); | 54 size += strlen(strings[i]); |
| 71 } | 55 } |
| 72 char* result = reinterpret_cast<char*>(malloc(size)); | 56 char* result = reinterpret_cast<char*>(malloc(size)); |
| 73 int index = 0; | 57 int index = 0; |
| 74 for (int i = 0; strings[i] != NULL; i++) { | 58 for (int i = 0; strings[i] != NULL; i++) { |
| 75 index += snprintf(result + index, size - index, "%s", strings[i]); | 59 index += snprintf(result + index, size - index, "%s", strings[i]); |
| 76 } | 60 } |
| 77 ASSERT(index == size - 1); | 61 ASSERT(index == size - 1); |
| 78 ASSERT(result[size - 1] == '\0'); | 62 ASSERT(result[size - 1] == '\0'); |
| 79 return result; | 63 return result; |
| 80 } | 64 } |
| 81 | 65 |
| 82 } // namespace bin | 66 } // namespace bin |
| 83 } // namespace dart | 67 } // namespace dart |
| OLD | NEW |