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