luaur_common/functions/create_token.rs
1//! Source: `Common/src/TimeTrace.cpp:115-123` (hand-ported)
2//! C++:
3//! ```cpp
4//! uint16_t createToken(GlobalContext& context, const char* name, const char* category)
5//! {
6//! std::scoped_lock lock(context.mutex);
7//! LUAU_ASSERT(context.tokens.size() < 64 * 1024);
8//! context.tokens.push_back({name, category});
9//! return uint16_t(context.tokens.size() - 1);
10//! }
11//! ```
12use crate::macros::luau_assert::LUAU_ASSERT;
13use crate::records::global_context::GlobalContext;
14use crate::records::token::Token;
15use core::ffi::c_char;
16
17pub fn create_token(context: &GlobalContext, name: *const c_char, category: *const c_char) -> u16 {
18 // `scoped_lock lock(context.mutex)` — the lock guards the mutable state.
19 let mut state = context
20 .state
21 .lock()
22 .expect("TimeTrace GlobalContext mutex poisoned");
23
24 LUAU_ASSERT!(state.tokens.len() < 64 * 1024);
25
26 state.tokens.push(Token { name, category });
27 (state.tokens.len() - 1) as u16
28}