mirror of
https://github.com/denoland/deno.git
synced 2024-12-25 16:49:18 -05:00
Add --current-thread flag (#2702)
This commit is contained in:
parent
421cbd39b4
commit
e7cee29c84
5 changed files with 53 additions and 1 deletions
26
cli/flags.rs
26
cli/flags.rs
|
@ -43,6 +43,8 @@ pub struct DenoFlags {
|
||||||
pub v8_flags: Option<Vec<String>>,
|
pub v8_flags: Option<Vec<String>>,
|
||||||
pub xeval_replvar: Option<String>,
|
pub xeval_replvar: Option<String>,
|
||||||
pub xeval_delim: Option<String>,
|
pub xeval_delim: Option<String>,
|
||||||
|
// Use tokio::runtime::current_thread
|
||||||
|
pub current_thread: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
static ENV_VARIABLES_HELP: &str = "ENVIRONMENT VARIABLES:
|
static ENV_VARIABLES_HELP: &str = "ENVIRONMENT VARIABLES:
|
||||||
|
@ -158,6 +160,12 @@ To get help on the another subcommands (run in this case):
|
||||||
.help("Load compiler configuration file")
|
.help("Load compiler configuration file")
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.global(true),
|
.global(true),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("current-thread")
|
||||||
|
.long("current-thread")
|
||||||
|
.global(true)
|
||||||
|
.help("Use tokio::runtime::current_thread"),
|
||||||
).arg(
|
).arg(
|
||||||
Arg::with_name("importmap")
|
Arg::with_name("importmap")
|
||||||
.long("importmap")
|
.long("importmap")
|
||||||
|
@ -443,6 +451,9 @@ pub fn parse_flags(
|
||||||
) -> DenoFlags {
|
) -> DenoFlags {
|
||||||
let mut flags = maybe_flags.unwrap_or_default();
|
let mut flags = maybe_flags.unwrap_or_default();
|
||||||
|
|
||||||
|
if matches.is_present("current-thread") {
|
||||||
|
flags.current_thread = true;
|
||||||
|
}
|
||||||
if matches.is_present("log-level") {
|
if matches.is_present("log-level") {
|
||||||
flags.log_level = match matches.value_of("log-level").unwrap() {
|
flags.log_level = match matches.value_of("log-level").unwrap() {
|
||||||
"debug" => Some(Level::Debug),
|
"debug" => Some(Level::Debug),
|
||||||
|
@ -1620,4 +1631,19 @@ mod tests {
|
||||||
assert_eq!(subcommand, DenoSubcommand::Run);
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
||||||
assert_eq!(argv, svec!["deno", "script.ts"])
|
assert_eq!(argv, svec!["deno", "script.ts"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_flags_from_vec_35() {
|
||||||
|
let (flags, subcommand, argv) =
|
||||||
|
flags_from_vec(svec!["deno", "--current-thread", "script.ts"]);
|
||||||
|
assert_eq!(
|
||||||
|
flags,
|
||||||
|
DenoFlags {
|
||||||
|
current_thread: true,
|
||||||
|
..DenoFlags::default()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
||||||
|
assert_eq!(argv, svec!["deno", "script.ts"])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -329,6 +329,7 @@ fn run_repl(flags: DenoFlags, argv: Vec<String>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_script(flags: DenoFlags, argv: Vec<String>) {
|
fn run_script(flags: DenoFlags, argv: Vec<String>) {
|
||||||
|
let use_current_thread = flags.current_thread;
|
||||||
let (mut worker, state) = create_worker_and_state(flags, argv);
|
let (mut worker, state) = create_worker_and_state(flags, argv);
|
||||||
|
|
||||||
let main_module = state.main_module().unwrap();
|
let main_module = state.main_module().unwrap();
|
||||||
|
@ -348,8 +349,13 @@ fn run_script(flags: DenoFlags, argv: Vec<String>) {
|
||||||
})
|
})
|
||||||
}).map_err(print_err_and_exit)
|
}).map_err(print_err_and_exit)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if use_current_thread {
|
||||||
|
tokio_util::run_on_current_thread(main_future);
|
||||||
|
} else {
|
||||||
tokio_util::run(main_future);
|
tokio_util::run(main_future);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
|
|
|
@ -26,6 +26,13 @@ where
|
||||||
rt.block_on_all(future).unwrap();
|
rt.block_on_all(future).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn run_on_current_thread<F>(future: F)
|
||||||
|
where
|
||||||
|
F: Future<Item = (), Error = ()> + Send + 'static,
|
||||||
|
{
|
||||||
|
tokio::runtime::current_thread::run(future);
|
||||||
|
}
|
||||||
|
|
||||||
/// THIS IS A HACK AND SHOULD BE AVOIDED.
|
/// THIS IS A HACK AND SHOULD BE AVOIDED.
|
||||||
///
|
///
|
||||||
/// This creates a new tokio runtime, with many new threads, to execute the
|
/// This creates a new tokio runtime, with many new threads, to execute the
|
||||||
|
|
2
tests/037_current_thread.test
Normal file
2
tests/037_current_thread.test
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
args: run --current-thread --reload tests/034_onload/main.ts
|
||||||
|
output: tests/034_onload.out
|
|
@ -32,6 +32,16 @@ def deno_tcp(deno_exe):
|
||||||
return run(deno_cmd, addr)
|
return run(deno_cmd, addr)
|
||||||
|
|
||||||
|
|
||||||
|
def deno_tcp_current_thread(deno_exe):
|
||||||
|
addr = get_addr()
|
||||||
|
deno_cmd = [
|
||||||
|
deno_exe, "run", "--current-thread", "--allow-net",
|
||||||
|
"tools/deno_tcp.ts", addr
|
||||||
|
]
|
||||||
|
print "http_benchmark testing DENO."
|
||||||
|
return run(deno_cmd, addr)
|
||||||
|
|
||||||
|
|
||||||
def deno_http(deno_exe):
|
def deno_http(deno_exe):
|
||||||
addr = get_addr()
|
addr = get_addr()
|
||||||
deno_cmd = [
|
deno_cmd = [
|
||||||
|
@ -146,6 +156,7 @@ def http_benchmark(build_dir):
|
||||||
return {
|
return {
|
||||||
# "deno_tcp" was once called "deno"
|
# "deno_tcp" was once called "deno"
|
||||||
"deno_tcp": deno_tcp(deno_exe),
|
"deno_tcp": deno_tcp(deno_exe),
|
||||||
|
"deno_tcp_current_thread": deno_tcp_current_thread(deno_exe),
|
||||||
# "deno_http" was once called "deno_net_http"
|
# "deno_http" was once called "deno_net_http"
|
||||||
"deno_http": deno_http(deno_exe),
|
"deno_http": deno_http(deno_exe),
|
||||||
"deno_proxy": deno_http_proxy(deno_exe, hyper_hello_exe),
|
"deno_proxy": deno_http_proxy(deno_exe, hyper_hello_exe),
|
||||||
|
|
Loading…
Reference in a new issue