JSClass

Struct JSClass 

Source
pub struct JSClass { /* private fields */ }
Expand description

A JavaScript class.

The best way to create a class is by using JSClass::builder.

Implementations§

Source§

impl JSClass

Source

pub fn builder<N>( ctx: &JSContext, name: N, ) -> Result<JSClassBuilder<'_>, JSException>
where N: Into<Vec<u8>>,

Create a new builder to build a Self.

/// Declare a class constructor.
#[constructor_callback]
fn foo(
    ctx: &JSContext,
    constructor: &JSObject,
    _arguments: &[JSValue],
) -> Result<JSValue, JSException> {
    /// Declare a function.
    #[function_callback]
    fn bar(
        ctx: &JSContext,
        _function: Option<&JSObject>,
        _this_object: Option<&JSObject>,
        _arguments: &[JSValue],
    ) -> Result<JSValue, JSException> {
        Ok(JSValue::new_number(&ctx, 42.))
    }

    constructor.set_property("bar", JSValue::new_function(ctx, "bar", Some(bar)))?;

    Ok(constructor.into())
}

let ctx = JSContext::default();
let class = JSClass::builder(&ctx, "Foo")
    .unwrap()
    .constructor(Some(foo))
    .build()
    .unwrap();

// We have a class! Now, let's populate it inside the global object, just for fun.

let object = class.new_object();
let global_object = ctx.global_object().unwrap();
global_object.set_property("Foo", object.into()).unwrap();

let result = evaluate_script(&ctx, "const foo = new Foo(); foo.bar()", None, "test.js", 1).unwrap();

assert_eq!(result.as_number().unwrap(), 42.);
Source

pub fn new_object(&self) -> JSObject

Transform the Self into a JSObject.

Note that it doesn’t instantiate the class. To do a proper instantiation, one has to call JSObject::call_as_constructor.

let ctx = JSContext::default();
let class = JSClass::builder(&ctx, "Foo").unwrap().build().unwrap();
let object = class.new_object();

assert!(object.is_object_of_class(&class));

Trait Implementations§

Source§

impl Drop for JSClass

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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