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

Unified Diff: runtime/bin/extensions_linux.cc

Issue 9430051: Add native extensions to the Dart shell, on the linux platform. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove stray edits. Created 8 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: runtime/bin/extensions_linux.cc
diff --git a/runtime/bin/extensions_linux.cc b/runtime/bin/extensions_linux.cc
new file mode 100644
index 0000000000000000000000000000000000000000..bf379092b2fbea84c8d3848b8aa62e97ba43eb7b
--- /dev/null
+++ b/runtime/bin/extensions_linux.cc
@@ -0,0 +1,49 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// 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 <stdio.h>
+#include <dlfcn.h>
+
+#include "bin/file.h"
Mads Ager (google) 2012/02/23 09:26:56 Is anything from file used in this file?
+#include "include/dart_api.h"
+#include "platform/assert.h"
+#include "platform/globals.h"
+#include "bin/extensions.h"
+#include "bin/dartutils.h"
+
+// The current implementation allows names only, not paths, and
Mads Ager (google) 2012/02/23 09:26:56 I would get rid of the fixed-size buffer here and
+// PATH_MAX is too large (it may be a huge number).
+const int kBufferLength = 101;
+char library_path[kBufferLength];
+char library_init_function[kBufferLength];
+
+Dart_Handle Extensions::LoadExtension(const char* extension_url,
+ Dart_Handle parent_library) {
+ // TODO(whesse): Make loading extensions lazy, so dynamic library is loaded
+ // only when first native function is called.
+ ASSERT(DartUtils::IsDartExtensionSchemeURL(extension_url));
+ const char* library_name =
+ extension_url + strlen(DartUtils::kDartExtensionScheme);
+ if (strlen(library_name) > kBufferLength - 1 - strlen("./lib.so")) {
+ return Dart_Error("extension library name too long");
+ }
+ if (strchr(library_name, '/') != NULL) {
+ return Dart_Error("path components not allowed in extension library name");
+ }
+ snprintf(library_path, kBufferLength, "./lib%s.so", library_name);
+
+ void* lib_handle = dlopen(library_path, RTLD_LAZY);
+ if (!lib_handle) {
+ return Dart_Error("cannot find extension library");
+ }
+ snprintf(library_init_function, kBufferLength, "%s_Init", library_name);
+ typedef Dart_Handle (*InitFunctionType)(Dart_Handle import_map);
+ InitFunctionType fn = reinterpret_cast<InitFunctionType>(
+ dlsym(lib_handle, library_init_function));
+ char *error = dlerror();
+ if (error != NULL) {
+ return Dart_Error(error);
+ }
+ return (*fn)(parent_library);
+}

Powered by Google App Engine
This is Rietveld 408576698