Skip to main content

Any

Struct Any 

Source
pub struct Any<A: IVm>(/* private fields */);
Expand description
use nanvm_lib::{
    vm::{Any, IVm, ToAny, String, Array, ToArray, ToObject, Object, Nullish, Number, BigInt},
    naive::Naive
};
fn any_test<A: IVm>() {
    let b: Any<A> = true.to_any();
    let n: Any<A> = Nullish::Null.to_any();
    let n: Any<A> = Number::from(42.0).to_any();
    let c: String<A> = "Hello".into();
    let m: Any<A> = c.to_any();
    let a: Array<A> = [].to_array();
    let o: Any<A> = a.to_any();
    let x: Object<A> = [].to_object();
    let p: Any<A> = x.to_any();
    let u: BigInt<A> = 123u64.into();
    let q: Any<A> = u.to_any();
}

any_test::<Naive>();

Implementations§

Source§

impl<A: IVm> Any<A>

Source

pub fn call(self, args: Any<A>) -> Result<Any<A>, Any<A>>

The EDAG’s () (['()', callee, args]): callee(...args), the second operand spread. Both operands are values, as every EDAG operand is: the callee a function and the arguments an array, and either being anything else throws — the TypeError JavaScript throws for calling what is not a function, or for spreading what is not iterable — through the conversions TryFrom<Any<A>> already makes. The result, answered or thrown, is the function’s own (IFunction::call).

Source§

impl<A: IVm> Any<A>

Source

pub fn conditional( self, consequent: impl FnOnce() -> Result<Self, Self>, alternate: impl FnOnce() -> Result<Self, Self>, ) -> Result<Self, Self>

?:. Not a core::ops trait — Rust has no operator for a ternary conditional (if/else takes a bool, not an Any<A>) — so this is a plain method over the native VM’s values.

Coerces self via ToBoolean, establishes the arm that selects — consequent or alternate — and returns its value. The arms are thunks because JavaScript establishes exactly one of them: true ? 1 : (1n / 0n) is 1, not a throw. FnOnce, since an arm is established at most once; a Result, since establishing it may throw, and that throw is this operation’s. https://tc39.es/ecma262/#sec-conditional-operator

Source§

impl<A: IVm> Any<A>

Source

pub fn member_present(self, key: Self, runtime: &Self) -> Result<Self, Self>

The internal member-presence query. Both operands are established before lookup; this performs the optional member-call guard without evaluating call arguments or checking that the answer is callable.

Source

pub fn dot(self, key: Self) -> PropertyLambda<A>

The EDAG’s . node, ['.', receiver, key] or with a continuation, ['.', receiver, key, k]: the property step, as a chain a continuation may follow (fjs/edag/README.md, Chains). dot(a, key).end() is a.b, the one spelling of the node whether or not a continuation follows, and the one property read nanvm-lib has; dot(a, key).end_call(&runtime, args) is the method call a.b(...args), the property called on its receiver, the runtime value asked for the body of a name the receiver does not own. The key is a value: a[k] evaluates both operands whatever they are.

A nullish receiver throws here, before any continuation’s arguments — the throw waits in the lambda until end or end_call surfaces it, so a.b(...c) on a nullish a throws with c untouched. What a live step reads or calls is [Member]’s.

Source

pub fn option_dot( self, key: impl FnOnce() -> Result<Self, Self>, ) -> OptionPropertyLambda<A>

The EDAG’s ?. node, ['?.', receiver, key, k]: a?.b, opening a short-circuit region. A nullish receiver skips the key and the rest of the chain — u?.[todo()] never calls todo — which is why the key is a thunk. Any other receiver takes the step dot takes.

Source§

impl<A: IVm> Any<A>

Source

pub fn get_iterator(self) -> Result<IteratorRecord<A>, Any<A>>

