Skip to content

Commit 25596c7

Browse files
authored
Remove support for handles and resources (#346)
* Remove support for handles and resources This commit removes all support for the `resource` and `Handle` types from the AST of `wit-parser` and all related support in all code generators. The motivation for this commit is that `wit-bindgen` is on the cusp of actually being able to work with components: producing a component from guest output and consuming components in host generators. More detail about this is in #314. With components as an intermediate format, however, there is no way to encode resources since they are not part of the component model proposal yet. All is not lost for handles and resources, though. The official design for handles and resources is being worked on upstream in the component model repository itself at this time and once added all of this support will be re-added to `wit-bindgen`. In the meantime though I personally think that the best way forward is to remove the interim support for a few reasons: * Primarily it unblocks progress at this time towards fully integrating components and the `wit-bindgen` generators. The requirement to run existing tests that use handles would mean that no host generator could actually switch to components and/or modes for today's core-wasm-lookalike would need to be preserved. * Otherwise though the semantics of the current handles are basically invented out of thin air by myself and were never really formally specified, debated, or designed deliberately. I grafted `witx`-style handles into `wit-component` and added features as necessary over time, but it seems highly unlikely that the handles designed as part of the component model will be the ones that `wit-bindgen` currently supports. This inevitably means that a new system would need new code anyway and would likely result in removal regardless. As usual git always has the history of handles and this all may come back in one shape or another if only slightly tweaked. I'm confident in our history spelunking abilities, though, so I don't feel that keeping support in the repository is necessary for this purpose. * Remove resources from the demo * Fix rebase conflict
1 parent a4e4f7a commit 25596c7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+200
-3757
lines changed

crates/bindgen-core/src/lib.rs

-10
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ pub trait Generator {
8484
);
8585
fn type_union(&mut self, iface: &Interface, id: TypeId, name: &str, union: &Union, docs: &Docs);
8686
fn type_enum(&mut self, iface: &Interface, id: TypeId, name: &str, enum_: &Enum, docs: &Docs);
87-
fn type_resource(&mut self, iface: &Interface, ty: ResourceId);
8887
fn type_alias(&mut self, iface: &Interface, id: TypeId, name: &str, ty: &Type, docs: &Docs);
8988
fn type_list(&mut self, iface: &Interface, id: TypeId, name: &str, ty: &Type, docs: &Docs);
9089
fn type_builtin(&mut self, iface: &Interface, id: TypeId, name: &str, ty: &Type, docs: &Docs);
@@ -131,10 +130,6 @@ pub trait Generator {
131130
}
132131
}
133132

134-
for (id, _resource) in iface.resources.iter() {
135-
self.type_resource(iface, id);
136-
}
137-
138133
self.preprocess_functions(iface, dir);
139134

