fix: op_set_exit_code (#13034)

Fixes "op_set_exit_code" by sharing a single "Arc" between
all workers (via "op state") instead of having a "global" value stored in
"deno_runtime" crate. As a consequence setting an exit code is always
scoped to a tree of workers, instead of being overridable if there are
multiple worker tree (like in "deno test --jobs" subcommand).

Refactored "cli/main.rs" functions to return "Result<i32, AnyError>" instead
of "Result<(), AnyError>" so they can return exit code.
This commit is contained in:
Bartek Iwańczuk 2021-12-11 15:56:45 +01:00 committed by GitHub
parent 13d7d57227
commit 0dec9b4381
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 131 additions and 70 deletions

View file

@ -25,6 +25,8 @@ use deno_web::BlobStore;
use log::debug;
use std::pin::Pin;
use std::rc::Rc;
use std::sync::atomic::AtomicI32;
use std::sync::atomic::Ordering::Relaxed;
use std::sync::Arc;
use std::task::Context;
use std::task::Poll;
@ -135,7 +137,7 @@ impl MainWorker {
unstable,
options.unsafely_ignore_certificate_errors.clone(),
),
ops::os::init(),
ops::os::init(None),
ops::permissions::init(),
ops::process::init(),
ops::signal::init(),
@ -289,6 +291,15 @@ impl MainWorker {
};
}
}
/// Return exit code set by the executed code (either in main worker
/// or one of child web workers).
pub fn get_exit_code(&mut self) -> i32 {
let op_state_rc = self.js_runtime.op_state();
let op_state = op_state_rc.borrow();
let exit_code = op_state.borrow::<Arc<AtomicI32>>().load(Relaxed);
exit_code
}
}
#[cfg(test)]