Skip to content

Commit 6bb0e60

Browse files
authored
Merge the Callback and Function type
There is no need for two of these This simplify some code Amend efdecf0
1 parent dff19c5 commit 6bb0e60

File tree

15 files changed

+72
-135
lines changed

15 files changed

+72
-135
lines changed

api/node/rust/interpreter/component_instance.rs

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,13 @@ impl JsComponentInstance {
160160
}
161161
};
162162

163-
if let Some(return_type) = &return_type {
164-
if let Ok(value) = super::to_value(&env, result, return_type) {
165-
return value;
166-
} else {
167-
eprintln!(
168-
"Node.js: cannot convert return type of callback {}",
169-
callback_name
170-
);
171-
return slint_interpreter::default_value_for_type(return_type);
172-
}
173-
} else {
163+
if matches!(return_type, Type::Void) {
174164
Value::Void
165+
} else if let Ok(value) = super::to_value(&env, result, &return_type) {
166+
return value;
167+
} else {
168+
eprintln!("Node.js: cannot convert return type of callback {callback_name}");
169+
return slint_interpreter::default_value_for_type(&return_type);
175170
}
176171
}
177172
})
@@ -237,18 +232,13 @@ impl JsComponentInstance {
237232
}
238233
};
239234

240-
if let Some(return_type) = &return_type {
241-
if let Ok(value) = super::to_value(&env, result, return_type) {
242-
return value;
243-
} else {
244-
eprintln!(
245-
"Node.js: cannot convert return type of callback {}",
246-
callback_name
247-
);
248-
return slint_interpreter::default_value_for_type(return_type);
249-
}
250-
} else {
235+
if matches!(return_type, Type::Void) {
251236
Value::Void
237+
} else if let Ok(value) = super::to_value(&env, result, &return_type) {
238+
return value;
239+
} else {
240+
eprintln!("Node.js: cannot convert return type of callback {callback_name}");
241+
return slint_interpreter::default_value_for_type(&return_type);
252242
}
253243
}
254244
})
@@ -306,10 +296,7 @@ impl JsComponentInstance {
306296
})?;
307297

