| 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_url, | 19 Dart_Handle Extensions::LoadExtension(const char* extension_url, |
| 20 Dart_Handle parent_library) { | 20 Dart_Handle parent_library) { |
| 21 char* library_path = strdup(extension_url); | 21 char* library_path = strdup(extension_url); |
| 22 | 22 |
| 23 if (library_path == NULL) { | 23 if (library_path == NULL) { |
| 24 return Dart_Error("Out of memory in LoadExtension"); | 24 return Dart_NewApiError("Out of memory in LoadExtension"); |
| 25 } | 25 } |
| 26 | 26 |
| 27 // Extract the path and the extension name from the url. | 27 // Extract the path and the extension name from the url. |
| 28 char* last_path_separator = strrchr(library_path, '/'); | 28 char* last_path_separator = strrchr(library_path, '/'); |
| 29 char* extension_name = last_path_separator + 1; | 29 char* extension_name = last_path_separator + 1; |
| 30 *last_path_separator = '\0'; // Terminate library_path at last separator. | 30 *last_path_separator = '\0'; // Terminate library_path at last separator. |
| 31 | 31 |
| 32 void* library_handle = LoadExtensionLibrary(library_path, extension_name); | 32 void* library_handle = LoadExtensionLibrary(library_path, extension_name); |
| 33 if (library_handle == NULL) { | 33 if (library_handle == NULL) { |
| 34 free(library_path); | 34 free(library_path); |
| 35 return Dart_Error("cannot find extension library"); | 35 return Dart_NewApiError("cannot find extension library"); |
| 36 } | 36 } |
| 37 | 37 |
| 38 const char* strings[] = { extension_name, "_Init", NULL }; | 38 const char* strings[] = { extension_name, "_Init", NULL }; |
| 39 char* init_function_name = Concatenate(strings); | 39 char* init_function_name = Concatenate(strings); |
| 40 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); | 40 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); |
| 41 InitFunctionType fn = reinterpret_cast<InitFunctionType>( | 41 InitFunctionType fn = reinterpret_cast<InitFunctionType>( |
| 42 ResolveSymbol(library_handle, init_function_name)); | 42 ResolveSymbol(library_handle, init_function_name)); |
| 43 free(init_function_name); | 43 free(init_function_name); |
| 44 free(library_path); | 44 free(library_path); |
| 45 | 45 |
| 46 if (fn == NULL) { | 46 if (fn == NULL) { |
| 47 return Dart_Error("cannot find initialization function in extension"); | 47 return Dart_NewApiError("cannot find initialization function in extension"); |
| 48 } | 48 } |
| 49 return (*fn)(parent_library); | 49 return (*fn)(parent_library); |
| 50 } | 50 } |
| 51 | 51 |
| 52 | 52 |
| 53 // Concatenates a NULL terminated array of strings. | 53 // Concatenates a NULL terminated array of strings. |
| 54 // The returned string must be freed. | 54 // The returned string must be freed. |
| 55 char* Extensions::Concatenate(const char** strings) { | 55 char* Extensions::Concatenate(const char** strings) { |
| 56 int size = 1; // null termination. | 56 int size = 1; // null termination. |
| 57 for (int i = 0; strings[i] != NULL; i++) { | 57 for (int i = 0; strings[i] != NULL; i++) { |
| 58 size += strlen(strings[i]); | 58 size += strlen(strings[i]); |
| 59 } | 59 } |
| 60 char* result = reinterpret_cast<char*>(malloc(size)); | 60 char* result = reinterpret_cast<char*>(malloc(size)); |
| 61 int index = 0; | 61 int index = 0; |
| 62 for (int i = 0; strings[i] != NULL; i++) { | 62 for (int i = 0; strings[i] != NULL; i++) { |
| 63 index += snprintf(result + index, size - index, "%s", strings[i]); | 63 index += snprintf(result + index, size - index, "%s", strings[i]); |
| 64 } | 64 } |
| 65 ASSERT(index == size - 1); | 65 ASSERT(index == size - 1); |
| 66 ASSERT(result[size - 1] == '\0'); | 66 ASSERT(result[size - 1] == '\0'); |
| 67 return result; | 67 return result; |
| 68 } | 68 } |
| 69 | 69 |
| 70 } // namespace bin | 70 } // namespace bin |
| 71 } // namespace dart | 71 } // namespace dart |
| OLD | NEW |