Struct rquickjs::Function

source ·
pub struct Function<'js>(/* private fields */);
Expand description

A JavaScript function.

Implementations§

source§

impl<'js> Function<'js>

source

pub fn new<P, F>(ctx: Ctx<'js>, f: F) -> Result<Function<'js>, Error>
where F: IntoJsFunc<'js, P> + 'js,

Create a new function from a Rust function which implements IntoJsFunc.

source

pub fn call<A, R>(&self, args: A) -> Result<R, Error>
where A: IntoArgs<'js>, R: FromJs<'js>,

Call the function with given arguments.

source

pub fn call_arg<R>(&self, args: Args<'js>) -> Result<R, Error>
where R: FromJs<'js>,

Call the function with given arguments in the form of an Args object.

source

pub fn defer<A>(&self, args: A) -> Result<(), Error>
where A: IntoArgs<'js>,

Defer call the function with given arguments.

Calling a function with defer is equivalent to calling a JavaScript function with setTimeout(func,0).

source

pub fn defer_arg(&self, args: Args<'js>) -> Result<(), Error>

Defer a function call with given arguments.

source

pub fn set_name<S>(&self, name: S) -> Result<(), Error>
where S: AsRef<str>,

Set the name property of this function

source

pub fn with_name<S>(self, name: S) -> Result<Function<'js>, Error>
where S: AsRef<str>,

Set the name property of this function and then return self.

source

pub fn set_length(&self, len: usize) -> Result<(), Error>

Sets the length property of the function.

source

pub fn with_length(self, len: usize) -> Result<Function<'js>, Error>

Sets the length property of the function and return self.

source

pub fn prototype(ctx: Ctx<'js>) -> Object<'js>

Returns the prototype which all JavaScript function by default have as its prototype, i.e. Function.prototype.

source

pub fn is_constructor(&self) -> bool

Returns whether this function is an constructor.

source

pub fn set_constructor(&self, is_constructor: bool)

Set whether this function is a constructor or not.

source

pub fn with_constructor(self, is_constructor: bool) -> Function<'js>

Set whether this function is a constructor or not then return self.

source§

impl<'js> Function<'js>

source

pub fn as_value(&self) -> &Value<'js>

Reference to value

source

pub fn into_value(self) -> Value<'js>

Convert into value

source

pub fn into_inner(self) -> Object<'js>

Returns the underlying super type.

source

pub fn as_inner(&self) -> &Object<'js>

Returns a reference to the underlying super type.

source

pub fn ctx(&self) -> &Ctx<'js>

Returns the Ctx object associated with this value

source

pub fn from_value(value: Value<'js>) -> Result<Function<'js>, Error>

Convert from value

Methods from Deref<Target = Object<'js>>§

source

pub fn prop<K, V, P>(&self, key: K, prop: V) -> Result<(), Error>
where K: IntoAtom<'js>, V: AsProperty<'js, P>,

Available on crate feature properties only.

Define a property of an object

// Define readonly property without value
obj.prop("no_val", ()).unwrap();
// Define readonly property with value
obj.prop("ro_str", "Some const text").unwrap();
// Define readonly property with value and make it to be writable
obj.prop("ro_str2", Property::from("Some const text").writable()).unwrap();
// Define readonly property using getter and make it to be enumerable
obj.prop("ro_str_get", Accessor::from(|| "Some readable text").enumerable()).unwrap();
// Define readonly property using getter and setter
obj.prop("ro_str_get_set",
    Accessor::from(|| "Some text")
        .set(|new_val: String| { /* do something */ })
).unwrap();
source

pub fn get<K, V>(&self, k: K) -> Result<V, Error>
where K: IntoAtom<'js>, V: FromJs<'js>,

Get a new value

source

pub fn contains_key<K>(&self, k: K) -> Result<bool, Error>
where K: IntoAtom<'js>,

check whether the object contains a certain key.

source

pub fn set<K, V>(&self, key: K, value: V) -> Result<(), Error>
where K: IntoAtom<'js>, V: IntoJs<'js>,

Set a member of an object to a certain value

source

pub fn remove<K>(&self, key: K) -> Result<(), Error>
where K: IntoAtom<'js>,

Remove a member of an object

source

pub fn is_empty(&self) -> bool

Check the object for empty

source

pub fn len(&self) -> usize

Get the number of properties

source

pub fn keys<K>(&self) -> ObjectKeysIter<'js, K>
where K: FromAtom<'js>,

Get own string enumerable property names of an object

source

pub fn own_keys<K>(&self, filter: Filter) -> ObjectKeysIter<'js, K>
where K: FromAtom<'js>,

Get own property names of an object

source

pub fn props<K, V>(&self) -> ObjectIter<'js, K, V>
where K: FromAtom<'js>, V: FromJs<'js>,

Get own string enumerable properties of an object

source

pub fn own_props<K, V>(&self, filter: Filter) -> ObjectIter<'js, K, V>
where K: FromAtom<'js>, V: FromJs<'js>,

Get own properties of an object

source

pub fn values<K>(&self) -> ObjectValuesIter<'js, K>
where K: FromAtom<'js>,

Get own string enumerable property values of an object

source

pub fn own_values<K>(&self, filter: Filter) -> ObjectValuesIter<'js, K>
where K: FromAtom<'js>,

Get own property values of an object

source

pub fn get_prototype(&self) -> Option<Object<'js>>

Get an object prototype

Objects can have no prototype, in this case this function will return null.

source

pub fn set_prototype(&self, proto: Option<&Object<'js>>) -> Result<(), Error>

Set an object prototype

If called with None the function will set the prototype of the object to null.

This function will error if setting the prototype causes a cycle in the prototype chain.

source

pub fn is_instance_of(&self, class: impl AsRef<Value<'js>>) -> bool

Check instance of object

source

pub fn is_array_buffer(&self) -> bool

Returns whether the object is an instance of ArrayBuffer.

source

pub unsafe fn ref_array_buffer(&self) -> &ArrayBuffer<'_>

Interpret as ArrayBuffer

§Safety

You should be sure that the object actually is the required type.

source

pub fn as_array_buffer(&self) -> Option<&ArrayBuffer<'_>>

Turn the object into an array buffer if the object is an instance of ArrayBuffer.

source

pub fn is_typed_array<T>(&self) -> bool
where T: TypedArrayItem,

source

pub unsafe fn ref_typed_array<'a, T>(&'a self) -> &'a TypedArray<'a, T>
where T: TypedArrayItem,

Interpret as TypedArray

§Safety

You should be sure that the object actually is the required type.

source

pub fn as_typed_array<T>(&self) -> Option<&TypedArray<'_, T>>
where T: TypedArrayItem,

source

pub fn as_value(&self) -> &Value<'js>

Reference to value

source

pub fn as_inner(&self) -> &Value<'js>

Returns a reference to the underlying super type.

source

pub fn ctx(&self) -> &Ctx<'js>

Returns the Ctx object associated with this value

source

pub fn instance_of<C>(&self) -> bool
where C: JsClass<'js>,

Returns if the object is of a certain Rust class.

source

pub fn into_class<C>(&self) -> Result<Class<'js, C>, &Object<'js>>
where C: JsClass<'js>,

Turn the object into the class if it is an instance of that class.

source

pub fn as_class<C>(&self) -> Option<&Class<'js, C>>
where C: JsClass<'js>,

Turn the object into the class if it is an instance of that class.

Methods from Deref<Target = Value<'js>>§

source

pub fn ctx(&self) -> &Ctx<'js>

Returns the Ctx object associated with this value.

source

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

Try get bool from value

source

pub fn as_int(&self) -> Option<i32>

Try get int from value

source

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

Try get float from value

source

pub fn as_number(&self) -> Option<f64>

Try get any number from value

source

pub fn is_null(&self) -> bool

Returns if the value is the JavaScript null value.

source

pub fn is_undefined(&self) -> bool

Returns if the value is the JavaScript undefined value.

source

pub fn is_bool(&self) -> bool

Check if the value is a bool

source

pub fn is_int(&self) -> bool

Check if the value is an int

source

pub fn is_float(&self) -> bool

Check if the value is a float

source

pub fn is_number(&self) -> bool

Check if the value is an any number

source

pub fn is_string(&self) -> bool

Check if the value is a string

source

pub fn is_symbol(&self) -> bool

Check if the value is a symbol

source

pub fn is_object(&self) -> bool

Check if the value is an object

source

pub fn is_module(&self) -> bool

Check if the value is a module

source

pub fn is_array(&self) -> bool

Check if the value is an array

source

pub fn is_function(&self) -> bool

Check if the value is a function

source

pub fn is_constructor(&self) -> bool

Check if the value is a constructor function

source

pub fn is_promise(&self) -> bool

Check if the value is a promise.

source

pub fn is_exception(&self) -> bool

Check if the value is an exception

source

pub fn is_error(&self) -> bool

Check if the value is an error

source

pub fn as_value(&self) -> &Value<'js>

Reference as value

source

pub fn get<T>(&self) -> Result<T, Error>
where T: FromJs<'js>,

Convert from value to specified type

source

pub fn as_raw(&self) -> JSValue

Returns the raw C library JavaScript value.

source

pub fn type_of(&self) -> Type

Get the type of value

source

pub fn type_name(&self) -> &'static str

Get the name of type

source

pub unsafe fn ref_string(&self) -> &String<'js>

Interpret as String

§Safety

You should be sure that the value already is of required type before to do it.

source

pub fn as_string(&self) -> Option<&String<'js>>

Try reinterpret as String

source

pub unsafe fn ref_symbol(&self) -> &Symbol<'js>

Interpret as Symbol

§Safety

You should be sure that the value already is of required type before to do it.

source

pub fn as_symbol(&self) -> Option<&Symbol<'js>>

Try reinterpret as Symbol

source

pub unsafe fn ref_object(&self) -> &Object<'js>

Interpret as Object

§Safety

You should be sure that the value already is of required type before to do it.

source

pub fn as_object(&self) -> Option<&Object<'js>>

Try reinterpret as Object

source

pub unsafe fn ref_function(&self) -> &Function<'js>

Interpret as Function

§Safety

You should be sure that the value already is of required type before to do it.

source

pub fn as_function(&self) -> Option<&Function<'js>>

Try reinterpret as Function

source

pub unsafe fn ref_constructor(&self) -> &Constructor<'js>

Interpret as Constructor

§Safety

You should be sure that the value already is of required type before to do it.

source

pub fn as_constructor(&self) -> Option<&Constructor<'js>>

Try reinterpret as Constructor

source

pub unsafe fn ref_promise(&self) -> &Promise<'js>

Interpret as Promise

§Safety

You should be sure that the value already is of required type before to do it.

source

pub fn as_promise(&self) -> Option<&Promise<'js>>

Try reinterpret as Promise

source

pub unsafe fn ref_array(&self) -> &Array<'js>

Interpret as Array

§Safety

You should be sure that the value already is of required type before to do it.

source

pub fn as_array(&self) -> Option<&Array<'js>>

Try reinterpret as Array

source

pub unsafe fn ref_exception(&self) -> &Exception<'js>

Interpret as Exception

§Safety

You should be sure that the value already is of required type before to do it.

source

pub fn as_exception(&self) -> Option<&Exception<'js>>

Try reinterpret as Exception

source

pub unsafe fn ref_big_int(&self) -> &BigInt<'js>

Interpret as BigInt

§Safety

You should be sure that the value already is of required type before to do it.

source

pub fn as_big_int(&self) -> Option<&BigInt<'js>>

Try reinterpret as BigInt

Trait Implementations§

source§

impl<'js> AsRef<Function<'js>> for Constructor<'js>

source§

fn as_ref(&self) -> &Function<'js>

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

impl<'js> AsRef<Object<'js>> for Function<'js>

source§

fn as_ref(&self) -> &Object<'js>

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

impl<'js> AsRef<Value<'js>> for Function<'js>

source§

fn as_ref(&self) -> &Value<'js>

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

impl<'js> Clone for Function<'js>

source§

fn clone(&self) -> Function<'js>

Returns a copy 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<'js> Debug for Function<'js>

source§

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

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

impl<'js> Deref for Function<'js>

§

type Target = Object<'js>

The resulting type after dereferencing.
source§

fn deref(&self) -> &<Function<'js> as Deref>::Target

Dereferences the value.
source§

impl<'js> From<Function<'js>> for Value<'js>

source§

fn from(value: Function<'js>) -> Value<'js>

Converts to this type from the input type.
source§

impl<'js> FromJs<'js> for Function<'js>

source§

fn from_js(_: &Ctx<'js>, value: Value<'js>) -> Result<Function<'js>, Error>

source§

impl<'js> Hash for Function<'js>

source§

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

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<'js> IntoAtom<'js> for Function<'js>

source§

fn into_atom(self, ctx: &Ctx<'js>) -> Result<Atom<'js>, Error>

source§

impl<'js> IntoJs<'js> for Function<'js>

source§

fn into_js(self, _ctx: &Ctx<'js>) -> Result<Value<'js>, Error>

source§

impl<'js> Outlive<'js> for Function<'js>

§

type Target<'to> = Function<'to>

The target which has the same type as a Self but with another lifetime 't
source§

impl<'js> PartialEq for Function<'js>

source§

fn eq(&self, other: &Function<'js>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'js> Trace<'js> for Function<'js>

source§

fn trace<'a>(&self, tracer: Tracer<'a, 'js>)

source§

impl<'js> Eq for Function<'js>

source§

impl<'js> StructuralPartialEq for Function<'js>

Auto Trait Implementations§

§

impl<'js> Freeze for Function<'js>

§

impl<'js> RefUnwindSafe for Function<'js>

§

impl<'js> !Send for Function<'js>

§

impl<'js> !Sync for Function<'js>

§

impl<'js> Unpin for Function<'js>

§

impl<'js> !UnwindSafe for Function<'js>

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<'js, T> AsProperty<'js, T> for T
where T: IntoJs<'js>,

source§

fn config( self, ctx: &Ctx<'js> ) -> Result<(i32, Value<'js>, Value<'js>, Value<'js>), Error>

Property configuration 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<'js, T> FromParam<'js> for T
where T: FromJs<'js>,

source§

fn param_requirement() -> ParamRequirement

The parameters requirements this value requires.
source§

fn from_param<'a>(params: &mut ParamsAccessor<'a, 'js>) -> Result<T, Error>

Convert from a parameter value.
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<'js, T> IntoArg<'js> for T
where T: IntoJs<'js>,

source§

fn num_args(&self) -> usize

The number of arguments this value produces.
source§

fn into_arg(self, args: &mut Args<'js>) -> Result<(), Error>

Convert the value into an argument.
source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

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

§

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>,

§

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>,

§

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.