Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(887)

Side by Side Diff: runtime/bin/extensions.cc

Issue 1401253004: Cleanup the extensions code to avoid the duplication that was introduced in the quick fix to turn t… (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/bin/extensions.h ('k') | runtime/bin/extensions_android.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 = NULL; 29 void* library_handle = LoadExtensionLibrary(library_file);
30 Dart_Handle result = LoadExtensionLibrary(library_file, &library_handle);
31 free(library_file); 30 free(library_file);
31 if (library_handle == NULL) {
32 return GetError();
33 }
34
35 const char* strings[] = { extension_name, "_Init", NULL };
36 char* init_function_name = Concatenate(strings);
37 void* init_function = ResolveSymbol(library_handle, init_function_name);
38 free(init_function_name);
39 Dart_Handle result = GetError();
32 if (Dart_IsError(result)) { 40 if (Dart_IsError(result)) {
33 return result; 41 return result;
34 } 42 }
35 ASSERT(library_handle != NULL);
36
37 const char* strings[] = { extension_name, "_Init", NULL };
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); 43 ASSERT(init_function != NULL);
46 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); 44 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map);
47 InitFunctionType fn = reinterpret_cast<InitFunctionType>(init_function); 45 InitFunctionType fn = reinterpret_cast<InitFunctionType>(init_function);
48 return (*fn)(parent_library); 46 return (*fn)(parent_library);
49 } 47 }
50 48
51 49
52 // Concatenates a NULL terminated array of strings. 50 // Concatenates a NULL terminated array of strings.
53 // The returned string must be freed. 51 // The returned string must be freed.
54 char* Extensions::Concatenate(const char** strings) { 52 char* Extensions::Concatenate(const char** strings) {
55 int size = 1; // null termination. 53 int size = 1; // null termination.
56 for (int i = 0; strings[i] != NULL; i++) { 54 for (int i = 0; strings[i] != NULL; i++) {
57 size += strlen(strings[i]); 55 size += strlen(strings[i]);
58 } 56 }
59 char* result = reinterpret_cast<char*>(malloc(size)); 57 char* result = reinterpret_cast<char*>(malloc(size));
60 int index = 0; 58 int index = 0;
61 for (int i = 0; strings[i] != NULL; i++) { 59 for (int i = 0; strings[i] != NULL; i++) {
62 index += snprintf(result + index, size - index, "%s", strings[i]); 60 index += snprintf(result + index, size - index, "%s", strings[i]);
63 } 61 }
64 ASSERT(index == size - 1); 62 ASSERT(index == size - 1);
65 ASSERT(result[size - 1] == '\0'); 63 ASSERT(result[size - 1] == '\0');
66 return result; 64 return result;
67 } 65 }
68 66
69 } // namespace bin 67 } // namespace bin
70 } // namespace dart 68 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/bin/extensions.h ('k') | runtime/bin/extensions_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698