From 3b470717cfb3816d5c2c56a4e86320e60477199d Mon Sep 17 00:00:00 2001 From: Benjamin Woodruff Date: Fri, 24 Jan 2025 11:20:26 -0800 Subject: [PATCH] WIP --- .../crates/turbo-tasks-macros/src/func.rs | 51 ++++++++++++------- turbopack/crates/turbo-tasks/src/lib.rs | 1 - .../crates/turbo-tasks/src/macro_helpers.rs | 7 ++- turbopack/crates/turbo-tasks/src/manager.rs | 5 +- .../crates/turbo-tasks/src/native_function.rs | 17 ++++++- turbopack/crates/turbo-tasks/src/registry.rs | 3 +- 6 files changed, 60 insertions(+), 24 deletions(-) diff --git a/turbopack/crates/turbo-tasks-macros/src/func.rs b/turbopack/crates/turbo-tasks-macros/src/func.rs index d2888b1dd4959..980809b983c44 100644 --- a/turbopack/crates/turbo-tasks-macros/src/func.rs +++ b/turbopack/crates/turbo-tasks-macros/src/func.rs @@ -1051,13 +1051,14 @@ pub struct NativeFn { pub function_path_string: String, pub function_path: ExprPath, pub is_method: bool, + pub filter_trait_call_args: Option, pub local: bool, pub local_cells: bool, } impl NativeFn { pub fn ty(&self) -> Type { - parse_quote! { turbo_tasks::NativeFunction } + parse_quote! { turbo_tasks::macro_helpers::NativeFunction } } pub fn definition(&self) -> TokenStream { @@ -1070,24 +1071,40 @@ impl NativeFn { } = self; let constructor = if *is_method { - quote! { new_method } + let arg_meta = if let Some(filter) = self.filter_trait_call_args { + quote! { turbo_tasks::macro_helpers::ArgMeta::with_filter_trait_call_args(#filter) } + } else { + quote! { turbo_tasks::macro_helpers::ArgMeta::new() } + }; + quote! { + { + #[allow(deprecated)] + turbo_tasks::macro_helpers::NativeFunction::new_method( + #function_path_string.to_owned(), + turbo_tasks::macro_helpers::FunctionMeta { + local: #local, + local_cells: #local_cells, + }, + turbo_tasks::macro_helpers::ArgMeta::new(), + #function_path, + ) + } + } } else { - quote! { new_function } - }; - - quote! { - { - #[allow(deprecated)] - turbo_tasks::NativeFunction::#constructor( - #function_path_string.to_owned(), - turbo_tasks::FunctionMeta { - local: #local, - local_cells: #local_cells, - }, - #function_path, - ) + quote! { + { + #[allow(deprecated)] + turbo_tasks::macro_helpers::NativeFunction::new_function( + #function_path_string.to_owned(), + turbo_tasks::macro_helpers::FunctionMeta { + local: #local, + local_cells: #local_cells, + }, + #function_path, + ) + } } - } + }; } pub fn id_ty(&self) -> Type { diff --git a/turbopack/crates/turbo-tasks/src/lib.rs b/turbopack/crates/turbo-tasks/src/lib.rs index 8bcc910a6544e..adaf1e5fc1b1c 100644 --- a/turbopack/crates/turbo-tasks/src/lib.rs +++ b/turbopack/crates/turbo-tasks/src/lib.rs @@ -107,7 +107,6 @@ pub use manager::{ CurrentCellRef, ReadConsistency, TaskPersistence, TurboTasks, TurboTasksApi, TurboTasksBackendApi, TurboTasksBackendApiExt, TurboTasksCallApi, Unused, UpdateInfo, }; -pub use native_function::{FunctionMeta, NativeFunction}; pub use output::OutputContent; pub use raw_vc::{CellId, RawVc, ReadRawVcFuture, ResolveTypeError}; pub use read_ref::ReadRef; diff --git a/turbopack/crates/turbo-tasks/src/macro_helpers.rs b/turbopack/crates/turbo-tasks/src/macro_helpers.rs index 1ccccdd660919..233ddd44dfef8 100644 --- a/turbopack/crates/turbo-tasks/src/macro_helpers.rs +++ b/turbopack/crates/turbo-tasks/src/macro_helpers.rs @@ -11,8 +11,11 @@ pub use super::{ manager::{find_cell_by_type, notify_scheduled_tasks, spawn_detached_for_testing}, }; use crate::{ - debug::ValueDebugFormatString, shrink_to_fit::ShrinkToFit, task::TaskOutput, NonLocalValue, - RawVc, TaskInput, TaskPersistence, Vc, + debug::ValueDebugFormatString, + native_function::{ArgMeta, FunctionMeta, NativeFunction}, + shrink_to_fit::ShrinkToFit, + task::TaskOutput, + NonLocalValue, RawVc, TaskInput, TaskPersistence, Vc, }; #[inline(never)] diff --git a/turbopack/crates/turbo-tasks/src/manager.rs b/turbopack/crates/turbo-tasks/src/manager.rs index ab3da0ddffc62..5eee6565ea6dc 100644 --- a/turbopack/crates/turbo-tasks/src/manager.rs +++ b/turbopack/crates/turbo-tasks/src/manager.rs @@ -37,6 +37,7 @@ use crate::{ }, id_factory::{IdFactory, IdFactoryWithReuse}, magic_any::MagicAny, + native_function::FunctionMeta, raw_vc::{CellId, RawVc}, registry::{self, get_function}, serialization_invalidation::SerializationInvalidator, @@ -48,8 +49,8 @@ use crate::{ trait_helpers::get_trait_method, util::StaticOrArc, vc::ReadVcFuture, - Completion, FunctionMeta, InvalidationReason, InvalidationReasonSet, ResolvedVc, - SharedReference, TaskId, TaskIdSet, ValueTypeId, Vc, VcRead, VcValueTrait, VcValueType, + Completion, InvalidationReason, InvalidationReasonSet, ResolvedVc, SharedReference, TaskId, + TaskIdSet, ValueTypeId, Vc, VcRead, VcValueTrait, VcValueType, }; pub trait TurboTasksCallApi: Sync + Send { diff --git a/turbopack/crates/turbo-tasks/src/native_function.rs b/turbopack/crates/turbo-tasks/src/native_function.rs index d676cd780c3f2..f222cfeb7ae28 100644 --- a/turbopack/crates/turbo-tasks/src/native_function.rs +++ b/turbopack/crates/turbo-tasks/src/native_function.rs @@ -1,4 +1,4 @@ -use std::{fmt::Debug, hash::Hash, pin::Pin}; +use std::{env::args, fmt::Debug, hash::Hash, pin::Pin}; use anyhow::{Context, Result}; use futures::Future; @@ -23,15 +23,28 @@ type ResolveFunctor = type IsResolvedFunctor = fn(&dyn MagicAny) -> bool; +type FilterArgsFunctor = for<'a> fn(Box) -> Box; + pub struct ArgMeta { serializer: MagicAnySerializeSeed, deserializer: MagicAnyDeserializeSeed, is_resolved: IsResolvedFunctor, resolve: ResolveFunctor, + filter_trait_call_args: FilterArgsFunctor, } impl ArgMeta { pub fn new() -> Self + where + T: TaskInput + Serialize + for<'de> Deserialize<'de> + 'static, + { + fn noop_filter_args(args: Box) -> Box { + args + } + Self::with_filter_trait_call_args::(noop_filter_args) + } + + pub fn with_filter_trait_call_args(filter_trait_call_args: FilterArgsFunctor) -> Self where T: TaskInput + Serialize + for<'de> Deserialize<'de> + 'static, { @@ -64,6 +77,7 @@ impl ArgMeta { Ok(Box::new(resolved) as Box) }) }, + filter_trait_call_args, } } @@ -144,6 +158,7 @@ impl NativeFunction { pub fn new_method( name: String, function_meta: FunctionMeta, + arg_meta: ArgMeta, implementation: I, ) -> Self where diff --git a/turbopack/crates/turbo-tasks/src/registry.rs b/turbopack/crates/turbo-tasks/src/registry.rs index cf8c29bd3eeb1..8f8dbc7368466 100644 --- a/turbopack/crates/turbo-tasks/src/registry.rs +++ b/turbopack/crates/turbo-tasks/src/registry.rs @@ -12,8 +12,9 @@ use rustc_hash::FxHasher; use crate::{ id::{FunctionId, TraitTypeId, ValueTypeId}, id_factory::IdFactory, + native_function::NativeFunction, no_move_vec::NoMoveVec, - NativeFunction, TraitType, ValueType, + TraitType, ValueType, }; type FxDashMap = DashMap>;