Struct rquickjs::Module

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

A JavaScript module.

§Safety

In QuickJS JavaScript modules are loaded in two steps. First a module is declared, the runtime is made aware of its existence, given a name, and its exports are defined.

Then after declaration the module is evaluated, the module’s exports are actually given a value.

QuickJS handles module lifetime differently then other JavaScript values. Modules live, once evaluated, for the entire lifetime of the runtime. However before they are evaluated they can be removed at any point.

Specifically all unevaluated modules are removed if any module generates an error during either declaration or evaluation. As a result while it is possible to acquire a unevaluated module, it is unsafe to hold onto such a module and any function which returns such an unevaluated modules is marked as unsafe.

Implementations§

source§

impl<'js> Module<'js>

source

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

Declares a new JS module in the context.

This function doesn’t return a module since holding on to unevaluated modules is unsafe. If you need to hold onto unsafe modules use the Module::unsafe_declare functions.

It is unsafe to hold onto unevaluated modules across this call.

source

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

Creates a new module from JS source, and evaluates it.

It is unsafe to hold onto unevaluated modules across this call.

source

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

Declare a module in the runtime.

This function doesn’t return a module since holding on to unevaluated modules is unsafe. If you need to hold onto unsafe modules use the Module::unsafe_declare_def functions.

It is unsound to hold onto an unevaluated module across any call to this function which returns an error.

source

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

Declares a module in the runtime and evaluates it.

It is unsound to hold onto an unevaluated module across any call to this function which returns an error.

source

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

Returns the name of the module

source

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

Return the import.meta object of a module

source

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

Write object bytecode for the module in little endian format.

source

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

Write object bytecode for the module in big endian format.

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 write_object(&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 declare_read_object(ctx: Ctx<'js>, bytes: &[u8]) -> Result<(), Error>

Read object bytecode and declare it as a module.

source

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

Read object bytecode and declare it as a module and then evaluate it.

source

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

Read object bytecode and declare it as a module.

§Safety

QuickJS frees all unevaluated modules if any error happens while declaring or evaluating a module. If any call to either Module::new or Module::eval fails it is undefined behavior to use any unevaluated modules.

It is unsound to hold onto an unevaluated module across any call to this function which returns an error.

source

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

Creates a new module from JS source but doesn’t evaluate the module.

§Safety

QuickJS frees all unevaluated modules if any error happens while declaring or evaluating a module. If any call to either Module::new or Module::eval fails it is undefined behavior to use any unevaluated modules.

It is unsound to hold onto an unevaluated module across any call to this function which returns an error.

source

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

Creates a new module from JS source but doesn’t evaluate the module.

§Safety

It is unsafe to use an unevaluated for anything other then evaluating it with Module::eval.

QuickJS frees all unevaluated modules if any error happens while declaring or evaluating a module. If any call to either Module::new or Module::eval fails it is undefined behavior to use any unevaluated modules.

It is unsound to hold onto an unevaluated module across any call to this function which returns an error.

source

pub unsafe fn eval(&self) -> Result<(), Error>

Evaluates an unevaluated module.

§Safety

This function should only be called on unevaluated modules.

QuickJS frees all unevaluated modules if any error happens while declaring or evaluating a module. If any call to either Module::new or Module::eval fails it is undefined behavior to use any unevaluated modules.

Prefer the use of either ModuleBuilder or Module::new.

It is unsound to hold onto an unevaluated module across any call to this function which returns an error.

source

pub fn import<V, S>(ctx: &Ctx<'js>, specifier: S) -> Result<V, Error>
where V: FromJs<'js>, S: AsRef<[u8]>,

Import and evaluate a module

This will work similar to an await import(specifier) statement in JavaScript but will return the import and not a promise

source§

impl<'js> Module<'js>

source

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

Return exported value by name

source

pub fn names<N>(self) -> ExportNamesIter<'js, N>
where N: FromAtom<'js>,

Available on crate feature exports only.

Returns a iterator over the exported names of the module export.

source

pub fn entries<N, T>(self) -> ExportEntriesIter<'js, N, T>
where N: FromAtom<'js>, T: FromJs<'js>,

Available on crate feature exports only.

Returns a iterator over the items the module export.

Trait Implementations§

source§

impl<'js> Clone for Module<'js>

source§

fn clone(&self) -> Module<'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> Trace<'js> for Module<'js>

source§

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

Auto Trait Implementations§

§

impl<'js> RefUnwindSafe for Module<'js>

§

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

§

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

§

impl<'js> Unpin for Module<'js>

§

impl<'js> !UnwindSafe for Module<'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<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> 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.