Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions docs/architecture/winrt-interface-implementations.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,35 @@ Common Python reverse-conversion helpers live once in each generated package's
generated code, not excluded coverage support. Method-specific ABI plans and
typed conversions remain with their interface.

Python handler, delegate, and result annotations share the enclosing module's
type-name mapping with ordinary projected members. Self-interface references use
the locally declared class name; imported structs use the same collision alias
in `.py` and `.pyi`. Types with identical short names in other namespaces remain
distinct imports, including references nested in arrays or delegate signatures.
Self-import filtering compares canonical identities, so a closed generic such
as `IBox<String>` cannot hide a distinct named `IBox_String` in the same namespace.
Standalone interface and runtime-class modules use the actual declared metadata
names for embedded struct helpers, even when their cross-module projected names
are disambiguated; standalone struct modules retain aliases for imported fields.
Python declarations, annotations, imports, and conversion helpers share a
module-local symbol mapping keyed by canonical `TypeIdentity` and symbol role.
Self-class and self-interface references use the actual local declaration;
foreign class `Like` and identity markers import the defining module's symbol
and alias it to the consuming module's reference name. Handler, delegate, array,
and result-dictionary conversions use the same mapping as ordinary members.
Self-import filtering distinguishes namespaces, kinds, and named versus closed
generic types, so `IBox<String>` cannot hide a distinct named `IBox_String`.

Struct modules retain their public `pack_*`, `unpack_*`, and `*_TYPE` names.
When different struct names normalize to the same helper name (for example,
`URLValue` and `UrlValue`), consumers import those helpers under distinct
identity-qualified aliases. Standalone interface and runtime-class modules keep
embedded struct class names and disambiguate only colliding helper symbols;
standalone struct modules retain aliases for imported fields. The existing
fail-closed guard for identical raw struct names in one closure remains.
The allocator also compares helpers with actual local and imported type/marker
symbols, not unrelated types elsewhere in the package. If a foreign type
collides with an owning struct's public helper or type constant, the foreign
import is aliased instead; the struct's isolated public API stays unchanged.
Actual native registration variables also participate in module allocation:
the interface registration, or a class's default/factory/static/required
registrations and its activation factory only when emitted. Registration
declarations and invocation sites read the same symbol roles, independently of
type aliases, so neither an existing helper nor a newly qualified helper alias
can overwrite a registration object.
Enum declarations, namespace exports, defaults, and runtime conversions use the
same projected name, including named/closed-generic collisions; native enum
descriptors continue to use the original metadata identity.

Language bindings additionally gate dispatch on their environment/interpreter
lifetime. Callback roots belong to the native object, not merely the original
Expand Down
27 changes: 16 additions & 11 deletions tools/dynwinrt-codegen/src/codegen/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,30 +597,32 @@ mod tests {

#[test]
fn py_wrap_arg_types() {
let context = PythonProjectionContext::default();
assert_eq!(
py_wrap_arg("s", &TypeMeta::String),
py_wrap_arg("s", &TypeMeta::String, &context),
"DynWinRTValue.from_hstring(s)"
);
assert_eq!(
py_wrap_arg("b", &TypeMeta::Bool),
py_wrap_arg("b", &TypeMeta::Bool, &context),
"DynWinRTValue.from_bool(b)"
);
assert_eq!(
py_wrap_arg("n", &TypeMeta::I32),
py_wrap_arg("n", &TypeMeta::I32, &context),
"DynWinRTValue.from_i32(n)"
);
assert_eq!(
py_wrap_arg("n", &TypeMeta::I64),
py_wrap_arg("n", &TypeMeta::I64, &context),
"DynWinRTValue.from_i64(n)"
);
assert_eq!(
py_wrap_arg("f", &TypeMeta::F64),
py_wrap_arg("f", &TypeMeta::F64, &context),
"DynWinRTValue.from_f64(f)"
);
}

#[test]
fn wraps_python_ireference_inputs() {
let context = PythonProjectionContext::default();
let reference = |inner| TypeMeta::Parameterized {
namespace: "Windows.Foundation".into(),
name: "IReference".into(),
Expand All @@ -629,7 +631,7 @@ mod tests {
};

assert!(
py_wrap_arg("value", &reference(TypeMeta::String))
py_wrap_arg("value", &reference(TypeMeta::String), &context)
.contains("lambda value: DynWinRTValue.from_hstring(value)")
);
assert!(
Expand All @@ -639,7 +641,8 @@ mod tests {
namespace: "Windows.Foundation".into(),
name: "Point".into(),
fields: vec![],
})
}),
&context
)
.contains("lambda value: _pack_point(value).to_value()")
);
Expand All @@ -654,7 +657,8 @@ mod tests {
is_flags: false,
doc: None,
deprecated: None,
})
}),
&context
)
.contains("lambda value: DynWinRTValue.enum_value(")
);
Expand Down Expand Up @@ -792,16 +796,17 @@ mod tests {

#[test]
fn py_struct_field_setter_expressions() {
let context = PythonProjectionContext::default();
assert_eq!(
py_struct_field_setter(&TypeMeta::Bool, 0, "v"),
py_struct_field_setter(&context, &TypeMeta::Bool, 0, "v"),
"s.set_u8(0, 1 if v else 0)"
);
assert_eq!(
py_struct_field_setter(&TypeMeta::I32, 1, "x"),
py_struct_field_setter(&context, &TypeMeta::I32, 1, "x"),
"s.set_i32(1, x)"
);
assert_eq!(
py_struct_field_setter(&TypeMeta::String, 2, "s_"),
py_struct_field_setter(&context, &TypeMeta::String, 2, "s_"),
"s.set_hstring(2, s_)"
);
}
Expand Down
Loading
Loading