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 <stdio.h> | 5 #include <stdio.h> |
6 #include <dlfcn.h> | 6 #include <dlfcn.h> |
7 | 7 |
8 #include "include/dart_api.h" | 8 #include "include/dart_api.h" |
9 #include "platform/assert.h" | 9 #include "platform/assert.h" |
10 #include "platform/globals.h" | 10 #include "platform/globals.h" |
11 #include "bin/extensions.h" | 11 #include "bin/extensions.h" |
12 #include "bin/dartutils.h" | 12 #include "bin/dartutils.h" |
13 | 13 |
14 Dart_Handle Extensions::LoadExtension(const char* extension_url, | 14 Dart_Handle Extensions::LoadExtension(const char* extension_url, |
15 Dart_Handle parent_library) { | 15 Dart_Handle parent_library) { |
16 // TODO(whesse): Consider making loading extensions lazy, so the | 16 // TODO(whesse): Consider making loading extensions lazy, so the |
17 // dynamic library is loaded only when first native function is called. | 17 // dynamic library is loaded only when first native function is called. |
18 ASSERT(DartUtils::IsDartExtensionSchemeURL(extension_url)); | 18 ASSERT(DartUtils::IsDartExtensionSchemeURL(extension_url)); |
19 const char* library_name = | 19 const char* library_name = |
20 extension_url + strlen(DartUtils::kDartExtensionScheme); | 20 extension_url + strlen(DartUtils::kDartExtensionScheme); |
21 if (strchr(library_name, '/') != NULL) { | 21 if (strchr(library_name, '/') != NULL) { |
22 return Dart_Error("path components not allowed in extension library name"); | 22 return Dart_Error("path components not allowed in extension library name"); |
23 } | 23 } |
24 const int buffer_length = strlen(library_name) + strlen("./lib.so") + 1; | 24 const int buffer_length = strlen(library_name) + strlen("./lib.so") + 1; |
Ivan Posva
2012/03/05 23:07:47
Please do not share Linux and Mac OS X code. There
| |
25 char* library_path = new char[buffer_length]; | 25 char* library_path = new char[buffer_length]; |
26 snprintf(library_path, buffer_length, "./lib%s.so", library_name); | 26 snprintf(library_path, buffer_length, "./lib%s.so", library_name); |
27 | 27 |
28 void* lib_handle = dlopen(library_path, RTLD_LAZY); | 28 void* lib_handle = dlopen(library_path, RTLD_LAZY); |
29 if (!lib_handle) { | 29 if (!lib_handle) { |
30 delete[] library_path; | 30 delete[] library_path; |
31 return Dart_Error("cannot find extension library"); | 31 return Dart_Error("cannot find extension library"); |
32 } | 32 } |
33 // Reuse library_path buffer for intialization function name. | 33 // Reuse library_path buffer for intialization function name. |
34 char* library_init_function = library_path; | 34 char* library_init_function = library_path; |
35 snprintf(library_init_function, buffer_length, "%s_Init", library_name); | 35 snprintf(library_init_function, buffer_length, "%s_Init", library_name); |
36 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); | 36 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); |
37 InitFunctionType fn = reinterpret_cast<InitFunctionType>( | 37 InitFunctionType fn = reinterpret_cast<InitFunctionType>( |
38 dlsym(lib_handle, library_init_function)); | 38 dlsym(lib_handle, library_init_function)); |
39 delete[] library_path; | 39 delete[] library_path; |
40 char *error = dlerror(); | 40 char *error = dlerror(); |
41 if (error != NULL) { | 41 if (error != NULL) { |
42 return Dart_Error(error); | 42 return Dart_Error(error); |
43 } | 43 } |
44 return (*fn)(parent_library); | 44 return (*fn)(parent_library); |
45 } | 45 } |
OLD | NEW |