1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

refactor(runtime): use Extensions (#10461)

This commit is contained in:
Aaron O'Mullan 2021-05-03 01:22:57 +02:00 committed by GitHub
parent 40961cda58
commit 8377957666
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 366 additions and 301 deletions

View file

@ -8,6 +8,9 @@ use deno_core::error::bad_resource_id;
use deno_core::error::custom_error; use deno_core::error::custom_error;
use deno_core::error::type_error; use deno_core::error::type_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op_async;
use deno_core::op_sync;
use deno_core::Extension;
use deno_core::OpState; use deno_core::OpState;
use deno_core::RcRef; use deno_core::RcRef;
use deno_core::ResourceId; use deno_core::ResourceId;
@ -33,80 +36,60 @@ use deno_core::error::generic_error;
#[cfg(not(unix))] #[cfg(not(unix))]
use deno_core::error::not_supported; use deno_core::error::not_supported;
pub fn init(rt: &mut deno_core::JsRuntime) { pub fn init() -> Extension {
super::reg_sync(rt, "op_open_sync", op_open_sync); Extension::builder()
super::reg_async(rt, "op_open_async", op_open_async); .ops(vec![
("op_open_sync", op_sync(op_open_sync)),
super::reg_sync(rt, "op_seek_sync", op_seek_sync); ("op_open_async", op_async(op_open_async)),
super::reg_async(rt, "op_seek_async", op_seek_async); ("op_seek_sync", op_sync(op_seek_sync)),
("op_seek_async", op_async(op_seek_async)),
super::reg_sync(rt, "op_fdatasync_sync", op_fdatasync_sync); ("op_fdatasync_sync", op_sync(op_fdatasync_sync)),
super::reg_async(rt, "op_fdatasync_async", op_fdatasync_async); ("op_fdatasync_async", op_async(op_fdatasync_async)),
("op_fsync_sync", op_sync(op_fsync_sync)),
super::reg_sync(rt, "op_fsync_sync", op_fsync_sync); ("op_fsync_async", op_async(op_fsync_async)),
super::reg_async(rt, "op_fsync_async", op_fsync_async); ("op_fstat_sync", op_sync(op_fstat_sync)),
("op_fstat_async", op_async(op_fstat_async)),
super::reg_sync(rt, "op_fstat_sync", op_fstat_sync); ("op_umask", op_sync(op_umask)),
super::reg_async(rt, "op_fstat_async", op_fstat_async); ("op_chdir", op_sync(op_chdir)),
("op_mkdir_sync", op_sync(op_mkdir_sync)),
super::reg_sync(rt, "op_umask", op_umask); ("op_mkdir_async", op_async(op_mkdir_async)),
super::reg_sync(rt, "op_chdir", op_chdir); ("op_chmod_sync", op_sync(op_chmod_sync)),
("op_chmod_async", op_async(op_chmod_async)),
super::reg_sync(rt, "op_mkdir_sync", op_mkdir_sync); ("op_chown_sync", op_sync(op_chown_sync)),
super::reg_async(rt, "op_mkdir_async", op_mkdir_async); ("op_chown_async", op_async(op_chown_async)),
("op_remove_sync", op_sync(op_remove_sync)),
super::reg_sync(rt, "op_chmod_sync", op_chmod_sync); ("op_remove_async", op_async(op_remove_async)),
super::reg_async(rt, "op_chmod_async", op_chmod_async); ("op_copy_file_sync", op_sync(op_copy_file_sync)),
("op_copy_file_async", op_async(op_copy_file_async)),
super::reg_sync(rt, "op_chown_sync", op_chown_sync); ("op_stat_sync", op_sync(op_stat_sync)),
super::reg_async(rt, "op_chown_async", op_chown_async); ("op_stat_async", op_async(op_stat_async)),
("op_realpath_sync", op_sync(op_realpath_sync)),
super::reg_sync(rt, "op_remove_sync", op_remove_sync); ("op_realpath_async", op_async(op_realpath_async)),
super::reg_async(rt, "op_remove_async", op_remove_async); ("op_read_dir_sync", op_sync(op_read_dir_sync)),
("op_read_dir_async", op_async(op_read_dir_async)),
super::reg_sync(rt, "op_copy_file_sync", op_copy_file_sync); ("op_rename_sync", op_sync(op_rename_sync)),
super::reg_async(rt, "op_copy_file_async", op_copy_file_async); ("op_rename_async", op_async(op_rename_async)),
("op_link_sync", op_sync(op_link_sync)),
super::reg_sync(rt, "op_stat_sync", op_stat_sync); ("op_link_async", op_async(op_link_async)),
super::reg_async(rt, "op_stat_async", op_stat_async); ("op_symlink_sync", op_sync(op_symlink_sync)),
("op_symlink_async", op_async(op_symlink_async)),
super::reg_sync(rt, "op_realpath_sync", op_realpath_sync); ("op_read_link_sync", op_sync(op_read_link_sync)),
super::reg_async(rt, "op_realpath_async", op_realpath_async); ("op_read_link_async", op_async(op_read_link_async)),
("op_ftruncate_sync", op_sync(op_ftruncate_sync)),
super::reg_sync(rt, "op_read_dir_sync", op_read_dir_sync); ("op_ftruncate_async", op_async(op_ftruncate_async)),
super::reg_async(rt, "op_read_dir_async", op_read_dir_async); ("op_truncate_sync", op_sync(op_truncate_sync)),
("op_truncate_async", op_async(op_truncate_async)),
super::reg_sync(rt, "op_rename_sync", op_rename_sync); ("op_make_temp_dir_sync", op_sync(op_make_temp_dir_sync)),
super::reg_async(rt, "op_rename_async", op_rename_async); ("op_make_temp_dir_async", op_async(op_make_temp_dir_async)),
("op_make_temp_file_sync", op_sync(op_make_temp_file_sync)),
super::reg_sync(rt, "op_link_sync", op_link_sync); ("op_make_temp_file_async", op_async(op_make_temp_file_async)),
super::reg_async(rt, "op_link_async", op_link_async); ("op_cwd", op_sync(op_cwd)),
("op_futime_sync", op_sync(op_futime_sync)),
super::reg_sync(rt, "op_symlink_sync", op_symlink_sync); ("op_futime_async", op_async(op_futime_async)),
super::reg_async(rt, "op_symlink_async", op_symlink_async); ("op_utime_sync", op_sync(op_utime_sync)),
("op_utime_async", op_async(op_utime_async)),
super::reg_sync(rt, "op_read_link_sync", op_read_link_sync); ])
super::reg_async(rt, "op_read_link_async", op_read_link_async); .build()
super::reg_sync(rt, "op_ftruncate_sync", op_ftruncate_sync);
super::reg_async(rt, "op_ftruncate_async", op_ftruncate_async);
super::reg_sync(rt, "op_truncate_sync", op_truncate_sync);
super::reg_async(rt, "op_truncate_async", op_truncate_async);
super::reg_sync(rt, "op_make_temp_dir_sync", op_make_temp_dir_sync);
super::reg_async(rt, "op_make_temp_dir_async", op_make_temp_dir_async);
super::reg_sync(rt, "op_make_temp_file_sync", op_make_temp_file_sync);
super::reg_async(rt, "op_make_temp_file_async", op_make_temp_file_async);
super::reg_sync(rt, "op_cwd", op_cwd);
super::reg_sync(rt, "op_futime_sync", op_futime_sync);
super::reg_async(rt, "op_futime_async", op_futime_async);
super::reg_sync(rt, "op_utime_sync", op_utime_sync);
super::reg_async(rt, "op_utime_async", op_utime_async);
} }
#[derive(Deserialize)] #[derive(Deserialize)]

View file

@ -12,6 +12,9 @@ use deno_core::Resource;
use deno_core::ResourceId; use deno_core::ResourceId;
use deno_core::ZeroCopyBuf; use deno_core::ZeroCopyBuf;
use deno_core::op_async;
use deno_core::op_sync;
use deno_core::Extension;
use notify::event::Event as NotifyEvent; use notify::event::Event as NotifyEvent;
use notify::Error as NotifyError; use notify::Error as NotifyError;
use notify::EventKind; use notify::EventKind;
@ -27,9 +30,13 @@ use std::path::PathBuf;
use std::rc::Rc; use std::rc::Rc;
use tokio::sync::mpsc; use tokio::sync::mpsc;
pub fn init(rt: &mut deno_core::JsRuntime) { pub fn init() -> Extension {
super::reg_sync(rt, "op_fs_events_open", op_fs_events_open); Extension::builder()
super::reg_async(rt, "op_fs_events_poll", op_fs_events_poll); .ops(vec![
("op_fs_events_open", op_sync(op_fs_events_open)),
("op_fs_events_poll", op_async(op_fs_events_poll)),
])
.build()
} }
struct FsEventsResource { struct FsEventsResource {

View file

@ -10,10 +10,13 @@ use deno_core::futures::future::poll_fn;
use deno_core::futures::FutureExt; use deno_core::futures::FutureExt;
use deno_core::futures::Stream; use deno_core::futures::Stream;
use deno_core::futures::StreamExt; use deno_core::futures::StreamExt;
use deno_core::op_async;
use deno_core::op_sync;
use deno_core::AsyncRefCell; use deno_core::AsyncRefCell;
use deno_core::CancelFuture; use deno_core::CancelFuture;
use deno_core::CancelHandle; use deno_core::CancelHandle;
use deno_core::CancelTryFuture; use deno_core::CancelTryFuture;
use deno_core::Extension;
use deno_core::OpState; use deno_core::OpState;
use deno_core::RcRef; use deno_core::RcRef;
use deno_core::Resource; use deno_core::Resource;
@ -43,15 +46,17 @@ use tokio::sync::oneshot;
use tokio_rustls::server::TlsStream; use tokio_rustls::server::TlsStream;
use tokio_util::io::StreamReader; use tokio_util::io::StreamReader;
pub fn init(rt: &mut deno_core::JsRuntime) { pub fn init() -> Extension {
super::reg_sync(rt, "op_http_start", op_http_start); Extension::builder()
.ops(vec![
super::reg_async(rt, "op_http_request_next", op_http_request_next); ("op_http_start", op_sync(op_http_start)),
super::reg_async(rt, "op_http_request_read", op_http_request_read); ("op_http_request_next", op_async(op_http_request_next)),
("op_http_request_read", op_async(op_http_request_read)),
super::reg_async(rt, "op_http_response", op_http_response); ("op_http_response", op_async(op_http_response)),
super::reg_async(rt, "op_http_response_write", op_http_response_write); ("op_http_response_write", op_async(op_http_response_write)),
super::reg_async(rt, "op_http_response_close", op_http_response_close); ("op_http_response_close", op_async(op_http_response_close)),
])
.build()
} }
struct ServiceInner { struct ServiceInner {

View file

@ -4,11 +4,13 @@ use deno_core::error::null_opbuf;
use deno_core::error::resource_unavailable; use deno_core::error::resource_unavailable;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::error::{bad_resource_id, not_supported}; use deno_core::error::{bad_resource_id, not_supported};
use deno_core::op_async;
use deno_core::op_sync;
use deno_core::AsyncMutFuture; use deno_core::AsyncMutFuture;
use deno_core::AsyncRefCell; use deno_core::AsyncRefCell;
use deno_core::CancelHandle; use deno_core::CancelHandle;
use deno_core::CancelTryFuture; use deno_core::CancelTryFuture;
use deno_core::JsRuntime; use deno_core::Extension;
use deno_core::OpState; use deno_core::OpState;
use deno_core::RcRef; use deno_core::RcRef;
use deno_core::Resource; use deno_core::Resource;
@ -95,14 +97,35 @@ lazy_static::lazy_static! {
}; };
} }
pub fn init(rt: &mut JsRuntime) { pub fn init() -> Extension {
super::reg_async(rt, "op_read_async", op_read_async); Extension::builder()
super::reg_async(rt, "op_write_async", op_write_async); .ops(vec![
("op_read_async", op_async(op_read_async)),
("op_write_async", op_async(op_write_async)),
("op_read_sync", op_sync(op_read_sync)),
("op_write_sync", op_sync(op_write_sync)),
("op_shutdown", op_async(op_shutdown)),
])
.build()
}
super::reg_sync(rt, "op_read_sync", op_read_sync); pub fn init_stdio() -> Extension {
super::reg_sync(rt, "op_write_sync", op_write_sync); Extension::builder()
.state(|state| {
super::reg_async(rt, "op_shutdown", op_shutdown); let t = &mut state.resource_table;
let (stdin, stdout, stderr) = get_stdio();
if let Some(stream) = stdin {
t.add(stream);
}
if let Some(stream) = stdout {
t.add(stream);
}
if let Some(stream) = stderr {
t.add(stream);
}
Ok(())
})
.build()
} }
pub fn get_stdio() -> ( pub fn get_stdio() -> (

View file

@ -9,9 +9,12 @@ use deno_core::error::generic_error;
use deno_core::error::null_opbuf; use deno_core::error::null_opbuf;
use deno_core::error::type_error; use deno_core::error::type_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op_async;
use deno_core::op_sync;
use deno_core::AsyncRefCell; use deno_core::AsyncRefCell;
use deno_core::CancelHandle; use deno_core::CancelHandle;
use deno_core::CancelTryFuture; use deno_core::CancelTryFuture;
use deno_core::Extension;
use deno_core::OpState; use deno_core::OpState;
use deno_core::RcRef; use deno_core::RcRef;
use deno_core::Resource; use deno_core::Resource;
@ -42,13 +45,17 @@ use crate::ops::io::UnixStreamResource;
#[cfg(unix)] #[cfg(unix)]
use std::path::Path; use std::path::Path;
pub fn init(rt: &mut deno_core::JsRuntime) { pub fn init() -> Extension {
super::reg_async(rt, "op_accept", op_accept); Extension::builder()
super::reg_async(rt, "op_connect", op_connect); .ops(vec![
super::reg_sync(rt, "op_listen", op_listen); ("op_accept", op_async(op_accept)),
super::reg_async(rt, "op_datagram_receive", op_datagram_receive); ("op_connect", op_async(op_connect)),
super::reg_async(rt, "op_datagram_send", op_datagram_send); ("op_listen", op_sync(op_listen)),
super::reg_async(rt, "op_dns_resolve", op_dns_resolve); ("op_datagram_receive", op_async(op_datagram_receive)),
("op_datagram_send", op_async(op_datagram_send)),
("op_dns_resolve", op_async(op_dns_resolve)),
])
.build()
} }
#[derive(Serialize)] #[derive(Serialize)]

View file

@ -3,7 +3,9 @@
use super::utils::into_string; use super::utils::into_string;
use crate::permissions::Permissions; use crate::permissions::Permissions;
use deno_core::error::{type_error, AnyError}; use deno_core::error::{type_error, AnyError};
use deno_core::op_sync;
use deno_core::url::Url; use deno_core::url::Url;
use deno_core::Extension;
use deno_core::OpState; use deno_core::OpState;
use deno_core::ZeroCopyBuf; use deno_core::ZeroCopyBuf;
use serde::Deserialize; use serde::Deserialize;
@ -11,18 +13,22 @@ use serde::Serialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
pub fn init(rt: &mut deno_core::JsRuntime) { pub fn init() -> Extension {
super::reg_sync(rt, "op_exit", op_exit); Extension::builder()
super::reg_sync(rt, "op_env", op_env); .ops(vec![
super::reg_sync(rt, "op_exec_path", op_exec_path); ("op_exit", op_sync(op_exit)),
super::reg_sync(rt, "op_set_env", op_set_env); ("op_env", op_sync(op_env)),
super::reg_sync(rt, "op_get_env", op_get_env); ("op_exec_path", op_sync(op_exec_path)),
super::reg_sync(rt, "op_delete_env", op_delete_env); ("op_set_env", op_sync(op_set_env)),
super::reg_sync(rt, "op_hostname", op_hostname); ("op_get_env", op_sync(op_get_env)),
super::reg_sync(rt, "op_loadavg", op_loadavg); ("op_delete_env", op_sync(op_delete_env)),
super::reg_sync(rt, "op_os_release", op_os_release); ("op_hostname", op_sync(op_hostname)),
super::reg_sync(rt, "op_system_memory_info", op_system_memory_info); ("op_loadavg", op_sync(op_loadavg)),
super::reg_sync(rt, "op_system_cpu_info", op_system_cpu_info); ("op_os_release", op_sync(op_os_release)),
("op_system_memory_info", op_sync(op_system_memory_info)),
("op_system_cpu_info", op_sync(op_system_cpu_info)),
])
.build()
} }
fn op_exec_path( fn op_exec_path(

View file

@ -4,16 +4,22 @@ use crate::permissions::Permissions;
use deno_core::error::custom_error; use deno_core::error::custom_error;
use deno_core::error::uri_error; use deno_core::error::uri_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op_sync;
use deno_core::url; use deno_core::url;
use deno_core::Extension;
use deno_core::OpState; use deno_core::OpState;
use deno_core::ZeroCopyBuf; use deno_core::ZeroCopyBuf;
use serde::Deserialize; use serde::Deserialize;
use std::path::Path; use std::path::Path;
pub fn init(rt: &mut deno_core::JsRuntime) { pub fn init() -> Extension {
super::reg_sync(rt, "op_query_permission", op_query_permission); Extension::builder()
super::reg_sync(rt, "op_revoke_permission", op_revoke_permission); .ops(vec![
super::reg_sync(rt, "op_request_permission", op_request_permission); ("op_query_permission", op_sync(op_query_permission)),
("op_revoke_permission", op_sync(op_revoke_permission)),
("op_request_permission", op_sync(op_request_permission)),
])
.build()
} }
#[derive(Deserialize)] #[derive(Deserialize)]

View file

@ -3,8 +3,9 @@ use crate::metrics::metrics_op;
use crate::permissions::Permissions; use crate::permissions::Permissions;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::futures::prelude::*; use deno_core::futures::prelude::*;
use deno_core::op_sync;
use deno_core::plugin_api; use deno_core::plugin_api;
use deno_core::JsRuntime; use deno_core::Extension;
use deno_core::Op; use deno_core::Op;
use deno_core::OpAsyncFuture; use deno_core::OpAsyncFuture;
use deno_core::OpFn; use deno_core::OpFn;
@ -22,8 +23,10 @@ use std::rc::Rc;
use std::task::Context; use std::task::Context;
use std::task::Poll; use std::task::Poll;
pub fn init(rt: &mut JsRuntime) { pub fn init() -> Extension {
super::reg_sync(rt, "op_open_plugin", op_open_plugin); Extension::builder()
.ops(vec![("op_open_plugin", op_sync(op_open_plugin))])
.build()
} }
pub fn op_open_plugin( pub fn op_open_plugin(

View file

@ -8,8 +8,11 @@ use crate::permissions::Permissions;
use deno_core::error::bad_resource_id; use deno_core::error::bad_resource_id;
use deno_core::error::type_error; use deno_core::error::type_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op_async;
use deno_core::op_sync;
use deno_core::AsyncMutFuture; use deno_core::AsyncMutFuture;
use deno_core::AsyncRefCell; use deno_core::AsyncRefCell;
use deno_core::Extension;
use deno_core::OpState; use deno_core::OpState;
use deno_core::RcRef; use deno_core::RcRef;
use deno_core::Resource; use deno_core::Resource;
@ -25,10 +28,14 @@ use tokio::process::Command;
#[cfg(unix)] #[cfg(unix)]
use std::os::unix::process::ExitStatusExt; use std::os::unix::process::ExitStatusExt;
pub fn init(rt: &mut deno_core::JsRuntime) { pub fn init() -> Extension {
super::reg_sync(rt, "op_run", op_run); Extension::builder()
super::reg_async(rt, "op_run_status", op_run_status); .ops(vec![
super::reg_sync(rt, "op_kill", op_kill); ("op_run", op_sync(op_run)),
("op_run_status", op_async(op_run_status)),
("op_kill", op_sync(op_kill)),
])
.build()
} }
fn clone_file( fn clone_file(

View file

@ -3,17 +3,20 @@
use crate::permissions::Permissions; use crate::permissions::Permissions;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::error::Context; use deno_core::error::Context;
use deno_core::op_sync;
use deno_core::Extension;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use deno_core::OpState; use deno_core::OpState;
use deno_core::ZeroCopyBuf; use deno_core::ZeroCopyBuf;
pub fn init(rt: &mut deno_core::JsRuntime, main_module: ModuleSpecifier) { pub fn init(main_module: ModuleSpecifier) -> Extension {
{ Extension::builder()
let op_state = rt.op_state(); .ops(vec![("op_main_module", op_sync(op_main_module))])
let mut state = op_state.borrow_mut(); .state(move |state| {
state.put::<ModuleSpecifier>(main_module); state.put::<ModuleSpecifier>(main_module.clone());
} Ok(())
super::reg_sync(rt, "op_main_module", op_main_module); })
.build()
} }
fn op_main_module( fn op_main_module(

View file

@ -1,5 +1,8 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op_async;
use deno_core::op_sync;
use deno_core::Extension;
use deno_core::OpState; use deno_core::OpState;
use deno_core::ZeroCopyBuf; use deno_core::ZeroCopyBuf;
use std::cell::RefCell; use std::cell::RefCell;
@ -24,10 +27,14 @@ use std::borrow::Cow;
#[cfg(unix)] #[cfg(unix)]
use tokio::signal::unix::{signal, Signal, SignalKind}; use tokio::signal::unix::{signal, Signal, SignalKind};
pub fn init(rt: &mut deno_core::JsRuntime) { pub fn init() -> Extension {
super::reg_sync(rt, "op_signal_bind", op_signal_bind); Extension::builder()
super::reg_sync(rt, "op_signal_unbind", op_signal_unbind); .ops(vec![
super::reg_async(rt, "op_signal_poll", op_signal_poll); ("op_signal_bind", op_sync(op_signal_bind)),
("op_signal_unbind", op_sync(op_signal_unbind)),
("op_signal_poll", op_async(op_signal_poll)),
])
.build()
} }
#[cfg(unix)] #[cfg(unix)]

View file

@ -15,9 +15,12 @@ use deno_core::error::custom_error;
use deno_core::error::generic_error; use deno_core::error::generic_error;
use deno_core::error::invalid_hostname; use deno_core::error::invalid_hostname;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op_async;
use deno_core::op_sync;
use deno_core::AsyncRefCell; use deno_core::AsyncRefCell;
use deno_core::CancelHandle; use deno_core::CancelHandle;
use deno_core::CancelTryFuture; use deno_core::CancelTryFuture;
use deno_core::Extension;
use deno_core::OpState; use deno_core::OpState;
use deno_core::RcRef; use deno_core::RcRef;
use deno_core::Resource; use deno_core::Resource;
@ -71,11 +74,15 @@ impl StoresClientSessions for ClientSessionMemoryCache {
} }
} }
pub fn init(rt: &mut deno_core::JsRuntime) { pub fn init() -> Extension {
super::reg_async(rt, "op_start_tls", op_start_tls); Extension::builder()
super::reg_async(rt, "op_connect_tls", op_connect_tls); .ops(vec![
super::reg_sync(rt, "op_listen_tls", op_listen_tls); ("op_start_tls", op_async(op_start_tls)),
super::reg_async(rt, "op_accept_tls", op_accept_tls); ("op_connect_tls", op_async(op_connect_tls)),
("op_listen_tls", op_sync(op_listen_tls)),
("op_accept_tls", op_async(op_accept_tls)),
])
.build()
} }
#[derive(Deserialize)] #[derive(Deserialize)]

View file

@ -5,6 +5,8 @@ use deno_core::error::bad_resource_id;
use deno_core::error::not_supported; use deno_core::error::not_supported;
use deno_core::error::resource_unavailable; use deno_core::error::resource_unavailable;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op_sync;
use deno_core::Extension;
use deno_core::OpState; use deno_core::OpState;
use deno_core::RcRef; use deno_core::RcRef;
use deno_core::ResourceId; use deno_core::ResourceId;
@ -43,10 +45,14 @@ fn get_windows_handle(
Ok(handle) Ok(handle)
} }
pub fn init(rt: &mut deno_core::JsRuntime) { pub fn init() -> Extension {
super::reg_sync(rt, "op_set_raw", op_set_raw); Extension::builder()
super::reg_sync(rt, "op_isatty", op_isatty); .ops(vec![
super::reg_sync(rt, "op_console_size", op_console_size); ("op_set_raw", op_sync(op_set_raw)),
("op_isatty", op_sync(op_isatty)),
("op_console_size", op_sync(op_console_size)),
])
.build()
} }
#[derive(Deserialize)] #[derive(Deserialize)]

View file

@ -4,34 +4,37 @@ use crate::web_worker::WebWorkerHandle;
use crate::web_worker::WorkerEvent; use crate::web_worker::WorkerEvent;
use deno_core::error::null_opbuf; use deno_core::error::null_opbuf;
use deno_core::futures::channel::mpsc; use deno_core::futures::channel::mpsc;
use deno_core::op_sync;
use deno_core::Extension;
pub fn init( pub fn init() -> Extension {
rt: &mut deno_core::JsRuntime, Extension::builder()
sender: mpsc::Sender<WorkerEvent>, .ops(vec![
handle: WebWorkerHandle, (
) { "op_worker_post_message",
// Post message to host as guest worker. op_sync(move |state, _args: (), buf| {
let sender_ = sender.clone(); let buf = buf.ok_or_else(null_opbuf)?;
super::reg_sync( let msg_buf: Box<[u8]> = (*buf).into();
rt, let mut sender = state.borrow::<mpsc::Sender<WorkerEvent>>().clone();
"op_worker_post_message", sender
move |_state, _args: (), buf| { .try_send(WorkerEvent::Message(msg_buf))
let buf = buf.ok_or_else(null_opbuf)?; .expect("Failed to post message to host");
let msg_buf: Box<[u8]> = (*buf).into(); Ok(())
sender_ }),
.clone() ),
.try_send(WorkerEvent::Message(msg_buf)) // Notify host that guest worker closes.
.expect("Failed to post message to host"); (
Ok(()) "op_worker_close",
}, op_sync(move |state, _args: (), _bufs| {
); // Notify parent that we're finished
let mut sender = state.borrow::<mpsc::Sender<WorkerEvent>>().clone();
// Notify host that guest worker closes. sender.close_channel();
super::reg_sync(rt, "op_worker_close", move |_state, _args: (), _bufs| { // Terminate execution of current worker
// Notify parent that we're finished let handle = state.borrow::<WebWorkerHandle>();
sender.clone().close_channel(); handle.terminate();
// Terminate execution of current worker Ok(())
handle.terminate(); }),
Ok(()) ),
}); ])
.build()
} }

View file

@ -21,12 +21,15 @@ use deno_core::error::null_opbuf;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::error::JsError; use deno_core::error::JsError;
use deno_core::futures::channel::mpsc; use deno_core::futures::channel::mpsc;
use deno_core::op_async;
use deno_core::op_sync;
use deno_core::serde::de; use deno_core::serde::de;
use deno_core::serde::de::SeqAccess; use deno_core::serde::de::SeqAccess;
use deno_core::serde::Deserialize; use deno_core::serde::Deserialize;
use deno_core::serde::Deserializer; use deno_core::serde::Deserializer;
use deno_core::serde_json::json; use deno_core::serde_json::json;
use deno_core::serde_json::Value; use deno_core::serde_json::Value;
use deno_core::Extension;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use deno_core::OpState; use deno_core::OpState;
use deno_core::ZeroCopyBuf; use deno_core::ZeroCopyBuf;
@ -69,37 +72,44 @@ pub type WorkersTable = HashMap<u32, WorkerThread>;
pub type WorkerId = u32; pub type WorkerId = u32;
pub fn init( pub fn init(
rt: &mut deno_core::JsRuntime, is_main_worker: bool,
sender: Option<mpsc::Sender<WorkerEvent>>,
create_web_worker_cb: Arc<CreateWebWorkerCb>, create_web_worker_cb: Arc<CreateWebWorkerCb>,
) { ) -> Extension {
{ Extension::builder()
let op_state = rt.op_state(); .state(move |state| {
let mut state = op_state.borrow_mut(); state.put::<WorkersTable>(WorkersTable::default());
state.put::<WorkersTable>(WorkersTable::default()); state.put::<WorkerId>(WorkerId::default());
state.put::<WorkerId>(WorkerId::default());
let create_module_loader = CreateWebWorkerCbHolder(create_web_worker_cb); let create_module_loader =
state.put::<CreateWebWorkerCbHolder>(create_module_loader); CreateWebWorkerCbHolder(create_web_worker_cb.clone());
} state.put::<CreateWebWorkerCbHolder>(create_module_loader);
super::reg_sync(rt, "op_create_worker", op_create_worker);
super::reg_sync(rt, "op_host_terminate_worker", op_host_terminate_worker); Ok(())
super::reg_sync(rt, "op_host_post_message", op_host_post_message); })
super::reg_async(rt, "op_host_get_message", op_host_get_message); .ops(vec![
super::reg_sync( ("op_create_worker", op_sync(op_create_worker)),
rt, (
"op_host_unhandled_error", "op_host_terminate_worker",
move |_state, message: String, _zero_copy| { op_sync(op_host_terminate_worker),
if let Some(mut sender) = sender.clone() { ),
sender ("op_host_post_message", op_sync(op_host_post_message)),
.try_send(WorkerEvent::Error(generic_error(message))) ("op_host_get_message", op_async(op_host_get_message)),
.expect("Failed to propagate error event to parent worker"); (
Ok(true) "op_host_unhandled_error",
} else { op_sync(move |state, message: String, _| {
Err(generic_error("Cannot be called from main worker.")) if is_main_worker {
} return Err(generic_error("Cannot be called from main worker."));
}, }
);
let mut sender = state.borrow::<mpsc::Sender<WorkerEvent>>().clone();
sender
.try_send(WorkerEvent::Error(generic_error(message)))
.expect("Failed to propagate error event to parent worker");
Ok(true)
}),
),
])
.build()
} }
fn merge_boolean_permission( fn merge_boolean_permission(

View file

@ -177,7 +177,17 @@ impl WebWorker {
worker_id: u32, worker_id: u32,
options: &WebWorkerOptions, options: &WebWorkerOptions,
) -> Self { ) -> Self {
let extensions: Vec<Extension> = vec![ // Permissions: many ops depend on this
let unstable = options.unstable;
let perm_ext = Extension::builder()
.state(move |state| {
state.put::<Permissions>(permissions.clone());
state.put(ops::UnstableChecker { unstable });
Ok(())
})
.build();
let mut extensions: Vec<Extension> = vec![
// Web APIs // Web APIs
deno_webidl::init(), deno_webidl::init(),
deno_console::init(), deno_console::init(),
@ -200,8 +210,42 @@ impl WebWorker {
deno_timers::init::<Permissions>(), deno_timers::init::<Permissions>(),
// Metrics // Metrics
metrics::init(), metrics::init(),
// Permissions ext (worker specific state)
perm_ext,
]; ];
// Runtime ops that are always initialized for WebWorkers
let runtime_exts = vec![
ops::web_worker::init(),
ops::runtime::init(main_module.clone()),
ops::worker_host::init(false, options.create_web_worker_cb.clone()),
ops::io::init(),
];
// Extensions providing Deno.* features
let deno_ns_exts = if options.use_deno_namespace {
vec![
ops::fs_events::init(),
ops::fs::init(),
ops::net::init(),
ops::os::init(),
ops::http::init(),
ops::permissions::init(),
ops::plugin::init(),
ops::process::init(),
ops::signal::init(),
ops::tls::init(),
ops::tty::init(),
ops::io::init_stdio(),
]
} else {
vec![]
};
// Append exts
extensions.extend(runtime_exts);
extensions.extend(deno_ns_exts); // May be empty
let mut js_runtime = JsRuntime::new(RuntimeOptions { let mut js_runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(options.module_loader.clone()), module_loader: Some(options.module_loader.clone()),
startup_snapshot: Some(js::deno_isolate_init()), startup_snapshot: Some(js::deno_isolate_init()),
@ -236,60 +280,20 @@ impl WebWorker {
terminate_rx, terminate_rx,
handle, handle,
use_deno_namespace: options.use_deno_namespace, use_deno_namespace: options.use_deno_namespace,
main_module: main_module.clone(), main_module,
}; };
// Setup worker-dependant OpState and return worker
{ {
let handle = worker.thread_safe_handle(); let handle = worker.thread_safe_handle();
let sender = worker.internal_channels.sender.clone(); let sender = worker.internal_channels.sender.clone();
let js_runtime = &mut worker.js_runtime; let js_runtime = &mut worker.js_runtime;
// All ops registered in this function depend on these let op_state = js_runtime.op_state();
{ let mut op_state = op_state.borrow_mut();
let op_state = js_runtime.op_state();
let mut op_state = op_state.borrow_mut();
op_state.put::<Permissions>(permissions);
op_state.put(ops::UnstableChecker {
unstable: options.unstable,
});
}
ops::web_worker::init(js_runtime, sender.clone(), handle); // Required by runtime::ops::worker_host/web_worker
ops::runtime::init(js_runtime, main_module); op_state.put(handle);
ops::worker_host::init( op_state.put(sender);
js_runtime,
Some(sender),
options.create_web_worker_cb.clone(),
);
ops::io::init(js_runtime);
if options.use_deno_namespace {
ops::fs_events::init(js_runtime);
ops::fs::init(js_runtime);
ops::net::init(js_runtime);
ops::os::init(js_runtime);
ops::http::init(js_runtime);
ops::permissions::init(js_runtime);
ops::plugin::init(js_runtime);
ops::process::init(js_runtime);
ops::signal::init(js_runtime);
ops::tls::init(js_runtime);
ops::tty::init(js_runtime);
let op_state = js_runtime.op_state();
let mut op_state = op_state.borrow_mut();
let t = &mut op_state.resource_table;
let (stdin, stdout, stderr) = ops::io::get_stdio();
if let Some(stream) = stdin {
t.add(stream);
}
if let Some(stream) = stdout {
t.add(stream);
}
if let Some(stream) = stderr {
t.add(stream);
}
}
js_runtime.sync_ops_cache();
worker worker
} }

View file

@ -78,6 +78,16 @@ impl MainWorker {
permissions: Permissions, permissions: Permissions,
options: &WorkerOptions, options: &WorkerOptions,
) -> Self { ) -> Self {
// Permissions: many ops depend on this
let unstable = options.unstable;
let perm_ext = Extension::builder()
.state(move |state| {
state.put::<Permissions>(permissions.clone());
state.put(ops::UnstableChecker { unstable });
Ok(())
})
.build();
// Internal modules // Internal modules
let extensions: Vec<Extension> = vec![ let extensions: Vec<Extension> = vec![
// Web APIs // Web APIs
@ -99,6 +109,24 @@ impl MainWorker {
deno_timers::init::<Permissions>(), deno_timers::init::<Permissions>(),
// Metrics // Metrics
metrics::init(), metrics::init(),
// Runtime ops
ops::runtime::init(main_module),
ops::worker_host::init(true, options.create_web_worker_cb.clone()),
ops::fs_events::init(),
ops::fs::init(),
ops::http::init(),
ops::io::init(),
ops::io::init_stdio(),
ops::net::init(),
ops::os::init(),
ops::permissions::init(),
ops::plugin::init(),
ops::process::init(),
ops::signal::init(),
ops::tls::init(),
ops::tty::init(),
// Permissions ext (worker specific state)
perm_ext,
]; ];
let mut js_runtime = JsRuntime::new(RuntimeOptions { let mut js_runtime = JsRuntime::new(RuntimeOptions {
@ -122,61 +150,11 @@ impl MainWorker {
let should_break_on_first_statement = let should_break_on_first_statement =
inspector.is_some() && options.should_break_on_first_statement; inspector.is_some() && options.should_break_on_first_statement;
let mut worker = Self { Self {
inspector, inspector,
js_runtime, js_runtime,
should_break_on_first_statement, should_break_on_first_statement,
};
let js_runtime = &mut worker.js_runtime;
{
// All ops registered in this function depend on these
{
let op_state = js_runtime.op_state();
let mut op_state = op_state.borrow_mut();
op_state.put::<Permissions>(permissions);
op_state.put(ops::UnstableChecker {
unstable: options.unstable,
});
}
ops::runtime::init(js_runtime, main_module);
ops::worker_host::init(
js_runtime,
None,
options.create_web_worker_cb.clone(),
);
ops::fs_events::init(js_runtime);
ops::fs::init(js_runtime);
ops::http::init(js_runtime);
ops::io::init(js_runtime);
ops::net::init(js_runtime);
ops::os::init(js_runtime);
ops::permissions::init(js_runtime);
ops::plugin::init(js_runtime);
ops::process::init(js_runtime);
ops::signal::init(js_runtime);
ops::tls::init(js_runtime);
ops::tty::init(js_runtime);
} }
{
let op_state = js_runtime.op_state();
let mut op_state = op_state.borrow_mut();
let t = &mut op_state.resource_table;
let (stdin, stdout, stderr) = ops::io::get_stdio();
if let Some(stream) = stdin {
t.add(stream);
}
if let Some(stream) = stdout {
t.add(stream);
}
if let Some(stream) = stderr {
t.add(stream);
}
}
js_runtime.sync_ops_cache();
worker
} }
pub fn bootstrap(&mut self, options: &WorkerOptions) { pub fn bootstrap(&mut self, options: &WorkerOptions) {