pub enum Value {
}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.
Symbol(Gc<JsSymbol>)
A symbol: a unique property key that is not a string.
BigInt(Gc<BigInt>)
A BigInt: an integer of any size, stored on the GC heap.
A separate variant rather than a number because the two never mix: 1n + 1 is a
TypeError, and 1n === 1 is false.
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
impl Value
Sourcepub fn string_wtf8(heap: &Heap, text: Wtf8) -> Value
pub fn string_wtf8(heap: &Heap, text: Wtf8) -> Value
A string from bytes that are already WTF-8, which is how a string containing an
unpaired surrogate reaches a Value.
Sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
The number as a double, widening an Value::Int.
Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
The value as a string, if it is one with no unpaired surrogate.
Sourcepub fn as_wtf8(&self) -> Option<&Wtf8>
pub fn as_wtf8(&self) -> Option<&Wtf8>
The value’s string content as WTF-8, which covers every string including one with an unpaired surrogate.
Sourcepub fn is_nullish(&self) -> bool
pub fn is_nullish(&self) -> bool
Whether this is one of the two null-ish values.
Sourcepub fn to_boolean(&self) -> bool
pub fn to_boolean(&self) -> bool
ToBoolean, which is also what a conditional jump tests.
Sourcepub fn to_js_string(&self) -> String
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.
Sourcepub fn strict_equals(&self, other: &Value) -> bool
pub fn strict_equals(&self, other: &Value) -> bool
===.
Sourcepub fn same_value(&self, other: &Value) -> bool
pub fn same_value(&self, other: &Value) -> bool
SameValue, the equality Object.is uses and a proxy invariant needs.
It is Value::same_value_zero plus the one extra distinction: +0 and -0
are different values here, which is what makes Object.is(0, -0) false.
Sourcepub fn same_value_zero(&self, other: &Value) -> bool
pub fn same_value_zero(&self, other: &Value) -> bool
SameValueZero, the equality a Map key and a Set element use.
It differs from === in exactly one place: NaN equals itself. That is what makes
map.set(NaN, 1); map.get(NaN) work, and it is why a Map cannot use strict_equals
directly. A bigint compares by value, since its representation is canonical.
Sourcepub fn loose_equals(&self, other: &Value) -> bool
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.