use crate::{GC, LuaValue, ObjectAllocator, lua_vm::TmKind};
const TM_N: usize = 26;
pub struct ConstString {
pub tmname: [LuaValue; TM_N],
pub tm_pairs: LuaValue, pub tm_ipairs: LuaValue, pub tm_name: LuaValue, pub tm_metatable: LuaValue,
pub str_suspended: LuaValue, pub str_running: LuaValue, pub str_normal: LuaValue, pub str_dead: LuaValue,
pub str_nil: LuaValue, pub str_boolean: LuaValue, pub str_number: LuaValue, pub str_string: LuaValue, pub str_table: LuaValue, pub str_function: LuaValue, pub str_userdata: LuaValue, pub str_thread: LuaValue, pub str_true: LuaValue, pub str_false: LuaValue, pub str_integer: LuaValue, pub str_float: LuaValue,
pub str_hook_call: LuaValue, pub str_hook_return: LuaValue, pub str_hook_line: LuaValue, pub str_hook_count: LuaValue, pub str_hook_tail_call: LuaValue, }
impl ConstString {
pub fn new(allocator: &mut ObjectAllocator, gc: &mut GC) -> Self {
let nil = LuaValue::nil();
let mut cs = Self {
tmname: [nil; TM_N],
tm_pairs: nil,
tm_ipairs: nil,
tm_name: nil,
tm_metatable: nil,
str_suspended: nil,
str_running: nil,
str_normal: nil,
str_dead: nil,
str_nil: nil,
str_boolean: nil,
str_number: nil,
str_string: nil,
str_table: nil,
str_function: nil,
str_userdata: nil,
str_thread: nil,
str_true: nil,
str_false: nil,
str_integer: nil,
str_float: nil,
str_hook_call: nil,
str_hook_return: nil,
str_hook_line: nil,
str_hook_count: nil,
str_hook_tail_call: nil,
};
let tm_names: [&str; TM_N] = [
"__index", "__newindex", "__gc", "__mode", "__len", "__eq", "__add", "__sub", "__mul", "__mod", "__pow", "__div", "__idiv", "__band", "__bor", "__bxor", "__shl", "__shr", "__unm", "__bnot", "__lt", "__le", "__concat", "__call", "__close", "__tostring", ];
for (i, name) in tm_names.iter().enumerate() {
cs.tmname[i] = allocator.create_string(gc, name).unwrap();
}
cs.tm_pairs = allocator.create_string(gc, "__pairs").unwrap();
cs.tm_ipairs = allocator.create_string(gc, "__ipairs").unwrap();
cs.tm_name = allocator.create_string(gc, "__name").unwrap();
cs.tm_metatable = allocator.create_string(gc, "__metatable").unwrap();
cs.str_suspended = allocator.create_string(gc, "suspended").unwrap();
cs.str_running = allocator.create_string(gc, "running").unwrap();
cs.str_normal = allocator.create_string(gc, "normal").unwrap();
cs.str_dead = allocator.create_string(gc, "dead").unwrap();
cs.str_nil = allocator.create_string(gc, "nil").unwrap();
cs.str_boolean = allocator.create_string(gc, "boolean").unwrap();
cs.str_number = allocator.create_string(gc, "number").unwrap();
cs.str_string = allocator.create_string(gc, "string").unwrap();
cs.str_table = allocator.create_string(gc, "table").unwrap();
cs.str_function = allocator.create_string(gc, "function").unwrap();
cs.str_userdata = allocator.create_string(gc, "userdata").unwrap();
cs.str_thread = allocator.create_string(gc, "thread").unwrap();
cs.str_true = allocator.create_string(gc, "true").unwrap();
cs.str_false = allocator.create_string(gc, "false").unwrap();
cs.str_integer = allocator.create_string(gc, "integer").unwrap();
cs.str_float = allocator.create_string(gc, "float").unwrap();
for i in 0..TM_N {
gc.fixed(cs.tmname[i].as_gc_ptr().unwrap());
}
gc.fixed(cs.tm_pairs.as_gc_ptr().unwrap());
gc.fixed(cs.tm_ipairs.as_gc_ptr().unwrap());
gc.fixed(cs.tm_name.as_gc_ptr().unwrap());
gc.fixed(cs.tm_metatable.as_gc_ptr().unwrap());
gc.fixed(cs.str_suspended.as_gc_ptr().unwrap());
gc.fixed(cs.str_running.as_gc_ptr().unwrap());
gc.fixed(cs.str_normal.as_gc_ptr().unwrap());
gc.fixed(cs.str_dead.as_gc_ptr().unwrap());
gc.fixed(cs.str_nil.as_gc_ptr().unwrap());
gc.fixed(cs.str_boolean.as_gc_ptr().unwrap());
gc.fixed(cs.str_number.as_gc_ptr().unwrap());
gc.fixed(cs.str_string.as_gc_ptr().unwrap());
gc.fixed(cs.str_table.as_gc_ptr().unwrap());
gc.fixed(cs.str_function.as_gc_ptr().unwrap());
gc.fixed(cs.str_userdata.as_gc_ptr().unwrap());
gc.fixed(cs.str_thread.as_gc_ptr().unwrap());
gc.fixed(cs.str_true.as_gc_ptr().unwrap());
gc.fixed(cs.str_false.as_gc_ptr().unwrap());
gc.fixed(cs.str_integer.as_gc_ptr().unwrap());
gc.fixed(cs.str_float.as_gc_ptr().unwrap());
cs.str_hook_call = allocator.create_string(gc, "call").unwrap();
cs.str_hook_return = allocator.create_string(gc, "return").unwrap();
cs.str_hook_line = allocator.create_string(gc, "line").unwrap();
cs.str_hook_count = allocator.create_string(gc, "count").unwrap();
cs.str_hook_tail_call = allocator.create_string(gc, "tail call").unwrap();
gc.fixed(cs.str_hook_call.as_gc_ptr().unwrap());
gc.fixed(cs.str_hook_return.as_gc_ptr().unwrap());
gc.fixed(cs.str_hook_line.as_gc_ptr().unwrap());
gc.fixed(cs.str_hook_count.as_gc_ptr().unwrap());
gc.fixed(cs.str_hook_tail_call.as_gc_ptr().unwrap());
gc.tm_gc = cs.tmname[TmKind::Gc as usize];
gc.tm_mode = cs.tmname[TmKind::Mode as usize];
cs
}
#[inline(always)]
pub fn get_tm_value(&self, tm: TmKind) -> LuaValue {
unsafe { *self.tmname.get_unchecked(tm as usize) }
}
}