Skip to content

Commit

Permalink
Required intterupt handler to be send
Browse files Browse the repository at this point in the history
  • Loading branch information
DelSkayn committed Jun 2, 2023
1 parent a14ad8a commit 501009b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
7 changes: 7 additions & 0 deletions core/src/runtime.rs
Expand Up @@ -5,6 +5,13 @@ pub(crate) mod raw;
mod base;
pub use base::{Runtime, WeakRuntime};

/// The type of the interrupt handler.
#[cfg(not(feature = "parallel"))]
pub type InterruptHandler = Box<dyn FnMut() -> bool + 'static>;
/// The type of the interrupt handler.
#[cfg(feature = "parallel")]
pub type InterruptHandler = Box<dyn FnMut() -> bool + Send + 'static>;

#[cfg(feature = "futures")]
mod r#async;
#[cfg(feature = "futures")]
Expand Down
9 changes: 6 additions & 3 deletions core/src/runtime/async.rs
Expand Up @@ -11,12 +11,15 @@ use async_lock::Mutex;
use crate::allocator::Allocator;
#[cfg(feature = "loader")]
use crate::loader::{RawLoader, Resolver};
use crate::{context::AsyncContext, result::AsyncJobException, Ctx, Error, Exception, Result};
use crate::{
context::AsyncContext, markers::ParallelSend, result::AsyncJobException, Ctx, Error, Exception,

Check warning on line 15 in core/src/runtime/async.rs

View workflow job for this annotation

GitHub Actions / check

[clippy] reported by reviewdog 🐶 unused import: `markers::ParallelSend` Raw Output: core/src/runtime/async.rs:15:28: warning: unused import: `markers::ParallelSend` (unused_imports)

Check warning on line 15 in core/src/runtime/async.rs

View workflow job for this annotation

GitHub Actions / check

[clippy] reported by reviewdog 🐶 unused import: `markers::ParallelSend` Raw Output: core/src/runtime/async.rs:15:28: warning: unused import: `markers::ParallelSend` (unused_imports)

Check warning on line 15 in core/src/runtime/async.rs

View workflow job for this annotation

GitHub Actions / check

[clippy] reported by reviewdog 🐶 unused import: `markers::ParallelSend` Raw Output: core/src/runtime/async.rs:15:28: warning: unused import: `markers::ParallelSend` (unused_imports)

Check warning on line 15 in core/src/runtime/async.rs

View workflow job for this annotation

GitHub Actions / check

[clippy] reported by reviewdog 🐶 unused import: `markers::ParallelSend` Raw Output: core/src/runtime/async.rs:15:28: warning: unused import: `markers::ParallelSend` (unused_imports)

Check warning on line 15 in core/src/runtime/async.rs

View workflow job for this annotation

GitHub Actions / check

[clippy] reported by reviewdog 🐶 unused import: `markers::ParallelSend` Raw Output: core/src/runtime/async.rs:15:28: warning: unused import: `markers::ParallelSend` (unused_imports)

Check warning on line 15 in core/src/runtime/async.rs

View workflow job for this annotation

GitHub Actions / check

[clippy] reported by reviewdog 🐶 unused import: `markers::ParallelSend` Raw Output: core/src/runtime/async.rs:15:28: warning: unused import: `markers::ParallelSend` (unused_imports)
Result,
};

use super::{
raw::{Opaque, RawRuntime},
spawner::DriveFuture,
MemoryUsage,
InterruptHandler, MemoryUsage,
};

#[derive(Clone)]
Expand Down Expand Up @@ -91,7 +94,7 @@ impl AsyncRuntime {
/// If the provided closure returns `true` the interpreter will raise and uncatchable
/// exception and return control flow to the caller.
#[inline]
pub async fn set_interrupt_handler(&self, handler: Option<Box<dyn FnMut() -> bool + 'static>>) {
pub async fn set_interrupt_handler(&self, handler: Option<InterruptHandler>) {
unsafe {
self.inner.lock().await.set_interrupt_handler(handler);
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/runtime/base.rs
Expand Up @@ -10,7 +10,7 @@ use crate::allocator::Allocator;

use super::{
raw::{Opaque, RawRuntime},
MemoryUsage,
InterruptHandler, MemoryUsage,
};

/// A weak handle to the runtime.
Expand Down Expand Up @@ -72,7 +72,7 @@ impl Runtime {
/// If the provided closure returns `true` the interpreter will raise and uncatchable
/// exception and return control flow to the caller.
#[inline]
pub fn set_interrupt_handler(&self, handler: Option<Box<dyn FnMut() -> bool + 'static>>) {
pub fn set_interrupt_handler(&self, handler: Option<InterruptHandler>) {
unsafe {
self.inner.lock().set_interrupt_handler(handler);
}
Expand Down
8 changes: 3 additions & 5 deletions core/src/runtime/raw.rs
Expand Up @@ -11,14 +11,15 @@ use crate::{qjs, Function};

#[cfg(feature = "futures")]
use super::spawner::Spawner;
use super::InterruptHandler;

/// Opaque book keeping data for rust.
pub(crate) struct Opaque<'js> {
/// Used to carry a panic if a callback triggered one.
pub panic: Option<Box<dyn Any + Send + 'static>>,

/// The user provided interrupt handler, if any.
pub interrupt_handler: Option<Box<dyn FnMut() -> bool + 'static>>,
pub interrupt_handler: Option<InterruptHandler>,

#[cfg(feature = "futures")]
pub spawner: Option<Spawner<'js>>,
Expand Down Expand Up @@ -233,10 +234,7 @@ impl RawRuntime {
/// Set a closure which is regularly called by the engine when it is executing code.
/// If the provided closure returns `true` the interpreter will raise and uncatchable
/// exception and return control flow to the caller.
pub unsafe fn set_interrupt_handler(
&mut self,
handler: Option<Box<dyn FnMut() -> bool + 'static>>,
) {
pub unsafe fn set_interrupt_handler(&mut self, handler: Option<InterruptHandler>) {
unsafe extern "C" fn interrupt_handler_trampoline(
_rt: *mut qjs::JSRuntime,
opaque: *mut ::std::os::raw::c_void,
Expand Down

0 comments on commit 501009b

Please sign in to comment.