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>
impl<'js> Module<'js>
sourcepub fn declare<N, S>(ctx: Ctx<'js>, name: N, source: S) -> Result<(), Error>
pub fn declare<N, S>(ctx: Ctx<'js>, name: N, source: S) -> Result<(), Error>
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.
sourcepub fn evaluate<N, S>(
ctx: Ctx<'js>,
name: N,
source: S
) -> Result<Module<'js>, Error>
pub fn evaluate<N, S>( ctx: Ctx<'js>, name: N, source: S ) -> Result<Module<'js>, Error>
Creates a new module from JS source, and evaluates it.
It is unsafe to hold onto unevaluated modules across this call.
sourcepub fn declare_def<D, N>(ctx: Ctx<'js>, name: N) -> Result<(), Error>
pub fn declare_def<D, N>(ctx: Ctx<'js>, name: N) -> Result<(), Error>
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.
sourcepub fn evaluate_def<D, N>(ctx: Ctx<'js>, name: N) -> Result<Module<'js>, Error>
pub fn evaluate_def<D, N>(ctx: Ctx<'js>, name: N) -> Result<Module<'js>, Error>
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.
sourcepub fn name<N>(&self) -> Result<N, Error>where
N: FromAtom<'js>,
pub fn name<N>(&self) -> Result<N, Error>where
N: FromAtom<'js>,
Returns the name of the module
sourcepub fn meta<T>(&self) -> Result<T, Error>where
T: FromJs<'js>,
pub fn meta<T>(&self) -> Result<T, Error>where
T: FromJs<'js>,
Return the import.meta
object of a module
sourcepub fn write_object_le(&self) -> Result<Vec<u8>, Error>
pub fn write_object_le(&self) -> Result<Vec<u8>, Error>
Write object bytecode for the module in little endian format.
sourcepub fn write_object_be(&self) -> Result<Vec<u8>, Error>
pub fn write_object_be(&self) -> Result<Vec<u8>, Error>
Write object bytecode for the module in big endian format.
sourcepub unsafe extern "C" fn init_raw<D>(
ctx: *mut JSContext,
name: *const i8
) -> *mut JSModuleDefwhere
D: ModuleDef,
pub unsafe extern "C" fn init_raw<D>(
ctx: *mut JSContext,
name: *const i8
) -> *mut JSModuleDefwhere
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.
sourcepub fn write_object(&self, swap_endianess: bool) -> Result<Vec<u8>, Error>
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.
sourcepub fn declare_read_object(ctx: Ctx<'js>, bytes: &[u8]) -> Result<(), Error>
pub fn declare_read_object(ctx: Ctx<'js>, bytes: &[u8]) -> Result<(), Error>
Read object bytecode and declare it as a module.
sourcepub fn instantiate_read_object(
ctx: Ctx<'js>,
bytes: &[u8]
) -> Result<Module<'js>, Error>
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.
sourcepub unsafe fn unsafe_declare_read_object(
ctx: Ctx<'js>,
bytes: &[u8]
) -> Result<Module<'js>, Error>
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.
sourcepub unsafe fn unsafe_declare<N, S>(
ctx: Ctx<'js>,
name: N,
source: S
) -> Result<Module<'js>, Error>
pub unsafe fn unsafe_declare<N, S>( ctx: Ctx<'js>, name: N, source: S ) -> Result<Module<'js>, Error>
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.
sourcepub unsafe fn unsafe_declare_def<D, N>(
ctx: Ctx<'js>,
name: N
) -> Result<Module<'js>, Error>
pub unsafe fn unsafe_declare_def<D, N>( ctx: Ctx<'js>, name: N ) -> Result<Module<'js>, Error>
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.
sourcepub unsafe fn eval(&self) -> Result<(), Error>
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.