308298
let args = match ty {
309-
Type::Callback(callback) => {
310-
Self::invoke_args(env, &callback_name, arguments, &callback.args)?
311-
}
312-
Type::Function(function) => {
299+
Type::Callback(function) | Type::Function(function) => {
313300
Self::invoke_args(env, &callback_name, arguments, &function.args)?
314301
}
315302
_ => {
@@ -352,10 +339,7 @@ impl JsComponentInstance {
352339
})?;
353340

354341
let args = match ty {
355-
Type::Callback(callback) => {
356-
Self::invoke_args(env, &callback_name, arguments, &callback.args)?
357-
}
358-
Type::Function(function) => {
342+
Type::Callback(function) | Type::Function(function) => {
359343
Self::invoke_args(env, &callback_name, arguments, &function.args)?
360344
}
361345
_ => {

internal/compiler/expression_tree.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,7 @@ impl Expression {
706706
Expression::Cast { to, .. } => to.clone(),
707707
Expression::CodeBlock(sub) => sub.last().map_or(Type::Void, |e| e.ty()),
708708
Expression::FunctionCall { function, .. } => match function.ty() {
709-
Type::Function(function) => function.return_type.clone(),
710-
Type::Callback(callback) => {
711-
callback.return_type.as_ref().unwrap_or(&Type::Void).clone()
712-
}
709+
Type::Function(f) | Type::Callback(f) => f.return_type.clone(),
713710
_ => Type::Invalid,
714711
},
715712
Expression::SelfAssignment { .. } => Type::Void,

internal/compiler/generator/cpp.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ fn handle_property_init(
626626
code = return_compile_expression(
627627
&binding_expression.expression.borrow(),
628628
&ctx2,
629-
callback.return_type.as_ref()
629+
Some(&callback.return_type)
630630
)
631631
));
632632
} else {
@@ -1861,10 +1861,7 @@ fn generate_sub_component(
18611861
let ty = if let Type::Callback(callback) = &property.ty {
18621862
let param_types =
18631863
callback.args.iter().map(|t| t.cpp_type().unwrap()).collect::<Vec<_>>();
1864-
let return_type = callback
1865-
.return_type
1866-
.as_ref()
1867-
.map_or(SmolStr::new_static("void"), |t| t.cpp_type().unwrap());
1864+
let return_type = callback.return_type.cpp_type().unwrap();
18681865
format_smolstr!(
18691866
"slint::private_api::Callback<{}({})>",
18701867
return_type,
@@ -2503,13 +2500,9 @@ fn generate_global(
25032500
let ty = if let Type::Callback(callback) = &property.ty {
25042501
let param_types =
25052502
callback.args.iter().map(|t| t.cpp_type().unwrap()).collect::<Vec<_>>();
2506-
let return_type = callback
2507-
.return_type
2508-
.as_ref()
2509-
.map_or(SmolStr::new_static("void"), |t| t.cpp_type().unwrap());
25102503
format_smolstr!(
25112504
"slint::private_api::Callback<{}({})>",
2512-
return_type,
2505+
callback.return_type.cpp_type().unwrap(),
25132506
param_types.join(", ")
25142507
)
25152508
} else {
@@ -2640,8 +2633,6 @@ fn generate_public_api_for_properties(
26402633
if let Type::Callback(callback) = &p.ty {
26412634
let param_types =
26422635
callback.args.iter().map(|t| t.cpp_type().unwrap()).collect::<Vec<_>>();
2643-
let return_type =
2644-
callback.return_type.as_ref().map_or("void".into(), |t| t.cpp_type().unwrap());
26452636
let callback_emitter = vec![
26462637
"[[maybe_unused]] auto self = this;".into(),
26472638
format!(
@@ -2661,7 +2652,7 @@ fn generate_public_api_for_properties(
26612652
.enumerate()
26622653
.map(|(i, ty)| format!("{} arg_{}", ty, i))
26632654
.join(", "),
2664-
return_type
2655+
callback.return_type.cpp_type().unwrap()
26652656
),
26662657
statements: Some(callback_emitter),
26672658
..Default::default()

internal/compiler/generator/rust.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,7 @@ fn handle_property_init(
466466
ctx2.argument_types = &callback.args;
467467
let tokens_for_expression =
468468
compile_expression(&binding_expression.expression.borrow(), &ctx2);
469-
let as_ = if callback.return_type.as_ref().map_or(true, |t| matches!(t, Type::Void)) {
470-
quote!(;)
471-
} else {
472-
quote!(as _)
473-
};
469+
let as_ = if matches!(callback.return_type, Type::Void) { quote!(;) } else { quote!(as _) };
474470
init.push(quote!({
475471
#[allow(unreachable_code, unused)]
476472
slint::private_unstable_api::set_callback_handler(#rust_property, &self_rc, {
@@ -553,10 +549,7 @@ fn public_api(
553549
if let Type::Callback(callback) = &p.ty {
554550
let callback_args =
555551
callback.args.iter().map(|a| rust_primitive_type(a).unwrap()).collect::<Vec<_>>();
556-
let return_type = callback
557-
.return_type
558-
.as_ref()
559-
.map_or(quote!(()), |a| rust_primitive_type(a).unwrap());
552+
let return_type = rust_primitive_type(&callback.return_type).unwrap();
560553
let args_name =
561554
(0..callback.args.len()).map(|i| format_ident!("arg_{}", i)).collect::<Vec<_>>();
562555
let caller_ident = format_ident!("invoke_{}", prop_ident);
@@ -684,10 +677,7 @@ fn generate_sub_component(
684677
if let Type::Callback(callback) = &property.ty {
685678
let callback_args =
686679
callback.args.iter().map(|a| rust_primitive_type(a).unwrap()).collect::<Vec<_>>();
687-
let return_type = callback
688-
.return_type
689-
.as_ref()
690-
.map_or(quote!(()), |a| rust_primitive_type(a).unwrap());
680+
let return_type = rust_primitive_type(&callback.return_type).unwrap();
691681
declared_callbacks.push(prop_ident.clone());
692682
declared_callbacks_types.push(callback_args);
693683
declared_callbacks_ret.push(return_type);
@@ -1312,10 +1302,7 @@ fn generate_global(global: &llr::GlobalComponent, root: &llr::CompilationUnit) -
13121302
if let Type::Callback(callback) = &property.ty {
13131303
let callback_args =
13141304
callback.args.iter().map(|a| rust_primitive_type(a).unwrap()).collect::<Vec<_>>();
1315-
let return_type = callback
1316-
.return_type
1317-
.as_ref()
1318-
.map_or(quote!(()), |a| rust_primitive_type(a).unwrap());
1305+
let return_type = rust_primitive_type(&callback.return_type);
13191306
declared_callbacks.push(prop_ident.clone());
13201307
declared_callbacks_types.push(callback_args);
13211308
declared_callbacks_ret.push(return_type);

internal/compiler/langtype.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub enum Type {
2727
/// The type of a callback alias whose type was not yet inferred
2828
InferredCallback,
2929

30-
Callback(Rc<Callback>),
30+
Callback(Rc<Function>),
3131
Function(Rc<Function>),
3232

3333
ComponentFactory,
@@ -127,9 +127,7 @@ impl Display for Type {
127127
}
128128
write!(f, ")")?
129129
}
130-
if let Some(rt) = &callback.return_type {
131-
write!(f, "-> {}", rt)?;
132-
}
130+
write!(f, "-> {}", callback.return_type)?;
133131
Ok(())
134132
}
135133
Type::ComponentFactory => write!(f, "component-factory"),
@@ -782,12 +780,6 @@ impl<'a> PropertyLookupResult<'a> {
782780
}
783781
}
784782

785-
#[derive(Debug, Clone, PartialEq)]
786-
pub struct Callback {
787-
pub return_type: Option<Type>,
788-
pub args: Vec<Type>,
789-
}
790-
791783
#[derive(Debug, Clone, PartialEq)]
792784
pub struct Function {
793785
pub return_type: Type,

internal/compiler/llr/expression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl Expression {
273273
},
274274
Self::CallBackCall { callback, .. } => {
275275
if let Type::Callback(callback) = ctx.property_ty(callback) {
276-
callback.return_type.as_ref().unwrap_or(&Type::Void).clone()
276+
callback.return_type.clone()
277277
} else {
278278
Type::Invalid
279279
}

internal/compiler/load_builtins.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use std::rc::Rc;
1212

1313
use crate::expression_tree::{BuiltinFunction, Expression};
1414
use crate::langtype::{
15-
BuiltinElement, BuiltinPropertyDefault, BuiltinPropertyInfo, Callback, DefaultSizeBinding,
16-
ElementType, NativeClass, Type,
15+
BuiltinElement, BuiltinPropertyDefault, BuiltinPropertyInfo, DefaultSizeBinding, ElementType,
16+
Function, NativeClass, Type,
1717
};
1818
use crate::object_tree::{self, *};
1919
use crate::parser::{identifier_text, syntax_nodes, SyntaxKind, SyntaxNode};
@@ -103,7 +103,7 @@ pub(crate) fn load_builtins(register: &mut TypeRegister) {
103103
.chain(e.CallbackDeclaration().map(|s| {
104104
(
105105
identifier_text(&s.DeclaredIdentifier()).unwrap(),
106-
BuiltinPropertyInfo::new(Type::Callback(Rc::new(Callback{
106+
BuiltinPropertyInfo::new(Type::Callback(Rc::new(Function{
107107
args: s
108108
.CallbackDeclarationParameter()
109109
.map(|a| {
@@ -116,7 +116,7 @@ pub(crate) fn load_builtins(register: &mut TypeRegister) {
116116
*diag.borrow_mut(),
117117
register,
118118
)
119-
}),
119+
}).unwrap_or(Type::Void),
120120
}))),
121121
)
122122
}))

internal/compiler/lookup.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ impl<'a> LookupCtx<'a> {
6565

6666
pub fn return_type(&self) -> &Type {
6767
match &self.property_type {
68-
Type::Callback(callback) => callback.return_type.as_ref().unwrap_or(&Type::Void),
69-
Type::Function(function) => &function.return_type,
68+
Type::Callback(f) | Type::Function(f) => &f.return_type,
7069
_ => &self.property_type,
7170
}
7271
}
@@ -194,8 +193,7 @@ impl LookupObject for ArgumentsLookup {
194193
f: &mut impl FnMut(&str, LookupResult) -> Option<R>,
195194
) -> Option<R> {
196195
let args = match &ctx.property_type {
197-
Type::Callback(callback) => &callback.args,
198-
Type::Function(function) => &function.args,
196+
Type::Callback(f) | Type::Function(f) => &f.args,
199197
_ => return None,
200198
};
201199
for (index, (name, ty)) in ctx.arguments.iter().zip(args.iter()).enumerate() {

internal/compiler/object_tree.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99

1010
use crate::diagnostics::{BuildDiagnostics, SourceLocation, Spanned};
1111
use crate::expression_tree::{self, BindingExpression, Expression, Unit};
12-
use crate::langtype::EnumerationValue;
1312
use crate::langtype::{
14-
BuiltinElement, BuiltinPropertyDefault, Callback, Enumeration, Function, NativeClass, Struct,
15-
Type,
13+
BuiltinElement, BuiltinPropertyDefault, Enumeration, EnumerationValue, Function, NativeClass,
14+
Struct, Type,
1615
};
1716
use crate::langtype::{ElementType, PropertyLookupResult};
1817
use crate::layout::{LayoutConstraints, Orientation};
@@ -1156,12 +1155,14 @@ impl Element {
11561155
}
11571156
type_from_node(p.Type(), diag, tr)})
11581157
.collect();
1159-
let return_type =
1160-
sig_decl.ReturnType().map(|ret_ty| type_from_node(ret_ty.Type(), diag, tr));
1158+
let return_type = sig_decl
1159+
.ReturnType()
1160+
.map(|ret_ty| type_from_node(ret_ty.Type(), diag, tr))
1161+
.unwrap_or(Type::Void);
11611162
r.property_declarations.insert(
11621163
name,
11631164
PropertyDeclaration {
1164-
property_type: Type::Callback(Rc::new(Callback { return_type, args })),
1165+
property_type: Type::Callback(Rc::new(Function { return_type, args })),
11651166
node: Some(sig_decl.into()),
11661167
visibility: PropertyVisibility::InOut,
11671168
pure,

internal/compiler/passes/collect_structs_and_enums.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,7 @@ fn visit_declared_type(ty: &Type, visitor: &mut impl FnMut(&SmolStr, &Type)) {
9191
}
9292
}
9393
Type::Array(x) => visit_declared_type(x, visitor),
94-
Type::Callback(callback) => {
95-
if let Some(rt) = &callback.return_type {
96-
visit_declared_type(rt, visitor);
97-
}
98-
for a in &callback.args {
99-
visit_declared_type(a, visitor);
100-
}
101-
}
102-
Type::Function(function) => {
94+
Type::Function(function) | Type::Callback(function) => {
10395
visit_declared_type(&function.return_type, visitor);
10496
for a in &function.args {
10597
visit_declared_type(a, visitor);

0 commit comments

Comments
 (0)