1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-01 16:51:13 -05:00

refactor(core): verify there are no ops with duplicate names (#19047)

This commit adds a "debug build" only check that verifies on startup
that there are no duplicate ops (ie. the op names are unique).
This commit is contained in:
Bartek Iwańczuk 2023-05-08 20:59:38 +02:00 committed by David Sherret
parent 4c07550d46
commit 386fa09920

View file

@ -769,7 +769,7 @@ impl JsRuntime {
let macroware = move |d| middleware.iter().fold(d, |d, m| m(d));
// Flatten ops, apply middlware & override disabled ops
exts
let ops: Vec<_> = exts
.iter_mut()
.filter_map(|e| e.init_ops())
.flatten()
@ -777,7 +777,37 @@ impl JsRuntime {
name: d.name,
..macroware(d)
})
.collect()
.collect();
// In debug build verify there are no duplicate ops.
#[cfg(debug_assertions)]
{
let mut count_by_name = HashMap::new();
for op in ops.iter() {
count_by_name
.entry(&op.name)
.or_insert(vec![])
.push(op.name.to_string());
}
let mut duplicate_ops = vec![];
for (op_name, _count) in
count_by_name.iter().filter(|(_k, v)| v.len() > 1)
{
duplicate_ops.push(op_name.to_string());
}
if !duplicate_ops.is_empty() {
let mut msg = "Found ops with duplicate names:\n".to_string();
for op_name in duplicate_ops {
msg.push_str(&format!(" - {}\n", op_name));
}
msg.push_str("Op names need to be unique.");
panic!("{}", msg);
}
}
ops
}
/// Initializes ops of provided Extensions
@ -4787,4 +4817,29 @@ Deno.core.opAsync("op_async_serialize_object_with_numbers_as_keys", {
"Cannot load extension module from external code"
);
}
#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "Found ops with duplicate names:")]
fn duplicate_op_names() {
mod a {
use super::*;
#[op]
fn op_test() -> Result<String, Error> {
Ok(String::from("Test"))
}
}
#[op]
fn op_test() -> Result<String, Error> {
Ok(String::from("Test"))
}
deno_core::extension!(test_ext, ops = [a::op_test, op_test]);
JsRuntime::new(RuntimeOptions {
extensions: vec![test_ext::init_ops()],
..Default::default()
});
}
}