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
use std::{cell::RefCell, mem::replace};

use swc_common::Span;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StackOverflowError {
    pub span: Span,
}

pub struct StartGuard {
    /// Previous value.
    prev: usize,
}

impl Drop for StartGuard {
    fn drop(&mut self) {
        with_ctx(|v| *v = self.prev)
    }
}

/// Start tracking for stack overflows. [track] will return error on `max`-th
/// call. (Currently it's noop)
pub fn start(max: usize) -> StartGuard {
    let prev = with_ctx(|v| replace(v, max));
    StartGuard { prev }
}

pub struct TrackGuard {
    _priv: (),
}

impl Drop for TrackGuard {
    fn drop(&mut self) {
        with_ctx(|v| *v += 1)
    }
}

/// Should be stored as a variable like `let _stack = stack::track(span);`.
pub fn track(span: Span) -> Result<TrackGuard, StackOverflowError> {
    with_ctx(|v| {
        if *v == 0 {
            tracing::error!("Stack overflow: {:?}", span);
            return Err(StackOverflowError { span });
        }

        *v -= 1;

        Ok(TrackGuard { _priv: () })
    })
}

/// closure argument: Stack left
fn with_ctx<T>(f: impl FnOnce(&mut usize) -> T) -> T {
    thread_local! {
        static CTX: RefCell<usize> = RefCell::new(0);
    }
    CTX.with(|ctx| f(&mut ctx.borrow_mut()))
}