Chromium Code Reviews| Index: runtime/bin/extensions.cc |
| diff --git a/runtime/bin/extensions_linux.cc b/runtime/bin/extensions.cc |
| similarity index 50% |
| copy from runtime/bin/extensions_linux.cc |
| copy to runtime/bin/extensions.cc |
| index ad6c84b9a3b024524f80f963a76e7cd2464f7d44..7c69727c67c3e93e197230a036e2146770510222 100644 |
| --- a/runtime/bin/extensions_linux.cc |
| +++ b/runtime/bin/extensions.cc |
| @@ -2,44 +2,45 @@ |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| +#include "bin/extensions.h" |
|
Mads Ager (google)
2012/03/07 14:31:48
We usually have a blank line after this include be
Bill Hesse
2012/03/08 12:04:37
Done.
|
| #include <stdio.h> |
| -#include <dlfcn.h> |
| #include "include/dart_api.h" |
| #include "platform/assert.h" |
| #include "platform/globals.h" |
| -#include "bin/extensions.h" |
| #include "bin/dartutils.h" |
| Dart_Handle Extensions::LoadExtension(const char* extension_url, |
| Dart_Handle parent_library) { |
| - // TODO(whesse): Consider making loading extensions lazy, so the |
| - // dynamic library is loaded only when first native function is called. |
| - ASSERT(DartUtils::IsDartExtensionSchemeURL(extension_url)); |
| + assert(DartUtils::IsDartExtensionSchemeURL(extension_url)); |
|
Mads Ager (google)
2012/03/07 14:31:48
Please use ASSERT.
Bill Hesse
2012/03/08 12:04:37
Done.
|
| const char* library_name = |
| extension_url + strlen(DartUtils::kDartExtensionScheme); |
| - if (strchr(library_name, '/') != NULL) { |
| + if (strchr(library_name, '/') != NULL || |
| + strchr(library_name, '\\') != NULL) { |
| return Dart_Error("path components not allowed in extension library name"); |
| } |
| - const int buffer_length = strlen(library_name) + strlen("./lib.so") + 1; |
| - char* library_path = new char[buffer_length]; |
| - snprintf(library_path, buffer_length, "./lib%s.so", library_name); |
| - |
| - void* lib_handle = dlopen(library_path, RTLD_LAZY); |
| - if (!lib_handle) { |
| - delete[] library_path; |
| + void* library_handle = LoadExtensionLibrary(library_name); |
| + if (!library_handle) { |
| return Dart_Error("cannot find extension library"); |
| } |
| - // Reuse library_path buffer for intialization function name. |
| - char* library_init_function = library_path; |
| - snprintf(library_init_function, buffer_length, "%s_Init", library_name); |
| + |
| + char* init_function_name = MakeString("%s_Init", library_name); |
|
Mads Ager (google)
2012/03/07 14:31:48
I think inlining this code here would be just as c
Bill Hesse
2012/03/08 12:04:37
Did you see all the uses of MakeString in the plat
|
| typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); |
| InitFunctionType fn = reinterpret_cast<InitFunctionType>( |
| - dlsym(lib_handle, library_init_function)); |
| - delete[] library_path; |
| - char *error = dlerror(); |
| - if (error != NULL) { |
| - return Dart_Error(error); |
| + ResolveSymbol(library_handle, init_function_name)); |
| + delete[] init_function_name; |
| + |
| + if (fn == NULL) { |
| + return Dart_Error("cannot find initialization function in extension"); |
| } |
| return (*fn)(parent_library); |
| } |
| + |
| +/* Returns a newly allocated string by printing name using format. |
|
Mads Ager (google)
2012/03/07 14:31:48
Please use '//' style comments.
Bill Hesse
2012/03/08 12:04:37
Done.
|
| + * The string must be deleted by the caller. */ |
| +char* Extensions::MakeString(const char* format, const char* name) { |
| + const int buffer_length = strlen(name) + strlen(format) + 1; |
| + char* new_string = new char[buffer_length]; |
| + snprintf(new_string, buffer_length, format, name); |
| + return new_string; |
| +} |