Struct JsValue

pub struct JsValue { /* private fields */ }

Representation of an object owned by JS.

A JsValue doesn't actually live in Rust right now but actually in a table owned by the wasm-bindgen generated JS glue code. Eventually the ownership will transfer into Wasm directly and this will likely become more efficient, but for now it may be slightly slow.

Implementations

impl JsValue

const NULL: JsValue = _;

The null JS value constant.

const UNDEFINED: JsValue = _;

The undefined JS value constant.

const TRUE: JsValue = _;

The true JS value constant.

const FALSE: JsValue = _;

The false JS value constant.

fn from_str(s: &str) -> JsValue

Creates a new JS value which is a string.

The utf-8 string provided is copied to the JS heap and the string will be owned by the JS garbage collector.

fn from_f64(n: f64) -> JsValue

Creates a new JS value which is a number.

This function creates a JS value representing a number (a heap allocated number) and returns a handle to the JS version of it.

fn bigint_from_str(s: &str) -> JsValue

Creates a new JS value which is a bigint from a string representing a number.

This function creates a JS value representing a bigint (a heap allocated large integer) and returns a handle to the JS version of it.

const fn from_bool(b: bool) -> JsValue

Creates a new JS value which is a boolean.

This function creates a JS object representing a boolean (a heap allocated boolean) and returns a handle to the JS version of it.

const fn undefined() -> JsValue

Creates a new JS value representing undefined.

const fn null() -> JsValue

Creates a new JS value representing null.

fn symbol(description: Option<&str>) -> JsValue

Creates a new JS symbol with the optional description specified.

This function will invoke the Symbol constructor in JS and return the JS object corresponding to the symbol created.

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

Returns the f64 value of this JS value if it's an instance of a number.

If this JS value is not an instance of a number then this returns None.

fn is_string(&self) -> bool

Tests whether this JS value is a JS string.

fn as_string(&self) -> Option<String>

If this JS value is a string value, this function copies the JS string value into Wasm linear memory, encoded as UTF-8, and returns it as a Rust String.

To avoid the copying and re-encoding, consider the JsString::try_from() function from js-sys instead.

If this JS value is not an instance of a string or if it's not valid utf-8 then this returns None.

UTF-16 vs UTF-8

JavaScript strings in general are encoded as UTF-16, but Rust strings are encoded as UTF-8. This can cause the Rust string to look a bit different than the JS string sometimes. For more details see the documentation about the str type which contains a few caveats about the encodings.

fn as_bool(&self) -> Option<bool>

Returns the bool value of this JS value if it's an instance of a boolean.

If this JS value is not an instance of a boolean then this returns None.

fn is_null(&self) -> bool

Tests whether this JS value is null

fn is_undefined(&self) -> bool

Tests whether this JS value is undefined

fn is_null_or_undefined(&self) -> bool

Tests whether this JS value is null or undefined

fn is_symbol(&self) -> bool

Tests whether the type of this JS value is symbol

fn is_object(&self) -> bool

Tests whether typeof self == "object" && self !== null.

fn is_array(&self) -> bool

Tests whether this JS value is an instance of Array.

fn is_function(&self) -> bool

Tests whether the type of this JS value is function.

fn is_bigint(&self) -> bool

Tests whether the type of this JS value is bigint.

fn js_typeof(&self) -> JsValue

Applies the unary typeof JS operator on a JsValue.

MDN documentation

fn js_in(&self, obj: &JsValue) -> bool

Applies the binary in JS operator on the two JsValues.

MDN documentation

fn is_truthy(&self) -> bool

Tests whether the value is "truthy".

fn is_falsy(&self) -> bool

Tests whether the value is "falsy".

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

Compare two JsValues for equality, using the == operator in JS.

MDN documentation

fn bit_not(&self) -> JsValue

Applies the unary ~ JS operator on a JsValue.

MDN documentation

fn unsigned_shr(&self, rhs: &Self) -> u32

Applies the binary >>> JS operator on the two JsValues.

MDN documentation

fn checked_div(&self, rhs: &Self) -> Self

Applies the binary / JS operator on two JsValues, catching and returning any RangeError thrown.

MDN documentation

fn pow(&self, rhs: &Self) -> Self

