Skip to main content

Value

Enum Value 

Source
pub enum Value {
    Undefined,
    Null,
    Bool(bool),
    Int(i32),
    Float(f64),
    String(Gc<JsString>),
    Object(Gc<JsObject>),
    CatchOffset(u32),
    ReturnAddress(u32),
    Hole,
    Uninitialized,
}
Expand description

A JavaScript value.

Variants§

§

Undefined

undefined

§

Null

null

§

Bool(bool)

true / false

§

Int(i32)

A 32-bit integer, exact.

§

Float(f64)

A double.

§

String(Gc<JsString>)

A string.

§

Object(Gc<JsObject>)

An object, function, or array.

§

CatchOffset(u32)

A try-handler target, pushed by catch and scanned for when an exception is raised.

QuickJS keeps this on the value stack as a JS_TAG_CATCH_OFFSET value and finds the innermost handler by walking the stack downwards, which is how try needs no separate table. Offset 0 marks an enumerator (a for-of loop) rather than a catch: it closes the iterator and keeps unwinding.

§

ReturnAddress(u32)

A gosub return position.

finally blocks are compiled as subroutines: gosub pushes the address to come back to and jumps into the block, ret pops the address and jumps back. QuickJS reuses the integer tag for this (and notes it as a hardening gap); a distinct variant keeps it out of reach of arithmetic.

§

Hole

An array hole: an index that was skipped rather than filled.

[1,,3] and [1,undefined,3] are different arrays: forEach skips the hole, in reports it absent, and the debug printer shows <1 empty item>. That distinction has to live in the element vector, and this is the marker for it. It is never observable from JavaScript: reading the index yields undefined and no language operation can produce it.

§

Uninitialized

The internal marker a let/const binding holds between its scope being entered and its initialiser running.

QuickJS calls this JS_UNINITIALIZED; it is deliberately not observable from JavaScript. It only ever sits in a slot or a cell, and every read that may see it (get_loc_check and friends) turns it into a ReferenceError.

Implementations§

Source§

impl Value

Source

pub fn string(heap: &Heap, text: impl Into<String>) -> Value

A string value on heap.

Source

pub fn as_f64(&self) -> Option<f64>

The number as a double, widening an Value::Int.

Source

pub fn as_str(&self) -> Option<&str>

The value as a string, if it is one.

Source

pub fn as_object(&self) -> Option<&Gc<JsObject>>

The object, if this is one.

Source

pub fn is_nullish(&self) -> bool

Whether this is one of the two null-ish values.

Source

pub fn type_of(&self) -> &'static str

typeof value.

Source

pub fn to_boolean(&self) -> bool

ToBoolean, which is also what a conditional jump tests.

Source

pub fn to_number(&self) -> f64

ToNumber, for the arithmetic operators.

Source

pub fn to_int32(&self) -> i32

ToInt32, the conversion the bitwise operators and <</>>/>>> use.

Source

pub fn to_uint32(&self) -> u32

ToUint32.

Source

pub fn to_js_string(&self) -> String

ToString.

Named to_js_string rather than to_string so it does not shadow std::fmt::Display: the two disagree on purpose (display keeps the sign of -0), and an accidental to_string on a Value would silently pick the wrong one.

Source

pub fn strict_equals(&self, other: &Value) -> bool

===.

Source

pub fn loose_equals(&self, other: &Value) -> bool

==, with the coercion rules.

NaN compares unequal to everything including itself, which the numeric branch already gets right. An object compares by identity against another object and by its primitive form against a primitive.

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Self

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 Debug for Value

Source§

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

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

impl Trace for Value

Source§

fn trace(&self, tracer: &mut Tracer<'_>)

Report every Gc owned by self to tracer.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Value

§

impl !Send for Value

§

impl !Sync for Value

§

impl !UnwindSafe for Value

§

impl Forget for Value

§

impl Freeze for Value

§

impl Move for Value

§

impl Unpin for Value

§

impl UnsafeUnpin for Value

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.