Struct rquickjs::Module

source ·
pub struct Module<'js, T = Declared> { /* private fields */ }
Expand description

A JavaScript module.

Implementations§

source§

impl<'js, T> Module<'js, T>

source

pub fn name<N>(&self) -> Result<N, Error>
where N: FromAtom<'js>,

Returns the name of the module

source§

impl<'js> Module<'js>

source

pub fn declare<N, S>( ctx: Ctx<'js>, name: N, source: S ) -> Result<Module<'js>, Error>
where N: Into<Vec<u8>>, S: Into<Vec<u8>>,

Declare a module but don’t evaluate it.

source

pub fn declare_def<D, N>(ctx: Ctx<'js>, name: N) -> Result<Module<'js>, Error>
where N: Into<Vec<u8>>, D: ModuleDef,

Declare a rust native module but don’t evaluate it.

source

pub fn evaluate<N, S>( ctx: Ctx<'js>, name: N, source: S ) -> Result<Promise<'js>, Error>
where N: Into<Vec<u8>>, S: Into<Vec<u8>>,

Evaluate the source of a module.

This function returns a promise which resolved when the modules was fully compiled and returns undefined.

Since QuickJS doesn’t give us a way to retrieve the module if we immediately evaluate a modules source this function doesn’t return a module object. If the module is required, you should first declare it with Module::declare and then call Module::eval on the returned module.

source

pub fn evaluate_def<D, N>( ctx: Ctx<'js>, name: N ) -> Result<(Module<'js, Evaluated>, Promise<'js>), Error>
where N: Into<Vec<u8>>, D: ModuleDef,

Declares a module in the runtime and evaluates it.

source

pub unsafe fn load(ctx: Ctx<'js>, bytes: &[u8]) -> Result<Module<'js>, Error>

Load a module from quickjs bytecode.

§Safety

User must ensure that bytes handed to this function contain valid bytecode.

source

pub unsafe fn from_load_fn<N>( ctx: Ctx<'js>, name: N, load_fn: unsafe extern "C" fn(_: *mut JSContext, _: *const i8) -> *mut JSModuleDef ) -> Result<Module<'js>, Error>
where N: Into<Vec<u8>>,

Load a module from a raw module loading function.

§Safety

The soundness of this function depends completely on if load_fn is implemented correctly. THe load_fn function must return a pointer to a valid module loaded with the given context.

source

pub fn eval(self) -> Result<(Module<'js, Evaluated>, Promise<'js>), Error>

Evaluate the module.

Returns the module as being evaluated and a promise which resolves when the module has finished evaluating. The return value of the promise is the JavaScript value undefined.

source

pub unsafe extern "C" fn init_raw<D>( ctx: *mut JSContext, name: *const i8 ) -> *mut JSModuleDef
where D: ModuleDef,

A function for loading a Rust module from C.

§Safety

This function should only be called when the module is loaded as part of a dynamically loaded library.

source

pub fn import<S>(ctx: &Ctx<'js>, specifier: S) -> Result<Promise<'js>, Error>
where S: Into<Vec<u8>>,

Import and evaluate a module

This will work similar to an import(specifier) statement in JavaScript returning a promise with the result of the imported module.

source§

impl<'js, Evaluated> Module<'js, Evaluated>

source

pub fn write_le(&self) -> Result<Vec<u8>, Error>

Write object bytecode for the module in little endian format.

source

pub fn write_be(&self) -> Result<Vec<u8>, Error>

Write object bytecode for the module in big endian format.

source

pub fn write(&self, swap_endianess: bool) -> Result<Vec<u8>, Error>

Write object bytecode for the module.

swap_endianess swaps the endianness of the bytecode, if true, from native to the other kind. Use if the bytecode is meant for a target with a different endianness than the current.

source

pub fn meta(&self) -> Result<Object<'js>, Error>

Return the import.meta object of a module

source

pub fn namespace(&self) -> Result<Object<'js>, Error>

Returns the module namespace, an object containing all the module exported values.

source

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

Return exported value by name

source

pub fn into_declared(self) -> Module<'js>

Change the module back to being only declared.

This is always safe to do since calling eval again on an already evaluated module is safe.

Trait Implementations§

source§

impl<'js, T> Clone for Module<'js, T>
where T: Clone,

source§

fn clone(&self) -> Module<'js, T>

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, T> Debug for Module<'js, T>
where T: Debug,

source§

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

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

impl<'js, T> Outlive<'js> for Module<'js, T>

§

type Target<'to> = Module<'to, T>

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

impl<'js, T> Trace<'js> for Module<'js, T>

source§

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

Auto Trait Implementations§

§

impl<'js, T> Freeze for Module<'js, T>

§

impl<'js, T> RefUnwindSafe for Module<'js, T>
where T: RefUnwindSafe,

§

impl<'js, T = Declared> !Send for Module<'js, T>

§

impl<'js, T = Declared> !Sync for Module<'js, T>

§

impl<'js, T> Unpin for Module<'js, T>
where T: Unpin,

§

impl<'js, T = Declared> !UnwindSafe for Module<'js, T>

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