Initialize dummy globals for tests
This commit is contained in:
parent
e543b6a030
commit
d550a9b124
Notes:
git
2025-04-18 13:48:48 +00:00
@ -137,6 +137,23 @@ impl CodeBlock {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl CodeBlock {
|
||||
/// Stubbed CodeBlock for testing. Can't execute generated code.
|
||||
pub fn new_dummy() -> Self {
|
||||
use std::ptr::NonNull;
|
||||
use crate::virtualmem::*;
|
||||
use crate::virtualmem::tests::TestingAllocator;
|
||||
|
||||
let mem_size = 1024;
|
||||
let alloc = TestingAllocator::new(mem_size);
|
||||
let mem_start: *const u8 = alloc.mem_start();
|
||||
let virt_mem = VirtualMem::new(alloc, 1, NonNull::new(mem_start as *mut u8).unwrap(), mem_size, 128 * 1024 * 1024);
|
||||
|
||||
Self::new(Rc::new(RefCell::new(virt_mem)))
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::virtualmem::CodePtrBase for CodeBlock {
|
||||
fn base_ptr(&self) -> std::ptr::NonNull<u8> {
|
||||
self.mem_block.borrow().base_ptr()
|
||||
|
@ -1,4 +1,6 @@
|
||||
use crate::{asm::CodeBlock, backend::ir::*, cruby::*, get_option, ir::{self, Function, Insn::*}, virtualmem::CodePtr};
|
||||
use crate::{asm::CodeBlock, backend::ir::*, cruby::*, ir::{self, Function, Insn::*}, virtualmem::CodePtr};
|
||||
#[cfg(feature = "disasm")]
|
||||
use crate::get_option;
|
||||
|
||||
/// Compile SSA IR into machine code
|
||||
pub fn gen_function(cb: &mut CodeBlock, function: &Function) -> Option<CodePtr> {
|
||||
|
@ -791,6 +791,8 @@ pub use manual_defs::*;
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod test_utils {
|
||||
use crate::state::ZJITState;
|
||||
|
||||
use super::*;
|
||||
|
||||
pub fn with_rubyvm(mut func: impl FnMut()) {
|
||||
@ -812,6 +814,9 @@ pub mod test_utils {
|
||||
Qnil
|
||||
}
|
||||
|
||||
// Set up globals for convenience
|
||||
ZJITState::init();
|
||||
|
||||
let mut state: c_int = 0;
|
||||
unsafe { super::rb_protect(Some(callback_wrapper), VALUE((&mut data) as *mut _ as usize), &mut state) };
|
||||
// TODO(alan): there should be a way to print the exception instead of swallowing it
|
||||
|
@ -23,6 +23,7 @@ use crate::cruby::*;
|
||||
pub static mut rb_zjit_enabled_p: bool = false;
|
||||
|
||||
/// Initialize ZJIT, given options allocated by rb_zjit_init_options()
|
||||
#[cfg(not(test))]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn rb_zjit_init(options: *const u8) {
|
||||
// Catch panics to avoid UB for unwinding into C frames.
|
||||
|
@ -27,11 +27,16 @@ pub(crate) use get_option;
|
||||
/// passed to rb_zjit_init() for initialization.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn rb_zjit_init_options() -> *const u8 {
|
||||
let options = Options {
|
||||
let options = init_options();
|
||||
Box::into_raw(Box::new(options)) as *const u8
|
||||
}
|
||||
|
||||
/// Return an Options with default values
|
||||
pub fn init_options() -> Options {
|
||||
Options {
|
||||
dump_ssa: false,
|
||||
dump_disasm: false,
|
||||
};
|
||||
Box::into_raw(Box::new(options)) as *const u8
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a --zjit* command-line flag
|
||||
|
@ -1,4 +1,3 @@
|
||||
use std::mem;
|
||||
use crate::options::Options;
|
||||
use crate::asm::CodeBlock;
|
||||
|
||||
@ -16,8 +15,8 @@ static mut ZJIT_STATE: Option<ZJITState> = None;
|
||||
|
||||
impl ZJITState {
|
||||
/// Initialize the ZJIT globals, given options allocated by rb_zjit_init_options()
|
||||
#[cfg(not(test))]
|
||||
pub fn init(options: *const u8) {
|
||||
#[cfg(not(test))]
|
||||
let cb = {
|
||||
use crate::cruby::*;
|
||||
|
||||
@ -55,17 +54,25 @@ impl ZJITState {
|
||||
};
|
||||
|
||||
let options = unsafe { Box::from_raw(options as *mut Options) };
|
||||
#[cfg(not(test))] // TODO: can we get rid of this #[cfg]?
|
||||
{
|
||||
let zjit_state = ZJITState {
|
||||
code_block: cb,
|
||||
options: *options,
|
||||
};
|
||||
let zjit_state = ZJITState {
|
||||
code_block: cb,
|
||||
options: *options,
|
||||
};
|
||||
|
||||
// Initialize the codegen globals instance
|
||||
unsafe { ZJIT_STATE = Some(zjit_state); }
|
||||
}
|
||||
mem::drop(options);
|
||||
// Initialize the codegen globals instance
|
||||
unsafe { ZJIT_STATE = Some(zjit_state); }
|
||||
std::mem::drop(options);
|
||||
}
|
||||
|
||||
/// Initialize the ZJIT globals for tests
|
||||
#[cfg(test)]
|
||||
pub fn init() {
|
||||
use crate::options::init_options;
|
||||
let zjit_state = ZJITState {
|
||||
code_block: CodeBlock::new_dummy(),
|
||||
options: init_options(),
|
||||
};
|
||||
unsafe { ZJIT_STATE = Some(zjit_state); }
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the codegen globals instance
|
||||
|
Loading…
x
Reference in New Issue
Block a user