|
| 1 | +use crate::{AbiFunction, Interface}; |
| 2 | +use genco::prelude::*; |
| 3 | +use genco::tokens::static_literal; |
| 4 | + |
| 5 | +pub struct DartGenerator { |
| 6 | + cdylib_name: String, |
| 7 | +} |
| 8 | + |
| 9 | +impl DartGenerator { |
| 10 | + pub fn new(cdylib_name: String) -> Self { |
| 11 | + Self { cdylib_name } |
| 12 | + } |
| 13 | + |
| 14 | + pub fn generate(&self, iface: Interface) -> dart::Tokens { |
| 15 | + quote! { |
| 16 | + #(static_literal("//")) AUTO GENERATED FILE, DO NOT EDIT. |
| 17 | + #(static_literal("//")) |
| 18 | + #(static_literal("//")) Generated by "ffi-gen". |
| 19 | + |
| 20 | + import "dart:ffi" as ffi; |
| 21 | + import "dart:io" show Platform; |
| 22 | + |
| 23 | + class Api { |
| 24 | + #(static_literal("///")) Holds the symbol lookup function. |
| 25 | + final ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName) |
| 26 | + _lookup; |
| 27 | + |
| 28 | + #(static_literal("///")) The symbols are looked up in [dynamicLibrary]. |
| 29 | + Api(ffi.DynamicLibrary dynamicLibrary) |
| 30 | + : _lookup = dynamicLibrary.lookup; |
| 31 | + |
| 32 | + #(static_literal("///")) The symbols are looked up with [lookup]. |
| 33 | + Api.fromLookup( |
| 34 | + ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName) |
| 35 | + lookup) |
| 36 | + : _lookup = lookup; |
| 37 | + |
| 38 | + #(static_literal("///")) The library is loaded from the executable. |
| 39 | + factory Api.loadStatic() { |
| 40 | + return Api(ffi.DynamicLibrary.executable()); |
| 41 | + } |
| 42 | + |
| 43 | + #(static_literal("///")) The library is dynamically loaded. |
| 44 | + factory Api.loadDynamic(String name) { |
| 45 | + return Api(ffi.DynamicLibrary.open(name)); |
| 46 | + } |
| 47 | + |
| 48 | + #(static_literal("///")) The library is loaded based on platform conventions. |
| 49 | + factory Api.load() { |
| 50 | + String? name; |
| 51 | + if (Platform.isLinux) name = #_(#("lib")#(&self.cdylib_name)#(".so")); |
| 52 | + if (Platform.isAndroid) name = #_(#("lib")#(&self.cdylib_name)#(".so")); |
| 53 | + if (Platform.isMacOS) name = #_(#("lib")#(&self.cdylib_name)#(".dylib")); |
| 54 | + if (Platform.isIOS) name = #_(""); |
| 55 | + if (Platform.isWindows) #_(#(&self.cdylib_name)#(".dll")); |
| 56 | + if (name == null) { |
| 57 | + throw UnsupportedError(#_("This platform is not supported.")); |
| 58 | + } |
| 59 | + if (name == "") { |
| 60 | + return Api.loadStatic(); |
| 61 | + } else { |
| 62 | + return Api.loadDynamic(name); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + #(for func in iface.into_functions() => #(self.generate_function(func))) |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + pub fn generate_function(&self, func: AbiFunction) -> dart::Tokens { |
| 72 | + quote! { |
| 73 | + void #(&func.name)() { |
| 74 | + #(format!("_{}", &func.name))(); |
| 75 | + } |
| 76 | + |
| 77 | + late final #(format!("_{}Ptr", &func.name)) = _lookup< |
| 78 | + ffi.NativeFunction< |
| 79 | + ffi.Void Function()>>(#_(#(format!("__{}", &func.name)))); |
| 80 | + |
| 81 | + late final #(format!("_{}", &func.name)) = #(format!("_{}Ptr", &func.name)) |
| 82 | + .asFunction<void Function()>(); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(test)] |
| 88 | +mod tests { |
| 89 | + use super::*; |
| 90 | + use crate::{Abi, RustGenerator}; |
| 91 | + use anyhow::Result; |
| 92 | + use std::io::Write; |
| 93 | + use tempfile::NamedTempFile; |
| 94 | + use trybuild::TestCases; |
| 95 | + |
| 96 | + fn compile_pass(iface: &str, rust: rust::Tokens, dart: dart::Tokens) -> Result<()> { |
| 97 | + let iface = Interface::parse(iface)?; |
| 98 | + let mut rust_file = NamedTempFile::new()?; |
| 99 | + let rust_gen = RustGenerator::new(Abi::Native); |
| 100 | + let rust_tokens = rust_gen.generate(iface.clone()); |
| 101 | + let mut dart_file = NamedTempFile::new()?; |
| 102 | + let dart_gen = DartGenerator::new("compile_pass".to_string()); |
| 103 | + let dart_tokens = dart_gen.generate(iface); |
| 104 | + |
| 105 | + let library_tokens = quote! { |
| 106 | + #rust_tokens |
| 107 | + #rust |
| 108 | + }; |
| 109 | + |
| 110 | + let bin_tokens = quote! { |
| 111 | + #dart_tokens |
| 112 | + |
| 113 | + void main() { |
| 114 | + final api = Api.load(); |
| 115 | + #dart |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + let library = library_tokens.to_file_string()?; |
| 120 | + rust_file.write_all(library.as_bytes())?; |
| 121 | + let bin = bin_tokens.to_file_string()?; |
| 122 | + dart_file.write_all(bin.as_bytes())?; |
| 123 | + |
| 124 | + let library_dir = tempfile::tempdir()?; |
| 125 | + let library_file = library_dir.as_ref().join("libcompile_pass.so"); |
| 126 | + let runner_tokens: rust::Tokens = quote! { |
| 127 | + fn main() { |
| 128 | + use std::process::Command; |
| 129 | + let ret = Command::new("rustc") |
| 130 | + .arg("--crate-name") |
| 131 | + .arg("compile_pass") |
| 132 | + .arg("--crate-type") |
| 133 | + .arg("cdylib") |
| 134 | + .arg("-o") |
| 135 | + .arg(#(quoted(library_file.as_path().to_str().unwrap()))) |
| 136 | + .arg(#(quoted(rust_file.as_ref().to_str().unwrap()))) |
| 137 | + .status() |
| 138 | + .unwrap() |
| 139 | + .success(); |
| 140 | + assert!(ret); |
| 141 | + let ret = Command::new("dart") |
| 142 | + .env("LD_LIBRARY_PATH", #(quoted(library_dir.as_ref().to_str().unwrap()))) |
| 143 | + .arg("run") |
| 144 | + .arg(#(quoted(dart_file.as_ref().to_str().unwrap()))) |
| 145 | + .status() |
| 146 | + .unwrap() |
| 147 | + .success(); |
| 148 | + assert!(ret); |
| 149 | + } |
| 150 | + }; |
| 151 | + |
| 152 | + let mut runner_file = NamedTempFile::new()?; |
| 153 | + let runner = runner_tokens.to_file_string()?; |
| 154 | + runner_file.write_all(runner.as_bytes())?; |
| 155 | + |
| 156 | + let test = TestCases::new(); |
| 157 | + test.pass(runner_file.as_ref()); |
| 158 | + Ok(()) |
| 159 | + } |
| 160 | + |
| 161 | + #[test] |
| 162 | + fn no_args_no_ret() { |
| 163 | + compile_pass( |
| 164 | + "hello_world fn();", |
| 165 | + quote!( |
| 166 | + pub fn hello_world() {} |
| 167 | + ), |
| 168 | + quote!(api.hello_world();), |
| 169 | + ) |
| 170 | + .unwrap(); |
| 171 | + } |
| 172 | +} |
0 commit comments