140135
for f in iface.functions.iter() {
@@ -181,17 +176,13 @@ pub struct TypeInfo {
181176

182177
/// Whether or not this type (transitively) has a list.
183178
pub has_list: bool,
184-
185-
/// Whether or not this type (transitively) has a handle.
186-
pub has_handle: bool,
187179
}
188180

189181
impl std::ops::BitOrAssign for TypeInfo {
190182
fn bitor_assign(&mut self, rhs: Self) {
191183
self.param |= rhs.param;
192184
self.result |= rhs.result;
193185
self.has_list |= rhs.has_list;
194-
self.has_handle |= rhs.has_handle;
195186
}
196187
}
197188

@@ -271,7 +262,6 @@ impl Types {
271262
pub fn type_info(&mut self, iface: &Interface, ty: &Type) -> TypeInfo {
272263
let mut info = TypeInfo::default();
273264
match ty {
274-
Type::Handle(_) => info.has_handle = true,
275265
Type::String => info.has_list = true,
276266
Type::Id(id) => return self.type_id_info(iface, *id),
277267
_ => {}

crates/gen-guest-c/src/lib.rs

-109
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,6 @@ impl C {
238238
Type::S64 => self.src.h("int64_t"),
239239
Type::Float32 => self.src.h("float"),
240240
Type::Float64 => self.src.h("double"),
241-
Type::Handle(id) => {
242-
self.print_namespace(iface);
243-
self.src.h(&iface.resources[*id].name.to_snake_case());
244-
self.src.h("_t");
245-
}
246241
Type::String => {
247242
self.print_namespace(iface);
248243
self.src.h("string_t");
@@ -285,7 +280,6 @@ impl C {
285280
Type::S64 => self.src.h("s64"),
286281
Type::Float32 => self.src.h("float32"),
287282
Type::Float64 => self.src.h("float64"),
288-
Type::Handle(id) => self.src.h(&iface.resources[*id].name.to_snake_case()),
289283
Type::String => self.src.h("string"),
290284
Type::Id(id) => {
291285
let ty = &iface.types[*id];
@@ -579,7 +573,6 @@ impl C {
579573
let id = match ty {
580574
Type::Id(id) => *id,
581575
Type::String => return true,
582-
Type::Handle(_) => return true,
583576
_ => return false,
584577
};
585578
match &iface.types[id].kind {
@@ -945,10 +938,6 @@ impl Generator for C {
945938
self.types.insert(id, mem::replace(&mut self.src.h, prev));
946939
}
947940

948-
fn type_resource(&mut self, iface: &Interface, ty: ResourceId) {
949-
drop((iface, ty));
950-
}
951-
952941
fn type_alias(&mut self, iface: &Interface, id: TypeId, name: &str, ty: &Type, docs: &Docs) {
953942
let prev = mem::take(&mut self.src.h);
954943
self.docs(docs);
@@ -1178,90 +1167,6 @@ impl Generator for C {
11781167

11791168
self.print_intrinsics();
11801169

1181-
for (_, resource) in iface.resources.iter() {
1182-
let ns = iface.name.to_snake_case();
1183-
let name = resource.name.to_snake_case();
1184-
uwrite!(
1185-
self.src.h,
1186-
"
1187-
typedef struct {{
1188-
uint32_t idx;
1189-
}} {ns}_{name}_t;
1190-
void {ns}_{name}_free({ns}_{name}_t *ptr);
1191-
{ns}_{name}_t {ns}_{name}_clone({ns}_{name}_t *ptr);
1192-
",
1193-
ns = ns,
1194-
name = name,
1195-
);
1196-
uwrite!(
1197-
self.src.c,
1198-
"
1199-
__attribute__((import_module(\"canonical_abi\"), import_name(\"resource_drop_{name_orig}\")))
1200-
void __resource_{name}_drop(uint32_t idx);
1201-
1202-
void {ns}_{name}_free({ns}_{name}_t *ptr) {{
1203-
__resource_{name}_drop(ptr->idx);
1204-
}}
1205-
1206-
__attribute__((import_module(\"canonical_abi\"), import_name(\"resource_clone_{name_orig}\")))
1207-
uint32_t __resource_{name}_clone(uint32_t idx);
1208-
1209-
{ns}_{name}_t {ns}_{name}_clone({ns}_{name}_t *ptr) {{
1210-
return ({ns}_{name}_t){{__resource_{name}_clone(ptr->idx)}};
1211-
}}
1212-
",
1213-
ns = ns,
1214-
name = name,
1215-
name_orig = resource.name,
1216-
);
1217-
1218-
// Exported resources have more capabilities, they can create new
1219-
// resources and get the private value that it was created with.
1220-
// Furthermore we also define the destructor which delegates to the
1221-
// actual user-defined destructor, if any.
1222-
if !self.in_import {
1223-
uwrite!(
1224-
self.src.h,
1225-
"\
1226-
{ns}_{name}_t {ns}_{name}_new(void *data);
1227-
void* {ns}_{name}_get({ns}_{name}_t *ptr);
1228-
1229-
__attribute__((weak))
1230-
void {ns}_{name}_dtor(void *data);
1231-
",
1232-
ns = ns,
1233-
name = name,
1234-
);
1235-
uwrite!(
1236-
self.src.c,
1237-
"
1238-
__attribute__((import_module(\"canonical_abi\"), import_name(\"resource_new_{name_orig}\")))
1239-
uint32_t __resource_{name}_new(uint32_t val);
1240-
1241-
{ns}_{name}_t {ns}_{name}_new(void *data) {{
1242-
return ({ns}_{name}_t){{__resource_{name}_new((uint32_t) data)}};
1243-
}}
1244-
1245-
__attribute__((import_module(\"canonical_abi\"), import_name(\"resource_get_{name_orig}\")))
1246-
uint32_t __resource_{name}_get(uint32_t idx);
1247-
1248-
void* {ns}_{name}_get({ns}_{name}_t *ptr) {{
1249-
return (void*) __resource_{name}_get(ptr->idx);
1250-
}}
1251-
1252-
__attribute__((export_name(\"canonical_abi_drop_{name_orig}\")))
1253-
void __resource_{name}_dtor(uint32_t val) {{
1254-
if ({ns}_{name}_dtor)
1255-
{ns}_{name}_dtor((void*) val);
1256-
}}
1257-
",
1258-
ns = ns,
1259-
name = name,
1260-
name_orig = resource.name,
1261-
);
1262-
}
1263-
}
1264-
12651170
// Continuously generate anonymous types while we continue to find more
12661171
//
12671172
// First we take care of the public set of anonymous types. This will
@@ -1595,20 +1500,6 @@ impl Bindgen for FunctionBindgen<'_> {
15951500
results.push(operands[0].clone());
15961501
}
15971502

1598-
Instruction::I32FromOwnedHandle { .. } | Instruction::I32FromBorrowedHandle { .. } => {
1599-
results.push(format!("({}).idx", operands[0]));
1600-
}
1601-
1602-
Instruction::HandleBorrowedFromI32 { ty, .. }
1603-
| Instruction::HandleOwnedFromI32 { ty, .. } => {
1604-
results.push(format!(
1605-
"({}_{}_t){{ {} }}",
1606-
iface.name.to_snake_case(),
1607-
iface.resources[*ty].name.to_snake_case(),
1608-
operands[0],
1609-
));
1610-
}
1611-
16121503
Instruction::RecordLower { record, .. } => {
16131504
let op = &operands[0];
16141505
for f in record.fields.iter() {

0 commit comments

Comments
 (0)