Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions apps/oxlint/src/js_plugins/external_linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,6 @@ pub fn create_external_linter(
)
}

/// Wrap `createWorkspace` JS callback as a normal Rust function.
///
/// The JS-side function is async. The returned Rust function blocks the current thread
/// until the `Promise` returned by the JS function resolves.
///
/// The returned function will panic if called outside of a Tokio runtime.
fn wrap_create_workspace(cb: JsCreateWorkspaceCb) -> ExternalLinterCreateWorkspaceCb {
Arc::new(Box::new(move |workspace_uri| {
let cb = &cb;
let res = tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async move {
cb.call_async(FnArgs::from((workspace_uri,))).await?.into_future().await
})
});

match res {
// `createWorkspace` completed successfully
Ok(()) => Ok(()),
// `createWorkspace` threw an error
Err(err) => Err(format!("`createWorkspace` threw an error: {err}")),
}
}))
}

/// Wrap `destroyWorkspace` JS callback as a normal Rust function.
fn wrap_destroy_workspace(cb: JsDestroyWorkspaceCb) -> ExternalLinterDestroyWorkspaceCb {
Arc::new(Box::new(move |workspace_uri: String| {
let _ = cb.call(FnArgs::from((workspace_uri,)), ThreadsafeFunctionCallMode::Blocking);
}))
}

/// Result returned by `loadPlugin` JS callback.
#[derive(Clone, Debug, Deserialize)]
pub enum LoadPluginReturnValue {
Expand Down Expand Up @@ -332,3 +301,34 @@ unsafe fn get_buffer(

(buffer_id, Some(buffer))
}

/// Wrap `createWorkspace` JS callback as a normal Rust function.
///
/// The JS-side function is async. The returned Rust function blocks the current thread
/// until the `Promise` returned by the JS function resolves.
///
/// The returned function will panic if called outside of a Tokio runtime.
fn wrap_create_workspace(cb: JsCreateWorkspaceCb) -> ExternalLinterCreateWorkspaceCb {
Arc::new(Box::new(move |workspace_uri| {
let cb = &cb;
let res = tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async move {
cb.call_async(FnArgs::from((workspace_uri,))).await?.into_future().await
})
});

match res {
// `createWorkspace` completed successfully
Ok(()) => Ok(()),
// `createWorkspace` threw an error
Err(err) => Err(format!("`createWorkspace` threw an error: {err}")),
}
}))
}

/// Wrap `destroyWorkspace` JS callback as a normal Rust function.
fn wrap_destroy_workspace(cb: JsDestroyWorkspaceCb) -> ExternalLinterDestroyWorkspaceCb {
Arc::new(Box::new(move |workspace_uri: String| {
let _ = cb.call(FnArgs::from((workspace_uri,)), ThreadsafeFunctionCallMode::Blocking);
}))
}
61 changes: 30 additions & 31 deletions apps/oxlint/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,6 @@ use crate::{
result::CliRunResult,
};

/// JS callback to create a workspace.
#[napi]
pub type JsCreateWorkspaceCb = ThreadsafeFunction<
// Arguments
FnArgs<(String,)>, // Workspace URI
// Return value
Promise<()>,
// Arguments (repeated)
FnArgs<(String,)>,
// Error status
Status,
// CalleeHandled
false,
>;

/// JS callback to destroy a workspace.
#[napi]
pub type JsDestroyWorkspaceCb = ThreadsafeFunction<
// Arguments
FnArgs<(String,)>, // Workspace URI
// Return value
(),
// Arguments (repeated)
FnArgs<(String,)>,
// Error status
Status,
// CalleeHandled
false,
>;

/// JS callback to load a JS plugin.
#[napi]
pub type JsLoadPluginCb = ThreadsafeFunction<
Expand Down Expand Up @@ -111,6 +81,36 @@ pub type JsSetupRuleConfigsCb = ThreadsafeFunction<
false,
>;

/// JS callback to create a workspace.
#[napi]
pub type JsCreateWorkspaceCb = ThreadsafeFunction<
// Arguments
FnArgs<(String,)>, // Workspace URI
// Return value
Promise<()>,
// Arguments (repeated)
FnArgs<(String,)>,
// Error status
Status,
// CalleeHandled
false,
>;

/// JS callback to destroy a workspace.
#[napi]
pub type JsDestroyWorkspaceCb = ThreadsafeFunction<
// Arguments
FnArgs<(String,)>, // Workspace URI
// Return value
(),
// Arguments (repeated)
FnArgs<(String,)>,
// Error status
Status,
// CalleeHandled
false,
>;

/// JS callback to load JavaScript config files.
#[napi]
pub type JsLoadJsConfigsCb = ThreadsafeFunction<
Expand All @@ -125,7 +125,6 @@ pub type JsLoadJsConfigsCb = ThreadsafeFunction<
// CalleeHandled
false,
>;

/// NAPI entry point.
///
/// JS side passes in:
Expand Down
Loading