Commit 1d63bdfa authored by Benjamin Woodruff's avatar Benjamin Woodruff

WIP napi v3 migration

parent 677821a6
...@@ -1167,7 +1167,7 @@ checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4" ...@@ -1167,7 +1167,7 @@ checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4"
dependencies = [ dependencies = [
"glob", "glob",
"libc", "libc",
"libloading", "libloading 0.8.1",
] ]
[[package]] [[package]]
...@@ -1460,6 +1460,15 @@ dependencies = [ ...@@ -1460,6 +1460,15 @@ dependencies = [
"unicode-segmentation", "unicode-segmentation",
] ]
[[package]]
name = "convert_case"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "affbf0190ed2caf063e3def54ff444b449371d55c58e513a95ab98eca50adb49"
dependencies = [
"unicode-segmentation",
]
[[package]] [[package]]
name = "cooked-waker" name = "cooked-waker"
version = "5.0.0" version = "5.0.0"
...@@ -1778,14 +1787,20 @@ dependencies = [ ...@@ -1778,14 +1787,20 @@ dependencies = [
[[package]] [[package]]
name = "ctor" name = "ctor"
version = "0.2.0" version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd4056f63fce3b82d852c3da92b08ea59959890813a7f4ce9c0ff85b10cf301b" checksum = "424e0138278faeb2b401f174ad17e715c829512d74f3d1e81eb43365c2e0590e"
dependencies = [ dependencies = [
"quote", "ctor-proc-macro",
"syn 2.0.104", "dtor",
] ]
[[package]]
name = "ctor-proc-macro"
version = "0.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52560adf09603e58c9a7ee1fe1dcb95a16927b17c127f0ac02d6e768a0e25bc1"
[[package]] [[package]]
name = "ctrlc" name = "ctrlc"
version = "3.4.7" version = "3.4.7"
...@@ -2162,6 +2177,21 @@ dependencies = [ ...@@ -2162,6 +2177,21 @@ dependencies = [
"dtoa", "dtoa",
] ]
[[package]]
name = "dtor"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "404d02eeb088a82cfd873006cb713fe411306c7d182c344905e101fb1167d301"
dependencies = [
"dtor-proc-macro",
]
[[package]]
name = "dtor-proc-macro"
version = "0.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f678cf4a922c215c63e0de95eb1ff08a958a81d47e485cf9da1e27bf6305cfa5"
[[package]] [[package]]
name = "dunce" name = "dunce"
version = "1.0.4" version = "1.0.4"
...@@ -3807,6 +3837,16 @@ dependencies = [ ...@@ -3807,6 +3837,16 @@ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "libloading"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "754ca22de805bb5744484a5b151a9e1a8e837d5dc232c2d7d8c2e3492edc8b60"
dependencies = [
"cfg-if",
"windows-link 0.2.1",
]
[[package]] [[package]]
name = "libmimalloc-sys" name = "libmimalloc-sys"
version = "0.1.44" version = "0.1.44"
...@@ -3870,7 +3910,7 @@ version = "1.0.0-alpha.43" ...@@ -3870,7 +3910,7 @@ version = "1.0.0-alpha.43"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84c12744d1279367caed41739ef094c325d53fb0ffcd4f9b84a368796f870252" checksum = "84c12744d1279367caed41739ef094c325d53fb0ffcd4f9b84a368796f870252"
dependencies = [ dependencies = [
"convert_case", "convert_case 0.6.0",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 1.0.109", "syn 1.0.109",
...@@ -4251,7 +4291,7 @@ version = "0.110.0" ...@@ -4251,7 +4291,7 @@ version = "0.110.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "733b723f4e2a9c561b611aed657e6c3938941124fa1cb83b6a3b8e61511ff3c4" checksum = "733b723f4e2a9c561b611aed657e6c3938941124fa1cb83b6a3b8e61511ff3c4"
dependencies = [ dependencies = [
"convert_case", "convert_case 0.6.0",
"handlebars", "handlebars",
"once_cell", "once_cell",
"regex", "regex",
...@@ -4301,16 +4341,18 @@ dependencies = [ ...@@ -4301,16 +4341,18 @@ dependencies = [
[[package]] [[package]]
name = "napi" name = "napi"
version = "2.16.13" version = "3.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "214f07a80874bb96a8433b3cdfc84980d56c7b02e1a0d7ba4ba0db5cef785e2b" checksum = "e6944d0bf100571cd6e1a98a316cdca262deb6fccf8d93f5ae1502ca3fc88bd3"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bitflags 2.9.1", "bitflags 2.9.1",
"ctor", "ctor",
"napi-derive", "futures",
"napi-build",
"napi-sys", "napi-sys",
"once_cell", "nohash-hasher",
"rustc-hash 2.1.1",
"serde", "serde",
"serde_json", "serde_json",
"tokio", "tokio",
...@@ -4318,18 +4360,18 @@ dependencies = [ ...@@ -4318,18 +4360,18 @@ dependencies = [
[[package]] [[package]]
name = "napi-build" name = "napi-build"
version = "2.1.3" version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1c0f5d67ee408a4685b61f5ab7e58605c8ae3f2b4189f0127d804ff13d5560a" checksum = "d376940fd5b723c6893cd1ee3f33abbfd86acb1cd1ec079f3ab04a2a3bc4d3b1"
[[package]] [[package]]
name = "napi-derive" name = "napi-derive"
version = "2.16.13" version = "3.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cbe2585d8ac223f7d34f13701434b9d5f4eb9c332cccce8dee57ea18ab8ab0c" checksum = "2c914b5e420182bfb73504e0607592cdb8e2e21437d450883077669fb72a114d"
dependencies = [ dependencies = [
"cfg-if", "convert_case 0.11.0",
"convert_case", "ctor",
"napi-derive-backend", "napi-derive-backend",
"proc-macro2", "proc-macro2",
"quote", "quote",
...@@ -4338,26 +4380,24 @@ dependencies = [ ...@@ -4338,26 +4380,24 @@ dependencies = [
[[package]] [[package]]
name = "napi-derive-backend" name = "napi-derive-backend"
version = "1.0.75" version = "5.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1639aaa9eeb76e91c6ae66da8ce3e89e921cd3885e99ec85f4abacae72fc91bf" checksum = "f0864cf6a82e2cfb69067374b64c9253d7e910e5b34db833ed7495dda56ccb18"
dependencies = [ dependencies = [
"convert_case", "convert_case 0.11.0",
"once_cell",
"proc-macro2", "proc-macro2",
"quote", "quote",
"regex",
"semver", "semver",
"syn 2.0.104", "syn 2.0.104",
] ]
[[package]] [[package]]
name = "napi-sys" name = "napi-sys"
version = "2.4.0" version = "3.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "427802e8ec3a734331fec1035594a210ce1ff4dc5bc1950530920ab717964ea3" checksum = "8eb602b84d7c1edae45e50bbf1374696548f36ae179dfa667f577e384bb90c2b"
dependencies = [ dependencies = [
"libloading", "libloading 0.9.0",
] ]
[[package]] [[package]]
......
...@@ -282,7 +282,7 @@ lzzzz = "2.0.0" ...@@ -282,7 +282,7 @@ lzzzz = "2.0.0"
markdown = "1.0.0-alpha.18" markdown = "1.0.0-alpha.18"
memchr = "2.7.4" memchr = "2.7.4"
mime = "0.3.16" mime = "0.3.16"
napi = { version = "2", default-features = false, features = [ napi = { version = "3", default-features = false, features = [
"napi3", "napi3",
"serde-json", "serde-json",
"tokio_rt", "tokio_rt",
...@@ -292,7 +292,7 @@ napi = { version = "2", default-features = false, features = [ ...@@ -292,7 +292,7 @@ napi = { version = "2", default-features = false, features = [
"napi5", "napi5",
"compat-mode", "compat-mode",
] } ] }
napi-derive = "2" napi-derive = { version = "3", features = ["compat-mode"] }
napi-build = "2" napi-build = "2"
nohash-hasher = "0.2.0" nohash-hasher = "0.2.0"
notify = "8.1.0" notify = "8.1.0"
......
...@@ -68,20 +68,8 @@ fn main() -> anyhow::Result<()> { ...@@ -68,20 +68,8 @@ fn main() -> anyhow::Result<()> {
_ => println!("cargo:warning=`git rev-parse HEAD` failed"), _ => println!("cargo:warning=`git rev-parse HEAD` failed"),
} }
#[cfg(not(all(target_os = "macos", target_arch = "aarch64")))]
napi_build::setup(); napi_build::setup();
// This is a workaround for napi always including a GCC specific flag.
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
{
println!("cargo:rerun-if-env-changed=DEBUG_GENERATED_CODE");
println!("cargo:rerun-if-env-changed=TYPE_DEF_TMP_PATH");
println!("cargo:rerun-if-env-changed=CARGO_CFG_NAPI_RS_CLI_VERSION");
println!("cargo:rustc-cdylib-link-arg=-undefined");
println!("cargo:rustc-cdylib-link-arg=dynamic_lookup");
}
// Resolve a potential linker issue for unit tests on linux // Resolve a potential linker issue for unit tests on linux
// https://github.com/napi-rs/napi-rs/issues/1782 // https://github.com/napi-rs/napi-rs/issues/1782
#[cfg(all(target_os = "linux", not(target_arch = "wasm32")))] #[cfg(all(target_os = "linux", not(target_arch = "wasm32")))]
......
use napi::{CallContext, JsObject, JsUnknown}; use napi::{CallContext, JsObject, Unknown as JsUnknown};
use napi_derive::{js_function, module_exports, napi}; use napi_derive::{js_function, module_exports, napi};
use next_core::next_config::lightningcss_feature_names_to_mask; use next_core::next_config::lightningcss_feature_names_to_mask;
......
...@@ -69,7 +69,7 @@ static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc; ...@@ -69,7 +69,7 @@ static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc;
static ALLOC: dhat::Alloc = dhat::Alloc; static ALLOC: dhat::Alloc = dhat::Alloc;
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
#[napi::module_init] #[napi_derive::module_init]
fn init() { fn init() {
use std::{ use std::{
cell::RefCell, cell::RefCell,
...@@ -129,7 +129,7 @@ pub fn complete_output( ...@@ -129,7 +129,7 @@ pub fn complete_output(
output: TransformOutput, output: TransformOutput,
eliminated_packages: FxHashSet<Atom>, eliminated_packages: FxHashSet<Atom>,
use_cache_telemetry_tracker: FxHashMap<String, usize>, use_cache_telemetry_tracker: FxHashMap<String, usize>,
) -> napi::Result<Object> { ) -> napi::Result<napi::JsObject> {
let mut js_output = env.create_object()?; let mut js_output = env.create_object()?;
js_output.set_named_property("code", env.create_string_from_std(output.code)?)?; js_output.set_named_property("code", env.create_string_from_std(output.code)?)?;
if let Some(map) = output.map { if let Some(map) = output.map {
......
...@@ -6,7 +6,7 @@ use std::{ ...@@ -6,7 +6,7 @@ use std::{
}; };
use anyhow::Context; use anyhow::Context;
use napi::bindgen_prelude::External; use napi::bindgen_prelude::{External, ExternalRef};
use napi_derive::napi; use napi_derive::napi;
/// A wrapper around [`File`] that is passed to JS, and is set to `None` when [`lockfile_unlock`] is /// A wrapper around [`File`] that is passed to JS, and is set to `None` when [`lockfile_unlock`] is
...@@ -116,7 +116,7 @@ pub async fn lockfile_try_acquire( ...@@ -116,7 +116,7 @@ pub async fn lockfile_try_acquire(
#[napi] #[napi]
pub fn lockfile_unlock_sync( pub fn lockfile_unlock_sync(
#[napi(ts_arg_type = "{ __napiType: \"Lockfile\" }")] lockfile: External<JsLockfile>, #[napi(ts_arg_type = "{ __napiType: \"Lockfile\" }")] mut lockfile: ExternalRef<JsLockfile>,
) { ) {
// We don't need the file handle anymore, so we don't need to call `File::unlock`. Locks are // We don't need the file handle anymore, so we don't need to call `File::unlock`. Locks are
// released during `drop`. Remove it from the `ManuallyDrop` wrapper. // released during `drop`. Remove it from the `ManuallyDrop` wrapper.
...@@ -140,12 +140,9 @@ pub fn lockfile_unlock_sync( ...@@ -140,12 +140,9 @@ pub fn lockfile_unlock_sync(
} }
#[napi] #[napi]
pub async fn lockfile_unlock( pub fn lockfile_unlock(
#[napi(ts_arg_type = "{ __napiType: \"Lockfile\" }")] lockfile: External<JsLockfile>, #[napi(ts_arg_type = "{ __napiType: \"Lockfile\" }")] lockfile: ExternalRef<JsLockfile>,
) -> napi::Result<()> { ) -> napi::Result<()> {
Ok( lockfile_unlock_sync(lockfile);
tokio::task::spawn_blocking(move || lockfile_unlock_sync(lockfile)) Ok(())
.await
.context("panicked while attempting to unlock lockfile")?,
)
} }
...@@ -2,7 +2,10 @@ use std::{ops::Deref, sync::Arc}; ...@@ -2,7 +2,10 @@ use std::{ops::Deref, sync::Arc};
use anyhow::Result; use anyhow::Result;
use futures_util::TryFutureExt; use futures_util::TryFutureExt;
use napi::{JsFunction, bindgen_prelude::External}; use napi::{
Env, JsFunction, Unknown as JsUnknown,
bindgen_prelude::{External, ExternalRef},
};
use napi_derive::napi; use napi_derive::napi;
use next_api::{ use next_api::{
operation::OptionEndpoint, operation::OptionEndpoint,
...@@ -20,8 +23,8 @@ use turbopack_core::{ ...@@ -20,8 +23,8 @@ use turbopack_core::{
}; };
use crate::next_api::utils::{ use crate::next_api::utils::{
DetachedVc, NapiDiagnostic, NapiIssue, RootTask, TurbopackResult, DetachedVc, NapiDiagnostic, NapiIssue, RootTask, SendableExternalRef, TurbopackResult,
strongly_consistent_catch_collectables, subscribe, func_ref_to_js_function, strongly_consistent_catch_collectables, subscribe,
}; };
#[napi(object)] #[napi(object)]
...@@ -142,7 +145,9 @@ async fn get_written_endpoint_with_issues_operation( ...@@ -142,7 +145,9 @@ async fn get_written_endpoint_with_issues_operation(
#[tracing::instrument(level = "info", name = "write endpoint to disk", skip_all)] #[tracing::instrument(level = "info", name = "write endpoint to disk", skip_all)]
#[napi] #[napi]
pub async fn endpoint_write_to_disk( pub async fn endpoint_write_to_disk(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>, #[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: SendableExternalRef<
ExternalEndpoint,
>,
) -> napi::Result<TurbopackResult<NapiWrittenEndpoint>> { ) -> napi::Result<TurbopackResult<NapiWrittenEndpoint>> {
let ctx = endpoint.turbopack_ctx(); let ctx = endpoint.turbopack_ctx();
let endpoint_op = ***endpoint; let endpoint_op = ***endpoint;
...@@ -176,12 +181,14 @@ pub async fn endpoint_write_to_disk( ...@@ -176,12 +181,14 @@ pub async fn endpoint_write_to_disk(
#[tracing::instrument(level = "info", name = "get server-side endpoint changes", skip_all)] #[tracing::instrument(level = "info", name = "get server-side endpoint changes", skip_all)]
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")] #[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn endpoint_server_changed_subscribe( pub fn endpoint_server_changed_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>, env: Env,
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: ExternalRef<ExternalEndpoint>,
issues: bool, issues: bool,
func: JsFunction, func: napi::bindgen_prelude::FunctionRef<JsUnknown<'static>, JsUnknown<'static>>,
) -> napi::Result<External<RootTask>> { ) -> napi::Result<External<RootTask>> {
let turbopack_ctx = endpoint.turbopack_ctx().clone(); let turbopack_ctx = endpoint.turbopack_ctx().clone();
let endpoint = ***endpoint; let endpoint = ***endpoint;
let func: JsFunction = func_ref_to_js_function(&env, &func)?;
subscribe( subscribe(
turbopack_ctx, turbopack_ctx,
func, func,
...@@ -202,14 +209,14 @@ pub fn endpoint_server_changed_subscribe( ...@@ -202,14 +209,14 @@ pub fn endpoint_server_changed_subscribe(
effects: _, effects: _,
} = &*ctx.value; } = &*ctx.value;
Ok(vec![TurbopackResult { Ok(TurbopackResult {
result: (), result: (),
issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(), issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(),
diagnostics: diagnostics diagnostics: diagnostics
.iter() .iter()
.map(|d| NapiDiagnostic::from(d)) .map(|d| NapiDiagnostic::from(d))
.collect(), .collect(),
}]) })
}, },
) )
} }
...@@ -268,11 +275,13 @@ async fn subscribe_issues_and_diags_operation( ...@@ -268,11 +275,13 @@ async fn subscribe_issues_and_diags_operation(
#[tracing::instrument(level = "info", name = "get client-side endpoint changes", skip_all)] #[tracing::instrument(level = "info", name = "get client-side endpoint changes", skip_all)]
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")] #[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn endpoint_client_changed_subscribe( pub fn endpoint_client_changed_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>, env: Env,
func: JsFunction, #[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: ExternalRef<ExternalEndpoint>,
func: napi::bindgen_prelude::FunctionRef<JsUnknown<'static>, JsUnknown<'static>>,
) -> napi::Result<External<RootTask>> { ) -> napi::Result<External<RootTask>> {
let turbopack_ctx = endpoint.turbopack_ctx().clone(); let turbopack_ctx = endpoint.turbopack_ctx().clone();
let endpoint_op = ***endpoint; let endpoint_op = ***endpoint;
let func: JsFunction = func_ref_to_js_function(&env, &func)?;
subscribe( subscribe(
turbopack_ctx, turbopack_ctx,
func, func,
...@@ -291,11 +300,11 @@ pub fn endpoint_client_changed_subscribe( ...@@ -291,11 +300,11 @@ pub fn endpoint_client_changed_subscribe(
.instrument(tracing::info_span!("client changes subscription")) .instrument(tracing::info_span!("client changes subscription"))
}, },
|_| { |_| {
Ok(vec![TurbopackResult { Ok(TurbopackResult {
result: (), result: (),
issues: vec![], issues: vec![],
diagnostics: vec![], diagnostics: vec![],
}]) })
}, },
) )
} }
...@@ -11,7 +11,11 @@ use std::{ ...@@ -11,7 +11,11 @@ use std::{
use anyhow::Result; use anyhow::Result;
use either::Either; use either::Either;
use napi::{Env, JsFunction, bindgen_prelude::Promise, threadsafe_function::ThreadsafeFunction}; use napi::{
Env, JsFunction, Status, Unknown as JsUnknown,
bindgen_prelude::{FunctionRef, Promise},
threadsafe_function::ThreadsafeFunction,
};
use napi_derive::napi; use napi_derive::napi;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use owo_colors::OwoColorize; use owo_colors::OwoColorize;
...@@ -96,15 +100,19 @@ impl NextTurbopackContext { ...@@ -96,15 +100,19 @@ impl NextTurbopackContext {
log_internal_error_and_inform(err); log_internal_error_and_inform(err);
async move { async move {
this.inner match this
.inner
.napi_callbacks .napi_callbacks
.throw_turbopack_internal_error .throw_turbopack_internal_error
.call_async::<()>(Ok(TurbopackInternalErrorOpts { .call_async(Ok(TurbopackInternalErrorOpts {
message, message,
anonymized_location: panic_location, anonymized_location: panic_location,
})) }))
.await .await
.expect_err("throwTurbopackInternalError must throw an error") {
Ok(_) => panic!("throwTurbopackInternalError must throw an error"),
Err(err) => err,
}
} }
} }
...@@ -127,7 +135,7 @@ impl NextTurbopackContext { ...@@ -127,7 +135,7 @@ impl NextTurbopackContext {
/// Calls the `onBeforeDeferredEntries` callback in Node.js if one was provided. /// Calls the `onBeforeDeferredEntries` callback in Node.js if one was provided.
pub async fn on_before_deferred_entries(&self) -> napi::Result<()> { pub async fn on_before_deferred_entries(&self) -> napi::Result<()> {
if let Some(callback) = &self.inner.napi_callbacks.on_before_deferred_entries { if let Some(callback) = &self.inner.napi_callbacks.on_before_deferred_entries {
let promise = callback.call_async::<Promise<()>>(Ok(())).await?; let promise = callback.call_async(Ok(())).await?;
promise.await?; promise.await?;
} }
Ok(()) Ok(())
...@@ -138,7 +146,7 @@ impl NextTurbopackContext { ...@@ -138,7 +146,7 @@ impl NextTurbopackContext {
/// ///
/// This can be converted into a [`NapiNextTurbopackCallbacks`] with /// This can be converted into a [`NapiNextTurbopackCallbacks`] with
/// [`NapiNextTurbopackCallbacks::from_js`]. /// [`NapiNextTurbopackCallbacks::from_js`].
#[napi(object)] #[napi(object, object_to_js = false)]
pub struct NapiNextTurbopackCallbacksJsObject { pub struct NapiNextTurbopackCallbacksJsObject {
/// Called when we've encountered a bug in Turbopack and not in the user's code. Constructs and /// Called when we've encountered a bug in Turbopack and not in the user's code. Constructs and
/// throws a `TurbopackInternalError` type. Logs to anonymized telemetry. /// throws a `TurbopackInternalError` type. Logs to anonymized telemetry.
...@@ -147,11 +155,11 @@ pub struct NapiNextTurbopackCallbacksJsObject { ...@@ -147,11 +155,11 @@ pub struct NapiNextTurbopackCallbacksJsObject {
/// there's a runtime conversion error. This should never happen, but if it does, the function /// there's a runtime conversion error. This should never happen, but if it does, the function
/// can throw it instead. /// can throw it instead.
#[napi(ts_type = "(conversionError: Error | null, opts: TurbopackInternalErrorOpts) => never")] #[napi(ts_type = "(conversionError: Error | null, opts: TurbopackInternalErrorOpts) => never")]
pub throw_turbopack_internal_error: JsFunction, pub throw_turbopack_internal_error: FunctionRef<JsUnknown<'static>, JsUnknown<'static>>,
/// Called before deferred entries are processed in a production build. /// Called before deferred entries are processed in a production build.
#[napi(ts_type = "() => Promise<void>")] #[napi(ts_type = "() => Promise<void>")]
pub on_before_deferred_entries: Option<JsFunction>, pub on_before_deferred_entries: Option<FunctionRef<JsUnknown<'static>, JsUnknown<'static>>>,
} }
/// A collection of helper JavaScript functions passed into /// A collection of helper JavaScript functions passed into
...@@ -167,8 +175,14 @@ pub struct NapiNextTurbopackCallbacks { ...@@ -167,8 +175,14 @@ pub struct NapiNextTurbopackCallbacks {
// to all of our async entrypoints, and would be complicated by `FunctionRef` being `!Send` (I // to all of our async entrypoints, and would be complicated by `FunctionRef` being `!Send` (I
// think it could be `Send`, as long as `napi::Env` is checked at call-time, which it should be // think it could be `Send`, as long as `napi::Env` is checked at call-time, which it should be
// anyways). // anyways).
throw_turbopack_internal_error: ThreadsafeFunction<TurbopackInternalErrorOpts>, throw_turbopack_internal_error: ThreadsafeFunction<
on_before_deferred_entries: Option<ThreadsafeFunction<()>>, TurbopackInternalErrorOpts,
(),
TurbopackInternalErrorOpts,
Status,
true,
>,
on_before_deferred_entries: Option<ThreadsafeFunction<(), Promise<()>, (), Status, true>>,
} }
/// Arguments for `NapiNextTurbopackCallbacks::throw_turbopack_internal_error`. /// Arguments for `NapiNextTurbopackCallbacks::throw_turbopack_internal_error`.
...@@ -180,14 +194,23 @@ pub struct TurbopackInternalErrorOpts { ...@@ -180,14 +194,23 @@ pub struct TurbopackInternalErrorOpts {
impl NapiNextTurbopackCallbacks { impl NapiNextTurbopackCallbacks {
pub fn from_js(env: &Env, obj: NapiNextTurbopackCallbacksJsObject) -> napi::Result<Self> { pub fn from_js(env: &Env, obj: NapiNextTurbopackCallbacksJsObject) -> napi::Result<Self> {
let mut throw_turbopack_internal_error: ThreadsafeFunction<TurbopackInternalErrorOpts> = let throw_turbopack_internal_error: JsFunction = {
obj.throw_turbopack_internal_error let func = obj.throw_turbopack_internal_error.borrow_back(env)?;
.create_threadsafe_function(0, |ctx| { let value = napi::JsValue::value(&func);
// Avoid unpacking the struct into positional arguments, we really want to make unsafe { <JsFunction as napi::NapiValue>::from_raw_unchecked(value.env, value.value) }
// sure we don't incorrectly order arguments and accidentally log a potentially };
// PII-containing message in anonymized telemetry. let mut throw_turbopack_internal_error: ThreadsafeFunction<
Ok(vec![ctx.value]) TurbopackInternalErrorOpts,
})?; (),
TurbopackInternalErrorOpts,
Status,
true,
> = throw_turbopack_internal_error.create_threadsafe_function(|ctx| {
// Avoid unpacking the struct into positional arguments, we really want to make
// sure we don't incorrectly order arguments and accidentally log a potentially
// PII-containing message in anonymized telemetry.
Ok(ctx.value)
})?;
// Unref so this ThreadsafeFunction doesn't keep the Node.js event loop alive // Unref so this ThreadsafeFunction doesn't keep the Node.js event loop alive
// after shutdown. // after shutdown.
let _ = throw_turbopack_internal_error.unref(env); let _ = throw_turbopack_internal_error.unref(env);
...@@ -195,7 +218,15 @@ impl NapiNextTurbopackCallbacks { ...@@ -195,7 +218,15 @@ impl NapiNextTurbopackCallbacks {
let on_before_deferred_entries = obj let on_before_deferred_entries = obj
.on_before_deferred_entries .on_before_deferred_entries
.map(|callback| { .map(|callback| {
let mut f = callback.create_threadsafe_function(0, |_| Ok::<Vec<()>, _>(vec![]))?; let callback: JsFunction = {
let func = callback.borrow_back(env)?;
let value = napi::JsValue::value(&func);
unsafe {
<JsFunction as napi::NapiValue>::from_raw_unchecked(value.env, value.value)
}
};
let mut f: ThreadsafeFunction<(), Promise<()>, (), Status, true> =
callback.create_threadsafe_function(|_| Ok::<(), _>(()))?;
let _ = f.unref(env); let _ = f.unref(env);
Ok::<_, napi::Error>(f) Ok::<_, napi::Error>(f)
}) })
......
use std::{future::Future, ops::Deref, sync::Arc}; use std::{future::Future, ops::Deref, sync::Arc};
/// Convert a `FunctionRef` to a compat-mode `JsFunction` by borrowing back from the reference.
pub fn func_ref_to_js_function(
env: &napi::Env,
func_ref: &napi::bindgen_prelude::FunctionRef<napi::Unknown<'static>, napi::Unknown<'static>>,
) -> napi::Result<napi::JsFunction> {
let func = func_ref.borrow_back(env)?;
let v = napi::JsValue::value(&func);
Ok(unsafe { <napi::JsFunction as napi::NapiValue>::from_raw_unchecked(v.env, v.value) })
}
use anyhow::{Context, Result, anyhow}; use anyhow::{Context, Result, anyhow};
use futures_util::TryFutureExt; use futures_util::TryFutureExt;
use napi::{ use napi::{
JsFunction, JsObject, JsUnknown, NapiRaw, NapiValue, Status, JsFunction, JsObject, JsString, NapiRaw, Status, Unknown as JsUnknown,
bindgen_prelude::{Buffer, External, ToNapiValue}, bindgen_prelude::{Buffer, External, ExternalRef, ToNapiValue},
threadsafe_function::{ThreadSafeCallContext, ThreadsafeFunction, ThreadsafeFunctionCallMode}, threadsafe_function::{ThreadsafeCallContext, ThreadsafeFunction, ThreadsafeFunctionCallMode},
}; };
use napi_derive::napi; use napi_derive::napi;
use next_code_frame::{CodeFrameLocation, CodeFrameOptions, Location, render_code_frame}; use next_code_frame::{CodeFrameLocation, CodeFrameOptions, Location, render_code_frame};
...@@ -64,6 +74,59 @@ impl<T> Deref for DetachedVc<T> { ...@@ -64,6 +74,59 @@ impl<T> Deref for DetachedVc<T> {
} }
} }
/// A wrapper around `&'static External<T>` that implements `Send` + `Sync`.
///
/// This is safe because `External<T>` data (the inner `T`) is `Send + Sync`, and the raw napi
/// pointers inside `External` are only used for GC tracking — we only access the inner `T` via
/// `Deref`.
///
/// This type should only be used as a parameter to `#[napi]` async functions where the napi v3
/// macro requires `Send` futures.
pub struct SendableExternalRef<T: 'static> {
inner: &'static External<T>,
}
// SAFETY: We only access the inner T (which is Send + Sync) through Deref.
// The raw napi pointers are never dereferenced across threads.
unsafe impl<T: Send + Sync + 'static> Send for SendableExternalRef<T> {}
unsafe impl<T: Send + Sync + 'static> Sync for SendableExternalRef<T> {}
impl<T: 'static> std::ops::Deref for SendableExternalRef<T> {
type Target = T;
fn deref(&self) -> &T {
self.inner
}
}
impl<T: Send + Sync + 'static> napi::bindgen_prelude::TypeName for SendableExternalRef<T> {
fn type_name() -> &'static str {
"External"
}
fn value_type() -> napi::ValueType {
napi::ValueType::External
}
}
impl<T: Send + Sync + 'static> napi::bindgen_prelude::ValidateNapiValue for SendableExternalRef<T> {}
impl<T: Send + Sync + 'static> napi::bindgen_prelude::FromNapiValue for SendableExternalRef<T> {
unsafe fn from_napi_value(
env: napi::sys::napi_env,
napi_val: napi::sys::napi_value,
) -> napi::Result<Self> {
let external = unsafe {
<External<T> as napi::bindgen_prelude::FromNapiMutRef>::from_napi_mut_ref(
env, napi_val,
)?
};
// SAFETY: The External is prevented from being GC'd by napi during the call.
// For async functions, the napi macro ensures the reference is held for the
// duration of the future.
let inner: &'static External<T> = unsafe { &*(external as *const External<T>) };
Ok(SendableExternalRef { inner })
}
}
/// An opaque handle to the root of a turbo-tasks computation created by /// An opaque handle to the root of a turbo-tasks computation created by
/// [`turbo_tasks::TurboTasks::spawn_root_task`] that can be passed back and forth to JS across the /// [`turbo_tasks::TurboTasks::spawn_root_task`] that can be passed back and forth to JS across the
/// [`napi`][mod@napi] boundary via [`External`]. /// [`napi`][mod@napi] boundary via [`External`].
...@@ -87,7 +150,7 @@ impl Drop for RootTask { ...@@ -87,7 +150,7 @@ impl Drop for RootTask {
#[napi] #[napi]
pub fn root_task_dispose( pub fn root_task_dispose(
#[napi(ts_arg_type = "{ __napiType: \"RootTask\" }")] mut root_task: External<RootTask>, #[napi(ts_arg_type = "{ __napiType: \"RootTask\" }")] mut root_task: ExternalRef<RootTask>,
) -> napi::Result<()> { ) -> napi::Result<()> {
if let Some(task) = root_task.task_id.take() { if let Some(task) = root_task.task_id.take() {
root_task root_task
...@@ -418,15 +481,19 @@ impl<T: ToNapiValue> ToNapiValue for TurbopackResult<T> { ...@@ -418,15 +481,19 @@ impl<T: ToNapiValue> ToNapiValue for TurbopackResult<T> {
let result = unsafe { let result = unsafe {
let result = T::to_napi_value(env, val.result)?; let result = T::to_napi_value(env, val.result)?;
JsUnknown::from_raw(env, result)? unsafe { JsUnknown::from_raw_unchecked(env, result) }
}; };
if matches!(result.get_type()?, napi::ValueType::Object) { if matches!(result.get_type()?, napi::ValueType::Object) {
// SAFETY: We know that result is an object, so we can cast it to a JsObject // SAFETY: We know that result is an object, so we can cast it to a JsObject
let result = unsafe { result.cast::<JsObject>() }; let result = unsafe { result.cast::<JsObject>()? };
let property_names = result.get_property_names()?;
for key in JsObject::keys(&result)? { let len = property_names.get_array_length()?;
let value: JsUnknown = result.get_named_property(&key)?;
obj.set_named_property(&key, value)?; for i in 0..len {
let key: napi::JsString = property_names.get_element(i)?;
let key_str = key.into_utf8()?.into_owned()?;
let value: JsUnknown = result.get_named_property(&key_str)?;
obj.set_named_property(&key_str, value)?;
} }
} }
...@@ -437,13 +504,19 @@ impl<T: ToNapiValue> ToNapiValue for TurbopackResult<T> { ...@@ -437,13 +504,19 @@ impl<T: ToNapiValue> ToNapiValue for TurbopackResult<T> {
} }
} }
pub fn subscribe<T: 'static + Send + Sync, F: Future<Output = Result<T>> + Send, V: ToNapiValue>( pub fn subscribe<
T: 'static + Send + Sync,
F: Future<Output = Result<T>> + Send,
V: 'static + ToNapiValue,
>(
ctx: NextTurbopackContext, ctx: NextTurbopackContext,
func: JsFunction, func: JsFunction,
handler: impl 'static + Sync + Send + Clone + Fn() -> F, handler: impl 'static + Sync + Send + Clone + Fn() -> F,
mapper: impl 'static + Sync + Send + FnMut(ThreadSafeCallContext<T>) -> napi::Result<Vec<V>>, mapper: impl 'static + Sync + Send + FnMut(ThreadsafeCallContext<T>) -> napi::Result<V>,
) -> napi::Result<External<RootTask>> { ) -> napi::Result<External<RootTask>> {
let func: ThreadsafeFunction<T> = func.create_threadsafe_function(0, mapper)?; let func: ThreadsafeFunction<T, (), V, Status, true> =
func.create_threadsafe_function(mapper)?;
let func = Arc::new(func);
let task_id = ctx.turbo_tasks().spawn_root_task({ let task_id = ctx.turbo_tasks().spawn_root_task({
let ctx = ctx.clone(); let ctx = ctx.clone();
move || { move || {
......
...@@ -70,7 +70,7 @@ pub struct FinderTask { ...@@ -70,7 +70,7 @@ pub struct FinderTask {
impl Task for FinderTask { impl Task for FinderTask {
type Output = Vec<Atom>; type Output = Vec<Atom>;
type JsValue = Array; type JsValue = Vec<String>;
fn compute(&mut self) -> napi::Result<Self::Output> { fn compute(&mut self) -> napi::Result<Self::Output> {
let resource_path = PathBuf::from(self.resource_path.take().unwrap()); let resource_path = PathBuf::from(self.resource_path.take().unwrap());
...@@ -156,12 +156,8 @@ impl Task for FinderTask { ...@@ -156,12 +156,8 @@ impl Task for FinderTask {
} }
fn resolve(&mut self, env: Env, result: Self::Output) -> napi::Result<Self::JsValue> { fn resolve(&mut self, env: Env, result: Self::Output) -> napi::Result<Self::JsValue> {
let mut array = env.create_array(result.len() as u32)?; let _ = env;
for (i, name) in result.iter().enumerate() { Ok(result.into_iter().map(|name| name.to_string()).collect())
let js_val = env.create_string(name.as_str())?;
array.set(i as u32, js_val)?;
}
Ok(array)
} }
} }
...@@ -172,9 +168,9 @@ pub fn get_module_named_exports(resource_path: String) -> AsyncTask<FinderTask> ...@@ -172,9 +168,9 @@ pub fn get_module_named_exports(resource_path: String) -> AsyncTask<FinderTask>
}) })
} }
#[napi(object)] #[napi(object, object_from_js = false)]
pub struct NapiSourceDiagnostic { pub struct NapiSourceDiagnostic {
pub severity: &'static str, pub severity: String,
pub message: String, pub message: String,
pub loc: NapiIssueSourceRange, pub loc: NapiIssueSourceRange,
} }
...@@ -251,7 +247,7 @@ impl Task for AnalyzeTask { ...@@ -251,7 +247,7 @@ impl Task for AnalyzeTask {
let start = c.cm.lookup_char_pos(span.lo); let start = c.cm.lookup_char_pos(span.lo);
let end = c.cm.lookup_char_pos(span.hi); let end = c.cm.lookup_char_pos(span.hi);
diagnostics.borrow_mut().push(NapiSourceDiagnostic { diagnostics.borrow_mut().push(NapiSourceDiagnostic {
severity: "Warning", severity: "Warning".to_string(),
message: msg, message: msg,
loc: NapiIssueSourceRange { loc: NapiIssueSourceRange {
start: NapiSourcePos { start: NapiSourcePos {
...@@ -269,7 +265,7 @@ impl Task for AnalyzeTask { ...@@ -269,7 +265,7 @@ impl Task for AnalyzeTask {
let start = c.cm.lookup_char_pos(span.lo); let start = c.cm.lookup_char_pos(span.lo);
let end = c.cm.lookup_char_pos(span.hi); let end = c.cm.lookup_char_pos(span.hi);
diagnostics.borrow_mut().push(NapiSourceDiagnostic { diagnostics.borrow_mut().push(NapiSourceDiagnostic {
severity: "Error", severity: "Error".to_string(),
message: msg, message: msg,
loc: NapiIssueSourceRange { loc: NapiIssueSourceRange {
start: NapiSourcePos { start: NapiSourcePos {
......
...@@ -83,7 +83,7 @@ fn skip_filename() -> bool { ...@@ -83,7 +83,7 @@ fn skip_filename() -> bool {
impl Task for TransformTask { impl Task for TransformTask {
type Output = (TransformOutput, FxHashSet<Atom>, FxHashMap<String, usize>); type Output = (TransformOutput, FxHashSet<Atom>, FxHashMap<String, usize>);
type JsValue = Object; type JsValue = napi::JsObject;
fn compute(&mut self) -> napi::Result<Self::Output> { fn compute(&mut self) -> napi::Result<Self::Output> {
GLOBALS.set(&Default::default(), || { GLOBALS.set(&Default::default(), || {
...@@ -226,7 +226,7 @@ pub fn transform_sync( ...@@ -226,7 +226,7 @@ pub fn transform_sync(
src: Either3<String, Buffer, Undefined>, src: Either3<String, Buffer, Undefined>,
_is_module: bool, _is_module: bool,
options: Buffer, options: Buffer,
) -> napi::Result<Object> { ) -> napi::Result<napi::JsObject> {
let c = get_compiler(); let c = get_compiler();
let input = match src { let input = match src {
......
...@@ -29,7 +29,7 @@ DEALINGS IN THE SOFTWARE. ...@@ -29,7 +29,7 @@ DEALINGS IN THE SOFTWARE.
use std::{cell::RefCell, env, path::PathBuf}; use std::{cell::RefCell, env, path::PathBuf};
use anyhow::anyhow; use anyhow::anyhow;
use napi::bindgen_prelude::{External, Status}; use napi::bindgen_prelude::{External, ExternalRef, Status};
use napi_derive::napi; use napi_derive::napi;
use tracing_chrome::{ChromeLayerBuilder, FlushGuard}; use tracing_chrome::{ChromeLayerBuilder, FlushGuard};
use tracing_subscriber::{Layer, filter, layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{Layer, filter, layer::SubscriberExt, util::SubscriberInitExt};
...@@ -124,7 +124,7 @@ pub fn init_custom_trace_subscriber( ...@@ -124,7 +124,7 @@ pub fn init_custom_trace_subscriber(
/// This should be called when parent node.js process exits, otherwise generated /// This should be called when parent node.js process exits, otherwise generated
/// trace may drop traces in the buffer. /// trace may drop traces in the buffer.
#[napi] #[napi]
pub fn teardown_trace_subscriber(guard_external: External<RefCell<Option<FlushGuard>>>) { pub fn teardown_trace_subscriber(guard_external: ExternalRef<RefCell<Option<FlushGuard>>>) {
let guard_cell = &*guard_external; let guard_cell = &*guard_external;
if let Some(guard) = guard_cell.take() { if let Some(guard) = guard_cell.take() {
......
...@@ -8,12 +8,12 @@ ...@@ -8,12 +8,12 @@
"scripts": { "scripts": {
"build-native-auto": "node maybe-build-native.mjs", "build-native-auto": "node maybe-build-native.mjs",
"clean": "node ../../scripts/rm.mjs native", "clean": "node ../../scripts/rm.mjs native",
"build-native": "napi build --platform -p next-napi-bindings --cargo-cwd ../../ --cargo-name next_napi_bindings --features plugin,image-extended --js false native", "build-native": "napi build --platform -p next-napi-bindings --manifest-path ../../Cargo.toml --features plugin,image-extended --no-js -o native",
"build-native-release": "napi build --platform -p next-napi-bindings --cargo-cwd ../../ --cargo-name next_napi_bindings --release --features plugin,image-extended,tracing/release_max_level_trace --js false native", "build-native-release": "napi build --platform -p next-napi-bindings --manifest-path ../../Cargo.toml --release --features plugin,image-extended,tracing/release_max_level_trace --no-js -o native",
"build-native-release-with-assertions": "napi build --platform -p next-napi-bindings --cargo-cwd ../../ --cargo-name next_napi_bindings --profile release-with-assertions --features plugin,image-extended,tracing/release_max_level_trace --js false native", "build-native-release-with-assertions": "napi build --platform -p next-napi-bindings --manifest-path ../../Cargo.toml --profile release-with-assertions --features plugin,image-extended,tracing/release_max_level_trace --no-js -o native",
"build-native-no-plugin": "napi build --platform -p next-napi-bindings --cargo-cwd ../../ --cargo-name next_napi_bindings --features image-webp --js false native", "build-native-no-plugin": "napi build --platform -p next-napi-bindings --manifest-path ../../Cargo.toml --features image-webp --no-js -o native",
"build-native-no-plugin-release": "napi build --platform -p next-napi-bindings --cargo-cwd ../../ --cargo-name next_napi_bindings --release --features image-webp,tracing/release_max_level_trace --js false native", "build-native-no-plugin-release": "napi build --platform -p next-napi-bindings --manifest-path ../../Cargo.toml --release --features image-webp,tracing/release_max_level_trace --no-js -o native",
"build-native-wasi": "npx --package=@napi-rs/cli@3.0.0-alpha.45 napi build --platform --target wasm32-wasip1-threads -p next-napi-bindings --cwd ../../ --output-dir packages/next-swc/native --no-default-features", "build-native-wasi": "napi build --platform --target wasm32-wasip1-threads -p next-napi-bindings --manifest-path ../../Cargo.toml -o native --no-default-features",
"build-wasm": "wasm-pack build ../../crates/wasm --scope=next", "build-wasm": "wasm-pack build ../../crates/wasm --scope=next",
"build-docker-image": "node ../../scripts/docker-image-cache.js", "build-docker-image": "node ../../scripts/docker-image-cache.js",
"cache-build-native": "[ -d native ] && echo $(ls native)", "cache-build-native": "[ -d native ] && echo $(ls native)",
...@@ -24,22 +24,22 @@ ...@@ -24,22 +24,22 @@
"test-cargo-unit": "cargo nextest run --workspace --exclude next-napi-bindings --exclude turbo-tasks-macros --cargo-profile release-with-assertions --no-fail-fast" "test-cargo-unit": "cargo nextest run --workspace --exclude next-napi-bindings --exclude turbo-tasks-macros --cargo-profile release-with-assertions --no-fail-fast"
}, },
"napi": { "napi": {
"name": "next-swc", "binaryName": "next-swc",
"triples": { "targets": [
"defaults": true, "x86_64-apple-darwin",
"additional": [ "x86_64-pc-windows-msvc",
"i686-pc-windows-msvc", "x86_64-unknown-linux-gnu",
"aarch64-unknown-linux-gnu", "i686-pc-windows-msvc",
"aarch64-apple-darwin", "aarch64-unknown-linux-gnu",
"x86_64-unknown-linux-musl", "aarch64-apple-darwin",
"aarch64-unknown-linux-musl", "x86_64-unknown-linux-musl",
"aarch64-pc-windows-msvc", "aarch64-unknown-linux-musl",
"wasm32-wasip1-threads" "aarch64-pc-windows-msvc",
] "wasm32-wasip1-threads"
} ]
}, },
"devDependencies": { "devDependencies": {
"@napi-rs/cli": "2.18.4", "@napi-rs/cli": "3.5.1",
"cross-env": "6.0.3", "cross-env": "6.0.3",
"wasm-pack": "0.13.1" "wasm-pack": "0.13.1"
} }
......
This diff is collapsed.
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
ptr,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
...@@ -15,7 +16,10 @@ use lightningcss::{ ...@@ -15,7 +16,10 @@ use lightningcss::{
targets::{Browsers, Features, Targets}, targets::{Browsers, Features, Targets},
visitor::Visit, visitor::Visit,
}; };
use napi::{CallContext, Env, JsObject, JsUnknown}; use napi::{
CallContext, Env, JsObject, JsValue, NapiRaw, Unknown as JsUnknown,
bindgen_prelude::JsObjectValue,
};
use parcel_sourcemap::SourceMap; use parcel_sourcemap::SourceMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
...@@ -40,7 +44,7 @@ struct TransformResult<'i> { ...@@ -40,7 +44,7 @@ struct TransformResult<'i> {
} }
impl<'i> TransformResult<'i> { impl<'i> TransformResult<'i> {
fn into_js(self, env: Env) -> napi::Result<JsUnknown> { fn into_js<'env>(self, env: Env) -> napi::Result<JsUnknown<'env>> {
let mut obj = env.create_object()?; let mut obj = env.create_object()?;
let buf = env.create_buffer_with_data(self.code)?; let buf = env.create_buffer_with_data(self.code)?;
obj.set_named_property("code", buf.into_raw())?; obj.set_named_property("code", buf.into_raw())?;
...@@ -50,7 +54,7 @@ impl<'i> TransformResult<'i> { ...@@ -50,7 +54,7 @@ impl<'i> TransformResult<'i> {
let buf = env.create_buffer_with_data(map)?; let buf = env.create_buffer_with_data(map)?;
buf.into_raw().into_unknown() buf.into_raw().into_unknown()
} else { } else {
env.get_null()?.into_unknown() null(env)?
}, },
)?; )?;
obj.set_named_property("exports", env.to_js_value(&self.exports)?)?; obj.set_named_property("exports", env.to_js_value(&self.exports)?)?;
...@@ -61,6 +65,14 @@ impl<'i> TransformResult<'i> { ...@@ -61,6 +65,14 @@ impl<'i> TransformResult<'i> {
} }
} }
fn null<'env>(env: Env) -> napi::Result<JsUnknown<'env>> {
let mut value = ptr::null_mut();
napi::check_pending_exception!(env.raw(), unsafe {
napi::sys::napi_get_null(env.raw(), &mut value)
})?;
Ok(unsafe { JsUnknown::from_raw_unchecked(env.raw(), value) })
}
fn get_visitor(env: Env, opts: &JsObject) -> Option<JsVisitor> { fn get_visitor(env: Env, opts: &JsObject) -> Option<JsVisitor> {
if let Ok(visitor) = get_named_property::<JsObject>(opts, "visitor") { if let Ok(visitor) = get_named_property::<JsObject>(opts, "visitor") {
Some(JsVisitor::new(env, visitor)) Some(JsVisitor::new(env, visitor))
...@@ -70,8 +82,10 @@ fn get_visitor(env: Env, opts: &JsObject) -> Option<JsVisitor> { ...@@ -70,8 +82,10 @@ fn get_visitor(env: Env, opts: &JsObject) -> Option<JsVisitor> {
} }
pub fn transform(ctx: CallContext) -> napi::Result<JsUnknown> { pub fn transform(ctx: CallContext) -> napi::Result<JsUnknown> {
let opts = ctx.get::<JsObject>(0)?; let opts = ctx.get::<JsUnknown>(0)?;
let mut visitor = get_visitor(*ctx.env, &opts); let mut visitor = unsafe { opts.cast::<JsObject>() }
.ok()
.and_then(|opts| get_visitor(*ctx.env, &opts));
let config: Config = ctx.env.from_js_value(opts)?; let config: Config = ctx.env.from_js_value(opts)?;
let code = unsafe { std::str::from_utf8_unchecked(&config.code) }; let code = unsafe { std::str::from_utf8_unchecked(&config.code) };
...@@ -84,8 +98,10 @@ pub fn transform(ctx: CallContext) -> napi::Result<JsUnknown> { ...@@ -84,8 +98,10 @@ pub fn transform(ctx: CallContext) -> napi::Result<JsUnknown> {
} }
pub fn transform_style_attribute(ctx: CallContext) -> napi::Result<JsUnknown> { pub fn transform_style_attribute(ctx: CallContext) -> napi::Result<JsUnknown> {
let opts = ctx.get::<JsObject>(0)?; let opts = ctx.get::<JsUnknown>(0)?;
let mut visitor = get_visitor(*ctx.env, &opts); let mut visitor = unsafe { opts.cast::<JsObject>() }
.ok()
.and_then(|opts| get_visitor(*ctx.env, &opts));
let config: AttrConfig = ctx.env.from_js_value(opts)?; let config: AttrConfig = ctx.env.from_js_value(opts)?;
let code = unsafe { std::str::from_utf8_unchecked(&config.code) }; let code = unsafe { std::str::from_utf8_unchecked(&config.code) };
...@@ -357,7 +373,7 @@ struct AttrResult<'i> { ...@@ -357,7 +373,7 @@ struct AttrResult<'i> {
} }
impl<'i> AttrResult<'i> { impl<'i> AttrResult<'i> {
fn into_js(self, ctx: CallContext) -> napi::Result<JsUnknown> { fn into_js<'env>(self, ctx: CallContext) -> napi::Result<JsUnknown<'env>> {
let mut obj = ctx.env.create_object()?; let mut obj = ctx.env.create_object()?;
let buf = ctx.env.create_buffer_with_data(self.code)?; let buf = ctx.env.create_buffer_with_data(self.code)?;
obj.set_named_property("code", buf.into_raw())?; obj.set_named_property("code", buf.into_raw())?;
...@@ -460,25 +476,38 @@ impl<'i> CompileError<'i> { ...@@ -460,25 +476,38 @@ impl<'i> CompileError<'i> {
CompileError::ParseError(Error { kind, .. }) => env.to_js_value(kind)?, CompileError::ParseError(Error { kind, .. }) => env.to_js_value(kind)?,
CompileError::PrinterError(Error { kind, .. }) => env.to_js_value(kind)?, CompileError::PrinterError(Error { kind, .. }) => env.to_js_value(kind)?,
CompileError::MinifyError(Error { kind, .. }) => env.to_js_value(kind)?, CompileError::MinifyError(Error { kind, .. }) => env.to_js_value(kind)?,
_ => env.get_null()?.into_unknown(), _ => null(env)?,
}; };
let (js_error, loc) = match self { let (js_error, loc) = match self {
CompileError::ParseError(Error { loc, .. }) CompileError::ParseError(Error { loc, .. })
| CompileError::PrinterError(Error { loc, .. }) | CompileError::PrinterError(Error { loc, .. })
| CompileError::MinifyError(Error { loc, .. }) => { | CompileError::MinifyError(Error { loc, .. }) => {
let syntax_error = env let syntax_error: napi::JsFunction = env
.get_global()? .get_global()?
.get_named_property::<napi::JsFunction>("SyntaxError")?; .get_named_property::<JsUnknown<'_>>("SyntaxError")?
.try_into()?;
let reason = env.create_string_from_std(reason)?; let reason = env.create_string_from_std(reason)?;
let obj = syntax_error.new_instance(&[reason])?; let mut obj = ptr::null_mut();
(obj.into_unknown(), loc) napi::check_pending_exception!(env.raw(), unsafe {
napi::sys::napi_new_instance(
env.raw(),
syntax_error.raw(),
1,
[reason.raw()].as_ptr(),
&mut obj,
)
})?;
(
unsafe { JsUnknown::from_raw_unchecked(env.raw(), obj) },
loc,
)
} }
_ => return Ok(self.into()), _ => return Ok(self.into()),
}; };
if js_error.get_type()? == napi::ValueType::Object { if js_error.get_type()? == napi::ValueType::Object {
let mut obj: JsObject = unsafe { js_error.cast() }; let mut obj: JsObject = unsafe { js_error.cast()? };
if let Some(loc) = loc { if let Some(loc) = loc {
let line = env.create_int32((loc.line + 1) as i32)?; let line = env.create_int32((loc.line + 1) as i32)?;
let col = env.create_int32(loc.column as i32)?; let col = env.create_int32(loc.column as i32)?;
......
use napi::{Error, JsObject, JsUnknown, Result}; use napi::{Error, JsObject, Result, Unknown as JsUnknown};
// Workaround for https://github.com/napi-rs/napi-rs/issues/1641 // Workaround for https://github.com/napi-rs/napi-rs/issues/1641
pub fn get_named_property<T: TryFrom<JsUnknown, Error = Error>>( pub fn get_named_property<T: for<'a> TryFrom<JsUnknown<'a>, Error = Error>>(
obj: &JsObject, obj: &JsObject,
property: &str, property: &str,
) -> Result<T> { ) -> Result<T> {
......
...@@ -2,8 +2,9 @@ use std::{collections::VecDeque, sync::Arc}; ...@@ -2,8 +2,9 @@ use std::{collections::VecDeque, sync::Arc};
use bytes::Bytes; use bytes::Bytes;
use napi::{ use napi::{
Env, Env, Status,
threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode}, bindgen_prelude::Unknown,
threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode},
}; };
use napi_derive::napi; use napi_derive::napi;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
...@@ -16,12 +17,12 @@ use crate::worker_pool::{ ...@@ -16,12 +17,12 @@ use crate::worker_pool::{
operation::{TaskMessage, WORKER_POOL_OPERATION}, operation::{TaskMessage, WORKER_POOL_OPERATION},
}; };
static WORKER_CREATOR: OnceCell<ThreadsafeFunction<NapiWorkerCreation, ErrorStrategy::Fatal>> = type FatalThreadsafeFunction<T> = ThreadsafeFunction<T, Unknown<'static>, T, Status, false>;
OnceCell::new();
static WORKER_CREATOR: OnceCell<FatalThreadsafeFunction<NapiWorkerCreation>> = OnceCell::new();
static WORKER_TERMINATOR: OnceCell< static WORKER_TERMINATOR: OnceCell<FatalThreadsafeFunction<NapiWorkerTermination>> =
ThreadsafeFunction<NapiWorkerTermination, ErrorStrategy::Fatal>, OnceCell::new();
> = OnceCell::new();
static PENDING_CREATIONS: OnceCell<Mutex<VecDeque<oneshot::Sender<u32>>>> = OnceCell::new(); static PENDING_CREATIONS: OnceCell<Mutex<VecDeque<oneshot::Sender<u32>>>> = OnceCell::new();
...@@ -30,8 +31,8 @@ static PENDING_CREATIONS: OnceCell<Mutex<VecDeque<oneshot::Sender<u32>>>> = Once ...@@ -30,8 +31,8 @@ static PENDING_CREATIONS: OnceCell<Mutex<VecDeque<oneshot::Sender<u32>>>> = Once
#[napi] #[napi]
pub fn register_worker_scheduler( pub fn register_worker_scheduler(
env: Env, env: Env,
creator: ThreadsafeFunction<NapiWorkerCreation, ErrorStrategy::Fatal>, creator: FatalThreadsafeFunction<NapiWorkerCreation>,
terminator: ThreadsafeFunction<NapiWorkerTermination, ErrorStrategy::Fatal>, terminator: FatalThreadsafeFunction<NapiWorkerTermination>,
) -> napi::Result<()> { ) -> napi::Result<()> {
// Unref ThreadsafeFunction so it doesn't keep the Node.js event loop alive. // Unref ThreadsafeFunction so it doesn't keep the Node.js event loop alive.
// Call unref on the functions before storing them globally. // Call unref on the functions before storing them globally.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment