Unverified Commit 8fa42dee authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

refactor(napi): Implement napi traits for RcStr (#79806)

This lets us use `RcStr` directly in function arguments and as part of `#[napi(object)]`s.

*You can just `RcStr` things!*

Here's an example of rspack doing something similar for a custom napi type: https://github.com/web-infra-dev/rspack/blob/35f1126963d5ba6860e9eba939fd5d62f9aa217f/crates/rspack_regex/src/napi.rs
parent 42487f26
...@@ -4587,6 +4587,7 @@ name = "next-build" ...@@ -4587,6 +4587,7 @@ name = "next-build"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"next-core", "next-core",
"turbo-rcstr",
"turbo-tasks-build", "turbo-tasks-build",
"turbopack-core", "turbopack-core",
] ]
...@@ -9558,6 +9559,7 @@ name = "turbo-rcstr" ...@@ -9558,6 +9559,7 @@ name = "turbo-rcstr"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"codspeed-criterion-compat", "codspeed-criterion-compat",
"napi",
"new_debug_unreachable", "new_debug_unreachable",
"serde", "serde",
"shrink-to-fit", "shrink-to-fit",
...@@ -10169,6 +10171,7 @@ version = "0.1.0" ...@@ -10169,6 +10171,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"turbo-rcstr",
"turbopack-cli-utils", "turbopack-cli-utils",
"turbopack-core", "turbopack-core",
] ]
......
...@@ -251,7 +251,7 @@ next-custom-transforms = { path = "crates/next-custom-transforms" } ...@@ -251,7 +251,7 @@ next-custom-transforms = { path = "crates/next-custom-transforms" }
auto-hash-map = { path = "turbopack/crates/turbo-tasks-auto-hash-map" } auto-hash-map = { path = "turbopack/crates/turbo-tasks-auto-hash-map" }
swc-ast-explorer = { path = "turbopack/crates/turbopack-swc-ast-explorer" } swc-ast-explorer = { path = "turbopack/crates/turbopack-swc-ast-explorer" }
turbo-prehash = { path = "turbopack/crates/turbo-prehash" } turbo-prehash = { path = "turbopack/crates/turbo-prehash" }
turbo-rcstr = { path = "turbopack/crates/turbo-rcstr" } turbo-rcstr = { path = "turbopack/crates/turbo-rcstr", features = ["napi"] }
turbo-esregex = { path = "turbopack/crates/turbo-esregex" } turbo-esregex = { path = "turbopack/crates/turbo-esregex" }
turbo-persistence = { path = "turbopack/crates/turbo-persistence" } turbo-persistence = { path = "turbopack/crates/turbo-persistence" }
turbo-tasks-malloc = { path = "turbopack/crates/turbo-tasks-malloc", default-features = false } turbo-tasks-malloc = { path = "turbopack/crates/turbo-tasks-malloc", default-features = false }
...@@ -370,6 +370,16 @@ lightningcss-napi = { version = "0.4.4", default-features = false, features = [ ...@@ -370,6 +370,16 @@ lightningcss-napi = { version = "0.4.4", default-features = false, features = [
]} ]}
markdown = "1.0.0-alpha.18" markdown = "1.0.0-alpha.18"
mime = "0.3.16" mime = "0.3.16"
napi = { version = "2", default-features = false, features = [
"napi3",
"serde-json",
"tokio_rt",
"error_anyhow",
# Lightningcss uses this features
"napi4",
"napi5",
"compat-mode"
] }
nohash-hasher = "0.2.0" nohash-hasher = "0.2.0"
notify = "8.0.0" notify = "8.0.0"
once_cell = "1.17.1" once_cell = "1.17.1"
......
...@@ -50,16 +50,7 @@ console-subscriber = { workspace = true, optional = true } ...@@ -50,16 +50,7 @@ console-subscriber = { workspace = true, optional = true }
dhat = { workspace = true, optional = true } dhat = { workspace = true, optional = true }
indexmap = { workspace = true } indexmap = { workspace = true }
owo-colors = { workspace = true } owo-colors = { workspace = true }
napi = { version = "2", default-features = false, features = [ napi = { workspace = true }
"napi3",
"serde-json",
"tokio_rt",
"error_anyhow",
# Lightningcss uses this features
"napi4",
"napi5",
"compat-mode"
] }
napi-derive = "2" napi-derive = "2"
next-custom-transforms = { workspace = true } next-custom-transforms = { workspace = true }
rand = { workspace = true } rand = { workspace = true }
......
This diff is collapsed.
...@@ -15,6 +15,7 @@ workspace = true ...@@ -15,6 +15,7 @@ workspace = true
[dependencies] [dependencies]
next-core = { workspace = true } next-core = { workspace = true }
turbopack-core = { workspace = true } turbopack-core = { workspace = true }
turbo-rcstr = { workspace = true }
[build-dependencies] [build-dependencies]
turbo-tasks-build = { workspace = true } turbo-tasks-build = { workspace = true }
use std::path::PathBuf; use std::path::PathBuf;
use next_core::next_config::Rewrites; use next_core::next_config::Rewrites;
use turbo_rcstr::RcStr;
use turbopack_core::issue::IssueSeverity; use turbopack_core::issue::IssueSeverity;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
...@@ -46,7 +47,7 @@ pub struct BuildContext { ...@@ -46,7 +47,7 @@ pub struct BuildContext {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct DefineEnv { pub struct DefineEnv {
pub client: Vec<(String, String)>, pub client: Vec<(RcStr, RcStr)>,
pub edge: Vec<(String, String)>, pub edge: Vec<(RcStr, RcStr)>,
pub nodejs: Vec<(String, String)>, pub nodejs: Vec<(RcStr, RcStr)>,
} }
...@@ -18,8 +18,9 @@ ...@@ -18,8 +18,9 @@
"name": "next" "name": "next"
} }
], ],
"strictNullChecks": true "strictNullChecks": true,
"target": "ES2017"
}, },
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"], "include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"] "exclude": ["node_modules", "**/*.test.ts", "**/*.test.tsx"]
} }
...@@ -5,8 +5,9 @@ edition = "2024" ...@@ -5,8 +5,9 @@ edition = "2024"
license = "MIT" license = "MIT"
[features] [features]
atom_size_64=[] atom_size_64 = []
atom_size_128=[] atom_size_128 = []
napi = ["dep:napi"]
[dependencies] [dependencies]
triomphe = { workspace = true } triomphe = { workspace = true }
...@@ -14,6 +15,7 @@ turbo-tasks-hash = { workspace = true } ...@@ -14,6 +15,7 @@ turbo-tasks-hash = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
new_debug_unreachable = "1.0.6" new_debug_unreachable = "1.0.6"
shrink-to-fit = { workspace = true } shrink-to-fit = { workspace = true }
napi = { workspace = true, optional = true }
[dev-dependencies] [dev-dependencies]
criterion = { workspace = true } criterion = { workspace = true }
......
...@@ -307,6 +307,46 @@ impl ShrinkToFit for RcStr { ...@@ -307,6 +307,46 @@ impl ShrinkToFit for RcStr {
fn shrink_to_fit(&mut self) {} fn shrink_to_fit(&mut self) {}
} }
#[cfg(feature = "napi")]
mod napi_impl {
use napi::{
bindgen_prelude::{FromNapiValue, ToNapiValue, TypeName, ValidateNapiValue},
sys::{napi_env, napi_value},
};
use super::*;
impl TypeName for RcStr {
fn type_name() -> &'static str {
String::type_name()
}
fn value_type() -> napi::ValueType {
String::value_type()
}
}
impl ToNapiValue for RcStr {
unsafe fn to_napi_value(env: napi_env, val: Self) -> napi::Result<napi_value> {
unsafe { ToNapiValue::to_napi_value(env, val.as_str()) }
}
}
impl FromNapiValue for RcStr {
unsafe fn from_napi_value(env: napi_env, napi_val: napi_value) -> napi::Result<Self> {
Ok(RcStr::from(unsafe {
String::from_napi_value(env, napi_val)
}?))
}
}
impl ValidateNapiValue for RcStr {
unsafe fn validate(env: napi_env, napi_val: napi_value) -> napi::Result<napi_value> {
unsafe { String::validate(env, napi_val) }
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
......
...@@ -16,5 +16,6 @@ workspace = true ...@@ -16,5 +16,6 @@ workspace = true
serde = { workspace = true } serde = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }
turbo-rcstr = { workspace = true }
turbopack-cli-utils = { workspace = true } turbopack-cli-utils = { workspace = true }
turbopack-core = { workspace = true } turbopack-core = { workspace = true }
...@@ -2,6 +2,7 @@ use std::{collections::BTreeMap, fmt::Display, ops::Deref, path::PathBuf}; ...@@ -2,6 +2,7 @@ use std::{collections::BTreeMap, fmt::Display, ops::Deref, path::PathBuf};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
use turbo_rcstr::RcStr;
use turbopack_cli_utils::issue::{LogOptions, format_issue}; use turbopack_cli_utils::issue::{LogOptions, format_issue};
use turbopack_core::{ use turbopack_core::{
issue::{IssueSeverity, IssueStage, PlainIssue, StyledString}, issue::{IssueSeverity, IssueStage, PlainIssue, StyledString},
...@@ -10,8 +11,8 @@ use turbopack_core::{ ...@@ -10,8 +11,8 @@ use turbopack_core::{
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct ResourceIdentifier { pub struct ResourceIdentifier {
pub path: String, pub path: RcStr,
pub headers: Option<BTreeMap<String, String>>, pub headers: Option<BTreeMap<RcStr, RcStr>>,
} }
impl Display for ResourceIdentifier { impl Display for ResourceIdentifier {
......
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