use alloc::collections::BTreeMap;
use alloc::format;
use alloc::rc::Rc;
use alloc::string::String;
use alloc::string::ToString;
use alloc::vec::Vec;
use core::cell::RefCell;
use crate::runtime::{Detail, Object, Runtime, RuntimeError, RuntimeFailure, TypeName, Value, put};
pub(crate) mod array;
pub(crate) mod buffer;
pub(crate) mod clipboard;
pub(crate) mod enumerator;
pub(crate) mod error;
pub(crate) mod file;
pub(crate) mod func;
pub(crate) mod gui;
pub(crate) mod hotkey;
pub(crate) mod inputhook;
pub(crate) mod map;
const METHODS: [(&str, &str, &str, i64, i64); 54] = [
("map", "set", "Set", 1, 1),
("map", "get", "Get", 2, 3),
("map", "has", "Has", 2, 2),
("map", "delete", "Delete", 2, 2),
("map", "clear", "Clear", 1, 1),
("map", "clone", "Clone", 1, 1),
("map", "__new", "__New", 1, 1),
("map", "__enum", "__Enum", 1, 2),
("array", "push", "Push", 1, 1),
("array", "pop", "Pop", 1, 1),
("array", "insertat", "InsertAt", 2, 2),
("array", "removeat", "RemoveAt", 2, 3),
("array", "delete", "Delete", 2, 2),
("array", "get", "Get", 2, 3),
("array", "has", "Has", 2, 2),
("array", "clone", "Clone", 1, 1),
("array", "__new", "__New", 1, 1),
("array", "__enum", "__Enum", 1, 2),
("buffer", "__new", "__New", 1, 3),
("func", "call", "Call", 1, 1),
("func", "isoptional", "IsOptional", 1, 1),
("func", "isbyref", "IsByRef", 1, 1),
("func", "bind", "Bind", 1, 1),
("enumerator", "call", "Call", 1, 3),
("enumerator", "isoptional", "IsOptional", 1, 1),
("enumerator", "isbyref", "IsByRef", 1, 1),
("enumerator", "bind", "Bind", 1, 1),
("error", "__new", "__New", 1, 4),
("file", "read", "Read", 1, 2),
("file", "readline", "ReadLine", 1, 1),
("file", "readchar", "ReadChar", 1, 1),
("file", "readuchar", "ReadUChar", 1, 1),
("file", "readshort", "ReadShort", 1, 1),
("file", "readushort", "ReadUShort", 1, 1),
("file", "readint", "ReadInt", 1, 1),
("file", "readuint", "ReadUInt", 1, 1),
("file", "readint64", "ReadInt64", 1, 1),
("file", "readfloat", "ReadFloat", 1, 1),
("file", "readdouble", "ReadDouble", 1, 1),
("file", "write", "Write", 2, 2),
("file", "writeline", "WriteLine", 1, 2),
("file", "writechar", "WriteChar", 2, 2),
("file", "writeuchar", "WriteUChar", 2, 2),
("file", "writeshort", "WriteShort", 2, 2),
("file", "writeushort", "WriteUShort", 2, 2),
("file", "writeint", "WriteInt", 2, 2),
("file", "writeuint", "WriteUInt", 2, 2),
("file", "writeint64", "WriteInt64", 2, 2),
("file", "writefloat", "WriteFloat", 2, 2),
("file", "writedouble", "WriteDouble", 2, 2),
("file", "rawread", "RawRead", 2, 3),
("file", "rawwrite", "RawWrite", 2, 3),
("file", "seek", "Seek", 2, 3),
("file", "close", "Close", 1, 1),
];
const CALLS: [(&str, &str, i64, i64); 60] = [
("object", "ownprops", 0, 0),
("object", "clone", 0, 0),
("object", "hasownprop", 1, 1),
("object", "defineprop", 2, 2),
("object", "deleteprop", 1, 1),
("object", "getownpropdesc", 1, 1),
("object", "hasprop", 1, 1),
("object", "hasmethod", 0, 2),
("object", "getmethod", 0, 2),
("object", "hasbase", 1, 1),
("map", "set", 0, -1),
("map", "get", 1, 2),
("map", "has", 1, 1),
("map", "delete", 1, 1),
("map", "clear", 0, 0),
("map", "clone", 0, 0),
("map", "__new", 0, -1),
("map", "__enum", 0, 1),
("array", "push", 0, -1),
("array", "pop", 0, 0),
("array", "insertat", 1, -1),
("array", "removeat", 1, 2),
("array", "delete", 1, 1),
("array", "get", 1, 2),
("array", "has", 1, 1),
("array", "clone", 0, 0),
("array", "__new", 0, -1),
("array", "__enum", 0, 1),
("buffer", "__new", 0, 2),
("func", "isoptional", 0, -1),
("func", "isbyref", 0, -1),
("func", "bind", 0, -1),
("enumerator", "call", 0, 2),
("error", "__new", 0, 3),
("file", "read", 0, 1),
("file", "readline", 0, 0),
("file", "readchar", 0, 0),
("file", "readuchar", 0, 0),
("file", "readshort", 0, 0),
("file", "readushort", 0, 0),
("file", "readint", 0, 0),
("file", "readuint", 0, 0),
("file", "readint64", 0, 0),
("file", "readfloat", 0, 0),
("file", "readdouble", 0, 0),
("file", "write", 1, 1),
("file", "writeline", 0, 1),
("file", "writechar", 1, 1),
("file", "writeuchar", 1, 1),
("file", "writeshort", 1, 1),
("file", "writeushort", 1, 1),
("file", "writeint", 1, 1),
("file", "writeuint", 1, 1),
("file", "writeint64", 1, 1),
("file", "writefloat", 1, 1),
("file", "writedouble", 1, 1),
("file", "rawread", 1, 2),
("file", "rawwrite", 1, 2),
("file", "seek", 1, 2),
("file", "close", 0, 0),
];
const CONSTRUCTORS: [(&str, i64, i64, &str, &str); 11] = [
("array", 0, -1, "Array.Call", "Array.Prototype.__New"),
("buffer", 0, 2, "Buffer.Call", "Buffer.Prototype.__New"),
("class", 0, 0, "Object.Call", ""),
(
"clipboardall",
0,
2,
"ClipboardAll.Call",
"ClipboardAll.Prototype.__New",
),
("comobject", 1, 2, "", "ComObject.Call"),
("comvalue", 2, 3, "", "ComValue.Call"),
("error", 0, 3, "Object.Call", "Error.Prototype.__New"),
("gui", 0, 3, "Gui.Call", "Gui.Prototype.__New"),
(
"inputhook",
0,
3,
"InputHook.Call",
"InputHook.Prototype.__New",
),
("menu", 0, 0, "Menu.Call", ""),
("object", 0, 0, "Object.Call", ""),
];
const NOT_MADE: [&str; 6] = [
"boundfunc",
"closure",
"file",
"func",
"enumerator",
"regexmatchinfo",
];
const THE_CALL_OF_THE_BASE: &str = "Object.Call";
const THE_CLIPBOARD_ALL: [&str; 2] = ["ptr", "size"];
const THE_COM_VALUE: [&str; 1] = ["ptr"];
const THE_BASE_OBJECT: [&str; 7] = [
"base",
"clone",
"defineprop",
"deleteprop",
"getownpropdesc",
"hasownprop",
"ownprops",
];
const THE_BASE_OBJECT_OUTSIDE: [&str; 4] = ["getmethod", "hasbase", "hasmethod", "hasprop"];
const THE_CLASS_VALUE: [&str; 4] = ["base", "call", "prototype", "__init"];
const BASE: [(&str, &str); 10] = [
("ownprops", "Object.Prototype.OwnProps"),
("hasownprop", "Object.Prototype.HasOwnProp"),
("defineprop", "Object.Prototype.DefineProp"),
("deleteprop", "Object.Prototype.DeleteProp"),
("getownpropdesc", "Object.Prototype.GetOwnPropDesc"),
("clone", "Object.Prototype.Clone"),
("hasprop", "HasProp"),
("hasmethod", "HasMethod"),
("getmethod", "GetMethod"),
("hasbase", "HasBase"),
];
const PROPERTIES: [(&str, &str, &str); 28] = [
("map", "count", "Count"),
("map", "capacity", "Capacity"),
("map", "casesense", "CaseSense"),
("array", "length", "Length"),
("array", "capacity", "Capacity"),
("buffer", "size", "Size"),
("buffer", "ptr", "Ptr"),
("func", "name", "Name"),
("func", "minparams", "MinParams"),
("func", "maxparams", "MaxParams"),
("func", "isbuiltin", "IsBuiltin"),
("func", "isvariadic", "IsVariadic"),
("enumerator", "name", "Name"),
("enumerator", "minparams", "MinParams"),
("enumerator", "maxparams", "MaxParams"),
("enumerator", "isbuiltin", "IsBuiltin"),
("enumerator", "isvariadic", "IsVariadic"),
("file", "pos", "Pos"),
("file", "length", "Length"),
("file", "ateof", "AtEOF"),
("file", "encoding", "Encoding"),
("file", "handle", "Handle"),
("error", "message", "Message"),
("error", "what", "What"),
("error", "extra", "Extra"),
("error", "file", "File"),
("error", "line", "Line"),
("error", "stack", "Stack"),
];
const READ_ONLY: [(&str, &[&str]); 5] = [
("map", &["count"]),
("buffer", &["ptr"]),
(
"func",
&["name", "minparams", "maxparams", "isbuiltin", "isvariadic"],
),
(
"enumerator",
&["name", "minparams", "maxparams", "isbuiltin", "isvariadic"],
),
("file", &["ateof", "handle"]),
];
pub(crate) fn the_property_is_read_only(class: &str, name: &str) -> bool {
let class = class.to_lowercase();
let name = name.to_lowercase();
READ_ONLY
.iter()
.any(|(owner, names)| *owner == class && names.iter().any(|property| *property == name))
}
const PROTOTYPE_MEMBERS: [(&str, &[&str]); 16] = [
(
"object",
&[
"Clone",
"DefineProp",
"DeleteProp",
"GetOwnPropDesc",
"HasOwnProp",
"OwnProps",
],
),
(
"map",
&[
"__Enum",
"__Item",
"__New",
"Capacity",
"CaseSense",
"Clear",
"Clone",
"Count",
"Delete",
"Get",
"Has",
"Set",
],
),
(
"array",
&[
"__Enum", "__Item", "__New", "Capacity", "Clone", "Delete", "Get", "Has", "InsertAt",
"Length", "Pop", "Push", "RemoveAt",
],
),
("buffer", &["__New", "Ptr", "Size"]),
("error", &["__New"]),
(
"file",
&[
"AtEOF",
"Close",
"Encoding",
"Handle",
"Length",
"Pos",
"RawRead",
"RawWrite",
"Read",
"ReadChar",
"ReadDouble",
"ReadFloat",
"ReadInt",
"ReadInt64",
"ReadLine",
"ReadShort",
"ReadUChar",
"ReadUInt",
"ReadUShort",
"Seek",
"Write",
"WriteChar",
"WriteDouble",
"WriteFloat",
"WriteInt",
"WriteInt64",
"WriteLine",
"WriteShort",
"WriteUChar",
"WriteUInt",
"WriteUShort",
],
),
(
"func",
&[
"Bind",
"Call",
"IsBuiltIn",
"IsByRef",
"IsOptional",
"IsVariadic",
"MaxParams",
"MinParams",
"Name",
],
),
("enumerator", &[]),
(
"menu",
&[
"Add",
"AddStandard",
"Check",
"ClickCount",
"Default",
"Delete",
"Disable",
"Enable",
"Handle",
"Insert",
"Rename",
"SetColor",
"SetIcon",
"Show",
"ToggleCheck",
"ToggleEnable",
"Uncheck",
],
),
(
"gui",
&[
"__Enum",
"__Item",
"__New",
"Add",
"AddActiveX",
"AddButton",
"AddCheckBox",
"AddComboBox",
"AddCustom",
"AddDateTime",
"AddDDL",
"AddDropDownList",
"AddEdit",
"AddGroupBox",
"AddHotkey",
"AddLink",
"AddListBox",
"AddListView",
"AddMonthCal",
"AddPic",
"AddPicture",
"AddProgress",
"AddRadio",
"AddSlider",
"AddStatusBar",
"AddTab",
"AddTab2",
"AddTab3",
"AddText",
"AddTreeView",
"AddUpDown",
"BackColor",
"Destroy",
"Flash",
"FocusedCtrl",
"GetClientPos",
"GetPos",
"Hide",
"Hwnd",
"MarginX",
"MarginY",
"Maximize",
"MenuBar",
"Minimize",
"Move",
"Name",
"OnEvent",
"Opt",
"Restore",
"SetFont",
"Show",
"Submit",
"Title",
],
),
("clipboardall", &["__New"]),
(
"inputhook",
&[
"__New",
"BackspaceIsUndo",
"CaseSensitive",
"EndKey",
"EndMods",
"EndReason",
"FindAnywhere",
"InProgress",
"Input",
"KeyOpt",
"Match",
"MinSendLevel",
"NotifyNonText",
"OnChar",
"OnEnd",
"OnKeyDown",
"OnKeyUp",
"Start",
"Stop",
"Timeout",
"VisibleNonText",
"VisibleText",
"Wait",
],
),
(
"regexmatchinfo",
&[
"__Enum", "__Get", "__Item", "Count", "Len", "Mark", "Name", "Pos",
],
),
("boundfunc", &[]),
("closure", &[]),
("class", &[]),
];
const PROTOTYPE_METHODS: [(&str, &[&str]); 16] = [
(
"object",
&[
"Clone",
"DefineProp",
"DeleteProp",
"GetOwnPropDesc",
"HasOwnProp",
"OwnProps",
],
),
(
"map",
&[
"__Enum", "__New", "Clear", "Clone", "Delete", "Get", "Has", "Set",
],
),
(
"array",
&[
"__Enum", "__New", "Clone", "Delete", "Get", "Has", "InsertAt", "Pop", "Push",
"RemoveAt",
],
),
("buffer", &["__New"]),
("error", &["__New"]),
(
"file",
&[
"Close",
"RawRead",
"RawWrite",
"Read",
"ReadChar",
"ReadDouble",
"ReadFloat",
"ReadInt",
"ReadInt64",
"ReadLine",
"ReadShort",
"ReadUChar",
"ReadUInt",
"ReadUShort",
"Seek",
"Write",
"WriteChar",
"WriteDouble",
"WriteFloat",
"WriteInt",
"WriteInt64",
"WriteLine",
"WriteShort",
"WriteUChar",
"WriteUInt",
"WriteUShort",
],
),
("func", &["Bind", "Call", "IsByRef", "IsOptional"]),
("enumerator", &[]),
(
"menu",
&[
"Add",
"AddStandard",
"Check",
"Delete",
"Disable",
"Enable",
"Insert",
"Rename",
"SetColor",
"SetIcon",
"Show",
"ToggleCheck",
"ToggleEnable",
"Uncheck",
],
),
(
"gui",
&[
"__Enum",
"__New",
"Add",
"AddActiveX",
"AddButton",
"AddCheckBox",
"AddComboBox",
"AddCustom",
"AddDateTime",
"AddDDL",
"AddDropDownList",
"AddEdit",
"AddGroupBox",
"AddHotkey",
"AddLink",
"AddListBox",
"AddListView",
"AddMonthCal",
"AddPic",
"AddPicture",
"AddProgress",
"AddRadio",
"AddSlider",
"AddStatusBar",
"AddTab",
"AddTab2",
"AddTab3",
"AddText",
"AddTreeView",
"AddUpDown",
"Destroy",
"Flash",
"GetClientPos",
"GetPos",
"Hide",
"Maximize",
"Minimize",
"Move",
"OnEvent",
"Opt",
"Restore",
"SetFont",
"Show",
"Submit",
],
),
("clipboardall", &["__New"]),
("inputhook", &["__New", "KeyOpt", "Start", "Stop", "Wait"]),
("regexmatchinfo", &["__Enum", "__Get", "Len", "Name", "Pos"]),
("boundfunc", &[]),
("closure", &[]),
("class", &[]),
];
const NOT_A_PROTOTYPE: [&str; 4] = ["any", "comobject", "comvalue", "varref"];
pub(crate) fn the_name_of_a_method_of_the_base_object(method: &str) -> String {
let lower = method.to_lowercase();
BASE.iter()
.find(|(name, _)| *name == lower)
.map(|(_, spelled)| spelled.to_string())
.unwrap_or_else(|| method.to_string())
}
pub(crate) fn the_base_object_has_the_method(name: &str) -> bool {
let name = name.to_lowercase();
CALLS
.iter()
.any(|(owner, member, _, _)| *owner == "object" && *member == name)
}
pub(crate) fn the_method_of(class: &str, method: &str) -> Option<(&'static str, i64, i64)> {
let class = class.to_lowercase();
let method = method.to_lowercase();
METHODS
.iter()
.find(|(owner, name, _, _, _)| *owner == class && *name == method)
.map(|(_, _, spelled, min, max)| (*spelled, *min, *max))
}
pub(crate) fn the_arity_of(class: &str, method: &str) -> Option<(i64, i64)> {
let class = class.to_lowercase();
let method = method.to_lowercase();
let own = CALLS
.iter()
.find(|(owner, name, _, _)| *owner == class && *name == method);
let found = match own {
Some(found) => Some(found),
None => CALLS
.iter()
.find(|(owner, name, _, _)| *owner == "object" && *name == method),
};
found.map(|(_, _, min, max)| (*min, *max))
}
pub(crate) fn the_static_methods_of_a_class() -> [&'static str; 11] {
[
"call",
"clone",
"defineprop",
"deleteprop",
"getmethod",
"getownpropdesc",
"hasbase",
"hasmethod",
"hasownprop",
"hasprop",
"ownprops",
]
}
pub(crate) fn the_name_of(class: &str, member: &str) -> String {
format!("{}.Prototype.{}", the_spelling_of(class), member)
}
fn the_spelling_of_the_method(class: &str, method: &str) -> String {
let lower = method.to_lowercase();
if let Some((_, spelled)) = BASE.iter().find(|(name, _)| *name == lower) {
return spelled.to_string();
}
the_method_of(class, method)
.map(|(spelled, _, _)| spelled.to_string())
.unwrap_or_else(|| method.to_string())
}
fn the_spelling_of_the_property(class: &str, property: &str) -> String {
let lower = property.to_lowercase();
PROPERTIES
.iter()
.find(|(owner, name, _)| *owner == class && *name == lower)
.map(|(_, _, spelled)| spelled.to_string())
.unwrap_or_else(|| property.to_string())
}
pub(crate) fn the_members_of_the_prototype(class: &str) -> &'static [&'static str] {
let lower = class.to_lowercase();
PROTOTYPE_MEMBERS
.iter()
.find(|(owner, _)| *owner == lower)
.map(|(_, members)| *members)
.unwrap_or(&[])
}
pub(crate) fn the_methods_of_the_prototype(class: &str) -> &'static [&'static str] {
let lower = class.to_lowercase();
PROTOTYPE_METHODS
.iter()
.find(|(owner, _)| *owner == lower)
.map(|(_, methods)| *methods)
.unwrap_or(&[])
}
pub(crate) fn the_property_of_the_prototype(class: &str, property: &str) -> Option<&'static str> {
let lower = property.to_lowercase();
let methods = the_methods_of_the_prototype(class);
the_members_of_the_prototype(class)
.iter()
.find(|member| member.to_lowercase() == lower)
.filter(|_| !methods.iter().any(|method| method.to_lowercase() == lower))
.copied()
}
pub(crate) fn the_prototype_is_not_one(class: &str) -> bool {
NOT_A_PROTOTYPE.contains(&class.to_lowercase().as_str())
}
fn the_full_name_of(class: &str, method: &str) -> String {
let lower = method.to_lowercase();
if BASE.iter().any(|(name, _)| *name == lower) {
return the_spelling_of_the_method(class, method);
}
the_name_of(class, &the_spelling_of_the_method(class, method))
}
pub(crate) fn named(failure: RuntimeFailure, what: &str) -> RuntimeFailure {
match failure {
RuntimeFailure::Error(error) => RuntimeFailure::Detailed(
error,
Detail {
what: what.to_string(),
extra: String::new(),
},
),
RuntimeFailure::Detailed(error, mut detail) => {
if detail.what.is_empty() {
detail.what = what.to_string();
}
RuntimeFailure::Detailed(error, detail)
}
other => other,
}
}
pub(crate) fn the_failure_with(message: &str, extra: &str, line: u32) -> RuntimeFailure {
RuntimeFailure::Detailed(
RuntimeError {
message: message.to_string(),
line,
},
Detail {
what: String::new(),
extra: extra.to_string(),
},
)
}
pub(crate) fn the_arity_failure(
class: &str,
method: &str,
message: &str,
line: u32,
) -> RuntimeFailure {
RuntimeFailure::Detailed(
RuntimeError {
message: message.to_string(),
line,
},
Detail {
what: String::new(),
extra: the_full_name_of(class, method),
},
)
}
pub(crate) fn the_arity_failure_of_a_method(
class: &str,
method: &str,
message: &str,
line: u32,
) -> RuntimeFailure {
RuntimeFailure::Detailed(
RuntimeError {
message: message.to_string(),
line,
},
Detail {
what: the_name_of("func", "Call"),
extra: the_full_name_of(class, method),
},
)
}
fn the_constructor_of(class: &str) -> Option<(i64, i64, &'static str, &'static str)> {
let class = class.to_lowercase();
let owner = if error::is_a_class(&class) {
"error"
} else {
class.as_str()
};
CONSTRUCTORS
.iter()
.find(|(name, _, _, _, _)| *name == owner)
.map(|(_, min, max, what, extra)| (*min, *max, *what, *extra))
}
fn the_failure_of_a_class_that_is_not_made(line: u32) -> RuntimeFailure {
RuntimeFailure::Detailed(
RuntimeError {
message: "Invalid base.".to_string(),
line,
},
Detail {
what: THE_CALL_OF_THE_BASE.to_string(),
extra: String::new(),
},
)
}
fn the_arity_failure_of_a_constructor(
what: &str,
extra: &str,
message: &str,
line: u32,
) -> RuntimeFailure {
RuntimeFailure::Detailed(
RuntimeError {
message: message.to_string(),
line,
},
Detail {
what: what.to_string(),
extra: extra.to_string(),
},
)
}
pub(crate) const TYPES: [&str; 25] = [
"any",
"array",
"boundfunc",
"buffer",
"class",
"clipboardall",
"closure",
"comobject",
"comvalue",
"enumerator",
"error",
"file",
"float",
"func",
"gui",
"hotkey",
"inputhook",
"integer",
"map",
"menu",
"number",
"object",
"primitive",
"regexmatchinfo",
"string",
];
pub(crate) fn is_type(name: &str) -> bool {
let name = name.to_lowercase();
TYPES.contains(&name.as_str()) || error::is_a_class(&name)
}
pub fn is_a_name_of_a_class(name: &str) -> bool {
let name = name.to_lowercase();
(TYPES.contains(&name.as_str()) && name != "hotkey") || error::is_a_class(&name)
}
pub fn the_class_types() -> Vec<String> {
let mut names: Vec<String> = TYPES
.iter()
.filter(|name| **name != "hotkey")
.map(|name| the_spelling_of(name))
.collect();
for (name, spelled, _) in error::CLASSES.iter() {
if *name != "error" {
names.push(spelled.to_string());
}
}
names
}
const THE_CLASSES_THAT_OWN_THE_CALL: [&str; 10] = [
"object",
"map",
"array",
"buffer",
"menu",
"inputhook",
"clipboardall",
"gui",
"comobject",
"comvalue",
];
pub(crate) fn the_class_owns_the_call(class: &str) -> bool {
THE_CLASSES_THAT_OWN_THE_CALL.contains(&class.to_lowercase().as_str())
}
const THE_CLASSES_THAT_ARE_NOT_HERE: [&str; 2] = ["comobject", "comvalue"];
fn the_failure_of_a_class_that_is_not_here(line: u32) -> RuntimeFailure {
RuntimeFailure::Error(RuntimeError {
message: "the COM objects are a thing of Windows, and this runtime does not carry them."
.to_string(),
line,
})
}
fn the_kind_of_a_com_value_argument(
index: usize,
value: Option<&Value>,
line: u32,
) -> Option<RuntimeFailure> {
let value = value?;
match value {
Value::Int(_) | Value::Float(_) => None,
other => Some(RuntimeFailure::Detailed(
RuntimeError {
message: crate::runtime::requires(index, "ComValue.Call", TypeName::Number, other),
line,
},
Detail {
what: "ComValue.Call".to_string(),
extra: other.to_text(),
},
)),
}
}
fn the_failure_of_a_class_that_cannot_be_called(rt: &Runtime, line: u32) -> RuntimeFailure {
let value = rt
.class_value_of("any")
.unwrap_or_else(|| class_value("any"));
rt.no_method(&value, "Call", line)
}
pub(crate) fn the_base_class_of_a_class(class: &str) -> Option<&'static str> {
let class = class.to_lowercase();
if class == "any" {
return None;
}
if class == "object" || class == "primitive" {
return Some("any");
}
if let Some(base) = the_base_of_the_primitive(&class) {
return Some(base);
}
if let Some(base) = the_base_of(&class) {
return Some(base);
}
Some("object")
}
fn the_base_of_the_primitive(class: &str) -> Option<&'static str> {
match class {
"number" | "string" => Some("primitive"),
"integer" | "float" => Some("number"),
"primitive" => Some("any"),
_ => None,
}
}
pub(crate) fn the_base_of_the_primitive_of_the_class(class: &str) -> Option<&'static str> {
the_base_of_the_primitive(&class.to_lowercase())
}
pub(crate) fn the_base_of(class: &str) -> Option<&'static str> {
if class.eq_ignore_ascii_case("enumerator") {
return Some("func");
}
error::the_base_of(class)
}
pub(crate) fn class_value(name: &str) -> Value {
let mut table = BTreeMap::new();
put(
&mut table,
crate::runtime::CLASS_NAME,
Value::Text(the_spelling_of(name)),
);
put(
&mut table,
"prototype",
crate::runtime::prototype_value(name),
);
Value::Object(Rc::new(RefCell::new(Object {
class: name.to_lowercase(),
properties: table,
})))
}
pub(crate) fn object(class: &str) -> Value {
Value::Object(Rc::new(RefCell::new(Object {
class: class.to_lowercase(),
properties: BTreeMap::new(),
})))
}
pub(crate) fn object_with(class: &str, properties: Vec<(&str, Value)>) -> Value {
let mut table = BTreeMap::new();
for (name, value) in properties {
put(&mut table, name, value);
}
Value::Object(Rc::new(RefCell::new(Object {
class: class.to_lowercase(),
properties: table,
})))
}
const THE_SPELLINGS: [(&str, &str); 7] = [
("boundfunc", "BoundFunc"),
("clipboardall", "ClipboardAll"),
("closure", "Closure"),
("comobject", "ComObject"),
("comvalue", "ComValue"),
("inputhook", "InputHook"),
("regexmatchinfo", "RegExMatchInfo"),
];
pub(crate) fn the_spelling_of(name: &str) -> String {
if let Some(spelled) = error::the_spelling_of(name) {
return spelled;
}
let lower = name.to_lowercase();
if let Some((_, spelled)) = THE_SPELLINGS.iter().find(|(key, _)| *key == lower) {
return spelled.to_string();
}
let mut spelled = name.to_string();
if let Some(first) = spelled.get_mut(0..1) {
first.make_ascii_uppercase();
}
spelled
}
pub(crate) fn construct(
class: &str,
arguments: &[Value],
rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
if NOT_MADE.contains(&class.to_lowercase().as_str()) {
return Some(Err(the_failure_of_a_class_that_is_not_made(line)));
}
if let Some((min, max, what, extra)) = the_constructor_of(class) {
let count = arguments.len() as i64;
if count < min || (max >= 0 && count > max) {
let message = if count < min {
"Too few parameters passed to function."
} else {
"Too many parameters passed to function."
};
return Some(Err(the_arity_failure_of_a_constructor(
what, extra, message, line,
)));
}
}
if class.eq_ignore_ascii_case("any") {
return Some(Err(the_failure_of_a_class_that_cannot_be_called(rt, line)));
}
if class.eq_ignore_ascii_case("comvalue") {
if let Some(failure) = the_kind_of_a_com_value_argument(1, arguments.first(), line) {
return Some(Err(failure));
}
if let Some(failure) = the_kind_of_a_com_value_argument(3, arguments.get(2), line) {
return Some(Err(failure));
}
}
if THE_CLASSES_THAT_ARE_NOT_HERE.contains(&class.to_lowercase().as_str()) {
return Some(Err(the_failure_of_a_class_that_is_not_here(line)));
}
if error::is_a_class(class) {
return Some(
error::construct(class, arguments, rt, line)
.map_err(|failure| named(failure, &the_name_of(class, "__New"))),
);
}
let made = match class.to_lowercase().as_str() {
"array" => Some(array::construct(arguments, rt, line)),
"buffer" => Some(buffer::construct(arguments, rt, line)),
"clipboardall" => Some(clipboard::construct(arguments, rt, line)),
"hotkey" => Some(hotkey::construct(arguments, rt, line)),
"map" => Some(map::construct(arguments, rt, line)),
"gui" | "menu" | "gui.control" | "gui.listview" | "gui.menu" | "gui.statusbar" => {
Some(gui::construct(class, arguments, rt, line))
}
_ => None,
};
made.map(|result| result.map_err(|failure| named(failure, &the_name_of(class, "__New"))))
}
pub(crate) fn call(
class: &str,
method: &str,
this: &Value,
arguments: &[Value],
rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
if method.eq_ignore_ascii_case("__item") {
let key = arguments
.first()
.cloned()
.unwrap_or(Value::Text(String::new()));
if let Some(result) = item(class, &key, this, rt, line) {
return Some(result);
}
}
let answer = call_once(class, method, this, arguments, rt, line);
match answer {
Some(result) => {
Some(result.map_err(|failure| named(failure, &the_full_name_of(class, method))))
}
None => {
let lower = method.to_lowercase();
if lower == method {
return None;
}
call_once(class, &lower, this, arguments, rt, line).map(|result| {
result.map_err(|failure| named(failure, &the_full_name_of(class, method)))
})
}
}
}
fn call_once(
class: &str,
method: &str,
this: &Value,
arguments: &[Value],
rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
match class.to_lowercase().as_str() {
"array" => array::call(method, this, arguments, rt, line),
"buffer" => buffer::call(method, this, arguments, rt, line),
"enumerator" => enumerator::call(method, this, arguments, rt, line),
"error" => error::call(method, this, arguments, rt, line),
"file" => file::call(method, this, arguments, rt, line),
"func" | "boundfunc" => func::call(method, this, arguments, rt, line),
"hotkey" => hotkey::call(method, this, arguments, rt, line),
"map" => map::call(method, this, arguments, rt, line),
"object" => rt.base_method(this, method, arguments, line),
"gui" | "menu" | "gui.control" | "gui.listview" | "gui.menu" | "gui.statusbar" => {
gui::call(class, method, this, arguments, rt, line)
}
_ => None,
}
}
pub(crate) fn call_the_value(
class: &str,
this: &Value,
arguments: &[Value],
rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
match class.to_lowercase().as_str() {
"func" | "boundfunc" => func::call_the_value(this, arguments, rt, line),
"enumerator" => enumerator::call_the_value(this, arguments, rt, line),
_ => call(class, "call", this, arguments, rt, line),
}
}
pub(crate) fn property(
class: &str,
name: &str,
this: &Value,
rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
match class.to_lowercase().as_str() {
"array" => array::property(name, this, rt, line),
"buffer" => buffer::property(name, this, rt, line),
"enumerator" => enumerator::property(name, this, rt, line),
"error" => error::property(name, this, rt, line),
"file" => file::property(name, this, rt, line),
"func" => func::property(name, this, rt, line),
"hotkey" => hotkey::property(name, this, rt, line),
"map" => map::property(name, this, rt, line),
"gui" | "menu" | "gui.control" | "gui.listview" | "gui.menu" | "gui.statusbar" => {
gui::property(class, name, this, rt, line)
}
_ => None,
}
}
pub(crate) fn set_property(
class: &str,
name: &str,
this: &Value,
value: Value,
rt: &mut Runtime,
line: u32,
) -> Option<Result<(), RuntimeFailure>> {
let written = match class.to_lowercase().as_str() {
"array" => array::set_property(name, this, value, rt, line),
"buffer" => buffer::set_property(name, this, value, rt, line),
"error" => error::set_property(name, this, value, rt, line),
"file" => file::set_property(name, this, value, rt, line),
"func" => func::set_property(name, this, value, rt, line),
"hotkey" => hotkey::set_property(name, this, value, rt, line),
"map" => map::set_property(name, this, value, rt, line),
"gui" | "menu" | "gui.control" | "gui.listview" | "gui.menu" | "gui.statusbar" => {
gui::set_property(class, name, this, value, rt, line)
}
_ => None,
};
let what = the_name_of(
class,
&format!("{}.Set", the_spelling_of_the_property(class, name)),
);
written.map(|result| result.map_err(|failure| named(failure, &what)))
}
pub(crate) fn item(
class: &str,
key: &Value,
this: &Value,
rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
let read = match class.to_lowercase().as_str() {
"array" => array::item(key, this, rt, line),
"file" => file::item(key, this, rt, line),
"map" => map::item(key, this, rt, line),
_ => None,
};
let what = the_name_of(class, "__Item.Get");
read.map(|result| result.map_err(|failure| named(failure, &what)))
}
pub(crate) fn set_item(
class: &str,
key: &Value,
this: &Value,
value: Value,
rt: &mut Runtime,
line: u32,
) -> Option<Result<(), RuntimeFailure>> {
let written = match class {
"array" => array::set_item(key, this, value, rt, line),
"map" => map::set_item(key, this, value, rt, line),
_ => None,
};
let what = the_name_of(class, "__Item.Set");
written.map(|result| result.map_err(|failure| named(failure, &what)))
}
pub fn names(class: &str) -> Vec<&'static str> {
if error::is_a_class(class) {
return error::names();
}
match class.to_lowercase().as_str() {
"array" => array::names(),
"buffer" => buffer::names(),
"clipboardall" => THE_CLIPBOARD_ALL.to_vec(),
"comvalue" => THE_COM_VALUE.to_vec(),
"enumerator" => enumerator::names(),
"error" => error::names(),
"file" => file::names(),
"func" => func::names(),
"hotkey" => hotkey::names(),
"map" => map::names(),
"gui" => gui::names("gui"),
"menu" | "gui.menu" => gui::names("gui.menu"),
"gui.control" => gui::names("gui.control"),
"gui.listview" => gui::names("gui.listview"),
"gui.statusbar" => gui::names("gui.statusbar"),
"gui.treeview" => gui::names("gui.treeview"),
"inputhook" => inputhook::NAMES.to_vec(),
"class" => THE_CLASS_VALUE.to_vec(),
"object" => THE_BASE_OBJECT
.iter()
.chain(THE_BASE_OBJECT_OUTSIDE.iter())
.copied()
.collect(),
_ => Vec::new(),
}
}
pub fn carries(class: &str, name: &str) -> bool {
let name = name.to_lowercase();
names(class).iter().any(|carried| *carried == name)
}
const THE_CLASSES_THAT_ARE_NOT_CARRIED: [&str; 6] = [
"any",
"class",
"clipboardall",
"comobject",
"comvalue",
"inputhook",
];
pub(crate) fn the_reason_of_a_class_that_is_not_carried(class: &str) -> Option<&'static str> {
let class = class.to_lowercase();
if THE_CLASSES_THAT_ARE_NOT_CARRIED.contains(&class.as_str()) {
return Some("this runtime does not carry that class yet.");
}
None
}