JSObject

Struct JSObject 

Source
pub struct JSObject<'a> { /* private fields */ }
Expand description

A JavaScript object.

Implementations§

Source§

impl<'a> JSObject<'a>

Source

pub fn new(ctx: &'a JSContext) -> Self

Create a new default javascript object.

Source

pub fn new_function_with_callback<F>(ctx: &'a JSContext, callback: F) -> Self
where for<'c> F: FnMut(&'c JSContext, &JSObject<'c>, &[JSValue<'c>]) -> Result<JSValue<'c>, JSValue<'c>>,

Create a JavaScript function with a given callback as its implementation.

Results in a JSObject that is a function. The object’s prototype will be the default function prototype.

This can be used to execute Rust code from JavaScript.

Source

pub fn new_function( ctx: &'a JSContext, name: Option<&str>, param_names: &[&str], body: &str, source_url: Option<&str>, starting_line_number: i32, ) -> Result<Self, JSValue<'a>>

Creates a function with a given script as its body.

Use this method when you want to execute a script repeatedly, to avoid the cost of re-parsing the script before each execution.

Can return Err if there was an exception when creating the function or if the script or parameters contain syntax errors.

Source

pub fn new_array( ctx: &'a JSContext, items: &[JSValue<'_>], ) -> Result<Self, JSValue<'a>>

Creates a JavaScript Array object.

Source

pub fn is_function(&self) -> bool

Tests whether an object can be called as a function.

Source

pub fn is_constructor(&self) -> bool

Tests whether an object can be called as a constructor.

Source

pub fn call_as_function( &self, this: Option<&JSObject<'_>>, args: &[JSValue<'_>], ) -> Result<JSValue<'_>, JSValue<'_>>

Calls an object as a function.

Return the JSValue that results from calling object as a function, or Err if an exception is thrown or object is not a function.

Source

