Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement uv_dlopen and friends

  • Loading branch information...
commit 16e0314c01d7d75396645bd8a0ab751e218da7a8 1 parent 721bc72
@piscisaureus piscisaureus authored
View
4 include/uv-private/uv-unix.h
@@ -43,6 +43,10 @@ typedef struct {
typedef int uv_file;
+/* Platform-specific definitions for uv_dlopen support. */
+typedef void* uv_lib_t;
+#define UV_DYNAMIC /* empty */
+
#define UV_LOOP_PRIVATE_FIELDS \
ares_channel channel; \
/* \
View
4 include/uv-private/uv-win.h
@@ -137,6 +137,10 @@ typedef struct uv_buf_t {
typedef int uv_file;
+/* Platform-specific definitions for uv_dlopen support. */
+typedef HMODULE uv_lib_t;
+#define UV_DYNAMIC FAR WINAPI
+
RB_HEAD(uv_timer_tree_s, uv_timer_s);
#define UV_LOOP_PRIVATE_FIELDS \
View
13 include/uv.h
@@ -1188,6 +1188,19 @@ UV_EXTERN uint64_t uv_get_total_memory(void);
UV_EXTERN extern uint64_t uv_hrtime(void);
+/*
+ * Opens a shared library. The filename is in utf-8. On success, -1 is
+ * and the variable pointed by library receives a handle to the library.
+ */
+UV_EXTERN uv_err_t uv_dlopen(const char* filename, uv_lib_t* library);
+UV_EXTERN uv_err_t uv_dlclose(uv_lib_t library);
+
+/*
+ * Retrieves a data pointer from a dynamic library.
+ */
+UV_EXTERN uv_err_t uv_dlsym(uv_lib_t library, const char* name, void** ptr);
+
+
/* the presence of these unions force similar struct layout */
union uv_any_handle {
uv_tcp_t tcp;
View
59 src/unix/dl.c
@@ -0,0 +1,59 @@
+/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "uv.h"
+#include "internal.h"
+
+#include <dlfcn.h>
+#include <errno.h>
+
+
+const uv_err_t uv_ok_ = { UV_OK, 0 };
+
+uv_err_t uv_dlopen(const char* filename, uv_lib_t* library) {
+ void* handle = dlopen(filename, RTLD_LAZY);
+ if (handle == NULL) {
+ return uv__new_sys_error(errno);
+ }
+
+ *library = handle;
+ return uv_ok_;
+}
+
+
+uv_err_t uv_dlclose(uv_lib_t library) {
+ if (dlclose(library) != 0) {
+ return uv__new_sys_error(errno);
+ }
+
+ return uv_ok_;
+}
+
+
+uv_err_t uv_dlsym(uv_lib_t library, const char* name, void** ptr) {
+ void* address = dlsym(library, name);
+ if (address == NULL) {
+ return uv__new_sys_error(errno);
+ }
+
+ *ptr = (void*) address;
+ return uv_ok_;
+}
View
63 src/win/dl.c
@@ -0,0 +1,63 @@
+/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "uv.h"
+#include "internal.h"
+
+
+uv_err_t uv_dlopen(const char* filename, uv_lib_t* library) {
+ wchar_t filename_w[32768];
+ HMODULE handle;
+
+ if (!uv_utf8_to_utf16(filename,
+ filename_w,
+ sizeof(filename_w) / sizeof(wchar_t))) {
+ return uv__new_sys_error(GetLastError());
+ }
+
+ handle = LoadLibraryW(filename_w);
+ if (handle == NULL) {
+ return uv__new_sys_error(GetLastError());
+ }
+
+ *library = handle;
+ return uv_ok_;
+}
+
+
+uv_err_t uv_dlclose(uv_lib_t library) {
+ if (!FreeLibrary(library)) {
+ return uv__new_sys_error(GetLastError());
+ }
+
+ return uv_ok_;
+}
+
+
+uv_err_t uv_dlsym(uv_lib_t library, const char* name, void** ptr) {
+ FARPROC proc = GetProcAddress(library, name);
+ if (proc == NULL) {
+ return uv__new_sys_error(GetLastError());
+ }
+
+ *ptr = (void*) proc;
+ return uv_ok_;
+}
View
2  uv.gyp
@@ -130,6 +130,7 @@
'src/win/async.c',
'src/win/cares.c',
'src/win/core.c',
+ 'src/win/dl.c',
'src/win/error.c',
'src/win/fs.c',
'src/win/fs-event.c',
@@ -182,6 +183,7 @@
'src/unix/tty.c',
'src/unix/stream.c',
'src/unix/cares.c',
+ 'src/unix/dl.c',
'src/unix/error.c',
'src/unix/process.c',
'src/unix/internal.h',
Please sign in to comment.
Something went wrong with that request. Please try again.