MCPcopy Create free account
hub / github.com/bytecodealliance/wit-bindgen / start_resource

Method start_resource

crates/csharp/src/interface.rs:1366–1523  ·  view source on GitHub ↗
(&mut self, id: TypeId, key: Option<&WorldKey>)

Source from the content-addressed store, hash-verified

1364 }
1365
1366 pub(crate) fn start_resource(&mut self, id: TypeId, key: Option<&WorldKey>) {
1367 let access = self.csharp_gen.access_modifier();
1368 let qualified = self.type_name_with_qualifier(&Type::Id(id), true);
1369 let info = &self.csharp_gen.all_resources[&id];
1370 let name = info.name.clone();
1371 let upper_camel = name.to_upper_camel_case();
1372 let docs = info.docs.clone();
1373 self.print_docs(&docs);
1374
1375 match self.direction {
1376 Direction::Import => {
1377 let module_name = key
1378 .map(|key| self.resolve.name_world_key(key))
1379 .unwrap_or_else(|| "$root".into());
1380
1381 // As of this writing, we cannot safely drop a handle to an imported resource from a .NET finalizer
1382 // because it may still have one or more open child resources. Once WIT has explicit syntax for
1383 // indicating parent/child relationships, we should be able to use that information to keep track
1384 // of child resources automatically in generated code, at which point we'll be able to drop them in
1385 // the correct order from finalizers.
1386 uwriteln!(
1387 self.src,
1388 r#"
1389 {access} class {upper_camel}: global::System.IDisposable {{
1390 internal int Handle {{ get; set; }}
1391
1392 {access} readonly record struct THandle(int Handle);
1393
1394 {access} {upper_camel}(THandle handle) {{
1395 Handle = handle.Handle;
1396 }}
1397
1398 public void Dispose() {{
1399 Dispose(true);
1400 }}
1401
1402 [global::System.Runtime.InteropServices.DllImportAttribute("{module_name}", EntryPoint = "[resource-drop]{name}"), global::System.Runtime.InteropServices.WasmImportLinkageAttribute]
1403 private static extern void wasmImportResourceDrop(int p0);
1404
1405 protected virtual void Dispose(bool disposing) {{
1406 if (disposing && Handle != 0) {{
1407 wasmImportResourceDrop(Handle);
1408 Handle = 0;
1409 }}
1410 }}
1411 "#
1412 );
1413 }
1414 Direction::Export => {
1415 let prefix = key
1416 .map(|s| format!("{}#", self.resolve.name_world_key(s)))
1417 .unwrap_or_default();
1418
1419 uwrite!(
1420 self.csharp_interop_src,
1421 r#"
1422 [global::System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(EntryPoint = "{prefix}[dtor]{name}")]
1423 {access} static unsafe void wasmExportResourceDtor{upper_camel}(int rep) {{

Callers 4

import_interfaceMethod · 0.80
import_funcsMethod · 0.80
export_interfaceMethod · 0.80
export_funcsMethod · 0.80

Calls 6

access_modifierMethod · 0.80
print_docsMethod · 0.80
mapMethod · 0.80
joinMethod · 0.80
iterMethod · 0.45

Tested by

no test coverage detected