Applies the binary ** JS operator on the two JsValues.

MDN documentation

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

Applies the binary < JS operator on the two JsValues.

MDN documentation

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

Applies the binary <= JS operator on the two JsValues.

MDN documentation

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

Applies the binary >= JS operator on the two JsValues.

MDN documentation

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

Applies the binary > JS operator on the two JsValues.

MDN documentation

fn unchecked_into_f64(&self) -> f64

Applies the unary + JS operator on a JsValue. Can throw.

MDN documentation

Trait Implementations

impl Add for JsValue

type Output = <&'static JsValue as Add>::Output;
fn add(self, other: JsValue) -> <&'static JsValue as Add<&'static JsValue>>::Output

impl Add<&JsValue> for JsValue

type Output = <&'static JsValue as Add>::Output;
fn add(self, other: &JsValue) -> <&'static JsValue as Add<&'static JsValue>>::Output

impl AsRef<JsValue> for JsValue

fn as_ref(&self) -> &JsValue

impl BitAnd for JsValue

type Output = <&'static JsValue as BitAnd>::Output;
fn bitand(self, other: JsValue) -> <&'static JsValue as BitAnd<&'static JsValue>>::Output

impl BitAnd<&JsValue> for JsValue

type Output = <&'static JsValue as BitAnd>::Output;
fn bitand(self, other: &JsValue) -> <&'static JsValue as BitAnd<&'static JsValue>>::Output

impl BitOr for JsValue

type Output = <&'static JsValue as BitOr>::Output;
fn bitor(self, other: JsValue) -> <&'static JsValue as BitOr<&'static JsValue>>::Output

impl BitOr<&JsValue> for JsValue

type Output = <&'static JsValue as BitOr>::Output;
fn bitor(self, other: &JsValue) -> <&'static JsValue as BitOr<&'static JsValue>>::Output

impl BitXor for JsValue

type Output = <&'static JsValue as BitXor>::Output;
fn bitxor(self, other: JsValue) -> <&'static JsValue as BitXor<&'static JsValue>>::Output

impl BitXor<&JsValue> for JsValue

type Output = <&'static JsValue as BitXor>::Output;
fn bitxor(self, other: &JsValue) -> <&'static JsValue as BitXor<&'static JsValue>>::Output

impl Clone for JsValue

fn clone(&self) -> JsValue

impl Debug for JsValue

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

impl Default for JsValue

fn default() -> Self

impl Div for JsValue

type Output = <&'static JsValue as Div>::Output;
fn div(self, other: JsValue) -> <&'static JsValue as Div<&'static JsValue>>::Output

impl Div<&JsValue> for JsValue

type Output = <&'static JsValue as Div>::Output;
fn div(self, other: &JsValue) -> <&'static JsValue as Div<&'static JsValue>>::Output

impl Drop for JsValue

fn drop(&mut self)

impl From<JsError> for JsValue

fn from(error: JsError) -> Self

impl From<Null> for JsValue

fn from(obj: Null) -> JsValue

impl From<String> for JsValue

fn from(s: String) -> JsValue

impl From<Undefined> for JsValue

fn from(obj: Undefined) -> JsValue

impl From<bool> for JsValue

fn from(s: bool) -> JsValue

impl From<f32> for JsValue

fn from(n: f32) -> JsValue

impl From<f64> for JsValue

fn from(n: f64) -> JsValue

impl From<i128> for JsValue

fn from(arg: i128) -> JsValue

impl From<i16> for JsValue

fn from(n: i16) -> JsValue

impl From<i32> for JsValue

fn from(n: i32) -> JsValue

impl From<i64> for JsValue

fn from(arg: i64) -> JsValue

impl From<i8> for JsValue

fn from(n: i8) -> JsValue

impl From<isize> for JsValue

fn from(n: isize) -> Self

impl From<u128> for JsValue

fn from(arg: u128) -> JsValue

impl From<u16> for JsValue

fn from(n: u16) -> JsValue

impl From<u32> for JsValue

fn from(n: u32) -> JsValue

impl From<u64> for JsValue

fn from(arg: u64) -> JsValue

impl From<u8> for JsValue

fn from(n: u8) -> JsValue

impl From<usize> for JsValue

fn from(n: usize) -> Self

