use std::collections::HashMap;
pub type Atom = u32;
pub const ATOM_NULL: Atom = 0;
pub const PINNED: [&str; 251] = [
"null", "false", "true", "if", "else", "return", "var", "this", "delete", "void", "typeof", "new", "in", "instanceof", "do", "while", "for", "break", "continue", "switch", "case", "default", "throw", "try", "catch", "finally", "function", "debugger", "with", "class", "const", "enum", "export", "extends", "import", "super", "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield", "await", "", "keys", "size", "length", "fileName", "lineNumber", "columnNumber", "message", "cause", "errors", "error", "suppressed", "stack", "name", "toString", "toLocaleString", "valueOf", "eval", "prototype", "constructor", "configurable", "writable", "enumerable", "value", "get", "set", "of", "using", "__proto__", "undefined", "number", "boolean", "string", "object", "symbol", "integer", "unknown", "arguments", "callee", "caller", "<eval>", "<ret>", "<var>", "<arg_var>", "<with>", "lastIndex", "target", "index", "input", "defineProperties", "apply", "join", "split", "construct", "getPrototypeOf", "setPrototypeOf", "isExtensible", "preventExtensions", "has", "deleteProperty", "defineProperty", "getOwnPropertyDescriptor", "ownKeys", "add", "done", "next", "values", "source", "flags", "global", "unicode", "raw", "rawJSON", "new.target", "this.active_func", "<home_object>", "<computed_field>", "<static_computed_field>", "<class_fields_init>", "<brand>", "#constructor", "as", "from", "meta", "*default*", "*", "Module", "then", "resolve", "reject", "promise", "proxy", "revoke", "async", "exec", "groups", "indices", "status", "reason", "globalThis", "bigint", "-0", "Infinity", "-Infinity", "NaN", "hasIndices", "ignoreCase", "multiline", "dotAll", "sticky", "unicodeSets", "not-equal", "timed-out", "ok", "toISOString", "alphabet", "lastChunkHandling", "omitPadding", "toJSON", "maxByteLength", "Object", "Array", "Error", "Number", "String", "Boolean", "Symbol", "Arguments", "Math", "JSON", "Date", "Function", "GeneratorFunction", "ForInIterator", "RegExp", "ArrayBuffer", "SharedArrayBuffer", "Uint8ClampedArray", "Int8Array", "Uint8Array", "Int16Array", "Uint16Array", "Int32Array", "Uint32Array", "BigInt64Array", "BigUint64Array", "Float16Array", "Float32Array", "Float64Array", "DataView", "BigInt", "WeakRef", "FinalizationRegistry", "Map", "Set", "WeakMap", "WeakSet", "Iterator", "Iterator Helper", "Iterator Concat", "Iterator Wrap", "Iterator Zip", "Array From Async", "Map Iterator", "Set Iterator", "Array Iterator", "String Iterator", "RegExp String Iterator", "Generator", "Proxy", "Promise", "PromiseResolveFunction", "PromiseRejectFunction", "AsyncFunction", "AsyncFunctionResolve", "AsyncFunctionReject", "AsyncGeneratorFunction", "AsyncGenerator", "DisposableStack", "AsyncDisposableStack", "EvalError", "RangeError", "ReferenceError", "SyntaxError", "TypeError", "URIError", "InternalError", "AggregateError", "SuppressedError", "<brand>", "Symbol.toPrimitive", "Symbol.iterator", "Symbol.match", "Symbol.matchAll", "Symbol.replace", "Symbol.search", "Symbol.split", "Symbol.toStringTag", "Symbol.isConcatSpreadable", "Symbol.hasInstance", "Symbol.species", "Symbol.unscopables", "Symbol.asyncIterator", "Symbol.dispose", "Symbol.asyncDispose", ];
pub const ATOM_END: Atom = PINNED.len() as Atom + 1;
pub struct AtomTable {
texts: Vec<String>,
indices: HashMap<String, Atom>,
}
impl AtomTable {
pub fn new() -> AtomTable {
let texts: Vec<String> = PINNED.iter().map(|text| (*text).to_string()).collect();
let indices = texts
.iter()
.enumerate()
.map(|(index, text)| (text.clone(), index as Atom + 1))
.collect();
AtomTable { texts, indices }
}
pub fn text(&self, atom: Atom) -> Option<&str> {
if atom == ATOM_NULL {
return None;
}
self.texts.get(atom as usize - 1).map(String::as_str)
}
pub fn intern(&mut self, text: &str) -> Atom {
if let Some(&atom) = self.indices.get(text) {
return atom;
}
self.texts.push(text.to_string());
let atom = self.texts.len() as Atom;
self.indices.insert(text.to_string(), atom);
atom
}
pub fn len(&self) -> usize {
self.texts.len()
}
pub fn is_empty(&self) -> bool {
self.texts.is_empty()
}
}
impl Default for AtomTable {
fn default() -> Self {
AtomTable::new()
}
}