-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from dxrcy/stack-env-var
Require `LACE_STACK=1` to use non-standard 'stack' instruction set extension
- Loading branch information
Showing
7 changed files
with
114 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use std::{cell::RefCell, ffi::OsStr}; | ||
|
||
#[derive(Clone, Copy)] | ||
struct Env { | ||
stack_enabled: bool, | ||
} | ||
|
||
thread_local! { | ||
/// Must only be mutated within `set_env` | ||
static ENV: RefCell<Option<Env>> = const { RefCell::new(None) }; | ||
} | ||
|
||
pub fn init() { | ||
let value = Env { | ||
stack_enabled: var_is("LACE_STACK", "1"), | ||
}; | ||
set_env(value); | ||
} | ||
|
||
pub fn is_stack_enabled() -> bool { | ||
with_env(|env| env.stack_enabled) | ||
} | ||
|
||
fn set_env(value: Env) { | ||
ENV.with(|env| { | ||
let mut env = env.borrow_mut(); | ||
assert!( | ||
env.is_none(), | ||
"tried to initialize environment state multiple times" | ||
); | ||
*env = Some(value); | ||
}); | ||
} | ||
|
||
fn with_env<F, R>(callback: F) -> R | ||
where | ||
F: Fn(&Env) -> R, | ||
{ | ||
ENV.with(|env| { | ||
let env = env.borrow(); | ||
let env = env.unwrap_or_else(|| { | ||
panic!("tried to access environment state before initialization"); | ||
}); | ||
callback(&env) | ||
}) | ||
} | ||
|
||
fn var_is(name: impl AsRef<OsStr>, value: impl AsRef<str>) -> bool { | ||
std::env::var(name.as_ref()).is_ok_and(|v| &v == value.as_ref()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,5 @@ pub use symbol::{reset_state, StaticSource}; | |
|
||
mod error; | ||
mod lexer; | ||
|
||
pub mod env; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters