pub struct JSObject<'a> { /* private fields */ }
Expand description
A JavaScript object.
Implementations§
Source§impl<'a> JSObject<'a>
impl<'a> JSObject<'a>
Sourcepub fn new_function_with_callback<F>(ctx: &'a JSContext, callback: F) -> Self
pub fn new_function_with_callback<F>(ctx: &'a JSContext, callback: F) -> Self
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.
Sourcepub 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>>
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.
Sourcepub fn new_array(
ctx: &'a JSContext,
items: &[JSValue<'_>],
) -> Result<Self, JSValue<'a>>
pub fn new_array( ctx: &'a JSContext, items: &[JSValue<'_>], ) -> Result<Self, JSValue<'a>>
Creates a JavaScript Array object.
Sourcepub fn is_function(&self) -> bool
pub fn is_function(&self) -> bool
Tests whether an object can be called as a function.
Sourcepub fn is_constructor(&self) -> bool
pub fn is_constructor(&self) -> bool
Tests whether an object can be called as a constructor.
Source§impl JSObject<'_>
impl JSObject<'_>
Sourcepub fn get_property(&self, name: &str) -> Result<JSValue<'_>, JSValue<'_>>
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.
Sourcepub fn get_property_at_index(
&self,
index: u32,
) -> Result<JSValue<'_>, JSValue<'_>>
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.
Sourcepub fn set_property(
&self,
name: &str,
value: &JSValue<'_>,
attributes: JSPropertyAttributes,
) -> Result<(), JSValue<'_>>
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.
Sourcepub fn set_property_at_index(
&self,
index: u32,
value: &JSValue<'_>,
) -> Result<(), JSValue<'_>>
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.
Sourcepub fn get_property_names(&self) -> JSPropertyNameArray<'_>
pub fn get_property_names(&self) -> JSPropertyNameArray<'_>
Gets the names of an object’s enumerable properties.
Sourcepub fn has_property(&self, name: &str) -> bool
pub fn has_property(&self, name: &str) -> bool
Tests whether an object has a property.
Sourcepub fn delete_property(&self, name: &str) -> Result<bool, JSValue<'_>>
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.
Sourcepub fn set_property_for_key(
&self,
key: &JSValue<'_>,
value: &JSValue<'_>,
attributes: JSPropertyAttributes,
) -> Result<(), JSValue<'_>>
pub fn set_property_for_key( &self, key: &JSValue<'_>, value: &JSValue<'_>, attributes: JSPropertyAttributes, ) -> Result<(), JSValue<'_>>
Sourcepub fn has_property_for_key(
&self,
key: &JSValue<'_>,
) -> Result<bool, JSValue<'_>>
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.
Sourcepub fn delete_property_for_key(
&self,
key: &JSValue<'_>,
) -> Result<bool, JSValue<'_>>
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>>§
Sourcepub fn is_undefined(&self) -> bool
pub fn is_undefined(&self) -> bool
Returns true
if the value is undefined
.
Sourcepub fn is_boolean(&self) -> bool
pub fn is_boolean(&self) -> bool
Returns true
if the value is a boolean.
Sourcepub fn is_typed_array(&self) -> bool
pub fn is_typed_array(&self) -> bool
Returns true
if the value is a Typed Array.
Sourcepub fn as_object(&self) -> Result<JSObject<'a>, JSValue<'a>>
pub fn as_object(&self) -> Result<JSObject<'a>, JSValue<'a>>
Converts a JavaScript value to object.
Returns an Err
if an exception is thrown.
Sourcepub fn as_string(&self) -> Result<JSString, JSValue<'a>>
pub fn as_string(&self) -> Result<JSString, JSValue<'a>>
Converts a JavaScript value to string.
Returns an Err
if an exception is thrown.
Sourcepub fn as_number(&self) -> Result<f64, JSValue<'a>>
pub fn as_number(&self) -> Result<f64, JSValue<'a>>
Converts a JavaScript value to number.
Returns an Err
if an exception is thrown.
Sourcepub fn as_boolean(&self) -> bool
pub fn as_boolean(&self) -> bool
Converts a JavaScript value to boolean.
Sourcepub fn as_typed_array(&self) -> Result<JSTypedArray<'a>, JSValue<'a>>
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.