impl FromWasmAbi for JsValue

type Abi = u32;
unsafe fn from_abi(js: u32) -> JsValue

impl IntoJsGeneric for JsValue

type JsCanon = JsValue;
fn to_js(self) -> JsValue

impl IntoWasmAbi for JsValue

type Abi = u32;
fn into_abi(self) -> u32

impl JsCast for JsValue

fn instanceof(_val: &JsValue) -> bool
fn unchecked_from_js(val: JsValue) -> Self
fn unchecked_from_js_ref(val: &JsValue) -> &Self

impl LongRefFromWasmAbi for JsValue

type Abi = u32;
type Anchor = JsValue;
unsafe fn long_ref_from_abi(js: u32) -> Self::Anchor

impl Mul for JsValue

type Output = <&'static JsValue as Mul>::Output;
fn mul(self, other: JsValue) -> <&'static JsValue as Mul<&'static JsValue>>::Output

impl Mul<&JsValue> for JsValue

type Output = <&'static JsValue as Mul>::Output;
fn mul(self, other: &JsValue) -> <&'static JsValue as Mul<&'static JsValue>>::Output

impl Neg for JsValue

type Output = <&'static JsValue as Neg>::Output;
fn neg(self) -> <&'static JsValue as Neg>::Output

impl Not for JsValue

type Output = <&'static JsValue as Not>::Output;
fn not(self) -> <&'static JsValue as Not>::Output

impl OptionFromWasmAbi for JsValue

fn is_none(js: &u32) -> bool

impl OptionIntoWasmAbi for JsValue

fn none() -> u32

impl PartialEq for JsValue

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

Compares two JsValues for equality, using the === operator in JS.

MDN documentation

impl PartialEq<String> for JsValue

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

impl PartialEq<bool> for JsValue

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

impl PartialEq<f32> for JsValue

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

impl PartialEq<f64> for JsValue

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

impl PartialEq<i128> for JsValue

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

impl PartialEq<i16> for JsValue

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

impl PartialEq<i32> for JsValue

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

impl PartialEq<i64> for JsValue

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

impl PartialEq<i8> for JsValue

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

impl PartialEq<isize> for JsValue

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

impl PartialEq<str> for JsValue

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

impl PartialEq<u128> for JsValue

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

impl PartialEq<u16> for JsValue

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

impl PartialEq<u32> for JsValue

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

impl PartialEq<u64> for JsValue

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

impl PartialEq<u8> for JsValue

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

impl PartialEq<usize> for JsValue

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

impl Promising for JsValue

type Resolution = JsValue;

impl RefFromWasmAbi for JsValue

type Abi = u32;
type Anchor = ManuallyDrop<JsValue>;
unsafe fn ref_from_abi(js: u32) -> Self::Anchor

impl Rem for JsValue

type Output = <&'static JsValue as Rem>::Output;
fn rem(self, other: JsValue) -> <&'static JsValue as Rem<&'static JsValue>>::Output

impl Rem<&JsValue> for JsValue

type Output = <&'static JsValue as Rem>::Output;
fn rem(self, other: &JsValue) -> <&'static JsValue as Rem<&'static JsValue>>::Output

impl Send for JsValue

impl Shl for JsValue

type Output = <&'static JsValue as Shl>::Output;
fn shl(self, other: JsValue) -> <&'static JsValue as Shl<&'static JsValue>>::Output

impl Shl<&JsValue> for JsValue

type Output = <&'static JsValue as Shl>::Output;
fn shl(self, other: &JsValue) -> <&'static JsValue as Shl<&'static JsValue>>::Output

impl Shr for JsValue

type Output = <&'static JsValue as Shr>::Output;
fn shr(self, other: JsValue) -> <&'static JsValue as Shr<&'static JsValue>>::Output

impl Shr<&JsValue> for JsValue

type Output = <&'static JsValue as Shr>::Output;
fn shr(self, other: &JsValue) -> <&'static JsValue as Shr<&'static JsValue>>::Output

impl Sub for JsValue

type Output = <&'static JsValue as Sub>::Output;
fn sub(self, other: JsValue) -> <&'static JsValue as Sub<&'static JsValue>>::Output

impl Sub<&JsValue> for JsValue

