diff --git a/xlang/src/plugin.rs b/xlang/src/plugin.rs index a1b23736..1246423a 100644 --- a/xlang/src/plugin.rs +++ b/xlang/src/plugin.rs @@ -7,7 +7,7 @@ pub mod v1 { use xlang_abi::{ - io::{self, Read, Write}, + io::{self, Read, ReadSeek, Write}, prelude::v1::*, result::Result, span::Span, @@ -55,6 +55,10 @@ pub mod v1 { fn set_target(&mut self, targ: &'static TargetProperties<'static>); } + pub trait DriverCallbacks { + fn read_relative_file(&mut self, path: StringView) -> io::Result>; + } + pub trait XLangFrontend: XLangPlugin { fn file_matches(&self, x: StringView) -> bool; fn set_file_path(&mut self, x: StringView); @@ -63,6 +67,7 @@ pub mod v1 { fn required_plugins(&self) -> Vec { Vec::new() } + fn set_callbacks(&mut self, _callbacks: DynBox) {} } pub trait XLangCodegen: XLangPlugin { @@ -76,13 +81,13 @@ pub mod v1 { result::Result, span::Span, string::{String, StringView}, - traits::{AbiSafeTrait, AbiSafeUnsize, DynMut, DynPtrSafe}, + traits::{AbiSafeTrait, AbiSafeUnsize, DynBox, DynMut, DynPtrSafe}, vec::Vec, }; use xlang_struct::File; use xlang_targets::properties::{MachineProperties, TargetProperties}; - use super::{Error, OutputMode, XLangCodegen, XLangFrontend, XLangPlugin}; + use super::{DriverCallbacks, Error, OutputMode, XLangCodegen, XLangFrontend, XLangPlugin}; unsafe impl AbiSafeTrait for dyn XLangPlugin { type VTable = vtable::XLangPlugin; @@ -116,12 +121,28 @@ pub mod v1 { type VTable = vtable::XLangFrontend; } + unsafe impl AbiSafeTrait for dyn DriverCallbacks { + type VTable = vtable::DriverCallbacks; + } + + unsafe impl AbiSafeTrait for dyn DriverCallbacks + Send { + type VTable = vtable::DriverCallbacks; + } + + unsafe impl AbiSafeTrait for dyn DriverCallbacks + Sync { + type VTable = vtable::DriverCallbacks; + } + + unsafe impl AbiSafeTrait for dyn DriverCallbacks + Send + Sync { + type VTable = vtable::DriverCallbacks; + } + mod vtable { use xlang_abi::{ result::Result, span::Span, string::{String, StringView}, - traits::{AbiSafeVTable, DynMut}, + traits::{AbiSafeVTable, DynBox, DynMut}, vec::Vec, }; use xlang_struct::File; @@ -169,6 +190,9 @@ pub mod v1 { xlang_host::rustcall!(unsafe extern "rustcall" fn(*mut (), &MachineProperties)), pub required_plugins: xlang_host::rustcall!(unsafe extern "rustcall" fn(*const ()) -> Vec), + pub set_callbacks: xlang_host::rustcall!( + unsafe extern "rustcall" fn(*mut (), DynBox) + ), } unsafe impl AbiSafeVTable for XLangFrontend {} @@ -197,6 +221,26 @@ pub mod v1 { unsafe impl AbiSafeVTable for XLangCodegen {} unsafe impl AbiSafeVTable for XLangCodegen {} unsafe impl AbiSafeVTable for XLangCodegen {} + + pub struct DriverCallbacks { + pub size: usize, + pub align: usize, + pub dtor: Option, + pub reserved: Option, + pub read_relative_file: xlang_host::rustcall!( + unsafe extern "rustcall" fn( + *mut (), + StringView, + ) -> xlang_abi::io::Result< + DynBox, + > + ), + } + + unsafe impl AbiSafeVTable for DriverCallbacks {} + unsafe impl AbiSafeVTable for DriverCallbacks {} + unsafe impl AbiSafeVTable for DriverCallbacks {} + unsafe impl AbiSafeVTable for DriverCallbacks {} } xlang_host::rustcall! {unsafe extern "rustcall" fn __dtor(x: *mut ()) { @@ -265,6 +309,18 @@ pub mod v1 { } } + xlang_host::rustcall! { + unsafe extern "rustcall" fn __read_relative_file(ptr: *mut (), path: StringView) -> xlang_abi::io::Result>{ + unsafe{&mut *(ptr.cast::())}.read_relative_file(path) + } + } + + xlang_host::rustcall! { + unsafe extern "rustcall" fn __set_callbacks(ptr: *mut (),features: DynBox){ + (&mut *(ptr.cast::())).set_callbacks(features); + } + } + unsafe impl AbiSafeUnsize for dyn XLangPlugin { fn construct_vtable_for() -> &'static Self::VTable { &vtable::XLangPlugin { @@ -294,6 +350,7 @@ pub mod v1 { read_source: __read_source::, set_machine: __set_machine::, required_plugins: __required_plugins::, + set_callbacks: __set_callbacks::, } } } @@ -341,6 +398,10 @@ pub mod v1 { fn required_plugins(&self) -> Vec { unsafe { (self.vtable().required_plugins)(self.as_raw()) } } + + fn set_callbacks(&mut self, callbacks: DynBox) { + unsafe { (self.vtable().set_callbacks)(self.as_raw_mut(), callbacks) } + } } unsafe impl AbiSafeTrait for dyn XLangCodegen { @@ -401,6 +462,27 @@ pub mod v1 { unsafe { (self.vtable().set_features)(self.as_raw_mut(), features) } } } + + unsafe impl AbiSafeUnsize for dyn DriverCallbacks { + fn construct_vtable_for() -> &'static Self::VTable { + &Self::VTable { + size: core::mem::size_of::(), + align: core::mem::align_of::(), + dtor: Some(__dtor::), + reserved: None, + read_relative_file: __read_relative_file::, + } + } + } + + impl DriverCallbacks for dyn DynPtrSafe { + fn read_relative_file( + &mut self, + path: StringView, + ) -> xlang_abi::io::Result> { + unsafe { (self.vtable().read_relative_file)(self.as_raw_mut()) } + } + } } }