ECMAScript’s GetIterator(self, sync) (https://tc39.es/ecma262/#sec-getiterator), restricted to the values a FunctionalScript module can build: an array’s elements, or a string’s code points, each a string. Every other value — null, undefined, a boolean, a number, a bigint, an object, a function — is not iterable, and the TypeError is raised here, before any element. No object is iterable: a module cannot spell Symbol.iterator.

The one protocol behind array spread, call spread and array destructuring. Not Array::concat, which keeps a non-array item whole: [...'ab'] is ['a', 'b'], not ['ab'], and [...{}] throws.

Source§

impl<A: IVm> Any<A>

Source

pub fn instanceof_(self, constructor: Constructor) -> Result<Any<A>, Any<A>>

instanceof, against a built-in constructor. Not a core::ops trait — Rust has no operator to spell it with — so this is a plain method, named as typeof_ is: a trailing underscore keeps the reserved-word spelling recognizable. Never throws: the right side is a constructor by construction, so JavaScript’s TypeError for a non-callable right operand has no case here; stays a Result to match every other operator’s shape. https://tc39.es/ecma262/#sec-instanceofoperator

Source§

impl<A: IVm> Any<A>

Source

pub fn object_spread(self) -> ObjectSpread<A> ⓘ

What {...self} copies: the source side of ECMAScript’s CopyDataProperties (https://tc39.es/ecma262/#sec-copydataproperties), the own enumerable string-keyed properties of self in [[OwnPropertyKeys]] order. An object gives its entries, each key once with its last value; an array its elements keyed "0", "1", …; a string one entry per UTF-16 code unit; and a number, a bigint, a boolean, a function, null and undefined none.

Never throws, unlike Any::get_iterator: {...null} is {}, where Object.entries(null) and object rest destructuring both throw. So an executor may evaluate an object spread whose result goes unused and lose nothing.

Source

pub fn entries(self) -> Result<Self, Self>

Object.entries(self): what Any::object_spread copies, each entry a fresh [key, value] array, in a fresh array — an object’s entries, an array’s elements and a string’s code units under their index strings, nothing for a number, a bigint, a boolean or a function. Unlike the spread it throws on null and undefined, as ToObject does (member_access::non_nullish_receiver).

The code of the static function no FunctionalScript body defines, enumerating keys being what the language has no other way to do: Object.entries in the runtime value is a closure over it (fjs/js/runtime/README.md, “How NaNVM finds it”).

Source§

impl<A: IVm> Any<A>

Source

pub fn option_call( self, args: impl FnOnce() -> Result<Self, Self>, ) -> OptionLambda<A>

The EDAG’s ?.() node, ['?.()', callee, args, k]: a?.(...args), opening a short-circuit region. A nullish callee skips the arguments and the rest of the chain, which is why they are a thunk; any other callee has them evaluated first and is then called as Any::call calls it — 1?.(...args) is the TypeError 1(...args) is, after the arguments.

Source§

impl<A: IVm> Any<A>

Source

pub fn same_value(&self, other: &Self) -> bool

SameValue (https://tc39.es/ecma262/#sec-samevalue), the equality of Object.is: ===, except that NaN equals NaN and 0 does not equal -0. Every NaN is the one quiet NaN (Number::NAN), so equal bits are equal values.

Source§

impl<A: IVm> Any<A>

Source

pub fn lt(self, rhs: Self) -> Result<Self, Self>

<. Throws where ToPrimitive does, and where the text of a function without text would be compared (error::function_text).

Source

pub fn gt(self, rhs: Self) -> Result<Self, Self>

>.

Source

pub fn le(self, rhs: Self) -> Result<Self, Self>

<=. Not !(x > y): a NaN anywhere gives false.

Source

pub fn ge(self, rhs: Self) -> Result<Self, Self>

>=. Not !(x < y): a NaN anywhere gives false.

Source§

impl<A: IVm> Any<A>

Source

pub fn to_json(self) -> Result<String, JsonError>

An Any<A> -> JSON serializer covering everything with a direct JSON counterpart: numbers, strings, booleans, null, and arrays/objects recursed into. See JsonError for what’s deliberately unhandled.

The containers being written are a stack on the heap and the text goes into one buffer, so traversal does not recurse with nesting and does not copy each container’s text into its parent’s buffer.

Destruction still depends on the VM. With Naive, consuming a sole-owned deeply nested value can overflow the stack when its containers are released, including on an error. See nanvm/lib/todo/array-deep-nesting.md.

An object’s entries are its own, in [[OwnPropertyKeys]] order, each key once with its last value: the view JSON.stringify and an object spread both read (Object::own_entries).

Source§

impl<A: IVm> Any<A>

Source

pub fn typeof_(self) -> Result<Any<A>, Any<A>>

typeof. Not a core::ops trait — Rust has no unary operator to spell it with — so this is a plain method, the same as unary_plus. Named typeof_, not type_of: typeof is itself a reserved word, and a trailing underscore — the convention FJS and JS both use for this — resolves the collision while keeping the name recognizable. Never throws, but stays a Result to match every other operator’s shape. https://tc39.es/ecma262/#sec-typeof-operator

Source§

impl<A: IVm> Any<A>

Source

pub fn undefined() -> Self

undefined.

Source

pub fn is_nullish(&self) -> bool

Whether self is undefined or null: Unpacked::is_nullish over one unpack of a clone. A caller that already holds the Unpacked asks it instead and keeps its single conversion.

Source

pub fn defined(self) -> Option<Self>

None for undefined, Some(self) for every other value, null included: the argument an optional parameter was passed, if it was passed one. Unpacked::is_undefined over one unpack of a clone.

Source

pub fn unary_plus(self) -> Result<Any<A>, Any<A>>

Unary plus is nothing but coercion to number. We use unary_plus as ECMAScript unary plus operator, and we use coerce_to_number for internals in places where ECMAScript’s abstract function ToNumber is needed, and also when we need Result<Number, Any> result type; here unary_plus returns Result<Any, Any> to match public API type of unary plus operator. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus https://tc39.es/ecma262/#sec-unary-plus-operator

Source

pub fn number(self) -> Result<Any<A>, Any<A>>

Number(value): ToNumeric of the value, a bigint converted to the nearest number and anything else as unary_plus has it. They differ in the bigint alone: +1n throws, Number(1n) is 1. https://tc39.es/ecma262/#sec-number-constructor-number-value

Source

pub fn bigint(self) -> Result<Any<A>, Any<A>>

BigInt(value): ordinary primitive conversion with the number hint, then an exact integer conversion. This is an operator, not a value. https://tc39.es/ecma262/#sec-bigint-constructor

Source

pub fn pow(self, rhs: Self) -> Result<Self, Self>

**. Not a core::ops trait — Rust has no operator for exponentiation, so this is a plain method, the same as unary_plus.

Source

pub fn bitwise_not(self) -> Result<Self, Self>

~. Not a core::ops trait — Not is already claimed by this type’s own logical ! (any/not.rs) — so this is a plain method, the same as unary_plus/pow. https://tc39.es/ecma262/#sec-bitwise-not-operator

Source

pub fn unsigned_right_shift(self, rhs: Self) -> Result<Self, Self>

>>>. Not a core::ops trait — Rust has no unsigned-right-shift operator — so this is a plain method, the same as pow/bitwise_not. https://tc39.es/ecma262/#sec-unsigned-right-shift-operator

Source

pub fn entry(self, key: Self) -> Result<Self, Self>

The entry helper, (a, b) => { const x = Object.getOwnPropertyDescriptor(a, b); return x?.enumerable ? x.value : undefined; }, as a method: the value of the enumerable own property the key names — an Object’s field (Object::own_property), an Array’s element or a String’s code unit by its canonical index string (Array::entry, String::entry) — and undefined for none, an entry holding undefined included, which Any::has_entry tells apart, and for nothing a value owns without enumerating it — never a length, and nothing of a function. No getter invocation and no prototype chain (nanvm-lib objects have no __proto__ to walk in the first place). Not a core::ops trait — no Rust operator fits a keyed property lookup — so this is a plain method, the same as pow/bitwise_not/unsigned_right_shift.

The receiver is checked before the key is converted (member_access::non_nullish_receiver), so a nullish receiver throws regardless of what the key is, and a key whose conversion throws does so under a receiver that is not. The key converts as ToPropertyKey converts it, which is to_string here, the language having no symbols: 0 and "0" name one entry, and an object converts through its own toString. A Number, Boolean or BigInt receiver owns no entry and answers undefined, as every absent key does.

Source

pub fn has_entry(self, key: Self) -> Result<bool, Self>

The hasEntry helper: whether the enumerable own property key names is there, true for an entry holding undefined, which Any::entry reads as none, the receiver and the key treated as entry treats them, so a nullish receiver throws and a key converts first (spec/property-access/entries/README.md).

Source

pub fn is_nan(self) -> bool

Same as Number.isNaN in ECMAScript. TODO: check and test.

Source

pub fn to_string(self) -> Result<String<A>, Any<A>>

Source

pub fn to_string_unused(self) -> Result<(), Any<A>>

ToString where the algorithm discards the result: its throws still happen, and an object with its own toString is still refused, but a function’s text, which cannot throw and is never read here, is not needed — so [].join(f) answers "", as it does in JavaScript (nanvm/lib/todo/to-primitive.md, Stage 1). An array converts through its elements alone, since it owns no toString, so a function inside one is skipped too: [].join([f]) is "".

Source

pub fn to_number(self) -> Result<Number, Any<A>>

Source

pub fn to_boolean(self) -> bool

Never fails, unlike to_number/to_string/to_numeric — ToBoolean inspects the operand’s type directly and never calls ToPrimitive.

Source

pub fn to_numeric(self) -> Result<Numeric<A>, Any<A>>

Source§

impl<A: IVm> Any<A>

Source

pub fn to_integer_or_infinity(self) -> Result<f64, Any<A>>

ToIntegerOrInfinity(ToNumber(self)), the reading every index, count and position argument starts from. The one throw is ToNumber’s, a bigint’s TypeError among them.

Source§

impl<A: IVm> Any<A>

Source

pub fn to_primitive( self, preferred_type: Option<ToPrimitivePreferredType>, ) -> Result<Primitive<A>, Any<A>>

Coerces the value to a primitive type Primitive<A>, possibly producing an error result. https://tc39.es/ecma262/#sec-toprimitive

Trait Implementations§

Source§

impl<A: IVm> Add for Any<A>

Source§

type Output = Result<Any<A>, Any<A>>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<A: IVm> BitAnd for Any<A>

Source§

type Output = Result<Any<A>, Any<A>>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Self) -> Self::Output

Performs the & operation. Read more
Source§

impl<A: IVm> BitOr for Any<A>

Source§

type Output = Result<Any<A>, Any<A>>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the | operation. Read more
Source§

impl<A: IVm> BitXor for Any<A>

Source§

type Output = Result<Any<A>, Any<A>>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Self) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<A: Clone + IVm> Clone for Any<A>

Source§

fn clone(&self) -> Any<A>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A: IVm> Debug for Any<A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A: IVm> Div for Any<A>

Source§

type Output = Result<Any<A>, Any<A>>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl<A: IVm> From<&str> for Any<A>

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl<A: IVm> From<Any<A>> for Unpacked<A>

Source§

fn from(v: Any<A>) -> Self

Converts to this type from the input type.
Source§

impl<A: IVm> From<Unpacked<A>> for Any<A>

Source§

fn from(value: Unpacked<A>) -> Self

Converts to this type from the input type.
Source§

impl<A: IVm> Mul for Any<A>

Source§

type Output = Result<Any<A>, Any<A>>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<A: IVm> Neg for Any<A>

Source§

type Output = Result<Any<A>, Any<A>>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<A: IVm> Not for Any<A>

!. Coerces via ToBoolean and negates. Never throws — ToBoolean itself cannot fail — but stays a Result to match every other operator’s shape. https://tc39.es/ecma262/#sec-logical-not-operator

Source§

type Output = Result<Any<A>, Any<A>>

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl<A: IVm> PartialEq for Any<A>

Same as === in ECMAScript.

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<A: IVm> Rem for Any<A>

Source§

type Output = Result<Any<A>, Any<A>>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Self) -> Self::Output

