1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 Dynamic library facilities.
15 A simple wrapper over the platforms dynamic library facilities
26 pub struct DynamicLibrary { priv handle: *libc::c_void }
28 impl Drop for DynamicLibrary {
30 match do dl::check_for_errors_in {
32 dl::close(self.handle)
36 Err(str) => fail!(str)
42 /// Lazily open a dynamic library. When passed None it gives a
43 /// handle to the calling process
44 pub fn open(filename: Option<&path::Path>) -> Result<DynamicLibrary, ~str> {
45 do dl::check_for_errors_in {
47 DynamicLibrary { handle:
49 Some(name) => dl::open_external(name),
50 None => dl::open_internal()
57 /// Access the value at the symbol of the dynamic library
58 pub unsafe fn symbol<T>(&self, symbol: &str) -> Result<T, ~str> {
59 // This function should have a lifetime constraint of 'self on
60 // T but that feature is still unimplemented
62 do dl::check_for_errors_in {
63 let symbol_value = do symbol.as_c_str |raw_string| {
64 dl::symbol(self.handle, raw_string)
67 cast::transmute(symbol_value)
73 #[ignore(cfg(windows))]
74 priv fn test_loading_cosine () {
75 // The math library does not need to be loaded since it is already
76 // statically linked in
77 let libm = match DynamicLibrary::open(None) {
78 Err (error) => fail!("Could not load self as module: %s", error),
82 // Unfortunately due to issue #6194 it is not possible to call
83 // this as a C function
84 let cosine: extern fn(libc::c_double) -> libc::c_double = unsafe {
85 match libm.symbol("cos") {
86 Err (error) => fail!("Could not load function cos: %s", error),
92 let expected_result = 1.0;
93 let result = cosine(argument);
94 if result != expected_result {
95 fail!("cos(%?) != %? but equaled %? instead", argument,
96 expected_result, result)
100 #[cfg(target_os = "linux")]
101 #[cfg(target_os = "android")]
102 #[cfg(target_os = "macos")]
103 #[cfg(target_os = "freebsd")]
112 pub unsafe fn open_external(filename: &path::Path) -> *libc::c_void {
113 do filename.to_str().as_c_str |raw_name| {
114 dlopen(raw_name, Lazy as libc::c_int)
118 pub unsafe fn open_internal() -> *libc::c_void {
119 dlopen(ptr::null(), Lazy as libc::c_int)
122 pub fn check_for_errors_in<T>(f: &fn()->T) -> Result<T, ~str> {
124 do task::atomically {
125 let _old_error = dlerror();
129 let last_error = dlerror();
130 if ptr::null() == last_error {
133 Err(str::raw::from_c_str(last_error))
139 pub unsafe fn symbol(handle: *libc::c_void, symbol: *libc::c_char) -> *libc::c_void {
140 dlsym(handle, symbol)
142 pub unsafe fn close(handle: *libc::c_void) {
155 fn dlopen(filename: *libc::c_char, flag: libc::c_int) -> *libc::c_void;
156 fn dlerror() -> *libc::c_char;
157 fn dlsym(handle: *libc::c_void, symbol: *libc::c_char) -> *libc::c_void;
158 fn dlclose(handle: *libc::c_void) -> libc::c_int;
162 #[cfg(target_os = "win32")]
172 pub unsafe fn open_external(filename: &path::Path) -> *libc::c_void {
173 do os::win32::as_utf16_p(filename.to_str()) |raw_name| {
174 LoadLibraryW(raw_name)
178 pub unsafe fn open_internal() -> *libc::c_void {
179 let mut handle = ptr::null();
180 GetModuleHandleExW(0 as libc::DWORD, ptr::null(), &handle as **libc::c_void);
184 pub fn check_for_errors_in<T>(f: &fn()->T) -> Result<T, ~str> {
186 do task::atomically {
191 let error = os::errno();
195 Err(fmt!("Error code %?", error))
200 pub unsafe fn symbol(handle: *libc::c_void, symbol: *libc::c_char) -> *libc::c_void {
201 GetProcAddress(handle, symbol)
203 pub unsafe fn close(handle: *libc::c_void) {
204 FreeLibrary(handle); ()
207 #[link_name = "kernel32"]
209 fn SetLastError(error: u32);
210 fn LoadLibraryW(name: *u16) -> *libc::c_void;
211 fn GetModuleHandleExW(dwFlags: libc::DWORD, name: *u16,
212 handle: **libc::c_void) -> *libc::c_void;
213 fn GetProcAddress(handle: *libc::c_void, name: *libc::c_char) -> *libc::c_void;
214 fn FreeLibrary(handle: *libc::c_void);