Struct deno_runtime::worker::MainWorker
source · pub struct MainWorker {
pub js_runtime: JsRuntime,
/* private fields */
}
Expand description
This worker is created and used by almost all subcommands in Deno executable.
It provides ops available in the Deno
namespace.
All WebWorker
s created during program execution
are descendants of this worker.
Fields§
§js_runtime: JsRuntime
Implementations§
source§impl MainWorker
impl MainWorker
sourcepub fn bootstrap_from_options(
main_module: ModuleSpecifier,
permissions: PermissionsContainer,
options: WorkerOptions
) -> Self
pub fn bootstrap_from_options( main_module: ModuleSpecifier, permissions: PermissionsContainer, options: WorkerOptions ) -> Self
Examples found in repository?
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
async fn main() -> Result<(), AnyError> {
let js_path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("examples/extension_with_ops/main.js");
let main_module = ModuleSpecifier::from_file_path(js_path).unwrap();
let mut worker = MainWorker::bootstrap_from_options(
main_module.clone(),
PermissionsContainer::allow_all(),
WorkerOptions {
module_loader: Rc::new(FsModuleLoader),
extensions: vec![hello_runtime::init_ops()],
..Default::default()
},
);
worker.execute_main_module(&main_module).await?;
worker.run_event_loop(false).await?;
Ok(())
}
More examples
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
async fn main() -> Result<(), AnyError> {
let js_path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("examples/extension_with_esm/main.js");
let main_module = ModuleSpecifier::from_file_path(js_path).unwrap();
let mut worker = MainWorker::bootstrap_from_options(
main_module.clone(),
PermissionsContainer::allow_all(),
WorkerOptions {
module_loader: Rc::new(FsModuleLoader),
extensions: vec![hello_runtime::init_ops_and_esm()],
..Default::default()
},
);
worker.execute_main_module(&main_module).await?;
worker.run_event_loop(false).await?;
Ok(())
}
pub fn from_options( main_module: ModuleSpecifier, permissions: PermissionsContainer, options: WorkerOptions ) -> Self
pub fn bootstrap(&mut self, options: &BootstrapOptions)
sourcepub fn execute_script(
&mut self,
script_name: &'static str,
source_code: ModuleCode
) -> Result<Global<Value>, AnyError>
pub fn execute_script( &mut self, script_name: &'static str, source_code: ModuleCode ) -> Result<Global<Value>, AnyError>
sourcepub async fn preload_main_module(
&mut self,
module_specifier: &ModuleSpecifier
) -> Result<ModuleId, AnyError>
pub async fn preload_main_module( &mut self, module_specifier: &ModuleSpecifier ) -> Result<ModuleId, AnyError>
Loads and instantiates specified JavaScript module as “main” module.
sourcepub async fn preload_side_module(
&mut self,
module_specifier: &ModuleSpecifier
) -> Result<ModuleId, AnyError>
pub async fn preload_side_module( &mut self, module_specifier: &ModuleSpecifier ) -> Result<ModuleId, AnyError>
Loads and instantiates specified JavaScript module as “side” module.
sourcepub async fn evaluate_module(&mut self, id: ModuleId) -> Result<(), AnyError>
pub async fn evaluate_module(&mut self, id: ModuleId) -> Result<(), AnyError>
Executes specified JavaScript module.
sourcepub async fn execute_side_module(
&mut self,
module_specifier: &ModuleSpecifier
) -> Result<(), AnyError>
pub async fn execute_side_module( &mut self, module_specifier: &ModuleSpecifier ) -> Result<(), AnyError>
Loads, instantiates and executes specified JavaScript module.
sourcepub async fn execute_main_module(
&mut self,
module_specifier: &ModuleSpecifier
) -> Result<(), AnyError>
pub async fn execute_main_module( &mut self, module_specifier: &ModuleSpecifier ) -> Result<(), AnyError>
Loads, instantiates and executes specified JavaScript module.
This module will have “import.meta.main” equal to true.
Examples found in repository?
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
async fn main() -> Result<(), AnyError> {
let js_path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("examples/extension_with_ops/main.js");
let main_module = ModuleSpecifier::from_file_path(js_path).unwrap();
let mut worker = MainWorker::bootstrap_from_options(
main_module.clone(),
PermissionsContainer::allow_all(),
WorkerOptions {
module_loader: Rc::new(FsModuleLoader),
extensions: vec![hello_runtime::init_ops()],
..Default::default()
},
);
worker.execute_main_module(&main_module).await?;
worker.run_event_loop(false).await?;
Ok(())
}
More examples
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
async fn main() -> Result<(), AnyError> {
let js_path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("examples/extension_with_esm/main.js");
let main_module = ModuleSpecifier::from_file_path(js_path).unwrap();
let mut worker = MainWorker::bootstrap_from_options(
main_module.clone(),
PermissionsContainer::allow_all(),
WorkerOptions {
module_loader: Rc::new(FsModuleLoader),
extensions: vec![hello_runtime::init_ops_and_esm()],
..Default::default()
},
);
worker.execute_main_module(&main_module).await?;
worker.run_event_loop(false).await?;
Ok(())
}
sourcepub async fn create_inspector_session(&mut self) -> LocalInspectorSession
pub async fn create_inspector_session(&mut self) -> LocalInspectorSession
Create new inspector session. This function panics if Worker was not configured to create inspector.
pub fn poll_event_loop( &mut self, cx: &mut Context<'_>, wait_for_inspector: bool ) -> Poll<Result<(), AnyError>>
sourcepub async fn run_event_loop(
&mut self,
wait_for_inspector: bool
) -> Result<(), AnyError>
pub async fn run_event_loop( &mut self, wait_for_inspector: bool ) -> Result<(), AnyError>
Examples found in repository?
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
async fn main() -> Result<(), AnyError> {
let js_path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("examples/extension_with_ops/main.js");
let main_module = ModuleSpecifier::from_file_path(js_path).unwrap();
let mut worker = MainWorker::bootstrap_from_options(
main_module.clone(),
PermissionsContainer::allow_all(),
WorkerOptions {
module_loader: Rc::new(FsModuleLoader),
extensions: vec![hello_runtime::init_ops()],
..Default::default()
},
);
worker.execute_main_module(&main_module).await?;
worker.run_event_loop(false).await?;
Ok(())
}
More examples
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
async fn main() -> Result<(), AnyError> {
let js_path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("examples/extension_with_esm/main.js");
let main_module = ModuleSpecifier::from_file_path(js_path).unwrap();
let mut worker = MainWorker::bootstrap_from_options(
main_module.clone(),
PermissionsContainer::allow_all(),
WorkerOptions {
module_loader: Rc::new(FsModuleLoader),
extensions: vec![hello_runtime::init_ops_and_esm()],
..Default::default()
},
);
worker.execute_main_module(&main_module).await?;
worker.run_event_loop(false).await?;
Ok(())
}
sourcepub async fn with_event_loop<'a, T>(
&mut self,
fut: Pin<Box<dyn Future<Output = T> + 'a>>
) -> T
pub async fn with_event_loop<'a, T>( &mut self, fut: Pin<Box<dyn Future<Output = T> + 'a>> ) -> T
A utility function that runs provided future concurrently with the event loop.
Useful when using a local inspector session.
sourcepub fn exit_code(&self) -> i32
pub fn exit_code(&self) -> i32
Return exit code set by the executed code (either in main worker or one of child web workers).
sourcepub fn dispatch_load_event(
&mut self,
script_name: &'static str
) -> Result<(), AnyError>
pub fn dispatch_load_event( &mut self, script_name: &'static str ) -> Result<(), AnyError>
Dispatches “load” event to the JavaScript runtime.
Does not poll event loop, and thus not await any of the “load” event handlers.