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