#![allow(clippy::redundant_closure_call)]
use super::{Builtin, Data, Slot, Wrapped, is_object, prototype_of};
use crate::markdown_it::js;
use crate::next::schema::value::{
self, JsString, JsStringBuilder, RANGE_ERROR, Value, get_property, has_own_property,
};
use crate::types::JsObject;
use std::cell::Cell;
#[derive(Debug, Clone)]
pub enum Called {
Value(Value),
Wrapper(Value),
}
impl Called {
pub fn is_primitive(&self) -> bool {
match self {
Called::Value(v) => !is_object(v),
Called::Wrapper(_) => false,
}
}
pub fn primitive(self) -> Option<Value> {
match self {
Called::Value(v) if !is_object(&v) => Some(v),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Site {
Convert,
Json,
}
thread_local! {
static SITE: Cell<Site> = const { Cell::new(Site::Convert) };
static DEPTH: Cell<usize> = const { Cell::new(0) };
}
pub fn at_site<T>(site: Site, f: impl FnOnce() -> T) -> T {
let before = SITE.replace(site);
let out = f();
SITE.set(before);
out
}
pub const MAX_CALL_DEPTH: usize = 200;
struct Depth;
#[cfg(test)]
thread_local! {
pub(super) static STACK_PROBE: Cell<(usize, usize, usize)> = const { Cell::new((0, usize::MAX, 0)) };
}
impl Depth {
fn enter() -> Result<Depth, String> {
let depth = DEPTH.get();
#[cfg(test)]
{
let here = &depth as *const usize as usize;
STACK_PROBE.with(|p| {
let (top, low, deepest) = p.get();
let top = if depth == 0 { here } else { top };
p.set((top, low.min(here), deepest.max(depth + 1)));
});
}
if depth >= MAX_CALL_DEPTH || !crate::next::transformer::call_fits_stack() {
return Err(RANGE_ERROR.into());
}
DEPTH.set(depth + 1);
Ok(Depth)
}
}
impl Drop for Depth {
fn drop(&mut self) {
DEPTH.set(DEPTH.get() - 1);
}
}
type R<T> = Result<T, String>;
pub(super) fn type_error(message: impl AsRef<str>) -> String {
format!("TypeError: {}", message.as_ref())
}
pub(super) fn range_error(message: impl AsRef<str>) -> String {
format!("RangeError: {}", message.as_ref())
}
pub const MAX_STRING_LENGTH: usize = (1 << 29) - 24;
fn invalid_string_length() -> String {
range_error("Invalid string length")
}
pub(super) fn arg(args: &[Value], i: usize) -> Value {
args.get(i).cloned().unwrap_or(Value::Undefined)
}
pub fn type_of(value: &Value) -> &'static str {
match value {
Value::Undefined => "undefined",
Value::Null => "object",
Value::Boolean(_) => "boolean",
Value::Number(_) => "number",
Value::String(_) => "string",
Value::Array(_) | Value::Object(_) | Value::Tag(_) => "object",
Value::Builtin(b) if b.is_callable() => "function",
Value::Builtin(_) => "object",
}
}
pub fn is_callable(value: &Value) -> bool {
matches!(value, Value::Builtin(b) if b.is_callable())
}
pub fn default_call_site(value: &Value) -> String {
let mut out = String::from(type_of(value));
match value {
Value::String(s) => {
out.push_str(" \"");
out.push_str(&s.to_string_lossy());
out.push('"');
}
Value::Null => out.push_str(" null"),
Value::Boolean(b) => out.push_str(if *b { " true" } else { " false" }),
Value::Number(n) => {
out.push(' ');
out.push_str(&js::number_to_string(*n));
}
_ => {}
}
out
}
fn not_a_function(value: &Value) -> String {
not_a_function_at(Site::Convert, value)
}
fn not_a_function_at(site: Site, value: &Value) -> String {
match site {
Site::Json => type_error("JSON.stringify is not a function"),
Site::Convert => type_error(format!("{} is not a function", default_call_site(value))),
}
}
pub(super) fn callback_not_a_function(value: &Value) -> String {
type_error(format!("{} is not a function", default_call_site(value)))
}
pub fn describe_primitive(value: &Value) -> String {
match value {
Value::Undefined => "undefined".into(),
Value::Null => "null".into(),
Value::Boolean(b) => b.to_string(),
Value::Number(n) => js::number_to_string(*n),
Value::String(s) => s.to_string_lossy().into_owned(),
_ => no_side_effects_to_string(value),
}
}
fn data_property(value: &Value, key: &str) -> Value {
match value {
Value::Object(o) => {
if let Some(v) = o.get(key) {
return v.clone();
}
}
Value::Tag(_) | Value::Array(_) | Value::String(_) => {
if let Ok(true) = has_own_property(value, key) {
return get_property(value, key).unwrap_or(Value::Undefined);
}
}
Value::Builtin(b) => {
if let Some(p) = b.own(key) {
return match &p.slot {
Slot::Data(d) => d.to_value(),
Slot::Accessor { .. } => Value::Undefined,
};
}
}
_ => {}
}
let mut proto = match prototype_of(value) {
Value::Builtin(b) => Some(b),
_ => None,
};
while let Some(b) = proto {
if let Some(p) = b.own(key) {
return match &p.slot {
Slot::Data(d) => d.to_value(),
Slot::Accessor { .. } => Value::Undefined,
};
}
proto = b.proto();
}
Value::Undefined
}
pub fn no_side_effects_to_string(value: &Value) -> String {
if let Value::Builtin(b) = value
&& let Some(source) = b.source()
{
let units: Vec<u16> = source.encode_utf16().collect();
if units.len() > 128 {
let mut out = String::from_utf16_lossy(&units[..111]);
out.push_str("...<omitted>...");
out.push_str(&String::from_utf16_lossy(&units[units.len() - 2..]));
return out;
}
return source.to_string();
}
if !is_object(value) {
return describe_primitive(value);
}
if let Value::Builtin(Builtin::OBJECT_PROTOTYPE_TO_STRING) = data_property(value, "toString")
&& let Value::Builtin(ctor) = data_property(value, "constructor")
&& ctor.is_callable()
{
let name = match ctor.own("name").map(|p| &p.slot) {
Some(Slot::Data(Data::String(name))) => *name,
_ => "",
};
if !name.is_empty() {
return format!("#<{name}>");
}
}
format!("[object {}]", class_name(value))
}
fn class_name(value: &Value) -> &'static str {
match value {
Value::Array(_) => "Array",
Value::Builtin(b) => b.data().class,
_ => "Object",
}
}
pub(super) fn to_string(value: &Value) -> R<JsString> {
value.to_js_string()
}
pub(super) fn to_number(value: &Value) -> R<f64> {
value::to_number(value)
}
pub(super) fn to_integer_or_infinity(value: &Value) -> R<f64> {
let n = to_number(value)?;
Ok(if n.is_nan() || n == 0.0 {
0.0
} else if n.is_infinite() {
n
} else {
n.trunc()
})
}
pub(super) fn to_length(value: &Value) -> R<f64> {
let n = to_integer_or_infinity(value)?;
Ok(if n <= 0.0 {
0.0
} else {
n.min(9007199254740991.0)
})
}
pub(super) fn to_uint32(value: &Value) -> R<u32> {
let n = to_number(value)?;
if !n.is_finite() || n == 0.0 {
return Ok(0);
}
Ok(n.trunc().rem_euclid(4294967296.0) as u32)
}
pub(super) fn to_uint16(value: &Value) -> R<u16> {
let n = to_number(value)?;
if !n.is_finite() || n == 0.0 {
return Ok(0);
}
Ok(n.trunc().rem_euclid(65536.0) as u16)
}
pub(super) fn to_key(value: &Value) -> R<String> {
Ok(to_string(value)?.to_string_lossy().into_owned())
}
pub(super) fn index_key(k: f64) -> String {
js::number_to_string(k)
}
pub(super) fn coercible(this: &Value, name: &str) -> R<()> {
if this.is_nullish() {
return Err(type_error(format!("{name} called on null or undefined")));
}
Ok(())
}
pub(super) fn length_of(o: &Value) -> R<f64> {
to_length(&get_property(o, "length")?)
}
pub(super) fn has_property(o: &Value, key: &str) -> R<bool> {
let mut at = o.clone();
loop {
if has_own_property(&at, key)? {
return Ok(true);
}
match value::prototype_of(&at) {
Some(value::Proto::Value(p)) => at = p,
Some(value::Proto::Builtin(b)) => return Ok(b.has_property(key)),
Some(value::Proto::Null) | None => return Ok(false),
}
}
}
pub(super) fn present_indices(o: &Value, len: f64) -> Vec<f64> {
let mut out: Vec<f64> = Vec::new();
let mut at = Some(o.clone());
while let Some(v) = at.take() {
match &v {
Value::Object(obj) => out.extend(
obj.keys()
.filter_map(|k| crate::types::array_index(k))
.map(f64::from)
.filter(|&i| i < len),
),
Value::Array(items) => out.extend(
items
.present()
.map(|(i, _)| f64::from(i))
.take_while(|&i| i < len),
),
Value::String(s) => {
out.extend((0..s.len_utf16()).map(|i| i as f64).filter(|&i| i < len))
}
_ => {}
}
if let Some(value::Proto::Value(p)) = value::prototype_of(&v) {
at = Some(p);
}
}
out.sort_by(f64::total_cmp);
out.dedup();
out
}
pub(super) fn with_set(o: &Value, sets: &[(String, Option<Value>)]) -> Value {
match o {
Value::Object(obj) => {
let (mut copy, proto) = (**obj).clone().into_parts();
for (k, v) in sets {
match v {
Some(v) => {
copy.insert(k.clone(), v.clone());
}
None => {
copy.remove(k);
}
}
}
Value::object_with_proto(copy, proto)
}
Value::Array(items) => {
let mut length = items.len() as u64;
let mut present: std::collections::BTreeMap<u32, Value> =
items.present().map(|(i, v)| (i, v.clone())).collect();
for (k, v) in sets {
if k == "length" {
if let Some(Value::Number(n)) = v {
length = *n as u64;
present.retain(|&i, _| u64::from(i) < length);
}
} else if let Some(i) = crate::types::array_index(k) {
match v {
Some(v) => {
present.insert(i, v.clone());
length = length.max(u64::from(i) + 1);
}
None => {
present.remove(&i);
}
}
}
}
let named: indexmap::IndexMap<String, Value> =
items.named().map(|(k, v)| (k.clone(), v.clone())).collect();
Value::sparse_array(length.min(u64::from(u32::MAX)) as u32, present, named)
}
other => other.clone(),
}
}
fn object_of(entries: Vec<(&str, Value)>) -> Value {
let mut o = JsObject::new();
for (k, v) in entries {
o.insert(k, v);
}
Value::object(o)
}
pub(super) fn opaque_object() -> Called {
Called::Value(Value::empty_object())
}
pub(super) fn opaque_function() -> Called {
Called::Value(Value::Builtin(Builtin::FUNCTION_PROTOTYPE))
}
pub(super) fn ok(v: Value) -> R<Called> {
Ok(Called::Value(v))
}
pub(super) fn ok_str(s: impl Into<JsString>) -> R<Called> {
ok(Value::String(s.into()))
}
pub(super) fn ok_num(n: f64) -> R<Called> {
ok(Value::Number(n))
}
pub(super) fn ok_bool(b: bool) -> R<Called> {
ok(Value::Boolean(b))
}
fn same_value(a: &Value, b: &Value) -> bool {
match (a, b) {
(Value::Number(x), Value::Number(y)) => {
(x.is_nan() && y.is_nan()) || (x == y && x.is_sign_negative() == y.is_sign_negative())
}
_ => value::strict_equals(a, b),
}
}
pub(super) fn same_value_zero(a: &Value, b: &Value) -> bool {
match (a, b) {
(Value::Number(x), Value::Number(y)) => (x.is_nan() && y.is_nan()) || x == y,
_ => value::strict_equals(a, b),
}
}
pub fn call(f: Builtin, this: &Value, args: &[Value]) -> Result<Called, String> {
let site = SITE.get();
if !f.is_frameless() {
SITE.set(Site::Convert);
}
let out = crate::depth::grow!(call_from(site, f, this, args));
SITE.set(site);
out
}
fn call_from(site: Site, f: Builtin, this: &Value, args: &[Value]) -> Result<Called, String> {
if !f.is_callable() {
return Err(not_a_function(&Value::Builtin(f)));
}
let _depth = Depth::enter()?;
if f.is_class() {
let name = match f.own("name").map(|p| &p.slot) {
Some(Slot::Data(Data::String(name))) => *name,
_ => "",
};
return Err(type_error(format!(
"Class constructor {name} cannot be invoked without 'new'"
)));
}
match f {
Builtin::FUNCTION_PROTOTYPE => (|| -> R<Called> { ok(Value::Undefined) })(),
Builtin::OBJECT_PROTOTYPE_TO_LOCALE_STRING => (|| -> R<Called> {
coercible(this, "Object.prototype.toLocaleString")?;
let to_string = get_property(this, "toString")?;
call_value(&to_string, this, &[])
})(),
b if is_object_function(b) => object_function(b, this, args),
b if is_function_function(b) => function_function(site, b, this, args),
b if is_array_function(b) => super::call_array::array_function(b, this, args),
b if is_string_function(b) => super::call_string::string_function(b, this, args),
b if is_number_function(b) => number_function(b, this, args),
b if is_boolean_function(b) => boolean_function(b, this, args),
b if is_regexp_function(b) => super::call_string::regexp_function(b, this, args),
b if is_markdoc_function(b) => markdoc_function(b, this, args),
b if name_starts(b, &["Node", "Variable", "AstFunction"]) => {
super::call_ast::ast_function(b, this, args)
}
b if name_starts(b, &["GeneratorFunction", "Generator", "Iterator"]) => {
super::call_ast::iterator_function(b, this, args)
}
other => Err(no_such_function(other)),
}
}
pub(super) fn no_such_function(f: Builtin) -> String {
format!("TypeError: {} has no implementation here", f.name())
}
pub(super) fn call_value(value: &Value, this: &Value, args: &[Value]) -> R<Called> {
match value {
Value::Builtin(f) if f.is_callable() => call(*f, this, args),
other => Err(not_a_function(other)),
}
}
fn name_starts(b: Builtin, prefixes: &[&str]) -> bool {
let name = b.name();
prefixes.iter().any(|p| {
name == *p
|| name
.strip_prefix(p)
.is_some_and(|rest| rest.starts_with('.'))
})
}
fn is_object_function(b: Builtin) -> bool {
name_starts(b, &["Object"])
}
fn is_function_function(b: Builtin) -> bool {
name_starts(b, &["Function"])
}
fn is_array_function(b: Builtin) -> bool {
name_starts(b, &["Array"])
}
fn is_string_function(b: Builtin) -> bool {
name_starts(b, &["String"])
}
fn is_number_function(b: Builtin) -> bool {
name_starts(b, &["Number"])
}
fn is_boolean_function(b: Builtin) -> bool {
name_starts(b, &["Boolean"])
}
fn is_regexp_function(b: Builtin) -> bool {
name_starts(b, &["RegExp"])
}
fn is_markdoc_function(b: Builtin) -> bool {
name_starts(
b,
&[
"Tag",
"Class",
"Id",
"ConditionalAttributeType",
"PartialFile",
],
)
}
fn to_object(value: &Value) -> R<Called> {
if value.is_nullish() {
return Err(type_error("Cannot convert undefined or null to object"));
}
Ok(if is_object(value) {
Called::Value(value.clone())
} else {
Called::Wrapper(value.clone())
})
}
pub(super) fn to_object_value(value: &Value) -> R<Value> {
to_object(value).map(|c| match c {
Called::Value(v) | Called::Wrapper(v) => v,
})
}
struct Descriptor {
value: Value,
accessor: Option<(bool, bool)>,
writable: bool,
enumerable: bool,
configurable: bool,
}
fn own_descriptor(o: &Value, key: &str) -> R<Option<Descriptor>> {
Ok(match o {
Value::Object(obj) => obj.get(key).map(|v| Descriptor {
value: v.clone(),
accessor: None,
writable: true,
enumerable: true,
configurable: true,
}),
Value::Tag(_) => {
if has_own_property(o, key)? {
Some(Descriptor {
value: get_property(o, key)?,
accessor: None,
writable: true,
enumerable: true,
configurable: true,
})
} else {
None
}
}
Value::Array(items) => {
if key == "length" {
Some(Descriptor {
value: Value::Number(items.len() as f64),
accessor: None,
writable: true,
enumerable: false,
configurable: false,
})
} else {
match crate::types::array_index(key) {
Some(i) => items.get(i as usize),
None => items.named_get(key),
}
.map(|v| Descriptor {
value: v.clone(),
accessor: None,
writable: true,
enumerable: true,
configurable: true,
})
}
}
Value::String(s) => {
if key == "length" {
Some(Descriptor {
value: Value::Number(s.len_utf16() as f64),
accessor: None,
writable: false,
enumerable: false,
configurable: false,
})
} else if let Some(i) =
crate::types::array_index(key).filter(|&i| (i as usize) < s.len_utf16())
{
Some(Descriptor {
value: get_property(o, &i.to_string())?,
accessor: None,
writable: false,
enumerable: true,
configurable: false,
})
} else {
None
}
}
Value::Builtin(b) => b.own(key).map(|p| {
let (value, accessor) = match &p.slot {
Slot::Data(d) => (d.to_value(), None),
Slot::Accessor { get, set } => (Value::Undefined, Some((get.is_some(), *set))),
};
Descriptor {
value,
accessor,
writable: p.attrs & super::WRITABLE != 0,
enumerable: p.attrs & super::ENUMERABLE != 0,
configurable: p.attrs & super::CONFIGURABLE != 0,
}
}),
_ => None,
})
}
fn own_keys(o: &Value) -> Vec<String> {
match o {
Value::Object(obj) => obj.keys().cloned().collect(),
Value::Tag(_) => ["$$mdtype", "name", "attributes", "children"]
.iter()
.map(|s| s.to_string())
.collect(),
Value::Array(items) => items
.present()
.map(|(i, _)| i.to_string())
.chain(std::iter::once("length".to_string()))
.chain(items.named().map(|(k, _)| k.clone()))
.collect(),
Value::String(s) => (0..s.len_utf16())
.map(|i| i.to_string())
.chain(std::iter::once("length".to_string()))
.collect(),
Value::Builtin(b) => b.own_keys().map(str::to_string).collect(),
_ => Vec::new(),
}
}
fn from_descriptor(d: Descriptor) -> Value {
match d.accessor {
None => object_of(vec![
("value", d.value),
("writable", Value::Boolean(d.writable)),
("enumerable", Value::Boolean(d.enumerable)),
("configurable", Value::Boolean(d.configurable)),
]),
Some((get, set)) => {
let f = |present: bool| {
if present {
Value::Builtin(Builtin::FUNCTION_PROTOTYPE)
} else {
Value::Undefined
}
};
object_of(vec![
("get", f(get)),
("set", f(set)),
("enumerable", Value::Boolean(d.enumerable)),
("configurable", Value::Boolean(d.configurable)),
])
}
}
}
fn enumerable_own(o: &Value) -> R<Vec<(String, Value)>> {
let mut out = Vec::new();
for key in own_keys(o) {
if let Some(d) = own_descriptor(o, &key)?
&& d.enumerable
{
let v = get_property(o, &key)?;
out.push((key, v));
}
}
Ok(out)
}
pub(super) fn object_to_string(this: &Value) -> String {
let tag = match this {
Value::Undefined => "Undefined",
Value::Null => "Null",
Value::Boolean(_) => "Boolean",
Value::Number(_) => "Number",
Value::String(_) => "String",
Value::Array(_) => "Array",
Value::Object(_) | Value::Tag(_) => "Object",
Value::Builtin(b) => b.data().class,
};
format!("[object {tag}]")
}
fn object_function(f: Builtin, this: &Value, args: &[Value]) -> R<Called> {
let a0 = arg(args, 0);
match f {
Builtin::OBJECT => (|| -> R<Called> {
if a0.is_nullish() {
Ok(opaque_object())
} else {
to_object(&a0)
}
})(),
Builtin::OBJECT_ASSIGN => (|| -> R<Called> {
let target = to_object(&a0)?;
for source in args.iter().skip(1) {
if source.is_nullish() {
continue;
}
let from = to_object_value(source)?;
if !enumerable_own(&from)?.is_empty() {
return Ok(match target {
Called::Value(t) => {
let sets: Vec<(String, Option<Value>)> = enumerable_own(&from)?
.into_iter()
.map(|(k, v)| (k, Some(v)))
.collect();
Called::Value(with_set(&t, &sets))
}
wrapper => wrapper,
});
}
}
Ok(target)
})(),
Builtin::OBJECT_GET_OWN_PROPERTY_DESCRIPTOR => (|| -> R<Called> {
let o = to_object_value(&a0)?;
let key = to_key(&arg(args, 1))?;
ok(own_descriptor(&o, &key)?
.map(from_descriptor)
.unwrap_or(Value::Undefined))
})(),
Builtin::OBJECT_GET_OWN_PROPERTY_DESCRIPTORS => (|| -> R<Called> {
let o = to_object_value(&a0)?;
let mut out = JsObject::new();
for key in own_keys(&o) {
if let Some(d) = own_descriptor(&o, &key)? {
out.insert(key, from_descriptor(d));
}
}
ok(Value::object(out))
})(),
Builtin::OBJECT_GET_OWN_PROPERTY_NAMES => (|| -> R<Called> {
let o = to_object_value(&a0)?;
ok(Value::array(
own_keys(&o).into_iter().map(Value::from).collect(),
))
})(),
Builtin::OBJECT_GET_OWN_PROPERTY_SYMBOLS => (|| -> R<Called> {
to_object_value(&a0)?;
ok(Value::array(Vec::new()))
})(),
Builtin::OBJECT_HAS_OWN => (|| -> R<Called> {
let o = to_object_value(&a0)?;
let key = to_key(&arg(args, 1))?;
ok_bool(has_own_property(&o, &key)?)
})(),
Builtin::OBJECT_IS => (|| -> R<Called> { ok_bool(same_value(&a0, &arg(args, 1))) })(),
Builtin::OBJECT_PREVENT_EXTENSIONS | Builtin::OBJECT_SEAL | Builtin::OBJECT_FREEZE => {
(|| -> R<Called> { ok(a0) })()
}
Builtin::OBJECT_CREATE => (|| -> R<Called> {
if !is_object(&a0) && !matches!(a0, Value::Null) {
return Err(type_error(format!(
"Object prototype may only be an Object or null: {}",
describe_primitive(&a0)
)));
}
let properties = arg(args, 1);
if !properties.is_undefined() {
let props = to_object_value(&properties)?;
if !enumerable_own(&props)?.is_empty() {
return Err(type_error(
"Property description must be an object: not modeled",
));
}
}
Ok(opaque_object())
})(),
Builtin::OBJECT_DEFINE_PROPERTIES => (|| -> R<Called> {
if !is_object(&a0) {
return Err(type_error("Object.defineProperties called on non-object"));
}
let props = to_object_value(&arg(args, 1))?;
if enumerable_own(&props)?.is_empty() {
return ok(a0);
}
Err(type_error(
"Object.defineProperties on an object: not modeled",
))
})(),
Builtin::OBJECT_DEFINE_PROPERTY => (|| -> R<Called> {
if !is_object(&a0) {
return Err(type_error("Object.defineProperty called on non-object"));
}
let attributes = arg(args, 2);
if !is_object(&attributes) {
return Err(type_error(format!(
"Property description must be an object: {}",
describe_primitive(&attributes)
)));
}
Err(type_error(
"Object.defineProperty on an object: not modeled",
))
})(),
Builtin::OBJECT_GET_PROTOTYPE_OF => (|| -> R<Called> {
let o = to_object_value(&a0)?;
ok(prototype_of(&o))
})(),
Builtin::OBJECT_SET_PROTOTYPE_OF => (|| -> R<Called> {
if a0.is_nullish() {
return Err(type_error(
"Object.setPrototypeOf called on null or undefined",
));
}
let proto = arg(args, 1);
if !is_object(&proto) && !matches!(proto, Value::Null) {
return Err(type_error(format!(
"Object prototype may only be an Object or null: {}",
describe_primitive(&proto)
)));
}
if !is_object(&a0) {
return ok(a0);
}
Err(type_error(
"Object.setPrototypeOf on an object: not modeled",
))
})(),
Builtin::OBJECT_IS_EXTENSIBLE => (|| -> R<Called> { ok_bool(is_object(&a0)) })(),
Builtin::OBJECT_IS_FROZEN | Builtin::OBJECT_IS_SEALED => {
(|| -> R<Called> { ok_bool(!is_object(&a0)) })()
}
Builtin::OBJECT_KEYS => (|| -> R<Called> {
let o = to_object_value(&a0)?;
ok(Value::array(
enumerable_own(&o)?
.into_iter()
.map(|(k, _)| Value::from(k))
.collect(),
))
})(),
Builtin::OBJECT_VALUES => (|| -> R<Called> {
let o = to_object_value(&a0)?;
ok(Value::array(
enumerable_own(&o)?.into_iter().map(|(_, v)| v).collect(),
))
})(),
Builtin::OBJECT_ENTRIES => (|| -> R<Called> {
let o = to_object_value(&a0)?;
ok(Value::array(
enumerable_own(&o)?
.into_iter()
.map(|(k, v)| Value::array(vec![Value::from(k), v]))
.collect(),
))
})(),
Builtin::OBJECT_FROM_ENTRIES => (|| -> R<Called> {
let entries = iterate(&a0, "not iterable")?;
let mut out = JsObject::new();
for entry in entries {
if !is_object(&entry) {
return Err(type_error(format!(
"Iterator value {} is not an entry object",
describe_primitive(&entry)
)));
}
let k = to_key(&get_property(&entry, "0")?)?;
let v = get_property(&entry, "1")?;
out.insert(k, v);
}
ok(Value::object(out))
})(),
Builtin::OBJECT_GROUP_BY => (|| -> R<Called> {
if a0.is_nullish() {
return Err(type_error("Object.groupBy called on null or undefined"));
}
let callback = arg(args, 1);
Err(callback_not_a_function(&callback))
})(),
Builtin::OBJECT_PROTOTYPE_DEFINE_GETTER | Builtin::OBJECT_PROTOTYPE_DEFINE_SETTER => {
(|| -> R<Called> {
to_object_value(this)?;
let which = if f == Builtin::OBJECT_PROTOTYPE_DEFINE_GETTER {
"__defineGetter__"
} else {
"__defineSetter__"
};
if !is_callable(&arg(args, 1)) {
return Err(type_error(format!(
"Object.prototype.{which}: Expecting function"
)));
}
Err(type_error(format!(
"Object.prototype.{which} with a function: not modeled"
)))
})()
}
Builtin::OBJECT_PROTOTYPE_HAS_OWN_PROPERTY => (|| -> R<Called> {
let key = to_key(&a0)?;
let o = to_object_value(this)?;
ok_bool(has_own_property(&o, &key)?)
})(),
Builtin::OBJECT_PROTOTYPE_LOOKUP_GETTER | Builtin::OBJECT_PROTOTYPE_LOOKUP_SETTER => {
(|| -> R<Called> {
let o = to_object_value(this)?;
let key = to_key(&a0)?;
let getter = f == Builtin::OBJECT_PROTOTYPE_LOOKUP_GETTER;
let mut current = Some(o);
while let Some(object) = current {
if let Some(d) = own_descriptor(&object, &key)? {
return match d.accessor {
Some((get, set)) if (getter && get) || (!getter && set) => {
Ok(opaque_function())
}
_ => ok(Value::Undefined),
};
}
current = match prototype_of(&object) {
Value::Null => None,
proto => Some(proto),
};
}
ok(Value::Undefined)
})()
}
Builtin::OBJECT_PROTOTYPE_IS_PROTOTYPE_OF => (|| -> R<Called> {
if !is_object(&a0) {
return ok_bool(false);
}
let o = to_object_value(this)?;
let mut proto = prototype_of(&a0);
while !matches!(proto, Value::Null) {
if same_value(&proto, &o) {
return ok_bool(true);
}
proto = prototype_of(&proto);
}
ok_bool(false)
})(),
Builtin::OBJECT_PROTOTYPE_PROPERTY_IS_ENUMERABLE => (|| -> R<Called> {
let key = to_key(&a0)?;
let o = to_object_value(this)?;
ok_bool(own_descriptor(&o, &key)?.is_some_and(|d| d.enumerable))
})(),
Builtin::OBJECT_PROTOTYPE_TO_STRING => {
(|| -> R<Called> { ok_str(object_to_string(this)) })()
}
Builtin::OBJECT_PROTOTYPE_VALUE_OF => (|| -> R<Called> { to_object(this) })(),
other => Err(no_such_function(other)),
}
}
pub(super) fn iterate(value: &Value, what: &str) -> R<Vec<Value>> {
match value {
Value::String(s) => {
let units = s.to_utf16();
let mut out = Vec::new();
let mut i = 0;
while i < units.len() {
let len = if (0xD800..=0xDBFF).contains(&units[i])
&& i + 1 < units.len()
&& (0xDC00..=0xDFFF).contains(&units[i + 1])
{
2
} else {
1
};
out.push(Value::String(JsString::from_utf16(&units[i..i + len])));
i += len;
}
Ok(out)
}
Value::Array(items) => Ok((0..items.len())
.map(|i| items.get(i).cloned().unwrap_or(Value::Undefined))
.collect()),
Value::Builtin(Builtin::ARRAY_PROTOTYPE) | Value::Builtin(Builtin::STRING_PROTOTYPE) => {
Ok(Vec::new())
}
other => Err(type_error(format!(
"{} is {what}",
describe_primitive(other)
))),
}
}
fn function_function(site: Site, f: Builtin, this: &Value, args: &[Value]) -> R<Called> {
match f {
Builtin::FUNCTION => (|| -> R<Called> {
for a in args {
to_string(a)?;
}
Ok(opaque_function())
})(),
Builtin::FUNCTION_PROTOTYPE_APPLY => (|| -> R<Called> {
if !is_callable(this) {
return Err(not_a_function_at(site, this));
}
let array = arg(args, 1);
let list = if array.is_nullish() {
Vec::new()
} else if !is_object(&array) {
return Err(type_error("CreateListFromArrayLike called on non-object"));
} else {
let len = length_of(&array)?;
let mut list = Vec::new();
let mut k = 0.0;
while k < len {
list.push(get_property(&array, &index_key(k))?);
k += 1.0;
}
list
};
call_value(this, &arg(args, 0), &list)
})(),
Builtin::FUNCTION_PROTOTYPE_CALL => (|| -> R<Called> {
if !is_callable(this) {
return Err(not_a_function_at(site, this));
}
let rest: Vec<Value> = args.iter().skip(1).cloned().collect();
call_value(this, &arg(args, 0), &rest)
})(),
Builtin::FUNCTION_PROTOTYPE_BIND => (|| -> R<Called> {
if !is_callable(this) {
return Err(type_error("Bind must be called on a function"));
}
Ok(opaque_function())
})(),
Builtin::FUNCTION_PROTOTYPE_TO_STRING => (|| -> R<Called> {
match this {
Value::Builtin(b) if b.is_callable() => {
ok_str(b.source().expect("a function has a source"))
}
_ => Err(type_error(
"Function.prototype.toString requires that 'this' be a Function",
)),
}
})(),
other => Err(no_such_function(other)),
}
}
fn this_number(this: &Value, method: &str) -> R<f64> {
match this {
Value::Number(n) => Ok(*n),
Value::Builtin(b) => match b.data().wrapped {
Some(Wrapped::Number(n)) => Ok(n),
_ => Err(type_error(format!(
"Number.prototype.{method} requires that 'this' be a Number"
))),
},
_ => Err(type_error(format!(
"Number.prototype.{method} requires that 'this' be a Number"
))),
}
}
fn this_boolean(this: &Value, method: &str) -> R<bool> {
match this {
Value::Boolean(b) => Ok(*b),
Value::Builtin(b) => match b.data().wrapped {
Some(Wrapped::Boolean(v)) => Ok(v),
_ => Err(type_error(format!(
"Boolean.prototype.{method} requires that 'this' be a Boolean"
))),
},
_ => Err(type_error(format!(
"Boolean.prototype.{method} requires that 'this' be a Boolean"
))),
}
}
fn parse_float(s: &[u16]) -> f64 {
let s = js::trim_start(s);
let text: String = s
.iter()
.take_while(|&&c| c < 0x80)
.map(|&c| c as u8 as char)
.collect();
let (sign, rest) = match text.as_bytes().first() {
Some(b'-') => (-1.0, &text[1..]),
Some(b'+') => (1.0, &text[1..]),
_ => (1.0, &text[..]),
};
if rest.starts_with("Infinity") {
return sign * f64::INFINITY;
}
let b = rest.as_bytes();
let mut i = 0;
let int_digits = b.iter().take_while(|c| c.is_ascii_digit()).count();
i += int_digits;
let mut frac_digits = 0;
if i < b.len() && b[i] == b'.' {
frac_digits = b[i + 1..].iter().take_while(|c| c.is_ascii_digit()).count();
if int_digits > 0 || frac_digits > 0 {
i += 1 + frac_digits;
}
}
if int_digits == 0 && frac_digits == 0 {
return f64::NAN;
}
if i < b.len() && (b[i] == b'e' || b[i] == b'E') {
let mut j = i + 1;
if j < b.len() && (b[j] == b'+' || b[j] == b'-') {
j += 1;
}
let exp_digits = b[j..].iter().take_while(|c| c.is_ascii_digit()).count();
if exp_digits > 0 {
i = j + exp_digits;
}
}
let literal = &rest[..i];
let literal = literal.strip_suffix('.').unwrap_or(literal);
sign * literal.parse::<f64>().unwrap_or(f64::NAN)
}
fn number_to_radix_string(x: f64, radix: u32) -> String {
if x.is_nan() {
return "NaN".into();
}
if x.is_infinite() {
return if x > 0.0 {
"Infinity".into()
} else {
"-Infinity".into()
};
}
if x == 0.0 {
return "0".into();
}
const CHARS: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyz";
let negative = x < 0.0;
let value = x.abs();
let mut integer = value.floor();
let mut fraction = value - integer;
let mut delta = 0.5 * (next_up(value) - value);
delta = delta.max(next_up(0.0));
let mut frac_digits = Vec::new();
if fraction >= delta {
loop {
fraction *= f64::from(radix);
delta *= f64::from(radix);
let digit = fraction.floor() as usize;
frac_digits.push(CHARS[digit]);
fraction -= digit as f64;
if (fraction > 0.5 || (fraction == 0.5 && (digit & 1) == 1)) && fraction + delta > 1.0 {
loop {
let Some(last) = frac_digits.pop() else {
integer += 1.0;
break;
};
let d = (last as char).to_digit(36).expect("a digit") + 1;
if d < radix {
frac_digits.push(CHARS[d as usize]);
break;
}
}
break;
}
if fraction < delta {
break;
}
}
}
let mut int_digits = Vec::new();
let mut exponent = 0;
while integer / f64::from(radix).powi(exponent) >= f64::from(radix) {
exponent += 1;
}
let mut n = integer;
if n < 9007199254740992.0 {
if n == 0.0 {
int_digits.push(b'0');
}
while n >= 1.0 {
let d = (n % f64::from(radix)) as usize;
int_digits.push(CHARS[d]);
n = ((n - d as f64) / f64::from(radix)).floor();
}
int_digits.reverse();
} else {
let mut digits = Vec::new();
while n >= 1.0 {
let rem = n % f64::from(radix);
digits.push(CHARS[rem as usize]);
n = (n - rem) / f64::from(radix);
}
digits.reverse();
int_digits = digits;
}
let mut out = String::new();
if negative {
out.push('-');
}
out.push_str(std::str::from_utf8(&int_digits).expect("ASCII digits"));
if !frac_digits.is_empty() {
out.push('.');
out.push_str(std::str::from_utf8(&frac_digits).expect("ASCII digits"));
}
out
}
fn next_up(x: f64) -> f64 {
if x.is_nan() || x == f64::INFINITY {
return x;
}
if x == 0.0 {
return f64::from_bits(1);
}
let bits = x.to_bits();
f64::from_bits(if x > 0.0 { bits + 1 } else { bits - 1 })
}
fn to_fixed(x: f64, digits: usize) -> String {
let negative = x < 0.0;
let decimal = exact_decimal(x.abs());
let rounded = round_decimal(&decimal, digits);
let mut out = String::new();
if negative {
out.push('-');
}
out.push_str(&rounded);
out
}
fn exact_decimal(x: f64) -> (String, String) {
let bits = x.to_bits();
let exponent = ((bits >> 52) & 0x7ff) as i32;
let mantissa = if exponent == 0 {
(bits & ((1 << 52) - 1)) << 1
} else {
(bits & ((1 << 52) - 1)) | (1 << 52)
};
let e = exponent - 1075;
let mut digits: Vec<u32> = mantissa
.to_string()
.bytes()
.map(|b| u32::from(b - b'0'))
.collect();
let mut frac_len = 0usize;
if e >= 0 {
for _ in 0..e {
let mut carry = 0;
for d in digits.iter_mut().rev() {
let v = *d * 2 + carry;
*d = v % 10;
carry = v / 10;
}
if carry > 0 {
digits.insert(0, carry);
}
}
} else {
for _ in 0..(-e) {
let mut carry = 0;
for d in digits.iter_mut().rev() {
let v = *d * 5 + carry;
*d = v % 10;
carry = v / 10;
}
if carry > 0 {
digits.insert(0, carry);
}
frac_len += 1;
}
}
while digits.len() <= frac_len {
digits.insert(0, 0);
}
let split = digits.len() - frac_len;
let int: String = digits[..split]
.iter()
.map(|d| char::from(b'0' + *d as u8))
.collect();
let frac: String = digits[split..]
.iter()
.map(|d| char::from(b'0' + *d as u8))
.collect();
let int = int.trim_start_matches('0').to_string();
(if int.is_empty() { "0".into() } else { int }, frac)
}
fn round_decimal((int, frac): &(String, String), digits: usize) -> String {
let mut all: Vec<u8> = int.bytes().chain(frac.bytes()).collect();
let keep = int.len() + digits;
let mut frac_padded = frac.clone();
while frac_padded.len() < digits {
frac_padded.push('0');
all.push(b'0');
}
let round_up = all.get(keep).is_some_and(|&d| d >= b'5');
all.truncate(keep);
if round_up {
let mut i = all.len();
loop {
if i == 0 {
all.insert(0, b'1');
break;
}
i -= 1;
if all[i] == b'9' {
all[i] = b'0';
} else {
all[i] += 1;
break;
}
}
}
let int_len = all.len() - digits;
let mut out = String::from_utf8(all[..int_len].to_vec()).expect("digits");
if digits > 0 {
out.push('.');
out.push_str(std::str::from_utf8(&all[int_len..]).expect("digits"));
}
out
}
fn number_function(f: Builtin, this: &Value, args: &[Value]) -> R<Called> {
let a0 = arg(args, 0);
match f {
Builtin::NUMBER => (|| -> R<Called> {
ok_num(if args.is_empty() {
0.0
} else {
to_number(&a0)?
})
})(),
Builtin::NUMBER_IS_FINITE => {
(|| -> R<Called> { ok_bool(matches!(a0, Value::Number(n) if n.is_finite())) })()
}
Builtin::NUMBER_IS_INTEGER => (|| -> R<Called> {
ok_bool(matches!(a0, Value::Number(n) if n.is_finite() && n.trunc() == n))
})(),
Builtin::NUMBER_IS_NAN => {
(|| -> R<Called> { ok_bool(matches!(a0, Value::Number(n) if n.is_nan())) })()
}
Builtin::NUMBER_IS_SAFE_INTEGER => (|| -> R<Called> {
ok_bool(
matches!(a0, Value::Number(n) if n.is_finite() && n.trunc() == n && n.abs() <= 9007199254740991.0),
)
})(),
Builtin::NUMBER_PARSE_FLOAT => (|| -> R<Called> {
let s = to_string(&a0)?;
ok_num(parse_float(&s.to_utf16()))
})(),
Builtin::NUMBER_PARSE_INT => (|| -> R<Called> {
let s = to_string(&a0)?;
let radix = to_number(&arg(args, 1))?;
let r = if radix.is_nan() || radix == 0.0 || !radix.is_finite() {
0
} else {
radix.trunc().rem_euclid(4294967296.0) as i64 as i32
};
if r != 0 && !(2..=36).contains(&r) {
return ok_num(f64::NAN);
}
ok_num(js::parse_int(&s.to_utf16(), r as u32))
})(),
Builtin::NUMBER_PROTOTYPE_VALUE_OF => {
(|| -> R<Called> { ok_num(this_number(this, "valueOf")?) })()
}
Builtin::NUMBER_PROTOTYPE_TO_STRING => (|| -> R<Called> {
let x = this_number(this, "toString")?;
let radix = arg(args, 0);
let r = if radix.is_undefined() {
10.0
} else {
to_integer_or_infinity(&radix)?
};
if !(2.0..=36.0).contains(&r) {
return Err(range_error("toString() radix must be between 2 and 36"));
}
if r == 10.0 {
ok_str(js::number_to_string(x))
} else {
ok_str(number_to_radix_string(x, r as u32))
}
})(),
Builtin::NUMBER_PROTOTYPE_TO_LOCALE_STRING => (|| -> R<Called> {
let x = this_number(this, "toLocaleString")?;
super::call_string::locale_language(&a0)?;
ok_str(locale_number(x))
})(),
Builtin::NUMBER_PROTOTYPE_TO_FIXED => (|| -> R<Called> {
let x = this_number(this, "toFixed")?;
let f = to_integer_or_infinity(&a0)?;
if !f.is_finite() || !(0.0..=100.0).contains(&f) {
return Err(range_error(
"toFixed() digits argument must be between 0 and 100",
));
}
if !x.is_finite() {
return ok_str(js::number_to_string(x));
}
if x.abs() >= 1e21 {
return ok_str(js::number_to_string(x));
}
let s = to_fixed(x, f as usize);
ok_str(if x == 0.0 {
s.trim_start_matches('-').to_string()
} else {
s
})
})(),
Builtin::NUMBER_PROTOTYPE_TO_EXPONENTIAL => (|| -> R<Called> {
let x = this_number(this, "toExponential")?;
let f = to_integer_or_infinity(&a0)?;
if !x.is_finite() {
return ok_str(js::number_to_string(x));
}
if !f.is_finite() || !(0.0..=100.0).contains(&f) {
return Err(range_error(
"toExponential() argument must be between 0 and 100",
));
}
ok_str(to_exponential(
x,
if a0.is_undefined() {
None
} else {
Some(f as usize)
},
))
})(),
Builtin::NUMBER_PROTOTYPE_TO_PRECISION => (|| -> R<Called> {
let x = this_number(this, "toPrecision")?;
if a0.is_undefined() {
return ok_str(js::number_to_string(x));
}
let p = to_integer_or_infinity(&a0)?;
if !x.is_finite() {
return ok_str(js::number_to_string(x));
}
if !p.is_finite() || !(1.0..=100.0).contains(&p) {
return Err(range_error(
"toPrecision() argument must be between 1 and 100",
));
}
ok_str(to_precision(x, p as usize))
})(),
other => Err(no_such_function(other)),
}
}
fn locale_number(x: f64) -> String {
if x.is_nan() {
return "NaN".into();
}
if x.is_infinite() {
return if x > 0.0 { "∞".into() } else { "-∞".into() };
}
let rounded = round_decimal(&exact_decimal(x.abs()), 3);
let (int, frac) = rounded.split_once('.').unwrap_or((&rounded, ""));
let frac = frac.trim_end_matches('0');
let mut grouped = String::new();
for (i, c) in int.chars().enumerate() {
if i > 0 && (int.len() - i) % 3 == 0 {
grouped.push(',');
}
grouped.push(c);
}
let mut out = String::new();
if x < 0.0 && (int != "0" || !frac.is_empty()) {
out.push('-');
}
out.push_str(&grouped);
if !frac.is_empty() {
out.push('.');
out.push_str(frac);
}
out
}
fn significant(x: f64, n: usize) -> (String, i32) {
let (int, frac) = exact_decimal(x.abs());
let all = format!("{}{}", if int == "0" { "" } else { &int }, frac);
let leading = all.bytes().take_while(|&b| b == b'0').count();
let e = if int == "0" {
-(leading as i32) - 1
} else {
int.len() as i32 - 1
};
let digits: Vec<u8> = all.bytes().skip(leading).collect();
let mut kept: Vec<u8> = digits.iter().copied().take(n).collect();
while kept.len() < n {
kept.push(b'0');
}
let mut e = e;
if digits.get(n).is_some_and(|&d| d >= b'5') {
let mut i = kept.len();
loop {
if i == 0 {
kept.insert(0, b'1');
kept.pop();
e += 1;
break;
}
i -= 1;
if kept[i] == b'9' {
kept[i] = b'0';
} else {
kept[i] += 1;
break;
}
}
}
(String::from_utf8(kept).expect("digits"), e)
}
fn to_exponential(x: f64, fraction_digits: Option<usize>) -> String {
let sign = if x < 0.0 { "-" } else { "" };
let (digits, e) = if x == 0.0 {
("0".repeat(fraction_digits.unwrap_or(0) + 1), 0)
} else {
match fraction_digits {
Some(f) => significant(x, f + 1),
None => {
let s = format!("{:e}", x.abs());
let (m, exp) = s.split_once('e').expect("an exponent");
(m.replace('.', ""), exp.parse().expect("an exponent"))
}
}
};
let mut out = String::from(sign);
out.push_str(&digits[..1]);
if digits.len() > 1 {
out.push('.');
out.push_str(&digits[1..]);
}
out.push('e');
out.push(if e < 0 { '-' } else { '+' });
out.push_str(&e.abs().to_string());
out
}
fn to_precision(x: f64, p: usize) -> String {
let sign = if x < 0.0 { "-" } else { "" };
let (digits, e) = if x == 0.0 {
("0".repeat(p), 0)
} else {
significant(x, p)
};
let mut out = String::from(sign);
if e < -6 || e >= p as i32 {
out.push_str(&digits[..1]);
if p > 1 {
out.push('.');
out.push_str(&digits[1..]);
}
out.push('e');
out.push(if e < 0 { '-' } else { '+' });
out.push_str(&e.abs().to_string());
return out;
}
if e == p as i32 - 1 {
out.push_str(&digits);
} else if e >= 0 {
let k = e as usize + 1;
out.push_str(&digits[..k]);
out.push('.');
out.push_str(&digits[k..]);
} else {
out.push_str("0.");
out.push_str(&"0".repeat((-(e + 1)) as usize));
out.push_str(&digits);
}
out
}
fn boolean_function(f: Builtin, this: &Value, args: &[Value]) -> R<Called> {
match f {
Builtin::BOOLEAN => (|| -> R<Called> { ok_bool(arg(args, 0).to_boolean()) })(),
Builtin::BOOLEAN_PROTOTYPE_TO_STRING => (|| -> R<Called> {
ok_str(if this_boolean(this, "toString")? {
"true"
} else {
"false"
})
})(),
Builtin::BOOLEAN_PROTOTYPE_VALUE_OF => {
(|| -> R<Called> { ok_bool(this_boolean(this, "valueOf")?) })()
}
other => Err(no_such_function(other)),
}
}
fn validation_error(id: &str, message: String) -> Value {
object_of(vec![
("id", Value::string(id)),
("level", Value::string("error")),
("message", Value::string(message)),
])
}
fn interpolate(value: &Value) -> R<String> {
Ok(to_string(value)?.to_string_lossy().into_owned())
}
fn markdoc_function(f: Builtin, _this: &Value, args: &[Value]) -> R<Called> {
let a0 = arg(args, 0);
match f {
Builtin::TAG_IS_TAG => (|| -> R<Called> {
if a0.is_nullish() {
return ok_bool(false);
}
let t = get_property(&a0, "$$mdtype")?;
ok_bool(matches!(t, Value::String(ref s) if s == "Tag"))
})(),
Builtin::CLASS_PROTOTYPE_VALIDATE => (|| -> R<Called> {
let t = type_of(&a0);
if t == "string" || t == "object" {
return ok(Value::array(Vec::new()));
}
let key = interpolate(&arg(args, 2))?;
ok(Value::array(vec![validation_error(
"attribute-type-invalid",
format!("Attribute '{key}' must be type 'string | object'"),
)]))
})(),
Builtin::CLASS_PROTOTYPE_TRANSFORM => (|| -> R<Called> {
if !a0.to_boolean() || matches!(a0, Value::String(_)) {
return ok(a0);
}
let mut classes: Vec<String> = Vec::new();
for (k, v) in enumerable_own(&to_object_value(&a0)?)? {
if v.to_boolean() {
classes.push(k);
}
}
ok_str(classes.join(" "))
})(),
Builtin::ID_PROTOTYPE_VALIDATE => (|| -> R<Called> {
if let Value::String(s) = &a0
&& s.code_units().next().is_some_and(|c| {
(u16::from(b'a')..=u16::from(b'z')).contains(&c)
|| (u16::from(b'A')..=u16::from(b'Z')).contains(&c)
})
{
return ok(Value::array(Vec::new()));
}
ok(Value::array(vec![validation_error(
"attribute-value-invalid",
"The 'id' attribute must start with a letter".into(),
)]))
})(),
Builtin::CONDITIONAL_ATTRIBUTE_TYPE_PROTOTYPE_VALIDATE => (|| -> R<Called> {
let t = type_of(&a0);
if t == "boolean" || a0.is_nullish() || t == "object" {
return ok(Value::array(Vec::new()));
}
let key = interpolate(&arg(args, 2))?;
ok(Value::array(vec![validation_error(
"attribute-type-invalid",
format!(
"Attribute '{key}' must be type 'boolean | object' (null or undefined are also allowed)"
),
)]))
})(),
Builtin::PARTIAL_FILE_PROTOTYPE_VALIDATE => (|| -> R<Called> {
let config = arg(args, 1);
if config.is_nullish() {
return Err(type_error(format!(
"Cannot read properties of {} (reading 'partials')",
if config.is_undefined() {
"undefined"
} else {
"null"
}
)));
}
let partials = match get_property(&config, "partials")? {
Value::Undefined => Value::empty_object(),
p => p,
};
let key = to_key(&a0)?;
let partial = get_property(&partials, &key)?;
if !partial.to_boolean() {
let file = interpolate(&a0)?;
return ok(Value::array(vec![object_of(vec![
("id", Value::string("attribute-value-invalid")),
("level", Value::string("error")),
(
"message",
Value::string(format!(
"Partial `{file}` not found. The 'file' attribute must be set in `config.partials`"
)),
),
])]));
}
ok(Value::array(Vec::new()))
})(),
other => Err(no_such_function(other)),
}
}
pub(super) struct Text {
out: JsStringBuilder,
len: usize,
}
impl Text {
pub(super) fn new() -> Self {
Text {
out: JsStringBuilder::new(),
len: 0,
}
}
pub(super) fn push(&mut self, s: &JsString) -> R<()> {
self.len += s.len_utf16();
if self.len > MAX_STRING_LENGTH {
return Err(invalid_string_length());
}
self.out.push_js(s);
Ok(())
}
pub(super) fn push_str(&mut self, s: &str) -> R<()> {
self.len += s.encode_utf16().count();
if self.len > MAX_STRING_LENGTH {
return Err(invalid_string_length());
}
self.out.push_str(s);
Ok(())
}
pub(super) fn reserve(&self, n: f64) -> R<()> {
if self.len as f64 + n > MAX_STRING_LENGTH as f64 {
return Err(invalid_string_length());
}
Ok(())
}
pub(super) fn finish(self) -> JsString {
self.out.finish()
}
}
pub(super) use {
arg as argument, callback_not_a_function as callback_error, coercible as require_coercible,
has_property as has_prop, index_key as key_of, length_of as len_of, ok as done,
ok_bool as done_bool, ok_num as done_num, ok_str as done_str, opaque_object as opaque,
present_indices as indices, range_error as range, same_value_zero as same_zero,
to_integer_or_infinity as integer, to_key as property_key, to_length as length,
to_number as number, to_object_value as object_value, to_string as string, to_uint16 as uint16,
to_uint32 as uint32, type_error as type_err, with_set as set_on,
};