type Output = <&'static JsValue as Sub>::Output;
fn sub(self, other: &JsValue) -> <&'static JsValue as Sub<&'static JsValue>>::Output

impl Sync for JsValue

impl UpcastFrom<()> for JsValue

impl UpcastFrom<JsError> for JsValue

impl UpcastFrom<JsValue> for JsValue

impl UpcastFrom<Null> for JsValue

impl UpcastFrom<Undefined> for JsValue

impl UpcastFrom<bool> for JsValue

impl UpcastFrom<char> for JsValue

impl UpcastFrom<f32> for JsValue

impl UpcastFrom<f64> for JsValue

impl UpcastFrom<i128> for JsValue

impl UpcastFrom<i16> for JsValue

impl UpcastFrom<i32> for JsValue

impl UpcastFrom<i64> for JsValue

impl UpcastFrom<i8> for JsValue

impl UpcastFrom<isize> for JsValue

impl UpcastFrom<u128> for JsValue

impl UpcastFrom<u16> for JsValue

impl UpcastFrom<u32> for JsValue

impl UpcastFrom<u64> for JsValue

impl UpcastFrom<u8> for JsValue

impl UpcastFrom<usize> for JsValue

impl<'a> From<&'a String> for JsValue

fn from(s: &'a String) -> JsValue

impl<'a> From<&'a str> for JsValue

fn from(s: &'a str) -> JsValue

impl<'a> PartialEq<&'a String> for JsValue

fn eq(&self, other: &&'a String) -> bool

impl<'a> PartialEq<&'a str> for JsValue

fn eq(&self, other: &&'a str) -> bool

impl<'a, T> From<&'a T> for JsValue where T: JsCast,

fn from(s: &'a T) -> JsValue

impl<T> From<*const T> for JsValue

fn from(s: *const T) -> JsValue

impl<T> From<*mut T> for JsValue

fn from(s: *mut T) -> JsValue

impl<T> From<JsNullable<T>> for JsValue

fn from(obj: JsNullable<T>) -> JsValue

impl<T> From<JsOption<T>> for JsValue

fn from(obj: JsOption<T>) -> JsValue

impl<T> From<NonNull<T>> for JsValue

fn from(s: NonNull<T>) -> JsValue

impl<T> From<Option<T>> for JsValue where JsValue: From<T>,

fn from(s: Option<T>) -> JsValue

impl<T> UpcastFrom<JsNullable<T>> for JsValue

impl<T> UpcastFrom<JsOption<T>> for JsValue

impl<T: VectorIntoWasmAbi> From<Box<[T]>> for JsValue

fn from(vector: Box<[T]>) -> Self

impl<T: VectorIntoWasmAbi> From<Clamped<Box<[T]>>> for JsValue

fn from(vector: Clamped<Box<[T]>>) -> Self

impl<T: VectorIntoWasmAbi> From<Clamped<Vec<T>>> for JsValue

fn from(vector: Clamped<Vec<T>>) -> Self

impl<T: VectorIntoWasmAbi> From<Vec<T>> for JsValue

fn from(vector: Vec<T>) -> Self

Auto Trait Implementations

impl Freeze for JsValue

impl RefUnwindSafe for JsValue

impl Unpin for JsValue

impl UnsafeUnpin for JsValue

impl UnwindSafe for JsValue

Blanket Implementations

impl<S, T> Upcast<T> for JsValue where T: UpcastFrom<S> + ?Sized, S: ?Sized,

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

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

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

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

impl<T> From<T> for JsValue

impl<T> JsGeneric for JsValue where T: ErasableGeneric<Repr = JsValue> + UpcastFrom<T> + Upcast<JsValue> + JsCast + 'static,

impl<T> ReturnWasmAbi for JsValue where T: IntoWasmAbi,

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

impl<T> TryFromJsValue for JsValue where T: JsCast,

impl<T> VectorFromWasmAbi for JsValue where T: ErasableGeneric<Repr = JsValue> + WasmDescribe,

impl<T> VectorIntoWasmAbi for JsValue where T: ErasableGeneric<Repr = JsValue> + WasmDescribe,

impl<T> VectorRefIntoWasmAbi for JsValue where &'a T: for<'a> Into<JsValue>,

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

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

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