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
311 changes: 246 additions & 65 deletions tools/dynwinrt-codegen/src/codegen/winrt/javascript/project/methods.rs

Large diffs are not rendered by default.

143 changes: 116 additions & 27 deletions tools/dynwinrt-codegen/src/codegen/winrt/javascript/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,19 @@ pub fn project_class(
// Collect delegate names only from interfaces of THIS class (not the entire batch)
// for delegate imports; but also include global delegate_type_names for type filtering
let mut delegate_names: HashSet<String> = HashSet::new();
let mut runtime_delegate_names: HashSet<String> = HashSet::new();
let all_ifaces: Vec<&InterfaceMeta> = class.all_interfaces().collect();
for iface in &all_ifaces {
collect_delegate_names_from_methods(&iface.methods, &mut delegate_names);
for method in &iface.methods {
for param in method
.params
.iter()
.filter(|param| param.direction == ParamDirection::In)
{
let type_name = ts_param_type_dts(&param.typ, known_types);
if delegate_type_names.contains(&type_name) {
delegate_names.insert(type_name);
}
}
}
collect_known_delegate_names_from_methods(
&iface.methods,
delegate_type_names,
&mut delegate_names,
);
collect_runtime_delegate_names_from_methods(
&iface.methods,
delegate_type_names,
&mut runtime_delegate_names,
);
}
// All known delegate type names (used to filter type imports — delegates typed as Interface
// in parameter metadata must not be imported as regular type imports)
Expand Down Expand Up @@ -240,7 +238,7 @@ pub fn project_class(
}

// Delegate imports (runtime only — IID + PARAM_TYPES)
let mut sorted_delegates: Vec<_> = delegate_names.iter().collect();
let mut sorted_delegates: Vec<_> = runtime_delegate_names.iter().collect();
sorted_delegates.sort();
for dname in &sorted_delegates {
imports.push(ProjectedImport {
Expand Down Expand Up @@ -1053,8 +1051,18 @@ pub fn project_interface(
let used_structs = collect_used_structs_from_iface(iface);
let has_structs = !used_structs.is_empty();

let mut delegate_names: HashSet<String> = delegate_type_names.clone();
collect_delegate_names_from_methods(&iface.methods, &mut delegate_names);
let mut delegate_names: HashSet<String> = HashSet::new();
let mut runtime_delegate_names: HashSet<String> = HashSet::new();
collect_known_delegate_names_from_methods(
&iface.methods,
delegate_type_names,
&mut delegate_names,
);
collect_runtime_delegate_names_from_methods(
&iface.methods,
delegate_type_names,
&mut runtime_delegate_names,
);

// Build imports
let mut imports = Vec::new();
Expand All @@ -1073,7 +1081,7 @@ pub fn project_interface(
}
}

let mut sorted_delegates: Vec<_> = delegate_names.iter().collect();
let mut sorted_delegates: Vec<_> = runtime_delegate_names.iter().collect();
sorted_delegates.sort();
for dname in &sorted_delegates {
imports.push(ProjectedImport {
Expand Down Expand Up @@ -1451,29 +1459,110 @@ fn format_type_import_projected(name: &str, kind: TypeKind) -> ProjectedImport {
}
}

fn collect_delegate_names_from_methods(
fn collect_runtime_delegate_names_from_methods(
methods: &[MethodMeta],
known_delegate_names: &HashSet<String>,
delegate_names: &mut HashSet<String>,
) {
for method in methods {
for p in &method.params {
match &p.typ {
for parameter in &method.params {
if parameter.direction != ParamDirection::In {
continue;
}
match &parameter.typ {
TypeMeta::Delegate { name, .. } => {
delegate_names.insert(name.clone());
}
TypeMeta::Interface { name, .. }
if method.is_event_add || method.is_event_remove =>
{
TypeMeta::Interface { name, .. } if known_delegate_names.contains(name) => {
delegate_names.insert(name.clone());
}
TypeMeta::Parameterized { name, args, .. } => {
let concrete = crate::meta::make_parameterized_name(name, args);
if known_delegate_names.contains(&concrete) {
delegate_names.insert(concrete);
}
}
_ => {}
}
if method.is_event_add || method.is_event_remove {
if let TypeMeta::Parameterized { name, args, .. } = &p.typ {
delegate_names.insert(crate::meta::make_parameterized_name(name, args));
}
}
}
}

fn collect_known_delegate_names_from_methods(
methods: &[MethodMeta],
known_delegate_names: &HashSet<String>,
delegate_names: &mut HashSet<String>,
) {
for method in methods {
for parameter in &method.params {
collect_known_delegate_names_from_type(
&parameter.typ,
known_delegate_names,
delegate_names,
);
}
if let Some(return_type) = &method.return_type {
collect_known_delegate_names_from_type(
return_type,
known_delegate_names,
delegate_names,
);
}
}
}

fn collect_known_delegate_names_from_type(
typ: &TypeMeta,
known_delegate_names: &HashSet<String>,
delegate_names: &mut HashSet<String>,
) {
match typ {
TypeMeta::Delegate { name, .. } => {
delegate_names.insert(name.clone());
}
TypeMeta::Interface { name, .. } => {
if known_delegate_names.contains(name) {
delegate_names.insert(name.clone());
}
}
TypeMeta::AsyncActionWithProgress(inner)
| TypeMeta::AsyncOperation(inner)
| TypeMeta::Array(inner) => {
collect_known_delegate_names_from_type(inner, known_delegate_names, delegate_names);
}
TypeMeta::AsyncOperationWithProgress(result, progress) => {
collect_known_delegate_names_from_type(result, known_delegate_names, delegate_names);
collect_known_delegate_names_from_type(progress, known_delegate_names, delegate_names);
}
TypeMeta::Parameterized { name, args, .. } => {
let concrete = crate::meta::make_parameterized_name(name, args);
if known_delegate_names.contains(&concrete) {
delegate_names.insert(concrete);
}
for argument in args {
collect_known_delegate_names_from_type(
argument,
known_delegate_names,
delegate_names,
);
}
}
TypeMeta::RuntimeClass {
default_interface: Some(interface),
..
} => {
collect_known_delegate_names_from_type(interface, known_delegate_names, delegate_names);
}
TypeMeta::Struct { fields, .. } => {
for field in fields {
collect_known_delegate_names_from_type(
&field.typ,
known_delegate_names,
delegate_names,
);
}
}
_ => {}
}
}

Expand Down
25 changes: 11 additions & 14 deletions tools/dynwinrt-codegen/src/codegen/winrt/python/generator/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,18 @@ pub fn generate_class(
let mut type_checking_imports = Vec::new();

// Collect delegate names from all interfaces of this class
let mut delegate_names: HashSet<String> = delegate_type_names.clone();
let mut delegate_names = HashSet::new();
let mut runtime_delegate_names = HashSet::new();
let all_ifaces: Vec<&InterfaceMeta> = class.all_interfaces().collect();
for iface in &all_ifaces {
for method in &iface.methods {
for p in &method.params {
if let TypeMeta::Delegate { name, .. } = &p.typ {
delegate_names.insert(name.clone());
}
if method.is_event_add || method.is_event_remove {
if let TypeMeta::Parameterized { name, args, .. } = &p.typ {
delegate_names.insert(crate::meta::make_parameterized_name(name, args));
}
}
}
}
delegate_names.extend(super::super::collect_referenced_delegate_names(
&iface.methods,
delegate_type_names,
));
runtime_delegate_names.extend(super::super::collect_runtime_delegate_names(
&iface.methods,
delegate_type_names,
));
}

// Collection generics import (skip delegates)
Expand All @@ -125,7 +122,7 @@ pub fn generate_class(
}

// Import delegate IID + PARAM_TYPES
let mut sorted_delegates: Vec<_> = delegate_names.iter().collect();
let mut sorted_delegates: Vec<_> = runtime_delegate_names.iter().collect();
sorted_delegates.sort();
for dname in &sorted_delegates {
let module = to_snake_case_filename(dname);
Expand Down
19 changes: 5 additions & 14 deletions tools/dynwinrt-codegen/src/codegen/winrt/python/generator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,10 @@ pub fn generate_interface(
let mut type_checking_imports = Vec::new();

// Collect delegate names
let mut delegate_names: HashSet<String> = delegate_type_names.clone();
for method in &iface.methods {
for p in &method.params {
if let TypeMeta::Delegate { name, .. } = &p.typ {
delegate_names.insert(name.clone());
}
if method.is_event_add || method.is_event_remove {
if let TypeMeta::Parameterized { name, args, .. } = &p.typ {
delegate_names.insert(crate::meta::make_parameterized_name(name, args));
}
}
}
}
let delegate_names =
super::super::collect_referenced_delegate_names(&iface.methods, delegate_type_names);
let runtime_delegate_names =
super::super::collect_runtime_delegate_names(&iface.methods, delegate_type_names);

// Import parameterized collection types (skip delegates)
let collection_names = collect_used_generics_from_methods(&iface.methods);
Expand Down Expand Up @@ -153,7 +144,7 @@ pub fn generate_interface(
}

// Import delegate IID + PARAM_TYPES
let mut sorted_delegates: Vec<_> = delegate_names.iter().collect();
let mut sorted_delegates: Vec<_> = runtime_delegate_names.iter().collect();
sorted_delegates.sort();
for dname in &sorted_delegates {
let module = to_snake_case_filename(dname);
Expand Down
64 changes: 55 additions & 9 deletions tools/dynwinrt-codegen/src/codegen/winrt/python/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,35 @@ use crate::codegen::winrt::shared::imports::{
use super::naming::to_snake_case;
use super::signature::{
py_convert_return, py_runtime_symbol, py_type_guard, py_wrap_arg, py_wrap_async,
py_wrap_async_with_converters,
};
use super::type_helpers::{
method_pydoc, py_delegate_callable_type, py_factory_return_type, py_method_abi_output_count,
py_method_outputs, py_method_return_type, py_param_list, py_return_type_safe,
py_method_outputs, py_method_return_type, py_output_type, py_param_list,
};

fn is_delegate_type(typ: &TypeMeta, delegate_type_names: &HashSet<String>) -> bool {
delegate_name(typ, delegate_type_names).is_some()
}

fn delegate_value_converter(
typ: &TypeMeta,
delegate_type_names: &HashSet<String>,
) -> Option<String> {
if is_delegate_type(typ, delegate_type_names) {
return Some("lambda value: None if value.is_null() else value".into());
}
if let TypeMeta::Array(inner) = typ
&& is_delegate_type(inner, delegate_type_names)
{
return Some(
"lambda value: [None if item.is_null() else item for item in value.as_array().to_values()]"
.into(),
);
}
None
}

/// Build a Python callback signature + wrapper expression for an event delegate.
///
/// Returns `(signature, wrapper)`:
Expand Down Expand Up @@ -143,8 +162,38 @@ fn convert_method_output(
known_types: &HashSet<String>,
delegate_type_names: &HashSet<String>,
) -> String {
if is_delegate_type(typ, delegate_type_names) {
return format!("(lambda value: None if value.is_null() else value)({expr})");
if let Some(converter) = delegate_value_converter(typ, delegate_type_names) {
return format!("({converter})({expr})");
}
match typ {
TypeMeta::AsyncOperation(result) => {
return py_wrap_async_with_converters(
expr,
typ,
delegate_value_converter(result, delegate_type_names),
None,
known_types,
);
}
TypeMeta::AsyncActionWithProgress(progress) => {
return py_wrap_async_with_converters(
expr,
typ,
None,
delegate_value_converter(progress, delegate_type_names),
known_types,
);
}
TypeMeta::AsyncOperationWithProgress(result, progress) => {
return py_wrap_async_with_converters(
expr,
typ,
delegate_value_converter(result, delegate_type_names),
delegate_value_converter(progress, delegate_type_names),
known_types,
);
}
_ => {}
}
py_convert_return(expr, Some(typ), typ.is_async(), known_types)
}
Expand Down Expand Up @@ -751,12 +800,9 @@ pub(crate) fn generate_method_body(

if method.is_property_getter && in_params.is_empty() {
let prop_name = to_snake_case(method.name.strip_prefix("get_").unwrap_or(&method.name));
let py_return = if return_type.is_some_and(|typ| is_delegate_type(typ, delegate_type_names))
{
"DynWinRTValue | None".to_string()
} else {
py_return_type_safe(return_type, known_types)
};
let py_return = return_type
.map(|typ| py_output_type(typ, known_types, delegate_type_names))
.unwrap_or_else(|| "None".to_string());
out.push_str(" @_property\n");
out.push_str(&format!(" def {}(self) -> {}:\n", prop_name, py_return));
out.push_str(&method_pydoc(method, &in_params));
Expand Down
Loading
Loading