pub(crate) mod call;
mod call_array;
mod call_ast;
mod call_string;
mod collation;
#[rustfmt::skip]
mod table;
#[cfg(test)]
mod tests;
pub use call::{Called, MAX_CALL_DEPTH, Site, call};
pub use table::{MARKDOC_VERSION, NODE_VERSION};
use crate::next::schema::value::Value;
use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Builtin(u16);
#[derive(Debug)]
pub struct ObjectData {
pub name: &'static str,
pub flags: u8,
pub proto: Option<Builtin>,
pub class: &'static str,
pub wrapped: Option<Wrapped>,
pub source: Option<&'static str>,
pub props: &'static [Prop],
}
pub const CALLABLE: u8 = 1;
pub const CONSTRUCTOR: u8 = 2;
pub const CLASS: u8 = 4;
pub const IS_ARRAY: u8 = 8;
pub const FRAMELESS: u8 = 16;
pub const WRITABLE: u8 = 1;
pub const ENUMERABLE: u8 = 2;
pub const CONFIGURABLE: u8 = 4;
#[derive(Debug)]
pub struct Prop {
pub key: &'static str,
pub slot: Slot,
pub attrs: u8,
}
#[derive(Debug)]
pub enum Slot {
Data(Data),
Accessor {
get: Option<Getter>,
set: bool,
},
}
#[derive(Debug, Clone, Copy)]
pub enum Data {
Undefined,
Null,
Boolean(bool),
Number(f64),
String(&'static str),
Object(Builtin),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Getter {
Proto,
Restricted,
RegExpFlag(u8),
RegExpSource,
RegExpFlags,
RegExpLegacy(Legacy),
Constant(Builtin),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Legacy {
Input,
LastMatch,
LastParen,
LeftContext,
RightContext,
Paren(u8),
}
#[derive(Debug, Clone, Copy)]
pub enum Wrapped {
String(&'static str),
Number(f64),
Boolean(bool),
}
pub const RESTRICTED: &str = "TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them";
impl Builtin {
pub fn all() -> impl Iterator<Item = Builtin> {
(0..table::OBJECTS.len()).map(|i| Builtin(i as u16))
}
pub fn data(self) -> &'static ObjectData {
&table::OBJECTS[self.0 as usize]
}
pub fn name(self) -> &'static str {
self.data().name
}
pub fn is_callable(self) -> bool {
self.data().flags & CALLABLE != 0
}
pub fn is_constructor(self) -> bool {
self.data().flags & CONSTRUCTOR != 0
}
pub fn is_class(self) -> bool {
self.data().flags & CLASS != 0
}
pub fn is_array(self) -> bool {
self.data().flags & IS_ARRAY != 0
}
pub fn is_frameless(self) -> bool {
self.data().flags & FRAMELESS != 0
}
pub fn proto(self) -> Option<Builtin> {
self.data().proto
}
pub fn source(self) -> Option<&'static str> {
self.data().source
}
pub fn own(self, key: &str) -> Option<&'static Prop> {
self.data().props.iter().find(|p| p.key == key)
}
pub fn own_keys(self) -> impl Iterator<Item = &'static str> {
self.data().props.iter().map(|p| p.key)
}
pub fn get(self, key: &str, receiver: &Value) -> Result<Value, String> {
get_inherited(self, key, receiver)
}
pub fn has_property(self, key: &str) -> bool {
let mut object = Some(self);
while let Some(o) = object {
if o.own(key).is_some() {
return true;
}
object = o.proto();
}
false
}
pub fn inherits_from(self, ancestor: Builtin) -> bool {
let mut object = self.proto();
while let Some(o) = object {
if o == ancestor {
return true;
}
object = o.proto();
}
false
}
}
impl fmt::Debug for Builtin {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
impl Data {
pub fn to_value(self) -> Value {
match self {
Data::Undefined => Value::Undefined,
Data::Null => Value::Null,
Data::Boolean(b) => Value::Boolean(b),
Data::Number(n) => Value::Number(n),
Data::String(s) => Value::string(s),
Data::Object(o) => Value::Builtin(o),
}
}
}
pub fn get_inherited(proto: Builtin, key: &str, receiver: &Value) -> Result<Value, String> {
let mut object = Some(proto);
while let Some(o) = object {
if let Some(prop) = o.own(key) {
return match &prop.slot {
Slot::Data(data) => Ok(data.to_value()),
Slot::Accessor { get: None, .. } => Ok(Value::Undefined),
Slot::Accessor {
get: Some(getter), ..
} => run_getter(*getter, receiver),
};
}
object = o.proto();
}
Ok(Value::Undefined)
}
pub fn intrinsic(proto: Builtin, key: &str) -> Option<Builtin> {
let mut object = Some(proto);
while let Some(o) = object {
if let Some(prop) = o.own(key) {
return match prop.slot {
Slot::Data(Data::Object(b)) => Some(b),
_ => None,
};
}
object = o.proto();
}
None
}
fn flag_getter_name(flag: u8) -> &'static str {
match flag {
b'd' => "hasIndices",
b'g' => "global",
b'i' => "ignoreCase",
b'm' => "multiline",
b's' => "dotAll",
b'u' => "unicode",
b'v' => "unicodeSets",
_ => "sticky",
}
}
const FLAGS: [u8; 8] = *b"dgimsuvy";
fn run_getter(getter: Getter, receiver: &Value) -> Result<Value, String> {
match getter {
Getter::Proto => Ok(prototype_of(receiver)),
Getter::Restricted => Err(RESTRICTED.into()),
Getter::RegExpFlag(flag) => match receiver {
Value::Builtin(Builtin::REGEXP_PROTOTYPE) => Ok(Value::Undefined),
_ if is_object(receiver) => Err(format!(
"TypeError: RegExp.prototype.{} getter called on non-RegExp object",
flag_getter_name(flag)
)),
_ => Err(format!(
"TypeError: RegExp.prototype.{} getter called on non-object {}",
flag_getter_name(flag),
call::describe_primitive(receiver)
)),
},
Getter::RegExpSource => match receiver {
Value::Builtin(Builtin::REGEXP_PROTOTYPE) => Ok(Value::string("(?:)")),
_ if is_object(receiver) => {
Err("TypeError: RegExp.prototype.source getter called on non-RegExp object".into())
}
_ => Err(format!(
"TypeError: RegExp.prototype.source getter called on non-object {}",
call::describe_primitive(receiver)
)),
},
Getter::RegExpFlags => {
if !is_object(receiver) {
return Err(format!(
"TypeError: RegExp.prototype.flags getter called on non-object {}",
call::describe_primitive(receiver)
));
}
let mut out = String::new();
for flag in FLAGS {
let value =
crate::next::schema::value::get_property(receiver, flag_getter_name(flag))?;
if value.to_boolean() {
out.push(flag as char);
}
}
Ok(Value::string(out))
}
Getter::RegExpLegacy(_) => Ok(Value::string("")),
Getter::Constant(object) => Ok(Value::Builtin(object)),
}
}
pub fn is_object(value: &Value) -> bool {
matches!(
value,
Value::Array(_) | Value::Object(_) | Value::Tag(_) | Value::Builtin(_)
)
}
pub fn prototype_of(value: &Value) -> Value {
let proto = match value {
Value::Undefined | Value::Null => return Value::Null,
Value::Boolean(_) => Builtin::BOOLEAN_PROTOTYPE,
Value::Number(_) => Builtin::NUMBER_PROTOTYPE,
Value::String(_) => Builtin::STRING_PROTOTYPE,
Value::Array(_) => Builtin::ARRAY_PROTOTYPE,
Value::Object(o) => match o.proto() {
crate::next::schema::value::Proto::Builtin(b) => *b,
crate::next::schema::value::Proto::Null => return Value::Null,
crate::next::schema::value::Proto::Value(v) => return v.clone(),
},
Value::Tag(_) => Builtin::TAG_PROTOTYPE,
Value::Builtin(b) => match b.proto() {
Some(p) => p,
None => return Value::Null,
},
};
Value::Builtin(proto)
}
pub fn inherited_by_plain_object(key: &str) -> Option<Value> {
let prop = Builtin::OBJECT_PROTOTYPE.own(key)?;
Some(match &prop.slot {
Slot::Data(data) => data.to_value(),
Slot::Accessor {
get: Some(Getter::Proto),
..
} => Value::Builtin(Builtin::OBJECT_PROTOTYPE),
Slot::Accessor { .. } => unreachable!("Object.prototype's only accessor is __proto__"),
})
}
pub fn harness_type(name: &str) -> Value {
let own = match name {
"String" => Builtin::STRING,
"Number" => Builtin::NUMBER,
"Boolean" => Builtin::BOOLEAN,
"Object" => Builtin::OBJECT,
"Array" => Builtin::ARRAY,
"Class" => Builtin::CLASS,
"Id" => Builtin::ID,
"ConditionalAttributeType" => Builtin::CONDITIONAL_ATTRIBUTE_TYPE,
"PartialFile" => Builtin::PARTIAL_FILE,
_ => {
return get_inherited(Builtin::OBJECT_PROTOTYPE, name, &Value::empty_object())
.unwrap_or(Value::Undefined);
}
};
Value::Builtin(own)
}