1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::{cell::Cell, marker::PhantomData};

use crate::error::{Error, ErrorKind};

struct StackLimit {
	max_stack_size: Cell<usize>,
	current_depth: Cell<usize>,
}

#[cfg(feature = "nightly")]
#[thread_local]
static STACK_LIMIT: StackLimit = StackLimit {
	max_stack_size: Cell::new(200),
	current_depth: Cell::new(0),
};
#[cfg(not(feature = "nightly"))]
thread_local! {
	static STACK_LIMIT: StackLimit = StackLimit {
		max_stack_size: Cell::new(200),
		current_depth: Cell::new(0),
	};
}

pub struct StackOverflowError;
impl From<StackOverflowError> for ErrorKind {
	fn from(_: StackOverflowError) -> Self {
		ErrorKind::StackOverflow
	}
}
impl From<StackOverflowError> for Error {
	fn from(_: StackOverflowError) -> Self {
		ErrorKind::StackOverflow.into()
	}
}

/// Used to implement stack depth limitation
pub struct StackDepthGuard(PhantomData<()>);
impl Drop for StackDepthGuard {
	#[cfg(feature = "nightly")]
	fn drop(&mut self) {
		STACK_LIMIT
			.current_depth
			.set(STACK_LIMIT.current_depth.get() - 1)
	}
	#[cfg(not(feature = "nightly"))]
	fn drop(&mut self) {
		STACK_LIMIT.with(|limit| limit.current_depth.set(limit.current_depth.get() - 1));
	}
}

// #[cfg(feature = "nightly")]
pub fn check_depth() -> Result<StackDepthGuard, StackOverflowError> {
	fn internal(limit: &StackLimit) -> Result<StackDepthGuard, StackOverflowError> {
		let current = limit.current_depth.get();
		if current < limit.max_stack_size.get() {
			limit.current_depth.set(current + 1);
			Ok(StackDepthGuard(PhantomData))
		} else {
			Err(StackOverflowError)
		}
	}
	#[cfg(feature = "nightly")]
	{
		internal(&STACK_LIMIT)
	}
	#[cfg(not(feature = "nightly"))]
	{
		STACK_LIMIT.with(internal)
	}
}

pub struct StackDepthLimitOverrideGuard {
	old_limit: usize,
}
impl Drop for StackDepthLimitOverrideGuard {
	#[cfg(feature = "nightly")]
	fn drop(&mut self) {
		STACK_LIMIT.max_stack_size.set(self.old_limit)
	}
	#[cfg(not(feature = "nightly"))]
	fn drop(&mut self) {
		STACK_LIMIT.with(|limit| limit.max_stack_size.set(self.old_limit));
	}
}

pub fn limit_stack_depth(depth_limit: usize) -> StackDepthLimitOverrideGuard {
	fn internal(limit: &StackLimit, depth_limit: usize) -> StackDepthLimitOverrideGuard {
		let old_limit = limit.max_stack_size.get();
		let current_depth = limit.current_depth.get();

		limit.max_stack_size.set(current_depth + depth_limit);
		StackDepthLimitOverrideGuard { old_limit }
	}
	#[cfg(feature = "nightly")]
	{
		internal(&STACK_LIMIT, depth_limit)
	}
	#[cfg(not(feature = "nightly"))]
	{
		STACK_LIMIT.with(|limit| internal(limit, depth_limit))
	}
}

/// Like [`limit_stack_depth`], but set depth is not guarded, and will be kept
///
/// Used to implement `set_max_stack` in C api, prefer to use [`limit_stack_depth`] instead
pub fn set_stack_depth_limit(depth_limit: usize) {
	std::mem::forget(limit_stack_depth(depth_limit));
}