Skip to main content

JsValue

Struct JsValue 

Source
pub struct JsValue(/* private fields */);
Expand description

A JSValue: the eight bytes the C API passes by value.

Implementations§

Source§

impl JsValue

Source

pub const fn new(bits: u64) -> JsValue

Wrap raw bits. The only way to build one without going through a constructor, and what JS_VALUE_GET_* is on the other side of.

Source

pub const fn bits(self) -> u64

The raw bits (JS_VALUE_GET_UINT64).

Source

pub const fn tag(self) -> i32

The tag, as JS_VALUE_GET_TAG computes it: an arithmetic shift of the whole word.

A negative answer is a reference-counted payload, and only -1 (OBJECT) can reach the engine: the reference’s other negative tags name its own containers (strings, symbols, bytecode, modules) which live inside objects here instead.

Source

pub const fn norm_tag(self) -> i32

JS_VALUE_GET_NORM_TAG: the tag with every float spelling collapsed to JS_TAG_FLOAT64.

Source

pub const fn tag_is_float(tag: i32) -> bool

JS_TAG_IS_FLOAT64(tag): whether a tag outside the named range means “double”.

Source

pub const fn is_float(self) -> bool

Whether this value is a double (JS_TAG_IS_FLOAT64(JS_VALUE_GET_NORM_TAG(v))).

Source

pub const fn is_nan(self) -> bool

JS_VALUE_IS_NAN.

Source

pub const fn has_ref_count(self) -> bool

JS_VALUE_HAS_REF_COUNT: every negative tag.

Source

pub const fn payload(self) -> u64

JS_VALUE_GET_PTR: the low 47 bits as a pointer.

Matching the reference means not checking the range here — the mask is what C does — and rejecting an out-of-range pointer at the point it is built instead (JsValue::mkptr).

Source

pub const fn mkval(tag: i32, val: u32) -> JsValue

JS_MKVAL(tag, val): a tag with a 32-bit payload.

Source

pub fn mkptr(tag: i32, ptr: *mut ()) -> JsValue

JS_MKPTR(tag, ptr): a tag with a pointer payload.

§Panics

When ptr does not fit in 47 bits. That is a broken invariant rather than a recoverable error: the reference would truncate the pointer and hand C a value that reads and writes the wrong memory. Every allocator this crate controls keeps its blocks in the low half of the address space; a host that supplies its own allocator has to keep the same promise (see docs/m15-plan.md §3, D1).

Source

pub const fn is_exception(self) -> bool

JS_IsException.

Source

pub const fn is_undefined(self) -> bool

JS_IsUndefined.

Source

pub const fn is_null(self) -> bool

JS_IsNull.

Source

pub const fn is_bool(self) -> bool

JS_IsBool.

Source

pub const fn is_number(self) -> bool

JS_IsNumber: an integer or a double.

Source

pub const fn is_object(self) -> bool

JS_IsObject.

Source

pub const fn is_string(self) -> bool

JS_IsString.

Source

pub const fn to_int(self) -> i32

The integer payload, as JS_VALUE_GET_INT.

Source

pub const fn to_bool(self) -> bool

The boolean payload, as JS_VALUE_GET_BOOL.

Source

pub const fn to_float(self) -> f64

The double, as JS_VALUE_GET_FLOAT64.

No tag check: the reference’s macro does not have one either, and its callers always know the tag. JsValue::as_float is the checked spelling.

Source

pub const fn as_float(self) -> Option<f64>

The double, when this value is one.

Source

pub fn as_ptr(self) -> Option<*mut ()>

The pointer, when this value is a reference-counted one.

None rather than a null pointer for a non-pointer tag: the reference’s macro answers garbage there, and every caller of ours would have to re-check the tag.

Trait Implementations§

Source§

impl Clone for JsValue

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 Copy for JsValue

Source§

impl Debug for JsValue

Source§

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

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

impl Default for JsValue

Source§

fn default() -> JsValue

JS_UNDEFINED, which is what a zero-initialised JSValue must not be: bits 0 decode as the integer 0.

Source§

impl Eq for JsValue

Source§

impl Hash for JsValue

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for JsValue

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 StructuralPartialEq for JsValue

Auto Trait Implementations§

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.