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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use crate::raw_types::{funcs, procs};
use crate::Proc;
use crate::StringRef;
use crate::Value;

pub struct StackFrame {
	pub context: *mut procs::ExecutionContext,
	pub instance: *mut procs::ProcInstance,
	pub proc: Proc,
	pub offset: u16,
	pub usr: Value,
	pub src: Value,
	pub dot: Value,
	pub args: Vec<(Option<StringRef>, Value)>,
	pub locals: Vec<(StringRef, Value)>,
	pub file_name: Option<StringRef>,
	pub line_number: Option<u32>,
	// pub time_to_resume: Option<u32>,
	// TODO: current instruction & bytecode offset
}

pub struct CallStacks {
	pub active: Vec<StackFrame>,
	pub suspended: Vec<Vec<StackFrame>>,
}

impl StackFrame {
	unsafe fn from_context(context: *mut procs::ExecutionContext) -> StackFrame {
		let instance = (*context).proc_instance;

		let proc = Proc::from_id((*instance).proc).unwrap();
		let offset = (*context).bytecode_offset;
		let param_names = proc.parameter_names();
		let local_names = proc.local_names();

		let usr = Value::from_raw((*instance).usr);
		let src = Value::from_raw((*instance).src);
		let dot = Value::from_raw((*context).dot);

		// Make sure to handle arguments/locals with no names (when there are more values than names)
		let args = (0..(*instance).args_count)
			.map(|i| {
				let name = match param_names.get(i as usize) {
					Some(name) => Some(name.clone()),
					None => None,
				};
				(name, Value::from_raw(*((*instance).args).add(i as usize)))
			})
			.collect();

		let locals = (0..(*context).locals_count)
			.map(|i| {
				(
					local_names.get(i as usize).unwrap().clone(),
					Value::from_raw(*((*context).locals).add(i as usize)),
				)
			})
			.collect();

		// Only populate the line number if we've got a file-name
		let mut file_name = None;
		let mut line_number = None;
		if (*context).filename.valid() {
			file_name = Some(StringRef::from_id((*context).filename));
			line_number = Some((*context).line);
		}

		// TODO: When set this? For all sleepers?
		// let time_to_resume = None;

		StackFrame {
			context,
			instance,
			proc,
			offset,
			usr,
			src,
			dot,
			args,
			locals,
			file_name,
			line_number,
			// time_to_resume,
		}
	}
}

enum CallStackKind {
	Active,
	Suspended,
}

impl CallStacks {
	pub fn new() -> CallStacks {
		let mut suspended = vec![];

		unsafe {
			let buffer = (*funcs::SUSPENDED_PROCS_BUFFER).buffer;
			let procs = funcs::SUSPENDED_PROCS;
			let front = (*procs).front;
			let back = (*procs).back;

			for x in front..back {
				let instance = *buffer.add(x);
				let context = (*instance).context;
				suspended.push(CallStacks::from_context(context, CallStackKind::Suspended));
			}
		}

		CallStacks {
			active: unsafe {
				CallStacks::from_context(*funcs::CURRENT_EXECUTION_CONTEXT, CallStackKind::Active)
			},
			suspended,
		}
	}

	fn from_context(
		mut context: *mut procs::ExecutionContext,
		kind: CallStackKind,
	) -> Vec<StackFrame> {
		let mut frames = vec![];

		loop {
			if context.is_null() {
				break;
			}

			unsafe {
				frames.push(StackFrame::from_context(context));
				context = (*context).parent_context;
			}
		}

		// BYOND stores sleeping stacks' frames in reverse-order
		match kind {
			CallStackKind::Active => frames,
			CallStackKind::Suspended => frames.into_iter().rev().collect(),
		}
	}
}