pub struct Module<'js, T = Declared> { /* private fields */ }Expand description
A JavaScript module.
Implementations§
Source§impl<'js> Module<'js, Declared>
impl<'js> Module<'js, Declared>
Sourcepub fn declare<N, S>(
ctx: Ctx<'js>,
name: N,
source: S,
) -> Result<Module<'js, Declared>>
pub fn declare<N, S>( ctx: Ctx<'js>, name: N, source: S, ) -> Result<Module<'js, Declared>>
Declare a module but don’t evaluate it.
Sourcepub fn declare_def<D, N>(
ctx: Ctx<'js>,
name: N,
) -> Result<Module<'js, Declared>>
pub fn declare_def<D, N>( ctx: Ctx<'js>, name: N, ) -> Result<Module<'js, Declared>>
Declare a rust native module but don’t evaluate it.
Sourcepub fn evaluate<N, S>(ctx: Ctx<'js>, name: N, source: S) -> Result<Promise<'js>>
pub fn evaluate<N, S>(ctx: Ctx<'js>, name: N, source: S) -> Result<Promise<'js>>
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.
Sourcepub fn evaluate_def<D, N>(
ctx: Ctx<'js>,
name: N,
) -> Result<(Module<'js, Evaluated>, Promise<'js>)>
pub fn evaluate_def<D, N>( ctx: Ctx<'js>, name: N, ) -> Result<(Module<'js, Evaluated>, Promise<'js>)>
Declares a module in the runtime and evaluates it.
Sourcepub unsafe fn load(ctx: Ctx<'js>, bytes: &[u8]) -> Result<Module<'js, Declared>>
pub unsafe fn load(ctx: Ctx<'js>, bytes: &[u8]) -> Result<Module<'js, Declared>>
Load a module from quickjs bytecode.
§Safety
User must ensure that bytes handed to this function contain valid bytecode.
Sourcepub unsafe fn from_load_fn<N>(
ctx: Ctx<'js>,
name: N,
load_fn: ModuleLoadFn,
) -> Result<Module<'js, Declared>>
pub unsafe fn from_load_fn<N>( ctx: Ctx<'js>, name: N, load_fn: ModuleLoadFn, ) -> Result<Module<'js, Declared>>
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.
Sourcepub fn eval(self) -> Result<(Module<'js, Evaluated>, Promise<'js>)>
pub fn eval(self) -> Result<(Module<'js, Evaluated>, Promise<'js>)>
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§impl<'js, Evaluated> Module<'js, Evaluated>
impl<'js, Evaluated> Module<'js, Evaluated>
Sourcepub fn write(&self, options: WriteOptions) -> Result<Vec<u8>>
pub fn write(&self, options: WriteOptions) -> Result<Vec<u8>>
Write object bytecode for the module.
§Examples
use rquickjs::{Context, Module, Result, Runtime, WriteOptions, WriteOptionsEndianness};
fn main() -> Result<()> {
let rt = Runtime::new()?;
let ctx = Context::full(&rt)?;
let bytecode = ctx.with(|ctx| {
let src = "console.log('hello world')";
let module = Module::declare(ctx.clone(), "foo.js", src)?;
module.write(WriteOptions {
endianness: WriteOptionsEndianness::Little,
..Default::default()
})
})?;
println!("bytecode: {bytecode:?}");
Ok(())
}Sourcepub fn namespace(&self) -> Result<Object<'js>>
pub fn namespace(&self) -> Result<Object<'js>>
Returns the module namespace, an object containing all the module exported values.
Sourcepub fn into_declared(self) -> Module<'js, Declared>
pub fn into_declared(self) -> Module<'js, Declared>
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: JsLifetime<'js>> JsLifetime<'js> for Module<'js, T>
impl<'js, T: JsLifetime<'js>> JsLifetime<'js> for Module<'js, T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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