|
| 1 | +use crate::ir::function::Abi; |
| 2 | +use proc_macro2::Ident; |
| 3 | + |
| 4 | +/// Used to build the output tokens for dynamic bindings. |
| 5 | +pub struct DynamicItems { |
| 6 | + /// Tracks the tokens that will appears inside the library struct -- e.g.: |
| 7 | + /// ```ignore |
| 8 | + /// struct Lib { |
| 9 | + /// __library: ::libloading::Library, |
| 10 | + /// x: Result<unsafe extern ..., ::libloading::Error>, // <- tracks these |
| 11 | + /// ... |
| 12 | + /// } |
| 13 | + /// ``` |
| 14 | + struct_members: Vec<proc_macro2::TokenStream>, |
| 15 | + |
| 16 | + /// Tracks the tokens that will appear inside the library struct's implementation, e.g.: |
| 17 | + /// |
| 18 | + /// ```ignore |
| 19 | + /// impl Lib { |
| 20 | + /// ... |
| 21 | + /// pub unsafe fn foo(&self, ...) { // <- tracks these |
| 22 | + /// ... |
| 23 | + /// } |
| 24 | + /// } |
| 25 | + /// ``` |
| 26 | + struct_implementation: Vec<proc_macro2::TokenStream>, |
| 27 | + |
| 28 | + /// Tracks the tokens that will appear inside the struct used for checking if a symbol is |
| 29 | + /// usable, e.g.: |
| 30 | + /// ```ignore |
| 31 | + /// pub fn f(&self) -> Result<(), &'a ::libloading::Error> { // <- tracks these |
| 32 | + /// self.__library.f.as_ref().map(|_| ()) |
| 33 | + /// } |
| 34 | + /// ``` |
| 35 | + runtime_checks: Vec<proc_macro2::TokenStream>, |
| 36 | + |
| 37 | + /// Tracks the initialization of the fields inside the `::new` constructor of the library |
| 38 | + /// struct, e.g.: |
| 39 | + /// ```ignore |
| 40 | + /// impl Lib { |
| 41 | + /// |
| 42 | + /// pub unsafe fn new<P>(path: P) -> Result<Self, ::libloading::Error> |
| 43 | + /// where |
| 44 | + /// P: AsRef<::std::ffi::OsStr>, |
| 45 | + /// { |
| 46 | + /// ... |
| 47 | + /// let foo = __library.get(...) ...; // <- tracks these |
| 48 | + /// ... |
| 49 | + /// } |
| 50 | + /// |
| 51 | + /// ... |
| 52 | + /// } |
| 53 | + /// ``` |
| 54 | + constructor_inits: Vec<proc_macro2::TokenStream>, |
| 55 | + |
| 56 | + /// Tracks the information that is passed to the library struct at the end of the `::new` |
| 57 | + /// constructor, e.g.: |
| 58 | + /// ```ignore |
| 59 | + /// impl LibFoo { |
| 60 | + /// pub unsafe fn new<P>(path: P) -> Result<Self, ::libloading::Error> |
| 61 | + /// where |
| 62 | + /// P: AsRef<::std::ffi::OsStr>, |
| 63 | + /// { |
| 64 | + /// ... |
| 65 | + /// Ok(LibFoo { |
| 66 | + /// __library: __library, |
| 67 | + /// foo, |
| 68 | + /// bar, // <- tracks these |
| 69 | + /// ... |
| 70 | + /// }) |
| 71 | + /// } |
| 72 | + /// } |
| 73 | + /// ``` |
| 74 | + init_fields: Vec<proc_macro2::TokenStream>, |
| 75 | +} |
| 76 | + |
| 77 | +impl Default for DynamicItems { |
| 78 | + fn default() -> Self { |
| 79 | + DynamicItems { |
| 80 | + struct_members: vec![], |
| 81 | + struct_implementation: vec![], |
| 82 | + runtime_checks: vec![], |
| 83 | + constructor_inits: vec![], |
| 84 | + init_fields: vec![], |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl DynamicItems { |
| 90 | + pub fn new() -> Self { |
| 91 | + Self::default() |
| 92 | + } |
| 93 | + |
| 94 | + pub fn get_tokens( |
| 95 | + &self, |
| 96 | + lib_ident: Ident, |
| 97 | + check_struct_ident: Ident, |
| 98 | + ) -> proc_macro2::TokenStream { |
| 99 | + let struct_members = &self.struct_members; |
| 100 | + let constructor_inits = &self.constructor_inits; |
| 101 | + let init_fields = &self.init_fields; |
| 102 | + let struct_implementation = &self.struct_implementation; |
| 103 | + let runtime_checks = &self.runtime_checks; |
| 104 | + quote! { |
| 105 | + extern crate libloading; |
| 106 | + |
| 107 | + pub struct #lib_ident { |
| 108 | + __library: ::libloading::Library, |
| 109 | + #(#struct_members)* |
| 110 | + } |
| 111 | + |
| 112 | + impl #lib_ident { |
| 113 | + pub unsafe fn new<P>( |
| 114 | + path: P |
| 115 | + ) -> Result<Self, ::libloading::Error> |
| 116 | + where P: AsRef<::std::ffi::OsStr> { |
| 117 | + let __library = ::libloading::Library::new(path)?; |
| 118 | + #( #constructor_inits )* |
| 119 | + Ok( |
| 120 | + #lib_ident { |
| 121 | + __library: __library, |
| 122 | + #( #init_fields ),* |
| 123 | + } |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + pub fn can_call(&self) -> #check_struct_ident { |
| 128 | + #check_struct_ident { __library: self } |
| 129 | + } |
| 130 | + |
| 131 | + #( #struct_implementation )* |
| 132 | + } |
| 133 | + |
| 134 | + pub struct #check_struct_ident<'a> { |
| 135 | + __library: &'a #lib_ident, |
| 136 | + } |
| 137 | + |
| 138 | + impl<'a> #check_struct_ident<'a> { |
| 139 | + #( #runtime_checks )* |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + pub fn add_function( |
| 145 | + &mut self, |
| 146 | + ident: Ident, |
| 147 | + abi: Abi, |
| 148 | + args: Vec<proc_macro2::TokenStream>, |
| 149 | + args_identifiers: Vec<proc_macro2::TokenStream>, |
| 150 | + ret: proc_macro2::TokenStream, |
| 151 | + ret_ty: proc_macro2::TokenStream, |
| 152 | + ) { |
| 153 | + assert_eq!(args.len(), args_identifiers.len()); |
| 154 | + |
| 155 | + self.struct_members.push(quote!{ |
| 156 | + #ident: Result<unsafe extern #abi fn ( #( #args ),* ) #ret, ::libloading::Error>, |
| 157 | + }); |
| 158 | + |
| 159 | + self.struct_implementation.push(quote! { |
| 160 | + pub unsafe fn #ident ( &self, #( #args ),* ) -> #ret_ty { |
| 161 | + let sym = self.#ident.as_ref().expect("Expected function, got error."); |
| 162 | + (sym)(#( #args_identifiers ),*) |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + self.runtime_checks.push(quote! { |
| 167 | + pub fn #ident (&self) -> Result<(), &'a::libloading::Error> { |
| 168 | + self.__library.#ident.as_ref().map(|_| ()) |
| 169 | + } |
| 170 | + }); |
| 171 | + |
| 172 | + let ident_str = ident.to_string(); |
| 173 | + self.constructor_inits.push(quote! { |
| 174 | + let #ident = __library.get(#ident_str.as_bytes()).map(|sym| *sym); |
| 175 | + }); |
| 176 | + |
| 177 | + self.init_fields.push(quote! { |
| 178 | + #ident |
| 179 | + }); |
| 180 | + } |
| 181 | +} |
0 commit comments