Skip to content

Commit 25da3de

Browse files
committed
Merge branch 'main' into inspector_changes2
2 parents 6aed578 + 761b84d commit 25da3de

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

core/extensions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ macro_rules! extension {
501501
// Includes the state and middleware functions, if defined.
502502
#[inline(always)]
503503
#[allow(unused_variables)]
504-
fn with_middleware(ext: &mut $crate::Extension)
504+
fn with_middleware $( < $( $param : $type + 'static ),+ > )?(ext: &mut $crate::Extension)
505505
{
506506
$(
507507
ext.global_template_middleware = ::std::option::Option::Some($global_template_middleware_fn);
@@ -534,7 +534,7 @@ macro_rules! extension {
534534
let mut ext = Self::ext $( ::< $( $param ),+ > )?();
535535
Self::with_ops_fn $( ::< $( $param ),+ > )?(&mut ext);
536536
$crate::extension!(! __config__ ext $( parameters = [ $( $param : $type ),* ] )? $( config = { $( $options_id : $options_type ),* } )? $( state_fn = $state_fn )? );
537-
Self::with_middleware(&mut ext);
537+
Self::with_middleware $( ::< $( $param ),+ > )?(&mut ext);
538538
Self::with_customizer(&mut ext);
539539
ext
540540
}
@@ -553,7 +553,7 @@ macro_rules! extension {
553553
let mut ext = Self::ext $( ::< $( $param ),+ > )?();
554554
Self::with_ops_fn $( ::< $( $param ),+ > )?(&mut ext);
555555
ext.needs_lazy_init = true;
556-
Self::with_middleware(&mut ext);
556+
Self::with_middleware$( ::< $( $param ),+ > )?(&mut ext);
557557
Self::with_customizer(&mut ext);
558558
ext
559559
}

core/runtime/tests/misc.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,74 @@ async fn tla_in_esm_extensions_panics() {
11101110
});
11111111
}
11121112

1113+
#[tokio::test]
1114+
async fn generic_in_extension_middleware() {
1115+
trait WelcomeWorld {
1116+
fn hello(&self) -> String;
1117+
}
1118+
1119+
struct English;
1120+
1121+
impl WelcomeWorld for English {
1122+
fn hello(&self) -> String {
1123+
"Hello World".to_string()
1124+
}
1125+
}
1126+
1127+
#[op2]
1128+
#[string]
1129+
fn say_greeting<W: WelcomeWorld + 'static>(state: &mut OpState) -> String {
1130+
let welcomer = state.borrow::<W>();
1131+
1132+
welcomer.hello()
1133+
}
1134+
1135+
#[op2]
1136+
#[string]
1137+
pub fn say_goodbye() -> String {
1138+
"Goodbye!".to_string()
1139+
}
1140+
1141+
deno_core::extension!(welcome_ext, parameters = [W: WelcomeWorld], ops = [say_greeting<W>, say_goodbye],
1142+
middleware = |op| {
1143+
match op.name {
1144+
"say_goodbye" => say_greeting::<W>(),
1145+
_ => op,
1146+
}
1147+
},
1148+
1149+
);
1150+
1151+
let mut runtime = JsRuntime::new(RuntimeOptions {
1152+
extensions: vec![welcome_ext::init::<English>()],
1153+
..Default::default()
1154+
});
1155+
1156+
{
1157+
let op_state = runtime.op_state();
1158+
let mut state = op_state.borrow_mut();
1159+
1160+
state.put(English);
1161+
}
1162+
1163+
let value_global = runtime
1164+
.execute_script(
1165+
"greet.js",
1166+
r#"
1167+
const greet = Deno.core.ops.say_greeting();
1168+
const bye = Deno.core.ops.say_goodbye();
1169+
greet + " and " + bye;
1170+
"#,
1171+
)
1172+
.unwrap();
1173+
1174+
// Check the result
1175+
let scope = &mut runtime.handle_scope();
1176+
let value = value_global.open(scope);
1177+
1178+
let result = value.to_rust_string_lossy(scope);
1179+
assert_eq!(result, "Hello World and Hello World");
1180+
}
11131181
// TODO(mmastrac): This is only fired in debug mode
11141182
#[cfg(debug_assertions)]
11151183
#[tokio::test]

0 commit comments

Comments
 (0)