Skip to content

Commit 4d241db

Browse files
Resource constructors
[skip ci]
1 parent 30367fc commit 4d241db

2 files changed

Lines changed: 46 additions & 35 deletions

File tree

crates/d/src/lib.rs

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,11 @@ fn escape_d_identifier(name: &str) -> &str {
211211
"WitFlags" => "WitFlags_",
212212
"Option" => "Option_",
213213
"Result" => "Result_",
214-
"bits" => "bits_", // part of WitFlags
215-
"borrow" => "borrow_", // part of the expansion of `resource`
216-
"drop" => "drop_", // part of the expansion of `resource`
214+
"bits" => "bits_", // part of WitFlags
215+
"borrow" => "borrow_", // part of the expansion of `resource`
216+
"drop" => "drop_", // part of the expansion of `resource`
217+
"makeNew" => "makeNew_", // part of the expansion of `resource`
218+
"constructor" => "constructor_", // part of the expansion of `resource`
217219

218220
s => s,
219221
}
@@ -893,15 +895,15 @@ impl<'a> DInterfaceGenerator<'a> {
893895
self.type_name(&ty, from_module_fqn)
894896
)),
895897
TypeDefKind::Future(_) => {
896-
Cow::Borrowed("/* todo - type_name of `future` */")
898+
todo!("type_name of `future`")
897899
}
898900
TypeDefKind::Stream(_) => {
899-
Cow::Borrowed("/* todo - type_name of `stream` */")
901+
todo!("type_name of `stream`")
900902
}
901903
TypeDefKind::FixedLengthList(ty, size) => {
902904
Cow::Owned(format!("{}[{size}]", self.type_name(ty, from_module_fqn)))
903905
}
904-
TypeDefKind::Map(_, _) => todo!(),
906+
TypeDefKind::Map(_, _) => todo!("type_name of `map`"),
905907
TypeDefKind::Unknown => unimplemented!(),
906908
unhandled => {
907909
panic!(
@@ -1027,10 +1029,12 @@ impl<'a> DInterfaceGenerator<'a> {
10271029

10281030
fn get_d_signature(&mut self, func: &Function) -> DSig {
10291031
match &func.kind {
1030-
FunctionKind::Freestanding | FunctionKind::Method(_) | FunctionKind::Static(_) => {}
1032+
FunctionKind::Freestanding
1033+
| FunctionKind::Method(_)
1034+
| FunctionKind::Static(_)
1035+
| FunctionKind::Constructor(_) => {}
10311036

10321037
FunctionKind::AsyncFreestanding
1033-
| FunctionKind::Constructor(_)
10341038
| FunctionKind::AsyncMethod(_)
10351039
| FunctionKind::AsyncStatic(_) => {
10361040
todo!()
@@ -1041,19 +1045,27 @@ impl<'a> DInterfaceGenerator<'a> {
10411045

10421046
let split_name = match &func.kind {
10431047
FunctionKind::Freestanding | FunctionKind::AsyncFreestanding => &func.name,
1048+
FunctionKind::Constructor(_) => "",
10441049
FunctionKind::Method(_)
10451050
| FunctionKind::Static(_)
1046-
| FunctionKind::Constructor(_)
10471051
| FunctionKind::AsyncMethod(_)
10481052
| FunctionKind::AsyncStatic(_) => func.name.split(".").skip(1).next().unwrap(),
10491053
};
10501054

10511055
let lower_name = split_name.to_lower_camel_case();
1052-
let escaped_name = escape_d_identifier(&lower_name);
1056+
let escaped_name = if let FunctionKind::Constructor(_) = &func.kind {
1057+
match self.direction {
1058+
Some(Direction::Import) => "makeNew",
1059+
_ => "constructor",
1060+
}
1061+
} else {
1062+
escape_d_identifier(&lower_name)
1063+
};
10531064

10541065
res.name = escaped_name.into();
10551066
res.static_member = match &func.kind {
10561067
FunctionKind::Static(_) => true,
1068+
FunctionKind::Constructor(_) => true,
10571069
_ => false,
10581070
};
10591071

@@ -1094,16 +1106,10 @@ impl<'a> DInterfaceGenerator<'a> {
10941106

10951107
fn import_func(&mut self, func: &Function) {
10961108
match &func.kind {
1097-
FunctionKind::Freestanding => {}
1098-
FunctionKind::Constructor(_) => {
1099-
self.src.push_str(&format!(
1100-
"// TODO: Import FunctionKind::Constructor - {}\n",
1101-
func.name
1102-
));
1103-
return;
1104-
}
1105-
FunctionKind::Method(_) => {}
1106-
FunctionKind::Static(_) => {}
1109+
FunctionKind::Freestanding
1110+
| FunctionKind::Constructor(_)
1111+
| FunctionKind::Method(_)
1112+
| FunctionKind::Static(_) => {}
11071113
kind => {
11081114
todo!("Import {kind:?} - {}\n", func.name);
11091115
}
@@ -1205,16 +1211,10 @@ impl<'a> DInterfaceGenerator<'a> {
12051211

12061212
fn export_func(&mut self, func: &Function) {
12071213
match &func.kind {
1208-
FunctionKind::Freestanding => {}
1209-
FunctionKind::Constructor(_) => {
1210-
self.src.push_str(&format!(
1211-
"// TODO: Export FunctionKind::Constructor - {}\n",
1212-
func.name
1213-
));
1214-
return;
1215-
}
1216-
FunctionKind::Method(_) => {}
1217-
FunctionKind::Static(_) => {}
1214+
FunctionKind::Freestanding
1215+
| FunctionKind::Constructor(_)
1216+
| FunctionKind::Method(_)
1217+
| FunctionKind::Static(_) => {}
12181218
kind => {
12191219
todo!("Export {kind:?} - {}\n", func.name);
12201220
}
@@ -1699,7 +1699,6 @@ impl<'a> InterfaceGenerator<'a> for DInterfaceGenerator<'a> {
16991699
}
17001700
},
17011701
}
1702-
//todo!("def of `resource`")
17031702
}
17041703

17051704
fn type_tuple(&mut self, id: TypeId, name: &str, tuple: &Tuple, docs: &Docs) {
@@ -1994,6 +1993,10 @@ impl<'a> InterfaceGenerator<'a> for DInterfaceGenerator<'a> {
19941993
));
19951994
}
19961995

1996+
fn type_map(&mut self, _id: TypeId, name: &str, _key: &Type, _value: &Type, _docs: &Docs) {
1997+
todo!("def of `map` - {name}");
1998+
}
1999+
19972000
fn type_future(&mut self, _id: TypeId, name: &str, _ty: &Option<Type>, _docs: &Docs) {
19982001
todo!("def of `future` - {name}");
19992002
}
@@ -2839,7 +2842,11 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
28392842
};
28402843

28412844
let lower_name = split_name.to_lower_camel_case();
2842-
let escaped_name = escape_d_identifier(&lower_name);
2845+
let escaped_name = if name.starts_with("[constructor]") {
2846+
"makeNew"
2847+
} else {
2848+
escape_d_identifier(&lower_name)
2849+
};
28432850

28442851
if !sig.results.is_empty() {
28452852
self.src.push_str("auto _ret = ");
@@ -2866,15 +2873,19 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
28662873

28672874
let split_name = match &func.kind {
28682875
FunctionKind::Freestanding | FunctionKind::AsyncFreestanding => &func.name,
2876+
FunctionKind::Constructor(_) => "",
28692877
FunctionKind::Method(_)
28702878
| FunctionKind::Static(_)
2871-
| FunctionKind::Constructor(_)
28722879
| FunctionKind::AsyncMethod(_)
28732880
| FunctionKind::AsyncStatic(_) => func.name.split(".").skip(1).next().unwrap(),
28742881
};
28752882

28762883
let lower_name = split_name.to_lower_camel_case();
2877-
let escaped_name = escape_d_identifier(&lower_name);
2884+
let escaped_name = if let FunctionKind::Constructor(_) = &func.kind {
2885+
"constructor"
2886+
} else {
2887+
escape_d_identifier(&lower_name)
2888+
};
28782889

28792890
let implicit_self = match &func.kind {
28802891
FunctionKind::Freestanding

crates/test/src/d.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl LanguageMethods for D {
3333
config: &crate::config::WitConfig,
3434
_args: &[String],
3535
) -> bool {
36-
config.async_ || config.error_context
36+
config.async_ || config.error_context || name == "map.wit"
3737
}
3838

3939
fn default_bindgen_args_for_codegen(&self) -> &[&str] {

0 commit comments

Comments
 (0)