From 19c0633a947f2ee28cc07b59c32322151779101d Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Thu, 9 May 2024 13:49:10 -0700 Subject: refactor(lsp): Have JS drive TSC event loop in LSP (#23565) --- cli/util/mod.rs | 1 + cli/util/result.rs | 16 ++++++++++++++ cli/util/v8.rs | 2 ++ cli/util/v8/convert.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 cli/util/result.rs create mode 100644 cli/util/v8/convert.rs (limited to 'cli/util') diff --git a/cli/util/mod.rs b/cli/util/mod.rs index 7e0e1bd37..c8155dc51 100644 --- a/cli/util/mod.rs +++ b/cli/util/mod.rs @@ -12,6 +12,7 @@ pub mod gitignore; pub mod logger; pub mod path; pub mod progress_bar; +pub mod result; pub mod sync; pub mod text_encoding; pub mod time; diff --git a/cli/util/result.rs b/cli/util/result.rs new file mode 100644 index 000000000..3203d04eb --- /dev/null +++ b/cli/util/result.rs @@ -0,0 +1,16 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +use std::convert::Infallible; + +pub trait InfallibleResultExt { + fn unwrap_infallible(self) -> T; +} + +impl InfallibleResultExt for Result { + fn unwrap_infallible(self) -> T { + match self { + Ok(value) => value, + Err(never) => match never {}, + } + } +} diff --git a/cli/util/v8.rs b/cli/util/v8.rs index a8ab2c3d0..fb16e67b7 100644 --- a/cli/util/v8.rs +++ b/cli/util/v8.rs @@ -1,5 +1,7 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +pub mod convert; + #[inline(always)] pub fn get_v8_flags_from_env() -> Vec { std::env::var("DENO_V8_FLAGS") diff --git a/cli/util/v8/convert.rs b/cli/util/v8/convert.rs new file mode 100644 index 000000000..28107d901 --- /dev/null +++ b/cli/util/v8/convert.rs @@ -0,0 +1,57 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +use deno_core::v8; +use deno_core::FromV8; +use deno_core::ToV8; + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +/// A wrapper type for `Option` that (de)serializes `None` as `null` +#[repr(transparent)] +pub struct OptionNull(pub Option); + +impl From> for OptionNull { + fn from(option: Option) -> Self { + Self(option) + } +} + +impl From> for Option { + fn from(value: OptionNull) -> Self { + value.0 + } +} + +impl<'a, T> ToV8<'a> for OptionNull +where + T: ToV8<'a>, +{ + type Error = T::Error; + + fn to_v8( + self, + scope: &mut v8::HandleScope<'a>, + ) -> Result, Self::Error> { + match self.0 { + Some(value) => value.to_v8(scope), + None => Ok(v8::null(scope).into()), + } + } +} + +impl<'a, T> FromV8<'a> for OptionNull +where + T: FromV8<'a>, +{ + type Error = T::Error; + + fn from_v8( + scope: &mut v8::HandleScope<'a>, + value: v8::Local<'a, v8::Value>, + ) -> Result { + if value.is_null() { + Ok(OptionNull(None)) + } else { + T::from_v8(scope, value).map(|v| OptionNull(Some(v))) + } + } +} -- cgit v1.2.3