pub fn call_as_constructor( &self, args: &[JSValue<'_>], ) -> Result<JSObject<'_>, JSValue<'_>>

Calls an object as a constructor.

Return the JSObject that results from calling object as a constructor, or Err if an exception is thrown or object is not a constructor.

Source§

impl JSObject<'_>

Source

pub fn get_property(&self, name: &str) -> Result<JSValue<'_>, JSValue<'_>>

Gets a property from an object by name.

Returns the property’s value if object has the property, otherwise the undefined value, or Err if an exception is thrown.

Source

pub fn get_property_at_index( &self, index: u32, ) -> Result<JSValue<'_>, JSValue<'_>>

Gets a property from an object by numeric index.

Returns the property’s value if object has the property, otherwise the undefined value, or Err if an exception is thrown.

Calling JSObject::get_property_at_index is equivalent to calling JSObject::get_property with a string containing index, but JSObject::get_property_at_index provides optimized access to numeric properties.

Source

pub fn set_property( &self, name: &str, value: &JSValue<'_>, attributes: JSPropertyAttributes, ) -> Result<(), JSValue<'_>>

Sets a property on an object by name.

Returns Err if an exception is thrown.

Source

pub fn set_property_at_index( &self, index: u32, value: &JSValue<'_>, ) -> Result<(), JSValue<'_>>

Sets a property on an object by numeric index.

Returns Err if an exception is thrown.

Calling JSObject::set_property_at_index is equivalent to calling JSObject::set_property with a string containing index, but JSObject::set_property_at_index provides optimized access to numeric properties.

Source

pub fn get_property_names(&self) -> JSPropertyNameArray<'_>

Gets the names of an object’s enumerable properties.

Source

pub fn has_property(&self, name: &str) -> bool

Tests whether an object has a property.

Source

pub fn delete_property(&self, name: &str) -> Result<bool, JSValue<'_>>

Deletes a property from an object by name.

Returns true if the property was deleted, false if the property was not present, or it had JSPropertyAttributes::dont_delete set, or Err if an exception is thrown.

Source

pub fn get_property_for_key( &self, key: &JSValue<'_>, ) -> Result<JSValue<'_>, JSValue<'_>>

Gets a property from an object using a JSValue as the property key.

Returns the property’s value if object has the property, otherwise the undefined value, or Err if an exception is thrown.

This function is the same as performing object[propertyKey](propertyKey) from JavaScript.

Source

pub fn set_property_for_key( &self, key: &JSValue<'_>, value: &JSValue<'_>, attributes: JSPropertyAttributes, ) -> Result<(), JSValue<'_>>

Sets a property on an object using a JSValue as the property key.

Returns Err if an exception is thrown.

This function is the same as performing object[propertyKey](propertyKey) = value from JavaScript.

Source

pub fn has_property_for_key( &self, key: &JSValue<'_>, ) -> Result<bool, JSValue<'_>>

Tests whether an object has a given property using a JSValue as the property key.

This function is the same as performing propertyKey in object from JavaScript.

Source

pub fn delete_property_for_key( &self, key: &JSValue<'_>, ) -> Result<bool, JSValue<'_>>

Deletes a property from an object using a JSValue as the property key.

Returns true if the property was deleted, false if the property was not present, or it had JSPropertyAttributes::dont_delete set, or Err if an exception is thrown.

This function is the same as performing delete object[propertyKey](propertyKey) from JavaScript.

Methods from Deref<Target = JSValue<'a>>§

Source

pub fn get_type(&self) -> JSType

Returns a JavaScript value’s type.

Source

pub fn is_undefined(&self) -> bool

Returns true if the value is undefined.

Source

pub fn is_null(&self) -> bool

Returns true if the value is null.

Source

pub fn is_date(&self) -> bool

Returns true if the value is a JavaScript date object.

Source

pub fn is_array(&self) -> bool

Returns true if the value is a JavaScript array.

Source

pub fn is_symbol(&self) -> bool

Returns true if the value’s type is the symbol type

Source

pub fn is_object(&self) -> bool

Returns true if the value is an object.

Source

pub fn is_string(&self) -> bool

Returns true if the value is a string.

Source

pub fn is_number(&self) -> bool

Returns true if the value is a number.

Source

pub fn is_boolean(&self) -> bool

Returns true if the value is a boolean.

Source

pub fn is_typed_array(&self) -> bool

Returns true if the value is a Typed Array.

Source

pub fn as_object(&self) -> Result<JSObject<'a>, JSValue<'a>>

Converts a JavaScript value to object.

Returns an Err if an exception is thrown.

Source

pub fn as_string(&self) -> Result<JSString, JSValue<'a>>

Converts a JavaScript value to string.

Returns an Err if an exception is thrown.

Source

pub fn as_number(&self) -> Result<f64, JSValue<'a>>

Converts a JavaScript value to number.

Returns an Err if an exception is thrown.

Source

pub fn as_boolean(&self) -> bool

Converts a JavaScript value to boolean.

Source

pub fn as_typed_array(&self) -> Result<JSTypedArray<'a>, JSValue<'a>>

Converts a JavaScript value to a typed array.

Returns an Err if the value is not a typed array, or if an exception is thrown.

Source

pub fn to_json_string(&self) -> Result<JSString, JSValue<'a>>

Converts a JavaScript value to JSON serialized representation of a JS value.

Returns an Err if an exception is thrown.

Trait Implementations§

Source§

impl<'a> AsJSValue<'a> for JSObject<'a>

Source§

fn into_value(self) -> JSValue<'a>

Source§

fn as_value(&self) -> &JSValue<'a>

Source§

impl<'a> AsRef<JSValue<'a>> for JSObject<'a>

Source§

fn as_ref(&self) -> &JSValue<'a>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a> Clone for JSObject<'a>

Source§

fn clone(&self) -> JSObject<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for JSObject<'a>

Source§

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

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

impl<'a> Deref for JSObject<'a>

Source§

type Target = JSValue<'a>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<'a> Freeze for JSObject<'a>

§

impl<'a> RefUnwindSafe for JSObject<'a>

§

impl<'a> !Send for JSObject<'a>

§

impl<'a> !Sync for JSObject<'a>

§

impl<'a> Unpin for JSObject<'a>

§

impl<'a> UnwindSafe for JSObject<'a>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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 = 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.