OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "bin/extensions.h" |
| 6 |
| 7 #include <stdio.h> |
| 8 |
| 9 #include "include/dart_api.h" |
| 10 #include "platform/assert.h" |
| 11 #include "platform/globals.h" |
| 12 #include "bin/dartutils.h" |
| 13 |
| 14 Dart_Handle Extensions::LoadExtension(const char* extension_url, |
| 15 Dart_Handle parent_library) { |
| 16 ASSERT(DartUtils::IsDartExtensionSchemeURL(extension_url)); |
| 17 const char* library_name = |
| 18 extension_url + strlen(DartUtils::kDartExtensionScheme); |
| 19 if (strchr(library_name, '/') != NULL || |
| 20 strchr(library_name, '\\') != NULL) { |
| 21 return Dart_Error("path components not allowed in extension library name"); |
| 22 } |
| 23 void* library_handle = LoadExtensionLibrary(library_name); |
| 24 if (!library_handle) { |
| 25 return Dart_Error("cannot find extension library"); |
| 26 } |
| 27 |
| 28 const char* strings[3] = { library_name, "_Init", NULL }; |
| 29 char* init_function_name = Concatenate(strings); |
| 30 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); |
| 31 InitFunctionType fn = reinterpret_cast<InitFunctionType>( |
| 32 ResolveSymbol(library_handle, init_function_name)); |
| 33 free(init_function_name); |
| 34 |
| 35 if (fn == NULL) { |
| 36 return Dart_Error("cannot find initialization function in extension"); |
| 37 } |
| 38 return (*fn)(parent_library); |
| 39 } |
| 40 |
| 41 |
| 42 // Concatenates a NULL terminated array of strings. |
| 43 // The returned string must be freed. |
| 44 char* Extensions::Concatenate(const char** strings) { |
| 45 int size = 1; // null termination. |
| 46 for (int i = 0; strings[i] != NULL; i++) { |
| 47 size += strlen(strings[i]); |
| 48 } |
| 49 char* result = reinterpret_cast<char*>(malloc(size)); |
| 50 int index = 0; |
| 51 for (int i = 0; strings[i] != NULL; i++) { |
| 52 index += snprintf(result + index, size - index, "%s", strings[i]); |
| 53 } |
| 54 ASSERT(index == size - 1); |
| 55 ASSERT(result[size - 1] == '\0'); |
| 56 return result; |
| 57 } |
OLD | NEW |