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

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

Issue 9465004: Add native extensions for the Dart shell to mac and windows (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add MacOS implementation, refactor and create extensions.cc Created 8 years, 9 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 | Annotate | Revision Log
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"
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.
5 #include <stdio.h> 6 #include <stdio.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"
12 #include "bin/dartutils.h" 11 #include "bin/dartutils.h"
13 12
14 Dart_Handle Extensions::LoadExtension(const char* extension_url, 13 Dart_Handle Extensions::LoadExtension(const char* extension_url,
15 Dart_Handle parent_library) { 14 Dart_Handle parent_library) {
16 // TODO(whesse): Consider making loading extensions lazy, so the 15 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.
17 // dynamic library is loaded only when first native function is called.
18 ASSERT(DartUtils::IsDartExtensionSchemeURL(extension_url));
19 const char* library_name = 16 const char* library_name =
20 extension_url + strlen(DartUtils::kDartExtensionScheme); 17 extension_url + strlen(DartUtils::kDartExtensionScheme);
21 if (strchr(library_name, '/') != NULL) { 18 if (strchr(library_name, '/') != NULL ||
19 strchr(library_name, '\\') != NULL) {
22 return Dart_Error("path components not allowed in extension library name"); 20 return Dart_Error("path components not allowed in extension library name");
23 } 21 }
24 const int buffer_length = strlen(library_name) + strlen("./lib.so") + 1; 22 void* library_handle = LoadExtensionLibrary(library_name);
25 char* library_path = new char[buffer_length]; 23 if (!library_handle) {
26 snprintf(library_path, buffer_length, "./lib%s.so", library_name);
27
28 void* lib_handle = dlopen(library_path, RTLD_LAZY);
29 if (!lib_handle) {
30 delete[] library_path;
31 return Dart_Error("cannot find extension library"); 24 return Dart_Error("cannot find extension library");
32 } 25 }
33 // Reuse library_path buffer for intialization function name. 26
34 char* library_init_function = library_path; 27 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
35 snprintf(library_init_function, buffer_length, "%s_Init", library_name);
36 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map); 28 typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map);
37 InitFunctionType fn = reinterpret_cast<InitFunctionType>( 29 InitFunctionType fn = reinterpret_cast<InitFunctionType>(
38 dlsym(lib_handle, library_init_function)); 30 ResolveSymbol(library_handle, init_function_name));
39 delete[] library_path; 31 delete[] init_function_name;
40 char *error = dlerror(); 32
41 if (error != NULL) { 33 if (fn == NULL) {
42 return Dart_Error(error); 34 return Dart_Error("cannot find initialization function in extension");
43 } 35 }
44 return (*fn)(parent_library); 36 return (*fn)(parent_library);
45 } 37 }
38
39 /* 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.
40 * The string must be deleted by the caller. */
41 char* Extensions::MakeString(const char* format, const char* name) {
42 const int buffer_length = strlen(name) + strlen(format) + 1;
43 char* new_string = new char[buffer_length];
44 snprintf(new_string, buffer_length, format, name);
45 return new_string;
46 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698