Performs the % operation. Read more
Source§

impl<A: IVm> Shl for Any<A>

Source§

type Output = Result<Any<A>, Any<A>>

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: Self) -> Self::Output

Performs the << operation. Read more
Source§

impl<A: IVm> Shr for Any<A>

Source§

type Output = Result<Any<A>, Any<A>>

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: Self) -> Self::Output

Performs the >> operation. Read more
Source§

impl<A: IVm> Sub for Any<A>

Source§

type Output = Result<Any<A>, Any<A>>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<A: IVm> TryFrom<Any<A>> for Nullish

Source§

type Error = Any<A>

The type returned in the event of a conversion error.
Source§

fn try_from(value: Any<A>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<A: IVm> TryFrom<Any<A>> for bool

Source§

type Error = Any<A>

The type returned in the event of a conversion error.
Source§

fn try_from(value: Any<A>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<A: IVm> TryFrom<Any<A>> for Number

Source§

type Error = Any<A>

The type returned in the event of a conversion error.
Source§

fn try_from(value: Any<A>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<A: IVm> TryFrom<Any<A>> for Array<A>

Source§

type Error = Any<A>

The type returned in the event of a conversion error.
Source§

fn try_from(value: Any<A>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<A: IVm> TryFrom<Any<A>> for BigInt<A>

Source§

type Error = Any<A>

The type returned in the event of a conversion error.
Source§

fn try_from(value: Any<A>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<A: IVm> TryFrom<Any<A>> for Function<A>

Source§

type Error = Any<A>

The type returned in the event of a conversion error.
Source§

fn try_from(value: Any<A>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<A: IVm> TryFrom<Any<A>> for Object<A>

Source§

type Error = Any<A>

The type returned in the event of a conversion error.
Source§

fn try_from(value: Any<A>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<A: IVm> TryFrom<Any<A>> for String<A>

Source§

type Error = Any<A>

The type returned in the event of a conversion error.
Source§

fn try_from(value: Any<A>) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<A> Freeze for Any<A>
where A: Freeze,

§

impl<A> RefUnwindSafe for Any<A>
where A: RefUnwindSafe,

§

impl<A> Send for Any<A>
where A: Send,

§

impl<A> Sync for Any<A>
where A: Sync,

§

impl<A> Unpin for Any<A>
where A: Unpin,

§

impl<A> UnsafeUnpin for Any<A>
where A: UnsafeUnpin,

§

impl<A> UnwindSafe for Any<A>
where A: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToAny for T

Source§

fn to_any<A: IVm>(self) -> Any<A>
where Self: Into<Unpacked<A>>,

Source§

impl<T> ToArray for T

Source§

fn to_array<A: IVm>(self) -> Array<A>
where Self: Sized + IntoIterator<Item = Any<A>>,

Source§

impl<T> ToObject for T

Source§

fn to_object<A: IVm>(self) -> Object<A>
where Self: Sized + IntoIterator<Item = Property<A>>,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T

Source§

fn try_to_string<A: IVm>(self) -> Result<String<A>, Any<A>>
where Self: Sized + IntoIterator<Item = Result<u16, Any<A>>>,

Source§

fn to_string<A: IVm>(self) -> String<A>
where Self: Sized + IntoIterator<Item = u16>,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.